blob: 7b34e60bef4b29f7ded47f5324f07d2a83a8e724 [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"
Shimrit Malichia00d7322012-08-05 13:56:28 +030048#include "f_qdss.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053049#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053050#include "f_rmnet_sdio.c"
51#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053052#include "f_rmnet.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080053#include "f_mass_storage.c"
54#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053055#include "u_sdio.c"
56#include "u_smd.c"
57#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053058#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080059#include "u_ctrl_hsic.c"
60#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053061#include "u_ctrl_hsuart.c"
62#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053063#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080064#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080065#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053066#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080067#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080068#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080069#define USB_ETH_RNDIS y
70#include "f_rndis.c"
71#include "rndis.c"
72#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030073#include "u_bam_data.c"
74#include "f_mbim.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030075#include "f_qc_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020076#include "f_qc_rndis.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030077#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053078#ifdef CONFIG_TARGET_CORE
79#include "f_tcm.c"
80#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080081
82MODULE_AUTHOR("Mike Lockwood");
83MODULE_DESCRIPTION("Android Composite USB Driver");
84MODULE_LICENSE("GPL");
85MODULE_VERSION("1.0");
86
87static const char longname[] = "Gadget Android";
88
89/* Default vendor and product IDs, overridden by userspace */
90#define VENDOR_ID 0x18D1
91#define PRODUCT_ID 0x0001
92
Ido Shayevitz23dc77c2012-07-18 16:16:06 +030093#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
94
Benoit Goby1e8ce152011-12-12 13:01:23 -080095struct android_usb_function {
96 char *name;
97 void *config;
98
99 struct device *dev;
100 char *dev_name;
101 struct device_attribute **attributes;
102
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300103 /* for android_conf.enabled_functions */
Benoit Goby1e8ce152011-12-12 13:01:23 -0800104 struct list_head enabled_list;
105
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300106 struct android_dev *android_dev;
107
Benoit Goby1e8ce152011-12-12 13:01:23 -0800108 /* Optional: initialization during gadget bind */
109 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
110 /* Optional: cleanup during gadget unbind */
111 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700112 /* Optional: called when the function is added the list of
113 * enabled functions */
114 void (*enable)(struct android_usb_function *);
115 /* Optional: called when it is removed */
116 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800117
118 int (*bind_config)(struct android_usb_function *,
119 struct usb_configuration *);
120
121 /* Optional: called when the configuration is removed */
122 void (*unbind_config)(struct android_usb_function *,
123 struct usb_configuration *);
124 /* Optional: handle ctrl requests before the device is configured */
125 int (*ctrlrequest)(struct android_usb_function *,
126 struct usb_composite_dev *,
127 const struct usb_ctrlrequest *);
128};
129
130struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300131 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800132 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800133 struct usb_composite_dev *cdev;
134 struct device *dev;
135
136 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700137 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800138 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700139 struct android_usb_platform_data *pdata;
140
Benoit Goby1e8ce152011-12-12 13:01:23 -0800141 bool connected;
142 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300143 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700144 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800145 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300146
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300147 /* A list of struct android_configuration */
148 struct list_head configs;
149 int configs_num;
150
151 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300152 struct list_head list_item;
153
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300154};
155
156struct android_configuration {
157 struct usb_configuration usb_config;
158
159 /* A list of the functions supported by this config */
160 struct list_head enabled_functions;
161
162 /* A list node inside the struct android_dev.configs list */
163 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800164};
165
166static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300167static struct list_head android_dev_list;
168static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800169static int android_bind_config(struct usb_configuration *c);
170static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300171static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300172static struct android_configuration *alloc_android_config
173 (struct android_dev *dev);
174static void free_android_config(struct android_dev *dev,
175 struct android_configuration *conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800176
177/* string IDs are assigned dynamically */
178#define STRING_MANUFACTURER_IDX 0
179#define STRING_PRODUCT_IDX 1
180#define STRING_SERIAL_IDX 2
181
182static char manufacturer_string[256];
183static char product_string[256];
184static char serial_string[256];
185
186/* String Table */
187static struct usb_string strings_dev[] = {
188 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
189 [STRING_PRODUCT_IDX].s = product_string,
190 [STRING_SERIAL_IDX].s = serial_string,
191 { } /* end of list */
192};
193
194static struct usb_gadget_strings stringtab_dev = {
195 .language = 0x0409, /* en-us */
196 .strings = strings_dev,
197};
198
199static struct usb_gadget_strings *dev_strings[] = {
200 &stringtab_dev,
201 NULL,
202};
203
204static struct usb_device_descriptor device_desc = {
205 .bLength = sizeof(device_desc),
206 .bDescriptorType = USB_DT_DEVICE,
207 .bcdUSB = __constant_cpu_to_le16(0x0200),
208 .bDeviceClass = USB_CLASS_PER_INTERFACE,
209 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
210 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
211 .bcdDevice = __constant_cpu_to_le16(0xffff),
212 .bNumConfigurations = 1,
213};
214
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530215static struct usb_otg_descriptor otg_descriptor = {
216 .bLength = sizeof otg_descriptor,
217 .bDescriptorType = USB_DT_OTG,
218 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
219 .bcdOTG = __constant_cpu_to_le16(0x0200),
220};
221
222static const struct usb_descriptor_header *otg_desc[] = {
223 (struct usb_descriptor_header *) &otg_descriptor,
224 NULL,
225};
226
Manu Gautama2b54142012-04-03 14:34:32 +0530227enum android_device_state {
228 USB_DISCONNECTED,
229 USB_CONNECTED,
230 USB_CONFIGURED,
231};
232
Ofir Cohen94213a72012-05-03 14:26:32 +0300233static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
234{
235 struct android_usb_platform_data *pdata = dev->pdata;
236 u32 swfi_latency = 0;
237 static int last_vote = -1;
238
Ofir Cohen56eb7072012-05-20 11:41:39 +0300239 if (!pdata || vote == last_vote
240 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300241 return;
242
243 swfi_latency = pdata->swfi_latency + 1;
244 if (vote)
245 pm_qos_update_request(&dev->pm_qos_req_dma,
246 swfi_latency);
247 else
248 pm_qos_update_request(&dev->pm_qos_req_dma,
249 PM_QOS_DEFAULT_VALUE);
250 last_vote = vote;
251}
252
Benoit Goby1e8ce152011-12-12 13:01:23 -0800253static void android_work(struct work_struct *data)
254{
255 struct android_dev *dev = container_of(data, struct android_dev, work);
256 struct usb_composite_dev *cdev = dev->cdev;
257 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
258 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
259 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
260 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530261 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800262 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300263 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800264
265 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530266 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800267 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530268 next_state = USB_CONFIGURED;
269 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800270 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530271 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300272 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300273 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300274 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300275 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530276 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800277 dev->sw_connected = dev->connected;
278 spin_unlock_irqrestore(&cdev->lock, flags);
279
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300280 if (pm_qos_vote != -1)
281 android_pm_qos_update_latency(dev, pm_qos_vote);
282
Benoit Goby1e8ce152011-12-12 13:01:23 -0800283 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530284 /*
285 * Some userspace modules, e.g. MTP, work correctly only if
286 * CONFIGURED uevent is preceded by DISCONNECT uevent.
287 * Check if we missed sending out a DISCONNECT uevent. This can
288 * happen if host PC resets and configures device really quick.
289 */
290 if (((uevent_envp == connected) &&
291 (last_uevent != USB_DISCONNECTED)) ||
292 ((uevent_envp == configured) &&
293 (last_uevent == USB_CONFIGURED))) {
294 pr_info("%s: sent missed DISCONNECT event\n", __func__);
295 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
296 disconnected);
297 msleep(20);
298 }
299 /*
300 * Before sending out CONFIGURED uevent give function drivers
301 * a chance to wakeup userspace threads and notify disconnect
302 */
303 if (uevent_envp == configured)
304 msleep(50);
305
Benoit Goby1e8ce152011-12-12 13:01:23 -0800306 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530307 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800308 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
309 } else {
310 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
311 dev->connected, dev->sw_connected, cdev->config);
312 }
313}
314
Benoit Goby80ba14d2012-03-19 18:56:52 -0700315static void android_enable(struct android_dev *dev)
316{
317 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300318 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700319
320 if (WARN_ON(!dev->disable_depth))
321 return;
322
323 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300324
325 list_for_each_entry(conf, &dev->configs, list_item)
326 usb_add_config(cdev, &conf->usb_config,
327 android_bind_config);
328
Benoit Goby80ba14d2012-03-19 18:56:52 -0700329 usb_gadget_connect(cdev->gadget);
330 }
331}
332
333static void android_disable(struct android_dev *dev)
334{
335 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300336 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700337
338 if (dev->disable_depth++ == 0) {
339 usb_gadget_disconnect(cdev->gadget);
340 /* Cancel pending control requests */
341 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300342
343 list_for_each_entry(conf, &dev->configs, list_item)
344 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700345 }
346}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800347
348/*-------------------------------------------------------------------------*/
349/* Supported functions initialization */
350
Benoit Goby80ba14d2012-03-19 18:56:52 -0700351struct adb_data {
352 bool opened;
353 bool enabled;
354};
355
Benoit Goby2b6862d2011-12-19 14:38:41 -0800356static int
357adb_function_init(struct android_usb_function *f,
358 struct usb_composite_dev *cdev)
359{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700360 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
361 if (!f->config)
362 return -ENOMEM;
363
Benoit Goby2b6862d2011-12-19 14:38:41 -0800364 return adb_setup();
365}
366
367static void adb_function_cleanup(struct android_usb_function *f)
368{
369 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700370 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800371}
372
373static int
374adb_function_bind_config(struct android_usb_function *f,
375 struct usb_configuration *c)
376{
377 return adb_bind_config(c);
378}
379
Benoit Goby80ba14d2012-03-19 18:56:52 -0700380static void adb_android_function_enable(struct android_usb_function *f)
381{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300382 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700383 struct adb_data *data = f->config;
384
385 data->enabled = true;
386
387 /* Disable the gadget until adbd is ready */
388 if (!data->opened)
389 android_disable(dev);
390}
391
392static void adb_android_function_disable(struct android_usb_function *f)
393{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300394 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700395 struct adb_data *data = f->config;
396
397 data->enabled = false;
398
399 /* Balance the disable that was called in closed_callback */
400 if (!data->opened)
401 android_enable(dev);
402}
403
Benoit Goby2b6862d2011-12-19 14:38:41 -0800404static struct android_usb_function adb_function = {
405 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700406 .enable = adb_android_function_enable,
407 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800408 .init = adb_function_init,
409 .cleanup = adb_function_cleanup,
410 .bind_config = adb_function_bind_config,
411};
412
Benoit Goby80ba14d2012-03-19 18:56:52 -0700413static void adb_ready_callback(void)
414{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300415 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700416 struct adb_data *data = adb_function.config;
417
Benoit Goby80ba14d2012-03-19 18:56:52 -0700418 data->opened = true;
419
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300420 if (data->enabled && dev) {
421 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700422 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300423 mutex_unlock(&dev->mutex);
424 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700425}
426
427static void adb_closed_callback(void)
428{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300429 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700430 struct adb_data *data = adb_function.config;
431
Benoit Goby80ba14d2012-03-19 18:56:52 -0700432 data->opened = false;
433
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300434 if (data->enabled) {
435 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700436 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300437 mutex_unlock(&dev->mutex);
438 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700439}
440
Benoit Goby2b6862d2011-12-19 14:38:41 -0800441
Benoit Gobyaab96812011-04-19 20:37:33 -0700442/*-------------------------------------------------------------------------*/
443/* Supported functions initialization */
444
Manu Gautam8e0719b2011-09-26 14:47:55 +0530445/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530446static int rmnet_smd_function_bind_config(struct android_usb_function *f,
447 struct usb_configuration *c)
448{
449 return rmnet_smd_bind_config(c);
450}
451
452static struct android_usb_function rmnet_smd_function = {
453 .name = "rmnet_smd",
454 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800455};
456
Manu Gautam8e0719b2011-09-26 14:47:55 +0530457/* RMNET_SDIO */
458static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
459 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800460{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530461 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800462}
463
Manu Gautam8e0719b2011-09-26 14:47:55 +0530464static struct android_usb_function rmnet_sdio_function = {
465 .name = "rmnet_sdio",
466 .bind_config = rmnet_sdio_function_bind_config,
467};
468
469/* RMNET_SMD_SDIO */
470static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
471 struct usb_composite_dev *cdev)
472{
473 return rmnet_smd_sdio_init();
474}
475
476static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
477{
478 rmnet_smd_sdio_cleanup();
479}
480
481static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
482 struct usb_configuration *c)
483{
484 return rmnet_smd_sdio_function_add(c);
485}
486
487static struct device_attribute *rmnet_smd_sdio_attributes[] = {
488 &dev_attr_transport, NULL };
489
490static struct android_usb_function rmnet_smd_sdio_function = {
491 .name = "rmnet_smd_sdio",
492 .init = rmnet_smd_sdio_function_init,
493 .cleanup = rmnet_smd_sdio_function_cleanup,
494 .bind_config = rmnet_smd_sdio_bind_config,
495 .attributes = rmnet_smd_sdio_attributes,
496};
497
Hemant Kumar1b820d52011-11-03 15:08:28 -0700498/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
499#define MAX_XPORT_STR_LEN 50
500static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530501
Manu Gautame3e897c2011-09-12 17:18:46 +0530502static void rmnet_function_cleanup(struct android_usb_function *f)
503{
504 frmnet_cleanup();
505}
506
Manu Gautam2b0234a2011-09-07 16:47:52 +0530507static int rmnet_function_bind_config(struct android_usb_function *f,
508 struct usb_configuration *c)
509{
510 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700511 int err = 0;
512 char *ctrl_name;
513 char *data_name;
514 char buf[MAX_XPORT_STR_LEN], *b;
515 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530516
Hemant Kumar1b820d52011-11-03 15:08:28 -0700517 if (!rmnet_initialized) {
518 rmnet_initialized = 1;
519 strlcpy(buf, rmnet_transports, sizeof(buf));
520 b = strim(buf);
521 while (b) {
522 ctrl_name = strsep(&b, ",");
523 data_name = strsep(&b, ",");
524 if (ctrl_name && data_name) {
525 err = frmnet_init_port(ctrl_name, data_name);
526 if (err) {
527 pr_err("rmnet: Cannot open ctrl port:"
528 "'%s' data port:'%s'\n",
529 ctrl_name, data_name);
530 goto out;
531 }
532 ports++;
533 }
534 }
535
536 err = rmnet_gport_setup();
537 if (err) {
538 pr_err("rmnet: Cannot setup transports");
539 goto out;
540 }
541 }
542
543 for (i = 0; i < ports; i++) {
544 err = frmnet_bind_config(c, i);
545 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530546 pr_err("Could not bind rmnet%u config\n", i);
547 break;
548 }
549 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700550out:
551 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530552}
553
Hemant Kumar1b820d52011-11-03 15:08:28 -0700554static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530555 struct device_attribute *attr, char *buf)
556{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700557 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530558}
559
Hemant Kumar1b820d52011-11-03 15:08:28 -0700560static ssize_t rmnet_transports_store(
561 struct device *device, struct device_attribute *attr,
562 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530563{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700564 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530565
Manu Gautam2b0234a2011-09-07 16:47:52 +0530566 return size;
567}
568
Hemant Kumar1b820d52011-11-03 15:08:28 -0700569static struct device_attribute dev_attr_rmnet_transports =
570 __ATTR(transports, S_IRUGO | S_IWUSR,
571 rmnet_transports_show,
572 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530573static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700574 &dev_attr_rmnet_transports,
575 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530576
577static struct android_usb_function rmnet_function = {
578 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530579 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530580 .bind_config = rmnet_function_bind_config,
581 .attributes = rmnet_function_attributes,
582};
583
Ofir Cohen7b155422012-07-31 13:02:49 +0300584struct ecm_function_config {
585 u8 ethaddr[ETH_ALEN];
586};
587
588static int ecm_function_init(struct android_usb_function *f,
589 struct usb_composite_dev *cdev)
590{
591 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
592 if (!f->config)
593 return -ENOMEM;
594 return 0;
595}
596
597static void ecm_function_cleanup(struct android_usb_function *f)
598{
599 kfree(f->config);
600 f->config = NULL;
601}
602
603static int ecm_qc_function_bind_config(struct android_usb_function *f,
604 struct usb_configuration *c)
605{
606 int ret;
607 struct ecm_function_config *ecm = f->config;
608
609 if (!ecm) {
610 pr_err("%s: ecm_pdata\n", __func__);
611 return -EINVAL;
612 }
613
614 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
615 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
616 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
617
618 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
619 if (ret) {
620 pr_err("%s: gether_setup failed\n", __func__);
621 return ret;
622 }
623
624 return ecm_qc_bind_config(c, ecm->ethaddr);
625}
626
627static void ecm_qc_function_unbind_config(struct android_usb_function *f,
628 struct usb_configuration *c)
629{
630 gether_qc_cleanup();
631}
632
633static ssize_t ecm_ethaddr_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
635{
636 struct android_usb_function *f = dev_get_drvdata(dev);
637 struct ecm_function_config *ecm = f->config;
638 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
639 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
640 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
641}
642
643static ssize_t ecm_ethaddr_store(struct device *dev,
644 struct device_attribute *attr, const char *buf, size_t size)
645{
646 struct android_usb_function *f = dev_get_drvdata(dev);
647 struct ecm_function_config *ecm = f->config;
648
649 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
650 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
651 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
652 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
653 return size;
654 return -EINVAL;
655}
656
657static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
658 ecm_ethaddr_store);
659
660static struct device_attribute *ecm_function_attributes[] = {
661 &dev_attr_ecm_ethaddr,
662 NULL
663};
664
665static struct android_usb_function ecm_qc_function = {
666 .name = "ecm_qc",
667 .init = ecm_function_init,
668 .cleanup = ecm_function_cleanup,
669 .bind_config = ecm_qc_function_bind_config,
670 .unbind_config = ecm_qc_function_unbind_config,
671 .attributes = ecm_function_attributes,
672};
Anna Perela8c991d2012-04-09 16:44:46 +0300673
674/* MBIM - used with BAM */
675#define MAX_MBIM_INSTANCES 1
676
677static int mbim_function_init(struct android_usb_function *f,
678 struct usb_composite_dev *cdev)
679{
680 return mbim_init(MAX_MBIM_INSTANCES);
681}
682
683static void mbim_function_cleanup(struct android_usb_function *f)
684{
685 fmbim_cleanup();
686}
687
688static int mbim_function_bind_config(struct android_usb_function *f,
689 struct usb_configuration *c)
690{
691 return mbim_bind_config(c, 0);
692}
693
694static struct android_usb_function mbim_function = {
695 .name = "usb_mbim",
696 .cleanup = mbim_function_cleanup,
697 .bind_config = mbim_function_bind_config,
698 .init = mbim_function_init,
699};
700
701
Manu Gautam8e0719b2011-09-26 14:47:55 +0530702/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530703static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704static ssize_t clients_store(
705 struct device *device, struct device_attribute *attr,
706 const char *buff, size_t size)
707{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530708 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709
710 return size;
711}
712
713static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
714static struct device_attribute *diag_function_attributes[] =
715 { &dev_attr_clients, NULL };
716
717static int diag_function_init(struct android_usb_function *f,
718 struct usb_composite_dev *cdev)
719{
720 return diag_setup();
721}
722
723static void diag_function_cleanup(struct android_usb_function *f)
724{
725 diag_cleanup();
726}
727
728static int diag_function_bind_config(struct android_usb_function *f,
729 struct usb_configuration *c)
730{
731 char *name;
732 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530733 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800734 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300735 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530737 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 b = strim(buf);
739
740 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800741 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530743 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300744 if (dev->pdata && !once++)
745 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530746
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530748 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 if (err)
750 pr_err("diag: Cannot open channel '%s'", name);
751 }
752 }
753
754 return err;
755}
756
757static struct android_usb_function diag_function = {
758 .name = "diag",
759 .init = diag_function_init,
760 .cleanup = diag_function_cleanup,
761 .bind_config = diag_function_bind_config,
762 .attributes = diag_function_attributes,
763};
764
Shimrit Malichia00d7322012-08-05 13:56:28 +0300765/* DEBUG */
766static int qdss_function_init(struct android_usb_function *f,
767 struct usb_composite_dev *cdev)
768{
769 return qdss_setup();
770}
771
772static void qdss_function_cleanup(struct android_usb_function *f)
773{
774 qdss_cleanup();
775}
776
777static int qdss_function_bind_config(struct android_usb_function *f,
778 struct usb_configuration *c)
779{
780 int err = -1;
781
782 err = qdss_bind_config(c, "qdss");
783 if (err)
784 pr_err("qdss: Cannot open channel qdss");
785
786 return err;
787}
788
789static struct android_usb_function qdss_function = {
790 .name = "qdss",
791 .init = qdss_function_init,
792 .cleanup = qdss_function_cleanup,
793 .bind_config = qdss_function_bind_config,
794};
795
Manu Gautam8e0719b2011-09-26 14:47:55 +0530796/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530797static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530798static ssize_t serial_transports_store(
799 struct device *device, struct device_attribute *attr,
800 const char *buff, size_t size)
801{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530802 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530803
804 return size;
805}
806
807static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
808static struct device_attribute *serial_function_attributes[] =
809 { &dev_attr_transports, NULL };
810
811static void serial_function_cleanup(struct android_usb_function *f)
812{
813 gserial_cleanup();
814}
815
816static int serial_function_bind_config(struct android_usb_function *f,
817 struct usb_configuration *c)
818{
819 char *name;
820 char buf[32], *b;
821 int err = -1, i;
822 static int serial_initialized = 0, ports = 0;
823
824 if (serial_initialized)
825 goto bind_config;
826
827 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530828 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530829 b = strim(buf);
830
831 while (b) {
832 name = strsep(&b, ",");
833
834 if (name) {
835 err = gserial_init_port(ports, name);
836 if (err) {
837 pr_err("serial: Cannot open port '%s'", name);
838 goto out;
839 }
840 ports++;
841 }
842 }
843 err = gport_setup(c);
844 if (err) {
845 pr_err("serial: Cannot setup transports");
846 goto out;
847 }
848
849bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200850 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530851 err = gser_bind_config(c, i);
852 if (err) {
853 pr_err("serial: bind_config failed for port %d", i);
854 goto out;
855 }
856 }
857
858out:
859 return err;
860}
861
862static struct android_usb_function serial_function = {
863 .name = "serial",
864 .cleanup = serial_function_cleanup,
865 .bind_config = serial_function_bind_config,
866 .attributes = serial_function_attributes,
867};
868
Anji jonnala92be1b42011-12-19 09:44:41 +0530869/* ACM */
870static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
871static ssize_t acm_transports_store(
872 struct device *device, struct device_attribute *attr,
873 const char *buff, size_t size)
874{
875 strlcpy(acm_transports, buff, sizeof(acm_transports));
876
877 return size;
878}
879
880static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
881static struct device_attribute *acm_function_attributes[] = {
882 &dev_attr_acm_transports, NULL };
883
Benoit Goby1e8ce152011-12-12 13:01:23 -0800884static void acm_function_cleanup(struct android_usb_function *f)
885{
886 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800887}
888
Anji jonnala92be1b42011-12-19 09:44:41 +0530889static int acm_function_bind_config(struct android_usb_function *f,
890 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800891{
Anji jonnala92be1b42011-12-19 09:44:41 +0530892 char *name;
893 char buf[32], *b;
894 int err = -1, i;
895 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800896
Anji jonnala92be1b42011-12-19 09:44:41 +0530897 if (acm_initialized)
898 goto bind_config;
899
900 acm_initialized = 1;
901 strlcpy(buf, acm_transports, sizeof(buf));
902 b = strim(buf);
903
904 while (b) {
905 name = strsep(&b, ",");
906
907 if (name) {
908 err = acm_init_port(ports, name);
909 if (err) {
910 pr_err("acm: Cannot open port '%s'", name);
911 goto out;
912 }
913 ports++;
914 }
915 }
916 err = acm_port_setup(c);
917 if (err) {
918 pr_err("acm: Cannot setup transports");
919 goto out;
920 }
921
922bind_config:
923 for (i = 0; i < ports; i++) {
924 err = acm_bind_config(c, i);
925 if (err) {
926 pr_err("acm: bind_config failed for port %d", i);
927 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800928 }
929 }
930
Anji jonnala92be1b42011-12-19 09:44:41 +0530931out:
932 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800933}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800934static struct android_usb_function acm_function = {
935 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800936 .cleanup = acm_function_cleanup,
937 .bind_config = acm_function_bind_config,
938 .attributes = acm_function_attributes,
939};
940
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530941/* CCID */
942static int ccid_function_init(struct android_usb_function *f,
943 struct usb_composite_dev *cdev)
944{
945 return ccid_setup();
946}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800947
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530948static void ccid_function_cleanup(struct android_usb_function *f)
949{
950 ccid_cleanup();
951}
952
953static int ccid_function_bind_config(struct android_usb_function *f,
954 struct usb_configuration *c)
955{
956 return ccid_bind_config(c);
957}
958
959static struct android_usb_function ccid_function = {
960 .name = "ccid",
961 .init = ccid_function_init,
962 .cleanup = ccid_function_cleanup,
963 .bind_config = ccid_function_bind_config,
964};
965
Steve Mucklef132c6c2012-06-06 18:30:57 -0700966static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800967 struct usb_composite_dev *cdev)
968{
969 return mtp_setup();
970}
971
972static void mtp_function_cleanup(struct android_usb_function *f)
973{
974 mtp_cleanup();
975}
976
Steve Mucklef132c6c2012-06-06 18:30:57 -0700977static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800978 struct usb_configuration *c)
979{
980 return mtp_bind_config(c, false);
981}
982
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400983static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800984{
985 /* nothing to do - initialization is handled by mtp_function_init */
986 return 0;
987}
988
989static void ptp_function_cleanup(struct android_usb_function *f)
990{
991 /* nothing to do - cleanup is handled by mtp_function_cleanup */
992}
993
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400994static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800995{
996 return mtp_bind_config(c, true);
997}
998
999static int mtp_function_ctrlrequest(struct android_usb_function *f,
1000 struct usb_composite_dev *cdev,
1001 const struct usb_ctrlrequest *c)
1002{
1003 return mtp_ctrlrequest(cdev, c);
1004}
1005
1006static struct android_usb_function mtp_function = {
1007 .name = "mtp",
1008 .init = mtp_function_init,
1009 .cleanup = mtp_function_cleanup,
1010 .bind_config = mtp_function_bind_config,
1011 .ctrlrequest = mtp_function_ctrlrequest,
1012};
1013
1014/* PTP function is same as MTP with slightly different interface descriptor */
1015static struct android_usb_function ptp_function = {
1016 .name = "ptp",
1017 .init = ptp_function_init,
1018 .cleanup = ptp_function_cleanup,
1019 .bind_config = ptp_function_bind_config,
1020};
1021
1022
Benoit Goby1e8ce152011-12-12 13:01:23 -08001023struct rndis_function_config {
1024 u8 ethaddr[ETH_ALEN];
1025 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001026 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001027 char manufacturer[256];
1028 /* "Wireless" RNDIS; auto-detected by Windows */
1029 bool wceis;
1030};
1031
1032static int
1033rndis_function_init(struct android_usb_function *f,
1034 struct usb_composite_dev *cdev)
1035{
1036 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1037 if (!f->config)
1038 return -ENOMEM;
1039 return 0;
1040}
1041
1042static void rndis_function_cleanup(struct android_usb_function *f)
1043{
1044 kfree(f->config);
1045 f->config = NULL;
1046}
1047
Ofir Cohenaef90b72012-07-31 12:37:04 +02001048static int rndis_qc_function_init(struct android_usb_function *f,
1049 struct usb_composite_dev *cdev)
1050{
1051 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1052 if (!f->config)
1053 return -ENOMEM;
1054
1055 return rndis_qc_init();
1056}
1057
1058static void rndis_qc_function_cleanup(struct android_usb_function *f)
1059{
1060 rndis_qc_cleanup();
1061 kfree(f->config);
1062}
1063
Benoit Goby1e8ce152011-12-12 13:01:23 -08001064static int
1065rndis_function_bind_config(struct android_usb_function *f,
1066 struct usb_configuration *c)
1067{
1068 int ret;
1069 struct rndis_function_config *rndis = f->config;
1070
1071 if (!rndis) {
1072 pr_err("%s: rndis_pdata\n", __func__);
1073 return -1;
1074 }
1075
1076 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1077 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1078 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1079
1080 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1081 if (ret) {
1082 pr_err("%s: gether_setup failed\n", __func__);
1083 return ret;
1084 }
1085
1086 if (rndis->wceis) {
1087 /* "Wireless" RNDIS; auto-detected by Windows */
1088 rndis_iad_descriptor.bFunctionClass =
1089 USB_CLASS_WIRELESS_CONTROLLER;
1090 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1091 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1092 rndis_control_intf.bInterfaceClass =
1093 USB_CLASS_WIRELESS_CONTROLLER;
1094 rndis_control_intf.bInterfaceSubClass = 0x01;
1095 rndis_control_intf.bInterfaceProtocol = 0x03;
1096 }
1097
1098 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1099 rndis->manufacturer);
1100}
1101
Ofir Cohenaef90b72012-07-31 12:37:04 +02001102static int rndis_qc_function_bind_config(struct android_usb_function *f,
1103 struct usb_configuration *c)
1104{
1105 int ret;
1106 struct rndis_function_config *rndis = f->config;
1107
1108 if (!rndis) {
1109 pr_err("%s: rndis_pdata\n", __func__);
1110 return -EINVAL;
1111 }
1112
1113 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1114 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1115 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1116
1117 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1118 if (ret) {
1119 pr_err("%s: gether_setup failed\n", __func__);
1120 return ret;
1121 }
1122
1123 if (rndis->wceis) {
1124 /* "Wireless" RNDIS; auto-detected by Windows */
1125 rndis_qc_iad_descriptor.bFunctionClass =
1126 USB_CLASS_WIRELESS_CONTROLLER;
1127 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1128 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1129 rndis_qc_control_intf.bInterfaceClass =
1130 USB_CLASS_WIRELESS_CONTROLLER;
1131 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1132 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1133 }
1134
1135 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1136 rndis->manufacturer,
1137 rndis->max_pkt_per_xfer);
1138}
1139
Benoit Goby1e8ce152011-12-12 13:01:23 -08001140static void rndis_function_unbind_config(struct android_usb_function *f,
1141 struct usb_configuration *c)
1142{
1143 gether_cleanup();
1144}
1145
Ofir Cohenaef90b72012-07-31 12:37:04 +02001146static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1147 struct usb_configuration *c)
1148{
1149 gether_qc_cleanup();
1150}
1151
Benoit Goby1e8ce152011-12-12 13:01:23 -08001152static ssize_t rndis_manufacturer_show(struct device *dev,
1153 struct device_attribute *attr, char *buf)
1154{
1155 struct android_usb_function *f = dev_get_drvdata(dev);
1156 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001157
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301158 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001159}
1160
1161static ssize_t rndis_manufacturer_store(struct device *dev,
1162 struct device_attribute *attr, const char *buf, size_t size)
1163{
1164 struct android_usb_function *f = dev_get_drvdata(dev);
1165 struct rndis_function_config *config = f->config;
1166
1167 if (size >= sizeof(config->manufacturer))
1168 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001169
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301170 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001171 return size;
1172 return -1;
1173}
1174
1175static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1176 rndis_manufacturer_store);
1177
1178static ssize_t rndis_wceis_show(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1180{
1181 struct android_usb_function *f = dev_get_drvdata(dev);
1182 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001183
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301184 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001185}
1186
1187static ssize_t rndis_wceis_store(struct device *dev,
1188 struct device_attribute *attr, const char *buf, size_t size)
1189{
1190 struct android_usb_function *f = dev_get_drvdata(dev);
1191 struct rndis_function_config *config = f->config;
1192 int value;
1193
1194 if (sscanf(buf, "%d", &value) == 1) {
1195 config->wceis = value;
1196 return size;
1197 }
1198 return -EINVAL;
1199}
1200
1201static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1202 rndis_wceis_store);
1203
1204static ssize_t rndis_ethaddr_show(struct device *dev,
1205 struct device_attribute *attr, char *buf)
1206{
1207 struct android_usb_function *f = dev_get_drvdata(dev);
1208 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001209
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301210 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001211 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1212 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1213}
1214
1215static ssize_t rndis_ethaddr_store(struct device *dev,
1216 struct device_attribute *attr, const char *buf, size_t size)
1217{
1218 struct android_usb_function *f = dev_get_drvdata(dev);
1219 struct rndis_function_config *rndis = f->config;
1220
1221 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1222 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1223 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1224 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1225 return size;
1226 return -EINVAL;
1227}
1228
1229static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1230 rndis_ethaddr_store);
1231
1232static ssize_t rndis_vendorID_show(struct device *dev,
1233 struct device_attribute *attr, char *buf)
1234{
1235 struct android_usb_function *f = dev_get_drvdata(dev);
1236 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001237
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301238 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001239}
1240
1241static ssize_t rndis_vendorID_store(struct device *dev,
1242 struct device_attribute *attr, const char *buf, size_t size)
1243{
1244 struct android_usb_function *f = dev_get_drvdata(dev);
1245 struct rndis_function_config *config = f->config;
1246 int value;
1247
1248 if (sscanf(buf, "%04x", &value) == 1) {
1249 config->vendorID = value;
1250 return size;
1251 }
1252 return -EINVAL;
1253}
1254
1255static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1256 rndis_vendorID_store);
1257
Ofir Cohenaef90b72012-07-31 12:37:04 +02001258static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1259 struct device_attribute *attr, char *buf)
1260{
1261 struct android_usb_function *f = dev_get_drvdata(dev);
1262 struct rndis_function_config *config = f->config;
1263 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1264}
1265
1266static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1267 struct device_attribute *attr, const char *buf, size_t size)
1268{
1269 struct android_usb_function *f = dev_get_drvdata(dev);
1270 struct rndis_function_config *config = f->config;
1271 int value;
1272
1273 if (sscanf(buf, "%d", &value) == 1) {
1274 config->max_pkt_per_xfer = value;
1275 return size;
1276 }
1277 return -EINVAL;
1278}
1279
1280static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1281 rndis_max_pkt_per_xfer_show,
1282 rndis_max_pkt_per_xfer_store);
1283
Benoit Goby1e8ce152011-12-12 13:01:23 -08001284static struct device_attribute *rndis_function_attributes[] = {
1285 &dev_attr_manufacturer,
1286 &dev_attr_wceis,
1287 &dev_attr_ethaddr,
1288 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001289 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001290 NULL
1291};
1292
1293static struct android_usb_function rndis_function = {
1294 .name = "rndis",
1295 .init = rndis_function_init,
1296 .cleanup = rndis_function_cleanup,
1297 .bind_config = rndis_function_bind_config,
1298 .unbind_config = rndis_function_unbind_config,
1299 .attributes = rndis_function_attributes,
1300};
1301
Ofir Cohenaef90b72012-07-31 12:37:04 +02001302static struct android_usb_function rndis_qc_function = {
1303 .name = "rndis_qc",
1304 .init = rndis_qc_function_init,
1305 .cleanup = rndis_qc_function_cleanup,
1306 .bind_config = rndis_qc_function_bind_config,
1307 .unbind_config = rndis_qc_function_unbind_config,
1308 .attributes = rndis_function_attributes,
1309};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001310
1311struct mass_storage_function_config {
1312 struct fsg_config fsg;
1313 struct fsg_common *common;
1314};
1315
1316static int mass_storage_function_init(struct android_usb_function *f,
1317 struct usb_composite_dev *cdev)
1318{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001319 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001320 struct mass_storage_function_config *config;
1321 struct fsg_common *common;
1322 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301323 int i;
1324 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001325
1326 config = kzalloc(sizeof(struct mass_storage_function_config),
1327 GFP_KERNEL);
1328 if (!config)
1329 return -ENOMEM;
1330
1331 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301332 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301333 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301334 config->fsg.nluns = 2;
1335 config->fsg.luns[1].cdrom = 1;
1336 config->fsg.luns[1].ro = 1;
1337 config->fsg.luns[1].removable = 1;
1338 name[1] = "lun0";
1339 }
1340
Benoit Goby1e8ce152011-12-12 13:01:23 -08001341 config->fsg.luns[0].removable = 1;
1342
1343 common = fsg_common_init(NULL, cdev, &config->fsg);
1344 if (IS_ERR(common)) {
1345 kfree(config);
1346 return PTR_ERR(common);
1347 }
1348
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301349 for (i = 0; i < config->fsg.nluns; i++) {
1350 err = sysfs_create_link(&f->dev->kobj,
1351 &common->luns[i].dev.kobj,
1352 name[i]);
1353 if (err)
1354 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001355 }
1356
1357 config->common = common;
1358 f->config = config;
1359 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301360error:
1361 for (; i > 0 ; i--)
1362 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1363
1364 fsg_common_release(&common->ref);
1365 kfree(config);
1366 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001367}
1368
1369static void mass_storage_function_cleanup(struct android_usb_function *f)
1370{
1371 kfree(f->config);
1372 f->config = NULL;
1373}
1374
1375static int mass_storage_function_bind_config(struct android_usb_function *f,
1376 struct usb_configuration *c)
1377{
1378 struct mass_storage_function_config *config = f->config;
1379 return fsg_bind_config(c->cdev, c, config->common);
1380}
1381
1382static ssize_t mass_storage_inquiry_show(struct device *dev,
1383 struct device_attribute *attr, char *buf)
1384{
1385 struct android_usb_function *f = dev_get_drvdata(dev);
1386 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301387 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001388}
1389
1390static ssize_t mass_storage_inquiry_store(struct device *dev,
1391 struct device_attribute *attr, const char *buf, size_t size)
1392{
1393 struct android_usb_function *f = dev_get_drvdata(dev);
1394 struct mass_storage_function_config *config = f->config;
1395 if (size >= sizeof(config->common->inquiry_string))
1396 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301397 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001398 return -EINVAL;
1399 return size;
1400}
1401
1402static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1403 mass_storage_inquiry_show,
1404 mass_storage_inquiry_store);
1405
1406static struct device_attribute *mass_storage_function_attributes[] = {
1407 &dev_attr_inquiry_string,
1408 NULL
1409};
1410
1411static struct android_usb_function mass_storage_function = {
1412 .name = "mass_storage",
1413 .init = mass_storage_function_init,
1414 .cleanup = mass_storage_function_cleanup,
1415 .bind_config = mass_storage_function_bind_config,
1416 .attributes = mass_storage_function_attributes,
1417};
1418
1419
Benoit Gobycf3fc062011-12-19 14:39:37 -08001420static int accessory_function_init(struct android_usb_function *f,
1421 struct usb_composite_dev *cdev)
1422{
1423 return acc_setup();
1424}
1425
1426static void accessory_function_cleanup(struct android_usb_function *f)
1427{
1428 acc_cleanup();
1429}
1430
1431static int accessory_function_bind_config(struct android_usb_function *f,
1432 struct usb_configuration *c)
1433{
1434 return acc_bind_config(c);
1435}
1436
1437static int accessory_function_ctrlrequest(struct android_usb_function *f,
1438 struct usb_composite_dev *cdev,
1439 const struct usb_ctrlrequest *c)
1440{
1441 return acc_ctrlrequest(cdev, c);
1442}
1443
1444static struct android_usb_function accessory_function = {
1445 .name = "accessory",
1446 .init = accessory_function_init,
1447 .cleanup = accessory_function_cleanup,
1448 .bind_config = accessory_function_bind_config,
1449 .ctrlrequest = accessory_function_ctrlrequest,
1450};
1451
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301452static int android_uasp_connect_cb(bool connect)
1453{
1454 /*
1455 * TODO
1456 * We may have to disable gadget till UASP configfs nodes
1457 * are configured which includes mapping LUN with the
1458 * backing file. It is a fundamental difference between
1459 * f_mass_storage and f_tcp. That means UASP can not be
1460 * in default composition.
1461 *
1462 * For now, assume that UASP configfs nodes are configured
1463 * before enabling android gadget. Or cable should be
1464 * reconnected after mapping the LUN.
1465 *
1466 * Also consider making UASP to respond to Host requests when
1467 * Lun is not mapped.
1468 */
1469 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1470
1471 return 0;
1472}
1473
1474static int uasp_function_init(struct android_usb_function *f,
1475 struct usb_composite_dev *cdev)
1476{
1477 return f_tcm_init(&android_uasp_connect_cb);
1478}
1479
1480static void uasp_function_cleanup(struct android_usb_function *f)
1481{
1482 f_tcm_exit();
1483}
1484
1485static int uasp_function_bind_config(struct android_usb_function *f,
1486 struct usb_configuration *c)
1487{
1488 return tcm_bind_config(c);
1489}
1490
1491static struct android_usb_function uasp_function = {
1492 .name = "uasp",
1493 .init = uasp_function_init,
1494 .cleanup = uasp_function_cleanup,
1495 .bind_config = uasp_function_bind_config,
1496};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001497
Benoit Goby1e8ce152011-12-12 13:01:23 -08001498static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001499 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001500 &ecm_qc_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301501 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301502 &rmnet_sdio_function,
1503 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301504 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001505 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001506 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301507 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001508 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301509 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001510 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001511 &mtp_function,
1512 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001513 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001514 &rndis_qc_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001515 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001516 &accessory_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301517 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001518 NULL
1519};
1520
Lena Salmand092f2d2012-03-12 17:27:24 +02001521static void android_cleanup_functions(struct android_usb_function **functions)
1522{
1523 struct android_usb_function *f;
1524 struct device_attribute **attrs;
1525 struct device_attribute *attr;
1526
1527 while (*functions) {
1528 f = *functions++;
1529
1530 if (f->dev) {
1531 device_destroy(android_class, f->dev->devt);
1532 kfree(f->dev_name);
1533 } else
1534 continue;
1535
1536 if (f->cleanup)
1537 f->cleanup(f);
1538
1539 attrs = f->attributes;
1540 if (attrs) {
1541 while ((attr = *attrs++))
1542 device_remove_file(f->dev, attr);
1543 }
1544 }
1545}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001546
1547static int android_init_functions(struct android_usb_function **functions,
1548 struct usb_composite_dev *cdev)
1549{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001550 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001551 struct android_usb_function *f;
1552 struct device_attribute **attrs;
1553 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301554 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001555 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001556
1557 for (; (f = *functions++); index++) {
1558 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001559 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001560 if (!f->dev_name) {
1561 err = -ENOMEM;
1562 goto err_out;
1563 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001564 f->dev = device_create(android_class, dev->dev,
1565 MKDEV(0, index), f, f->dev_name);
1566 if (IS_ERR(f->dev)) {
1567 pr_err("%s: Failed to create dev %s", __func__,
1568 f->dev_name);
1569 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001570 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001571 goto err_create;
1572 }
1573
1574 if (f->init) {
1575 err = f->init(f, cdev);
1576 if (err) {
1577 pr_err("%s: Failed to init %s", __func__,
1578 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001579 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001580 }
1581 }
1582
1583 attrs = f->attributes;
1584 if (attrs) {
1585 while ((attr = *attrs++) && !err)
1586 err = device_create_file(f->dev, attr);
1587 }
1588 if (err) {
1589 pr_err("%s: Failed to create function %s attributes",
1590 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001591 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001592 }
1593 }
1594 return 0;
1595
Lena Salmand092f2d2012-03-12 17:27:24 +02001596err_attrs:
1597 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1598 device_remove_file(f->dev, attr);
1599 if (f->cleanup)
1600 f->cleanup(f);
1601err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001602 device_destroy(android_class, f->dev->devt);
1603err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001604 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001605 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001606err_out:
1607 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001608 return err;
1609}
1610
Benoit Goby1e8ce152011-12-12 13:01:23 -08001611static int
1612android_bind_enabled_functions(struct android_dev *dev,
1613 struct usb_configuration *c)
1614{
1615 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001616 struct android_configuration *conf =
1617 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001618 int ret;
1619
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001620 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001621 ret = f->bind_config(f, c);
1622 if (ret) {
1623 pr_err("%s: %s failed", __func__, f->name);
1624 return ret;
1625 }
1626 }
1627 return 0;
1628}
1629
1630static void
1631android_unbind_enabled_functions(struct android_dev *dev,
1632 struct usb_configuration *c)
1633{
1634 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001635 struct android_configuration *conf =
1636 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001637
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001638 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001639 if (f->unbind_config)
1640 f->unbind_config(f, c);
1641 }
1642}
1643
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001644static int android_enable_function(struct android_dev *dev,
1645 struct android_configuration *conf,
1646 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001647{
1648 struct android_usb_function **functions = dev->functions;
1649 struct android_usb_function *f;
1650 while ((f = *functions++)) {
1651 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001652 if (f->android_dev)
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001653 pr_err("%s already enabled in other " \
1654 "configuration or device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001655 f->name);
1656 else {
1657 list_add_tail(&f->enabled_list,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001658 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001659 f->android_dev = dev;
1660 return 0;
1661 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001662 }
1663 }
1664 return -EINVAL;
1665}
1666
1667/*-------------------------------------------------------------------------*/
1668/* /sys/class/android_usb/android%d/ interface */
1669
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301670static ssize_t remote_wakeup_show(struct device *pdev,
1671 struct device_attribute *attr, char *buf)
1672{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001673 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001674 struct android_configuration *conf;
1675
1676 /*
1677 * Show the wakeup attribute of the first configuration,
1678 * since all configurations have the same wakeup attribute
1679 */
1680 if (dev->configs_num == 0)
1681 return 0;
1682 conf = list_entry(dev->configs.next,
1683 struct android_configuration,
1684 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001685
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301686 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001687 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301688 USB_CONFIG_ATT_WAKEUP));
1689}
1690
1691static ssize_t remote_wakeup_store(struct device *pdev,
1692 struct device_attribute *attr, const char *buff, size_t size)
1693{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001694 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001695 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301696 int enable = 0;
1697
1698 sscanf(buff, "%d", &enable);
1699
1700 pr_debug("android_usb: %s remote wakeup\n",
1701 enable ? "enabling" : "disabling");
1702
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001703 list_for_each_entry(conf, &dev->configs, list_item)
1704 if (enable)
1705 conf->usb_config.bmAttributes |=
1706 USB_CONFIG_ATT_WAKEUP;
1707 else
1708 conf->usb_config.bmAttributes &=
1709 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301710
1711 return size;
1712}
1713
Benoit Goby1e8ce152011-12-12 13:01:23 -08001714static ssize_t
1715functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1716{
1717 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001718 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001719 struct android_usb_function *f;
1720 char *buff = buf;
1721
1722 mutex_lock(&dev->mutex);
1723
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001724 list_for_each_entry(conf, &dev->configs, list_item) {
1725 if (buff != buf)
1726 *(buff-1) = ':';
1727 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1728 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
1729 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001730
1731 mutex_unlock(&dev->mutex);
1732
1733 if (buff != buf)
1734 *(buff-1) = '\n';
1735 return buff - buf;
1736}
1737
1738static ssize_t
1739functions_store(struct device *pdev, struct device_attribute *attr,
1740 const char *buff, size_t size)
1741{
1742 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001743 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001744 struct list_head *curr_conf = &dev->configs;
1745 struct android_configuration *conf;
1746 char *conf_str;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001747 char *name;
1748 char buf[256], *b;
1749 int err;
1750
1751 mutex_lock(&dev->mutex);
1752
1753 if (dev->enabled) {
1754 mutex_unlock(&dev->mutex);
1755 return -EBUSY;
1756 }
1757
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001758 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001759 list_for_each_entry(conf, &dev->configs, list_item) {
1760 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1761 f->android_dev = NULL;
1762 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001763 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001764
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301765 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001766 b = strim(buf);
1767
1768 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001769 conf_str = strsep(&b, ":");
1770 if (conf_str) {
1771 /* If the next not equal to the head, take it */
1772 if (curr_conf->next != &dev->configs)
1773 conf = list_entry(curr_conf->next,
1774 struct android_configuration,
1775 list_item);
1776 else
1777 conf = alloc_android_config(dev);
1778
1779 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001780 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001781
1782 while (conf_str) {
1783 name = strsep(&conf_str, ",");
1784 if (name) {
1785 err = android_enable_function(dev, conf, name);
1786 if (err)
1787 pr_err("android_usb: Cannot enable %s",
1788 name);
1789 }
1790 }
1791 }
1792
1793 /* Free uneeded configurations if exists */
1794 while (curr_conf->next != &dev->configs) {
1795 conf = list_entry(curr_conf->next,
1796 struct android_configuration, list_item);
1797 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001798 }
1799
1800 mutex_unlock(&dev->mutex);
1801
1802 return size;
1803}
1804
1805static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1806 char *buf)
1807{
1808 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001809
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301810 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001811}
1812
1813static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1814 const char *buff, size_t size)
1815{
1816 struct android_dev *dev = dev_get_drvdata(pdev);
1817 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001818 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001819 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001820 int enabled = 0;
1821
Benoit Gobycf3fc062011-12-19 14:39:37 -08001822 if (!cdev)
1823 return -ENODEV;
1824
Benoit Goby1e8ce152011-12-12 13:01:23 -08001825 mutex_lock(&dev->mutex);
1826
1827 sscanf(buff, "%d", &enabled);
1828 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001829 /*
1830 * Update values in composite driver's copy of
1831 * device descriptor.
1832 */
1833 cdev->desc.idVendor = device_desc.idVendor;
1834 cdev->desc.idProduct = device_desc.idProduct;
1835 cdev->desc.bcdDevice = device_desc.bcdDevice;
1836 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1837 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1838 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001839 list_for_each_entry(conf, &dev->configs, list_item)
1840 list_for_each_entry(f, &conf->enabled_functions,
1841 enabled_list) {
1842 if (f->enable)
1843 f->enable(f);
1844 }
Benoit Goby80ba14d2012-03-19 18:56:52 -07001845 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001846 dev->enabled = true;
1847 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001848 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001849 list_for_each_entry(conf, &dev->configs, list_item)
1850 list_for_each_entry(f, &conf->enabled_functions,
1851 enabled_list) {
1852 if (f->disable)
1853 f->disable(f);
1854 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001855 dev->enabled = false;
1856 } else {
1857 pr_err("android_usb: already %s\n",
1858 dev->enabled ? "enabled" : "disabled");
1859 }
1860
1861 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001862
Benoit Gobyaab96812011-04-19 20:37:33 -07001863 return size;
1864}
1865
Ofir Cohen94213a72012-05-03 14:26:32 +03001866static ssize_t pm_qos_show(struct device *pdev,
1867 struct device_attribute *attr, char *buf)
1868{
1869 struct android_dev *dev = dev_get_drvdata(pdev);
1870
1871 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1872}
1873
1874static ssize_t pm_qos_store(struct device *pdev,
1875 struct device_attribute *attr,
1876 const char *buff, size_t size)
1877{
1878 struct android_dev *dev = dev_get_drvdata(pdev);
1879
1880 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1881
Benoit Goby1e8ce152011-12-12 13:01:23 -08001882 return size;
1883}
1884
1885static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1886 char *buf)
1887{
1888 struct android_dev *dev = dev_get_drvdata(pdev);
1889 struct usb_composite_dev *cdev = dev->cdev;
1890 char *state = "DISCONNECTED";
1891 unsigned long flags;
1892
1893 if (!cdev)
1894 goto out;
1895
1896 spin_lock_irqsave(&cdev->lock, flags);
1897 if (cdev->config)
1898 state = "CONFIGURED";
1899 else if (dev->connected)
1900 state = "CONNECTED";
1901 spin_unlock_irqrestore(&cdev->lock, flags);
1902out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301903 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001904}
1905
1906#define DESCRIPTOR_ATTR(field, format_string) \
1907static ssize_t \
1908field ## _show(struct device *dev, struct device_attribute *attr, \
1909 char *buf) \
1910{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301911 return snprintf(buf, PAGE_SIZE, \
1912 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001913} \
1914static ssize_t \
1915field ## _store(struct device *dev, struct device_attribute *attr, \
1916 const char *buf, size_t size) \
1917{ \
1918 int value; \
1919 if (sscanf(buf, format_string, &value) == 1) { \
1920 device_desc.field = value; \
1921 return size; \
1922 } \
1923 return -1; \
1924} \
1925static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1926
1927#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1928static ssize_t \
1929field ## _show(struct device *dev, struct device_attribute *attr, \
1930 char *buf) \
1931{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301932 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001933} \
1934static ssize_t \
1935field ## _store(struct device *dev, struct device_attribute *attr, \
1936 const char *buf, size_t size) \
1937{ \
1938 if (size >= sizeof(buffer)) \
1939 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05301940 strlcpy(buffer, buf, sizeof(buffer)); \
1941 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05301942 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001943} \
1944static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1945
1946
1947DESCRIPTOR_ATTR(idVendor, "%04x\n")
1948DESCRIPTOR_ATTR(idProduct, "%04x\n")
1949DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1950DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1951DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1952DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1953DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1954DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1955DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1956
1957static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1958 functions_store);
1959static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001960static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1961 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001962static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301963static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1964 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001965
1966static struct device_attribute *android_usb_attributes[] = {
1967 &dev_attr_idVendor,
1968 &dev_attr_idProduct,
1969 &dev_attr_bcdDevice,
1970 &dev_attr_bDeviceClass,
1971 &dev_attr_bDeviceSubClass,
1972 &dev_attr_bDeviceProtocol,
1973 &dev_attr_iManufacturer,
1974 &dev_attr_iProduct,
1975 &dev_attr_iSerial,
1976 &dev_attr_functions,
1977 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001978 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001979 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301980 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001981 NULL
1982};
1983
1984/*-------------------------------------------------------------------------*/
1985/* Composite driver */
1986
1987static int android_bind_config(struct usb_configuration *c)
1988{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001989 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001990 int ret = 0;
1991
1992 ret = android_bind_enabled_functions(dev, c);
1993 if (ret)
1994 return ret;
1995
1996 return 0;
1997}
1998
1999static void android_unbind_config(struct usb_configuration *c)
2000{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002001 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002002
2003 android_unbind_enabled_functions(dev, c);
2004}
2005
2006static int android_bind(struct usb_composite_dev *cdev)
2007{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002008 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002009 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002010 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002011 int gcnum, id, ret;
2012
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002013 /* Bind to the last android_dev that was probed */
2014 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2015
2016 dev->cdev = cdev;
2017
Benoit Goby1e8ce152011-12-12 13:01:23 -08002018 /*
2019 * Start disconnected. Userspace will connect the gadget once
2020 * it is done configuring the functions.
2021 */
2022 usb_gadget_disconnect(gadget);
2023
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002024 /* Init the supported functions only once, on the first android_dev */
2025 if (android_dev_count == 1) {
2026 ret = android_init_functions(dev->functions, cdev);
2027 if (ret)
2028 return ret;
2029 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002030
2031 /* Allocate string descriptor numbers ... note that string
2032 * contents can be overridden by the composite_dev glue.
2033 */
2034 id = usb_string_id(cdev);
2035 if (id < 0)
2036 return id;
2037 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2038 device_desc.iManufacturer = id;
2039
2040 id = usb_string_id(cdev);
2041 if (id < 0)
2042 return id;
2043 strings_dev[STRING_PRODUCT_IDX].id = id;
2044 device_desc.iProduct = id;
2045
2046 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302047 strlcpy(manufacturer_string, "Android",
2048 sizeof(manufacturer_string) - 1);
2049 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2050 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002051
2052 id = usb_string_id(cdev);
2053 if (id < 0)
2054 return id;
2055 strings_dev[STRING_SERIAL_IDX].id = id;
2056 device_desc.iSerialNumber = id;
2057
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302058 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002059 list_for_each_entry(conf, &dev->configs, list_item)
2060 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302061
Benoit Goby1e8ce152011-12-12 13:01:23 -08002062 gcnum = usb_gadget_controller_number(gadget);
2063 if (gcnum >= 0)
2064 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2065 else {
2066 pr_warning("%s: controller '%s' not recognized\n",
2067 longname, gadget->name);
2068 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2069 }
2070
Benoit Goby1e8ce152011-12-12 13:01:23 -08002071 return 0;
2072}
2073
2074static int android_usb_unbind(struct usb_composite_dev *cdev)
2075{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002076 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002077
Lena Salmand092f2d2012-03-12 17:27:24 +02002078 manufacturer_string[0] = '\0';
2079 product_string[0] = '\0';
2080 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002081 cancel_work_sync(&dev->work);
2082 android_cleanup_functions(dev->functions);
2083 return 0;
2084}
2085
2086static struct usb_composite_driver android_usb_driver = {
2087 .name = "android_usb",
2088 .dev = &device_desc,
2089 .strings = dev_strings,
2090 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002091 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002092};
2093
2094static int
2095android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2096{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002097 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002098 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002099 struct usb_request *req = cdev->req;
2100 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002101 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002102 int value = -EOPNOTSUPP;
2103 unsigned long flags;
2104
2105 req->zero = 0;
2106 req->complete = composite_setup_complete;
2107 req->length = 0;
2108 gadget->ep0->driver_data = cdev;
2109
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002110 list_for_each_entry(conf, &dev->configs, list_item)
2111 if (&conf->usb_config == cdev->config)
2112 list_for_each_entry(f,
2113 &conf->enabled_functions,
2114 enabled_list)
2115 if (f->ctrlrequest) {
2116 value = f->ctrlrequest(f, cdev, c);
2117 if (value >= 0)
2118 break;
2119 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002120
Benoit Gobycf3fc062011-12-19 14:39:37 -08002121 /* Special case the accessory function.
2122 * It needs to handle control requests before it is enabled.
2123 */
2124 if (value < 0)
2125 value = acc_ctrlrequest(cdev, c);
2126
Benoit Goby1e8ce152011-12-12 13:01:23 -08002127 if (value < 0)
2128 value = composite_setup(gadget, c);
2129
2130 spin_lock_irqsave(&cdev->lock, flags);
2131 if (!dev->connected) {
2132 dev->connected = 1;
2133 schedule_work(&dev->work);
2134 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2135 cdev->config) {
2136 schedule_work(&dev->work);
2137 }
2138 spin_unlock_irqrestore(&cdev->lock, flags);
2139
2140 return value;
2141}
2142
2143static void android_disconnect(struct usb_gadget *gadget)
2144{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002145 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002146 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002147 unsigned long flags;
2148
2149 composite_disconnect(gadget);
2150
2151 spin_lock_irqsave(&cdev->lock, flags);
2152 dev->connected = 0;
2153 schedule_work(&dev->work);
2154 spin_unlock_irqrestore(&cdev->lock, flags);
2155}
2156
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002157static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002158{
2159 struct device_attribute **attrs = android_usb_attributes;
2160 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002161 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002162 int err;
2163
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002164 /*
2165 * The primary usb core should always have usb_core_id=0, since
2166 * Android user space is currently interested in android0 events.
2167 */
2168 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2169 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002170 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002171 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002172 if (IS_ERR(dev->dev))
2173 return PTR_ERR(dev->dev);
2174
2175 dev_set_drvdata(dev->dev, dev);
2176
2177 while ((attr = *attrs++)) {
2178 err = device_create_file(dev->dev, attr);
2179 if (err) {
2180 device_destroy(android_class, dev->dev->devt);
2181 return err;
2182 }
2183 }
2184 return 0;
2185}
2186
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302187static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002188{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302189 struct device_attribute **attrs = android_usb_attributes;
2190 struct device_attribute *attr;
2191
2192 while ((attr = *attrs++))
2193 device_remove_file(dev->dev, attr);
2194 device_destroy(android_class, dev->dev->devt);
2195}
2196
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002197static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2198{
2199 struct android_dev *dev = NULL;
2200
2201 /* Find the android dev from the list */
2202 list_for_each_entry(dev, &android_dev_list, list_item) {
2203 if (dev->cdev == cdev)
2204 break;
2205 }
2206
2207 return dev;
2208}
2209
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002210static struct android_configuration *alloc_android_config
2211 (struct android_dev *dev)
2212{
2213 struct android_configuration *conf;
2214
2215 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2216 if (!conf) {
2217 pr_err("%s(): Failed to alloc memory for android conf\n",
2218 __func__);
2219 return ERR_PTR(-ENOMEM);
2220 }
2221
2222 dev->configs_num++;
2223 conf->usb_config.label = dev->name;
2224 conf->usb_config.unbind = android_unbind_config;
2225 conf->usb_config.bConfigurationValue = dev->configs_num;
2226
2227 INIT_LIST_HEAD(&conf->enabled_functions);
2228
2229 list_add_tail(&conf->list_item, &dev->configs);
2230
2231 return conf;
2232}
2233
2234static void free_android_config(struct android_dev *dev,
2235 struct android_configuration *conf)
2236{
2237 list_del(&conf->list_item);
2238 dev->configs_num--;
2239 kfree(conf);
2240}
2241
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002242static int __devinit android_probe(struct platform_device *pdev)
2243{
2244 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002245 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02002246 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002247
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002248 if (!android_class) {
2249 android_class = class_create(THIS_MODULE, "android_usb");
2250 if (IS_ERR(android_class))
2251 return PTR_ERR(android_class);
2252 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002253
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002254 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2255 if (!android_dev) {
2256 pr_err("%s(): Failed to alloc memory for android_dev\n",
2257 __func__);
2258 ret = -ENOMEM;
2259 goto err_alloc;
2260 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002261
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002262 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002263 android_dev->disable_depth = 1;
2264 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002265 android_dev->configs_num = 0;
2266 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002267 INIT_WORK(&android_dev->work, android_work);
2268 mutex_init(&android_dev->mutex);
2269
2270 android_dev->pdata = pdata;
2271
2272 list_add_tail(&android_dev->list_item, &android_dev_list);
2273 android_dev_count++;
2274
2275 if (pdata)
2276 composite_driver.usb_core_id = pdata->usb_core_id;
2277 else
2278 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2279
2280 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302281 if (ret) {
2282 pr_err("%s(): android_create_device failed\n", __func__);
2283 goto err_dev;
2284 }
2285
Lena Salmand092f2d2012-03-12 17:27:24 +02002286 ret = usb_composite_probe(&android_usb_driver, android_bind);
2287 if (ret) {
2288 pr_err("%s(): Failed to register android "
2289 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302290 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002291 }
2292
Ofir Cohen94213a72012-05-03 14:26:32 +03002293 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302294 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002295 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002296 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002297 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002298
Lena Salmand092f2d2012-03-12 17:27:24 +02002299 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302300err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002301 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302302err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002303 list_del(&android_dev->list_item);
2304 android_dev_count--;
2305 kfree(android_dev);
2306err_alloc:
2307 if (list_empty(&android_dev_list)) {
2308 class_destroy(android_class);
2309 android_class = NULL;
2310 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302311 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002312}
2313
2314static int android_remove(struct platform_device *pdev)
2315{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002316 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002317 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002318 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302319
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002320 if (pdata)
2321 usb_core_id = pdata->usb_core_id;
2322
2323 /* Find the android dev from the list */
2324 list_for_each_entry(dev, &android_dev_list, list_item) {
2325 if (!dev->pdata)
2326 break; /*To backward compatibility*/
2327 if (dev->pdata->usb_core_id == usb_core_id)
2328 break;
2329 }
2330
2331 if (dev) {
2332 android_destroy_device(dev);
2333 if (pdata && pdata->swfi_latency)
2334 pm_qos_remove_request(&dev->pm_qos_req_dma);
2335 list_del(&dev->list_item);
2336 android_dev_count--;
2337 kfree(dev);
2338 }
2339
2340 if (list_empty(&android_dev_list)) {
2341 class_destroy(android_class);
2342 android_class = NULL;
2343 usb_composite_unregister(&android_usb_driver);
2344 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002346 return 0;
2347}
2348
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002349static const struct platform_device_id android_id_table[] __devinitconst = {
2350 {
2351 .name = "android_usb",
2352 },
2353 {
2354 .name = "android_usb_hsic",
2355 },
2356};
2357
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002358static struct platform_driver android_platform_driver = {
2359 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002360 .probe = android_probe,
2361 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002362 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002363};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002364
2365static int __init init(void)
2366{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302367 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002368
Benoit Goby1e8ce152011-12-12 13:01:23 -08002369 /* Override composite driver functions */
2370 composite_driver.setup = android_setup;
2371 composite_driver.disconnect = android_disconnect;
2372
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002373 INIT_LIST_HEAD(&android_dev_list);
2374 android_dev_count = 0;
2375
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302376 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302377 if (ret) {
2378 pr_err("%s(): Failed to register android"
2379 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302380 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002381
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302382 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002383}
2384module_init(init);
2385
2386static void __exit cleanup(void)
2387{
Lena Salmand092f2d2012-03-12 17:27:24 +02002388 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002389}
2390module_exit(cleanup);