1 // SPDX-License-Identifier: GPL-2.0+
3 * ncm.c -- NCM gadget driver
5 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
8 * The driver borrows from ether.c which is:
10 * Copyright (C) 2003-2005,2008 David Brownell
11 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
12 * Copyright (C) 2008 Nokia Corporation
16 /* #define VERBOSE_DEBUG */
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/usb/composite.h>
25 #define DRIVER_DESC "NCM Gadget"
27 /*-------------------------------------------------------------------------*/
29 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
30 * Instead: allocate your own, using normal USB-IF procedures.
33 /* Thanks to NetChip Technologies for donating this product ID.
34 * It's for devices with only CDC Ethernet configurations.
36 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
37 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
39 /*-------------------------------------------------------------------------*/
40 USB_GADGET_COMPOSITE_OPTIONS();
42 USB_ETHERNET_MODULE_PARAMETERS();
44 static struct usb_device_descriptor device_desc
= {
45 .bLength
= sizeof device_desc
,
46 .bDescriptorType
= USB_DT_DEVICE
,
48 /* .bcdUSB = DYNAMIC */
50 .bDeviceClass
= USB_CLASS_COMM
,
53 /* .bMaxPacketSize0 = f(hardware) */
55 /* Vendor and product id defaults change according to what configs
56 * we support. (As does bNumConfigurations.) These values can
57 * also be overridden by module parameters.
59 .idVendor
= cpu_to_le16 (CDC_VENDOR_NUM
),
60 .idProduct
= cpu_to_le16 (CDC_PRODUCT_NUM
),
61 /* .bcdDevice = f(hardware) */
62 /* .iManufacturer = DYNAMIC */
63 /* .iProduct = DYNAMIC */
64 /* NO SERIAL NUMBER */
65 .bNumConfigurations
= 1,
68 static const struct usb_descriptor_header
*otg_desc
[2];
70 /* string IDs are assigned dynamically */
71 static struct usb_string strings_dev
[] = {
72 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
73 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
74 [USB_GADGET_SERIAL_IDX
].s
= "",
78 static struct usb_gadget_strings stringtab_dev
= {
79 .language
= 0x0409, /* en-us */
80 .strings
= strings_dev
,
83 static struct usb_gadget_strings
*dev_strings
[] = {
88 static struct usb_function_instance
*f_ncm_inst
;
89 static struct usb_function
*f_ncm
;
91 /*-------------------------------------------------------------------------*/
93 static int ncm_do_config(struct usb_configuration
*c
)
97 /* FIXME alloc iConfiguration string, set it in c->strings */
99 if (gadget_is_otg(c
->cdev
->gadget
)) {
100 c
->descriptors
= otg_desc
;
101 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
104 f_ncm
= usb_get_function(f_ncm_inst
);
106 return PTR_ERR(f_ncm
);
108 status
= usb_add_function(c
, f_ncm
);
110 usb_put_function(f_ncm
);
117 static struct usb_configuration ncm_config_driver
= {
118 /* .label = f(hardware) */
119 .label
= "CDC Ethernet (NCM)",
120 .bConfigurationValue
= 1,
121 /* .iConfiguration = DYNAMIC */
122 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
125 /*-------------------------------------------------------------------------*/
127 static int gncm_bind(struct usb_composite_dev
*cdev
)
129 struct usb_gadget
*gadget
= cdev
->gadget
;
130 struct f_ncm_opts
*ncm_opts
;
133 f_ncm_inst
= usb_get_function_instance("ncm");
134 if (IS_ERR(f_ncm_inst
))
135 return PTR_ERR(f_ncm_inst
);
137 ncm_opts
= container_of(f_ncm_inst
, struct f_ncm_opts
, func_inst
);
139 gether_set_qmult(ncm_opts
->net
, qmult
);
140 if (!gether_set_host_addr(ncm_opts
->net
, host_addr
))
141 pr_info("using host ethernet address: %s", host_addr
);
142 if (!gether_set_dev_addr(ncm_opts
->net
, dev_addr
))
143 pr_info("using self ethernet address: %s", dev_addr
);
145 /* Allocate string descriptor numbers ... note that string
146 * contents can be overridden by the composite_dev glue.
149 status
= usb_string_ids_tab(cdev
, strings_dev
);
152 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
153 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
155 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
156 struct usb_descriptor_header
*usb_desc
;
158 usb_desc
= usb_otg_descriptor_alloc(gadget
);
161 usb_otg_descriptor_init(gadget
, usb_desc
);
162 otg_desc
[0] = usb_desc
;
166 status
= usb_add_config(cdev
, &ncm_config_driver
,
171 usb_composite_overwrite_options(cdev
, &coverwrite
);
172 dev_info(&gadget
->dev
, "%s\n", DRIVER_DESC
);
180 usb_put_function_instance(f_ncm_inst
);
184 static int gncm_unbind(struct usb_composite_dev
*cdev
)
186 if (!IS_ERR_OR_NULL(f_ncm
))
187 usb_put_function(f_ncm
);
188 if (!IS_ERR_OR_NULL(f_ncm_inst
))
189 usb_put_function_instance(f_ncm_inst
);
196 static struct usb_composite_driver ncm_driver
= {
199 .strings
= dev_strings
,
200 .max_speed
= USB_SPEED_SUPER
,
202 .unbind
= gncm_unbind
,
205 module_usb_composite_driver(ncm_driver
);
207 MODULE_DESCRIPTION(DRIVER_DESC
);
208 MODULE_AUTHOR("Yauheni Kaliuta");
209 MODULE_LICENSE("GPL");