blob: eebdd0e0b1d6b86ade1a85cf594f801f280ca569 [file] [log] [blame]
Felipe Balbif358f5b2010-01-05 16:10:13 +02001/*
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>
Felipe Balbif358f5b2010-01-05 16:10:13 +020019#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 */
Andrzej Pietrasiewicz1d8fc252013-03-21 15:33:42 +010040#define USBF_OBEX_INCLUDED
Felipe Balbif358f5b2010-01-05 16:10:13 +020041#include "f_obex.c"
Felipe Balbif358f5b2010-01-05 16:10:13 +020042#include "f_phonet.c"
43#include "u_ether.c"
44
45/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020046USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020047
48#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
49#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
50
51/* string IDs are assigned dynamically */
52
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020053#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
Felipe Balbif358f5b2010-01-05 16:10:13 +020054
55static char manufacturer_nokia[] = "Nokia";
56static const char product_nokia[] = NOKIA_LONG_NAME;
57static const char description_nokia[] = "PC-Suite Configuration";
58
59static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020060 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
61 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
62 [USB_GADGET_SERIAL_IDX].s = "",
Felipe Balbif358f5b2010-01-05 16:10:13 +020063 [STRING_DESCRIPTION_IDX].s = description_nokia,
64 { } /* end of list */
65};
66
67static struct usb_gadget_strings stringtab_dev = {
68 .language = 0x0409, /* en-us */
69 .strings = strings_dev,
70};
71
72static struct usb_gadget_strings *dev_strings[] = {
73 &stringtab_dev,
74 NULL,
75};
76
77static struct usb_device_descriptor device_desc = {
78 .bLength = USB_DT_DEVICE_SIZE,
79 .bDescriptorType = USB_DT_DEVICE,
80 .bcdUSB = __constant_cpu_to_le16(0x0200),
81 .bDeviceClass = USB_CLASS_COMM,
82 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
83 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +020084 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
Felipe Balbif358f5b2010-01-05 16:10:13 +020085 /* .iManufacturer = DYNAMIC */
86 /* .iProduct = DYNAMIC */
87 .bNumConfigurations = 1,
88};
89
90/*-------------------------------------------------------------------------*/
91
92/* Module */
93MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
94MODULE_AUTHOR("Felipe Balbi");
95MODULE_LICENSE("GPL");
96
97/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +010098static struct usb_function *f_acm_cfg1;
99static struct usb_function *f_acm_cfg2;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200100static u8 hostaddr[ETH_ALEN];
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100101static struct eth_dev *the_dev;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200102
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100103enum {
104 TTY_PORT_OBEX0,
105 TTY_PORT_OBEX1,
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100106 TTY_PORTS_MAX,
107};
108
109static unsigned char tty_lines[TTY_PORTS_MAX];
110
Felipe Balbif358f5b2010-01-05 16:10:13 +0200111static struct usb_configuration nokia_config_500ma_driver = {
112 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200113 .bConfigurationValue = 1,
114 /* .iConfiguration = DYNAMIC */
115 .bmAttributes = USB_CONFIG_ATT_ONE,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100116 .MaxPower = 500,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200117};
118
119static struct usb_configuration nokia_config_100ma_driver = {
120 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200121 .bConfigurationValue = 2,
122 /* .iConfiguration = DYNAMIC */
123 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100124 .MaxPower = 100,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200125};
126
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100127static struct usb_function_instance *fi_acm;
128
129static int __init nokia_bind_config(struct usb_configuration *c)
130{
131 struct usb_function *f_acm;
132 int status = 0;
133
134 status = phonet_bind_config(c);
135 if (status)
136 printk(KERN_DEBUG "could not bind phonet config\n");
137
138 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]);
139 if (status)
140 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
141
142 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]);
143 if (status)
144 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
145
146 f_acm = usb_get_function(fi_acm);
147 if (IS_ERR(f_acm))
148 return PTR_ERR(f_acm);
149
150 status = usb_add_function(c, f_acm);
151 if (status)
152 goto err_conf;
153
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100154 status = ecm_bind_config(c, hostaddr, the_dev);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100155 if (status) {
156 pr_debug("could not bind ecm config %d\n", status);
157 goto err_ecm;
158 }
159 if (c == &nokia_config_500ma_driver)
160 f_acm_cfg1 = f_acm;
161 else
162 f_acm_cfg2 = f_acm;
163
164 return status;
165err_ecm:
166 usb_remove_function(c, f_acm);
167err_conf:
168 usb_put_function(f_acm);
169 return status;
170}
171
Felipe Balbif358f5b2010-01-05 16:10:13 +0200172static int __init nokia_bind(struct usb_composite_dev *cdev)
173{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200174 struct usb_gadget *gadget = cdev->gadget;
175 int status;
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100176 int cur_line;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200177
178 status = gphonet_setup(cdev->gadget);
179 if (status < 0)
180 goto err_phonet;
181
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100182 for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) {
183 status = gserial_alloc_line(&tty_lines[cur_line]);
184 if (status)
185 goto err_ether;
186 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200187
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100188 the_dev = gether_setup(cdev->gadget, hostaddr);
189 if (IS_ERR(the_dev)) {
190 status = PTR_ERR(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200191 goto err_ether;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100192 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200193
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200194 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200195 if (status < 0)
196 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200197 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
198 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200199 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200200 nokia_config_500ma_driver.iConfiguration = status;
201 nokia_config_100ma_driver.iConfiguration = status;
202
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200203 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200204 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200205
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100206 fi_acm = usb_get_function_instance("acm");
207 if (IS_ERR(fi_acm))
208 goto err_usb;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100209
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300210 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200211 status = usb_add_config(cdev, &nokia_config_500ma_driver,
212 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200213 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100214 goto err_acm_inst;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200215
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200216 status = usb_add_config(cdev, &nokia_config_100ma_driver,
217 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200218 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100219 goto err_put_cfg1;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200220
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200221 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200222 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
223
224 return 0;
225
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100226err_put_cfg1:
227 usb_put_function(f_acm_cfg1);
228err_acm_inst:
229 usb_put_function_instance(fi_acm);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200230err_usb:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100231 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200232err_ether:
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100233 cur_line--;
234 while (cur_line >= 0)
235 gserial_free_line(tty_lines[cur_line--]);
236
Felipe Balbif358f5b2010-01-05 16:10:13 +0200237 gphonet_cleanup();
238err_phonet:
239 return status;
240}
241
242static int __exit nokia_unbind(struct usb_composite_dev *cdev)
243{
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100244 int i;
245
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100246 usb_put_function(f_acm_cfg1);
247 usb_put_function(f_acm_cfg2);
248 usb_put_function_instance(fi_acm);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200249 gphonet_cleanup();
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100250
251 for (i = 0; i < TTY_PORTS_MAX; i++)
252 gserial_free_line(tty_lines[i]);
253
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100254 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200255
256 return 0;
257}
258
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200259static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200260 .name = "g_nokia",
261 .dev = &device_desc,
262 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300263 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200264 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200265 .unbind = __exit_p(nokia_unbind),
266};
267
268static int __init nokia_init(void)
269{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200270 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200271}
272module_init(nokia_init);
273
274static void __exit nokia_cleanup(void)
275{
276 usb_composite_unregister(&nokia_driver);
277}
278module_exit(nokia_cleanup);