2 * nokia.c -- Nokia Composite Gadget Driver
4 * Copyright (C) 2008-2010 Nokia Corporation
5 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
7 * This gadget driver borrows from serial.c which is:
9 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
10 * Copyright (C) 2008 by David Brownell
11 * Copyright (C) 2008 by Nokia Corporation
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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
26 #include "f_mass_storage.h"
30 #define NOKIA_VERSION_NUM 0x0211
31 #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
33 USB_GADGET_COMPOSITE_OPTIONS();
35 USB_ETHERNET_MODULE_PARAMETERS();
37 static struct fsg_module_parameters fsg_mod_data
= {
41 .removable
= { 1, 1, },
44 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
46 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
51 * Number of buffers we will use.
52 * 2 is usually enough for good buffering pipeline
54 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
56 #endif /* CONFIG_USB_DEBUG */
58 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data
);
60 #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
61 #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
63 /* string IDs are assigned dynamically */
65 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
67 static char manufacturer_nokia
[] = "Nokia";
68 static const char product_nokia
[] = NOKIA_LONG_NAME
;
69 static const char description_nokia
[] = "PC-Suite Configuration";
71 static struct usb_string strings_dev
[] = {
72 [USB_GADGET_MANUFACTURER_IDX
].s
= manufacturer_nokia
,
73 [USB_GADGET_PRODUCT_IDX
].s
= NOKIA_LONG_NAME
,
74 [USB_GADGET_SERIAL_IDX
].s
= "",
75 [STRING_DESCRIPTION_IDX
].s
= description_nokia
,
79 static struct usb_gadget_strings stringtab_dev
= {
80 .language
= 0x0409, /* en-us */
81 .strings
= strings_dev
,
84 static struct usb_gadget_strings
*dev_strings
[] = {
89 static struct usb_device_descriptor device_desc
= {
90 .bLength
= USB_DT_DEVICE_SIZE
,
91 .bDescriptorType
= USB_DT_DEVICE
,
92 .bcdUSB
= cpu_to_le16(0x0200),
93 .bDeviceClass
= USB_CLASS_COMM
,
94 .idVendor
= cpu_to_le16(NOKIA_VENDOR_ID
),
95 .idProduct
= cpu_to_le16(NOKIA_PRODUCT_ID
),
96 .bcdDevice
= cpu_to_le16(NOKIA_VERSION_NUM
),
97 /* .iManufacturer = DYNAMIC */
98 /* .iProduct = DYNAMIC */
99 .bNumConfigurations
= 1,
102 /*-------------------------------------------------------------------------*/
105 MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
106 MODULE_AUTHOR("Felipe Balbi");
107 MODULE_LICENSE("GPL");
109 /*-------------------------------------------------------------------------*/
110 static struct usb_function
*f_acm_cfg1
;
111 static struct usb_function
*f_acm_cfg2
;
112 static struct usb_function
*f_ecm_cfg1
;
113 static struct usb_function
*f_ecm_cfg2
;
114 static struct usb_function
*f_obex1_cfg1
;
115 static struct usb_function
*f_obex2_cfg1
;
116 static struct usb_function
*f_obex1_cfg2
;
117 static struct usb_function
*f_obex2_cfg2
;
118 static struct usb_function
*f_phonet_cfg1
;
119 static struct usb_function
*f_phonet_cfg2
;
120 static struct usb_function
*f_msg_cfg1
;
121 static struct usb_function
*f_msg_cfg2
;
124 static struct usb_configuration nokia_config_500ma_driver
= {
125 .label
= "Bus Powered",
126 .bConfigurationValue
= 1,
127 /* .iConfiguration = DYNAMIC */
128 .bmAttributes
= USB_CONFIG_ATT_ONE
,
132 static struct usb_configuration nokia_config_100ma_driver
= {
133 .label
= "Self Powered",
134 .bConfigurationValue
= 2,
135 /* .iConfiguration = DYNAMIC */
136 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
140 static struct usb_function_instance
*fi_acm
;
141 static struct usb_function_instance
*fi_ecm
;
142 static struct usb_function_instance
*fi_obex1
;
143 static struct usb_function_instance
*fi_obex2
;
144 static struct usb_function_instance
*fi_phonet
;
145 static struct usb_function_instance
*fi_msg
;
147 static int nokia_bind_config(struct usb_configuration
*c
)
149 struct usb_function
*f_acm
;
150 struct usb_function
*f_phonet
= NULL
;
151 struct usb_function
*f_obex1
= NULL
;
152 struct usb_function
*f_ecm
;
153 struct usb_function
*f_obex2
= NULL
;
154 struct usb_function
*f_msg
;
158 int phonet_stat
= -1;
160 if (!IS_ERR(fi_phonet
)) {
161 f_phonet
= usb_get_function(fi_phonet
);
162 if (IS_ERR(f_phonet
))
163 pr_debug("could not get phonet function\n");
166 if (!IS_ERR(fi_obex1
)) {
167 f_obex1
= usb_get_function(fi_obex1
);
169 pr_debug("could not get obex function 0\n");
172 if (!IS_ERR(fi_obex2
)) {
173 f_obex2
= usb_get_function(fi_obex2
);
175 pr_debug("could not get obex function 1\n");
178 f_acm
= usb_get_function(fi_acm
);
180 status
= PTR_ERR(f_acm
);
184 f_ecm
= usb_get_function(fi_ecm
);
186 status
= PTR_ERR(f_ecm
);
190 f_msg
= usb_get_function(fi_msg
);
192 status
= PTR_ERR(f_msg
);
196 if (!IS_ERR_OR_NULL(f_phonet
)) {
197 phonet_stat
= usb_add_function(c
, f_phonet
);
199 pr_debug("could not add phonet function\n");
202 if (!IS_ERR_OR_NULL(f_obex1
)) {
203 obex1_stat
= usb_add_function(c
, f_obex1
);
205 pr_debug("could not add obex function 0\n");
208 if (!IS_ERR_OR_NULL(f_obex2
)) {
209 obex2_stat
= usb_add_function(c
, f_obex2
);
211 pr_debug("could not add obex function 1\n");
214 status
= usb_add_function(c
, f_acm
);
218 status
= usb_add_function(c
, f_ecm
);
220 pr_debug("could not bind ecm config %d\n", status
);
224 status
= usb_add_function(c
, f_msg
);
228 if (c
== &nokia_config_500ma_driver
) {
231 f_phonet_cfg1
= f_phonet
;
232 f_obex1_cfg1
= f_obex1
;
233 f_obex2_cfg1
= f_obex2
;
238 f_phonet_cfg2
= f_phonet
;
239 f_obex1_cfg2
= f_obex1
;
240 f_obex2_cfg2
= f_obex2
;
246 usb_remove_function(c
, f_ecm
);
248 usb_remove_function(c
, f_acm
);
251 usb_remove_function(c
, f_obex2
);
253 usb_remove_function(c
, f_obex1
);
255 usb_remove_function(c
, f_phonet
);
256 usb_put_function(f_msg
);
258 usb_put_function(f_ecm
);
260 usb_put_function(f_acm
);
262 if (!IS_ERR_OR_NULL(f_obex2
))
263 usb_put_function(f_obex2
);
264 if (!IS_ERR_OR_NULL(f_obex1
))
265 usb_put_function(f_obex1
);
266 if (!IS_ERR_OR_NULL(f_phonet
))
267 usb_put_function(f_phonet
);
271 static int nokia_bind(struct usb_composite_dev
*cdev
)
273 struct usb_gadget
*gadget
= cdev
->gadget
;
274 struct fsg_opts
*fsg_opts
;
275 struct fsg_config fsg_config
;
278 status
= usb_string_ids_tab(cdev
, strings_dev
);
281 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
282 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
283 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
284 nokia_config_500ma_driver
.iConfiguration
= status
;
285 nokia_config_100ma_driver
.iConfiguration
= status
;
287 if (!gadget_is_altset_supported(gadget
)) {
292 fi_phonet
= usb_get_function_instance("phonet");
293 if (IS_ERR(fi_phonet
))
294 pr_debug("could not find phonet function\n");
296 fi_obex1
= usb_get_function_instance("obex");
297 if (IS_ERR(fi_obex1
))
298 pr_debug("could not find obex function 1\n");
300 fi_obex2
= usb_get_function_instance("obex");
301 if (IS_ERR(fi_obex2
))
302 pr_debug("could not find obex function 2\n");
304 fi_acm
= usb_get_function_instance("acm");
305 if (IS_ERR(fi_acm
)) {
306 status
= PTR_ERR(fi_acm
);
310 fi_ecm
= usb_get_function_instance("ecm");
311 if (IS_ERR(fi_ecm
)) {
312 status
= PTR_ERR(fi_ecm
);
316 fi_msg
= usb_get_function_instance("mass_storage");
317 if (IS_ERR(fi_msg
)) {
318 status
= PTR_ERR(fi_msg
);
322 /* set up mass storage function */
323 fsg_config_from_params(&fsg_config
, &fsg_mod_data
, fsg_num_buffers
);
324 fsg_config
.vendor_name
= "Nokia";
325 fsg_config
.product_name
= "N900";
327 fsg_opts
= fsg_opts_from_func_inst(fi_msg
);
328 fsg_opts
->no_configfs
= true;
330 status
= fsg_common_set_num_buffers(fsg_opts
->common
, fsg_num_buffers
);
334 status
= fsg_common_set_cdev(fsg_opts
->common
, cdev
, fsg_config
.can_stall
);
338 fsg_common_set_sysfs(fsg_opts
->common
, true);
340 status
= fsg_common_create_luns(fsg_opts
->common
, &fsg_config
);
344 fsg_common_set_inquiry_string(fsg_opts
->common
, fsg_config
.vendor_name
,
345 fsg_config
.product_name
);
347 /* finally register the configuration */
348 status
= usb_add_config(cdev
, &nokia_config_500ma_driver
,
353 status
= usb_add_config(cdev
, &nokia_config_100ma_driver
,
358 usb_composite_overwrite_options(cdev
, &coverwrite
);
359 dev_info(&gadget
->dev
, "%s\n", NOKIA_LONG_NAME
);
364 usb_put_function(f_acm_cfg1
);
365 if (!IS_ERR_OR_NULL(f_obex1_cfg1
))
366 usb_put_function(f_obex1_cfg1
);
367 if (!IS_ERR_OR_NULL(f_obex2_cfg1
))
368 usb_put_function(f_obex2_cfg1
);
369 if (!IS_ERR_OR_NULL(f_phonet_cfg1
))
370 usb_put_function(f_phonet_cfg1
);
371 usb_put_function(f_ecm_cfg1
);
373 fsg_common_remove_luns(fsg_opts
->common
);
375 fsg_common_free_buffers(fsg_opts
->common
);
377 usb_put_function_instance(fi_msg
);
379 usb_put_function_instance(fi_ecm
);
381 usb_put_function_instance(fi_acm
);
383 if (!IS_ERR(fi_obex2
))
384 usb_put_function_instance(fi_obex2
);
385 if (!IS_ERR(fi_obex1
))
386 usb_put_function_instance(fi_obex1
);
387 if (!IS_ERR(fi_phonet
))
388 usb_put_function_instance(fi_phonet
);
393 static int nokia_unbind(struct usb_composite_dev
*cdev
)
395 if (!IS_ERR_OR_NULL(f_obex1_cfg2
))
396 usb_put_function(f_obex1_cfg2
);
397 if (!IS_ERR_OR_NULL(f_obex2_cfg2
))
398 usb_put_function(f_obex2_cfg2
);
399 if (!IS_ERR_OR_NULL(f_obex1_cfg1
))
400 usb_put_function(f_obex1_cfg1
);
401 if (!IS_ERR_OR_NULL(f_obex2_cfg1
))
402 usb_put_function(f_obex2_cfg1
);
403 if (!IS_ERR_OR_NULL(f_phonet_cfg1
))
404 usb_put_function(f_phonet_cfg1
);
405 if (!IS_ERR_OR_NULL(f_phonet_cfg2
))
406 usb_put_function(f_phonet_cfg2
);
407 usb_put_function(f_acm_cfg1
);
408 usb_put_function(f_acm_cfg2
);
409 usb_put_function(f_ecm_cfg1
);
410 usb_put_function(f_ecm_cfg2
);
411 usb_put_function(f_msg_cfg1
);
412 usb_put_function(f_msg_cfg2
);
414 usb_put_function_instance(fi_msg
);
415 usb_put_function_instance(fi_ecm
);
416 if (!IS_ERR(fi_obex2
))
417 usb_put_function_instance(fi_obex2
);
418 if (!IS_ERR(fi_obex1
))
419 usb_put_function_instance(fi_obex1
);
420 if (!IS_ERR(fi_phonet
))
421 usb_put_function_instance(fi_phonet
);
422 usb_put_function_instance(fi_acm
);
427 static struct usb_composite_driver nokia_driver
= {
430 .strings
= dev_strings
,
431 .max_speed
= USB_SPEED_HIGH
,
433 .unbind
= nokia_unbind
,
436 module_usb_composite_driver(nokia_driver
);