blob: 49d7c0f7d85a67645fc4eca094f7579c2c1a2604 [file] [log] [blame]
Benoit Goby1e8ce152011-12-12 13:01:23 -08001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/utsname.h>
25#include <linux/platform_device.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/pm_qos.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080027
28#include <linux/usb/ch9.h>
29#include <linux/usb/composite.h>
30#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <linux/usb/android.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080032
33#include "gadget_chips.h"
34
35/*
36 * Kbuild is not very cooperative with respect to linking separately
37 * compiled library objects into one module. So for now we won't use
38 * separate compilation ... ensuring init/exit sections work to shrink
39 * the runtime footprint, and giving us at least some parts of what
40 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
41 */
42#include "usbstring.c"
43#include "config.c"
44#include "epautoconf.c"
45#include "composite.c"
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053048#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053049#include "f_rmnet_sdio.c"
50#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053051#include "f_rmnet.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080052#include "f_mass_storage.c"
53#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053054#include "u_sdio.c"
55#include "u_smd.c"
56#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053057#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080058#include "u_ctrl_hsic.c"
59#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053060#include "u_ctrl_hsuart.c"
61#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053062#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080063#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080064#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053065#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080066#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080067#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080068#define USB_ETH_RNDIS y
69#include "f_rndis.c"
70#include "rndis.c"
71#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030072#include "u_bam_data.c"
73#include "f_mbim.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030074#include "f_qc_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020075#include "f_qc_rndis.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030076#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053077#ifdef CONFIG_TARGET_CORE
78#include "f_tcm.c"
79#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080080
81MODULE_AUTHOR("Mike Lockwood");
82MODULE_DESCRIPTION("Android Composite USB Driver");
83MODULE_LICENSE("GPL");
84MODULE_VERSION("1.0");
85
86static const char longname[] = "Gadget Android";
87
88/* Default vendor and product IDs, overridden by userspace */
89#define VENDOR_ID 0x18D1
90#define PRODUCT_ID 0x0001
91
Ido Shayevitz23dc77c2012-07-18 16:16:06 +030092#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
93
Benoit Goby1e8ce152011-12-12 13:01:23 -080094struct android_usb_function {
95 char *name;
96 void *config;
97
98 struct device *dev;
99 char *dev_name;
100 struct device_attribute **attributes;
101
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300102 /* for android_conf.enabled_functions */
Benoit Goby1e8ce152011-12-12 13:01:23 -0800103 struct list_head enabled_list;
104
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300105 struct android_dev *android_dev;
106
Benoit Goby1e8ce152011-12-12 13:01:23 -0800107 /* Optional: initialization during gadget bind */
108 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
109 /* Optional: cleanup during gadget unbind */
110 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700111 /* Optional: called when the function is added the list of
112 * enabled functions */
113 void (*enable)(struct android_usb_function *);
114 /* Optional: called when it is removed */
115 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800116
117 int (*bind_config)(struct android_usb_function *,
118 struct usb_configuration *);
119
120 /* Optional: called when the configuration is removed */
121 void (*unbind_config)(struct android_usb_function *,
122 struct usb_configuration *);
123 /* Optional: handle ctrl requests before the device is configured */
124 int (*ctrlrequest)(struct android_usb_function *,
125 struct usb_composite_dev *,
126 const struct usb_ctrlrequest *);
127};
128
129struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300130 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800131 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800132 struct usb_composite_dev *cdev;
133 struct device *dev;
134
135 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700136 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800137 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700138 struct android_usb_platform_data *pdata;
139
Benoit Goby1e8ce152011-12-12 13:01:23 -0800140 bool connected;
141 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300142 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700143 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800144 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300145
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300146 /* A list of struct android_configuration */
147 struct list_head configs;
148 int configs_num;
149
150 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300151 struct list_head list_item;
152
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300153};
154
155struct android_configuration {
156 struct usb_configuration usb_config;
157
158 /* A list of the functions supported by this config */
159 struct list_head enabled_functions;
160
161 /* A list node inside the struct android_dev.configs list */
162 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800163};
164
165static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300166static struct list_head android_dev_list;
167static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800168static int android_bind_config(struct usb_configuration *c);
169static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300170static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300171static struct android_configuration *alloc_android_config
172 (struct android_dev *dev);
173static void free_android_config(struct android_dev *dev,
174 struct android_configuration *conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800175
176/* string IDs are assigned dynamically */
177#define STRING_MANUFACTURER_IDX 0
178#define STRING_PRODUCT_IDX 1
179#define STRING_SERIAL_IDX 2
180
181static char manufacturer_string[256];
182static char product_string[256];
183static char serial_string[256];
184
185/* String Table */
186static struct usb_string strings_dev[] = {
187 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
188 [STRING_PRODUCT_IDX].s = product_string,
189 [STRING_SERIAL_IDX].s = serial_string,
190 { } /* end of list */
191};
192
193static struct usb_gadget_strings stringtab_dev = {
194 .language = 0x0409, /* en-us */
195 .strings = strings_dev,
196};
197
198static struct usb_gadget_strings *dev_strings[] = {
199 &stringtab_dev,
200 NULL,
201};
202
203static struct usb_device_descriptor device_desc = {
204 .bLength = sizeof(device_desc),
205 .bDescriptorType = USB_DT_DEVICE,
206 .bcdUSB = __constant_cpu_to_le16(0x0200),
207 .bDeviceClass = USB_CLASS_PER_INTERFACE,
208 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
209 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
210 .bcdDevice = __constant_cpu_to_le16(0xffff),
211 .bNumConfigurations = 1,
212};
213
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530214static struct usb_otg_descriptor otg_descriptor = {
215 .bLength = sizeof otg_descriptor,
216 .bDescriptorType = USB_DT_OTG,
217 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
218 .bcdOTG = __constant_cpu_to_le16(0x0200),
219};
220
221static const struct usb_descriptor_header *otg_desc[] = {
222 (struct usb_descriptor_header *) &otg_descriptor,
223 NULL,
224};
225
Manu Gautama2b54142012-04-03 14:34:32 +0530226enum android_device_state {
227 USB_DISCONNECTED,
228 USB_CONNECTED,
229 USB_CONFIGURED,
230};
231
Ofir Cohen94213a72012-05-03 14:26:32 +0300232static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
233{
234 struct android_usb_platform_data *pdata = dev->pdata;
235 u32 swfi_latency = 0;
236 static int last_vote = -1;
237
Ofir Cohen56eb7072012-05-20 11:41:39 +0300238 if (!pdata || vote == last_vote
239 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300240 return;
241
242 swfi_latency = pdata->swfi_latency + 1;
243 if (vote)
244 pm_qos_update_request(&dev->pm_qos_req_dma,
245 swfi_latency);
246 else
247 pm_qos_update_request(&dev->pm_qos_req_dma,
248 PM_QOS_DEFAULT_VALUE);
249 last_vote = vote;
250}
251
Benoit Goby1e8ce152011-12-12 13:01:23 -0800252static void android_work(struct work_struct *data)
253{
254 struct android_dev *dev = container_of(data, struct android_dev, work);
255 struct usb_composite_dev *cdev = dev->cdev;
256 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
257 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
258 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
259 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530260 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800261 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300262 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800263
264 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530265 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800266 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530267 next_state = USB_CONFIGURED;
268 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800269 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530270 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300271 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300272 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300273 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300274 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530275 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800276 dev->sw_connected = dev->connected;
277 spin_unlock_irqrestore(&cdev->lock, flags);
278
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300279 if (pm_qos_vote != -1)
280 android_pm_qos_update_latency(dev, pm_qos_vote);
281
Benoit Goby1e8ce152011-12-12 13:01:23 -0800282 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530283 /*
284 * Some userspace modules, e.g. MTP, work correctly only if
285 * CONFIGURED uevent is preceded by DISCONNECT uevent.
286 * Check if we missed sending out a DISCONNECT uevent. This can
287 * happen if host PC resets and configures device really quick.
288 */
289 if (((uevent_envp == connected) &&
290 (last_uevent != USB_DISCONNECTED)) ||
291 ((uevent_envp == configured) &&
292 (last_uevent == USB_CONFIGURED))) {
293 pr_info("%s: sent missed DISCONNECT event\n", __func__);
294 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
295 disconnected);
296 msleep(20);
297 }
298 /*
299 * Before sending out CONFIGURED uevent give function drivers
300 * a chance to wakeup userspace threads and notify disconnect
301 */
302 if (uevent_envp == configured)
303 msleep(50);
304
Benoit Goby1e8ce152011-12-12 13:01:23 -0800305 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530306 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800307 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
308 } else {
309 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
310 dev->connected, dev->sw_connected, cdev->config);
311 }
312}
313
Benoit Goby80ba14d2012-03-19 18:56:52 -0700314static void android_enable(struct android_dev *dev)
315{
316 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300317 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700318
319 if (WARN_ON(!dev->disable_depth))
320 return;
321
322 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300323
324 list_for_each_entry(conf, &dev->configs, list_item)
325 usb_add_config(cdev, &conf->usb_config,
326 android_bind_config);
327
Benoit Goby80ba14d2012-03-19 18:56:52 -0700328 usb_gadget_connect(cdev->gadget);
329 }
330}
331
332static void android_disable(struct android_dev *dev)
333{
334 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300335 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700336
337 if (dev->disable_depth++ == 0) {
338 usb_gadget_disconnect(cdev->gadget);
339 /* Cancel pending control requests */
340 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300341
342 list_for_each_entry(conf, &dev->configs, list_item)
343 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700344 }
345}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800346
347/*-------------------------------------------------------------------------*/
348/* Supported functions initialization */
349
Benoit Goby80ba14d2012-03-19 18:56:52 -0700350struct adb_data {
351 bool opened;
352 bool enabled;
353};
354
Benoit Goby2b6862d2011-12-19 14:38:41 -0800355static int
356adb_function_init(struct android_usb_function *f,
357 struct usb_composite_dev *cdev)
358{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700359 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
360 if (!f->config)
361 return -ENOMEM;
362
Benoit Goby2b6862d2011-12-19 14:38:41 -0800363 return adb_setup();
364}
365
366static void adb_function_cleanup(struct android_usb_function *f)
367{
368 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700369 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800370}
371
372static int
373adb_function_bind_config(struct android_usb_function *f,
374 struct usb_configuration *c)
375{
376 return adb_bind_config(c);
377}
378
Benoit Goby80ba14d2012-03-19 18:56:52 -0700379static void adb_android_function_enable(struct android_usb_function *f)
380{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300381 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700382 struct adb_data *data = f->config;
383
384 data->enabled = true;
385
386 /* Disable the gadget until adbd is ready */
387 if (!data->opened)
388 android_disable(dev);
389}
390
391static void adb_android_function_disable(struct android_usb_function *f)
392{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300393 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700394 struct adb_data *data = f->config;
395
396 data->enabled = false;
397
398 /* Balance the disable that was called in closed_callback */
399 if (!data->opened)
400 android_enable(dev);
401}
402
Benoit Goby2b6862d2011-12-19 14:38:41 -0800403static struct android_usb_function adb_function = {
404 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700405 .enable = adb_android_function_enable,
406 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800407 .init = adb_function_init,
408 .cleanup = adb_function_cleanup,
409 .bind_config = adb_function_bind_config,
410};
411
Benoit Goby80ba14d2012-03-19 18:56:52 -0700412static void adb_ready_callback(void)
413{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300414 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700415 struct adb_data *data = adb_function.config;
416
Benoit Goby80ba14d2012-03-19 18:56:52 -0700417 data->opened = true;
418
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300419 if (data->enabled && dev) {
420 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700421 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300422 mutex_unlock(&dev->mutex);
423 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700424}
425
426static void adb_closed_callback(void)
427{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300428 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700429 struct adb_data *data = adb_function.config;
430
Benoit Goby80ba14d2012-03-19 18:56:52 -0700431 data->opened = false;
432
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300433 if (data->enabled) {
434 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700435 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300436 mutex_unlock(&dev->mutex);
437 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700438}
439
Benoit Goby2b6862d2011-12-19 14:38:41 -0800440
Benoit Gobyaab96812011-04-19 20:37:33 -0700441/*-------------------------------------------------------------------------*/
442/* Supported functions initialization */
443
Manu Gautam8e0719b2011-09-26 14:47:55 +0530444/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530445static int rmnet_smd_function_bind_config(struct android_usb_function *f,
446 struct usb_configuration *c)
447{
448 return rmnet_smd_bind_config(c);
449}
450
451static struct android_usb_function rmnet_smd_function = {
452 .name = "rmnet_smd",
453 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800454};
455
Manu Gautam8e0719b2011-09-26 14:47:55 +0530456/* RMNET_SDIO */
457static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
458 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800459{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530460 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800461}
462
Manu Gautam8e0719b2011-09-26 14:47:55 +0530463static struct android_usb_function rmnet_sdio_function = {
464 .name = "rmnet_sdio",
465 .bind_config = rmnet_sdio_function_bind_config,
466};
467
468/* RMNET_SMD_SDIO */
469static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
470 struct usb_composite_dev *cdev)
471{
472 return rmnet_smd_sdio_init();
473}
474
475static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
476{
477 rmnet_smd_sdio_cleanup();
478}
479
480static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
481 struct usb_configuration *c)
482{
483 return rmnet_smd_sdio_function_add(c);
484}
485
486static struct device_attribute *rmnet_smd_sdio_attributes[] = {
487 &dev_attr_transport, NULL };
488
489static struct android_usb_function rmnet_smd_sdio_function = {
490 .name = "rmnet_smd_sdio",
491 .init = rmnet_smd_sdio_function_init,
492 .cleanup = rmnet_smd_sdio_function_cleanup,
493 .bind_config = rmnet_smd_sdio_bind_config,
494 .attributes = rmnet_smd_sdio_attributes,
495};
496
Hemant Kumar1b820d52011-11-03 15:08:28 -0700497/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
498#define MAX_XPORT_STR_LEN 50
499static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530500
Manu Gautame3e897c2011-09-12 17:18:46 +0530501static void rmnet_function_cleanup(struct android_usb_function *f)
502{
503 frmnet_cleanup();
504}
505
Manu Gautam2b0234a2011-09-07 16:47:52 +0530506static int rmnet_function_bind_config(struct android_usb_function *f,
507 struct usb_configuration *c)
508{
509 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700510 int err = 0;
511 char *ctrl_name;
512 char *data_name;
513 char buf[MAX_XPORT_STR_LEN], *b;
514 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530515
Hemant Kumar1b820d52011-11-03 15:08:28 -0700516 if (!rmnet_initialized) {
517 rmnet_initialized = 1;
518 strlcpy(buf, rmnet_transports, sizeof(buf));
519 b = strim(buf);
520 while (b) {
521 ctrl_name = strsep(&b, ",");
522 data_name = strsep(&b, ",");
523 if (ctrl_name && data_name) {
524 err = frmnet_init_port(ctrl_name, data_name);
525 if (err) {
526 pr_err("rmnet: Cannot open ctrl port:"
527 "'%s' data port:'%s'\n",
528 ctrl_name, data_name);
529 goto out;
530 }
531 ports++;
532 }
533 }
534
535 err = rmnet_gport_setup();
536 if (err) {
537 pr_err("rmnet: Cannot setup transports");
538 goto out;
539 }
540 }
541
542 for (i = 0; i < ports; i++) {
543 err = frmnet_bind_config(c, i);
544 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530545 pr_err("Could not bind rmnet%u config\n", i);
546 break;
547 }
548 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700549out:
550 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530551}
552
Hemant Kumar1b820d52011-11-03 15:08:28 -0700553static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530554 struct device_attribute *attr, char *buf)
555{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700556 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530557}
558
Hemant Kumar1b820d52011-11-03 15:08:28 -0700559static ssize_t rmnet_transports_store(
560 struct device *device, struct device_attribute *attr,
561 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530562{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700563 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530564
Manu Gautam2b0234a2011-09-07 16:47:52 +0530565 return size;
566}
567
Hemant Kumar1b820d52011-11-03 15:08:28 -0700568static struct device_attribute dev_attr_rmnet_transports =
569 __ATTR(transports, S_IRUGO | S_IWUSR,
570 rmnet_transports_show,
571 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530572static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700573 &dev_attr_rmnet_transports,
574 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530575
576static struct android_usb_function rmnet_function = {
577 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530578 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530579 .bind_config = rmnet_function_bind_config,
580 .attributes = rmnet_function_attributes,
581};
582
Ofir Cohen7b155422012-07-31 13:02:49 +0300583struct ecm_function_config {
584 u8 ethaddr[ETH_ALEN];
585};
586
587static int ecm_function_init(struct android_usb_function *f,
588 struct usb_composite_dev *cdev)
589{
590 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
591 if (!f->config)
592 return -ENOMEM;
593 return 0;
594}
595
596static void ecm_function_cleanup(struct android_usb_function *f)
597{
598 kfree(f->config);
599 f->config = NULL;
600}
601
602static int ecm_qc_function_bind_config(struct android_usb_function *f,
603 struct usb_configuration *c)
604{
605 int ret;
606 struct ecm_function_config *ecm = f->config;
607
608 if (!ecm) {
609 pr_err("%s: ecm_pdata\n", __func__);
610 return -EINVAL;
611 }
612
613 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
614 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
615 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
616
617 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
618 if (ret) {
619 pr_err("%s: gether_setup failed\n", __func__);
620 return ret;
621 }
622
623 return ecm_qc_bind_config(c, ecm->ethaddr);
624}
625
626static void ecm_qc_function_unbind_config(struct android_usb_function *f,
627 struct usb_configuration *c)
628{
629 gether_qc_cleanup();
630}
631
632static ssize_t ecm_ethaddr_show(struct device *dev,
633 struct device_attribute *attr, char *buf)
634{
635 struct android_usb_function *f = dev_get_drvdata(dev);
636 struct ecm_function_config *ecm = f->config;
637 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
638 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
639 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
640}
641
642static ssize_t ecm_ethaddr_store(struct device *dev,
643 struct device_attribute *attr, const char *buf, size_t size)
644{
645 struct android_usb_function *f = dev_get_drvdata(dev);
646 struct ecm_function_config *ecm = f->config;
647
648 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
649 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
650 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
651 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
652 return size;
653 return -EINVAL;
654}
655
656static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
657 ecm_ethaddr_store);
658
659static struct device_attribute *ecm_function_attributes[] = {
660 &dev_attr_ecm_ethaddr,
661 NULL
662};
663
664static struct android_usb_function ecm_qc_function = {
665 .name = "ecm_qc",
666 .init = ecm_function_init,
667 .cleanup = ecm_function_cleanup,
668 .bind_config = ecm_qc_function_bind_config,
669 .unbind_config = ecm_qc_function_unbind_config,
670 .attributes = ecm_function_attributes,
671};
Anna Perela8c991d2012-04-09 16:44:46 +0300672
673/* MBIM - used with BAM */
674#define MAX_MBIM_INSTANCES 1
675
676static int mbim_function_init(struct android_usb_function *f,
677 struct usb_composite_dev *cdev)
678{
679 return mbim_init(MAX_MBIM_INSTANCES);
680}
681
682static void mbim_function_cleanup(struct android_usb_function *f)
683{
684 fmbim_cleanup();
685}
686
687static int mbim_function_bind_config(struct android_usb_function *f,
688 struct usb_configuration *c)
689{
690 return mbim_bind_config(c, 0);
691}
692
693static struct android_usb_function mbim_function = {
694 .name = "usb_mbim",
695 .cleanup = mbim_function_cleanup,
696 .bind_config = mbim_function_bind_config,
697 .init = mbim_function_init,
698};
699
700
Manu Gautam8e0719b2011-09-26 14:47:55 +0530701/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530702static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703static ssize_t clients_store(
704 struct device *device, struct device_attribute *attr,
705 const char *buff, size_t size)
706{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530707 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708
709 return size;
710}
711
712static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
713static struct device_attribute *diag_function_attributes[] =
714 { &dev_attr_clients, NULL };
715
716static int diag_function_init(struct android_usb_function *f,
717 struct usb_composite_dev *cdev)
718{
719 return diag_setup();
720}
721
722static void diag_function_cleanup(struct android_usb_function *f)
723{
724 diag_cleanup();
725}
726
727static int diag_function_bind_config(struct android_usb_function *f,
728 struct usb_configuration *c)
729{
730 char *name;
731 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530732 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800733 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300734 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530736 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 b = strim(buf);
738
739 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800740 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530742 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300743 if (dev->pdata && !once++)
744 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530747 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 if (err)
749 pr_err("diag: Cannot open channel '%s'", name);
750 }
751 }
752
753 return err;
754}
755
756static struct android_usb_function diag_function = {
757 .name = "diag",
758 .init = diag_function_init,
759 .cleanup = diag_function_cleanup,
760 .bind_config = diag_function_bind_config,
761 .attributes = diag_function_attributes,
762};
763
Manu Gautam8e0719b2011-09-26 14:47:55 +0530764/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530765static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530766static ssize_t serial_transports_store(
767 struct device *device, struct device_attribute *attr,
768 const char *buff, size_t size)
769{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530770 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530771
772 return size;
773}
774
775static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
776static struct device_attribute *serial_function_attributes[] =
777 { &dev_attr_transports, NULL };
778
779static void serial_function_cleanup(struct android_usb_function *f)
780{
781 gserial_cleanup();
782}
783
784static int serial_function_bind_config(struct android_usb_function *f,
785 struct usb_configuration *c)
786{
787 char *name;
788 char buf[32], *b;
789 int err = -1, i;
790 static int serial_initialized = 0, ports = 0;
791
792 if (serial_initialized)
793 goto bind_config;
794
795 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530796 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530797 b = strim(buf);
798
799 while (b) {
800 name = strsep(&b, ",");
801
802 if (name) {
803 err = gserial_init_port(ports, name);
804 if (err) {
805 pr_err("serial: Cannot open port '%s'", name);
806 goto out;
807 }
808 ports++;
809 }
810 }
811 err = gport_setup(c);
812 if (err) {
813 pr_err("serial: Cannot setup transports");
814 goto out;
815 }
816
817bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200818 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530819 err = gser_bind_config(c, i);
820 if (err) {
821 pr_err("serial: bind_config failed for port %d", i);
822 goto out;
823 }
824 }
825
826out:
827 return err;
828}
829
830static struct android_usb_function serial_function = {
831 .name = "serial",
832 .cleanup = serial_function_cleanup,
833 .bind_config = serial_function_bind_config,
834 .attributes = serial_function_attributes,
835};
836
Anji jonnala92be1b42011-12-19 09:44:41 +0530837/* ACM */
838static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
839static ssize_t acm_transports_store(
840 struct device *device, struct device_attribute *attr,
841 const char *buff, size_t size)
842{
843 strlcpy(acm_transports, buff, sizeof(acm_transports));
844
845 return size;
846}
847
848static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
849static struct device_attribute *acm_function_attributes[] = {
850 &dev_attr_acm_transports, NULL };
851
Benoit Goby1e8ce152011-12-12 13:01:23 -0800852static void acm_function_cleanup(struct android_usb_function *f)
853{
854 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800855}
856
Anji jonnala92be1b42011-12-19 09:44:41 +0530857static int acm_function_bind_config(struct android_usb_function *f,
858 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800859{
Anji jonnala92be1b42011-12-19 09:44:41 +0530860 char *name;
861 char buf[32], *b;
862 int err = -1, i;
863 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800864
Anji jonnala92be1b42011-12-19 09:44:41 +0530865 if (acm_initialized)
866 goto bind_config;
867
868 acm_initialized = 1;
869 strlcpy(buf, acm_transports, sizeof(buf));
870 b = strim(buf);
871
872 while (b) {
873 name = strsep(&b, ",");
874
875 if (name) {
876 err = acm_init_port(ports, name);
877 if (err) {
878 pr_err("acm: Cannot open port '%s'", name);
879 goto out;
880 }
881 ports++;
882 }
883 }
884 err = acm_port_setup(c);
885 if (err) {
886 pr_err("acm: Cannot setup transports");
887 goto out;
888 }
889
890bind_config:
891 for (i = 0; i < ports; i++) {
892 err = acm_bind_config(c, i);
893 if (err) {
894 pr_err("acm: bind_config failed for port %d", i);
895 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800896 }
897 }
898
Anji jonnala92be1b42011-12-19 09:44:41 +0530899out:
900 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800901}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800902static struct android_usb_function acm_function = {
903 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800904 .cleanup = acm_function_cleanup,
905 .bind_config = acm_function_bind_config,
906 .attributes = acm_function_attributes,
907};
908
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530909/* CCID */
910static int ccid_function_init(struct android_usb_function *f,
911 struct usb_composite_dev *cdev)
912{
913 return ccid_setup();
914}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800915
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530916static void ccid_function_cleanup(struct android_usb_function *f)
917{
918 ccid_cleanup();
919}
920
921static int ccid_function_bind_config(struct android_usb_function *f,
922 struct usb_configuration *c)
923{
924 return ccid_bind_config(c);
925}
926
927static struct android_usb_function ccid_function = {
928 .name = "ccid",
929 .init = ccid_function_init,
930 .cleanup = ccid_function_cleanup,
931 .bind_config = ccid_function_bind_config,
932};
933
Steve Mucklef132c6c2012-06-06 18:30:57 -0700934static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800935 struct usb_composite_dev *cdev)
936{
937 return mtp_setup();
938}
939
940static void mtp_function_cleanup(struct android_usb_function *f)
941{
942 mtp_cleanup();
943}
944
Steve Mucklef132c6c2012-06-06 18:30:57 -0700945static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800946 struct usb_configuration *c)
947{
948 return mtp_bind_config(c, false);
949}
950
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400951static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800952{
953 /* nothing to do - initialization is handled by mtp_function_init */
954 return 0;
955}
956
957static void ptp_function_cleanup(struct android_usb_function *f)
958{
959 /* nothing to do - cleanup is handled by mtp_function_cleanup */
960}
961
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400962static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800963{
964 return mtp_bind_config(c, true);
965}
966
967static int mtp_function_ctrlrequest(struct android_usb_function *f,
968 struct usb_composite_dev *cdev,
969 const struct usb_ctrlrequest *c)
970{
971 return mtp_ctrlrequest(cdev, c);
972}
973
974static struct android_usb_function mtp_function = {
975 .name = "mtp",
976 .init = mtp_function_init,
977 .cleanup = mtp_function_cleanup,
978 .bind_config = mtp_function_bind_config,
979 .ctrlrequest = mtp_function_ctrlrequest,
980};
981
982/* PTP function is same as MTP with slightly different interface descriptor */
983static struct android_usb_function ptp_function = {
984 .name = "ptp",
985 .init = ptp_function_init,
986 .cleanup = ptp_function_cleanup,
987 .bind_config = ptp_function_bind_config,
988};
989
990
Benoit Goby1e8ce152011-12-12 13:01:23 -0800991struct rndis_function_config {
992 u8 ethaddr[ETH_ALEN];
993 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200994 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800995 char manufacturer[256];
996 /* "Wireless" RNDIS; auto-detected by Windows */
997 bool wceis;
998};
999
1000static int
1001rndis_function_init(struct android_usb_function *f,
1002 struct usb_composite_dev *cdev)
1003{
1004 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1005 if (!f->config)
1006 return -ENOMEM;
1007 return 0;
1008}
1009
1010static void rndis_function_cleanup(struct android_usb_function *f)
1011{
1012 kfree(f->config);
1013 f->config = NULL;
1014}
1015
Ofir Cohenaef90b72012-07-31 12:37:04 +02001016static int rndis_qc_function_init(struct android_usb_function *f,
1017 struct usb_composite_dev *cdev)
1018{
1019 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1020 if (!f->config)
1021 return -ENOMEM;
1022
1023 return rndis_qc_init();
1024}
1025
1026static void rndis_qc_function_cleanup(struct android_usb_function *f)
1027{
1028 rndis_qc_cleanup();
1029 kfree(f->config);
1030}
1031
Benoit Goby1e8ce152011-12-12 13:01:23 -08001032static int
1033rndis_function_bind_config(struct android_usb_function *f,
1034 struct usb_configuration *c)
1035{
1036 int ret;
1037 struct rndis_function_config *rndis = f->config;
1038
1039 if (!rndis) {
1040 pr_err("%s: rndis_pdata\n", __func__);
1041 return -1;
1042 }
1043
1044 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1045 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1046 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1047
1048 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1049 if (ret) {
1050 pr_err("%s: gether_setup failed\n", __func__);
1051 return ret;
1052 }
1053
1054 if (rndis->wceis) {
1055 /* "Wireless" RNDIS; auto-detected by Windows */
1056 rndis_iad_descriptor.bFunctionClass =
1057 USB_CLASS_WIRELESS_CONTROLLER;
1058 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1059 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1060 rndis_control_intf.bInterfaceClass =
1061 USB_CLASS_WIRELESS_CONTROLLER;
1062 rndis_control_intf.bInterfaceSubClass = 0x01;
1063 rndis_control_intf.bInterfaceProtocol = 0x03;
1064 }
1065
1066 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1067 rndis->manufacturer);
1068}
1069
Ofir Cohenaef90b72012-07-31 12:37:04 +02001070static int rndis_qc_function_bind_config(struct android_usb_function *f,
1071 struct usb_configuration *c)
1072{
1073 int ret;
1074 struct rndis_function_config *rndis = f->config;
1075
1076 if (!rndis) {
1077 pr_err("%s: rndis_pdata\n", __func__);
1078 return -EINVAL;
1079 }
1080
1081 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1082 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1083 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1084
1085 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1086 if (ret) {
1087 pr_err("%s: gether_setup failed\n", __func__);
1088 return ret;
1089 }
1090
1091 if (rndis->wceis) {
1092 /* "Wireless" RNDIS; auto-detected by Windows */
1093 rndis_qc_iad_descriptor.bFunctionClass =
1094 USB_CLASS_WIRELESS_CONTROLLER;
1095 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1096 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1097 rndis_qc_control_intf.bInterfaceClass =
1098 USB_CLASS_WIRELESS_CONTROLLER;
1099 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1100 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1101 }
1102
1103 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1104 rndis->manufacturer,
1105 rndis->max_pkt_per_xfer);
1106}
1107
Benoit Goby1e8ce152011-12-12 13:01:23 -08001108static void rndis_function_unbind_config(struct android_usb_function *f,
1109 struct usb_configuration *c)
1110{
1111 gether_cleanup();
1112}
1113
Ofir Cohenaef90b72012-07-31 12:37:04 +02001114static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1115 struct usb_configuration *c)
1116{
1117 gether_qc_cleanup();
1118}
1119
Benoit Goby1e8ce152011-12-12 13:01:23 -08001120static ssize_t rndis_manufacturer_show(struct device *dev,
1121 struct device_attribute *attr, char *buf)
1122{
1123 struct android_usb_function *f = dev_get_drvdata(dev);
1124 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001125
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301126 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001127}
1128
1129static ssize_t rndis_manufacturer_store(struct device *dev,
1130 struct device_attribute *attr, const char *buf, size_t size)
1131{
1132 struct android_usb_function *f = dev_get_drvdata(dev);
1133 struct rndis_function_config *config = f->config;
1134
1135 if (size >= sizeof(config->manufacturer))
1136 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001137
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301138 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001139 return size;
1140 return -1;
1141}
1142
1143static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1144 rndis_manufacturer_store);
1145
1146static ssize_t rndis_wceis_show(struct device *dev,
1147 struct device_attribute *attr, char *buf)
1148{
1149 struct android_usb_function *f = dev_get_drvdata(dev);
1150 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001151
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301152 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001153}
1154
1155static ssize_t rndis_wceis_store(struct device *dev,
1156 struct device_attribute *attr, const char *buf, size_t size)
1157{
1158 struct android_usb_function *f = dev_get_drvdata(dev);
1159 struct rndis_function_config *config = f->config;
1160 int value;
1161
1162 if (sscanf(buf, "%d", &value) == 1) {
1163 config->wceis = value;
1164 return size;
1165 }
1166 return -EINVAL;
1167}
1168
1169static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1170 rndis_wceis_store);
1171
1172static ssize_t rndis_ethaddr_show(struct device *dev,
1173 struct device_attribute *attr, char *buf)
1174{
1175 struct android_usb_function *f = dev_get_drvdata(dev);
1176 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001177
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301178 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001179 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1180 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1181}
1182
1183static ssize_t rndis_ethaddr_store(struct device *dev,
1184 struct device_attribute *attr, const char *buf, size_t size)
1185{
1186 struct android_usb_function *f = dev_get_drvdata(dev);
1187 struct rndis_function_config *rndis = f->config;
1188
1189 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1190 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1191 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1192 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1193 return size;
1194 return -EINVAL;
1195}
1196
1197static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1198 rndis_ethaddr_store);
1199
1200static ssize_t rndis_vendorID_show(struct device *dev,
1201 struct device_attribute *attr, char *buf)
1202{
1203 struct android_usb_function *f = dev_get_drvdata(dev);
1204 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001205
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301206 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001207}
1208
1209static ssize_t rndis_vendorID_store(struct device *dev,
1210 struct device_attribute *attr, const char *buf, size_t size)
1211{
1212 struct android_usb_function *f = dev_get_drvdata(dev);
1213 struct rndis_function_config *config = f->config;
1214 int value;
1215
1216 if (sscanf(buf, "%04x", &value) == 1) {
1217 config->vendorID = value;
1218 return size;
1219 }
1220 return -EINVAL;
1221}
1222
1223static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1224 rndis_vendorID_store);
1225
Ofir Cohenaef90b72012-07-31 12:37:04 +02001226static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1227 struct device_attribute *attr, char *buf)
1228{
1229 struct android_usb_function *f = dev_get_drvdata(dev);
1230 struct rndis_function_config *config = f->config;
1231 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1232}
1233
1234static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1235 struct device_attribute *attr, const char *buf, size_t size)
1236{
1237 struct android_usb_function *f = dev_get_drvdata(dev);
1238 struct rndis_function_config *config = f->config;
1239 int value;
1240
1241 if (sscanf(buf, "%d", &value) == 1) {
1242 config->max_pkt_per_xfer = value;
1243 return size;
1244 }
1245 return -EINVAL;
1246}
1247
1248static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1249 rndis_max_pkt_per_xfer_show,
1250 rndis_max_pkt_per_xfer_store);
1251
Benoit Goby1e8ce152011-12-12 13:01:23 -08001252static struct device_attribute *rndis_function_attributes[] = {
1253 &dev_attr_manufacturer,
1254 &dev_attr_wceis,
1255 &dev_attr_ethaddr,
1256 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001257 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001258 NULL
1259};
1260
1261static struct android_usb_function rndis_function = {
1262 .name = "rndis",
1263 .init = rndis_function_init,
1264 .cleanup = rndis_function_cleanup,
1265 .bind_config = rndis_function_bind_config,
1266 .unbind_config = rndis_function_unbind_config,
1267 .attributes = rndis_function_attributes,
1268};
1269
Ofir Cohenaef90b72012-07-31 12:37:04 +02001270static struct android_usb_function rndis_qc_function = {
1271 .name = "rndis_qc",
1272 .init = rndis_qc_function_init,
1273 .cleanup = rndis_qc_function_cleanup,
1274 .bind_config = rndis_qc_function_bind_config,
1275 .unbind_config = rndis_qc_function_unbind_config,
1276 .attributes = rndis_function_attributes,
1277};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001278
1279struct mass_storage_function_config {
1280 struct fsg_config fsg;
1281 struct fsg_common *common;
1282};
1283
1284static int mass_storage_function_init(struct android_usb_function *f,
1285 struct usb_composite_dev *cdev)
1286{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001287 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001288 struct mass_storage_function_config *config;
1289 struct fsg_common *common;
1290 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301291 int i;
1292 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001293
1294 config = kzalloc(sizeof(struct mass_storage_function_config),
1295 GFP_KERNEL);
1296 if (!config)
1297 return -ENOMEM;
1298
1299 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301300 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301301 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301302 config->fsg.nluns = 2;
1303 config->fsg.luns[1].cdrom = 1;
1304 config->fsg.luns[1].ro = 1;
1305 config->fsg.luns[1].removable = 1;
1306 name[1] = "lun0";
1307 }
1308
Benoit Goby1e8ce152011-12-12 13:01:23 -08001309 config->fsg.luns[0].removable = 1;
1310
1311 common = fsg_common_init(NULL, cdev, &config->fsg);
1312 if (IS_ERR(common)) {
1313 kfree(config);
1314 return PTR_ERR(common);
1315 }
1316
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301317 for (i = 0; i < config->fsg.nluns; i++) {
1318 err = sysfs_create_link(&f->dev->kobj,
1319 &common->luns[i].dev.kobj,
1320 name[i]);
1321 if (err)
1322 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001323 }
1324
1325 config->common = common;
1326 f->config = config;
1327 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301328error:
1329 for (; i > 0 ; i--)
1330 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1331
1332 fsg_common_release(&common->ref);
1333 kfree(config);
1334 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001335}
1336
1337static void mass_storage_function_cleanup(struct android_usb_function *f)
1338{
1339 kfree(f->config);
1340 f->config = NULL;
1341}
1342
1343static int mass_storage_function_bind_config(struct android_usb_function *f,
1344 struct usb_configuration *c)
1345{
1346 struct mass_storage_function_config *config = f->config;
1347 return fsg_bind_config(c->cdev, c, config->common);
1348}
1349
1350static ssize_t mass_storage_inquiry_show(struct device *dev,
1351 struct device_attribute *attr, char *buf)
1352{
1353 struct android_usb_function *f = dev_get_drvdata(dev);
1354 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301355 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001356}
1357
1358static ssize_t mass_storage_inquiry_store(struct device *dev,
1359 struct device_attribute *attr, const char *buf, size_t size)
1360{
1361 struct android_usb_function *f = dev_get_drvdata(dev);
1362 struct mass_storage_function_config *config = f->config;
1363 if (size >= sizeof(config->common->inquiry_string))
1364 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301365 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001366 return -EINVAL;
1367 return size;
1368}
1369
1370static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1371 mass_storage_inquiry_show,
1372 mass_storage_inquiry_store);
1373
1374static struct device_attribute *mass_storage_function_attributes[] = {
1375 &dev_attr_inquiry_string,
1376 NULL
1377};
1378
1379static struct android_usb_function mass_storage_function = {
1380 .name = "mass_storage",
1381 .init = mass_storage_function_init,
1382 .cleanup = mass_storage_function_cleanup,
1383 .bind_config = mass_storage_function_bind_config,
1384 .attributes = mass_storage_function_attributes,
1385};
1386
1387
Benoit Gobycf3fc062011-12-19 14:39:37 -08001388static int accessory_function_init(struct android_usb_function *f,
1389 struct usb_composite_dev *cdev)
1390{
1391 return acc_setup();
1392}
1393
1394static void accessory_function_cleanup(struct android_usb_function *f)
1395{
1396 acc_cleanup();
1397}
1398
1399static int accessory_function_bind_config(struct android_usb_function *f,
1400 struct usb_configuration *c)
1401{
1402 return acc_bind_config(c);
1403}
1404
1405static int accessory_function_ctrlrequest(struct android_usb_function *f,
1406 struct usb_composite_dev *cdev,
1407 const struct usb_ctrlrequest *c)
1408{
1409 return acc_ctrlrequest(cdev, c);
1410}
1411
1412static struct android_usb_function accessory_function = {
1413 .name = "accessory",
1414 .init = accessory_function_init,
1415 .cleanup = accessory_function_cleanup,
1416 .bind_config = accessory_function_bind_config,
1417 .ctrlrequest = accessory_function_ctrlrequest,
1418};
1419
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301420static int android_uasp_connect_cb(bool connect)
1421{
1422 /*
1423 * TODO
1424 * We may have to disable gadget till UASP configfs nodes
1425 * are configured which includes mapping LUN with the
1426 * backing file. It is a fundamental difference between
1427 * f_mass_storage and f_tcp. That means UASP can not be
1428 * in default composition.
1429 *
1430 * For now, assume that UASP configfs nodes are configured
1431 * before enabling android gadget. Or cable should be
1432 * reconnected after mapping the LUN.
1433 *
1434 * Also consider making UASP to respond to Host requests when
1435 * Lun is not mapped.
1436 */
1437 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1438
1439 return 0;
1440}
1441
1442static int uasp_function_init(struct android_usb_function *f,
1443 struct usb_composite_dev *cdev)
1444{
1445 return f_tcm_init(&android_uasp_connect_cb);
1446}
1447
1448static void uasp_function_cleanup(struct android_usb_function *f)
1449{
1450 f_tcm_exit();
1451}
1452
1453static int uasp_function_bind_config(struct android_usb_function *f,
1454 struct usb_configuration *c)
1455{
1456 return tcm_bind_config(c);
1457}
1458
1459static struct android_usb_function uasp_function = {
1460 .name = "uasp",
1461 .init = uasp_function_init,
1462 .cleanup = uasp_function_cleanup,
1463 .bind_config = uasp_function_bind_config,
1464};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001465
Benoit Goby1e8ce152011-12-12 13:01:23 -08001466static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001467 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001468 &ecm_qc_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301469 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301470 &rmnet_sdio_function,
1471 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301472 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001473 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301474 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001475 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301476 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001477 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001478 &mtp_function,
1479 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001480 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001481 &rndis_qc_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001482 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001483 &accessory_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301484 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001485 NULL
1486};
1487
Lena Salmand092f2d2012-03-12 17:27:24 +02001488static void android_cleanup_functions(struct android_usb_function **functions)
1489{
1490 struct android_usb_function *f;
1491 struct device_attribute **attrs;
1492 struct device_attribute *attr;
1493
1494 while (*functions) {
1495 f = *functions++;
1496
1497 if (f->dev) {
1498 device_destroy(android_class, f->dev->devt);
1499 kfree(f->dev_name);
1500 } else
1501 continue;
1502
1503 if (f->cleanup)
1504 f->cleanup(f);
1505
1506 attrs = f->attributes;
1507 if (attrs) {
1508 while ((attr = *attrs++))
1509 device_remove_file(f->dev, attr);
1510 }
1511 }
1512}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001513
1514static int android_init_functions(struct android_usb_function **functions,
1515 struct usb_composite_dev *cdev)
1516{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001517 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001518 struct android_usb_function *f;
1519 struct device_attribute **attrs;
1520 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301521 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001522 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001523
1524 for (; (f = *functions++); index++) {
1525 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001526 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001527 if (!f->dev_name) {
1528 err = -ENOMEM;
1529 goto err_out;
1530 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001531 f->dev = device_create(android_class, dev->dev,
1532 MKDEV(0, index), f, f->dev_name);
1533 if (IS_ERR(f->dev)) {
1534 pr_err("%s: Failed to create dev %s", __func__,
1535 f->dev_name);
1536 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001537 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001538 goto err_create;
1539 }
1540
1541 if (f->init) {
1542 err = f->init(f, cdev);
1543 if (err) {
1544 pr_err("%s: Failed to init %s", __func__,
1545 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001546 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001547 }
1548 }
1549
1550 attrs = f->attributes;
1551 if (attrs) {
1552 while ((attr = *attrs++) && !err)
1553 err = device_create_file(f->dev, attr);
1554 }
1555 if (err) {
1556 pr_err("%s: Failed to create function %s attributes",
1557 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001558 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001559 }
1560 }
1561 return 0;
1562
Lena Salmand092f2d2012-03-12 17:27:24 +02001563err_attrs:
1564 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1565 device_remove_file(f->dev, attr);
1566 if (f->cleanup)
1567 f->cleanup(f);
1568err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001569 device_destroy(android_class, f->dev->devt);
1570err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001571 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001572 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001573err_out:
1574 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001575 return err;
1576}
1577
Benoit Goby1e8ce152011-12-12 13:01:23 -08001578static int
1579android_bind_enabled_functions(struct android_dev *dev,
1580 struct usb_configuration *c)
1581{
1582 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001583 struct android_configuration *conf =
1584 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001585 int ret;
1586
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001587 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001588 ret = f->bind_config(f, c);
1589 if (ret) {
1590 pr_err("%s: %s failed", __func__, f->name);
1591 return ret;
1592 }
1593 }
1594 return 0;
1595}
1596
1597static void
1598android_unbind_enabled_functions(struct android_dev *dev,
1599 struct usb_configuration *c)
1600{
1601 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001602 struct android_configuration *conf =
1603 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001604
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001605 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001606 if (f->unbind_config)
1607 f->unbind_config(f, c);
1608 }
1609}
1610
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001611static int android_enable_function(struct android_dev *dev,
1612 struct android_configuration *conf,
1613 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001614{
1615 struct android_usb_function **functions = dev->functions;
1616 struct android_usb_function *f;
1617 while ((f = *functions++)) {
1618 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001619 if (f->android_dev)
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001620 pr_err("%s already enabled in other " \
1621 "configuration or device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001622 f->name);
1623 else {
1624 list_add_tail(&f->enabled_list,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001625 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001626 f->android_dev = dev;
1627 return 0;
1628 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001629 }
1630 }
1631 return -EINVAL;
1632}
1633
1634/*-------------------------------------------------------------------------*/
1635/* /sys/class/android_usb/android%d/ interface */
1636
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301637static ssize_t remote_wakeup_show(struct device *pdev,
1638 struct device_attribute *attr, char *buf)
1639{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001640 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001641 struct android_configuration *conf;
1642
1643 /*
1644 * Show the wakeup attribute of the first configuration,
1645 * since all configurations have the same wakeup attribute
1646 */
1647 if (dev->configs_num == 0)
1648 return 0;
1649 conf = list_entry(dev->configs.next,
1650 struct android_configuration,
1651 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001652
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301653 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001654 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301655 USB_CONFIG_ATT_WAKEUP));
1656}
1657
1658static ssize_t remote_wakeup_store(struct device *pdev,
1659 struct device_attribute *attr, const char *buff, size_t size)
1660{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001661 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001662 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301663 int enable = 0;
1664
1665 sscanf(buff, "%d", &enable);
1666
1667 pr_debug("android_usb: %s remote wakeup\n",
1668 enable ? "enabling" : "disabling");
1669
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001670 list_for_each_entry(conf, &dev->configs, list_item)
1671 if (enable)
1672 conf->usb_config.bmAttributes |=
1673 USB_CONFIG_ATT_WAKEUP;
1674 else
1675 conf->usb_config.bmAttributes &=
1676 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301677
1678 return size;
1679}
1680
Benoit Goby1e8ce152011-12-12 13:01:23 -08001681static ssize_t
1682functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1683{
1684 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001685 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001686 struct android_usb_function *f;
1687 char *buff = buf;
1688
1689 mutex_lock(&dev->mutex);
1690
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001691 list_for_each_entry(conf, &dev->configs, list_item) {
1692 if (buff != buf)
1693 *(buff-1) = ':';
1694 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1695 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
1696 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001697
1698 mutex_unlock(&dev->mutex);
1699
1700 if (buff != buf)
1701 *(buff-1) = '\n';
1702 return buff - buf;
1703}
1704
1705static ssize_t
1706functions_store(struct device *pdev, struct device_attribute *attr,
1707 const char *buff, size_t size)
1708{
1709 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001710 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001711 struct list_head *curr_conf = &dev->configs;
1712 struct android_configuration *conf;
1713 char *conf_str;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001714 char *name;
1715 char buf[256], *b;
1716 int err;
1717
1718 mutex_lock(&dev->mutex);
1719
1720 if (dev->enabled) {
1721 mutex_unlock(&dev->mutex);
1722 return -EBUSY;
1723 }
1724
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001725 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001726 list_for_each_entry(conf, &dev->configs, list_item) {
1727 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1728 f->android_dev = NULL;
1729 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001730 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001731
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301732 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001733 b = strim(buf);
1734
1735 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001736 conf_str = strsep(&b, ":");
1737 if (conf_str) {
1738 /* If the next not equal to the head, take it */
1739 if (curr_conf->next != &dev->configs)
1740 conf = list_entry(curr_conf->next,
1741 struct android_configuration,
1742 list_item);
1743 else
1744 conf = alloc_android_config(dev);
1745
1746 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001747 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001748
1749 while (conf_str) {
1750 name = strsep(&conf_str, ",");
1751 if (name) {
1752 err = android_enable_function(dev, conf, name);
1753 if (err)
1754 pr_err("android_usb: Cannot enable %s",
1755 name);
1756 }
1757 }
1758 }
1759
1760 /* Free uneeded configurations if exists */
1761 while (curr_conf->next != &dev->configs) {
1762 conf = list_entry(curr_conf->next,
1763 struct android_configuration, list_item);
1764 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001765 }
1766
1767 mutex_unlock(&dev->mutex);
1768
1769 return size;
1770}
1771
1772static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1773 char *buf)
1774{
1775 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001776
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301777 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001778}
1779
1780static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1781 const char *buff, size_t size)
1782{
1783 struct android_dev *dev = dev_get_drvdata(pdev);
1784 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001785 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001786 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001787 int enabled = 0;
1788
Benoit Gobycf3fc062011-12-19 14:39:37 -08001789 if (!cdev)
1790 return -ENODEV;
1791
Benoit Goby1e8ce152011-12-12 13:01:23 -08001792 mutex_lock(&dev->mutex);
1793
1794 sscanf(buff, "%d", &enabled);
1795 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001796 /*
1797 * Update values in composite driver's copy of
1798 * device descriptor.
1799 */
1800 cdev->desc.idVendor = device_desc.idVendor;
1801 cdev->desc.idProduct = device_desc.idProduct;
1802 cdev->desc.bcdDevice = device_desc.bcdDevice;
1803 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1804 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1805 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001806 list_for_each_entry(conf, &dev->configs, list_item)
1807 list_for_each_entry(f, &conf->enabled_functions,
1808 enabled_list) {
1809 if (f->enable)
1810 f->enable(f);
1811 }
Benoit Goby80ba14d2012-03-19 18:56:52 -07001812 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001813 dev->enabled = true;
1814 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001815 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001816 list_for_each_entry(conf, &dev->configs, list_item)
1817 list_for_each_entry(f, &conf->enabled_functions,
1818 enabled_list) {
1819 if (f->disable)
1820 f->disable(f);
1821 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001822 dev->enabled = false;
1823 } else {
1824 pr_err("android_usb: already %s\n",
1825 dev->enabled ? "enabled" : "disabled");
1826 }
1827
1828 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001829
Benoit Gobyaab96812011-04-19 20:37:33 -07001830 return size;
1831}
1832
Ofir Cohen94213a72012-05-03 14:26:32 +03001833static ssize_t pm_qos_show(struct device *pdev,
1834 struct device_attribute *attr, char *buf)
1835{
1836 struct android_dev *dev = dev_get_drvdata(pdev);
1837
1838 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1839}
1840
1841static ssize_t pm_qos_store(struct device *pdev,
1842 struct device_attribute *attr,
1843 const char *buff, size_t size)
1844{
1845 struct android_dev *dev = dev_get_drvdata(pdev);
1846
1847 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1848
Benoit Goby1e8ce152011-12-12 13:01:23 -08001849 return size;
1850}
1851
1852static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1853 char *buf)
1854{
1855 struct android_dev *dev = dev_get_drvdata(pdev);
1856 struct usb_composite_dev *cdev = dev->cdev;
1857 char *state = "DISCONNECTED";
1858 unsigned long flags;
1859
1860 if (!cdev)
1861 goto out;
1862
1863 spin_lock_irqsave(&cdev->lock, flags);
1864 if (cdev->config)
1865 state = "CONFIGURED";
1866 else if (dev->connected)
1867 state = "CONNECTED";
1868 spin_unlock_irqrestore(&cdev->lock, flags);
1869out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301870 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001871}
1872
1873#define DESCRIPTOR_ATTR(field, format_string) \
1874static ssize_t \
1875field ## _show(struct device *dev, struct device_attribute *attr, \
1876 char *buf) \
1877{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301878 return snprintf(buf, PAGE_SIZE, \
1879 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001880} \
1881static ssize_t \
1882field ## _store(struct device *dev, struct device_attribute *attr, \
1883 const char *buf, size_t size) \
1884{ \
1885 int value; \
1886 if (sscanf(buf, format_string, &value) == 1) { \
1887 device_desc.field = value; \
1888 return size; \
1889 } \
1890 return -1; \
1891} \
1892static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1893
1894#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1895static ssize_t \
1896field ## _show(struct device *dev, struct device_attribute *attr, \
1897 char *buf) \
1898{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301899 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001900} \
1901static ssize_t \
1902field ## _store(struct device *dev, struct device_attribute *attr, \
1903 const char *buf, size_t size) \
1904{ \
1905 if (size >= sizeof(buffer)) \
1906 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05301907 strlcpy(buffer, buf, sizeof(buffer)); \
1908 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05301909 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001910} \
1911static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1912
1913
1914DESCRIPTOR_ATTR(idVendor, "%04x\n")
1915DESCRIPTOR_ATTR(idProduct, "%04x\n")
1916DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1917DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1918DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1919DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1920DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1921DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1922DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1923
1924static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1925 functions_store);
1926static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001927static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1928 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001929static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301930static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1931 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001932
1933static struct device_attribute *android_usb_attributes[] = {
1934 &dev_attr_idVendor,
1935 &dev_attr_idProduct,
1936 &dev_attr_bcdDevice,
1937 &dev_attr_bDeviceClass,
1938 &dev_attr_bDeviceSubClass,
1939 &dev_attr_bDeviceProtocol,
1940 &dev_attr_iManufacturer,
1941 &dev_attr_iProduct,
1942 &dev_attr_iSerial,
1943 &dev_attr_functions,
1944 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001945 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001946 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301947 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001948 NULL
1949};
1950
1951/*-------------------------------------------------------------------------*/
1952/* Composite driver */
1953
1954static int android_bind_config(struct usb_configuration *c)
1955{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001956 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001957 int ret = 0;
1958
1959 ret = android_bind_enabled_functions(dev, c);
1960 if (ret)
1961 return ret;
1962
1963 return 0;
1964}
1965
1966static void android_unbind_config(struct usb_configuration *c)
1967{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001968 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001969
1970 android_unbind_enabled_functions(dev, c);
1971}
1972
1973static int android_bind(struct usb_composite_dev *cdev)
1974{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001975 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001976 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001977 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001978 int gcnum, id, ret;
1979
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001980 /* Bind to the last android_dev that was probed */
1981 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
1982
1983 dev->cdev = cdev;
1984
Benoit Goby1e8ce152011-12-12 13:01:23 -08001985 /*
1986 * Start disconnected. Userspace will connect the gadget once
1987 * it is done configuring the functions.
1988 */
1989 usb_gadget_disconnect(gadget);
1990
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001991 /* Init the supported functions only once, on the first android_dev */
1992 if (android_dev_count == 1) {
1993 ret = android_init_functions(dev->functions, cdev);
1994 if (ret)
1995 return ret;
1996 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001997
1998 /* Allocate string descriptor numbers ... note that string
1999 * contents can be overridden by the composite_dev glue.
2000 */
2001 id = usb_string_id(cdev);
2002 if (id < 0)
2003 return id;
2004 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2005 device_desc.iManufacturer = id;
2006
2007 id = usb_string_id(cdev);
2008 if (id < 0)
2009 return id;
2010 strings_dev[STRING_PRODUCT_IDX].id = id;
2011 device_desc.iProduct = id;
2012
2013 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302014 strlcpy(manufacturer_string, "Android",
2015 sizeof(manufacturer_string) - 1);
2016 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2017 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002018
2019 id = usb_string_id(cdev);
2020 if (id < 0)
2021 return id;
2022 strings_dev[STRING_SERIAL_IDX].id = id;
2023 device_desc.iSerialNumber = id;
2024
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302025 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002026 list_for_each_entry(conf, &dev->configs, list_item)
2027 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302028
Benoit Goby1e8ce152011-12-12 13:01:23 -08002029 gcnum = usb_gadget_controller_number(gadget);
2030 if (gcnum >= 0)
2031 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2032 else {
2033 pr_warning("%s: controller '%s' not recognized\n",
2034 longname, gadget->name);
2035 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2036 }
2037
Benoit Goby1e8ce152011-12-12 13:01:23 -08002038 return 0;
2039}
2040
2041static int android_usb_unbind(struct usb_composite_dev *cdev)
2042{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002043 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002044
Lena Salmand092f2d2012-03-12 17:27:24 +02002045 manufacturer_string[0] = '\0';
2046 product_string[0] = '\0';
2047 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002048 cancel_work_sync(&dev->work);
2049 android_cleanup_functions(dev->functions);
2050 return 0;
2051}
2052
2053static struct usb_composite_driver android_usb_driver = {
2054 .name = "android_usb",
2055 .dev = &device_desc,
2056 .strings = dev_strings,
2057 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002058 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002059};
2060
2061static int
2062android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2063{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002064 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002065 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002066 struct usb_request *req = cdev->req;
2067 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002068 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002069 int value = -EOPNOTSUPP;
2070 unsigned long flags;
2071
2072 req->zero = 0;
2073 req->complete = composite_setup_complete;
2074 req->length = 0;
2075 gadget->ep0->driver_data = cdev;
2076
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002077 list_for_each_entry(conf, &dev->configs, list_item)
2078 if (&conf->usb_config == cdev->config)
2079 list_for_each_entry(f,
2080 &conf->enabled_functions,
2081 enabled_list)
2082 if (f->ctrlrequest) {
2083 value = f->ctrlrequest(f, cdev, c);
2084 if (value >= 0)
2085 break;
2086 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002087
Benoit Gobycf3fc062011-12-19 14:39:37 -08002088 /* Special case the accessory function.
2089 * It needs to handle control requests before it is enabled.
2090 */
2091 if (value < 0)
2092 value = acc_ctrlrequest(cdev, c);
2093
Benoit Goby1e8ce152011-12-12 13:01:23 -08002094 if (value < 0)
2095 value = composite_setup(gadget, c);
2096
2097 spin_lock_irqsave(&cdev->lock, flags);
2098 if (!dev->connected) {
2099 dev->connected = 1;
2100 schedule_work(&dev->work);
2101 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2102 cdev->config) {
2103 schedule_work(&dev->work);
2104 }
2105 spin_unlock_irqrestore(&cdev->lock, flags);
2106
2107 return value;
2108}
2109
2110static void android_disconnect(struct usb_gadget *gadget)
2111{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002112 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002113 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002114 unsigned long flags;
2115
2116 composite_disconnect(gadget);
2117
2118 spin_lock_irqsave(&cdev->lock, flags);
2119 dev->connected = 0;
2120 schedule_work(&dev->work);
2121 spin_unlock_irqrestore(&cdev->lock, flags);
2122}
2123
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002124static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002125{
2126 struct device_attribute **attrs = android_usb_attributes;
2127 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002128 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002129 int err;
2130
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002131 /*
2132 * The primary usb core should always have usb_core_id=0, since
2133 * Android user space is currently interested in android0 events.
2134 */
2135 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2136 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002137 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002138 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002139 if (IS_ERR(dev->dev))
2140 return PTR_ERR(dev->dev);
2141
2142 dev_set_drvdata(dev->dev, dev);
2143
2144 while ((attr = *attrs++)) {
2145 err = device_create_file(dev->dev, attr);
2146 if (err) {
2147 device_destroy(android_class, dev->dev->devt);
2148 return err;
2149 }
2150 }
2151 return 0;
2152}
2153
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302154static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002155{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302156 struct device_attribute **attrs = android_usb_attributes;
2157 struct device_attribute *attr;
2158
2159 while ((attr = *attrs++))
2160 device_remove_file(dev->dev, attr);
2161 device_destroy(android_class, dev->dev->devt);
2162}
2163
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002164static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2165{
2166 struct android_dev *dev = NULL;
2167
2168 /* Find the android dev from the list */
2169 list_for_each_entry(dev, &android_dev_list, list_item) {
2170 if (dev->cdev == cdev)
2171 break;
2172 }
2173
2174 return dev;
2175}
2176
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002177static struct android_configuration *alloc_android_config
2178 (struct android_dev *dev)
2179{
2180 struct android_configuration *conf;
2181
2182 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2183 if (!conf) {
2184 pr_err("%s(): Failed to alloc memory for android conf\n",
2185 __func__);
2186 return ERR_PTR(-ENOMEM);
2187 }
2188
2189 dev->configs_num++;
2190 conf->usb_config.label = dev->name;
2191 conf->usb_config.unbind = android_unbind_config;
2192 conf->usb_config.bConfigurationValue = dev->configs_num;
2193
2194 INIT_LIST_HEAD(&conf->enabled_functions);
2195
2196 list_add_tail(&conf->list_item, &dev->configs);
2197
2198 return conf;
2199}
2200
2201static void free_android_config(struct android_dev *dev,
2202 struct android_configuration *conf)
2203{
2204 list_del(&conf->list_item);
2205 dev->configs_num--;
2206 kfree(conf);
2207}
2208
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209static int __devinit android_probe(struct platform_device *pdev)
2210{
2211 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002212 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02002213 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002215 if (!android_class) {
2216 android_class = class_create(THIS_MODULE, "android_usb");
2217 if (IS_ERR(android_class))
2218 return PTR_ERR(android_class);
2219 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002220
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002221 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2222 if (!android_dev) {
2223 pr_err("%s(): Failed to alloc memory for android_dev\n",
2224 __func__);
2225 ret = -ENOMEM;
2226 goto err_alloc;
2227 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002228
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002229 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002230 android_dev->disable_depth = 1;
2231 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002232 android_dev->configs_num = 0;
2233 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002234 INIT_WORK(&android_dev->work, android_work);
2235 mutex_init(&android_dev->mutex);
2236
2237 android_dev->pdata = pdata;
2238
2239 list_add_tail(&android_dev->list_item, &android_dev_list);
2240 android_dev_count++;
2241
2242 if (pdata)
2243 composite_driver.usb_core_id = pdata->usb_core_id;
2244 else
2245 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2246
2247 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302248 if (ret) {
2249 pr_err("%s(): android_create_device failed\n", __func__);
2250 goto err_dev;
2251 }
2252
Lena Salmand092f2d2012-03-12 17:27:24 +02002253 ret = usb_composite_probe(&android_usb_driver, android_bind);
2254 if (ret) {
2255 pr_err("%s(): Failed to register android "
2256 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302257 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002258 }
2259
Ofir Cohen94213a72012-05-03 14:26:32 +03002260 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302261 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002262 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002263 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002264 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002265
Lena Salmand092f2d2012-03-12 17:27:24 +02002266 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302267err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002268 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302269err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002270 list_del(&android_dev->list_item);
2271 android_dev_count--;
2272 kfree(android_dev);
2273err_alloc:
2274 if (list_empty(&android_dev_list)) {
2275 class_destroy(android_class);
2276 android_class = NULL;
2277 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302278 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002279}
2280
2281static int android_remove(struct platform_device *pdev)
2282{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002283 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002284 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002285 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302286
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002287 if (pdata)
2288 usb_core_id = pdata->usb_core_id;
2289
2290 /* Find the android dev from the list */
2291 list_for_each_entry(dev, &android_dev_list, list_item) {
2292 if (!dev->pdata)
2293 break; /*To backward compatibility*/
2294 if (dev->pdata->usb_core_id == usb_core_id)
2295 break;
2296 }
2297
2298 if (dev) {
2299 android_destroy_device(dev);
2300 if (pdata && pdata->swfi_latency)
2301 pm_qos_remove_request(&dev->pm_qos_req_dma);
2302 list_del(&dev->list_item);
2303 android_dev_count--;
2304 kfree(dev);
2305 }
2306
2307 if (list_empty(&android_dev_list)) {
2308 class_destroy(android_class);
2309 android_class = NULL;
2310 usb_composite_unregister(&android_usb_driver);
2311 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002312
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002313 return 0;
2314}
2315
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002316static const struct platform_device_id android_id_table[] __devinitconst = {
2317 {
2318 .name = "android_usb",
2319 },
2320 {
2321 .name = "android_usb_hsic",
2322 },
2323};
2324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002325static struct platform_driver android_platform_driver = {
2326 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002327 .probe = android_probe,
2328 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002329 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002330};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002331
2332static int __init init(void)
2333{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302334 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002335
Benoit Goby1e8ce152011-12-12 13:01:23 -08002336 /* Override composite driver functions */
2337 composite_driver.setup = android_setup;
2338 composite_driver.disconnect = android_disconnect;
2339
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002340 INIT_LIST_HEAD(&android_dev_list);
2341 android_dev_count = 0;
2342
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302343 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302344 if (ret) {
2345 pr_err("%s(): Failed to register android"
2346 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302347 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002348
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302349 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002350}
2351module_init(init);
2352
2353static void __exit cleanup(void)
2354{
Lena Salmand092f2d2012-03-12 17:27:24 +02002355 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002356}
2357module_exit(cleanup);