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