Commit | Line | Data |
---|---|---|
8a40819e DB |
1 | /* |
2 | * f_subset.c -- "CDC Subset" Ethernet link function driver | |
3 | * | |
4 | * Copyright (C) 2003-2005,2008 David Brownell | |
5 | * Copyright (C) 2008 Nokia Corporation | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include <linux/kernel.h> | |
23 | #include <linux/device.h> | |
24 | #include <linux/etherdevice.h> | |
25 | ||
26 | #include "u_ether.h" | |
27 | ||
28 | ||
29 | /* | |
30 | * This function packages a simple "CDC Subset" Ethernet port with no real | |
31 | * control mechanisms; just raw data transfer over two bulk endpoints. | |
32 | * The data transfer model is exactly that of CDC Ethernet, which is | |
33 | * why we call it the "CDC Subset". | |
34 | * | |
35 | * Because it's not standardized, this has some interoperability issues. | |
36 | * They mostly relate to driver binding, since the data transfer model is | |
37 | * so simple (CDC Ethernet). The original versions of this protocol used | |
38 | * specific product/vendor IDs: byteswapped IDs for Digital Equipment's | |
39 | * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported | |
40 | * daughtercards with USB peripheral connectors. (It was used more often | |
41 | * with other boards, using the Itsy identifiers.) Linux hosts recognized | |
42 | * this with CONFIG_USB_ARMLINUX; these devices have only one configuration | |
43 | * and one interface. | |
44 | * | |
45 | * At some point, MCCI defined a (nonconformant) CDC MDLM variant called | |
46 | * "SAFE", which happens to have a mode which is identical to the "CDC | |
47 | * Subset" in terms of data transfer and lack of control model. This was | |
48 | * adopted by later Sharp Zaurus models, and by some other software which | |
49 | * Linux hosts recognize with CONFIG_USB_NET_ZAURUS. | |
50 | * | |
51 | * Because Microsoft's RNDIS drivers are far from robust, we added a few | |
52 | * descriptors to the CDC Subset code, making this code look like a SAFE | |
53 | * implementation. This lets you use MCCI's host side MS-Windows drivers | |
54 | * if you get fed up with RNDIS. It also makes it easier for composite | |
55 | * drivers to work, since they can use class based binding instead of | |
56 | * caring about specific product and vendor IDs. | |
57 | */ | |
58 | ||
59 | struct geth_descs { | |
60 | struct usb_endpoint_descriptor *in; | |
61 | struct usb_endpoint_descriptor *out; | |
62 | }; | |
63 | ||
64 | struct f_gether { | |
65 | struct gether port; | |
66 | ||
67 | char ethaddr[14]; | |
68 | ||
8a40819e | 69 | struct geth_descs fs; |
8a40819e DB |
70 | struct geth_descs hs; |
71 | }; | |
72 | ||
73 | static inline struct f_gether *func_to_geth(struct usb_function *f) | |
74 | { | |
75 | return container_of(f, struct f_gether, port.func); | |
76 | } | |
77 | ||
78 | /*-------------------------------------------------------------------------*/ | |
79 | ||
80 | /* | |
81 | * "Simple" CDC-subset option is a simple vendor-neutral model that most | |
82 | * full speed controllers can handle: one interface, two bulk endpoints. | |
83 | * To assist host side drivers, we fancy it up a bit, and add descriptors so | |
84 | * some host side drivers will understand it as a "SAFE" variant. | |
85 | * | |
86 | * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways. | |
87 | * Data endpoints live in the control interface, there's no data interface. | |
88 | * And it's not used to talk to a cell phone radio. | |
89 | */ | |
90 | ||
91 | /* interface descriptor: */ | |
92 | ||
93 | static struct usb_interface_descriptor subset_data_intf __initdata = { | |
94 | .bLength = sizeof subset_data_intf, | |
95 | .bDescriptorType = USB_DT_INTERFACE, | |
96 | ||
97 | /* .bInterfaceNumber = DYNAMIC */ | |
98 | .bAlternateSetting = 0, | |
99 | .bNumEndpoints = 2, | |
100 | .bInterfaceClass = USB_CLASS_COMM, | |
101 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, | |
102 | .bInterfaceProtocol = 0, | |
103 | /* .iInterface = DYNAMIC */ | |
104 | }; | |
105 | ||
33376c1c DB |
106 | static struct usb_cdc_header_desc mdlm_header_desc __initdata = { |
107 | .bLength = sizeof mdlm_header_desc, | |
8a40819e DB |
108 | .bDescriptorType = USB_DT_CS_INTERFACE, |
109 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | |
110 | ||
111 | .bcdCDC = __constant_cpu_to_le16(0x0110), | |
112 | }; | |
113 | ||
114 | static struct usb_cdc_mdlm_desc mdlm_desc __initdata = { | |
115 | .bLength = sizeof mdlm_desc, | |
116 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
117 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, | |
118 | ||
119 | .bcdVersion = __constant_cpu_to_le16(0x0100), | |
120 | .bGUID = { | |
121 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, | |
122 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, | |
123 | }, | |
124 | }; | |
125 | ||
126 | /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we | |
127 | * can't really use its struct. All we do here is say that we're using | |
128 | * the submode of "SAFE" which directly matches the CDC Subset. | |
129 | */ | |
130 | static u8 mdlm_detail_desc[] __initdata = { | |
131 | 6, | |
132 | USB_DT_CS_INTERFACE, | |
133 | USB_CDC_MDLM_DETAIL_TYPE, | |
134 | ||
135 | 0, /* "SAFE" */ | |
136 | 0, /* network control capabilities (none) */ | |
137 | 0, /* network data capabilities ("raw" encapsulation) */ | |
138 | }; | |
139 | ||
140 | static struct usb_cdc_ether_desc ether_desc __initdata = { | |
141 | .bLength = sizeof ether_desc, | |
142 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
143 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, | |
144 | ||
145 | /* this descriptor actually adds value, surprise! */ | |
146 | /* .iMACAddress = DYNAMIC */ | |
147 | .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ | |
148 | .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), | |
149 | .wNumberMCFilters = __constant_cpu_to_le16(0), | |
150 | .bNumberPowerFilters = 0, | |
151 | }; | |
152 | ||
153 | /* full speed support: */ | |
154 | ||
33376c1c | 155 | static struct usb_endpoint_descriptor fs_subset_in_desc __initdata = { |
8a40819e DB |
156 | .bLength = USB_DT_ENDPOINT_SIZE, |
157 | .bDescriptorType = USB_DT_ENDPOINT, | |
158 | ||
159 | .bEndpointAddress = USB_DIR_IN, | |
160 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
161 | }; | |
162 | ||
33376c1c | 163 | static struct usb_endpoint_descriptor fs_subset_out_desc __initdata = { |
8a40819e DB |
164 | .bLength = USB_DT_ENDPOINT_SIZE, |
165 | .bDescriptorType = USB_DT_ENDPOINT, | |
166 | ||
167 | .bEndpointAddress = USB_DIR_OUT, | |
168 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
169 | }; | |
170 | ||
171 | static struct usb_descriptor_header *fs_eth_function[] __initdata = { | |
172 | (struct usb_descriptor_header *) &subset_data_intf, | |
33376c1c | 173 | (struct usb_descriptor_header *) &mdlm_header_desc, |
8a40819e DB |
174 | (struct usb_descriptor_header *) &mdlm_desc, |
175 | (struct usb_descriptor_header *) &mdlm_detail_desc, | |
176 | (struct usb_descriptor_header *) ðer_desc, | |
33376c1c DB |
177 | (struct usb_descriptor_header *) &fs_subset_in_desc, |
178 | (struct usb_descriptor_header *) &fs_subset_out_desc, | |
8a40819e DB |
179 | NULL, |
180 | }; | |
181 | ||
182 | /* high speed support: */ | |
183 | ||
33376c1c | 184 | static struct usb_endpoint_descriptor hs_subset_in_desc __initdata = { |
8a40819e DB |
185 | .bLength = USB_DT_ENDPOINT_SIZE, |
186 | .bDescriptorType = USB_DT_ENDPOINT, | |
187 | ||
188 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
189 | .wMaxPacketSize = __constant_cpu_to_le16(512), | |
190 | }; | |
191 | ||
33376c1c | 192 | static struct usb_endpoint_descriptor hs_subset_out_desc __initdata = { |
8a40819e DB |
193 | .bLength = USB_DT_ENDPOINT_SIZE, |
194 | .bDescriptorType = USB_DT_ENDPOINT, | |
195 | ||
196 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
197 | .wMaxPacketSize = __constant_cpu_to_le16(512), | |
198 | }; | |
199 | ||
200 | static struct usb_descriptor_header *hs_eth_function[] __initdata = { | |
201 | (struct usb_descriptor_header *) &subset_data_intf, | |
33376c1c | 202 | (struct usb_descriptor_header *) &mdlm_header_desc, |
8a40819e DB |
203 | (struct usb_descriptor_header *) &mdlm_desc, |
204 | (struct usb_descriptor_header *) &mdlm_detail_desc, | |
205 | (struct usb_descriptor_header *) ðer_desc, | |
33376c1c DB |
206 | (struct usb_descriptor_header *) &hs_subset_in_desc, |
207 | (struct usb_descriptor_header *) &hs_subset_out_desc, | |
8a40819e DB |
208 | NULL, |
209 | }; | |
210 | ||
211 | /* string descriptors: */ | |
212 | ||
213 | static struct usb_string geth_string_defs[] = { | |
214 | [0].s = "CDC Ethernet Subset/SAFE", | |
215 | [1].s = NULL /* DYNAMIC */, | |
216 | { } /* end of list */ | |
217 | }; | |
218 | ||
219 | static struct usb_gadget_strings geth_string_table = { | |
220 | .language = 0x0409, /* en-us */ | |
221 | .strings = geth_string_defs, | |
222 | }; | |
223 | ||
224 | static struct usb_gadget_strings *geth_strings[] = { | |
225 | &geth_string_table, | |
226 | NULL, | |
227 | }; | |
228 | ||
229 | /*-------------------------------------------------------------------------*/ | |
230 | ||
231 | static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | |
232 | { | |
233 | struct f_gether *geth = func_to_geth(f); | |
234 | struct usb_composite_dev *cdev = f->config->cdev; | |
235 | struct net_device *net; | |
236 | ||
237 | /* we know alt == 0, so this is an activation or a reset */ | |
238 | ||
239 | if (geth->port.in_ep->driver_data) { | |
240 | DBG(cdev, "reset cdc subset\n"); | |
241 | gether_disconnect(&geth->port); | |
242 | } | |
243 | ||
244 | DBG(cdev, "init + activate cdc subset\n"); | |
245 | geth->port.in = ep_choose(cdev->gadget, | |
246 | geth->hs.in, geth->fs.in); | |
247 | geth->port.out = ep_choose(cdev->gadget, | |
248 | geth->hs.out, geth->fs.out); | |
249 | ||
250 | net = gether_connect(&geth->port); | |
251 | return IS_ERR(net) ? PTR_ERR(net) : 0; | |
252 | } | |
253 | ||
254 | static void geth_disable(struct usb_function *f) | |
255 | { | |
256 | struct f_gether *geth = func_to_geth(f); | |
257 | struct usb_composite_dev *cdev = f->config->cdev; | |
258 | ||
259 | DBG(cdev, "net deactivated\n"); | |
260 | gether_disconnect(&geth->port); | |
261 | } | |
262 | ||
263 | /*-------------------------------------------------------------------------*/ | |
264 | ||
265 | /* serial function driver setup/binding */ | |
266 | ||
267 | static int __init | |
268 | geth_bind(struct usb_configuration *c, struct usb_function *f) | |
269 | { | |
270 | struct usb_composite_dev *cdev = c->cdev; | |
271 | struct f_gether *geth = func_to_geth(f); | |
272 | int status; | |
273 | struct usb_ep *ep; | |
274 | ||
275 | /* allocate instance-specific interface IDs */ | |
276 | status = usb_interface_id(c, f); | |
277 | if (status < 0) | |
278 | goto fail; | |
279 | subset_data_intf.bInterfaceNumber = status; | |
280 | ||
281 | status = -ENODEV; | |
282 | ||
283 | /* allocate instance-specific endpoints */ | |
33376c1c | 284 | ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc); |
8a40819e DB |
285 | if (!ep) |
286 | goto fail; | |
287 | geth->port.in_ep = ep; | |
288 | ep->driver_data = cdev; /* claim */ | |
289 | ||
33376c1c | 290 | ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc); |
8a40819e DB |
291 | if (!ep) |
292 | goto fail; | |
293 | geth->port.out_ep = ep; | |
294 | ep->driver_data = cdev; /* claim */ | |
295 | ||
296 | /* copy descriptors, and track endpoint copies */ | |
297 | f->descriptors = usb_copy_descriptors(fs_eth_function); | |
298 | ||
299 | geth->fs.in = usb_find_endpoint(fs_eth_function, | |
33376c1c | 300 | f->descriptors, &fs_subset_in_desc); |
8a40819e | 301 | geth->fs.out = usb_find_endpoint(fs_eth_function, |
33376c1c | 302 | f->descriptors, &fs_subset_out_desc); |
8a40819e DB |
303 | |
304 | ||
305 | /* support all relevant hardware speeds... we expect that when | |
306 | * hardware is dual speed, all bulk-capable endpoints work at | |
307 | * both speeds | |
308 | */ | |
309 | if (gadget_is_dualspeed(c->cdev->gadget)) { | |
33376c1c DB |
310 | hs_subset_in_desc.bEndpointAddress = |
311 | fs_subset_in_desc.bEndpointAddress; | |
312 | hs_subset_out_desc.bEndpointAddress = | |
313 | fs_subset_out_desc.bEndpointAddress; | |
8a40819e DB |
314 | |
315 | /* copy descriptors, and track endpoint copies */ | |
316 | f->hs_descriptors = usb_copy_descriptors(hs_eth_function); | |
317 | ||
318 | geth->hs.in = usb_find_endpoint(hs_eth_function, | |
33376c1c | 319 | f->hs_descriptors, &hs_subset_in_desc); |
8a40819e | 320 | geth->hs.out = usb_find_endpoint(hs_eth_function, |
33376c1c | 321 | f->hs_descriptors, &hs_subset_out_desc); |
8a40819e DB |
322 | } |
323 | ||
324 | /* NOTE: all that is done without knowing or caring about | |
325 | * the network link ... which is unavailable to this code | |
326 | * until we're activated via set_alt(). | |
327 | */ | |
328 | ||
329 | DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n", | |
330 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | |
331 | geth->port.in_ep->name, geth->port.out_ep->name); | |
332 | return 0; | |
333 | ||
334 | fail: | |
335 | /* we might as well release our claims on endpoints */ | |
336 | if (geth->port.out) | |
337 | geth->port.out_ep->driver_data = NULL; | |
338 | if (geth->port.in) | |
339 | geth->port.in_ep->driver_data = NULL; | |
340 | ||
341 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | |
342 | ||
343 | return status; | |
344 | } | |
345 | ||
346 | static void | |
347 | geth_unbind(struct usb_configuration *c, struct usb_function *f) | |
348 | { | |
349 | if (gadget_is_dualspeed(c->cdev->gadget)) | |
350 | usb_free_descriptors(f->hs_descriptors); | |
351 | usb_free_descriptors(f->descriptors); | |
352 | geth_string_defs[1].s = NULL; | |
353 | kfree(func_to_geth(f)); | |
354 | } | |
355 | ||
356 | /** | |
357 | * geth_bind_config - add CDC Subset network link to a configuration | |
358 | * @c: the configuration to support the network link | |
359 | * @ethaddr: a buffer in which the ethernet address of the host side | |
360 | * side of the link was recorded | |
361 | * Context: single threaded during gadget setup | |
362 | * | |
363 | * Returns zero on success, else negative errno. | |
364 | * | |
365 | * Caller must have called @gether_setup(). Caller is also responsible | |
366 | * for calling @gether_cleanup() before module unload. | |
367 | */ | |
368 | int __init geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) | |
369 | { | |
370 | struct f_gether *geth; | |
371 | int status; | |
372 | ||
373 | if (!ethaddr) | |
374 | return -EINVAL; | |
375 | ||
376 | /* maybe allocate device-global string IDs */ | |
377 | if (geth_string_defs[0].id == 0) { | |
378 | ||
379 | /* interface label */ | |
380 | status = usb_string_id(c->cdev); | |
381 | if (status < 0) | |
382 | return status; | |
383 | geth_string_defs[0].id = status; | |
384 | subset_data_intf.iInterface = status; | |
385 | ||
386 | /* MAC address */ | |
387 | status = usb_string_id(c->cdev); | |
388 | if (status < 0) | |
389 | return status; | |
390 | geth_string_defs[1].id = status; | |
391 | ether_desc.iMACAddress = status; | |
392 | } | |
393 | ||
394 | /* allocate and initialize one new instance */ | |
395 | geth = kzalloc(sizeof *geth, GFP_KERNEL); | |
396 | if (!geth) | |
397 | return -ENOMEM; | |
398 | ||
399 | /* export host's Ethernet address in CDC format */ | |
400 | snprintf(geth->ethaddr, sizeof geth->ethaddr, | |
401 | "%02X%02X%02X%02X%02X%02X", | |
402 | ethaddr[0], ethaddr[1], ethaddr[2], | |
403 | ethaddr[3], ethaddr[4], ethaddr[5]); | |
404 | geth_string_defs[1].s = geth->ethaddr; | |
405 | ||
406 | geth->port.cdc_filter = DEFAULT_FILTER; | |
407 | ||
408 | geth->port.func.name = "cdc_subset"; | |
409 | geth->port.func.strings = geth_strings; | |
410 | geth->port.func.bind = geth_bind; | |
411 | geth->port.func.unbind = geth_unbind; | |
412 | geth->port.func.set_alt = geth_set_alt; | |
413 | geth->port.func.disable = geth_disable; | |
414 | ||
415 | status = usb_add_function(c, &geth->port.func); | |
416 | if (status) { | |
417 | geth_string_defs[1].s = NULL; | |
418 | kfree(geth); | |
419 | } | |
420 | return status; | |
421 | } |