2 * Gadget Driver for Android
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@android.com>
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.
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.
21 #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
23 #include <linux/init.h>
24 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/kernel.h>
28 #include <linux/utsname.h>
29 #include <linux/platform_device.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/composite.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/printk.h>
36 /* Add for HW/SW connect */
37 #include <linux/musb/mtk_musb.h>
38 /* Add for HW/SW connect */
40 #include "gadget_chips.h"
43 #include "f_audio_source.c"
44 #include "f_mass_storage.c"
47 #include "f_accessory.c"
48 #define USB_ETH_RNDIS y
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
);
61 MODULE_AUTHOR("Mike Lockwood");
62 MODULE_DESCRIPTION("Android Composite USB Driver");
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION("1.0");
66 static const char longname
[] = "Gadget Android";
68 /* Default vendor and product IDs, overridden by userspace */
69 #define VENDOR_ID 0x0BB4
70 #define PRODUCT_ID 0x0001
72 /* Default manufacturer and product string , overridden by userspace */
73 #define MANUFACTURER_STRING "MediaTek"
74 #define PRODUCT_STRING "MT65xx Android Phone"
77 //#define USB_LOG "USB"
79 struct android_usb_function
{
85 struct device_attribute
**attributes
;
87 /* for android_dev.enabled_functions */
88 struct list_head enabled_list
;
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
*);
100 int (*bind_config
)(struct android_usb_function
*,
101 struct usb_configuration
*);
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
*);
113 struct android_usb_function
**functions
;
114 struct list_head enabled_functions
;
115 struct usb_composite_dev
*cdev
;
123 struct work_struct work
;
124 char ffs_aliases
[256];
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
);
134 /* string IDs are assigned dynamically */
135 #define STRING_MANUFACTURER_IDX 0
136 #define STRING_PRODUCT_IDX 1
137 #define STRING_SERIAL_IDX 2
139 static char manufacturer_string
[256];
140 static char product_string
[256];
141 static char serial_string
[256];
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 */
151 static struct usb_gadget_strings stringtab_dev
= {
152 .language
= 0x0409, /* en-us */
153 .strings
= strings_dev
,
156 static struct usb_gadget_strings
*dev_strings
[] = {
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),
167 .bcdUSB
= __constant_cpu_to_le16(0x0200),
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,
176 static struct usb_configuration android_config_driver
= {
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,
186 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
188 #ifdef CONFIG_USB_MU3D_DRV
189 .MaxPower
= 192, /* Only consume 192ma for passing USB30CV Descriptor Test [USB3.0 devices]*/
191 .MaxPower
= 500, /* 500ma */
195 static void android_work(struct work_struct
*data
)
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
};
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 */
208 char *rezero_event
[2] = { "USB_STATE=REZEROCMD", NULL
};
209 char *showcdrom_event
[2] = { "USB_STATE=SHOWCDROMCMD", NULL
};
211 char **uevent_envp
= NULL
;
212 char **uevent_envp_cdrom
= NULL
;
214 /* Add for HW/SW connect */
215 bool is_hwconnected
= true;
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*/
223 if(usb_cable_connected())
224 is_hwconnected
= true;
226 is_hwconnected
= false;
228 pr_debug("[XLOG_INFO][USB]%s: is_hwconnected=%d \n", __func__
, is_hwconnected
);
229 /* Add for HW/SW connect */
231 spin_lock_irqsave(&cdev
->lock
, flags
);
233 uevent_envp
= configured
;
234 } else if (dev
->connected
!= dev
->sw_connected
) {
235 uevent_envp
= dev
->connected
? connected
: disconnected
;
238 dev
->sw_connected
= dev
->connected
;
240 if (dev
->rezero_cmd
== 1) {
241 uevent_envp_cdrom
= rezero_event
;
243 } else if (dev
->rezero_cmd
== 2) {
244 uevent_envp_cdrom
= showcdrom_event
;
248 spin_unlock_irqrestore(&cdev
->lock
, flags
);
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]);
254 pr_debug("[XLOG_INFO][USB]%s: did not send uevent (%d %d %p)\n", __func__
,
255 dev
->connected
, dev
->sw_connected
, cdev
->config
);
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]);
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]);
267 pr_debug("[XLOG_INFO][USB]%s: did not send zero uevent\n", __func__
);
272 static void android_enable(struct android_dev
*dev
)
274 struct usb_composite_dev
*cdev
= dev
->cdev
;
276 if (WARN_ON(!dev
->disable_depth
))
279 if (--dev
->disable_depth
== 0) {
280 usb_add_config(cdev
, &android_config_driver
,
281 android_bind_config
);
282 usb_gadget_connect(cdev
->gadget
);
286 static void android_disable(struct android_dev
*dev
)
288 struct usb_composite_dev
*cdev
= dev
->cdev
;
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
);
298 /*-------------------------------------------------------------------------*/
299 /* Supported functions initialization */
301 struct functionfs_config
{
304 struct ffs_data
*data
;
307 static int ffs_function_init(struct android_usb_function
*f
,
308 struct usb_composite_dev
*cdev
)
310 f
->config
= kzalloc(sizeof(struct functionfs_config
), GFP_KERNEL
);
314 return functionfs_init();
317 static void ffs_function_cleanup(struct android_usb_function
*f
)
319 functionfs_cleanup();
323 static void ffs_function_enable(struct android_usb_function
*f
)
325 struct android_dev
*dev
= _android_dev
;
326 struct functionfs_config
*config
= f
->config
;
328 config
->enabled
= true;
330 /* Disable the gadget until the function is ready */
332 android_disable(dev
);
335 static void ffs_function_disable(struct android_usb_function
*f
)
337 struct android_dev
*dev
= _android_dev
;
338 struct functionfs_config
*config
= f
->config
;
340 config
->enabled
= false;
342 /* Balance the disable that was called in closed_callback */
347 static int ffs_function_bind_config(struct android_usb_function
*f
,
348 struct usb_configuration
*c
)
350 struct functionfs_config
*config
= f
->config
;
351 return functionfs_bind_config(c
->cdev
, c
, config
->data
);
355 ffs_aliases_show(struct device
*pdev
, struct device_attribute
*attr
, char *buf
)
357 struct android_dev
*dev
= _android_dev
;
360 mutex_lock(&dev
->mutex
);
361 ret
= sprintf(buf
, "%s\n", dev
->ffs_aliases
);
362 mutex_unlock(&dev
->mutex
);
368 ffs_aliases_store(struct device
*pdev
, struct device_attribute
*attr
,
369 const char *buf
, size_t size
)
371 struct android_dev
*dev
= _android_dev
;
374 mutex_lock(&dev
->mutex
);
377 mutex_unlock(&dev
->mutex
);
381 strlcpy(buff
, buf
, sizeof(buff
));
382 strlcpy(dev
->ffs_aliases
, strim(buff
), sizeof(dev
->ffs_aliases
));
384 mutex_unlock(&dev
->mutex
);
389 static DEVICE_ATTR(aliases
, S_IRUGO
| S_IWUSR
, ffs_aliases_show
,
391 static struct device_attribute
*ffs_function_attributes
[] = {
396 static struct android_usb_function ffs_function
= {
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
,
406 static int functionfs_ready_callback(struct ffs_data
*ffs
)
408 struct android_dev
*dev
= _android_dev
;
409 struct functionfs_config
*config
= ffs_function
.config
;
412 mutex_lock(&dev
->mutex
);
414 ret
= functionfs_bind(ffs
, dev
->cdev
);
419 config
->opened
= true;
425 mutex_unlock(&dev
->mutex
);
429 static void functionfs_closed_callback(struct ffs_data
*ffs
)
431 struct android_dev
*dev
= _android_dev
;
432 struct functionfs_config
*config
= ffs_function
.config
;
434 mutex_lock(&dev
->mutex
);
437 android_disable(dev
);
439 config
->opened
= false;
442 functionfs_unbind(ffs
);
444 mutex_unlock(&dev
->mutex
);
447 static void *functionfs_acquire_dev_callback(const char *dev_name
)
452 static void functionfs_release_dev_callback(struct ffs_data
*ffs_data
)
462 adb_function_init(struct android_usb_function
*f
,
463 struct usb_composite_dev
*cdev
)
465 f
->config
= kzalloc(sizeof(struct adb_data
), GFP_KERNEL
);
472 static void adb_function_cleanup(struct android_usb_function
*f
)
479 adb_function_bind_config(struct android_usb_function
*f
,
480 struct usb_configuration
*c
)
482 return adb_bind_config(c
);
485 static void adb_android_function_enable(struct android_usb_function
*f
)
487 /* This patch cause WHQL fail */
489 struct android_dev
*dev
= _android_dev
;
490 struct adb_data
*data
= f
->config
;
492 data
->enabled
= true;
494 /* Disable the gadget until adbd is ready */
496 android_disable(dev
);
500 static void adb_android_function_disable(struct android_usb_function
*f
)
502 /* This patch cause WHQL fail */
504 struct android_dev
*dev
= _android_dev
;
505 struct adb_data
*data
= f
->config
;
507 data
->enabled
= false;
509 /* Balance the disable that was called in closed_callback */
515 static struct android_usb_function adb_function
= {
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
,
524 static void adb_ready_callback(void)
526 /* This patch cause WHQL fail */
528 struct android_dev
*dev
= _android_dev
;
529 struct adb_data
*data
= adb_function
.config
;
531 mutex_lock(&dev
->mutex
);
538 mutex_unlock(&dev
->mutex
);
542 static void adb_closed_callback(void)
544 /* This patch cause WHQL fail */
546 struct android_dev
*dev
= _android_dev
;
547 struct adb_data
*data
= adb_function
.config
;
549 mutex_lock(&dev
->mutex
);
551 data
->opened
= false;
554 android_disable(dev
);
556 mutex_unlock(&dev
->mutex
);
560 #define MAX_ACM_INSTANCES 4
561 struct acm_function_config
{
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
];
571 acm_function_init(struct android_usb_function
*f
,
572 struct usb_composite_dev
*cdev
)
576 struct acm_function_config
*config
;
577 #ifdef CONFIG_USBIF_COMPLIANCE
578 /* FIXME, USB_IF workaround */
582 config
= kzalloc(sizeof(struct acm_function_config
), GFP_KERNEL
);
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
;
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
;
600 err_usb_get_function_instance
:
601 pr_err("Could not usb_get_function_instance() %d\n", i
);
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
]);
611 static void acm_function_cleanup(struct android_usb_function
*f
)
614 struct acm_function_config
*config
= f
->config
;
615 #ifdef CONFIG_USBIF_COMPLIANCE
616 /* FIXME, USB_IF workaround */
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
]);
629 acm_function_bind_config(struct android_usb_function
*f
,
630 struct usb_configuration
*c
)
634 struct acm_function_config
*config
= f
->config
;
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
]);
641 pr_err("Could not bind acm%u config\n", i
);
642 goto err_usb_add_function
;
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;
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
]);
655 pr_err("Could not bind acm%u config\n", i
);
656 goto err_usb_add_function
;
658 pr_info("%s Open /dev/ttyGS%d\n", __func__
, i
);
663 err_usb_add_function
:
665 usb_remove_function(c
, config
->f_acm
[i
]);
669 static void acm_function_unbind_config(struct android_usb_function
*f
,
670 struct usb_configuration
*c
)
674 //struct acm_function_config *config = f->config;
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.
682 * What does the original kernel code look likes?
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]);
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]);
703 static ssize_t
acm_instances_show(struct device
*dev
,
704 struct device_attribute
*attr
, char *buf
)
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
);
711 static ssize_t
acm_instances_store(struct device
*dev
,
712 struct device_attribute
*attr
, const char *buf
, size_t size
)
714 struct android_usb_function
*f
= dev_get_drvdata(dev
);
715 struct acm_function_config
*config
= f
->config
;
718 sscanf(buf
, "%d", &value
);
719 if (value
> MAX_ACM_INSTANCES
)
720 value
= MAX_ACM_INSTANCES
;
721 config
->instances
= value
;
725 static DEVICE_ATTR(instances
, S_IRUGO
| S_IWUSR
, acm_instances_show
,
726 acm_instances_store
);
728 static ssize_t
acm_port_index_show(struct device
*dev
,
729 struct device_attribute
*attr
, char *buf
)
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]);
737 static ssize_t
acm_port_index_store(struct device
*dev
,
738 struct device_attribute
*attr
, const char *buf
, size_t size
)
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};
746 num
= sscanf(buf
, "%d,%d,%d,%d", &(val
[0]), &(val
[1]), &(val
[2]), &(val
[3]));
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
);
751 /* Set all port_index as 0*/
752 for (tmp
= 0; tmp
< MAX_ACM_INSTANCES
; tmp
++)
753 config
->port_index
[tmp
] = 0;
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;
763 static DEVICE_ATTR(port_index
, S_IRUGO
| S_IWUSR
, acm_port_index_show
,
764 acm_port_index_store
);
766 static struct device_attribute
*acm_function_attributes
[] = {
768 &dev_attr_port_index
, /*Only open the specific port*/
772 static struct android_usb_function acm_function
= {
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
,
781 #ifdef CONFIG_USB_F_LOOPBACK
783 #define MAX_LOOPBACK_INSTANCES 1
785 struct loopback_function_config
{
787 struct usb_function
*f_lp
[MAX_LOOPBACK_INSTANCES
];
788 struct usb_function_instance
*f_lp_inst
[MAX_LOOPBACK_INSTANCES
];
791 static int loopback_function_init(struct android_usb_function
*f
, struct usb_composite_dev
*cdev
)
795 struct loopback_function_config
*config
;
797 config
= kzalloc(sizeof(struct loopback_function_config
), GFP_KERNEL
);
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
;
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
;
815 err_usb_get_function_instance
:
816 pr_err("Could not usb_get_function_instance() %d\n", i
);
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
]);
826 static void loopback_function_cleanup(struct android_usb_function
*f
)
829 struct loopback_function_config
*config
= f
->config
;
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
]);
840 loopback_function_bind_config(struct android_usb_function
*f
, struct usb_configuration
*c
)
844 struct loopback_function_config
*config
= f
->config
;
846 ret
= usb_add_function(c
, config
->f_lp
[config
->port_num
]);
848 pr_err("Could not bind loopback%u config\n", config
->port_num
);
849 goto err_usb_add_function
;
851 pr_info("%s Open loopback\n", __func__
);
855 err_usb_add_function
:
857 usb_remove_function(c
, config
->f_lp
[i
]);
861 static struct android_usb_function loopback_function
= {
863 .init
= loopback_function_init
,
864 .cleanup
= loopback_function_cleanup
,
865 .bind_config
= loopback_function_bind_config
,
869 #define MAX_SERIAL_INSTANCES 4
871 struct serial_function_config
{
873 struct usb_function
*f_ser
[MAX_SERIAL_INSTANCES
];
874 struct usb_function_instance
*f_ser_inst
[MAX_SERIAL_INSTANCES
];
877 static int serial_function_init(struct android_usb_function
*f
, struct usb_composite_dev
*cdev
)
881 struct serial_function_config
*config
;
882 #ifdef CONFIG_USBIF_COMPLIANCE
883 /* FIXME, USB_IF workaround */
887 config
= kzalloc(sizeof(struct serial_function_config
), GFP_KERNEL
);
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
;
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
;
905 err_usb_get_function_instance
:
906 pr_err("Could not usb_get_function_instance() %d\n", i
);
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
]);
916 static void serial_function_cleanup(struct android_usb_function
*f
)
919 struct serial_function_config
*config
= f
->config
;
920 #ifdef CONFIG_USBIF_COMPLIANCE
921 /* FIXME, USB_IF workaround */
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
]);
934 serial_function_bind_config(struct android_usb_function
*f
, struct usb_configuration
*c
)
938 struct serial_function_config
*config
= f
->config
;
940 ret
= usb_add_function(c
, config
->f_ser
[config
->port_num
]);
942 pr_err("Could not bind ser%u config\n", config
->port_num
);
943 goto err_usb_add_function
;
945 pr_info("%s Open /dev/ttyGS%d\n", __func__
, i
);
949 err_usb_add_function
:
951 usb_remove_function(c
, config
->f_ser
[i
]);
955 static ssize_t
serial_port_show(struct device
*dev
,
956 struct device_attribute
*attr
, char *buf
)
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
);
963 static ssize_t
serial_port_store(struct device
*dev
,
964 struct device_attribute
*attr
, const char *buf
, size_t size
)
966 struct android_usb_function
*f
= dev_get_drvdata(dev
);
967 struct serial_function_config
*config
= f
->config
;
970 sscanf(buf
, "%d", &value
);
971 if (value
> MAX_SERIAL_INSTANCES
)
972 value
= MAX_SERIAL_INSTANCES
;
973 config
->port_num
= value
;
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
};
980 static struct android_usb_function serial_function
= {
982 .init
= serial_function_init
,
983 .cleanup
= serial_function_cleanup
,
984 .bind_config
= serial_function_bind_config
,
985 .attributes
= serial_function_attributes
,
989 mtp_function_init(struct android_usb_function
*f
,
990 struct usb_composite_dev
*cdev
)
995 static void mtp_function_cleanup(struct android_usb_function
*f
)
1001 mtp_function_bind_config(struct android_usb_function
*f
,
1002 struct usb_configuration
*c
)
1004 return mtp_bind_config(c
, false);
1008 ptp_function_init(struct android_usb_function
*f
,
1009 struct usb_composite_dev
*cdev
)
1011 /* nothing to do - initialization is handled by mtp_function_init */
1015 static void ptp_function_cleanup(struct android_usb_function
*f
)
1017 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1021 ptp_function_bind_config(struct android_usb_function
*f
,
1022 struct usb_configuration
*c
)
1024 return mtp_bind_config(c
, true);
1027 static int mtp_function_ctrlrequest(struct android_usb_function
*f
,
1028 struct usb_composite_dev
*cdev
,
1029 const struct usb_ctrlrequest
*c
)
1031 /* MTP MSFT OS Descriptor */
1032 struct android_dev
*dev
= _android_dev
;
1033 struct android_usb_function
*f_count
;
1035 char usb_function_string
[32];
1036 char * buff
= usb_function_string
;
1038 list_for_each_entry(f_count
, &dev
->enabled_functions
, enabled_list
)
1041 buff
+= sprintf(buff
, "%s,", f_count
->name
);
1045 mtp_read_usb_functions(functions_no
, usb_function_string
);
1046 return mtp_ctrlrequest(cdev
, c
);
1049 static int ptp_function_ctrlrequest(struct android_usb_function
*f
,
1050 struct usb_composite_dev
*cdev
,
1051 const struct usb_ctrlrequest
*c
)
1053 return ptp_ctrlrequest(cdev
, c
);
1057 static struct android_usb_function mtp_function
= {
1059 .init
= mtp_function_init
,
1060 .cleanup
= mtp_function_cleanup
,
1061 .bind_config
= mtp_function_bind_config
,
1062 .ctrlrequest
= mtp_function_ctrlrequest
,
1065 /* PTP function is same as MTP with slightly different interface descriptor */
1066 static struct android_usb_function ptp_function
= {
1068 .init
= ptp_function_init
,
1069 .cleanup
= ptp_function_cleanup
,
1070 .bind_config
= ptp_function_bind_config
,
1071 .ctrlrequest
= ptp_function_ctrlrequest
, //ALPS01832160
1074 struct ecm_function_config
{
1075 u8 ethaddr
[ETH_ALEN
];
1076 struct eth_dev
*dev
;
1079 static int ecm_function_init(struct android_usb_function
*f
, struct usb_composite_dev
*cdev
)
1081 f
->config
= kzalloc(sizeof(struct ecm_function_config
), GFP_KERNEL
);
1087 static void ecm_function_cleanup(struct android_usb_function
*f
)
1094 ecm_function_bind_config(struct android_usb_function
*f
,
1095 struct usb_configuration
*c
)
1098 struct eth_dev
*dev
;
1099 struct ecm_function_config
*ecm
= f
->config
;
1102 pr_err("%s: ecm_pdata\n", __func__
);
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]);
1111 dev
= gether_setup_name(c
->cdev
->gadget
, ecm
->ethaddr
, "rndis");
1114 pr_err("%s: gether_setup failed\n", __func__
);
1119 return ecm_bind_config(c
, ecm
->ethaddr
, ecm
->dev
);
1122 static void ecm_function_unbind_config(struct android_usb_function
*f
,
1123 struct usb_configuration
*c
)
1125 struct ecm_function_config
*ecm
= f
->config
;
1126 gether_cleanup(ecm
->dev
);
1129 static ssize_t
ecm_ethaddr_show(struct device
*dev
,
1130 struct device_attribute
*attr
, char *buf
)
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]);
1139 static ssize_t
ecm_ethaddr_store(struct device
*dev
,
1140 struct device_attribute
*attr
, const char *buf
, size_t size
)
1142 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1143 struct ecm_function_config
*ecm
= f
->config
;
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)
1153 static DEVICE_ATTR(ecm_ethaddr
, S_IRUGO
| S_IWUSR
, ecm_ethaddr_show
,
1156 static struct device_attribute
*ecm_function_attributes
[] = {
1157 &dev_attr_ecm_ethaddr
,
1161 static struct android_usb_function ecm_function
= {
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
,
1170 struct eem_function_config
{
1171 u8 ethaddr
[ETH_ALEN
];
1172 char manufacturer
[256];
1173 struct eth_dev
*dev
;
1177 eem_function_init(struct android_usb_function
*f
,
1178 struct usb_composite_dev
*cdev
)
1180 f
->config
= kzalloc(sizeof(struct eem_function_config
), GFP_KERNEL
);
1186 static void eem_function_cleanup(struct android_usb_function
*f
)
1193 eem_function_bind_config(struct android_usb_function
*f
,
1194 struct usb_configuration
*c
)
1197 struct eth_dev
*dev
;
1198 struct eem_function_config
*eem
= f
->config
;
1200 pr_debug("[XLOG_DEBUG][USB]%s: \n", __func__
);
1203 pr_err("%s: rndis_pdata\n", __func__
);
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]);
1211 dev
= gether_setup_name(c
->cdev
->gadget
, eem
->ethaddr
, "rndis");
1214 pr_err("%s: gether_setup failed\n", __func__
);
1219 return eem_bind_config(c
, eem
->dev
);
1222 static void eem_function_unbind_config(struct android_usb_function
*f
,
1223 struct usb_configuration
*c
)
1225 struct eem_function_config
*eem
= f
->config
;
1226 gether_cleanup(eem
->dev
);
1229 static ssize_t
eem_ethaddr_show(struct device
*dev
,
1230 struct device_attribute
*attr
, char *buf
)
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]);
1239 static ssize_t
eem_ethaddr_store(struct device
*dev
,
1240 struct device_attribute
*attr
, const char *buf
, size_t size
)
1242 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1243 struct eem_function_config
*eem
= f
->config
;
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)
1253 static DEVICE_ATTR(eem_ethaddr
, S_IRUGO
| S_IWUSR
, eem_ethaddr_show
,
1256 static struct device_attribute
*eem_function_attributes
[] = {
1257 &dev_attr_eem_ethaddr
,
1261 static struct android_usb_function eem_function
= {
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
,
1270 struct rndis_function_config
{
1271 u8 ethaddr
[ETH_ALEN
];
1273 char manufacturer
[256];
1274 /* "Wireless" RNDIS; auto-detected by Windows */
1276 struct eth_dev
*dev
;
1280 rndis_function_init(struct android_usb_function
*f
,
1281 struct usb_composite_dev
*cdev
)
1283 f
->config
= kzalloc(sizeof(struct rndis_function_config
), GFP_KERNEL
);
1289 static void rndis_function_cleanup(struct android_usb_function
*f
)
1296 rndis_function_bind_config(struct android_usb_function
*f
,
1297 struct usb_configuration
*c
)
1300 struct eth_dev
*dev
;
1301 struct rndis_function_config
*rndis
= f
->config
;
1303 pr_debug("[XLOG_DEBUG][USB]%s: \n", __func__
);
1306 pr_err("%s: rndis_pdata\n", __func__
);
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]);
1314 dev
= gether_setup_name(c
->cdev
->gadget
, rndis
->ethaddr
, "rndis");
1317 pr_err("%s: gether_setup failed\n", __func__
);
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;
1334 return rndis_bind_config_vendor(c
, rndis
->ethaddr
, rndis
->vendorID
,
1335 rndis
->manufacturer
, rndis
->dev
);
1338 static void rndis_function_unbind_config(struct android_usb_function
*f
,
1339 struct usb_configuration
*c
)
1341 struct rndis_function_config
*rndis
= f
->config
;
1342 gether_cleanup(rndis
->dev
);
1345 static ssize_t
rndis_manufacturer_show(struct device
*dev
,
1346 struct device_attribute
*attr
, char *buf
)
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
);
1353 static ssize_t
rndis_manufacturer_store(struct device
*dev
,
1354 struct device_attribute
*attr
, const char *buf
, size_t size
)
1356 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1357 struct rndis_function_config
*config
= f
->config
;
1359 if (size
>= sizeof(config
->manufacturer
))
1361 if (sscanf(buf
, "%s", config
->manufacturer
) == 1)
1366 static DEVICE_ATTR(manufacturer
, S_IRUGO
| S_IWUSR
, rndis_manufacturer_show
,
1367 rndis_manufacturer_store
);
1369 static ssize_t
rndis_wceis_show(struct device
*dev
,
1370 struct device_attribute
*attr
, char *buf
)
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
);
1377 static ssize_t
rndis_wceis_store(struct device
*dev
,
1378 struct device_attribute
*attr
, const char *buf
, size_t size
)
1380 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1381 struct rndis_function_config
*config
= f
->config
;
1384 if (sscanf(buf
, "%d", &value
) == 1) {
1385 config
->wceis
= value
;
1391 static DEVICE_ATTR(wceis
, S_IRUGO
| S_IWUSR
, rndis_wceis_show
,
1394 static ssize_t
rndis_ethaddr_show(struct device
*dev
,
1395 struct device_attribute
*attr
, char *buf
)
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]);
1404 static ssize_t
rndis_ethaddr_store(struct device
*dev
,
1405 struct device_attribute
*attr
, const char *buf
, size_t size
)
1407 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1408 struct rndis_function_config
*rndis
= f
->config
;
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)
1418 static DEVICE_ATTR(ethaddr
, S_IRUGO
| S_IWUSR
, rndis_ethaddr_show
,
1419 rndis_ethaddr_store
);
1421 static ssize_t
rndis_vendorID_show(struct device
*dev
,
1422 struct device_attribute
*attr
, char *buf
)
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
);
1429 static ssize_t
rndis_vendorID_store(struct device
*dev
,
1430 struct device_attribute
*attr
, const char *buf
, size_t size
)
1432 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1433 struct rndis_function_config
*config
= f
->config
;
1436 if (sscanf(buf
, "%04x", &value
) == 1) {
1437 config
->vendorID
= value
;
1443 static DEVICE_ATTR(vendorID
, S_IRUGO
| S_IWUSR
, rndis_vendorID_show
,
1444 rndis_vendorID_store
);
1446 static struct device_attribute
*rndis_function_attributes
[] = {
1447 &dev_attr_manufacturer
,
1454 static struct android_usb_function rndis_function
= {
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
,
1464 struct mass_storage_function_config
{
1465 struct fsg_config fsg
;
1466 struct fsg_common
*common
;
1469 static int mass_storage_function_init(struct android_usb_function
*f
,
1470 struct usb_composite_dev
*cdev
)
1472 struct mass_storage_function_config
*config
;
1473 struct fsg_common
*common
;
1477 config
= kzalloc(sizeof(struct mass_storage_function_config
),
1482 #ifdef CONFIG_MTK_MULTI_STORAGE_SUPPORT
1483 #define LUN_MULTI (1)
1485 #define LUN_MULTI (0)
1488 #ifdef CONFIG_MTK_SHARED_SDCARD
1489 #define LUN_SHARED_SD (-1)
1491 #define LUN_SHARED_SD (0)
1494 #ifdef CONFIG_MTK_ICUSB_SUPPORT
1495 #define LUN_ICUSB (1)
1497 #define LUN_ICUSB (0)
1500 #define LUN_NUM LUN_MULTI + LUN_SHARED_SD + LUN_ICUSB + 1
1502 config
->fsg
.nluns
= LUN_NUM
;
1504 for(i
= 0; i
< config
->fsg
.nluns
; i
++) {
1505 config
->fsg
.luns
[i
].removable
= 1;
1506 config
->fsg
.luns
[i
].nofua
= 1;
1509 common
= fsg_common_init(NULL
, cdev
, &config
->fsg
);
1510 if (IS_ERR(common
)) {
1512 return PTR_ERR(common
);
1515 err
= sysfs_create_link(&f
->dev
->kobj
,
1516 &common
->luns
[0].dev
.kobj
,
1524 * "i" starts from "1", cuz dont want to change the naming of
1525 * the original path of "lun0".
1527 for(i
= 1; i
< config
->fsg
.nluns
; i
++) {
1528 char string_lun
[5]={0};
1530 sprintf(string_lun
, "lun%d",i
);
1532 err
= sysfs_create_link(&f
->dev
->kobj
,
1533 &common
->luns
[i
].dev
.kobj
,
1541 config
->common
= common
;
1546 static void mass_storage_function_cleanup(struct android_usb_function
*f
)
1552 static int mass_storage_function_bind_config(struct android_usb_function
*f
,
1553 struct usb_configuration
*c
)
1555 struct mass_storage_function_config
*config
= f
->config
;
1556 return fsg_bind_config(c
->cdev
, c
, config
->common
);
1559 static ssize_t
mass_storage_inquiry_show(struct device
*dev
,
1560 struct device_attribute
*attr
, char *buf
)
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
);
1567 static ssize_t
mass_storage_inquiry_store(struct device
*dev
,
1568 struct device_attribute
*attr
, const char *buf
, size_t size
)
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
))
1574 if (sscanf(buf
, "%s", config
->common
->inquiry_string
) != 1)
1579 static DEVICE_ATTR(inquiry_string
, S_IRUGO
| S_IWUSR
,
1580 mass_storage_inquiry_show
,
1581 mass_storage_inquiry_store
);
1583 #ifdef CONFIG_MTK_BICR_SUPPORT
1585 static ssize_t
mass_storage_bicr_show(struct device
*dev
,
1586 struct device_attribute
*attr
, char *buf
)
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
);
1593 static ssize_t
mass_storage_bicr_store(struct device
*dev
,
1594 struct device_attribute
*attr
, const char *buf
, size_t size
)
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
))
1600 if (sscanf(buf
, "%d", &config
->common
->bicr
) != 1)
1603 /* Set Lun[0] is a CDROM when enable bicr.*/
1604 if (!strcmp(buf
, "1"))
1605 config
->common
->luns
[0].cdrom
= 1;
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;
1617 static DEVICE_ATTR(bicr
, S_IRUGO
| S_IWUSR
,
1618 mass_storage_bicr_show
,
1619 mass_storage_bicr_store
);
1623 static struct device_attribute
*mass_storage_function_attributes
[] = {
1624 &dev_attr_inquiry_string
,
1625 #ifdef CONFIG_MTK_BICR_SUPPORT
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
,
1640 static int accessory_function_init(struct android_usb_function
*f
,
1641 struct usb_composite_dev
*cdev
)
1646 static void accessory_function_cleanup(struct android_usb_function
*f
)
1651 static int accessory_function_bind_config(struct android_usb_function
*f
,
1652 struct usb_configuration
*c
)
1654 return acc_bind_config(c
);
1657 static int accessory_function_ctrlrequest(struct android_usb_function
*f
,
1658 struct usb_composite_dev
*cdev
,
1659 const struct usb_ctrlrequest
*c
)
1661 return acc_ctrlrequest(cdev
, c
);
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
,
1672 static int audio_source_function_init(struct android_usb_function
*f
,
1673 struct usb_composite_dev
*cdev
)
1675 struct audio_source_config
*config
;
1677 config
= kzalloc(sizeof(struct audio_source_config
), GFP_KERNEL
);
1681 config
->device
= -1;
1686 static void audio_source_function_cleanup(struct android_usb_function
*f
)
1691 static int audio_source_function_bind_config(struct android_usb_function
*f
,
1692 struct usb_configuration
*c
)
1694 struct audio_source_config
*config
= f
->config
;
1696 return audio_source_bind_config(c
, config
);
1699 static void audio_source_function_unbind_config(struct android_usb_function
*f
,
1700 struct usb_configuration
*c
)
1702 struct audio_source_config
*config
= f
->config
;
1705 config
->device
= -1;
1708 static ssize_t
audio_source_pcm_show(struct device
*dev
,
1709 struct device_attribute
*attr
, char *buf
)
1711 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1712 struct audio_source_config
*config
= f
->config
;
1714 /* print PCM card and device numbers */
1715 return sprintf(buf
, "%d %d\n", config
->card
, config
->device
);
1718 static DEVICE_ATTR(pcm
, S_IRUGO
, audio_source_pcm_show
, NULL
);
1720 static struct device_attribute
*audio_source_function_attributes
[] = {
1725 #ifdef CONFIG_EVDO_DT_SUPPORT
1726 static int rawbulk_function_init(struct android_usb_function
*f
,
1727 struct usb_composite_dev
*cdev
)
1732 static void rawbulk_function_cleanup(struct android_usb_function
*f
)
1737 static int rawbulk_function_bind_config(struct android_usb_function
*f
,
1738 struct usb_configuration
*c
)
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
);
1754 static int rawbulk_function_modem_ctrlrequest(struct android_usb_function
*f
,
1755 struct usb_composite_dev
*cdev
,
1756 const struct usb_ctrlrequest
*c
)
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
);
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
,
1774 static struct android_usb_function rawbulk_ets_function
= {
1776 .init
= rawbulk_function_init
,
1777 .cleanup
= rawbulk_function_cleanup
,
1778 .bind_config
= rawbulk_function_bind_config
,
1781 static struct android_usb_function rawbulk_atc_function
= {
1783 .init
= rawbulk_function_init
,
1784 .cleanup
= rawbulk_function_cleanup
,
1785 .bind_config
= rawbulk_function_bind_config
,
1788 static struct android_usb_function rawbulk_pcv_function
= {
1790 .init
= rawbulk_function_init
,
1791 .cleanup
= rawbulk_function_cleanup
,
1792 .bind_config
= rawbulk_function_bind_config
,
1795 static struct android_usb_function rawbulk_gps_function
= {
1797 .init
= rawbulk_function_init
,
1798 .cleanup
= rawbulk_function_cleanup
,
1799 .bind_config
= rawbulk_function_bind_config
,
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
,
1812 static struct android_usb_function
*supported_functions
[] = {
1818 #ifndef CONFIG_USBIF_COMPLIANCE
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
,
1834 #ifdef CONFIG_USB_F_LOOPBACK
1841 static int android_init_functions(struct android_usb_function
**functions
,
1842 struct usb_composite_dev
*cdev
)
1844 struct android_dev
*dev
= _android_dev
;
1845 struct android_usb_function
*f
;
1846 struct device_attribute
**attrs
;
1847 struct device_attribute
*attr
;
1850 #ifdef CONFIG_USBIF_COMPLIANCE
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__
,
1866 err
= PTR_ERR(f
->dev
);
1871 err
= f
->init(f
, cdev
);
1873 pr_err("%s: Failed to init %s", __func__
,
1877 pr_debug("[XLOG_INFO][USB]%s: init %s success!!\n", __func__
, f
->name
);
1881 attrs
= f
->attributes
;
1883 while ((attr
= *attrs
++) && !err
)
1884 err
= device_create_file(f
->dev
, attr
);
1887 pr_err("%s: Failed to create function %s attributes",
1895 device_destroy(android_class
, f
->dev
->devt
);
1901 static void android_cleanup_functions(struct android_usb_function
**functions
)
1903 struct android_usb_function
*f
;
1905 while (*functions
) {
1909 device_destroy(android_class
, f
->dev
->devt
);
1919 android_bind_enabled_functions(struct android_dev
*dev
,
1920 struct usb_configuration
*c
)
1922 struct android_usb_function
*f
;
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 */
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
);
1933 pr_err("%s: %s failed", __func__
, f
->name
);
1941 android_unbind_enabled_functions(struct android_dev
*dev
,
1942 struct usb_configuration
*c
)
1944 struct android_usb_function
*f
;
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
);
1953 static int android_enable_function(struct android_dev
*dev
, char *name
)
1955 struct android_usb_function
**functions
= dev
->functions
;
1956 struct android_usb_function
*f
;
1957 while ((f
= *functions
++)) {
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
);
1971 /*-------------------------------------------------------------------------*/
1972 /* /sys/class/android_usb/android%d/ interface */
1975 functions_show(struct device
*pdev
, struct device_attribute
*attr
, char *buf
)
1977 struct android_dev
*dev
= dev_get_drvdata(pdev
);
1978 struct android_usb_function
*f
;
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 */
1985 mutex_lock(&dev
->mutex
);
1987 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
)
1988 buff
+= sprintf(buff
, "%s,", f
->name
);
1990 mutex_unlock(&dev
->mutex
);
1998 functions_store(struct device
*pdev
, struct device_attribute
*attr
,
1999 const char *buff
, size_t size
)
2001 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2004 char aliases
[256], *a
;
2007 int ffs_enabled
= 0;
2009 mutex_lock(&dev
->mutex
);
2012 mutex_unlock(&dev
->mutex
);
2016 INIT_LIST_HEAD(&dev
->enabled_functions
);
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 */
2022 strlcpy(buf
, buff
, sizeof(buf
));
2026 name
= strsep(&b
, ",");
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 */
2036 strlcpy(aliases
, dev
->ffs_aliases
, sizeof(aliases
));
2040 char *alias
= strsep(&a
, ",");
2041 if (alias
&& !strcmp(name
, alias
)) {
2050 err
= android_enable_function(dev
, "ffs");
2052 pr_err("android_usb: Cannot enable ffs (%d)",
2059 err
= android_enable_function(dev
, name
);
2061 pr_err("android_usb: Cannot enable '%s' (%d)",
2065 mutex_unlock(&dev
->mutex
);
2070 static ssize_t
enable_show(struct device
*pdev
, struct device_attribute
*attr
,
2073 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2074 return sprintf(buf
, "%d\n", dev
->enabled
);
2077 static ssize_t
enable_store(struct device
*pdev
, struct device_attribute
*attr
,
2078 const char *buff
, size_t size
)
2080 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2081 struct usb_composite_dev
*cdev
= dev
->cdev
;
2082 struct android_usb_function
*f
;
2089 mutex_lock(&dev
->mutex
);
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 */
2095 sscanf(buff
, "%d", &enabled
);
2096 if (enabled
&& !dev
->enabled
) {
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.
2105 cdev
->next_string_id
= 0x10;
2108 * Update values in composite driver's copy of
2109 * device descriptor.
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
;
2118 /* special case for meta mode */
2119 if (serial_string
[0] == 0x0) {
2120 cdev
->desc
.iSerialNumber
= 0;
2122 cdev
->desc
.iSerialNumber
= device_desc
.iSerialNumber
;
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
);
2130 android_enable(dev
);
2131 dev
->enabled
= true;
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 */
2137 } else if (!enabled
&& dev
->enabled
) {
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 */
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
);
2150 dev
->enabled
= false;
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__
);
2159 /* Add for HW/SW connect */
2162 mutex_unlock(&dev
->mutex
);
2166 static ssize_t
state_show(struct device
*pdev
, struct device_attribute
*attr
,
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
;
2177 spin_lock_irqsave(&cdev
->lock
, flags
);
2179 state
= "CONFIGURED";
2180 else if (dev
->connected
)
2181 state
= "CONNECTED";
2182 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2184 return sprintf(buf
, "%s\n", state
);
2187 #define DESCRIPTOR_ATTR(field, format_string) \
2189 field ## _show(struct device *dev, struct device_attribute *attr, \
2192 return sprintf(buf, format_string, device_desc.field); \
2195 field ## _store(struct device *dev, struct device_attribute *attr, \
2196 const char *buf, size_t size) \
2199 if (sscanf(buf, format_string, &value) == 1) { \
2200 device_desc.field = value; \
2205 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2207 #define DESCRIPTOR_STRING_ATTR(field, buffer) \
2209 field ## _show(struct device *dev, struct device_attribute *attr, \
2212 return sprintf(buf, "%s", buffer); \
2215 field ## _store(struct device *dev, struct device_attribute *attr, \
2216 const char *buf, size_t size) \
2218 if (size >= sizeof(buffer)) \
2220 if (sscanf(buf, "%s", buffer) == 1) { \
2225 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
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
)
2238 static DEVICE_ATTR(functions
, S_IRUGO
| S_IWUSR
, functions_show
,
2240 static DEVICE_ATTR(enable
, S_IRUGO
| S_IWUSR
, enable_show
, enable_store
);
2241 static DEVICE_ATTR(state
, S_IRUGO
, state_show
, NULL
);
2243 static struct device_attribute
*android_usb_attributes
[] = {
2245 &dev_attr_idProduct
,
2246 &dev_attr_bcdDevice
,
2247 &dev_attr_bDeviceClass
,
2248 &dev_attr_bDeviceSubClass
,
2249 &dev_attr_bDeviceProtocol
,
2250 &dev_attr_iManufacturer
,
2253 &dev_attr_functions
,
2259 /*-------------------------------------------------------------------------*/
2260 /* Composite driver */
2262 static int android_bind_config(struct usb_configuration
*c
)
2264 struct android_dev
*dev
= _android_dev
;
2267 ret
= android_bind_enabled_functions(dev
, c
);
2274 static void android_unbind_config(struct usb_configuration
*c
)
2276 struct android_dev
*dev
= _android_dev
;
2278 android_unbind_enabled_functions(dev
, c
);
2281 static int android_setup_config(struct usb_configuration
*c
, const struct usb_ctrlrequest
*ctrl
)
2283 int handled
= -EINVAL
;
2284 const u8 recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
2286 pr_debug("%s bRequestType=%x, bRequest=%x, recip=%x\n", __func__
, ctrl
->bRequestType
, ctrl
->bRequest
, recip
);
2288 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
2289 switch (ctrl
->bRequest
)
2291 case USB_REQ_CLEAR_FEATURE
:
2294 case USB_RECIP_DEVICE
:
2295 switch (ctrl
->wValue
)
2297 case USB_DEVICE_U1_ENABLE
:
2299 pr_debug("Clear Feature->U1 Enable\n");
2302 case USB_DEVICE_U2_ENABLE
:
2304 pr_debug("Clear Feature->U2 Enable\n");
2318 case USB_REQ_SET_FEATURE
:
2321 case USB_RECIP_DEVICE
:
2322 switch (ctrl
->wValue
)
2324 case USB_DEVICE_U1_ENABLE
:
2325 pr_debug("Set Feature->U1 Enable\n");
2328 case USB_DEVICE_U2_ENABLE
:
2329 pr_debug("Set Feature->U2 Enable\n");
2353 static int android_bind(struct usb_composite_dev
*cdev
)
2355 struct android_dev
*dev
= _android_dev
;
2356 struct usb_gadget
*gadget
= cdev
->gadget
;
2360 * Start disconnected. Userspace will connect the gadget once
2361 * it is done configuring the functions.
2363 usb_gadget_disconnect(gadget
);
2365 ret
= android_init_functions(dev
->functions
, cdev
);
2369 /* Allocate string descriptor numbers ... note that string
2370 * contents can be overridden by the composite_dev glue.
2372 id
= usb_string_id(cdev
);
2375 strings_dev
[STRING_MANUFACTURER_IDX
].id
= id
;
2376 device_desc
.iManufacturer
= id
;
2378 id
= usb_string_id(cdev
);
2381 strings_dev
[STRING_PRODUCT_IDX
].id
= id
;
2382 device_desc
.iProduct
= id
;
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);
2389 id
= usb_string_id(cdev
);
2392 strings_dev
[STRING_SERIAL_IDX
].id
= id
;
2393 device_desc
.iSerialNumber
= id
;
2395 #ifdef CONFIG_USBIF_COMPLIANCE
2396 usb_gadget_clear_selfpowered(gadget
);
2398 usb_gadget_set_selfpowered(gadget
);
2405 static int android_usb_unbind(struct usb_composite_dev
*cdev
)
2407 struct android_dev
*dev
= _android_dev
;
2409 cancel_work_sync(&dev
->work
);
2410 android_cleanup_functions(dev
->functions
);
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
);
2419 android_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*c
)
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
;
2428 pr_debug("[XLOG_VERBOSE][USB]%s\n", __func__
);
2431 req
->complete
= composite_setup_complete
;
2433 gadget
->ep0
->driver_data
= cdev
;
2435 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
) {
2436 if (f
->ctrlrequest
) {
2437 value
= f
->ctrlrequest(f
, cdev
, c
);
2443 /* Special case the accessory function.
2444 * It needs to handle control requests before it is enabled.
2447 value
= acc_ctrlrequest(cdev
, c
);
2450 value
= composite_setup_func(gadget
, c
);
2452 spin_lock_irqsave(&cdev
->lock
, flags
);
2453 if (!dev
->connected
) {
2455 schedule_work(&dev
->work
);
2456 } else if (c
->bRequest
== USB_REQ_SET_CONFIGURATION
&&
2458 schedule_work(&dev
->work
);
2460 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2465 static void android_disconnect(struct usb_composite_dev
*cdev
)
2467 struct android_dev
*dev
= _android_dev
;
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 */
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.
2480 schedule_work(&dev
->work
);
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 */
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
2497 .max_speed
= USB_SPEED_HIGH
2501 static int android_create_device(struct android_dev
*dev
)
2503 struct device_attribute
**attrs
= android_usb_attributes
;
2504 struct device_attribute
*attr
;
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
);
2512 dev_set_drvdata(dev
->dev
, dev
);
2514 while ((attr
= *attrs
++)) {
2515 err
= device_create_file(dev
->dev
, attr
);
2517 device_destroy(android_class
, dev
->dev
->devt
);
2524 #ifdef CONFIG_USBIF_COMPLIANCE
2526 #include <linux/proc_fs.h>
2527 #include <asm/uaccess.h>
2528 #include <linux/seq_file.h>
2531 static int andoid_usbif_driver_on
= 0 ;
2534 static int android_start(void)
2536 struct android_dev
*dev
;
2539 pr_debug("android_start ===> \n");
2541 err
= usb_composite_probe(&android_usb_driver
);
2543 pr_err("%s: failed to probe driver %d", __func__
, err
);
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
;
2550 pr_debug("android_start <=== \n");
2555 static int android_stop(void)
2557 pr_debug("android_stop ===> \n");
2559 usb_composite_unregister(&android_usb_driver
);
2561 pr_debug("android_stop <=== \n");
2564 static int andoid_usbif_proc_show(struct seq_file
*seq
, void *v
)
2566 seq_printf(seq
, "andoid_usbif_proc_show, andoid_usbif_driver_on is %d (on:1, off:0)\n", andoid_usbif_driver_on
);
2570 static int andoid_usbif_proc_open(struct inode
*inode
, struct file
*file
)
2572 return single_open(file
, andoid_usbif_proc_show
, inode
->i_private
);
2575 static ssize_t
andoid_usbif_proc_write(struct file
*file
, const char __user
*buf
, size_t length
, loff_t
*ppos
)
2581 struct device
*dev
;
2583 struct resource
*iomem
;
2586 void __iomem
*ctrl_base
;
2588 if (length
>= sizeof(msg
)) {
2589 pr_debug("andoid_usbif_proc_write length error, the error len is %d\n", (unsigned int)length
);
2592 if (copy_from_user(msg
, buf
, length
))
2597 pr_debug("andoid_usbif_proc_write: %s, current driver on/off: %d\n", msg
, andoid_usbif_driver_on
);
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");
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 ;
2611 pr_debug("stop usb android driver <=== \n");
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
,
2622 .llseek
= seq_lseek
,
2626 static int __init
init(void)
2628 struct android_dev
*dev
;
2630 struct proc_dir_entry
*prEntry
;
2632 android_class
= class_create(THIS_MODULE
, "android_usb");
2633 if (IS_ERR(android_class
))
2634 return PTR_ERR(android_class
);
2636 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
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
);
2648 err
= android_create_device(dev
);
2650 pr_err("%s: failed to create android device %d", __func__
, err
);
2656 prEntry
= proc_create("android_usbif_init", 0666, NULL
, &andoid_usbif_proc_fops
);
2660 printk("create the android_usbif_init proc OK!\n") ;
2662 printk("[ERROR] create the android_usbif_init proc FAIL\n") ;
2665 // set android up at boot up
2667 andoid_usbif_driver_on
= 1 ;
2674 class_destroy(android_class
);
2678 late_initcall(init
);
2682 static void __exit
cleanup(void)
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") ;
2690 module_exit(cleanup
);
2693 static int __init
init(void)
2695 struct android_dev
*dev
;
2698 android_class
= class_create(THIS_MODULE
, "android_usb");
2699 if (IS_ERR(android_class
))
2700 return PTR_ERR(android_class
);
2702 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
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
);
2714 err
= android_create_device(dev
);
2716 pr_err("%s: failed to create android device %d", __func__
, err
);
2722 err
= usb_composite_probe(&android_usb_driver
);
2724 pr_err("%s: failed to probe driver %d", __func__
, err
);
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
;
2735 device_destroy(android_class
, dev
->dev
->devt
);
2739 class_destroy(android_class
);
2742 late_initcall(init
);
2744 static void __exit
cleanup(void)
2746 usb_composite_unregister(&android_usb_driver
);
2747 class_destroy(android_class
);
2748 kfree(_android_dev
);
2749 _android_dev
= NULL
;
2751 module_exit(cleanup
);