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>
25 #include "gadget_chips.h"
29 #define NOKIA_VERSION_NUM 0x0211
30 #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
32 /*-------------------------------------------------------------------------*/
35 * Kbuild is not very cooperative with respect to linking separately
36 * compiled library objects into one module. So for now we won't use
37 * separate compilation ... ensuring init/exit sections work to shrink
38 * the runtime footprint, and giving us at least some parts of what
39 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
41 #define USBF_ECM_INCLUDED
45 /*-------------------------------------------------------------------------*/
46 USB_GADGET_COMPOSITE_OPTIONS();
48 USB_ETHERNET_MODULE_PARAMETERS();
50 #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
51 #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
53 /* string IDs are assigned dynamically */
55 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
57 static char manufacturer_nokia
[] = "Nokia";
58 static const char product_nokia
[] = NOKIA_LONG_NAME
;
59 static const char description_nokia
[] = "PC-Suite Configuration";
61 static struct usb_string strings_dev
[] = {
62 [USB_GADGET_MANUFACTURER_IDX
].s
= manufacturer_nokia
,
63 [USB_GADGET_PRODUCT_IDX
].s
= NOKIA_LONG_NAME
,
64 [USB_GADGET_SERIAL_IDX
].s
= "",
65 [STRING_DESCRIPTION_IDX
].s
= description_nokia
,
69 static struct usb_gadget_strings stringtab_dev
= {
70 .language
= 0x0409, /* en-us */
71 .strings
= strings_dev
,
74 static struct usb_gadget_strings
*dev_strings
[] = {
79 static struct usb_device_descriptor device_desc
= {
80 .bLength
= USB_DT_DEVICE_SIZE
,
81 .bDescriptorType
= USB_DT_DEVICE
,
82 .bcdUSB
= __constant_cpu_to_le16(0x0200),
83 .bDeviceClass
= USB_CLASS_COMM
,
84 .idVendor
= __constant_cpu_to_le16(NOKIA_VENDOR_ID
),
85 .idProduct
= __constant_cpu_to_le16(NOKIA_PRODUCT_ID
),
86 .bcdDevice
= cpu_to_le16(NOKIA_VERSION_NUM
),
87 /* .iManufacturer = DYNAMIC */
88 /* .iProduct = DYNAMIC */
89 .bNumConfigurations
= 1,
92 /*-------------------------------------------------------------------------*/
95 MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
96 MODULE_AUTHOR("Felipe Balbi");
97 MODULE_LICENSE("GPL");
99 /*-------------------------------------------------------------------------*/
100 static struct usb_function
*f_acm_cfg1
;
101 static struct usb_function
*f_acm_cfg2
;
102 static u8 host_mac
[ETH_ALEN
];
103 static struct usb_function
*f_obex1_cfg1
;
104 static struct usb_function
*f_obex2_cfg1
;
105 static struct usb_function
*f_obex1_cfg2
;
106 static struct usb_function
*f_obex2_cfg2
;
107 static struct usb_function
*f_phonet_cfg1
;
108 static struct usb_function
*f_phonet_cfg2
;
109 static struct eth_dev
*the_dev
;
112 static struct usb_configuration nokia_config_500ma_driver
= {
113 .label
= "Bus Powered",
114 .bConfigurationValue
= 1,
115 /* .iConfiguration = DYNAMIC */
116 .bmAttributes
= USB_CONFIG_ATT_ONE
,
120 static struct usb_configuration nokia_config_100ma_driver
= {
121 .label
= "Self Powered",
122 .bConfigurationValue
= 2,
123 /* .iConfiguration = DYNAMIC */
124 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
128 static struct usb_function_instance
*fi_acm
;
129 static struct usb_function_instance
*fi_obex1
;
130 static struct usb_function_instance
*fi_obex2
;
131 static struct usb_function_instance
*fi_phonet
;
133 static int __init
nokia_bind_config(struct usb_configuration
*c
)
135 struct usb_function
*f_acm
;
136 struct usb_function
*f_phonet
= NULL
;
137 struct usb_function
*f_obex1
= NULL
;
138 struct usb_function
*f_obex2
= NULL
;
144 if (!IS_ERR(fi_phonet
)) {
145 f_phonet
= usb_get_function(fi_phonet
);
146 if (IS_ERR(f_phonet
))
147 pr_debug("could not get phonet function\n");
150 if (!IS_ERR(fi_obex1
)) {
151 f_obex1
= usb_get_function(fi_obex1
);
153 pr_debug("could not get obex function 0\n");
156 if (!IS_ERR(fi_obex2
)) {
157 f_obex2
= usb_get_function(fi_obex2
);
159 pr_debug("could not get obex function 1\n");
162 f_acm
= usb_get_function(fi_acm
);
164 status
= PTR_ERR(f_acm
);
168 if (!IS_ERR_OR_NULL(f_phonet
)) {
169 phonet_stat
= usb_add_function(c
, f_phonet
);
171 pr_debug("could not add phonet function\n");
174 if (!IS_ERR_OR_NULL(f_obex1
)) {
175 obex1_stat
= usb_add_function(c
, f_obex1
);
177 pr_debug("could not add obex function 0\n");
180 if (!IS_ERR_OR_NULL(f_obex2
)) {
181 obex2_stat
= usb_add_function(c
, f_obex2
);
183 pr_debug("could not add obex function 1\n");
186 status
= usb_add_function(c
, f_acm
);
190 status
= ecm_bind_config(c
, host_mac
, the_dev
);
192 pr_debug("could not bind ecm config %d\n", status
);
195 if (c
== &nokia_config_500ma_driver
) {
197 f_phonet_cfg1
= f_phonet
;
198 f_obex1_cfg1
= f_obex1
;
199 f_obex2_cfg1
= f_obex2
;
202 f_phonet_cfg2
= f_phonet
;
203 f_obex1_cfg2
= f_obex1
;
204 f_obex2_cfg2
= f_obex2
;
209 usb_remove_function(c
, f_acm
);
212 usb_remove_function(c
, f_obex2
);
214 usb_remove_function(c
, f_obex1
);
216 usb_remove_function(c
, f_phonet
);
217 usb_put_function(f_acm
);
219 if (!IS_ERR_OR_NULL(f_obex2
))
220 usb_put_function(f_obex2
);
221 if (!IS_ERR_OR_NULL(f_obex1
))
222 usb_put_function(f_obex1
);
223 if (!IS_ERR_OR_NULL(f_phonet
))
224 usb_put_function(f_phonet
);
228 static int __init
nokia_bind(struct usb_composite_dev
*cdev
)
230 struct usb_gadget
*gadget
= cdev
->gadget
;
233 the_dev
= gether_setup(cdev
->gadget
, dev_addr
, host_addr
, host_mac
,
235 if (IS_ERR(the_dev
)) {
236 status
= PTR_ERR(the_dev
);
240 status
= usb_string_ids_tab(cdev
, strings_dev
);
243 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
244 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
245 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
246 nokia_config_500ma_driver
.iConfiguration
= status
;
247 nokia_config_100ma_driver
.iConfiguration
= status
;
249 if (!gadget_supports_altsettings(gadget
))
252 fi_phonet
= usb_get_function_instance("phonet");
253 if (IS_ERR(fi_phonet
))
254 pr_debug("could not find phonet function\n");
256 fi_obex1
= usb_get_function_instance("obex");
257 if (IS_ERR(fi_obex1
))
258 pr_debug("could not find obex function 1\n");
260 fi_obex2
= usb_get_function_instance("obex");
261 if (IS_ERR(fi_obex2
))
262 pr_debug("could not find obex function 2\n");
264 fi_acm
= usb_get_function_instance("acm");
268 /* finally register the configuration */
269 status
= usb_add_config(cdev
, &nokia_config_500ma_driver
,
274 status
= usb_add_config(cdev
, &nokia_config_100ma_driver
,
279 usb_composite_overwrite_options(cdev
, &coverwrite
);
280 dev_info(&gadget
->dev
, "%s\n", NOKIA_LONG_NAME
);
285 usb_put_function(f_acm_cfg1
);
286 if (!IS_ERR_OR_NULL(f_obex1_cfg1
))
287 usb_put_function(f_obex1_cfg1
);
288 if (!IS_ERR_OR_NULL(f_obex2_cfg1
))
289 usb_put_function(f_obex2_cfg1
);
290 if (!IS_ERR_OR_NULL(f_phonet_cfg1
))
291 usb_put_function(f_phonet_cfg1
);
293 usb_put_function_instance(fi_acm
);
295 if (!IS_ERR(fi_obex2
))
296 usb_put_function_instance(fi_obex2
);
297 if (!IS_ERR(fi_obex1
))
298 usb_put_function_instance(fi_obex1
);
299 if (!IS_ERR(fi_phonet
))
300 usb_put_function_instance(fi_phonet
);
302 gether_cleanup(the_dev
);
307 static int __exit
nokia_unbind(struct usb_composite_dev
*cdev
)
309 if (!IS_ERR_OR_NULL(f_obex1_cfg2
))
310 usb_put_function(f_obex1_cfg2
);
311 if (!IS_ERR_OR_NULL(f_obex2_cfg2
))
312 usb_put_function(f_obex2_cfg2
);
313 if (!IS_ERR_OR_NULL(f_obex1_cfg1
))
314 usb_put_function(f_obex1_cfg1
);
315 if (!IS_ERR_OR_NULL(f_obex2_cfg1
))
316 usb_put_function(f_obex2_cfg1
);
317 if (!IS_ERR_OR_NULL(f_phonet_cfg1
))
318 usb_put_function(f_phonet_cfg1
);
319 if (!IS_ERR_OR_NULL(f_phonet_cfg2
))
320 usb_put_function(f_phonet_cfg2
);
321 usb_put_function(f_acm_cfg1
);
322 usb_put_function(f_acm_cfg2
);
324 if (!IS_ERR(fi_obex2
))
325 usb_put_function_instance(fi_obex2
);
326 if (!IS_ERR(fi_obex1
))
327 usb_put_function_instance(fi_obex1
);
328 if (!IS_ERR(fi_phonet
))
329 usb_put_function_instance(fi_phonet
);
330 usb_put_function_instance(fi_acm
);
332 gether_cleanup(the_dev
);
337 static __refdata
struct usb_composite_driver nokia_driver
= {
340 .strings
= dev_strings
,
341 .max_speed
= USB_SPEED_HIGH
,
343 .unbind
= __exit_p(nokia_unbind
),
346 static int __init
nokia_init(void)
348 return usb_composite_probe(&nokia_driver
);
350 module_init(nokia_init
);
352 static void __exit
nokia_cleanup(void)
354 usb_composite_unregister(&nokia_driver
);
356 module_exit(nokia_cleanup
);