blob: 5650ece3e09cba1bc3e7cb254bfb7bb0de5c5b23 [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"
Felipe Balbif358f5b2010-01-05 16:10:13 +020043#include "f_phonet.c"
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020044#include "u_ether.h"
Felipe Balbif358f5b2010-01-05 16:10:13 +020045
46/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020047USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020048
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020049USB_ETHERNET_MODULE_PARAMETERS();
50
Felipe Balbif358f5b2010-01-05 16:10:13 +020051#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
52#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
53
54/* string IDs are assigned dynamically */
55
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020056#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
Felipe Balbif358f5b2010-01-05 16:10:13 +020057
58static char manufacturer_nokia[] = "Nokia";
59static const char product_nokia[] = NOKIA_LONG_NAME;
60static const char description_nokia[] = "PC-Suite Configuration";
61
62static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020063 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
64 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
65 [USB_GADGET_SERIAL_IDX].s = "",
Felipe Balbif358f5b2010-01-05 16:10:13 +020066 [STRING_DESCRIPTION_IDX].s = description_nokia,
67 { } /* end of list */
68};
69
70static struct usb_gadget_strings stringtab_dev = {
71 .language = 0x0409, /* en-us */
72 .strings = strings_dev,
73};
74
75static struct usb_gadget_strings *dev_strings[] = {
76 &stringtab_dev,
77 NULL,
78};
79
80static struct usb_device_descriptor device_desc = {
81 .bLength = USB_DT_DEVICE_SIZE,
82 .bDescriptorType = USB_DT_DEVICE,
83 .bcdUSB = __constant_cpu_to_le16(0x0200),
84 .bDeviceClass = USB_CLASS_COMM,
85 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
86 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +020087 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
Felipe Balbif358f5b2010-01-05 16:10:13 +020088 /* .iManufacturer = DYNAMIC */
89 /* .iProduct = DYNAMIC */
90 .bNumConfigurations = 1,
91};
92
93/*-------------------------------------------------------------------------*/
94
95/* Module */
96MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
97MODULE_AUTHOR("Felipe Balbi");
98MODULE_LICENSE("GPL");
99
100/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100101static struct usb_function *f_acm_cfg1;
102static struct usb_function *f_acm_cfg2;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200103static u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200104static struct usb_function *f_obex1_cfg1;
105static struct usb_function *f_obex2_cfg1;
106static struct usb_function *f_obex1_cfg2;
107static struct usb_function *f_obex2_cfg2;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100108static struct eth_dev *the_dev;
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200109static struct net_device *phonet_dev;
110
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;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100131
132static int __init nokia_bind_config(struct usb_configuration *c)
133{
134 struct usb_function *f_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200135 struct usb_function *f_obex1 = NULL;
136 struct usb_function *f_obex2 = NULL;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100137 int status = 0;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200138 int obex1_stat = 0;
139 int obex2_stat = 0;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100140
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200141 status = phonet_bind_config(c, phonet_dev);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100142 if (status)
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200143 pr_debug("could not bind phonet config\n");
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100144
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200145 if (!IS_ERR(fi_obex1)) {
146 f_obex1 = usb_get_function(fi_obex1);
147 if (IS_ERR(f_obex1))
148 pr_debug("could not get obex function 0\n");
149 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100150
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200151 if (!IS_ERR(fi_obex2)) {
152 f_obex2 = usb_get_function(fi_obex2);
153 if (IS_ERR(f_obex2))
154 pr_debug("could not get obex function 1\n");
155 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100156
157 f_acm = usb_get_function(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200158 if (IS_ERR(f_acm)) {
159 status = PTR_ERR(f_acm);
160 goto err_get_acm;
161 }
162
163 if (!IS_ERR_OR_NULL(f_obex1)) {
164 obex1_stat = usb_add_function(c, f_obex1);
165 if (obex1_stat)
166 pr_debug("could not add obex function 0\n");
167 }
168
169 if (!IS_ERR_OR_NULL(f_obex2)) {
170 obex2_stat = usb_add_function(c, f_obex2);
171 if (obex2_stat)
172 pr_debug("could not add obex function 1\n");
173 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100174
175 status = usb_add_function(c, f_acm);
176 if (status)
177 goto err_conf;
178
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200179 status = ecm_bind_config(c, host_mac, the_dev);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100180 if (status) {
181 pr_debug("could not bind ecm config %d\n", status);
182 goto err_ecm;
183 }
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200184 if (c == &nokia_config_500ma_driver) {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100185 f_acm_cfg1 = f_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200186 f_obex1_cfg1 = f_obex1;
187 f_obex2_cfg1 = f_obex2;
188 } else {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100189 f_acm_cfg2 = f_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200190 f_obex1_cfg2 = f_obex1;
191 f_obex2_cfg2 = f_obex2;
192 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100193
194 return status;
195err_ecm:
196 usb_remove_function(c, f_acm);
197err_conf:
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200198 if (!obex2_stat)
199 usb_remove_function(c, f_obex2);
200 if (!obex1_stat)
201 usb_remove_function(c, f_obex1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100202 usb_put_function(f_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200203err_get_acm:
204 if (!IS_ERR_OR_NULL(f_obex2))
205 usb_put_function(f_obex2);
206 if (!IS_ERR_OR_NULL(f_obex1))
207 usb_put_function(f_obex1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100208 return status;
209}
210
Felipe Balbif358f5b2010-01-05 16:10:13 +0200211static int __init nokia_bind(struct usb_composite_dev *cdev)
212{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200213 struct usb_gadget *gadget = cdev->gadget;
214 int status;
215
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200216 phonet_dev = gphonet_setup(cdev->gadget);
217 if (IS_ERR(phonet_dev)) {
218 status = PTR_ERR(phonet_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200219 goto err_phonet;
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200220 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200221
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200222 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
223 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100224 if (IS_ERR(the_dev)) {
225 status = PTR_ERR(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200226 goto err_ether;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100227 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200228
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200229 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200230 if (status < 0)
231 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200232 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
233 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200234 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200235 nokia_config_500ma_driver.iConfiguration = status;
236 nokia_config_100ma_driver.iConfiguration = status;
237
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200238 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200239 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200240
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200241 fi_obex1 = usb_get_function_instance("obex");
242 if (IS_ERR(fi_obex1))
243 pr_debug("could not find obex function 1\n");
244
245 fi_obex2 = usb_get_function_instance("obex");
246 if (IS_ERR(fi_obex2))
247 pr_debug("could not find obex function 2\n");
248
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100249 fi_acm = usb_get_function_instance("acm");
250 if (IS_ERR(fi_acm))
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200251 goto err_obex2_inst;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100252
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300253 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200254 status = usb_add_config(cdev, &nokia_config_500ma_driver,
255 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200256 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100257 goto err_acm_inst;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200258
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200259 status = usb_add_config(cdev, &nokia_config_100ma_driver,
260 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200261 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100262 goto err_put_cfg1;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200263
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200264 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200265 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
266
267 return 0;
268
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100269err_put_cfg1:
270 usb_put_function(f_acm_cfg1);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200271 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
272 usb_put_function(f_obex1_cfg1);
273 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
274 usb_put_function(f_obex2_cfg1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100275err_acm_inst:
276 usb_put_function_instance(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200277err_obex2_inst:
278 if (!IS_ERR(fi_obex2))
279 usb_put_function_instance(fi_obex2);
280 if (!IS_ERR(fi_obex1))
281 usb_put_function_instance(fi_obex1);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200282err_usb:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100283 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200284err_ether:
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200285 gphonet_cleanup(phonet_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200286err_phonet:
287 return status;
288}
289
290static int __exit nokia_unbind(struct usb_composite_dev *cdev)
291{
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200292 if (!IS_ERR_OR_NULL(f_obex1_cfg2))
293 usb_put_function(f_obex1_cfg2);
294 if (!IS_ERR_OR_NULL(f_obex2_cfg2))
295 usb_put_function(f_obex2_cfg2);
296 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
297 usb_put_function(f_obex1_cfg1);
298 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
299 usb_put_function(f_obex2_cfg1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100300 usb_put_function(f_acm_cfg1);
301 usb_put_function(f_acm_cfg2);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200302 if (!IS_ERR(fi_obex1))
303 usb_put_function_instance(fi_obex1);
304 if (!IS_ERR(fi_obex2))
305 usb_put_function_instance(fi_obex2);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100306 usb_put_function_instance(fi_acm);
Andrzej Pietrasiewicz0189e632013-05-23 10:51:10 +0200307 gphonet_cleanup(phonet_dev);
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100308
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100309 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200310
311 return 0;
312}
313
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200314static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200315 .name = "g_nokia",
316 .dev = &device_desc,
317 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300318 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200319 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200320 .unbind = __exit_p(nokia_unbind),
321};
322
323static int __init nokia_init(void)
324{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200325 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200326}
327module_init(nokia_init);
328
329static void __exit nokia_cleanup(void)
330{
331 usb_composite_unregister(&nokia_driver);
332}
333module_exit(nokia_cleanup);