2 * ncm.c -- NCM gadget driver
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
7 * The driver borrows from ether.c which is:
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
11 * Copyright (C) 2008 Nokia Corporation
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.
20 /* #define VERBOSE_DEBUG */
22 #include <linux/kernel.h>
23 #include <linux/utsname.h>
28 #define DRIVER_DESC "NCM Gadget"
30 /*-------------------------------------------------------------------------*/
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.
39 #include "composite.c"
40 #include "usbstring.c"
42 #include "epautoconf.c"
47 /*-------------------------------------------------------------------------*/
49 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50 * Instead: allocate your own, using normal USB-IF procedures.
53 /* Thanks to NetChip Technologies for donating this product ID.
54 * It's for devices with only CDC Ethernet configurations.
56 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
57 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
59 /*-------------------------------------------------------------------------*/
61 static struct usb_device_descriptor device_desc
= {
62 .bLength
= sizeof device_desc
,
63 .bDescriptorType
= USB_DT_DEVICE
,
65 .bcdUSB
= cpu_to_le16 (0x0200),
67 .bDeviceClass
= USB_CLASS_COMM
,
70 /* .bMaxPacketSize0 = f(hardware) */
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.
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,
85 static struct usb_otg_descriptor otg_descriptor
= {
86 .bLength
= sizeof otg_descriptor
,
87 .bDescriptorType
= USB_DT_OTG
,
89 /* REVISIT SRP-only hardware is possible, although
90 * it would not be called "OTG" ...
92 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
95 static const struct usb_descriptor_header
*otg_desc
[] = {
96 (struct usb_descriptor_header
*) &otg_descriptor
,
101 /* string IDs are assigned dynamically */
103 #define STRING_MANUFACTURER_IDX 0
104 #define STRING_PRODUCT_IDX 1
106 static char manufacturer
[50];
108 static struct usb_string strings_dev
[] = {
109 [STRING_MANUFACTURER_IDX
].s
= manufacturer
,
110 [STRING_PRODUCT_IDX
].s
= DRIVER_DESC
,
111 { } /* end of list */
114 static struct usb_gadget_strings stringtab_dev
= {
115 .language
= 0x0409, /* en-us */
116 .strings
= strings_dev
,
119 static struct usb_gadget_strings
*dev_strings
[] = {
124 static u8 hostaddr
[ETH_ALEN
];
126 /*-------------------------------------------------------------------------*/
128 static int __init
ncm_do_config(struct usb_configuration
*c
)
130 /* FIXME alloc iConfiguration string, set it in c->strings */
132 if (gadget_is_otg(c
->cdev
->gadget
)) {
133 c
->descriptors
= otg_desc
;
134 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
137 return ncm_bind_config(c
, hostaddr
);
140 static 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
,
148 /*-------------------------------------------------------------------------*/
150 static int __init
gncm_bind(struct usb_composite_dev
*cdev
)
153 struct usb_gadget
*gadget
= cdev
->gadget
;
156 /* set up network link layer */
157 status
= gether_setup(cdev
->gadget
, hostaddr
);
161 gcnum
= usb_gadget_controller_number(gadget
);
163 device_desc
.bcdDevice
= cpu_to_le16(0x0300 | gcnum
);
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.
169 dev_warn(&gadget
->dev
,
170 "controller '%s' not recognized; trying %s\n",
172 ncm_config_driver
.label
);
173 device_desc
.bcdDevice
=
174 cpu_to_le16(0x0300 | 0x0099);
178 /* Allocate string descriptor numbers ... note that string
179 * contents can be overridden by the composite_dev glue.
182 /* device descriptor strings: manufacturer, product */
183 snprintf(manufacturer
, sizeof manufacturer
, "%s %s with %s",
184 init_utsname()->sysname
, init_utsname()->release
,
186 status
= usb_string_id(cdev
);
189 strings_dev
[STRING_MANUFACTURER_IDX
].id
= status
;
190 device_desc
.iManufacturer
= status
;
192 status
= usb_string_id(cdev
);
195 strings_dev
[STRING_PRODUCT_IDX
].id
= status
;
196 device_desc
.iProduct
= status
;
198 status
= usb_add_config(cdev
, &ncm_config_driver
,
203 dev_info(&gadget
->dev
, "%s\n", DRIVER_DESC
);
212 static int __exit
gncm_unbind(struct usb_composite_dev
*cdev
)
218 static struct usb_composite_driver ncm_driver
= {
221 .strings
= dev_strings
,
222 .max_speed
= USB_SPEED_HIGH
,
223 .unbind
= __exit_p(gncm_unbind
),
226 MODULE_DESCRIPTION(DRIVER_DESC
);
227 MODULE_AUTHOR("Yauheni Kaliuta");
228 MODULE_LICENSE("GPL");
230 static int __init
init(void)
232 return usb_composite_probe(&ncm_driver
, gncm_bind
);
236 static void __exit
cleanup(void)
238 usb_composite_unregister(&ncm_driver
);
240 module_exit(cleanup
);