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> | |
14 | #include <linux/utsname.h> | |
6eb0de82 | 15 | #include <linux/module.h> |
19e20680 DB |
16 | |
17 | #include "u_ether.h" | |
18 | #include "u_serial.h" | |
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 | ||
36 | /*-------------------------------------------------------------------------*/ | |
7d16e8d3 | 37 | USB_GADGET_COMPOSITE_OPTIONS(); |
19e20680 | 38 | |
8a1ce2c0 DB |
39 | /* |
40 | * Kbuild is not very cooperative with respect to linking separately | |
41 | * compiled library objects into one module. So for now we won't use | |
42 | * separate compilation ... ensuring init/exit sections work to shrink | |
43 | * the runtime footprint, and giving us at least some parts of what | |
44 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
45 | */ | |
46 | ||
47 | #include "composite.c" | |
8a1ce2c0 DB |
48 | #include "u_serial.c" |
49 | #include "f_acm.c" | |
50 | #include "f_ecm.c" | |
51 | #include "u_ether.c" | |
52 | ||
53 | /*-------------------------------------------------------------------------*/ | |
54 | ||
19e20680 DB |
55 | static struct usb_device_descriptor device_desc = { |
56 | .bLength = sizeof device_desc, | |
57 | .bDescriptorType = USB_DT_DEVICE, | |
58 | ||
551509d2 | 59 | .bcdUSB = cpu_to_le16(0x0200), |
19e20680 DB |
60 | |
61 | .bDeviceClass = USB_CLASS_COMM, | |
62 | .bDeviceSubClass = 0, | |
63 | .bDeviceProtocol = 0, | |
64 | /* .bMaxPacketSize0 = f(hardware) */ | |
65 | ||
66 | /* Vendor and product id can be overridden by module parameters. */ | |
551509d2 HH |
67 | .idVendor = cpu_to_le16(CDC_VENDOR_NUM), |
68 | .idProduct = cpu_to_le16(CDC_PRODUCT_NUM), | |
19e20680 DB |
69 | /* .bcdDevice = f(hardware) */ |
70 | /* .iManufacturer = DYNAMIC */ | |
71 | /* .iProduct = DYNAMIC */ | |
72 | /* NO SERIAL NUMBER */ | |
73 | .bNumConfigurations = 1, | |
74 | }; | |
75 | ||
76 | static struct usb_otg_descriptor otg_descriptor = { | |
77 | .bLength = sizeof otg_descriptor, | |
78 | .bDescriptorType = USB_DT_OTG, | |
79 | ||
80 | /* REVISIT SRP-only hardware is possible, although | |
81 | * it would not be called "OTG" ... | |
82 | */ | |
83 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
84 | }; | |
85 | ||
86 | static const struct usb_descriptor_header *otg_desc[] = { | |
87 | (struct usb_descriptor_header *) &otg_descriptor, | |
88 | NULL, | |
89 | }; | |
90 | ||
91 | ||
92 | /* string IDs are assigned dynamically */ | |
93 | ||
94 | #define STRING_MANUFACTURER_IDX 0 | |
95 | #define STRING_PRODUCT_IDX 1 | |
96 | ||
97 | static char manufacturer[50]; | |
98 | ||
99 | static struct usb_string strings_dev[] = { | |
100 | [STRING_MANUFACTURER_IDX].s = manufacturer, | |
101 | [STRING_PRODUCT_IDX].s = DRIVER_DESC, | |
102 | { } /* end of list */ | |
103 | }; | |
104 | ||
105 | static struct usb_gadget_strings stringtab_dev = { | |
106 | .language = 0x0409, /* en-us */ | |
107 | .strings = strings_dev, | |
108 | }; | |
109 | ||
110 | static struct usb_gadget_strings *dev_strings[] = { | |
111 | &stringtab_dev, | |
112 | NULL, | |
113 | }; | |
114 | ||
115 | static u8 hostaddr[ETH_ALEN]; | |
116 | ||
117 | /*-------------------------------------------------------------------------*/ | |
118 | ||
119 | /* | |
120 | * We _always_ have both CDC ECM and CDC ACM functions. | |
121 | */ | |
e12995ec | 122 | static int __init cdc_do_config(struct usb_configuration *c) |
19e20680 DB |
123 | { |
124 | int status; | |
125 | ||
126 | if (gadget_is_otg(c->cdev->gadget)) { | |
127 | c->descriptors = otg_desc; | |
128 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
129 | } | |
130 | ||
131 | status = ecm_bind_config(c, hostaddr); | |
132 | if (status < 0) | |
133 | return status; | |
134 | ||
135 | status = acm_bind_config(c, 0); | |
ac90e365 | 136 | if (status < 0) |
19e20680 DB |
137 | return status; |
138 | ||
139 | return 0; | |
140 | } | |
141 | ||
142 | static struct usb_configuration cdc_config_driver = { | |
143 | .label = "CDC Composite (ECM + ACM)", | |
19e20680 DB |
144 | .bConfigurationValue = 1, |
145 | /* .iConfiguration = DYNAMIC */ | |
146 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
19e20680 DB |
147 | }; |
148 | ||
149 | /*-------------------------------------------------------------------------*/ | |
150 | ||
e12995ec | 151 | static int __init cdc_bind(struct usb_composite_dev *cdev) |
19e20680 DB |
152 | { |
153 | int gcnum; | |
154 | struct usb_gadget *gadget = cdev->gadget; | |
155 | int status; | |
156 | ||
157 | if (!can_support_ecm(cdev->gadget)) { | |
8a1ce2c0 DB |
158 | dev_err(&gadget->dev, "controller '%s' not usable\n", |
159 | gadget->name); | |
19e20680 DB |
160 | return -EINVAL; |
161 | } | |
162 | ||
163 | /* set up network link layer */ | |
164 | status = gether_setup(cdev->gadget, hostaddr); | |
165 | if (status < 0) | |
166 | return status; | |
167 | ||
168 | /* set up serial link layer */ | |
169 | status = gserial_setup(cdev->gadget, 1); | |
170 | if (status < 0) | |
171 | goto fail0; | |
172 | ||
173 | gcnum = usb_gadget_controller_number(gadget); | |
174 | if (gcnum >= 0) | |
175 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); | |
176 | else { | |
177 | /* We assume that can_support_ecm() tells the truth; | |
178 | * but if the controller isn't recognized at all then | |
179 | * that assumption is a bit more likely to be wrong. | |
180 | */ | |
b6c63937 | 181 | WARNING(cdev, "controller '%s' not recognized; trying %s\n", |
19e20680 DB |
182 | gadget->name, |
183 | cdc_config_driver.label); | |
184 | device_desc.bcdDevice = | |
551509d2 | 185 | cpu_to_le16(0x0300 | 0x0099); |
19e20680 DB |
186 | } |
187 | ||
188 | ||
189 | /* Allocate string descriptor numbers ... note that string | |
190 | * contents can be overridden by the composite_dev glue. | |
191 | */ | |
192 | ||
193 | /* device descriptor strings: manufacturer, product */ | |
194 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | |
195 | init_utsname()->sysname, init_utsname()->release, | |
196 | gadget->name); | |
e1f15ccb | 197 | status = usb_string_ids_tab(cdev, strings_dev); |
19e20680 DB |
198 | if (status < 0) |
199 | goto fail1; | |
e1f15ccb SAS |
200 | device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id; |
201 | device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id; | |
19e20680 DB |
202 | |
203 | /* register our configuration */ | |
c9bfff9c | 204 | status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config); |
19e20680 DB |
205 | if (status < 0) |
206 | goto fail1; | |
207 | ||
7d16e8d3 | 208 | usb_composite_overwrite_options(cdev, &coverwrite); |
8a1ce2c0 DB |
209 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
210 | DRIVER_DESC); | |
19e20680 DB |
211 | |
212 | return 0; | |
213 | ||
214 | fail1: | |
215 | gserial_cleanup(); | |
216 | fail0: | |
217 | gether_cleanup(); | |
218 | return status; | |
219 | } | |
220 | ||
221 | static int __exit cdc_unbind(struct usb_composite_dev *cdev) | |
222 | { | |
223 | gserial_cleanup(); | |
224 | gether_cleanup(); | |
225 | return 0; | |
226 | } | |
227 | ||
c2ec75c2 | 228 | static __refdata 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, |
19e20680 DB |
234 | .unbind = __exit_p(cdc_unbind), |
235 | }; | |
236 | ||
237 | MODULE_DESCRIPTION(DRIVER_DESC); | |
238 | MODULE_AUTHOR("David Brownell"); | |
239 | MODULE_LICENSE("GPL"); | |
240 | ||
241 | static int __init init(void) | |
242 | { | |
03e42bd5 | 243 | return usb_composite_probe(&cdc_driver); |
19e20680 DB |
244 | } |
245 | module_init(init); | |
246 | ||
247 | static void __exit cleanup(void) | |
248 | { | |
249 | usb_composite_unregister(&cdc_driver); | |
250 | } | |
251 | module_exit(cleanup); |