blob: 48f0d5c372a8cffaa384e42aa36af26d3845c604 [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 "f_acm.c"
41#include "f_ecm.c"
42#include "f_obex.c"
43#include "f_serial.c"
44#include "f_phonet.c"
45#include "u_ether.c"
46
47/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020048USB_GADGET_COMPOSITE_OPTIONS();
Felipe Balbif358f5b2010-01-05 16:10:13 +020049
50#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
51#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
52
53/* string IDs are assigned dynamically */
54
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020055#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
Felipe Balbif358f5b2010-01-05 16:10:13 +020056
57static char manufacturer_nokia[] = "Nokia";
58static const char product_nokia[] = NOKIA_LONG_NAME;
59static const char description_nokia[] = "PC-Suite Configuration";
60
61static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020062 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
63 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
64 [USB_GADGET_SERIAL_IDX].s = "",
Felipe Balbif358f5b2010-01-05 16:10:13 +020065 [STRING_DESCRIPTION_IDX].s = description_nokia,
66 { } /* end of list */
67};
68
69static struct usb_gadget_strings stringtab_dev = {
70 .language = 0x0409, /* en-us */
71 .strings = strings_dev,
72};
73
74static struct usb_gadget_strings *dev_strings[] = {
75 &stringtab_dev,
76 NULL,
77};
78
79static struct usb_device_descriptor device_desc = {
80 .bLength = USB_DT_DEVICE_SIZE,
81 .bDescriptorType = USB_DT_DEVICE,
82 .bcdUSB = __constant_cpu_to_le16(0x0200),
83 .bDeviceClass = USB_CLASS_COMM,
84 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
85 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +020086 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM),
Felipe Balbif358f5b2010-01-05 16:10:13 +020087 /* .iManufacturer = DYNAMIC */
88 /* .iProduct = DYNAMIC */
89 .bNumConfigurations = 1,
90};
91
92/*-------------------------------------------------------------------------*/
93
94/* Module */
95MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
96MODULE_AUTHOR("Felipe Balbi");
97MODULE_LICENSE("GPL");
98
99/*-------------------------------------------------------------------------*/
100
101static u8 hostaddr[ETH_ALEN];
102
103static int __init nokia_bind_config(struct usb_configuration *c)
104{
105 int status = 0;
106
107 status = phonet_bind_config(c);
108 if (status)
109 printk(KERN_DEBUG "could not bind phonet config\n");
110
111 status = obex_bind_config(c, 0);
112 if (status)
113 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
114
115 status = obex_bind_config(c, 1);
116 if (status)
117 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
118
119 status = acm_bind_config(c, 2);
120 if (status)
121 printk(KERN_DEBUG "could not bind acm config\n");
122
123 status = ecm_bind_config(c, hostaddr);
124 if (status)
125 printk(KERN_DEBUG "could not bind ecm config\n");
126
127 return status;
128}
129
130static struct usb_configuration nokia_config_500ma_driver = {
131 .label = "Bus Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200132 .bConfigurationValue = 1,
133 /* .iConfiguration = DYNAMIC */
134 .bmAttributes = USB_CONFIG_ATT_ONE,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100135 .MaxPower = 500,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200136};
137
138static struct usb_configuration nokia_config_100ma_driver = {
139 .label = "Self Powered",
Felipe Balbif358f5b2010-01-05 16:10:13 +0200140 .bConfigurationValue = 2,
141 /* .iConfiguration = DYNAMIC */
142 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100143 .MaxPower = 100,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200144};
145
146static int __init nokia_bind(struct usb_composite_dev *cdev)
147{
Felipe Balbif358f5b2010-01-05 16:10:13 +0200148 struct usb_gadget *gadget = cdev->gadget;
149 int status;
150
151 status = gphonet_setup(cdev->gadget);
152 if (status < 0)
153 goto err_phonet;
154
155 status = gserial_setup(cdev->gadget, 3);
156 if (status < 0)
157 goto err_serial;
158
159 status = gether_setup(cdev->gadget, hostaddr);
160 if (status < 0)
161 goto err_ether;
162
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200163 status = usb_string_ids_tab(cdev, strings_dev);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200164 if (status < 0)
165 goto err_usb;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200166 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
167 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200168 status = strings_dev[STRING_DESCRIPTION_IDX].id;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200169 nokia_config_500ma_driver.iConfiguration = status;
170 nokia_config_100ma_driver.iConfiguration = status;
171
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200172 if (!gadget_supports_altsettings(gadget))
Felipe Balbif358f5b2010-01-05 16:10:13 +0200173 goto err_usb;
Felipe Balbif358f5b2010-01-05 16:10:13 +0200174
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300175 /* finally register the configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200176 status = usb_add_config(cdev, &nokia_config_500ma_driver,
177 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200178 if (status < 0)
179 goto err_usb;
180
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200181 status = usb_add_config(cdev, &nokia_config_100ma_driver,
182 nokia_bind_config);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200183 if (status < 0)
184 goto err_usb;
185
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200186 usb_composite_overwrite_options(cdev, &coverwrite);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200187 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
188
189 return 0;
190
191err_usb:
192 gether_cleanup();
193err_ether:
194 gserial_cleanup();
195err_serial:
196 gphonet_cleanup();
197err_phonet:
198 return status;
199}
200
201static int __exit nokia_unbind(struct usb_composite_dev *cdev)
202{
203 gphonet_cleanup();
204 gserial_cleanup();
205 gether_cleanup();
206
207 return 0;
208}
209
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200210static __refdata struct usb_composite_driver nokia_driver = {
Felipe Balbif358f5b2010-01-05 16:10:13 +0200211 .name = "g_nokia",
212 .dev = &device_desc,
213 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300214 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200215 .bind = nokia_bind,
Felipe Balbif358f5b2010-01-05 16:10:13 +0200216 .unbind = __exit_p(nokia_unbind),
217};
218
219static int __init nokia_init(void)
220{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200221 return usb_composite_probe(&nokia_driver);
Felipe Balbif358f5b2010-01-05 16:10:13 +0200222}
223module_init(nokia_init);
224
225static void __exit nokia_cleanup(void)
226{
227 usb_composite_unregister(&nokia_driver);
228}
229module_exit(nokia_cleanup);
230