blob: 1a26452ce4ca96bad080e172bff1c69780768181 [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"
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020040
41#include "f_ncm.c"
42#include "u_ether.c"
43
44/*-------------------------------------------------------------------------*/
45
46/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
47 * Instead: allocate your own, using normal USB-IF procedures.
48 */
49
50/* Thanks to NetChip Technologies for donating this product ID.
51 * It's for devices with only CDC Ethernet configurations.
52 */
53#define CDC_VENDOR_NUM 0x0525 /* NetChip */
54#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
55
56/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020057USB_GADGET_COMPOSITE_OPTIONS();
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020058
59static struct usb_device_descriptor device_desc = {
60 .bLength = sizeof device_desc,
61 .bDescriptorType = USB_DT_DEVICE,
62
63 .bcdUSB = cpu_to_le16 (0x0200),
64
65 .bDeviceClass = USB_CLASS_COMM,
66 .bDeviceSubClass = 0,
67 .bDeviceProtocol = 0,
68 /* .bMaxPacketSize0 = f(hardware) */
69
70 /* Vendor and product id defaults change according to what configs
71 * we support. (As does bNumConfigurations.) These values can
72 * also be overridden by module parameters.
73 */
74 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
75 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
76 /* .bcdDevice = f(hardware) */
77 /* .iManufacturer = DYNAMIC */
78 /* .iProduct = DYNAMIC */
79 /* NO SERIAL NUMBER */
80 .bNumConfigurations = 1,
81};
82
83static struct usb_otg_descriptor otg_descriptor = {
84 .bLength = sizeof otg_descriptor,
85 .bDescriptorType = USB_DT_OTG,
86
87 /* REVISIT SRP-only hardware is possible, although
88 * it would not be called "OTG" ...
89 */
90 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
91};
92
93static const struct usb_descriptor_header *otg_desc[] = {
94 (struct usb_descriptor_header *) &otg_descriptor,
95 NULL,
96};
97
98
99/* string IDs are assigned dynamically */
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200100static char manufacturer[50];
101
102static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200103 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer,
104 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
105 [USB_GADGET_SERIAL_IDX].s = "",
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200106 { } /* end of list */
107};
108
109static struct usb_gadget_strings stringtab_dev = {
110 .language = 0x0409, /* en-us */
111 .strings = strings_dev,
112};
113
114static struct usb_gadget_strings *dev_strings[] = {
115 &stringtab_dev,
116 NULL,
117};
118
119static u8 hostaddr[ETH_ALEN];
120
121/*-------------------------------------------------------------------------*/
122
123static int __init ncm_do_config(struct usb_configuration *c)
124{
125 /* FIXME alloc iConfiguration string, set it in c->strings */
126
127 if (gadget_is_otg(c->cdev->gadget)) {
128 c->descriptors = otg_desc;
129 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
130 }
131
132 return ncm_bind_config(c, hostaddr);
133}
134
135static struct usb_configuration ncm_config_driver = {
136 /* .label = f(hardware) */
137 .label = "CDC Ethernet (NCM)",
138 .bConfigurationValue = 1,
139 /* .iConfiguration = DYNAMIC */
140 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
141};
142
143/*-------------------------------------------------------------------------*/
144
145static int __init gncm_bind(struct usb_composite_dev *cdev)
146{
147 int gcnum;
148 struct usb_gadget *gadget = cdev->gadget;
149 int status;
150
151 /* set up network link layer */
152 status = gether_setup(cdev->gadget, hostaddr);
153 if (status < 0)
154 return status;
155
156 gcnum = usb_gadget_controller_number(gadget);
157 if (gcnum >= 0)
158 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
159 else {
160 /* We assume that can_support_ecm() tells the truth;
161 * but if the controller isn't recognized at all then
162 * that assumption is a bit more likely to be wrong.
163 */
164 dev_warn(&gadget->dev,
165 "controller '%s' not recognized; trying %s\n",
166 gadget->name,
167 ncm_config_driver.label);
168 device_desc.bcdDevice =
169 cpu_to_le16(0x0300 | 0x0099);
170 }
171
172
173 /* Allocate string descriptor numbers ... note that string
174 * contents can be overridden by the composite_dev glue.
175 */
176
177 /* device descriptor strings: manufacturer, product */
178 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
179 init_utsname()->sysname, init_utsname()->release,
180 gadget->name);
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200181 status = usb_string_ids_tab(cdev, strings_dev);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200182 if (status < 0)
183 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200184 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
185 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200186
187 status = usb_add_config(cdev, &ncm_config_driver,
188 ncm_do_config);
189 if (status < 0)
190 goto fail;
191
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200192 usb_composite_overwrite_options(cdev, &coverwrite);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200193 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
194
195 return 0;
196
197fail:
198 gether_cleanup();
199 return status;
200}
201
202static int __exit gncm_unbind(struct usb_composite_dev *cdev)
203{
204 gether_cleanup();
205 return 0;
206}
207
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200208static __refdata struct usb_composite_driver ncm_driver = {
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200209 .name = "g_ncm",
210 .dev = &device_desc,
211 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300212 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200213 .bind = gncm_bind,
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200214 .unbind = __exit_p(gncm_unbind),
215};
216
217MODULE_DESCRIPTION(DRIVER_DESC);
218MODULE_AUTHOR("Yauheni Kaliuta");
219MODULE_LICENSE("GPL");
220
221static int __init init(void)
222{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200223 return usb_composite_probe(&ncm_driver);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200224}
225module_init(init);
226
227static void __exit cleanup(void)
228{
229 usb_composite_unregister(&ncm_driver);
230}
231module_exit(cleanup);