blob: 8966bdec15349469a11a70aaaa080ab3f820934d [file] [log] [blame]
David Brownell19e20682008-06-19 18:20:26 -07001/*
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.
David Brownell19e20682008-06-19 18:20:26 -070011 */
12
13#include <linux/kernel.h>
14#include <linux/utsname.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040015#include <linux/module.h>
David Brownell19e20682008-06-19 18:20:26 -070016
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/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020037USB_GADGET_COMPOSITE_OPTIONS();
David Brownell19e20682008-06-19 18:20:26 -070038
David Brownell8a1ce2c2008-08-18 17:43:56 -070039/*
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"
David Brownell8a1ce2c2008-08-18 17:43:56 -070048#include "u_serial.c"
49#include "f_acm.c"
50#include "f_ecm.c"
51#include "u_ether.c"
52
53/*-------------------------------------------------------------------------*/
54
David Brownell19e20682008-06-19 18:20:26 -070055static struct usb_device_descriptor device_desc = {
56 .bLength = sizeof device_desc,
57 .bDescriptorType = USB_DT_DEVICE,
58
Harvey Harrison551509d2009-02-11 14:11:36 -080059 .bcdUSB = cpu_to_le16(0x0200),
David Brownell19e20682008-06-19 18:20:26 -070060
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. */
Harvey Harrison551509d2009-02-11 14:11:36 -080067 .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
68 .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
David Brownell19e20682008-06-19 18:20:26 -070069 /* .bcdDevice = f(hardware) */
70 /* .iManufacturer = DYNAMIC */
71 /* .iProduct = DYNAMIC */
72 /* NO SERIAL NUMBER */
73 .bNumConfigurations = 1,
74};
75
76static 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
86static 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 */
David Brownell19e20682008-06-19 18:20:26 -070093static char manufacturer[50];
94
95static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020096 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer,
97 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
98 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell19e20682008-06-19 18:20:26 -070099 { } /* end of list */
100};
101
102static struct usb_gadget_strings stringtab_dev = {
103 .language = 0x0409, /* en-us */
104 .strings = strings_dev,
105};
106
107static struct usb_gadget_strings *dev_strings[] = {
108 &stringtab_dev,
109 NULL,
110};
111
112static u8 hostaddr[ETH_ALEN];
113
114/*-------------------------------------------------------------------------*/
115
116/*
117 * We _always_ have both CDC ECM and CDC ACM functions.
118 */
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200119static int __init cdc_do_config(struct usb_configuration *c)
David Brownell19e20682008-06-19 18:20:26 -0700120{
121 int status;
122
123 if (gadget_is_otg(c->cdev->gadget)) {
124 c->descriptors = otg_desc;
125 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
126 }
127
128 status = ecm_bind_config(c, hostaddr);
129 if (status < 0)
130 return status;
131
132 status = acm_bind_config(c, 0);
David Brownellac90e362008-07-01 13:18:20 -0700133 if (status < 0)
David Brownell19e20682008-06-19 18:20:26 -0700134 return status;
135
136 return 0;
137}
138
139static struct usb_configuration cdc_config_driver = {
140 .label = "CDC Composite (ECM + ACM)",
David Brownell19e20682008-06-19 18:20:26 -0700141 .bConfigurationValue = 1,
142 /* .iConfiguration = DYNAMIC */
143 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell19e20682008-06-19 18:20:26 -0700144};
145
146/*-------------------------------------------------------------------------*/
147
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200148static int __init cdc_bind(struct usb_composite_dev *cdev)
David Brownell19e20682008-06-19 18:20:26 -0700149{
150 int gcnum;
151 struct usb_gadget *gadget = cdev->gadget;
152 int status;
153
154 if (!can_support_ecm(cdev->gadget)) {
David Brownell8a1ce2c2008-08-18 17:43:56 -0700155 dev_err(&gadget->dev, "controller '%s' not usable\n",
156 gadget->name);
David Brownell19e20682008-06-19 18:20:26 -0700157 return -EINVAL;
158 }
159
160 /* set up network link layer */
161 status = gether_setup(cdev->gadget, hostaddr);
162 if (status < 0)
163 return status;
164
165 /* set up serial link layer */
166 status = gserial_setup(cdev->gadget, 1);
167 if (status < 0)
168 goto fail0;
169
170 gcnum = usb_gadget_controller_number(gadget);
171 if (gcnum >= 0)
172 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
173 else {
174 /* We assume that can_support_ecm() tells the truth;
175 * but if the controller isn't recognized at all then
176 * that assumption is a bit more likely to be wrong.
177 */
Arjan van de Venb6c63932008-07-25 01:45:52 -0700178 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
David Brownell19e20682008-06-19 18:20:26 -0700179 gadget->name,
180 cdc_config_driver.label);
181 device_desc.bcdDevice =
Harvey Harrison551509d2009-02-11 14:11:36 -0800182 cpu_to_le16(0x0300 | 0x0099);
David Brownell19e20682008-06-19 18:20:26 -0700183 }
184
185
186 /* Allocate string descriptor numbers ... note that string
187 * contents can be overridden by the composite_dev glue.
188 */
189
190 /* device descriptor strings: manufacturer, product */
191 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
192 init_utsname()->sysname, init_utsname()->release,
193 gadget->name);
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200194 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell19e20682008-06-19 18:20:26 -0700195 if (status < 0)
196 goto fail1;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200197 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
198 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
David Brownell19e20682008-06-19 18:20:26 -0700199
200 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200201 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
David Brownell19e20682008-06-19 18:20:26 -0700202 if (status < 0)
203 goto fail1;
204
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200205 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell8a1ce2c2008-08-18 17:43:56 -0700206 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
207 DRIVER_DESC);
David Brownell19e20682008-06-19 18:20:26 -0700208
209 return 0;
210
211fail1:
212 gserial_cleanup();
213fail0:
214 gether_cleanup();
215 return status;
216}
217
218static int __exit cdc_unbind(struct usb_composite_dev *cdev)
219{
220 gserial_cleanup();
221 gether_cleanup();
222 return 0;
223}
224
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200225static __refdata struct usb_composite_driver cdc_driver = {
David Brownell19e20682008-06-19 18:20:26 -0700226 .name = "g_cdc",
227 .dev = &device_desc,
228 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300229 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200230 .bind = cdc_bind,
David Brownell19e20682008-06-19 18:20:26 -0700231 .unbind = __exit_p(cdc_unbind),
232};
233
234MODULE_DESCRIPTION(DRIVER_DESC);
235MODULE_AUTHOR("David Brownell");
236MODULE_LICENSE("GPL");
237
238static int __init init(void)
239{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200240 return usb_composite_probe(&cdc_driver);
David Brownell19e20682008-06-19 18:20:26 -0700241}
242module_init(init);
243
244static void __exit cleanup(void)
245{
246 usb_composite_unregister(&cdc_driver);
247}
248module_exit(cleanup);