Commit | Line | Data |
---|---|---|
f358f5b4 FB |
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> | |
f358f5b4 FB |
19 | #include <linux/device.h> |
20 | ||
21 | #include "u_serial.h" | |
22 | #include "u_ether.h" | |
23 | #include "u_phonet.h" | |
24 | #include "gadget_chips.h" | |
25 | ||
26 | /* Defines */ | |
27 | ||
28 | #define NOKIA_VERSION_NUM 0x0211 | |
29 | #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)" | |
30 | ||
31 | /*-------------------------------------------------------------------------*/ | |
32 | ||
33 | /* | |
34 | * Kbuild is not very cooperative with respect to linking separately | |
35 | * compiled library objects into one module. So for now we won't use | |
36 | * separate compilation ... ensuring init/exit sections work to shrink | |
37 | * the runtime footprint, and giving us at least some parts of what | |
38 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
39 | */ | |
1d8fc251 | 40 | #define USBF_OBEX_INCLUDED |
f358f5b4 | 41 | #include "f_obex.c" |
f358f5b4 FB |
42 | #include "f_phonet.c" |
43 | #include "u_ether.c" | |
44 | ||
45 | /*-------------------------------------------------------------------------*/ | |
7d16e8d3 | 46 | USB_GADGET_COMPOSITE_OPTIONS(); |
f358f5b4 FB |
47 | |
48 | #define NOKIA_VENDOR_ID 0x0421 /* Nokia */ | |
49 | #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */ | |
50 | ||
51 | /* string IDs are assigned dynamically */ | |
52 | ||
276e2e4f | 53 | #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX |
f358f5b4 FB |
54 | |
55 | static char manufacturer_nokia[] = "Nokia"; | |
56 | static const char product_nokia[] = NOKIA_LONG_NAME; | |
57 | static const char description_nokia[] = "PC-Suite Configuration"; | |
58 | ||
59 | static struct usb_string strings_dev[] = { | |
276e2e4f SAS |
60 | [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia, |
61 | [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME, | |
62 | [USB_GADGET_SERIAL_IDX].s = "", | |
f358f5b4 FB |
63 | [STRING_DESCRIPTION_IDX].s = description_nokia, |
64 | { } /* end of list */ | |
65 | }; | |
66 | ||
67 | static struct usb_gadget_strings stringtab_dev = { | |
68 | .language = 0x0409, /* en-us */ | |
69 | .strings = strings_dev, | |
70 | }; | |
71 | ||
72 | static struct usb_gadget_strings *dev_strings[] = { | |
73 | &stringtab_dev, | |
74 | NULL, | |
75 | }; | |
76 | ||
77 | static struct usb_device_descriptor device_desc = { | |
78 | .bLength = USB_DT_DEVICE_SIZE, | |
79 | .bDescriptorType = USB_DT_DEVICE, | |
80 | .bcdUSB = __constant_cpu_to_le16(0x0200), | |
81 | .bDeviceClass = USB_CLASS_COMM, | |
82 | .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID), | |
83 | .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID), | |
ed9cbda6 | 84 | .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM), |
f358f5b4 FB |
85 | /* .iManufacturer = DYNAMIC */ |
86 | /* .iProduct = DYNAMIC */ | |
87 | .bNumConfigurations = 1, | |
88 | }; | |
89 | ||
90 | /*-------------------------------------------------------------------------*/ | |
91 | ||
92 | /* Module */ | |
93 | MODULE_DESCRIPTION("Nokia composite gadget driver for N900"); | |
94 | MODULE_AUTHOR("Felipe Balbi"); | |
95 | MODULE_LICENSE("GPL"); | |
96 | ||
97 | /*-------------------------------------------------------------------------*/ | |
15761826 SAS |
98 | static struct usb_function *f_acm_cfg1; |
99 | static struct usb_function *f_acm_cfg2; | |
f358f5b4 | 100 | static u8 hostaddr[ETH_ALEN]; |
d6a01439 | 101 | static struct eth_dev *the_dev; |
f358f5b4 | 102 | |
19b10a88 SAS |
103 | enum { |
104 | TTY_PORT_OBEX0, | |
105 | TTY_PORT_OBEX1, | |
19b10a88 SAS |
106 | TTY_PORTS_MAX, |
107 | }; | |
108 | ||
109 | static unsigned char tty_lines[TTY_PORTS_MAX]; | |
110 | ||
15761826 SAS |
111 | static struct usb_configuration nokia_config_500ma_driver = { |
112 | .label = "Bus Powered", | |
113 | .bConfigurationValue = 1, | |
114 | /* .iConfiguration = DYNAMIC */ | |
115 | .bmAttributes = USB_CONFIG_ATT_ONE, | |
116 | .MaxPower = 500, | |
117 | }; | |
118 | ||
119 | static struct usb_configuration nokia_config_100ma_driver = { | |
120 | .label = "Self Powered", | |
121 | .bConfigurationValue = 2, | |
122 | /* .iConfiguration = DYNAMIC */ | |
123 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | |
124 | .MaxPower = 100, | |
125 | }; | |
126 | ||
127 | static struct usb_function_instance *fi_acm; | |
128 | ||
f358f5b4 FB |
129 | static int __init nokia_bind_config(struct usb_configuration *c) |
130 | { | |
15761826 | 131 | struct usb_function *f_acm; |
f358f5b4 FB |
132 | int status = 0; |
133 | ||
134 | status = phonet_bind_config(c); | |
135 | if (status) | |
136 | printk(KERN_DEBUG "could not bind phonet config\n"); | |
137 | ||
19b10a88 | 138 | status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]); |
f358f5b4 FB |
139 | if (status) |
140 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); | |
141 | ||
19b10a88 | 142 | status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]); |
f358f5b4 FB |
143 | if (status) |
144 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); | |
145 | ||
15761826 SAS |
146 | f_acm = usb_get_function(fi_acm); |
147 | if (IS_ERR(f_acm)) | |
148 | return PTR_ERR(f_acm); | |
149 | ||
150 | status = usb_add_function(c, f_acm); | |
f358f5b4 | 151 | if (status) |
15761826 | 152 | goto err_conf; |
f358f5b4 | 153 | |
d6a01439 | 154 | status = ecm_bind_config(c, hostaddr, the_dev); |
15761826 SAS |
155 | if (status) { |
156 | pr_debug("could not bind ecm config %d\n", status); | |
157 | goto err_ecm; | |
158 | } | |
159 | if (c == &nokia_config_500ma_driver) | |
160 | f_acm_cfg1 = f_acm; | |
161 | else | |
162 | f_acm_cfg2 = f_acm; | |
f358f5b4 FB |
163 | |
164 | return status; | |
15761826 SAS |
165 | err_ecm: |
166 | usb_remove_function(c, f_acm); | |
167 | err_conf: | |
168 | usb_put_function(f_acm); | |
169 | return status; | |
f358f5b4 FB |
170 | } |
171 | ||
f358f5b4 FB |
172 | static int __init nokia_bind(struct usb_composite_dev *cdev) |
173 | { | |
f358f5b4 FB |
174 | struct usb_gadget *gadget = cdev->gadget; |
175 | int status; | |
19b10a88 | 176 | int cur_line; |
f358f5b4 FB |
177 | |
178 | status = gphonet_setup(cdev->gadget); | |
179 | if (status < 0) | |
180 | goto err_phonet; | |
181 | ||
19b10a88 SAS |
182 | for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) { |
183 | status = gserial_alloc_line(&tty_lines[cur_line]); | |
184 | if (status) | |
185 | goto err_ether; | |
186 | } | |
f358f5b4 | 187 | |
d6a01439 SAS |
188 | the_dev = gether_setup(cdev->gadget, hostaddr); |
189 | if (IS_ERR(the_dev)) { | |
190 | status = PTR_ERR(the_dev); | |
f358f5b4 | 191 | goto err_ether; |
d6a01439 | 192 | } |
f358f5b4 | 193 | |
e1f15ccb | 194 | status = usb_string_ids_tab(cdev, strings_dev); |
f358f5b4 FB |
195 | if (status < 0) |
196 | goto err_usb; | |
276e2e4f SAS |
197 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
198 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
e1f15ccb | 199 | status = strings_dev[STRING_DESCRIPTION_IDX].id; |
f358f5b4 FB |
200 | nokia_config_500ma_driver.iConfiguration = status; |
201 | nokia_config_100ma_driver.iConfiguration = status; | |
202 | ||
ed9cbda6 | 203 | if (!gadget_supports_altsettings(gadget)) |
f358f5b4 | 204 | goto err_usb; |
f358f5b4 | 205 | |
15761826 SAS |
206 | fi_acm = usb_get_function_instance("acm"); |
207 | if (IS_ERR(fi_acm)) | |
208 | goto err_usb; | |
15761826 | 209 | |
25985edc | 210 | /* finally register the configuration */ |
c9bfff9c UKK |
211 | status = usb_add_config(cdev, &nokia_config_500ma_driver, |
212 | nokia_bind_config); | |
f358f5b4 | 213 | if (status < 0) |
15761826 | 214 | goto err_acm_inst; |
f358f5b4 | 215 | |
c9bfff9c UKK |
216 | status = usb_add_config(cdev, &nokia_config_100ma_driver, |
217 | nokia_bind_config); | |
f358f5b4 | 218 | if (status < 0) |
15761826 | 219 | goto err_put_cfg1; |
f358f5b4 | 220 | |
7d16e8d3 | 221 | usb_composite_overwrite_options(cdev, &coverwrite); |
f358f5b4 FB |
222 | dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME); |
223 | ||
224 | return 0; | |
225 | ||
15761826 SAS |
226 | err_put_cfg1: |
227 | usb_put_function(f_acm_cfg1); | |
228 | err_acm_inst: | |
229 | usb_put_function_instance(fi_acm); | |
f358f5b4 | 230 | err_usb: |
d6a01439 | 231 | gether_cleanup(the_dev); |
f358f5b4 | 232 | err_ether: |
19b10a88 SAS |
233 | cur_line--; |
234 | while (cur_line >= 0) | |
235 | gserial_free_line(tty_lines[cur_line--]); | |
236 | ||
f358f5b4 FB |
237 | gphonet_cleanup(); |
238 | err_phonet: | |
239 | return status; | |
240 | } | |
241 | ||
242 | static int __exit nokia_unbind(struct usb_composite_dev *cdev) | |
243 | { | |
19b10a88 SAS |
244 | int i; |
245 | ||
15761826 SAS |
246 | usb_put_function(f_acm_cfg1); |
247 | usb_put_function(f_acm_cfg2); | |
248 | usb_put_function_instance(fi_acm); | |
f358f5b4 | 249 | gphonet_cleanup(); |
19b10a88 SAS |
250 | |
251 | for (i = 0; i < TTY_PORTS_MAX; i++) | |
252 | gserial_free_line(tty_lines[i]); | |
253 | ||
d6a01439 | 254 | gether_cleanup(the_dev); |
f358f5b4 FB |
255 | |
256 | return 0; | |
257 | } | |
258 | ||
c2ec75c2 | 259 | static __refdata struct usb_composite_driver nokia_driver = { |
f358f5b4 FB |
260 | .name = "g_nokia", |
261 | .dev = &device_desc, | |
262 | .strings = dev_strings, | |
35a0e0bf | 263 | .max_speed = USB_SPEED_HIGH, |
03e42bd5 | 264 | .bind = nokia_bind, |
f358f5b4 FB |
265 | .unbind = __exit_p(nokia_unbind), |
266 | }; | |
267 | ||
268 | static int __init nokia_init(void) | |
269 | { | |
03e42bd5 | 270 | return usb_composite_probe(&nokia_driver); |
f358f5b4 FB |
271 | } |
272 | module_init(nokia_init); | |
273 | ||
274 | static void __exit nokia_cleanup(void) | |
275 | { | |
276 | usb_composite_unregister(&nokia_driver); | |
277 | } | |
278 | module_exit(nokia_cleanup); |