blob: 0a8099a488c4b4dfe77f97f4c7b78e6b17d69353 [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>
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +020019#include <linux/module.h>
Felipe Balbif358f5b2010-01-05 16:10:13 +020020#include <linux/device.h>
21
22#include "u_serial.h"
23#include "u_ether.h"
24#include "u_phonet.h"
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +020025#include "u_ecm.h"
Felipe Balbif358f5b2010-01-05 16:10:13 +020026#include "gadget_chips.h"
27
28/* Defines */
29
30#define NOKIA_VERSION_NUM 0x0211
31#define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
32
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020033USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020034
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020035USB_ETHERNET_MODULE_PARAMETERS();
36
Felipe Balbif358f5b2010-01-05 16:10:13 +020037#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
38#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
39
40/* string IDs are assigned dynamically */
41
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020042#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
Felipe Balbif358f5b2010-01-05 16:10:13 +020043
44static char manufacturer_nokia[] = "Nokia";
45static const char product_nokia[] = NOKIA_LONG_NAME;
46static const char description_nokia[] = "PC-Suite Configuration";
47
48static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020049 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
50 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
51 [USB_GADGET_SERIAL_IDX].s = "",
Felipe Balbif358f5b2010-01-05 16:10:13 +020052 [STRING_DESCRIPTION_IDX].s = description_nokia,
53 { } /* end of list */
54};
55
56static struct usb_gadget_strings stringtab_dev = {
57 .language = 0x0409, /* en-us */
58 .strings = strings_dev,
59};
60
61static struct usb_gadget_strings *dev_strings[] = {
62 &stringtab_dev,
63 NULL,
64};
65
66static struct usb_device_descriptor device_desc = {
67 .bLength = USB_DT_DEVICE_SIZE,
68 .bDescriptorType = USB_DT_DEVICE,
69 .bcdUSB = __constant_cpu_to_le16(0x0200),
70 .bDeviceClass = USB_CLASS_COMM,
71 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
72 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +020073 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
Felipe Balbif358f5b2010-01-05 16:10:13 +020074 /* .iManufacturer = DYNAMIC */
75 /* .iProduct = DYNAMIC */
76 .bNumConfigurations = 1,
77};
78
79/*-------------------------------------------------------------------------*/
80
81/* Module */
82MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
83MODULE_AUTHOR("Felipe Balbi");
84MODULE_LICENSE("GPL");
85
86/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +010087static struct usb_function *f_acm_cfg1;
88static struct usb_function *f_acm_cfg2;
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +020089static struct usb_function *f_ecm_cfg1;
90static struct usb_function *f_ecm_cfg2;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +020091static struct usb_function *f_obex1_cfg1;
92static struct usb_function *f_obex2_cfg1;
93static struct usb_function *f_obex1_cfg2;
94static struct usb_function *f_obex2_cfg2;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +020095static struct usb_function *f_phonet_cfg1;
96static struct usb_function *f_phonet_cfg2;
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +020097
Felipe Balbif358f5b2010-01-05 16:10:13 +020098
Felipe Balbif358f5b2010-01-05 16:10:13 +020099static struct usb_configuration nokia_config_500ma_driver = {
100 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200101 .bConfigurationValue = 1,
102 /* .iConfiguration = DYNAMIC */
103 .bmAttributes = USB_CONFIG_ATT_ONE,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100104 .MaxPower = 500,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200105};
106
107static struct usb_configuration nokia_config_100ma_driver = {
108 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200109 .bConfigurationValue = 2,
110 /* .iConfiguration = DYNAMIC */
111 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100112 .MaxPower = 100,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200113};
114
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100115static struct usb_function_instance *fi_acm;
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200116static struct usb_function_instance *fi_ecm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200117static struct usb_function_instance *fi_obex1;
118static struct usb_function_instance *fi_obex2;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200119static struct usb_function_instance *fi_phonet;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100120
121static int __init nokia_bind_config(struct usb_configuration *c)
122{
123 struct usb_function *f_acm;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200124 struct usb_function *f_phonet = NULL;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200125 struct usb_function *f_obex1 = NULL;
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200126 struct usb_function *f_ecm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200127 struct usb_function *f_obex2 = NULL;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100128 int status = 0;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200129 int obex1_stat = 0;
130 int obex2_stat = 0;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200131 int phonet_stat = 0;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100132
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200133 if (!IS_ERR(fi_phonet)) {
134 f_phonet = usb_get_function(fi_phonet);
135 if (IS_ERR(f_phonet))
136 pr_debug("could not get phonet function\n");
137 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100138
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200139 if (!IS_ERR(fi_obex1)) {
140 f_obex1 = usb_get_function(fi_obex1);
141 if (IS_ERR(f_obex1))
142 pr_debug("could not get obex function 0\n");
143 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100144
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200145 if (!IS_ERR(fi_obex2)) {
146 f_obex2 = usb_get_function(fi_obex2);
147 if (IS_ERR(f_obex2))
148 pr_debug("could not get obex function 1\n");
149 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100150
151 f_acm = usb_get_function(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200152 if (IS_ERR(f_acm)) {
153 status = PTR_ERR(f_acm);
154 goto err_get_acm;
155 }
156
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200157 f_ecm = usb_get_function(fi_ecm);
158 if (IS_ERR(f_ecm)) {
159 status = PTR_ERR(f_ecm);
160 goto err_get_ecm;
161 }
162
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200163 if (!IS_ERR_OR_NULL(f_phonet)) {
164 phonet_stat = usb_add_function(c, f_phonet);
165 if (phonet_stat)
166 pr_debug("could not add phonet function\n");
167 }
168
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200169 if (!IS_ERR_OR_NULL(f_obex1)) {
170 obex1_stat = usb_add_function(c, f_obex1);
171 if (obex1_stat)
172 pr_debug("could not add obex function 0\n");
173 }
174
175 if (!IS_ERR_OR_NULL(f_obex2)) {
176 obex2_stat = usb_add_function(c, f_obex2);
177 if (obex2_stat)
178 pr_debug("could not add obex function 1\n");
179 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100180
181 status = usb_add_function(c, f_acm);
182 if (status)
183 goto err_conf;
184
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200185 status = usb_add_function(c, f_ecm);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100186 if (status) {
187 pr_debug("could not bind ecm config %d\n", status);
188 goto err_ecm;
189 }
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200190 if (c == &nokia_config_500ma_driver) {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100191 f_acm_cfg1 = f_acm;
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200192 f_ecm_cfg1 = f_ecm;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200193 f_phonet_cfg1 = f_phonet;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200194 f_obex1_cfg1 = f_obex1;
195 f_obex2_cfg1 = f_obex2;
196 } else {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100197 f_acm_cfg2 = f_acm;
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200198 f_ecm_cfg2 = f_ecm;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200199 f_phonet_cfg2 = f_phonet;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200200 f_obex1_cfg2 = f_obex1;
201 f_obex2_cfg2 = f_obex2;
202 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100203
204 return status;
205err_ecm:
206 usb_remove_function(c, f_acm);
207err_conf:
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200208 if (!obex2_stat)
209 usb_remove_function(c, f_obex2);
210 if (!obex1_stat)
211 usb_remove_function(c, f_obex1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200212 if (!phonet_stat)
213 usb_remove_function(c, f_phonet);
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200214 usb_put_function(f_ecm);
215err_get_ecm:
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100216 usb_put_function(f_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200217err_get_acm:
218 if (!IS_ERR_OR_NULL(f_obex2))
219 usb_put_function(f_obex2);
220 if (!IS_ERR_OR_NULL(f_obex1))
221 usb_put_function(f_obex1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200222 if (!IS_ERR_OR_NULL(f_phonet))
223 usb_put_function(f_phonet);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100224 return status;
225}
226
Felipe Balbif358f5b2010-01-05 16:10:13 +0200227static int __init nokia_bind(struct usb_composite_dev *cdev)
228{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200229 struct usb_gadget *gadget = cdev->gadget;
230 int status;
231
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200232 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200233 if (status < 0)
234 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200235 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
236 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200237 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200238 nokia_config_500ma_driver.iConfiguration = status;
239 nokia_config_100ma_driver.iConfiguration = status;
240
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200241 if (!gadget_supports_altsettings(gadget)) {
242 status = -ENODEV;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200243 goto err_usb;
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200244 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200245
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200246 fi_phonet = usb_get_function_instance("phonet");
247 if (IS_ERR(fi_phonet))
248 pr_debug("could not find phonet function\n");
249
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200250 fi_obex1 = usb_get_function_instance("obex");
251 if (IS_ERR(fi_obex1))
252 pr_debug("could not find obex function 1\n");
253
254 fi_obex2 = usb_get_function_instance("obex");
255 if (IS_ERR(fi_obex2))
256 pr_debug("could not find obex function 2\n");
257
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100258 fi_acm = usb_get_function_instance("acm");
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200259 if (IS_ERR(fi_acm)) {
260 status = PTR_ERR(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200261 goto err_obex2_inst;
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200262 }
263
264 fi_ecm = usb_get_function_instance("ecm");
265 if (IS_ERR(fi_ecm)) {
266 status = PTR_ERR(fi_ecm);
267 goto err_acm_inst;
268 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100269
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300270 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200271 status = usb_add_config(cdev, &nokia_config_500ma_driver,
272 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200273 if (status < 0)
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200274 goto err_ecm_inst;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200275
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200276 status = usb_add_config(cdev, &nokia_config_100ma_driver,
277 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200278 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100279 goto err_put_cfg1;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200280
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200281 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200282 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
283
284 return 0;
285
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100286err_put_cfg1:
287 usb_put_function(f_acm_cfg1);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200288 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
289 usb_put_function(f_obex1_cfg1);
290 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
291 usb_put_function(f_obex2_cfg1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200292 if (!IS_ERR_OR_NULL(f_phonet_cfg1))
293 usb_put_function(f_phonet_cfg1);
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200294 usb_put_function(f_ecm_cfg1);
295err_ecm_inst:
296 usb_put_function_instance(fi_ecm);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100297err_acm_inst:
298 usb_put_function_instance(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200299err_obex2_inst:
300 if (!IS_ERR(fi_obex2))
301 usb_put_function_instance(fi_obex2);
302 if (!IS_ERR(fi_obex1))
303 usb_put_function_instance(fi_obex1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200304 if (!IS_ERR(fi_phonet))
305 usb_put_function_instance(fi_phonet);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200306err_usb:
Felipe Balbif358f5b2010-01-05 16:10:13 +0200307 return status;
308}
309
310static int __exit nokia_unbind(struct usb_composite_dev *cdev)
311{
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200312 if (!IS_ERR_OR_NULL(f_obex1_cfg2))
313 usb_put_function(f_obex1_cfg2);
314 if (!IS_ERR_OR_NULL(f_obex2_cfg2))
315 usb_put_function(f_obex2_cfg2);
316 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
317 usb_put_function(f_obex1_cfg1);
318 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
319 usb_put_function(f_obex2_cfg1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200320 if (!IS_ERR_OR_NULL(f_phonet_cfg1))
321 usb_put_function(f_phonet_cfg1);
322 if (!IS_ERR_OR_NULL(f_phonet_cfg2))
323 usb_put_function(f_phonet_cfg2);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100324 usb_put_function(f_acm_cfg1);
325 usb_put_function(f_acm_cfg2);
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200326 usb_put_function(f_ecm_cfg1);
327 usb_put_function(f_ecm_cfg2);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200328
Andrzej Pietrasiewiczb904d082013-05-23 10:51:14 +0200329 usb_put_function_instance(fi_ecm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200330 if (!IS_ERR(fi_obex2))
331 usb_put_function_instance(fi_obex2);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200332 if (!IS_ERR(fi_obex1))
333 usb_put_function_instance(fi_obex1);
334 if (!IS_ERR(fi_phonet))
335 usb_put_function_instance(fi_phonet);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100336 usb_put_function_instance(fi_acm);
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100337
Felipe Balbif358f5b2010-01-05 16:10:13 +0200338 return 0;
339}
340
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200341static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200342 .name = "g_nokia",
343 .dev = &device_desc,
344 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300345 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200346 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200347 .unbind = __exit_p(nokia_unbind),
348};
349
350static int __init nokia_init(void)
351{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200352 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200353}
354module_init(nokia_init);
355
356static void __exit nokia_cleanup(void)
357{
358 usb_composite_unregister(&nokia_driver);
359}
360module_exit(nokia_cleanup);