usb: gadget: libcomposite: move composite.c into libcomposite
[deliverable/linux.git] / drivers / usb / gadget / nokia.c
CommitLineData
f358f5b4
FB
1/*
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>
f358f5b4
FB
19#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 */
f358f5b4
FB
40#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/*-------------------------------------------------------------------------*/
7d16e8d3 49USB_GADGET_COMPOSITE_OPTIONS();
f358f5b4
FB
50
51#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
52#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
53
54/* string IDs are assigned dynamically */
55
276e2e4f 56#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
f358f5b4
FB
57
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[] = {
276e2e4f
SAS
63 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
64 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
65 [USB_GADGET_SERIAL_IDX].s = "",
f358f5b4
FB
66 [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),
87 /* .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",
f358f5b4
FB
132 .bConfigurationValue = 1,
133 /* .iConfiguration = DYNAMIC */
134 .bmAttributes = USB_CONFIG_ATT_ONE,
135 .bMaxPower = 250, /* 500mA */
136};
137
138static struct usb_configuration nokia_config_100ma_driver = {
139 .label = "Self Powered",
f358f5b4
FB
140 .bConfigurationValue = 2,
141 /* .iConfiguration = DYNAMIC */
142 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
143 .bMaxPower = 50, /* 100 mA */
144};
145
146static int __init nokia_bind(struct usb_composite_dev *cdev)
147{
148 int gcnum;
149 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
e1f15ccb 164 status = usb_string_ids_tab(cdev, strings_dev);
f358f5b4
FB
165 if (status < 0)
166 goto err_usb;
276e2e4f
SAS
167 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
168 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
e1f15ccb 169 status = strings_dev[STRING_DESCRIPTION_IDX].id;
f358f5b4
FB
170 nokia_config_500ma_driver.iConfiguration = status;
171 nokia_config_100ma_driver.iConfiguration = status;
172
173 /* set up other descriptors */
174 gcnum = usb_gadget_controller_number(gadget);
175 if (gcnum >= 0)
176 device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
177 else {
178 /* this should only work with hw that supports altsettings
179 * and several endpoints, anything else, panic.
180 */
181 pr_err("nokia_bind: controller '%s' not recognized\n",
182 gadget->name);
183 goto err_usb;
184 }
185
25985edc 186 /* finally register the configuration */
c9bfff9c
UKK
187 status = usb_add_config(cdev, &nokia_config_500ma_driver,
188 nokia_bind_config);
f358f5b4
FB
189 if (status < 0)
190 goto err_usb;
191
c9bfff9c
UKK
192 status = usb_add_config(cdev, &nokia_config_100ma_driver,
193 nokia_bind_config);
f358f5b4
FB
194 if (status < 0)
195 goto err_usb;
196
7d16e8d3 197 usb_composite_overwrite_options(cdev, &coverwrite);
f358f5b4
FB
198 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
199
200 return 0;
201
202err_usb:
203 gether_cleanup();
204err_ether:
205 gserial_cleanup();
206err_serial:
207 gphonet_cleanup();
208err_phonet:
209 return status;
210}
211
212static int __exit nokia_unbind(struct usb_composite_dev *cdev)
213{
214 gphonet_cleanup();
215 gserial_cleanup();
216 gether_cleanup();
217
218 return 0;
219}
220
c2ec75c2 221static __refdata struct usb_composite_driver nokia_driver = {
f358f5b4
FB
222 .name = "g_nokia",
223 .dev = &device_desc,
224 .strings = dev_strings,
35a0e0bf 225 .max_speed = USB_SPEED_HIGH,
03e42bd5 226 .bind = nokia_bind,
f358f5b4
FB
227 .unbind = __exit_p(nokia_unbind),
228};
229
230static int __init nokia_init(void)
231{
03e42bd5 232 return usb_composite_probe(&nokia_driver);
f358f5b4
FB
233}
234module_init(nokia_init);
235
236static void __exit nokia_cleanup(void)
237{
238 usb_composite_unregister(&nokia_driver);
239}
240module_exit(nokia_cleanup);
241
This page took 0.205599 seconds and 5 git commands to generate.