1 // SPDX-License-Identifier: GPL-2.0
3 * nokia.c -- Nokia Composite Gadget Driver
5 * Copyright (C) 2008-2010 Nokia Corporation
6 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
8 * This gadget driver borrows from serial.c which is:
10 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
11 * Copyright (C) 2008 by David Brownell
12 * Copyright (C) 2008 by Nokia Corporation
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
23 #include "f_mass_storage.h"
27 #define NOKIA_VERSION_NUM 0x0211
28 #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
30 USB_GADGET_COMPOSITE_OPTIONS();
32 USB_ETHERNET_MODULE_PARAMETERS();
34 static struct fsg_module_parameters fsg_mod_data
= {
38 .removable
= { 1, 1, },
41 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
43 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
48 * Number of buffers we will use.
49 * 2 is usually enough for good buffering pipeline
51 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
53 #endif /* CONFIG_USB_DEBUG */
55 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data
);
57 #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
58 #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
60 /* string IDs are assigned dynamically */
62 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
64 static char manufacturer_nokia
[] = "Nokia";
65 static const char description_nokia
[] = "PC-Suite Configuration";
67 static struct usb_string strings_dev
[] = {
68 [USB_GADGET_MANUFACTURER_IDX
].s
= manufacturer_nokia
,
69 [USB_GADGET_PRODUCT_IDX
].s
= NOKIA_LONG_NAME
,
70 [USB_GADGET_SERIAL_IDX
].s
= "",
71 [STRING_DESCRIPTION_IDX
].s
= description_nokia
,
75 static struct usb_gadget_strings stringtab_dev
= {
76 .language
= 0x0409, /* en-us */
77 .strings
= strings_dev
,
80 static struct usb_gadget_strings
*dev_strings
[] = {
85 static struct usb_device_descriptor device_desc
= {
86 .bLength
= USB_DT_DEVICE_SIZE
,
87 .bDescriptorType
= USB_DT_DEVICE
,
88 /* .bcdUSB = DYNAMIC */
89 .bDeviceClass
= USB_CLASS_COMM
,
90 .idVendor
= cpu_to_le16(NOKIA_VENDOR_ID
),
91 .idProduct
= cpu_to_le16(NOKIA_PRODUCT_ID
),
92 .bcdDevice
= cpu_to_le16(NOKIA_VERSION_NUM
),
93 /* .iManufacturer = DYNAMIC */
94 /* .iProduct = DYNAMIC */
95 .bNumConfigurations
= 1,
98 /*-------------------------------------------------------------------------*/
101 MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
102 MODULE_AUTHOR("Felipe Balbi");
103 MODULE_LICENSE("GPL");
105 /*-------------------------------------------------------------------------*/
106 static struct usb_function
*f_acm_cfg1
;
107 static struct usb_function
*f_acm_cfg2
;
108 static struct usb_function
*f_ecm_cfg1
;
109 static struct usb_function
*f_ecm_cfg2
;
110 static struct usb_function
*f_obex1_cfg1
;
111 static struct usb_function
*f_obex2_cfg1
;
112 static struct usb_function
*f_obex1_cfg2
;
113 static struct usb_function
*f_obex2_cfg2
;
114 static struct usb_function
*f_phonet_cfg1
;
115 static struct usb_function
*f_phonet_cfg2
;
116 static struct usb_function
*f_msg_cfg1
;
117 static struct usb_function
*f_msg_cfg2
;
120 static struct usb_configuration nokia_config_500ma_driver
= {
121 .label
= "Bus Powered",
122 .bConfigurationValue
= 1,
123 /* .iConfiguration = DYNAMIC */
124 .bmAttributes
= USB_CONFIG_ATT_ONE
,
128 static struct usb_configuration nokia_config_100ma_driver
= {
129 .label
= "Self Powered",
130 .bConfigurationValue
= 2,
131 /* .iConfiguration = DYNAMIC */
132 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
136 static struct usb_function_instance
*fi_acm
;
137 static struct usb_function_instance
*fi_ecm
;
138 static struct usb_function_instance
*fi_obex1
;
139 static struct usb_function_instance
*fi_obex2
;
140 static struct usb_function_instance
*fi_phonet
;
141 static struct usb_function_instance
*fi_msg
;
143 static int nokia_bind_config(struct usb_configuration
*c
)
145 struct usb_function
*f_acm
;
146 struct usb_function
*f_phonet
= NULL
;
147 struct usb_function
*f_obex1
= NULL
;
148 struct usb_function
*f_ecm
;
149 struct usb_function
*f_obex2
= NULL
;
150 struct usb_function
*f_msg
;
154 int phonet_stat
= -1;
156 if (!IS_ERR(fi_phonet
)) {
157 f_phonet
= usb_get_function(fi_phonet
);
158 if (IS_ERR(f_phonet
))
159 pr_debug("could not get phonet function\n");
162 if (!IS_ERR(fi_obex1
)) {
163 f_obex1
= usb_get_function(fi_obex1
);
165 pr_debug("could not get obex function 0\n");
168 if (!IS_ERR(fi_obex2
)) {
169 f_obex2
= usb_get_function(fi_obex2
);
171 pr_debug("could not get obex function 1\n");
174 f_acm
= usb_get_function(fi_acm
);
176 status
= PTR_ERR(f_acm
);
180 f_ecm
= usb_get_function(fi_ecm
);
182 status
= PTR_ERR(f_ecm
);
186 f_msg
= usb_get_function(fi_msg
);
188 status
= PTR_ERR(f_msg
);
192 if (!IS_ERR_OR_NULL(f_phonet
)) {
193 phonet_stat
= usb_add_function(c
, f_phonet
);
195 pr_debug("could not add phonet function\n");
198 if (!IS_ERR_OR_NULL(f_obex1
)) {
199 obex1_stat
= usb_add_function(c
, f_obex1
);
201 pr_debug("could not add obex function 0\n");
204 if (!IS_ERR_OR_NULL(f_obex2
)) {
205 obex2_stat
= usb_add_function(c
, f_obex2
);
207 pr_debug("could not add obex function 1\n");
210 status
= usb_add_function(c
, f_acm
);
214 status
= usb_add_function(c
, f_ecm
);
216 pr_debug("could not bind ecm config %d\n", status
);
220 status
= usb_add_function(c
, f_msg
);
224 if (c
== &nokia_config_500ma_driver
) {
227 f_phonet_cfg1
= f_phonet
;
228 f_obex1_cfg1
= f_obex1
;
229 f_obex2_cfg1
= f_obex2
;
234 f_phonet_cfg2
= f_phonet
;
235 f_obex1_cfg2
= f_obex1
;
236 f_obex2_cfg2
= f_obex2
;
242 usb_remove_function(c
, f_ecm
);
244 usb_remove_function(c
, f_acm
);
247 usb_remove_function(c
, f_obex2
);
249 usb_remove_function(c
, f_obex1
);
251 usb_remove_function(c
, f_phonet
);
252 usb_put_function(f_msg
);
254 usb_put_function(f_ecm
);
256 usb_put_function(f_acm
);
258 if (!IS_ERR_OR_NULL(f_obex2
))
259 usb_put_function(f_obex2
);
260 if (!IS_ERR_OR_NULL(f_obex1
))
261 usb_put_function(f_obex1
);
262 if (!IS_ERR_OR_NULL(f_phonet
))
263 usb_put_function(f_phonet
);
267 static int nokia_bind(struct usb_composite_dev
*cdev
)
269 struct usb_gadget
*gadget
= cdev
->gadget
;
270 struct fsg_opts
*fsg_opts
;
271 struct fsg_config fsg_config
;
274 status
= usb_string_ids_tab(cdev
, strings_dev
);
277 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
278 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
279 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
280 nokia_config_500ma_driver
.iConfiguration
= status
;
281 nokia_config_100ma_driver
.iConfiguration
= status
;
283 if (!gadget_is_altset_supported(gadget
)) {
288 fi_phonet
= usb_get_function_instance("phonet");
289 if (IS_ERR(fi_phonet
))
290 pr_debug("could not find phonet function\n");
292 fi_obex1
= usb_get_function_instance("obex");
293 if (IS_ERR(fi_obex1
))
294 pr_debug("could not find obex function 1\n");
296 fi_obex2
= usb_get_function_instance("obex");
297 if (IS_ERR(fi_obex2
))
298 pr_debug("could not find obex function 2\n");
300 fi_acm
= usb_get_function_instance("acm");
301 if (IS_ERR(fi_acm
)) {
302 status
= PTR_ERR(fi_acm
);
306 fi_ecm
= usb_get_function_instance("ecm");
307 if (IS_ERR(fi_ecm
)) {
308 status
= PTR_ERR(fi_ecm
);
312 fi_msg
= usb_get_function_instance("mass_storage");
313 if (IS_ERR(fi_msg
)) {
314 status
= PTR_ERR(fi_msg
);
318 /* set up mass storage function */
319 fsg_config_from_params(&fsg_config
, &fsg_mod_data
, fsg_num_buffers
);
320 fsg_config
.vendor_name
= "Nokia";
321 fsg_config
.product_name
= "N900";
323 fsg_opts
= fsg_opts_from_func_inst(fi_msg
);
324 fsg_opts
->no_configfs
= true;
326 status
= fsg_common_set_num_buffers(fsg_opts
->common
, fsg_num_buffers
);
330 status
= fsg_common_set_cdev(fsg_opts
->common
, cdev
, fsg_config
.can_stall
);
334 fsg_common_set_sysfs(fsg_opts
->common
, true);
336 status
= fsg_common_create_luns(fsg_opts
->common
, &fsg_config
);
340 fsg_common_set_inquiry_string(fsg_opts
->common
, fsg_config
.vendor_name
,
341 fsg_config
.product_name
);
343 /* finally register the configuration */
344 status
= usb_add_config(cdev
, &nokia_config_500ma_driver
,
349 status
= usb_add_config(cdev
, &nokia_config_100ma_driver
,
354 usb_composite_overwrite_options(cdev
, &coverwrite
);
355 dev_info(&gadget
->dev
, "%s\n", NOKIA_LONG_NAME
);
360 usb_put_function(f_acm_cfg1
);
361 if (!IS_ERR_OR_NULL(f_obex1_cfg1
))
362 usb_put_function(f_obex1_cfg1
);
363 if (!IS_ERR_OR_NULL(f_obex2_cfg1
))
364 usb_put_function(f_obex2_cfg1
);
365 if (!IS_ERR_OR_NULL(f_phonet_cfg1
))
366 usb_put_function(f_phonet_cfg1
);
367 usb_put_function(f_ecm_cfg1
);
369 fsg_common_remove_luns(fsg_opts
->common
);
371 fsg_common_free_buffers(fsg_opts
->common
);
373 usb_put_function_instance(fi_msg
);
375 usb_put_function_instance(fi_ecm
);
377 usb_put_function_instance(fi_acm
);
379 if (!IS_ERR(fi_obex2
))
380 usb_put_function_instance(fi_obex2
);
381 if (!IS_ERR(fi_obex1
))
382 usb_put_function_instance(fi_obex1
);
383 if (!IS_ERR(fi_phonet
))
384 usb_put_function_instance(fi_phonet
);
389 static int nokia_unbind(struct usb_composite_dev
*cdev
)
391 if (!IS_ERR_OR_NULL(f_obex1_cfg2
))
392 usb_put_function(f_obex1_cfg2
);
393 if (!IS_ERR_OR_NULL(f_obex2_cfg2
))
394 usb_put_function(f_obex2_cfg2
);
395 if (!IS_ERR_OR_NULL(f_obex1_cfg1
))
396 usb_put_function(f_obex1_cfg1
);
397 if (!IS_ERR_OR_NULL(f_obex2_cfg1
))
398 usb_put_function(f_obex2_cfg1
);
399 if (!IS_ERR_OR_NULL(f_phonet_cfg1
))
400 usb_put_function(f_phonet_cfg1
);
401 if (!IS_ERR_OR_NULL(f_phonet_cfg2
))
402 usb_put_function(f_phonet_cfg2
);
403 usb_put_function(f_acm_cfg1
);
404 usb_put_function(f_acm_cfg2
);
405 usb_put_function(f_ecm_cfg1
);
406 usb_put_function(f_ecm_cfg2
);
407 usb_put_function(f_msg_cfg1
);
408 usb_put_function(f_msg_cfg2
);
410 usb_put_function_instance(fi_msg
);
411 usb_put_function_instance(fi_ecm
);
412 if (!IS_ERR(fi_obex2
))
413 usb_put_function_instance(fi_obex2
);
414 if (!IS_ERR(fi_obex1
))
415 usb_put_function_instance(fi_obex1
);
416 if (!IS_ERR(fi_phonet
))
417 usb_put_function_instance(fi_phonet
);
418 usb_put_function_instance(fi_acm
);
423 static struct usb_composite_driver nokia_driver
= {
426 .strings
= dev_strings
,
427 .max_speed
= USB_SPEED_HIGH
,
429 .unbind
= nokia_unbind
,
432 module_usb_composite_driver(nokia_driver
);