blob: 3f240c0f3d38469e6fe2b4b3b1eac6ce129ccf09 [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"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070068#ifdef CONFIG_USB_ANDROID_CDC_ECM
69#include "f_ecm.c"
70#else
Benoit Goby1e8ce152011-12-12 13:01:23 -080071#define USB_ETH_RNDIS y
72#include "f_rndis.c"
73#include "rndis.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070074#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080075#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030076#include "u_bam_data.c"
77#include "f_mbim.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080078
79MODULE_AUTHOR("Mike Lockwood");
80MODULE_DESCRIPTION("Android Composite USB Driver");
81MODULE_LICENSE("GPL");
82MODULE_VERSION("1.0");
83
84static const char longname[] = "Gadget Android";
85
86/* Default vendor and product IDs, overridden by userspace */
87#define VENDOR_ID 0x18D1
88#define PRODUCT_ID 0x0001
89
90struct android_usb_function {
91 char *name;
92 void *config;
93
94 struct device *dev;
95 char *dev_name;
96 struct device_attribute **attributes;
97
98 /* for android_dev.enabled_functions */
99 struct list_head enabled_list;
100
101 /* 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;
139};
140
141static struct class *android_class;
142static struct android_dev *_android_dev;
143static int android_bind_config(struct usb_configuration *c);
144static void android_unbind_config(struct usb_configuration *c);
145
146/* string IDs are assigned dynamically */
147#define STRING_MANUFACTURER_IDX 0
148#define STRING_PRODUCT_IDX 1
149#define STRING_SERIAL_IDX 2
150
151static char manufacturer_string[256];
152static char product_string[256];
153static char serial_string[256];
154
155/* String Table */
156static struct usb_string strings_dev[] = {
157 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
158 [STRING_PRODUCT_IDX].s = product_string,
159 [STRING_SERIAL_IDX].s = serial_string,
160 { } /* end of list */
161};
162
163static struct usb_gadget_strings stringtab_dev = {
164 .language = 0x0409, /* en-us */
165 .strings = strings_dev,
166};
167
168static struct usb_gadget_strings *dev_strings[] = {
169 &stringtab_dev,
170 NULL,
171};
172
173static struct usb_device_descriptor device_desc = {
174 .bLength = sizeof(device_desc),
175 .bDescriptorType = USB_DT_DEVICE,
176 .bcdUSB = __constant_cpu_to_le16(0x0200),
177 .bDeviceClass = USB_CLASS_PER_INTERFACE,
178 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
179 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
180 .bcdDevice = __constant_cpu_to_le16(0xffff),
181 .bNumConfigurations = 1,
182};
183
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530184static struct usb_otg_descriptor otg_descriptor = {
185 .bLength = sizeof otg_descriptor,
186 .bDescriptorType = USB_DT_OTG,
187 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
188 .bcdOTG = __constant_cpu_to_le16(0x0200),
189};
190
191static const struct usb_descriptor_header *otg_desc[] = {
192 (struct usb_descriptor_header *) &otg_descriptor,
193 NULL,
194};
195
Benoit Goby1e8ce152011-12-12 13:01:23 -0800196static struct usb_configuration android_config_driver = {
197 .label = "android",
198 .unbind = android_unbind_config,
199 .bConfigurationValue = 1,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800200};
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) {
298 usb_add_config(cdev, &android_config_driver,
299 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);
312 usb_remove_config(cdev, &android_config_driver);
313 }
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{
350 struct android_dev *dev = _android_dev;
351 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{
362 struct android_dev *dev = _android_dev;
363 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{
383 struct android_dev *dev = _android_dev;
384 struct adb_data *data = adb_function.config;
385
386 mutex_lock(&dev->mutex);
387
388 data->opened = true;
389
390 if (data->enabled)
391 android_enable(dev);
392
393 mutex_unlock(&dev->mutex);
394}
395
396static void adb_closed_callback(void)
397{
398 struct android_dev *dev = _android_dev;
399 struct adb_data *data = adb_function.config;
400
401 mutex_lock(&dev->mutex);
402
403 data->opened = false;
404
405 if (data->enabled)
406 android_disable(dev);
407
408 mutex_unlock(&dev->mutex);
409}
410
Benoit Goby2b6862d2011-12-19 14:38:41 -0800411
Benoit Gobyaab96812011-04-19 20:37:33 -0700412/*-------------------------------------------------------------------------*/
413/* Supported functions initialization */
414
Manu Gautam8e0719b2011-09-26 14:47:55 +0530415/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530416static int rmnet_smd_function_bind_config(struct android_usb_function *f,
417 struct usb_configuration *c)
418{
419 return rmnet_smd_bind_config(c);
420}
421
422static struct android_usb_function rmnet_smd_function = {
423 .name = "rmnet_smd",
424 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800425};
426
Manu Gautam8e0719b2011-09-26 14:47:55 +0530427/* RMNET_SDIO */
428static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
429 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800430{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530431 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800432}
433
Manu Gautam8e0719b2011-09-26 14:47:55 +0530434static struct android_usb_function rmnet_sdio_function = {
435 .name = "rmnet_sdio",
436 .bind_config = rmnet_sdio_function_bind_config,
437};
438
439/* RMNET_SMD_SDIO */
440static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
441 struct usb_composite_dev *cdev)
442{
443 return rmnet_smd_sdio_init();
444}
445
446static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
447{
448 rmnet_smd_sdio_cleanup();
449}
450
451static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
452 struct usb_configuration *c)
453{
454 return rmnet_smd_sdio_function_add(c);
455}
456
457static struct device_attribute *rmnet_smd_sdio_attributes[] = {
458 &dev_attr_transport, NULL };
459
460static struct android_usb_function rmnet_smd_sdio_function = {
461 .name = "rmnet_smd_sdio",
462 .init = rmnet_smd_sdio_function_init,
463 .cleanup = rmnet_smd_sdio_function_cleanup,
464 .bind_config = rmnet_smd_sdio_bind_config,
465 .attributes = rmnet_smd_sdio_attributes,
466};
467
Hemant Kumar1b820d52011-11-03 15:08:28 -0700468/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
469#define MAX_XPORT_STR_LEN 50
470static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530471
Manu Gautame3e897c2011-09-12 17:18:46 +0530472static void rmnet_function_cleanup(struct android_usb_function *f)
473{
474 frmnet_cleanup();
475}
476
Manu Gautam2b0234a2011-09-07 16:47:52 +0530477static int rmnet_function_bind_config(struct android_usb_function *f,
478 struct usb_configuration *c)
479{
480 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700481 int err = 0;
482 char *ctrl_name;
483 char *data_name;
484 char buf[MAX_XPORT_STR_LEN], *b;
485 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530486
Hemant Kumar1b820d52011-11-03 15:08:28 -0700487 if (!rmnet_initialized) {
488 rmnet_initialized = 1;
489 strlcpy(buf, rmnet_transports, sizeof(buf));
490 b = strim(buf);
491 while (b) {
492 ctrl_name = strsep(&b, ",");
493 data_name = strsep(&b, ",");
494 if (ctrl_name && data_name) {
495 err = frmnet_init_port(ctrl_name, data_name);
496 if (err) {
497 pr_err("rmnet: Cannot open ctrl port:"
498 "'%s' data port:'%s'\n",
499 ctrl_name, data_name);
500 goto out;
501 }
502 ports++;
503 }
504 }
505
506 err = rmnet_gport_setup();
507 if (err) {
508 pr_err("rmnet: Cannot setup transports");
509 goto out;
510 }
511 }
512
513 for (i = 0; i < ports; i++) {
514 err = frmnet_bind_config(c, i);
515 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530516 pr_err("Could not bind rmnet%u config\n", i);
517 break;
518 }
519 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700520out:
521 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530522}
523
Hemant Kumar1b820d52011-11-03 15:08:28 -0700524static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530525 struct device_attribute *attr, char *buf)
526{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700527 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530528}
529
Hemant Kumar1b820d52011-11-03 15:08:28 -0700530static ssize_t rmnet_transports_store(
531 struct device *device, struct device_attribute *attr,
532 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530533{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700534 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530535
Manu Gautam2b0234a2011-09-07 16:47:52 +0530536 return size;
537}
538
Hemant Kumar1b820d52011-11-03 15:08:28 -0700539static struct device_attribute dev_attr_rmnet_transports =
540 __ATTR(transports, S_IRUGO | S_IWUSR,
541 rmnet_transports_show,
542 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530543static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700544 &dev_attr_rmnet_transports,
545 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530546
547static struct android_usb_function rmnet_function = {
548 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530549 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530550 .bind_config = rmnet_function_bind_config,
551 .attributes = rmnet_function_attributes,
552};
553
Anna Perela8c991d2012-04-09 16:44:46 +0300554
555/* MBIM - used with BAM */
556#define MAX_MBIM_INSTANCES 1
557
558static int mbim_function_init(struct android_usb_function *f,
559 struct usb_composite_dev *cdev)
560{
561 return mbim_init(MAX_MBIM_INSTANCES);
562}
563
564static void mbim_function_cleanup(struct android_usb_function *f)
565{
566 fmbim_cleanup();
567}
568
569static int mbim_function_bind_config(struct android_usb_function *f,
570 struct usb_configuration *c)
571{
572 return mbim_bind_config(c, 0);
573}
574
575static struct android_usb_function mbim_function = {
576 .name = "usb_mbim",
577 .cleanup = mbim_function_cleanup,
578 .bind_config = mbim_function_bind_config,
579 .init = mbim_function_init,
580};
581
582
Manu Gautam8e0719b2011-09-26 14:47:55 +0530583/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530584static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585static ssize_t clients_store(
586 struct device *device, struct device_attribute *attr,
587 const char *buff, size_t size)
588{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530589 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
591 return size;
592}
593
594static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
595static struct device_attribute *diag_function_attributes[] =
596 { &dev_attr_clients, NULL };
597
598static int diag_function_init(struct android_usb_function *f,
599 struct usb_composite_dev *cdev)
600{
601 return diag_setup();
602}
603
604static void diag_function_cleanup(struct android_usb_function *f)
605{
606 diag_cleanup();
607}
608
609static int diag_function_bind_config(struct android_usb_function *f,
610 struct usb_configuration *c)
611{
612 char *name;
613 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530614 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800615 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530617 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 b = strim(buf);
619
620 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800621 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530623 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700624 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530625 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530626
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530628 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629 if (err)
630 pr_err("diag: Cannot open channel '%s'", name);
631 }
632 }
633
634 return err;
635}
636
637static struct android_usb_function diag_function = {
638 .name = "diag",
639 .init = diag_function_init,
640 .cleanup = diag_function_cleanup,
641 .bind_config = diag_function_bind_config,
642 .attributes = diag_function_attributes,
643};
644
Manu Gautam8e0719b2011-09-26 14:47:55 +0530645/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530646static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530647static ssize_t serial_transports_store(
648 struct device *device, struct device_attribute *attr,
649 const char *buff, size_t size)
650{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530651 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530652
653 return size;
654}
655
656static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
657static struct device_attribute *serial_function_attributes[] =
658 { &dev_attr_transports, NULL };
659
660static void serial_function_cleanup(struct android_usb_function *f)
661{
662 gserial_cleanup();
663}
664
665static int serial_function_bind_config(struct android_usb_function *f,
666 struct usb_configuration *c)
667{
668 char *name;
669 char buf[32], *b;
670 int err = -1, i;
671 static int serial_initialized = 0, ports = 0;
672
673 if (serial_initialized)
674 goto bind_config;
675
676 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530677 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530678 b = strim(buf);
679
680 while (b) {
681 name = strsep(&b, ",");
682
683 if (name) {
684 err = gserial_init_port(ports, name);
685 if (err) {
686 pr_err("serial: Cannot open port '%s'", name);
687 goto out;
688 }
689 ports++;
690 }
691 }
692 err = gport_setup(c);
693 if (err) {
694 pr_err("serial: Cannot setup transports");
695 goto out;
696 }
697
698bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200699 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530700 err = gser_bind_config(c, i);
701 if (err) {
702 pr_err("serial: bind_config failed for port %d", i);
703 goto out;
704 }
705 }
706
707out:
708 return err;
709}
710
711static struct android_usb_function serial_function = {
712 .name = "serial",
713 .cleanup = serial_function_cleanup,
714 .bind_config = serial_function_bind_config,
715 .attributes = serial_function_attributes,
716};
717
Anji jonnala92be1b42011-12-19 09:44:41 +0530718/* ACM */
719static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
720static ssize_t acm_transports_store(
721 struct device *device, struct device_attribute *attr,
722 const char *buff, size_t size)
723{
724 strlcpy(acm_transports, buff, sizeof(acm_transports));
725
726 return size;
727}
728
729static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
730static struct device_attribute *acm_function_attributes[] = {
731 &dev_attr_acm_transports, NULL };
732
Benoit Goby1e8ce152011-12-12 13:01:23 -0800733static void acm_function_cleanup(struct android_usb_function *f)
734{
735 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800736}
737
Anji jonnala92be1b42011-12-19 09:44:41 +0530738static int acm_function_bind_config(struct android_usb_function *f,
739 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800740{
Anji jonnala92be1b42011-12-19 09:44:41 +0530741 char *name;
742 char buf[32], *b;
743 int err = -1, i;
744 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800745
Anji jonnala92be1b42011-12-19 09:44:41 +0530746 if (acm_initialized)
747 goto bind_config;
748
749 acm_initialized = 1;
750 strlcpy(buf, acm_transports, sizeof(buf));
751 b = strim(buf);
752
753 while (b) {
754 name = strsep(&b, ",");
755
756 if (name) {
757 err = acm_init_port(ports, name);
758 if (err) {
759 pr_err("acm: Cannot open port '%s'", name);
760 goto out;
761 }
762 ports++;
763 }
764 }
765 err = acm_port_setup(c);
766 if (err) {
767 pr_err("acm: Cannot setup transports");
768 goto out;
769 }
770
771bind_config:
772 for (i = 0; i < ports; i++) {
773 err = acm_bind_config(c, i);
774 if (err) {
775 pr_err("acm: bind_config failed for port %d", i);
776 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800777 }
778 }
779
Anji jonnala92be1b42011-12-19 09:44:41 +0530780out:
781 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800782}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800783static struct android_usb_function acm_function = {
784 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800785 .cleanup = acm_function_cleanup,
786 .bind_config = acm_function_bind_config,
787 .attributes = acm_function_attributes,
788};
789
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530790/* CCID */
791static int ccid_function_init(struct android_usb_function *f,
792 struct usb_composite_dev *cdev)
793{
794 return ccid_setup();
795}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800796
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530797static void ccid_function_cleanup(struct android_usb_function *f)
798{
799 ccid_cleanup();
800}
801
802static int ccid_function_bind_config(struct android_usb_function *f,
803 struct usb_configuration *c)
804{
805 return ccid_bind_config(c);
806}
807
808static struct android_usb_function ccid_function = {
809 .name = "ccid",
810 .init = ccid_function_init,
811 .cleanup = ccid_function_cleanup,
812 .bind_config = ccid_function_bind_config,
813};
814
Steve Mucklef132c6c2012-06-06 18:30:57 -0700815static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800816 struct usb_composite_dev *cdev)
817{
818 return mtp_setup();
819}
820
821static void mtp_function_cleanup(struct android_usb_function *f)
822{
823 mtp_cleanup();
824}
825
Steve Mucklef132c6c2012-06-06 18:30:57 -0700826static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800827 struct usb_configuration *c)
828{
829 return mtp_bind_config(c, false);
830}
831
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400832static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800833{
834 /* nothing to do - initialization is handled by mtp_function_init */
835 return 0;
836}
837
838static void ptp_function_cleanup(struct android_usb_function *f)
839{
840 /* nothing to do - cleanup is handled by mtp_function_cleanup */
841}
842
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400843static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800844{
845 return mtp_bind_config(c, true);
846}
847
848static int mtp_function_ctrlrequest(struct android_usb_function *f,
849 struct usb_composite_dev *cdev,
850 const struct usb_ctrlrequest *c)
851{
852 return mtp_ctrlrequest(cdev, c);
853}
854
855static struct android_usb_function mtp_function = {
856 .name = "mtp",
857 .init = mtp_function_init,
858 .cleanup = mtp_function_cleanup,
859 .bind_config = mtp_function_bind_config,
860 .ctrlrequest = mtp_function_ctrlrequest,
861};
862
863/* PTP function is same as MTP with slightly different interface descriptor */
864static struct android_usb_function ptp_function = {
865 .name = "ptp",
866 .init = ptp_function_init,
867 .cleanup = ptp_function_cleanup,
868 .bind_config = ptp_function_bind_config,
869};
870
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700871#ifdef CONFIG_USB_ANDROID_CDC_ECM
872struct ecm_function_config {
873 u8 ethaddr[ETH_ALEN];
874 u32 vendorID;
875 char manufacturer[256];
876};
877
878static int ecm_function_init(struct android_usb_function *f,
879 struct usb_composite_dev *cdev)
880{
881 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
882 if (!f->config)
883 return -ENOMEM;
884 return 0;
885}
886
887static void ecm_function_cleanup(struct android_usb_function *f)
888{
889 kfree(f->config);
890 f->config = NULL;
891}
892
893static int ecm_function_bind_config(struct android_usb_function *f,
894 struct usb_configuration *c)
895{
896 int ret;
897 struct ecm_function_config *ecm = f->config;
898
899 if (!ecm) {
900 pr_err("%s: ecm_function_config\n", __func__);
901 return -1;
902 }
903
904 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
905 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
906 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
907
908 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usb");
909 if (ret) {
910 pr_err("%s: gether_setup failed\n", __func__);
911 return ret;
912 }
913
914 return ecm_bind_config(c, ecm->ethaddr);
915}
916
917static void ecm_function_unbind_config(struct android_usb_function *f,
918 struct usb_configuration *c)
919{
920 gether_cleanup();
921}
922
923static ssize_t ecm_manufacturer_show(struct device *dev,
924 struct device_attribute *attr, char *buf)
925{
926 struct android_usb_function *f = dev_get_drvdata(dev);
927 struct ecm_function_config *config = f->config;
928
929 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
930}
931
932static ssize_t ecm_manufacturer_store(struct device *dev,
933 struct device_attribute *attr, const char *buf, size_t size)
934{
935 struct android_usb_function *f = dev_get_drvdata(dev);
936 struct ecm_function_config *config = f->config;
937
938 if (size >= sizeof(config->manufacturer))
939 return -EINVAL;
940
941 if (sscanf(buf, "%255s", config->manufacturer) == 1)
942 return size;
943 return -1;
944}
945
946static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, ecm_manufacturer_show,
947 ecm_manufacturer_store);
948
949static ssize_t ecm_ethaddr_show(struct device *dev,
950 struct device_attribute *attr, char *buf)
951{
952 struct android_usb_function *f = dev_get_drvdata(dev);
953 struct ecm_function_config *ecm = f->config;
954 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
955 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
956 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
957}
958
959static ssize_t ecm_ethaddr_store(struct device *dev,
960 struct device_attribute *attr, const char *buf, size_t size)
961{
962 struct android_usb_function *f = dev_get_drvdata(dev);
963 struct ecm_function_config *ecm = f->config;
964
965 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
966 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
967 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
968 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
969 return size;
970 return -EINVAL;
971}
972
973static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
974 ecm_ethaddr_store);
975
976static ssize_t ecm_vendorID_show(struct device *dev,
977 struct device_attribute *attr, char *buf)
978{
979 struct android_usb_function *f = dev_get_drvdata(dev);
980 struct ecm_function_config *config = f->config;
981
982 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
983}
984
985static ssize_t ecm_vendorID_store(struct device *dev,
986 struct device_attribute *attr, const char *buf, size_t size)
987{
988 struct android_usb_function *f = dev_get_drvdata(dev);
989 struct ecm_function_config *config = f->config;
990 int value;
991
992 if (sscanf(buf, "%04x", &value) == 1) {
993 config->vendorID = value;
994 return size;
995 }
996 return -EINVAL;
997}
998
999static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, ecm_vendorID_show,
1000 ecm_vendorID_store);
1001
1002static struct device_attribute *ecm_function_attributes[] = {
1003 &dev_attr_manufacturer,
1004 &dev_attr_ethaddr,
1005 &dev_attr_vendorID,
1006 NULL
1007};
1008
1009static struct android_usb_function ecm_function = {
1010 .name = "ecm",
1011 .init = ecm_function_init,
1012 .cleanup = ecm_function_cleanup,
1013 .bind_config = ecm_function_bind_config,
1014 .unbind_config = ecm_function_unbind_config,
1015 .attributes = ecm_function_attributes,
1016};
1017
1018#else
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001019
Benoit Goby1e8ce152011-12-12 13:01:23 -08001020struct rndis_function_config {
1021 u8 ethaddr[ETH_ALEN];
1022 u32 vendorID;
1023 char manufacturer[256];
1024 /* "Wireless" RNDIS; auto-detected by Windows */
1025 bool wceis;
1026};
1027
1028static int
1029rndis_function_init(struct android_usb_function *f,
1030 struct usb_composite_dev *cdev)
1031{
1032 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1033 if (!f->config)
1034 return -ENOMEM;
1035 return 0;
1036}
1037
1038static void rndis_function_cleanup(struct android_usb_function *f)
1039{
1040 kfree(f->config);
1041 f->config = NULL;
1042}
1043
1044static int
1045rndis_function_bind_config(struct android_usb_function *f,
1046 struct usb_configuration *c)
1047{
1048 int ret;
1049 struct rndis_function_config *rndis = f->config;
1050
1051 if (!rndis) {
1052 pr_err("%s: rndis_pdata\n", __func__);
1053 return -1;
1054 }
1055
1056 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1057 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1058 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1059
1060 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1061 if (ret) {
1062 pr_err("%s: gether_setup failed\n", __func__);
1063 return ret;
1064 }
1065
1066 if (rndis->wceis) {
1067 /* "Wireless" RNDIS; auto-detected by Windows */
1068 rndis_iad_descriptor.bFunctionClass =
1069 USB_CLASS_WIRELESS_CONTROLLER;
1070 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1071 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1072 rndis_control_intf.bInterfaceClass =
1073 USB_CLASS_WIRELESS_CONTROLLER;
1074 rndis_control_intf.bInterfaceSubClass = 0x01;
1075 rndis_control_intf.bInterfaceProtocol = 0x03;
1076 }
1077
1078 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1079 rndis->manufacturer);
1080}
1081
1082static void rndis_function_unbind_config(struct android_usb_function *f,
1083 struct usb_configuration *c)
1084{
1085 gether_cleanup();
1086}
1087
1088static ssize_t rndis_manufacturer_show(struct device *dev,
1089 struct device_attribute *attr, char *buf)
1090{
1091 struct android_usb_function *f = dev_get_drvdata(dev);
1092 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001093
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301094 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001095}
1096
1097static ssize_t rndis_manufacturer_store(struct device *dev,
1098 struct device_attribute *attr, const char *buf, size_t size)
1099{
1100 struct android_usb_function *f = dev_get_drvdata(dev);
1101 struct rndis_function_config *config = f->config;
1102
1103 if (size >= sizeof(config->manufacturer))
1104 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001105
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301106 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001107 return size;
1108 return -1;
1109}
1110
1111static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1112 rndis_manufacturer_store);
1113
1114static ssize_t rndis_wceis_show(struct device *dev,
1115 struct device_attribute *attr, char *buf)
1116{
1117 struct android_usb_function *f = dev_get_drvdata(dev);
1118 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001119
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301120 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001121}
1122
1123static ssize_t rndis_wceis_store(struct device *dev,
1124 struct device_attribute *attr, const char *buf, size_t size)
1125{
1126 struct android_usb_function *f = dev_get_drvdata(dev);
1127 struct rndis_function_config *config = f->config;
1128 int value;
1129
1130 if (sscanf(buf, "%d", &value) == 1) {
1131 config->wceis = value;
1132 return size;
1133 }
1134 return -EINVAL;
1135}
1136
1137static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1138 rndis_wceis_store);
1139
1140static ssize_t rndis_ethaddr_show(struct device *dev,
1141 struct device_attribute *attr, char *buf)
1142{
1143 struct android_usb_function *f = dev_get_drvdata(dev);
1144 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001145
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301146 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001147 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1148 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1149}
1150
1151static ssize_t rndis_ethaddr_store(struct device *dev,
1152 struct device_attribute *attr, const char *buf, size_t size)
1153{
1154 struct android_usb_function *f = dev_get_drvdata(dev);
1155 struct rndis_function_config *rndis = f->config;
1156
1157 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1158 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1159 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1160 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1161 return size;
1162 return -EINVAL;
1163}
1164
1165static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1166 rndis_ethaddr_store);
1167
1168static ssize_t rndis_vendorID_show(struct device *dev,
1169 struct device_attribute *attr, char *buf)
1170{
1171 struct android_usb_function *f = dev_get_drvdata(dev);
1172 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001173
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301174 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001175}
1176
1177static ssize_t rndis_vendorID_store(struct device *dev,
1178 struct device_attribute *attr, const char *buf, size_t size)
1179{
1180 struct android_usb_function *f = dev_get_drvdata(dev);
1181 struct rndis_function_config *config = f->config;
1182 int value;
1183
1184 if (sscanf(buf, "%04x", &value) == 1) {
1185 config->vendorID = value;
1186 return size;
1187 }
1188 return -EINVAL;
1189}
1190
1191static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1192 rndis_vendorID_store);
1193
1194static struct device_attribute *rndis_function_attributes[] = {
1195 &dev_attr_manufacturer,
1196 &dev_attr_wceis,
1197 &dev_attr_ethaddr,
1198 &dev_attr_vendorID,
1199 NULL
1200};
1201
1202static struct android_usb_function rndis_function = {
1203 .name = "rndis",
1204 .init = rndis_function_init,
1205 .cleanup = rndis_function_cleanup,
1206 .bind_config = rndis_function_bind_config,
1207 .unbind_config = rndis_function_unbind_config,
1208 .attributes = rndis_function_attributes,
1209};
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001210#endif /* CONFIG_USB_ANDROID_CDC_ECM */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001211
1212struct mass_storage_function_config {
1213 struct fsg_config fsg;
1214 struct fsg_common *common;
1215};
1216
1217static int mass_storage_function_init(struct android_usb_function *f,
1218 struct usb_composite_dev *cdev)
1219{
1220 struct mass_storage_function_config *config;
1221 struct fsg_common *common;
1222 int err;
1223
1224 config = kzalloc(sizeof(struct mass_storage_function_config),
1225 GFP_KERNEL);
1226 if (!config)
1227 return -ENOMEM;
1228
1229 config->fsg.nluns = 1;
1230 config->fsg.luns[0].removable = 1;
1231
1232 common = fsg_common_init(NULL, cdev, &config->fsg);
1233 if (IS_ERR(common)) {
1234 kfree(config);
1235 return PTR_ERR(common);
1236 }
1237
1238 err = sysfs_create_link(&f->dev->kobj,
1239 &common->luns[0].dev.kobj,
1240 "lun");
1241 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +05301242 fsg_common_release(&common->ref);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001243 kfree(config);
1244 return err;
1245 }
1246
1247 config->common = common;
1248 f->config = config;
1249 return 0;
1250}
1251
1252static void mass_storage_function_cleanup(struct android_usb_function *f)
1253{
1254 kfree(f->config);
1255 f->config = NULL;
1256}
1257
1258static int mass_storage_function_bind_config(struct android_usb_function *f,
1259 struct usb_configuration *c)
1260{
1261 struct mass_storage_function_config *config = f->config;
1262 return fsg_bind_config(c->cdev, c, config->common);
1263}
1264
1265static ssize_t mass_storage_inquiry_show(struct device *dev,
1266 struct device_attribute *attr, char *buf)
1267{
1268 struct android_usb_function *f = dev_get_drvdata(dev);
1269 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301270 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001271}
1272
1273static ssize_t mass_storage_inquiry_store(struct device *dev,
1274 struct device_attribute *attr, const char *buf, size_t size)
1275{
1276 struct android_usb_function *f = dev_get_drvdata(dev);
1277 struct mass_storage_function_config *config = f->config;
1278 if (size >= sizeof(config->common->inquiry_string))
1279 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301280 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001281 return -EINVAL;
1282 return size;
1283}
1284
1285static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1286 mass_storage_inquiry_show,
1287 mass_storage_inquiry_store);
1288
1289static struct device_attribute *mass_storage_function_attributes[] = {
1290 &dev_attr_inquiry_string,
1291 NULL
1292};
1293
1294static struct android_usb_function mass_storage_function = {
1295 .name = "mass_storage",
1296 .init = mass_storage_function_init,
1297 .cleanup = mass_storage_function_cleanup,
1298 .bind_config = mass_storage_function_bind_config,
1299 .attributes = mass_storage_function_attributes,
1300};
1301
1302
Benoit Gobycf3fc062011-12-19 14:39:37 -08001303static int accessory_function_init(struct android_usb_function *f,
1304 struct usb_composite_dev *cdev)
1305{
1306 return acc_setup();
1307}
1308
1309static void accessory_function_cleanup(struct android_usb_function *f)
1310{
1311 acc_cleanup();
1312}
1313
1314static int accessory_function_bind_config(struct android_usb_function *f,
1315 struct usb_configuration *c)
1316{
1317 return acc_bind_config(c);
1318}
1319
1320static int accessory_function_ctrlrequest(struct android_usb_function *f,
1321 struct usb_composite_dev *cdev,
1322 const struct usb_ctrlrequest *c)
1323{
1324 return acc_ctrlrequest(cdev, c);
1325}
1326
1327static struct android_usb_function accessory_function = {
1328 .name = "accessory",
1329 .init = accessory_function_init,
1330 .cleanup = accessory_function_cleanup,
1331 .bind_config = accessory_function_bind_config,
1332 .ctrlrequest = accessory_function_ctrlrequest,
1333};
1334
1335
Benoit Goby1e8ce152011-12-12 13:01:23 -08001336static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001337 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301338 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301339 &rmnet_sdio_function,
1340 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301341 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301343 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001344 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301345 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001346 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001347 &mtp_function,
1348 &ptp_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001349#ifdef CONFIG_USB_ANDROID_CDC_ECM
1350 &ecm_function,
1351#else
Benoit Goby1e8ce152011-12-12 13:01:23 -08001352 &rndis_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001353#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -08001354 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001355 &accessory_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001356 NULL
1357};
1358
Lena Salmand092f2d2012-03-12 17:27:24 +02001359static void android_cleanup_functions(struct android_usb_function **functions)
1360{
1361 struct android_usb_function *f;
1362 struct device_attribute **attrs;
1363 struct device_attribute *attr;
1364
1365 while (*functions) {
1366 f = *functions++;
1367
1368 if (f->dev) {
1369 device_destroy(android_class, f->dev->devt);
1370 kfree(f->dev_name);
1371 } else
1372 continue;
1373
1374 if (f->cleanup)
1375 f->cleanup(f);
1376
1377 attrs = f->attributes;
1378 if (attrs) {
1379 while ((attr = *attrs++))
1380 device_remove_file(f->dev, attr);
1381 }
1382 }
1383}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001384
1385static int android_init_functions(struct android_usb_function **functions,
1386 struct usb_composite_dev *cdev)
1387{
1388 struct android_dev *dev = _android_dev;
1389 struct android_usb_function *f;
1390 struct device_attribute **attrs;
1391 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301392 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001393 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001394
1395 for (; (f = *functions++); index++) {
1396 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001397 if (!f->dev_name) {
1398 err = -ENOMEM;
1399 goto err_out;
1400 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001401 f->dev = device_create(android_class, dev->dev,
1402 MKDEV(0, index), f, f->dev_name);
1403 if (IS_ERR(f->dev)) {
1404 pr_err("%s: Failed to create dev %s", __func__,
1405 f->dev_name);
1406 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001407 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001408 goto err_create;
1409 }
1410
1411 if (f->init) {
1412 err = f->init(f, cdev);
1413 if (err) {
1414 pr_err("%s: Failed to init %s", __func__,
1415 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001416 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001417 }
1418 }
1419
1420 attrs = f->attributes;
1421 if (attrs) {
1422 while ((attr = *attrs++) && !err)
1423 err = device_create_file(f->dev, attr);
1424 }
1425 if (err) {
1426 pr_err("%s: Failed to create function %s attributes",
1427 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001428 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001429 }
1430 }
1431 return 0;
1432
Lena Salmand092f2d2012-03-12 17:27:24 +02001433err_attrs:
1434 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1435 device_remove_file(f->dev, attr);
1436 if (f->cleanup)
1437 f->cleanup(f);
1438err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001439 device_destroy(android_class, f->dev->devt);
1440err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001441 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001442 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001443err_out:
1444 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001445 return err;
1446}
1447
Benoit Goby1e8ce152011-12-12 13:01:23 -08001448static int
1449android_bind_enabled_functions(struct android_dev *dev,
1450 struct usb_configuration *c)
1451{
1452 struct android_usb_function *f;
1453 int ret;
1454
1455 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1456 ret = f->bind_config(f, c);
1457 if (ret) {
1458 pr_err("%s: %s failed", __func__, f->name);
1459 return ret;
1460 }
1461 }
1462 return 0;
1463}
1464
1465static void
1466android_unbind_enabled_functions(struct android_dev *dev,
1467 struct usb_configuration *c)
1468{
1469 struct android_usb_function *f;
1470
1471 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1472 if (f->unbind_config)
1473 f->unbind_config(f, c);
1474 }
1475}
1476
1477static int android_enable_function(struct android_dev *dev, char *name)
1478{
1479 struct android_usb_function **functions = dev->functions;
1480 struct android_usb_function *f;
1481 while ((f = *functions++)) {
1482 if (!strcmp(name, f->name)) {
1483 list_add_tail(&f->enabled_list,
1484 &dev->enabled_functions);
1485 return 0;
1486 }
1487 }
1488 return -EINVAL;
1489}
1490
1491/*-------------------------------------------------------------------------*/
1492/* /sys/class/android_usb/android%d/ interface */
1493
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301494static ssize_t remote_wakeup_show(struct device *pdev,
1495 struct device_attribute *attr, char *buf)
1496{
1497 return snprintf(buf, PAGE_SIZE, "%d\n",
1498 !!(android_config_driver.bmAttributes &
1499 USB_CONFIG_ATT_WAKEUP));
1500}
1501
1502static ssize_t remote_wakeup_store(struct device *pdev,
1503 struct device_attribute *attr, const char *buff, size_t size)
1504{
1505 int enable = 0;
1506
1507 sscanf(buff, "%d", &enable);
1508
1509 pr_debug("android_usb: %s remote wakeup\n",
1510 enable ? "enabling" : "disabling");
1511
1512 if (enable)
1513 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1514 else
1515 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1516
1517 return size;
1518}
1519
Benoit Goby1e8ce152011-12-12 13:01:23 -08001520static ssize_t
1521functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1522{
1523 struct android_dev *dev = dev_get_drvdata(pdev);
1524 struct android_usb_function *f;
1525 char *buff = buf;
1526
1527 mutex_lock(&dev->mutex);
1528
1529 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301530 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001531
1532 mutex_unlock(&dev->mutex);
1533
1534 if (buff != buf)
1535 *(buff-1) = '\n';
1536 return buff - buf;
1537}
1538
1539static ssize_t
1540functions_store(struct device *pdev, struct device_attribute *attr,
1541 const char *buff, size_t size)
1542{
1543 struct android_dev *dev = dev_get_drvdata(pdev);
1544 char *name;
1545 char buf[256], *b;
1546 int err;
1547
1548 mutex_lock(&dev->mutex);
1549
1550 if (dev->enabled) {
1551 mutex_unlock(&dev->mutex);
1552 return -EBUSY;
1553 }
1554
1555 INIT_LIST_HEAD(&dev->enabled_functions);
1556
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301557 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001558 b = strim(buf);
1559
1560 while (b) {
1561 name = strsep(&b, ",");
1562 if (name) {
1563 err = android_enable_function(dev, name);
1564 if (err)
1565 pr_err("android_usb: Cannot enable '%s'", name);
1566 }
1567 }
1568
1569 mutex_unlock(&dev->mutex);
1570
1571 return size;
1572}
1573
1574static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1575 char *buf)
1576{
1577 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001578
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301579 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001580}
1581
1582static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1583 const char *buff, size_t size)
1584{
1585 struct android_dev *dev = dev_get_drvdata(pdev);
1586 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001587 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001588 int enabled = 0;
1589
Benoit Gobycf3fc062011-12-19 14:39:37 -08001590 if (!cdev)
1591 return -ENODEV;
1592
Benoit Goby1e8ce152011-12-12 13:01:23 -08001593 mutex_lock(&dev->mutex);
1594
1595 sscanf(buff, "%d", &enabled);
1596 if (enabled && !dev->enabled) {
1597 cdev->next_string_id = 0;
1598 /*
1599 * Update values in composite driver's copy of
1600 * device descriptor.
1601 */
1602 cdev->desc.idVendor = device_desc.idVendor;
1603 cdev->desc.idProduct = device_desc.idProduct;
1604 cdev->desc.bcdDevice = device_desc.bcdDevice;
1605 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1606 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1607 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001608 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1609 if (f->enable)
1610 f->enable(f);
1611 }
1612 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001613 dev->enabled = true;
1614 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001615 android_disable(dev);
1616 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1617 if (f->disable)
1618 f->disable(f);
1619 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001620 dev->enabled = false;
1621 } else {
1622 pr_err("android_usb: already %s\n",
1623 dev->enabled ? "enabled" : "disabled");
1624 }
1625
1626 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001627
Benoit Gobyaab96812011-04-19 20:37:33 -07001628 return size;
1629}
1630
Ofir Cohen94213a72012-05-03 14:26:32 +03001631static ssize_t pm_qos_show(struct device *pdev,
1632 struct device_attribute *attr, char *buf)
1633{
1634 struct android_dev *dev = dev_get_drvdata(pdev);
1635
1636 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1637}
1638
1639static ssize_t pm_qos_store(struct device *pdev,
1640 struct device_attribute *attr,
1641 const char *buff, size_t size)
1642{
1643 struct android_dev *dev = dev_get_drvdata(pdev);
1644
1645 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1646
Benoit Goby1e8ce152011-12-12 13:01:23 -08001647 return size;
1648}
1649
1650static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1651 char *buf)
1652{
1653 struct android_dev *dev = dev_get_drvdata(pdev);
1654 struct usb_composite_dev *cdev = dev->cdev;
1655 char *state = "DISCONNECTED";
1656 unsigned long flags;
1657
1658 if (!cdev)
1659 goto out;
1660
1661 spin_lock_irqsave(&cdev->lock, flags);
1662 if (cdev->config)
1663 state = "CONFIGURED";
1664 else if (dev->connected)
1665 state = "CONNECTED";
1666 spin_unlock_irqrestore(&cdev->lock, flags);
1667out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301668 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001669}
1670
1671#define DESCRIPTOR_ATTR(field, format_string) \
1672static ssize_t \
1673field ## _show(struct device *dev, struct device_attribute *attr, \
1674 char *buf) \
1675{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301676 return snprintf(buf, PAGE_SIZE, \
1677 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001678} \
1679static ssize_t \
1680field ## _store(struct device *dev, struct device_attribute *attr, \
1681 const char *buf, size_t size) \
1682{ \
1683 int value; \
1684 if (sscanf(buf, format_string, &value) == 1) { \
1685 device_desc.field = value; \
1686 return size; \
1687 } \
1688 return -1; \
1689} \
1690static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1691
1692#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1693static ssize_t \
1694field ## _show(struct device *dev, struct device_attribute *attr, \
1695 char *buf) \
1696{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301697 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001698} \
1699static ssize_t \
1700field ## _store(struct device *dev, struct device_attribute *attr, \
1701 const char *buf, size_t size) \
1702{ \
1703 if (size >= sizeof(buffer)) \
1704 return -EINVAL; \
Benoit Gobydcefb2e2012-05-29 13:57:27 -07001705 return strlcpy(buffer, buf, sizeof(buffer)); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001706} \
1707static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1708
1709
1710DESCRIPTOR_ATTR(idVendor, "%04x\n")
1711DESCRIPTOR_ATTR(idProduct, "%04x\n")
1712DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1713DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1714DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1715DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1716DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1717DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1718DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1719
1720static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1721 functions_store);
1722static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001723static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1724 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001725static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301726static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1727 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001728
1729static struct device_attribute *android_usb_attributes[] = {
1730 &dev_attr_idVendor,
1731 &dev_attr_idProduct,
1732 &dev_attr_bcdDevice,
1733 &dev_attr_bDeviceClass,
1734 &dev_attr_bDeviceSubClass,
1735 &dev_attr_bDeviceProtocol,
1736 &dev_attr_iManufacturer,
1737 &dev_attr_iProduct,
1738 &dev_attr_iSerial,
1739 &dev_attr_functions,
1740 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001741 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001742 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301743 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001744 NULL
1745};
1746
1747/*-------------------------------------------------------------------------*/
1748/* Composite driver */
1749
1750static int android_bind_config(struct usb_configuration *c)
1751{
1752 struct android_dev *dev = _android_dev;
1753 int ret = 0;
1754
1755 ret = android_bind_enabled_functions(dev, c);
1756 if (ret)
1757 return ret;
1758
1759 return 0;
1760}
1761
1762static void android_unbind_config(struct usb_configuration *c)
1763{
1764 struct android_dev *dev = _android_dev;
1765
1766 android_unbind_enabled_functions(dev, c);
1767}
1768
1769static int android_bind(struct usb_composite_dev *cdev)
1770{
1771 struct android_dev *dev = _android_dev;
1772 struct usb_gadget *gadget = cdev->gadget;
1773 int gcnum, id, ret;
1774
1775 /*
1776 * Start disconnected. Userspace will connect the gadget once
1777 * it is done configuring the functions.
1778 */
1779 usb_gadget_disconnect(gadget);
1780
1781 ret = android_init_functions(dev->functions, cdev);
1782 if (ret)
1783 return ret;
1784
1785 /* Allocate string descriptor numbers ... note that string
1786 * contents can be overridden by the composite_dev glue.
1787 */
1788 id = usb_string_id(cdev);
1789 if (id < 0)
1790 return id;
1791 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1792 device_desc.iManufacturer = id;
1793
1794 id = usb_string_id(cdev);
1795 if (id < 0)
1796 return id;
1797 strings_dev[STRING_PRODUCT_IDX].id = id;
1798 device_desc.iProduct = id;
1799
1800 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301801 strlcpy(manufacturer_string, "Android",
1802 sizeof(manufacturer_string) - 1);
1803 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1804 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001805
1806 id = usb_string_id(cdev);
1807 if (id < 0)
1808 return id;
1809 strings_dev[STRING_SERIAL_IDX].id = id;
1810 device_desc.iSerialNumber = id;
1811
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301812 if (gadget_is_otg(cdev->gadget))
1813 android_config_driver.descriptors = otg_desc;
1814
Benoit Goby1e8ce152011-12-12 13:01:23 -08001815 gcnum = usb_gadget_controller_number(gadget);
1816 if (gcnum >= 0)
1817 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1818 else {
1819 pr_warning("%s: controller '%s' not recognized\n",
1820 longname, gadget->name);
1821 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1822 }
1823
Benoit Goby1e8ce152011-12-12 13:01:23 -08001824 dev->cdev = cdev;
1825
1826 return 0;
1827}
1828
1829static int android_usb_unbind(struct usb_composite_dev *cdev)
1830{
1831 struct android_dev *dev = _android_dev;
1832
Lena Salmand092f2d2012-03-12 17:27:24 +02001833 manufacturer_string[0] = '\0';
1834 product_string[0] = '\0';
1835 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08001836 cancel_work_sync(&dev->work);
1837 android_cleanup_functions(dev->functions);
1838 return 0;
1839}
1840
1841static struct usb_composite_driver android_usb_driver = {
1842 .name = "android_usb",
1843 .dev = &device_desc,
1844 .strings = dev_strings,
1845 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001846 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08001847};
1848
1849static int
1850android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1851{
1852 struct android_dev *dev = _android_dev;
1853 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1854 struct usb_request *req = cdev->req;
1855 struct android_usb_function *f;
1856 int value = -EOPNOTSUPP;
1857 unsigned long flags;
1858
1859 req->zero = 0;
1860 req->complete = composite_setup_complete;
1861 req->length = 0;
1862 gadget->ep0->driver_data = cdev;
1863
1864 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1865 if (f->ctrlrequest) {
1866 value = f->ctrlrequest(f, cdev, c);
1867 if (value >= 0)
1868 break;
1869 }
1870 }
1871
Benoit Gobycf3fc062011-12-19 14:39:37 -08001872 /* Special case the accessory function.
1873 * It needs to handle control requests before it is enabled.
1874 */
1875 if (value < 0)
1876 value = acc_ctrlrequest(cdev, c);
1877
Benoit Goby1e8ce152011-12-12 13:01:23 -08001878 if (value < 0)
1879 value = composite_setup(gadget, c);
1880
1881 spin_lock_irqsave(&cdev->lock, flags);
1882 if (!dev->connected) {
1883 dev->connected = 1;
1884 schedule_work(&dev->work);
1885 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1886 cdev->config) {
1887 schedule_work(&dev->work);
1888 }
1889 spin_unlock_irqrestore(&cdev->lock, flags);
1890
1891 return value;
1892}
1893
1894static void android_disconnect(struct usb_gadget *gadget)
1895{
1896 struct android_dev *dev = _android_dev;
1897 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1898 unsigned long flags;
1899
1900 composite_disconnect(gadget);
1901
1902 spin_lock_irqsave(&cdev->lock, flags);
1903 dev->connected = 0;
1904 schedule_work(&dev->work);
1905 spin_unlock_irqrestore(&cdev->lock, flags);
1906}
1907
1908static int android_create_device(struct android_dev *dev)
1909{
1910 struct device_attribute **attrs = android_usb_attributes;
1911 struct device_attribute *attr;
1912 int err;
1913
1914 dev->dev = device_create(android_class, NULL,
1915 MKDEV(0, 0), NULL, "android0");
1916 if (IS_ERR(dev->dev))
1917 return PTR_ERR(dev->dev);
1918
1919 dev_set_drvdata(dev->dev, dev);
1920
1921 while ((attr = *attrs++)) {
1922 err = device_create_file(dev->dev, attr);
1923 if (err) {
1924 device_destroy(android_class, dev->dev->devt);
1925 return err;
1926 }
1927 }
1928 return 0;
1929}
1930
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301931static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001932{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301933 struct device_attribute **attrs = android_usb_attributes;
1934 struct device_attribute *attr;
1935
1936 while ((attr = *attrs++))
1937 device_remove_file(dev->dev, attr);
1938 device_destroy(android_class, dev->dev->devt);
1939}
1940
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941static int __devinit android_probe(struct platform_device *pdev)
1942{
1943 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1944 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001945 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946
1947 dev->pdata = pdata;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001948
1949 android_class = class_create(THIS_MODULE, "android_usb");
1950 if (IS_ERR(android_class))
1951 return PTR_ERR(android_class);
1952
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301953 ret = android_create_device(dev);
1954 if (ret) {
1955 pr_err("%s(): android_create_device failed\n", __func__);
1956 goto err_dev;
1957 }
1958
Lena Salmand092f2d2012-03-12 17:27:24 +02001959 ret = usb_composite_probe(&android_usb_driver, android_bind);
1960 if (ret) {
1961 pr_err("%s(): Failed to register android "
1962 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301963 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001964 }
1965
Ofir Cohen94213a72012-05-03 14:26:32 +03001966 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301967 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001968 pm_qos_add_request(&dev->pm_qos_req_dma,
1969 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1970 strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos));
1971
Lena Salmand092f2d2012-03-12 17:27:24 +02001972 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301973err_probe:
1974 android_destroy_device(dev);
1975err_dev:
1976 class_destroy(android_class);
1977 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001978}
1979
1980static int android_remove(struct platform_device *pdev)
1981{
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301982 struct android_dev *dev = _android_dev;
Ofir Cohen94213a72012-05-03 14:26:32 +03001983 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301984
1985 android_destroy_device(dev);
1986 class_destroy(android_class);
Lena Salmand092f2d2012-03-12 17:27:24 +02001987 usb_composite_unregister(&android_usb_driver);
Manu Gautam94dc6142012-05-08 14:35:24 +05301988 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001989 pm_qos_remove_request(&dev->pm_qos_req_dma);
1990
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001991 return 0;
1992}
1993
1994static struct platform_driver android_platform_driver = {
1995 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001996 .probe = android_probe,
1997 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001998};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001999
2000static int __init init(void)
2001{
2002 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302003 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002004
Benoit Goby1e8ce152011-12-12 13:01:23 -08002005 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302006 if (!dev) {
2007 pr_err("%s(): Failed to alloc memory for android_dev\n",
2008 __func__);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002009 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302010 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002011
Benoit Goby80ba14d2012-03-19 18:56:52 -07002012 dev->disable_depth = 1;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002013 dev->functions = supported_functions;
2014 INIT_LIST_HEAD(&dev->enabled_functions);
2015 INIT_WORK(&dev->work, android_work);
2016 mutex_init(&dev->mutex);
2017
Benoit Goby1e8ce152011-12-12 13:01:23 -08002018 _android_dev = dev;
2019
2020 /* Override composite driver functions */
2021 composite_driver.setup = android_setup;
2022 composite_driver.disconnect = android_disconnect;
2023
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302024 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302025 if (ret) {
2026 pr_err("%s(): Failed to register android"
2027 "platform driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302028 kfree(dev);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302029 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002030
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302031 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002032}
2033module_init(init);
2034
2035static void __exit cleanup(void)
2036{
Lena Salmand092f2d2012-03-12 17:27:24 +02002037 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002038 kfree(_android_dev);
2039 _android_dev = NULL;
2040}
2041module_exit(cleanup);