blob: 2c52551827692c5bd119bf78513e32e3f0fd7116 [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>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040014#include <linux/module.h>
David Brownell19e20682008-06-19 18:20:26 -070015
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/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020036USB_GADGET_COMPOSITE_OPTIONS();
David Brownell19e20682008-06-19 18:20:26 -070037
David Brownell8a1ce2c2008-08-18 17:43:56 -070038/*
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 */
David Brownell8a1ce2c2008-08-18 17:43:56 -070045#include "f_ecm.c"
46#include "u_ether.c"
47
48/*-------------------------------------------------------------------------*/
49
David Brownell19e20682008-06-19 18:20:26 -070050static struct usb_device_descriptor device_desc = {
51 .bLength = sizeof device_desc,
52 .bDescriptorType = USB_DT_DEVICE,
53
Harvey Harrison551509d2009-02-11 14:11:36 -080054 .bcdUSB = cpu_to_le16(0x0200),
David Brownell19e20682008-06-19 18:20:26 -070055
56 .bDeviceClass = USB_CLASS_COMM,
57 .bDeviceSubClass = 0,
58 .bDeviceProtocol = 0,
59 /* .bMaxPacketSize0 = f(hardware) */
60
61 /* Vendor and product id can be overridden by module parameters. */
Harvey Harrison551509d2009-02-11 14:11:36 -080062 .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
63 .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
David Brownell19e20682008-06-19 18:20:26 -070064 /* .bcdDevice = f(hardware) */
65 /* .iManufacturer = DYNAMIC */
66 /* .iProduct = DYNAMIC */
67 /* NO SERIAL NUMBER */
68 .bNumConfigurations = 1,
69};
70
71static struct usb_otg_descriptor otg_descriptor = {
72 .bLength = sizeof otg_descriptor,
73 .bDescriptorType = USB_DT_OTG,
74
75 /* REVISIT SRP-only hardware is possible, although
76 * it would not be called "OTG" ...
77 */
78 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
79};
80
81static const struct usb_descriptor_header *otg_desc[] = {
82 (struct usb_descriptor_header *) &otg_descriptor,
83 NULL,
84};
85
86
87/* string IDs are assigned dynamically */
David Brownell19e20682008-06-19 18:20:26 -070088static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020089 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020090 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
91 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell19e20682008-06-19 18:20:26 -070092 { } /* end of list */
93};
94
95static struct usb_gadget_strings stringtab_dev = {
96 .language = 0x0409, /* en-us */
97 .strings = strings_dev,
98};
99
100static struct usb_gadget_strings *dev_strings[] = {
101 &stringtab_dev,
102 NULL,
103};
104
105static u8 hostaddr[ETH_ALEN];
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100106static struct eth_dev *the_dev;
David Brownell19e20682008-06-19 18:20:26 -0700107/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior29a66452012-12-23 21:10:09 +0100108static struct usb_function *f_acm;
109static struct usb_function_instance *fi_serial;
David Brownell19e20682008-06-19 18:20:26 -0700110
111/*
112 * We _always_ have both CDC ECM and CDC ACM functions.
113 */
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200114static int __init cdc_do_config(struct usb_configuration *c)
David Brownell19e20682008-06-19 18:20:26 -0700115{
116 int status;
117
118 if (gadget_is_otg(c->cdev->gadget)) {
119 c->descriptors = otg_desc;
120 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
121 }
122
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100123 status = ecm_bind_config(c, hostaddr, the_dev);
David Brownell19e20682008-06-19 18:20:26 -0700124 if (status < 0)
125 return status;
126
Sebastian Andrzej Siewior29a66452012-12-23 21:10:09 +0100127 fi_serial = usb_get_function_instance("acm");
128 if (IS_ERR(fi_serial))
129 return PTR_ERR(fi_serial);
David Brownell19e20682008-06-19 18:20:26 -0700130
Sebastian Andrzej Siewior29a66452012-12-23 21:10:09 +0100131 f_acm = usb_get_function(fi_serial);
Wei Yongjun8d6f51b2013-04-06 17:25:21 +0800132 if (IS_ERR(f_acm)) {
133 status = PTR_ERR(f_acm);
Sebastian Andrzej Siewior29a66452012-12-23 21:10:09 +0100134 goto err_func_acm;
Wei Yongjun8d6f51b2013-04-06 17:25:21 +0800135 }
Sebastian Andrzej Siewior29a66452012-12-23 21:10:09 +0100136
137 status = usb_add_function(c, f_acm);
138 if (status)
139 goto err_conf;
David Brownell19e20682008-06-19 18:20:26 -0700140 return 0;
Sebastian Andrzej Siewior29a66452012-12-23 21:10:09 +0100141err_conf:
142 usb_put_function(f_acm);
143err_func_acm:
144 usb_put_function_instance(fi_serial);
145 return status;
David Brownell19e20682008-06-19 18:20:26 -0700146}
147
148static struct usb_configuration cdc_config_driver = {
149 .label = "CDC Composite (ECM + ACM)",
David Brownell19e20682008-06-19 18:20:26 -0700150 .bConfigurationValue = 1,
151 /* .iConfiguration = DYNAMIC */
152 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell19e20682008-06-19 18:20:26 -0700153};
154
155/*-------------------------------------------------------------------------*/
156
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200157static int __init cdc_bind(struct usb_composite_dev *cdev)
David Brownell19e20682008-06-19 18:20:26 -0700158{
David Brownell19e20682008-06-19 18:20:26 -0700159 struct usb_gadget *gadget = cdev->gadget;
160 int status;
161
162 if (!can_support_ecm(cdev->gadget)) {
David Brownell8a1ce2c2008-08-18 17:43:56 -0700163 dev_err(&gadget->dev, "controller '%s' not usable\n",
164 gadget->name);
David Brownell19e20682008-06-19 18:20:26 -0700165 return -EINVAL;
166 }
167
168 /* set up network link layer */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100169 the_dev = gether_setup(cdev->gadget, hostaddr);
170 if (IS_ERR(the_dev))
171 return PTR_ERR(the_dev);
David Brownell19e20682008-06-19 18:20:26 -0700172
David Brownell19e20682008-06-19 18:20:26 -0700173 /* Allocate string descriptor numbers ... note that string
174 * contents can be overridden by the composite_dev glue.
175 */
176
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200177 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell19e20682008-06-19 18:20:26 -0700178 if (status < 0)
179 goto fail1;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200180 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
181 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
David Brownell19e20682008-06-19 18:20:26 -0700182
183 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200184 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
David Brownell19e20682008-06-19 18:20:26 -0700185 if (status < 0)
186 goto fail1;
187
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200188 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell8a1ce2c2008-08-18 17:43:56 -0700189 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
190 DRIVER_DESC);
David Brownell19e20682008-06-19 18:20:26 -0700191
192 return 0;
193
194fail1:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100195 gether_cleanup(the_dev);
David Brownell19e20682008-06-19 18:20:26 -0700196 return status;
197}
198
199static int __exit cdc_unbind(struct usb_composite_dev *cdev)
200{
Sebastian Andrzej Siewior29a66452012-12-23 21:10:09 +0100201 usb_put_function(f_acm);
202 usb_put_function_instance(fi_serial);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100203 gether_cleanup(the_dev);
David Brownell19e20682008-06-19 18:20:26 -0700204 return 0;
205}
206
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200207static __refdata struct usb_composite_driver cdc_driver = {
David Brownell19e20682008-06-19 18:20:26 -0700208 .name = "g_cdc",
209 .dev = &device_desc,
210 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300211 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200212 .bind = cdc_bind,
David Brownell19e20682008-06-19 18:20:26 -0700213 .unbind = __exit_p(cdc_unbind),
214};
215
216MODULE_DESCRIPTION(DRIVER_DESC);
217MODULE_AUTHOR("David Brownell");
218MODULE_LICENSE("GPL");
219
220static int __init init(void)
221{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200222 return usb_composite_probe(&cdc_driver);
David Brownell19e20682008-06-19 18:20:26 -0700223}
224module_init(init);
225
226static void __exit cleanup(void)
227{
228 usb_composite_unregister(&cdc_driver);
229}
230module_exit(cleanup);