blob: 13b17f0b2da12c64d59473a37e728cd7ae443c21 [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 */
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +010045#define USB_FACM_INCLUDED
David Brownell8a1ce2c2008-08-18 17:43:56 -070046#include "f_acm.c"
47#include "f_ecm.c"
48#include "u_ether.c"
49
50/*-------------------------------------------------------------------------*/
51
David Brownell19e20682008-06-19 18:20:26 -070052static struct usb_device_descriptor device_desc = {
53 .bLength = sizeof device_desc,
54 .bDescriptorType = USB_DT_DEVICE,
55
Harvey Harrison551509d2009-02-11 14:11:36 -080056 .bcdUSB = cpu_to_le16(0x0200),
David Brownell19e20682008-06-19 18:20:26 -070057
58 .bDeviceClass = USB_CLASS_COMM,
59 .bDeviceSubClass = 0,
60 .bDeviceProtocol = 0,
61 /* .bMaxPacketSize0 = f(hardware) */
62
63 /* Vendor and product id can be overridden by module parameters. */
Harvey Harrison551509d2009-02-11 14:11:36 -080064 .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
65 .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
David Brownell19e20682008-06-19 18:20:26 -070066 /* .bcdDevice = f(hardware) */
67 /* .iManufacturer = DYNAMIC */
68 /* .iProduct = DYNAMIC */
69 /* NO SERIAL NUMBER */
70 .bNumConfigurations = 1,
71};
72
73static struct usb_otg_descriptor otg_descriptor = {
74 .bLength = sizeof otg_descriptor,
75 .bDescriptorType = USB_DT_OTG,
76
77 /* REVISIT SRP-only hardware is possible, although
78 * it would not be called "OTG" ...
79 */
80 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
81};
82
83static const struct usb_descriptor_header *otg_desc[] = {
84 (struct usb_descriptor_header *) &otg_descriptor,
85 NULL,
86};
87
88
89/* string IDs are assigned dynamically */
David Brownell19e20682008-06-19 18:20:26 -070090static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020091 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020092 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
93 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell19e20682008-06-19 18:20:26 -070094 { } /* end of list */
95};
96
97static struct usb_gadget_strings stringtab_dev = {
98 .language = 0x0409, /* en-us */
99 .strings = strings_dev,
100};
101
102static struct usb_gadget_strings *dev_strings[] = {
103 &stringtab_dev,
104 NULL,
105};
106
107static u8 hostaddr[ETH_ALEN];
108
109/*-------------------------------------------------------------------------*/
110
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100111static unsigned char tty_line;
David Brownell19e20682008-06-19 18:20:26 -0700112/*
113 * We _always_ have both CDC ECM and CDC ACM functions.
114 */
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200115static int __init cdc_do_config(struct usb_configuration *c)
David Brownell19e20682008-06-19 18:20:26 -0700116{
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
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100128 status = acm_bind_config(c, tty_line);
David Brownellac90e362008-07-01 13:18:20 -0700129 if (status < 0)
David Brownell19e20682008-06-19 18:20:26 -0700130 return status;
131
132 return 0;
133}
134
135static struct usb_configuration cdc_config_driver = {
136 .label = "CDC Composite (ECM + ACM)",
David Brownell19e20682008-06-19 18:20:26 -0700137 .bConfigurationValue = 1,
138 /* .iConfiguration = DYNAMIC */
139 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell19e20682008-06-19 18:20:26 -0700140};
141
142/*-------------------------------------------------------------------------*/
143
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200144static int __init cdc_bind(struct usb_composite_dev *cdev)
David Brownell19e20682008-06-19 18:20:26 -0700145{
David Brownell19e20682008-06-19 18:20:26 -0700146 struct usb_gadget *gadget = cdev->gadget;
147 int status;
148
149 if (!can_support_ecm(cdev->gadget)) {
David Brownell8a1ce2c2008-08-18 17:43:56 -0700150 dev_err(&gadget->dev, "controller '%s' not usable\n",
151 gadget->name);
David Brownell19e20682008-06-19 18:20:26 -0700152 return -EINVAL;
153 }
154
155 /* set up network link layer */
156 status = gether_setup(cdev->gadget, hostaddr);
157 if (status < 0)
158 return status;
159
160 /* set up serial link layer */
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100161 status = gserial_alloc_line(&tty_line);
David Brownell19e20682008-06-19 18:20:26 -0700162 if (status < 0)
163 goto fail0;
164
David Brownell19e20682008-06-19 18:20:26 -0700165 /* Allocate string descriptor numbers ... note that string
166 * contents can be overridden by the composite_dev glue.
167 */
168
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200169 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell19e20682008-06-19 18:20:26 -0700170 if (status < 0)
171 goto fail1;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200172 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
173 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
David Brownell19e20682008-06-19 18:20:26 -0700174
175 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200176 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
David Brownell19e20682008-06-19 18:20:26 -0700177 if (status < 0)
178 goto fail1;
179
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200180 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell8a1ce2c2008-08-18 17:43:56 -0700181 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
182 DRIVER_DESC);
David Brownell19e20682008-06-19 18:20:26 -0700183
184 return 0;
185
186fail1:
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100187 gserial_free_line(tty_line);
David Brownell19e20682008-06-19 18:20:26 -0700188fail0:
189 gether_cleanup();
190 return status;
191}
192
193static int __exit cdc_unbind(struct usb_composite_dev *cdev)
194{
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100195 gserial_free_line(tty_line);
David Brownell19e20682008-06-19 18:20:26 -0700196 gether_cleanup();
197 return 0;
198}
199
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200200static __refdata struct usb_composite_driver cdc_driver = {
David Brownell19e20682008-06-19 18:20:26 -0700201 .name = "g_cdc",
202 .dev = &device_desc,
203 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300204 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200205 .bind = cdc_bind,
David Brownell19e20682008-06-19 18:20:26 -0700206 .unbind = __exit_p(cdc_unbind),
207};
208
209MODULE_DESCRIPTION(DRIVER_DESC);
210MODULE_AUTHOR("David Brownell");
211MODULE_LICENSE("GPL");
212
213static int __init init(void)
214{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200215 return usb_composite_probe(&cdc_driver);
David Brownell19e20682008-06-19 18:20:26 -0700216}
217module_init(init);
218
219static void __exit cleanup(void)
220{
221 usb_composite_unregister(&cdc_driver);
222}
223module_exit(cleanup);