blob: 047df1a6f5e4f61eb22cae48ccaf68f618fe5e05 [file] [log] [blame]
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18/* #define DEBUG */
19/* #define VERBOSE_DEBUG */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/fs.h>
24
25#include <linux/delay.h>
26#include <linux/kernel.h>
27#include <linux/utsname.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050028#include <linux/platform_device.h>
29
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050030#include <linux/usb/ch9.h>
31#include <linux/usb/composite.h>
32#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <linux/usb/android.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050034
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050035#include "gadget_chips.h"
36
37/*
38 * Kbuild is not very cooperative with respect to linking separately
39 * compiled library objects into one module. So for now we won't use
40 * separate compilation ... ensuring init/exit sections work to shrink
41 * the runtime footprint, and giving us at least some parts of what
42 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
43 */
44#include "usbstring.c"
45#include "config.c"
46#include "epautoconf.c"
47#include "composite.c"
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053050#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053051#include "f_rmnet_sdio.c"
52#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053053#include "f_rmnet.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070054#include "f_mass_storage.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053055#include "u_serial.c"
56#include "u_sdio.c"
57#include "u_smd.c"
58#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053059#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080060#include "u_ctrl_hsic.c"
61#include "u_data_hsic.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053062#include "f_serial.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063//#include "f_acm.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070064#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053065#include "f_ccid.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070066#include "f_mtp.c"
67#include "f_accessory.c"
68#define USB_ETH_RNDIS y
69#include "f_rndis.c"
70#include "rndis.c"
71#include "u_ether.c"
72
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050073MODULE_AUTHOR("Mike Lockwood");
74MODULE_DESCRIPTION("Android Composite USB Driver");
75MODULE_LICENSE("GPL");
76MODULE_VERSION("1.0");
77
78static const char longname[] = "Gadget Android";
79
Benoit Gobyaab96812011-04-19 20:37:33 -070080/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050081#define VENDOR_ID 0x18D1
82#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050083
Benoit Gobyaab96812011-04-19 20:37:33 -070084struct android_usb_function {
85 char *name;
86 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050087
Benoit Gobyaab96812011-04-19 20:37:33 -070088 struct device *dev;
89 char *dev_name;
90 struct device_attribute **attributes;
91
92 /* for android_dev.enabled_functions */
93 struct list_head enabled_list;
94
95 /* Optional: initialization during gadget bind */
96 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
97 /* Optional: cleanup during gadget unbind */
98 void (*cleanup)(struct android_usb_function *);
99
100 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
101
102 /* Optional: called when the configuration is removed */
103 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
Mike Lockwood686d33a2011-09-07 09:55:12 -0700104 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -0700105 int (*ctrlrequest)(struct android_usb_function *,
106 struct usb_composite_dev *,
107 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500108};
109
Benoit Gobyaab96812011-04-19 20:37:33 -0700110struct android_dev {
111 struct android_usb_function **functions;
112 struct list_head enabled_functions;
113 struct usb_composite_dev *cdev;
114 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700116
117 bool enabled;
118 bool connected;
119 bool sw_connected;
120 struct work_struct work;
121};
122
123static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500124static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700125static int android_bind_config(struct usb_configuration *c);
126static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500127
128/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500129#define STRING_MANUFACTURER_IDX 0
130#define STRING_PRODUCT_IDX 1
131#define STRING_SERIAL_IDX 2
132
Benoit Gobyaab96812011-04-19 20:37:33 -0700133static char manufacturer_string[256];
134static char product_string[256];
135static char serial_string[256];
136
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500137/* String Table */
138static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700139 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
140 [STRING_PRODUCT_IDX].s = product_string,
141 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500142 { } /* end of list */
143};
144
145static struct usb_gadget_strings stringtab_dev = {
146 .language = 0x0409, /* en-us */
147 .strings = strings_dev,
148};
149
150static struct usb_gadget_strings *dev_strings[] = {
151 &stringtab_dev,
152 NULL,
153};
154
155static struct usb_device_descriptor device_desc = {
156 .bLength = sizeof(device_desc),
157 .bDescriptorType = USB_DT_DEVICE,
158 .bcdUSB = __constant_cpu_to_le16(0x0200),
159 .bDeviceClass = USB_CLASS_PER_INTERFACE,
160 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
161 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
162 .bcdDevice = __constant_cpu_to_le16(0xffff),
163 .bNumConfigurations = 1,
164};
165
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500166static struct usb_configuration android_config_driver = {
167 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700168 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500169 .bConfigurationValue = 1,
170 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
171 .bMaxPower = 0xFA, /* 500ma */
172};
173
Benoit Gobyaab96812011-04-19 20:37:33 -0700174static void android_work(struct work_struct *data)
175{
176 struct android_dev *dev = container_of(data, struct android_dev, work);
177 struct usb_composite_dev *cdev = dev->cdev;
178 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
179 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
180 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700181 char **uevent_envp = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -0700182 unsigned long flags;
183
184 spin_lock_irqsave(&cdev->lock, flags);
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700185 if (cdev->config)
Dima Zavinfc753492011-09-14 11:52:45 -0700186 uevent_envp = configured;
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700187 else if (dev->connected != dev->sw_connected)
188 uevent_envp = dev->connected ? connected : disconnected;
189 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700190 spin_unlock_irqrestore(&cdev->lock, flags);
191
192 if (uevent_envp) {
193 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
194 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700195 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700196 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
197 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700198 }
199}
200
201
202/*-------------------------------------------------------------------------*/
203/* Supported functions initialization */
204
Manu Gautam8e0719b2011-09-26 14:47:55 +0530205/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530206static int rmnet_smd_function_bind_config(struct android_usb_function *f,
207 struct usb_configuration *c)
208{
209 return rmnet_smd_bind_config(c);
210}
211
212static struct android_usb_function rmnet_smd_function = {
213 .name = "rmnet_smd",
214 .bind_config = rmnet_smd_function_bind_config,
215};
216
Manu Gautam8e0719b2011-09-26 14:47:55 +0530217/* RMNET_SDIO */
218static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
219 struct usb_configuration *c)
220{
221 return rmnet_sdio_function_add(c);
222}
223
224static struct android_usb_function rmnet_sdio_function = {
225 .name = "rmnet_sdio",
226 .bind_config = rmnet_sdio_function_bind_config,
227};
228
229/* RMNET_SMD_SDIO */
230static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
231 struct usb_composite_dev *cdev)
232{
233 return rmnet_smd_sdio_init();
234}
235
236static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
237{
238 rmnet_smd_sdio_cleanup();
239}
240
241static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
242 struct usb_configuration *c)
243{
244 return rmnet_smd_sdio_function_add(c);
245}
246
247static struct device_attribute *rmnet_smd_sdio_attributes[] = {
248 &dev_attr_transport, NULL };
249
250static struct android_usb_function rmnet_smd_sdio_function = {
251 .name = "rmnet_smd_sdio",
252 .init = rmnet_smd_sdio_function_init,
253 .cleanup = rmnet_smd_sdio_function_cleanup,
254 .bind_config = rmnet_smd_sdio_bind_config,
255 .attributes = rmnet_smd_sdio_attributes,
256};
257
Hemant Kumar1b820d52011-11-03 15:08:28 -0700258/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
259#define MAX_XPORT_STR_LEN 50
260static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530261
Manu Gautame3e897c2011-09-12 17:18:46 +0530262static void rmnet_function_cleanup(struct android_usb_function *f)
263{
264 frmnet_cleanup();
265}
266
Manu Gautam2b0234a2011-09-07 16:47:52 +0530267static int rmnet_function_bind_config(struct android_usb_function *f,
268 struct usb_configuration *c)
269{
270 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700271 int err = 0;
272 char *ctrl_name;
273 char *data_name;
274 char buf[MAX_XPORT_STR_LEN], *b;
275 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530276
Hemant Kumar1b820d52011-11-03 15:08:28 -0700277 if (!rmnet_initialized) {
278 rmnet_initialized = 1;
279 strlcpy(buf, rmnet_transports, sizeof(buf));
280 b = strim(buf);
281 while (b) {
282 ctrl_name = strsep(&b, ",");
283 data_name = strsep(&b, ",");
284 if (ctrl_name && data_name) {
285 err = frmnet_init_port(ctrl_name, data_name);
286 if (err) {
287 pr_err("rmnet: Cannot open ctrl port:"
288 "'%s' data port:'%s'\n",
289 ctrl_name, data_name);
290 goto out;
291 }
292 ports++;
293 }
294 }
295
296 err = rmnet_gport_setup();
297 if (err) {
298 pr_err("rmnet: Cannot setup transports");
299 goto out;
300 }
301 }
302
303 for (i = 0; i < ports; i++) {
304 err = frmnet_bind_config(c, i);
305 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530306 pr_err("Could not bind rmnet%u config\n", i);
307 break;
308 }
309 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700310out:
311 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530312}
313
Hemant Kumar1b820d52011-11-03 15:08:28 -0700314static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530315 struct device_attribute *attr, char *buf)
316{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700317 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530318}
319
Hemant Kumar1b820d52011-11-03 15:08:28 -0700320static ssize_t rmnet_transports_store(
321 struct device *device, struct device_attribute *attr,
322 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530323{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700324 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530325
Manu Gautam2b0234a2011-09-07 16:47:52 +0530326 return size;
327}
328
Hemant Kumar1b820d52011-11-03 15:08:28 -0700329static struct device_attribute dev_attr_rmnet_transports =
330 __ATTR(transports, S_IRUGO | S_IWUSR,
331 rmnet_transports_show,
332 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530333static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700334 &dev_attr_rmnet_transports,
335 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530336
337static struct android_usb_function rmnet_function = {
338 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530339 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530340 .bind_config = rmnet_function_bind_config,
341 .attributes = rmnet_function_attributes,
342};
343
Manu Gautam8e0719b2011-09-26 14:47:55 +0530344/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530345static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346static ssize_t clients_store(
347 struct device *device, struct device_attribute *attr,
348 const char *buff, size_t size)
349{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530350 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351
352 return size;
353}
354
355static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
356static struct device_attribute *diag_function_attributes[] =
357 { &dev_attr_clients, NULL };
358
359static int diag_function_init(struct android_usb_function *f,
360 struct usb_composite_dev *cdev)
361{
362 return diag_setup();
363}
364
365static void diag_function_cleanup(struct android_usb_function *f)
366{
367 diag_cleanup();
368}
369
370static int diag_function_bind_config(struct android_usb_function *f,
371 struct usb_configuration *c)
372{
373 char *name;
374 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530375 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800376 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530378 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379 b = strim(buf);
380
381 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800382 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530384 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700385 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530386 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530387
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530389 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390 if (err)
391 pr_err("diag: Cannot open channel '%s'", name);
392 }
393 }
394
395 return err;
396}
397
398static struct android_usb_function diag_function = {
399 .name = "diag",
400 .init = diag_function_init,
401 .cleanup = diag_function_cleanup,
402 .bind_config = diag_function_bind_config,
403 .attributes = diag_function_attributes,
404};
405
Manu Gautam8e0719b2011-09-26 14:47:55 +0530406/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530407static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530408static ssize_t serial_transports_store(
409 struct device *device, struct device_attribute *attr,
410 const char *buff, size_t size)
411{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530412 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530413
414 return size;
415}
416
417static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
418static struct device_attribute *serial_function_attributes[] =
419 { &dev_attr_transports, NULL };
420
421static void serial_function_cleanup(struct android_usb_function *f)
422{
423 gserial_cleanup();
424}
425
426static int serial_function_bind_config(struct android_usb_function *f,
427 struct usb_configuration *c)
428{
429 char *name;
430 char buf[32], *b;
431 int err = -1, i;
432 static int serial_initialized = 0, ports = 0;
433
434 if (serial_initialized)
435 goto bind_config;
436
437 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530438 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530439 b = strim(buf);
440
441 while (b) {
442 name = strsep(&b, ",");
443
444 if (name) {
445 err = gserial_init_port(ports, name);
446 if (err) {
447 pr_err("serial: Cannot open port '%s'", name);
448 goto out;
449 }
450 ports++;
451 }
452 }
453 err = gport_setup(c);
454 if (err) {
455 pr_err("serial: Cannot setup transports");
456 goto out;
457 }
458
459bind_config:
460 for (i = 0; i < ports; i++) {
461 err = gser_bind_config(c, i);
462 if (err) {
463 pr_err("serial: bind_config failed for port %d", i);
464 goto out;
465 }
466 }
467
468out:
469 return err;
470}
471
472static struct android_usb_function serial_function = {
473 .name = "serial",
474 .cleanup = serial_function_cleanup,
475 .bind_config = serial_function_bind_config,
476 .attributes = serial_function_attributes,
477};
478
479
Manu Gautam8e0719b2011-09-26 14:47:55 +0530480/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700481static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
482{
483 return adb_setup();
484}
485
486static void adb_function_cleanup(struct android_usb_function *f)
487{
488 adb_cleanup();
489}
490
491static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
492{
493 return adb_bind_config(c);
494}
495
496static struct android_usb_function adb_function = {
497 .name = "adb",
498 .init = adb_function_init,
499 .cleanup = adb_function_cleanup,
500 .bind_config = adb_function_bind_config,
501};
502
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530503/* CCID */
504static int ccid_function_init(struct android_usb_function *f,
505 struct usb_composite_dev *cdev)
506{
507 return ccid_setup();
508}
509
510static void ccid_function_cleanup(struct android_usb_function *f)
511{
512 ccid_cleanup();
513}
514
515static int ccid_function_bind_config(struct android_usb_function *f,
516 struct usb_configuration *c)
517{
518 return ccid_bind_config(c);
519}
520
521static struct android_usb_function ccid_function = {
522 .name = "ccid",
523 .init = ccid_function_init,
524 .cleanup = ccid_function_cleanup,
525 .bind_config = ccid_function_bind_config,
526};
527
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528#if 0
Benoit Gobyaab96812011-04-19 20:37:33 -0700529#define MAX_ACM_INSTANCES 4
530struct acm_function_config {
531 int instances;
532};
533
534static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
535{
536 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
537 if (!f->config)
538 return -ENOMEM;
539
540 return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
541}
542
543static void acm_function_cleanup(struct android_usb_function *f)
544{
545 gserial_cleanup();
546 kfree(f->config);
547 f->config = NULL;
548}
549
550static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530551{
552 int i;
Benoit Gobyaab96812011-04-19 20:37:33 -0700553 int ret = 0;
554 struct acm_function_config *config = f->config;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530555
Benoit Gobyaab96812011-04-19 20:37:33 -0700556 for (i = 0; i < config->instances; i++) {
557 ret = acm_bind_config(c, i);
558 if (ret) {
559 pr_err("Could not bind acm%u config\n", i);
560 break;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530561 }
562 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700563
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530564 return ret;
565}
566
Benoit Gobyaab96812011-04-19 20:37:33 -0700567static ssize_t acm_instances_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530569{
Benoit Gobyaab96812011-04-19 20:37:33 -0700570 struct android_usb_function *f = dev_get_drvdata(dev);
571 struct acm_function_config *config = f->config;
572 return sprintf(buf, "%d\n", config->instances);
573}
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530574
Benoit Gobyaab96812011-04-19 20:37:33 -0700575static ssize_t acm_instances_store(struct device *dev,
576 struct device_attribute *attr, const char *buf, size_t size)
577{
578 struct android_usb_function *f = dev_get_drvdata(dev);
579 struct acm_function_config *config = f->config;
580 int value;
581
582 sscanf(buf, "%d", &value);
583 if (value > MAX_ACM_INSTANCES)
584 value = MAX_ACM_INSTANCES;
585 config->instances = value;
586 return size;
587}
588
589static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
590static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
591
592static struct android_usb_function acm_function = {
593 .name = "acm",
594 .init = acm_function_init,
595 .cleanup = acm_function_cleanup,
596 .bind_config = acm_function_bind_config,
597 .attributes = acm_function_attributes,
598};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599#endif
Benoit Gobyaab96812011-04-19 20:37:33 -0700600
601static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
602{
603 return mtp_setup();
604}
605
606static void mtp_function_cleanup(struct android_usb_function *f)
607{
608 mtp_cleanup();
609}
610
611static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
612{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400613 return mtp_bind_config(c, false);
614}
615
616static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
617{
618 /* nothing to do - initialization is handled by mtp_function_init */
619 return 0;
620}
621
622static void ptp_function_cleanup(struct android_usb_function *f)
623{
624 /* nothing to do - cleanup is handled by mtp_function_cleanup */
625}
626
627static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
628{
629 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700630}
631
632static int mtp_function_ctrlrequest(struct android_usb_function *f,
633 struct usb_composite_dev *cdev,
634 const struct usb_ctrlrequest *c)
635{
636 return mtp_ctrlrequest(cdev, c);
637}
638
639static struct android_usb_function mtp_function = {
640 .name = "mtp",
641 .init = mtp_function_init,
642 .cleanup = mtp_function_cleanup,
643 .bind_config = mtp_function_bind_config,
644 .ctrlrequest = mtp_function_ctrlrequest,
645};
646
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400647/* PTP function is same as MTP with slightly different interface descriptor */
648static struct android_usb_function ptp_function = {
649 .name = "ptp",
650 .init = ptp_function_init,
651 .cleanup = ptp_function_cleanup,
652 .bind_config = ptp_function_bind_config,
653};
654
Benoit Gobyaab96812011-04-19 20:37:33 -0700655
656struct rndis_function_config {
657 u8 ethaddr[ETH_ALEN];
658 u32 vendorID;
659 char manufacturer[256];
660 bool wceis;
661};
662
663static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
664{
665 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
666 if (!f->config)
667 return -ENOMEM;
668 return 0;
669}
670
671static void rndis_function_cleanup(struct android_usb_function *f)
672{
673 kfree(f->config);
674 f->config = NULL;
675}
676
677static int rndis_function_bind_config(struct android_usb_function *f,
678 struct usb_configuration *c)
679{
Manu Gautamf4741132011-11-25 09:08:53 +0530680 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700681 struct rndis_function_config *rndis = f->config;
682
683 if (!rndis) {
684 pr_err("%s: rndis_pdata\n", __func__);
685 return -1;
686 }
687
688 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
689 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
690 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
691
Manu Gautamf4741132011-11-25 09:08:53 +0530692 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
693 if (ret) {
694 pr_err("%s: gether_setup failed\n", __func__);
695 return ret;
696 }
697
Benoit Gobyaab96812011-04-19 20:37:33 -0700698 if (rndis->wceis) {
699 /* "Wireless" RNDIS; auto-detected by Windows */
700 rndis_iad_descriptor.bFunctionClass =
701 USB_CLASS_WIRELESS_CONTROLLER;
702 rndis_iad_descriptor.bFunctionSubClass = 0x01;
703 rndis_iad_descriptor.bFunctionProtocol = 0x03;
704 rndis_control_intf.bInterfaceClass =
705 USB_CLASS_WIRELESS_CONTROLLER;
706 rndis_control_intf.bInterfaceSubClass = 0x01;
707 rndis_control_intf.bInterfaceProtocol = 0x03;
708 }
709
710 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
711 rndis->manufacturer);
712}
713
714static void rndis_function_unbind_config(struct android_usb_function *f,
715 struct usb_configuration *c)
716{
Manu Gautamf4741132011-11-25 09:08:53 +0530717 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700718}
719
720static ssize_t rndis_manufacturer_show(struct device *dev,
721 struct device_attribute *attr, char *buf)
722{
723 struct android_usb_function *f = dev_get_drvdata(dev);
724 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530725 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700726}
727
728static ssize_t rndis_manufacturer_store(struct device *dev,
729 struct device_attribute *attr, const char *buf, size_t size)
730{
731 struct android_usb_function *f = dev_get_drvdata(dev);
732 struct rndis_function_config *config = f->config;
733
734 if (size >= sizeof(config->manufacturer))
735 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530736 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700737 return size;
738 return -1;
739}
740
741static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
742 rndis_manufacturer_store);
743
744static ssize_t rndis_wceis_show(struct device *dev,
745 struct device_attribute *attr, char *buf)
746{
747 struct android_usb_function *f = dev_get_drvdata(dev);
748 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530749 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700750}
751
752static ssize_t rndis_wceis_store(struct device *dev,
753 struct device_attribute *attr, const char *buf, size_t size)
754{
755 struct android_usb_function *f = dev_get_drvdata(dev);
756 struct rndis_function_config *config = f->config;
757 int value;
758
759 if (sscanf(buf, "%d", &value) == 1) {
760 config->wceis = value;
761 return size;
762 }
763 return -EINVAL;
764}
765
766static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
767 rndis_wceis_store);
768
769static ssize_t rndis_ethaddr_show(struct device *dev,
770 struct device_attribute *attr, char *buf)
771{
772 struct android_usb_function *f = dev_get_drvdata(dev);
773 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530774 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700775 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
776 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
777}
778
779static ssize_t rndis_ethaddr_store(struct device *dev,
780 struct device_attribute *attr, const char *buf, size_t size)
781{
782 struct android_usb_function *f = dev_get_drvdata(dev);
783 struct rndis_function_config *rndis = f->config;
784
785 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
786 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
787 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
788 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
789 return size;
790 return -EINVAL;
791}
792
793static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
794 rndis_ethaddr_store);
795
796static ssize_t rndis_vendorID_show(struct device *dev,
797 struct device_attribute *attr, char *buf)
798{
799 struct android_usb_function *f = dev_get_drvdata(dev);
800 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530801 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700802}
803
804static ssize_t rndis_vendorID_store(struct device *dev,
805 struct device_attribute *attr, const char *buf, size_t size)
806{
807 struct android_usb_function *f = dev_get_drvdata(dev);
808 struct rndis_function_config *config = f->config;
809 int value;
810
811 if (sscanf(buf, "%04x", &value) == 1) {
812 config->vendorID = value;
813 return size;
814 }
815 return -EINVAL;
816}
817
818static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
819 rndis_vendorID_store);
820
821static struct device_attribute *rndis_function_attributes[] = {
822 &dev_attr_manufacturer,
823 &dev_attr_wceis,
824 &dev_attr_ethaddr,
825 &dev_attr_vendorID,
826 NULL
827};
828
829static struct android_usb_function rndis_function = {
830 .name = "rndis",
831 .init = rndis_function_init,
832 .cleanup = rndis_function_cleanup,
833 .bind_config = rndis_function_bind_config,
834 .unbind_config = rndis_function_unbind_config,
835 .attributes = rndis_function_attributes,
836};
837
838
839struct mass_storage_function_config {
840 struct fsg_config fsg;
841 struct fsg_common *common;
842};
843
844static int mass_storage_function_init(struct android_usb_function *f,
845 struct usb_composite_dev *cdev)
846{
847 struct mass_storage_function_config *config;
848 struct fsg_common *common;
849 int err;
850
851 config = kzalloc(sizeof(struct mass_storage_function_config),
852 GFP_KERNEL);
853 if (!config)
854 return -ENOMEM;
855
856 config->fsg.nluns = 1;
857 config->fsg.luns[0].removable = 1;
858
859 common = fsg_common_init(NULL, cdev, &config->fsg);
860 if (IS_ERR(common)) {
861 kfree(config);
862 return PTR_ERR(common);
863 }
864
865 err = sysfs_create_link(&f->dev->kobj,
866 &common->luns[0].dev.kobj,
867 "lun");
868 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530869 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700870 kfree(config);
871 return err;
872 }
873
874 config->common = common;
875 f->config = config;
876 return 0;
877}
878
879static void mass_storage_function_cleanup(struct android_usb_function *f)
880{
881 kfree(f->config);
882 f->config = NULL;
883}
884
885static int mass_storage_function_bind_config(struct android_usb_function *f,
886 struct usb_configuration *c)
887{
888 struct mass_storage_function_config *config = f->config;
889 return fsg_bind_config(c->cdev, c, config->common);
890}
891
892static ssize_t mass_storage_inquiry_show(struct device *dev,
893 struct device_attribute *attr, char *buf)
894{
895 struct android_usb_function *f = dev_get_drvdata(dev);
896 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530897 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -0700898}
899
900static ssize_t mass_storage_inquiry_store(struct device *dev,
901 struct device_attribute *attr, const char *buf, size_t size)
902{
903 struct android_usb_function *f = dev_get_drvdata(dev);
904 struct mass_storage_function_config *config = f->config;
905 if (size >= sizeof(config->common->inquiry_string))
906 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530907 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700908 return -EINVAL;
909 return size;
910}
911
912static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
913 mass_storage_inquiry_show,
914 mass_storage_inquiry_store);
915
916static struct device_attribute *mass_storage_function_attributes[] = {
917 &dev_attr_inquiry_string,
918 NULL
919};
920
921static struct android_usb_function mass_storage_function = {
922 .name = "mass_storage",
923 .init = mass_storage_function_init,
924 .cleanup = mass_storage_function_cleanup,
925 .bind_config = mass_storage_function_bind_config,
926 .attributes = mass_storage_function_attributes,
927};
928
929
930static int accessory_function_init(struct android_usb_function *f,
931 struct usb_composite_dev *cdev)
932{
933 return acc_setup();
934}
935
936static void accessory_function_cleanup(struct android_usb_function *f)
937{
938 acc_cleanup();
939}
940
941static int accessory_function_bind_config(struct android_usb_function *f,
942 struct usb_configuration *c)
943{
944 return acc_bind_config(c);
945}
946
947static int accessory_function_ctrlrequest(struct android_usb_function *f,
948 struct usb_composite_dev *cdev,
949 const struct usb_ctrlrequest *c)
950{
951 return acc_ctrlrequest(cdev, c);
952}
953
954static struct android_usb_function accessory_function = {
955 .name = "accessory",
956 .init = accessory_function_init,
957 .cleanup = accessory_function_cleanup,
958 .bind_config = accessory_function_bind_config,
959 .ctrlrequest = accessory_function_ctrlrequest,
960};
961
962
963static struct android_usb_function *supported_functions[] = {
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530964 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +0530965 &rmnet_sdio_function,
966 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530967 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +0530969 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700970 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530971 &ccid_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972// &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700973 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400974 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700975 &rndis_function,
976 &mass_storage_function,
977 &accessory_function,
978 NULL
979};
980
981
982static int android_init_functions(struct android_usb_function **functions,
983 struct usb_composite_dev *cdev)
984{
985 struct android_dev *dev = _android_dev;
986 struct android_usb_function *f;
987 struct device_attribute **attrs;
988 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530989 int err = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700990 int index = 0;
991
992 for (; (f = *functions++); index++) {
993 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
994 f->dev = device_create(android_class, dev->dev,
995 MKDEV(0, index), f, f->dev_name);
996 if (IS_ERR(f->dev)) {
997 pr_err("%s: Failed to create dev %s", __func__,
998 f->dev_name);
999 err = PTR_ERR(f->dev);
1000 goto err_create;
1001 }
1002
1003 if (f->init) {
1004 err = f->init(f, cdev);
1005 if (err) {
1006 pr_err("%s: Failed to init %s", __func__,
1007 f->name);
1008 goto err_out;
1009 }
1010 }
1011
1012 attrs = f->attributes;
1013 if (attrs) {
1014 while ((attr = *attrs++) && !err)
1015 err = device_create_file(f->dev, attr);
1016 }
1017 if (err) {
1018 pr_err("%s: Failed to create function %s attributes",
1019 __func__, f->name);
1020 goto err_out;
1021 }
1022 }
1023 return 0;
1024
1025err_out:
1026 device_destroy(android_class, f->dev->devt);
1027err_create:
1028 kfree(f->dev_name);
1029 return err;
1030}
1031
1032static void android_cleanup_functions(struct android_usb_function **functions)
1033{
1034 struct android_usb_function *f;
1035
1036 while (*functions) {
1037 f = *functions++;
1038
1039 if (f->dev) {
1040 device_destroy(android_class, f->dev->devt);
1041 kfree(f->dev_name);
1042 }
1043
1044 if (f->cleanup)
1045 f->cleanup(f);
1046 }
1047}
1048
1049static int
1050android_bind_enabled_functions(struct android_dev *dev,
1051 struct usb_configuration *c)
1052{
1053 struct android_usb_function *f;
1054 int ret;
1055
1056 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1057 ret = f->bind_config(f, c);
1058 if (ret) {
1059 pr_err("%s: %s failed", __func__, f->name);
1060 return ret;
1061 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301062 }
1063 return 0;
1064}
1065
Benoit Gobyaab96812011-04-19 20:37:33 -07001066static void
1067android_unbind_enabled_functions(struct android_dev *dev,
1068 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301069{
Benoit Gobyaab96812011-04-19 20:37:33 -07001070 struct android_usb_function *f;
1071
1072 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1073 if (f->unbind_config)
1074 f->unbind_config(f, c);
1075 }
1076}
1077
1078static int android_enable_function(struct android_dev *dev, char *name)
1079{
1080 struct android_usb_function **functions = dev->functions;
1081 struct android_usb_function *f;
1082 while ((f = *functions++)) {
1083 if (!strcmp(name, f->name)) {
1084 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301085 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001086 }
1087 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001088 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001089}
1090
Benoit Gobyaab96812011-04-19 20:37:33 -07001091/*-------------------------------------------------------------------------*/
1092/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301093
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301094static ssize_t remote_wakeup_show(struct device *pdev,
1095 struct device_attribute *attr, char *buf)
1096{
1097 return snprintf(buf, PAGE_SIZE, "%d\n",
1098 !!(android_config_driver.bmAttributes &
1099 USB_CONFIG_ATT_WAKEUP));
1100}
1101
1102static ssize_t remote_wakeup_store(struct device *pdev,
1103 struct device_attribute *attr, const char *buff, size_t size)
1104{
1105 int enable = 0;
1106
1107 sscanf(buff, "%d", &enable);
1108
1109 pr_debug("android_usb: %s remote wakeup\n",
1110 enable ? "enabling" : "disabling");
1111
1112 if (enable)
1113 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1114 else
1115 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1116
1117 return size;
1118}
1119
Benoit Gobyaab96812011-04-19 20:37:33 -07001120static ssize_t
1121functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1122{
1123 struct android_dev *dev = dev_get_drvdata(pdev);
1124 struct android_usb_function *f;
1125 char *buff = buf;
1126
1127 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301128 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobyaab96812011-04-19 20:37:33 -07001129 if (buff != buf)
1130 *(buff-1) = '\n';
1131 return buff - buf;
1132}
1133
1134static ssize_t
1135functions_store(struct device *pdev, struct device_attribute *attr,
1136 const char *buff, size_t size)
1137{
1138 struct android_dev *dev = dev_get_drvdata(pdev);
1139 char *name;
1140 char buf[256], *b;
1141 int err;
1142
1143 INIT_LIST_HEAD(&dev->enabled_functions);
1144
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301145 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001146 b = strim(buf);
1147
1148 while (b) {
1149 name = strsep(&b, ",");
1150 if (name) {
1151 err = android_enable_function(dev, name);
1152 if (err)
1153 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301154 }
1155 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001156
1157 return size;
1158}
1159
1160static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1161 char *buf)
1162{
1163 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301164 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001165}
1166
1167static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1168 const char *buff, size_t size)
1169{
1170 struct android_dev *dev = dev_get_drvdata(pdev);
1171 struct usb_composite_dev *cdev = dev->cdev;
1172 int enabled = 0;
1173
1174 sscanf(buff, "%d", &enabled);
1175 if (enabled && !dev->enabled) {
1176 /* update values in composite driver's copy of device descriptor */
1177 cdev->desc.idVendor = device_desc.idVendor;
1178 cdev->desc.idProduct = device_desc.idProduct;
1179 cdev->desc.bcdDevice = device_desc.bcdDevice;
1180 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1181 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1182 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301183 if (usb_add_config(cdev, &android_config_driver,
1184 android_bind_config))
1185 return size;
1186
Benoit Gobyaab96812011-04-19 20:37:33 -07001187 usb_gadget_connect(cdev->gadget);
1188 dev->enabled = true;
1189 } else if (!enabled && dev->enabled) {
1190 usb_gadget_disconnect(cdev->gadget);
1191 usb_remove_config(cdev, &android_config_driver);
1192 dev->enabled = false;
1193 } else {
1194 pr_err("android_usb: already %s\n",
1195 dev->enabled ? "enabled" : "disabled");
1196 }
1197 return size;
1198}
1199
1200static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1201 char *buf)
1202{
1203 struct android_dev *dev = dev_get_drvdata(pdev);
1204 struct usb_composite_dev *cdev = dev->cdev;
1205 char *state = "DISCONNECTED";
1206 unsigned long flags;
1207
1208 if (!cdev)
1209 goto out;
1210
1211 spin_lock_irqsave(&cdev->lock, flags);
1212 if (cdev->config)
1213 state = "CONFIGURED";
1214 else if (dev->connected)
1215 state = "CONNECTED";
1216 spin_unlock_irqrestore(&cdev->lock, flags);
1217out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301218 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001219}
1220
1221#define DESCRIPTOR_ATTR(field, format_string) \
1222static ssize_t \
1223field ## _show(struct device *dev, struct device_attribute *attr, \
1224 char *buf) \
1225{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301226 return snprintf(buf, PAGE_SIZE, \
1227 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001228} \
1229static ssize_t \
1230field ## _store(struct device *dev, struct device_attribute *attr, \
1231 const char *buf, size_t size) \
1232{ \
1233 int value; \
1234 if (sscanf(buf, format_string, &value) == 1) { \
1235 device_desc.field = value; \
1236 return size; \
1237 } \
1238 return -1; \
1239} \
1240static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1241
1242#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1243static ssize_t \
1244field ## _show(struct device *dev, struct device_attribute *attr, \
1245 char *buf) \
1246{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301247 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001248} \
1249static ssize_t \
1250field ## _store(struct device *dev, struct device_attribute *attr, \
1251 const char *buf, size_t size) \
1252{ \
1253 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301254 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001255 return size; \
1256 } \
1257 return -1; \
1258} \
1259static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1260
1261
1262DESCRIPTOR_ATTR(idVendor, "%04x\n")
1263DESCRIPTOR_ATTR(idProduct, "%04x\n")
1264DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1265DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1266DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1267DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1268DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1269DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1270DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1271
1272static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1273static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1274static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301275static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1276 remote_wakeup_show, remote_wakeup_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001277
1278static struct device_attribute *android_usb_attributes[] = {
1279 &dev_attr_idVendor,
1280 &dev_attr_idProduct,
1281 &dev_attr_bcdDevice,
1282 &dev_attr_bDeviceClass,
1283 &dev_attr_bDeviceSubClass,
1284 &dev_attr_bDeviceProtocol,
1285 &dev_attr_iManufacturer,
1286 &dev_attr_iProduct,
1287 &dev_attr_iSerial,
1288 &dev_attr_functions,
1289 &dev_attr_enable,
1290 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301291 &dev_attr_remote_wakeup,
Benoit Gobyaab96812011-04-19 20:37:33 -07001292 NULL
1293};
1294
1295/*-------------------------------------------------------------------------*/
1296/* Composite driver */
1297
1298static int android_bind_config(struct usb_configuration *c)
1299{
1300 struct android_dev *dev = _android_dev;
1301 int ret = 0;
1302
1303 ret = android_bind_enabled_functions(dev, c);
1304 if (ret)
1305 return ret;
1306
1307 return 0;
1308}
1309
1310static void android_unbind_config(struct usb_configuration *c)
1311{
1312 struct android_dev *dev = _android_dev;
1313
1314 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301315}
1316
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001317static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001318{
1319 struct android_dev *dev = _android_dev;
1320 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001321 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001322
Benoit Gobyaab96812011-04-19 20:37:33 -07001323 usb_gadget_disconnect(gadget);
1324
1325 ret = android_init_functions(dev->functions, cdev);
1326 if (ret)
1327 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001328
1329 /* Allocate string descriptor numbers ... note that string
1330 * contents can be overridden by the composite_dev glue.
1331 */
1332 id = usb_string_id(cdev);
1333 if (id < 0)
1334 return id;
1335 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1336 device_desc.iManufacturer = id;
1337
1338 id = usb_string_id(cdev);
1339 if (id < 0)
1340 return id;
1341 strings_dev[STRING_PRODUCT_IDX].id = id;
1342 device_desc.iProduct = id;
1343
Benoit Gobyaab96812011-04-19 20:37:33 -07001344 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301345 strlcpy(manufacturer_string, "Android",
1346 sizeof(manufacturer_string) - 1);
1347 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1348 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001349
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001350 id = usb_string_id(cdev);
1351 if (id < 0)
1352 return id;
1353 strings_dev[STRING_SERIAL_IDX].id = id;
1354 device_desc.iSerialNumber = id;
1355
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001356 gcnum = usb_gadget_controller_number(gadget);
1357 if (gcnum >= 0)
1358 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1359 else {
1360 /* gadget zero is so simple (for now, no altsettings) that
1361 * it SHOULD NOT have problems with bulk-capable hardware.
1362 * so just warn about unrcognized controllers -- don't panic.
1363 *
1364 * things like configuration and altsetting numbering
1365 * can need hardware-specific attention though.
1366 */
1367 pr_warning("%s: controller '%s' not recognized\n",
1368 longname, gadget->name);
1369 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1370 }
1371
1372 usb_gadget_set_selfpowered(gadget);
1373 dev->cdev = cdev;
1374
1375 return 0;
1376}
1377
Benoit Gobyaab96812011-04-19 20:37:33 -07001378static int android_usb_unbind(struct usb_composite_dev *cdev)
1379{
1380 struct android_dev *dev = _android_dev;
1381
1382 cancel_work_sync(&dev->work);
1383 android_cleanup_functions(dev->functions);
1384 return 0;
1385}
1386
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001387static struct usb_composite_driver android_usb_driver = {
1388 .name = "android_usb",
1389 .dev = &device_desc,
1390 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001391 .unbind = android_usb_unbind,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001392};
1393
Benoit Gobyaab96812011-04-19 20:37:33 -07001394static int
1395android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1396{
1397 struct android_dev *dev = _android_dev;
1398 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1399 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001400 struct android_usb_function *f;
1401 int value = -EOPNOTSUPP;
1402 unsigned long flags;
1403
1404 req->zero = 0;
1405 req->complete = composite_setup_complete;
1406 req->length = 0;
1407 gadget->ep0->driver_data = cdev;
1408
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001409 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001410 if (f->ctrlrequest) {
1411 value = f->ctrlrequest(f, cdev, c);
1412 if (value >= 0)
1413 break;
1414 }
1415 }
1416
Mike Lockwood686d33a2011-09-07 09:55:12 -07001417 /* Special case the accessory function.
1418 * It needs to handle control requests before it is enabled.
1419 */
1420 if (value < 0)
1421 value = acc_ctrlrequest(cdev, c);
1422
Benoit Gobyaab96812011-04-19 20:37:33 -07001423 if (value < 0)
1424 value = composite_setup(gadget, c);
1425
1426 spin_lock_irqsave(&cdev->lock, flags);
1427 if (!dev->connected) {
1428 dev->connected = 1;
1429 schedule_work(&dev->work);
1430 }
1431 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1432 schedule_work(&dev->work);
1433 }
1434 spin_unlock_irqrestore(&cdev->lock, flags);
1435
1436 return value;
1437}
1438
1439static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001440{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301441 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001442 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1443 unsigned long flags;
1444
1445 composite_disconnect(gadget);
1446
1447 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001448 dev->connected = 0;
1449 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001450 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301451}
1452
Benoit Gobyaab96812011-04-19 20:37:33 -07001453static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001454{
Benoit Gobyaab96812011-04-19 20:37:33 -07001455 struct device_attribute **attrs = android_usb_attributes;
1456 struct device_attribute *attr;
1457 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001458
Benoit Gobyaab96812011-04-19 20:37:33 -07001459 dev->dev = device_create(android_class, NULL,
1460 MKDEV(0, 0), NULL, "android0");
1461 if (IS_ERR(dev->dev))
1462 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001463
Benoit Gobyaab96812011-04-19 20:37:33 -07001464 dev_set_drvdata(dev->dev, dev);
1465
1466 while ((attr = *attrs++)) {
1467 err = device_create_file(dev->dev, attr);
1468 if (err) {
1469 device_destroy(android_class, dev->dev->devt);
1470 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001471 }
1472 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001473 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001474}
1475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001476static int __devinit android_probe(struct platform_device *pdev)
1477{
1478 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1479 struct android_dev *dev = _android_dev;
1480
1481 dev->pdata = pdata;
1482
1483 return 0;
1484}
1485
1486static struct platform_driver android_platform_driver = {
1487 .driver = { .name = "android_usb"},
1488};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001489
1490static int __init init(void)
1491{
1492 struct android_dev *dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001493 int err;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001494
Benoit Gobyaab96812011-04-19 20:37:33 -07001495 android_class = class_create(THIS_MODULE, "android_usb");
1496 if (IS_ERR(android_class))
1497 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001498
1499 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1500 if (!dev)
1501 return -ENOMEM;
1502
Benoit Gobyaab96812011-04-19 20:37:33 -07001503 dev->functions = supported_functions;
1504 INIT_LIST_HEAD(&dev->enabled_functions);
1505 INIT_WORK(&dev->work, android_work);
1506
1507 err = android_create_device(dev);
1508 if (err) {
1509 class_destroy(android_class);
1510 kfree(dev);
1511 return err;
1512 }
1513
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001514 _android_dev = dev;
1515
Benoit Gobyaab96812011-04-19 20:37:33 -07001516 /* Override composite driver functions */
1517 composite_driver.setup = android_setup;
1518 composite_driver.disconnect = android_disconnect;
1519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520 platform_driver_probe(&android_platform_driver, android_probe);
1521
Benoit Gobyaab96812011-04-19 20:37:33 -07001522 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001523}
1524module_init(init);
1525
1526static void __exit cleanup(void)
1527{
1528 usb_composite_unregister(&android_usb_driver);
Benoit Gobyaab96812011-04-19 20:37:33 -07001529 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001530 kfree(_android_dev);
1531 _android_dev = NULL;
1532}
1533module_exit(cleanup);