blob: 89530034dff18bbfcf0223b200c0f75b43d0cfb7 [file] [log] [blame]
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +02001/*
2 * ncm.c -- NCM gadget driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
6 *
7 * The driver borrows from ether.c which is:
8 *
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
11 * Copyright (C) 2008 Nokia Corporation
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020017 */
18
19/* #define DEBUG */
20/* #define VERBOSE_DEBUG */
21
22#include <linux/kernel.h>
23#include <linux/utsname.h>
24
25
26#include "u_ether.h"
27
28#define DRIVER_DESC "NCM Gadget"
29
30/*-------------------------------------------------------------------------*/
31
32/*
33 * Kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38 */
39#include "composite.c"
40#include "usbstring.c"
41#include "config.c"
42#include "epautoconf.c"
43
44#include "f_ncm.c"
45#include "u_ether.c"
46
47/*-------------------------------------------------------------------------*/
48
49/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50 * Instead: allocate your own, using normal USB-IF procedures.
51 */
52
53/* Thanks to NetChip Technologies for donating this product ID.
54 * It's for devices with only CDC Ethernet configurations.
55 */
56#define CDC_VENDOR_NUM 0x0525 /* NetChip */
57#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
58
59/*-------------------------------------------------------------------------*/
60
61static struct usb_device_descriptor device_desc = {
62 .bLength = sizeof device_desc,
63 .bDescriptorType = USB_DT_DEVICE,
64
65 .bcdUSB = cpu_to_le16 (0x0200),
66
67 .bDeviceClass = USB_CLASS_COMM,
68 .bDeviceSubClass = 0,
69 .bDeviceProtocol = 0,
70 /* .bMaxPacketSize0 = f(hardware) */
71
72 /* Vendor and product id defaults change according to what configs
73 * we support. (As does bNumConfigurations.) These values can
74 * also be overridden by module parameters.
75 */
76 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
77 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
78 /* .bcdDevice = f(hardware) */
79 /* .iManufacturer = DYNAMIC */
80 /* .iProduct = DYNAMIC */
81 /* NO SERIAL NUMBER */
82 .bNumConfigurations = 1,
83};
84
85static struct usb_otg_descriptor otg_descriptor = {
86 .bLength = sizeof otg_descriptor,
87 .bDescriptorType = USB_DT_OTG,
88
89 /* REVISIT SRP-only hardware is possible, although
90 * it would not be called "OTG" ...
91 */
92 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
93};
94
95static const struct usb_descriptor_header *otg_desc[] = {
96 (struct usb_descriptor_header *) &otg_descriptor,
97 NULL,
98};
99
100
101/* string IDs are assigned dynamically */
102
103#define STRING_MANUFACTURER_IDX 0
104#define STRING_PRODUCT_IDX 1
105
106static char manufacturer[50];
107
108static struct usb_string strings_dev[] = {
109 [STRING_MANUFACTURER_IDX].s = manufacturer,
110 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
111 { } /* end of list */
112};
113
114static struct usb_gadget_strings stringtab_dev = {
115 .language = 0x0409, /* en-us */
116 .strings = strings_dev,
117};
118
119static struct usb_gadget_strings *dev_strings[] = {
120 &stringtab_dev,
121 NULL,
122};
123
124static u8 hostaddr[ETH_ALEN];
125
126/*-------------------------------------------------------------------------*/
127
128static int __init ncm_do_config(struct usb_configuration *c)
129{
130 /* FIXME alloc iConfiguration string, set it in c->strings */
131
132 if (gadget_is_otg(c->cdev->gadget)) {
133 c->descriptors = otg_desc;
134 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
135 }
136
137 return ncm_bind_config(c, hostaddr);
138}
139
140static struct usb_configuration ncm_config_driver = {
141 /* .label = f(hardware) */
142 .label = "CDC Ethernet (NCM)",
143 .bConfigurationValue = 1,
144 /* .iConfiguration = DYNAMIC */
145 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
146};
147
148/*-------------------------------------------------------------------------*/
149
150static int __init gncm_bind(struct usb_composite_dev *cdev)
151{
152 int gcnum;
153 struct usb_gadget *gadget = cdev->gadget;
154 int status;
155
156 /* set up network link layer */
157 status = gether_setup(cdev->gadget, hostaddr);
158 if (status < 0)
159 return status;
160
161 gcnum = usb_gadget_controller_number(gadget);
162 if (gcnum >= 0)
163 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
164 else {
165 /* We assume that can_support_ecm() tells the truth;
166 * but if the controller isn't recognized at all then
167 * that assumption is a bit more likely to be wrong.
168 */
169 dev_warn(&gadget->dev,
170 "controller '%s' not recognized; trying %s\n",
171 gadget->name,
172 ncm_config_driver.label);
173 device_desc.bcdDevice =
174 cpu_to_le16(0x0300 | 0x0099);
175 }
176
177
178 /* Allocate string descriptor numbers ... note that string
179 * contents can be overridden by the composite_dev glue.
180 */
181
182 /* device descriptor strings: manufacturer, product */
183 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
184 init_utsname()->sysname, init_utsname()->release,
185 gadget->name);
186 status = usb_string_id(cdev);
187 if (status < 0)
188 goto fail;
189 strings_dev[STRING_MANUFACTURER_IDX].id = status;
190 device_desc.iManufacturer = status;
191
192 status = usb_string_id(cdev);
193 if (status < 0)
194 goto fail;
195 strings_dev[STRING_PRODUCT_IDX].id = status;
196 device_desc.iProduct = status;
197
198 status = usb_add_config(cdev, &ncm_config_driver,
199 ncm_do_config);
200 if (status < 0)
201 goto fail;
202
203 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
204
205 return 0;
206
207fail:
208 gether_cleanup();
209 return status;
210}
211
212static int __exit gncm_unbind(struct usb_composite_dev *cdev)
213{
214 gether_cleanup();
215 return 0;
216}
217
218static struct usb_composite_driver ncm_driver = {
219 .name = "g_ncm",
220 .dev = &device_desc,
221 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300222 .max_speed = USB_SPEED_HIGH,
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200223 .unbind = __exit_p(gncm_unbind),
224};
225
226MODULE_DESCRIPTION(DRIVER_DESC);
227MODULE_AUTHOR("Yauheni Kaliuta");
228MODULE_LICENSE("GPL");
229
230static int __init init(void)
231{
232 return usb_composite_probe(&ncm_driver, gncm_bind);
233}
234module_init(init);
235
236static void __exit cleanup(void)
237{
238 usb_composite_unregister(&ncm_driver);
239}
240module_exit(cleanup);