usb: gadget: Convert use of __constant_cpu_to_le16 to cpu_to_le16
[linux-2.6-block.git] / drivers / usb / gadget / legacy / nokia.c
1 /*
2  * nokia.c -- Nokia Composite Gadget Driver
3  *
4  * Copyright (C) 2008-2010 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/module.h>
20 #include <linux/device.h>
21
22 #include "u_serial.h"
23 #include "u_ether.h"
24 #include "u_phonet.h"
25 #include "u_ecm.h"
26 #include "gadget_chips.h"
27 #include "f_mass_storage.h"
28
29 /* Defines */
30
31 #define NOKIA_VERSION_NUM               0x0211
32 #define NOKIA_LONG_NAME                 "N900 (PC-Suite Mode)"
33
34 USB_GADGET_COMPOSITE_OPTIONS();
35
36 USB_ETHERNET_MODULE_PARAMETERS();
37
38 static struct fsg_module_parameters fsg_mod_data = {
39         .stall = 0,
40         .luns = 2,
41         .removable_count = 2,
42         .removable = { 1, 1, },
43 };
44
45 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
46
47 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
48
49 #else
50
51 /*
52  * Number of buffers we will use.
53  * 2 is usually enough for good buffering pipeline
54  */
55 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
56
57 #endif /* CONFIG_USB_DEBUG */
58
59 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
60
61 #define NOKIA_VENDOR_ID                 0x0421  /* Nokia */
62 #define NOKIA_PRODUCT_ID                0x01c8  /* Nokia Gadget */
63
64 /* string IDs are assigned dynamically */
65
66 #define STRING_DESCRIPTION_IDX          USB_GADGET_FIRST_AVAIL_IDX
67
68 static char manufacturer_nokia[] = "Nokia";
69 static const char product_nokia[] = NOKIA_LONG_NAME;
70 static const char description_nokia[] = "PC-Suite Configuration";
71
72 static struct usb_string strings_dev[] = {
73         [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
74         [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
75         [USB_GADGET_SERIAL_IDX].s = "",
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                 = cpu_to_le16(0x0200),
94         .bDeviceClass           = USB_CLASS_COMM,
95         .idVendor               = cpu_to_le16(NOKIA_VENDOR_ID),
96         .idProduct              = cpu_to_le16(NOKIA_PRODUCT_ID),
97         .bcdDevice              = cpu_to_le16(NOKIA_VERSION_NUM),
98         /* .iManufacturer = DYNAMIC */
99         /* .iProduct = DYNAMIC */
100         .bNumConfigurations =   1,
101 };
102
103 /*-------------------------------------------------------------------------*/
104
105 /* Module */
106 MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
107 MODULE_AUTHOR("Felipe Balbi");
108 MODULE_LICENSE("GPL");
109
110 /*-------------------------------------------------------------------------*/
111 static struct usb_function *f_acm_cfg1;
112 static struct usb_function *f_acm_cfg2;
113 static struct usb_function *f_ecm_cfg1;
114 static struct usb_function *f_ecm_cfg2;
115 static struct usb_function *f_obex1_cfg1;
116 static struct usb_function *f_obex2_cfg1;
117 static struct usb_function *f_obex1_cfg2;
118 static struct usb_function *f_obex2_cfg2;
119 static struct usb_function *f_phonet_cfg1;
120 static struct usb_function *f_phonet_cfg2;
121 static struct usb_function *f_msg_cfg1;
122 static struct usb_function *f_msg_cfg2;
123
124
125 static struct usb_configuration nokia_config_500ma_driver = {
126         .label          = "Bus Powered",
127         .bConfigurationValue = 1,
128         /* .iConfiguration = DYNAMIC */
129         .bmAttributes   = USB_CONFIG_ATT_ONE,
130         .MaxPower       = 500,
131 };
132
133 static struct usb_configuration nokia_config_100ma_driver = {
134         .label          = "Self Powered",
135         .bConfigurationValue = 2,
136         /* .iConfiguration = DYNAMIC */
137         .bmAttributes   = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
138         .MaxPower       = 100,
139 };
140
141 static struct usb_function_instance *fi_acm;
142 static struct usb_function_instance *fi_ecm;
143 static struct usb_function_instance *fi_obex1;
144 static struct usb_function_instance *fi_obex2;
145 static struct usb_function_instance *fi_phonet;
146 static struct usb_function_instance *fi_msg;
147
148 static int nokia_bind_config(struct usb_configuration *c)
149 {
150         struct usb_function *f_acm;
151         struct usb_function *f_phonet = NULL;
152         struct usb_function *f_obex1 = NULL;
153         struct usb_function *f_ecm;
154         struct usb_function *f_obex2 = NULL;
155         struct usb_function *f_msg;
156         struct fsg_opts *fsg_opts;
157         int status = 0;
158         int obex1_stat = -1;
159         int obex2_stat = -1;
160         int phonet_stat = -1;
161
162         if (!IS_ERR(fi_phonet)) {
163                 f_phonet = usb_get_function(fi_phonet);
164                 if (IS_ERR(f_phonet))
165                         pr_debug("could not get phonet function\n");
166         }
167
168         if (!IS_ERR(fi_obex1)) {
169                 f_obex1 = usb_get_function(fi_obex1);
170                 if (IS_ERR(f_obex1))
171                         pr_debug("could not get obex function 0\n");
172         }
173
174         if (!IS_ERR(fi_obex2)) {
175                 f_obex2 = usb_get_function(fi_obex2);
176                 if (IS_ERR(f_obex2))
177                         pr_debug("could not get obex function 1\n");
178         }
179
180         f_acm = usb_get_function(fi_acm);
181         if (IS_ERR(f_acm)) {
182                 status = PTR_ERR(f_acm);
183                 goto err_get_acm;
184         }
185
186         f_ecm = usb_get_function(fi_ecm);
187         if (IS_ERR(f_ecm)) {
188                 status = PTR_ERR(f_ecm);
189                 goto err_get_ecm;
190         }
191
192         f_msg = usb_get_function(fi_msg);
193         if (IS_ERR(f_msg)) {
194                 status = PTR_ERR(f_msg);
195                 goto err_get_msg;
196         }
197
198         if (!IS_ERR_OR_NULL(f_phonet)) {
199                 phonet_stat = usb_add_function(c, f_phonet);
200                 if (phonet_stat)
201                         pr_debug("could not add phonet function\n");
202         }
203
204         if (!IS_ERR_OR_NULL(f_obex1)) {
205                 obex1_stat = usb_add_function(c, f_obex1);
206                 if (obex1_stat)
207                         pr_debug("could not add obex function 0\n");
208         }
209
210         if (!IS_ERR_OR_NULL(f_obex2)) {
211                 obex2_stat = usb_add_function(c, f_obex2);
212                 if (obex2_stat)
213                         pr_debug("could not add obex function 1\n");
214         }
215
216         status = usb_add_function(c, f_acm);
217         if (status)
218                 goto err_conf;
219
220         status = usb_add_function(c, f_ecm);
221         if (status) {
222                 pr_debug("could not bind ecm config %d\n", status);
223                 goto err_ecm;
224         }
225
226         fsg_opts = fsg_opts_from_func_inst(fi_msg);
227
228         status = fsg_common_run_thread(fsg_opts->common);
229         if (status)
230                 goto err_msg;
231
232         status = usb_add_function(c, f_msg);
233         if (status)
234                 goto err_msg;
235
236         if (c == &nokia_config_500ma_driver) {
237                 f_acm_cfg1 = f_acm;
238                 f_ecm_cfg1 = f_ecm;
239                 f_phonet_cfg1 = f_phonet;
240                 f_obex1_cfg1 = f_obex1;
241                 f_obex2_cfg1 = f_obex2;
242                 f_msg_cfg1 = f_msg;
243         } else {
244                 f_acm_cfg2 = f_acm;
245                 f_ecm_cfg2 = f_ecm;
246                 f_phonet_cfg2 = f_phonet;
247                 f_obex1_cfg2 = f_obex1;
248                 f_obex2_cfg2 = f_obex2;
249                 f_msg_cfg2 = f_msg;
250         }
251
252         return status;
253 err_msg:
254         usb_remove_function(c, f_ecm);
255 err_ecm:
256         usb_remove_function(c, f_acm);
257 err_conf:
258         if (!obex2_stat)
259                 usb_remove_function(c, f_obex2);
260         if (!obex1_stat)
261                 usb_remove_function(c, f_obex1);
262         if (!phonet_stat)
263                 usb_remove_function(c, f_phonet);
264         usb_put_function(f_msg);
265 err_get_msg:
266         usb_put_function(f_ecm);
267 err_get_ecm:
268         usb_put_function(f_acm);
269 err_get_acm:
270         if (!IS_ERR_OR_NULL(f_obex2))
271                 usb_put_function(f_obex2);
272         if (!IS_ERR_OR_NULL(f_obex1))
273                 usb_put_function(f_obex1);
274         if (!IS_ERR_OR_NULL(f_phonet))
275                 usb_put_function(f_phonet);
276         return status;
277 }
278
279 static int nokia_bind(struct usb_composite_dev *cdev)
280 {
281         struct usb_gadget       *gadget = cdev->gadget;
282         struct fsg_opts         *fsg_opts;
283         struct fsg_config       fsg_config;
284         int                     status;
285
286         status = usb_string_ids_tab(cdev, strings_dev);
287         if (status < 0)
288                 goto err_usb;
289         device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
290         device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
291         status = strings_dev[STRING_DESCRIPTION_IDX].id;
292         nokia_config_500ma_driver.iConfiguration = status;
293         nokia_config_100ma_driver.iConfiguration = status;
294
295         if (!gadget_supports_altsettings(gadget)) {
296                 status = -ENODEV;
297                 goto err_usb;
298         }
299
300         fi_phonet = usb_get_function_instance("phonet");
301         if (IS_ERR(fi_phonet))
302                 pr_debug("could not find phonet function\n");
303
304         fi_obex1 = usb_get_function_instance("obex");
305         if (IS_ERR(fi_obex1))
306                 pr_debug("could not find obex function 1\n");
307
308         fi_obex2 = usb_get_function_instance("obex");
309         if (IS_ERR(fi_obex2))
310                 pr_debug("could not find obex function 2\n");
311
312         fi_acm = usb_get_function_instance("acm");
313         if (IS_ERR(fi_acm)) {
314                 status = PTR_ERR(fi_acm);
315                 goto err_obex2_inst;
316         }
317
318         fi_ecm = usb_get_function_instance("ecm");
319         if (IS_ERR(fi_ecm)) {
320                 status = PTR_ERR(fi_ecm);
321                 goto err_acm_inst;
322         }
323
324         fi_msg = usb_get_function_instance("mass_storage");
325         if (IS_ERR(fi_msg)) {
326                 status = PTR_ERR(fi_msg);
327                 goto err_ecm_inst;
328         }
329
330         /* set up mass storage function */
331         fsg_config_from_params(&fsg_config, &fsg_mod_data, fsg_num_buffers);
332         fsg_config.vendor_name = "Nokia";
333         fsg_config.product_name = "N900";
334
335         fsg_opts = fsg_opts_from_func_inst(fi_msg);
336         fsg_opts->no_configfs = true;
337
338         status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
339         if (status)
340                 goto err_msg_inst;
341
342         status = fsg_common_set_nluns(fsg_opts->common, fsg_config.nluns);
343         if (status)
344                 goto err_msg_buf;
345
346         status = fsg_common_set_cdev(fsg_opts->common, cdev, fsg_config.can_stall);
347         if (status)
348                 goto err_msg_set_nluns;
349
350         fsg_common_set_sysfs(fsg_opts->common, true);
351
352         status = fsg_common_create_luns(fsg_opts->common, &fsg_config);
353         if (status)
354                 goto err_msg_set_nluns;
355
356         fsg_common_set_inquiry_string(fsg_opts->common, fsg_config.vendor_name,
357                                       fsg_config.product_name);
358
359         /* finally register the configuration */
360         status = usb_add_config(cdev, &nokia_config_500ma_driver,
361                         nokia_bind_config);
362         if (status < 0)
363                 goto err_msg_set_cdev;
364
365         status = usb_add_config(cdev, &nokia_config_100ma_driver,
366                         nokia_bind_config);
367         if (status < 0)
368                 goto err_put_cfg1;
369
370         usb_composite_overwrite_options(cdev, &coverwrite);
371         dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
372
373         return 0;
374
375 err_put_cfg1:
376         usb_put_function(f_acm_cfg1);
377         if (!IS_ERR_OR_NULL(f_obex1_cfg1))
378                 usb_put_function(f_obex1_cfg1);
379         if (!IS_ERR_OR_NULL(f_obex2_cfg1))
380                 usb_put_function(f_obex2_cfg1);
381         if (!IS_ERR_OR_NULL(f_phonet_cfg1))
382                 usb_put_function(f_phonet_cfg1);
383         usb_put_function(f_ecm_cfg1);
384 err_msg_set_cdev:
385         fsg_common_remove_luns(fsg_opts->common);
386 err_msg_set_nluns:
387         fsg_common_free_luns(fsg_opts->common);
388 err_msg_buf:
389         fsg_common_free_buffers(fsg_opts->common);
390 err_msg_inst:
391         usb_put_function_instance(fi_msg);
392 err_ecm_inst:
393         usb_put_function_instance(fi_ecm);
394 err_acm_inst:
395         usb_put_function_instance(fi_acm);
396 err_obex2_inst:
397         if (!IS_ERR(fi_obex2))
398                 usb_put_function_instance(fi_obex2);
399         if (!IS_ERR(fi_obex1))
400                 usb_put_function_instance(fi_obex1);
401         if (!IS_ERR(fi_phonet))
402                 usb_put_function_instance(fi_phonet);
403 err_usb:
404         return status;
405 }
406
407 static int nokia_unbind(struct usb_composite_dev *cdev)
408 {
409         if (!IS_ERR_OR_NULL(f_obex1_cfg2))
410                 usb_put_function(f_obex1_cfg2);
411         if (!IS_ERR_OR_NULL(f_obex2_cfg2))
412                 usb_put_function(f_obex2_cfg2);
413         if (!IS_ERR_OR_NULL(f_obex1_cfg1))
414                 usb_put_function(f_obex1_cfg1);
415         if (!IS_ERR_OR_NULL(f_obex2_cfg1))
416                 usb_put_function(f_obex2_cfg1);
417         if (!IS_ERR_OR_NULL(f_phonet_cfg1))
418                 usb_put_function(f_phonet_cfg1);
419         if (!IS_ERR_OR_NULL(f_phonet_cfg2))
420                 usb_put_function(f_phonet_cfg2);
421         usb_put_function(f_acm_cfg1);
422         usb_put_function(f_acm_cfg2);
423         usb_put_function(f_ecm_cfg1);
424         usb_put_function(f_ecm_cfg2);
425         usb_put_function(f_msg_cfg1);
426         usb_put_function(f_msg_cfg2);
427
428         usb_put_function_instance(fi_msg);
429         usb_put_function_instance(fi_ecm);
430         if (!IS_ERR(fi_obex2))
431                 usb_put_function_instance(fi_obex2);
432         if (!IS_ERR(fi_obex1))
433                 usb_put_function_instance(fi_obex1);
434         if (!IS_ERR(fi_phonet))
435                 usb_put_function_instance(fi_phonet);
436         usb_put_function_instance(fi_acm);
437
438         return 0;
439 }
440
441 static struct usb_composite_driver nokia_driver = {
442         .name           = "g_nokia",
443         .dev            = &device_desc,
444         .strings        = dev_strings,
445         .max_speed      = USB_SPEED_HIGH,
446         .bind           = nokia_bind,
447         .unbind         = nokia_unbind,
448 };
449
450 module_usb_composite_driver(nokia_driver);