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/module.h>
24 #include <linux/usb/composite.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.
41 /*-------------------------------------------------------------------------*/
43 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
44 * Instead: allocate your own, using normal USB-IF procedures.
47 /* Thanks to NetChip Technologies for donating this product ID.
48 * It's for devices with only CDC Ethernet configurations.
50 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
51 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
53 /*-------------------------------------------------------------------------*/
54 USB_GADGET_COMPOSITE_OPTIONS();
56 USB_ETHERNET_MODULE_PARAMETERS();
58 static struct usb_device_descriptor device_desc
= {
59 .bLength
= sizeof device_desc
,
60 .bDescriptorType
= USB_DT_DEVICE
,
62 .bcdUSB
= cpu_to_le16 (0x0200),
64 .bDeviceClass
= USB_CLASS_COMM
,
67 /* .bMaxPacketSize0 = f(hardware) */
69 /* Vendor and product id defaults change according to what configs
70 * we support. (As does bNumConfigurations.) These values can
71 * also be overridden by module parameters.
73 .idVendor
= cpu_to_le16 (CDC_VENDOR_NUM
),
74 .idProduct
= cpu_to_le16 (CDC_PRODUCT_NUM
),
75 /* .bcdDevice = f(hardware) */
76 /* .iManufacturer = DYNAMIC */
77 /* .iProduct = DYNAMIC */
78 /* NO SERIAL NUMBER */
79 .bNumConfigurations
= 1,
82 static struct usb_otg_descriptor otg_descriptor
= {
83 .bLength
= sizeof otg_descriptor
,
84 .bDescriptorType
= USB_DT_OTG
,
86 /* REVISIT SRP-only hardware is possible, although
87 * it would not be called "OTG" ...
89 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
92 static const struct usb_descriptor_header
*otg_desc
[] = {
93 (struct usb_descriptor_header
*) &otg_descriptor
,
97 /* string IDs are assigned dynamically */
98 static struct usb_string strings_dev
[] = {
99 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
100 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
101 [USB_GADGET_SERIAL_IDX
].s
= "",
102 { } /* end of list */
105 static struct usb_gadget_strings stringtab_dev
= {
106 .language
= 0x0409, /* en-us */
107 .strings
= strings_dev
,
110 static struct usb_gadget_strings
*dev_strings
[] = {
115 struct eth_dev
*the_dev
;
116 static u8 host_mac
[ETH_ALEN
];
118 /*-------------------------------------------------------------------------*/
120 static int __init
ncm_do_config(struct usb_configuration
*c
)
122 /* FIXME alloc iConfiguration string, set it in c->strings */
124 if (gadget_is_otg(c
->cdev
->gadget
)) {
125 c
->descriptors
= otg_desc
;
126 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
129 return ncm_bind_config(c
, host_mac
, the_dev
);
132 static struct usb_configuration ncm_config_driver
= {
133 /* .label = f(hardware) */
134 .label
= "CDC Ethernet (NCM)",
135 .bConfigurationValue
= 1,
136 /* .iConfiguration = DYNAMIC */
137 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
140 /*-------------------------------------------------------------------------*/
142 static int __init
gncm_bind(struct usb_composite_dev
*cdev
)
144 struct usb_gadget
*gadget
= cdev
->gadget
;
147 /* set up network link layer */
148 the_dev
= gether_setup(cdev
->gadget
, dev_addr
, host_addr
, host_mac
,
151 return PTR_ERR(the_dev
);
153 /* Allocate string descriptor numbers ... note that string
154 * contents can be overridden by the composite_dev glue.
157 status
= usb_string_ids_tab(cdev
, strings_dev
);
160 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
161 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
163 status
= usb_add_config(cdev
, &ncm_config_driver
,
168 usb_composite_overwrite_options(cdev
, &coverwrite
);
169 dev_info(&gadget
->dev
, "%s\n", DRIVER_DESC
);
174 gether_cleanup(the_dev
);
178 static int __exit
gncm_unbind(struct usb_composite_dev
*cdev
)
180 gether_cleanup(the_dev
);
184 static __refdata
struct usb_composite_driver ncm_driver
= {
187 .strings
= dev_strings
,
188 .max_speed
= USB_SPEED_HIGH
,
190 .unbind
= __exit_p(gncm_unbind
),
193 MODULE_DESCRIPTION(DRIVER_DESC
);
194 MODULE_AUTHOR("Yauheni Kaliuta");
195 MODULE_LICENSE("GPL");
197 static int __init
init(void)
199 return usb_composite_probe(&ncm_driver
);
203 static void __exit
cleanup(void)
205 usb_composite_unregister(&ncm_driver
);
207 module_exit(cleanup
);