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> | |
19 | #include <linux/utsname.h> | |
20 | #include <linux/device.h> | |
21 | ||
22 | #include "u_serial.h" | |
23 | #include "u_ether.h" | |
24 | #include "u_phonet.h" | |
25 | #include "gadget_chips.h" | |
26 | ||
27 | /* Defines */ | |
28 | ||
29 | #define NOKIA_VERSION_NUM 0x0211 | |
30 | #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)" | |
31 | ||
32 | /*-------------------------------------------------------------------------*/ | |
33 | ||
34 | /* | |
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. | |
40 | */ | |
41 | #include "composite.c" | |
f358f5b4 FB |
42 | |
43 | #include "u_serial.c" | |
44 | #include "f_acm.c" | |
45 | #include "f_ecm.c" | |
46 | #include "f_obex.c" | |
47 | #include "f_serial.c" | |
48 | #include "f_phonet.c" | |
49 | #include "u_ether.c" | |
50 | ||
51 | /*-------------------------------------------------------------------------*/ | |
7d16e8d3 | 52 | USB_GADGET_COMPOSITE_OPTIONS(); |
f358f5b4 FB |
53 | |
54 | #define NOKIA_VENDOR_ID 0x0421 /* Nokia */ | |
55 | #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */ | |
56 | ||
57 | /* string IDs are assigned dynamically */ | |
58 | ||
276e2e4f | 59 | #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX |
f358f5b4 FB |
60 | |
61 | static char manufacturer_nokia[] = "Nokia"; | |
62 | static const char product_nokia[] = NOKIA_LONG_NAME; | |
63 | static const char description_nokia[] = "PC-Suite Configuration"; | |
64 | ||
65 | static struct usb_string strings_dev[] = { | |
276e2e4f SAS |
66 | [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia, |
67 | [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME, | |
68 | [USB_GADGET_SERIAL_IDX].s = "", | |
f358f5b4 FB |
69 | [STRING_DESCRIPTION_IDX].s = description_nokia, |
70 | { } /* end of list */ | |
71 | }; | |
72 | ||
73 | static struct usb_gadget_strings stringtab_dev = { | |
74 | .language = 0x0409, /* en-us */ | |
75 | .strings = strings_dev, | |
76 | }; | |
77 | ||
78 | static struct usb_gadget_strings *dev_strings[] = { | |
79 | &stringtab_dev, | |
80 | NULL, | |
81 | }; | |
82 | ||
83 | static struct usb_device_descriptor device_desc = { | |
84 | .bLength = USB_DT_DEVICE_SIZE, | |
85 | .bDescriptorType = USB_DT_DEVICE, | |
86 | .bcdUSB = __constant_cpu_to_le16(0x0200), | |
87 | .bDeviceClass = USB_CLASS_COMM, | |
88 | .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID), | |
89 | .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID), | |
90 | /* .iManufacturer = DYNAMIC */ | |
91 | /* .iProduct = DYNAMIC */ | |
92 | .bNumConfigurations = 1, | |
93 | }; | |
94 | ||
95 | /*-------------------------------------------------------------------------*/ | |
96 | ||
97 | /* Module */ | |
98 | MODULE_DESCRIPTION("Nokia composite gadget driver for N900"); | |
99 | MODULE_AUTHOR("Felipe Balbi"); | |
100 | MODULE_LICENSE("GPL"); | |
101 | ||
102 | /*-------------------------------------------------------------------------*/ | |
103 | ||
104 | static u8 hostaddr[ETH_ALEN]; | |
105 | ||
106 | static int __init nokia_bind_config(struct usb_configuration *c) | |
107 | { | |
108 | int status = 0; | |
109 | ||
110 | status = phonet_bind_config(c); | |
111 | if (status) | |
112 | printk(KERN_DEBUG "could not bind phonet config\n"); | |
113 | ||
114 | status = obex_bind_config(c, 0); | |
115 | if (status) | |
116 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); | |
117 | ||
118 | status = obex_bind_config(c, 1); | |
119 | if (status) | |
120 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); | |
121 | ||
122 | status = acm_bind_config(c, 2); | |
123 | if (status) | |
124 | printk(KERN_DEBUG "could not bind acm config\n"); | |
125 | ||
126 | status = ecm_bind_config(c, hostaddr); | |
127 | if (status) | |
128 | printk(KERN_DEBUG "could not bind ecm config\n"); | |
129 | ||
130 | return status; | |
131 | } | |
132 | ||
133 | static struct usb_configuration nokia_config_500ma_driver = { | |
134 | .label = "Bus Powered", | |
f358f5b4 FB |
135 | .bConfigurationValue = 1, |
136 | /* .iConfiguration = DYNAMIC */ | |
137 | .bmAttributes = USB_CONFIG_ATT_ONE, | |
138 | .bMaxPower = 250, /* 500mA */ | |
139 | }; | |
140 | ||
141 | static struct usb_configuration nokia_config_100ma_driver = { | |
142 | .label = "Self Powered", | |
f358f5b4 FB |
143 | .bConfigurationValue = 2, |
144 | /* .iConfiguration = DYNAMIC */ | |
145 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | |
146 | .bMaxPower = 50, /* 100 mA */ | |
147 | }; | |
148 | ||
149 | static int __init nokia_bind(struct usb_composite_dev *cdev) | |
150 | { | |
151 | int gcnum; | |
152 | struct usb_gadget *gadget = cdev->gadget; | |
153 | int status; | |
154 | ||
155 | status = gphonet_setup(cdev->gadget); | |
156 | if (status < 0) | |
157 | goto err_phonet; | |
158 | ||
159 | status = gserial_setup(cdev->gadget, 3); | |
160 | if (status < 0) | |
161 | goto err_serial; | |
162 | ||
163 | status = gether_setup(cdev->gadget, hostaddr); | |
164 | if (status < 0) | |
165 | goto err_ether; | |
166 | ||
e1f15ccb | 167 | status = usb_string_ids_tab(cdev, strings_dev); |
f358f5b4 FB |
168 | if (status < 0) |
169 | goto err_usb; | |
276e2e4f SAS |
170 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
171 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
e1f15ccb | 172 | status = strings_dev[STRING_DESCRIPTION_IDX].id; |
f358f5b4 FB |
173 | nokia_config_500ma_driver.iConfiguration = status; |
174 | nokia_config_100ma_driver.iConfiguration = status; | |
175 | ||
176 | /* set up other descriptors */ | |
177 | gcnum = usb_gadget_controller_number(gadget); | |
178 | if (gcnum >= 0) | |
179 | device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM); | |
180 | else { | |
181 | /* this should only work with hw that supports altsettings | |
182 | * and several endpoints, anything else, panic. | |
183 | */ | |
184 | pr_err("nokia_bind: controller '%s' not recognized\n", | |
185 | gadget->name); | |
186 | goto err_usb; | |
187 | } | |
188 | ||
25985edc | 189 | /* finally register the configuration */ |
c9bfff9c UKK |
190 | status = usb_add_config(cdev, &nokia_config_500ma_driver, |
191 | nokia_bind_config); | |
f358f5b4 FB |
192 | if (status < 0) |
193 | goto err_usb; | |
194 | ||
c9bfff9c UKK |
195 | status = usb_add_config(cdev, &nokia_config_100ma_driver, |
196 | nokia_bind_config); | |
f358f5b4 FB |
197 | if (status < 0) |
198 | goto err_usb; | |
199 | ||
7d16e8d3 | 200 | usb_composite_overwrite_options(cdev, &coverwrite); |
f358f5b4 FB |
201 | dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME); |
202 | ||
203 | return 0; | |
204 | ||
205 | err_usb: | |
206 | gether_cleanup(); | |
207 | err_ether: | |
208 | gserial_cleanup(); | |
209 | err_serial: | |
210 | gphonet_cleanup(); | |
211 | err_phonet: | |
212 | return status; | |
213 | } | |
214 | ||
215 | static int __exit nokia_unbind(struct usb_composite_dev *cdev) | |
216 | { | |
217 | gphonet_cleanup(); | |
218 | gserial_cleanup(); | |
219 | gether_cleanup(); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
c2ec75c2 | 224 | static __refdata struct usb_composite_driver nokia_driver = { |
f358f5b4 FB |
225 | .name = "g_nokia", |
226 | .dev = &device_desc, | |
227 | .strings = dev_strings, | |
35a0e0bf | 228 | .max_speed = USB_SPEED_HIGH, |
03e42bd5 | 229 | .bind = nokia_bind, |
f358f5b4 FB |
230 | .unbind = __exit_p(nokia_unbind), |
231 | }; | |
232 | ||
233 | static int __init nokia_init(void) | |
234 | { | |
03e42bd5 | 235 | return usb_composite_probe(&nokia_driver); |
f358f5b4 FB |
236 | } |
237 | module_init(nokia_init); | |
238 | ||
239 | static void __exit nokia_cleanup(void) | |
240 | { | |
241 | usb_composite_unregister(&nokia_driver); | |
242 | } | |
243 | module_exit(nokia_cleanup); | |
244 |