blob: 9094fd31743812c6be26fbc739bb6cc43c3bb759 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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>
26
27#include <linux/usb/ch9.h>
28#include <linux/usb/composite.h>
29#include <linux/usb/gadget.h>
30#include <linux/android_notify.h>
31#include <mach/highspeed_debug.h>
32
33#include "gadget_chips.h"
34#include <mach/iomap.h>
35
36/*
37 * Kbuild is not very cooperative with respect to linking separately
38 * compiled library objects into one module. So for now we won't use
39 * separate compilation ... ensuring init/exit sections work to shrink
40 * the runtime footprint, and giving us at least some parts of what
41 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
42 */
43#include "usbstring.c"
44#include "config.c"
45#include "epautoconf.c"
46#include "composite.c"
47
48//#include "f_fs.c"
49//#include "f_audio_source.c"
50#include "f_mass_storage.c"
51#include "u_serial.c"
52#include "f_acm.c"
53#include "f_serial.c"
54#include "f_adb.c"
55//#include "f_mtp.c"
56//#include "f_accessory.c"
57#define USB_ETH_RNDIS y
58#include "f_ecm.c"
59#include "f_rndis.c"
60#include "rndis.c"
61#include "f_mbim.c"
62#include "u_ether.c"
63#include "u_diag.c"
64#ifdef _USE_MBIM//CONFIG_USB_F_DIAG_ACM
65#include "f_diag_acm.c"
66#else
67#include "f_diag.c"
68#endif
69
70MODULE_AUTHOR("Mike Lockwood");
71MODULE_DESCRIPTION("Android Composite USB Driver");
72MODULE_LICENSE("GPL");
73MODULE_VERSION("1.0");
74
75#ifndef CONFIG_SYSTEM_RECOVERY
76int zDrvNand_WriteBootflag( int flag );
77#endif
78
79#define pr_err USB_DEBUG
80
81
82static const char longname[] = "Gadget Android";
83
84/* Default vendor and product IDs, overridden by userspace */
85#define VENDOR_ID 0x19D2
86#define PRODUCT_ID 0x0193
87
88/* USB net card name */
89#define USB_VNIC_NAME "usblan"
90
91/* USB NET CARD MAX NUM */
92#define MAX_ECM_INSTANCES 3
93
94
95#define CFG_LUN_NUM_TWO 1
96
97#define MBIM_PACKET_MAX_NUM 10
98#define RNDIS_PACKET_MAX_NUM 10
99
100
101#define OS_string_descriptor_id 0xEE
102#define bMS_Code_Original 0x04
103#define bMS_Code_Change 0x08
104struct android_usb_function {
105 char *name;
106 void *config;
107
108 struct device *dev;
109 struct usb_config_descriptor *cof;
110 char *dev_name;
111 struct device_attribute **attributes;
112
113 /* for android_dev.enabled_functions */
114 struct list_head enabled_list;
115
116 /* Optional: initialization during gadget bind */
117 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
118 /* Optional: cleanup during gadget unbind */
119 void (*cleanup)(struct android_usb_function *);
120 /* Optional: called when the function is added the list of
121 * enabled functions */
122 void (*enable)(struct android_usb_function *);
123 /* Optional: called when it is removed */
124 void (*disable)(struct android_usb_function *);
125
126 int (*bind_config)(struct android_usb_function *,
127 struct usb_configuration *);
128
129 /* Optional: called when the configuration is removed */
130 void (*unbind_config)(struct android_usb_function *,
131 struct usb_configuration *);
132 /* Optional: handle ctrl requests before the device is configured */
133 int (*ctrlrequest)(struct android_usb_function *,
134 struct usb_composite_dev *,
135 const struct usb_ctrlrequest *);
136};
137
138struct android_dev {
139 struct android_usb_function **functions;
140 struct list_head enabled_functions;
141 struct usb_composite_dev *cdev;
142 struct device *dev;
143
144 bool cdrom_only;
145 bool enabled;
146 int disable_depth;
147 //int bRequest;
148 //int wValue;
149 struct mutex mutex;
150 bool connected;
151 bool sw_connected;
152 struct work_struct work;
153 struct work_struct usbmode;
154 char ffs_aliases[256];
155
156 struct usb_ctrlrequest vendor_req;
157};
158
159static struct class *android_class;
160static struct android_dev *_android_dev;
161static int multi_packet_num = 10;
162static int ether_skb_num = 32;
163
164static int android_bind_config(struct usb_configuration *c);
165static void android_unbind_config(struct usb_configuration *c);
166void usb_mods_init(void);
167void usb_mods_exit(void);
168void usb_mods_activate(void);
169void usb_set_ms_auto_reject(int flag);
170extern void dwc_otg_clk_enable(int isOn);
171
172typedef enum usb_enum_mode_type{
173 USB_ENUM_MODE_DEBUG = 0,
174 USB_ENUM_MODE_USER,
175 USB_ENUM_MODE_FACTORY,
176 USB_ENUM_MODE_AMT,
177 USB_ENUM_MODE_EYE_DIAGRAM,
178 USB_ENUM_MODE_MAX,
179}enum_mode_type;
180
181enum_mode_type usb_cur_enum_mode = USB_ENUM_MODE_DEBUG;
182
183
184
185/* string IDs are assigned dynamically */
186#define STRING_MANUFACTURER_IDX 0
187#define STRING_PRODUCT_IDX 1
188#define STRING_SERIAL_IDX 2
189#define STRING_CONFIGURATION_IDX 3
190
191static char manufacturer_string[256];
192static char product_string[256];
193static char serial_string[256];
194static char configuration_string[256];
195
196/* String Table */
197static struct usb_string strings_dev[] = {
198 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
199 [STRING_PRODUCT_IDX].s = product_string,
200 [STRING_SERIAL_IDX].s = serial_string,
201 [STRING_CONFIGURATION_IDX].s = configuration_string,
202 { } /* end of list */
203};
204
205static struct usb_gadget_strings stringtab_dev = {
206 .language = 0x0409, /* en-us */
207 .strings = strings_dev,
208};
209
210static struct usb_gadget_strings *dev_strings[] = {
211 &stringtab_dev,
212 NULL,
213};
214
215static struct usb_device_descriptor device_desc = {
216 .bLength = sizeof(device_desc),
217 .bDescriptorType = USB_DT_DEVICE,
218 .bcdUSB = __constant_cpu_to_le16(0x0200),
219 .bDeviceClass = USB_CLASS_PER_INTERFACE,
220 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
221 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
222 .bcdDevice = __constant_cpu_to_le16(0xffff),
223 .bNumConfigurations = 1,
224};
225
226static struct usb_configuration android_config_driver = {
227 .label = "android",
228 .unbind = android_unbind_config,
229 .bConfigurationValue = 1,
230 .iConfiguration = 1,
231#if 0
232 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | USB_CONFIG_ATT_WAKEUP,
233#else
234 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
235#endif
236 .bMaxPower = 0xFA, /* 500ma */
237};
238
239int get_usb_enum_mode(void)
240{
241 return usb_cur_enum_mode;
242}
243EXPORT_SYMBOL_GPL(get_usb_enum_mode);
244
245static void usb_mode_work(struct work_struct *data)
246{
247 struct android_dev *dev = container_of(data, struct android_dev, usbmode);
248
249 switch(dev->vendor_req.bRequest)
250 {
251 case USB_SWITCH_to_DEBUG:
252 if(usb_cur_enum_mode == USB_ENUM_MODE_DEBUG){
253 printk("usb_mode_work already debug mode\n");
254 break;
255 }
256 if(dev->vendor_req.wValue == 0x0002)
257 {
258#ifndef CONFIG_SYSTEM_RECOVERY
259 zDrvNand_WriteBootflag(1);
260#endif
261 usb_notify_up(USB_SWITCH_DEBUG,NULL);
262 }
263 break;
264
265 case USB_SWITCH_to_DEBUG_AT:
266 if(dev->vendor_req.wValue == 0x0101)
267 {
268#ifndef CONFIG_SYSTEM_RECOVERY
269 zDrvNand_WriteBootflag(0);
270#endif
271 usb_notify_up(USB_SWITCH_DEBUG,NULL);
272 }
273 break;
274
275 case USB_SWITCH_to_USER:
276 if(usb_cur_enum_mode == USB_ENUM_MODE_USER){
277 printk("usb_mode_work already user mode\n");
278 break;
279 }
280#ifndef CONFIG_SYSTEM_RECOVERY
281 zDrvNand_WriteBootflag(1);
282#endif
283 usb_notify_up(USB_SWITCH_USER, NULL);
284 break;
285
286 case USB_SWITCH_to_FACTORY:
287 usb_notify_up(USB_SWITCH_FACTORY, NULL);
288 break;
289
290 case USB_SWITCH_to_AMT:
291 usb_notify_up(USB_SWITCH_AMT, NULL);
292 break;
293 case USB_SWITCH_to_EYE_DIAGRAM:
294 usb_notify_up(USB_SWITCH_EYE_DIAGRAM, NULL);
295 break;
296 }
297}
298
299int get_vnic_multi_packet_num(void)
300{
301 return multi_packet_num;
302}
303
304unsigned int gether_ether_skb_num(void)
305{
306 return ether_skb_num;
307}
308
309static void android_work(struct work_struct *data)
310{
311 struct android_dev *dev = container_of(data, struct android_dev, work);
312 struct usb_composite_dev *cdev = dev->cdev;
313 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
314 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
315 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
316 char **uevent_envp = NULL;
317 unsigned long flags;
318 USBSTACK_DBG("%s, %u", __func__, __LINE__);
319 spin_lock_irqsave(&cdev->lock, flags);
320 if (cdev->config)
321 uevent_envp = configured;
322 else if (dev->connected != dev->sw_connected)
323 uevent_envp = dev->connected ? connected : disconnected;
324 dev->sw_connected = dev->connected;
325 spin_unlock_irqrestore(&cdev->lock, flags);
326
327 if (uevent_envp) {
328 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
329 //pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
330 } else {
331 //pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
332 // dev->connected, dev->sw_connected, cdev->config);
333 }
334}
335
336static void android_enable(struct android_dev *dev)
337{
338 int ret;
339 struct usb_composite_dev *cdev = dev->cdev;
340
341 //USB_DEBUG("disable_depth:%d", dev->disable_depth);
342 USBSTACK_DBG("%s, disable_depth:%d", __func__, dev->disable_depth);
343 if (WARN_ON(!dev->disable_depth))
344 return;
345 printk("----android_enable, bmattr:%x\n", android_config_driver.bmAttributes);
346 if (--dev->disable_depth == 0) {
347 ret =usb_add_config(cdev, &android_config_driver,
348 android_bind_config);
349 if(ret)
350 USBSTACK_DBG("usb_add_config, ret:%d", ret);
351 usb_gadget_connect(cdev->gadget);
352 }
353}
354
355static void android_disable(struct android_dev *dev)
356{
357 struct usb_composite_dev *cdev = dev->cdev;
358 USBSTACK_DBG("%s, disable_depth:%d", __func__, dev->disable_depth);
359 if (dev->disable_depth++ == 0) {
360 usb_gadget_disconnect(cdev->gadget);
361 /* Cancel pending control requests */
362 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
363 usb_remove_config(cdev, &android_config_driver);
364 }
365}
366
367/*-------------------------------------------------------------------------*/
368/* Supported functions initialization */
369
370#if 0
371struct functionfs_config {
372 bool opened;
373 bool enabled;
374 struct ffs_data *data;
375};
376
377static int ffs_function_init(struct android_usb_function *f,
378 struct usb_composite_dev *cdev)
379{
380 f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL);
381 if (!f->config)
382 return -ENOMEM;
383
384 return functionfs_init();
385}
386
387static void ffs_function_cleanup(struct android_usb_function *f)
388{
389 functionfs_cleanup();
390 kfree(f->config);
391}
392
393static void ffs_function_enable(struct android_usb_function *f)
394{
395 struct android_dev *dev = _android_dev;
396 struct functionfs_config *config = f->config;
397
398 config->enabled = true;
399
400 /* Disable the gadget until the function is ready */
401 if (!config->opened)
402 android_disable(dev);
403}
404
405static void ffs_function_disable(struct android_usb_function *f)
406{
407 struct android_dev *dev = _android_dev;
408 struct functionfs_config *config = f->config;
409
410 config->enabled = false;
411
412 /* Balance the disable that was called in closed_callback */
413 if (!config->opened)
414 android_enable(dev);
415}
416
417static int ffs_function_bind_config(struct android_usb_function *f,
418 struct usb_configuration *c)
419{
420 struct functionfs_config *config = f->config;
421 return functionfs_bind_config(c->cdev, c, config->data);
422}
423
424static ssize_t
425ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf)
426{
427 struct android_dev *dev = _android_dev;
428 int ret;
429
430 mutex_lock(&dev->mutex);
431 ret = sprintf(buf, "%s\n", dev->ffs_aliases);
432 mutex_unlock(&dev->mutex);
433
434 return ret;
435}
436
437static ssize_t
438ffs_aliases_store(struct device *pdev, struct device_attribute *attr,
439 const char *buf, size_t size)
440{
441 struct android_dev *dev = _android_dev;
442 char buff[256];
443
444 mutex_lock(&dev->mutex);
445
446 if (dev->enabled) {
447 mutex_unlock(&dev->mutex);
448 return -EBUSY;
449 }
450
451 strlcpy(buff, buf, sizeof(buff));
452 strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases));
453
454 mutex_unlock(&dev->mutex);
455
456 return size;
457}
458
459static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show,
460 ffs_aliases_store);
461static struct device_attribute *ffs_function_attributes[] = {
462 &dev_attr_aliases,
463 NULL
464};
465
466static struct android_usb_function ffs_function = {
467 .name = "ffs",
468 .init = ffs_function_init,
469 .enable = ffs_function_enable,
470 .disable = ffs_function_disable,
471 .cleanup = ffs_function_cleanup,
472 .bind_config = ffs_function_bind_config,
473 .attributes = ffs_function_attributes,
474};
475
476static int functionfs_ready_callback(struct ffs_data *ffs)
477{
478 struct android_dev *dev = _android_dev;
479 struct functionfs_config *config = ffs_function.config;
480 int ret = 0;
481
482 mutex_lock(&dev->mutex);
483
484 ret = functionfs_bind(ffs, dev->cdev);
485 if (ret)
486 goto err;
487
488 config->data = ffs;
489 config->opened = true;
490
491 if (config->enabled)
492 android_enable(dev);
493
494err:
495 mutex_unlock(&dev->mutex);
496 return ret;
497}
498
499static void functionfs_closed_callback(struct ffs_data *ffs)
500{
501 struct android_dev *dev = _android_dev;
502 struct functionfs_config *config = ffs_function.config;
503
504 mutex_lock(&dev->mutex);
505
506 if (config->enabled)
507 android_disable(dev);
508
509 config->opened = false;
510 config->data = NULL;
511
512 functionfs_unbind(ffs);
513
514 mutex_unlock(&dev->mutex);
515}
516
517static int functionfs_check_dev_callback(const char *dev_name)
518{
519 return 0;
520}
521#endif
522
523#define ECM_TIMEOUT 6000
524
525struct ecm_function_config {
526 u8 ethaddr[MAX_ECM_INSTANCES][ETH_ALEN];;
527 int instances;
528 int receive_setIfac;
529 int receive_setICfg;
530 struct delayed_work work;
531};
532
533extern void dwc_otg_wakelock(int lock_flag,int phase);
534static void ecm_function_work(struct work_struct *data)
535{
536 USBSTACK_DBG("ecm work");
537 dwc_otg_wakelock(1,0);
538 usb_notify_up(USB_DEVICE_PLUGOUT, NULL);
539 usb_notify_up(USB_DEVICE_PLUGIN, NULL);
540 dwc_otg_wakelock(0,0);
541}
542static int ecm_function_ctrlrequest(struct android_usb_function * f,
543 struct usb_composite_dev *dev,
544 const struct usb_ctrlrequest * ctrl)
545{
546 int value = -1;
547 struct ecm_function_config *ecm = f->config;
548#if 1
549 switch (ctrl->bRequestType & USB_TYPE_MASK){
550 case USB_TYPE_STANDARD:
551 if(ctrl->bRequest == USB_REQ_SET_CONFIGURATION){
552 if((!ecm->receive_setIfac)&&(!ecm->receive_setICfg)){
553 printk("ecm-receive_setICfg\n");
554 //schedule_delayed_work(&ecm->work, msecs_to_jiffies(ECM_TIMEOUT));
555 }
556 if((ecm->receive_setIfac)&&(!ecm->receive_setICfg))
557 gether_ecm_uevent(ecm->instances, 1);
558 ecm->receive_setICfg = 1;
559 }else if(ctrl->bRequest == USB_REQ_SET_INTERFACE){
560 u8 intf = (le16_to_cpu(ctrl->wIndex)) & 0xFF;
561 struct usb_function *f_intf = dev->config->interface[intf];
562 struct f_ecm *f_ecm = func_to_ecm(f_intf);
563 printk("ecm-receive_setinterface\n");
564 if (intf != f_ecm->data_id)
565 return value;
566 if(ecm->receive_setICfg){
567 //USBSTACK_DBG("ecm-delayed-work cancel");
568 //cancel_delayed_work_sync(&ecm->work);
569 gether_ecm_uevent(ecm->instances, 1);
570 }
571 ecm->receive_setIfac = 1;
572 }
573 break;
574 case USB_TYPE_VENDOR:
575 if((ctrl->bRequest == bMS_Code_Original )||
576 (ctrl->bRequest == bMS_Code_Change )){
577 printk("ecm-sys-id-err");
578 schedule_delayed_work(&ecm->work, msecs_to_jiffies(50));
579 }
580 break;
581 }
582#endif
583 return value;
584}
585
586static int
587ecm_function_init(struct android_usb_function *f,
588 struct usb_composite_dev *cdev)
589{
590 struct ecm_function_config *ecm;
591
592 USBSTACK_DBG("%s", __func__);
593
594 ecm = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
595
596 if (!ecm)
597 return -ENOMEM;
598
599 ecm->instances = 1;
600 INIT_DELAYED_WORK(&ecm->work, ecm_function_work);
601 f->config = ecm;
602
603 return 0;
604}
605
606static void ecm_function_cleanup(struct android_usb_function *f)
607{
608 USBSTACK_DBG("%s", __func__);
609 kfree(f->config);
610 f->config = NULL;
611}
612
613static int
614ecm_function_bind_config(struct android_usb_function *f,
615 struct usb_configuration *c)
616{
617 int ret, i;
618 ret =0;
619 struct ecm_function_config *ecm = f->config;
620
621 if (!ecm) {
622 pr_err("%s: ecm_pdata\n", __func__);
623 return -1;
624 }
625 USBSTACK_DBG("%s, instances:%d", __func__, ecm->instances);
626 ecm->receive_setIfac = 0;
627 ecm->receive_setICfg = 0;
628
629 for (i = 0; ((i < ecm->instances) && (i< MAX_ECM_INSTANCES)); i++) {
630 ret = gether_setup_name_num(c->cdev->gadget,
631 ecm->ethaddr[i], USB_VNIC_NAME, i);
632 if (ret) {
633 pr_err("%s: gether_setup failed\n", __func__);
634 return ret;
635 }
636
637 ret = ecm_bind_config_num(c, ecm->ethaddr[i], i);
638 if (ret) {
639 pr_err("Could not bind ecm%u config\n", i);
640 break;
641 }
642 }
643
644 return ret;
645}
646
647static void ecm_function_unbind_config(struct android_usb_function *f,
648 struct usb_configuration *c)
649{
650 int i;
651 struct ecm_function_config *ecm = f->config;
652
653 USBSTACK_DBG("%s", __func__);
654 cancel_delayed_work_sync(&ecm->work);
655
656 for(i=0; i<ecm->instances; i++){
657 gether_cleanup_num(i);
658 }
659}
660
661static ssize_t ecm_ethaddr_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
663{
664 struct android_usb_function *f = dev_get_drvdata(dev);
665 if(NULL == f)
666 {
667 return -EINVAL;
668 }
669 struct ecm_function_config *ecm = f->config;
670 if(NULL == ecm)
671 {
672 return -EINVAL;
673 }
674 USBSTACK_DBG("%s", __func__);
675 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
676 ecm->ethaddr[0][0], ecm->ethaddr[0][1], ecm->ethaddr[0][2],
677 ecm->ethaddr[0][3], ecm->ethaddr[0][4], ecm->ethaddr[0][5]);
678}
679
680static ssize_t ecm_ethaddr_store(struct device *dev,
681 struct device_attribute *attr, const char *buf, size_t size)
682{
683 struct android_usb_function *f = dev_get_drvdata(dev);
684 if(NULL == f)
685 {
686 return -EINVAL;
687 }
688 struct ecm_function_config *ecm = f->config;
689 if(NULL == ecm)
690 {
691 return -EINVAL;
692 }
693 USBSTACK_DBG("%s", __func__);
694 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
695 (unsigned int *)&ecm->ethaddr[0][0], (unsigned int *)&ecm->ethaddr[0][1],
696 (unsigned int *)&ecm->ethaddr[0][2], (unsigned int *)&ecm->ethaddr[0][3],
697 (unsigned int *)&ecm->ethaddr[0][4], (unsigned int *)&ecm->ethaddr[0][5]) == 6)
698 return size;
699 return -EINVAL;
700}
701
702static DEVICE_ATTR(ethaddr_ecm, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
703 ecm_ethaddr_store);
704
705static ssize_t ecm_instances_show(struct device *dev,
706 struct device_attribute *attr, char *buf)
707{
708 struct android_usb_function *f = dev_get_drvdata(dev);
709 if(NULL == f)
710 {
711 return -EINVAL;
712 }
713 struct ecm_function_config *config = f->config;
714 if(NULL == config)
715 {
716 return -EINVAL;
717 }
718 USBSTACK_DBG("%s", __func__);
719 return sprintf(buf, "%d\n", config->instances);
720}
721
722static ssize_t ecm_instances_store(struct device *dev,
723 struct device_attribute *attr, const char *buf, size_t size)
724{
725 struct android_usb_function *f = dev_get_drvdata(dev);
726 if(NULL == f)
727 {
728 return -EINVAL;
729 }
730 struct ecm_function_config *config = f->config;
731 if(NULL == config)
732 {
733 return -EINVAL;
734 }
735 int value;
736 USBSTACK_DBG("%s", __func__);
737 sscanf(buf, "%d", &value);
738 if ((value > MAX_ECM_INSTANCES)||(value < 1)){
739 USB_DEBUG("WARNNING:SET INVALID ECM NUM:%d!!!", value);
740 value = 1;
741 }
742 config->instances = value;
743 return size;
744}
745
746static DEVICE_ATTR(ecm_instances, S_IRUGO | S_IWUSR, ecm_instances_show,
747 ecm_instances_store);
748
749static struct device_attribute *ecm_function_attributes[] = {
750 &dev_attr_ethaddr_ecm,
751 &dev_attr_ecm_instances,
752 NULL
753};
754
755
756static struct android_usb_function ecm_function = {
757 .name = "ecm",
758 .init = ecm_function_init,
759 .cleanup = ecm_function_cleanup,
760 .bind_config = ecm_function_bind_config,
761 .unbind_config = ecm_function_unbind_config,
762 .attributes = ecm_function_attributes,
763 .ctrlrequest = ecm_function_ctrlrequest,
764};
765
766
767#define MAX_DIAG_INSTANCES 1
768struct diag_function_config {
769 int instances;
770};
771
772static int
773diag_function_init(struct android_usb_function *f,
774 struct usb_composite_dev *cdev)
775{
776 USB_DEBUG("DIAG INIT");
777 USBSTACK_DBG("%s", __func__);
778 f->config = kzalloc(sizeof(struct diag_function_config), GFP_KERNEL);
779 if (!f->config)
780 return -ENOMEM;
781 ((struct diag_function_config *)(f->config))->instances = 1;
782 return diag_setup(cdev->gadget, MAX_DIAG_INSTANCES);
783}
784
785
786static void diag_function_cleanup(struct android_usb_function *f)
787{
788 USBSTACK_DBG("%s", __func__);
789 diag_cleanup();
790 kfree(f->config);
791 f->config = NULL;
792}
793
794static int
795diag_function_bind_config(struct android_usb_function *f,
796 struct usb_configuration *c)
797{
798 int i;
799 int ret = 0;
800 struct diag_function_config *config = f->config;
801 USB_DEBUG("diag config, instances:%d",config->instances);
802 USBSTACK_DBG("%s", __func__);
803 for (i = 0; i < config->instances; i++) {
804 //ret = acm_bind_config(c, i);
805 USB_DEBUG("instance :%d", i);
806
807#ifdef _USE_MBIM//CONFIG_USB_F_DIAG_ACM
808 ret = diag_acm_bind_config(c, i);
809#else
810 ret = diag_bind_config(c, i);
811#endif
812 USB_DEBUG("ret:%d",ret);
813 if (ret) {
814 pr_err("Could not bind diag%u config\n", i);
815 break;
816 }
817 }
818
819 return ret;
820}
821
822static struct android_usb_function diag_function = {
823 .name = "diag",
824 //.enable = adb_android_function_enable,
825 //.disable = adb_android_function_disable,
826 .init = diag_function_init,
827 .cleanup = diag_function_cleanup,
828 .bind_config = diag_function_bind_config,
829};
830
831struct adb_data {
832 bool opened;
833 bool enabled;
834};
835
836static int
837adb_function_init(struct android_usb_function *f,
838 struct usb_composite_dev *cdev)
839{
840 USBSTACK_DBG("%s", __func__);
841 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
842 if (!f->config)
843 return -ENOMEM;
844
845 return adb_setup();
846}
847
848static void adb_function_cleanup(struct android_usb_function *f)
849{
850 USBSTACK_DBG("%s", __func__);
851 adb_cleanup();
852 kfree(f->config);
853}
854
855static int
856adb_function_bind_config(struct android_usb_function *f,
857 struct usb_configuration *c)
858{
859 USBSTACK_DBG("%s", __func__);
860 return adb_bind_config(c);
861}
862
863static void adb_android_function_enable(struct android_usb_function *f)
864{
865 struct android_dev *dev = _android_dev;
866 struct adb_data *data = f->config;
867 USBSTACK_DBG("%s, open:%d", __func__, data->opened);
868 data->enabled = true;
869
870 /* Disable the gadget until adbd is ready */
871 if (!data->opened)
872 android_disable(dev);
873}
874
875static void adb_android_function_disable(struct android_usb_function *f)
876{
877 struct android_dev *dev = _android_dev;
878 struct adb_data *data = f->config;
879 USBSTACK_DBG("%s data opend:%d", __func__, data->opened);
880 data->enabled = false;
881
882 /* Balance the disable that was called in closed_callback */
883 if (!data->opened)
884 android_enable(dev);
885}
886
887static struct android_usb_function adb_function = {
888 .name = "adb",
889 //.enable = adb_android_function_enable,
890 //.disable = adb_android_function_disable,
891 .init = adb_function_init,
892 .cleanup = adb_function_cleanup,
893 .bind_config = adb_function_bind_config,
894};
895
896static void adb_ready_callback(void)
897{
898 struct android_dev *dev = _android_dev;
899 struct adb_data *data = adb_function.config;
900 USBSTACK_DBG("%s, data enable:%d", __func__, data->enabled);
901 mutex_lock(&dev->mutex);
902
903 data->opened = true;
904
905 if (data->enabled){
906 usb_gadget_set_selfpowered(dev->cdev->gadget);
907 android_enable(dev);
908 }
909
910 mutex_unlock(&dev->mutex);
911 USBSTACK_DBG("%s,%u", __func__, __LINE__);
912}
913
914static void adb_closed_callback(void)
915{
916 struct android_dev *dev = _android_dev;
917 struct adb_data *data = adb_function.config;
918 USBSTACK_DBG("%s data enabled:%d", __func__, data->enabled);
919 mutex_lock(&dev->mutex);
920
921 data->opened = false;
922
923 if (data->enabled){
924 dwc_otg_clk_enable(1);
925 android_disable(dev);
926 usb_gadget_clear_selfpowered(dev->cdev->gadget);
927 }
928
929 mutex_unlock(&dev->mutex);
930 USBSTACK_DBG("%s %u", __func__, __LINE__);
931}
932
933
934#define MAX_ACM_INSTANCES 3
935#define MAX_SERIAL_INSTANCES 4
936struct acm_function_config {
937 int instances;
938};
939
940static int
941acm_function_init(struct android_usb_function *f,
942 struct usb_composite_dev *cdev)
943{
944 USB_DEBUG("ACM INIT");
945 USBSTACK_DBG("%s", __func__);
946 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
947 if (!f->config)
948 return -ENOMEM;
949 ((struct acm_function_config *)(f->config))->instances =1;
950 return gserial_setup(cdev->gadget, MAX_SERIAL_INSTANCES);
951}
952
953static void acm_function_cleanup(struct android_usb_function *f)
954{
955 USBSTACK_DBG("%s", __func__);
956 gserial_cleanup();
957 kfree(f->config);
958 f->config = NULL;
959}
960
961static int
962acm_function_bind_config(struct android_usb_function *f,
963 struct usb_configuration *c)
964{
965 int i;
966 int ret = 0;
967 struct acm_function_config *config = f->config;
968 USB_DEBUG("acm config, instances:%d",config->instances);
969 USBSTACK_DBG("%s", __func__);
970 for (i = 0; i < config->instances; i++) {
971 USB_DEBUG("instance :%d", i);
972 ret = acm_bind_config(c, i);
973 //ret = gser_bind_config(c, i);
974 USB_DEBUG("ret:%d",ret);
975 if (ret) {
976 pr_err("Could not bind acm%u config\n", i);
977 break;
978 }
979 }
980
981 return ret;
982}
983
984static int
985serial_function_bind_config(struct android_usb_function *f,
986 struct usb_configuration *c)
987{
988 int i;
989 int ret = 0;
990 struct acm_function_config *config = f->config;
991 USB_DEBUG("acm config, instances:%d",config->instances);
992 USBSTACK_DBG("%s", __func__);
993 for (i = 0; i < config->instances; i++) {
994 USB_DEBUG("instance :%d", i);
995 ret = gser_bind_config(c, i);
996 USB_DEBUG("ret:%d",ret);
997 if (ret) {
998 pr_err("Could not bind acm%u config\n", i);
999 break;
1000 }
1001 }
1002
1003 return ret;
1004}
1005
1006static ssize_t acm_instances_show(struct device *dev,
1007 struct device_attribute *attr, char *buf)
1008{
1009 struct android_usb_function *f = dev_get_drvdata(dev);
1010 if(NULL == f)
1011 {
1012 return -EINVAL;
1013 }
1014 struct acm_function_config *config = f->config;
1015 if(NULL == config)
1016 {
1017 return -EINVAL;
1018 }
1019
1020 USBSTACK_DBG("%s", __func__);
1021 return sprintf(buf, "%d\n", config->instances);
1022}
1023
1024static ssize_t acm_instances_store(struct device *dev,
1025 struct device_attribute *attr, const char *buf, size_t size)
1026{
1027 struct android_usb_function *f = dev_get_drvdata(dev);
1028 if(NULL == f)
1029 {
1030 return -EINVAL;
1031 }
1032 struct acm_function_config *config = f->config;
1033 if(NULL == config)
1034 {
1035 return -EINVAL;
1036 }
1037 int value;
1038 USBSTACK_DBG("%s", __func__);
1039 sscanf(buf, "%d", &value);
1040 if ((value > MAX_ACM_INSTANCES)||(value<1)){
1041 USB_DEBUG("WARNNING:SET INVALID ACM NUM:%d!!!", value);
1042 value = MAX_ACM_INSTANCES;
1043 }
1044 config->instances = value;
1045 return size;
1046}
1047
1048static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
1049 acm_instances_store);
1050static struct device_attribute *acm_function_attributes[] = {
1051 &dev_attr_instances,
1052 NULL
1053};
1054
1055static struct android_usb_function acm_function = {
1056 .name = "acm",
1057 .init = acm_function_init,
1058 .cleanup = acm_function_cleanup,
1059 .bind_config = acm_function_bind_config,
1060 .attributes = acm_function_attributes,
1061};
1062
1063
1064static int
1065serial_function_init(struct android_usb_function *f,
1066 struct usb_composite_dev *cdev)
1067{
1068 USB_DEBUG("ACM INIT");
1069 USBSTACK_DBG("%s", __func__);
1070 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
1071 if (!f->config)
1072 return -ENOMEM;
1073 ((struct acm_function_config *)(f->config))->instances =1;
1074 return gserial_setup(cdev->gadget, MAX_SERIAL_INSTANCES);
1075}
1076
1077
1078static ssize_t serial_instances_show(struct device *dev,
1079 struct device_attribute *attr, char *buf)
1080{
1081 struct android_usb_function *f = dev_get_drvdata(dev);
1082 if(NULL == f)
1083 {
1084 return -EINVAL;
1085 }
1086 struct acm_function_config *config = f->config;
1087 if(NULL == config)
1088 {
1089 return -EINVAL;
1090 }
1091 USBSTACK_DBG("%s", __func__);
1092 return sprintf(buf, "%d\n", config->instances);
1093}
1094
1095static ssize_t serial_instances_store(struct device *dev,
1096 struct device_attribute *attr, const char *buf, size_t size)
1097{
1098 struct android_usb_function *f = dev_get_drvdata(dev);
1099 if(NULL == f)
1100 {
1101 return -EINVAL;
1102 }
1103 struct acm_function_config *config = f->config;
1104 if(NULL == config)
1105 {
1106 return -EINVAL;
1107 }
1108 int value;
1109 USBSTACK_DBG("%s", __func__);
1110 sscanf(buf, "%d", &value);
1111 if ((value > MAX_ACM_INSTANCES)||(value<1)){
1112 USB_DEBUG("WARNNING:SET INVALID ACM NUM:%d!!!", value);
1113 value = MAX_ACM_INSTANCES;
1114 }
1115 config->instances = value;
1116 return size;
1117}
1118
1119static DEVICE_ATTR(instances_serial, S_IRUGO | S_IWUSR, serial_instances_show,
1120 serial_instances_store);
1121static struct device_attribute *serial_function_attributes[] = {
1122 &dev_attr_instances_serial,
1123 NULL
1124};
1125
1126
1127static struct android_usb_function serial_function = {
1128 .name = "serial",
1129 .init = serial_function_init,
1130 .cleanup = acm_function_cleanup,
1131 .bind_config = serial_function_bind_config,
1132 .attributes = serial_function_attributes,
1133};
1134
1135
1136#if 0
1137static int
1138mtp_function_init(struct android_usb_function *f,
1139 struct usb_composite_dev *cdev)
1140{
1141 return mtp_setup();
1142}
1143
1144static void mtp_function_cleanup(struct android_usb_function *f)
1145{
1146 mtp_cleanup();
1147}
1148
1149static int
1150mtp_function_bind_config(struct android_usb_function *f,
1151 struct usb_configuration *c)
1152{
1153 return mtp_bind_config(c, false);
1154}
1155
1156static int
1157ptp_function_init(struct android_usb_function *f,
1158 struct usb_composite_dev *cdev)
1159{
1160 /* nothing to do - initialization is handled by mtp_function_init */
1161 return 0;
1162}
1163
1164static void ptp_function_cleanup(struct android_usb_function *f)
1165{
1166 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1167}
1168
1169static int
1170ptp_function_bind_config(struct android_usb_function *f,
1171 struct usb_configuration *c)
1172{
1173 return mtp_bind_config(c, true);
1174}
1175
1176static int mtp_function_ctrlrequest(struct android_usb_function *f,
1177 struct usb_composite_dev *cdev,
1178 const struct usb_ctrlrequest *c)
1179{
1180 return mtp_ctrlrequest(cdev, c);
1181}
1182
1183static struct android_usb_function mtp_function = {
1184 .name = "mtp",
1185 .init = mtp_function_init,
1186 .cleanup = mtp_function_cleanup,
1187 .bind_config = mtp_function_bind_config,
1188 .ctrlrequest = mtp_function_ctrlrequest,
1189};
1190
1191/* PTP function is same as MTP with slightly different interface descriptor */
1192static struct android_usb_function ptp_function = {
1193 .name = "ptp",
1194 .init = ptp_function_init,
1195 .cleanup = ptp_function_cleanup,
1196 .bind_config = ptp_function_bind_config,
1197};
1198#endif
1199
1200struct rndis_function_config {
1201 u8 ethaddr[ETH_ALEN];
1202 u32 vendorID;
1203 char manufacturer[256];
1204 /* "Wireless" RNDIS; auto-detected by Windows */
1205 bool wceis;
1206};
1207
1208static int
1209rndis_function_init(struct android_usb_function *f,
1210 struct usb_composite_dev *cdev)
1211{
1212 USBSTACK_DBG("%s", __func__);
1213 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1214 if (!f->config)
1215 return -ENOMEM;
1216 return 0;
1217}
1218
1219static void rndis_function_cleanup(struct android_usb_function *f)
1220{
1221 USBSTACK_DBG("%s", __func__);
1222 kfree(f->config);
1223 f->config = NULL;
1224}
1225
1226extern unsigned int ecm_setup_work_time;
1227static int
1228rndis_function_bind_config(struct android_usb_function *f,
1229 struct usb_configuration *c)
1230{
1231 int ret;
1232 struct rndis_function_config *rndis = f->config;
1233
1234 if(ecm_setup_work_time == 1){
1235 ecm_setup_work_time = 0;
1236 ecm_work_run_cnt = 0;
1237 }
1238 if (!rndis) {
1239 pr_err("%s: rndis_pdata\n", __func__);
1240 return -1;
1241 }
1242 USBSTACK_DBG("%s", __func__);
1243
1244 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1245 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1246 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1247
1248
1249 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, USB_VNIC_NAME);
1250 if (ret) {
1251 printk("%s: gether_setup_name failed, ret:%d", __func__, ret);
1252 return ret;
1253 }
1254
1255 //Ó¦ÓòãδʹÓÃ
1256 if (rndis->wceis) {
1257 /* "Wireless" RNDIS; auto-detected by Windows */
1258 rndis_iad_descriptor.bFunctionClass =
1259 USB_CLASS_WIRELESS_CONTROLLER;
1260 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1261 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1262 rndis_control_intf.bInterfaceClass =
1263 USB_CLASS_WIRELESS_CONTROLLER;
1264 rndis_control_intf.bInterfaceSubClass = 0x01;
1265 rndis_control_intf.bInterfaceProtocol = 0x03;
1266 }
1267 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,rndis->manufacturer);
1268
1269}
1270
1271static void rndis_function_unbind_config(struct android_usb_function *f,
1272 struct usb_configuration *c)
1273{
1274 USBSTACK_DBG("%s", __func__);
1275 gether_cleanup();
1276}
1277
1278static ssize_t rndis_manufacturer_show(struct device *dev,
1279 struct device_attribute *attr, char *buf)
1280{
1281 struct android_usb_function *f = dev_get_drvdata(dev);
1282 if(NULL == f)
1283 {
1284 return -EINVAL;
1285 }
1286 struct rndis_function_config *config = f->config;
1287 if(NULL == config)
1288 {
1289 return -EINVAL;
1290 }
1291 return sprintf(buf, "%s\n", config->manufacturer);
1292}
1293
1294static ssize_t rndis_manufacturer_store(struct device *dev,
1295 struct device_attribute *attr, const char *buf, size_t size)
1296{
1297 struct android_usb_function *f = dev_get_drvdata(dev);
1298 if(NULL == f)
1299 {
1300 return -EINVAL;
1301 }
1302 struct rndis_function_config *config = f->config;
1303 if(NULL == config)
1304 {
1305 return -EINVAL;
1306 }
1307 if (size >= sizeof(config->manufacturer))
1308 return -EINVAL;
1309 if (sscanf(buf, "%s", config->manufacturer) == 1)
1310 return size;
1311 return -1;
1312}
1313
1314static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1315 rndis_manufacturer_store);
1316
1317static ssize_t rndis_wceis_show(struct device *dev,
1318 struct device_attribute *attr, char *buf)
1319{
1320 struct android_usb_function *f = dev_get_drvdata(dev);
1321 if(NULL == f)
1322 {
1323 return -EINVAL;
1324 }
1325 struct rndis_function_config *config = f->config;
1326 if(NULL == config)
1327 {
1328 return -EINVAL;
1329 }
1330
1331 return sprintf(buf, "%d\n", config->wceis);
1332}
1333
1334static ssize_t rndis_wceis_store(struct device *dev,
1335 struct device_attribute *attr, const char *buf, size_t size)
1336{
1337 struct android_usb_function *f = dev_get_drvdata(dev);
1338 if(NULL == f)
1339 {
1340 return -EINVAL;
1341 }
1342 struct rndis_function_config *config = f->config;
1343 if(NULL == config)
1344 {
1345 return -EINVAL;
1346 }
1347 int value;
1348
1349 if (sscanf(buf, "%d", &value) == 1) {
1350 config->wceis = value;
1351 return size;
1352 }
1353 return -EINVAL;
1354}
1355
1356static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1357 rndis_wceis_store);
1358
1359static ssize_t rndis_ethaddr_show(struct device *dev,
1360 struct device_attribute *attr, char *buf)
1361{
1362 struct android_usb_function *f = dev_get_drvdata(dev);
1363 if(NULL == f)
1364 {
1365 return -EINVAL;
1366 }
1367 struct rndis_function_config *rndis = f->config;
1368 if(NULL == rndis)
1369 {
1370 return -EINVAL;
1371 }
1372
1373 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1374 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1375 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1376}
1377
1378static ssize_t rndis_ethaddr_store(struct device *dev,
1379 struct device_attribute *attr, const char *buf, size_t size)
1380{
1381 struct android_usb_function *f = dev_get_drvdata(dev);
1382 if(NULL == f)
1383 {
1384 return -EINVAL;
1385 }
1386 struct rndis_function_config *rndis = f->config;
1387 if(NULL == rndis)
1388 {
1389 return -EINVAL;
1390 }
1391
1392 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1393 (unsigned int *)&rndis->ethaddr[0], (unsigned int *)&rndis->ethaddr[1],
1394 (unsigned int *)&rndis->ethaddr[2], (unsigned int *)&rndis->ethaddr[3],
1395 (unsigned int *)&rndis->ethaddr[4], (unsigned int *)&rndis->ethaddr[5]) == 6)
1396 return size;
1397 return -EINVAL;
1398}
1399
1400static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1401 rndis_ethaddr_store);
1402
1403static ssize_t rndis_vendorID_show(struct device *dev,
1404 struct device_attribute *attr, char *buf)
1405{
1406 struct android_usb_function *f = dev_get_drvdata(dev);
1407 if(NULL == f)
1408 {
1409 return -EINVAL;
1410 }
1411 struct rndis_function_config *config = f->config;
1412 if(NULL == config)
1413 {
1414 return -EINVAL;
1415 }
1416 return sprintf(buf, "%04x\n", config->vendorID);
1417}
1418
1419static ssize_t rndis_vendorID_store(struct device *dev,
1420 struct device_attribute *attr, const char *buf, size_t size)
1421{
1422 struct android_usb_function *f = dev_get_drvdata(dev);
1423 if(NULL == f)
1424 {
1425 return -EINVAL;
1426 }
1427
1428 struct rndis_function_config *config = f->config;
1429 if(NULL == config)
1430 {
1431 return -EINVAL;
1432 }
1433 int value;
1434
1435 if (sscanf(buf, "%04x", (unsigned int *)(&value)) == 1) {
1436 config->vendorID = value;
1437 return size;
1438 }
1439 return -EINVAL;
1440}
1441
1442static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1443 rndis_vendorID_store);
1444
1445
1446extern unsigned long long g_test_xmit_pktnum;
1447extern unsigned long long g_test_xmit_pkterr1;
1448extern unsigned long long g_test_xmit_pkterr2;
1449extern unsigned long long g_test_rx_pkt ;
1450extern unsigned long long g_test_rx_complt_pkt;
1451
1452static ssize_t rndis_pktNum_show(struct device *dev,
1453 struct device_attribute *attr, char *buf)
1454{
1455 return sprintf(buf, "pktnun:%04x, tx_total:%lld, tx_err1:%lld, tx_err2:%lld,rx:%lld, rx_cpl:%lld\n", multi_packet_num,
1456 g_test_xmit_pktnum, g_test_xmit_pkterr1, g_test_xmit_pkterr2,
1457 g_test_rx_pkt,g_test_rx_complt_pkt);
1458}
1459
1460static ssize_t rndis_pktNum_store(struct device *dev,
1461 struct device_attribute *attr, const char *buf, size_t size)
1462{
1463 int value;
1464
1465 sscanf(buf, "%d", &value);
1466 if ((value > RNDIS_PACKET_MAX_NUM )||(value<1)){
1467 USB_DEBUG("WARNNING:SET INVALID PACKET NUM:%d!!!", value);
1468 value = RNDIS_PACKET_MAX_NUM;
1469 }
1470 multi_packet_num = value;
1471 return size;
1472}
1473static DEVICE_ATTR(pktNum, S_IRUGO | S_IWUSR, rndis_pktNum_show,
1474 rndis_pktNum_store);
1475
1476static ssize_t ether_skbNum_show(struct device *dev,
1477 struct device_attribute *attr, char *buf)
1478{
1479 return sprintf(buf, "skbnun:%04x\n", ether_skb_num);
1480}
1481
1482static ssize_t ether_skbNum_store(struct device *dev,
1483 struct device_attribute *attr, const char *buf, size_t size)
1484{
1485 int value;
1486
1487 sscanf(buf, "%d", &value);
1488 if ((value > 512)||(value<5)){
1489 USB_DEBUG("WARNNING:SET INVALID PACKET NUM:%d!!!", value);
1490 value = 512;
1491 }
1492 ether_skb_num = value;
1493 return size;
1494}
1495
1496static DEVICE_ATTR(skbNum, S_IRUGO | S_IWUSR, ether_skbNum_show,
1497 ether_skbNum_store);
1498
1499static struct device_attribute *rndis_function_attributes[] = {
1500 &dev_attr_manufacturer,
1501 &dev_attr_wceis,
1502 &dev_attr_ethaddr,
1503 &dev_attr_vendorID,
1504 &dev_attr_pktNum,
1505 &dev_attr_skbNum,
1506 NULL
1507};
1508
1509static struct android_usb_function rndis_function = {
1510 .name = "rndis",
1511 .init = rndis_function_init,
1512 .cleanup = rndis_function_cleanup,
1513 .bind_config = rndis_function_bind_config,
1514 .unbind_config = rndis_function_unbind_config,
1515 .attributes = rndis_function_attributes,
1516};
1517
1518
1519
1520struct mbim_function_config {
1521 u8 ethaddr[ETH_ALEN];
1522 u32 vendorID;
1523 char manufacturer[256];
1524 bool wceis;
1525};
1526
1527static int mbim_function_init(struct android_usb_function *f,
1528 struct usb_composite_dev *cdev)
1529{
1530 printk("mbim_function_init\n");
1531 USBSTACK_DBG("%s", __func__);
1532 int ret = 0 ;
1533 f->config = kzalloc(sizeof(struct mbim_function_config), GFP_KERNEL);
1534 if (!f->config)
1535 return -ENOMEM;
1536#if 1
1537 ret = mbim_conn_chanel_init() ;
1538
1539 if(ret)
1540 {
1541 kfree(f->config) ;
1542 f->config = NULL ;
1543 return ret ;
1544 }
1545#endif
1546 return 0;
1547}
1548
1549static void mbim_function_cleanup(struct android_usb_function *f)
1550{
1551 USBSTACK_DBG("%s", __func__);
1552 kfree(f->config);
1553 f->config = NULL;
1554}
1555
1556//extern unsigned int ecm_setup_work_time;
1557static int mbim_function_bind_config(struct android_usb_function *f,
1558 struct usb_configuration *c)
1559{
1560 printk("mbim_function_bind_config\n");
1561 int ret;
1562 struct mbim_function_config *mbim = f->config;
1563
1564
1565 if (!mbim) {
1566 pr_err("%s: mbim_pdata\n", __func__);
1567 return -1;
1568 }
1569 USBSTACK_DBG("%s", __func__);
1570
1571 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1572 mbim->ethaddr[0], mbim->ethaddr[1], mbim->ethaddr[2],
1573 mbim->ethaddr[3], mbim->ethaddr[4], mbim->ethaddr[5]);
1574
1575 ret = mbim_bind_config(c, mbim->ethaddr);
1576 if (ret) {
1577 printk("%s: mbim_bind_config failed, ret:%d", __func__, ret);
1578 return ret;
1579 }
1580 return gether_setup_name(c->cdev->gadget, mbim->ethaddr, USB_VNIC_NAME);
1581}
1582
1583static void mbim_function_unbind_config(struct android_usb_function *f,
1584 struct usb_configuration *c)
1585{
1586 USBSTACK_DBG("%s", __func__);
1587 gether_cleanup();
1588}
1589
1590#if 0
1591static int mbim_function_ctrlrequest(struct android_usb_function * f,
1592 struct usb_composite_dev *dev,
1593 const struct usb_ctrlrequest * ctrl)
1594{
1595 int value = -1;
1596 struct mbim_function_config *mbim = f->config;
1597#if 1
1598 switch (ctrl->bRequestType & USB_TYPE_MASK){
1599 case USB_TYPE_STANDARD:
1600 if(ctrl->bRequest == USB_REQ_SET_CONFIGURATION){
1601 if((!mbim->receive_setIfac)&&(!mbim->receive_setICfg)){
1602 printk("mbim-receive_setICfg\n");
1603 //schedule_delayed_work(&ecm->work, msecs_to_jiffies(ECM_TIMEOUT));
1604 }
1605 if((mbim->receive_setIfac)&&(!mbim->receive_setICfg))
1606 gether_mbim_uevent(mbim->instances, 1);
1607 mbim->receive_setICfg = 1;
1608 }else if(ctrl->bRequest == USB_REQ_SET_INTERFACE){
1609 u8 intf = (le16_to_cpu(ctrl->wIndex)) & 0xFF;
1610 struct usb_function *f_intf = dev->config->interface[intf];
1611 struct f_mbim *f_mbim = func_to_mbim(f_intf);
1612 printk("ecm-receive_setinterface\n");
1613 if (intf != f_mbim->data_id)
1614 return value;
1615 if(mbim->receive_setICfg){
1616 //USBSTACK_DBG("ecm-delayed-work cancel");
1617 //cancel_delayed_work_sync(&ecm->work);
1618 gether_mbim_uevent(mbim->instances, 1);
1619 }
1620 mbim->receive_setIfac = 1;
1621 }
1622 break;
1623 case USB_TYPE_VENDOR:
1624 if((ctrl->bRequest == bMS_Code_Original )||
1625 (ctrl->bRequest == bMS_Code_Change )){
1626 printk("mbim-sys-id-err");
1627 schedule_delayed_work(&mbim->work, msecs_to_jiffies(50));
1628 }
1629 break;
1630 case USB_TYPE_CLASS:
1631 if(ctrl->bRequest == USB_CDC_GET_NTB_PARAMETERS){
1632 value = w_length > sizeof mbim_ntb_parameters ?
1633 sizeof mbim_ntb_parameters : w_length;
1634 memcpy(req->buf, &mbim_ntb_parameters, value);
1635 //USBSTACK_DBG(cdev, "Host asked NTB parameters\n");
1636 break;
1637 }
1638 }
1639#endif
1640 return value;
1641}
1642#endif
1643
1644
1645#if 0
1646static ssize_t mbim_manufacturer_show(struct device *dev,
1647 struct device_attribute *attr, char *buf)
1648{
1649 struct android_usb_function *f = dev_get_drvdata(dev);
1650 struct rndis_function_config *config = f->config;
1651 return sprintf(buf, "%s\n", config->manufacturer);
1652}
1653
1654static ssize_t mbim_manufacturer_store(struct device *dev,
1655 struct device_attribute *attr, const char *buf, size_t size)
1656{
1657 struct android_usb_function *f = dev_get_drvdata(dev);
1658 struct mbim_function_config *config = f->config;
1659
1660 if (size >= sizeof(config->manufacturer))
1661 return -EINVAL;
1662 if (sscanf(buf, "%s", config->manufacturer) == 1)
1663 return size;
1664 return -1;
1665}
1666
1667static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, mbim_manufacturer_show,
1668 mbim_manufacturer_store);
1669
1670static ssize_t mbim_wceis_show(struct device *dev,
1671 struct device_attribute *attr, char *buf)
1672{
1673 struct android_usb_function *f = dev_get_drvdata(dev);
1674 struct mbim_function_config *config = f->config;
1675 return sprintf(buf, "%d\n", config->wceis);
1676}
1677static ssize_t mbim_wceis_store(struct device *dev,
1678 struct device_attribute *attr, const char *buf, size_t size)
1679{
1680 struct android_usb_function *f = dev_get_drvdata(dev);
1681 struct mbim_function_config *config = f->config;
1682 int value;
1683
1684 if (sscanf(buf, "%d", &value) == 1) {
1685 config->wceis = value;
1686 return size;
1687 }
1688 return -EINVAL;
1689}
1690
1691static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, mbim_wceis_show,
1692 mbim_wceis_store);
1693
1694static ssize_t mbim_ethaddr_show(struct device *dev,
1695 struct device_attribute *attr, char *buf)
1696{
1697 struct android_usb_function *f = dev_get_drvdata(dev);
1698 struct mbim_function_config *mbim = f->config;
1699 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1700 mbim->ethaddr[0], mbim->ethaddr[1], mbim->ethaddr[2],
1701 mbim->ethaddr[3], mbim->ethaddr[4], mbim->ethaddr[5]);
1702}
1703
1704static ssize_t mbim_ethaddr_store(struct device *dev,
1705 struct device_attribute *attr, const char *buf, size_t size)
1706{
1707 struct android_usb_function *f = dev_get_drvdata(dev);
1708 struct mbim_function_config *mbim = f->config;
1709
1710 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1711 (unsigned int *)&mbim->ethaddr[0], (unsigned int *)&mbim->ethaddr[1],
1712 (unsigned int *)&mbim->ethaddr[2], (unsigned int *)&mbim->ethaddr[3],
1713 (unsigned int *)&mbim->ethaddr[4], (unsigned int *)&mbim->ethaddr[5]) == 6)
1714 return size;
1715 return -EINVAL;
1716}
1717
1718static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, mbim_ethaddr_show,
1719 mbim_ethaddr_store);
1720
1721static ssize_t mbim_vendorID_show(struct device *dev,
1722 struct device_attribute *attr, char *buf)
1723{
1724 struct android_usb_function *f = dev_get_drvdata(dev);
1725 struct mbim_function_config *config = f->config;
1726 return sprintf(buf, "%04x\n", config->vendorID);
1727}
1728
1729static ssize_t mbim_vendorID_store(struct device *dev,
1730 struct device_attribute *attr, const char *buf, size_t size)
1731{
1732 struct android_usb_function *f = dev_get_drvdata(dev);
1733 struct mbim_function_config *config = f->config;
1734 int value;
1735
1736 if (sscanf(buf, "%04x", (unsigned int *)(&value)) == 1) {
1737 config->vendorID = value;
1738 return size;
1739 }
1740 return -EINVAL;
1741}
1742
1743static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, mbim_vendorID_show,
1744 mbim_vendorID_store);
1745
1746
1747extern unsigned long long g_test_xmit_pktnum;
1748extern unsigned long long g_test_xmit_pkterr1;
1749extern unsigned long long g_test_xmit_pkterr2;
1750extern unsigned long long g_test_rx_pkt ;
1751extern unsigned long long g_test_rx_complt_pkt;
1752
1753static DEVICE_ATTR(skbNum, S_IRUGO | S_IWUSR, ether_skbNum_show,
1754 ether_skbNum_store);
1755#endif
1756
1757static ssize_t mbim_pktNum_show(struct device *dev,
1758 struct device_attribute *attr, char *buf)
1759{
1760 return sprintf(buf, "pktnun:%04x, tx_total:%lld, tx_err1:%lld, tx_err2:%lld,rx:%lld, rx_cpl:%lld\n", multi_packet_num,
1761 g_test_xmit_pktnum, g_test_xmit_pkterr1, g_test_xmit_pkterr2,
1762 g_test_rx_pkt,g_test_rx_complt_pkt);
1763}
1764
1765static ssize_t mbim_pktNum_store(struct device *dev,
1766 struct device_attribute *attr, const char *buf, size_t size)
1767{
1768 int value;
1769
1770 sscanf(buf, "%d", &value);
1771 if ((value > MBIM_PACKET_MAX_NUM) || (value<1)){
1772 USB_DEBUG("WARNNING:SET INVALID PACKET NUM:%d!!!", value);
1773 value = MBIM_PACKET_MAX_NUM ;
1774 }
1775 multi_packet_num = value;
1776 return size;
1777}
1778static DEVICE_ATTR(mbimPktNum, S_IRUGO | S_IWUSR, mbim_pktNum_show,
1779 mbim_pktNum_store);
1780
1781static struct device_attribute *mbim_function_attributes[] = {
1782 //&dev_attr_mbim,
1783 //&dev_attr_wceis,
1784 //&dev_attr_ethaddr,
1785 //&dev_attr_vendorID,
1786 &dev_attr_mbimPktNum,
1787 //&dev_attr_skbNum,
1788 NULL,
1789} ;
1790
1791
1792
1793
1794
1795 static struct android_usb_function mbim_function = {
1796 .name = "mbim",
1797 .init = mbim_function_init,
1798 .cleanup = mbim_function_cleanup,
1799 .bind_config = mbim_function_bind_config,
1800 .unbind_config = mbim_function_unbind_config,
1801 .attributes = mbim_function_attributes,
1802 //.ctrlrequest = mbim_function_ctrlrequest,
1803 };
1804
1805
1806
1807struct mass_storage_function_config {
1808 struct fsg_config fsg;
1809 struct fsg_common *common;
1810 int fsg_mods_init;
1811};
1812
1813
1814
1815static char OS_str_des[] =
1816{ 0x12, 0x03, 0x4D, 0x00, 0x53, 0x00, 0x46, 0x00,
1817 0x54, 0x00, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00,
1818 0x04, 0x00
1819};
1820
1821static char OS_ext_cfg_des_null[] =
1822{
1823 0x28,0x00,0x00,0x00,0x00,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1824 0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1825 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
1826};
1827
1828static int mass_storage_function_ctrlrequest(struct android_usb_function * f,
1829 struct usb_composite_dev *dev,
1830 const struct usb_ctrlrequest * ctrl)
1831{
1832 int value = -1;
1833 u16 w_value = le16_to_cpu(ctrl->wValue);
1834 u16 w_index = le16_to_cpu(ctrl->wIndex);
1835 u16 w_length = le16_to_cpu(ctrl->wLength);
1836
1837 struct mass_storage_function_config *config = f->config;
1838 struct usb_request *req = dev->req;
1839
1840 /* ·Çµ¥¹âÅÌÅäÖ㬲»Ê¶±ð²Ù×÷ϵͳ */
1841 if(!config->fsg_mods_init){
1842 /*ºóÐø´¦Àí¶¼ÓëmodsÓйأ¬Èç δ¼ÓÔØmods£¬ÔòÖ±½Ó·µ»Ø */
1843 return value;
1844 }
1845
1846 switch (ctrl->bRequestType & USB_TYPE_MASK){
1847
1848 case USB_TYPE_STANDARD:
1849 if(ctrl->bRequest == USB_REQ_GET_DESCRIPTOR){
1850 if(((w_value>>8) == USB_DT_STRING)&&((w_value & 0xFF) == OS_string_descriptor_id)){
1851 USBSTACK_DBG("Get Sys des");
1852 printk("Get Sys des\n");
1853 value = min(w_length, sizeof(OS_str_des));
1854 memcpy(req->buf, OS_str_des, value);
1855 req->length = value;
1856 req->zero = value < w_length;
1857
1858 value = usb_ep_queue(dev->gadget->ep0, req, GFP_ATOMIC);
1859 if (value < 0) {
1860 USBSTACK_DBG("ep_queue --> %d", value);
1861 req->status = 0;
1862 composite_setup_complete(dev->gadget->ep0, req);
1863 }
1864 }
1865 }else if(ctrl->bRequest == USB_REQ_SET_CONFIGURATION){
1866 // usb_mods_activate();
1867 }
1868 break;
1869
1870 case USB_TYPE_CLASS:
1871 if(ctrl->bRequest == US_BULK_GET_MAX_LUN){
1872 usb_mods_activate();
1873 }
1874 break;
1875 case USB_TYPE_VENDOR:
1876 if(ctrl->bRequest == bMS_Code_Original ){
1877 USBSTACK_DBG("os string bms org code");
1878 printk("os string bms org code\n");
1879#if ((defined CONFIG_ARCH_ZX297520V3_MIFI)||(defined CONFIG_ARCH_ZX297520V3_UFI)) && (defined CONFIG_MIN_VERSION)
1880 usb_set_ms_auto_reject(1);
1881#else
1882 usb_set_ms_auto_reject(0);
1883#endif
1884 }else if(ctrl->bRequest == bMS_Code_Change){
1885 USBSTACK_DBG("os string bms changed code");
1886 printk("os string bms changed code\n");
1887 usb_set_ms_auto_reject(1);
1888 }
1889 if((ctrl->bRequest == bMS_Code_Original )||
1890 (ctrl->bRequest == bMS_Code_Change )){
1891 usb_set_sys_id(0);
1892 value = min(w_length, sizeof(OS_ext_cfg_des_null));
1893 memcpy(req->buf, &OS_ext_cfg_des_null[0], value);
1894 req->length = value;
1895 req->zero = value < w_length;
1896 value = usb_ep_queue(dev->gadget->ep0, req, GFP_ATOMIC);
1897 if (value < 0) {
1898 USBSTACK_DBG("ep_queue --> %d", value);
1899 req->status = 0;
1900 composite_setup_complete(dev->gadget->ep0, req);
1901 }
1902 }
1903 break;
1904 }
1905
1906 return value;
1907}
1908
1909
1910static int mass_storage_function_init(struct android_usb_function *f,
1911 struct usb_composite_dev *cdev)
1912{
1913 struct mass_storage_function_config *config;
1914 struct fsg_common *common;
1915 int err;
1916 USBSTACK_DBG("%s", __func__);
1917 config = kzalloc(sizeof(struct mass_storage_function_config),
1918 GFP_KERNEL);
1919 if (!config)
1920 return -ENOMEM;
1921
1922 /* config cdrom */
1923 config->fsg.nluns = 1;
1924 config->fsg.luns[0].removable = 1;
1925 config->fsg.luns[0].cdrom = 1;
1926 config->fsg.luns[0].ro = 1;
1927
1928#if CFG_LUN_NUM_TWO
1929 /* config Udisk */
1930 config->fsg.nluns = 2;
1931 config->fsg.luns[1].removable = 1;
1932 config->fsg.luns[1].cdrom = 0;
1933 config->fsg.luns[1].ro = 0;
1934#endif
1935
1936 common = fsg_common_init(NULL, cdev, &config->fsg);
1937 if (IS_ERR(common)) {
1938 kfree(config);
1939 return PTR_ERR(common);
1940 }
1941
1942 err = sysfs_create_link(&f->dev->kobj,
1943 &common->luns[0].dev.kobj,
1944 "lun");
1945 if (err) {
1946 kfree(config);
1947 kfree(common);
1948 return err;
1949 }
1950
1951 config->common = common;
1952 f->config = config;
1953
1954 return 0;
1955}
1956
1957static void mass_storage_function_cleanup(struct android_usb_function *f)
1958{
1959 struct mass_storage_function_config *config;
1960 USBSTACK_DBG("%s", __func__);
1961 //add by gsn, must kill process file-storage
1962 config = (struct mass_storage_function_config *)f->config;
1963 if(config != NULL){
1964 fsg_common_release(&(config->common->ref));
1965 }
1966 kfree(f->config);
1967 f->config = NULL;
1968}
1969
1970static int mass_storage_function_bind_config(struct android_usb_function *f,
1971 struct usb_configuration *c)
1972{
1973 struct mass_storage_function_config *config = f->config;
1974 USBSTACK_DBG("%s", __func__);
1975 //usb_set_ms_auto_eject(1);// auto-reject cdrom
1976 if(_android_dev->cdrom_only){
1977 usb_mods_init();
1978 config->fsg_mods_init = 1;
1979 }
1980 return fsg_bind_config(c->cdev, c, config->common);
1981}
1982static void mass_storage_function_unbind_config(struct android_usb_function *f,
1983 struct usb_configuration *c)
1984{
1985 struct mass_storage_function_config *config = f->config;
1986 USBSTACK_DBG("%s", __func__);
1987 if(config->fsg_mods_init){
1988 usb_mods_exit();
1989 config->fsg_mods_init = 0;
1990 }
1991}
1992
1993#if CFG_LUN_NUM_TWO
1994
1995static ssize_t mass_storage_nluns_show(struct device *dev,
1996 struct device_attribute *attr, char *buf)
1997{
1998 struct android_usb_function *f = dev_get_drvdata(dev);
1999 if(NULL == f)
2000 {
2001 return -EINVAL;
2002 }
2003 struct mass_storage_function_config *config = f->config;
2004 if(NULL == config)
2005 {
2006 return -EINVAL;
2007 }
2008
2009 USBSTACK_DBG("fsg nluns_show: %d", config->common->nluns);
2010
2011 return sprintf(buf, "%d\n", config->common->nluns);
2012}
2013
2014static ssize_t mass_storage_nluns_store(struct device *dev,
2015 struct device_attribute *attr, const char *buf, size_t size)
2016{
2017 struct android_usb_function *f = dev_get_drvdata(dev);
2018 if(NULL == f)
2019 {
2020 return -EINVAL;
2021 }
2022 struct mass_storage_function_config *config = f->config;
2023 if(NULL == config)
2024 {
2025 return -EINVAL;
2026 }
2027
2028 int value;
2029 if (sscanf(buf, "%d", &value) == 1) {
2030 if((value>2)||(value<1)){
2031 USB_DEBUG("WARNNING:SET INVALID LUN NUM:%d!!!", value);
2032 value = 2;
2033 }
2034
2035 config->common->nluns = value;
2036
2037 USBSTACK_DBG("fsg nluns_store: %d", value);
2038 return size;
2039 }
2040 return -1;
2041}
2042
2043static DEVICE_ATTR(nluns, S_IRUGO | S_IWUSR,
2044 mass_storage_nluns_show,
2045 mass_storage_nluns_store);
2046
2047#endif
2048
2049static ssize_t mass_storage_inquiry_show(struct device *dev,
2050 struct device_attribute *attr, char *buf)
2051{
2052 struct android_usb_function *f = dev_get_drvdata(dev);
2053 if(NULL == f)
2054 {
2055 return -EINVAL;
2056 }
2057 struct mass_storage_function_config *config = f->config;
2058 if(NULL == config)
2059 {
2060 return -EINVAL;
2061 }
2062
2063 return sprintf(buf, "%s\n", config->common->inquiry_string);
2064}
2065
2066static ssize_t mass_storage_inquiry_store(struct device *dev,
2067 struct device_attribute *attr, const char *buf, size_t size)
2068{
2069 struct android_usb_function *f = dev_get_drvdata(dev);
2070 if(NULL == f)
2071 {
2072 return -EINVAL;
2073 }
2074 struct mass_storage_function_config *config = f->config;
2075 if(NULL == config)
2076 {
2077 return -EINVAL;
2078 }
2079 if (size >= sizeof(config->common->inquiry_string))
2080 return -EINVAL;
2081
2082 //sscanfÓöµ½¿Õ¸ñÍ£Ö¹, ¹ÊÐÞ¸ÄΪmemcpy
2083#if 1
2084 memset(config->common->inquiry_string, 0, sizeof(config->common->inquiry_string));
2085 memcpy(config->common->inquiry_string, buf, size);
2086#else
2087 if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
2088 return -EINVAL;
2089#endif
2090 return size;
2091}
2092
2093static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
2094 mass_storage_inquiry_show,
2095 mass_storage_inquiry_store);
2096
2097static struct device_attribute *mass_storage_function_attributes[] = {
2098 //&dev_attr_inquiry_string,
2099#if CFG_LUN_NUM_TWO
2100 &dev_attr_nluns,
2101#endif
2102 NULL
2103};
2104
2105static struct android_usb_function mass_storage_function = {
2106 .name = "mass_storage",
2107 .init = mass_storage_function_init,
2108 .cleanup = mass_storage_function_cleanup,
2109 .bind_config = mass_storage_function_bind_config,
2110 .unbind_config = mass_storage_function_unbind_config,
2111 .attributes = mass_storage_function_attributes,
2112 .ctrlrequest = mass_storage_function_ctrlrequest,
2113};
2114
2115
2116#if 0
2117static int accessory_function_init(struct android_usb_function *f,
2118 struct usb_composite_dev *cdev)
2119{
2120 return acc_setup();
2121}
2122
2123static void accessory_function_cleanup(struct android_usb_function *f)
2124{
2125 acc_cleanup();
2126}
2127
2128static int accessory_function_bind_config(struct android_usb_function *f,
2129 struct usb_configuration *c)
2130{
2131 return acc_bind_config(c);
2132}
2133
2134static int accessory_function_ctrlrequest(struct android_usb_function *f,
2135 struct usb_composite_dev *cdev,
2136 const struct usb_ctrlrequest *c)
2137{
2138 return acc_ctrlrequest(cdev, c);
2139}
2140
2141
2142static struct android_usb_function accessory_function = {
2143 .name = "accessory",
2144 .init = accessory_function_init,
2145 .cleanup = accessory_function_cleanup,
2146 .bind_config = accessory_function_bind_config,
2147 .ctrlrequest = accessory_function_ctrlrequest,
2148};
2149
2150static int audio_source_function_init(struct android_usb_function *f,
2151 struct usb_composite_dev *cdev)
2152{
2153 struct audio_source_config *config;
2154
2155 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
2156 if (!config)
2157 return -ENOMEM;
2158 config->card = -1;
2159 config->device = -1;
2160 f->config = config;
2161 return 0;
2162}
2163
2164static void audio_source_function_cleanup(struct android_usb_function *f)
2165{
2166 kfree(f->config);
2167}
2168
2169static int audio_source_function_bind_config(struct android_usb_function *f,
2170 struct usb_configuration *c)
2171{
2172 struct audio_source_config *config = f->config;
2173
2174 return audio_source_bind_config(c, config);
2175}
2176
2177static void audio_source_function_unbind_config(struct android_usb_function *f,
2178 struct usb_configuration *c)
2179{
2180 struct audio_source_config *config = f->config;
2181
2182 config->card = -1;
2183 config->device = -1;
2184}
2185
2186static ssize_t audio_source_pcm_show(struct device *dev,
2187 struct device_attribute *attr, char *buf)
2188{
2189 struct android_usb_function *f = dev_get_drvdata(dev);
2190 struct audio_source_config *config = f->config;
2191
2192 /* print PCM card and device numbers */
2193 return sprintf(buf, "%d %d\n", config->card, config->device);
2194}
2195
2196static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
2197
2198static struct device_attribute *audio_source_function_attributes[] = {
2199 &dev_attr_pcm,
2200 NULL
2201};
2202
2203static struct android_usb_function audio_source_function = {
2204 .name = "audio_source",
2205 .init = audio_source_function_init,
2206 .cleanup = audio_source_function_cleanup,
2207 .bind_config = audio_source_function_bind_config,
2208 .unbind_config = audio_source_function_unbind_config,
2209 .attributes = audio_source_function_attributes,
2210};
2211#endif
2212
2213static struct android_usb_function *supported_functions[] = {
2214 //&ffs_function,
2215 &ecm_function,
2216 &diag_function,
2217 &adb_function,
2218 &acm_function,
2219 &serial_function,
2220// &mtp_function,
2221// &ptp_function,
2222 &rndis_function,
2223 &mass_storage_function,
2224 &mbim_function,
2225// &accessory_function,
2226// &audio_source_function,
2227 NULL
2228};
2229
2230
2231static int android_init_functions(struct android_usb_function **functions,
2232 struct usb_composite_dev *cdev)
2233{
2234 struct android_dev *dev = _android_dev;
2235 struct android_usb_function *f;
2236 struct device_attribute **attrs;
2237 struct device_attribute *attr;
2238 int err =0;
2239 int index = 0;
2240 USBSTACK_DBG("%s", __func__);
2241 for (; (f = *functions++); index++) {
2242 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
2243 if(f->dev_name == NULL){
2244 err = -ENOMEM;
2245 goto err_out;
2246 }
2247 f->dev = device_create(android_class, dev->dev,
2248 MKDEV(0, index), f, f->dev_name);
2249 if (IS_ERR(f->dev)) {
2250 pr_err("%s: Failed to create dev %s", __func__,
2251 f->dev_name);
2252 err = PTR_ERR(f->dev);
2253 goto err_create;
2254 }
2255
2256 if (f->init) {
2257 err = f->init(f, cdev);
2258 if (err) {
2259 pr_err("%s: Failed to init %s", __func__,
2260 f->name);
2261 goto err_out;
2262 }
2263 }
2264
2265 attrs = f->attributes;
2266 if (attrs) {
2267 while ((attr = *attrs++) && !err)
2268 err = device_create_file(f->dev, attr);
2269 }
2270 if (err) {
2271 pr_err("%s: Failed to create function %s attributes",
2272 __func__, f->name);
2273 goto err_out;
2274 }
2275 }
2276 return 0;
2277
2278err_out:
2279 device_destroy(android_class, f->dev->devt);
2280err_create:
2281 kfree(f->dev_name);
2282 return err;
2283}
2284
2285static void android_cleanup_functions(struct android_usb_function **functions)
2286{
2287 struct android_usb_function *f;
2288 USBSTACK_DBG("%s", __func__);
2289 while (*functions) {
2290 f = *functions++;
2291
2292 if (f->dev) {
2293 device_destroy(android_class, f->dev->devt);
2294 kfree(f->dev_name);
2295 }
2296
2297 if (f->cleanup)
2298 f->cleanup(f);
2299 }
2300}
2301
2302static int
2303android_bind_enabled_functions(struct android_dev *dev,
2304 struct usb_configuration *c)
2305{
2306 struct android_usb_function *f;
2307 int ret;
2308 USBSTACK_DBG("%s", __func__);
2309 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2310 USBSTACK_DBG("%s, %u %s", __func__, __LINE__, f->name);
2311 ret = f->bind_config(f, c);
2312 if (ret) {
2313 USBSTACK_DBG("%s: %s failed", __func__, f->name);
2314 return ret;
2315 }
2316 }
2317 return 0;
2318}
2319
2320static void
2321android_unbind_enabled_functions(struct android_dev *dev,
2322 struct usb_configuration *c)
2323{
2324 struct android_usb_function *f;
2325 USBSTACK_DBG("%s", __func__);
2326 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2327 if (f->unbind_config)
2328 f->unbind_config(f, c);
2329 }
2330}
2331
2332static int android_enable_function(struct android_dev *dev, char *name)
2333{
2334 struct android_usb_function **functions = dev->functions;
2335 struct android_usb_function *f;
2336
2337 while ((f = *functions++)) {
2338 if (!strcmp(name, f->name)) {
2339 USBSTACK_DBG("%s--%s", __func__ , f->name );
2340 list_add_tail(&f->enabled_list,
2341 &dev->enabled_functions);
2342 return 0;
2343 }
2344 }
2345 return -EINVAL;
2346}
2347
2348/*-------------------------------------------------------------------------*/
2349/* /sys/class/android_usb/android%d/ interface */
2350
2351static ssize_t
2352functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
2353{
2354 struct android_dev *dev = dev_get_drvdata(pdev);
2355 if(NULL == dev)
2356 {
2357 return -EINVAL;
2358 }
2359 struct android_usb_function *f;
2360 char *buff = buf;
2361 USBSTACK_DBG("%s", __func__);
2362 mutex_lock(&dev->mutex);
2363
2364 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
2365 buff += sprintf(buff, "%s,", f->name);
2366
2367 mutex_unlock(&dev->mutex);
2368 USBSTACK_DBG("%s, %u", __func__, __LINE__);
2369 if (buff != buf)
2370 *(buff-1) = '\n';
2371 return buff - buf;
2372}
2373
2374static ssize_t
2375functions_store(struct device *pdev, struct device_attribute *attr,
2376 const char *buff, size_t size)
2377{
2378 struct android_dev *dev = dev_get_drvdata(pdev);
2379 if(NULL == dev)
2380 {
2381 return -EINVAL;
2382 }
2383 char *name;
2384 char buf[256], *b;
2385 char aliases[256], *a;
2386 int err;
2387 int is_ffs;
2388 int ffs_enabled = 0;
2389 int count = 0;
2390 USBSTACK_DBG("%s", __func__);
2391 mutex_lock(&dev->mutex);
2392
2393 if (dev->enabled) {
2394 mutex_unlock(&dev->mutex);
2395 USBSTACK_DBG("%s, %u", __func__, __LINE__);
2396 return -EBUSY;
2397 }
2398
2399 INIT_LIST_HEAD(&dev->enabled_functions);
2400
2401 strlcpy(buf, buff, sizeof(buf));
2402 b = strim(buf);
2403
2404 dev->cdrom_only = false;
2405 while (b) {
2406 name = strsep(&b, ",");
2407 if (!name)
2408 continue;
2409
2410 is_ffs = 0;
2411 strlcpy(aliases, dev->ffs_aliases, sizeof(aliases));
2412 a = aliases;
2413
2414 while (a) {
2415 char *alias = strsep(&a, ",");
2416 if (alias && !strcmp(name, alias)) {
2417 is_ffs = 1;
2418 break;
2419 }
2420 }
2421
2422 if (is_ffs) {
2423 if (ffs_enabled)
2424 continue;
2425 err = android_enable_function(dev, "ffs");
2426 if (err)
2427 pr_err("android_usb: Cannot enable ffs (%d)",
2428 err);
2429 else
2430 ffs_enabled = 1;
2431 continue;
2432 }
2433
2434 if((++count == 1)&&(strcmp(name, "mass_storage")==0)){
2435 dev->cdrom_only = true;
2436 }else{
2437 dev->cdrom_only = false;
2438 }
2439
2440 err = android_enable_function(dev, name);
2441 if (err){
2442 pr_err("android_usb: Cannot enable '%s' (%d)",
2443 name, err);
2444 printk(KERN_WARNING "android_usb: Cannot enable %s \n", name);
2445 }
2446 }
2447
2448 mutex_unlock(&dev->mutex);
2449 USBSTACK_DBG("%s, %u", __func__, __LINE__);
2450 return size;
2451}
2452
2453static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2454 char *buf)
2455{
2456 struct android_dev *dev = dev_get_drvdata(pdev);
2457 if(NULL == dev)
2458 {
2459 return -EINVAL;
2460 }
2461 USBSTACK_DBG("%s", __func__);
2462 return sprintf(buf, "%d\n", dev->enabled);
2463}
2464
2465static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2466 const char *buff, size_t size)
2467{
2468 struct android_dev *dev = dev_get_drvdata(pdev);
2469 if(NULL == dev)
2470 {
2471 return -EINVAL;
2472 }
2473
2474 struct usb_composite_dev *cdev = dev->cdev;
2475 if(NULL == cdev)
2476 {
2477 return -EINVAL;
2478 }
2479
2480 //struct usb_configuration *c_desc = cdev->config;
2481 struct android_usb_function *f;
2482 int enabled = 0;
2483
2484 if (!cdev)
2485 return -ENODEV;
2486
2487 USBSTACK_DBG("%s, %u", __func__, __LINE__);
2488 mutex_lock(&dev->mutex);
2489
2490 sscanf(buff, "%d", &enabled);
2491 USBSTACK_DBG("enable_store enable:%d, dev->enabled:%d", enabled, (size_t)dev->enabled);
2492
2493 if (enabled && !dev->enabled) {
2494 USB_DEBUG("USB ENABLE");
2495 USBSTACK_DBG("USB ENABLE");
2496
2497 //close usb power
2498 usb_gadget_set_selfpowered(cdev->gadget);
2499
2500 /*
2501 * Update values in composite driver's copy of
2502 * device descriptor.
2503 */
2504 cdev->desc.idVendor = device_desc.idVendor;
2505 cdev->desc.idProduct = device_desc.idProduct;
2506 cdev->desc.bcdDevice = device_desc.bcdDevice;
2507 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2508 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2509 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
2510 //c_desc->iConfiguration= android_config_driver.iConfiguration;
2511 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2512 USB_DEBUG("function name:%s", f->name);
2513 USBSTACK_DBG("function name:%s", f->name);
2514 if (f->enable)
2515 f->enable(f);
2516#if 0
2517 if(!strcmp(f->name,"ecm"))
2518 {
2519 cdev->desc.bDeviceClass = USB_CLASS_COMM;
2520 }
2521#endif
2522 }
2523 android_enable(dev);
2524 dev->enabled = true;
2525 } else if (!enabled && dev->enabled) {
2526 USB_DEBUG("USB DISENABLE");
2527 USBSTACK_DBG("USB DISENABLE");
2528 android_disable(dev);
2529 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2530 if (f->disable)
2531 f->disable(f);
2532 }
2533 dev->enabled = false;
2534 //close usb power
2535 usb_gadget_clear_selfpowered(cdev->gadget);
2536 } else {
2537 pr_err("android_usb: already %s\n",
2538 dev->enabled ? "enabled" : "disabled");
2539 USBSTACK_DBG("android_usb: already %s\n",
2540 dev->enabled ? "enabled" : "disabled");
2541 }
2542
2543 mutex_unlock(&dev->mutex);
2544 USBSTACK_DBG("%s, %u", __func__, __LINE__);
2545 return size;
2546}
2547
2548void gadget_disable(void)
2549{
2550 struct android_dev *dev = _android_dev;
2551 struct android_usb_function *f;
2552 struct usb_composite_dev *cdev = dev->cdev;
2553
2554 USB_DEBUG("USB DISENABLE");
2555 USBSTACK_DBG("USB DISENABLE");
2556 android_disable(dev);
2557 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2558 if (f->disable)
2559 f->disable(f);
2560 }
2561 dev->enabled = false;
2562 //close usb power
2563 usb_gadget_clear_selfpowered(cdev->gadget);
2564
2565}
2566EXPORT_SYMBOL(gadget_disable);
2567
2568static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2569 char *buf)
2570{
2571 struct android_dev *dev = dev_get_drvdata(pdev);
2572 if(NULL == dev)
2573 {
2574 return -EINVAL;
2575 }
2576 struct usb_composite_dev *cdev = dev->cdev;
2577 if(NULL == cdev)
2578 {
2579 return -EINVAL;
2580 }
2581 char *state = "DISCONNECTED";
2582 unsigned long flags;
2583
2584 if (!cdev)
2585 goto out;
2586
2587 spin_lock_irqsave(&cdev->lock, flags);
2588 if (cdev->config)
2589 state = "CONFIGURED";
2590 else if (dev->connected)
2591 state = "CONNECTED";
2592 spin_unlock_irqrestore(&cdev->lock, flags);
2593out:
2594 return sprintf(buf, "%s\n", state);
2595}
2596static ssize_t log_show(struct device *pdev, struct device_attribute *attr,
2597 char *buf)
2598{
2599 usb_dbg_showLog();
2600 return sprintf(buf, "%s\n", "OK"); \
2601}
2602
2603
2604static ssize_t iSerial_enable_show(struct device *pdev, struct device_attribute *attr,
2605 char *buf)
2606{
2607 return sprintf(buf, "%d\n", strings_dev[STRING_SERIAL_IDX].id); \
2608}
2609
2610static u8 snID = 0;
2611static ssize_t iSerial_enable_store(struct device *dev,
2612 struct device_attribute *attr, const char *buf, size_t size)
2613{
2614 int enabled = 0;
2615 struct android_dev *pdev = dev_get_drvdata(dev);
2616 if(NULL == pdev)
2617 {
2618 return -EINVAL ;
2619 }
2620 struct usb_composite_dev *cdev = pdev->cdev;
2621 if(NULL == cdev)
2622 {
2623 return -EINVAL;
2624 }
2625
2626 sscanf(buf, "%d", &enabled);
2627
2628 if(enabled == 0){
2629 strings_dev[STRING_SERIAL_IDX].id = 0;
2630 device_desc.iSerialNumber = 0;
2631 cdev->desc.iSerialNumber = 0;
2632 }else{
2633 strings_dev[STRING_SERIAL_IDX].id = snID;
2634 device_desc.iSerialNumber = snID;
2635 cdev->desc.iSerialNumber = snID;
2636 }
2637 return size;
2638}
2639
2640
2641#define DESCRIPTOR_ATTR(field, format_string) \
2642static ssize_t \
2643field ## _show(struct device *dev, struct device_attribute *attr, \
2644 char *buf) \
2645{ \
2646 return sprintf(buf, format_string, device_desc.field); \
2647} \
2648static ssize_t \
2649field ## _store(struct device *dev, struct device_attribute *attr, \
2650 const char *buf, size_t size) \
2651{ \
2652 int value; \
2653 if (sscanf(buf, format_string, &value) == 1) { \
2654 device_desc.field = value; \
2655 return size; \
2656 } \
2657 return -1; \
2658} \
2659static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2660
2661#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2662static ssize_t \
2663field ## _show(struct device *dev, struct device_attribute *attr, \
2664 char *buf) \
2665{ \
2666 return sprintf(buf, "%s", buffer); \
2667} \
2668static ssize_t \
2669field ## _store(struct device *dev, struct device_attribute *attr, \
2670 const char *buf, size_t size) \
2671{ \
2672 if (size >= sizeof(buffer)) \
2673 return -EINVAL; \
2674 return strlcpy(buffer, buf, sizeof(buffer)); \
2675} \
2676static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2677
2678#define CONFIGURATION_ATTR(field, format_string) \
2679static ssize_t \
2680field ## _show(struct device *dev, struct device_attribute *attr, \
2681 char *buf) \
2682{ \
2683 return sprintf(buf, format_string, android_config_driver.field); \
2684} \
2685static ssize_t \
2686field ## _store(struct device *dev, struct device_attribute *attr, \
2687 const char *buf, size_t size) \
2688{ \
2689 int value; \
2690 if (sscanf(buf, format_string, &value) == 1) { \
2691 android_config_driver.field = value; \
2692 return size; \
2693 } \
2694 return -1; \
2695} \
2696static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field## _show, field ## _store);
2697
2698#define CONFIGURATION_STRING_ATTR(field, buffer) \
2699static ssize_t \
2700field ## _show(struct device *dev, struct device_attribute *attr, \
2701 char *buf) \
2702{ \
2703 return sprintf(buf, "%s\n", buffer); \
2704} \
2705static ssize_t \
2706field ## _store(struct device *dev, struct device_attribute *attr, \
2707 const char *buf, size_t size) \
2708{ \
2709 if (size >= sizeof(buffer)) \
2710 return -EINVAL; \
2711 return strlcpy(buffer, buf, sizeof(buffer)); \
2712} \
2713static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field## _show, field ## _store);
2714
2715
2716
2717DESCRIPTOR_ATTR(idVendor, "%04x\n")
2718DESCRIPTOR_ATTR(idProduct, "%04x\n")
2719DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2720DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2721DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2722DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2723DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2724DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2725DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2726
2727CONFIGURATION_STRING_ATTR(iConfiguration, configuration_string)
2728static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2729 functions_store);
2730static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
2731static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
2732
2733//DESCRIPTOR_ATTR(bMaxPacketSize0, "%d\n")
2734
2735CONFIGURATION_ATTR(bmAttributes, "%d\n")
2736
2737static DEVICE_ATTR(log, S_IRUGO, log_show, NULL);
2738static DEVICE_ATTR(iSerial_enable, S_IRUGO | S_IWUSR, iSerial_enable_show, iSerial_enable_store);
2739
2740/*
2741 *this function show is suspend of device,1:suspended; 0:running
2742 */
2743static ssize_t suspend_state_show(struct device *dev, struct device_attribute *attr,
2744 char *buf)
2745{ struct android_dev *pdev = dev_get_drvdata(dev);
2746 if(NULL == pdev)
2747 {
2748 return -EINVAL;
2749 }
2750 struct usb_composite_dev *cdev = pdev->cdev;
2751 if(NULL == cdev)
2752 {
2753 return -EINVAL;
2754 }
2755
2756 return sprintf(buf, "%d\n", cdev->suspended);
2757}
2758static DEVICE_ATTR(suspend_state, S_IRUGO, suspend_state_show, NULL);
2759
2760/*
2761 * this function used by app to wakeup usb host and device
2762 * to use this function, usb device should enable attribute remotewakeup
2763 * in android_config_driver
2764*/
2765static ssize_t usb_wakeup_store(struct device *dev,
2766 struct device_attribute *attr, const char *buf, size_t n)
2767{
2768 struct android_dev *pdev = dev_get_drvdata(dev);
2769 struct usb_composite_dev *cdev = NULL;
2770
2771 if(NULL!=pdev)
2772 cdev= pdev->cdev;
2773 else
2774 return n;
2775
2776 if (sysfs_streq(buf, "1"))
2777 usb_gadget_wakeup(cdev->gadget);
2778
2779 return n;
2780}
2781static DEVICE_ATTR(wakeup, S_IWUSR, NULL, usb_wakeup_store);
2782
2783
2784static ssize_t usb_enum_mode_show(struct device *pdev, struct device_attribute *attr,
2785 char *buf)
2786{
2787 return sprintf(buf, "%d\n", usb_cur_enum_mode);
2788}
2789static ssize_t usb_enum_mode_store(struct device *dev,
2790 struct device_attribute *attr, const char *buf, size_t size)
2791{
2792 unsigned long enum_mode = 0;
2793 enum_mode = simple_strtoul(buf, NULL, 16);
2794
2795 if(enum_mode < 5){
2796 usb_cur_enum_mode = enum_mode;
2797 }else
2798 printk("set mode fail, default mode is %d\n", usb_cur_enum_mode);
2799 return size;
2800}
2801static DEVICE_ATTR(usb_enum_mode, S_IRUGO | S_IWUSR, usb_enum_mode_show, usb_enum_mode_store);
2802
2803
2804static struct device_attribute *android_usb_attributes[] = {
2805 &dev_attr_idVendor,
2806 &dev_attr_idProduct,
2807 &dev_attr_bcdDevice,
2808 &dev_attr_bDeviceClass,
2809 &dev_attr_bDeviceSubClass,
2810 &dev_attr_bDeviceProtocol,
2811 &dev_attr_iManufacturer,
2812 &dev_attr_iProduct,
2813 &dev_attr_iSerial,
2814 &dev_attr_functions,
2815 &dev_attr_enable,
2816 &dev_attr_state,
2817 &dev_attr_log,
2818 &dev_attr_iConfiguration,
2819 &dev_attr_iSerial_enable,
2820 &dev_attr_suspend_state,
2821 &dev_attr_wakeup,
2822 &dev_attr_usb_enum_mode,
2823 &dev_attr_bmAttributes,
2824 NULL
2825};
2826
2827/*-------------------------------------------------------------------------*/
2828/* Composite driver */
2829
2830static int android_bind_config(struct usb_configuration *c)
2831{
2832 struct android_dev *dev = _android_dev;
2833 int ret = 0;
2834 USBSTACK_DBG("%s", __func__);
2835 ret = android_bind_enabled_functions(dev, c);
2836 if (ret)
2837 return ret;
2838
2839 return 0;
2840}
2841
2842static void android_unbind_config(struct usb_configuration *c)
2843{
2844 struct android_dev *dev = _android_dev;
2845 USBSTACK_DBG("%s", __func__);
2846 android_unbind_enabled_functions(dev, c);
2847}
2848
2849static int android_bind(struct usb_composite_dev *cdev)
2850{
2851 struct android_dev *dev = _android_dev;
2852 struct usb_gadget *gadget = cdev->gadget;
2853 int gcnum, id, ret;
2854 USBSTACK_DBG("%s", __func__);
2855 /*
2856 * Start disconnected. Userspace will connect the gadget once
2857 * it is done configuring the functions.
2858 */
2859 usb_gadget_disconnect(gadget);
2860
2861 ret = android_init_functions(dev->functions, cdev);
2862 if (ret)
2863 return ret;
2864
2865 /* Allocate string descriptor numbers ... note that string
2866 * contents can be overridden by the composite_dev glue.
2867 */
2868 id = usb_string_id(cdev);
2869 if (id < 0)
2870 return id;
2871 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2872 device_desc.iManufacturer = id;
2873
2874 id = usb_string_id(cdev);
2875 if (id < 0)
2876 return id;
2877 strings_dev[STRING_CONFIGURATION_IDX].id = id;
2878 android_config_driver.iConfiguration= id;
2879 id = usb_string_id(cdev);
2880 if (id < 0)
2881 return id;
2882 strings_dev[STRING_PRODUCT_IDX].id = id;
2883 device_desc.iProduct = id;
2884
2885 /* Default strings - should be updated by userspace */
2886 strncpy(manufacturer_string, "DEMO,Incorporated", sizeof(manufacturer_string)-1);
2887 strncpy(product_string, "DEMO Mobile Boardband", sizeof(product_string) - 1);
2888 strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
2889 strncpy(configuration_string, "DEMO Configuration", sizeof(configuration_string) - 1);
2890
2891 id = usb_string_id(cdev);
2892 if (id < 0)
2893 return id;
2894 strings_dev[STRING_SERIAL_IDX].id = id;
2895 device_desc.iSerialNumber = id;
2896 snID = id;
2897
2898 gcnum = usb_gadget_controller_number(gadget);
2899 if (gcnum >= 0)
2900 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2901 else {
2902 pr_warning("%s: controller '%s' not recognized\n",
2903 longname, gadget->name);
2904 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2905 }
2906
2907 dev->cdev = cdev;
2908
2909 return 0;
2910}
2911
2912static int android_usb_unbind(struct usb_composite_dev *cdev)
2913{
2914 struct android_dev *dev = _android_dev;
2915 USBSTACK_DBG("%s", __func__);
2916 cancel_work_sync(&dev->work);
2917 android_cleanup_functions(dev->functions);
2918 return 0;
2919}
2920
2921static struct usb_composite_driver android_usb_driver = {
2922 .name = "android_usb",
2923 .dev = &device_desc,
2924 .strings = dev_strings,
2925 .unbind = android_usb_unbind,
2926 .max_speed = USB_SPEED_HIGH,
2927};
2928
2929static int
2930android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2931{
2932 struct android_dev *dev = _android_dev;
2933 if(NULL == dev)
2934 {
2935 return -EINVAL;
2936 }
2937 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2938 if(NULL == cdev)
2939 {
2940 return -EINVAL;
2941 }
2942 struct usb_request *req = cdev->req;
2943 struct android_usb_function *f;
2944 int value = -EOPNOTSUPP;
2945 unsigned long flags;
2946
2947#if 1
2948 USBSTACK_DBG("SETUP 0x%x,%x,v0x%x,w0x%x,l0x%x",
2949 c->bRequestType,
2950 c->bRequest,
2951 c->wValue,
2952 c->wIndex,
2953 c->wLength);
2954 USBSTACK_DBG("%s, %u", __func__, __LINE__);
2955// usb_dbg_ep0reg();
2956#endif
2957 req->zero = 0;
2958 req->complete = composite_setup_complete;
2959 req->length = 0;
2960 gadget->ep0->driver_data = cdev;
2961
2962 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
2963 if (f->ctrlrequest) {
2964 value = f->ctrlrequest(f, cdev, c);
2965 if (value >= 0)
2966 break;
2967 }
2968 }
2969
2970 spin_lock_irqsave(&cdev->lock, flags);
2971 if((c->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR){
2972 //USBSTACK_DBG("schedule_work vendor");
2973 dev->vendor_req.bRequest = c->bRequest;
2974 dev->vendor_req.wValue = c->wValue;
2975 if(dev->vendor_req.bRequest == 0xA2){
2976 panic("now panic by user\n");
2977 }
2978 //if(usb_cur_enum_mode != USB_ENUM_MODE_USER)
2979 schedule_work(&dev->usbmode);
2980 //else
2981 // printk("user mode can not do mode switch\n");
2982 }
2983 spin_unlock_irqrestore(&cdev->lock, flags);
2984
2985 if(((c->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)&&(value==-1))
2986 return value=-1;
2987
2988 /* Special case the accessory function.
2989 * It needs to handle control requests before it is enabled.
2990 */
2991#if 0
2992 if (value < 0)
2993 value = acc_ctrlrequest(cdev, c);
2994#endif
2995 if (value < 0)
2996 value = composite_setup(gadget, c);
2997
2998 spin_lock_irqsave(&cdev->lock, flags);
2999 if (!dev->connected) {
3000 dev->connected = 1;
3001 schedule_work(&dev->work);
3002 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
3003 cdev->config) {
3004 schedule_work(&dev->work);
3005 }
3006 spin_unlock_irqrestore(&cdev->lock, flags);
3007
3008 return value;
3009}
3010
3011static void android_disconnect(struct usb_gadget *gadget)
3012{
3013 struct android_dev *dev = _android_dev;
3014 struct usb_composite_dev *cdev = get_gadget_data(gadget);
3015 unsigned long flags;
3016 USBSTACK_DBG("%s", __func__);
3017 composite_disconnect(gadget);
3018 /* accessory HID support can be active while the
3019 accessory function is not actually enabled,
3020 so we need to inform it when we are disconnected.
3021 */
3022#if 0
3023 acc_disconnect();
3024#endif
3025 spin_lock_irqsave(&cdev->lock, flags);
3026 dev->connected = 0;
3027 schedule_work(&dev->work);
3028 spin_unlock_irqrestore(&cdev->lock, flags);
3029}
3030
3031static int android_create_device(struct android_dev *dev)
3032{
3033 struct device_attribute **attrs = android_usb_attributes;
3034 struct device_attribute *attr;
3035 int err;
3036
3037 dev->dev = device_create(android_class, NULL,
3038 MKDEV(0, 0), NULL, "android0");
3039 if (IS_ERR(dev->dev))
3040 return PTR_ERR(dev->dev);
3041
3042 dev_set_drvdata(dev->dev, dev);
3043
3044 while ((attr = *attrs++)) {
3045 err = device_create_file(dev->dev, attr);
3046 if (err) {
3047 device_destroy(android_class, dev->dev->devt);
3048 return err;
3049 }
3050 }
3051 return 0;
3052}
3053
3054
3055static int __init init(void)
3056{
3057 struct android_dev *dev;
3058 int err;
3059 USBSTACK_DBG("REGISTER USB STACK DRIVER BEGIN");
3060 android_class = class_create(THIS_MODULE, "android_usb");
3061 if (IS_ERR(android_class))
3062 return PTR_ERR(android_class);
3063
3064 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3065 if (!dev)
3066 return -ENOMEM;
3067
3068 dev->disable_depth = 1;
3069 //dev->disable_depth = 0;
3070 dev->functions = supported_functions;
3071 INIT_LIST_HEAD(&dev->enabled_functions);
3072 INIT_WORK(&dev->work, android_work);
3073 INIT_WORK(&dev->usbmode, usb_mode_work);
3074 USBSTACK_DBG("%s, %u", __func__, __LINE__);
3075 mutex_init(&dev->mutex);
3076
3077 err = android_create_device(dev);
3078 if (err) {
3079 class_destroy(android_class);
3080 kfree(dev);
3081 return err;
3082 }
3083
3084 _android_dev = dev;
3085
3086 /* Override composite driver functions */
3087 composite_driver.setup = android_setup;
3088 composite_driver.disconnect = android_disconnect;
3089
3090 err = usb_composite_probe(&android_usb_driver, android_bind);
3091
3092 //close usb power at last
3093 usb_gadget_clear_selfpowered(dev->cdev->gadget);
3094 USBSTACK_DBG("REGISTER USB STACK DRIVER END");
3095 return err;
3096}
3097module_init(init);
3098
3099static void __exit cleanup(void)
3100{
3101 usb_composite_unregister(&android_usb_driver);
3102 class_destroy(android_class);
3103 kfree(_android_dev);
3104 _android_dev = NULL;
3105}
3106module_exit(cleanup);