1 // SPDX-License-Identifier: GPL-2.0+
3 * cdc2.c -- CDC Composite driver, with ECM and ACM support
5 * Copyright (C) 2008 David Brownell
6 * Copyright (C) 2008 Nokia Corporation
9 #include <linux/kernel.h>
10 #include <linux/module.h>
17 #define DRIVER_DESC "CDC Composite Gadget"
18 #define DRIVER_VERSION "King Kamehameha Day 2008"
20 /*-------------------------------------------------------------------------*/
22 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
23 * Instead: allocate your own, using normal USB-IF procedures.
26 /* Thanks to NetChip Technologies for donating this product ID.
27 * It's for devices with only this composite CDC configuration.
29 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
30 #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
32 USB_GADGET_COMPOSITE_OPTIONS();
34 USB_ETHERNET_MODULE_PARAMETERS();
36 /*-------------------------------------------------------------------------*/
38 static struct usb_device_descriptor device_desc
= {
39 .bLength
= sizeof device_desc
,
40 .bDescriptorType
= USB_DT_DEVICE
,
42 /* .bcdUSB = DYNAMIC */
44 .bDeviceClass
= USB_CLASS_COMM
,
47 /* .bMaxPacketSize0 = f(hardware) */
49 /* Vendor and product id can be overridden by module parameters. */
50 .idVendor
= cpu_to_le16(CDC_VENDOR_NUM
),
51 .idProduct
= cpu_to_le16(CDC_PRODUCT_NUM
),
52 /* .bcdDevice = f(hardware) */
53 /* .iManufacturer = DYNAMIC */
54 /* .iProduct = DYNAMIC */
55 /* NO SERIAL NUMBER */
56 .bNumConfigurations
= 1,
59 static const struct usb_descriptor_header
*otg_desc
[2];
61 /* string IDs are assigned dynamically */
62 static struct usb_string strings_dev
[] = {
63 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
64 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
65 [USB_GADGET_SERIAL_IDX
].s
= "",
69 static struct usb_gadget_strings stringtab_dev
= {
70 .language
= 0x0409, /* en-us */
71 .strings
= strings_dev
,
74 static struct usb_gadget_strings
*dev_strings
[] = {
79 /*-------------------------------------------------------------------------*/
80 static struct usb_function
*f_acm
;
81 static struct usb_function_instance
*fi_serial
;
83 static struct usb_function
*f_ecm
;
84 static struct usb_function_instance
*fi_ecm
;
87 * We _always_ have both CDC ECM and CDC ACM functions.
89 static int cdc_do_config(struct usb_configuration
*c
)
93 if (gadget_is_otg(c
->cdev
->gadget
)) {
94 c
->descriptors
= otg_desc
;
95 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
98 f_ecm
= usb_get_function(fi_ecm
);
100 status
= PTR_ERR(f_ecm
);
104 status
= usb_add_function(c
, f_ecm
);
108 f_acm
= usb_get_function(fi_serial
);
110 status
= PTR_ERR(f_acm
);
114 status
= usb_add_function(c
, f_acm
);
120 usb_put_function(f_acm
);
122 usb_remove_function(c
, f_ecm
);
124 usb_put_function(f_ecm
);
129 static struct usb_configuration cdc_config_driver
= {
130 .label
= "CDC Composite (ECM + ACM)",
131 .bConfigurationValue
= 1,
132 /* .iConfiguration = DYNAMIC */
133 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
136 /*-------------------------------------------------------------------------*/
138 static int cdc_bind(struct usb_composite_dev
*cdev
)
140 struct usb_gadget
*gadget
= cdev
->gadget
;
141 struct f_ecm_opts
*ecm_opts
;
144 if (!can_support_ecm(cdev
->gadget
)) {
145 dev_err(&gadget
->dev
, "controller '%s' not usable\n",
150 fi_ecm
= usb_get_function_instance("ecm");
152 return PTR_ERR(fi_ecm
);
154 ecm_opts
= container_of(fi_ecm
, struct f_ecm_opts
, func_inst
);
156 gether_set_qmult(ecm_opts
->net
, qmult
);
157 if (!gether_set_host_addr(ecm_opts
->net
, host_addr
))
158 pr_info("using host ethernet address: %s", host_addr
);
159 if (!gether_set_dev_addr(ecm_opts
->net
, dev_addr
))
160 pr_info("using self ethernet address: %s", dev_addr
);
162 fi_serial
= usb_get_function_instance("acm");
163 if (IS_ERR(fi_serial
)) {
164 status
= PTR_ERR(fi_serial
);
168 /* Allocate string descriptor numbers ... note that string
169 * contents can be overridden by the composite_dev glue.
172 status
= usb_string_ids_tab(cdev
, strings_dev
);
175 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
176 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
178 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
179 struct usb_descriptor_header
*usb_desc
;
181 usb_desc
= usb_otg_descriptor_alloc(gadget
);
184 usb_otg_descriptor_init(gadget
, usb_desc
);
185 otg_desc
[0] = usb_desc
;
189 /* register our configuration */
190 status
= usb_add_config(cdev
, &cdc_config_driver
, cdc_do_config
);
194 usb_composite_overwrite_options(cdev
, &coverwrite
);
195 dev_info(&gadget
->dev
, "%s, version: " DRIVER_VERSION
"\n",
204 usb_put_function_instance(fi_serial
);
206 usb_put_function_instance(fi_ecm
);
210 static int cdc_unbind(struct usb_composite_dev
*cdev
)
212 usb_put_function(f_acm
);
213 usb_put_function_instance(fi_serial
);
214 if (!IS_ERR_OR_NULL(f_ecm
))
215 usb_put_function(f_ecm
);
216 if (!IS_ERR_OR_NULL(fi_ecm
))
217 usb_put_function_instance(fi_ecm
);
224 static struct usb_composite_driver cdc_driver
= {
227 .strings
= dev_strings
,
228 .max_speed
= USB_SPEED_HIGH
,
230 .unbind
= cdc_unbind
,
233 module_usb_composite_driver(cdc_driver
);
235 MODULE_DESCRIPTION(DRIVER_DESC
);
236 MODULE_AUTHOR("David Brownell");
237 MODULE_LICENSE("GPL");