blob: 1e3beac567a0f7cd8efa3e82ff54f49805d9cabd [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"
65#include "f_mtp.c"
66#include "f_accessory.c"
67#define USB_ETH_RNDIS y
68#include "f_rndis.c"
69#include "rndis.c"
70#include "u_ether.c"
71
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050072MODULE_AUTHOR("Mike Lockwood");
73MODULE_DESCRIPTION("Android Composite USB Driver");
74MODULE_LICENSE("GPL");
75MODULE_VERSION("1.0");
76
77static const char longname[] = "Gadget Android";
78
Benoit Gobyaab96812011-04-19 20:37:33 -070079/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050080#define VENDOR_ID 0x18D1
81#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050082
Benoit Gobyaab96812011-04-19 20:37:33 -070083struct android_usb_function {
84 char *name;
85 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050086
Benoit Gobyaab96812011-04-19 20:37:33 -070087 struct device *dev;
88 char *dev_name;
89 struct device_attribute **attributes;
90
91 /* for android_dev.enabled_functions */
92 struct list_head enabled_list;
93
94 /* Optional: initialization during gadget bind */
95 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
96 /* Optional: cleanup during gadget unbind */
97 void (*cleanup)(struct android_usb_function *);
98
99 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
100
101 /* Optional: called when the configuration is removed */
102 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
Mike Lockwood686d33a2011-09-07 09:55:12 -0700103 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -0700104 int (*ctrlrequest)(struct android_usb_function *,
105 struct usb_composite_dev *,
106 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500107};
108
Benoit Gobyaab96812011-04-19 20:37:33 -0700109struct android_dev {
110 struct android_usb_function **functions;
111 struct list_head enabled_functions;
112 struct usb_composite_dev *cdev;
113 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700115
116 bool enabled;
117 bool connected;
118 bool sw_connected;
119 struct work_struct work;
120};
121
122static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500123static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700124static int android_bind_config(struct usb_configuration *c);
125static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500126
127/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500128#define STRING_MANUFACTURER_IDX 0
129#define STRING_PRODUCT_IDX 1
130#define STRING_SERIAL_IDX 2
131
Benoit Gobyaab96812011-04-19 20:37:33 -0700132static char manufacturer_string[256];
133static char product_string[256];
134static char serial_string[256];
135
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500136/* String Table */
137static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700138 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
139 [STRING_PRODUCT_IDX].s = product_string,
140 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500141 { } /* end of list */
142};
143
144static struct usb_gadget_strings stringtab_dev = {
145 .language = 0x0409, /* en-us */
146 .strings = strings_dev,
147};
148
149static struct usb_gadget_strings *dev_strings[] = {
150 &stringtab_dev,
151 NULL,
152};
153
154static struct usb_device_descriptor device_desc = {
155 .bLength = sizeof(device_desc),
156 .bDescriptorType = USB_DT_DEVICE,
157 .bcdUSB = __constant_cpu_to_le16(0x0200),
158 .bDeviceClass = USB_CLASS_PER_INTERFACE,
159 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
160 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
161 .bcdDevice = __constant_cpu_to_le16(0xffff),
162 .bNumConfigurations = 1,
163};
164
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500165static struct usb_configuration android_config_driver = {
166 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700167 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500168 .bConfigurationValue = 1,
169 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
170 .bMaxPower = 0xFA, /* 500ma */
171};
172
Benoit Gobyaab96812011-04-19 20:37:33 -0700173static void android_work(struct work_struct *data)
174{
175 struct android_dev *dev = container_of(data, struct android_dev, work);
176 struct usb_composite_dev *cdev = dev->cdev;
177 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
178 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
179 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700180 char **uevent_envp = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -0700181 unsigned long flags;
182
183 spin_lock_irqsave(&cdev->lock, flags);
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700184 if (cdev->config)
Dima Zavinfc753492011-09-14 11:52:45 -0700185 uevent_envp = configured;
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700186 else if (dev->connected != dev->sw_connected)
187 uevent_envp = dev->connected ? connected : disconnected;
188 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700189 spin_unlock_irqrestore(&cdev->lock, flags);
190
191 if (uevent_envp) {
192 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
193 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700194 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700195 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
196 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700197 }
198}
199
200
201/*-------------------------------------------------------------------------*/
202/* Supported functions initialization */
203
Manu Gautam8e0719b2011-09-26 14:47:55 +0530204/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530205static int rmnet_smd_function_bind_config(struct android_usb_function *f,
206 struct usb_configuration *c)
207{
208 return rmnet_smd_bind_config(c);
209}
210
211static struct android_usb_function rmnet_smd_function = {
212 .name = "rmnet_smd",
213 .bind_config = rmnet_smd_function_bind_config,
214};
215
Manu Gautam8e0719b2011-09-26 14:47:55 +0530216/* RMNET_SDIO */
217static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
218 struct usb_configuration *c)
219{
220 return rmnet_sdio_function_add(c);
221}
222
223static struct android_usb_function rmnet_sdio_function = {
224 .name = "rmnet_sdio",
225 .bind_config = rmnet_sdio_function_bind_config,
226};
227
228/* RMNET_SMD_SDIO */
229static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
230 struct usb_composite_dev *cdev)
231{
232 return rmnet_smd_sdio_init();
233}
234
235static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
236{
237 rmnet_smd_sdio_cleanup();
238}
239
240static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
241 struct usb_configuration *c)
242{
243 return rmnet_smd_sdio_function_add(c);
244}
245
246static struct device_attribute *rmnet_smd_sdio_attributes[] = {
247 &dev_attr_transport, NULL };
248
249static struct android_usb_function rmnet_smd_sdio_function = {
250 .name = "rmnet_smd_sdio",
251 .init = rmnet_smd_sdio_function_init,
252 .cleanup = rmnet_smd_sdio_function_cleanup,
253 .bind_config = rmnet_smd_sdio_bind_config,
254 .attributes = rmnet_smd_sdio_attributes,
255};
256
Hemant Kumar1b820d52011-11-03 15:08:28 -0700257/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
258#define MAX_XPORT_STR_LEN 50
259static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530260
Manu Gautame3e897c2011-09-12 17:18:46 +0530261static void rmnet_function_cleanup(struct android_usb_function *f)
262{
263 frmnet_cleanup();
264}
265
Manu Gautam2b0234a2011-09-07 16:47:52 +0530266static int rmnet_function_bind_config(struct android_usb_function *f,
267 struct usb_configuration *c)
268{
269 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700270 int err = 0;
271 char *ctrl_name;
272 char *data_name;
273 char buf[MAX_XPORT_STR_LEN], *b;
274 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530275
Hemant Kumar1b820d52011-11-03 15:08:28 -0700276 if (!rmnet_initialized) {
277 rmnet_initialized = 1;
278 strlcpy(buf, rmnet_transports, sizeof(buf));
279 b = strim(buf);
280 while (b) {
281 ctrl_name = strsep(&b, ",");
282 data_name = strsep(&b, ",");
283 if (ctrl_name && data_name) {
284 err = frmnet_init_port(ctrl_name, data_name);
285 if (err) {
286 pr_err("rmnet: Cannot open ctrl port:"
287 "'%s' data port:'%s'\n",
288 ctrl_name, data_name);
289 goto out;
290 }
291 ports++;
292 }
293 }
294
295 err = rmnet_gport_setup();
296 if (err) {
297 pr_err("rmnet: Cannot setup transports");
298 goto out;
299 }
300 }
301
302 for (i = 0; i < ports; i++) {
303 err = frmnet_bind_config(c, i);
304 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530305 pr_err("Could not bind rmnet%u config\n", i);
306 break;
307 }
308 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700309out:
310 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530311}
312
Hemant Kumar1b820d52011-11-03 15:08:28 -0700313static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530314 struct device_attribute *attr, char *buf)
315{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700316 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530317}
318
Hemant Kumar1b820d52011-11-03 15:08:28 -0700319static ssize_t rmnet_transports_store(
320 struct device *device, struct device_attribute *attr,
321 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530322{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700323 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530324
Manu Gautam2b0234a2011-09-07 16:47:52 +0530325 return size;
326}
327
Hemant Kumar1b820d52011-11-03 15:08:28 -0700328static struct device_attribute dev_attr_rmnet_transports =
329 __ATTR(transports, S_IRUGO | S_IWUSR,
330 rmnet_transports_show,
331 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530332static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700333 &dev_attr_rmnet_transports,
334 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530335
336static struct android_usb_function rmnet_function = {
337 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530338 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530339 .bind_config = rmnet_function_bind_config,
340 .attributes = rmnet_function_attributes,
341};
342
Manu Gautam8e0719b2011-09-26 14:47:55 +0530343/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530344static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345static ssize_t clients_store(
346 struct device *device, struct device_attribute *attr,
347 const char *buff, size_t size)
348{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530349 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350
351 return size;
352}
353
354static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
355static struct device_attribute *diag_function_attributes[] =
356 { &dev_attr_clients, NULL };
357
358static int diag_function_init(struct android_usb_function *f,
359 struct usb_composite_dev *cdev)
360{
361 return diag_setup();
362}
363
364static void diag_function_cleanup(struct android_usb_function *f)
365{
366 diag_cleanup();
367}
368
369static int diag_function_bind_config(struct android_usb_function *f,
370 struct usb_configuration *c)
371{
372 char *name;
373 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530374 int once = 0, err = -1;
Hemant Kumar1136c002011-10-18 22:04:06 -0700375 int (*notify)(uint32_t, const char *) = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530377 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 b = strim(buf);
379
380 while (b) {
381 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530382 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700383 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530384 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530385
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530387 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 if (err)
389 pr_err("diag: Cannot open channel '%s'", name);
390 }
391 }
392
393 return err;
394}
395
396static struct android_usb_function diag_function = {
397 .name = "diag",
398 .init = diag_function_init,
399 .cleanup = diag_function_cleanup,
400 .bind_config = diag_function_bind_config,
401 .attributes = diag_function_attributes,
402};
403
Manu Gautam8e0719b2011-09-26 14:47:55 +0530404/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530405static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530406static ssize_t serial_transports_store(
407 struct device *device, struct device_attribute *attr,
408 const char *buff, size_t size)
409{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530410 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530411
412 return size;
413}
414
415static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
416static struct device_attribute *serial_function_attributes[] =
417 { &dev_attr_transports, NULL };
418
419static void serial_function_cleanup(struct android_usb_function *f)
420{
421 gserial_cleanup();
422}
423
424static int serial_function_bind_config(struct android_usb_function *f,
425 struct usb_configuration *c)
426{
427 char *name;
428 char buf[32], *b;
429 int err = -1, i;
430 static int serial_initialized = 0, ports = 0;
431
432 if (serial_initialized)
433 goto bind_config;
434
435 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530436 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530437 b = strim(buf);
438
439 while (b) {
440 name = strsep(&b, ",");
441
442 if (name) {
443 err = gserial_init_port(ports, name);
444 if (err) {
445 pr_err("serial: Cannot open port '%s'", name);
446 goto out;
447 }
448 ports++;
449 }
450 }
451 err = gport_setup(c);
452 if (err) {
453 pr_err("serial: Cannot setup transports");
454 goto out;
455 }
456
457bind_config:
458 for (i = 0; i < ports; i++) {
459 err = gser_bind_config(c, i);
460 if (err) {
461 pr_err("serial: bind_config failed for port %d", i);
462 goto out;
463 }
464 }
465
466out:
467 return err;
468}
469
470static struct android_usb_function serial_function = {
471 .name = "serial",
472 .cleanup = serial_function_cleanup,
473 .bind_config = serial_function_bind_config,
474 .attributes = serial_function_attributes,
475};
476
477
Manu Gautam8e0719b2011-09-26 14:47:55 +0530478/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700479static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
480{
481 return adb_setup();
482}
483
484static void adb_function_cleanup(struct android_usb_function *f)
485{
486 adb_cleanup();
487}
488
489static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
490{
491 return adb_bind_config(c);
492}
493
494static struct android_usb_function adb_function = {
495 .name = "adb",
496 .init = adb_function_init,
497 .cleanup = adb_function_cleanup,
498 .bind_config = adb_function_bind_config,
499};
500
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501#if 0
Benoit Gobyaab96812011-04-19 20:37:33 -0700502#define MAX_ACM_INSTANCES 4
503struct acm_function_config {
504 int instances;
505};
506
507static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
508{
509 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
510 if (!f->config)
511 return -ENOMEM;
512
513 return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
514}
515
516static void acm_function_cleanup(struct android_usb_function *f)
517{
518 gserial_cleanup();
519 kfree(f->config);
520 f->config = NULL;
521}
522
523static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530524{
525 int i;
Benoit Gobyaab96812011-04-19 20:37:33 -0700526 int ret = 0;
527 struct acm_function_config *config = f->config;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530528
Benoit Gobyaab96812011-04-19 20:37:33 -0700529 for (i = 0; i < config->instances; i++) {
530 ret = acm_bind_config(c, i);
531 if (ret) {
532 pr_err("Could not bind acm%u config\n", i);
533 break;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530534 }
535 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700536
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530537 return ret;
538}
539
Benoit Gobyaab96812011-04-19 20:37:33 -0700540static ssize_t acm_instances_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530542{
Benoit Gobyaab96812011-04-19 20:37:33 -0700543 struct android_usb_function *f = dev_get_drvdata(dev);
544 struct acm_function_config *config = f->config;
545 return sprintf(buf, "%d\n", config->instances);
546}
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530547
Benoit Gobyaab96812011-04-19 20:37:33 -0700548static ssize_t acm_instances_store(struct device *dev,
549 struct device_attribute *attr, const char *buf, size_t size)
550{
551 struct android_usb_function *f = dev_get_drvdata(dev);
552 struct acm_function_config *config = f->config;
553 int value;
554
555 sscanf(buf, "%d", &value);
556 if (value > MAX_ACM_INSTANCES)
557 value = MAX_ACM_INSTANCES;
558 config->instances = value;
559 return size;
560}
561
562static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
563static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
564
565static struct android_usb_function acm_function = {
566 .name = "acm",
567 .init = acm_function_init,
568 .cleanup = acm_function_cleanup,
569 .bind_config = acm_function_bind_config,
570 .attributes = acm_function_attributes,
571};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572#endif
Benoit Gobyaab96812011-04-19 20:37:33 -0700573
574static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
575{
576 return mtp_setup();
577}
578
579static void mtp_function_cleanup(struct android_usb_function *f)
580{
581 mtp_cleanup();
582}
583
584static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
585{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400586 return mtp_bind_config(c, false);
587}
588
589static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
590{
591 /* nothing to do - initialization is handled by mtp_function_init */
592 return 0;
593}
594
595static void ptp_function_cleanup(struct android_usb_function *f)
596{
597 /* nothing to do - cleanup is handled by mtp_function_cleanup */
598}
599
600static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
601{
602 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700603}
604
605static int mtp_function_ctrlrequest(struct android_usb_function *f,
606 struct usb_composite_dev *cdev,
607 const struct usb_ctrlrequest *c)
608{
609 return mtp_ctrlrequest(cdev, c);
610}
611
612static struct android_usb_function mtp_function = {
613 .name = "mtp",
614 .init = mtp_function_init,
615 .cleanup = mtp_function_cleanup,
616 .bind_config = mtp_function_bind_config,
617 .ctrlrequest = mtp_function_ctrlrequest,
618};
619
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400620/* PTP function is same as MTP with slightly different interface descriptor */
621static struct android_usb_function ptp_function = {
622 .name = "ptp",
623 .init = ptp_function_init,
624 .cleanup = ptp_function_cleanup,
625 .bind_config = ptp_function_bind_config,
626};
627
Benoit Gobyaab96812011-04-19 20:37:33 -0700628
629struct rndis_function_config {
630 u8 ethaddr[ETH_ALEN];
631 u32 vendorID;
632 char manufacturer[256];
633 bool wceis;
634};
635
636static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
637{
638 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
639 if (!f->config)
640 return -ENOMEM;
641 return 0;
642}
643
644static void rndis_function_cleanup(struct android_usb_function *f)
645{
646 kfree(f->config);
647 f->config = NULL;
648}
649
650static int rndis_function_bind_config(struct android_usb_function *f,
651 struct usb_configuration *c)
652{
Manu Gautamf4741132011-11-25 09:08:53 +0530653 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700654 struct rndis_function_config *rndis = f->config;
655
656 if (!rndis) {
657 pr_err("%s: rndis_pdata\n", __func__);
658 return -1;
659 }
660
661 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
662 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
663 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
664
Manu Gautamf4741132011-11-25 09:08:53 +0530665 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
666 if (ret) {
667 pr_err("%s: gether_setup failed\n", __func__);
668 return ret;
669 }
670
Benoit Gobyaab96812011-04-19 20:37:33 -0700671 if (rndis->wceis) {
672 /* "Wireless" RNDIS; auto-detected by Windows */
673 rndis_iad_descriptor.bFunctionClass =
674 USB_CLASS_WIRELESS_CONTROLLER;
675 rndis_iad_descriptor.bFunctionSubClass = 0x01;
676 rndis_iad_descriptor.bFunctionProtocol = 0x03;
677 rndis_control_intf.bInterfaceClass =
678 USB_CLASS_WIRELESS_CONTROLLER;
679 rndis_control_intf.bInterfaceSubClass = 0x01;
680 rndis_control_intf.bInterfaceProtocol = 0x03;
681 }
682
683 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
684 rndis->manufacturer);
685}
686
687static void rndis_function_unbind_config(struct android_usb_function *f,
688 struct usb_configuration *c)
689{
Manu Gautamf4741132011-11-25 09:08:53 +0530690 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700691}
692
693static ssize_t rndis_manufacturer_show(struct device *dev,
694 struct device_attribute *attr, char *buf)
695{
696 struct android_usb_function *f = dev_get_drvdata(dev);
697 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530698 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700699}
700
701static ssize_t rndis_manufacturer_store(struct device *dev,
702 struct device_attribute *attr, const char *buf, size_t size)
703{
704 struct android_usb_function *f = dev_get_drvdata(dev);
705 struct rndis_function_config *config = f->config;
706
707 if (size >= sizeof(config->manufacturer))
708 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530709 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700710 return size;
711 return -1;
712}
713
714static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
715 rndis_manufacturer_store);
716
717static ssize_t rndis_wceis_show(struct device *dev,
718 struct device_attribute *attr, char *buf)
719{
720 struct android_usb_function *f = dev_get_drvdata(dev);
721 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530722 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700723}
724
725static ssize_t rndis_wceis_store(struct device *dev,
726 struct device_attribute *attr, const char *buf, size_t size)
727{
728 struct android_usb_function *f = dev_get_drvdata(dev);
729 struct rndis_function_config *config = f->config;
730 int value;
731
732 if (sscanf(buf, "%d", &value) == 1) {
733 config->wceis = value;
734 return size;
735 }
736 return -EINVAL;
737}
738
739static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
740 rndis_wceis_store);
741
742static ssize_t rndis_ethaddr_show(struct device *dev,
743 struct device_attribute *attr, char *buf)
744{
745 struct android_usb_function *f = dev_get_drvdata(dev);
746 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530747 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700748 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
749 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
750}
751
752static ssize_t rndis_ethaddr_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 *rndis = f->config;
757
758 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
759 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
760 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
761 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
762 return size;
763 return -EINVAL;
764}
765
766static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
767 rndis_ethaddr_store);
768
769static ssize_t rndis_vendorID_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 *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530774 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700775}
776
777static ssize_t rndis_vendorID_store(struct device *dev,
778 struct device_attribute *attr, const char *buf, size_t size)
779{
780 struct android_usb_function *f = dev_get_drvdata(dev);
781 struct rndis_function_config *config = f->config;
782 int value;
783
784 if (sscanf(buf, "%04x", &value) == 1) {
785 config->vendorID = value;
786 return size;
787 }
788 return -EINVAL;
789}
790
791static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
792 rndis_vendorID_store);
793
794static struct device_attribute *rndis_function_attributes[] = {
795 &dev_attr_manufacturer,
796 &dev_attr_wceis,
797 &dev_attr_ethaddr,
798 &dev_attr_vendorID,
799 NULL
800};
801
802static struct android_usb_function rndis_function = {
803 .name = "rndis",
804 .init = rndis_function_init,
805 .cleanup = rndis_function_cleanup,
806 .bind_config = rndis_function_bind_config,
807 .unbind_config = rndis_function_unbind_config,
808 .attributes = rndis_function_attributes,
809};
810
811
812struct mass_storage_function_config {
813 struct fsg_config fsg;
814 struct fsg_common *common;
815};
816
817static int mass_storage_function_init(struct android_usb_function *f,
818 struct usb_composite_dev *cdev)
819{
820 struct mass_storage_function_config *config;
821 struct fsg_common *common;
822 int err;
823
824 config = kzalloc(sizeof(struct mass_storage_function_config),
825 GFP_KERNEL);
826 if (!config)
827 return -ENOMEM;
828
829 config->fsg.nluns = 1;
830 config->fsg.luns[0].removable = 1;
831
832 common = fsg_common_init(NULL, cdev, &config->fsg);
833 if (IS_ERR(common)) {
834 kfree(config);
835 return PTR_ERR(common);
836 }
837
838 err = sysfs_create_link(&f->dev->kobj,
839 &common->luns[0].dev.kobj,
840 "lun");
841 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530842 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700843 kfree(config);
844 return err;
845 }
846
847 config->common = common;
848 f->config = config;
849 return 0;
850}
851
852static void mass_storage_function_cleanup(struct android_usb_function *f)
853{
854 kfree(f->config);
855 f->config = NULL;
856}
857
858static int mass_storage_function_bind_config(struct android_usb_function *f,
859 struct usb_configuration *c)
860{
861 struct mass_storage_function_config *config = f->config;
862 return fsg_bind_config(c->cdev, c, config->common);
863}
864
865static ssize_t mass_storage_inquiry_show(struct device *dev,
866 struct device_attribute *attr, char *buf)
867{
868 struct android_usb_function *f = dev_get_drvdata(dev);
869 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530870 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -0700871}
872
873static ssize_t mass_storage_inquiry_store(struct device *dev,
874 struct device_attribute *attr, const char *buf, size_t size)
875{
876 struct android_usb_function *f = dev_get_drvdata(dev);
877 struct mass_storage_function_config *config = f->config;
878 if (size >= sizeof(config->common->inquiry_string))
879 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530880 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700881 return -EINVAL;
882 return size;
883}
884
885static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
886 mass_storage_inquiry_show,
887 mass_storage_inquiry_store);
888
889static struct device_attribute *mass_storage_function_attributes[] = {
890 &dev_attr_inquiry_string,
891 NULL
892};
893
894static struct android_usb_function mass_storage_function = {
895 .name = "mass_storage",
896 .init = mass_storage_function_init,
897 .cleanup = mass_storage_function_cleanup,
898 .bind_config = mass_storage_function_bind_config,
899 .attributes = mass_storage_function_attributes,
900};
901
902
903static int accessory_function_init(struct android_usb_function *f,
904 struct usb_composite_dev *cdev)
905{
906 return acc_setup();
907}
908
909static void accessory_function_cleanup(struct android_usb_function *f)
910{
911 acc_cleanup();
912}
913
914static int accessory_function_bind_config(struct android_usb_function *f,
915 struct usb_configuration *c)
916{
917 return acc_bind_config(c);
918}
919
920static int accessory_function_ctrlrequest(struct android_usb_function *f,
921 struct usb_composite_dev *cdev,
922 const struct usb_ctrlrequest *c)
923{
924 return acc_ctrlrequest(cdev, c);
925}
926
927static struct android_usb_function accessory_function = {
928 .name = "accessory",
929 .init = accessory_function_init,
930 .cleanup = accessory_function_cleanup,
931 .bind_config = accessory_function_bind_config,
932 .ctrlrequest = accessory_function_ctrlrequest,
933};
934
935
936static struct android_usb_function *supported_functions[] = {
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530937 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +0530938 &rmnet_sdio_function,
939 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530940 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +0530942 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700943 &adb_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944// &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700945 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400946 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700947 &rndis_function,
948 &mass_storage_function,
949 &accessory_function,
950 NULL
951};
952
953
954static int android_init_functions(struct android_usb_function **functions,
955 struct usb_composite_dev *cdev)
956{
957 struct android_dev *dev = _android_dev;
958 struct android_usb_function *f;
959 struct device_attribute **attrs;
960 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530961 int err = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700962 int index = 0;
963
964 for (; (f = *functions++); index++) {
965 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
966 f->dev = device_create(android_class, dev->dev,
967 MKDEV(0, index), f, f->dev_name);
968 if (IS_ERR(f->dev)) {
969 pr_err("%s: Failed to create dev %s", __func__,
970 f->dev_name);
971 err = PTR_ERR(f->dev);
972 goto err_create;
973 }
974
975 if (f->init) {
976 err = f->init(f, cdev);
977 if (err) {
978 pr_err("%s: Failed to init %s", __func__,
979 f->name);
980 goto err_out;
981 }
982 }
983
984 attrs = f->attributes;
985 if (attrs) {
986 while ((attr = *attrs++) && !err)
987 err = device_create_file(f->dev, attr);
988 }
989 if (err) {
990 pr_err("%s: Failed to create function %s attributes",
991 __func__, f->name);
992 goto err_out;
993 }
994 }
995 return 0;
996
997err_out:
998 device_destroy(android_class, f->dev->devt);
999err_create:
1000 kfree(f->dev_name);
1001 return err;
1002}
1003
1004static void android_cleanup_functions(struct android_usb_function **functions)
1005{
1006 struct android_usb_function *f;
1007
1008 while (*functions) {
1009 f = *functions++;
1010
1011 if (f->dev) {
1012 device_destroy(android_class, f->dev->devt);
1013 kfree(f->dev_name);
1014 }
1015
1016 if (f->cleanup)
1017 f->cleanup(f);
1018 }
1019}
1020
1021static int
1022android_bind_enabled_functions(struct android_dev *dev,
1023 struct usb_configuration *c)
1024{
1025 struct android_usb_function *f;
1026 int ret;
1027
1028 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1029 ret = f->bind_config(f, c);
1030 if (ret) {
1031 pr_err("%s: %s failed", __func__, f->name);
1032 return ret;
1033 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301034 }
1035 return 0;
1036}
1037
Benoit Gobyaab96812011-04-19 20:37:33 -07001038static void
1039android_unbind_enabled_functions(struct android_dev *dev,
1040 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301041{
Benoit Gobyaab96812011-04-19 20:37:33 -07001042 struct android_usb_function *f;
1043
1044 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1045 if (f->unbind_config)
1046 f->unbind_config(f, c);
1047 }
1048}
1049
1050static int android_enable_function(struct android_dev *dev, char *name)
1051{
1052 struct android_usb_function **functions = dev->functions;
1053 struct android_usb_function *f;
1054 while ((f = *functions++)) {
1055 if (!strcmp(name, f->name)) {
1056 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301057 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001058 }
1059 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001060 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001061}
1062
Benoit Gobyaab96812011-04-19 20:37:33 -07001063/*-------------------------------------------------------------------------*/
1064/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301065
Benoit Gobyaab96812011-04-19 20:37:33 -07001066static ssize_t
1067functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1068{
1069 struct android_dev *dev = dev_get_drvdata(pdev);
1070 struct android_usb_function *f;
1071 char *buff = buf;
1072
1073 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301074 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobyaab96812011-04-19 20:37:33 -07001075 if (buff != buf)
1076 *(buff-1) = '\n';
1077 return buff - buf;
1078}
1079
1080static ssize_t
1081functions_store(struct device *pdev, struct device_attribute *attr,
1082 const char *buff, size_t size)
1083{
1084 struct android_dev *dev = dev_get_drvdata(pdev);
1085 char *name;
1086 char buf[256], *b;
1087 int err;
1088
1089 INIT_LIST_HEAD(&dev->enabled_functions);
1090
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301091 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001092 b = strim(buf);
1093
1094 while (b) {
1095 name = strsep(&b, ",");
1096 if (name) {
1097 err = android_enable_function(dev, name);
1098 if (err)
1099 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301100 }
1101 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001102
1103 return size;
1104}
1105
1106static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1107 char *buf)
1108{
1109 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301110 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001111}
1112
1113static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1114 const char *buff, size_t size)
1115{
1116 struct android_dev *dev = dev_get_drvdata(pdev);
1117 struct usb_composite_dev *cdev = dev->cdev;
1118 int enabled = 0;
1119
1120 sscanf(buff, "%d", &enabled);
1121 if (enabled && !dev->enabled) {
1122 /* update values in composite driver's copy of device descriptor */
1123 cdev->desc.idVendor = device_desc.idVendor;
1124 cdev->desc.idProduct = device_desc.idProduct;
1125 cdev->desc.bcdDevice = device_desc.bcdDevice;
1126 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1127 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1128 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301129 if (usb_add_config(cdev, &android_config_driver,
1130 android_bind_config))
1131 return size;
1132
Benoit Gobyaab96812011-04-19 20:37:33 -07001133 usb_gadget_connect(cdev->gadget);
1134 dev->enabled = true;
1135 } else if (!enabled && dev->enabled) {
1136 usb_gadget_disconnect(cdev->gadget);
1137 usb_remove_config(cdev, &android_config_driver);
1138 dev->enabled = false;
1139 } else {
1140 pr_err("android_usb: already %s\n",
1141 dev->enabled ? "enabled" : "disabled");
1142 }
1143 return size;
1144}
1145
1146static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1147 char *buf)
1148{
1149 struct android_dev *dev = dev_get_drvdata(pdev);
1150 struct usb_composite_dev *cdev = dev->cdev;
1151 char *state = "DISCONNECTED";
1152 unsigned long flags;
1153
1154 if (!cdev)
1155 goto out;
1156
1157 spin_lock_irqsave(&cdev->lock, flags);
1158 if (cdev->config)
1159 state = "CONFIGURED";
1160 else if (dev->connected)
1161 state = "CONNECTED";
1162 spin_unlock_irqrestore(&cdev->lock, flags);
1163out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301164 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001165}
1166
1167#define DESCRIPTOR_ATTR(field, format_string) \
1168static ssize_t \
1169field ## _show(struct device *dev, struct device_attribute *attr, \
1170 char *buf) \
1171{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301172 return snprintf(buf, PAGE_SIZE, \
1173 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001174} \
1175static ssize_t \
1176field ## _store(struct device *dev, struct device_attribute *attr, \
1177 const char *buf, size_t size) \
1178{ \
1179 int value; \
1180 if (sscanf(buf, format_string, &value) == 1) { \
1181 device_desc.field = value; \
1182 return size; \
1183 } \
1184 return -1; \
1185} \
1186static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1187
1188#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1189static ssize_t \
1190field ## _show(struct device *dev, struct device_attribute *attr, \
1191 char *buf) \
1192{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301193 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001194} \
1195static ssize_t \
1196field ## _store(struct device *dev, struct device_attribute *attr, \
1197 const char *buf, size_t size) \
1198{ \
1199 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301200 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001201 return size; \
1202 } \
1203 return -1; \
1204} \
1205static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1206
1207
1208DESCRIPTOR_ATTR(idVendor, "%04x\n")
1209DESCRIPTOR_ATTR(idProduct, "%04x\n")
1210DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1211DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1212DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1213DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1214DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1215DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1216DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1217
1218static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1219static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1220static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1221
1222static struct device_attribute *android_usb_attributes[] = {
1223 &dev_attr_idVendor,
1224 &dev_attr_idProduct,
1225 &dev_attr_bcdDevice,
1226 &dev_attr_bDeviceClass,
1227 &dev_attr_bDeviceSubClass,
1228 &dev_attr_bDeviceProtocol,
1229 &dev_attr_iManufacturer,
1230 &dev_attr_iProduct,
1231 &dev_attr_iSerial,
1232 &dev_attr_functions,
1233 &dev_attr_enable,
1234 &dev_attr_state,
1235 NULL
1236};
1237
1238/*-------------------------------------------------------------------------*/
1239/* Composite driver */
1240
1241static int android_bind_config(struct usb_configuration *c)
1242{
1243 struct android_dev *dev = _android_dev;
1244 int ret = 0;
1245
1246 ret = android_bind_enabled_functions(dev, c);
1247 if (ret)
1248 return ret;
1249
1250 return 0;
1251}
1252
1253static void android_unbind_config(struct usb_configuration *c)
1254{
1255 struct android_dev *dev = _android_dev;
1256
1257 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301258}
1259
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001260static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001261{
1262 struct android_dev *dev = _android_dev;
1263 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001264 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001265
Benoit Gobyaab96812011-04-19 20:37:33 -07001266 usb_gadget_disconnect(gadget);
1267
1268 ret = android_init_functions(dev->functions, cdev);
1269 if (ret)
1270 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001271
1272 /* Allocate string descriptor numbers ... note that string
1273 * contents can be overridden by the composite_dev glue.
1274 */
1275 id = usb_string_id(cdev);
1276 if (id < 0)
1277 return id;
1278 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1279 device_desc.iManufacturer = id;
1280
1281 id = usb_string_id(cdev);
1282 if (id < 0)
1283 return id;
1284 strings_dev[STRING_PRODUCT_IDX].id = id;
1285 device_desc.iProduct = id;
1286
Benoit Gobyaab96812011-04-19 20:37:33 -07001287 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301288 strlcpy(manufacturer_string, "Android",
1289 sizeof(manufacturer_string) - 1);
1290 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1291 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001292
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001293 id = usb_string_id(cdev);
1294 if (id < 0)
1295 return id;
1296 strings_dev[STRING_SERIAL_IDX].id = id;
1297 device_desc.iSerialNumber = id;
1298
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001299 gcnum = usb_gadget_controller_number(gadget);
1300 if (gcnum >= 0)
1301 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1302 else {
1303 /* gadget zero is so simple (for now, no altsettings) that
1304 * it SHOULD NOT have problems with bulk-capable hardware.
1305 * so just warn about unrcognized controllers -- don't panic.
1306 *
1307 * things like configuration and altsetting numbering
1308 * can need hardware-specific attention though.
1309 */
1310 pr_warning("%s: controller '%s' not recognized\n",
1311 longname, gadget->name);
1312 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1313 }
1314
1315 usb_gadget_set_selfpowered(gadget);
1316 dev->cdev = cdev;
1317
1318 return 0;
1319}
1320
Benoit Gobyaab96812011-04-19 20:37:33 -07001321static int android_usb_unbind(struct usb_composite_dev *cdev)
1322{
1323 struct android_dev *dev = _android_dev;
1324
1325 cancel_work_sync(&dev->work);
1326 android_cleanup_functions(dev->functions);
1327 return 0;
1328}
1329
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001330static struct usb_composite_driver android_usb_driver = {
1331 .name = "android_usb",
1332 .dev = &device_desc,
1333 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001334 .unbind = android_usb_unbind,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001335};
1336
Benoit Gobyaab96812011-04-19 20:37:33 -07001337static int
1338android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1339{
1340 struct android_dev *dev = _android_dev;
1341 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1342 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001343 struct android_usb_function *f;
1344 int value = -EOPNOTSUPP;
1345 unsigned long flags;
1346
1347 req->zero = 0;
1348 req->complete = composite_setup_complete;
1349 req->length = 0;
1350 gadget->ep0->driver_data = cdev;
1351
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001352 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001353 if (f->ctrlrequest) {
1354 value = f->ctrlrequest(f, cdev, c);
1355 if (value >= 0)
1356 break;
1357 }
1358 }
1359
Mike Lockwood686d33a2011-09-07 09:55:12 -07001360 /* Special case the accessory function.
1361 * It needs to handle control requests before it is enabled.
1362 */
1363 if (value < 0)
1364 value = acc_ctrlrequest(cdev, c);
1365
Benoit Gobyaab96812011-04-19 20:37:33 -07001366 if (value < 0)
1367 value = composite_setup(gadget, c);
1368
1369 spin_lock_irqsave(&cdev->lock, flags);
1370 if (!dev->connected) {
1371 dev->connected = 1;
1372 schedule_work(&dev->work);
1373 }
1374 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1375 schedule_work(&dev->work);
1376 }
1377 spin_unlock_irqrestore(&cdev->lock, flags);
1378
1379 return value;
1380}
1381
1382static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001383{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301384 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001385 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1386 unsigned long flags;
1387
1388 composite_disconnect(gadget);
1389
1390 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001391 dev->connected = 0;
1392 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001393 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301394}
1395
Benoit Gobyaab96812011-04-19 20:37:33 -07001396static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001397{
Benoit Gobyaab96812011-04-19 20:37:33 -07001398 struct device_attribute **attrs = android_usb_attributes;
1399 struct device_attribute *attr;
1400 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001401
Benoit Gobyaab96812011-04-19 20:37:33 -07001402 dev->dev = device_create(android_class, NULL,
1403 MKDEV(0, 0), NULL, "android0");
1404 if (IS_ERR(dev->dev))
1405 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001406
Benoit Gobyaab96812011-04-19 20:37:33 -07001407 dev_set_drvdata(dev->dev, dev);
1408
1409 while ((attr = *attrs++)) {
1410 err = device_create_file(dev->dev, attr);
1411 if (err) {
1412 device_destroy(android_class, dev->dev->devt);
1413 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001414 }
1415 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001416 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001417}
1418
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419static int __devinit android_probe(struct platform_device *pdev)
1420{
1421 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1422 struct android_dev *dev = _android_dev;
1423
1424 dev->pdata = pdata;
1425
1426 return 0;
1427}
1428
1429static struct platform_driver android_platform_driver = {
1430 .driver = { .name = "android_usb"},
1431};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001432
1433static int __init init(void)
1434{
1435 struct android_dev *dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001436 int err;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001437
Benoit Gobyaab96812011-04-19 20:37:33 -07001438 android_class = class_create(THIS_MODULE, "android_usb");
1439 if (IS_ERR(android_class))
1440 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001441
1442 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1443 if (!dev)
1444 return -ENOMEM;
1445
Benoit Gobyaab96812011-04-19 20:37:33 -07001446 dev->functions = supported_functions;
1447 INIT_LIST_HEAD(&dev->enabled_functions);
1448 INIT_WORK(&dev->work, android_work);
1449
1450 err = android_create_device(dev);
1451 if (err) {
1452 class_destroy(android_class);
1453 kfree(dev);
1454 return err;
1455 }
1456
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001457 _android_dev = dev;
1458
Benoit Gobyaab96812011-04-19 20:37:33 -07001459 /* Override composite driver functions */
1460 composite_driver.setup = android_setup;
1461 composite_driver.disconnect = android_disconnect;
1462
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001463 platform_driver_probe(&android_platform_driver, android_probe);
1464
Benoit Gobyaab96812011-04-19 20:37:33 -07001465 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001466}
1467module_init(init);
1468
1469static void __exit cleanup(void)
1470{
1471 usb_composite_unregister(&android_usb_driver);
Benoit Gobyaab96812011-04-19 20:37:33 -07001472 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001473 kfree(_android_dev);
1474 _android_dev = NULL;
1475}
1476module_exit(cleanup);