blob: 39f6cb5f984d53b1ae885ba0e9fd5b88e628c0a4 [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 Pietrasiewicz1d8fc252013-03-21 15:33:42 +010040#define USBF_OBEX_INCLUDED
Felipe Balbi9b192de2013-04-03 21:01:44 +030041#include "f_ecm.c"
Felipe Balbif358f5b2010-01-05 16:10:13 +020042#include "f_obex.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];
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100104static struct eth_dev *the_dev;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200105
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100106enum {
107 TTY_PORT_OBEX0,
108 TTY_PORT_OBEX1,
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100109 TTY_PORTS_MAX,
110};
111
112static unsigned char tty_lines[TTY_PORTS_MAX];
113
Felipe Balbif358f5b2010-01-05 16:10:13 +0200114static struct usb_configuration nokia_config_500ma_driver = {
115 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200116 .bConfigurationValue = 1,
117 /* .iConfiguration = DYNAMIC */
118 .bmAttributes = USB_CONFIG_ATT_ONE,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100119 .MaxPower = 500,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200120};
121
122static struct usb_configuration nokia_config_100ma_driver = {
123 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200124 .bConfigurationValue = 2,
125 /* .iConfiguration = DYNAMIC */
126 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100127 .MaxPower = 100,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200128};
129
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100130static struct usb_function_instance *fi_acm;
131
132static int __init nokia_bind_config(struct usb_configuration *c)
133{
134 struct usb_function *f_acm;
135 int status = 0;
136
137 status = phonet_bind_config(c);
138 if (status)
139 printk(KERN_DEBUG "could not bind phonet config\n");
140
141 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]);
142 if (status)
143 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
144
145 status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]);
146 if (status)
147 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
148
149 f_acm = usb_get_function(fi_acm);
150 if (IS_ERR(f_acm))
151 return PTR_ERR(f_acm);
152
153 status = usb_add_function(c, f_acm);
154 if (status)
155 goto err_conf;
156
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200157 status = ecm_bind_config(c, host_mac, the_dev);
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100158 if (status) {
159 pr_debug("could not bind ecm config %d\n", status);
160 goto err_ecm;
161 }
162 if (c == &nokia_config_500ma_driver)
163 f_acm_cfg1 = f_acm;
164 else
165 f_acm_cfg2 = f_acm;
166
167 return status;
168err_ecm:
169 usb_remove_function(c, f_acm);
170err_conf:
171 usb_put_function(f_acm);
172 return status;
173}
174
Felipe Balbif358f5b2010-01-05 16:10:13 +0200175static int __init nokia_bind(struct usb_composite_dev *cdev)
176{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200177 struct usb_gadget *gadget = cdev->gadget;
178 int status;
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100179 int cur_line;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200180
181 status = gphonet_setup(cdev->gadget);
182 if (status < 0)
183 goto err_phonet;
184
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100185 for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) {
186 status = gserial_alloc_line(&tty_lines[cur_line]);
187 if (status)
188 goto err_ether;
189 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200190
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200191 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
192 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100193 if (IS_ERR(the_dev)) {
194 status = PTR_ERR(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200195 goto err_ether;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100196 }
Felipe Balbif358f5b2010-01-05 16:10:13 +0200197
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200198 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200199 if (status < 0)
200 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200201 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
202 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200203 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200204 nokia_config_500ma_driver.iConfiguration = status;
205 nokia_config_100ma_driver.iConfiguration = status;
206
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200207 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200208 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200209
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100210 fi_acm = usb_get_function_instance("acm");
211 if (IS_ERR(fi_acm))
212 goto err_usb;
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100213
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300214 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200215 status = usb_add_config(cdev, &nokia_config_500ma_driver,
216 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200217 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100218 goto err_acm_inst;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200219
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200220 status = usb_add_config(cdev, &nokia_config_100ma_driver,
221 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200222 if (status < 0)
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100223 goto err_put_cfg1;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200224
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200225 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200226 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
227
228 return 0;
229
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100230err_put_cfg1:
231 usb_put_function(f_acm_cfg1);
232err_acm_inst:
233 usb_put_function_instance(fi_acm);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200234err_usb:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100235 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200236err_ether:
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100237 cur_line--;
238 while (cur_line >= 0)
239 gserial_free_line(tty_lines[cur_line--]);
240
Felipe Balbif358f5b2010-01-05 16:10:13 +0200241 gphonet_cleanup();
242err_phonet:
243 return status;
244}
245
246static int __exit nokia_unbind(struct usb_composite_dev *cdev)
247{
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100248 int i;
249
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100250 usb_put_function(f_acm_cfg1);
251 usb_put_function(f_acm_cfg2);
252 usb_put_function_instance(fi_acm);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200253 gphonet_cleanup();
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100254
255 for (i = 0; i < TTY_PORTS_MAX; i++)
256 gserial_free_line(tty_lines[i]);
257
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100258 gether_cleanup(the_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200259
260 return 0;
261}
262
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200263static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200264 .name = "g_nokia",
265 .dev = &device_desc,
266 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300267 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200268 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200269 .unbind = __exit_p(nokia_unbind),
270};
271
272static int __init nokia_init(void)
273{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200274 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200275}
276module_init(nokia_init);
277
278static void __exit nokia_cleanup(void)
279{
280 usb_composite_unregister(&nokia_driver);
281}
282module_exit(nokia_cleanup);