blob: 661600abace8deb8a17fe0d8318c53c0b291cf02 [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 */
Felipe Balbif358f5b2010-01-05 16:10:13 +020040#include "u_serial.c"
41#include "f_acm.c"
42#include "f_ecm.c"
43#include "f_obex.c"
44#include "f_serial.c"
45#include "f_phonet.c"
46#include "u_ether.c"
47
48/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020049USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020050
51#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/*-------------------------------------------------------------------------*/
101
102static u8 hostaddr[ETH_ALEN];
103
104static int __init nokia_bind_config(struct usb_configuration *c)
105{
106 int status = 0;
107
108 status = phonet_bind_config(c);
109 if (status)
110 printk(KERN_DEBUG "could not bind phonet config\n");
111
112 status = obex_bind_config(c, 0);
113 if (status)
114 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
115
116 status = obex_bind_config(c, 1);
117 if (status)
118 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
119
120 status = acm_bind_config(c, 2);
121 if (status)
122 printk(KERN_DEBUG "could not bind acm config\n");
123
124 status = ecm_bind_config(c, hostaddr);
125 if (status)
126 printk(KERN_DEBUG "could not bind ecm config\n");
127
128 return status;
129}
130
131static struct usb_configuration nokia_config_500ma_driver = {
132 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200133 .bConfigurationValue = 1,
134 /* .iConfiguration = DYNAMIC */
135 .bmAttributes = USB_CONFIG_ATT_ONE,
136 .bMaxPower = 250, /* 500mA */
137};
138
139static struct usb_configuration nokia_config_100ma_driver = {
140 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200141 .bConfigurationValue = 2,
142 /* .iConfiguration = DYNAMIC */
143 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
144 .bMaxPower = 50, /* 100 mA */
145};
146
147static int __init nokia_bind(struct usb_composite_dev *cdev)
148{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200149 struct usb_gadget *gadget = cdev->gadget;
150 int status;
151
152 status = gphonet_setup(cdev->gadget);
153 if (status < 0)
154 goto err_phonet;
155
156 status = gserial_setup(cdev->gadget, 3);
157 if (status < 0)
158 goto err_serial;
159
160 status = gether_setup(cdev->gadget, hostaddr);
161 if (status < 0)
162 goto err_ether;
163
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200164 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200165 if (status < 0)
166 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200167 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
168 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200169 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200170 nokia_config_500ma_driver.iConfiguration = status;
171 nokia_config_100ma_driver.iConfiguration = status;
172
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200173 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200174 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200175
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300176 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200177 status = usb_add_config(cdev, &nokia_config_500ma_driver,
178 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200179 if (status < 0)
180 goto err_usb;
181
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200182 status = usb_add_config(cdev, &nokia_config_100ma_driver,
183 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200184 if (status < 0)
185 goto err_usb;
186
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200187 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200188 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
189
190 return 0;
191
192err_usb:
193 gether_cleanup();
194err_ether:
195 gserial_cleanup();
196err_serial:
197 gphonet_cleanup();
198err_phonet:
199 return status;
200}
201
202static int __exit nokia_unbind(struct usb_composite_dev *cdev)
203{
204 gphonet_cleanup();
205 gserial_cleanup();
206 gether_cleanup();
207
208 return 0;
209}
210
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200211static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200212 .name = "g_nokia",
213 .dev = &device_desc,
214 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300215 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200216 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200217 .unbind = __exit_p(nokia_unbind),
218};
219
220static int __init nokia_init(void)
221{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200222 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200223}
224module_init(nokia_init);
225
226static void __exit nokia_cleanup(void)
227{
228 usb_composite_unregister(&nokia_driver);
229}
230module_exit(nokia_cleanup);
231