blob: a69e8bfb3ecb91443fda555fbebe618b69e55a1c [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;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200109
Felipe Balbif358f5b2010-01-05 16:10:13 +0200110static struct usb_configuration nokia_config_500ma_driver = {
111 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200112 .bConfigurationValue = 1,
113 /* .iConfiguration = DYNAMIC */
114 .bmAttributes = USB_CONFIG_ATT_ONE,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100115 .MaxPower = 500,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200116};
117
118static struct usb_configuration nokia_config_100ma_driver = {
119 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200120 .bConfigurationValue = 2,
121 /* .iConfiguration = DYNAMIC */
122 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100123 .MaxPower = 100,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200124};
125
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100126static struct usb_function_instance *fi_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200127static struct usb_function_instance *fi_obex1;
128static struct usb_function_instance *fi_obex2;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100129
130static int __init nokia_bind_config(struct usb_configuration *c)
131{
132 struct usb_function *f_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200133 struct usb_function *f_obex1 = NULL;
134 struct usb_function *f_obex2 = NULL;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100135 int status = 0;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200136 int obex1_stat = 0;
137 int obex2_stat = 0;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100138
139 status = phonet_bind_config(c);
140 if (status)
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200141 pr_debug("could not bind phonet config\n");
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100142
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200143 if (!IS_ERR(fi_obex1)) {
144 f_obex1 = usb_get_function(fi_obex1);
145 if (IS_ERR(f_obex1))
146 pr_debug("could not get obex function 0\n");
147 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100148
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200149 if (!IS_ERR(fi_obex2)) {
150 f_obex2 = usb_get_function(fi_obex2);
151 if (IS_ERR(f_obex2))
152 pr_debug("could not get obex function 1\n");
153 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100154
155 f_acm = usb_get_function(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200156 if (IS_ERR(f_acm)) {
157 status = PTR_ERR(f_acm);
158 goto err_get_acm;
159 }
160
161 if (!IS_ERR_OR_NULL(f_obex1)) {
162 obex1_stat = usb_add_function(c, f_obex1);
163 if (obex1_stat)
164 pr_debug("could not add obex function 0\n");
165 }
166
167 if (!IS_ERR_OR_NULL(f_obex2)) {
168 obex2_stat = usb_add_function(c, f_obex2);
169 if (obex2_stat)
170 pr_debug("could not add obex function 1\n");
171 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100172
173 status = usb_add_function(c, f_acm);
174 if (status)
175 goto err_conf;
176
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200177 status = ecm_bind_config(c, host_mac, the_dev);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100178 if (status) {
179 pr_debug("could not bind ecm config %d\n", status);
180 goto err_ecm;
181 }
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200182 if (c == &nokia_config_500ma_driver) {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100183 f_acm_cfg1 = f_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200184 f_obex1_cfg1 = f_obex1;
185 f_obex2_cfg1 = f_obex2;
186 } else {
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100187 f_acm_cfg2 = f_acm;
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200188 f_obex1_cfg2 = f_obex1;
189 f_obex2_cfg2 = f_obex2;
190 }
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100191
192 return status;
193err_ecm:
194 usb_remove_function(c, f_acm);
195err_conf:
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200196 if (!obex2_stat)
197 usb_remove_function(c, f_obex2);
198 if (!obex1_stat)
199 usb_remove_function(c, f_obex1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100200 usb_put_function(f_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200201err_get_acm:
202 if (!IS_ERR_OR_NULL(f_obex2))
203 usb_put_function(f_obex2);
204 if (!IS_ERR_OR_NULL(f_obex1))
205 usb_put_function(f_obex1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100206 return status;
207}
208
Felipe Balbif358f5b2010-01-05 16:10:13 +0200209static int __init nokia_bind(struct usb_composite_dev *cdev)
210{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200211 struct usb_gadget *gadget = cdev->gadget;
212 int status;
213
214 status = gphonet_setup(cdev->gadget);
215 if (status < 0)
216 goto err_phonet;
217
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200218 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
219 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100220 if (IS_ERR(the_dev)) {
221 status = PTR_ERR(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200222 goto err_ether;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100223 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200224
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200225 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200226 if (status < 0)
227 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200228 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
229 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200230 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200231 nokia_config_500ma_driver.iConfiguration = status;
232 nokia_config_100ma_driver.iConfiguration = status;
233
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200234 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200235 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200236
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200237 fi_obex1 = usb_get_function_instance("obex");
238 if (IS_ERR(fi_obex1))
239 pr_debug("could not find obex function 1\n");
240
241 fi_obex2 = usb_get_function_instance("obex");
242 if (IS_ERR(fi_obex2))
243 pr_debug("could not find obex function 2\n");
244
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100245 fi_acm = usb_get_function_instance("acm");
246 if (IS_ERR(fi_acm))
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200247 goto err_obex2_inst;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100248
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300249 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200250 status = usb_add_config(cdev, &nokia_config_500ma_driver,
251 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200252 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100253 goto err_acm_inst;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200254
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200255 status = usb_add_config(cdev, &nokia_config_100ma_driver,
256 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200257 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100258 goto err_put_cfg1;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200259
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200260 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200261 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
262
263 return 0;
264
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100265err_put_cfg1:
266 usb_put_function(f_acm_cfg1);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200267 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
268 usb_put_function(f_obex1_cfg1);
269 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
270 usb_put_function(f_obex2_cfg1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100271err_acm_inst:
272 usb_put_function_instance(fi_acm);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200273err_obex2_inst:
274 if (!IS_ERR(fi_obex2))
275 usb_put_function_instance(fi_obex2);
276 if (!IS_ERR(fi_obex1))
277 usb_put_function_instance(fi_obex1);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200278err_usb:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100279 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200280err_ether:
Felipe Balbif358f5b2010-01-05 16:10:13 +0200281 gphonet_cleanup();
282err_phonet:
283 return status;
284}
285
286static int __exit nokia_unbind(struct usb_composite_dev *cdev)
287{
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200288 if (!IS_ERR_OR_NULL(f_obex1_cfg2))
289 usb_put_function(f_obex1_cfg2);
290 if (!IS_ERR_OR_NULL(f_obex2_cfg2))
291 usb_put_function(f_obex2_cfg2);
292 if (!IS_ERR_OR_NULL(f_obex1_cfg1))
293 usb_put_function(f_obex1_cfg1);
294 if (!IS_ERR_OR_NULL(f_obex2_cfg1))
295 usb_put_function(f_obex2_cfg1);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100296 usb_put_function(f_acm_cfg1);
297 usb_put_function(f_acm_cfg2);
Andrzej Pietrasiewicz3a343442013-05-23 10:51:08 +0200298 if (!IS_ERR(fi_obex1))
299 usb_put_function_instance(fi_obex1);
300 if (!IS_ERR(fi_obex2))
301 usb_put_function_instance(fi_obex2);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100302 usb_put_function_instance(fi_acm);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200303 gphonet_cleanup();
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100304
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100305 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200306
307 return 0;
308}
309
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200310static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200311 .name = "g_nokia",
312 .dev = &device_desc,
313 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300314 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200315 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200316 .unbind = __exit_p(nokia_unbind),
317};
318
319static int __init nokia_init(void)
320{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200321 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200322}
323module_init(nokia_init);
324
325static void __exit nokia_cleanup(void)
326{
327 usb_composite_unregister(&nokia_driver);
328}
329module_exit(nokia_cleanup);