blob: 5ae0bf6124288464a1f33b3f453bdd429e5ddc56 [file] [log] [blame]
David Brownell8a408192008-06-19 18:19:32 -07001/*
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.
David Brownell8a408192008-06-19 18:19:32 -070011 */
12
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Brownell8a408192008-06-19 18:19:32 -070014#include <linux/kernel.h>
Andrzej Pietrasiewicz8cedba72013-05-28 09:15:53 +020015#include <linux/module.h>
David Brownell8a408192008-06-19 18:19:32 -070016#include <linux/device.h>
17#include <linux/etherdevice.h>
18
19#include "u_ether.h"
Andrzej Pietrasiewicz8cedba72013-05-28 09:15:53 +020020#include "u_gether.h"
David Brownell8a408192008-06-19 18:19:32 -070021
22/*
23 * This function packages a simple "CDC Subset" Ethernet port with no real
24 * control mechanisms; just raw data transfer over two bulk endpoints.
25 * The data transfer model is exactly that of CDC Ethernet, which is
26 * why we call it the "CDC Subset".
27 *
28 * Because it's not standardized, this has some interoperability issues.
29 * They mostly relate to driver binding, since the data transfer model is
30 * so simple (CDC Ethernet). The original versions of this protocol used
31 * specific product/vendor IDs: byteswapped IDs for Digital Equipment's
32 * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
33 * daughtercards with USB peripheral connectors. (It was used more often
34 * with other boards, using the Itsy identifiers.) Linux hosts recognized
35 * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
36 * and one interface.
37 *
38 * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
39 * "SAFE", which happens to have a mode which is identical to the "CDC
40 * Subset" in terms of data transfer and lack of control model. This was
41 * adopted by later Sharp Zaurus models, and by some other software which
42 * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
43 *
44 * Because Microsoft's RNDIS drivers are far from robust, we added a few
45 * descriptors to the CDC Subset code, making this code look like a SAFE
46 * implementation. This lets you use MCCI's host side MS-Windows drivers
47 * if you get fed up with RNDIS. It also makes it easier for composite
48 * drivers to work, since they can use class based binding instead of
49 * caring about specific product and vendor IDs.
50 */
51
David Brownell8a408192008-06-19 18:19:32 -070052struct f_gether {
53 struct gether port;
54
55 char ethaddr[14];
David Brownell8a408192008-06-19 18:19:32 -070056};
57
58static inline struct f_gether *func_to_geth(struct usb_function *f)
59{
60 return container_of(f, struct f_gether, port.func);
61}
62
63/*-------------------------------------------------------------------------*/
64
65/*
66 * "Simple" CDC-subset option is a simple vendor-neutral model that most
67 * full speed controllers can handle: one interface, two bulk endpoints.
68 * To assist host side drivers, we fancy it up a bit, and add descriptors so
69 * some host side drivers will understand it as a "SAFE" variant.
70 *
71 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
72 * Data endpoints live in the control interface, there's no data interface.
73 * And it's not used to talk to a cell phone radio.
74 */
75
76/* interface descriptor: */
77
Lothar Waßmann8d069842012-03-11 15:08:46 +010078static struct usb_interface_descriptor subset_data_intf = {
David Brownell8a408192008-06-19 18:19:32 -070079 .bLength = sizeof subset_data_intf,
80 .bDescriptorType = USB_DT_INTERFACE,
81
82 /* .bInterfaceNumber = DYNAMIC */
83 .bAlternateSetting = 0,
84 .bNumEndpoints = 2,
85 .bInterfaceClass = USB_CLASS_COMM,
86 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
87 .bInterfaceProtocol = 0,
88 /* .iInterface = DYNAMIC */
89};
90
Lothar Waßmann8d069842012-03-11 15:08:46 +010091static struct usb_cdc_header_desc mdlm_header_desc = {
David Brownell33376c12008-08-18 17:45:07 -070092 .bLength = sizeof mdlm_header_desc,
David Brownell8a408192008-06-19 18:19:32 -070093 .bDescriptorType = USB_DT_CS_INTERFACE,
94 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
95
Harvey Harrison551509d2009-02-11 14:11:36 -080096 .bcdCDC = cpu_to_le16(0x0110),
David Brownell8a408192008-06-19 18:19:32 -070097};
98
Lothar Waßmann8d069842012-03-11 15:08:46 +010099static struct usb_cdc_mdlm_desc mdlm_desc = {
David Brownell8a408192008-06-19 18:19:32 -0700100 .bLength = sizeof mdlm_desc,
101 .bDescriptorType = USB_DT_CS_INTERFACE,
102 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
103
Harvey Harrison551509d2009-02-11 14:11:36 -0800104 .bcdVersion = cpu_to_le16(0x0100),
David Brownell8a408192008-06-19 18:19:32 -0700105 .bGUID = {
106 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
107 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
108 },
109};
110
111/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
112 * can't really use its struct. All we do here is say that we're using
113 * the submode of "SAFE" which directly matches the CDC Subset.
114 */
Lothar Waßmann8d069842012-03-11 15:08:46 +0100115static u8 mdlm_detail_desc[] = {
David Brownell8a408192008-06-19 18:19:32 -0700116 6,
117 USB_DT_CS_INTERFACE,
118 USB_CDC_MDLM_DETAIL_TYPE,
119
120 0, /* "SAFE" */
121 0, /* network control capabilities (none) */
122 0, /* network data capabilities ("raw" encapsulation) */
123};
124
Lothar Waßmann8d069842012-03-11 15:08:46 +0100125static struct usb_cdc_ether_desc ether_desc = {
David Brownell8a408192008-06-19 18:19:32 -0700126 .bLength = sizeof ether_desc,
127 .bDescriptorType = USB_DT_CS_INTERFACE,
128 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
129
130 /* this descriptor actually adds value, surprise! */
131 /* .iMACAddress = DYNAMIC */
Harvey Harrison551509d2009-02-11 14:11:36 -0800132 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
133 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
134 .wNumberMCFilters = cpu_to_le16(0),
David Brownell8a408192008-06-19 18:19:32 -0700135 .bNumberPowerFilters = 0,
136};
137
138/* full speed support: */
139
Lothar Waßmann8d069842012-03-11 15:08:46 +0100140static struct usb_endpoint_descriptor fs_subset_in_desc = {
David Brownell8a408192008-06-19 18:19:32 -0700141 .bLength = USB_DT_ENDPOINT_SIZE,
142 .bDescriptorType = USB_DT_ENDPOINT,
143
144 .bEndpointAddress = USB_DIR_IN,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146};
147
Lothar Waßmann8d069842012-03-11 15:08:46 +0100148static struct usb_endpoint_descriptor fs_subset_out_desc = {
David Brownell8a408192008-06-19 18:19:32 -0700149 .bLength = USB_DT_ENDPOINT_SIZE,
150 .bDescriptorType = USB_DT_ENDPOINT,
151
152 .bEndpointAddress = USB_DIR_OUT,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154};
155
Lothar Waßmann8d069842012-03-11 15:08:46 +0100156static struct usb_descriptor_header *fs_eth_function[] = {
David Brownell8a408192008-06-19 18:19:32 -0700157 (struct usb_descriptor_header *) &subset_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700158 (struct usb_descriptor_header *) &mdlm_header_desc,
David Brownell8a408192008-06-19 18:19:32 -0700159 (struct usb_descriptor_header *) &mdlm_desc,
160 (struct usb_descriptor_header *) &mdlm_detail_desc,
161 (struct usb_descriptor_header *) &ether_desc,
David Brownell33376c12008-08-18 17:45:07 -0700162 (struct usb_descriptor_header *) &fs_subset_in_desc,
163 (struct usb_descriptor_header *) &fs_subset_out_desc,
David Brownell8a408192008-06-19 18:19:32 -0700164 NULL,
165};
166
167/* high speed support: */
168
Lothar Waßmann8d069842012-03-11 15:08:46 +0100169static struct usb_endpoint_descriptor hs_subset_in_desc = {
David Brownell8a408192008-06-19 18:19:32 -0700170 .bLength = USB_DT_ENDPOINT_SIZE,
171 .bDescriptorType = USB_DT_ENDPOINT,
172
173 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800174 .wMaxPacketSize = cpu_to_le16(512),
David Brownell8a408192008-06-19 18:19:32 -0700175};
176
Lothar Waßmann8d069842012-03-11 15:08:46 +0100177static struct usb_endpoint_descriptor hs_subset_out_desc = {
David Brownell8a408192008-06-19 18:19:32 -0700178 .bLength = USB_DT_ENDPOINT_SIZE,
179 .bDescriptorType = USB_DT_ENDPOINT,
180
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800182 .wMaxPacketSize = cpu_to_le16(512),
David Brownell8a408192008-06-19 18:19:32 -0700183};
184
Lothar Waßmann8d069842012-03-11 15:08:46 +0100185static struct usb_descriptor_header *hs_eth_function[] = {
David Brownell8a408192008-06-19 18:19:32 -0700186 (struct usb_descriptor_header *) &subset_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700187 (struct usb_descriptor_header *) &mdlm_header_desc,
David Brownell8a408192008-06-19 18:19:32 -0700188 (struct usb_descriptor_header *) &mdlm_desc,
189 (struct usb_descriptor_header *) &mdlm_detail_desc,
190 (struct usb_descriptor_header *) &ether_desc,
David Brownell33376c12008-08-18 17:45:07 -0700191 (struct usb_descriptor_header *) &hs_subset_in_desc,
192 (struct usb_descriptor_header *) &hs_subset_out_desc,
David Brownell8a408192008-06-19 18:19:32 -0700193 NULL,
194};
195
Paul Zimmerman04617db2011-06-27 14:13:18 -0700196/* super speed support: */
197
Lothar Waßmann8d069842012-03-11 15:08:46 +0100198static struct usb_endpoint_descriptor ss_subset_in_desc = {
Paul Zimmerman04617db2011-06-27 14:13:18 -0700199 .bLength = USB_DT_ENDPOINT_SIZE,
200 .bDescriptorType = USB_DT_ENDPOINT,
201
202 .bmAttributes = USB_ENDPOINT_XFER_BULK,
203 .wMaxPacketSize = cpu_to_le16(1024),
204};
205
Lothar Waßmann8d069842012-03-11 15:08:46 +0100206static struct usb_endpoint_descriptor ss_subset_out_desc = {
Paul Zimmerman04617db2011-06-27 14:13:18 -0700207 .bLength = USB_DT_ENDPOINT_SIZE,
208 .bDescriptorType = USB_DT_ENDPOINT,
209
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
211 .wMaxPacketSize = cpu_to_le16(1024),
212};
213
Lothar Waßmann8d069842012-03-11 15:08:46 +0100214static struct usb_ss_ep_comp_descriptor ss_subset_bulk_comp_desc = {
Paul Zimmerman04617db2011-06-27 14:13:18 -0700215 .bLength = sizeof ss_subset_bulk_comp_desc,
216 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
217
218 /* the following 2 values can be tweaked if necessary */
219 /* .bMaxBurst = 0, */
220 /* .bmAttributes = 0, */
221};
222
Lothar Waßmann8d069842012-03-11 15:08:46 +0100223static struct usb_descriptor_header *ss_eth_function[] = {
Paul Zimmerman04617db2011-06-27 14:13:18 -0700224 (struct usb_descriptor_header *) &subset_data_intf,
225 (struct usb_descriptor_header *) &mdlm_header_desc,
226 (struct usb_descriptor_header *) &mdlm_desc,
227 (struct usb_descriptor_header *) &mdlm_detail_desc,
228 (struct usb_descriptor_header *) &ether_desc,
229 (struct usb_descriptor_header *) &ss_subset_in_desc,
230 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
231 (struct usb_descriptor_header *) &ss_subset_out_desc,
232 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
233 NULL,
234};
235
David Brownell8a408192008-06-19 18:19:32 -0700236/* string descriptors: */
237
238static struct usb_string geth_string_defs[] = {
239 [0].s = "CDC Ethernet Subset/SAFE",
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200240 [1].s = "",
David Brownell8a408192008-06-19 18:19:32 -0700241 { } /* end of list */
242};
243
244static struct usb_gadget_strings geth_string_table = {
245 .language = 0x0409, /* en-us */
246 .strings = geth_string_defs,
247};
248
249static struct usb_gadget_strings *geth_strings[] = {
250 &geth_string_table,
251 NULL,
252};
253
254/*-------------------------------------------------------------------------*/
255
256static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
257{
258 struct f_gether *geth = func_to_geth(f);
259 struct usb_composite_dev *cdev = f->config->cdev;
260 struct net_device *net;
261
262 /* we know alt == 0, so this is an activation or a reset */
263
264 if (geth->port.in_ep->driver_data) {
265 DBG(cdev, "reset cdc subset\n");
266 gether_disconnect(&geth->port);
267 }
268
269 DBG(cdev, "init + activate cdc subset\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300270 if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) ||
271 config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) {
272 geth->port.in_ep->desc = NULL;
273 geth->port.out_ep->desc = NULL;
274 return -EINVAL;
275 }
David Brownell8a408192008-06-19 18:19:32 -0700276
277 net = gether_connect(&geth->port);
278 return IS_ERR(net) ? PTR_ERR(net) : 0;
279}
280
281static void geth_disable(struct usb_function *f)
282{
283 struct f_gether *geth = func_to_geth(f);
284 struct usb_composite_dev *cdev = f->config->cdev;
285
286 DBG(cdev, "net deactivated\n");
287 gether_disconnect(&geth->port);
288}
289
290/*-------------------------------------------------------------------------*/
291
292/* serial function driver setup/binding */
293
Lothar Waßmann8d069842012-03-11 15:08:46 +0100294static int
David Brownell8a408192008-06-19 18:19:32 -0700295geth_bind(struct usb_configuration *c, struct usb_function *f)
296{
297 struct usb_composite_dev *cdev = c->cdev;
298 struct f_gether *geth = func_to_geth(f);
299 int status;
300 struct usb_ep *ep;
301
Andrzej Pietrasiewicz8cedba72013-05-28 09:15:53 +0200302#ifndef USB_FSUBSET_INCLUDED
303 struct f_gether_opts *gether_opts;
304
305 gether_opts = container_of(f->fi, struct f_gether_opts, func_inst);
306
307 /*
308 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
309 * configurations are bound in sequence with list_for_each_entry,
310 * in each configuration its functions are bound in sequence
311 * with list_for_each_entry, so we assume no race condition
312 * with regard to gether_opts->bound access
313 */
314 if (!gether_opts->bound) {
315 gether_set_gadget(gether_opts->net, cdev->gadget);
316 status = gether_register_netdev(gether_opts->net);
317 if (status)
318 return status;
319 gether_opts->bound = true;
320 }
321#endif
322 /* maybe allocate device-global string IDs */
323 if (geth_string_defs[0].id == 0) {
324 status = usb_string_ids_tab(c->cdev, geth_string_defs);
325 if (status < 0)
326 return status;
327 subset_data_intf.iInterface = geth_string_defs[0].id;
328 ether_desc.iMACAddress = geth_string_defs[1].id;
329 }
330
David Brownell8a408192008-06-19 18:19:32 -0700331 /* allocate instance-specific interface IDs */
332 status = usb_interface_id(c, f);
333 if (status < 0)
334 goto fail;
335 subset_data_intf.bInterfaceNumber = status;
336
337 status = -ENODEV;
338
339 /* allocate instance-specific endpoints */
David Brownell33376c12008-08-18 17:45:07 -0700340 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
David Brownell8a408192008-06-19 18:19:32 -0700341 if (!ep)
342 goto fail;
343 geth->port.in_ep = ep;
344 ep->driver_data = cdev; /* claim */
345
David Brownell33376c12008-08-18 17:45:07 -0700346 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
David Brownell8a408192008-06-19 18:19:32 -0700347 if (!ep)
348 goto fail;
349 geth->port.out_ep = ep;
350 ep->driver_data = cdev; /* claim */
351
David Brownell8a408192008-06-19 18:19:32 -0700352 /* support all relevant hardware speeds... we expect that when
353 * hardware is dual speed, all bulk-capable endpoints work at
354 * both speeds
355 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200356 hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
357 hs_subset_out_desc.bEndpointAddress =
358 fs_subset_out_desc.bEndpointAddress;
David Brownell8a408192008-06-19 18:19:32 -0700359
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200360 ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
361 ss_subset_out_desc.bEndpointAddress =
362 fs_subset_out_desc.bEndpointAddress;
Paul Zimmerman04617db2011-06-27 14:13:18 -0700363
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200364 status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function,
365 ss_eth_function);
366 if (status)
367 goto fail;
David Brownell8a408192008-06-19 18:19:32 -0700368
369 /* NOTE: all that is done without knowing or caring about
370 * the network link ... which is unavailable to this code
371 * until we're activated via set_alt().
372 */
373
374 DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700375 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell8a408192008-06-19 18:19:32 -0700376 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
377 geth->port.in_ep->name, geth->port.out_ep->name);
378 return 0;
379
380fail:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200381 usb_free_all_descriptors(f);
David Brownell8a408192008-06-19 18:19:32 -0700382 /* we might as well release our claims on endpoints */
Sebastian Andrzej Siewiore79cc612012-10-22 22:15:00 +0200383 if (geth->port.out_ep)
David Brownell8a408192008-06-19 18:19:32 -0700384 geth->port.out_ep->driver_data = NULL;
Sebastian Andrzej Siewiore79cc612012-10-22 22:15:00 +0200385 if (geth->port.in_ep)
David Brownell8a408192008-06-19 18:19:32 -0700386 geth->port.in_ep->driver_data = NULL;
387
388 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
389
390 return status;
391}
392
Andrzej Pietrasiewicz8cedba72013-05-28 09:15:53 +0200393#ifdef USB_FSUBSET_INCLUDED
394
David Brownell8a408192008-06-19 18:19:32 -0700395static void
Andrzej Pietrasiewicz8cedba72013-05-28 09:15:53 +0200396geth_old_unbind(struct usb_configuration *c, struct usb_function *f)
David Brownell8a408192008-06-19 18:19:32 -0700397{
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200398 geth_string_defs[0].id = 0;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200399 usb_free_all_descriptors(f);
David Brownell8a408192008-06-19 18:19:32 -0700400 kfree(func_to_geth(f));
401}
402
403/**
404 * geth_bind_config - add CDC Subset network link to a configuration
405 * @c: the configuration to support the network link
406 * @ethaddr: a buffer in which the ethernet address of the host side
407 * side of the link was recorded
Robert P. J. Day21c7dee2013-05-02 10:28:33 -0400408 * @dev: eth_dev structure
David Brownell8a408192008-06-19 18:19:32 -0700409 * Context: single threaded during gadget setup
410 *
411 * Returns zero on success, else negative errno.
412 *
413 * Caller must have called @gether_setup(). Caller is also responsible
414 * for calling @gether_cleanup() before module unload.
415 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100416int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
417 struct eth_dev *dev)
David Brownell8a408192008-06-19 18:19:32 -0700418{
419 struct f_gether *geth;
420 int status;
421
David Brownell8a408192008-06-19 18:19:32 -0700422 /* allocate and initialize one new instance */
423 geth = kzalloc(sizeof *geth, GFP_KERNEL);
424 if (!geth)
425 return -ENOMEM;
426
427 /* export host's Ethernet address in CDC format */
Andy Shevchenko8c7ca992012-07-06 18:30:21 +0300428 snprintf(geth->ethaddr, sizeof geth->ethaddr, "%pm", ethaddr);
David Brownell8a408192008-06-19 18:19:32 -0700429 geth_string_defs[1].s = geth->ethaddr;
430
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100431 geth->port.ioport = dev;
David Brownell8a408192008-06-19 18:19:32 -0700432 geth->port.cdc_filter = DEFAULT_FILTER;
433
434 geth->port.func.name = "cdc_subset";
435 geth->port.func.strings = geth_strings;
436 geth->port.func.bind = geth_bind;
Andrzej Pietrasiewicz8cedba72013-05-28 09:15:53 +0200437 geth->port.func.unbind = geth_old_unbind;
David Brownell8a408192008-06-19 18:19:32 -0700438 geth->port.func.set_alt = geth_set_alt;
439 geth->port.func.disable = geth_disable;
440
441 status = usb_add_function(c, &geth->port.func);
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200442 if (status)
David Brownell8a408192008-06-19 18:19:32 -0700443 kfree(geth);
David Brownell8a408192008-06-19 18:19:32 -0700444 return status;
445}
Andrzej Pietrasiewicz8cedba72013-05-28 09:15:53 +0200446
447#else
448
449static void geth_free_inst(struct usb_function_instance *f)
450{
451 struct f_gether_opts *opts;
452
453 opts = container_of(f, struct f_gether_opts, func_inst);
454 if (opts->bound)
455 gether_cleanup(netdev_priv(opts->net));
456 else
457 free_netdev(opts->net);
458 kfree(opts);
459}
460
461static struct usb_function_instance *geth_alloc_inst(void)
462{
463 struct f_gether_opts *opts;
464
465 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
466 if (!opts)
467 return ERR_PTR(-ENOMEM);
468
469 opts->func_inst.free_func_inst = geth_free_inst;
470 opts->net = gether_setup_default();
471 if (IS_ERR(opts->net))
472 return ERR_CAST(opts->net);
473
474 return &opts->func_inst;
475}
476
477static void geth_free(struct usb_function *f)
478{
479 struct f_gether *eth;
480
481 eth = func_to_geth(f);
482 kfree(eth);
483}
484
485static void geth_unbind(struct usb_configuration *c, struct usb_function *f)
486{
487 geth_string_defs[0].id = 0;
488 usb_free_all_descriptors(f);
489}
490
491static struct usb_function *geth_alloc(struct usb_function_instance *fi)
492{
493 struct f_gether *geth;
494 struct f_gether_opts *opts;
495 int status;
496
497 /* allocate and initialize one new instance */
498 geth = kzalloc(sizeof(*geth), GFP_KERNEL);
499 if (!geth)
500 return ERR_PTR(-ENOMEM);
501
502 opts = container_of(fi, struct f_gether_opts, func_inst);
503
504 /* export host's Ethernet address in CDC format */
505 status = gether_get_host_addr_cdc(opts->net, geth->ethaddr,
506 sizeof(geth->ethaddr));
507 if (status < 12) {
508 kfree(geth);
509 return ERR_PTR(-EINVAL);
510 }
511 geth_string_defs[1].s = geth->ethaddr;
512
513 geth->port.ioport = netdev_priv(opts->net);
514 geth->port.cdc_filter = DEFAULT_FILTER;
515
516 geth->port.func.name = "cdc_subset";
517 geth->port.func.strings = geth_strings;
518 geth->port.func.bind = geth_bind;
519 geth->port.func.unbind = geth_unbind;
520 geth->port.func.set_alt = geth_set_alt;
521 geth->port.func.disable = geth_disable;
522 geth->port.func.free_func = geth_free;
523
524 return &geth->port.func;
525}
526
527DECLARE_USB_FUNCTION_INIT(geth, geth_alloc_inst, geth_alloc);
528MODULE_LICENSE("GPL");
529MODULE_AUTHOR("David Brownell");
530
531#endif