blob: ac8863656308ae07ac683de671f4024766c84d6d [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
102 /* for android_dev.enabled_functions */
103 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 {
130 struct android_usb_function **functions;
131 struct list_head enabled_functions;
132 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
146 struct list_head list_item;
147
148 struct usb_configuration config;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800149};
150
151static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300152static struct list_head android_dev_list;
153static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800154static int android_bind_config(struct usb_configuration *c);
155static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300156static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800157
158/* string IDs are assigned dynamically */
159#define STRING_MANUFACTURER_IDX 0
160#define STRING_PRODUCT_IDX 1
161#define STRING_SERIAL_IDX 2
162
163static char manufacturer_string[256];
164static char product_string[256];
165static char serial_string[256];
166
167/* String Table */
168static struct usb_string strings_dev[] = {
169 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
170 [STRING_PRODUCT_IDX].s = product_string,
171 [STRING_SERIAL_IDX].s = serial_string,
172 { } /* end of list */
173};
174
175static struct usb_gadget_strings stringtab_dev = {
176 .language = 0x0409, /* en-us */
177 .strings = strings_dev,
178};
179
180static struct usb_gadget_strings *dev_strings[] = {
181 &stringtab_dev,
182 NULL,
183};
184
185static struct usb_device_descriptor device_desc = {
186 .bLength = sizeof(device_desc),
187 .bDescriptorType = USB_DT_DEVICE,
188 .bcdUSB = __constant_cpu_to_le16(0x0200),
189 .bDeviceClass = USB_CLASS_PER_INTERFACE,
190 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
191 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
192 .bcdDevice = __constant_cpu_to_le16(0xffff),
193 .bNumConfigurations = 1,
194};
195
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530196static struct usb_otg_descriptor otg_descriptor = {
197 .bLength = sizeof otg_descriptor,
198 .bDescriptorType = USB_DT_OTG,
199 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
200 .bcdOTG = __constant_cpu_to_le16(0x0200),
201};
202
203static const struct usb_descriptor_header *otg_desc[] = {
204 (struct usb_descriptor_header *) &otg_descriptor,
205 NULL,
206};
207
Manu Gautama2b54142012-04-03 14:34:32 +0530208enum android_device_state {
209 USB_DISCONNECTED,
210 USB_CONNECTED,
211 USB_CONFIGURED,
212};
213
Ofir Cohen94213a72012-05-03 14:26:32 +0300214static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
215{
216 struct android_usb_platform_data *pdata = dev->pdata;
217 u32 swfi_latency = 0;
218 static int last_vote = -1;
219
Ofir Cohen56eb7072012-05-20 11:41:39 +0300220 if (!pdata || vote == last_vote
221 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300222 return;
223
224 swfi_latency = pdata->swfi_latency + 1;
225 if (vote)
226 pm_qos_update_request(&dev->pm_qos_req_dma,
227 swfi_latency);
228 else
229 pm_qos_update_request(&dev->pm_qos_req_dma,
230 PM_QOS_DEFAULT_VALUE);
231 last_vote = vote;
232}
233
Benoit Goby1e8ce152011-12-12 13:01:23 -0800234static void android_work(struct work_struct *data)
235{
236 struct android_dev *dev = container_of(data, struct android_dev, work);
237 struct usb_composite_dev *cdev = dev->cdev;
238 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
239 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
240 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
241 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530242 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800243 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300244 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800245
246 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530247 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800248 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530249 next_state = USB_CONFIGURED;
250 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800251 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530252 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300253 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300254 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300255 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300256 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530257 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800258 dev->sw_connected = dev->connected;
259 spin_unlock_irqrestore(&cdev->lock, flags);
260
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300261 if (pm_qos_vote != -1)
262 android_pm_qos_update_latency(dev, pm_qos_vote);
263
Benoit Goby1e8ce152011-12-12 13:01:23 -0800264 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530265 /*
266 * Some userspace modules, e.g. MTP, work correctly only if
267 * CONFIGURED uevent is preceded by DISCONNECT uevent.
268 * Check if we missed sending out a DISCONNECT uevent. This can
269 * happen if host PC resets and configures device really quick.
270 */
271 if (((uevent_envp == connected) &&
272 (last_uevent != USB_DISCONNECTED)) ||
273 ((uevent_envp == configured) &&
274 (last_uevent == USB_CONFIGURED))) {
275 pr_info("%s: sent missed DISCONNECT event\n", __func__);
276 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
277 disconnected);
278 msleep(20);
279 }
280 /*
281 * Before sending out CONFIGURED uevent give function drivers
282 * a chance to wakeup userspace threads and notify disconnect
283 */
284 if (uevent_envp == configured)
285 msleep(50);
286
Benoit Goby1e8ce152011-12-12 13:01:23 -0800287 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530288 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800289 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
290 } else {
291 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
292 dev->connected, dev->sw_connected, cdev->config);
293 }
294}
295
Benoit Goby80ba14d2012-03-19 18:56:52 -0700296static void android_enable(struct android_dev *dev)
297{
298 struct usb_composite_dev *cdev = dev->cdev;
299
300 if (WARN_ON(!dev->disable_depth))
301 return;
302
303 if (--dev->disable_depth == 0) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300304 usb_add_config(cdev, &dev->config,
Benoit Goby80ba14d2012-03-19 18:56:52 -0700305 android_bind_config);
306 usb_gadget_connect(cdev->gadget);
307 }
308}
309
310static void android_disable(struct android_dev *dev)
311{
312 struct usb_composite_dev *cdev = dev->cdev;
313
314 if (dev->disable_depth++ == 0) {
315 usb_gadget_disconnect(cdev->gadget);
316 /* Cancel pending control requests */
317 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300318 usb_remove_config(cdev, &dev->config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700319 }
320}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800321
322/*-------------------------------------------------------------------------*/
323/* Supported functions initialization */
324
Benoit Goby80ba14d2012-03-19 18:56:52 -0700325struct adb_data {
326 bool opened;
327 bool enabled;
328};
329
Benoit Goby2b6862d2011-12-19 14:38:41 -0800330static int
331adb_function_init(struct android_usb_function *f,
332 struct usb_composite_dev *cdev)
333{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700334 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
335 if (!f->config)
336 return -ENOMEM;
337
Benoit Goby2b6862d2011-12-19 14:38:41 -0800338 return adb_setup();
339}
340
341static void adb_function_cleanup(struct android_usb_function *f)
342{
343 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700344 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800345}
346
347static int
348adb_function_bind_config(struct android_usb_function *f,
349 struct usb_configuration *c)
350{
351 return adb_bind_config(c);
352}
353
Benoit Goby80ba14d2012-03-19 18:56:52 -0700354static void adb_android_function_enable(struct android_usb_function *f)
355{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300356 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700357 struct adb_data *data = f->config;
358
359 data->enabled = true;
360
361 /* Disable the gadget until adbd is ready */
362 if (!data->opened)
363 android_disable(dev);
364}
365
366static void adb_android_function_disable(struct android_usb_function *f)
367{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300368 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700369 struct adb_data *data = f->config;
370
371 data->enabled = false;
372
373 /* Balance the disable that was called in closed_callback */
374 if (!data->opened)
375 android_enable(dev);
376}
377
Benoit Goby2b6862d2011-12-19 14:38:41 -0800378static struct android_usb_function adb_function = {
379 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700380 .enable = adb_android_function_enable,
381 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800382 .init = adb_function_init,
383 .cleanup = adb_function_cleanup,
384 .bind_config = adb_function_bind_config,
385};
386
Benoit Goby80ba14d2012-03-19 18:56:52 -0700387static void adb_ready_callback(void)
388{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300389 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700390 struct adb_data *data = adb_function.config;
391
Benoit Goby80ba14d2012-03-19 18:56:52 -0700392 data->opened = true;
393
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300394 if (data->enabled && dev) {
395 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700396 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300397 mutex_unlock(&dev->mutex);
398 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700399}
400
401static void adb_closed_callback(void)
402{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300403 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700404 struct adb_data *data = adb_function.config;
405
Benoit Goby80ba14d2012-03-19 18:56:52 -0700406 data->opened = false;
407
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300408 if (data->enabled) {
409 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700410 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300411 mutex_unlock(&dev->mutex);
412 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700413}
414
Benoit Goby2b6862d2011-12-19 14:38:41 -0800415
Benoit Gobyaab96812011-04-19 20:37:33 -0700416/*-------------------------------------------------------------------------*/
417/* Supported functions initialization */
418
Manu Gautam8e0719b2011-09-26 14:47:55 +0530419/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530420static int rmnet_smd_function_bind_config(struct android_usb_function *f,
421 struct usb_configuration *c)
422{
423 return rmnet_smd_bind_config(c);
424}
425
426static struct android_usb_function rmnet_smd_function = {
427 .name = "rmnet_smd",
428 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800429};
430
Manu Gautam8e0719b2011-09-26 14:47:55 +0530431/* RMNET_SDIO */
432static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
433 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800434{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530435 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800436}
437
Manu Gautam8e0719b2011-09-26 14:47:55 +0530438static struct android_usb_function rmnet_sdio_function = {
439 .name = "rmnet_sdio",
440 .bind_config = rmnet_sdio_function_bind_config,
441};
442
443/* RMNET_SMD_SDIO */
444static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
445 struct usb_composite_dev *cdev)
446{
447 return rmnet_smd_sdio_init();
448}
449
450static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
451{
452 rmnet_smd_sdio_cleanup();
453}
454
455static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
456 struct usb_configuration *c)
457{
458 return rmnet_smd_sdio_function_add(c);
459}
460
461static struct device_attribute *rmnet_smd_sdio_attributes[] = {
462 &dev_attr_transport, NULL };
463
464static struct android_usb_function rmnet_smd_sdio_function = {
465 .name = "rmnet_smd_sdio",
466 .init = rmnet_smd_sdio_function_init,
467 .cleanup = rmnet_smd_sdio_function_cleanup,
468 .bind_config = rmnet_smd_sdio_bind_config,
469 .attributes = rmnet_smd_sdio_attributes,
470};
471
Hemant Kumar1b820d52011-11-03 15:08:28 -0700472/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
473#define MAX_XPORT_STR_LEN 50
474static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530475
Manu Gautame3e897c2011-09-12 17:18:46 +0530476static void rmnet_function_cleanup(struct android_usb_function *f)
477{
478 frmnet_cleanup();
479}
480
Manu Gautam2b0234a2011-09-07 16:47:52 +0530481static int rmnet_function_bind_config(struct android_usb_function *f,
482 struct usb_configuration *c)
483{
484 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700485 int err = 0;
486 char *ctrl_name;
487 char *data_name;
488 char buf[MAX_XPORT_STR_LEN], *b;
489 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530490
Hemant Kumar1b820d52011-11-03 15:08:28 -0700491 if (!rmnet_initialized) {
492 rmnet_initialized = 1;
493 strlcpy(buf, rmnet_transports, sizeof(buf));
494 b = strim(buf);
495 while (b) {
496 ctrl_name = strsep(&b, ",");
497 data_name = strsep(&b, ",");
498 if (ctrl_name && data_name) {
499 err = frmnet_init_port(ctrl_name, data_name);
500 if (err) {
501 pr_err("rmnet: Cannot open ctrl port:"
502 "'%s' data port:'%s'\n",
503 ctrl_name, data_name);
504 goto out;
505 }
506 ports++;
507 }
508 }
509
510 err = rmnet_gport_setup();
511 if (err) {
512 pr_err("rmnet: Cannot setup transports");
513 goto out;
514 }
515 }
516
517 for (i = 0; i < ports; i++) {
518 err = frmnet_bind_config(c, i);
519 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530520 pr_err("Could not bind rmnet%u config\n", i);
521 break;
522 }
523 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700524out:
525 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530526}
527
Hemant Kumar1b820d52011-11-03 15:08:28 -0700528static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530529 struct device_attribute *attr, char *buf)
530{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700531 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530532}
533
Hemant Kumar1b820d52011-11-03 15:08:28 -0700534static ssize_t rmnet_transports_store(
535 struct device *device, struct device_attribute *attr,
536 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530537{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700538 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530539
Manu Gautam2b0234a2011-09-07 16:47:52 +0530540 return size;
541}
542
Hemant Kumar1b820d52011-11-03 15:08:28 -0700543static struct device_attribute dev_attr_rmnet_transports =
544 __ATTR(transports, S_IRUGO | S_IWUSR,
545 rmnet_transports_show,
546 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530547static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700548 &dev_attr_rmnet_transports,
549 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530550
551static struct android_usb_function rmnet_function = {
552 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530553 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530554 .bind_config = rmnet_function_bind_config,
555 .attributes = rmnet_function_attributes,
556};
557
Ofir Cohen7b155422012-07-31 13:02:49 +0300558struct ecm_function_config {
559 u8 ethaddr[ETH_ALEN];
560};
561
562static int ecm_function_init(struct android_usb_function *f,
563 struct usb_composite_dev *cdev)
564{
565 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
566 if (!f->config)
567 return -ENOMEM;
568 return 0;
569}
570
571static void ecm_function_cleanup(struct android_usb_function *f)
572{
573 kfree(f->config);
574 f->config = NULL;
575}
576
577static int ecm_qc_function_bind_config(struct android_usb_function *f,
578 struct usb_configuration *c)
579{
580 int ret;
581 struct ecm_function_config *ecm = f->config;
582
583 if (!ecm) {
584 pr_err("%s: ecm_pdata\n", __func__);
585 return -EINVAL;
586 }
587
588 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
589 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
590 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
591
592 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
593 if (ret) {
594 pr_err("%s: gether_setup failed\n", __func__);
595 return ret;
596 }
597
598 return ecm_qc_bind_config(c, ecm->ethaddr);
599}
600
601static void ecm_qc_function_unbind_config(struct android_usb_function *f,
602 struct usb_configuration *c)
603{
604 gether_qc_cleanup();
605}
606
607static ssize_t ecm_ethaddr_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
609{
610 struct android_usb_function *f = dev_get_drvdata(dev);
611 struct ecm_function_config *ecm = f->config;
612 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
613 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
614 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
615}
616
617static ssize_t ecm_ethaddr_store(struct device *dev,
618 struct device_attribute *attr, const char *buf, size_t size)
619{
620 struct android_usb_function *f = dev_get_drvdata(dev);
621 struct ecm_function_config *ecm = f->config;
622
623 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
624 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
625 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
626 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
627 return size;
628 return -EINVAL;
629}
630
631static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
632 ecm_ethaddr_store);
633
634static struct device_attribute *ecm_function_attributes[] = {
635 &dev_attr_ecm_ethaddr,
636 NULL
637};
638
639static struct android_usb_function ecm_qc_function = {
640 .name = "ecm_qc",
641 .init = ecm_function_init,
642 .cleanup = ecm_function_cleanup,
643 .bind_config = ecm_qc_function_bind_config,
644 .unbind_config = ecm_qc_function_unbind_config,
645 .attributes = ecm_function_attributes,
646};
Anna Perela8c991d2012-04-09 16:44:46 +0300647
648/* MBIM - used with BAM */
649#define MAX_MBIM_INSTANCES 1
650
651static int mbim_function_init(struct android_usb_function *f,
652 struct usb_composite_dev *cdev)
653{
654 return mbim_init(MAX_MBIM_INSTANCES);
655}
656
657static void mbim_function_cleanup(struct android_usb_function *f)
658{
659 fmbim_cleanup();
660}
661
662static int mbim_function_bind_config(struct android_usb_function *f,
663 struct usb_configuration *c)
664{
665 return mbim_bind_config(c, 0);
666}
667
668static struct android_usb_function mbim_function = {
669 .name = "usb_mbim",
670 .cleanup = mbim_function_cleanup,
671 .bind_config = mbim_function_bind_config,
672 .init = mbim_function_init,
673};
674
675
Manu Gautam8e0719b2011-09-26 14:47:55 +0530676/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530677static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678static ssize_t clients_store(
679 struct device *device, struct device_attribute *attr,
680 const char *buff, size_t size)
681{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530682 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683
684 return size;
685}
686
687static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
688static struct device_attribute *diag_function_attributes[] =
689 { &dev_attr_clients, NULL };
690
691static int diag_function_init(struct android_usb_function *f,
692 struct usb_composite_dev *cdev)
693{
694 return diag_setup();
695}
696
697static void diag_function_cleanup(struct android_usb_function *f)
698{
699 diag_cleanup();
700}
701
702static int diag_function_bind_config(struct android_usb_function *f,
703 struct usb_configuration *c)
704{
705 char *name;
706 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530707 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800708 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300709 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530711 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 b = strim(buf);
713
714 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800715 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530717 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300718 if (dev->pdata && !once++)
719 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530720
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530722 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 if (err)
724 pr_err("diag: Cannot open channel '%s'", name);
725 }
726 }
727
728 return err;
729}
730
731static struct android_usb_function diag_function = {
732 .name = "diag",
733 .init = diag_function_init,
734 .cleanup = diag_function_cleanup,
735 .bind_config = diag_function_bind_config,
736 .attributes = diag_function_attributes,
737};
738
Manu Gautam8e0719b2011-09-26 14:47:55 +0530739/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530740static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530741static ssize_t serial_transports_store(
742 struct device *device, struct device_attribute *attr,
743 const char *buff, size_t size)
744{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530745 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530746
747 return size;
748}
749
750static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
751static struct device_attribute *serial_function_attributes[] =
752 { &dev_attr_transports, NULL };
753
754static void serial_function_cleanup(struct android_usb_function *f)
755{
756 gserial_cleanup();
757}
758
759static int serial_function_bind_config(struct android_usb_function *f,
760 struct usb_configuration *c)
761{
762 char *name;
763 char buf[32], *b;
764 int err = -1, i;
765 static int serial_initialized = 0, ports = 0;
766
767 if (serial_initialized)
768 goto bind_config;
769
770 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530771 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530772 b = strim(buf);
773
774 while (b) {
775 name = strsep(&b, ",");
776
777 if (name) {
778 err = gserial_init_port(ports, name);
779 if (err) {
780 pr_err("serial: Cannot open port '%s'", name);
781 goto out;
782 }
783 ports++;
784 }
785 }
786 err = gport_setup(c);
787 if (err) {
788 pr_err("serial: Cannot setup transports");
789 goto out;
790 }
791
792bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200793 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530794 err = gser_bind_config(c, i);
795 if (err) {
796 pr_err("serial: bind_config failed for port %d", i);
797 goto out;
798 }
799 }
800
801out:
802 return err;
803}
804
805static struct android_usb_function serial_function = {
806 .name = "serial",
807 .cleanup = serial_function_cleanup,
808 .bind_config = serial_function_bind_config,
809 .attributes = serial_function_attributes,
810};
811
Anji jonnala92be1b42011-12-19 09:44:41 +0530812/* ACM */
813static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
814static ssize_t acm_transports_store(
815 struct device *device, struct device_attribute *attr,
816 const char *buff, size_t size)
817{
818 strlcpy(acm_transports, buff, sizeof(acm_transports));
819
820 return size;
821}
822
823static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
824static struct device_attribute *acm_function_attributes[] = {
825 &dev_attr_acm_transports, NULL };
826
Benoit Goby1e8ce152011-12-12 13:01:23 -0800827static void acm_function_cleanup(struct android_usb_function *f)
828{
829 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800830}
831
Anji jonnala92be1b42011-12-19 09:44:41 +0530832static int acm_function_bind_config(struct android_usb_function *f,
833 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800834{
Anji jonnala92be1b42011-12-19 09:44:41 +0530835 char *name;
836 char buf[32], *b;
837 int err = -1, i;
838 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800839
Anji jonnala92be1b42011-12-19 09:44:41 +0530840 if (acm_initialized)
841 goto bind_config;
842
843 acm_initialized = 1;
844 strlcpy(buf, acm_transports, sizeof(buf));
845 b = strim(buf);
846
847 while (b) {
848 name = strsep(&b, ",");
849
850 if (name) {
851 err = acm_init_port(ports, name);
852 if (err) {
853 pr_err("acm: Cannot open port '%s'", name);
854 goto out;
855 }
856 ports++;
857 }
858 }
859 err = acm_port_setup(c);
860 if (err) {
861 pr_err("acm: Cannot setup transports");
862 goto out;
863 }
864
865bind_config:
866 for (i = 0; i < ports; i++) {
867 err = acm_bind_config(c, i);
868 if (err) {
869 pr_err("acm: bind_config failed for port %d", i);
870 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800871 }
872 }
873
Anji jonnala92be1b42011-12-19 09:44:41 +0530874out:
875 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800876}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800877static struct android_usb_function acm_function = {
878 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800879 .cleanup = acm_function_cleanup,
880 .bind_config = acm_function_bind_config,
881 .attributes = acm_function_attributes,
882};
883
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530884/* CCID */
885static int ccid_function_init(struct android_usb_function *f,
886 struct usb_composite_dev *cdev)
887{
888 return ccid_setup();
889}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800890
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530891static void ccid_function_cleanup(struct android_usb_function *f)
892{
893 ccid_cleanup();
894}
895
896static int ccid_function_bind_config(struct android_usb_function *f,
897 struct usb_configuration *c)
898{
899 return ccid_bind_config(c);
900}
901
902static struct android_usb_function ccid_function = {
903 .name = "ccid",
904 .init = ccid_function_init,
905 .cleanup = ccid_function_cleanup,
906 .bind_config = ccid_function_bind_config,
907};
908
Steve Mucklef132c6c2012-06-06 18:30:57 -0700909static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800910 struct usb_composite_dev *cdev)
911{
912 return mtp_setup();
913}
914
915static void mtp_function_cleanup(struct android_usb_function *f)
916{
917 mtp_cleanup();
918}
919
Steve Mucklef132c6c2012-06-06 18:30:57 -0700920static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800921 struct usb_configuration *c)
922{
923 return mtp_bind_config(c, false);
924}
925
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400926static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800927{
928 /* nothing to do - initialization is handled by mtp_function_init */
929 return 0;
930}
931
932static void ptp_function_cleanup(struct android_usb_function *f)
933{
934 /* nothing to do - cleanup is handled by mtp_function_cleanup */
935}
936
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400937static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800938{
939 return mtp_bind_config(c, true);
940}
941
942static int mtp_function_ctrlrequest(struct android_usb_function *f,
943 struct usb_composite_dev *cdev,
944 const struct usb_ctrlrequest *c)
945{
946 return mtp_ctrlrequest(cdev, c);
947}
948
949static struct android_usb_function mtp_function = {
950 .name = "mtp",
951 .init = mtp_function_init,
952 .cleanup = mtp_function_cleanup,
953 .bind_config = mtp_function_bind_config,
954 .ctrlrequest = mtp_function_ctrlrequest,
955};
956
957/* PTP function is same as MTP with slightly different interface descriptor */
958static struct android_usb_function ptp_function = {
959 .name = "ptp",
960 .init = ptp_function_init,
961 .cleanup = ptp_function_cleanup,
962 .bind_config = ptp_function_bind_config,
963};
964
965
Benoit Goby1e8ce152011-12-12 13:01:23 -0800966struct rndis_function_config {
967 u8 ethaddr[ETH_ALEN];
968 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200969 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800970 char manufacturer[256];
971 /* "Wireless" RNDIS; auto-detected by Windows */
972 bool wceis;
973};
974
975static int
976rndis_function_init(struct android_usb_function *f,
977 struct usb_composite_dev *cdev)
978{
979 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
980 if (!f->config)
981 return -ENOMEM;
982 return 0;
983}
984
985static void rndis_function_cleanup(struct android_usb_function *f)
986{
987 kfree(f->config);
988 f->config = NULL;
989}
990
Ofir Cohenaef90b72012-07-31 12:37:04 +0200991static int rndis_qc_function_init(struct android_usb_function *f,
992 struct usb_composite_dev *cdev)
993{
994 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
995 if (!f->config)
996 return -ENOMEM;
997
998 return rndis_qc_init();
999}
1000
1001static void rndis_qc_function_cleanup(struct android_usb_function *f)
1002{
1003 rndis_qc_cleanup();
1004 kfree(f->config);
1005}
1006
Benoit Goby1e8ce152011-12-12 13:01:23 -08001007static int
1008rndis_function_bind_config(struct android_usb_function *f,
1009 struct usb_configuration *c)
1010{
1011 int ret;
1012 struct rndis_function_config *rndis = f->config;
1013
1014 if (!rndis) {
1015 pr_err("%s: rndis_pdata\n", __func__);
1016 return -1;
1017 }
1018
1019 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1020 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1021 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1022
1023 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1024 if (ret) {
1025 pr_err("%s: gether_setup failed\n", __func__);
1026 return ret;
1027 }
1028
1029 if (rndis->wceis) {
1030 /* "Wireless" RNDIS; auto-detected by Windows */
1031 rndis_iad_descriptor.bFunctionClass =
1032 USB_CLASS_WIRELESS_CONTROLLER;
1033 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1034 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1035 rndis_control_intf.bInterfaceClass =
1036 USB_CLASS_WIRELESS_CONTROLLER;
1037 rndis_control_intf.bInterfaceSubClass = 0x01;
1038 rndis_control_intf.bInterfaceProtocol = 0x03;
1039 }
1040
1041 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1042 rndis->manufacturer);
1043}
1044
Ofir Cohenaef90b72012-07-31 12:37:04 +02001045static int rndis_qc_function_bind_config(struct android_usb_function *f,
1046 struct usb_configuration *c)
1047{
1048 int ret;
1049 struct rndis_function_config *rndis = f->config;
1050
1051 if (!rndis) {
1052 pr_err("%s: rndis_pdata\n", __func__);
1053 return -EINVAL;
1054 }
1055
1056 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1057 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1058 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1059
1060 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1061 if (ret) {
1062 pr_err("%s: gether_setup failed\n", __func__);
1063 return ret;
1064 }
1065
1066 if (rndis->wceis) {
1067 /* "Wireless" RNDIS; auto-detected by Windows */
1068 rndis_qc_iad_descriptor.bFunctionClass =
1069 USB_CLASS_WIRELESS_CONTROLLER;
1070 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1071 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1072 rndis_qc_control_intf.bInterfaceClass =
1073 USB_CLASS_WIRELESS_CONTROLLER;
1074 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1075 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1076 }
1077
1078 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1079 rndis->manufacturer,
1080 rndis->max_pkt_per_xfer);
1081}
1082
Benoit Goby1e8ce152011-12-12 13:01:23 -08001083static void rndis_function_unbind_config(struct android_usb_function *f,
1084 struct usb_configuration *c)
1085{
1086 gether_cleanup();
1087}
1088
Ofir Cohenaef90b72012-07-31 12:37:04 +02001089static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1090 struct usb_configuration *c)
1091{
1092 gether_qc_cleanup();
1093}
1094
Benoit Goby1e8ce152011-12-12 13:01:23 -08001095static ssize_t rndis_manufacturer_show(struct device *dev,
1096 struct device_attribute *attr, char *buf)
1097{
1098 struct android_usb_function *f = dev_get_drvdata(dev);
1099 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001100
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301101 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001102}
1103
1104static ssize_t rndis_manufacturer_store(struct device *dev,
1105 struct device_attribute *attr, const char *buf, size_t size)
1106{
1107 struct android_usb_function *f = dev_get_drvdata(dev);
1108 struct rndis_function_config *config = f->config;
1109
1110 if (size >= sizeof(config->manufacturer))
1111 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001112
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301113 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001114 return size;
1115 return -1;
1116}
1117
1118static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1119 rndis_manufacturer_store);
1120
1121static ssize_t rndis_wceis_show(struct device *dev,
1122 struct device_attribute *attr, char *buf)
1123{
1124 struct android_usb_function *f = dev_get_drvdata(dev);
1125 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001126
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301127 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001128}
1129
1130static ssize_t rndis_wceis_store(struct device *dev,
1131 struct device_attribute *attr, const char *buf, size_t size)
1132{
1133 struct android_usb_function *f = dev_get_drvdata(dev);
1134 struct rndis_function_config *config = f->config;
1135 int value;
1136
1137 if (sscanf(buf, "%d", &value) == 1) {
1138 config->wceis = value;
1139 return size;
1140 }
1141 return -EINVAL;
1142}
1143
1144static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1145 rndis_wceis_store);
1146
1147static ssize_t rndis_ethaddr_show(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1149{
1150 struct android_usb_function *f = dev_get_drvdata(dev);
1151 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001152
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301153 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001154 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1155 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1156}
1157
1158static ssize_t rndis_ethaddr_store(struct device *dev,
1159 struct device_attribute *attr, const char *buf, size_t size)
1160{
1161 struct android_usb_function *f = dev_get_drvdata(dev);
1162 struct rndis_function_config *rndis = f->config;
1163
1164 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1165 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1166 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1167 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1168 return size;
1169 return -EINVAL;
1170}
1171
1172static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1173 rndis_ethaddr_store);
1174
1175static ssize_t rndis_vendorID_show(struct device *dev,
1176 struct device_attribute *attr, char *buf)
1177{
1178 struct android_usb_function *f = dev_get_drvdata(dev);
1179 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001180
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301181 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001182}
1183
1184static ssize_t rndis_vendorID_store(struct device *dev,
1185 struct device_attribute *attr, const char *buf, size_t size)
1186{
1187 struct android_usb_function *f = dev_get_drvdata(dev);
1188 struct rndis_function_config *config = f->config;
1189 int value;
1190
1191 if (sscanf(buf, "%04x", &value) == 1) {
1192 config->vendorID = value;
1193 return size;
1194 }
1195 return -EINVAL;
1196}
1197
1198static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1199 rndis_vendorID_store);
1200
Ofir Cohenaef90b72012-07-31 12:37:04 +02001201static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1202 struct device_attribute *attr, char *buf)
1203{
1204 struct android_usb_function *f = dev_get_drvdata(dev);
1205 struct rndis_function_config *config = f->config;
1206 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1207}
1208
1209static ssize_t rndis_max_pkt_per_xfer_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, "%d", &value) == 1) {
1217 config->max_pkt_per_xfer = value;
1218 return size;
1219 }
1220 return -EINVAL;
1221}
1222
1223static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1224 rndis_max_pkt_per_xfer_show,
1225 rndis_max_pkt_per_xfer_store);
1226
Benoit Goby1e8ce152011-12-12 13:01:23 -08001227static struct device_attribute *rndis_function_attributes[] = {
1228 &dev_attr_manufacturer,
1229 &dev_attr_wceis,
1230 &dev_attr_ethaddr,
1231 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001232 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001233 NULL
1234};
1235
1236static struct android_usb_function rndis_function = {
1237 .name = "rndis",
1238 .init = rndis_function_init,
1239 .cleanup = rndis_function_cleanup,
1240 .bind_config = rndis_function_bind_config,
1241 .unbind_config = rndis_function_unbind_config,
1242 .attributes = rndis_function_attributes,
1243};
1244
Ofir Cohenaef90b72012-07-31 12:37:04 +02001245static struct android_usb_function rndis_qc_function = {
1246 .name = "rndis_qc",
1247 .init = rndis_qc_function_init,
1248 .cleanup = rndis_qc_function_cleanup,
1249 .bind_config = rndis_qc_function_bind_config,
1250 .unbind_config = rndis_qc_function_unbind_config,
1251 .attributes = rndis_function_attributes,
1252};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001253
1254struct mass_storage_function_config {
1255 struct fsg_config fsg;
1256 struct fsg_common *common;
1257};
1258
1259static int mass_storage_function_init(struct android_usb_function *f,
1260 struct usb_composite_dev *cdev)
1261{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001262 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001263 struct mass_storage_function_config *config;
1264 struct fsg_common *common;
1265 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301266 int i;
1267 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001268
1269 config = kzalloc(sizeof(struct mass_storage_function_config),
1270 GFP_KERNEL);
1271 if (!config)
1272 return -ENOMEM;
1273
1274 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301275 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301276 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301277 config->fsg.nluns = 2;
1278 config->fsg.luns[1].cdrom = 1;
1279 config->fsg.luns[1].ro = 1;
1280 config->fsg.luns[1].removable = 1;
1281 name[1] = "lun0";
1282 }
1283
Benoit Goby1e8ce152011-12-12 13:01:23 -08001284 config->fsg.luns[0].removable = 1;
1285
1286 common = fsg_common_init(NULL, cdev, &config->fsg);
1287 if (IS_ERR(common)) {
1288 kfree(config);
1289 return PTR_ERR(common);
1290 }
1291
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301292 for (i = 0; i < config->fsg.nluns; i++) {
1293 err = sysfs_create_link(&f->dev->kobj,
1294 &common->luns[i].dev.kobj,
1295 name[i]);
1296 if (err)
1297 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001298 }
1299
1300 config->common = common;
1301 f->config = config;
1302 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301303error:
1304 for (; i > 0 ; i--)
1305 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1306
1307 fsg_common_release(&common->ref);
1308 kfree(config);
1309 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001310}
1311
1312static void mass_storage_function_cleanup(struct android_usb_function *f)
1313{
1314 kfree(f->config);
1315 f->config = NULL;
1316}
1317
1318static int mass_storage_function_bind_config(struct android_usb_function *f,
1319 struct usb_configuration *c)
1320{
1321 struct mass_storage_function_config *config = f->config;
1322 return fsg_bind_config(c->cdev, c, config->common);
1323}
1324
1325static ssize_t mass_storage_inquiry_show(struct device *dev,
1326 struct device_attribute *attr, char *buf)
1327{
1328 struct android_usb_function *f = dev_get_drvdata(dev);
1329 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301330 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001331}
1332
1333static ssize_t mass_storage_inquiry_store(struct device *dev,
1334 struct device_attribute *attr, const char *buf, size_t size)
1335{
1336 struct android_usb_function *f = dev_get_drvdata(dev);
1337 struct mass_storage_function_config *config = f->config;
1338 if (size >= sizeof(config->common->inquiry_string))
1339 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301340 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001341 return -EINVAL;
1342 return size;
1343}
1344
1345static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1346 mass_storage_inquiry_show,
1347 mass_storage_inquiry_store);
1348
1349static struct device_attribute *mass_storage_function_attributes[] = {
1350 &dev_attr_inquiry_string,
1351 NULL
1352};
1353
1354static struct android_usb_function mass_storage_function = {
1355 .name = "mass_storage",
1356 .init = mass_storage_function_init,
1357 .cleanup = mass_storage_function_cleanup,
1358 .bind_config = mass_storage_function_bind_config,
1359 .attributes = mass_storage_function_attributes,
1360};
1361
1362
Benoit Gobycf3fc062011-12-19 14:39:37 -08001363static int accessory_function_init(struct android_usb_function *f,
1364 struct usb_composite_dev *cdev)
1365{
1366 return acc_setup();
1367}
1368
1369static void accessory_function_cleanup(struct android_usb_function *f)
1370{
1371 acc_cleanup();
1372}
1373
1374static int accessory_function_bind_config(struct android_usb_function *f,
1375 struct usb_configuration *c)
1376{
1377 return acc_bind_config(c);
1378}
1379
1380static int accessory_function_ctrlrequest(struct android_usb_function *f,
1381 struct usb_composite_dev *cdev,
1382 const struct usb_ctrlrequest *c)
1383{
1384 return acc_ctrlrequest(cdev, c);
1385}
1386
1387static struct android_usb_function accessory_function = {
1388 .name = "accessory",
1389 .init = accessory_function_init,
1390 .cleanup = accessory_function_cleanup,
1391 .bind_config = accessory_function_bind_config,
1392 .ctrlrequest = accessory_function_ctrlrequest,
1393};
1394
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301395static int android_uasp_connect_cb(bool connect)
1396{
1397 /*
1398 * TODO
1399 * We may have to disable gadget till UASP configfs nodes
1400 * are configured which includes mapping LUN with the
1401 * backing file. It is a fundamental difference between
1402 * f_mass_storage and f_tcp. That means UASP can not be
1403 * in default composition.
1404 *
1405 * For now, assume that UASP configfs nodes are configured
1406 * before enabling android gadget. Or cable should be
1407 * reconnected after mapping the LUN.
1408 *
1409 * Also consider making UASP to respond to Host requests when
1410 * Lun is not mapped.
1411 */
1412 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1413
1414 return 0;
1415}
1416
1417static int uasp_function_init(struct android_usb_function *f,
1418 struct usb_composite_dev *cdev)
1419{
1420 return f_tcm_init(&android_uasp_connect_cb);
1421}
1422
1423static void uasp_function_cleanup(struct android_usb_function *f)
1424{
1425 f_tcm_exit();
1426}
1427
1428static int uasp_function_bind_config(struct android_usb_function *f,
1429 struct usb_configuration *c)
1430{
1431 return tcm_bind_config(c);
1432}
1433
1434static struct android_usb_function uasp_function = {
1435 .name = "uasp",
1436 .init = uasp_function_init,
1437 .cleanup = uasp_function_cleanup,
1438 .bind_config = uasp_function_bind_config,
1439};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001440
Benoit Goby1e8ce152011-12-12 13:01:23 -08001441static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001442 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001443 &ecm_qc_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301444 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301445 &rmnet_sdio_function,
1446 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301447 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301449 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001450 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301451 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001452 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001453 &mtp_function,
1454 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001455 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001456 &rndis_qc_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001457 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001458 &accessory_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301459 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001460 NULL
1461};
1462
Lena Salmand092f2d2012-03-12 17:27:24 +02001463static void android_cleanup_functions(struct android_usb_function **functions)
1464{
1465 struct android_usb_function *f;
1466 struct device_attribute **attrs;
1467 struct device_attribute *attr;
1468
1469 while (*functions) {
1470 f = *functions++;
1471
1472 if (f->dev) {
1473 device_destroy(android_class, f->dev->devt);
1474 kfree(f->dev_name);
1475 } else
1476 continue;
1477
1478 if (f->cleanup)
1479 f->cleanup(f);
1480
1481 attrs = f->attributes;
1482 if (attrs) {
1483 while ((attr = *attrs++))
1484 device_remove_file(f->dev, attr);
1485 }
1486 }
1487}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001488
1489static int android_init_functions(struct android_usb_function **functions,
1490 struct usb_composite_dev *cdev)
1491{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001492 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001493 struct android_usb_function *f;
1494 struct device_attribute **attrs;
1495 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301496 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001497 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001498
1499 for (; (f = *functions++); index++) {
1500 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001501 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001502 if (!f->dev_name) {
1503 err = -ENOMEM;
1504 goto err_out;
1505 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001506 f->dev = device_create(android_class, dev->dev,
1507 MKDEV(0, index), f, f->dev_name);
1508 if (IS_ERR(f->dev)) {
1509 pr_err("%s: Failed to create dev %s", __func__,
1510 f->dev_name);
1511 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001512 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001513 goto err_create;
1514 }
1515
1516 if (f->init) {
1517 err = f->init(f, cdev);
1518 if (err) {
1519 pr_err("%s: Failed to init %s", __func__,
1520 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001521 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001522 }
1523 }
1524
1525 attrs = f->attributes;
1526 if (attrs) {
1527 while ((attr = *attrs++) && !err)
1528 err = device_create_file(f->dev, attr);
1529 }
1530 if (err) {
1531 pr_err("%s: Failed to create function %s attributes",
1532 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001533 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001534 }
1535 }
1536 return 0;
1537
Lena Salmand092f2d2012-03-12 17:27:24 +02001538err_attrs:
1539 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1540 device_remove_file(f->dev, attr);
1541 if (f->cleanup)
1542 f->cleanup(f);
1543err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001544 device_destroy(android_class, f->dev->devt);
1545err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001546 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001547 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001548err_out:
1549 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001550 return err;
1551}
1552
Benoit Goby1e8ce152011-12-12 13:01:23 -08001553static int
1554android_bind_enabled_functions(struct android_dev *dev,
1555 struct usb_configuration *c)
1556{
1557 struct android_usb_function *f;
1558 int ret;
1559
1560 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1561 ret = f->bind_config(f, c);
1562 if (ret) {
1563 pr_err("%s: %s failed", __func__, f->name);
1564 return ret;
1565 }
1566 }
1567 return 0;
1568}
1569
1570static void
1571android_unbind_enabled_functions(struct android_dev *dev,
1572 struct usb_configuration *c)
1573{
1574 struct android_usb_function *f;
1575
1576 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1577 if (f->unbind_config)
1578 f->unbind_config(f, c);
1579 }
1580}
1581
1582static int android_enable_function(struct android_dev *dev, char *name)
1583{
1584 struct android_usb_function **functions = dev->functions;
1585 struct android_usb_function *f;
1586 while ((f = *functions++)) {
1587 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001588 if (f->android_dev)
1589 pr_err("%s cannot be enabled on two devices\n",
1590 f->name);
1591 else {
1592 list_add_tail(&f->enabled_list,
1593 &dev->enabled_functions);
1594 f->android_dev = dev;
1595 return 0;
1596 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001597 }
1598 }
1599 return -EINVAL;
1600}
1601
1602/*-------------------------------------------------------------------------*/
1603/* /sys/class/android_usb/android%d/ interface */
1604
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301605static ssize_t remote_wakeup_show(struct device *pdev,
1606 struct device_attribute *attr, char *buf)
1607{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001608 struct android_dev *dev = dev_get_drvdata(pdev);
1609
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301610 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001611 !!(dev->config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301612 USB_CONFIG_ATT_WAKEUP));
1613}
1614
1615static ssize_t remote_wakeup_store(struct device *pdev,
1616 struct device_attribute *attr, const char *buff, size_t size)
1617{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001618 struct android_dev *dev = dev_get_drvdata(pdev);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301619 int enable = 0;
1620
1621 sscanf(buff, "%d", &enable);
1622
1623 pr_debug("android_usb: %s remote wakeup\n",
1624 enable ? "enabling" : "disabling");
1625
1626 if (enable)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001627 dev->config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301628 else
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001629 dev->config.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301630
1631 return size;
1632}
1633
Benoit Goby1e8ce152011-12-12 13:01:23 -08001634static ssize_t
1635functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1636{
1637 struct android_dev *dev = dev_get_drvdata(pdev);
1638 struct android_usb_function *f;
1639 char *buff = buf;
1640
1641 mutex_lock(&dev->mutex);
1642
1643 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301644 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001645
1646 mutex_unlock(&dev->mutex);
1647
1648 if (buff != buf)
1649 *(buff-1) = '\n';
1650 return buff - buf;
1651}
1652
1653static ssize_t
1654functions_store(struct device *pdev, struct device_attribute *attr,
1655 const char *buff, size_t size)
1656{
1657 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001658 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001659 char *name;
1660 char buf[256], *b;
1661 int err;
1662
1663 mutex_lock(&dev->mutex);
1664
1665 if (dev->enabled) {
1666 mutex_unlock(&dev->mutex);
1667 return -EBUSY;
1668 }
1669
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001670 /* Clear previous enabled list */
1671 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1672 f->android_dev = NULL;
1673 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001674 INIT_LIST_HEAD(&dev->enabled_functions);
1675
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301676 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001677 b = strim(buf);
1678
1679 while (b) {
1680 name = strsep(&b, ",");
1681 if (name) {
1682 err = android_enable_function(dev, name);
1683 if (err)
1684 pr_err("android_usb: Cannot enable '%s'", name);
1685 }
1686 }
1687
1688 mutex_unlock(&dev->mutex);
1689
1690 return size;
1691}
1692
1693static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1694 char *buf)
1695{
1696 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001697
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301698 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001699}
1700
1701static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1702 const char *buff, size_t size)
1703{
1704 struct android_dev *dev = dev_get_drvdata(pdev);
1705 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001706 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001707 int enabled = 0;
1708
Benoit Gobycf3fc062011-12-19 14:39:37 -08001709 if (!cdev)
1710 return -ENODEV;
1711
Benoit Goby1e8ce152011-12-12 13:01:23 -08001712 mutex_lock(&dev->mutex);
1713
1714 sscanf(buff, "%d", &enabled);
1715 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001716 /*
1717 * Update values in composite driver's copy of
1718 * device descriptor.
1719 */
1720 cdev->desc.idVendor = device_desc.idVendor;
1721 cdev->desc.idProduct = device_desc.idProduct;
1722 cdev->desc.bcdDevice = device_desc.bcdDevice;
1723 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1724 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1725 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001726 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1727 if (f->enable)
1728 f->enable(f);
1729 }
1730 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001731 dev->enabled = true;
1732 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001733 android_disable(dev);
1734 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1735 if (f->disable)
1736 f->disable(f);
1737 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001738 dev->enabled = false;
1739 } else {
1740 pr_err("android_usb: already %s\n",
1741 dev->enabled ? "enabled" : "disabled");
1742 }
1743
1744 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001745
Benoit Gobyaab96812011-04-19 20:37:33 -07001746 return size;
1747}
1748
Ofir Cohen94213a72012-05-03 14:26:32 +03001749static ssize_t pm_qos_show(struct device *pdev,
1750 struct device_attribute *attr, char *buf)
1751{
1752 struct android_dev *dev = dev_get_drvdata(pdev);
1753
1754 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1755}
1756
1757static ssize_t pm_qos_store(struct device *pdev,
1758 struct device_attribute *attr,
1759 const char *buff, size_t size)
1760{
1761 struct android_dev *dev = dev_get_drvdata(pdev);
1762
1763 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1764
Benoit Goby1e8ce152011-12-12 13:01:23 -08001765 return size;
1766}
1767
1768static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1769 char *buf)
1770{
1771 struct android_dev *dev = dev_get_drvdata(pdev);
1772 struct usb_composite_dev *cdev = dev->cdev;
1773 char *state = "DISCONNECTED";
1774 unsigned long flags;
1775
1776 if (!cdev)
1777 goto out;
1778
1779 spin_lock_irqsave(&cdev->lock, flags);
1780 if (cdev->config)
1781 state = "CONFIGURED";
1782 else if (dev->connected)
1783 state = "CONNECTED";
1784 spin_unlock_irqrestore(&cdev->lock, flags);
1785out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301786 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001787}
1788
1789#define DESCRIPTOR_ATTR(field, format_string) \
1790static ssize_t \
1791field ## _show(struct device *dev, struct device_attribute *attr, \
1792 char *buf) \
1793{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301794 return snprintf(buf, PAGE_SIZE, \
1795 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001796} \
1797static ssize_t \
1798field ## _store(struct device *dev, struct device_attribute *attr, \
1799 const char *buf, size_t size) \
1800{ \
1801 int value; \
1802 if (sscanf(buf, format_string, &value) == 1) { \
1803 device_desc.field = value; \
1804 return size; \
1805 } \
1806 return -1; \
1807} \
1808static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1809
1810#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1811static ssize_t \
1812field ## _show(struct device *dev, struct device_attribute *attr, \
1813 char *buf) \
1814{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301815 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001816} \
1817static ssize_t \
1818field ## _store(struct device *dev, struct device_attribute *attr, \
1819 const char *buf, size_t size) \
1820{ \
1821 if (size >= sizeof(buffer)) \
1822 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05301823 strlcpy(buffer, buf, sizeof(buffer)); \
1824 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05301825 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001826} \
1827static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1828
1829
1830DESCRIPTOR_ATTR(idVendor, "%04x\n")
1831DESCRIPTOR_ATTR(idProduct, "%04x\n")
1832DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1833DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1834DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1835DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1836DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1837DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1838DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1839
1840static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1841 functions_store);
1842static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001843static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1844 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001845static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301846static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1847 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001848
1849static struct device_attribute *android_usb_attributes[] = {
1850 &dev_attr_idVendor,
1851 &dev_attr_idProduct,
1852 &dev_attr_bcdDevice,
1853 &dev_attr_bDeviceClass,
1854 &dev_attr_bDeviceSubClass,
1855 &dev_attr_bDeviceProtocol,
1856 &dev_attr_iManufacturer,
1857 &dev_attr_iProduct,
1858 &dev_attr_iSerial,
1859 &dev_attr_functions,
1860 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001861 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001862 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301863 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001864 NULL
1865};
1866
1867/*-------------------------------------------------------------------------*/
1868/* Composite driver */
1869
1870static int android_bind_config(struct usb_configuration *c)
1871{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001872 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001873 int ret = 0;
1874
1875 ret = android_bind_enabled_functions(dev, c);
1876 if (ret)
1877 return ret;
1878
1879 return 0;
1880}
1881
1882static void android_unbind_config(struct usb_configuration *c)
1883{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001884 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001885
1886 android_unbind_enabled_functions(dev, c);
1887}
1888
1889static int android_bind(struct usb_composite_dev *cdev)
1890{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001891 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001892 struct usb_gadget *gadget = cdev->gadget;
1893 int gcnum, id, ret;
1894
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001895 /* Bind to the last android_dev that was probed */
1896 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
1897
1898 dev->cdev = cdev;
1899
Benoit Goby1e8ce152011-12-12 13:01:23 -08001900 /*
1901 * Start disconnected. Userspace will connect the gadget once
1902 * it is done configuring the functions.
1903 */
1904 usb_gadget_disconnect(gadget);
1905
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001906 /* Init the supported functions only once, on the first android_dev */
1907 if (android_dev_count == 1) {
1908 ret = android_init_functions(dev->functions, cdev);
1909 if (ret)
1910 return ret;
1911 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001912
1913 /* Allocate string descriptor numbers ... note that string
1914 * contents can be overridden by the composite_dev glue.
1915 */
1916 id = usb_string_id(cdev);
1917 if (id < 0)
1918 return id;
1919 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1920 device_desc.iManufacturer = id;
1921
1922 id = usb_string_id(cdev);
1923 if (id < 0)
1924 return id;
1925 strings_dev[STRING_PRODUCT_IDX].id = id;
1926 device_desc.iProduct = id;
1927
1928 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301929 strlcpy(manufacturer_string, "Android",
1930 sizeof(manufacturer_string) - 1);
1931 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1932 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001933
1934 id = usb_string_id(cdev);
1935 if (id < 0)
1936 return id;
1937 strings_dev[STRING_SERIAL_IDX].id = id;
1938 device_desc.iSerialNumber = id;
1939
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301940 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001941 dev->config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301942
Benoit Goby1e8ce152011-12-12 13:01:23 -08001943 gcnum = usb_gadget_controller_number(gadget);
1944 if (gcnum >= 0)
1945 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1946 else {
1947 pr_warning("%s: controller '%s' not recognized\n",
1948 longname, gadget->name);
1949 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1950 }
1951
Benoit Goby1e8ce152011-12-12 13:01:23 -08001952 return 0;
1953}
1954
1955static int android_usb_unbind(struct usb_composite_dev *cdev)
1956{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001957 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001958
Lena Salmand092f2d2012-03-12 17:27:24 +02001959 manufacturer_string[0] = '\0';
1960 product_string[0] = '\0';
1961 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08001962 cancel_work_sync(&dev->work);
1963 android_cleanup_functions(dev->functions);
1964 return 0;
1965}
1966
1967static struct usb_composite_driver android_usb_driver = {
1968 .name = "android_usb",
1969 .dev = &device_desc,
1970 .strings = dev_strings,
1971 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001972 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08001973};
1974
1975static int
1976android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1977{
Benoit Goby1e8ce152011-12-12 13:01:23 -08001978 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001979 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001980 struct usb_request *req = cdev->req;
1981 struct android_usb_function *f;
1982 int value = -EOPNOTSUPP;
1983 unsigned long flags;
1984
1985 req->zero = 0;
1986 req->complete = composite_setup_complete;
1987 req->length = 0;
1988 gadget->ep0->driver_data = cdev;
1989
1990 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1991 if (f->ctrlrequest) {
1992 value = f->ctrlrequest(f, cdev, c);
1993 if (value >= 0)
1994 break;
1995 }
1996 }
1997
Benoit Gobycf3fc062011-12-19 14:39:37 -08001998 /* Special case the accessory function.
1999 * It needs to handle control requests before it is enabled.
2000 */
2001 if (value < 0)
2002 value = acc_ctrlrequest(cdev, c);
2003
Benoit Goby1e8ce152011-12-12 13:01:23 -08002004 if (value < 0)
2005 value = composite_setup(gadget, c);
2006
2007 spin_lock_irqsave(&cdev->lock, flags);
2008 if (!dev->connected) {
2009 dev->connected = 1;
2010 schedule_work(&dev->work);
2011 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2012 cdev->config) {
2013 schedule_work(&dev->work);
2014 }
2015 spin_unlock_irqrestore(&cdev->lock, flags);
2016
2017 return value;
2018}
2019
2020static void android_disconnect(struct usb_gadget *gadget)
2021{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002022 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002023 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002024 unsigned long flags;
2025
2026 composite_disconnect(gadget);
2027
2028 spin_lock_irqsave(&cdev->lock, flags);
2029 dev->connected = 0;
2030 schedule_work(&dev->work);
2031 spin_unlock_irqrestore(&cdev->lock, flags);
2032}
2033
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002034static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002035{
2036 struct device_attribute **attrs = android_usb_attributes;
2037 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002038 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002039 int err;
2040
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002041 /*
2042 * The primary usb core should always have usb_core_id=0, since
2043 * Android user space is currently interested in android0 events.
2044 */
2045 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2046 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002047 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002048 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002049 if (IS_ERR(dev->dev))
2050 return PTR_ERR(dev->dev);
2051
2052 dev_set_drvdata(dev->dev, dev);
2053
2054 while ((attr = *attrs++)) {
2055 err = device_create_file(dev->dev, attr);
2056 if (err) {
2057 device_destroy(android_class, dev->dev->devt);
2058 return err;
2059 }
2060 }
2061 return 0;
2062}
2063
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302064static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002065{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302066 struct device_attribute **attrs = android_usb_attributes;
2067 struct device_attribute *attr;
2068
2069 while ((attr = *attrs++))
2070 device_remove_file(dev->dev, attr);
2071 device_destroy(android_class, dev->dev->devt);
2072}
2073
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002074static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2075{
2076 struct android_dev *dev = NULL;
2077
2078 /* Find the android dev from the list */
2079 list_for_each_entry(dev, &android_dev_list, list_item) {
2080 if (dev->cdev == cdev)
2081 break;
2082 }
2083
2084 return dev;
2085}
2086
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087static int __devinit android_probe(struct platform_device *pdev)
2088{
2089 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002090 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02002091 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002092
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002093 if (!android_class) {
2094 android_class = class_create(THIS_MODULE, "android_usb");
2095 if (IS_ERR(android_class))
2096 return PTR_ERR(android_class);
2097 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002098
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002099 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2100 if (!android_dev) {
2101 pr_err("%s(): Failed to alloc memory for android_dev\n",
2102 __func__);
2103 ret = -ENOMEM;
2104 goto err_alloc;
2105 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002106
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002107 android_dev->config.label = pdev->name;
2108 android_dev->config.unbind = android_unbind_config;
2109 android_dev->config.bConfigurationValue = 1;
2110 android_dev->disable_depth = 1;
2111 android_dev->functions = supported_functions;
2112 INIT_LIST_HEAD(&android_dev->enabled_functions);
2113 INIT_WORK(&android_dev->work, android_work);
2114 mutex_init(&android_dev->mutex);
2115
2116 android_dev->pdata = pdata;
2117
2118 list_add_tail(&android_dev->list_item, &android_dev_list);
2119 android_dev_count++;
2120
2121 if (pdata)
2122 composite_driver.usb_core_id = pdata->usb_core_id;
2123 else
2124 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2125
2126 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302127 if (ret) {
2128 pr_err("%s(): android_create_device failed\n", __func__);
2129 goto err_dev;
2130 }
2131
Lena Salmand092f2d2012-03-12 17:27:24 +02002132 ret = usb_composite_probe(&android_usb_driver, android_bind);
2133 if (ret) {
2134 pr_err("%s(): Failed to register android "
2135 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302136 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002137 }
2138
Ofir Cohen94213a72012-05-03 14:26:32 +03002139 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302140 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002141 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002142 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002143 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002144
Lena Salmand092f2d2012-03-12 17:27:24 +02002145 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302146err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002147 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302148err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002149 list_del(&android_dev->list_item);
2150 android_dev_count--;
2151 kfree(android_dev);
2152err_alloc:
2153 if (list_empty(&android_dev_list)) {
2154 class_destroy(android_class);
2155 android_class = NULL;
2156 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302157 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002158}
2159
2160static int android_remove(struct platform_device *pdev)
2161{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002162 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002163 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002164 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302165
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002166 if (pdata)
2167 usb_core_id = pdata->usb_core_id;
2168
2169 /* Find the android dev from the list */
2170 list_for_each_entry(dev, &android_dev_list, list_item) {
2171 if (!dev->pdata)
2172 break; /*To backward compatibility*/
2173 if (dev->pdata->usb_core_id == usb_core_id)
2174 break;
2175 }
2176
2177 if (dev) {
2178 android_destroy_device(dev);
2179 if (pdata && pdata->swfi_latency)
2180 pm_qos_remove_request(&dev->pm_qos_req_dma);
2181 list_del(&dev->list_item);
2182 android_dev_count--;
2183 kfree(dev);
2184 }
2185
2186 if (list_empty(&android_dev_list)) {
2187 class_destroy(android_class);
2188 android_class = NULL;
2189 usb_composite_unregister(&android_usb_driver);
2190 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002191
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192 return 0;
2193}
2194
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002195static const struct platform_device_id android_id_table[] __devinitconst = {
2196 {
2197 .name = "android_usb",
2198 },
2199 {
2200 .name = "android_usb_hsic",
2201 },
2202};
2203
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002204static struct platform_driver android_platform_driver = {
2205 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002206 .probe = android_probe,
2207 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002208 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002210
2211static int __init init(void)
2212{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302213 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002214
Benoit Goby1e8ce152011-12-12 13:01:23 -08002215 /* Override composite driver functions */
2216 composite_driver.setup = android_setup;
2217 composite_driver.disconnect = android_disconnect;
2218
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002219 INIT_LIST_HEAD(&android_dev_list);
2220 android_dev_count = 0;
2221
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302222 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302223 if (ret) {
2224 pr_err("%s(): Failed to register android"
2225 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302226 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002227
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302228 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002229}
2230module_init(init);
2231
2232static void __exit cleanup(void)
2233{
Lena Salmand092f2d2012-03-12 17:27:24 +02002234 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002235}
2236module_exit(cleanup);