usb: gadget: allocate & giveback serial ports instead hard code them
[deliverable/linux.git] / 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/device.h>
20
21 #include "u_serial.h"
22 #include "u_ether.h"
23 #include "u_phonet.h"
24 #include "gadget_chips.h"
25
26 /* Defines */
27
28 #define NOKIA_VERSION_NUM 0x0211
29 #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
30
31 /*-------------------------------------------------------------------------*/
32
33 /*
34 * Kbuild is not very cooperative with respect to linking separately
35 * compiled library objects into one module. So for now we won't use
36 * separate compilation ... ensuring init/exit sections work to shrink
37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
39 */
40 #include "f_acm.c"
41 #include "f_ecm.c"
42 #include "f_obex.c"
43 #include "f_serial.c"
44 #include "f_phonet.c"
45 #include "u_ether.c"
46
47 /*-------------------------------------------------------------------------*/
48 USB_GADGET_COMPOSITE_OPTIONS();
49
50 #define NOKIA_VENDOR_ID 0x0421 /* Nokia */
51 #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
52
53 /* string IDs are assigned dynamically */
54
55 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
56
57 static char manufacturer_nokia[] = "Nokia";
58 static const char product_nokia[] = NOKIA_LONG_NAME;
59 static const char description_nokia[] = "PC-Suite Configuration";
60
61 static struct usb_string strings_dev[] = {
62 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
63 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
64 [USB_GADGET_SERIAL_IDX].s = "",
65 [STRING_DESCRIPTION_IDX].s = description_nokia,
66 { } /* end of list */
67 };
68
69 static struct usb_gadget_strings stringtab_dev = {
70 .language = 0x0409, /* en-us */
71 .strings = strings_dev,
72 };
73
74 static struct usb_gadget_strings *dev_strings[] = {
75 &stringtab_dev,
76 NULL,
77 };
78
79 static struct usb_device_descriptor device_desc = {
80 .bLength = USB_DT_DEVICE_SIZE,
81 .bDescriptorType = USB_DT_DEVICE,
82 .bcdUSB = __constant_cpu_to_le16(0x0200),
83 .bDeviceClass = USB_CLASS_COMM,
84 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
85 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
86 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
87 /* .iManufacturer = DYNAMIC */
88 /* .iProduct = DYNAMIC */
89 .bNumConfigurations = 1,
90 };
91
92 /*-------------------------------------------------------------------------*/
93
94 /* Module */
95 MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
96 MODULE_AUTHOR("Felipe Balbi");
97 MODULE_LICENSE("GPL");
98
99 /*-------------------------------------------------------------------------*/
100
101 static u8 hostaddr[ETH_ALEN];
102
103 enum {
104 TTY_PORT_OBEX0,
105 TTY_PORT_OBEX1,
106 TTY_PORT_ACM,
107 TTY_PORTS_MAX,
108 };
109
110 static unsigned char tty_lines[TTY_PORTS_MAX];
111
112 static int __init nokia_bind_config(struct usb_configuration *c)
113 {
114 int status = 0;
115
116 status = phonet_bind_config(c);
117 if (status)
118 printk(KERN_DEBUG "could not bind phonet config\n");
119
120 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]);
121 if (status)
122 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
123
124 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]);
125 if (status)
126 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
127
128 status = acm_bind_config(c, tty_lines[TTY_PORT_ACM]);
129 if (status)
130 printk(KERN_DEBUG "could not bind acm config\n");
131
132 status = ecm_bind_config(c, hostaddr);
133 if (status)
134 printk(KERN_DEBUG "could not bind ecm config\n");
135
136 return status;
137 }
138
139 static struct usb_configuration nokia_config_500ma_driver = {
140 .label = "Bus Powered",
141 .bConfigurationValue = 1,
142 /* .iConfiguration = DYNAMIC */
143 .bmAttributes = USB_CONFIG_ATT_ONE,
144 .MaxPower = 500,
145 };
146
147 static struct usb_configuration nokia_config_100ma_driver = {
148 .label = "Self Powered",
149 .bConfigurationValue = 2,
150 /* .iConfiguration = DYNAMIC */
151 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
152 .MaxPower = 100,
153 };
154
155 static int __init nokia_bind(struct usb_composite_dev *cdev)
156 {
157 struct usb_gadget *gadget = cdev->gadget;
158 int status;
159 int cur_line;
160
161 status = gphonet_setup(cdev->gadget);
162 if (status < 0)
163 goto err_phonet;
164
165 for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) {
166 status = gserial_alloc_line(&tty_lines[cur_line]);
167 if (status)
168 goto err_ether;
169 }
170
171 status = gether_setup(cdev->gadget, hostaddr);
172 if (status < 0)
173 goto err_ether;
174
175 status = usb_string_ids_tab(cdev, strings_dev);
176 if (status < 0)
177 goto err_usb;
178 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
179 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
180 status = strings_dev[STRING_DESCRIPTION_IDX].id;
181 nokia_config_500ma_driver.iConfiguration = status;
182 nokia_config_100ma_driver.iConfiguration = status;
183
184 if (!gadget_supports_altsettings(gadget))
185 goto err_usb;
186
187 /* finally register the configuration */
188 status = usb_add_config(cdev, &nokia_config_500ma_driver,
189 nokia_bind_config);
190 if (status < 0)
191 goto err_usb;
192
193 status = usb_add_config(cdev, &nokia_config_100ma_driver,
194 nokia_bind_config);
195 if (status < 0)
196 goto err_usb;
197
198 usb_composite_overwrite_options(cdev, &coverwrite);
199 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
200
201 return 0;
202
203 err_usb:
204 gether_cleanup();
205 err_ether:
206 cur_line--;
207 while (cur_line >= 0)
208 gserial_free_line(tty_lines[cur_line--]);
209
210 gphonet_cleanup();
211 err_phonet:
212 return status;
213 }
214
215 static int __exit nokia_unbind(struct usb_composite_dev *cdev)
216 {
217 int i;
218
219 gphonet_cleanup();
220
221 for (i = 0; i < TTY_PORTS_MAX; i++)
222 gserial_free_line(tty_lines[i]);
223
224 gether_cleanup();
225
226 return 0;
227 }
228
229 static __refdata struct usb_composite_driver nokia_driver = {
230 .name = "g_nokia",
231 .dev = &device_desc,
232 .strings = dev_strings,
233 .max_speed = USB_SPEED_HIGH,
234 .bind = nokia_bind,
235 .unbind = __exit_p(nokia_unbind),
236 };
237
238 static int __init nokia_init(void)
239 {
240 return usb_composite_probe(&nokia_driver);
241 }
242 module_init(nokia_init);
243
244 static void __exit nokia_cleanup(void)
245 {
246 usb_composite_unregister(&nokia_driver);
247 }
248 module_exit(nokia_cleanup);
249
This page took 0.035532 seconds and 5 git commands to generate.