blob: 93a75809b4deb4118c436c42434126e47c4c9a06 [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 */
93
94#define STRING_MANUFACTURER_IDX 0
95#define STRING_PRODUCT_IDX 1
96
97static char manufacturer[50];
98
99static struct usb_string strings_dev[] = {
100 [STRING_MANUFACTURER_IDX].s = manufacturer,
101 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
102 { } /* end of list */
103};
104
105static struct usb_gadget_strings stringtab_dev = {
106 .language = 0x0409, /* en-us */
107 .strings = strings_dev,
108};
109
110static struct usb_gadget_strings *dev_strings[] = {
111 &stringtab_dev,
112 NULL,
113};
114
115static u8 hostaddr[ETH_ALEN];
116
117/*-------------------------------------------------------------------------*/
118
119/*
120 * We _always_ have both CDC ECM and CDC ACM functions.
121 */
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200122static int __init cdc_do_config(struct usb_configuration *c)
David Brownell19e20682008-06-19 18:20:26 -0700123{
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);
David Brownellac90e362008-07-01 13:18:20 -0700136 if (status < 0)
David Brownell19e20682008-06-19 18:20:26 -0700137 return status;
138
139 return 0;
140}
141
142static struct usb_configuration cdc_config_driver = {
143 .label = "CDC Composite (ECM + ACM)",
David Brownell19e20682008-06-19 18:20:26 -0700144 .bConfigurationValue = 1,
145 /* .iConfiguration = DYNAMIC */
146 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell19e20682008-06-19 18:20:26 -0700147};
148
149/*-------------------------------------------------------------------------*/
150
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200151static int __init cdc_bind(struct usb_composite_dev *cdev)
David Brownell19e20682008-06-19 18:20:26 -0700152{
153 int gcnum;
154 struct usb_gadget *gadget = cdev->gadget;
155 int status;
156
157 if (!can_support_ecm(cdev->gadget)) {
David Brownell8a1ce2c2008-08-18 17:43:56 -0700158 dev_err(&gadget->dev, "controller '%s' not usable\n",
159 gadget->name);
David Brownell19e20682008-06-19 18:20:26 -0700160 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 */
Arjan van de Venb6c63932008-07-25 01:45:52 -0700181 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
David Brownell19e20682008-06-19 18:20:26 -0700182 gadget->name,
183 cdc_config_driver.label);
184 device_desc.bcdDevice =
Harvey Harrison551509d2009-02-11 14:11:36 -0800185 cpu_to_le16(0x0300 | 0x0099);
David Brownell19e20682008-06-19 18:20:26 -0700186 }
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);
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200197 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell19e20682008-06-19 18:20:26 -0700198 if (status < 0)
199 goto fail1;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200200 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
201 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
David Brownell19e20682008-06-19 18:20:26 -0700202
203 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200204 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
David Brownell19e20682008-06-19 18:20:26 -0700205 if (status < 0)
206 goto fail1;
207
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200208 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell8a1ce2c2008-08-18 17:43:56 -0700209 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
210 DRIVER_DESC);
David Brownell19e20682008-06-19 18:20:26 -0700211
212 return 0;
213
214fail1:
215 gserial_cleanup();
216fail0:
217 gether_cleanup();
218 return status;
219}
220
221static int __exit cdc_unbind(struct usb_composite_dev *cdev)
222{
223 gserial_cleanup();
224 gether_cleanup();
225 return 0;
226}
227
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200228static __refdata struct usb_composite_driver cdc_driver = {
David Brownell19e20682008-06-19 18:20:26 -0700229 .name = "g_cdc",
230 .dev = &device_desc,
231 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300232 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200233 .bind = cdc_bind,
David Brownell19e20682008-06-19 18:20:26 -0700234 .unbind = __exit_p(cdc_unbind),
235};
236
237MODULE_DESCRIPTION(DRIVER_DESC);
238MODULE_AUTHOR("David Brownell");
239MODULE_LICENSE("GPL");
240
241static int __init init(void)
242{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200243 return usb_composite_probe(&cdc_driver);
David Brownell19e20682008-06-19 18:20:26 -0700244}
245module_init(init);
246
247static void __exit cleanup(void)
248{
249 usb_composite_unregister(&cdc_driver);
250}
251module_exit(cleanup);