blob: 9339800520cf13ae208d38192d4412cbc202e669 [file] [log] [blame]
Benoit Goby1e8ce152011-12-12 13:01:23 -08001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/utsname.h>
25#include <linux/platform_device.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/pm_qos.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080027
28#include <linux/usb/ch9.h>
29#include <linux/usb/composite.h>
30#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <linux/usb/android.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080032
33#include "gadget_chips.h"
34
35/*
36 * Kbuild is not very cooperative with respect to linking separately
37 * compiled library objects into one module. So for now we won't use
38 * separate compilation ... ensuring init/exit sections work to shrink
39 * the runtime footprint, and giving us at least some parts of what
40 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
41 */
42#include "usbstring.c"
43#include "config.c"
44#include "epautoconf.c"
45#include "composite.c"
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053048#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053049#include "f_rmnet_sdio.c"
50#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053051#include "f_rmnet.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080052#include "f_mass_storage.c"
53#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053054#include "u_sdio.c"
55#include "u_smd.c"
56#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053057#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080058#include "u_ctrl_hsic.c"
59#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053060#include "u_ctrl_hsuart.c"
61#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053062#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080063#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080064#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053065#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080066#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080067#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080068#define USB_ETH_RNDIS y
69#include "f_rndis.c"
70#include "rndis.c"
71#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030072#include "u_bam_data.c"
73#include "f_mbim.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080074
75MODULE_AUTHOR("Mike Lockwood");
76MODULE_DESCRIPTION("Android Composite USB Driver");
77MODULE_LICENSE("GPL");
78MODULE_VERSION("1.0");
79
80static const char longname[] = "Gadget Android";
81
82/* Default vendor and product IDs, overridden by userspace */
83#define VENDOR_ID 0x18D1
84#define PRODUCT_ID 0x0001
85
Ido Shayevitz23dc77c2012-07-18 16:16:06 +030086#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
87
Benoit Goby1e8ce152011-12-12 13:01:23 -080088struct android_usb_function {
89 char *name;
90 void *config;
91
92 struct device *dev;
93 char *dev_name;
94 struct device_attribute **attributes;
95
96 /* for android_dev.enabled_functions */
97 struct list_head enabled_list;
98
Ido Shayevitz23dc77c2012-07-18 16:16:06 +030099 struct android_dev *android_dev;
100
Benoit Goby1e8ce152011-12-12 13:01:23 -0800101 /* Optional: initialization during gadget bind */
102 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
103 /* Optional: cleanup during gadget unbind */
104 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700105 /* Optional: called when the function is added the list of
106 * enabled functions */
107 void (*enable)(struct android_usb_function *);
108 /* Optional: called when it is removed */
109 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800110
111 int (*bind_config)(struct android_usb_function *,
112 struct usb_configuration *);
113
114 /* Optional: called when the configuration is removed */
115 void (*unbind_config)(struct android_usb_function *,
116 struct usb_configuration *);
117 /* Optional: handle ctrl requests before the device is configured */
118 int (*ctrlrequest)(struct android_usb_function *,
119 struct usb_composite_dev *,
120 const struct usb_ctrlrequest *);
121};
122
123struct android_dev {
124 struct android_usb_function **functions;
125 struct list_head enabled_functions;
126 struct usb_composite_dev *cdev;
127 struct device *dev;
128
129 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700130 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800131 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700132 struct android_usb_platform_data *pdata;
133
Benoit Goby1e8ce152011-12-12 13:01:23 -0800134 bool connected;
135 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300136 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700137 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800138 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300139
140 struct list_head list_item;
141
142 struct usb_configuration config;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800143};
144
145static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300146static struct list_head android_dev_list;
147static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800148static int android_bind_config(struct usb_configuration *c);
149static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300150static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800151
152/* string IDs are assigned dynamically */
153#define STRING_MANUFACTURER_IDX 0
154#define STRING_PRODUCT_IDX 1
155#define STRING_SERIAL_IDX 2
156
157static char manufacturer_string[256];
158static char product_string[256];
159static char serial_string[256];
160
161/* String Table */
162static struct usb_string strings_dev[] = {
163 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
164 [STRING_PRODUCT_IDX].s = product_string,
165 [STRING_SERIAL_IDX].s = serial_string,
166 { } /* end of list */
167};
168
169static struct usb_gadget_strings stringtab_dev = {
170 .language = 0x0409, /* en-us */
171 .strings = strings_dev,
172};
173
174static struct usb_gadget_strings *dev_strings[] = {
175 &stringtab_dev,
176 NULL,
177};
178
179static struct usb_device_descriptor device_desc = {
180 .bLength = sizeof(device_desc),
181 .bDescriptorType = USB_DT_DEVICE,
182 .bcdUSB = __constant_cpu_to_le16(0x0200),
183 .bDeviceClass = USB_CLASS_PER_INTERFACE,
184 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
185 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
186 .bcdDevice = __constant_cpu_to_le16(0xffff),
187 .bNumConfigurations = 1,
188};
189
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530190static struct usb_otg_descriptor otg_descriptor = {
191 .bLength = sizeof otg_descriptor,
192 .bDescriptorType = USB_DT_OTG,
193 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
194 .bcdOTG = __constant_cpu_to_le16(0x0200),
195};
196
197static const struct usb_descriptor_header *otg_desc[] = {
198 (struct usb_descriptor_header *) &otg_descriptor,
199 NULL,
200};
201
Manu Gautama2b54142012-04-03 14:34:32 +0530202enum android_device_state {
203 USB_DISCONNECTED,
204 USB_CONNECTED,
205 USB_CONFIGURED,
206};
207
Ofir Cohen94213a72012-05-03 14:26:32 +0300208static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
209{
210 struct android_usb_platform_data *pdata = dev->pdata;
211 u32 swfi_latency = 0;
212 static int last_vote = -1;
213
Ofir Cohen56eb7072012-05-20 11:41:39 +0300214 if (!pdata || vote == last_vote
215 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300216 return;
217
218 swfi_latency = pdata->swfi_latency + 1;
219 if (vote)
220 pm_qos_update_request(&dev->pm_qos_req_dma,
221 swfi_latency);
222 else
223 pm_qos_update_request(&dev->pm_qos_req_dma,
224 PM_QOS_DEFAULT_VALUE);
225 last_vote = vote;
226}
227
Benoit Goby1e8ce152011-12-12 13:01:23 -0800228static void android_work(struct work_struct *data)
229{
230 struct android_dev *dev = container_of(data, struct android_dev, work);
231 struct usb_composite_dev *cdev = dev->cdev;
232 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
233 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
234 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
235 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530236 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800237 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300238 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800239
240 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530241 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800242 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530243 next_state = USB_CONFIGURED;
244 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800245 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530246 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300247 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300248 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300249 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300250 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530251 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800252 dev->sw_connected = dev->connected;
253 spin_unlock_irqrestore(&cdev->lock, flags);
254
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300255 if (pm_qos_vote != -1)
256 android_pm_qos_update_latency(dev, pm_qos_vote);
257
Benoit Goby1e8ce152011-12-12 13:01:23 -0800258 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530259 /*
260 * Some userspace modules, e.g. MTP, work correctly only if
261 * CONFIGURED uevent is preceded by DISCONNECT uevent.
262 * Check if we missed sending out a DISCONNECT uevent. This can
263 * happen if host PC resets and configures device really quick.
264 */
265 if (((uevent_envp == connected) &&
266 (last_uevent != USB_DISCONNECTED)) ||
267 ((uevent_envp == configured) &&
268 (last_uevent == USB_CONFIGURED))) {
269 pr_info("%s: sent missed DISCONNECT event\n", __func__);
270 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
271 disconnected);
272 msleep(20);
273 }
274 /*
275 * Before sending out CONFIGURED uevent give function drivers
276 * a chance to wakeup userspace threads and notify disconnect
277 */
278 if (uevent_envp == configured)
279 msleep(50);
280
Benoit Goby1e8ce152011-12-12 13:01:23 -0800281 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530282 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800283 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
284 } else {
285 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
286 dev->connected, dev->sw_connected, cdev->config);
287 }
288}
289
Benoit Goby80ba14d2012-03-19 18:56:52 -0700290static void android_enable(struct android_dev *dev)
291{
292 struct usb_composite_dev *cdev = dev->cdev;
293
294 if (WARN_ON(!dev->disable_depth))
295 return;
296
297 if (--dev->disable_depth == 0) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300298 usb_add_config(cdev, &dev->config,
Benoit Goby80ba14d2012-03-19 18:56:52 -0700299 android_bind_config);
300 usb_gadget_connect(cdev->gadget);
301 }
302}
303
304static void android_disable(struct android_dev *dev)
305{
306 struct usb_composite_dev *cdev = dev->cdev;
307
308 if (dev->disable_depth++ == 0) {
309 usb_gadget_disconnect(cdev->gadget);
310 /* Cancel pending control requests */
311 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300312 usb_remove_config(cdev, &dev->config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700313 }
314}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800315
316/*-------------------------------------------------------------------------*/
317/* Supported functions initialization */
318
Benoit Goby80ba14d2012-03-19 18:56:52 -0700319struct adb_data {
320 bool opened;
321 bool enabled;
322};
323
Benoit Goby2b6862d2011-12-19 14:38:41 -0800324static int
325adb_function_init(struct android_usb_function *f,
326 struct usb_composite_dev *cdev)
327{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700328 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
329 if (!f->config)
330 return -ENOMEM;
331
Benoit Goby2b6862d2011-12-19 14:38:41 -0800332 return adb_setup();
333}
334
335static void adb_function_cleanup(struct android_usb_function *f)
336{
337 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700338 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800339}
340
341static int
342adb_function_bind_config(struct android_usb_function *f,
343 struct usb_configuration *c)
344{
345 return adb_bind_config(c);
346}
347
Benoit Goby80ba14d2012-03-19 18:56:52 -0700348static void adb_android_function_enable(struct android_usb_function *f)
349{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300350 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700351 struct adb_data *data = f->config;
352
353 data->enabled = true;
354
355 /* Disable the gadget until adbd is ready */
356 if (!data->opened)
357 android_disable(dev);
358}
359
360static void adb_android_function_disable(struct android_usb_function *f)
361{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300362 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700363 struct adb_data *data = f->config;
364
365 data->enabled = false;
366
367 /* Balance the disable that was called in closed_callback */
368 if (!data->opened)
369 android_enable(dev);
370}
371
Benoit Goby2b6862d2011-12-19 14:38:41 -0800372static struct android_usb_function adb_function = {
373 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700374 .enable = adb_android_function_enable,
375 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800376 .init = adb_function_init,
377 .cleanup = adb_function_cleanup,
378 .bind_config = adb_function_bind_config,
379};
380
Benoit Goby80ba14d2012-03-19 18:56:52 -0700381static void adb_ready_callback(void)
382{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300383 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700384 struct adb_data *data = adb_function.config;
385
Benoit Goby80ba14d2012-03-19 18:56:52 -0700386 data->opened = true;
387
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300388 if (data->enabled && dev) {
389 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700390 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300391 mutex_unlock(&dev->mutex);
392 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700393}
394
395static void adb_closed_callback(void)
396{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300397 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700398 struct adb_data *data = adb_function.config;
399
Benoit Goby80ba14d2012-03-19 18:56:52 -0700400 data->opened = false;
401
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300402 if (data->enabled) {
403 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700404 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300405 mutex_unlock(&dev->mutex);
406 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700407}
408
Benoit Goby2b6862d2011-12-19 14:38:41 -0800409
Benoit Gobyaab96812011-04-19 20:37:33 -0700410/*-------------------------------------------------------------------------*/
411/* Supported functions initialization */
412
Manu Gautam8e0719b2011-09-26 14:47:55 +0530413/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530414static int rmnet_smd_function_bind_config(struct android_usb_function *f,
415 struct usb_configuration *c)
416{
417 return rmnet_smd_bind_config(c);
418}
419
420static struct android_usb_function rmnet_smd_function = {
421 .name = "rmnet_smd",
422 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800423};
424
Manu Gautam8e0719b2011-09-26 14:47:55 +0530425/* RMNET_SDIO */
426static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
427 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800428{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530429 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800430}
431
Manu Gautam8e0719b2011-09-26 14:47:55 +0530432static struct android_usb_function rmnet_sdio_function = {
433 .name = "rmnet_sdio",
434 .bind_config = rmnet_sdio_function_bind_config,
435};
436
437/* RMNET_SMD_SDIO */
438static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
439 struct usb_composite_dev *cdev)
440{
441 return rmnet_smd_sdio_init();
442}
443
444static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
445{
446 rmnet_smd_sdio_cleanup();
447}
448
449static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
450 struct usb_configuration *c)
451{
452 return rmnet_smd_sdio_function_add(c);
453}
454
455static struct device_attribute *rmnet_smd_sdio_attributes[] = {
456 &dev_attr_transport, NULL };
457
458static struct android_usb_function rmnet_smd_sdio_function = {
459 .name = "rmnet_smd_sdio",
460 .init = rmnet_smd_sdio_function_init,
461 .cleanup = rmnet_smd_sdio_function_cleanup,
462 .bind_config = rmnet_smd_sdio_bind_config,
463 .attributes = rmnet_smd_sdio_attributes,
464};
465
Hemant Kumar1b820d52011-11-03 15:08:28 -0700466/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
467#define MAX_XPORT_STR_LEN 50
468static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530469
Manu Gautame3e897c2011-09-12 17:18:46 +0530470static void rmnet_function_cleanup(struct android_usb_function *f)
471{
472 frmnet_cleanup();
473}
474
Manu Gautam2b0234a2011-09-07 16:47:52 +0530475static int rmnet_function_bind_config(struct android_usb_function *f,
476 struct usb_configuration *c)
477{
478 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700479 int err = 0;
480 char *ctrl_name;
481 char *data_name;
482 char buf[MAX_XPORT_STR_LEN], *b;
483 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530484
Hemant Kumar1b820d52011-11-03 15:08:28 -0700485 if (!rmnet_initialized) {
486 rmnet_initialized = 1;
487 strlcpy(buf, rmnet_transports, sizeof(buf));
488 b = strim(buf);
489 while (b) {
490 ctrl_name = strsep(&b, ",");
491 data_name = strsep(&b, ",");
492 if (ctrl_name && data_name) {
493 err = frmnet_init_port(ctrl_name, data_name);
494 if (err) {
495 pr_err("rmnet: Cannot open ctrl port:"
496 "'%s' data port:'%s'\n",
497 ctrl_name, data_name);
498 goto out;
499 }
500 ports++;
501 }
502 }
503
504 err = rmnet_gport_setup();
505 if (err) {
506 pr_err("rmnet: Cannot setup transports");
507 goto out;
508 }
509 }
510
511 for (i = 0; i < ports; i++) {
512 err = frmnet_bind_config(c, i);
513 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530514 pr_err("Could not bind rmnet%u config\n", i);
515 break;
516 }
517 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700518out:
519 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530520}
521
Hemant Kumar1b820d52011-11-03 15:08:28 -0700522static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530523 struct device_attribute *attr, char *buf)
524{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700525 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530526}
527
Hemant Kumar1b820d52011-11-03 15:08:28 -0700528static ssize_t rmnet_transports_store(
529 struct device *device, struct device_attribute *attr,
530 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530531{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700532 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530533
Manu Gautam2b0234a2011-09-07 16:47:52 +0530534 return size;
535}
536
Hemant Kumar1b820d52011-11-03 15:08:28 -0700537static struct device_attribute dev_attr_rmnet_transports =
538 __ATTR(transports, S_IRUGO | S_IWUSR,
539 rmnet_transports_show,
540 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530541static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700542 &dev_attr_rmnet_transports,
543 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530544
545static struct android_usb_function rmnet_function = {
546 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530547 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530548 .bind_config = rmnet_function_bind_config,
549 .attributes = rmnet_function_attributes,
550};
551
Anna Perela8c991d2012-04-09 16:44:46 +0300552
553/* MBIM - used with BAM */
554#define MAX_MBIM_INSTANCES 1
555
556static int mbim_function_init(struct android_usb_function *f,
557 struct usb_composite_dev *cdev)
558{
559 return mbim_init(MAX_MBIM_INSTANCES);
560}
561
562static void mbim_function_cleanup(struct android_usb_function *f)
563{
564 fmbim_cleanup();
565}
566
567static int mbim_function_bind_config(struct android_usb_function *f,
568 struct usb_configuration *c)
569{
570 return mbim_bind_config(c, 0);
571}
572
573static struct android_usb_function mbim_function = {
574 .name = "usb_mbim",
575 .cleanup = mbim_function_cleanup,
576 .bind_config = mbim_function_bind_config,
577 .init = mbim_function_init,
578};
579
580
Manu Gautam8e0719b2011-09-26 14:47:55 +0530581/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530582static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583static ssize_t clients_store(
584 struct device *device, struct device_attribute *attr,
585 const char *buff, size_t size)
586{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530587 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588
589 return size;
590}
591
592static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
593static struct device_attribute *diag_function_attributes[] =
594 { &dev_attr_clients, NULL };
595
596static int diag_function_init(struct android_usb_function *f,
597 struct usb_composite_dev *cdev)
598{
599 return diag_setup();
600}
601
602static void diag_function_cleanup(struct android_usb_function *f)
603{
604 diag_cleanup();
605}
606
607static int diag_function_bind_config(struct android_usb_function *f,
608 struct usb_configuration *c)
609{
610 char *name;
611 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530612 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800613 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300614 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530616 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 b = strim(buf);
618
619 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800620 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530622 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300623 if (dev->pdata && !once++)
624 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530625
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530627 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 if (err)
629 pr_err("diag: Cannot open channel '%s'", name);
630 }
631 }
632
633 return err;
634}
635
636static struct android_usb_function diag_function = {
637 .name = "diag",
638 .init = diag_function_init,
639 .cleanup = diag_function_cleanup,
640 .bind_config = diag_function_bind_config,
641 .attributes = diag_function_attributes,
642};
643
Manu Gautam8e0719b2011-09-26 14:47:55 +0530644/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530645static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530646static ssize_t serial_transports_store(
647 struct device *device, struct device_attribute *attr,
648 const char *buff, size_t size)
649{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530650 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530651
652 return size;
653}
654
655static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
656static struct device_attribute *serial_function_attributes[] =
657 { &dev_attr_transports, NULL };
658
659static void serial_function_cleanup(struct android_usb_function *f)
660{
661 gserial_cleanup();
662}
663
664static int serial_function_bind_config(struct android_usb_function *f,
665 struct usb_configuration *c)
666{
667 char *name;
668 char buf[32], *b;
669 int err = -1, i;
670 static int serial_initialized = 0, ports = 0;
671
672 if (serial_initialized)
673 goto bind_config;
674
675 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530676 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530677 b = strim(buf);
678
679 while (b) {
680 name = strsep(&b, ",");
681
682 if (name) {
683 err = gserial_init_port(ports, name);
684 if (err) {
685 pr_err("serial: Cannot open port '%s'", name);
686 goto out;
687 }
688 ports++;
689 }
690 }
691 err = gport_setup(c);
692 if (err) {
693 pr_err("serial: Cannot setup transports");
694 goto out;
695 }
696
697bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200698 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530699 err = gser_bind_config(c, i);
700 if (err) {
701 pr_err("serial: bind_config failed for port %d", i);
702 goto out;
703 }
704 }
705
706out:
707 return err;
708}
709
710static struct android_usb_function serial_function = {
711 .name = "serial",
712 .cleanup = serial_function_cleanup,
713 .bind_config = serial_function_bind_config,
714 .attributes = serial_function_attributes,
715};
716
Anji jonnala92be1b42011-12-19 09:44:41 +0530717/* ACM */
718static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
719static ssize_t acm_transports_store(
720 struct device *device, struct device_attribute *attr,
721 const char *buff, size_t size)
722{
723 strlcpy(acm_transports, buff, sizeof(acm_transports));
724
725 return size;
726}
727
728static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
729static struct device_attribute *acm_function_attributes[] = {
730 &dev_attr_acm_transports, NULL };
731
Benoit Goby1e8ce152011-12-12 13:01:23 -0800732static void acm_function_cleanup(struct android_usb_function *f)
733{
734 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800735}
736
Anji jonnala92be1b42011-12-19 09:44:41 +0530737static int acm_function_bind_config(struct android_usb_function *f,
738 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800739{
Anji jonnala92be1b42011-12-19 09:44:41 +0530740 char *name;
741 char buf[32], *b;
742 int err = -1, i;
743 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800744
Anji jonnala92be1b42011-12-19 09:44:41 +0530745 if (acm_initialized)
746 goto bind_config;
747
748 acm_initialized = 1;
749 strlcpy(buf, acm_transports, sizeof(buf));
750 b = strim(buf);
751
752 while (b) {
753 name = strsep(&b, ",");
754
755 if (name) {
756 err = acm_init_port(ports, name);
757 if (err) {
758 pr_err("acm: Cannot open port '%s'", name);
759 goto out;
760 }
761 ports++;
762 }
763 }
764 err = acm_port_setup(c);
765 if (err) {
766 pr_err("acm: Cannot setup transports");
767 goto out;
768 }
769
770bind_config:
771 for (i = 0; i < ports; i++) {
772 err = acm_bind_config(c, i);
773 if (err) {
774 pr_err("acm: bind_config failed for port %d", i);
775 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800776 }
777 }
778
Anji jonnala92be1b42011-12-19 09:44:41 +0530779out:
780 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800781}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800782static struct android_usb_function acm_function = {
783 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800784 .cleanup = acm_function_cleanup,
785 .bind_config = acm_function_bind_config,
786 .attributes = acm_function_attributes,
787};
788
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530789/* CCID */
790static int ccid_function_init(struct android_usb_function *f,
791 struct usb_composite_dev *cdev)
792{
793 return ccid_setup();
794}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800795
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530796static void ccid_function_cleanup(struct android_usb_function *f)
797{
798 ccid_cleanup();
799}
800
801static int ccid_function_bind_config(struct android_usb_function *f,
802 struct usb_configuration *c)
803{
804 return ccid_bind_config(c);
805}
806
807static struct android_usb_function ccid_function = {
808 .name = "ccid",
809 .init = ccid_function_init,
810 .cleanup = ccid_function_cleanup,
811 .bind_config = ccid_function_bind_config,
812};
813
Steve Mucklef132c6c2012-06-06 18:30:57 -0700814static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800815 struct usb_composite_dev *cdev)
816{
817 return mtp_setup();
818}
819
820static void mtp_function_cleanup(struct android_usb_function *f)
821{
822 mtp_cleanup();
823}
824
Steve Mucklef132c6c2012-06-06 18:30:57 -0700825static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800826 struct usb_configuration *c)
827{
828 return mtp_bind_config(c, false);
829}
830
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400831static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800832{
833 /* nothing to do - initialization is handled by mtp_function_init */
834 return 0;
835}
836
837static void ptp_function_cleanup(struct android_usb_function *f)
838{
839 /* nothing to do - cleanup is handled by mtp_function_cleanup */
840}
841
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400842static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800843{
844 return mtp_bind_config(c, true);
845}
846
847static int mtp_function_ctrlrequest(struct android_usb_function *f,
848 struct usb_composite_dev *cdev,
849 const struct usb_ctrlrequest *c)
850{
851 return mtp_ctrlrequest(cdev, c);
852}
853
854static struct android_usb_function mtp_function = {
855 .name = "mtp",
856 .init = mtp_function_init,
857 .cleanup = mtp_function_cleanup,
858 .bind_config = mtp_function_bind_config,
859 .ctrlrequest = mtp_function_ctrlrequest,
860};
861
862/* PTP function is same as MTP with slightly different interface descriptor */
863static struct android_usb_function ptp_function = {
864 .name = "ptp",
865 .init = ptp_function_init,
866 .cleanup = ptp_function_cleanup,
867 .bind_config = ptp_function_bind_config,
868};
869
870
Benoit Goby1e8ce152011-12-12 13:01:23 -0800871struct rndis_function_config {
872 u8 ethaddr[ETH_ALEN];
873 u32 vendorID;
874 char manufacturer[256];
875 /* "Wireless" RNDIS; auto-detected by Windows */
876 bool wceis;
877};
878
879static int
880rndis_function_init(struct android_usb_function *f,
881 struct usb_composite_dev *cdev)
882{
883 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
884 if (!f->config)
885 return -ENOMEM;
886 return 0;
887}
888
889static void rndis_function_cleanup(struct android_usb_function *f)
890{
891 kfree(f->config);
892 f->config = NULL;
893}
894
895static int
896rndis_function_bind_config(struct android_usb_function *f,
897 struct usb_configuration *c)
898{
899 int ret;
900 struct rndis_function_config *rndis = f->config;
901
902 if (!rndis) {
903 pr_err("%s: rndis_pdata\n", __func__);
904 return -1;
905 }
906
907 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
908 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
909 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
910
911 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
912 if (ret) {
913 pr_err("%s: gether_setup failed\n", __func__);
914 return ret;
915 }
916
917 if (rndis->wceis) {
918 /* "Wireless" RNDIS; auto-detected by Windows */
919 rndis_iad_descriptor.bFunctionClass =
920 USB_CLASS_WIRELESS_CONTROLLER;
921 rndis_iad_descriptor.bFunctionSubClass = 0x01;
922 rndis_iad_descriptor.bFunctionProtocol = 0x03;
923 rndis_control_intf.bInterfaceClass =
924 USB_CLASS_WIRELESS_CONTROLLER;
925 rndis_control_intf.bInterfaceSubClass = 0x01;
926 rndis_control_intf.bInterfaceProtocol = 0x03;
927 }
928
929 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
930 rndis->manufacturer);
931}
932
933static void rndis_function_unbind_config(struct android_usb_function *f,
934 struct usb_configuration *c)
935{
936 gether_cleanup();
937}
938
939static ssize_t rndis_manufacturer_show(struct device *dev,
940 struct device_attribute *attr, char *buf)
941{
942 struct android_usb_function *f = dev_get_drvdata(dev);
943 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700944
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530945 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800946}
947
948static ssize_t rndis_manufacturer_store(struct device *dev,
949 struct device_attribute *attr, const char *buf, size_t size)
950{
951 struct android_usb_function *f = dev_get_drvdata(dev);
952 struct rndis_function_config *config = f->config;
953
954 if (size >= sizeof(config->manufacturer))
955 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700956
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530957 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800958 return size;
959 return -1;
960}
961
962static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
963 rndis_manufacturer_store);
964
965static ssize_t rndis_wceis_show(struct device *dev,
966 struct device_attribute *attr, char *buf)
967{
968 struct android_usb_function *f = dev_get_drvdata(dev);
969 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700970
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530971 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800972}
973
974static ssize_t rndis_wceis_store(struct device *dev,
975 struct device_attribute *attr, const char *buf, size_t size)
976{
977 struct android_usb_function *f = dev_get_drvdata(dev);
978 struct rndis_function_config *config = f->config;
979 int value;
980
981 if (sscanf(buf, "%d", &value) == 1) {
982 config->wceis = value;
983 return size;
984 }
985 return -EINVAL;
986}
987
988static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
989 rndis_wceis_store);
990
991static ssize_t rndis_ethaddr_show(struct device *dev,
992 struct device_attribute *attr, char *buf)
993{
994 struct android_usb_function *f = dev_get_drvdata(dev);
995 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700996
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530997 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800998 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
999 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1000}
1001
1002static ssize_t rndis_ethaddr_store(struct device *dev,
1003 struct device_attribute *attr, const char *buf, size_t size)
1004{
1005 struct android_usb_function *f = dev_get_drvdata(dev);
1006 struct rndis_function_config *rndis = f->config;
1007
1008 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1009 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1010 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1011 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1012 return size;
1013 return -EINVAL;
1014}
1015
1016static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1017 rndis_ethaddr_store);
1018
1019static ssize_t rndis_vendorID_show(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1021{
1022 struct android_usb_function *f = dev_get_drvdata(dev);
1023 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001024
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301025 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001026}
1027
1028static ssize_t rndis_vendorID_store(struct device *dev,
1029 struct device_attribute *attr, const char *buf, size_t size)
1030{
1031 struct android_usb_function *f = dev_get_drvdata(dev);
1032 struct rndis_function_config *config = f->config;
1033 int value;
1034
1035 if (sscanf(buf, "%04x", &value) == 1) {
1036 config->vendorID = value;
1037 return size;
1038 }
1039 return -EINVAL;
1040}
1041
1042static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1043 rndis_vendorID_store);
1044
1045static struct device_attribute *rndis_function_attributes[] = {
1046 &dev_attr_manufacturer,
1047 &dev_attr_wceis,
1048 &dev_attr_ethaddr,
1049 &dev_attr_vendorID,
1050 NULL
1051};
1052
1053static struct android_usb_function rndis_function = {
1054 .name = "rndis",
1055 .init = rndis_function_init,
1056 .cleanup = rndis_function_cleanup,
1057 .bind_config = rndis_function_bind_config,
1058 .unbind_config = rndis_function_unbind_config,
1059 .attributes = rndis_function_attributes,
1060};
1061
1062
1063struct mass_storage_function_config {
1064 struct fsg_config fsg;
1065 struct fsg_common *common;
1066};
1067
1068static int mass_storage_function_init(struct android_usb_function *f,
1069 struct usb_composite_dev *cdev)
1070{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001071 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001072 struct mass_storage_function_config *config;
1073 struct fsg_common *common;
1074 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301075 int i;
1076 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001077
1078 config = kzalloc(sizeof(struct mass_storage_function_config),
1079 GFP_KERNEL);
1080 if (!config)
1081 return -ENOMEM;
1082
1083 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301084 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301085 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301086 config->fsg.nluns = 2;
1087 config->fsg.luns[1].cdrom = 1;
1088 config->fsg.luns[1].ro = 1;
1089 config->fsg.luns[1].removable = 1;
1090 name[1] = "lun0";
1091 }
1092
Benoit Goby1e8ce152011-12-12 13:01:23 -08001093 config->fsg.luns[0].removable = 1;
1094
1095 common = fsg_common_init(NULL, cdev, &config->fsg);
1096 if (IS_ERR(common)) {
1097 kfree(config);
1098 return PTR_ERR(common);
1099 }
1100
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301101 for (i = 0; i < config->fsg.nluns; i++) {
1102 err = sysfs_create_link(&f->dev->kobj,
1103 &common->luns[i].dev.kobj,
1104 name[i]);
1105 if (err)
1106 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001107 }
1108
1109 config->common = common;
1110 f->config = config;
1111 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301112error:
1113 for (; i > 0 ; i--)
1114 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1115
1116 fsg_common_release(&common->ref);
1117 kfree(config);
1118 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001119}
1120
1121static void mass_storage_function_cleanup(struct android_usb_function *f)
1122{
1123 kfree(f->config);
1124 f->config = NULL;
1125}
1126
1127static int mass_storage_function_bind_config(struct android_usb_function *f,
1128 struct usb_configuration *c)
1129{
1130 struct mass_storage_function_config *config = f->config;
1131 return fsg_bind_config(c->cdev, c, config->common);
1132}
1133
1134static ssize_t mass_storage_inquiry_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1136{
1137 struct android_usb_function *f = dev_get_drvdata(dev);
1138 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301139 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001140}
1141
1142static ssize_t mass_storage_inquiry_store(struct device *dev,
1143 struct device_attribute *attr, const char *buf, size_t size)
1144{
1145 struct android_usb_function *f = dev_get_drvdata(dev);
1146 struct mass_storage_function_config *config = f->config;
1147 if (size >= sizeof(config->common->inquiry_string))
1148 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301149 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001150 return -EINVAL;
1151 return size;
1152}
1153
1154static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1155 mass_storage_inquiry_show,
1156 mass_storage_inquiry_store);
1157
1158static struct device_attribute *mass_storage_function_attributes[] = {
1159 &dev_attr_inquiry_string,
1160 NULL
1161};
1162
1163static struct android_usb_function mass_storage_function = {
1164 .name = "mass_storage",
1165 .init = mass_storage_function_init,
1166 .cleanup = mass_storage_function_cleanup,
1167 .bind_config = mass_storage_function_bind_config,
1168 .attributes = mass_storage_function_attributes,
1169};
1170
1171
Benoit Gobycf3fc062011-12-19 14:39:37 -08001172static int accessory_function_init(struct android_usb_function *f,
1173 struct usb_composite_dev *cdev)
1174{
1175 return acc_setup();
1176}
1177
1178static void accessory_function_cleanup(struct android_usb_function *f)
1179{
1180 acc_cleanup();
1181}
1182
1183static int accessory_function_bind_config(struct android_usb_function *f,
1184 struct usb_configuration *c)
1185{
1186 return acc_bind_config(c);
1187}
1188
1189static int accessory_function_ctrlrequest(struct android_usb_function *f,
1190 struct usb_composite_dev *cdev,
1191 const struct usb_ctrlrequest *c)
1192{
1193 return acc_ctrlrequest(cdev, c);
1194}
1195
1196static struct android_usb_function accessory_function = {
1197 .name = "accessory",
1198 .init = accessory_function_init,
1199 .cleanup = accessory_function_cleanup,
1200 .bind_config = accessory_function_bind_config,
1201 .ctrlrequest = accessory_function_ctrlrequest,
1202};
1203
1204
Benoit Goby1e8ce152011-12-12 13:01:23 -08001205static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001206 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301207 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301208 &rmnet_sdio_function,
1209 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301210 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301212 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001213 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301214 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001215 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001216 &mtp_function,
1217 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001218 &rndis_function,
1219 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001220 &accessory_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001221 NULL
1222};
1223
Lena Salmand092f2d2012-03-12 17:27:24 +02001224static void android_cleanup_functions(struct android_usb_function **functions)
1225{
1226 struct android_usb_function *f;
1227 struct device_attribute **attrs;
1228 struct device_attribute *attr;
1229
1230 while (*functions) {
1231 f = *functions++;
1232
1233 if (f->dev) {
1234 device_destroy(android_class, f->dev->devt);
1235 kfree(f->dev_name);
1236 } else
1237 continue;
1238
1239 if (f->cleanup)
1240 f->cleanup(f);
1241
1242 attrs = f->attributes;
1243 if (attrs) {
1244 while ((attr = *attrs++))
1245 device_remove_file(f->dev, attr);
1246 }
1247 }
1248}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001249
1250static int android_init_functions(struct android_usb_function **functions,
1251 struct usb_composite_dev *cdev)
1252{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001253 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001254 struct android_usb_function *f;
1255 struct device_attribute **attrs;
1256 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301257 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001258 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001259
1260 for (; (f = *functions++); index++) {
1261 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001262 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001263 if (!f->dev_name) {
1264 err = -ENOMEM;
1265 goto err_out;
1266 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001267 f->dev = device_create(android_class, dev->dev,
1268 MKDEV(0, index), f, f->dev_name);
1269 if (IS_ERR(f->dev)) {
1270 pr_err("%s: Failed to create dev %s", __func__,
1271 f->dev_name);
1272 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001273 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001274 goto err_create;
1275 }
1276
1277 if (f->init) {
1278 err = f->init(f, cdev);
1279 if (err) {
1280 pr_err("%s: Failed to init %s", __func__,
1281 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001282 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001283 }
1284 }
1285
1286 attrs = f->attributes;
1287 if (attrs) {
1288 while ((attr = *attrs++) && !err)
1289 err = device_create_file(f->dev, attr);
1290 }
1291 if (err) {
1292 pr_err("%s: Failed to create function %s attributes",
1293 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001294 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001295 }
1296 }
1297 return 0;
1298
Lena Salmand092f2d2012-03-12 17:27:24 +02001299err_attrs:
1300 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1301 device_remove_file(f->dev, attr);
1302 if (f->cleanup)
1303 f->cleanup(f);
1304err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001305 device_destroy(android_class, f->dev->devt);
1306err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001307 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001308 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001309err_out:
1310 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001311 return err;
1312}
1313
Benoit Goby1e8ce152011-12-12 13:01:23 -08001314static int
1315android_bind_enabled_functions(struct android_dev *dev,
1316 struct usb_configuration *c)
1317{
1318 struct android_usb_function *f;
1319 int ret;
1320
1321 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1322 ret = f->bind_config(f, c);
1323 if (ret) {
1324 pr_err("%s: %s failed", __func__, f->name);
1325 return ret;
1326 }
1327 }
1328 return 0;
1329}
1330
1331static void
1332android_unbind_enabled_functions(struct android_dev *dev,
1333 struct usb_configuration *c)
1334{
1335 struct android_usb_function *f;
1336
1337 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1338 if (f->unbind_config)
1339 f->unbind_config(f, c);
1340 }
1341}
1342
1343static int android_enable_function(struct android_dev *dev, char *name)
1344{
1345 struct android_usb_function **functions = dev->functions;
1346 struct android_usb_function *f;
1347 while ((f = *functions++)) {
1348 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001349 if (f->android_dev)
1350 pr_err("%s cannot be enabled on two devices\n",
1351 f->name);
1352 else {
1353 list_add_tail(&f->enabled_list,
1354 &dev->enabled_functions);
1355 f->android_dev = dev;
1356 return 0;
1357 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001358 }
1359 }
1360 return -EINVAL;
1361}
1362
1363/*-------------------------------------------------------------------------*/
1364/* /sys/class/android_usb/android%d/ interface */
1365
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301366static ssize_t remote_wakeup_show(struct device *pdev,
1367 struct device_attribute *attr, char *buf)
1368{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001369 struct android_dev *dev = dev_get_drvdata(pdev);
1370
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301371 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001372 !!(dev->config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301373 USB_CONFIG_ATT_WAKEUP));
1374}
1375
1376static ssize_t remote_wakeup_store(struct device *pdev,
1377 struct device_attribute *attr, const char *buff, size_t size)
1378{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001379 struct android_dev *dev = dev_get_drvdata(pdev);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301380 int enable = 0;
1381
1382 sscanf(buff, "%d", &enable);
1383
1384 pr_debug("android_usb: %s remote wakeup\n",
1385 enable ? "enabling" : "disabling");
1386
1387 if (enable)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001388 dev->config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301389 else
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001390 dev->config.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301391
1392 return size;
1393}
1394
Benoit Goby1e8ce152011-12-12 13:01:23 -08001395static ssize_t
1396functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1397{
1398 struct android_dev *dev = dev_get_drvdata(pdev);
1399 struct android_usb_function *f;
1400 char *buff = buf;
1401
1402 mutex_lock(&dev->mutex);
1403
1404 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301405 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001406
1407 mutex_unlock(&dev->mutex);
1408
1409 if (buff != buf)
1410 *(buff-1) = '\n';
1411 return buff - buf;
1412}
1413
1414static ssize_t
1415functions_store(struct device *pdev, struct device_attribute *attr,
1416 const char *buff, size_t size)
1417{
1418 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001419 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001420 char *name;
1421 char buf[256], *b;
1422 int err;
1423
1424 mutex_lock(&dev->mutex);
1425
1426 if (dev->enabled) {
1427 mutex_unlock(&dev->mutex);
1428 return -EBUSY;
1429 }
1430
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001431 /* Clear previous enabled list */
1432 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1433 f->android_dev = NULL;
1434 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001435 INIT_LIST_HEAD(&dev->enabled_functions);
1436
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301437 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001438 b = strim(buf);
1439
1440 while (b) {
1441 name = strsep(&b, ",");
1442 if (name) {
1443 err = android_enable_function(dev, name);
1444 if (err)
1445 pr_err("android_usb: Cannot enable '%s'", name);
1446 }
1447 }
1448
1449 mutex_unlock(&dev->mutex);
1450
1451 return size;
1452}
1453
1454static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1455 char *buf)
1456{
1457 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001458
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301459 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001460}
1461
1462static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1463 const char *buff, size_t size)
1464{
1465 struct android_dev *dev = dev_get_drvdata(pdev);
1466 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001467 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001468 int enabled = 0;
1469
Benoit Gobycf3fc062011-12-19 14:39:37 -08001470 if (!cdev)
1471 return -ENODEV;
1472
Benoit Goby1e8ce152011-12-12 13:01:23 -08001473 mutex_lock(&dev->mutex);
1474
1475 sscanf(buff, "%d", &enabled);
1476 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001477 /*
1478 * Update values in composite driver's copy of
1479 * device descriptor.
1480 */
1481 cdev->desc.idVendor = device_desc.idVendor;
1482 cdev->desc.idProduct = device_desc.idProduct;
1483 cdev->desc.bcdDevice = device_desc.bcdDevice;
1484 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1485 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1486 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001487 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1488 if (f->enable)
1489 f->enable(f);
1490 }
1491 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001492 dev->enabled = true;
1493 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001494 android_disable(dev);
1495 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1496 if (f->disable)
1497 f->disable(f);
1498 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001499 dev->enabled = false;
1500 } else {
1501 pr_err("android_usb: already %s\n",
1502 dev->enabled ? "enabled" : "disabled");
1503 }
1504
1505 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001506
Benoit Gobyaab96812011-04-19 20:37:33 -07001507 return size;
1508}
1509
Ofir Cohen94213a72012-05-03 14:26:32 +03001510static ssize_t pm_qos_show(struct device *pdev,
1511 struct device_attribute *attr, char *buf)
1512{
1513 struct android_dev *dev = dev_get_drvdata(pdev);
1514
1515 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1516}
1517
1518static ssize_t pm_qos_store(struct device *pdev,
1519 struct device_attribute *attr,
1520 const char *buff, size_t size)
1521{
1522 struct android_dev *dev = dev_get_drvdata(pdev);
1523
1524 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1525
Benoit Goby1e8ce152011-12-12 13:01:23 -08001526 return size;
1527}
1528
1529static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1530 char *buf)
1531{
1532 struct android_dev *dev = dev_get_drvdata(pdev);
1533 struct usb_composite_dev *cdev = dev->cdev;
1534 char *state = "DISCONNECTED";
1535 unsigned long flags;
1536
1537 if (!cdev)
1538 goto out;
1539
1540 spin_lock_irqsave(&cdev->lock, flags);
1541 if (cdev->config)
1542 state = "CONFIGURED";
1543 else if (dev->connected)
1544 state = "CONNECTED";
1545 spin_unlock_irqrestore(&cdev->lock, flags);
1546out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301547 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001548}
1549
1550#define DESCRIPTOR_ATTR(field, format_string) \
1551static ssize_t \
1552field ## _show(struct device *dev, struct device_attribute *attr, \
1553 char *buf) \
1554{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301555 return snprintf(buf, PAGE_SIZE, \
1556 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001557} \
1558static ssize_t \
1559field ## _store(struct device *dev, struct device_attribute *attr, \
1560 const char *buf, size_t size) \
1561{ \
1562 int value; \
1563 if (sscanf(buf, format_string, &value) == 1) { \
1564 device_desc.field = value; \
1565 return size; \
1566 } \
1567 return -1; \
1568} \
1569static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1570
1571#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1572static ssize_t \
1573field ## _show(struct device *dev, struct device_attribute *attr, \
1574 char *buf) \
1575{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301576 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001577} \
1578static ssize_t \
1579field ## _store(struct device *dev, struct device_attribute *attr, \
1580 const char *buf, size_t size) \
1581{ \
1582 if (size >= sizeof(buffer)) \
1583 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05301584 strlcpy(buffer, buf, sizeof(buffer)); \
1585 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05301586 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001587} \
1588static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1589
1590
1591DESCRIPTOR_ATTR(idVendor, "%04x\n")
1592DESCRIPTOR_ATTR(idProduct, "%04x\n")
1593DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1594DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1595DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1596DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1597DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1598DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1599DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1600
1601static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1602 functions_store);
1603static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001604static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1605 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001606static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301607static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1608 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001609
1610static struct device_attribute *android_usb_attributes[] = {
1611 &dev_attr_idVendor,
1612 &dev_attr_idProduct,
1613 &dev_attr_bcdDevice,
1614 &dev_attr_bDeviceClass,
1615 &dev_attr_bDeviceSubClass,
1616 &dev_attr_bDeviceProtocol,
1617 &dev_attr_iManufacturer,
1618 &dev_attr_iProduct,
1619 &dev_attr_iSerial,
1620 &dev_attr_functions,
1621 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001622 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001623 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301624 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001625 NULL
1626};
1627
1628/*-------------------------------------------------------------------------*/
1629/* Composite driver */
1630
1631static int android_bind_config(struct usb_configuration *c)
1632{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001633 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001634 int ret = 0;
1635
1636 ret = android_bind_enabled_functions(dev, c);
1637 if (ret)
1638 return ret;
1639
1640 return 0;
1641}
1642
1643static void android_unbind_config(struct usb_configuration *c)
1644{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001645 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001646
1647 android_unbind_enabled_functions(dev, c);
1648}
1649
1650static int android_bind(struct usb_composite_dev *cdev)
1651{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001652 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001653 struct usb_gadget *gadget = cdev->gadget;
1654 int gcnum, id, ret;
1655
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001656 /* Bind to the last android_dev that was probed */
1657 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
1658
1659 dev->cdev = cdev;
1660
Benoit Goby1e8ce152011-12-12 13:01:23 -08001661 /*
1662 * Start disconnected. Userspace will connect the gadget once
1663 * it is done configuring the functions.
1664 */
1665 usb_gadget_disconnect(gadget);
1666
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001667 /* Init the supported functions only once, on the first android_dev */
1668 if (android_dev_count == 1) {
1669 ret = android_init_functions(dev->functions, cdev);
1670 if (ret)
1671 return ret;
1672 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001673
1674 /* Allocate string descriptor numbers ... note that string
1675 * contents can be overridden by the composite_dev glue.
1676 */
1677 id = usb_string_id(cdev);
1678 if (id < 0)
1679 return id;
1680 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1681 device_desc.iManufacturer = id;
1682
1683 id = usb_string_id(cdev);
1684 if (id < 0)
1685 return id;
1686 strings_dev[STRING_PRODUCT_IDX].id = id;
1687 device_desc.iProduct = id;
1688
1689 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301690 strlcpy(manufacturer_string, "Android",
1691 sizeof(manufacturer_string) - 1);
1692 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1693 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001694
1695 id = usb_string_id(cdev);
1696 if (id < 0)
1697 return id;
1698 strings_dev[STRING_SERIAL_IDX].id = id;
1699 device_desc.iSerialNumber = id;
1700
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301701 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001702 dev->config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301703
Benoit Goby1e8ce152011-12-12 13:01:23 -08001704 gcnum = usb_gadget_controller_number(gadget);
1705 if (gcnum >= 0)
1706 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1707 else {
1708 pr_warning("%s: controller '%s' not recognized\n",
1709 longname, gadget->name);
1710 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1711 }
1712
Benoit Goby1e8ce152011-12-12 13:01:23 -08001713 return 0;
1714}
1715
1716static int android_usb_unbind(struct usb_composite_dev *cdev)
1717{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001718 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001719
Lena Salmand092f2d2012-03-12 17:27:24 +02001720 manufacturer_string[0] = '\0';
1721 product_string[0] = '\0';
1722 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08001723 cancel_work_sync(&dev->work);
1724 android_cleanup_functions(dev->functions);
1725 return 0;
1726}
1727
1728static struct usb_composite_driver android_usb_driver = {
1729 .name = "android_usb",
1730 .dev = &device_desc,
1731 .strings = dev_strings,
1732 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001733 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08001734};
1735
1736static int
1737android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1738{
Benoit Goby1e8ce152011-12-12 13:01:23 -08001739 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001740 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001741 struct usb_request *req = cdev->req;
1742 struct android_usb_function *f;
1743 int value = -EOPNOTSUPP;
1744 unsigned long flags;
1745
1746 req->zero = 0;
1747 req->complete = composite_setup_complete;
1748 req->length = 0;
1749 gadget->ep0->driver_data = cdev;
1750
1751 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1752 if (f->ctrlrequest) {
1753 value = f->ctrlrequest(f, cdev, c);
1754 if (value >= 0)
1755 break;
1756 }
1757 }
1758
Benoit Gobycf3fc062011-12-19 14:39:37 -08001759 /* Special case the accessory function.
1760 * It needs to handle control requests before it is enabled.
1761 */
1762 if (value < 0)
1763 value = acc_ctrlrequest(cdev, c);
1764
Benoit Goby1e8ce152011-12-12 13:01:23 -08001765 if (value < 0)
1766 value = composite_setup(gadget, c);
1767
1768 spin_lock_irqsave(&cdev->lock, flags);
1769 if (!dev->connected) {
1770 dev->connected = 1;
1771 schedule_work(&dev->work);
1772 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1773 cdev->config) {
1774 schedule_work(&dev->work);
1775 }
1776 spin_unlock_irqrestore(&cdev->lock, flags);
1777
1778 return value;
1779}
1780
1781static void android_disconnect(struct usb_gadget *gadget)
1782{
Benoit Goby1e8ce152011-12-12 13:01:23 -08001783 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001784 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001785 unsigned long flags;
1786
1787 composite_disconnect(gadget);
1788
1789 spin_lock_irqsave(&cdev->lock, flags);
1790 dev->connected = 0;
1791 schedule_work(&dev->work);
1792 spin_unlock_irqrestore(&cdev->lock, flags);
1793}
1794
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001795static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001796{
1797 struct device_attribute **attrs = android_usb_attributes;
1798 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001799 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001800 int err;
1801
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001802 /*
1803 * The primary usb core should always have usb_core_id=0, since
1804 * Android user space is currently interested in android0 events.
1805 */
1806 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
1807 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001808 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001809 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001810 if (IS_ERR(dev->dev))
1811 return PTR_ERR(dev->dev);
1812
1813 dev_set_drvdata(dev->dev, dev);
1814
1815 while ((attr = *attrs++)) {
1816 err = device_create_file(dev->dev, attr);
1817 if (err) {
1818 device_destroy(android_class, dev->dev->devt);
1819 return err;
1820 }
1821 }
1822 return 0;
1823}
1824
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301825static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001826{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301827 struct device_attribute **attrs = android_usb_attributes;
1828 struct device_attribute *attr;
1829
1830 while ((attr = *attrs++))
1831 device_remove_file(dev->dev, attr);
1832 device_destroy(android_class, dev->dev->devt);
1833}
1834
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001835static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
1836{
1837 struct android_dev *dev = NULL;
1838
1839 /* Find the android dev from the list */
1840 list_for_each_entry(dev, &android_dev_list, list_item) {
1841 if (dev->cdev == cdev)
1842 break;
1843 }
1844
1845 return dev;
1846}
1847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001848static int __devinit android_probe(struct platform_device *pdev)
1849{
1850 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001851 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001852 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001853
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001854 if (!android_class) {
1855 android_class = class_create(THIS_MODULE, "android_usb");
1856 if (IS_ERR(android_class))
1857 return PTR_ERR(android_class);
1858 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001859
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001860 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
1861 if (!android_dev) {
1862 pr_err("%s(): Failed to alloc memory for android_dev\n",
1863 __func__);
1864 ret = -ENOMEM;
1865 goto err_alloc;
1866 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001867
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001868 android_dev->config.label = pdev->name;
1869 android_dev->config.unbind = android_unbind_config;
1870 android_dev->config.bConfigurationValue = 1;
1871 android_dev->disable_depth = 1;
1872 android_dev->functions = supported_functions;
1873 INIT_LIST_HEAD(&android_dev->enabled_functions);
1874 INIT_WORK(&android_dev->work, android_work);
1875 mutex_init(&android_dev->mutex);
1876
1877 android_dev->pdata = pdata;
1878
1879 list_add_tail(&android_dev->list_item, &android_dev_list);
1880 android_dev_count++;
1881
1882 if (pdata)
1883 composite_driver.usb_core_id = pdata->usb_core_id;
1884 else
1885 composite_driver.usb_core_id = 0; /*To backward compatibility*/
1886
1887 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301888 if (ret) {
1889 pr_err("%s(): android_create_device failed\n", __func__);
1890 goto err_dev;
1891 }
1892
Lena Salmand092f2d2012-03-12 17:27:24 +02001893 ret = usb_composite_probe(&android_usb_driver, android_bind);
1894 if (ret) {
1895 pr_err("%s(): Failed to register android "
1896 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301897 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001898 }
1899
Ofir Cohen94213a72012-05-03 14:26:32 +03001900 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301901 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001902 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03001903 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001904 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03001905
Lena Salmand092f2d2012-03-12 17:27:24 +02001906 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301907err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001908 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301909err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001910 list_del(&android_dev->list_item);
1911 android_dev_count--;
1912 kfree(android_dev);
1913err_alloc:
1914 if (list_empty(&android_dev_list)) {
1915 class_destroy(android_class);
1916 android_class = NULL;
1917 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301918 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001919}
1920
1921static int android_remove(struct platform_device *pdev)
1922{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001923 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03001924 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001925 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301926
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001927 if (pdata)
1928 usb_core_id = pdata->usb_core_id;
1929
1930 /* Find the android dev from the list */
1931 list_for_each_entry(dev, &android_dev_list, list_item) {
1932 if (!dev->pdata)
1933 break; /*To backward compatibility*/
1934 if (dev->pdata->usb_core_id == usb_core_id)
1935 break;
1936 }
1937
1938 if (dev) {
1939 android_destroy_device(dev);
1940 if (pdata && pdata->swfi_latency)
1941 pm_qos_remove_request(&dev->pm_qos_req_dma);
1942 list_del(&dev->list_item);
1943 android_dev_count--;
1944 kfree(dev);
1945 }
1946
1947 if (list_empty(&android_dev_list)) {
1948 class_destroy(android_class);
1949 android_class = NULL;
1950 usb_composite_unregister(&android_usb_driver);
1951 }
Ofir Cohen94213a72012-05-03 14:26:32 +03001952
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953 return 0;
1954}
1955
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001956static const struct platform_device_id android_id_table[] __devinitconst = {
1957 {
1958 .name = "android_usb",
1959 },
1960 {
1961 .name = "android_usb_hsic",
1962 },
1963};
1964
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001965static struct platform_driver android_platform_driver = {
1966 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001967 .probe = android_probe,
1968 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001969 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001971
1972static int __init init(void)
1973{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301974 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001975
Benoit Goby1e8ce152011-12-12 13:01:23 -08001976 /* Override composite driver functions */
1977 composite_driver.setup = android_setup;
1978 composite_driver.disconnect = android_disconnect;
1979
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001980 INIT_LIST_HEAD(&android_dev_list);
1981 android_dev_count = 0;
1982
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301983 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301984 if (ret) {
1985 pr_err("%s(): Failed to register android"
1986 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301987 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001988
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301989 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001990}
1991module_init(init);
1992
1993static void __exit cleanup(void)
1994{
Lena Salmand092f2d2012-03-12 17:27:24 +02001995 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001996}
1997module_exit(cleanup);