Commit | Line | Data |
---|---|---|
19e20680 DB |
1 | /* |
2 | * cdc2.c -- CDC Composite driver, with ECM and ACM support | |
3 | * | |
4 | * Copyright (C) 2008 David Brownell | |
5 | * Copyright (C) 2008 Nokia Corporation | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
19e20680 DB |
11 | */ |
12 | ||
13 | #include <linux/kernel.h> | |
6eb0de82 | 14 | #include <linux/module.h> |
19e20680 DB |
15 | |
16 | #include "u_ether.h" | |
17 | #include "u_serial.h" | |
a38a2750 | 18 | #include "u_ecm.h" |
19e20680 DB |
19 | |
20 | ||
21 | #define DRIVER_DESC "CDC Composite Gadget" | |
22 | #define DRIVER_VERSION "King Kamehameha Day 2008" | |
23 | ||
24 | /*-------------------------------------------------------------------------*/ | |
25 | ||
26 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
27 | * Instead: allocate your own, using normal USB-IF procedures. | |
28 | */ | |
29 | ||
30 | /* Thanks to NetChip Technologies for donating this product ID. | |
31 | * It's for devices with only this composite CDC configuration. | |
32 | */ | |
33 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ | |
34 | #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */ | |
35 | ||
7d16e8d3 | 36 | USB_GADGET_COMPOSITE_OPTIONS(); |
19e20680 | 37 | |
f1a1823f AP |
38 | USB_ETHERNET_MODULE_PARAMETERS(); |
39 | ||
8a1ce2c0 DB |
40 | /*-------------------------------------------------------------------------*/ |
41 | ||
19e20680 DB |
42 | static struct usb_device_descriptor device_desc = { |
43 | .bLength = sizeof device_desc, | |
44 | .bDescriptorType = USB_DT_DEVICE, | |
45 | ||
0aecfc1b | 46 | /* .bcdUSB = DYNAMIC */ |
19e20680 DB |
47 | |
48 | .bDeviceClass = USB_CLASS_COMM, | |
49 | .bDeviceSubClass = 0, | |
50 | .bDeviceProtocol = 0, | |
51 | /* .bMaxPacketSize0 = f(hardware) */ | |
52 | ||
53 | /* Vendor and product id can be overridden by module parameters. */ | |
551509d2 HH |
54 | .idVendor = cpu_to_le16(CDC_VENDOR_NUM), |
55 | .idProduct = cpu_to_le16(CDC_PRODUCT_NUM), | |
19e20680 DB |
56 | /* .bcdDevice = f(hardware) */ |
57 | /* .iManufacturer = DYNAMIC */ | |
58 | /* .iProduct = DYNAMIC */ | |
59 | /* NO SERIAL NUMBER */ | |
60 | .bNumConfigurations = 1, | |
61 | }; | |
62 | ||
ab6796ae | 63 | static const struct usb_descriptor_header *otg_desc[2]; |
19e20680 DB |
64 | |
65 | /* string IDs are assigned dynamically */ | |
19e20680 | 66 | static struct usb_string strings_dev[] = { |
cc2683c3 | 67 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
276e2e4f SAS |
68 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
69 | [USB_GADGET_SERIAL_IDX].s = "", | |
19e20680 DB |
70 | { } /* end of list */ |
71 | }; | |
72 | ||
73 | static struct usb_gadget_strings stringtab_dev = { | |
74 | .language = 0x0409, /* en-us */ | |
75 | .strings = strings_dev, | |
76 | }; | |
77 | ||
78 | static struct usb_gadget_strings *dev_strings[] = { | |
79 | &stringtab_dev, | |
80 | NULL, | |
81 | }; | |
82 | ||
19e20680 | 83 | /*-------------------------------------------------------------------------*/ |
29a6645f SAS |
84 | static struct usb_function *f_acm; |
85 | static struct usb_function_instance *fi_serial; | |
19e20680 | 86 | |
a38a2750 AP |
87 | static struct usb_function *f_ecm; |
88 | static struct usb_function_instance *fi_ecm; | |
89 | ||
19e20680 DB |
90 | /* |
91 | * We _always_ have both CDC ECM and CDC ACM functions. | |
92 | */ | |
c94e289f | 93 | static int cdc_do_config(struct usb_configuration *c) |
19e20680 DB |
94 | { |
95 | int status; | |
96 | ||
97 | if (gadget_is_otg(c->cdev->gadget)) { | |
98 | c->descriptors = otg_desc; | |
99 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
100 | } | |
101 | ||
a38a2750 AP |
102 | f_ecm = usb_get_function(fi_ecm); |
103 | if (IS_ERR(f_ecm)) { | |
104 | status = PTR_ERR(f_ecm); | |
105 | goto err_get_ecm; | |
106 | } | |
107 | ||
108 | status = usb_add_function(c, f_ecm); | |
109 | if (status) | |
110 | goto err_add_ecm; | |
19e20680 | 111 | |
29a6645f | 112 | f_acm = usb_get_function(fi_serial); |
8d6f51bd WY |
113 | if (IS_ERR(f_acm)) { |
114 | status = PTR_ERR(f_acm); | |
d2ff4059 | 115 | goto err_get_acm; |
8d6f51bd | 116 | } |
29a6645f SAS |
117 | |
118 | status = usb_add_function(c, f_acm); | |
119 | if (status) | |
a38a2750 | 120 | goto err_add_acm; |
19e20680 | 121 | return 0; |
a38a2750 AP |
122 | |
123 | err_add_acm: | |
29a6645f | 124 | usb_put_function(f_acm); |
a38a2750 AP |
125 | err_get_acm: |
126 | usb_remove_function(c, f_ecm); | |
127 | err_add_ecm: | |
128 | usb_put_function(f_ecm); | |
129 | err_get_ecm: | |
29a6645f | 130 | return status; |
19e20680 DB |
131 | } |
132 | ||
133 | static struct usb_configuration cdc_config_driver = { | |
134 | .label = "CDC Composite (ECM + ACM)", | |
19e20680 DB |
135 | .bConfigurationValue = 1, |
136 | /* .iConfiguration = DYNAMIC */ | |
137 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
19e20680 DB |
138 | }; |
139 | ||
140 | /*-------------------------------------------------------------------------*/ | |
141 | ||
c94e289f | 142 | static int cdc_bind(struct usb_composite_dev *cdev) |
19e20680 | 143 | { |
19e20680 | 144 | struct usb_gadget *gadget = cdev->gadget; |
a38a2750 | 145 | struct f_ecm_opts *ecm_opts; |
19e20680 DB |
146 | int status; |
147 | ||
148 | if (!can_support_ecm(cdev->gadget)) { | |
8a1ce2c0 DB |
149 | dev_err(&gadget->dev, "controller '%s' not usable\n", |
150 | gadget->name); | |
19e20680 DB |
151 | return -EINVAL; |
152 | } | |
153 | ||
a38a2750 AP |
154 | fi_ecm = usb_get_function_instance("ecm"); |
155 | if (IS_ERR(fi_ecm)) | |
156 | return PTR_ERR(fi_ecm); | |
157 | ||
158 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); | |
159 | ||
160 | gether_set_qmult(ecm_opts->net, qmult); | |
161 | if (!gether_set_host_addr(ecm_opts->net, host_addr)) | |
162 | pr_info("using host ethernet address: %s", host_addr); | |
163 | if (!gether_set_dev_addr(ecm_opts->net, dev_addr)) | |
164 | pr_info("using self ethernet address: %s", dev_addr); | |
165 | ||
166 | fi_serial = usb_get_function_instance("acm"); | |
167 | if (IS_ERR(fi_serial)) { | |
168 | status = PTR_ERR(fi_serial); | |
169 | goto fail; | |
170 | } | |
19e20680 | 171 | |
19e20680 DB |
172 | /* Allocate string descriptor numbers ... note that string |
173 | * contents can be overridden by the composite_dev glue. | |
174 | */ | |
175 | ||
e1f15ccb | 176 | status = usb_string_ids_tab(cdev, strings_dev); |
19e20680 DB |
177 | if (status < 0) |
178 | goto fail1; | |
276e2e4f SAS |
179 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
180 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
19e20680 | 181 | |
ab6796ae LJ |
182 | if (gadget_is_otg(gadget) && !otg_desc[0]) { |
183 | struct usb_descriptor_header *usb_desc; | |
184 | ||
185 | usb_desc = usb_otg_descriptor_alloc(gadget); | |
186 | if (!usb_desc) | |
187 | goto fail1; | |
188 | usb_otg_descriptor_init(gadget, usb_desc); | |
189 | otg_desc[0] = usb_desc; | |
190 | otg_desc[1] = NULL; | |
191 | } | |
192 | ||
19e20680 | 193 | /* register our configuration */ |
c9bfff9c | 194 | status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config); |
19e20680 | 195 | if (status < 0) |
ab6796ae | 196 | goto fail2; |
19e20680 | 197 | |
7d16e8d3 | 198 | usb_composite_overwrite_options(cdev, &coverwrite); |
8a1ce2c0 DB |
199 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
200 | DRIVER_DESC); | |
19e20680 DB |
201 | |
202 | return 0; | |
203 | ||
ab6796ae LJ |
204 | fail2: |
205 | kfree(otg_desc[0]); | |
206 | otg_desc[0] = NULL; | |
19e20680 | 207 | fail1: |
a38a2750 AP |
208 | usb_put_function_instance(fi_serial); |
209 | fail: | |
210 | usb_put_function_instance(fi_ecm); | |
19e20680 DB |
211 | return status; |
212 | } | |
213 | ||
c94e289f | 214 | static int cdc_unbind(struct usb_composite_dev *cdev) |
19e20680 | 215 | { |
29a6645f SAS |
216 | usb_put_function(f_acm); |
217 | usb_put_function_instance(fi_serial); | |
a38a2750 AP |
218 | if (!IS_ERR_OR_NULL(f_ecm)) |
219 | usb_put_function(f_ecm); | |
220 | if (!IS_ERR_OR_NULL(fi_ecm)) | |
221 | usb_put_function_instance(fi_ecm); | |
ab6796ae LJ |
222 | kfree(otg_desc[0]); |
223 | otg_desc[0] = NULL; | |
224 | ||
19e20680 DB |
225 | return 0; |
226 | } | |
227 | ||
c94e289f | 228 | static struct usb_composite_driver cdc_driver = { |
19e20680 DB |
229 | .name = "g_cdc", |
230 | .dev = &device_desc, | |
231 | .strings = dev_strings, | |
35a0e0bf | 232 | .max_speed = USB_SPEED_HIGH, |
03e42bd5 | 233 | .bind = cdc_bind, |
c94e289f | 234 | .unbind = cdc_unbind, |
19e20680 DB |
235 | }; |
236 | ||
909346a8 TK |
237 | module_usb_composite_driver(cdc_driver); |
238 | ||
19e20680 DB |
239 | MODULE_DESCRIPTION(DRIVER_DESC); |
240 | MODULE_AUTHOR("David Brownell"); | |
241 | MODULE_LICENSE("GPL"); |