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
;
305 struct android_dev
*dev
;
308 static int ffs_function_init(struct android_usb_function
*f
,
309 struct usb_composite_dev
*cdev
)
311 f
->config
= kzalloc(sizeof(struct functionfs_config
), GFP_KERNEL
);
315 return functionfs_init();
318 static void ffs_function_cleanup(struct android_usb_function
*f
)
320 functionfs_cleanup();
324 static void ffs_function_enable(struct android_usb_function
*f
)
326 struct android_dev
*dev
= _android_dev
;
327 struct functionfs_config
*config
= f
->config
;
329 config
->enabled
= true;
331 /* Disable the gadget until the function is ready */
333 android_disable(dev
);
336 static void ffs_function_disable(struct android_usb_function
*f
)
338 struct android_dev
*dev
= _android_dev
;
339 struct functionfs_config
*config
= f
->config
;
341 config
->enabled
= false;
343 /* Balance the disable that was called in closed_callback */
348 static int ffs_function_bind_config(struct android_usb_function
*f
,
349 struct usb_configuration
*c
)
351 struct functionfs_config
*config
= f
->config
;
352 return functionfs_bind_config(c
->cdev
, c
, config
->data
);
356 ffs_aliases_show(struct device
*pdev
, struct device_attribute
*attr
, char *buf
)
358 struct android_dev
*dev
= _android_dev
;
361 mutex_lock(&dev
->mutex
);
362 ret
= sprintf(buf
, "%s\n", dev
->ffs_aliases
);
363 mutex_unlock(&dev
->mutex
);
369 ffs_aliases_store(struct device
*pdev
, struct device_attribute
*attr
,
370 const char *buf
, size_t size
)
372 struct android_dev
*dev
= _android_dev
;
375 mutex_lock(&dev
->mutex
);
378 mutex_unlock(&dev
->mutex
);
382 strlcpy(buff
, buf
, sizeof(buff
));
383 strlcpy(dev
->ffs_aliases
, strim(buff
), sizeof(dev
->ffs_aliases
));
385 mutex_unlock(&dev
->mutex
);
390 static DEVICE_ATTR(aliases
, S_IRUGO
| S_IWUSR
, ffs_aliases_show
,
392 static struct device_attribute
*ffs_function_attributes
[] = {
397 static struct android_usb_function ffs_function
= {
399 .init
= ffs_function_init
,
400 .enable
= ffs_function_enable
,
401 .disable
= ffs_function_disable
,
402 .cleanup
= ffs_function_cleanup
,
403 .bind_config
= ffs_function_bind_config
,
404 .attributes
= ffs_function_attributes
,
407 static int functionfs_ready_callback(struct ffs_data
*ffs
)
409 struct android_dev
*dev
= _android_dev
;
410 struct functionfs_config
*config
= ffs_function
.config
;
413 /* dev is null in case ADB is not in the composition */
415 mutex_lock(&dev
->mutex
);
416 ret
= functionfs_bind(ffs
, dev
->cdev
);
418 mutex_unlock(&dev
->mutex
);
422 /* android ffs_func requires daemon to start only after enable*/
423 pr_debug("start adbd only in ADB composition\n");
428 config
->opened
= true;
429 /* Save dev in case the adb function will get disabled */
435 mutex_unlock(&dev
->mutex
);
439 static void functionfs_closed_callback(struct ffs_data
*ffs
)
441 struct android_dev
*dev
= _android_dev
;
442 struct functionfs_config
*config
= ffs_function
.config
;
445 * In case new composition is without ADB or ADB got disabled by the
446 * time ffs_daemon was stopped then use saved one
451 /* fatal-error: It should never happen */
453 pr_err("adb_closed_callback: config->dev is NULL");
456 mutex_lock(&dev
->mutex
);
458 if (config
->enabled
&& dev
)
459 android_disable(dev
);
463 config
->opened
= false;
466 functionfs_unbind(ffs
);
469 mutex_unlock(&dev
->mutex
);
472 static void *functionfs_acquire_dev_callback(const char *dev_name
)
477 static void functionfs_release_dev_callback(struct ffs_data
*ffs_data
)
487 adb_function_init(struct android_usb_function
*f
,
488 struct usb_composite_dev
*cdev
)
490 f
->config
= kzalloc(sizeof(struct adb_data
), GFP_KERNEL
);
497 static void adb_function_cleanup(struct android_usb_function
*f
)
504 adb_function_bind_config(struct android_usb_function
*f
,
505 struct usb_configuration
*c
)
507 return adb_bind_config(c
);
510 static void adb_android_function_enable(struct android_usb_function
*f
)
512 /* This patch cause WHQL fail */
514 struct android_dev
*dev
= _android_dev
;
515 struct adb_data
*data
= f
->config
;
517 data
->enabled
= true;
519 /* Disable the gadget until adbd is ready */
521 android_disable(dev
);
525 static void adb_android_function_disable(struct android_usb_function
*f
)
527 /* This patch cause WHQL fail */
529 struct android_dev
*dev
= _android_dev
;
530 struct adb_data
*data
= f
->config
;
532 data
->enabled
= false;
534 /* Balance the disable that was called in closed_callback */
540 static struct android_usb_function adb_function
= {
542 .enable
= adb_android_function_enable
,
543 .disable
= adb_android_function_disable
,
544 .init
= adb_function_init
,
545 .cleanup
= adb_function_cleanup
,
546 .bind_config
= adb_function_bind_config
,
549 static void adb_ready_callback(void)
551 /* This patch cause WHQL fail */
553 struct android_dev
*dev
= _android_dev
;
554 struct adb_data
*data
= adb_function
.config
;
556 mutex_lock(&dev
->mutex
);
563 mutex_unlock(&dev
->mutex
);
567 static void adb_closed_callback(void)
569 /* This patch cause WHQL fail */
571 struct android_dev
*dev
= _android_dev
;
572 struct adb_data
*data
= adb_function
.config
;
574 mutex_lock(&dev
->mutex
);
576 data
->opened
= false;
579 android_disable(dev
);
581 mutex_unlock(&dev
->mutex
);
585 #define MAX_ACM_INSTANCES 4
586 struct acm_function_config
{
589 struct usb_function
*f_acm
[MAX_ACM_INSTANCES
];
590 struct usb_function_instance
*f_acm_inst
[MAX_ACM_INSTANCES
];
591 int port_index
[MAX_ACM_INSTANCES
];
592 int port_index_on
[MAX_ACM_INSTANCES
];
596 acm_function_init(struct android_usb_function
*f
,
597 struct usb_composite_dev
*cdev
)
601 struct acm_function_config
*config
;
602 #ifdef CONFIG_USBIF_COMPLIANCE
603 /* FIXME, USB_IF workaround */
607 config
= kzalloc(sizeof(struct acm_function_config
), GFP_KERNEL
);
612 for (i
= 0; i
< MAX_ACM_INSTANCES
; i
++) {
613 config
->f_acm_inst
[i
] = usb_get_function_instance("acm");
614 if (IS_ERR(config
->f_acm_inst
[i
])) {
615 ret
= PTR_ERR(config
->f_acm_inst
[i
]);
616 goto err_usb_get_function_instance
;
618 config
->f_acm
[i
] = usb_get_function(config
->f_acm_inst
[i
]);
619 if (IS_ERR(config
->f_acm
[i
])) {
620 ret
= PTR_ERR(config
->f_acm
[i
]);
621 goto err_usb_get_function
;
625 err_usb_get_function_instance
:
626 pr_err("Could not usb_get_function_instance() %d\n", i
);
628 usb_put_function(config
->f_acm
[i
]);
629 err_usb_get_function
:
630 pr_err("Could not usb_get_function() %d\n", i
);
631 usb_put_function_instance(config
->f_acm_inst
[i
]);
636 static void acm_function_cleanup(struct android_usb_function
*f
)
639 struct acm_function_config
*config
= f
->config
;
640 #ifdef CONFIG_USBIF_COMPLIANCE
641 /* FIXME, USB_IF workaround */
645 for (i
= 0; i
< MAX_ACM_INSTANCES
; i
++) {
646 usb_put_function(config
->f_acm
[i
]);
647 usb_put_function_instance(config
->f_acm_inst
[i
]);
654 acm_function_bind_config(struct android_usb_function
*f
,
655 struct usb_configuration
*c
)
659 struct acm_function_config
*config
= f
->config
;
661 /*1st:Modem, 2nd:Modem, 3rd:BT, 4th:MD logger*/
662 for (i
= 0; i
< MAX_ACM_INSTANCES
; i
++) {
663 if(config
->port_index
[i
] != 0) {
664 ret
= usb_add_function(c
, config
->f_acm
[i
]);
666 pr_err("Could not bind acm%u config\n", i
);
667 goto err_usb_add_function
;
669 pr_info("%s Open /dev/ttyGS%d\n", __func__
, i
);
670 config
->port_index
[i
] = 0;
671 config
->port_index_on
[i
] = 1;
672 config
->instances
= 0;
676 config
->instances_on
= config
->instances
;
677 for (i
= 0; i
< config
->instances_on
; i
++) {
678 ret
= usb_add_function(c
, config
->f_acm
[i
]);
680 pr_err("Could not bind acm%u config\n", i
);
681 goto err_usb_add_function
;
683 pr_info("%s Open /dev/ttyGS%d\n", __func__
, i
);
688 err_usb_add_function
:
690 usb_remove_function(c
, config
->f_acm
[i
]);
694 static void acm_function_unbind_config(struct android_usb_function
*f
,
695 struct usb_configuration
*c
)
699 //struct acm_function_config *config = f->config;
702 * list_del(&f->list) and f->unbind() are called at unbind_config().
703 * f->disable() is called at enable_store.
704 * So directly return this function.
705 * If does NOT return, f->list is deleted twice and cuased KE.
707 * What does the original kernel code look likes?
713 if (config->instances_on != 0) {
714 for (i = 0; i < config->instances_on; i++) {
715 pr_debug("%s f_acm[%d]=%p\n", __func__, i, config->f_acm[i]);
716 usb_remove_function(c, config->f_acm[i]);
719 for (i = 0; i < MAX_ACM_INSTANCES; i++)
720 pr_debug("%s port_index_on=%d\n", __func__, config->port_index_on[i]);
721 if (config->port_index_on[i] != 0) {
722 usb_remove_function(c, config->f_acm[i]);
728 static ssize_t
acm_instances_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\n", config
->instances
);
736 static ssize_t
acm_instances_store(struct device
*dev
,
737 struct device_attribute
*attr
, const char *buf
, size_t size
)
739 struct android_usb_function
*f
= dev_get_drvdata(dev
);
740 struct acm_function_config
*config
= f
->config
;
743 sscanf(buf
, "%d", &value
);
744 if (value
> MAX_ACM_INSTANCES
)
745 value
= MAX_ACM_INSTANCES
;
746 config
->instances
= value
;
750 static DEVICE_ATTR(instances
, S_IRUGO
| S_IWUSR
, acm_instances_show
,
751 acm_instances_store
);
753 static ssize_t
acm_port_index_show(struct device
*dev
,
754 struct device_attribute
*attr
, char *buf
)
756 struct android_usb_function
*f
= dev_get_drvdata(dev
);
757 struct acm_function_config
*config
= f
->config
;
758 return sprintf(buf
, "%d,%d,%d,%d\n", config
->port_index
[0], config
->port_index
[1],
759 config
->port_index
[2], config
->port_index
[3]);
762 static ssize_t
acm_port_index_store(struct device
*dev
,
763 struct device_attribute
*attr
, const char *buf
, size_t size
)
765 struct android_usb_function
*f
= dev_get_drvdata(dev
);
766 struct acm_function_config
*config
= f
->config
;
767 int val
[MAX_ACM_INSTANCES
]={0};
771 num
= sscanf(buf
, "%d,%d,%d,%d", &(val
[0]), &(val
[1]), &(val
[2]), &(val
[3]));
773 pr_debug("%s [0]=%d,[1]=%d,[2]=%d,[3]=%d, num=%d\n", __func__
, val
[0], val
[1], \
774 val
[2], val
[3], num
);
776 /* Set all port_index as 0*/
777 for (tmp
= 0; tmp
< MAX_ACM_INSTANCES
; tmp
++)
778 config
->port_index
[tmp
] = 0;
780 for (tmp
= 0; tmp
< num
; tmp
++) {
781 int port
= (val
[tmp
] > MAX_ACM_INSTANCES
|| val
[tmp
] < 1) ? 0 : val
[tmp
]-1;
782 config
->port_index
[port
] = 1;
788 static DEVICE_ATTR(port_index
, S_IRUGO
| S_IWUSR
, acm_port_index_show
,
789 acm_port_index_store
);
791 static struct device_attribute
*acm_function_attributes
[] = {
793 &dev_attr_port_index
, /*Only open the specific port*/
797 static struct android_usb_function acm_function
= {
799 .init
= acm_function_init
,
800 .cleanup
= acm_function_cleanup
,
801 .bind_config
= acm_function_bind_config
,
802 .unbind_config
= acm_function_unbind_config
,
803 .attributes
= acm_function_attributes
,
806 #ifdef CONFIG_USB_F_LOOPBACK
808 #define MAX_LOOPBACK_INSTANCES 1
810 struct loopback_function_config
{
812 struct usb_function
*f_lp
[MAX_LOOPBACK_INSTANCES
];
813 struct usb_function_instance
*f_lp_inst
[MAX_LOOPBACK_INSTANCES
];
816 static int loopback_function_init(struct android_usb_function
*f
, struct usb_composite_dev
*cdev
)
820 struct loopback_function_config
*config
;
822 config
= kzalloc(sizeof(struct loopback_function_config
), GFP_KERNEL
);
827 for (i
= 0; i
< MAX_LOOPBACK_INSTANCES
; i
++) {
828 config
->f_lp_inst
[i
] = usb_get_function_instance("loopback");
829 if (IS_ERR(config
->f_lp_inst
[i
])) {
830 ret
= PTR_ERR(config
->f_lp_inst
[i
]);
831 goto err_usb_get_function_instance
;
833 config
->f_lp
[i
] = usb_get_function(config
->f_lp_inst
[i
]);
834 if (IS_ERR(config
->f_lp
[i
])) {
835 ret
= PTR_ERR(config
->f_lp
[i
]);
836 goto err_usb_get_function
;
840 err_usb_get_function_instance
:
841 pr_err("Could not usb_get_function_instance() %d\n", i
);
843 usb_put_function(config
->f_lp
[i
]);
844 err_usb_get_function
:
845 pr_err("Could not usb_get_function() %d\n", i
);
846 usb_put_function_instance(config
->f_lp_inst
[i
]);
851 static void loopback_function_cleanup(struct android_usb_function
*f
)
854 struct loopback_function_config
*config
= f
->config
;
856 for (i
= 0; i
< MAX_LOOPBACK_INSTANCES
; i
++) {
857 usb_put_function(config
->f_lp
[i
]);
858 usb_put_function_instance(config
->f_lp_inst
[i
]);
865 loopback_function_bind_config(struct android_usb_function
*f
, struct usb_configuration
*c
)
869 struct loopback_function_config
*config
= f
->config
;
871 ret
= usb_add_function(c
, config
->f_lp
[config
->port_num
]);
873 pr_err("Could not bind loopback%u config\n", config
->port_num
);
874 goto err_usb_add_function
;
876 pr_info("%s Open loopback\n", __func__
);
880 err_usb_add_function
:
882 usb_remove_function(c
, config
->f_lp
[i
]);
886 static struct android_usb_function loopback_function
= {
888 .init
= loopback_function_init
,
889 .cleanup
= loopback_function_cleanup
,
890 .bind_config
= loopback_function_bind_config
,
894 #define MAX_SERIAL_INSTANCES 4
896 struct serial_function_config
{
898 struct usb_function
*f_ser
[MAX_SERIAL_INSTANCES
];
899 struct usb_function_instance
*f_ser_inst
[MAX_SERIAL_INSTANCES
];
902 static int serial_function_init(struct android_usb_function
*f
, struct usb_composite_dev
*cdev
)
906 struct serial_function_config
*config
;
907 #ifdef CONFIG_USBIF_COMPLIANCE
908 /* FIXME, USB_IF workaround */
912 config
= kzalloc(sizeof(struct serial_function_config
), GFP_KERNEL
);
917 for (i
= 0; i
< MAX_SERIAL_INSTANCES
; i
++) {
918 config
->f_ser_inst
[i
] = usb_get_function_instance("gser");
919 if (IS_ERR(config
->f_ser_inst
[i
])) {
920 ret
= PTR_ERR(config
->f_ser_inst
[i
]);
921 goto err_usb_get_function_instance
;
923 config
->f_ser
[i
] = usb_get_function(config
->f_ser_inst
[i
]);
924 if (IS_ERR(config
->f_ser
[i
])) {
925 ret
= PTR_ERR(config
->f_ser
[i
]);
926 goto err_usb_get_function
;
930 err_usb_get_function_instance
:
931 pr_err("Could not usb_get_function_instance() %d\n", i
);
933 usb_put_function(config
->f_ser
[i
]);
934 err_usb_get_function
:
935 pr_err("Could not usb_get_function() %d\n", i
);
936 usb_put_function_instance(config
->f_ser_inst
[i
]);
941 static void serial_function_cleanup(struct android_usb_function
*f
)
944 struct serial_function_config
*config
= f
->config
;
945 #ifdef CONFIG_USBIF_COMPLIANCE
946 /* FIXME, USB_IF workaround */
950 for (i
= 0; i
< MAX_SERIAL_INSTANCES
; i
++) {
951 usb_put_function(config
->f_ser
[i
]);
952 usb_put_function_instance(config
->f_ser_inst
[i
]);
959 serial_function_bind_config(struct android_usb_function
*f
, struct usb_configuration
*c
)
963 struct serial_function_config
*config
= f
->config
;
965 ret
= usb_add_function(c
, config
->f_ser
[config
->port_num
]);
967 pr_err("Could not bind ser%u config\n", config
->port_num
);
968 goto err_usb_add_function
;
970 pr_info("%s Open /dev/ttyGS%d\n", __func__
, i
);
974 err_usb_add_function
:
976 usb_remove_function(c
, config
->f_ser
[i
]);
980 static ssize_t
serial_port_show(struct device
*dev
,
981 struct device_attribute
*attr
, char *buf
)
983 struct android_usb_function
*f
= dev_get_drvdata(dev
);
984 struct serial_function_config
*config
= f
->config
;
985 return sprintf(buf
, "%d\n", config
->port_num
);
988 static ssize_t
serial_port_store(struct device
*dev
,
989 struct device_attribute
*attr
, const char *buf
, size_t size
)
991 struct android_usb_function
*f
= dev_get_drvdata(dev
);
992 struct serial_function_config
*config
= f
->config
;
995 sscanf(buf
, "%d", &value
);
996 if (value
> MAX_SERIAL_INSTANCES
)
997 value
= MAX_SERIAL_INSTANCES
;
998 config
->port_num
= value
;
1002 static DEVICE_ATTR(port
, S_IRUGO
| S_IWUSR
, serial_port_show
, serial_port_store
);
1003 static struct device_attribute
*serial_function_attributes
[] = { &dev_attr_port
, NULL
};
1005 static struct android_usb_function serial_function
= {
1007 .init
= serial_function_init
,
1008 .cleanup
= serial_function_cleanup
,
1009 .bind_config
= serial_function_bind_config
,
1010 .attributes
= serial_function_attributes
,
1014 mtp_function_init(struct android_usb_function
*f
,
1015 struct usb_composite_dev
*cdev
)
1020 static void mtp_function_cleanup(struct android_usb_function
*f
)
1026 mtp_function_bind_config(struct android_usb_function
*f
,
1027 struct usb_configuration
*c
)
1029 return mtp_bind_config(c
, false);
1033 ptp_function_init(struct android_usb_function
*f
,
1034 struct usb_composite_dev
*cdev
)
1036 /* nothing to do - initialization is handled by mtp_function_init */
1040 static void ptp_function_cleanup(struct android_usb_function
*f
)
1042 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1046 ptp_function_bind_config(struct android_usb_function
*f
,
1047 struct usb_configuration
*c
)
1049 return mtp_bind_config(c
, true);
1052 static int mtp_function_ctrlrequest(struct android_usb_function
*f
,
1053 struct usb_composite_dev
*cdev
,
1054 const struct usb_ctrlrequest
*c
)
1056 /* MTP MSFT OS Descriptor */
1057 struct android_dev
*dev
= _android_dev
;
1058 struct android_usb_function
*f_count
;
1060 char usb_function_string
[32];
1061 char * buff
= usb_function_string
;
1063 list_for_each_entry(f_count
, &dev
->enabled_functions
, enabled_list
)
1066 buff
+= sprintf(buff
, "%s,", f_count
->name
);
1070 mtp_read_usb_functions(functions_no
, usb_function_string
);
1071 return mtp_ctrlrequest(cdev
, c
);
1074 static int ptp_function_ctrlrequest(struct android_usb_function
*f
,
1075 struct usb_composite_dev
*cdev
,
1076 const struct usb_ctrlrequest
*c
)
1078 return ptp_ctrlrequest(cdev
, c
);
1082 static struct android_usb_function mtp_function
= {
1084 .init
= mtp_function_init
,
1085 .cleanup
= mtp_function_cleanup
,
1086 .bind_config
= mtp_function_bind_config
,
1087 .ctrlrequest
= mtp_function_ctrlrequest
,
1090 /* PTP function is same as MTP with slightly different interface descriptor */
1091 static struct android_usb_function ptp_function
= {
1093 .init
= ptp_function_init
,
1094 .cleanup
= ptp_function_cleanup
,
1095 .bind_config
= ptp_function_bind_config
,
1096 .ctrlrequest
= ptp_function_ctrlrequest
, //ALPS01832160
1099 struct ecm_function_config
{
1100 u8 ethaddr
[ETH_ALEN
];
1101 struct eth_dev
*dev
;
1104 static int ecm_function_init(struct android_usb_function
*f
, struct usb_composite_dev
*cdev
)
1106 f
->config
= kzalloc(sizeof(struct ecm_function_config
), GFP_KERNEL
);
1112 static void ecm_function_cleanup(struct android_usb_function
*f
)
1119 ecm_function_bind_config(struct android_usb_function
*f
,
1120 struct usb_configuration
*c
)
1123 struct eth_dev
*dev
;
1124 struct ecm_function_config
*ecm
= f
->config
;
1127 pr_err("%s: ecm_pdata\n", __func__
);
1132 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__
,
1133 ecm
->ethaddr
[0], ecm
->ethaddr
[1], ecm
->ethaddr
[2],
1134 ecm
->ethaddr
[3], ecm
->ethaddr
[4], ecm
->ethaddr
[5]);
1136 dev
= gether_setup_name(c
->cdev
->gadget
, ecm
->ethaddr
, "rndis");
1139 pr_err("%s: gether_setup failed\n", __func__
);
1144 return ecm_bind_config(c
, ecm
->ethaddr
, ecm
->dev
);
1147 static void ecm_function_unbind_config(struct android_usb_function
*f
,
1148 struct usb_configuration
*c
)
1150 struct ecm_function_config
*ecm
= f
->config
;
1151 gether_cleanup(ecm
->dev
);
1154 static ssize_t
ecm_ethaddr_show(struct device
*dev
,
1155 struct device_attribute
*attr
, char *buf
)
1157 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1158 struct ecm_function_config
*ecm
= f
->config
;
1159 return sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1160 ecm
->ethaddr
[0], ecm
->ethaddr
[1], ecm
->ethaddr
[2],
1161 ecm
->ethaddr
[3], ecm
->ethaddr
[4], ecm
->ethaddr
[5]);
1164 static ssize_t
ecm_ethaddr_store(struct device
*dev
,
1165 struct device_attribute
*attr
, const char *buf
, size_t size
)
1167 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1168 struct ecm_function_config
*ecm
= f
->config
;
1170 if (sscanf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1171 (int *)&ecm
->ethaddr
[0], (int *)&ecm
->ethaddr
[1],
1172 (int *)&ecm
->ethaddr
[2], (int *)&ecm
->ethaddr
[3],
1173 (int *)&ecm
->ethaddr
[4], (int *)&ecm
->ethaddr
[5]) == 6)
1178 static DEVICE_ATTR(ecm_ethaddr
, S_IRUGO
| S_IWUSR
, ecm_ethaddr_show
,
1181 static struct device_attribute
*ecm_function_attributes
[] = {
1182 &dev_attr_ecm_ethaddr
,
1186 static struct android_usb_function ecm_function
= {
1188 .init
= ecm_function_init
,
1189 .cleanup
= ecm_function_cleanup
,
1190 .bind_config
= ecm_function_bind_config
,
1191 .unbind_config
= ecm_function_unbind_config
,
1192 .attributes
= ecm_function_attributes
,
1195 struct eem_function_config
{
1196 u8 ethaddr
[ETH_ALEN
];
1197 char manufacturer
[256];
1198 struct eth_dev
*dev
;
1202 eem_function_init(struct android_usb_function
*f
,
1203 struct usb_composite_dev
*cdev
)
1205 f
->config
= kzalloc(sizeof(struct eem_function_config
), GFP_KERNEL
);
1211 static void eem_function_cleanup(struct android_usb_function
*f
)
1218 eem_function_bind_config(struct android_usb_function
*f
,
1219 struct usb_configuration
*c
)
1222 struct eth_dev
*dev
;
1223 struct eem_function_config
*eem
= f
->config
;
1225 pr_debug("[XLOG_DEBUG][USB]%s: \n", __func__
);
1228 pr_err("%s: rndis_pdata\n", __func__
);
1232 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__
,
1233 eem
->ethaddr
[0], eem
->ethaddr
[1], eem
->ethaddr
[2],
1234 eem
->ethaddr
[3], eem
->ethaddr
[4], eem
->ethaddr
[5]);
1236 dev
= gether_setup_name(c
->cdev
->gadget
, eem
->ethaddr
, "rndis");
1239 pr_err("%s: gether_setup failed\n", __func__
);
1244 return eem_bind_config(c
, eem
->dev
);
1247 static void eem_function_unbind_config(struct android_usb_function
*f
,
1248 struct usb_configuration
*c
)
1250 struct eem_function_config
*eem
= f
->config
;
1251 gether_cleanup(eem
->dev
);
1254 static ssize_t
eem_ethaddr_show(struct device
*dev
,
1255 struct device_attribute
*attr
, char *buf
)
1257 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1258 struct eem_function_config
*eem
= f
->config
;
1259 return sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1260 eem
->ethaddr
[0], eem
->ethaddr
[1], eem
->ethaddr
[2],
1261 eem
->ethaddr
[3], eem
->ethaddr
[4], eem
->ethaddr
[5]);
1264 static ssize_t
eem_ethaddr_store(struct device
*dev
,
1265 struct device_attribute
*attr
, const char *buf
, size_t size
)
1267 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1268 struct eem_function_config
*eem
= f
->config
;
1270 if (sscanf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1271 (int *)&eem
->ethaddr
[0], (int *)&eem
->ethaddr
[1],
1272 (int *)&eem
->ethaddr
[2], (int *)&eem
->ethaddr
[3],
1273 (int *)&eem
->ethaddr
[4], (int *)&eem
->ethaddr
[5]) == 6)
1278 static DEVICE_ATTR(eem_ethaddr
, S_IRUGO
| S_IWUSR
, eem_ethaddr_show
,
1281 static struct device_attribute
*eem_function_attributes
[] = {
1282 &dev_attr_eem_ethaddr
,
1286 static struct android_usb_function eem_function
= {
1288 .init
= eem_function_init
,
1289 .cleanup
= eem_function_cleanup
,
1290 .bind_config
= eem_function_bind_config
,
1291 .unbind_config
= eem_function_unbind_config
,
1292 .attributes
= eem_function_attributes
,
1295 struct rndis_function_config
{
1296 u8 ethaddr
[ETH_ALEN
];
1298 char manufacturer
[256];
1299 /* "Wireless" RNDIS; auto-detected by Windows */
1301 struct eth_dev
*dev
;
1305 rndis_function_init(struct android_usb_function
*f
,
1306 struct usb_composite_dev
*cdev
)
1308 f
->config
= kzalloc(sizeof(struct rndis_function_config
), GFP_KERNEL
);
1314 static void rndis_function_cleanup(struct android_usb_function
*f
)
1321 rndis_function_bind_config(struct android_usb_function
*f
,
1322 struct usb_configuration
*c
)
1325 struct eth_dev
*dev
;
1326 struct rndis_function_config
*rndis
= f
->config
;
1328 pr_debug("[XLOG_DEBUG][USB]%s: \n", __func__
);
1331 pr_err("%s: rndis_pdata\n", __func__
);
1335 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__
,
1336 rndis
->ethaddr
[0], rndis
->ethaddr
[1], rndis
->ethaddr
[2],
1337 rndis
->ethaddr
[3], rndis
->ethaddr
[4], rndis
->ethaddr
[5]);
1339 dev
= gether_setup_name(c
->cdev
->gadget
, rndis
->ethaddr
, "rndis");
1342 pr_err("%s: gether_setup failed\n", __func__
);
1348 /* "Wireless" RNDIS; auto-detected by Windows */
1349 rndis_iad_descriptor
.bFunctionClass
=
1350 USB_CLASS_WIRELESS_CONTROLLER
;
1351 rndis_iad_descriptor
.bFunctionSubClass
= 0x01;
1352 rndis_iad_descriptor
.bFunctionProtocol
= 0x03;
1353 rndis_control_intf
.bInterfaceClass
=
1354 USB_CLASS_WIRELESS_CONTROLLER
;
1355 rndis_control_intf
.bInterfaceSubClass
= 0x01;
1356 rndis_control_intf
.bInterfaceProtocol
= 0x03;
1359 return rndis_bind_config_vendor(c
, rndis
->ethaddr
, rndis
->vendorID
,
1360 rndis
->manufacturer
, rndis
->dev
);
1363 static void rndis_function_unbind_config(struct android_usb_function
*f
,
1364 struct usb_configuration
*c
)
1366 struct rndis_function_config
*rndis
= f
->config
;
1367 gether_cleanup(rndis
->dev
);
1370 static ssize_t
rndis_manufacturer_show(struct device
*dev
,
1371 struct device_attribute
*attr
, char *buf
)
1373 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1374 struct rndis_function_config
*config
= f
->config
;
1375 return sprintf(buf
, "%s\n", config
->manufacturer
);
1378 static ssize_t
rndis_manufacturer_store(struct device
*dev
,
1379 struct device_attribute
*attr
, const char *buf
, size_t size
)
1381 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1382 struct rndis_function_config
*config
= f
->config
;
1384 if (size
>= sizeof(config
->manufacturer
))
1386 if (sscanf(buf
, "%s", config
->manufacturer
) == 1)
1391 static DEVICE_ATTR(manufacturer
, S_IRUGO
| S_IWUSR
, rndis_manufacturer_show
,
1392 rndis_manufacturer_store
);
1394 static ssize_t
rndis_wceis_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
*config
= f
->config
;
1399 return sprintf(buf
, "%d\n", config
->wceis
);
1402 static ssize_t
rndis_wceis_store(struct device
*dev
,
1403 struct device_attribute
*attr
, const char *buf
, size_t size
)
1405 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1406 struct rndis_function_config
*config
= f
->config
;
1409 if (sscanf(buf
, "%d", &value
) == 1) {
1410 config
->wceis
= value
;
1416 static DEVICE_ATTR(wceis
, S_IRUGO
| S_IWUSR
, rndis_wceis_show
,
1419 static ssize_t
rndis_ethaddr_show(struct device
*dev
,
1420 struct device_attribute
*attr
, char *buf
)
1422 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1423 struct rndis_function_config
*rndis
= f
->config
;
1424 return sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1425 rndis
->ethaddr
[0], rndis
->ethaddr
[1], rndis
->ethaddr
[2],
1426 rndis
->ethaddr
[3], rndis
->ethaddr
[4], rndis
->ethaddr
[5]);
1429 static ssize_t
rndis_ethaddr_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
*rndis
= f
->config
;
1435 if (sscanf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1436 (int *)&rndis
->ethaddr
[0], (int *)&rndis
->ethaddr
[1],
1437 (int *)&rndis
->ethaddr
[2], (int *)&rndis
->ethaddr
[3],
1438 (int *)&rndis
->ethaddr
[4], (int *)&rndis
->ethaddr
[5]) == 6)
1443 static DEVICE_ATTR(ethaddr
, S_IRUGO
| S_IWUSR
, rndis_ethaddr_show
,
1444 rndis_ethaddr_store
);
1446 static ssize_t
rndis_vendorID_show(struct device
*dev
,
1447 struct device_attribute
*attr
, char *buf
)
1449 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1450 struct rndis_function_config
*config
= f
->config
;
1451 return sprintf(buf
, "%04x\n", config
->vendorID
);
1454 static ssize_t
rndis_vendorID_store(struct device
*dev
,
1455 struct device_attribute
*attr
, const char *buf
, size_t size
)
1457 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1458 struct rndis_function_config
*config
= f
->config
;
1461 if (sscanf(buf
, "%04x", &value
) == 1) {
1462 config
->vendorID
= value
;
1468 static DEVICE_ATTR(vendorID
, S_IRUGO
| S_IWUSR
, rndis_vendorID_show
,
1469 rndis_vendorID_store
);
1471 static struct device_attribute
*rndis_function_attributes
[] = {
1472 &dev_attr_manufacturer
,
1479 static struct android_usb_function rndis_function
= {
1481 .init
= rndis_function_init
,
1482 .cleanup
= rndis_function_cleanup
,
1483 .bind_config
= rndis_function_bind_config
,
1484 .unbind_config
= rndis_function_unbind_config
,
1485 .attributes
= rndis_function_attributes
,
1489 struct mass_storage_function_config
{
1490 struct fsg_config fsg
;
1491 struct fsg_common
*common
;
1494 static int mass_storage_function_init(struct android_usb_function
*f
,
1495 struct usb_composite_dev
*cdev
)
1497 struct mass_storage_function_config
*config
;
1498 struct fsg_common
*common
;
1502 config
= kzalloc(sizeof(struct mass_storage_function_config
),
1507 #ifdef CONFIG_MTK_MULTI_STORAGE_SUPPORT
1508 #define LUN_MULTI (1)
1510 #define LUN_MULTI (0)
1513 #ifdef CONFIG_MTK_SHARED_SDCARD
1514 #define LUN_SHARED_SD (-1)
1516 #define LUN_SHARED_SD (0)
1519 #ifdef CONFIG_MTK_ICUSB_SUPPORT
1520 #define LUN_ICUSB (1)
1522 #define LUN_ICUSB (0)
1525 #define LUN_NUM LUN_MULTI + LUN_SHARED_SD + LUN_ICUSB + 1
1527 config
->fsg
.nluns
= LUN_NUM
;
1529 for(i
= 0; i
< config
->fsg
.nluns
; i
++) {
1530 config
->fsg
.luns
[i
].removable
= 1;
1531 config
->fsg
.luns
[i
].nofua
= 1;
1534 common
= fsg_common_init(NULL
, cdev
, &config
->fsg
);
1535 if (IS_ERR(common
)) {
1537 return PTR_ERR(common
);
1540 err
= sysfs_create_link(&f
->dev
->kobj
,
1541 &common
->luns
[0].dev
.kobj
,
1549 * "i" starts from "1", cuz dont want to change the naming of
1550 * the original path of "lun0".
1552 for(i
= 1; i
< config
->fsg
.nluns
; i
++) {
1553 char string_lun
[5]={0};
1555 sprintf(string_lun
, "lun%d",i
);
1557 err
= sysfs_create_link(&f
->dev
->kobj
,
1558 &common
->luns
[i
].dev
.kobj
,
1566 config
->common
= common
;
1571 static void mass_storage_function_cleanup(struct android_usb_function
*f
)
1577 static int mass_storage_function_bind_config(struct android_usb_function
*f
,
1578 struct usb_configuration
*c
)
1580 struct mass_storage_function_config
*config
= f
->config
;
1581 return fsg_bind_config(c
->cdev
, c
, config
->common
);
1584 static ssize_t
mass_storage_inquiry_show(struct device
*dev
,
1585 struct device_attribute
*attr
, char *buf
)
1587 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1588 struct mass_storage_function_config
*config
= f
->config
;
1589 return sprintf(buf
, "%s\n", config
->common
->inquiry_string
);
1592 static ssize_t
mass_storage_inquiry_store(struct device
*dev
,
1593 struct device_attribute
*attr
, const char *buf
, size_t size
)
1595 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1596 struct mass_storage_function_config
*config
= f
->config
;
1597 if (size
>= sizeof(config
->common
->inquiry_string
))
1599 if (sscanf(buf
, "%s", config
->common
->inquiry_string
) != 1)
1604 static DEVICE_ATTR(inquiry_string
, S_IRUGO
| S_IWUSR
,
1605 mass_storage_inquiry_show
,
1606 mass_storage_inquiry_store
);
1608 #ifdef CONFIG_MTK_BICR_SUPPORT
1610 static ssize_t
mass_storage_bicr_show(struct device
*dev
,
1611 struct device_attribute
*attr
, char *buf
)
1613 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1614 struct mass_storage_function_config
*config
= f
->config
;
1615 return sprintf(buf
, "%d\n", config
->common
->bicr
);
1618 static ssize_t
mass_storage_bicr_store(struct device
*dev
,
1619 struct device_attribute
*attr
, const char *buf
, size_t size
)
1621 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1622 struct mass_storage_function_config
*config
= f
->config
;
1623 if (size
>= sizeof(config
->common
->bicr
))
1625 if (sscanf(buf
, "%d", &config
->common
->bicr
) != 1)
1628 /* Set Lun[0] is a CDROM when enable bicr.*/
1629 if (!strcmp(buf
, "1"))
1630 config
->common
->luns
[0].cdrom
= 1;
1632 /*Reset the value. Clean the cdrom's parameters*/
1633 config
->common
->luns
[0].cdrom
= 0;
1634 config
->common
->luns
[0].blkbits
= 0;
1635 config
->common
->luns
[0].blksize
= 0;
1636 config
->common
->luns
[0].num_sectors
= 0;
1642 static DEVICE_ATTR(bicr
, S_IRUGO
| S_IWUSR
,
1643 mass_storage_bicr_show
,
1644 mass_storage_bicr_store
);
1648 static struct device_attribute
*mass_storage_function_attributes
[] = {
1649 &dev_attr_inquiry_string
,
1650 #ifdef CONFIG_MTK_BICR_SUPPORT
1656 static struct android_usb_function mass_storage_function
= {
1657 .name
= "mass_storage",
1658 .init
= mass_storage_function_init
,
1659 .cleanup
= mass_storage_function_cleanup
,
1660 .bind_config
= mass_storage_function_bind_config
,
1661 .attributes
= mass_storage_function_attributes
,
1665 static int accessory_function_init(struct android_usb_function
*f
,
1666 struct usb_composite_dev
*cdev
)
1671 static void accessory_function_cleanup(struct android_usb_function
*f
)
1676 static int accessory_function_bind_config(struct android_usb_function
*f
,
1677 struct usb_configuration
*c
)
1679 return acc_bind_config(c
);
1682 static int accessory_function_ctrlrequest(struct android_usb_function
*f
,
1683 struct usb_composite_dev
*cdev
,
1684 const struct usb_ctrlrequest
*c
)
1686 return acc_ctrlrequest(cdev
, c
);
1689 static struct android_usb_function accessory_function
= {
1690 .name
= "accessory",
1691 .init
= accessory_function_init
,
1692 .cleanup
= accessory_function_cleanup
,
1693 .bind_config
= accessory_function_bind_config
,
1694 .ctrlrequest
= accessory_function_ctrlrequest
,
1697 static int audio_source_function_init(struct android_usb_function
*f
,
1698 struct usb_composite_dev
*cdev
)
1700 struct audio_source_config
*config
;
1702 config
= kzalloc(sizeof(struct audio_source_config
), GFP_KERNEL
);
1706 config
->device
= -1;
1711 static void audio_source_function_cleanup(struct android_usb_function
*f
)
1716 static int audio_source_function_bind_config(struct android_usb_function
*f
,
1717 struct usb_configuration
*c
)
1719 struct audio_source_config
*config
= f
->config
;
1721 return audio_source_bind_config(c
, config
);
1724 static void audio_source_function_unbind_config(struct android_usb_function
*f
,
1725 struct usb_configuration
*c
)
1727 struct audio_source_config
*config
= f
->config
;
1730 config
->device
= -1;
1733 static ssize_t
audio_source_pcm_show(struct device
*dev
,
1734 struct device_attribute
*attr
, char *buf
)
1736 struct android_usb_function
*f
= dev_get_drvdata(dev
);
1737 struct audio_source_config
*config
= f
->config
;
1739 /* print PCM card and device numbers */
1740 return sprintf(buf
, "%d %d\n", config
->card
, config
->device
);
1743 static DEVICE_ATTR(pcm
, S_IRUGO
, audio_source_pcm_show
, NULL
);
1745 static struct device_attribute
*audio_source_function_attributes
[] = {
1750 #ifdef CONFIG_EVDO_DT_SUPPORT
1751 static int rawbulk_function_init(struct android_usb_function
*f
,
1752 struct usb_composite_dev
*cdev
)
1757 static void rawbulk_function_cleanup(struct android_usb_function
*f
)
1762 static int rawbulk_function_bind_config(struct android_usb_function
*f
,
1763 struct usb_configuration
*c
)
1765 char *i
= f
->name
+ strlen("via_");
1766 if (!strncmp(i
, "modem", 5))
1767 return rawbulk_bind_config(c
, RAWBULK_TID_MODEM
);
1768 else if (!strncmp(i
, "ets", 3))
1769 return rawbulk_bind_config(c
, RAWBULK_TID_ETS
);
1770 else if (!strncmp(i
, "atc", 3))
1771 return rawbulk_bind_config(c
, RAWBULK_TID_AT
);
1772 else if (!strncmp(i
, "pcv", 3))
1773 return rawbulk_bind_config(c
, RAWBULK_TID_PCV
);
1774 else if (!strncmp(i
, "gps", 3))
1775 return rawbulk_bind_config(c
, RAWBULK_TID_GPS
);
1779 static int rawbulk_function_modem_ctrlrequest(struct android_usb_function
*f
,
1780 struct usb_composite_dev
*cdev
,
1781 const struct usb_ctrlrequest
*c
)
1783 if ((c
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
&&
1784 (c
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_VENDOR
) {
1785 struct rawbulk_function
*fn
= rawbulk_lookup_function(RAWBULK_TID_MODEM
);
1786 return rawbulk_function_setup(&fn
->function
, c
);
1791 static struct android_usb_function rawbulk_modem_function
= {
1792 .name
= "via_modem",
1793 .init
= rawbulk_function_init
,
1794 .cleanup
= rawbulk_function_cleanup
,
1795 .bind_config
= rawbulk_function_bind_config
,
1796 .ctrlrequest
= rawbulk_function_modem_ctrlrequest
,
1799 static struct android_usb_function rawbulk_ets_function
= {
1801 .init
= rawbulk_function_init
,
1802 .cleanup
= rawbulk_function_cleanup
,
1803 .bind_config
= rawbulk_function_bind_config
,
1806 static struct android_usb_function rawbulk_atc_function
= {
1808 .init
= rawbulk_function_init
,
1809 .cleanup
= rawbulk_function_cleanup
,
1810 .bind_config
= rawbulk_function_bind_config
,
1813 static struct android_usb_function rawbulk_pcv_function
= {
1815 .init
= rawbulk_function_init
,
1816 .cleanup
= rawbulk_function_cleanup
,
1817 .bind_config
= rawbulk_function_bind_config
,
1820 static struct android_usb_function rawbulk_gps_function
= {
1822 .init
= rawbulk_function_init
,
1823 .cleanup
= rawbulk_function_cleanup
,
1824 .bind_config
= rawbulk_function_bind_config
,
1828 static struct android_usb_function audio_source_function
= {
1829 .name
= "audio_source",
1830 .init
= audio_source_function_init
,
1831 .cleanup
= audio_source_function_cleanup
,
1832 .bind_config
= audio_source_function_bind_config
,
1833 .unbind_config
= audio_source_function_unbind_config
,
1834 .attributes
= audio_source_function_attributes
,
1837 static struct android_usb_function
*supported_functions
[] = {
1843 #ifndef CONFIG_USBIF_COMPLIANCE
1849 &mass_storage_function
,
1850 &accessory_function
,
1851 &audio_source_function
,
1852 #ifdef CONFIG_EVDO_DT_SUPPORT
1853 &rawbulk_modem_function
,
1854 &rawbulk_ets_function
,
1855 &rawbulk_atc_function
,
1856 &rawbulk_pcv_function
,
1857 &rawbulk_gps_function
,
1859 #ifdef CONFIG_USB_F_LOOPBACK
1866 static int android_init_functions(struct android_usb_function
**functions
,
1867 struct usb_composite_dev
*cdev
)
1869 struct android_dev
*dev
= _android_dev
;
1870 struct android_usb_function
*f
;
1871 struct device_attribute
**attrs
;
1872 struct device_attribute
*attr
;
1875 #ifdef CONFIG_USBIF_COMPLIANCE
1881 for (; (f
= *functions
++); index
++) {
1882 f
->dev_name
= kasprintf(GFP_KERNEL
, "f_%s", f
->name
);
1883 /* Added for USB Develpment debug, more log for more debuging help */
1884 pr_debug("[XLOG_INFO][USB]%s: f->dev_name = %s, f->name = %s\n", __func__
, f
->dev_name
, f
->name
);
1885 /* Added for USB Develpment debug, more log for more debuging help */
1886 f
->dev
= device_create(android_class
, dev
->dev
,
1887 MKDEV(0, index
), f
, f
->dev_name
);
1888 if (IS_ERR(f
->dev
)) {
1889 pr_err("%s: Failed to create dev %s", __func__
,
1891 err
= PTR_ERR(f
->dev
);
1896 err
= f
->init(f
, cdev
);
1898 pr_err("%s: Failed to init %s", __func__
,
1902 pr_debug("[XLOG_INFO][USB]%s: init %s success!!\n", __func__
, f
->name
);
1906 attrs
= f
->attributes
;
1908 while ((attr
= *attrs
++) && !err
)
1909 err
= device_create_file(f
->dev
, attr
);
1912 pr_err("%s: Failed to create function %s attributes",
1920 device_destroy(android_class
, f
->dev
->devt
);
1926 static void android_cleanup_functions(struct android_usb_function
**functions
)
1928 struct android_usb_function
*f
;
1930 while (*functions
) {
1934 device_destroy(android_class
, f
->dev
->devt
);
1944 android_bind_enabled_functions(struct android_dev
*dev
,
1945 struct usb_configuration
*c
)
1947 struct android_usb_function
*f
;
1950 /* Added for USB Develpment debug, more log for more debuging help */
1951 pr_debug("[XLOG_INFO][USB]%s: ", __func__
);
1952 /* Added for USB Develpment debug, more log for more debuging help */
1954 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
) {
1955 pr_debug("[XLOG_INFO][USB]bind_config function '%s'/%p\n", f
->name
, f
);
1956 ret
= f
->bind_config(f
, c
);
1958 pr_err("%s: %s failed", __func__
, f
->name
);
1966 android_unbind_enabled_functions(struct android_dev
*dev
,
1967 struct usb_configuration
*c
)
1969 struct android_usb_function
*f
;
1971 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
) {
1972 pr_debug("[XLOG_INFO][USB]unbind_config function '%s'/%p\n", f
->name
, f
);
1973 if (f
->unbind_config
)
1974 f
->unbind_config(f
, c
);
1978 static int android_enable_function(struct android_dev
*dev
, char *name
)
1980 struct android_usb_function
**functions
= dev
->functions
;
1981 struct android_usb_function
*f
;
1982 while ((f
= *functions
++)) {
1984 /* Added for USB Develpment debug, more log for more debuging help */
1985 pr_debug("[XLOG_INFO][USB]%s: name = %s, f->name=%s \n", __func__
, name
, f
->name
);
1986 /* Added for USB Develpment debug, more log for more debuging help */
1987 if (!strcmp(name
, f
->name
)) {
1988 list_add_tail(&f
->enabled_list
,
1989 &dev
->enabled_functions
);
1996 /*-------------------------------------------------------------------------*/
1997 /* /sys/class/android_usb/android%d/ interface */
2000 functions_show(struct device
*pdev
, struct device_attribute
*attr
, char *buf
)
2002 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2003 struct android_usb_function
*f
;
2006 /* Added for USB Develpment debug, more log for more debuging help */
2007 pr_debug("[XLOG_INFO][USB]%s: ", __func__
);
2008 /* Added for USB Develpment debug, more log for more debuging help */
2010 mutex_lock(&dev
->mutex
);
2012 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
)
2013 buff
+= sprintf(buff
, "%s,", f
->name
);
2015 mutex_unlock(&dev
->mutex
);
2023 functions_store(struct device
*pdev
, struct device_attribute
*attr
,
2024 const char *buff
, size_t size
)
2026 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2029 char aliases
[256], *a
;
2032 int ffs_enabled
= 0;
2034 mutex_lock(&dev
->mutex
);
2037 mutex_unlock(&dev
->mutex
);
2041 INIT_LIST_HEAD(&dev
->enabled_functions
);
2043 /* Added for USB Develpment debug, more log for more debuging help */
2044 pr_debug("[XLOG_INFO][USB]%s: \n", __func__
);
2045 /* Added for USB Develpment debug, more log for more debuging help */
2047 strlcpy(buf
, buff
, sizeof(buf
));
2051 name
= strsep(&b
, ",");
2053 /* Added for USB Develpment debug, more log for more debuging help */
2054 pr_debug("[XLOG_INFO][USB]%s: name = %s \n", __func__
, name
);
2055 /* Added for USB Develpment debug, more log for more debuging help */
2061 strlcpy(aliases
, dev
->ffs_aliases
, sizeof(aliases
));
2065 char *alias
= strsep(&a
, ",");
2066 if (alias
&& !strcmp(name
, alias
)) {
2075 err
= android_enable_function(dev
, "ffs");
2077 pr_err("android_usb: Cannot enable ffs (%d)",
2084 err
= android_enable_function(dev
, name
);
2086 pr_err("android_usb: Cannot enable '%s' (%d)",
2090 mutex_unlock(&dev
->mutex
);
2095 static ssize_t
enable_show(struct device
*pdev
, struct device_attribute
*attr
,
2098 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2099 return sprintf(buf
, "%d\n", dev
->enabled
);
2102 static ssize_t
enable_store(struct device
*pdev
, struct device_attribute
*attr
,
2103 const char *buff
, size_t size
)
2105 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2106 struct usb_composite_dev
*cdev
= dev
->cdev
;
2107 struct android_usb_function
*f
;
2114 mutex_lock(&dev
->mutex
);
2116 /* Added for USB Develpment debug, more log for more debuging help */
2117 pr_debug("[XLOG_INFO][USB]%s: device_attr->attr.name: %s\n", __func__
, attr
->attr
.name
);
2118 /* Added for USB Develpment debug, more log for more debuging help */
2120 sscanf(buff
, "%d", &enabled
);
2121 if (enabled
&& !dev
->enabled
) {
2123 * Reset next_string_id to 0 before enabling the gadget driver.
2124 * Otherwise, after a large number of enable/disable cycles,
2125 * function bind will fail because we cannot allocate new string ids.
2126 * String ids cannot be larger than 254 per USB spec.
2127 * 0~15 are reserved for usb device descriptor
2128 * 16~254 are for functions.
2130 cdev
->next_string_id
= 0x10;
2133 * Update values in composite driver's copy of
2134 * device descriptor.
2136 cdev
->desc
.idVendor
= device_desc
.idVendor
;
2137 cdev
->desc
.idProduct
= device_desc
.idProduct
;
2138 cdev
->desc
.bcdDevice
= device_desc
.bcdDevice
;
2139 cdev
->desc
.bDeviceClass
= device_desc
.bDeviceClass
;
2140 cdev
->desc
.bDeviceSubClass
= device_desc
.bDeviceSubClass
;
2141 cdev
->desc
.bDeviceProtocol
= device_desc
.bDeviceProtocol
;
2143 /* special case for meta mode */
2144 if (serial_string
[0] == 0x0) {
2145 cdev
->desc
.iSerialNumber
= 0;
2147 cdev
->desc
.iSerialNumber
= device_desc
.iSerialNumber
;
2150 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
) {
2151 pr_debug("[XLOG_INFO][USB]enable function '%s'/%p\n", f
->name
, f
);
2155 android_enable(dev
);
2156 dev
->enabled
= true;
2158 /* Added for USB Develpment debug, more log for more debuging help */
2159 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
);
2160 /* Added for USB Develpment debug, more log for more debuging help */
2162 } else if (!enabled
&& dev
->enabled
) {
2164 /* Added for USB Develpment debug, more log for more debuging help */
2165 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
);
2166 /* Added for USB Develpment debug, more log for more debuging help */
2168 android_disable(dev
);
2169 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
) {
2170 pr_debug("[XLOG_INFO][USB]disable function '%s'/%p\n", f
->name
, f
);
2175 dev
->enabled
= false;
2177 pr_err("android_usb: already %s\n",
2178 dev
->enabled
? "enabled" : "disabled");
2179 /* Add for HW/SW connect */
2180 if (!usb_cable_connected()) {
2181 schedule_work(&dev
->work
);
2182 pr_debug("[XLOG_VERBOSE][USB]%s: enable 0->0 case - no usb cable", __func__
);
2184 /* Add for HW/SW connect */
2187 mutex_unlock(&dev
->mutex
);
2191 static ssize_t
state_show(struct device
*pdev
, struct device_attribute
*attr
,
2194 struct android_dev
*dev
= dev_get_drvdata(pdev
);
2195 struct usb_composite_dev
*cdev
= dev
->cdev
;
2196 char *state
= "DISCONNECTED";
2197 unsigned long flags
;
2202 spin_lock_irqsave(&cdev
->lock
, flags
);
2204 state
= "CONFIGURED";
2205 else if (dev
->connected
)
2206 state
= "CONNECTED";
2207 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2209 return sprintf(buf
, "%s\n", state
);
2212 #define DESCRIPTOR_ATTR(field, format_string) \
2214 field ## _show(struct device *dev, struct device_attribute *attr, \
2217 return sprintf(buf, format_string, device_desc.field); \
2220 field ## _store(struct device *dev, struct device_attribute *attr, \
2221 const char *buf, size_t size) \
2224 if (sscanf(buf, format_string, &value) == 1) { \
2225 device_desc.field = value; \
2230 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2232 #define DESCRIPTOR_STRING_ATTR(field, buffer) \
2234 field ## _show(struct device *dev, struct device_attribute *attr, \
2237 return sprintf(buf, "%s", buffer); \
2240 field ## _store(struct device *dev, struct device_attribute *attr, \
2241 const char *buf, size_t size) \
2243 if (size >= sizeof(buffer)) \
2245 if (sscanf(buf, "%s", buffer) == 1) { \
2250 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2253 DESCRIPTOR_ATTR(idVendor
, "%04x\n")
2254 DESCRIPTOR_ATTR(idProduct
, "%04x\n")
2255 DESCRIPTOR_ATTR(bcdDevice
, "%04x\n")
2256 DESCRIPTOR_ATTR(bDeviceClass
, "%d\n")
2257 DESCRIPTOR_ATTR(bDeviceSubClass
, "%d\n")
2258 DESCRIPTOR_ATTR(bDeviceProtocol
, "%d\n")
2259 DESCRIPTOR_STRING_ATTR(iManufacturer
, manufacturer_string
)
2260 DESCRIPTOR_STRING_ATTR(iProduct
, product_string
)
2261 DESCRIPTOR_STRING_ATTR(iSerial
, serial_string
)
2263 static DEVICE_ATTR(functions
, S_IRUGO
| S_IWUSR
, functions_show
,
2265 static DEVICE_ATTR(enable
, S_IRUGO
| S_IWUSR
, enable_show
, enable_store
);
2266 static DEVICE_ATTR(state
, S_IRUGO
, state_show
, NULL
);
2268 static struct device_attribute
*android_usb_attributes
[] = {
2270 &dev_attr_idProduct
,
2271 &dev_attr_bcdDevice
,
2272 &dev_attr_bDeviceClass
,
2273 &dev_attr_bDeviceSubClass
,
2274 &dev_attr_bDeviceProtocol
,
2275 &dev_attr_iManufacturer
,
2278 &dev_attr_functions
,
2284 /*-------------------------------------------------------------------------*/
2285 /* Composite driver */
2287 static int android_bind_config(struct usb_configuration
*c
)
2289 struct android_dev
*dev
= _android_dev
;
2292 ret
= android_bind_enabled_functions(dev
, c
);
2299 static void android_unbind_config(struct usb_configuration
*c
)
2301 struct android_dev
*dev
= _android_dev
;
2303 android_unbind_enabled_functions(dev
, c
);
2306 static int android_setup_config(struct usb_configuration
*c
, const struct usb_ctrlrequest
*ctrl
)
2308 int handled
= -EINVAL
;
2309 const u8 recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
2311 pr_debug("%s bRequestType=%x, bRequest=%x, recip=%x\n", __func__
, ctrl
->bRequestType
, ctrl
->bRequest
, recip
);
2313 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
2314 switch (ctrl
->bRequest
)
2316 case USB_REQ_CLEAR_FEATURE
:
2319 case USB_RECIP_DEVICE
:
2320 switch (ctrl
->wValue
)
2322 case USB_DEVICE_U1_ENABLE
:
2324 pr_debug("Clear Feature->U1 Enable\n");
2327 case USB_DEVICE_U2_ENABLE
:
2329 pr_debug("Clear Feature->U2 Enable\n");
2343 case USB_REQ_SET_FEATURE
:
2346 case USB_RECIP_DEVICE
:
2347 switch (ctrl
->wValue
)
2349 case USB_DEVICE_U1_ENABLE
:
2350 pr_debug("Set Feature->U1 Enable\n");
2353 case USB_DEVICE_U2_ENABLE
:
2354 pr_debug("Set Feature->U2 Enable\n");
2378 static int android_bind(struct usb_composite_dev
*cdev
)
2380 struct android_dev
*dev
= _android_dev
;
2381 struct usb_gadget
*gadget
= cdev
->gadget
;
2385 * Start disconnected. Userspace will connect the gadget once
2386 * it is done configuring the functions.
2388 usb_gadget_disconnect(gadget
);
2390 ret
= android_init_functions(dev
->functions
, cdev
);
2394 /* Allocate string descriptor numbers ... note that string
2395 * contents can be overridden by the composite_dev glue.
2397 id
= usb_string_id(cdev
);
2400 strings_dev
[STRING_MANUFACTURER_IDX
].id
= id
;
2401 device_desc
.iManufacturer
= id
;
2403 id
= usb_string_id(cdev
);
2406 strings_dev
[STRING_PRODUCT_IDX
].id
= id
;
2407 device_desc
.iProduct
= id
;
2409 /* Default strings - should be updated by userspace */
2410 strncpy(manufacturer_string
, MANUFACTURER_STRING
, sizeof(manufacturer_string
) - 1);
2411 strncpy(product_string
, PRODUCT_STRING
, sizeof(product_string
) - 1);
2412 strncpy(serial_string
, "0123456789ABCDEF", sizeof(serial_string
) - 1);
2414 id
= usb_string_id(cdev
);
2417 strings_dev
[STRING_SERIAL_IDX
].id
= id
;
2418 device_desc
.iSerialNumber
= id
;
2420 #ifdef CONFIG_USBIF_COMPLIANCE
2421 usb_gadget_clear_selfpowered(gadget
);
2423 usb_gadget_set_selfpowered(gadget
);
2430 static int android_usb_unbind(struct usb_composite_dev
*cdev
)
2432 struct android_dev
*dev
= _android_dev
;
2434 cancel_work_sync(&dev
->work
);
2435 android_cleanup_functions(dev
->functions
);
2439 /* HACK: android needs to override setup for accessory to work */
2440 static int (*composite_setup_func
)(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*c
);
2441 extern void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
);
2444 android_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*c
)
2446 struct android_dev
*dev
= _android_dev
;
2447 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2448 struct usb_request
*req
= cdev
->req
;
2449 struct android_usb_function
*f
;
2450 int value
= -EOPNOTSUPP
;
2451 unsigned long flags
;
2453 pr_debug("[XLOG_VERBOSE][USB]%s\n", __func__
);
2456 req
->complete
= composite_setup_complete
;
2458 gadget
->ep0
->driver_data
= cdev
;
2460 list_for_each_entry(f
, &dev
->enabled_functions
, enabled_list
) {
2461 if (f
->ctrlrequest
) {
2462 value
= f
->ctrlrequest(f
, cdev
, c
);
2468 /* Special case the accessory function.
2469 * It needs to handle control requests before it is enabled.
2472 value
= acc_ctrlrequest(cdev
, c
);
2475 value
= composite_setup_func(gadget
, c
);
2477 spin_lock_irqsave(&cdev
->lock
, flags
);
2478 if (!dev
->connected
) {
2480 schedule_work(&dev
->work
);
2481 } else if (c
->bRequest
== USB_REQ_SET_CONFIGURATION
&&
2483 schedule_work(&dev
->work
);
2485 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2490 static void android_disconnect(struct usb_composite_dev
*cdev
)
2492 struct android_dev
*dev
= _android_dev
;
2494 /* Added for USB Develpment debug, more log for more debuging help */
2495 pr_debug("[XLOG_VERBOSE][USB]%s: \n", __func__
);
2496 /* Added for USB Develpment debug, more log for more debuging help */
2498 /* accessory HID support can be active while the
2499 accessory function is not actually enabled,
2500 so we need to inform it when we are disconnected.
2505 schedule_work(&dev
->work
);
2507 /* Added for USB Develpment debug, more log for more debuging help */
2508 pr_debug("[XLOG_VERBOSE][USB]%s: dev->connected = %d \n", __func__
, dev
->connected
);
2509 /* Added for USB Develpment debug, more log for more debuging help */
2512 static struct usb_composite_driver android_usb_driver
= {
2513 .name
= "android_usb",
2514 .dev
= &device_desc
,
2515 .strings
= dev_strings
,
2516 .bind
= android_bind
,
2517 .unbind
= android_usb_unbind
,
2518 .disconnect
= android_disconnect
,
2519 #ifdef CONFIG_USB_MU3D_DRV
2520 .max_speed
= USB_SPEED_SUPER
2522 .max_speed
= USB_SPEED_HIGH
2526 static int android_create_device(struct android_dev
*dev
)
2528 struct device_attribute
**attrs
= android_usb_attributes
;
2529 struct device_attribute
*attr
;
2532 dev
->dev
= device_create(android_class
, NULL
,
2533 MKDEV(0, 0), NULL
, "android0");
2534 if (IS_ERR(dev
->dev
))
2535 return PTR_ERR(dev
->dev
);
2537 dev_set_drvdata(dev
->dev
, dev
);
2539 while ((attr
= *attrs
++)) {
2540 err
= device_create_file(dev
->dev
, attr
);
2542 device_destroy(android_class
, dev
->dev
->devt
);
2549 #ifdef CONFIG_USBIF_COMPLIANCE
2551 #include <linux/proc_fs.h>
2552 #include <asm/uaccess.h>
2553 #include <linux/seq_file.h>
2556 static int andoid_usbif_driver_on
= 0 ;
2559 static int android_start(void)
2561 struct android_dev
*dev
;
2564 pr_debug("android_start ===> \n");
2566 err
= usb_composite_probe(&android_usb_driver
);
2568 pr_err("%s: failed to probe driver %d", __func__
, err
);
2571 /* HACK: exchange composite's setup with ours */
2572 composite_setup_func
= android_usb_driver
.gadget_driver
.setup
;
2573 android_usb_driver
.gadget_driver
.setup
= android_setup
;
2575 pr_debug("android_start <=== \n");
2580 static int android_stop(void)
2582 pr_debug("android_stop ===> \n");
2584 usb_composite_unregister(&android_usb_driver
);
2586 pr_debug("android_stop <=== \n");
2589 static int andoid_usbif_proc_show(struct seq_file
*seq
, void *v
)
2591 seq_printf(seq
, "andoid_usbif_proc_show, andoid_usbif_driver_on is %d (on:1, off:0)\n", andoid_usbif_driver_on
);
2595 static int andoid_usbif_proc_open(struct inode
*inode
, struct file
*file
)
2597 return single_open(file
, andoid_usbif_proc_show
, inode
->i_private
);
2600 static ssize_t
andoid_usbif_proc_write(struct file
*file
, const char __user
*buf
, size_t length
, loff_t
*ppos
)
2606 struct device
*dev
;
2608 struct resource
*iomem
;
2611 void __iomem
*ctrl_base
;
2613 if (length
>= sizeof(msg
)) {
2614 pr_debug("andoid_usbif_proc_write length error, the error len is %d\n", (unsigned int)length
);
2617 if (copy_from_user(msg
, buf
, length
))
2622 pr_debug("andoid_usbif_proc_write: %s, current driver on/off: %d\n", msg
, andoid_usbif_driver_on
);
2624 if ((msg
[0] == '1') && (andoid_usbif_driver_on
== 0)){
2625 pr_debug("start usb android driver ===> \n");
2626 printk("start usb android driver ===> \n");
2628 andoid_usbif_driver_on
= 1 ;
2629 pr_debug("start usb android driver <=== \n");
2630 }else if ((msg
[0] == '0') && (andoid_usbif_driver_on
== 1)){
2631 pr_debug("stop usb android driver ===> \n");
2632 printk("stop usb android driver ===> \n");
2633 andoid_usbif_driver_on
= 0 ;
2636 pr_debug("stop usb android driver <=== \n");
2642 static const struct file_operations andoid_usbif_proc_fops
= {
2643 .owner
= THIS_MODULE
,
2644 .open
= andoid_usbif_proc_open
,
2645 .write
= andoid_usbif_proc_write
,
2647 .llseek
= seq_lseek
,
2651 static int __init
init(void)
2653 struct android_dev
*dev
;
2655 struct proc_dir_entry
*prEntry
;
2657 android_class
= class_create(THIS_MODULE
, "android_usb");
2658 if (IS_ERR(android_class
))
2659 return PTR_ERR(android_class
);
2661 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2667 dev
->disable_depth
= 1;
2668 dev
->functions
= supported_functions
;
2669 INIT_LIST_HEAD(&dev
->enabled_functions
);
2670 INIT_WORK(&dev
->work
, android_work
);
2671 mutex_init(&dev
->mutex
);
2673 err
= android_create_device(dev
);
2675 pr_err("%s: failed to create android device %d", __func__
, err
);
2681 prEntry
= proc_create("android_usbif_init", 0666, NULL
, &andoid_usbif_proc_fops
);
2685 printk("create the android_usbif_init proc OK!\n") ;
2687 printk("[ERROR] create the android_usbif_init proc FAIL\n") ;
2690 // set android up at boot up
2692 andoid_usbif_driver_on
= 1 ;
2699 class_destroy(android_class
);
2703 late_initcall(init
);
2707 static void __exit
cleanup(void)
2709 printk("[U3D] android cleanup ===> \n") ;
2710 class_destroy(android_class
);
2711 kfree(_android_dev
);
2712 _android_dev
= NULL
;
2713 printk("[U3D] android cleanup <=== \n") ;
2715 module_exit(cleanup
);
2718 static int __init
init(void)
2720 struct android_dev
*dev
;
2723 android_class
= class_create(THIS_MODULE
, "android_usb");
2724 if (IS_ERR(android_class
))
2725 return PTR_ERR(android_class
);
2727 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2733 dev
->disable_depth
= 1;
2734 dev
->functions
= supported_functions
;
2735 INIT_LIST_HEAD(&dev
->enabled_functions
);
2736 INIT_WORK(&dev
->work
, android_work
);
2737 mutex_init(&dev
->mutex
);
2739 err
= android_create_device(dev
);
2741 pr_err("%s: failed to create android device %d", __func__
, err
);
2747 err
= usb_composite_probe(&android_usb_driver
);
2749 pr_err("%s: failed to probe driver %d", __func__
, err
);
2753 /* HACK: exchange composite's setup with ours */
2754 composite_setup_func
= android_usb_driver
.gadget_driver
.setup
;
2755 android_usb_driver
.gadget_driver
.setup
= android_setup
;
2760 device_destroy(android_class
, dev
->dev
->devt
);
2764 class_destroy(android_class
);
2767 late_initcall(init
);
2769 static void __exit
cleanup(void)
2771 usb_composite_unregister(&android_usb_driver
);
2772 class_destroy(android_class
);
2773 kfree(_android_dev
);
2774 _android_dev
= NULL
;
2776 module_exit(cleanup
);