blob: 084f947ffd79aebb04914add43f97b5e3e369681 [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"
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 */
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +020041#define USBF_ECM_INCLUDED
Felipe Balbi9b192de2013-04-03 21:01:44 +030042#include "f_ecm.c"
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020043#include "u_ether.h"
Felipe Balbif358f5b2010-01-05 16:10:13 +020044
45/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020046USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020047
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020048USB_ETHERNET_MODULE_PARAMETERS();
49
Felipe Balbif358f5b2010-01-05 16:10:13 +020050#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
51#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
52
53/* string IDs are assigned dynamically */
54
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020055#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
Felipe Balbif358f5b2010-01-05 16:10:13 +020056
57static char manufacturer_nokia[] = "Nokia";
58static const char product_nokia[] = NOKIA_LONG_NAME;
59static const char description_nokia[] = "PC-Suite Configuration";
60
61static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020062 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
63 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
64 [USB_GADGET_SERIAL_IDX].s = "",
Felipe Balbif358f5b2010-01-05 16:10:13 +020065 [STRING_DESCRIPTION_IDX].s = description_nokia,
66 { } /* end of list */
67};
68
69static struct usb_gadget_strings stringtab_dev = {
70 .language = 0x0409, /* en-us */
71 .strings = strings_dev,
72};
73
74static struct usb_gadget_strings *dev_strings[] = {
75 &stringtab_dev,
76 NULL,
77};
78
79static 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),
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +020086 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
Felipe Balbif358f5b2010-01-05 16:10:13 +020087 /* .iManufacturer = DYNAMIC */
88 /* .iProduct = DYNAMIC */
89 .bNumConfigurations = 1,
90};
91
92/*-------------------------------------------------------------------------*/
93
94/* Module */
95MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
96MODULE_AUTHOR("Felipe Balbi");
97MODULE_LICENSE("GPL");
98
99/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100100static struct usb_function *f_acm_cfg1;
101static struct usb_function *f_acm_cfg2;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200102static u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200103static struct usb_function *f_obex1_cfg1;
104static struct usb_function *f_obex2_cfg1;
105static struct usb_function *f_obex1_cfg2;
106static struct usb_function *f_obex2_cfg2;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200107static struct usb_function *f_phonet_cfg1;
108static struct usb_function *f_phonet_cfg2;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100109static struct eth_dev *the_dev;
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200110
Felipe Balbif358f5b2010-01-05 16:10:13 +0200111
Felipe Balbif358f5b2010-01-05 16:10:13 +0200112static struct usb_configuration nokia_config_500ma_driver = {
113 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200114 .bConfigurationValue = 1,
115 /* .iConfiguration = DYNAMIC */
116 .bmAttributes = USB_CONFIG_ATT_ONE,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100117 .MaxPower = 500,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200118};
119
120static struct usb_configuration nokia_config_100ma_driver = {
121 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200122 .bConfigurationValue = 2,
123 /* .iConfiguration = DYNAMIC */
124 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100125 .MaxPower = 100,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200126};
127
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100128static struct usb_function_instance *fi_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200129static struct usb_function_instance *fi_obex1;
130static struct usb_function_instance *fi_obex2;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200131static struct usb_function_instance *fi_phonet;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100132
133static int __init nokia_bind_config(struct usb_configuration *c)
134{
135 struct usb_function *f_acm;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200136 struct usb_function *f_phonet = NULL;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200137 struct usb_function *f_obex1 = NULL;
138 struct usb_function *f_obex2 = NULL;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100139 int status = 0;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200140 int obex1_stat = 0;
141 int obex2_stat = 0;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200142 int phonet_stat = 0;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100143
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200144 if (!IS_ERR(fi_phonet)) {
145 f_phonet = usb_get_function(fi_phonet);
146 if (IS_ERR(f_phonet))
147 pr_debug("could not get phonet function\n");
148 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100149
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200150 if (!IS_ERR(fi_obex1)) {
151 f_obex1 = usb_get_function(fi_obex1);
152 if (IS_ERR(f_obex1))
153 pr_debug("could not get obex function 0\n");
154 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100155
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200156 if (!IS_ERR(fi_obex2)) {
157 f_obex2 = usb_get_function(fi_obex2);
158 if (IS_ERR(f_obex2))
159 pr_debug("could not get obex function 1\n");
160 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100161
162 f_acm = usb_get_function(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200163 if (IS_ERR(f_acm)) {
164 status = PTR_ERR(f_acm);
165 goto err_get_acm;
166 }
167
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200168 if (!IS_ERR_OR_NULL(f_phonet)) {
169 phonet_stat = usb_add_function(c, f_phonet);
170 if (phonet_stat)
171 pr_debug("could not add phonet function\n");
172 }
173
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200174 if (!IS_ERR_OR_NULL(f_obex1)) {
175 obex1_stat = usb_add_function(c, f_obex1);
176 if (obex1_stat)
177 pr_debug("could not add obex function 0\n");
178 }
179
180 if (!IS_ERR_OR_NULL(f_obex2)) {
181 obex2_stat = usb_add_function(c, f_obex2);
182 if (obex2_stat)
183 pr_debug("could not add obex function 1\n");
184 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100185
186 status = usb_add_function(c, f_acm);
187 if (status)
188 goto err_conf;
189
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200190 status = ecm_bind_config(c, host_mac, the_dev);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100191 if (status) {
192 pr_debug("could not bind ecm config %d\n", status);
193 goto err_ecm;
194 }
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200195 if (c == &nokia_config_500ma_driver) {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100196 f_acm_cfg1 = f_acm;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200197 f_phonet_cfg1 = f_phonet;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200198 f_obex1_cfg1 = f_obex1;
199 f_obex2_cfg1 = f_obex2;
200 } else {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100201 f_acm_cfg2 = f_acm;
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200202 f_phonet_cfg2 = f_phonet;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200203 f_obex1_cfg2 = f_obex1;
204 f_obex2_cfg2 = f_obex2;
205 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100206
207 return status;
208err_ecm:
209 usb_remove_function(c, f_acm);
210err_conf:
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200211 if (!obex2_stat)
212 usb_remove_function(c, f_obex2);
213 if (!obex1_stat)
214 usb_remove_function(c, f_obex1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200215 if (!phonet_stat)
216 usb_remove_function(c, f_phonet);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100217 usb_put_function(f_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200218err_get_acm:
219 if (!IS_ERR_OR_NULL(f_obex2))
220 usb_put_function(f_obex2);
221 if (!IS_ERR_OR_NULL(f_obex1))
222 usb_put_function(f_obex1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200223 if (!IS_ERR_OR_NULL(f_phonet))
224 usb_put_function(f_phonet);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100225 return status;
226}
227
Felipe Balbif358f5b2010-01-05 16:10:13 +0200228static int __init nokia_bind(struct usb_composite_dev *cdev)
229{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200230 struct usb_gadget *gadget = cdev->gadget;
231 int status;
232
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200233 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
234 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100235 if (IS_ERR(the_dev)) {
236 status = PTR_ERR(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200237 goto err_ether;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100238 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200239
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200240 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200241 if (status < 0)
242 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200243 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
244 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200245 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200246 nokia_config_500ma_driver.iConfiguration = status;
247 nokia_config_100ma_driver.iConfiguration = status;
248
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200249 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200250 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200251
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200252 fi_phonet = usb_get_function_instance("phonet");
253 if (IS_ERR(fi_phonet))
254 pr_debug("could not find phonet function\n");
255
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200256 fi_obex1 = usb_get_function_instance("obex");
257 if (IS_ERR(fi_obex1))
258 pr_debug("could not find obex function 1\n");
259
260 fi_obex2 = usb_get_function_instance("obex");
261 if (IS_ERR(fi_obex2))
262 pr_debug("could not find obex function 2\n");
263
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100264 fi_acm = usb_get_function_instance("acm");
265 if (IS_ERR(fi_acm))
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200266 goto err_obex2_inst;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100267
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300268 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200269 status = usb_add_config(cdev, &nokia_config_500ma_driver,
270 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200271 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100272 goto err_acm_inst;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200273
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200274 status = usb_add_config(cdev, &nokia_config_100ma_driver,
275 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200276 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100277 goto err_put_cfg1;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200278
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200279 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200280 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
281
282 return 0;
283
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100284err_put_cfg1:
285 usb_put_function(f_acm_cfg1);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200286 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
287 usb_put_function(f_obex1_cfg1);
288 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
289 usb_put_function(f_obex2_cfg1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200290 if (!IS_ERR_OR_NULL(f_phonet_cfg1))
291 usb_put_function(f_phonet_cfg1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100292err_acm_inst:
293 usb_put_function_instance(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200294err_obex2_inst:
295 if (!IS_ERR(fi_obex2))
296 usb_put_function_instance(fi_obex2);
297 if (!IS_ERR(fi_obex1))
298 usb_put_function_instance(fi_obex1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200299 if (!IS_ERR(fi_phonet))
300 usb_put_function_instance(fi_phonet);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200301err_usb:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100302 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200303err_ether:
Felipe Balbif358f5b2010-01-05 16:10:13 +0200304 return status;
305}
306
307static int __exit nokia_unbind(struct usb_composite_dev *cdev)
308{
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200309 if (!IS_ERR_OR_NULL(f_obex1_cfg2))
310 usb_put_function(f_obex1_cfg2);
311 if (!IS_ERR_OR_NULL(f_obex2_cfg2))
312 usb_put_function(f_obex2_cfg2);
313 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
314 usb_put_function(f_obex1_cfg1);
315 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
316 usb_put_function(f_obex2_cfg1);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200317 if (!IS_ERR_OR_NULL(f_phonet_cfg1))
318 usb_put_function(f_phonet_cfg1);
319 if (!IS_ERR_OR_NULL(f_phonet_cfg2))
320 usb_put_function(f_phonet_cfg2);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100321 usb_put_function(f_acm_cfg1);
322 usb_put_function(f_acm_cfg2);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200323
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200324 if (!IS_ERR(fi_obex2))
325 usb_put_function_instance(fi_obex2);
Andrzej Pietrasiewicz83167f12013-05-23 10:51:12 +0200326 if (!IS_ERR(fi_obex1))
327 usb_put_function_instance(fi_obex1);
328 if (!IS_ERR(fi_phonet))
329 usb_put_function_instance(fi_phonet);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100330 usb_put_function_instance(fi_acm);
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100331
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100332 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200333
334 return 0;
335}
336
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200337static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200338 .name = "g_nokia",
339 .dev = &device_desc,
340 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300341 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200342 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200343 .unbind = __exit_p(nokia_unbind),
344};
345
346static int __init nokia_init(void)
347{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200348 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200349}
350module_init(nokia_init);
351
352static void __exit nokia_cleanup(void)
353{
354 usb_composite_unregister(&nokia_driver);
355}
356module_exit(nokia_cleanup);