]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/cdc2.c
ASoC: arizona: Improve handling of setting REFCLK to 0
[karo-tx-linux.git] / drivers / usb / gadget / cdc2.c
1 /*
2  * cdc2.c -- CDC Composite driver, with ECM and ACM support
3  *
4  * Copyright (C) 2008 David Brownell
5  * Copyright (C) 2008 Nokia Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15
16 #include "u_ether.h"
17 #include "u_serial.h"
18 #include "u_ecm.h"
19
20
21 #define DRIVER_DESC             "CDC Composite Gadget"
22 #define DRIVER_VERSION          "King Kamehameha Day 2008"
23
24 /*-------------------------------------------------------------------------*/
25
26 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
27  * Instead:  allocate your own, using normal USB-IF procedures.
28  */
29
30 /* Thanks to NetChip Technologies for donating this product ID.
31  * It's for devices with only this composite CDC configuration.
32  */
33 #define CDC_VENDOR_NUM          0x0525  /* NetChip */
34 #define CDC_PRODUCT_NUM         0xa4aa  /* CDC Composite: ECM + ACM */
35
36 USB_GADGET_COMPOSITE_OPTIONS();
37
38 USB_ETHERNET_MODULE_PARAMETERS();
39
40 /*-------------------------------------------------------------------------*/
41
42 static struct usb_device_descriptor device_desc = {
43         .bLength =              sizeof device_desc,
44         .bDescriptorType =      USB_DT_DEVICE,
45
46         .bcdUSB =               cpu_to_le16(0x0200),
47
48         .bDeviceClass =         USB_CLASS_COMM,
49         .bDeviceSubClass =      0,
50         .bDeviceProtocol =      0,
51         /* .bMaxPacketSize0 = f(hardware) */
52
53         /* Vendor and product id can be overridden by module parameters.  */
54         .idVendor =             cpu_to_le16(CDC_VENDOR_NUM),
55         .idProduct =            cpu_to_le16(CDC_PRODUCT_NUM),
56         /* .bcdDevice = f(hardware) */
57         /* .iManufacturer = DYNAMIC */
58         /* .iProduct = DYNAMIC */
59         /* NO SERIAL NUMBER */
60         .bNumConfigurations =   1,
61 };
62
63 static struct usb_otg_descriptor otg_descriptor = {
64         .bLength =              sizeof otg_descriptor,
65         .bDescriptorType =      USB_DT_OTG,
66
67         /* REVISIT SRP-only hardware is possible, although
68          * it would not be called "OTG" ...
69          */
70         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
71 };
72
73 static const struct usb_descriptor_header *otg_desc[] = {
74         (struct usb_descriptor_header *) &otg_descriptor,
75         NULL,
76 };
77
78
79 /* string IDs are assigned dynamically */
80 static struct usb_string strings_dev[] = {
81         [USB_GADGET_MANUFACTURER_IDX].s = "",
82         [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
83         [USB_GADGET_SERIAL_IDX].s = "",
84         {  } /* end of list */
85 };
86
87 static struct usb_gadget_strings stringtab_dev = {
88         .language       = 0x0409,       /* en-us */
89         .strings        = strings_dev,
90 };
91
92 static struct usb_gadget_strings *dev_strings[] = {
93         &stringtab_dev,
94         NULL,
95 };
96
97 /*-------------------------------------------------------------------------*/
98 static struct usb_function *f_acm;
99 static struct usb_function_instance *fi_serial;
100
101 static struct usb_function *f_ecm;
102 static struct usb_function_instance *fi_ecm;
103
104 /*
105  * We _always_ have both CDC ECM and CDC ACM functions.
106  */
107 static int __init cdc_do_config(struct usb_configuration *c)
108 {
109         int     status;
110
111         if (gadget_is_otg(c->cdev->gadget)) {
112                 c->descriptors = otg_desc;
113                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
114         }
115
116         fi_ecm = usb_get_function_instance("ecm");
117         if (IS_ERR(fi_ecm)) {
118                 status = PTR_ERR(fi_ecm);
119                 goto err_func_ecm;
120         }
121
122         f_ecm = usb_get_function(fi_ecm);
123         if (IS_ERR(f_ecm)) {
124                 status = PTR_ERR(f_ecm);
125                 goto err_get_ecm;
126         }
127
128         status = usb_add_function(c, f_ecm);
129         if (status)
130                 goto err_add_ecm;
131
132         fi_serial = usb_get_function_instance("acm");
133         if (IS_ERR(fi_serial)) {
134                 status = PTR_ERR(fi_serial);
135                 goto err_get_acm;
136         }
137
138         f_acm = usb_get_function(fi_serial);
139         if (IS_ERR(f_acm)) {
140                 status = PTR_ERR(f_acm);
141                 goto err_func_acm;
142         }
143
144         status = usb_add_function(c, f_acm);
145         if (status)
146                 goto err_add_acm;
147
148         return 0;
149
150 err_add_acm:
151         usb_put_function(f_acm);
152 err_func_acm:
153         usb_put_function_instance(fi_serial);
154 err_get_acm:
155         usb_remove_function(c, f_ecm);
156 err_add_ecm:
157         usb_put_function(f_ecm);
158 err_get_ecm:
159         usb_put_function_instance(fi_ecm);
160 err_func_ecm:
161         return status;
162 }
163
164 static struct usb_configuration cdc_config_driver = {
165         .label                  = "CDC Composite (ECM + ACM)",
166         .bConfigurationValue    = 1,
167         /* .iConfiguration = DYNAMIC */
168         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
169 };
170
171 /*-------------------------------------------------------------------------*/
172
173 static int __init cdc_bind(struct usb_composite_dev *cdev)
174 {
175         struct usb_gadget       *gadget = cdev->gadget;
176         struct f_ecm_opts       *ecm_opts;
177         int                     status;
178
179         if (!can_support_ecm(cdev->gadget)) {
180                 dev_err(&gadget->dev, "controller '%s' not usable\n",
181                                 gadget->name);
182                 return -EINVAL;
183         }
184
185         fi_ecm = usb_get_function_instance("ecm");
186         if (IS_ERR(fi_ecm))
187                 return PTR_ERR(fi_ecm);
188
189         ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
190
191         gether_set_qmult(ecm_opts->net, qmult);
192         if (!gether_set_host_addr(ecm_opts->net, host_addr))
193                 pr_info("using host ethernet address: %s", host_addr);
194         if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
195                 pr_info("using self ethernet address: %s", dev_addr);
196
197         fi_serial = usb_get_function_instance("acm");
198         if (IS_ERR(fi_serial)) {
199                 status = PTR_ERR(fi_serial);
200                 goto fail;
201         }
202
203         /* Allocate string descriptor numbers ... note that string
204          * contents can be overridden by the composite_dev glue.
205          */
206
207         status = usb_string_ids_tab(cdev, strings_dev);
208         if (status < 0)
209                 goto fail1;
210         device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
211         device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
212
213         /* register our configuration */
214         status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
215         if (status < 0)
216                 goto fail1;
217
218         usb_composite_overwrite_options(cdev, &coverwrite);
219         dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
220                         DRIVER_DESC);
221
222         return 0;
223
224 fail1:
225         usb_put_function_instance(fi_serial);
226 fail:
227         usb_put_function_instance(fi_ecm);
228         return status;
229 }
230
231 static int __exit cdc_unbind(struct usb_composite_dev *cdev)
232 {
233         usb_put_function(f_acm);
234         usb_put_function_instance(fi_serial);
235         if (!IS_ERR_OR_NULL(f_ecm))
236                 usb_put_function(f_ecm);
237         if (!IS_ERR_OR_NULL(fi_ecm))
238                 usb_put_function_instance(fi_ecm);
239         return 0;
240 }
241
242 static __refdata struct usb_composite_driver cdc_driver = {
243         .name           = "g_cdc",
244         .dev            = &device_desc,
245         .strings        = dev_strings,
246         .max_speed      = USB_SPEED_HIGH,
247         .bind           = cdc_bind,
248         .unbind         = __exit_p(cdc_unbind),
249 };
250
251 MODULE_DESCRIPTION(DRIVER_DESC);
252 MODULE_AUTHOR("David Brownell");
253 MODULE_LICENSE("GPL");
254
255 static int __init init(void)
256 {
257         return usb_composite_probe(&cdc_driver);
258 }
259 module_init(init);
260
261 static void __exit cleanup(void)
262 {
263         usb_composite_unregister(&cdc_driver);
264 }
265 module_exit(cleanup);