]> Pileus Git - ~andy/linux/blob - drivers/usb/gadget/nokia.c
5ed927b16c0e3d7dd0a84f840f7b2e5abd1be136
[~andy/linux] / drivers / usb / gadget / nokia.c
1 /*
2  * nokia.c -- Nokia Composite Gadget Driver
3  *
4  * Copyright (C) 2008-2010 Nokia Corporation
5  * Contact: Felipe Balbi <felipe.balbi@nokia.com>
6  *
7  * This gadget driver borrows from serial.c which is:
8  *
9  * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
10  * Copyright (C) 2008 by David Brownell
11  * Copyright (C) 2008 by Nokia Corporation
12  *
13  * This software is distributed under the terms of the GNU General
14  * Public License ("GPL") as published by the Free Software Foundation,
15  * version 2 of that License.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/utsname.h>
20 #include <linux/device.h>
21
22 #include "u_serial.h"
23 #include "u_ether.h"
24 #include "u_phonet.h"
25 #include "gadget_chips.h"
26
27 /* Defines */
28
29 #define NOKIA_VERSION_NUM               0x0211
30 #define NOKIA_LONG_NAME                 "N900 (PC-Suite Mode)"
31
32 /*-------------------------------------------------------------------------*/
33
34 /*
35  * Kbuild is not very cooperative with respect to linking separately
36  * compiled library objects into one module.  So for now we won't use
37  * separate compilation ... ensuring init/exit sections work to shrink
38  * the runtime footprint, and giving us at least some parts of what
39  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
40  */
41 #include "composite.c"
42
43 #include "u_serial.c"
44 #include "f_acm.c"
45 #include "f_ecm.c"
46 #include "f_obex.c"
47 #include "f_serial.c"
48 #include "f_phonet.c"
49 #include "u_ether.c"
50
51 /*-------------------------------------------------------------------------*/
52 USB_GADGET_COMPOSITE_OPTIONS();
53
54 #define NOKIA_VENDOR_ID                 0x0421  /* Nokia */
55 #define NOKIA_PRODUCT_ID                0x01c8  /* Nokia Gadget */
56
57 /* string IDs are assigned dynamically */
58
59 #define STRING_MANUFACTURER_IDX         0
60 #define STRING_PRODUCT_IDX              1
61 #define STRING_DESCRIPTION_IDX          2
62
63 static char manufacturer_nokia[] = "Nokia";
64 static const char product_nokia[] = NOKIA_LONG_NAME;
65 static const char description_nokia[] = "PC-Suite Configuration";
66
67 static struct usb_string strings_dev[] = {
68         [STRING_MANUFACTURER_IDX].s = manufacturer_nokia,
69         [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME,
70         [STRING_DESCRIPTION_IDX].s = description_nokia,
71         {  } /* end of list */
72 };
73
74 static struct usb_gadget_strings stringtab_dev = {
75         .language       = 0x0409,       /* en-us */
76         .strings        = strings_dev,
77 };
78
79 static struct usb_gadget_strings *dev_strings[] = {
80         &stringtab_dev,
81         NULL,
82 };
83
84 static struct usb_device_descriptor device_desc = {
85         .bLength                = USB_DT_DEVICE_SIZE,
86         .bDescriptorType        = USB_DT_DEVICE,
87         .bcdUSB                 = __constant_cpu_to_le16(0x0200),
88         .bDeviceClass           = USB_CLASS_COMM,
89         .idVendor               = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
90         .idProduct              = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
91         /* .iManufacturer = DYNAMIC */
92         /* .iProduct = DYNAMIC */
93         .bNumConfigurations =   1,
94 };
95
96 /*-------------------------------------------------------------------------*/
97
98 /* Module */
99 MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
100 MODULE_AUTHOR("Felipe Balbi");
101 MODULE_LICENSE("GPL");
102
103 /*-------------------------------------------------------------------------*/
104
105 static u8 hostaddr[ETH_ALEN];
106
107 static int __init nokia_bind_config(struct usb_configuration *c)
108 {
109         int status = 0;
110
111         status = phonet_bind_config(c);
112         if (status)
113                 printk(KERN_DEBUG "could not bind phonet config\n");
114
115         status = obex_bind_config(c, 0);
116         if (status)
117                 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
118
119         status = obex_bind_config(c, 1);
120         if (status)
121                 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
122
123         status = acm_bind_config(c, 2);
124         if (status)
125                 printk(KERN_DEBUG "could not bind acm config\n");
126
127         status = ecm_bind_config(c, hostaddr);
128         if (status)
129                 printk(KERN_DEBUG "could not bind ecm config\n");
130
131         return status;
132 }
133
134 static struct usb_configuration nokia_config_500ma_driver = {
135         .label          = "Bus Powered",
136         .bConfigurationValue = 1,
137         /* .iConfiguration = DYNAMIC */
138         .bmAttributes   = USB_CONFIG_ATT_ONE,
139         .bMaxPower      = 250, /* 500mA */
140 };
141
142 static struct usb_configuration nokia_config_100ma_driver = {
143         .label          = "Self Powered",
144         .bConfigurationValue = 2,
145         /* .iConfiguration = DYNAMIC */
146         .bmAttributes   = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
147         .bMaxPower      = 50, /* 100 mA */
148 };
149
150 static int __init nokia_bind(struct usb_composite_dev *cdev)
151 {
152         int                     gcnum;
153         struct usb_gadget       *gadget = cdev->gadget;
154         int                     status;
155
156         status = gphonet_setup(cdev->gadget);
157         if (status < 0)
158                 goto err_phonet;
159
160         status = gserial_setup(cdev->gadget, 3);
161         if (status < 0)
162                 goto err_serial;
163
164         status = gether_setup(cdev->gadget, hostaddr);
165         if (status < 0)
166                 goto err_ether;
167
168         status = usb_string_ids_tab(cdev, strings_dev);
169         if (status < 0)
170                 goto err_usb;
171         device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
172         device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
173         status = strings_dev[STRING_DESCRIPTION_IDX].id;
174         nokia_config_500ma_driver.iConfiguration = status;
175         nokia_config_100ma_driver.iConfiguration = status;
176
177         /* set up other descriptors */
178         gcnum = usb_gadget_controller_number(gadget);
179         if (gcnum >= 0)
180                 device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
181         else {
182                 /* this should only work with hw that supports altsettings
183                  * and several endpoints, anything else, panic.
184                  */
185                 pr_err("nokia_bind: controller '%s' not recognized\n",
186                         gadget->name);
187                 goto err_usb;
188         }
189
190         /* finally register the configuration */
191         status = usb_add_config(cdev, &nokia_config_500ma_driver,
192                         nokia_bind_config);
193         if (status < 0)
194                 goto err_usb;
195
196         status = usb_add_config(cdev, &nokia_config_100ma_driver,
197                         nokia_bind_config);
198         if (status < 0)
199                 goto err_usb;
200
201         usb_composite_overwrite_options(cdev, &coverwrite);
202         dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
203
204         return 0;
205
206 err_usb:
207         gether_cleanup();
208 err_ether:
209         gserial_cleanup();
210 err_serial:
211         gphonet_cleanup();
212 err_phonet:
213         return status;
214 }
215
216 static int __exit nokia_unbind(struct usb_composite_dev *cdev)
217 {
218         gphonet_cleanup();
219         gserial_cleanup();
220         gether_cleanup();
221
222         return 0;
223 }
224
225 static __refdata struct usb_composite_driver nokia_driver = {
226         .name           = "g_nokia",
227         .dev            = &device_desc,
228         .strings        = dev_strings,
229         .max_speed      = USB_SPEED_HIGH,
230         .bind           = nokia_bind,
231         .unbind         = __exit_p(nokia_unbind),
232 };
233
234 static int __init nokia_init(void)
235 {
236         return usb_composite_probe(&nokia_driver);
237 }
238 module_init(nokia_init);
239
240 static void __exit nokia_cleanup(void)
241 {
242         usb_composite_unregister(&nokia_driver);
243 }
244 module_exit(nokia_cleanup);
245