blob: 8e42c88b453b3d7fc4e2c664ac8ee492344a4a9a [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 Pietrasiewiczfee562a2013-05-23 10:32:03 +020040#define USBF_ECM_INCLUDED
Felipe Balbi9b192de2013-04-03 21:01:44 +030041#include "f_ecm.c"
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +020042#define USBF_OBEX_INCLUDED
Felipe Balbif358f5b2010-01-05 16:10:13 +020043#include "f_obex.c"
Felipe Balbif358f5b2010-01-05 16:10:13 +020044#include "f_phonet.c"
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020045#include "u_ether.h"
Felipe Balbif358f5b2010-01-05 16:10:13 +020046
47/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020048USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020049
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020050USB_ETHERNET_MODULE_PARAMETERS();
51
Felipe Balbif358f5b2010-01-05 16:10:13 +020052#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
53#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
54
55/* string IDs are assigned dynamically */
56
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020057#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
Felipe Balbif358f5b2010-01-05 16:10:13 +020058
59static char manufacturer_nokia[] = "Nokia";
60static const char product_nokia[] = NOKIA_LONG_NAME;
61static const char description_nokia[] = "PC-Suite Configuration";
62
63static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020064 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
65 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
66 [USB_GADGET_SERIAL_IDX].s = "",
Felipe Balbif358f5b2010-01-05 16:10:13 +020067 [STRING_DESCRIPTION_IDX].s = description_nokia,
68 { } /* end of list */
69};
70
71static struct usb_gadget_strings stringtab_dev = {
72 .language = 0x0409, /* en-us */
73 .strings = strings_dev,
74};
75
76static struct usb_gadget_strings *dev_strings[] = {
77 &stringtab_dev,
78 NULL,
79};
80
81static struct usb_device_descriptor device_desc = {
82 .bLength = USB_DT_DEVICE_SIZE,
83 .bDescriptorType = USB_DT_DEVICE,
84 .bcdUSB = __constant_cpu_to_le16(0x0200),
85 .bDeviceClass = USB_CLASS_COMM,
86 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
87 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +020088 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
Felipe Balbif358f5b2010-01-05 16:10:13 +020089 /* .iManufacturer = DYNAMIC */
90 /* .iProduct = DYNAMIC */
91 .bNumConfigurations = 1,
92};
93
94/*-------------------------------------------------------------------------*/
95
96/* Module */
97MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
98MODULE_AUTHOR("Felipe Balbi");
99MODULE_LICENSE("GPL");
100
101/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100102static struct usb_function *f_acm_cfg1;
103static struct usb_function *f_acm_cfg2;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200104static u8 host_mac[ETH_ALEN];
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100105static struct eth_dev *the_dev;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200106
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100107enum {
108 TTY_PORT_OBEX0,
109 TTY_PORT_OBEX1,
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100110 TTY_PORTS_MAX,
111};
112
113static unsigned char tty_lines[TTY_PORTS_MAX];
114
Felipe Balbif358f5b2010-01-05 16:10:13 +0200115static struct usb_configuration nokia_config_500ma_driver = {
116 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200117 .bConfigurationValue = 1,
118 /* .iConfiguration = DYNAMIC */
119 .bmAttributes = USB_CONFIG_ATT_ONE,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100120 .MaxPower = 500,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200121};
122
123static struct usb_configuration nokia_config_100ma_driver = {
124 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200125 .bConfigurationValue = 2,
126 /* .iConfiguration = DYNAMIC */
127 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100128 .MaxPower = 100,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200129};
130
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100131static struct usb_function_instance *fi_acm;
132
133static int __init nokia_bind_config(struct usb_configuration *c)
134{
135 struct usb_function *f_acm;
136 int status = 0;
137
138 status = phonet_bind_config(c);
139 if (status)
140 printk(KERN_DEBUG "could not bind phonet config\n");
141
142 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]);
143 if (status)
144 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
145
146 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]);
147 if (status)
148 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
149
150 f_acm = usb_get_function(fi_acm);
151 if (IS_ERR(f_acm))
152 return PTR_ERR(f_acm);
153
154 status = usb_add_function(c, f_acm);
155 if (status)
156 goto err_conf;
157
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200158 status = ecm_bind_config(c, host_mac, the_dev);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100159 if (status) {
160 pr_debug("could not bind ecm config %d\n", status);
161 goto err_ecm;
162 }
163 if (c == &nokia_config_500ma_driver)
164 f_acm_cfg1 = f_acm;
165 else
166 f_acm_cfg2 = f_acm;
167
168 return status;
169err_ecm:
170 usb_remove_function(c, f_acm);
171err_conf:
172 usb_put_function(f_acm);
173 return status;
174}
175
Felipe Balbif358f5b2010-01-05 16:10:13 +0200176static int __init nokia_bind(struct usb_composite_dev *cdev)
177{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200178 struct usb_gadget *gadget = cdev->gadget;
179 int status;
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100180 int cur_line;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200181
182 status = gphonet_setup(cdev->gadget);
183 if (status < 0)
184 goto err_phonet;
185
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100186 for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) {
187 status = gserial_alloc_line(&tty_lines[cur_line]);
188 if (status)
189 goto err_ether;
190 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200191
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200192 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
193 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100194 if (IS_ERR(the_dev)) {
195 status = PTR_ERR(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200196 goto err_ether;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100197 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200198
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200199 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200200 if (status < 0)
201 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200202 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
203 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200204 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200205 nokia_config_500ma_driver.iConfiguration = status;
206 nokia_config_100ma_driver.iConfiguration = status;
207
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200208 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200209 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200210
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100211 fi_acm = usb_get_function_instance("acm");
212 if (IS_ERR(fi_acm))
213 goto err_usb;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100214
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300215 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200216 status = usb_add_config(cdev, &nokia_config_500ma_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_acm_inst;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200220
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200221 status = usb_add_config(cdev, &nokia_config_100ma_driver,
222 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200223 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100224 goto err_put_cfg1;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200225
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200226 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200227 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
228
229 return 0;
230
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100231err_put_cfg1:
232 usb_put_function(f_acm_cfg1);
233err_acm_inst:
234 usb_put_function_instance(fi_acm);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200235err_usb:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100236 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200237err_ether:
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100238 cur_line--;
239 while (cur_line >= 0)
240 gserial_free_line(tty_lines[cur_line--]);
241
Felipe Balbif358f5b2010-01-05 16:10:13 +0200242 gphonet_cleanup();
243err_phonet:
244 return status;
245}
246
247static int __exit nokia_unbind(struct usb_composite_dev *cdev)
248{
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100249 int i;
250
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100251 usb_put_function(f_acm_cfg1);
252 usb_put_function(f_acm_cfg2);
253 usb_put_function_instance(fi_acm);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200254 gphonet_cleanup();
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100255
256 for (i = 0; i < TTY_PORTS_MAX; i++)
257 gserial_free_line(tty_lines[i]);
258
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100259 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200260
261 return 0;
262}
263
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200264static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200265 .name = "g_nokia",
266 .dev = &device_desc,
267 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300268 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200269 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200270 .unbind = __exit_p(nokia_unbind),
271};
272
273static int __init nokia_init(void)
274{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200275 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200276}
277module_init(nokia_init);
278
279static void __exit nokia_cleanup(void)
280{
281 usb_composite_unregister(&nokia_driver);
282}
283module_exit(nokia_cleanup);