hcm's fixes
[kernel-power] / usbhost / drivers / usb2 / gadget / nokia.c
1 /*
2  * nokia.c -- Nokia Composite Gadget Driver
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  * Contact: Felipe Balbi <felipe.balbi@nokia.com>
6  *
7  * This gadget driver borrows from serial.c which is:
8  *
9  * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
10  * Copyright (C) 2008 by David Brownell
11  * Copyright (C) 2008 by Nokia Corporation
12  *
13  * This software is distributed under the terms of the GNU General
14  * Public License ("GPL") as published by the Free Software Foundation,
15  * version 2 of that License.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/utsname.h>
20 #include <linux/device.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23
24 #include "u_serial.h"
25 #include "u_ether.h"
26 #include "u_phonet.h"
27 #include "gadget_chips.h"
28
29 #include <asm/mach-types.h>
30
31 /* Defines */
32
33 #define NOKIA_VERSION_NUM               0x0211
34 #define NOKIA_LONG_NAME                 "N900 (PC-Suite Mode)"
35
36 /*-------------------------------------------------------------------------*/
37
38 /*
39  * Kbuild is not very cooperative with respect to linking separately
40  * compiled library objects into one module.  So for now we won't use
41  * separate compilation ... ensuring init/exit sections work to shrink
42  * the runtime footprint, and giving us at least some parts of what
43  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44  */
45 #include "composite.c"
46 #include "usbstring.c"
47 #include "config.c"
48 #include "epautoconf.c"
49
50 #include "f_acm.c"
51 #include "f_ecm.c"
52 #include "f_obex.c"
53 #include "f_serial.c"
54 #include "f_phonet.c"
55 #include "u_serial.c"
56 #include "u_ether.c"
57
58 /*-------------------------------------------------------------------------*/
59
60 #define NOKIA_VENDOR_ID                 0x0421  /* Nokia */
61 #define NOKIA_PRODUCT_ID                0x01c8  /* Nokia Gadget */
62
63 /* string IDs are assigned dynamically */
64
65 #define STRING_MANUFACTURER_IDX         0
66 #define STRING_PRODUCT_IDX              1
67 #define STRING_DESCRIPTION_IDX          2
68
69 static char manufacturer_nokia[] = "Nokia";
70 static const char product_nokia[] = NOKIA_LONG_NAME;
71 static const char description_nokia[] = "PC-Suite Configuration";
72
73 static struct usb_string strings_dev[] = {
74         [STRING_MANUFACTURER_IDX].s = manufacturer_nokia,
75         [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME,
76         [STRING_DESCRIPTION_IDX].s = description_nokia,
77         {  } /* end of list */
78 };
79
80 static struct usb_gadget_strings stringtab_dev = {
81         .language       = 0x0409,       /* en-us */
82         .strings        = strings_dev,
83 };
84
85 static struct usb_gadget_strings *dev_strings[] = {
86         &stringtab_dev,
87         NULL,
88 };
89
90 static struct usb_device_descriptor device_desc = {
91         .bLength                = USB_DT_DEVICE_SIZE,
92         .bDescriptorType        = USB_DT_DEVICE,
93         .bcdUSB                 = __constant_cpu_to_le16(0x0200),
94         .bDeviceClass           = USB_CLASS_COMM,
95         .idVendor               = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
96         .idProduct              = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
97         /* .iManufacturer = DYNAMIC */
98         /* .iProduct = DYNAMIC */
99         .bNumConfigurations =   1,
100 };
101
102 /*-------------------------------------------------------------------------*/
103
104 /* Module */
105 MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
106 MODULE_AUTHOR("Felipe Balbi");
107 MODULE_LICENSE("GPL");
108
109 /*-------------------------------------------------------------------------*/
110
111 static u8 hostaddr[ETH_ALEN];
112
113 static int __init nokia_bind_config(struct usb_configuration *c)
114 {
115         int status = 0;
116
117         status = phonet_bind_config(c);
118         if (status)
119                 printk(KERN_DEBUG "could not bind phonet config\n");
120
121         status = obex_bind_config(c, 0);
122         if (status)
123                 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
124
125         status = obex_bind_config(c, 1);
126         if (status)
127                 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
128
129         status = acm_bind_config(c, 2);
130         if (status)
131                 printk(KERN_DEBUG "could not bind acm config\n");
132
133         status = ecm_bind_config(c, hostaddr);
134         if (status)
135                 printk(KERN_DEBUG "could not bind ecm config\n");
136
137         return status;
138 }
139
140 static struct usb_configuration nokia_config_500ma_driver = {
141         .label          = "nokia1",
142         .bind           = nokia_bind_config,
143         .bConfigurationValue = 1,
144         /* .iConfiguration = DYNAMIC */
145         .bmAttributes   = USB_CONFIG_ATT_ONE,
146         .bMaxPower      = 250, /* 500mA */
147 };
148
149 static struct usb_configuration nokia_config_100ma_driver = {
150         .label          = "nokia2",
151         .bind           = nokia_bind_config,
152         .bConfigurationValue = 2,
153         /* .iConfiguration = DYNAMIC */
154         .bmAttributes   = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
155         .bMaxPower      = 50, /* 100 mA */
156 };
157
158 static int __init nokia_bind(struct usb_composite_dev *cdev)
159 {
160         int                     gcnum;
161         struct usb_gadget       *gadget = cdev->gadget;
162         int                     status;
163
164         status = gphonet_setup(cdev->gadget);
165         if (status < 0)
166                 goto err_phonet;
167
168         status = gserial_setup(cdev->gadget, 3);
169         if (status < 0)
170                 goto err_serial;
171
172         status = gether_setup(cdev->gadget, hostaddr);
173         if (status < 0)
174                 goto err_ether;
175
176         status = usb_string_id(cdev);
177         if (status < 0)
178                 goto err_usb;
179         strings_dev[STRING_MANUFACTURER_IDX].id = status;
180
181         device_desc.iManufacturer = status;
182
183         status = usb_string_id(cdev);
184         if (status < 0)
185                 goto err_usb;
186         strings_dev[STRING_PRODUCT_IDX].id = status;
187
188         device_desc.iProduct = status;
189
190         /* config description */
191         status = usb_string_id(cdev);
192         if (status < 0)
193                 goto err_usb;
194         strings_dev[STRING_DESCRIPTION_IDX].id = status;
195
196         nokia_config_500ma_driver.iConfiguration = status;
197         nokia_config_100ma_driver.iConfiguration = status;
198
199         /* set up other descriptors */
200         gcnum = usb_gadget_controller_number(gadget);
201         if (gcnum >= 0)
202                 device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
203         else {
204                 /* this should only work with hw that supports altsettings
205                  * and several endpoints, anything else, panic.
206                  */
207                 pr_err("nokia_bind: controller '%s' not recognized\n",
208                         gadget->name);
209                 goto err_usb;
210         }
211
212         /* finaly register the configuration */
213         status = usb_add_config(cdev, &nokia_config_500ma_driver);
214         if (status < 0)
215                 goto err_usb;
216
217         status = usb_add_config(cdev, &nokia_config_100ma_driver);
218         if (status < 0)
219                 goto err_usb;
220
221         INFO(cdev, "%s\n", NOKIA_LONG_NAME);
222
223         return 0;
224
225 err_usb:
226         gether_cleanup();
227 err_ether:
228         gserial_cleanup();
229 err_serial:
230         gphonet_cleanup();
231 err_phonet:
232         return status;
233 }
234
235 static int __exit nokia_unbind(struct usb_composite_dev *cdev)
236 {
237         gphonet_cleanup();
238         gserial_cleanup();
239         gether_cleanup();
240
241         return 0;
242 }
243
244 static struct usb_composite_driver nokia_driver = {
245         .name           = "g_nokia",
246         .dev            = &device_desc,
247         .strings        = dev_strings,
248         .bind           = nokia_bind,
249         .unbind         = __exit_p(nokia_unbind),
250 };
251
252 static int __init nokia_init(void)
253 {
254         return usb_composite_register(&nokia_driver);
255 }
256 module_init(nokia_init);
257
258 static void __exit nokia_cleanup(void)
259 {
260         usb_composite_unregister(&nokia_driver);
261 }
262 module_exit(nokia_cleanup);
263