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