Merge tag 'v3.10.105' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / gadget / android.c
1 /*
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 #ifdef pr_fmt
19 #undef pr_fmt
20 #endif
21 #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
22
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/fs.h>
26 #include <linux/delay.h>
27 #include <linux/kernel.h>
28 #include <linux/utsname.h>
29 #include <linux/platform_device.h>
30
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/composite.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/printk.h>
35
36 /* Add for HW/SW connect */
37 #include <linux/musb/mtk_musb.h>
38 /* Add for HW/SW connect */
39
40 #include "gadget_chips.h"
41
42 #include "f_fs.c"
43 #include "f_audio_source.c"
44 #include "f_mass_storage.c"
45 #include "f_adb.c"
46 #include "f_mtp.c"
47 #include "f_accessory.c"
48 #define USB_ETH_RNDIS y
49 #include "f_rndis.c"
50 #include "rndis.c"
51 #include "f_ecm.c"
52 #include "f_eem.c"
53 #include "u_ether.c"
54
55 #ifdef CONFIG_EVDO_DT_SUPPORT
56 #include <mach/viatel_rawbulk.h>
57 int rawbulk_bind_config(struct usb_configuration *c, int transfer_id);
58 int rawbulk_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl);
59 #endif
60
61 MODULE_AUTHOR("Mike Lockwood");
62 MODULE_DESCRIPTION("Android Composite USB Driver");
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION("1.0");
65
66 static const char longname[] = "Gadget Android";
67
68 /* Default vendor and product IDs, overridden by userspace */
69 #define VENDOR_ID 0x0BB4
70 #define PRODUCT_ID 0x0001
71
72 /* Default manufacturer and product string , overridden by userspace */
73 #define MANUFACTURER_STRING "MediaTek"
74 #define PRODUCT_STRING "MT65xx Android Phone"
75
76
77 //#define USB_LOG "USB"
78
79 struct android_usb_function {
80 char *name;
81 void *config;
82
83 struct device *dev;
84 char *dev_name;
85 struct device_attribute **attributes;
86
87 /* for android_dev.enabled_functions */
88 struct list_head enabled_list;
89
90 /* Optional: initialization during gadget bind */
91 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
92 /* Optional: cleanup during gadget unbind */
93 void (*cleanup)(struct android_usb_function *);
94 /* Optional: called when the function is added the list of
95 * enabled functions */
96 void (*enable)(struct android_usb_function *);
97 /* Optional: called when it is removed */
98 void (*disable)(struct android_usb_function *);
99
100 int (*bind_config)(struct android_usb_function *,
101 struct usb_configuration *);
102
103 /* Optional: called when the configuration is removed */
104 void (*unbind_config)(struct android_usb_function *,
105 struct usb_configuration *);
106 /* Optional: handle ctrl requests before the device is configured */
107 int (*ctrlrequest)(struct android_usb_function *,
108 struct usb_composite_dev *,
109 const struct usb_ctrlrequest *);
110 };
111
112 struct android_dev {
113 struct android_usb_function **functions;
114 struct list_head enabled_functions;
115 struct usb_composite_dev *cdev;
116 struct device *dev;
117
118 bool enabled;
119 int disable_depth;
120 struct mutex mutex;
121 bool connected;
122 bool sw_connected;
123 struct work_struct work;
124 char ffs_aliases[256];
125 int rezero_cmd;
126 };
127
128 static struct class *android_class;
129 static struct android_dev *_android_dev;
130 static int android_bind_config(struct usb_configuration *c);
131 static void android_unbind_config(struct usb_configuration *c);
132 static int android_setup_config(struct usb_configuration *c, const struct usb_ctrlrequest *ctrl);
133
134 /* string IDs are assigned dynamically */
135 #define STRING_MANUFACTURER_IDX 0
136 #define STRING_PRODUCT_IDX 1
137 #define STRING_SERIAL_IDX 2
138
139 static char manufacturer_string[256];
140 static char product_string[256];
141 static char serial_string[256];
142
143 /* String Table */
144 static struct usb_string strings_dev[] = {
145 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
146 [STRING_PRODUCT_IDX].s = product_string,
147 [STRING_SERIAL_IDX].s = serial_string,
148 { } /* end of list */
149 };
150
151 static struct usb_gadget_strings stringtab_dev = {
152 .language = 0x0409, /* en-us */
153 .strings = strings_dev,
154 };
155
156 static struct usb_gadget_strings *dev_strings[] = {
157 &stringtab_dev,
158 NULL,
159 };
160
161 static struct usb_device_descriptor device_desc = {
162 .bLength = sizeof(device_desc),
163 .bDescriptorType = USB_DT_DEVICE,
164 #ifdef CONFIG_USB_MU3D_DRV
165 .bcdUSB = __constant_cpu_to_le16(0x0300),
166 #else
167 .bcdUSB = __constant_cpu_to_le16(0x0200),
168 #endif
169 .bDeviceClass = USB_CLASS_PER_INTERFACE,
170 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
171 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
172 .bcdDevice = __constant_cpu_to_le16(0xffff),
173 .bNumConfigurations = 1,
174 };
175
176 static struct usb_configuration android_config_driver = {
177 .label = "android",
178 .unbind = android_unbind_config,
179 .setup = android_setup_config,
180 .bConfigurationValue = 1,
181 #ifdef CONFIG_USBIF_COMPLIANCE
182 // USBIF, do we need to set bus powered
183 .bmAttributes = USB_CONFIG_ATT_ONE,
184 //.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
185 #else
186 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
187 #endif
188 #ifdef CONFIG_USB_MU3D_DRV
189 .MaxPower = 192, /* Only consume 192ma for passing USB30CV Descriptor Test [USB3.0 devices]*/
190 #else
191 .MaxPower = 500, /* 500ma */
192 #endif
193 };
194
195 static void android_work(struct work_struct *data)
196 {
197 struct android_dev *dev = container_of(data, struct android_dev, work);
198 struct usb_composite_dev *cdev = dev->cdev;
199 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
200 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
201 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
202
203 /* Add for HW/SW connect */
204 char *hwdisconnected[2] = { "USB_STATE=HWDISCONNECTED", NULL };
205 // char *hwconnected[2] = { "USB_STATE=HWCONNECTED", NULL };
206 /* Add for HW/SW connect */
207
208 char *rezero_event[2] = { "USB_STATE=REZEROCMD", NULL };
209 char *showcdrom_event[2] = { "USB_STATE=SHOWCDROMCMD", NULL };
210
211 char **uevent_envp = NULL;
212 char **uevent_envp_cdrom = NULL;
213 unsigned long flags;
214 /* Add for HW/SW connect */
215 bool is_hwconnected = true;
216
217 /* patch for ALPS00345130, if the disconnect followed by hw_disconnect, then the hw_disconnect
218 will not notify the UsbDeviceManager due to that musb->g.speed == USB_SPEED_UNKNOWN*/
219 if (!cdev){
220 return ;
221 }
222
223 if(usb_cable_connected())
224 is_hwconnected = true;
225 else
226 is_hwconnected = false;
227
228 pr_debug("[XLOG_INFO][USB]%s: is_hwconnected=%d \n", __func__, is_hwconnected);
229 /* Add for HW/SW connect */
230
231 spin_lock_irqsave(&cdev->lock, flags);
232 if (cdev->config) {
233 uevent_envp = configured;
234 } else if (dev->connected != dev->sw_connected) {
235 uevent_envp = dev->connected ? connected : disconnected;
236 }
237
238 dev->sw_connected = dev->connected;
239
240 if (dev->rezero_cmd == 1) {
241 uevent_envp_cdrom = rezero_event;
242 dev->rezero_cmd = 0;
243 } else if (dev->rezero_cmd == 2) {
244 uevent_envp_cdrom = showcdrom_event;
245 dev->rezero_cmd = 0;
246 }
247
248 spin_unlock_irqrestore(&cdev->lock, flags);
249
250 if (uevent_envp) {
251 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
252 pr_debug("[XLOG_INFO][USB]%s: sent uevent %s\n", __func__, uevent_envp[0]);
253 } else {
254 pr_debug("[XLOG_INFO][USB]%s: did not send uevent (%d %d %p)\n", __func__,
255 dev->connected, dev->sw_connected, cdev->config);
256 }
257
258 if (!is_hwconnected) {
259 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, hwdisconnected);
260 pr_debug("[XLOG_INFO][USB]%s: sent uevent %s\n", __func__, hwdisconnected[0]);
261 }
262
263 if (uevent_envp_cdrom) {
264 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp_cdrom);
265 pr_debug("[XLOG_INFO][USB]%s: sent uevent %s\n", __func__, uevent_envp_cdrom[0]);
266 } else {
267 pr_debug("[XLOG_INFO][USB]%s: did not send zero uevent\n", __func__);
268 }
269
270 }
271
272 static void android_enable(struct android_dev *dev)
273 {
274 struct usb_composite_dev *cdev = dev->cdev;
275
276 if (WARN_ON(!dev->disable_depth))
277 return;
278
279 if (--dev->disable_depth == 0) {
280 usb_add_config(cdev, &android_config_driver,
281 android_bind_config);
282 usb_gadget_connect(cdev->gadget);
283 }
284 }
285
286 static void android_disable(struct android_dev *dev)
287 {
288 struct usb_composite_dev *cdev = dev->cdev;
289
290 if (dev->disable_depth++ == 0) {
291 usb_gadget_disconnect(cdev->gadget);
292 /* Cancel pending control requests */
293 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
294 usb_remove_config(cdev, &android_config_driver);
295 }
296 }
297
298 /*-------------------------------------------------------------------------*/
299 /* Supported functions initialization */
300
301 struct functionfs_config {
302 bool opened;
303 bool enabled;
304 struct ffs_data *data;
305 };
306
307 static int ffs_function_init(struct android_usb_function *f,
308 struct usb_composite_dev *cdev)
309 {
310 f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL);
311 if (!f->config)
312 return -ENOMEM;
313
314 return functionfs_init();
315 }
316
317 static void ffs_function_cleanup(struct android_usb_function *f)
318 {
319 functionfs_cleanup();
320 kfree(f->config);
321 }
322
323 static void ffs_function_enable(struct android_usb_function *f)
324 {
325 struct android_dev *dev = _android_dev;
326 struct functionfs_config *config = f->config;
327
328 config->enabled = true;
329
330 /* Disable the gadget until the function is ready */
331 if (!config->opened)
332 android_disable(dev);
333 }
334
335 static void ffs_function_disable(struct android_usb_function *f)
336 {
337 struct android_dev *dev = _android_dev;
338 struct functionfs_config *config = f->config;
339
340 config->enabled = false;
341
342 /* Balance the disable that was called in closed_callback */
343 if (!config->opened)
344 android_enable(dev);
345 }
346
347 static int ffs_function_bind_config(struct android_usb_function *f,
348 struct usb_configuration *c)
349 {
350 struct functionfs_config *config = f->config;
351 return functionfs_bind_config(c->cdev, c, config->data);
352 }
353
354 static ssize_t
355 ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf)
356 {
357 struct android_dev *dev = _android_dev;
358 int ret;
359
360 mutex_lock(&dev->mutex);
361 ret = sprintf(buf, "%s\n", dev->ffs_aliases);
362 mutex_unlock(&dev->mutex);
363
364 return ret;
365 }
366
367 static ssize_t
368 ffs_aliases_store(struct device *pdev, struct device_attribute *attr,
369 const char *buf, size_t size)
370 {
371 struct android_dev *dev = _android_dev;
372 char buff[256];
373
374 mutex_lock(&dev->mutex);
375
376 if (dev->enabled) {
377 mutex_unlock(&dev->mutex);
378 return -EBUSY;
379 }
380
381 strlcpy(buff, buf, sizeof(buff));
382 strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases));
383
384 mutex_unlock(&dev->mutex);
385
386 return size;
387 }
388
389 static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show,
390 ffs_aliases_store);
391 static struct device_attribute *ffs_function_attributes[] = {
392 &dev_attr_aliases,
393 NULL
394 };
395
396 static struct android_usb_function ffs_function = {
397 .name = "ffs",
398 .init = ffs_function_init,
399 .enable = ffs_function_enable,
400 .disable = ffs_function_disable,
401 .cleanup = ffs_function_cleanup,
402 .bind_config = ffs_function_bind_config,
403 .attributes = ffs_function_attributes,
404 };
405
406 static int functionfs_ready_callback(struct ffs_data *ffs)
407 {
408 struct android_dev *dev = _android_dev;
409 struct functionfs_config *config = ffs_function.config;
410 int ret = 0;
411
412 mutex_lock(&dev->mutex);
413
414 ret = functionfs_bind(ffs, dev->cdev);
415 if (ret)
416 goto err;
417
418 config->data = ffs;
419 config->opened = true;
420
421 if (config->enabled)
422 android_enable(dev);
423
424 err:
425 mutex_unlock(&dev->mutex);
426 return ret;
427 }
428
429 static void functionfs_closed_callback(struct ffs_data *ffs)
430 {
431 struct android_dev *dev = _android_dev;
432 struct functionfs_config *config = ffs_function.config;
433
434 mutex_lock(&dev->mutex);
435
436 if (config->enabled)
437 android_disable(dev);
438
439 config->opened = false;
440 config->data = NULL;
441
442 functionfs_unbind(ffs);
443
444 mutex_unlock(&dev->mutex);
445 }
446
447 static void *functionfs_acquire_dev_callback(const char *dev_name)
448 {
449 return 0;
450 }
451
452 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
453 {
454 }
455
456 struct adb_data {
457 bool opened;
458 bool enabled;
459 };
460
461 static int
462 adb_function_init(struct android_usb_function *f,
463 struct usb_composite_dev *cdev)
464 {
465 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
466 if (!f->config)
467 return -ENOMEM;
468
469 return adb_setup();
470 }
471
472 static void adb_function_cleanup(struct android_usb_function *f)
473 {
474 adb_cleanup();
475 kfree(f->config);
476 }
477
478 static int
479 adb_function_bind_config(struct android_usb_function *f,
480 struct usb_configuration *c)
481 {
482 return adb_bind_config(c);
483 }
484
485 static void adb_android_function_enable(struct android_usb_function *f)
486 {
487 /* This patch cause WHQL fail */
488 #if 0
489 struct android_dev *dev = _android_dev;
490 struct adb_data *data = f->config;
491
492 data->enabled = true;
493
494 /* Disable the gadget until adbd is ready */
495 if (!data->opened)
496 android_disable(dev);
497 #endif
498 }
499
500 static void adb_android_function_disable(struct android_usb_function *f)
501 {
502 /* This patch cause WHQL fail */
503 #if 0
504 struct android_dev *dev = _android_dev;
505 struct adb_data *data = f->config;
506
507 data->enabled = false;
508
509 /* Balance the disable that was called in closed_callback */
510 if (!data->opened)
511 android_enable(dev);
512 #endif
513 }
514
515 static struct android_usb_function adb_function = {
516 .name = "adb",
517 .enable = adb_android_function_enable,
518 .disable = adb_android_function_disable,
519 .init = adb_function_init,
520 .cleanup = adb_function_cleanup,
521 .bind_config = adb_function_bind_config,
522 };
523
524 static void adb_ready_callback(void)
525 {
526 /* This patch cause WHQL fail */
527 #if 0
528 struct android_dev *dev = _android_dev;
529 struct adb_data *data = adb_function.config;
530
531 mutex_lock(&dev->mutex);
532
533 data->opened = true;
534
535 if (data->enabled)
536 android_enable(dev);
537
538 mutex_unlock(&dev->mutex);
539 #endif
540 }
541
542 static void adb_closed_callback(void)
543 {
544 /* This patch cause WHQL fail */
545 #if 0
546 struct android_dev *dev = _android_dev;
547 struct adb_data *data = adb_function.config;
548
549 mutex_lock(&dev->mutex);
550
551 data->opened = false;
552
553 if (data->enabled)
554 android_disable(dev);
555
556 mutex_unlock(&dev->mutex);
557 #endif
558 }
559
560 #define MAX_ACM_INSTANCES 4
561 struct acm_function_config {
562 int instances;
563 int instances_on;
564 struct usb_function *f_acm[MAX_ACM_INSTANCES];
565 struct usb_function_instance *f_acm_inst[MAX_ACM_INSTANCES];
566 int port_index[MAX_ACM_INSTANCES];
567 int port_index_on[MAX_ACM_INSTANCES];
568 };
569
570 static int
571 acm_function_init(struct android_usb_function *f,
572 struct usb_composite_dev *cdev)
573 {
574 int i;
575 int ret;
576 struct acm_function_config *config;
577 #ifdef CONFIG_USBIF_COMPLIANCE
578 /* FIXME, USB_IF workaround */
579 return 0;
580 #endif
581
582 config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
583 if (!config)
584 return -ENOMEM;
585 f->config = config;
586
587 for (i = 0; i < MAX_ACM_INSTANCES; i++) {
588 config->f_acm_inst[i] = usb_get_function_instance("acm");
589 if (IS_ERR(config->f_acm_inst[i])) {
590 ret = PTR_ERR(config->f_acm_inst[i]);
591 goto err_usb_get_function_instance;
592 }
593 config->f_acm[i] = usb_get_function(config->f_acm_inst[i]);
594 if (IS_ERR(config->f_acm[i])) {
595 ret = PTR_ERR(config->f_acm[i]);
596 goto err_usb_get_function;
597 }
598 }
599 return 0;
600 err_usb_get_function_instance:
601 pr_err("Could not usb_get_function_instance() %d\n", i);
602 while (i-- > 0) {
603 usb_put_function(config->f_acm[i]);
604 err_usb_get_function:
605 pr_err("Could not usb_get_function() %d\n", i);
606 usb_put_function_instance(config->f_acm_inst[i]);
607 }
608 return ret;
609 }
610
611 static void acm_function_cleanup(struct android_usb_function *f)
612 {
613 int i;
614 struct acm_function_config *config = f->config;
615 #ifdef CONFIG_USBIF_COMPLIANCE
616 /* FIXME, USB_IF workaround */
617 return 0;
618 #endif
619
620 for (i = 0; i < MAX_ACM_INSTANCES; i++) {
621 usb_put_function(config->f_acm[i]);
622 usb_put_function_instance(config->f_acm_inst[i]);
623 }
624 kfree(f->config);
625 f->config = NULL;
626 }
627
628 static int
629 acm_function_bind_config(struct android_usb_function *f,
630 struct usb_configuration *c)
631 {
632 int i;
633 int ret = 0;
634 struct acm_function_config *config = f->config;
635
636 /*1st:Modem, 2nd:Modem, 3rd:BT, 4th:MD logger*/
637 for (i = 0; i < MAX_ACM_INSTANCES; i++) {
638 if(config->port_index[i] != 0) {
639 ret = usb_add_function(c, config->f_acm[i]);
640 if (ret) {
641 pr_err("Could not bind acm%u config\n", i);
642 goto err_usb_add_function;
643 }
644 pr_info("%s Open /dev/ttyGS%d\n", __func__, i);
645 config->port_index[i] = 0;
646 config->port_index_on[i] = 1;
647 config->instances = 0;
648 }
649 }
650
651 config->instances_on = config->instances;
652 for (i = 0; i < config->instances_on; i++) {
653 ret = usb_add_function(c, config->f_acm[i]);
654 if (ret) {
655 pr_err("Could not bind acm%u config\n", i);
656 goto err_usb_add_function;
657 }
658 pr_info("%s Open /dev/ttyGS%d\n", __func__, i);
659 }
660
661 return 0;
662
663 err_usb_add_function:
664 while (i-- > 0)
665 usb_remove_function(c, config->f_acm[i]);
666 return ret;
667 }
668
669 static void acm_function_unbind_config(struct android_usb_function *f,
670 struct usb_configuration *c)
671 {
672
673 //int i;
674 //struct acm_function_config *config = f->config;
675
676 /* REVISIT:
677 * list_del(&f->list) and f->unbind() are called at unbind_config().
678 * f->disable() is called at enable_store.
679 * So directly return this function.
680 * If does NOT return, f->list is deleted twice and cuased KE.
681 * ToDo:
682 * What does the original kernel code look likes?
683 */
684
685 return;
686
687 /*
688 if (config->instances_on != 0) {
689 for (i = 0; i < config->instances_on; i++) {
690 pr_debug("%s f_acm[%d]=%p\n", __func__, i, config->f_acm[i]);
691 usb_remove_function(c, config->f_acm[i]);
692 }
693 } else {
694 for (i = 0; i < MAX_ACM_INSTANCES; i++)
695 pr_debug("%s port_index_on=%d\n", __func__, config->port_index_on[i]);
696 if (config->port_index_on[i] != 0) {
697 usb_remove_function(c, config->f_acm[i]);
698 }
699 }
700 */
701 }
702
703 static ssize_t acm_instances_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705 {
706 struct android_usb_function *f = dev_get_drvdata(dev);
707 struct acm_function_config *config = f->config;
708 return sprintf(buf, "%d\n", config->instances);
709 }
710
711 static ssize_t acm_instances_store(struct device *dev,
712 struct device_attribute *attr, const char *buf, size_t size)
713 {
714 struct android_usb_function *f = dev_get_drvdata(dev);
715 struct acm_function_config *config = f->config;
716 int value;
717
718 sscanf(buf, "%d", &value);
719 if (value > MAX_ACM_INSTANCES)
720 value = MAX_ACM_INSTANCES;
721 config->instances = value;
722 return size;
723 }
724
725 static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
726 acm_instances_store);
727
728 static ssize_t acm_port_index_show(struct device *dev,
729 struct device_attribute *attr, char *buf)
730 {
731 struct android_usb_function *f = dev_get_drvdata(dev);
732 struct acm_function_config *config = f->config;
733 return sprintf(buf, "%d,%d,%d,%d\n", config->port_index[0], config->port_index[1],
734 config->port_index[2], config->port_index[3]);
735 }
736
737 static ssize_t acm_port_index_store(struct device *dev,
738 struct device_attribute *attr, const char *buf, size_t size)
739 {
740 struct android_usb_function *f = dev_get_drvdata(dev);
741 struct acm_function_config *config = f->config;
742 int val[MAX_ACM_INSTANCES]={0};
743 int num = 0;
744 int tmp = 0;
745
746 num = sscanf(buf, "%d,%d,%d,%d", &(val[0]), &(val[1]), &(val[2]), &(val[3]));
747
748 pr_debug("%s [0]=%d,[1]=%d,[2]=%d,[3]=%d, num=%d\n", __func__, val[0], val[1], \
749 val[2], val[3], num);
750
751 /* Set all port_index as 0*/
752 for (tmp = 0; tmp < MAX_ACM_INSTANCES; tmp ++)
753 config->port_index[tmp] = 0;
754
755 for (tmp = 0; tmp < num; tmp++) {
756 int port = (val[tmp] > MAX_ACM_INSTANCES || val[tmp] < 1) ? 0 : val[tmp]-1;
757 config->port_index[port] = 1;
758 }
759
760 return size;
761 }
762
763 static DEVICE_ATTR(port_index, S_IRUGO | S_IWUSR, acm_port_index_show,
764 acm_port_index_store);
765
766 static struct device_attribute *acm_function_attributes[] = {
767 &dev_attr_instances,
768 &dev_attr_port_index, /*Only open the specific port*/
769 NULL
770 };
771
772 static struct android_usb_function acm_function = {
773 .name = "acm",
774 .init = acm_function_init,
775 .cleanup = acm_function_cleanup,
776 .bind_config = acm_function_bind_config,
777 .unbind_config = acm_function_unbind_config,
778 .attributes = acm_function_attributes,
779 };
780
781 #ifdef CONFIG_USB_F_LOOPBACK
782
783 #define MAX_LOOPBACK_INSTANCES 1
784
785 struct loopback_function_config {
786 int port_num;
787 struct usb_function *f_lp[MAX_LOOPBACK_INSTANCES];
788 struct usb_function_instance *f_lp_inst[MAX_LOOPBACK_INSTANCES];
789 };
790
791 static int loopback_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
792 {
793 int i;
794 int ret;
795 struct loopback_function_config *config;
796
797 config = kzalloc(sizeof(struct loopback_function_config), GFP_KERNEL);
798 if (!config)
799 return -ENOMEM;
800 f->config = config;
801
802 for (i = 0; i < MAX_LOOPBACK_INSTANCES; i++) {
803 config->f_lp_inst[i] = usb_get_function_instance("loopback");
804 if (IS_ERR(config->f_lp_inst[i])) {
805 ret = PTR_ERR(config->f_lp_inst[i]);
806 goto err_usb_get_function_instance;
807 }
808 config->f_lp[i] = usb_get_function(config->f_lp_inst[i]);
809 if (IS_ERR(config->f_lp[i])) {
810 ret = PTR_ERR(config->f_lp[i]);
811 goto err_usb_get_function;
812 }
813 }
814 return 0;
815 err_usb_get_function_instance:
816 pr_err("Could not usb_get_function_instance() %d\n", i);
817 while (i-- > 0) {
818 usb_put_function(config->f_lp[i]);
819 err_usb_get_function:
820 pr_err("Could not usb_get_function() %d\n", i);
821 usb_put_function_instance(config->f_lp_inst[i]);
822 }
823 return ret;
824 }
825
826 static void loopback_function_cleanup(struct android_usb_function *f)
827 {
828 int i;
829 struct loopback_function_config *config = f->config;
830
831 for (i = 0; i < MAX_LOOPBACK_INSTANCES; i++) {
832 usb_put_function(config->f_lp[i]);
833 usb_put_function_instance(config->f_lp_inst[i]);
834 }
835 kfree(f->config);
836 f->config = NULL;
837 }
838
839 static int
840 loopback_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
841 {
842 int i = 0;
843 int ret = 0;
844 struct loopback_function_config *config = f->config;
845
846 ret = usb_add_function(c, config->f_lp[config->port_num]);
847 if (ret) {
848 pr_err("Could not bind loopback%u config\n", config->port_num);
849 goto err_usb_add_function;
850 }
851 pr_info("%s Open loopback\n", __func__);
852
853 return 0;
854
855 err_usb_add_function:
856 while (i-- > 0)
857 usb_remove_function(c, config->f_lp[i]);
858 return ret;
859 }
860
861 static struct android_usb_function loopback_function = {
862 .name = "loopback",
863 .init = loopback_function_init,
864 .cleanup = loopback_function_cleanup,
865 .bind_config = loopback_function_bind_config,
866 };
867 #endif
868
869 #define MAX_SERIAL_INSTANCES 4
870
871 struct serial_function_config {
872 int port_num;
873 struct usb_function *f_ser[MAX_SERIAL_INSTANCES];
874 struct usb_function_instance *f_ser_inst[MAX_SERIAL_INSTANCES];
875 };
876
877 static int serial_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
878 {
879 int i;
880 int ret;
881 struct serial_function_config *config;
882 #ifdef CONFIG_USBIF_COMPLIANCE
883 /* FIXME, USB_IF workaround */
884 return 0;
885 #endif
886
887 config = kzalloc(sizeof(struct serial_function_config), GFP_KERNEL);
888 if (!config)
889 return -ENOMEM;
890 f->config = config;
891
892 for (i = 0; i < MAX_SERIAL_INSTANCES; i++) {
893 config->f_ser_inst[i] = usb_get_function_instance("gser");
894 if (IS_ERR(config->f_ser_inst[i])) {
895 ret = PTR_ERR(config->f_ser_inst[i]);
896 goto err_usb_get_function_instance;
897 }
898 config->f_ser[i] = usb_get_function(config->f_ser_inst[i]);
899 if (IS_ERR(config->f_ser[i])) {
900 ret = PTR_ERR(config->f_ser[i]);
901 goto err_usb_get_function;
902 }
903 }
904 return 0;
905 err_usb_get_function_instance:
906 pr_err("Could not usb_get_function_instance() %d\n", i);
907 while (i-- > 0) {
908 usb_put_function(config->f_ser[i]);
909 err_usb_get_function:
910 pr_err("Could not usb_get_function() %d\n", i);
911 usb_put_function_instance(config->f_ser_inst[i]);
912 }
913 return ret;
914 }
915
916 static void serial_function_cleanup(struct android_usb_function *f)
917 {
918 int i;
919 struct serial_function_config *config = f->config;
920 #ifdef CONFIG_USBIF_COMPLIANCE
921 /* FIXME, USB_IF workaround */
922 return 0;
923 #endif
924
925 for (i = 0; i < MAX_SERIAL_INSTANCES; i++) {
926 usb_put_function(config->f_ser[i]);
927 usb_put_function_instance(config->f_ser_inst[i]);
928 }
929 kfree(f->config);
930 f->config = NULL;
931 }
932
933 static int
934 serial_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
935 {
936 int i;
937 int ret = 0;
938 struct serial_function_config *config = f->config;
939
940 ret = usb_add_function(c, config->f_ser[config->port_num]);
941 if (ret) {
942 pr_err("Could not bind ser%u config\n", config->port_num);
943 goto err_usb_add_function;
944 }
945 pr_info("%s Open /dev/ttyGS%d\n", __func__, i);
946
947 return 0;
948
949 err_usb_add_function:
950 while (i-- > 0)
951 usb_remove_function(c, config->f_ser[i]);
952 return ret;
953 }
954
955 static ssize_t serial_port_show(struct device *dev,
956 struct device_attribute *attr, char *buf)
957 {
958 struct android_usb_function *f = dev_get_drvdata(dev);
959 struct serial_function_config *config = f->config;
960 return sprintf(buf, "%d\n", config->port_num);
961 }
962
963 static ssize_t serial_port_store(struct device *dev,
964 struct device_attribute *attr, const char *buf, size_t size)
965 {
966 struct android_usb_function *f = dev_get_drvdata(dev);
967 struct serial_function_config *config = f->config;
968 int value;
969
970 sscanf(buf, "%d", &value);
971 if (value > MAX_SERIAL_INSTANCES)
972 value = MAX_SERIAL_INSTANCES;
973 config->port_num = value;
974 return size;
975 }
976
977 static DEVICE_ATTR(port, S_IRUGO | S_IWUSR, serial_port_show, serial_port_store);
978 static struct device_attribute *serial_function_attributes[] = { &dev_attr_port, NULL };
979
980 static struct android_usb_function serial_function = {
981 .name = "gser",
982 .init = serial_function_init,
983 .cleanup = serial_function_cleanup,
984 .bind_config = serial_function_bind_config,
985 .attributes = serial_function_attributes,
986 };
987
988 static int
989 mtp_function_init(struct android_usb_function *f,
990 struct usb_composite_dev *cdev)
991 {
992 return mtp_setup();
993 }
994
995 static void mtp_function_cleanup(struct android_usb_function *f)
996 {
997 mtp_cleanup();
998 }
999
1000 static int
1001 mtp_function_bind_config(struct android_usb_function *f,
1002 struct usb_configuration *c)
1003 {
1004 return mtp_bind_config(c, false);
1005 }
1006
1007 static int
1008 ptp_function_init(struct android_usb_function *f,
1009 struct usb_composite_dev *cdev)
1010 {
1011 /* nothing to do - initialization is handled by mtp_function_init */
1012 return 0;
1013 }
1014
1015 static void ptp_function_cleanup(struct android_usb_function *f)
1016 {
1017 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1018 }
1019
1020 static int
1021 ptp_function_bind_config(struct android_usb_function *f,
1022 struct usb_configuration *c)
1023 {
1024 return mtp_bind_config(c, true);
1025 }
1026
1027 static int mtp_function_ctrlrequest(struct android_usb_function *f,
1028 struct usb_composite_dev *cdev,
1029 const struct usb_ctrlrequest *c)
1030 {
1031 /* MTP MSFT OS Descriptor */
1032 struct android_dev *dev = _android_dev;
1033 struct android_usb_function *f_count;
1034 int functions_no=0;
1035 char usb_function_string[32];
1036 char * buff = usb_function_string;
1037
1038 list_for_each_entry(f_count, &dev->enabled_functions, enabled_list)
1039 {
1040 functions_no++;
1041 buff += sprintf(buff, "%s,", f_count->name);
1042 }
1043 *(buff-1) = '\n';
1044
1045 mtp_read_usb_functions(functions_no, usb_function_string);
1046 return mtp_ctrlrequest(cdev, c);
1047 }
1048
1049 static int ptp_function_ctrlrequest(struct android_usb_function *f,
1050 struct usb_composite_dev *cdev,
1051 const struct usb_ctrlrequest *c)
1052 {
1053 return ptp_ctrlrequest(cdev, c);
1054 }
1055
1056
1057 static struct android_usb_function mtp_function = {
1058 .name = "mtp",
1059 .init = mtp_function_init,
1060 .cleanup = mtp_function_cleanup,
1061 .bind_config = mtp_function_bind_config,
1062 .ctrlrequest = mtp_function_ctrlrequest,
1063 };
1064
1065 /* PTP function is same as MTP with slightly different interface descriptor */
1066 static struct android_usb_function ptp_function = {
1067 .name = "ptp",
1068 .init = ptp_function_init,
1069 .cleanup = ptp_function_cleanup,
1070 .bind_config = ptp_function_bind_config,
1071 .ctrlrequest = ptp_function_ctrlrequest, //ALPS01832160
1072 };
1073
1074 struct ecm_function_config {
1075 u8 ethaddr[ETH_ALEN];
1076 struct eth_dev *dev;
1077 };
1078
1079 static int ecm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
1080 {
1081 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
1082 if (!f->config)
1083 return -ENOMEM;
1084 return 0;
1085 }
1086
1087 static void ecm_function_cleanup(struct android_usb_function *f)
1088 {
1089 kfree(f->config);
1090 f->config = NULL;
1091 }
1092
1093 static int
1094 ecm_function_bind_config(struct android_usb_function *f,
1095 struct usb_configuration *c)
1096 {
1097 int ret;
1098 struct eth_dev *dev;
1099 struct ecm_function_config *ecm = f->config;
1100
1101 if (!ecm) {
1102 pr_err("%s: ecm_pdata\n", __func__);
1103 return -1;
1104 }
1105
1106
1107 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1108 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1109 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1110
1111 dev = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "rndis");
1112 if (IS_ERR(dev)) {
1113 ret = PTR_ERR(dev);
1114 pr_err("%s: gether_setup failed\n", __func__);
1115 return ret;
1116 }
1117 ecm->dev = dev;
1118
1119 return ecm_bind_config(c, ecm->ethaddr, ecm->dev);
1120 }
1121
1122 static void ecm_function_unbind_config(struct android_usb_function *f,
1123 struct usb_configuration *c)
1124 {
1125 struct ecm_function_config *ecm = f->config;
1126 gether_cleanup(ecm->dev);
1127 }
1128
1129 static ssize_t ecm_ethaddr_show(struct device *dev,
1130 struct device_attribute *attr, char *buf)
1131 {
1132 struct android_usb_function *f = dev_get_drvdata(dev);
1133 struct ecm_function_config *ecm = f->config;
1134 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1135 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1136 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1137 }
1138
1139 static ssize_t ecm_ethaddr_store(struct device *dev,
1140 struct device_attribute *attr, const char *buf, size_t size)
1141 {
1142 struct android_usb_function *f = dev_get_drvdata(dev);
1143 struct ecm_function_config *ecm = f->config;
1144
1145 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1146 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
1147 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
1148 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
1149 return size;
1150 return -EINVAL;
1151 }
1152
1153 static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
1154 ecm_ethaddr_store);
1155
1156 static struct device_attribute *ecm_function_attributes[] = {
1157 &dev_attr_ecm_ethaddr,
1158 NULL
1159 };
1160
1161 static struct android_usb_function ecm_function = {
1162 .name = "ecm",
1163 .init = ecm_function_init,
1164 .cleanup = ecm_function_cleanup,
1165 .bind_config = ecm_function_bind_config,
1166 .unbind_config = ecm_function_unbind_config,
1167 .attributes = ecm_function_attributes,
1168 };
1169
1170 struct eem_function_config {
1171 u8 ethaddr[ETH_ALEN];
1172 char manufacturer[256];
1173 struct eth_dev *dev;
1174 };
1175
1176 static int
1177 eem_function_init(struct android_usb_function *f,
1178 struct usb_composite_dev *cdev)
1179 {
1180 f->config = kzalloc(sizeof(struct eem_function_config), GFP_KERNEL);
1181 if (!f->config)
1182 return -ENOMEM;
1183 return 0;
1184 }
1185
1186 static void eem_function_cleanup(struct android_usb_function *f)
1187 {
1188 kfree(f->config);
1189 f->config = NULL;
1190 }
1191
1192 static int
1193 eem_function_bind_config(struct android_usb_function *f,
1194 struct usb_configuration *c)
1195 {
1196 int ret;
1197 struct eth_dev *dev;
1198 struct eem_function_config *eem = f->config;
1199
1200 pr_debug("[XLOG_DEBUG][USB]%s: \n", __func__);
1201
1202 if (!eem) {
1203 pr_err("%s: rndis_pdata\n", __func__);
1204 return -1;
1205 }
1206
1207 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1208 eem->ethaddr[0], eem->ethaddr[1], eem->ethaddr[2],
1209 eem->ethaddr[3], eem->ethaddr[4], eem->ethaddr[5]);
1210
1211 dev = gether_setup_name(c->cdev->gadget, eem->ethaddr, "rndis");
1212 if (IS_ERR(dev)) {
1213 ret = PTR_ERR(dev);
1214 pr_err("%s: gether_setup failed\n", __func__);
1215 return ret;
1216 }
1217 eem->dev = dev;
1218
1219 return eem_bind_config(c, eem->dev);
1220 }
1221
1222 static void eem_function_unbind_config(struct android_usb_function *f,
1223 struct usb_configuration *c)
1224 {
1225 struct eem_function_config *eem = f->config;
1226 gether_cleanup(eem->dev);
1227 }
1228
1229 static ssize_t eem_ethaddr_show(struct device *dev,
1230 struct device_attribute *attr, char *buf)
1231 {
1232 struct android_usb_function *f = dev_get_drvdata(dev);
1233 struct eem_function_config *eem = f->config;
1234 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1235 eem->ethaddr[0], eem->ethaddr[1], eem->ethaddr[2],
1236 eem->ethaddr[3], eem->ethaddr[4], eem->ethaddr[5]);
1237 }
1238
1239 static ssize_t eem_ethaddr_store(struct device *dev,
1240 struct device_attribute *attr, const char *buf, size_t size)
1241 {
1242 struct android_usb_function *f = dev_get_drvdata(dev);
1243 struct eem_function_config *eem = f->config;
1244
1245 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1246 (int *)&eem->ethaddr[0], (int *)&eem->ethaddr[1],
1247 (int *)&eem->ethaddr[2], (int *)&eem->ethaddr[3],
1248 (int *)&eem->ethaddr[4], (int *)&eem->ethaddr[5]) == 6)
1249 return size;
1250 return -EINVAL;
1251 }
1252
1253 static DEVICE_ATTR(eem_ethaddr, S_IRUGO | S_IWUSR, eem_ethaddr_show,
1254 eem_ethaddr_store);
1255
1256 static struct device_attribute *eem_function_attributes[] = {
1257 &dev_attr_eem_ethaddr,
1258 NULL
1259 };
1260
1261 static struct android_usb_function eem_function = {
1262 .name = "eem",
1263 .init = eem_function_init,
1264 .cleanup = eem_function_cleanup,
1265 .bind_config = eem_function_bind_config,
1266 .unbind_config = eem_function_unbind_config,
1267 .attributes = eem_function_attributes,
1268 };
1269
1270 struct rndis_function_config {
1271 u8 ethaddr[ETH_ALEN];
1272 u32 vendorID;
1273 char manufacturer[256];
1274 /* "Wireless" RNDIS; auto-detected by Windows */
1275 bool wceis;
1276 struct eth_dev *dev;
1277 };
1278
1279 static int
1280 rndis_function_init(struct android_usb_function *f,
1281 struct usb_composite_dev *cdev)
1282 {
1283 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1284 if (!f->config)
1285 return -ENOMEM;
1286 return 0;
1287 }
1288
1289 static void rndis_function_cleanup(struct android_usb_function *f)
1290 {
1291 kfree(f->config);
1292 f->config = NULL;
1293 }
1294
1295 static int
1296 rndis_function_bind_config(struct android_usb_function *f,
1297 struct usb_configuration *c)
1298 {
1299 int ret;
1300 struct eth_dev *dev;
1301 struct rndis_function_config *rndis = f->config;
1302
1303 pr_debug("[XLOG_DEBUG][USB]%s: \n", __func__);
1304
1305 if (!rndis) {
1306 pr_err("%s: rndis_pdata\n", __func__);
1307 return -1;
1308 }
1309
1310 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1311 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1312 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1313
1314 dev = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1315 if (IS_ERR(dev)) {
1316 ret = PTR_ERR(dev);
1317 pr_err("%s: gether_setup failed\n", __func__);
1318 return ret;
1319 }
1320 rndis->dev = dev;
1321
1322 if (rndis->wceis) {
1323 /* "Wireless" RNDIS; auto-detected by Windows */
1324 rndis_iad_descriptor.bFunctionClass =
1325 USB_CLASS_WIRELESS_CONTROLLER;
1326 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1327 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1328 rndis_control_intf.bInterfaceClass =
1329 USB_CLASS_WIRELESS_CONTROLLER;
1330 rndis_control_intf.bInterfaceSubClass = 0x01;
1331 rndis_control_intf.bInterfaceProtocol = 0x03;
1332 }
1333
1334 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1335 rndis->manufacturer, rndis->dev);
1336 }
1337
1338 static void rndis_function_unbind_config(struct android_usb_function *f,
1339 struct usb_configuration *c)
1340 {
1341 struct rndis_function_config *rndis = f->config;
1342 gether_cleanup(rndis->dev);
1343 }
1344
1345 static ssize_t rndis_manufacturer_show(struct device *dev,
1346 struct device_attribute *attr, char *buf)
1347 {
1348 struct android_usb_function *f = dev_get_drvdata(dev);
1349 struct rndis_function_config *config = f->config;
1350 return sprintf(buf, "%s\n", config->manufacturer);
1351 }
1352
1353 static ssize_t rndis_manufacturer_store(struct device *dev,
1354 struct device_attribute *attr, const char *buf, size_t size)
1355 {
1356 struct android_usb_function *f = dev_get_drvdata(dev);
1357 struct rndis_function_config *config = f->config;
1358
1359 if (size >= sizeof(config->manufacturer))
1360 return -EINVAL;
1361 if (sscanf(buf, "%s", config->manufacturer) == 1)
1362 return size;
1363 return -1;
1364 }
1365
1366 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1367 rndis_manufacturer_store);
1368
1369 static ssize_t rndis_wceis_show(struct device *dev,
1370 struct device_attribute *attr, char *buf)
1371 {
1372 struct android_usb_function *f = dev_get_drvdata(dev);
1373 struct rndis_function_config *config = f->config;
1374 return sprintf(buf, "%d\n", config->wceis);
1375 }
1376
1377 static ssize_t rndis_wceis_store(struct device *dev,
1378 struct device_attribute *attr, const char *buf, size_t size)
1379 {
1380 struct android_usb_function *f = dev_get_drvdata(dev);
1381 struct rndis_function_config *config = f->config;
1382 int value;
1383
1384 if (sscanf(buf, "%d", &value) == 1) {
1385 config->wceis = value;
1386 return size;
1387 }
1388 return -EINVAL;
1389 }
1390
1391 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1392 rndis_wceis_store);
1393
1394 static ssize_t rndis_ethaddr_show(struct device *dev,
1395 struct device_attribute *attr, char *buf)
1396 {
1397 struct android_usb_function *f = dev_get_drvdata(dev);
1398 struct rndis_function_config *rndis = f->config;
1399 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1400 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1401 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1402 }
1403
1404 static ssize_t rndis_ethaddr_store(struct device *dev,
1405 struct device_attribute *attr, const char *buf, size_t size)
1406 {
1407 struct android_usb_function *f = dev_get_drvdata(dev);
1408 struct rndis_function_config *rndis = f->config;
1409
1410 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1411 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1412 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1413 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1414 return size;
1415 return -EINVAL;
1416 }
1417
1418 static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1419 rndis_ethaddr_store);
1420
1421 static ssize_t rndis_vendorID_show(struct device *dev,
1422 struct device_attribute *attr, char *buf)
1423 {
1424 struct android_usb_function *f = dev_get_drvdata(dev);
1425 struct rndis_function_config *config = f->config;
1426 return sprintf(buf, "%04x\n", config->vendorID);
1427 }
1428
1429 static ssize_t rndis_vendorID_store(struct device *dev,
1430 struct device_attribute *attr, const char *buf, size_t size)
1431 {
1432 struct android_usb_function *f = dev_get_drvdata(dev);
1433 struct rndis_function_config *config = f->config;
1434 int value;
1435
1436 if (sscanf(buf, "%04x", &value) == 1) {
1437 config->vendorID = value;
1438 return size;
1439 }
1440 return -EINVAL;
1441 }
1442
1443 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1444 rndis_vendorID_store);
1445
1446 static struct device_attribute *rndis_function_attributes[] = {
1447 &dev_attr_manufacturer,
1448 &dev_attr_wceis,
1449 &dev_attr_ethaddr,
1450 &dev_attr_vendorID,
1451 NULL
1452 };
1453
1454 static struct android_usb_function rndis_function = {
1455 .name = "rndis",
1456 .init = rndis_function_init,
1457 .cleanup = rndis_function_cleanup,
1458 .bind_config = rndis_function_bind_config,
1459 .unbind_config = rndis_function_unbind_config,
1460 .attributes = rndis_function_attributes,
1461 };
1462
1463
1464 struct mass_storage_function_config {
1465 struct fsg_config fsg;
1466 struct fsg_common *common;
1467 };
1468
1469 static int mass_storage_function_init(struct android_usb_function *f,
1470 struct usb_composite_dev *cdev)
1471 {
1472 struct mass_storage_function_config *config;
1473 struct fsg_common *common;
1474 int err;
1475 int i;
1476
1477 config = kzalloc(sizeof(struct mass_storage_function_config),
1478 GFP_KERNEL);
1479 if (!config)
1480 return -ENOMEM;
1481
1482 #ifdef CONFIG_MTK_MULTI_STORAGE_SUPPORT
1483 #define LUN_MULTI (1)
1484 #else
1485 #define LUN_MULTI (0)
1486 #endif
1487
1488 #ifdef CONFIG_MTK_SHARED_SDCARD
1489 #define LUN_SHARED_SD (-1)
1490 #else
1491 #define LUN_SHARED_SD (0)
1492 #endif
1493
1494 #ifdef CONFIG_MTK_ICUSB_SUPPORT
1495 #define LUN_ICUSB (1)
1496 #else
1497 #define LUN_ICUSB (0)
1498 #endif
1499
1500 #define LUN_NUM LUN_MULTI + LUN_SHARED_SD + LUN_ICUSB + 1
1501
1502 config->fsg.nluns = LUN_NUM;
1503
1504 for(i = 0; i < config->fsg.nluns; i++) {
1505 config->fsg.luns[i].removable = 1;
1506 config->fsg.luns[i].nofua = 1;
1507 }
1508
1509 common = fsg_common_init(NULL, cdev, &config->fsg);
1510 if (IS_ERR(common)) {
1511 kfree(config);
1512 return PTR_ERR(common);
1513 }
1514
1515 err = sysfs_create_link(&f->dev->kobj,
1516 &common->luns[0].dev.kobj,
1517 "lun");
1518 if (err) {
1519 kfree(config);
1520 return err;
1521 }
1522
1523 /*
1524 * "i" starts from "1", cuz dont want to change the naming of
1525 * the original path of "lun0".
1526 */
1527 for(i = 1; i < config->fsg.nluns; i++) {
1528 char string_lun[5]={0};
1529
1530 sprintf(string_lun, "lun%d",i);
1531
1532 err = sysfs_create_link(&f->dev->kobj,
1533 &common->luns[i].dev.kobj,
1534 string_lun);
1535 if (err) {
1536 kfree(config);
1537 return err;
1538 }
1539 }
1540
1541 config->common = common;
1542 f->config = config;
1543 return 0;
1544 }
1545
1546 static void mass_storage_function_cleanup(struct android_usb_function *f)
1547 {
1548 kfree(f->config);
1549 f->config = NULL;
1550 }
1551
1552 static int mass_storage_function_bind_config(struct android_usb_function *f,
1553 struct usb_configuration *c)
1554 {
1555 struct mass_storage_function_config *config = f->config;
1556 return fsg_bind_config(c->cdev, c, config->common);
1557 }
1558
1559 static ssize_t mass_storage_inquiry_show(struct device *dev,
1560 struct device_attribute *attr, char *buf)
1561 {
1562 struct android_usb_function *f = dev_get_drvdata(dev);
1563 struct mass_storage_function_config *config = f->config;
1564 return sprintf(buf, "%s\n", config->common->inquiry_string);
1565 }
1566
1567 static ssize_t mass_storage_inquiry_store(struct device *dev,
1568 struct device_attribute *attr, const char *buf, size_t size)
1569 {
1570 struct android_usb_function *f = dev_get_drvdata(dev);
1571 struct mass_storage_function_config *config = f->config;
1572 if (size >= sizeof(config->common->inquiry_string))
1573 return -EINVAL;
1574 if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
1575 return -EINVAL;
1576 return size;
1577 }
1578
1579 static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1580 mass_storage_inquiry_show,
1581 mass_storage_inquiry_store);
1582
1583 #ifdef CONFIG_MTK_BICR_SUPPORT
1584
1585 static ssize_t mass_storage_bicr_show(struct device *dev,
1586 struct device_attribute *attr, char *buf)
1587 {
1588 struct android_usb_function *f = dev_get_drvdata(dev);
1589 struct mass_storage_function_config *config = f->config;
1590 return sprintf(buf, "%d\n", config->common->bicr);
1591 }
1592
1593 static ssize_t mass_storage_bicr_store(struct device *dev,
1594 struct device_attribute *attr, const char *buf, size_t size)
1595 {
1596 struct android_usb_function *f = dev_get_drvdata(dev);
1597 struct mass_storage_function_config *config = f->config;
1598 if (size >= sizeof(config->common->bicr))
1599 return -EINVAL;
1600 if (sscanf(buf, "%d", &config->common->bicr) != 1)
1601 return -EINVAL;
1602
1603 /* Set Lun[0] is a CDROM when enable bicr.*/
1604 if (!strcmp(buf, "1"))
1605 config->common->luns[0].cdrom = 1;
1606 else {
1607 /*Reset the value. Clean the cdrom's parameters*/
1608 config->common->luns[0].cdrom = 0;
1609 config->common->luns[0].blkbits = 0;
1610 config->common->luns[0].blksize = 0;
1611 config->common->luns[0].num_sectors = 0;
1612 }
1613
1614 return size;
1615 }
1616
1617 static DEVICE_ATTR(bicr, S_IRUGO | S_IWUSR,
1618 mass_storage_bicr_show,
1619 mass_storage_bicr_store);
1620
1621 #endif
1622
1623 static struct device_attribute *mass_storage_function_attributes[] = {
1624 &dev_attr_inquiry_string,
1625 #ifdef CONFIG_MTK_BICR_SUPPORT
1626 &dev_attr_bicr,
1627 #endif
1628 NULL
1629 };
1630
1631 static struct android_usb_function mass_storage_function = {
1632 .name = "mass_storage",
1633 .init = mass_storage_function_init,
1634 .cleanup = mass_storage_function_cleanup,
1635 .bind_config = mass_storage_function_bind_config,
1636 .attributes = mass_storage_function_attributes,
1637 };
1638
1639
1640 static int accessory_function_init(struct android_usb_function *f,
1641 struct usb_composite_dev *cdev)
1642 {
1643 return acc_setup();
1644 }
1645
1646 static void accessory_function_cleanup(struct android_usb_function *f)
1647 {
1648 acc_cleanup();
1649 }
1650
1651 static int accessory_function_bind_config(struct android_usb_function *f,
1652 struct usb_configuration *c)
1653 {
1654 return acc_bind_config(c);
1655 }
1656
1657 static int accessory_function_ctrlrequest(struct android_usb_function *f,
1658 struct usb_composite_dev *cdev,
1659 const struct usb_ctrlrequest *c)
1660 {
1661 return acc_ctrlrequest(cdev, c);
1662 }
1663
1664 static struct android_usb_function accessory_function = {
1665 .name = "accessory",
1666 .init = accessory_function_init,
1667 .cleanup = accessory_function_cleanup,
1668 .bind_config = accessory_function_bind_config,
1669 .ctrlrequest = accessory_function_ctrlrequest,
1670 };
1671
1672 static int audio_source_function_init(struct android_usb_function *f,
1673 struct usb_composite_dev *cdev)
1674 {
1675 struct audio_source_config *config;
1676
1677 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1678 if (!config)
1679 return -ENOMEM;
1680 config->card = -1;
1681 config->device = -1;
1682 f->config = config;
1683 return 0;
1684 }
1685
1686 static void audio_source_function_cleanup(struct android_usb_function *f)
1687 {
1688 kfree(f->config);
1689 }
1690
1691 static int audio_source_function_bind_config(struct android_usb_function *f,
1692 struct usb_configuration *c)
1693 {
1694 struct audio_source_config *config = f->config;
1695
1696 return audio_source_bind_config(c, config);
1697 }
1698
1699 static void audio_source_function_unbind_config(struct android_usb_function *f,
1700 struct usb_configuration *c)
1701 {
1702 struct audio_source_config *config = f->config;
1703
1704 config->card = -1;
1705 config->device = -1;
1706 }
1707
1708 static ssize_t audio_source_pcm_show(struct device *dev,
1709 struct device_attribute *attr, char *buf)
1710 {
1711 struct android_usb_function *f = dev_get_drvdata(dev);
1712 struct audio_source_config *config = f->config;
1713
1714 /* print PCM card and device numbers */
1715 return sprintf(buf, "%d %d\n", config->card, config->device);
1716 }
1717
1718 static DEVICE_ATTR(pcm, S_IRUGO, audio_source_pcm_show, NULL);
1719
1720 static struct device_attribute *audio_source_function_attributes[] = {
1721 &dev_attr_pcm,
1722 NULL
1723 };
1724
1725 #ifdef CONFIG_EVDO_DT_SUPPORT
1726 static int rawbulk_function_init(struct android_usb_function *f,
1727 struct usb_composite_dev *cdev)
1728 {
1729 return 0;
1730 }
1731
1732 static void rawbulk_function_cleanup(struct android_usb_function *f)
1733 {
1734 ;
1735 }
1736
1737 static int rawbulk_function_bind_config(struct android_usb_function *f,
1738 struct usb_configuration *c)
1739 {
1740 char *i = f->name + strlen("via_");
1741 if (!strncmp(i, "modem", 5))
1742 return rawbulk_bind_config(c, RAWBULK_TID_MODEM);
1743 else if (!strncmp(i, "ets", 3))
1744 return rawbulk_bind_config(c, RAWBULK_TID_ETS);
1745 else if (!strncmp(i, "atc", 3))
1746 return rawbulk_bind_config(c, RAWBULK_TID_AT);
1747 else if (!strncmp(i, "pcv", 3))
1748 return rawbulk_bind_config(c, RAWBULK_TID_PCV);
1749 else if (!strncmp(i, "gps", 3))
1750 return rawbulk_bind_config(c, RAWBULK_TID_GPS);
1751 return -EINVAL;
1752 }
1753
1754 static int rawbulk_function_modem_ctrlrequest(struct android_usb_function *f,
1755 struct usb_composite_dev *cdev,
1756 const struct usb_ctrlrequest *c)
1757 {
1758 if ((c->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE &&
1759 (c->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
1760 struct rawbulk_function *fn = rawbulk_lookup_function(RAWBULK_TID_MODEM);
1761 return rawbulk_function_setup(&fn->function, c);
1762 }
1763 return -1;
1764 }
1765
1766 static struct android_usb_function rawbulk_modem_function = {
1767 .name = "via_modem",
1768 .init = rawbulk_function_init,
1769 .cleanup = rawbulk_function_cleanup,
1770 .bind_config = rawbulk_function_bind_config,
1771 .ctrlrequest = rawbulk_function_modem_ctrlrequest,
1772 };
1773
1774 static struct android_usb_function rawbulk_ets_function = {
1775 .name = "via_ets",
1776 .init = rawbulk_function_init,
1777 .cleanup = rawbulk_function_cleanup,
1778 .bind_config = rawbulk_function_bind_config,
1779 };
1780
1781 static struct android_usb_function rawbulk_atc_function = {
1782 .name = "via_atc",
1783 .init = rawbulk_function_init,
1784 .cleanup = rawbulk_function_cleanup,
1785 .bind_config = rawbulk_function_bind_config,
1786 };
1787
1788 static struct android_usb_function rawbulk_pcv_function = {
1789 .name = "via_pcv",
1790 .init = rawbulk_function_init,
1791 .cleanup = rawbulk_function_cleanup,
1792 .bind_config = rawbulk_function_bind_config,
1793 };
1794
1795 static struct android_usb_function rawbulk_gps_function = {
1796 .name = "via_gps",
1797 .init = rawbulk_function_init,
1798 .cleanup = rawbulk_function_cleanup,
1799 .bind_config = rawbulk_function_bind_config,
1800 };
1801 #endif
1802
1803 static struct android_usb_function audio_source_function = {
1804 .name = "audio_source",
1805 .init = audio_source_function_init,
1806 .cleanup = audio_source_function_cleanup,
1807 .bind_config = audio_source_function_bind_config,
1808 .unbind_config = audio_source_function_unbind_config,
1809 .attributes = audio_source_function_attributes,
1810 };
1811
1812 static struct android_usb_function *supported_functions[] = {
1813 &ffs_function,
1814 &adb_function,
1815 &acm_function,
1816 &mtp_function,
1817 &ptp_function,
1818 #ifndef CONFIG_USBIF_COMPLIANCE
1819 &ecm_function,
1820 &eem_function,
1821 #endif
1822 &serial_function,
1823 &rndis_function,
1824 &mass_storage_function,
1825 &accessory_function,
1826 &audio_source_function,
1827 #ifdef CONFIG_EVDO_DT_SUPPORT
1828 &rawbulk_modem_function,
1829 &rawbulk_ets_function,
1830 &rawbulk_atc_function,
1831 &rawbulk_pcv_function,
1832 &rawbulk_gps_function,
1833 #endif
1834 #ifdef CONFIG_USB_F_LOOPBACK
1835 &loopback_function,
1836 #endif
1837 NULL
1838 };
1839
1840
1841 static int android_init_functions(struct android_usb_function **functions,
1842 struct usb_composite_dev *cdev)
1843 {
1844 struct android_dev *dev = _android_dev;
1845 struct android_usb_function *f;
1846 struct device_attribute **attrs;
1847 struct device_attribute *attr;
1848 int err;
1849
1850 #ifdef CONFIG_USBIF_COMPLIANCE
1851 int index = 1;
1852 #else
1853 int index = 0;
1854 #endif
1855
1856 for (; (f = *functions++); index++) {
1857 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
1858 /* Added for USB Develpment debug, more log for more debuging help */
1859 pr_debug("[XLOG_INFO][USB]%s: f->dev_name = %s, f->name = %s\n", __func__, f->dev_name, f->name);
1860 /* Added for USB Develpment debug, more log for more debuging help */
1861 f->dev = device_create(android_class, dev->dev,
1862 MKDEV(0, index), f, f->dev_name);
1863 if (IS_ERR(f->dev)) {
1864 pr_err("%s: Failed to create dev %s", __func__,
1865 f->dev_name);
1866 err = PTR_ERR(f->dev);
1867 goto err_create;
1868 }
1869
1870 if (f->init) {
1871 err = f->init(f, cdev);
1872 if (err) {
1873 pr_err("%s: Failed to init %s", __func__,
1874 f->name);
1875 goto err_out;
1876 } else {
1877 pr_debug("[XLOG_INFO][USB]%s: init %s success!!\n", __func__, f->name);
1878 }
1879 }
1880
1881 attrs = f->attributes;
1882 if (attrs) {
1883 while ((attr = *attrs++) && !err)
1884 err = device_create_file(f->dev, attr);
1885 }
1886 if (err) {
1887 pr_err("%s: Failed to create function %s attributes",
1888 __func__, f->name);
1889 goto err_out;
1890 }
1891 }
1892 return 0;
1893
1894 err_out:
1895 device_destroy(android_class, f->dev->devt);
1896 err_create:
1897 kfree(f->dev_name);
1898 return err;
1899 }
1900
1901 static void android_cleanup_functions(struct android_usb_function **functions)
1902 {
1903 struct android_usb_function *f;
1904
1905 while (*functions) {
1906 f = *functions++;
1907
1908 if (f->dev) {
1909 device_destroy(android_class, f->dev->devt);
1910 kfree(f->dev_name);
1911 }
1912
1913 if (f->cleanup)
1914 f->cleanup(f);
1915 }
1916 }
1917
1918 static int
1919 android_bind_enabled_functions(struct android_dev *dev,
1920 struct usb_configuration *c)
1921 {
1922 struct android_usb_function *f;
1923 int ret;
1924
1925 /* Added for USB Develpment debug, more log for more debuging help */
1926 pr_debug("[XLOG_INFO][USB]%s: ", __func__);
1927 /* Added for USB Develpment debug, more log for more debuging help */
1928
1929 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1930 pr_debug("[XLOG_INFO][USB]bind_config function '%s'/%p\n", f->name, f);
1931 ret = f->bind_config(f, c);
1932 if (ret) {
1933 pr_err("%s: %s failed", __func__, f->name);
1934 return ret;
1935 }
1936 }
1937 return 0;
1938 }
1939
1940 static void
1941 android_unbind_enabled_functions(struct android_dev *dev,
1942 struct usb_configuration *c)
1943 {
1944 struct android_usb_function *f;
1945
1946 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1947 pr_debug("[XLOG_INFO][USB]unbind_config function '%s'/%p\n", f->name, f);
1948 if (f->unbind_config)
1949 f->unbind_config(f, c);
1950 }
1951 }
1952
1953 static int android_enable_function(struct android_dev *dev, char *name)
1954 {
1955 struct android_usb_function **functions = dev->functions;
1956 struct android_usb_function *f;
1957 while ((f = *functions++)) {
1958
1959 /* Added for USB Develpment debug, more log for more debuging help */
1960 pr_debug("[XLOG_INFO][USB]%s: name = %s, f->name=%s \n", __func__, name, f->name);
1961 /* Added for USB Develpment debug, more log for more debuging help */
1962 if (!strcmp(name, f->name)) {
1963 list_add_tail(&f->enabled_list,
1964 &dev->enabled_functions);
1965 return 0;
1966 }
1967 }
1968 return -EINVAL;
1969 }
1970
1971 /*-------------------------------------------------------------------------*/
1972 /* /sys/class/android_usb/android%d/ interface */
1973
1974 static ssize_t
1975 functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1976 {
1977 struct android_dev *dev = dev_get_drvdata(pdev);
1978 struct android_usb_function *f;
1979 char *buff = buf;
1980
1981 /* Added for USB Develpment debug, more log for more debuging help */
1982 pr_debug("[XLOG_INFO][USB]%s: ", __func__);
1983 /* Added for USB Develpment debug, more log for more debuging help */
1984
1985 mutex_lock(&dev->mutex);
1986
1987 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
1988 buff += sprintf(buff, "%s,", f->name);
1989
1990 mutex_unlock(&dev->mutex);
1991
1992 if (buff != buf)
1993 *(buff-1) = '\n';
1994 return buff - buf;
1995 }
1996
1997 static ssize_t
1998 functions_store(struct device *pdev, struct device_attribute *attr,
1999 const char *buff, size_t size)
2000 {
2001 struct android_dev *dev = dev_get_drvdata(pdev);
2002 char *name;
2003 char buf[256], *b;
2004 char aliases[256], *a;
2005 int err;
2006 int is_ffs;
2007 int ffs_enabled = 0;
2008
2009 mutex_lock(&dev->mutex);
2010
2011 if (dev->enabled) {
2012 mutex_unlock(&dev->mutex);
2013 return -EBUSY;
2014 }
2015
2016 INIT_LIST_HEAD(&dev->enabled_functions);
2017
2018 /* Added for USB Develpment debug, more log for more debuging help */
2019 pr_debug("[XLOG_INFO][USB]%s: \n", __func__);
2020 /* Added for USB Develpment debug, more log for more debuging help */
2021
2022 strlcpy(buf, buff, sizeof(buf));
2023 b = strim(buf);
2024
2025 while (b) {
2026 name = strsep(&b, ",");
2027
2028 /* Added for USB Develpment debug, more log for more debuging help */
2029 pr_debug("[XLOG_INFO][USB]%s: name = %s \n", __func__, name);
2030 /* Added for USB Develpment debug, more log for more debuging help */
2031
2032 if (!name)
2033 continue;
2034
2035 is_ffs = 0;
2036 strlcpy(aliases, dev->ffs_aliases, sizeof(aliases));
2037 a = aliases;
2038
2039 while (a) {
2040 char *alias = strsep(&a, ",");
2041 if (alias && !strcmp(name, alias)) {
2042 is_ffs = 1;
2043 break;
2044 }
2045 }
2046
2047 if (is_ffs) {
2048 if (ffs_enabled)
2049 continue;
2050 err = android_enable_function(dev, "ffs");
2051 if (err)
2052 pr_err("android_usb: Cannot enable ffs (%d)",
2053 err);
2054 else
2055 ffs_enabled = 1;
2056 continue;
2057 }
2058
2059 err = android_enable_function(dev, name);
2060 if (err)
2061 pr_err("android_usb: Cannot enable '%s' (%d)",
2062 name, err);
2063 }
2064
2065 mutex_unlock(&dev->mutex);
2066
2067 return size;
2068 }
2069
2070 static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2071 char *buf)
2072 {
2073 struct android_dev *dev = dev_get_drvdata(pdev);
2074 return sprintf(buf, "%d\n", dev->enabled);
2075 }
2076
2077 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2078 const char *buff, size_t size)
2079 {
2080 struct android_dev *dev = dev_get_drvdata(pdev);
2081 struct usb_composite_dev *cdev = dev->cdev;
2082 struct android_usb_function *f;
2083 int enabled = 0;
2084
2085
2086 if (!cdev)
2087 return -ENODEV;
2088
2089 mutex_lock(&dev->mutex);
2090
2091 /* Added for USB Develpment debug, more log for more debuging help */
2092 pr_debug("[XLOG_INFO][USB]%s: device_attr->attr.name: %s\n", __func__, attr->attr.name);
2093 /* Added for USB Develpment debug, more log for more debuging help */
2094
2095 sscanf(buff, "%d", &enabled);
2096 if (enabled && !dev->enabled) {
2097 /* ALPS01770952
2098 * Reset next_string_id to 0 before enabling the gadget driver.
2099 * Otherwise, after a large number of enable/disable cycles,
2100 * function bind will fail because we cannot allocate new string ids.
2101 * String ids cannot be larger than 254 per USB spec.
2102 * 0~15 are reserved for usb device descriptor
2103 * 16~254 are for functions.
2104 */
2105 cdev->next_string_id = 0x10;
2106
2107 /*
2108 * Update values in composite driver's copy of
2109 * device descriptor.
2110 */
2111 cdev->desc.idVendor = device_desc.idVendor;
2112 cdev->desc.idProduct = device_desc.idProduct;
2113 cdev->desc.bcdDevice = device_desc.bcdDevice;
2114 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2115 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2116 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
2117
2118 /* special case for meta mode */
2119 if (serial_string[0] == 0x0) {
2120 cdev->desc.iSerialNumber = 0;
2121 } else {
2122 cdev->desc.iSerialNumber = device_desc.iSerialNumber;
2123 }
2124
2125 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2126 pr_debug("[XLOG_INFO][USB]enable function '%s'/%p\n", f->name, f);
2127 if (f->enable)
2128 f->enable(f);
2129 }
2130 android_enable(dev);
2131 dev->enabled = true;
2132
2133 /* Added for USB Develpment debug, more log for more debuging help */
2134 pr_debug("[XLOG_INFO][USB]%s: enable 0->1 case, device_desc.idVendor = 0x%x, device_desc.idProduct = 0x%x\n", __func__, device_desc.idVendor, device_desc.idProduct);
2135 /* Added for USB Develpment debug, more log for more debuging help */
2136
2137 } else if (!enabled && dev->enabled) {
2138
2139 /* Added for USB Develpment debug, more log for more debuging help */
2140 pr_debug("[XLOG_INFO][USB]%s: enable 1->0 case, device_desc.idVendor = 0x%x, device_desc.idProduct = 0x%x\n", __func__, device_desc.idVendor, device_desc.idProduct);
2141 /* Added for USB Develpment debug, more log for more debuging help */
2142
2143 android_disable(dev);
2144 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2145 pr_debug("[XLOG_INFO][USB]disable function '%s'/%p\n", f->name, f);
2146 if (f->disable) {
2147 f->disable(f);
2148 }
2149 }
2150 dev->enabled = false;
2151 } else {
2152 pr_err("android_usb: already %s\n",
2153 dev->enabled ? "enabled" : "disabled");
2154 /* Add for HW/SW connect */
2155 if (!usb_cable_connected()) {
2156 schedule_work(&dev->work);
2157 pr_debug("[XLOG_VERBOSE][USB]%s: enable 0->0 case - no usb cable", __func__);
2158 }
2159 /* Add for HW/SW connect */
2160 }
2161
2162 mutex_unlock(&dev->mutex);
2163 return size;
2164 }
2165
2166 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2167 char *buf)
2168 {
2169 struct android_dev *dev = dev_get_drvdata(pdev);
2170 struct usb_composite_dev *cdev = dev->cdev;
2171 char *state = "DISCONNECTED";
2172 unsigned long flags;
2173
2174 if (!cdev)
2175 goto out;
2176
2177 spin_lock_irqsave(&cdev->lock, flags);
2178 if (cdev->config)
2179 state = "CONFIGURED";
2180 else if (dev->connected)
2181 state = "CONNECTED";
2182 spin_unlock_irqrestore(&cdev->lock, flags);
2183 out:
2184 return sprintf(buf, "%s\n", state);
2185 }
2186
2187 #define DESCRIPTOR_ATTR(field, format_string) \
2188 static ssize_t \
2189 field ## _show(struct device *dev, struct device_attribute *attr, \
2190 char *buf) \
2191 { \
2192 return sprintf(buf, format_string, device_desc.field); \
2193 } \
2194 static ssize_t \
2195 field ## _store(struct device *dev, struct device_attribute *attr, \
2196 const char *buf, size_t size) \
2197 { \
2198 int value; \
2199 if (sscanf(buf, format_string, &value) == 1) { \
2200 device_desc.field = value; \
2201 return size; \
2202 } \
2203 return -1; \
2204 } \
2205 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2206
2207 #define DESCRIPTOR_STRING_ATTR(field, buffer) \
2208 static ssize_t \
2209 field ## _show(struct device *dev, struct device_attribute *attr, \
2210 char *buf) \
2211 { \
2212 return sprintf(buf, "%s", buffer); \
2213 } \
2214 static ssize_t \
2215 field ## _store(struct device *dev, struct device_attribute *attr, \
2216 const char *buf, size_t size) \
2217 { \
2218 if (size >= sizeof(buffer)) \
2219 return -EINVAL; \
2220 if (sscanf(buf, "%s", buffer) == 1) { \
2221 return size; \
2222 } \
2223 return -1; \
2224 } \
2225 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2226
2227
2228 DESCRIPTOR_ATTR(idVendor, "%04x\n")
2229 DESCRIPTOR_ATTR(idProduct, "%04x\n")
2230 DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2231 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2232 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2233 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2234 DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2235 DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2236 DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2237
2238 static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2239 functions_store);
2240 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
2241 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
2242
2243 static struct device_attribute *android_usb_attributes[] = {
2244 &dev_attr_idVendor,
2245 &dev_attr_idProduct,
2246 &dev_attr_bcdDevice,
2247 &dev_attr_bDeviceClass,
2248 &dev_attr_bDeviceSubClass,
2249 &dev_attr_bDeviceProtocol,
2250 &dev_attr_iManufacturer,
2251 &dev_attr_iProduct,
2252 &dev_attr_iSerial,
2253 &dev_attr_functions,
2254 &dev_attr_enable,
2255 &dev_attr_state,
2256 NULL
2257 };
2258
2259 /*-------------------------------------------------------------------------*/
2260 /* Composite driver */
2261
2262 static int android_bind_config(struct usb_configuration *c)
2263 {
2264 struct android_dev *dev = _android_dev;
2265 int ret = 0;
2266
2267 ret = android_bind_enabled_functions(dev, c);
2268 if (ret)
2269 return ret;
2270
2271 return 0;
2272 }
2273
2274 static void android_unbind_config(struct usb_configuration *c)
2275 {
2276 struct android_dev *dev = _android_dev;
2277
2278 android_unbind_enabled_functions(dev, c);
2279 }
2280
2281 static int android_setup_config(struct usb_configuration *c, const struct usb_ctrlrequest *ctrl)
2282 {
2283 int handled = -EINVAL;
2284 const u8 recip = ctrl->bRequestType & USB_RECIP_MASK;
2285
2286 pr_debug("%s bRequestType=%x, bRequest=%x, recip=%x\n", __func__, ctrl->bRequestType, ctrl->bRequest, recip);
2287
2288 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
2289 switch (ctrl->bRequest)
2290 {
2291 case USB_REQ_CLEAR_FEATURE:
2292 switch (recip)
2293 {
2294 case USB_RECIP_DEVICE:
2295 switch (ctrl->wValue)
2296 {
2297 case USB_DEVICE_U1_ENABLE:
2298 handled = 1;
2299 pr_debug("Clear Feature->U1 Enable\n");
2300 break;
2301
2302 case USB_DEVICE_U2_ENABLE:
2303 handled = 1;
2304 pr_debug("Clear Feature->U2 Enable\n");
2305 break;
2306
2307 default:
2308 handled = -EINVAL;
2309 break;
2310 }
2311 break;
2312 default:
2313 handled = -EINVAL;
2314 break;
2315 }
2316 break;
2317
2318 case USB_REQ_SET_FEATURE:
2319 switch (recip)
2320 {
2321 case USB_RECIP_DEVICE:
2322 switch (ctrl->wValue)
2323 {
2324 case USB_DEVICE_U1_ENABLE:
2325 pr_debug("Set Feature->U1 Enable\n");
2326 handled = 1;
2327 break;
2328 case USB_DEVICE_U2_ENABLE:
2329 pr_debug("Set Feature->U2 Enable\n");
2330 handled = 1;
2331 break;
2332 default:
2333 handled = -EINVAL;
2334 break;
2335 }
2336 break;
2337
2338 default:
2339 handled = -EINVAL;
2340 break;
2341 }
2342 break;
2343
2344 default:
2345 handled = -EINVAL;
2346 break;
2347 }
2348 }
2349
2350 return handled;
2351 }
2352
2353 static int android_bind(struct usb_composite_dev *cdev)
2354 {
2355 struct android_dev *dev = _android_dev;
2356 struct usb_gadget *gadget = cdev->gadget;
2357 int id, ret;
2358
2359 /*
2360 * Start disconnected. Userspace will connect the gadget once
2361 * it is done configuring the functions.
2362 */
2363 usb_gadget_disconnect(gadget);
2364
2365 ret = android_init_functions(dev->functions, cdev);
2366 if (ret)
2367 return ret;
2368
2369 /* Allocate string descriptor numbers ... note that string
2370 * contents can be overridden by the composite_dev glue.
2371 */
2372 id = usb_string_id(cdev);
2373 if (id < 0)
2374 return id;
2375 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2376 device_desc.iManufacturer = id;
2377
2378 id = usb_string_id(cdev);
2379 if (id < 0)
2380 return id;
2381 strings_dev[STRING_PRODUCT_IDX].id = id;
2382 device_desc.iProduct = id;
2383
2384 /* Default strings - should be updated by userspace */
2385 strncpy(manufacturer_string, MANUFACTURER_STRING, sizeof(manufacturer_string) - 1);
2386 strncpy(product_string, PRODUCT_STRING, sizeof(product_string) - 1);
2387 strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
2388
2389 id = usb_string_id(cdev);
2390 if (id < 0)
2391 return id;
2392 strings_dev[STRING_SERIAL_IDX].id = id;
2393 device_desc.iSerialNumber = id;
2394
2395 #ifdef CONFIG_USBIF_COMPLIANCE
2396 usb_gadget_clear_selfpowered(gadget);
2397 #else
2398 usb_gadget_set_selfpowered(gadget);
2399 #endif
2400 dev->cdev = cdev;
2401
2402 return 0;
2403 }
2404
2405 static int android_usb_unbind(struct usb_composite_dev *cdev)
2406 {
2407 struct android_dev *dev = _android_dev;
2408
2409 cancel_work_sync(&dev->work);
2410 android_cleanup_functions(dev->functions);
2411 return 0;
2412 }
2413
2414 /* HACK: android needs to override setup for accessory to work */
2415 static int (*composite_setup_func)(struct usb_gadget *gadget, const struct usb_ctrlrequest *c);
2416 extern void composite_setup_complete(struct usb_ep *ep, struct usb_request *req);
2417
2418 static int
2419 android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2420 {
2421 struct android_dev *dev = _android_dev;
2422 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2423 struct usb_request *req = cdev->req;
2424 struct android_usb_function *f;
2425 int value = -EOPNOTSUPP;
2426 unsigned long flags;
2427
2428 pr_debug("[XLOG_VERBOSE][USB]%s\n", __func__);
2429
2430 req->zero = 0;
2431 req->complete = composite_setup_complete;
2432 req->length = 0;
2433 gadget->ep0->driver_data = cdev;
2434
2435 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2436 if (f->ctrlrequest) {
2437 value = f->ctrlrequest(f, cdev, c);
2438 if (value >= 0)
2439 break;
2440 }
2441 }
2442
2443 /* Special case the accessory function.
2444 * It needs to handle control requests before it is enabled.
2445 */
2446 if (value < 0)
2447 value = acc_ctrlrequest(cdev, c);
2448
2449 if (value < 0)
2450 value = composite_setup_func(gadget, c);
2451
2452 spin_lock_irqsave(&cdev->lock, flags);
2453 if (!dev->connected) {
2454 dev->connected = 1;
2455 schedule_work(&dev->work);
2456 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2457 cdev->config) {
2458 schedule_work(&dev->work);
2459 }
2460 spin_unlock_irqrestore(&cdev->lock, flags);
2461
2462 return value;
2463 }
2464
2465 static void android_disconnect(struct usb_composite_dev *cdev)
2466 {
2467 struct android_dev *dev = _android_dev;
2468
2469 /* Added for USB Develpment debug, more log for more debuging help */
2470 pr_debug("[XLOG_VERBOSE][USB]%s: \n", __func__);
2471 /* Added for USB Develpment debug, more log for more debuging help */
2472
2473 /* accessory HID support can be active while the
2474 accessory function is not actually enabled,
2475 so we need to inform it when we are disconnected.
2476 */
2477 acc_disconnect();
2478
2479 dev->connected = 0;
2480 schedule_work(&dev->work);
2481
2482 /* Added for USB Develpment debug, more log for more debuging help */
2483 pr_debug("[XLOG_VERBOSE][USB]%s: dev->connected = %d \n", __func__, dev->connected);
2484 /* Added for USB Develpment debug, more log for more debuging help */
2485 }
2486
2487 static struct usb_composite_driver android_usb_driver = {
2488 .name = "android_usb",
2489 .dev = &device_desc,
2490 .strings = dev_strings,
2491 .bind = android_bind,
2492 .unbind = android_usb_unbind,
2493 .disconnect = android_disconnect,
2494 #ifdef CONFIG_USB_MU3D_DRV
2495 .max_speed = USB_SPEED_SUPER
2496 #else
2497 .max_speed = USB_SPEED_HIGH
2498 #endif
2499 };
2500
2501 static int android_create_device(struct android_dev *dev)
2502 {
2503 struct device_attribute **attrs = android_usb_attributes;
2504 struct device_attribute *attr;
2505 int err;
2506
2507 dev->dev = device_create(android_class, NULL,
2508 MKDEV(0, 0), NULL, "android0");
2509 if (IS_ERR(dev->dev))
2510 return PTR_ERR(dev->dev);
2511
2512 dev_set_drvdata(dev->dev, dev);
2513
2514 while ((attr = *attrs++)) {
2515 err = device_create_file(dev->dev, attr);
2516 if (err) {
2517 device_destroy(android_class, dev->dev->devt);
2518 return err;
2519 }
2520 }
2521 return 0;
2522 }
2523
2524 #ifdef CONFIG_USBIF_COMPLIANCE
2525
2526 #include <linux/proc_fs.h>
2527 #include <asm/uaccess.h>
2528 #include <linux/seq_file.h>
2529
2530
2531 static int andoid_usbif_driver_on = 0 ;
2532
2533
2534 static int android_start(void)
2535 {
2536 struct android_dev *dev;
2537 int err;
2538
2539 pr_debug("android_start ===> \n");
2540
2541 err = usb_composite_probe(&android_usb_driver);
2542 if (err) {
2543 pr_err("%s: failed to probe driver %d", __func__, err);
2544 }
2545
2546 /* HACK: exchange composite's setup with ours */
2547 composite_setup_func = android_usb_driver.gadget_driver.setup;
2548 android_usb_driver.gadget_driver.setup = android_setup;
2549
2550 pr_debug("android_start <=== \n");
2551
2552 return err;
2553 }
2554
2555 static int android_stop(void)
2556 {
2557 pr_debug("android_stop ===> \n");
2558
2559 usb_composite_unregister(&android_usb_driver);
2560
2561 pr_debug("android_stop <=== \n");
2562 }
2563
2564 static int andoid_usbif_proc_show(struct seq_file *seq, void *v)
2565 {
2566 seq_printf(seq, "andoid_usbif_proc_show, andoid_usbif_driver_on is %d (on:1, off:0)\n", andoid_usbif_driver_on);
2567 return 0;
2568 }
2569
2570 static int andoid_usbif_proc_open(struct inode *inode, struct file *file)
2571 {
2572 return single_open(file, andoid_usbif_proc_show, inode->i_private);
2573 }
2574
2575 static ssize_t andoid_usbif_proc_write(struct file *file, const char __user *buf, size_t length, loff_t *ppos)
2576 {
2577 int ret ;
2578 char msg[32] ;
2579 int result;
2580 int status;
2581 struct device *dev ;
2582 int irq ;
2583 struct resource *iomem;
2584 void __iomem *base;
2585 struct musb *musb ;
2586 void __iomem *ctrl_base;
2587
2588 if (length >= sizeof(msg)) {
2589 pr_debug("andoid_usbif_proc_write length error, the error len is %d\n", (unsigned int)length);
2590 return -EINVAL;
2591 }
2592 if (copy_from_user(msg, buf, length))
2593 return -EFAULT;
2594
2595 msg[length] = 0 ;
2596
2597 pr_debug("andoid_usbif_proc_write: %s, current driver on/off: %d\n", msg, andoid_usbif_driver_on);
2598
2599 if ((msg[0] == '1') && (andoid_usbif_driver_on == 0)){
2600 pr_debug("start usb android driver ===> \n");
2601 printk("start usb android driver ===> \n");
2602 android_start() ;
2603 andoid_usbif_driver_on = 1 ;
2604 pr_debug("start usb android driver <=== \n");
2605 }else if ((msg[0] == '0') && (andoid_usbif_driver_on == 1)){
2606 pr_debug("stop usb android driver ===> \n");
2607 printk("stop usb android driver ===> \n");
2608 andoid_usbif_driver_on = 0 ;
2609 android_stop() ;
2610
2611 pr_debug("stop usb android driver <=== \n");
2612 }
2613
2614 return length;
2615 }
2616
2617 static const struct file_operations andoid_usbif_proc_fops = {
2618 .owner = THIS_MODULE,
2619 .open = andoid_usbif_proc_open,
2620 .write = andoid_usbif_proc_write,
2621 .read = seq_read,
2622 .llseek = seq_lseek,
2623
2624 };
2625
2626 static int __init init(void)
2627 {
2628 struct android_dev *dev;
2629 int err;
2630 struct proc_dir_entry *prEntry;
2631
2632 android_class = class_create(THIS_MODULE, "android_usb");
2633 if (IS_ERR(android_class))
2634 return PTR_ERR(android_class);
2635
2636 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2637 if (!dev) {
2638 err = -ENOMEM;
2639 goto err_dev;
2640 }
2641
2642 dev->disable_depth = 1;
2643 dev->functions = supported_functions;
2644 INIT_LIST_HEAD(&dev->enabled_functions);
2645 INIT_WORK(&dev->work, android_work);
2646 mutex_init(&dev->mutex);
2647
2648 err = android_create_device(dev);
2649 if (err) {
2650 pr_err("%s: failed to create android device %d", __func__, err);
2651 goto err_create;
2652 }
2653
2654 _android_dev = dev;
2655
2656 prEntry = proc_create("android_usbif_init", 0666, NULL, &andoid_usbif_proc_fops);
2657
2658 if (prEntry)
2659 {
2660 printk("create the android_usbif_init proc OK!\n") ;
2661 }else{
2662 printk("[ERROR] create the android_usbif_init proc FAIL\n") ;
2663 }
2664
2665 // set android up at boot up
2666 android_start() ;
2667 andoid_usbif_driver_on = 1 ;
2668
2669 return 0;
2670
2671 err_create:
2672 kfree(dev);
2673 err_dev:
2674 class_destroy(android_class);
2675 return err;
2676 }
2677
2678 late_initcall(init);
2679
2680
2681
2682 static void __exit cleanup(void)
2683 {
2684 printk("[U3D] android cleanup ===> \n") ;
2685 class_destroy(android_class);
2686 kfree(_android_dev);
2687 _android_dev = NULL;
2688 printk("[U3D] android cleanup <=== \n") ;
2689 }
2690 module_exit(cleanup);
2691
2692 #else
2693 static int __init init(void)
2694 {
2695 struct android_dev *dev;
2696 int err;
2697
2698 android_class = class_create(THIS_MODULE, "android_usb");
2699 if (IS_ERR(android_class))
2700 return PTR_ERR(android_class);
2701
2702 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2703 if (!dev) {
2704 err = -ENOMEM;
2705 goto err_dev;
2706 }
2707
2708 dev->disable_depth = 1;
2709 dev->functions = supported_functions;
2710 INIT_LIST_HEAD(&dev->enabled_functions);
2711 INIT_WORK(&dev->work, android_work);
2712 mutex_init(&dev->mutex);
2713
2714 err = android_create_device(dev);
2715 if (err) {
2716 pr_err("%s: failed to create android device %d", __func__, err);
2717 goto err_create;
2718 }
2719
2720 _android_dev = dev;
2721
2722 err = usb_composite_probe(&android_usb_driver);
2723 if (err) {
2724 pr_err("%s: failed to probe driver %d", __func__, err);
2725 goto err_probe;
2726 }
2727
2728 /* HACK: exchange composite's setup with ours */
2729 composite_setup_func = android_usb_driver.gadget_driver.setup;
2730 android_usb_driver.gadget_driver.setup = android_setup;
2731
2732 return 0;
2733
2734 err_probe:
2735 device_destroy(android_class, dev->dev->devt);
2736 err_create:
2737 kfree(dev);
2738 err_dev:
2739 class_destroy(android_class);
2740 return err;
2741 }
2742 late_initcall(init);
2743
2744 static void __exit cleanup(void)
2745 {
2746 usb_composite_unregister(&android_usb_driver);
2747 class_destroy(android_class);
2748 kfree(_android_dev);
2749 _android_dev = NULL;
2750 }
2751 module_exit(cleanup);
2752 #endif