]>
Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0+ |
19e20680 DB |
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. | |
19e20680 DB |
12 | */ |
13 | ||
14 | #include <linux/kernel.h> | |
6eb0de82 | 15 | #include <linux/module.h> |
19e20680 DB |
16 | |
17 | #include "u_ether.h" | |
18 | #include "u_serial.h" | |
a38a2750 | 19 | #include "u_ecm.h" |
19e20680 DB |
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 | ||
7d16e8d3 | 37 | USB_GADGET_COMPOSITE_OPTIONS(); |
19e20680 | 38 | |
f1a1823f AP |
39 | USB_ETHERNET_MODULE_PARAMETERS(); |
40 | ||
8a1ce2c0 DB |
41 | /*-------------------------------------------------------------------------*/ |
42 | ||
19e20680 DB |
43 | static struct usb_device_descriptor device_desc = { |
44 | .bLength = sizeof device_desc, | |
45 | .bDescriptorType = USB_DT_DEVICE, | |
46 | ||
0aecfc1b | 47 | /* .bcdUSB = DYNAMIC */ |
19e20680 DB |
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. */ | |
551509d2 HH |
55 | .idVendor = cpu_to_le16(CDC_VENDOR_NUM), |
56 | .idProduct = cpu_to_le16(CDC_PRODUCT_NUM), | |
19e20680 DB |
57 | /* .bcdDevice = f(hardware) */ |
58 | /* .iManufacturer = DYNAMIC */ | |
59 | /* .iProduct = DYNAMIC */ | |
60 | /* NO SERIAL NUMBER */ | |
61 | .bNumConfigurations = 1, | |
62 | }; | |
63 | ||
ab6796ae | 64 | static const struct usb_descriptor_header *otg_desc[2]; |
19e20680 DB |
65 | |
66 | /* string IDs are assigned dynamically */ | |
19e20680 | 67 | static struct usb_string strings_dev[] = { |
cc2683c3 | 68 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
276e2e4f SAS |
69 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
70 | [USB_GADGET_SERIAL_IDX].s = "", | |
19e20680 DB |
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 | ||
19e20680 | 84 | /*-------------------------------------------------------------------------*/ |
29a6645f SAS |
85 | static struct usb_function *f_acm; |
86 | static struct usb_function_instance *fi_serial; | |
19e20680 | 87 | |
a38a2750 AP |
88 | static struct usb_function *f_ecm; |
89 | static struct usb_function_instance *fi_ecm; | |
90 | ||
19e20680 DB |
91 | /* |
92 | * We _always_ have both CDC ECM and CDC ACM functions. | |
93 | */ | |
c94e289f | 94 | static int cdc_do_config(struct usb_configuration *c) |
19e20680 DB |
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 | ||
a38a2750 AP |
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; | |
19e20680 | 112 | |
29a6645f | 113 | f_acm = usb_get_function(fi_serial); |
8d6f51bd WY |
114 | if (IS_ERR(f_acm)) { |
115 | status = PTR_ERR(f_acm); | |
d2ff4059 | 116 | goto err_get_acm; |
8d6f51bd | 117 | } |
29a6645f SAS |
118 | |
119 | status = usb_add_function(c, f_acm); | |
120 | if (status) | |
a38a2750 | 121 | goto err_add_acm; |
19e20680 | 122 | return 0; |
a38a2750 AP |
123 | |
124 | err_add_acm: | |
29a6645f | 125 | usb_put_function(f_acm); |
a38a2750 AP |
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: | |
29a6645f | 131 | return status; |
19e20680 DB |
132 | } |
133 | ||
134 | static struct usb_configuration cdc_config_driver = { | |
135 | .label = "CDC Composite (ECM + ACM)", | |
19e20680 DB |
136 | .bConfigurationValue = 1, |
137 | /* .iConfiguration = DYNAMIC */ | |
138 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
19e20680 DB |
139 | }; |
140 | ||
141 | /*-------------------------------------------------------------------------*/ | |
142 | ||
c94e289f | 143 | static int cdc_bind(struct usb_composite_dev *cdev) |
19e20680 | 144 | { |
19e20680 | 145 | struct usb_gadget *gadget = cdev->gadget; |
a38a2750 | 146 | struct f_ecm_opts *ecm_opts; |
19e20680 DB |
147 | int status; |
148 | ||
149 | if (!can_support_ecm(cdev->gadget)) { | |
8a1ce2c0 DB |
150 | dev_err(&gadget->dev, "controller '%s' not usable\n", |
151 | gadget->name); | |
19e20680 DB |
152 | return -EINVAL; |
153 | } | |
154 | ||
a38a2750 AP |
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 | } | |
19e20680 | 172 | |
19e20680 DB |
173 | /* Allocate string descriptor numbers ... note that string |
174 | * contents can be overridden by the composite_dev glue. | |
175 | */ | |
176 | ||
e1f15ccb | 177 | status = usb_string_ids_tab(cdev, strings_dev); |
19e20680 DB |
178 | if (status < 0) |
179 | goto fail1; | |
276e2e4f SAS |
180 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
181 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
19e20680 | 182 | |
ab6796ae LJ |
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 | ||
19e20680 | 194 | /* register our configuration */ |
c9bfff9c | 195 | status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config); |
19e20680 | 196 | if (status < 0) |
ab6796ae | 197 | goto fail2; |
19e20680 | 198 | |
7d16e8d3 | 199 | usb_composite_overwrite_options(cdev, &coverwrite); |
8a1ce2c0 DB |
200 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
201 | DRIVER_DESC); | |
19e20680 DB |
202 | |
203 | return 0; | |
204 | ||
ab6796ae LJ |
205 | fail2: |
206 | kfree(otg_desc[0]); | |
207 | otg_desc[0] = NULL; | |
19e20680 | 208 | fail1: |
a38a2750 AP |
209 | usb_put_function_instance(fi_serial); |
210 | fail: | |
211 | usb_put_function_instance(fi_ecm); | |
19e20680 DB |
212 | return status; |
213 | } | |
214 | ||
c94e289f | 215 | static int cdc_unbind(struct usb_composite_dev *cdev) |
19e20680 | 216 | { |
29a6645f SAS |
217 | usb_put_function(f_acm); |
218 | usb_put_function_instance(fi_serial); | |
a38a2750 AP |
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); | |
ab6796ae LJ |
223 | kfree(otg_desc[0]); |
224 | otg_desc[0] = NULL; | |
225 | ||
19e20680 DB |
226 | return 0; |
227 | } | |
228 | ||
c94e289f | 229 | static struct usb_composite_driver cdc_driver = { |
19e20680 DB |
230 | .name = "g_cdc", |
231 | .dev = &device_desc, | |
232 | .strings = dev_strings, | |
35a0e0bf | 233 | .max_speed = USB_SPEED_HIGH, |
03e42bd5 | 234 | .bind = cdc_bind, |
c94e289f | 235 | .unbind = cdc_unbind, |
19e20680 DB |
236 | }; |
237 | ||
909346a8 TK |
238 | module_usb_composite_driver(cdc_driver); |
239 | ||
19e20680 DB |
240 | MODULE_DESCRIPTION(DRIVER_DESC); |
241 | MODULE_AUTHOR("David Brownell"); | |
242 | MODULE_LICENSE("GPL"); |