]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/usb/gadget/legacy/cdc2.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / gadget / legacy / cdc2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * cdc2.c -- CDC Composite driver, with ECM and ACM support
4 *
5 * Copyright (C) 2008 David Brownell
6 * Copyright (C) 2008 Nokia Corporation
7 *
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.
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16
17 #include "u_ether.h"
18 #include "u_serial.h"
19 #include "u_ecm.h"
20
21
22 #define DRIVER_DESC "CDC Composite Gadget"
23 #define DRIVER_VERSION "King Kamehameha Day 2008"
24
25 /*-------------------------------------------------------------------------*/
26
27 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
28 * Instead: allocate your own, using normal USB-IF procedures.
29 */
30
31 /* Thanks to NetChip Technologies for donating this product ID.
32 * It's for devices with only this composite CDC configuration.
33 */
34 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
35 #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
36
37 USB_GADGET_COMPOSITE_OPTIONS();
38
39 USB_ETHERNET_MODULE_PARAMETERS();
40
41 /*-------------------------------------------------------------------------*/
42
43 static struct usb_device_descriptor device_desc = {
44 .bLength = sizeof device_desc,
45 .bDescriptorType = USB_DT_DEVICE,
46
47 /* .bcdUSB = DYNAMIC */
48
49 .bDeviceClass = USB_CLASS_COMM,
50 .bDeviceSubClass = 0,
51 .bDeviceProtocol = 0,
52 /* .bMaxPacketSize0 = f(hardware) */
53
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,
62 };
63
64 static const struct usb_descriptor_header *otg_desc[2];
65
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 = "",
71 { } /* end of list */
72 };
73
74 static struct usb_gadget_strings stringtab_dev = {
75 .language = 0x0409, /* en-us */
76 .strings = strings_dev,
77 };
78
79 static struct usb_gadget_strings *dev_strings[] = {
80 &stringtab_dev,
81 NULL,
82 };
83
84 /*-------------------------------------------------------------------------*/
85 static struct usb_function *f_acm;
86 static struct usb_function_instance *fi_serial;
87
88 static struct usb_function *f_ecm;
89 static struct usb_function_instance *fi_ecm;
90
91 /*
92 * We _always_ have both CDC ECM and CDC ACM functions.
93 */
94 static int cdc_do_config(struct usb_configuration *c)
95 {
96 int status;
97
98 if (gadget_is_otg(c->cdev->gadget)) {
99 c->descriptors = otg_desc;
100 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
101 }
102
103 f_ecm = usb_get_function(fi_ecm);
104 if (IS_ERR(f_ecm)) {
105 status = PTR_ERR(f_ecm);
106 goto err_get_ecm;
107 }
108
109 status = usb_add_function(c, f_ecm);
110 if (status)
111 goto err_add_ecm;
112
113 f_acm = usb_get_function(fi_serial);
114 if (IS_ERR(f_acm)) {
115 status = PTR_ERR(f_acm);
116 goto err_get_acm;
117 }
118
119 status = usb_add_function(c, f_acm);
120 if (status)
121 goto err_add_acm;
122 return 0;
123
124 err_add_acm:
125 usb_put_function(f_acm);
126 err_get_acm:
127 usb_remove_function(c, f_ecm);
128 err_add_ecm:
129 usb_put_function(f_ecm);
130 err_get_ecm:
131 return status;
132 }
133
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,
139 };
140
141 /*-------------------------------------------------------------------------*/
142
143 static int cdc_bind(struct usb_composite_dev *cdev)
144 {
145 struct usb_gadget *gadget = cdev->gadget;
146 struct f_ecm_opts *ecm_opts;
147 int status;
148
149 if (!can_support_ecm(cdev->gadget)) {
150 dev_err(&gadget->dev, "controller '%s' not usable\n",
151 gadget->name);
152 return -EINVAL;
153 }
154
155 fi_ecm = usb_get_function_instance("ecm");
156 if (IS_ERR(fi_ecm))
157 return PTR_ERR(fi_ecm);
158
159 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
160
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);
166
167 fi_serial = usb_get_function_instance("acm");
168 if (IS_ERR(fi_serial)) {
169 status = PTR_ERR(fi_serial);
170 goto fail;
171 }
172
173 /* Allocate string descriptor numbers ... note that string
174 * contents can be overridden by the composite_dev glue.
175 */
176
177 status = usb_string_ids_tab(cdev, strings_dev);
178 if (status < 0)
179 goto fail1;
180 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
181 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
182
183 if (gadget_is_otg(gadget) && !otg_desc[0]) {
184 struct usb_descriptor_header *usb_desc;
185
186 usb_desc = usb_otg_descriptor_alloc(gadget);
187 if (!usb_desc)
188 goto fail1;
189 usb_otg_descriptor_init(gadget, usb_desc);
190 otg_desc[0] = usb_desc;
191 otg_desc[1] = NULL;
192 }
193
194 /* register our configuration */
195 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
196 if (status < 0)
197 goto fail2;
198
199 usb_composite_overwrite_options(cdev, &coverwrite);
200 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
201 DRIVER_DESC);
202
203 return 0;
204
205 fail2:
206 kfree(otg_desc[0]);
207 otg_desc[0] = NULL;
208 fail1:
209 usb_put_function_instance(fi_serial);
210 fail:
211 usb_put_function_instance(fi_ecm);
212 return status;
213 }
214
215 static int cdc_unbind(struct usb_composite_dev *cdev)
216 {
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);
223 kfree(otg_desc[0]);
224 otg_desc[0] = NULL;
225
226 return 0;
227 }
228
229 static struct usb_composite_driver cdc_driver = {
230 .name = "g_cdc",
231 .dev = &device_desc,
232 .strings = dev_strings,
233 .max_speed = USB_SPEED_HIGH,
234 .bind = cdc_bind,
235 .unbind = cdc_unbind,
236 };
237
238 module_usb_composite_driver(cdc_driver);
239
240 MODULE_DESCRIPTION(DRIVER_DESC);
241 MODULE_AUTHOR("David Brownell");
242 MODULE_LICENSE("GPL");