blob: e411135b21e52898cdd1c1eb824e8909255efb28 [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>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020023#include <linux/module.h>
24#include <linux/usb/composite.h>
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020025
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 */
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020039#include "f_ncm.c"
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020040
41/*-------------------------------------------------------------------------*/
42
43/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
44 * Instead: allocate your own, using normal USB-IF procedures.
45 */
46
47/* Thanks to NetChip Technologies for donating this product ID.
48 * It's for devices with only CDC Ethernet configurations.
49 */
50#define CDC_VENDOR_NUM 0x0525 /* NetChip */
51#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
52
53/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020054USB_GADGET_COMPOSITE_OPTIONS();
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020055
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020056USB_ETHERNET_MODULE_PARAMETERS();
57
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020058static struct usb_device_descriptor device_desc = {
59 .bLength = sizeof device_desc,
60 .bDescriptorType = USB_DT_DEVICE,
61
62 .bcdUSB = cpu_to_le16 (0x0200),
63
64 .bDeviceClass = USB_CLASS_COMM,
65 .bDeviceSubClass = 0,
66 .bDeviceProtocol = 0,
67 /* .bMaxPacketSize0 = f(hardware) */
68
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.
72 */
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,
80};
81
82static struct usb_otg_descriptor otg_descriptor = {
83 .bLength = sizeof otg_descriptor,
84 .bDescriptorType = USB_DT_OTG,
85
86 /* REVISIT SRP-only hardware is possible, although
87 * it would not be called "OTG" ...
88 */
89 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
90};
91
92static const struct usb_descriptor_header *otg_desc[] = {
93 (struct usb_descriptor_header *) &otg_descriptor,
94 NULL,
95};
96
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020097/* string IDs are assigned dynamically */
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +020098static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020099 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200100 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
101 [USB_GADGET_SERIAL_IDX].s = "",
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200102 { } /* end of list */
103};
104
105static struct usb_gadget_strings stringtab_dev = {
106 .language = 0x0409, /* en-us */
107 .strings = strings_dev,
108};
109
110static struct usb_gadget_strings *dev_strings[] = {
111 &stringtab_dev,
112 NULL,
113};
114
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100115struct eth_dev *the_dev;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200116static u8 host_mac[ETH_ALEN];
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200117
118/*-------------------------------------------------------------------------*/
119
120static int __init ncm_do_config(struct usb_configuration *c)
121{
122 /* FIXME alloc iConfiguration string, set it in c->strings */
123
124 if (gadget_is_otg(c->cdev->gadget)) {
125 c->descriptors = otg_desc;
126 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
127 }
128
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200129 return ncm_bind_config(c, host_mac, the_dev);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200130}
131
132static 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,
138};
139
140/*-------------------------------------------------------------------------*/
141
142static int __init gncm_bind(struct usb_composite_dev *cdev)
143{
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200144 struct usb_gadget *gadget = cdev->gadget;
145 int status;
146
147 /* set up network link layer */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200148 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
149 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100150 if (IS_ERR(the_dev))
151 return PTR_ERR(the_dev);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200152
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200153 /* Allocate string descriptor numbers ... note that string
154 * contents can be overridden by the composite_dev glue.
155 */
156
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200157 status = usb_string_ids_tab(cdev, strings_dev);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200158 if (status < 0)
159 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200160 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
161 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200162
163 status = usb_add_config(cdev, &ncm_config_driver,
164 ncm_do_config);
165 if (status < 0)
166 goto fail;
167
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200168 usb_composite_overwrite_options(cdev, &coverwrite);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200169 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
170
171 return 0;
172
173fail:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100174 gether_cleanup(the_dev);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200175 return status;
176}
177
178static int __exit gncm_unbind(struct usb_composite_dev *cdev)
179{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100180 gether_cleanup(the_dev);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200181 return 0;
182}
183
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200184static __refdata struct usb_composite_driver ncm_driver = {
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200185 .name = "g_ncm",
186 .dev = &device_desc,
187 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300188 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200189 .bind = gncm_bind,
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200190 .unbind = __exit_p(gncm_unbind),
191};
192
193MODULE_DESCRIPTION(DRIVER_DESC);
194MODULE_AUTHOR("Yauheni Kaliuta");
195MODULE_LICENSE("GPL");
196
197static int __init init(void)
198{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200199 return usb_composite_probe(&ncm_driver);
Yauheni Kaliuta6c34d282010-12-08 13:12:06 +0200200}
201module_init(init);
202
203static void __exit cleanup(void)
204{
205 usb_composite_unregister(&ncm_driver);
206}
207module_exit(cleanup);