blob: 5ed927b16c0e3d7dd0a84f840f7b2e5abd1be136 [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>
19#include <linux/utsname.h>
20#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 */
41#include "composite.c"
Felipe Balbif358f5b2010-01-05 16:10:13 +020042
43#include "u_serial.c"
44#include "f_acm.c"
45#include "f_ecm.c"
46#include "f_obex.c"
47#include "f_serial.c"
48#include "f_phonet.c"
49#include "u_ether.c"
50
51/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020052USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020053
54#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
55#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
56
57/* string IDs are assigned dynamically */
58
59#define STRING_MANUFACTURER_IDX 0
60#define STRING_PRODUCT_IDX 1
61#define STRING_DESCRIPTION_IDX 2
62
63static char manufacturer_nokia[] = "Nokia";
64static const char product_nokia[] = NOKIA_LONG_NAME;
65static const char description_nokia[] = "PC-Suite Configuration";
66
67static struct usb_string strings_dev[] = {
68 [STRING_MANUFACTURER_IDX].s = manufacturer_nokia,
69 [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME,
70 [STRING_DESCRIPTION_IDX].s = description_nokia,
71 { } /* end of list */
72};
73
74static struct usb_gadget_strings stringtab_dev = {
75 .language = 0x0409, /* en-us */
76 .strings = strings_dev,
77};
78
79static struct usb_gadget_strings *dev_strings[] = {
80 &stringtab_dev,
81 NULL,
82};
83
84static struct usb_device_descriptor device_desc = {
85 .bLength = USB_DT_DEVICE_SIZE,
86 .bDescriptorType = USB_DT_DEVICE,
87 .bcdUSB = __constant_cpu_to_le16(0x0200),
88 .bDeviceClass = USB_CLASS_COMM,
89 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
90 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
91 /* .iManufacturer = DYNAMIC */
92 /* .iProduct = DYNAMIC */
93 .bNumConfigurations = 1,
94};
95
96/*-------------------------------------------------------------------------*/
97
98/* Module */
99MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
100MODULE_AUTHOR("Felipe Balbi");
101MODULE_LICENSE("GPL");
102
103/*-------------------------------------------------------------------------*/
104
105static u8 hostaddr[ETH_ALEN];
106
107static int __init nokia_bind_config(struct usb_configuration *c)
108{
109 int status = 0;
110
111 status = phonet_bind_config(c);
112 if (status)
113 printk(KERN_DEBUG "could not bind phonet config\n");
114
115 status = obex_bind_config(c, 0);
116 if (status)
117 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
118
119 status = obex_bind_config(c, 1);
120 if (status)
121 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
122
123 status = acm_bind_config(c, 2);
124 if (status)
125 printk(KERN_DEBUG "could not bind acm config\n");
126
127 status = ecm_bind_config(c, hostaddr);
128 if (status)
129 printk(KERN_DEBUG "could not bind ecm config\n");
130
131 return status;
132}
133
134static struct usb_configuration nokia_config_500ma_driver = {
135 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200136 .bConfigurationValue = 1,
137 /* .iConfiguration = DYNAMIC */
138 .bmAttributes = USB_CONFIG_ATT_ONE,
139 .bMaxPower = 250, /* 500mA */
140};
141
142static struct usb_configuration nokia_config_100ma_driver = {
143 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200144 .bConfigurationValue = 2,
145 /* .iConfiguration = DYNAMIC */
146 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
147 .bMaxPower = 50, /* 100 mA */
148};
149
150static int __init nokia_bind(struct usb_composite_dev *cdev)
151{
152 int gcnum;
153 struct usb_gadget *gadget = cdev->gadget;
154 int status;
155
156 status = gphonet_setup(cdev->gadget);
157 if (status < 0)
158 goto err_phonet;
159
160 status = gserial_setup(cdev->gadget, 3);
161 if (status < 0)
162 goto err_serial;
163
164 status = gether_setup(cdev->gadget, hostaddr);
165 if (status < 0)
166 goto err_ether;
167
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200168 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200169 if (status < 0)
170 goto err_usb;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200171 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
172 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
173 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200174 nokia_config_500ma_driver.iConfiguration = status;
175 nokia_config_100ma_driver.iConfiguration = status;
176
177 /* set up other descriptors */
178 gcnum = usb_gadget_controller_number(gadget);
179 if (gcnum >= 0)
180 device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
181 else {
182 /* this should only work with hw that supports altsettings
183 * and several endpoints, anything else, panic.
184 */
185 pr_err("nokia_bind: controller '%s' not recognized\n",
186 gadget->name);
187 goto err_usb;
188 }
189
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300190 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200191 status = usb_add_config(cdev, &nokia_config_500ma_driver,
192 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200193 if (status < 0)
194 goto err_usb;
195
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200196 status = usb_add_config(cdev, &nokia_config_100ma_driver,
197 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200198 if (status < 0)
199 goto err_usb;
200
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200201 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200202 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
203
204 return 0;
205
206err_usb:
207 gether_cleanup();
208err_ether:
209 gserial_cleanup();
210err_serial:
211 gphonet_cleanup();
212err_phonet:
213 return status;
214}
215
216static int __exit nokia_unbind(struct usb_composite_dev *cdev)
217{
218 gphonet_cleanup();
219 gserial_cleanup();
220 gether_cleanup();
221
222 return 0;
223}
224
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200225static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200226 .name = "g_nokia",
227 .dev = &device_desc,
228 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300229 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200230 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200231 .unbind = __exit_p(nokia_unbind),
232};
233
234static int __init nokia_init(void)
235{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200236 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200237}
238module_init(nokia_init);
239
240static void __exit nokia_cleanup(void)
241{
242 usb_composite_unregister(&nokia_driver);
243}
244module_exit(nokia_cleanup);
245