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
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
22 #define DRIVER_DESC "CDC Composite Gadget"
23 #define DRIVER_VERSION "King Kamehameha Day 2008"
25 /*-------------------------------------------------------------------------*/
27 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
28 * Instead: allocate your own, using normal USB-IF procedures.
31 /* Thanks to NetChip Technologies for donating this product ID.
32 * It's for devices with only this composite CDC configuration.
34 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
35 #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
37 USB_GADGET_COMPOSITE_OPTIONS();
39 USB_ETHERNET_MODULE_PARAMETERS();
41 /*-------------------------------------------------------------------------*/
43 static struct usb_device_descriptor device_desc
= {
44 .bLength
= sizeof device_desc
,
45 .bDescriptorType
= USB_DT_DEVICE
,
47 /* .bcdUSB = DYNAMIC */
49 .bDeviceClass
= USB_CLASS_COMM
,
52 /* .bMaxPacketSize0 = f(hardware) */
54 /* Vendor and product id can be overridden by module parameters. */
55 .idVendor
= cpu_to_le16(CDC_VENDOR_NUM
),
56 .idProduct
= cpu_to_le16(CDC_PRODUCT_NUM
),
57 /* .bcdDevice = f(hardware) */
58 /* .iManufacturer = DYNAMIC */
59 /* .iProduct = DYNAMIC */
60 /* NO SERIAL NUMBER */
61 .bNumConfigurations
= 1,
64 static const struct usb_descriptor_header
*otg_desc
[2];
66 /* string IDs are assigned dynamically */
67 static struct usb_string strings_dev
[] = {
68 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
69 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
70 [USB_GADGET_SERIAL_IDX
].s
= "",
74 static struct usb_gadget_strings stringtab_dev
= {
75 .language
= 0x0409, /* en-us */
76 .strings
= strings_dev
,
79 static struct usb_gadget_strings
*dev_strings
[] = {
84 /*-------------------------------------------------------------------------*/
85 static struct usb_function
*f_acm
;
86 static struct usb_function_instance
*fi_serial
;
88 static struct usb_function
*f_ecm
;
89 static struct usb_function_instance
*fi_ecm
;
92 * We _always_ have both CDC ECM and CDC ACM functions.
94 static int cdc_do_config(struct usb_configuration
*c
)
98 if (gadget_is_otg(c
->cdev
->gadget
)) {
99 c
->descriptors
= otg_desc
;
100 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
103 f_ecm
= usb_get_function(fi_ecm
);
105 status
= PTR_ERR(f_ecm
);
109 status
= usb_add_function(c
, f_ecm
);
113 f_acm
= usb_get_function(fi_serial
);
115 status
= PTR_ERR(f_acm
);
119 status
= usb_add_function(c
, f_acm
);
125 usb_put_function(f_acm
);
127 usb_remove_function(c
, f_ecm
);
129 usb_put_function(f_ecm
);
134 static struct usb_configuration cdc_config_driver
= {
135 .label
= "CDC Composite (ECM + ACM)",
136 .bConfigurationValue
= 1,
137 /* .iConfiguration = DYNAMIC */
138 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
141 /*-------------------------------------------------------------------------*/
143 static int cdc_bind(struct usb_composite_dev
*cdev
)
145 struct usb_gadget
*gadget
= cdev
->gadget
;
146 struct f_ecm_opts
*ecm_opts
;
149 if (!can_support_ecm(cdev
->gadget
)) {
150 dev_err(&gadget
->dev
, "controller '%s' not usable\n",
155 fi_ecm
= usb_get_function_instance("ecm");
157 return PTR_ERR(fi_ecm
);
159 ecm_opts
= container_of(fi_ecm
, struct f_ecm_opts
, func_inst
);
161 gether_set_qmult(ecm_opts
->net
, qmult
);
162 if (!gether_set_host_addr(ecm_opts
->net
, host_addr
))
163 pr_info("using host ethernet address: %s", host_addr
);
164 if (!gether_set_dev_addr(ecm_opts
->net
, dev_addr
))
165 pr_info("using self ethernet address: %s", dev_addr
);
167 fi_serial
= usb_get_function_instance("acm");
168 if (IS_ERR(fi_serial
)) {
169 status
= PTR_ERR(fi_serial
);
173 /* Allocate string descriptor numbers ... note that string
174 * contents can be overridden by the composite_dev glue.
177 status
= usb_string_ids_tab(cdev
, strings_dev
);
180 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
181 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
183 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
184 struct usb_descriptor_header
*usb_desc
;
186 usb_desc
= usb_otg_descriptor_alloc(gadget
);
189 usb_otg_descriptor_init(gadget
, usb_desc
);
190 otg_desc
[0] = usb_desc
;
194 /* register our configuration */
195 status
= usb_add_config(cdev
, &cdc_config_driver
, cdc_do_config
);
199 usb_composite_overwrite_options(cdev
, &coverwrite
);
200 dev_info(&gadget
->dev
, "%s, version: " DRIVER_VERSION
"\n",
209 usb_put_function_instance(fi_serial
);
211 usb_put_function_instance(fi_ecm
);
215 static int cdc_unbind(struct usb_composite_dev
*cdev
)
217 usb_put_function(f_acm
);
218 usb_put_function_instance(fi_serial
);
219 if (!IS_ERR_OR_NULL(f_ecm
))
220 usb_put_function(f_ecm
);
221 if (!IS_ERR_OR_NULL(fi_ecm
))
222 usb_put_function_instance(fi_ecm
);
229 static struct usb_composite_driver cdc_driver
= {
232 .strings
= dev_strings
,
233 .max_speed
= USB_SPEED_HIGH
,
235 .unbind
= cdc_unbind
,
238 module_usb_composite_driver(cdc_driver
);
240 MODULE_DESCRIPTION(DRIVER_DESC
);
241 MODULE_AUTHOR("David Brownell");
242 MODULE_LICENSE("GPL");