2 * Gadget Driver for SLP based on Android
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Modified : Yongsul Oh <yongsul96.oh@samsung.com>
8 * Heavily based on android.c
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
22 /* #define VERBOSE_DEBUG */
24 #include <linux/init.h>
25 #include <linux/module.h>
28 #include <linux/delay.h>
29 #include <linux/kernel.h>
30 #include <linux/utsname.h>
31 #include <linux/platform_device.h>
32 #include <linux/mutex.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/composite.h>
36 #include <linux/usb/gadget.h>
37 #include <linux/usb/slp_multi.h>
38 #include <asm/system_info.h>
39 #include <linux/pm_qos.h>
41 #include <linux/workqueue.h>
42 #include <linux/spinlock.h>
44 #include "gadget_chips.h"
48 #include "f_mtp_slp.c"
49 #define USB_ETH_RNDIS y
50 #define USB_FRNDIS_INCLUDED y
54 #ifdef CONFIG_USB_SPRD_DWC
55 #include "f_vserial.c"
58 #define USB_MODE_VERSION "1.1"
60 MODULE_AUTHOR("Yongsul Oh <yongsul96.oh@samsung.com>");
61 MODULE_DESCRIPTION("SLP Composite USB Driver similar to Android Compiste");
62 MODULE_LICENSE("GPL");
63 MODULE_VERSION(USB_MODE_VERSION);
65 static const char slp_longname[] = "Gadget SLP";
66 #if defined(CONFIG_USB_G_ANDROID_SAMSUNG_COMPOSITE) || defined(CONFIG_USB_G_SLP)
67 static int composite_string_index;
70 /* Default vendor and product IDs, overridden by userspace */
71 #define VENDOR_ID 0x04E8 /* Samsung VID */
72 #define PRODUCT_ID 0x6860 /* KIES mode PID */
74 /* DM_PORT NUM : /dev/ttyGS* port number */
77 struct slp_multi_func_data {
79 enum slp_multi_config_id usb_config_id;
82 struct slp_multi_usb_function {
88 struct device_attribute **attributes;
90 /* for slp_multi_dev.funcs_fconf */
91 struct list_head fconf_list;
93 /* for slp_multi_dev.funcs_sconf */
94 struct list_head sconf_list;
96 /* for slp_multi_dev.available_functions */
97 struct list_head available_list;
99 /* Manndatory: initialization during gadget bind */
100 int (*init) (struct slp_multi_usb_function *,
101 struct usb_composite_dev *);
102 /* Optional: cleanup during gadget unbind */
103 void (*cleanup) (struct slp_multi_usb_function *);
104 /* Optional: called when the function is added the list of
105 * enabled functions */
106 void (*enable)(struct slp_multi_usb_function *);
107 /* Optional: called when it is removed */
108 void (*disable)(struct slp_multi_usb_function *);
110 /* Mandatory: called when the usb enabled */
111 int (*bind_config) (struct slp_multi_usb_function *,
112 struct usb_configuration *);
113 /* Optional: called when the configuration is removed */
114 void (*unbind_config) (struct slp_multi_usb_function *,
115 struct usb_configuration *);
116 /* Optional: handle ctrl requests before the device is configured */
117 int (*ctrlrequest) (struct slp_multi_usb_function *,
118 struct usb_composite_dev *,
119 const struct usb_ctrlrequest *);
122 struct slp_multi_dev {
123 struct list_head available_functions;
125 /* for each configuration control */
126 struct list_head funcs_fconf;
127 struct list_head funcs_sconf;
129 struct usb_composite_dev *cdev;
135 /* current USB state */
136 enum usb_device_state ustate;
139 /* to control DMA QOS */
143 struct pm_qos_request pm_qos_req_dma;
144 struct work_struct evt_work;
145 /* asserted events */
146 struct list_head evt_list;
147 struct mutex evt_mutex;
148 struct mutex enable_lock;
152 static unsigned slp_multi_nluns;
153 static struct class *slp_multi_class;
154 static struct slp_multi_dev *_slp_multi_dev;
155 static int slp_multi_bind_config(struct usb_configuration *c);
156 static void slp_multi_unbind_config(struct usb_configuration *c);
158 /* string IDs are assigned dynamically */
159 #define STRING_MANUFACTURER_IDX 0
160 #define STRING_PRODUCT_IDX 1
161 #define STRING_SERIAL_IDX 2
163 static char manufacturer_string[256];
164 static char product_string[256];
165 static char serial_string[256];
168 static struct usb_string strings_dev[] = {
169 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
170 [STRING_PRODUCT_IDX].s = product_string,
171 [STRING_SERIAL_IDX].s = serial_string,
175 static struct usb_gadget_strings stringtab_dev = {
176 .language = 0x0409, /* en-us */
177 .strings = strings_dev,
180 static struct usb_gadget_strings *slp_dev_strings[] = {
185 static struct usb_device_descriptor device_desc = {
186 .bLength = sizeof(device_desc),
187 .bDescriptorType = USB_DT_DEVICE,
188 .bcdUSB = __constant_cpu_to_le16(0x0200),
189 .bDeviceClass = USB_CLASS_PER_INTERFACE,
190 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
191 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
192 .bcdDevice = __constant_cpu_to_le16(0x0400),
193 .bNumConfigurations = 1,
196 static struct usb_configuration first_config_driver = {
197 .label = "slp_first_config",
198 .unbind = slp_multi_unbind_config,
199 .bConfigurationValue = USB_CONFIGURATION_1,
200 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
201 .MaxPower = 0x30, /* 96ma */
204 static struct usb_configuration second_config_driver = {
205 .label = "slp_second_config",
206 .unbind = slp_multi_unbind_config,
207 .bConfigurationValue = USB_CONFIGURATION_2,
208 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
209 .MaxPower = 0x30, /* 96ma */
212 /*-------------------------------------------------------------------------*/
213 /* Supported functions initialization */
215 static int sdb_function_init(struct slp_multi_usb_function *f,
216 struct usb_composite_dev *cdev)
218 return sdb_setup(cdev);
221 static void sdb_function_cleanup(struct slp_multi_usb_function *f)
226 static int sdb_function_bind_config(struct slp_multi_usb_function *f,
227 struct usb_configuration *c)
229 return sdb_bind_config(c);
232 static struct slp_multi_usb_function sdb_function = {
234 .init = sdb_function_init,
235 .cleanup = sdb_function_cleanup,
236 .bind_config = sdb_function_bind_config,
239 struct acm_function_config {
241 struct usb_function *f_acm[MAX_U_SERIAL_PORTS];
242 struct usb_function_instance *f_acm_inst[MAX_U_SERIAL_PORTS];
245 static int acm_function_init(struct slp_multi_usb_function *f,
246 struct usb_composite_dev *cdev)
248 struct acm_function_config *config;
251 config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
255 config->instances = 1;
257 for (i = 0; i < MAX_U_SERIAL_PORTS; i++) {
258 config->f_acm_inst[i] = usb_get_function_instance("acm");
259 if (IS_ERR(config->f_acm_inst[i])) {
260 status = PTR_ERR(config->f_acm_inst[i]);
261 goto err_usb_get_instance;
265 for (j = 0; j < MAX_U_SERIAL_PORTS; j++) {
266 config->f_acm[j] = usb_get_function(config->f_acm_inst[j]);
267 if (IS_ERR(config->f_acm[j])) {
268 status = PTR_ERR(config->f_acm[j]);
269 goto err_usb_get_function;
274 err_usb_get_function:
276 usb_put_function(config->f_acm[j]);
278 err_usb_get_instance:
280 usb_put_function_instance(config->f_acm_inst[i]);
286 static void acm_function_cleanup(struct slp_multi_usb_function *f)
289 struct acm_function_config *config = f->config;
291 for (i = 0; i < MAX_U_SERIAL_PORTS; i++) {
292 usb_put_function(config->f_acm[i]);
293 usb_put_function_instance(config->f_acm_inst[i]);
300 static int acm_function_bind_config(struct slp_multi_usb_function *f,
301 struct usb_configuration *c)
305 struct acm_function_config *config = f->config;
307 for (i = 0; i < config->instances; i++) {
308 ret = usb_add_function(c, config->f_acm[i]);
310 dev_err(f->dev, "Could not bind acm%u config\n", i);
311 goto err_usb_add_function;
317 err_usb_add_function:
319 usb_remove_function(c, config->f_acm[i]);
324 static ssize_t acm_instances_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
327 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
328 struct acm_function_config *config = f->config;
329 return snprintf(buf, PAGE_SIZE, "%d\n", config->instances);
332 static ssize_t acm_instances_store(struct device *dev,
333 struct device_attribute *attr,
334 const char *buf, size_t size)
336 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
337 struct acm_function_config *config = f->config;
340 sscanf(buf, "%d", &value);
341 if (value > MAX_U_SERIAL_PORTS)
342 value = MAX_U_SERIAL_PORTS;
343 config->instances = value;
347 static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR,
348 acm_instances_show, acm_instances_store);
349 static struct device_attribute *acm_function_attributes[] = {
350 &dev_attr_instances, NULL };
352 static struct slp_multi_usb_function acm_function = {
354 .init = acm_function_init,
355 .cleanup = acm_function_cleanup,
356 .bind_config = acm_function_bind_config,
357 .attributes = acm_function_attributes,
361 mtp_function_init(struct slp_multi_usb_function *f,
362 struct usb_composite_dev *cdev)
364 return mtp_setup(cdev);
367 static void mtp_function_cleanup(struct slp_multi_usb_function *f)
373 mtp_function_bind_config(struct slp_multi_usb_function *f,
374 struct usb_configuration *c)
376 return mtp_bind_config(c);
379 static int mtp_function_ctrlrequest(struct slp_multi_usb_function *f,
380 struct usb_composite_dev *cdev,
381 const struct usb_ctrlrequest *c)
383 struct usb_request *req = cdev->req;
384 struct usb_gadget *gadget = cdev->gadget;
385 int value = -EOPNOTSUPP;
386 u16 w_length = le16_to_cpu(c->wLength);
387 struct usb_string_descriptor *os_func_desc = req->buf;
388 char ms_descriptor[38] = {
390 /* Upper 2byte of dwLength */
394 /* wIndex, Extended compatID index */
396 /* bCount, we use only 1 function(MTP) */
399 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
401 /* First Function section for MTP */
402 /* bFirstInterfaceNumber,
403 * we always use it by 0 for MTP
406 /* RESERVED, fixed value 1 */
408 /* CompatibleID for MTP */
409 0x4D, 0x54, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
410 /* Sub-compatibleID for MTP */
411 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
413 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
416 switch (c->bRequest) {
417 /* Added handler to respond to host about MS OS Descriptors.
418 * Below handler is requirement if you use MTP.
419 * So, If you set composite included MTP,
420 * you have to respond to host about 0x54 or 0x64 request
421 * refer to following site.
422 * http://msdn.microsoft.com/en-us/windows/hardware/gg463179
426 os_func_desc->bLength = 0x28;
427 os_func_desc->bDescriptorType = 0x00;
428 value = min(w_length, (u16) (sizeof(ms_descriptor) + 2));
429 memcpy(os_func_desc->wData, &ms_descriptor, value);
433 req->zero = value < w_length;
434 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
437 cdev->req->complete(gadget->ep0, req);
446 static struct slp_multi_usb_function mtp_function = {
448 .init = mtp_function_init,
449 .cleanup = mtp_function_cleanup,
450 .bind_config = mtp_function_bind_config,
451 .ctrlrequest = mtp_function_ctrlrequest,
454 struct rndis_function_config {
455 u8 ethaddr[ETH_ALEN];
457 char manufacturer[256];
459 u8 rndis_string_defs0_id;
460 struct eth_dev *edev;
462 static char host_addr_string[18];
464 static int rndis_function_init(struct slp_multi_usb_function *f,
465 struct usb_composite_dev *cdev)
467 struct rndis_function_config *config;
470 config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
474 /* maybe allocate device-global string IDs */
475 if (rndis_string_defs[0].id == 0) {
477 /* control interface label */
478 status = usb_string_id(cdev);
480 goto rndis_init_error;
481 config->rndis_string_defs0_id = status;
482 rndis_string_defs[0].id = status;
483 rndis_control_intf.iInterface = status;
485 /* data interface label */
486 status = usb_string_id(cdev);
488 goto rndis_init_error;
489 rndis_string_defs[1].id = status;
490 rndis_data_intf.iInterface = status;
492 /* IAD iFunction label */
493 status = usb_string_id(cdev);
495 goto rndis_init_error;
496 rndis_string_defs[2].id = status;
497 rndis_iad_descriptor.iFunction = status;
500 /* create a fake MAC address from our serial number. */
501 for (i = 0; (i < 256) && serial_string[i]; i++) {
502 /* XOR the USB serial across the remaining bytes */
503 config->ethaddr[i % (ETH_ALEN - 1) + 1] ^= serial_string[i];
505 config->ethaddr[0] &= 0xfe; /* clear multicast bit */
506 config->ethaddr[0] |= 0x02; /* set local assignment bit (IEEE802) */
508 snprintf(host_addr_string, sizeof(host_addr_string),
509 "%02x:%02x:%02x:%02x:%02x:%02x",
510 config->ethaddr[0], config->ethaddr[1],
511 config->ethaddr[2], config->ethaddr[3],
512 config->ethaddr[4], config->ethaddr[5]);
522 static void rndis_function_cleanup(struct slp_multi_usb_function *f)
528 static int rndis_function_bind_config(struct slp_multi_usb_function *f,
529 struct usb_configuration *c)
532 struct rndis_function_config *rndis = f->config;
535 dev_err(f->dev, "error rndis_pdata is null\n");
539 rndis->edev = gether_setup_name(c->cdev->gadget, rndis->ethaddr,"usb");
541 if (IS_ERR(rndis->edev)) {
542 dev_err(f->dev, "gether_setup failed\n");
547 /* "Wireless" RNDIS; auto-detected by Windows */
548 rndis_iad_descriptor.bFunctionClass =
549 USB_CLASS_WIRELESS_CONTROLLER;
550 rndis_iad_descriptor.bFunctionSubClass = 0x01;
551 rndis_iad_descriptor.bFunctionProtocol = 0x03;
552 rndis_control_intf.bInterfaceClass =
553 USB_CLASS_WIRELESS_CONTROLLER;
554 rndis_control_intf.bInterfaceSubClass = 0x01;
555 rndis_control_intf.bInterfaceProtocol = 0x03;
558 /* ... and setup RNDIS itself */
561 dev_err(f->dev, "rndis_init failed(ret:%d)\n", ret);
562 gether_cleanup(rndis->edev);
566 /* Android team reset "rndis_string_defs[0].id" when RNDIS unbinded
567 * in f_rndis.c but, that makes failure of rndis_bind_config() by
568 * the overflow of "next_string_id" value in usb_string_id().
569 * So, Android team also reset "next_string_id" value in android.c
570 * but SLP does not reset "next_string_id" value. And we decided to
571 * re-update "rndis_string_defs[0].id" by old value.
572 * 20120224 yongsul96.oh@samsung.com
574 if (rndis_string_defs[0].id == 0)
575 rndis_string_defs[0].id = rndis->rndis_string_defs0_id;
577 ret = rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
578 rndis->manufacturer, rndis->edev);
581 gether_cleanup(rndis->edev);
582 dev_err(f->dev, "rndis_bind_config failed(ret:%d)\n", ret);
588 static void rndis_function_unbind_config(struct slp_multi_usb_function *f,
589 struct usb_configuration *c)
591 struct rndis_function_config *rndis = f->config;
592 gether_cleanup(rndis->edev);
595 static ssize_t rndis_manufacturer_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
598 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
599 struct rndis_function_config *config = f->config;
600 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
603 static ssize_t rndis_manufacturer_store(struct device *dev,
604 struct device_attribute *attr,
605 const char *buf, size_t size)
607 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
608 struct rndis_function_config *config = f->config;
610 if ((size >= sizeof(config->manufacturer)) ||
611 (sscanf(buf, "%s", config->manufacturer) != 1))
617 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
618 rndis_manufacturer_store);
620 static ssize_t rndis_wceis_show(struct device *dev,
621 struct device_attribute *attr, char *buf)
623 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
624 struct rndis_function_config *config = f->config;
625 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
628 static ssize_t rndis_wceis_store(struct device *dev,
629 struct device_attribute *attr, const char *buf,
632 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
633 struct rndis_function_config *config = f->config;
636 if (sscanf(buf, "%d", &value) == 1) {
637 config->wceis = value;
643 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
646 static ssize_t rndis_ethaddr_show(struct device *dev,
647 struct device_attribute *attr, char *buf)
649 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
650 struct rndis_function_config *rndis = f->config;
651 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
652 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
653 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
656 static DEVICE_ATTR(ethaddr, S_IRUGO, rndis_ethaddr_show,
659 static ssize_t rndis_vendorID_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
663 struct rndis_function_config *config = f->config;
664 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
667 static ssize_t rndis_vendorID_store(struct device *dev,
668 struct device_attribute *attr,
669 const char *buf, size_t size)
671 struct slp_multi_usb_function *f = dev_get_drvdata(dev);
672 struct rndis_function_config *config = f->config;
675 if (sscanf(buf, "%04x", &value) == 1) {
676 config->vendorID = value;
682 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
683 rndis_vendorID_store);
685 static struct device_attribute *rndis_function_attributes[] = {
686 &dev_attr_manufacturer,
693 static struct slp_multi_usb_function rndis_function = {
695 .init = rndis_function_init,
696 .cleanup = rndis_function_cleanup,
697 .bind_config = rndis_function_bind_config,
698 .unbind_config = rndis_function_unbind_config,
699 .attributes = rndis_function_attributes,
701 #ifdef CONFIG_USB_SPRD_DWC
702 #define GSER_PORT_MAX_COUNT MAX_U_SERIAL_PORTS
703 static int vser_function_init(struct slp_multi_usb_function *f,
704 struct usb_composite_dev *cdev)
709 static void vser_function_cleanup(struct slp_multi_usb_function *f)
714 static int vser_function_bind_config(struct slp_multi_usb_function *f,
715 struct usb_configuration *c)
717 return vser_bind_config(c);
720 static int vser_function_ctrlrequest(struct slp_multi_usb_function *f,
721 struct usb_composite_dev *cdev,
722 const struct usb_ctrlrequest *c)
724 return vser_setup(cdev, c);
726 static struct slp_multi_usb_function vser_function = {
728 .init = vser_function_init,
729 .cleanup = vser_function_cleanup,
730 .bind_config = vser_function_bind_config,
731 .ctrlrequest = vser_function_ctrlrequest,
734 struct gser_function_config {
736 struct usb_function *f_gser[GSER_PORT_MAX_COUNT];
737 struct usb_function_instance *f_gser_inst[GSER_PORT_MAX_COUNT];
739 static int gser_port_count=1;
740 static int gser_function_init(struct slp_multi_usb_function *f,
741 struct usb_composite_dev *cdev)
745 struct gser_function_config *config;
747 config = kzalloc(sizeof(struct gser_function_config), GFP_KERNEL);
752 for (i = 0; i < GSER_PORT_MAX_COUNT; i++) {
753 config->f_gser_inst[i] = usb_get_function_instance("gser");
754 if (IS_ERR(config->f_gser_inst[i])) {
755 ret = PTR_ERR(config->f_gser_inst[i]);
756 goto err_usb_get_function_instance;
758 config->f_gser[i] = usb_get_function(config->f_gser_inst[i]);
759 if (IS_ERR(config->f_gser[i])) {
760 ret = PTR_ERR(config->f_gser[i]);
761 goto err_usb_get_function;
765 err_usb_get_function_instance:
767 usb_put_function(config->f_gser[i]);
768 err_usb_get_function:
769 usb_put_function_instance(config->f_gser_inst[i]);
774 static void gser_function_cleanup(struct slp_multi_usb_function *f)
777 struct gser_function_config *config = f->config;
779 for (i = 0; i < GSER_PORT_MAX_COUNT; i++) {
780 usb_put_function(config->f_gser[i]);
781 usb_put_function_instance(config->f_gser_inst[i]);
787 static int gser_function_bind_config(struct slp_multi_usb_function *f,
788 struct usb_configuration *c)
792 struct gser_function_config *config = f->config;
794 config->instances_on = gser_port_count;
795 for (i = 0; i < config->instances_on; i++) {
796 ret = usb_add_function(c, config->f_gser[i]);
798 pr_err("Could not bind gser%u config\n", i);
799 goto err_usb_add_function;
805 err_usb_add_function:
807 usb_remove_function(c, config->f_gser[i]);
811 *should notice that when adb disable/enable, it will call usb_remove_config/usb_add_config
812 *and in usb_remove_config it will call unbind_config , it will also delete function list and unbind
814 *in kernel 3.4 no acm_function_unbind_config, so now we don't use gser_function_unbind_config
815 *to avoid kernel data abort. because in current s/w architecture it will delete function list two times.
817 static void gser_function_unbind_config(struct slp_multi_usb_function *f,
818 struct usb_configuration *c)
821 struct gser_function_config *config = f->config;
823 for (i = 0; i < config->instances_on; i++)
824 usb_remove_function(c, config->f_gser[i]);
827 static void gser_setup_complete(struct usb_ep *ep, struct usb_request *req)
831 static int gser_setup(struct usb_composite_dev *cdev, const struct usb_ctrlrequest *ctrl)
833 u16 w_length = le16_to_cpu(ctrl->wLength);
834 int value = -EOPNOTSUPP;
836 DBG(cdev, "%s\n", __func__);
837 /* Handle Bulk-only class-specific requests */
838 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
839 switch (ctrl->bRequest) {
846 /* respond with data transfer or status phase? */
849 cdev->req->zero = value < w_length;
850 cdev->req->length = value;
851 cdev->req->complete = gser_setup_complete;
852 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
854 printk("%s setup response queue error\n", __func__);
857 if (value == -EOPNOTSUPP)
859 "unknown class-specific control req "
860 "%02x.%02x v%04x i%04x l%u\n",
861 ctrl->bRequestType, ctrl->bRequest,
862 le16_to_cpu(ctrl->wValue), le16_to_cpu(ctrl->wIndex),
863 le16_to_cpu(ctrl->wLength));
867 static int gser_function_ctrlrequest(struct slp_multi_usb_function *f,
868 struct usb_composite_dev *cdev,
869 const struct usb_ctrlrequest *c)
871 return gser_setup(cdev, c);
873 static ssize_t gser_port_store(struct device *dev,
874 struct device_attribute *attr, char *buf)
878 printk("%s %s\n",__func__,buf);
879 sscanf(buf, "%d", &count);
880 if(count > GSER_PORT_MAX_COUNT)
882 gser_port_count = count;
885 static ssize_t gser_port_show(struct device *dev,
886 struct device_attribute *attr, char *buf)
888 /* print gser instance numbers */
889 return sprintf(buf, "%d \n",gser_port_count );
891 static DEVICE_ATTR(port_count, S_IRUGO | S_IWUSR, gser_port_show, gser_port_store);
893 static struct device_attribute *gser_function_attributes[] = {
894 &dev_attr_port_count,
897 static struct slp_multi_usb_function gser_function = {
899 .init = gser_function_init,
900 .cleanup = gser_function_cleanup,
901 .bind_config = gser_function_bind_config,
902 //.unbind_config = gser_function_unbind_config,
903 .ctrlrequest = gser_function_ctrlrequest,
904 .attributes = gser_function_attributes,
908 /*-------------------------------------------------------------------------*/
909 /* Supported functions initialization */
911 static struct slp_multi_usb_function *supported_functions[] = {
916 #ifdef CONFIG_USB_SPRD_DWC
923 static void slp_multi_evt_emit(struct slp_multi_dev *smdev,
924 struct slp_multi_evt *evt)
928 switch (evt->evt_type) {
929 case SMDEV_EVT_QOS_CHANGE:
930 if (smdev->curr_latency != evt->qos) {
931 smdev->curr_latency = evt->qos;
932 pm_qos_update_request(&smdev->pm_qos_req_dma,
934 dev_info(smdev->dev, "PM QOS value changed to %s\n",
935 (evt->qos != PM_QOS_DEFAULT_VALUE) ?
940 case SMDEV_EVT_STATE_CHANGE:
941 if (smdev->ustate != evt->ustate) {
942 dev_info(smdev->dev, "usb_state changed from %d to %d\n",
943 smdev->ustate, evt->ustate);
944 smdev->ustate = evt->ustate;
945 err = kobject_uevent(&smdev->dev->kobj, KOBJ_CHANGE);
947 dev_err(smdev->dev, "can't send usb_state[%d]\n",
950 dev_info(smdev->dev, "uevent usb_state [%s]\n",
951 (smdev->ustate == USB_STATE_CONFIGURED) ?
952 "CONFIGURED" : "OTHERS");
957 dev_err(smdev->dev, "not supported type(%d)\n", evt->evt_type);
962 static void slp_multi_evt_thread(struct work_struct *data)
964 struct slp_multi_dev *smdev = _slp_multi_dev;
967 mutex_lock(&smdev->evt_mutex);
969 goto evt_thread_done;
972 struct slp_multi_evt *evt;
973 struct list_head *this, *tmp;
976 spin_lock_irqsave(&smdev->evt_lock, flags);
977 list_splice_init(&smdev->evt_list, &evt_list);
978 spin_unlock_irqrestore(&smdev->evt_lock, flags);
980 if (list_empty(&evt_list))
983 list_for_each_safe(this, tmp, &evt_list) {
984 evt = list_entry(this, struct slp_multi_evt, node);
985 list_del(&evt->node);
986 slp_multi_evt_emit(smdev, evt);
992 mutex_unlock(&smdev->evt_mutex);
995 static void slp_multi_evt_send(struct slp_multi_dev *smdev,
996 struct slp_multi_evt *evt)
1000 spin_lock_irqsave(&smdev->evt_lock, flags);
1001 list_add_tail(&evt->node, &smdev->evt_list);
1002 schedule_work(&smdev->evt_work);
1003 spin_unlock_irqrestore(&smdev->evt_lock, flags);
1006 static void slp_multi_qos_evt(struct slp_multi_dev *smdev,
1009 struct slp_multi_evt *evt;
1011 evt = kzalloc(sizeof(struct slp_multi_evt), GFP_ATOMIC);
1013 dev_err(smdev->dev, "can't queue qos(%d) by ENOMEM\n",
1018 evt->evt_type = SMDEV_EVT_QOS_CHANGE;
1019 INIT_LIST_HEAD(&evt->node);
1020 evt->qos = new_value;
1022 slp_multi_evt_send(smdev, evt);
1025 static void slp_multi_state_evt(struct slp_multi_dev *smdev,
1026 enum usb_device_state new_state)
1028 struct slp_multi_evt *evt;
1030 evt = kzalloc(sizeof(struct slp_multi_evt), GFP_ATOMIC);
1032 dev_err(smdev->dev, "can't queue [%d] evt by ENOMEM\n",
1037 evt->evt_type = SMDEV_EVT_STATE_CHANGE;
1038 INIT_LIST_HEAD(&evt->node);
1039 evt->ustate = new_state;
1041 slp_multi_evt_send(smdev, evt);
1044 static int slp_multi_init_functions(struct slp_multi_dev *smdev,
1045 struct usb_composite_dev *cdev)
1047 struct slp_multi_usb_function *f;
1048 struct device_attribute **attrs;
1049 struct device_attribute *attr;
1053 list_for_each_entry(f, &smdev->available_functions, available_list) {
1054 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
1055 f->dev = device_create(slp_multi_class, smdev->dev,
1056 MKDEV(0, index++), f, f->dev_name);
1057 if (IS_ERR(f->dev)) {
1059 "Failed to create dev %s", f->dev_name);
1060 err = PTR_ERR(f->dev);
1061 goto init_func_err_create;
1065 err = f->init(f, cdev);
1068 "Failed to init %s", f->name);
1069 goto init_func_err_out;
1073 attrs = f->attributes;
1075 while ((attr = *attrs++) && !err)
1076 err = device_create_file(f->dev, attr);
1079 dev_err(f->dev, "Failed to create function %s attributes",
1081 goto init_func_err_out;
1087 device_destroy(slp_multi_class, f->dev->devt);
1088 init_func_err_create:
1093 static void slp_multi_cleanup_functions(struct slp_multi_dev *smdev)
1095 struct slp_multi_usb_function *f;
1097 list_for_each_entry(f, &smdev->available_functions, available_list) {
1099 device_destroy(slp_multi_class, f->dev->devt);
1109 slp_multi_bind_enabled_functions(struct slp_multi_dev *smdev,
1110 struct usb_configuration *c)
1112 struct slp_multi_usb_function *f;
1115 if (c->bConfigurationValue == USB_CONFIGURATION_1) {
1116 list_for_each_entry(f, &smdev->funcs_fconf, fconf_list) {
1117 dev_dbg(smdev->dev, "usb_bind_conf(1st) f:%s\n",
1119 ret = f->bind_config(f, c);
1121 dev_err(smdev->dev, "%s bind_conf(1st) failed\n",
1126 } else if (c->bConfigurationValue == USB_CONFIGURATION_2) {
1127 list_for_each_entry(f, &smdev->funcs_sconf, sconf_list) {
1128 dev_dbg(smdev->dev, "usb_bind_conf(2nd) f:%s\n",
1130 ret = f->bind_config(f, c);
1132 dev_err(smdev->dev, "%s bind_conf(2nd) failed\n",
1138 dev_err(smdev->dev, "Not supported configuraton(%d)\n",
1139 c->bConfigurationValue);
1146 slp_multi_unbind_enabled_functions(struct slp_multi_dev *smdev,
1147 struct usb_configuration *c)
1149 struct slp_multi_usb_function *f;
1151 if (c->bConfigurationValue == USB_CONFIGURATION_1) {
1152 list_for_each_entry(f, &smdev->funcs_fconf, fconf_list) {
1153 if (f->unbind_config)
1154 f->unbind_config(f, c);
1156 } else if (c->bConfigurationValue == USB_CONFIGURATION_2) {
1157 list_for_each_entry(f, &smdev->funcs_sconf, sconf_list) {
1158 if (f->unbind_config)
1159 f->unbind_config(f, c);
1164 #define ADD_FUNCS_LIST(head, member) \
1165 static inline int add_##member(struct slp_multi_dev *smdev, char *name) \
1167 struct slp_multi_usb_function *av_f, *en_f; \
1169 dev_dbg(smdev->dev, "usb: name=%s\n", name); \
1170 list_for_each_entry(av_f, &smdev->available_functions, \
1172 if (!strcmp(name, av_f->name)) { \
1173 list_for_each_entry(en_f, &smdev->head, \
1175 if (av_f == en_f) { \
1176 dev_info(smdev->dev, \
1177 "usb:%s already enabled!\n", \
1182 list_add_tail(&av_f->member, &smdev->head); \
1188 static ssize_t show_##head(struct device *pdev, \
1189 struct device_attribute *attr, char *buf) \
1191 struct slp_multi_dev *smdev = dev_get_drvdata(pdev); \
1192 struct slp_multi_usb_function *f; \
1195 list_for_each_entry(f, &smdev->head, member) { \
1196 dev_dbg(pdev, "usb: enabled_func=%s\n", \
1198 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name); \
1201 *(buff - 1) = '\n'; \
1203 return buff - buf; \
1205 static ssize_t store_##head(struct device *pdev, \
1206 struct device_attribute *attr, \
1207 const char *buff, size_t size) \
1209 struct slp_multi_dev *smdev = dev_get_drvdata(pdev); \
1211 char buf[256], *b; \
1214 if (smdev->enabled) { \
1215 dev_info(pdev, "can't change usb functions" \
1216 "(already enabled)!!\n"); \
1220 INIT_LIST_HEAD(&smdev->head); \
1222 dev_dbg(pdev, "usb: buff=%s\n", buff); \
1223 strlcpy(buf, buff, sizeof(buf)); \
1227 name = strsep(&b, ","); \
1229 err = add_##member(smdev, name); \
1232 "slp_multi_usb: Cannot enable '%s'", \
1236 "slp_multi_usb: enable SUCCESS '%s'", \
1243 static DEVICE_ATTR(head, S_IRUGO | S_IWUSR, show_##head, store_##head);
1245 ADD_FUNCS_LIST(funcs_fconf, fconf_list)
1246 ADD_FUNCS_LIST(funcs_sconf, sconf_list)
1248 /*-------------------------------------------------------------------------*/
1249 /* /sys/class/usb_mode/usb%d/ interface */
1251 static ssize_t pm_qos_show(struct device *pdev,
1252 struct device_attribute *attr, char *buf)
1254 struct slp_multi_dev *smdev = dev_get_drvdata(pdev);
1256 return snprintf(buf, PAGE_SIZE, "%s\n", smdev->pm_qos);
1259 static ssize_t pm_qos_store(struct device *pdev,
1260 struct device_attribute *attr,
1261 const char *buff, size_t size)
1263 struct slp_multi_dev *smdev = dev_get_drvdata(pdev);
1265 if (smdev->enabled) {
1266 dev_info(pdev, "Already usb enabled, can't change qos\n");
1270 if (!(strncmp(buff, "high", 4)) || !(strncmp(buff, "low", 3))) {
1271 dev_err(pdev, "not supported cmd, can't set it\n");
1275 strlcpy(smdev->pm_qos, buff, sizeof(smdev->pm_qos));
1279 static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR, pm_qos_show, pm_qos_store);
1281 static ssize_t enable_show(struct device *pdev,
1282 struct device_attribute *attr, char *buf)
1284 struct slp_multi_dev *smdev = dev_get_drvdata(pdev);
1285 dev_dbg(pdev, "usb: smdev->enabled=%d\n", smdev->enabled);
1286 return snprintf(buf, PAGE_SIZE, "%d\n", smdev->enabled);
1289 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1290 const char *buff, size_t size)
1292 struct slp_multi_dev *smdev = dev_get_drvdata(pdev);
1293 struct usb_composite_dev *cdev = smdev->cdev;
1294 struct list_head *this, *tmp;
1298 if (sysfs_streq(buff, "1"))
1300 else if (sysfs_streq(buff, "0"))
1303 dev_err(pdev, "Invalid cmd %c%c..", *buff, *(buff+1));
1307 dev_dbg(pdev, "usb: %s enabled=%d, !smdev->enabled=%d\n",
1308 __func__, enabled, !smdev->enabled);
1310 mutex_lock(&smdev->enable_lock);
1312 if (enabled && !smdev->enabled) {
1313 struct slp_multi_usb_function *f;
1315 #if defined(CONFIG_USB_G_ANDROID_SAMSUNG_COMPOSITE) || defined(CONFIG_USB_G_SLP)
1316 cdev->next_string_id = composite_string_index;
1318 cdev->next_string_id = 0;
1320 /* update values in composite driver's
1321 * copy of device descriptor
1323 cdev->desc.idVendor = device_desc.idVendor;
1324 cdev->desc.idProduct = device_desc.idProduct;
1325 cdev->desc.bcdDevice = device_desc.bcdDevice;
1327 list_for_each_entry(f, &smdev->funcs_fconf, fconf_list) {
1328 printk(KERN_DEBUG "1st-config usb: %s f:%s\n", __func__, f->name);
1329 if (!strcmp(f->name, "acm")) {
1330 printk(KERN_DEBUG "usb: acm is enabled. (bcdDevice=0x400)\n");
1331 /* Samsung KIES needs fixed bcdDevice number */
1332 cdev->desc.bcdDevice =
1333 cpu_to_le16(0x0400);
1337 list_for_each_entry(f, &smdev->funcs_sconf, sconf_list) {
1338 printk(KERN_DEBUG "2nd-config usb: %s f:%s\n", __func__, f->name);
1339 if (!strcmp(f->name, "acm")) {
1340 printk(KERN_DEBUG "usb: acm is enabled. (bcdDevice=0x400)\n");
1341 /* Samsung KIES needs fixed bcdDevice number */
1342 cdev->desc.bcdDevice =
1343 cpu_to_le16(0x0400);
1345 smdev->dual_config = true;
1347 strncpy(manufacturer_string, "SAMSUNG",
1348 sizeof(manufacturer_string) - 1);
1349 strncpy(product_string, "SLP",
1350 sizeof(product_string) - 1);
1352 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1353 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1354 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1356 dev_dbg(pdev, "usb: %s vendor=%x,product=%x,bcdDevice=%x",
1357 __func__, cdev->desc.idVendor,
1358 cdev->desc.idProduct, cdev->desc.bcdDevice);
1359 dev_dbg(pdev, ",Class=%x,SubClass=%x,Protocol=%x\n",
1360 cdev->desc.bDeviceClass,
1361 cdev->desc.bDeviceSubClass, cdev->desc.bDeviceProtocol);
1362 dev_dbg(pdev, "usb: %s next cmd : usb_add_config\n",
1365 ret = usb_add_config(cdev,
1366 &first_config_driver, slp_multi_bind_config);
1369 "usb_add_config fail-1st(%d)\n", ret);
1370 smdev->dual_config = false;
1374 if (smdev->dual_config) {
1375 ret = usb_add_config(cdev, &second_config_driver,
1376 slp_multi_bind_config);
1379 "usb_add_config fail-2nd(%d)\n", ret);
1380 smdev->dual_config = false;
1381 goto enable_conf_err;
1385 if ((smdev->swfi_latency != PM_QOS_DEFAULT_VALUE) &&
1386 !(strncmp(smdev->pm_qos, "high", 4)) &&
1387 (smdev->curr_latency == PM_QOS_DEFAULT_VALUE)) {
1388 smdev->curr_latency = smdev->swfi_latency;
1389 pm_qos_update_request(&smdev->pm_qos_req_dma,
1390 smdev->swfi_latency);
1391 dev_info(pdev, "PM QOS changed to HIGH\n");
1394 smdev->enabled = true;
1396 ret = usb_gadget_connect(cdev->gadget);
1398 dev_err(pdev, "can't connected gadget(%d)\n", ret);
1399 smdev->enabled = false;
1400 goto enable_conf_err;
1403 } else if (!enabled && smdev->enabled) {
1404 usb_gadget_disconnect(cdev->gadget);
1406 smdev->enabled = false;
1408 /* Cancel pending control requests if it available */
1409 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
1411 usb_remove_config(cdev, &first_config_driver);
1412 if (smdev->dual_config)
1413 usb_remove_config(cdev, &second_config_driver);
1414 smdev->dual_config = false;
1416 /* remove all evt if it remained */
1417 mutex_lock(&smdev->evt_mutex);
1418 if (!list_empty(&smdev->evt_list)) {
1419 list_for_each_safe(this, tmp, &smdev->evt_list) {
1420 struct slp_multi_evt *evt;
1422 evt = list_entry(this,
1423 struct slp_multi_evt, node);
1424 list_del(&evt->node);
1428 mutex_unlock(&smdev->evt_mutex);
1430 if (smdev->curr_latency != PM_QOS_DEFAULT_VALUE) {
1431 smdev->curr_latency = PM_QOS_DEFAULT_VALUE;
1432 pm_qos_update_request(&smdev->pm_qos_req_dma,
1433 PM_QOS_DEFAULT_VALUE);
1434 dev_info(pdev, "PM QOS changed to DEFAULT\n");
1437 if (smdev->ustate != USB_STATE_NOTATTACHED) {
1438 dev_info(pdev, "forcely send disconnect uevent\n");
1439 smdev->ustate = USB_STATE_NOTATTACHED;
1440 kobject_uevent(&smdev->dev->kobj, KOBJ_CHANGE);
1444 dev_info(pdev, "slp_multi_usb: already %s\n",
1445 smdev->enabled ? "enabled" : "disabled");
1451 if (smdev->dual_config) {
1452 usb_remove_config(cdev, &second_config_driver);
1453 smdev->dual_config = false;
1455 usb_remove_config(cdev, &first_config_driver);
1457 mutex_lock(&smdev->evt_mutex);
1458 if (!list_empty(&smdev->evt_list)) {
1459 list_for_each_safe(this, tmp, &smdev->evt_list) {
1460 struct slp_multi_evt *evt;
1462 evt = list_entry(this, struct slp_multi_evt, node);
1463 list_del(&evt->node);
1467 mutex_unlock(&smdev->evt_mutex);
1469 if (smdev->ustate != USB_STATE_NOTATTACHED) {
1470 dev_info(pdev, "forcely send disconnect uevent\n");
1471 smdev->ustate = USB_STATE_NOTATTACHED;
1472 kobject_uevent(&smdev->dev->kobj, KOBJ_CHANGE);
1476 mutex_unlock(&smdev->enable_lock);
1477 return (ret < 0 ? ret : size);
1480 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1482 #define DESCRIPTOR_ATTR(field, format_string) \
1484 field ## _show(struct device *dev, struct device_attribute *attr, \
1487 return snprintf(buf, PAGE_SIZE, format_string, device_desc.field);\
1490 field ## _store(struct device *dev, struct device_attribute *attr, \
1491 const char *buf, size_t size) \
1494 if (sscanf(buf, format_string, &value) == 1) { \
1495 device_desc.field = value; \
1500 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1502 #define DESCRIPTOR_STRING_ATTR(field, buffer) \
1504 field ## _show(struct device *dev, struct device_attribute *attr, \
1507 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
1510 field ## _store(struct device *dev, struct device_attribute *attr, \
1511 const char *buf, size_t size) \
1513 if ((size >= sizeof(buffer)) || \
1514 (sscanf(buf, "%s", buffer) != 1)) { \
1519 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1521 DESCRIPTOR_ATTR(idVendor, "%04x\n")
1522 DESCRIPTOR_ATTR(idProduct, "%04x\n")
1523 DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1524 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1525 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1526 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1527 DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1528 DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1529 DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1531 static struct device_attribute *slp_multi_usb_attributes[] = {
1533 &dev_attr_idProduct,
1534 &dev_attr_bcdDevice,
1535 &dev_attr_bDeviceClass,
1536 &dev_attr_bDeviceSubClass,
1537 &dev_attr_bDeviceProtocol,
1538 &dev_attr_iManufacturer,
1541 &dev_attr_funcs_fconf,
1542 &dev_attr_funcs_sconf,
1548 /*-------------------------------------------------------------------------*/
1549 /* Composite driver */
1551 static int slp_multi_bind_config(struct usb_configuration *c)
1553 struct slp_multi_dev *smdev = _slp_multi_dev;
1556 ret = slp_multi_bind_enabled_functions(smdev, c);
1561 static void slp_multi_unbind_config(struct usb_configuration *c)
1563 struct slp_multi_dev *smdev = _slp_multi_dev;
1566 slp_multi_unbind_enabled_functions(smdev, c);
1569 static int slp_multi_bind(struct usb_composite_dev *cdev)
1571 struct slp_multi_dev *smdev = _slp_multi_dev;
1572 struct usb_gadget *gadget = cdev->gadget;
1575 dev_dbg(smdev->dev, "usb: %s disconnect\n", __func__);
1576 usb_gadget_disconnect(gadget);
1578 /* Allocate string descriptor numbers ... note that string
1579 * contents can be overridden by the composite_dev glue.
1581 id = usb_string_id(cdev);
1584 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1585 device_desc.iManufacturer = id;
1587 id = usb_string_id(cdev);
1590 strings_dev[STRING_PRODUCT_IDX].id = id;
1591 device_desc.iProduct = id;
1593 /* Default strings - should be updated by userspace */
1594 strlcpy(manufacturer_string, "Samsung\0",
1595 sizeof(manufacturer_string) - 1);
1596 strlcpy(product_string, "SLP\0", sizeof(product_string) - 1);
1597 snprintf(serial_string, sizeof(serial_string),
1598 "%08x%08x", system_serial_high, system_serial_low);
1600 id = usb_string_id(cdev);
1603 strings_dev[STRING_SERIAL_IDX].id = id;
1604 device_desc.iSerialNumber = id;
1606 #if defined(CONFIG_USB_G_ANDROID_SAMSUNG_COMPOSITE)
1607 composite_string_index = 4;
1608 #elif defined(CONFIG_USB_G_SLP)
1609 /*3 (slp) + 3 (rndis) + 1 (sdb) + 1(mtp) = 8 */
1610 composite_string_index = 8;
1612 ret = slp_multi_init_functions(smdev, cdev);
1616 usb_gadget_set_selfpowered(gadget);
1622 static int slp_multi_usb_unbind(struct usb_composite_dev *cdev)
1624 struct slp_multi_dev *smdev = _slp_multi_dev;
1625 dev_dbg(smdev->dev, "usb: %s\n", __func__);
1626 slp_multi_cleanup_functions(smdev);
1630 static void slp_multi_usb_disconnect(struct usb_composite_dev *cdev)
1632 struct slp_multi_dev *smdev = _slp_multi_dev;
1633 dev_dbg(smdev->dev, "usb:%s (%d)\n", __func__, __LINE__);
1635 /* to prevent evt queuing during dis/enable control*/
1636 if (list_empty(&cdev->configs))
1639 if (!strncmp(smdev->pm_qos, "high", 4)) {
1640 dev_info(smdev->dev, "queue default qos evt\n");
1641 slp_multi_qos_evt(smdev, PM_QOS_DEFAULT_VALUE);
1644 dev_info(smdev->dev, "queue disconnect evt\n");
1645 slp_multi_state_evt(smdev, USB_STATE_NOTATTACHED);
1648 static void slp_multi_usb_resume(struct usb_composite_dev *cdev)
1650 struct slp_multi_dev *smdev = _slp_multi_dev;
1652 dev_dbg(smdev->dev, "usb: %s\n", __func__);
1654 /* to prevent evt queuing during dis/enable control*/
1655 if (list_empty(&cdev->configs))
1658 if (!strncmp(smdev->pm_qos, "high", 4)) {
1659 dev_info(smdev->dev, "queue high qos evt\n");
1660 slp_multi_qos_evt(smdev, smdev->swfi_latency);
1664 static void slp_multi_usb_suspend(struct usb_composite_dev *cdev)
1666 struct slp_multi_dev *smdev = _slp_multi_dev;
1668 dev_dbg(smdev->dev, "usb: %s\n", __func__);
1670 /* to prevent evt queuing during dis/enable control*/
1671 if (list_empty(&cdev->configs))
1674 if (!strncmp(smdev->pm_qos, "high", 4)) {
1675 dev_info(smdev->dev, "queue default qos evt\n");
1676 slp_multi_qos_evt(smdev, PM_QOS_DEFAULT_VALUE);
1680 static struct usb_composite_driver slp_multi_composite = {
1681 .name = "slp_multi_composite",
1682 .dev = &device_desc,
1683 .strings = slp_dev_strings,
1684 .bind = slp_multi_bind,
1685 .unbind = slp_multi_usb_unbind,
1686 .disconnect = slp_multi_usb_disconnect,
1687 .max_speed = USB_SPEED_HIGH,
1688 .resume = slp_multi_usb_resume,
1689 .suspend = slp_multi_usb_suspend,
1692 /* HACK: android needs to override setup for accessory to work */
1693 static int (*composite_setup_func)(struct usb_gadget *gadget,
1694 const struct usb_ctrlrequest *c);
1697 slp_multi_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1699 struct slp_multi_dev *smdev = _slp_multi_dev;
1700 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1701 u8 b_requestType = ctrl->bRequestType;
1702 struct slp_multi_usb_function *f;
1703 int value = -EOPNOTSUPP;
1705 if (smdev && ((b_requestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)) {
1706 struct usb_request *req = cdev->req;
1710 gadget->ep0->driver_data = cdev;
1712 /* To check & report it to platform , we check it all */
1713 list_for_each_entry(f, &smdev->available_functions,
1715 if (f->ctrlrequest) {
1716 value = f->ctrlrequest(f, cdev, ctrl);
1724 value = composite_setup_func(gadget, ctrl);
1726 if (smdev && (value >= 0) && (cdev->config)
1727 && (ctrl->bRequest == USB_REQ_SET_CONFIGURATION))
1728 slp_multi_state_evt(smdev, USB_STATE_CONFIGURED);
1733 static struct slp_multi_dev *slp_multi_create_device(struct device *pdev,
1734 struct slp_multi_platform_data *pdata)
1736 struct slp_multi_dev *smdev;
1737 struct slp_multi_usb_function *f;
1738 struct slp_multi_usb_function **functions = supported_functions;
1739 struct device_attribute **attrs = slp_multi_usb_attributes;
1740 struct device_attribute *attr;
1743 smdev = kzalloc(sizeof(*smdev), GFP_KERNEL);
1745 dev_err(pdev, "usb_mode: can't alloc for smdev\n");
1746 return ERR_PTR(-ENOMEM);
1749 INIT_LIST_HEAD(&smdev->available_functions);
1750 INIT_LIST_HEAD(&smdev->funcs_fconf);
1751 INIT_LIST_HEAD(&smdev->funcs_sconf);
1752 INIT_LIST_HEAD(&smdev->evt_list);
1754 mutex_init(&smdev->evt_mutex);
1755 mutex_init(&smdev->enable_lock);
1756 spin_lock_init(&smdev->evt_lock);
1758 INIT_WORK(&smdev->evt_work, slp_multi_evt_thread);
1760 smdev->ustate = USB_STATE_NOTATTACHED;
1762 while ((f = *functions++)) {
1763 for (i = 0; i < pdata->nfuncs; i++)
1764 if (!strcmp(pdata->enable_funcs[i], f->name))
1765 list_add_tail(&f->available_list,
1766 &smdev->available_functions);
1769 smdev->dev = device_create(slp_multi_class, NULL,
1770 MKDEV(0, 0), NULL, "usb0");
1771 if (IS_ERR(smdev->dev)) {
1773 return ERR_PTR(-ENODEV);
1776 dev_set_drvdata(smdev->dev, smdev);
1778 while ((attr = *attrs++)) {
1779 err = device_create_file(smdev->dev, attr);
1781 dev_set_drvdata(smdev->dev, NULL);
1782 device_destroy(slp_multi_class, smdev->dev->devt);
1783 return ERR_PTR(err);
1789 static void slp_multi_destroy_device(struct slp_multi_dev *smdev)
1791 struct device_attribute **attrs = slp_multi_usb_attributes;
1792 struct device_attribute *attr;
1793 struct list_head *this, *tmp;
1796 while ((attr = *attrs++))
1797 device_destroy(slp_multi_class, smdev->dev->devt);
1799 cancel_work_sync(&smdev->evt_work);
1801 if (!list_empty(&smdev->evt_list)) {
1802 list_for_each_safe(this, tmp, &smdev->evt_list) {
1803 struct slp_multi_evt *evt;
1805 evt = list_entry(this, struct slp_multi_evt, node);
1806 list_del(&evt->node);
1811 dev_set_drvdata(smdev->dev, NULL);
1813 device_unregister(smdev->dev);
1816 static int slp_multi_probe(struct platform_device *pdev)
1818 struct slp_multi_dev *smdev;
1819 struct device_node *np = pdev->dev.of_node;
1820 struct slp_multi_platform_data *pdata;
1821 const char **enable_funcs;
1825 dev_err(&pdev->dev, "usb : %s device tree enabled\n", __func__);
1826 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1828 dev_err(&pdev->dev, "usb : %s unable to allocate platform"
1829 "data\n", __func__);
1833 if (of_find_property(np, "slp,slp_mult-nluns", NULL))
1834 of_property_read_u32(np, "slp,slp_mult-nluns",
1837 if (of_find_property(np, "slp,slp_mult-swfi-latency", NULL))
1838 of_property_read_u32(np, "slp,slp_mult-swfi-latency",
1839 &pdata->swfi_latency);
1841 if (of_find_property(np, "slp,slp_mult-enable-funcs", NULL)) {
1842 pdata->nfuncs = of_property_count_strings(np,
1843 "slp,slp_mult-enable-funcs");
1845 if (pdata->nfuncs < 0) {
1847 "fail to count enable-funcs(%d)\n",
1852 enable_funcs = devm_kzalloc(&pdev->dev,
1853 pdata->nfuncs * sizeof(**enable_funcs),
1855 if (!enable_funcs) {
1856 dev_err(&pdev->dev, "usb : %s unable to allocate"
1857 "platform data\n", __func__);
1861 for (i = 0; i < pdata->nfuncs; i++)
1862 of_property_read_string_index(pdev->dev.of_node,
1863 "slp,slp_mult-enable-funcs",
1864 i, &enable_funcs[i]);
1865 pdata->enable_funcs = enable_funcs;
1867 dev_err(&pdev->dev, "usb : %s There is no"
1868 "enable-functions\n", __func__);
1872 pdata = pdev->dev.platform_data;
1875 smdev = slp_multi_create_device(&pdev->dev, pdata);
1876 if (IS_ERR(smdev)) {
1877 dev_err(&pdev->dev, "usb_mode: %s can't create device\n", __func__);
1878 return PTR_ERR(smdev);
1881 slp_multi_nluns = pdata->nluns;
1882 _slp_multi_dev = smdev;
1884 err = usb_composite_probe(&slp_multi_composite);
1886 dev_err(&pdev->dev, "usb_mode: %s can't probe composite\n", __func__);
1890 if (pdata->swfi_latency) {
1891 smdev->swfi_latency = pdata->swfi_latency + 1;
1892 pm_qos_add_request(&smdev->pm_qos_req_dma,
1893 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1894 strlcpy(smdev->pm_qos, "high", sizeof(smdev->pm_qos));
1896 smdev->swfi_latency = PM_QOS_DEFAULT_VALUE;
1897 strlcpy(smdev->pm_qos, "NONE", sizeof(smdev->pm_qos));
1899 smdev->curr_latency = PM_QOS_DEFAULT_VALUE;
1901 platform_set_drvdata(pdev, smdev);
1903 /* Override composite driver functions */
1904 /* HACK: exchange composite's setup with ours */
1905 composite_setup_func = slp_multi_composite.gadget_driver.setup;
1906 slp_multi_composite.gadget_driver.setup = slp_multi_setup;
1908 dev_err(&pdev->dev, "usb : %s usb_mode driver, version:" USB_MODE_VERSION
1909 "," " init Ok\n", __func__);
1914 platform_set_drvdata(pdev, NULL);
1915 slp_multi_destroy_device(smdev);
1920 static int slp_multi_remove(struct platform_device *pdev)
1922 struct slp_multi_platform_data *pdata = pdev->dev.platform_data;
1923 struct slp_multi_dev *smdev = platform_get_drvdata(pdev);
1925 if (smdev->enabled) {
1926 dev_info(&pdev->dev, "start remove usb_mode driver without disabled\n");
1927 smdev->enabled = false;
1930 usb_composite_unregister(&slp_multi_composite);
1932 platform_set_drvdata(pdev, NULL);
1934 if (pdata->swfi_latency != PM_QOS_DEFAULT_VALUE)
1935 pm_qos_remove_request(&smdev->pm_qos_req_dma);
1937 slp_multi_destroy_device(smdev);
1938 kfree(_slp_multi_dev);
1939 _slp_multi_dev = NULL;
1945 static const struct of_device_id of_slp_multi_match[] = {
1947 .compatible = "slp,slp_multi",
1951 MODULE_DEVICE_TABLE(of, of_slp_multi_match);
1954 static struct platform_driver slp_multi_driver = {
1955 .probe = slp_multi_probe,
1956 .remove = slp_multi_remove,
1958 .name = "slp_multi",
1959 .owner = THIS_MODULE,
1960 .of_match_table = of_match_ptr(of_slp_multi_match),
1964 static CLASS_ATTR_STRING(version, S_IRUSR | S_IRGRP | S_IROTH,
1967 static int slp_multi_uevent(struct device *dev, struct kobj_uevent_env *env)
1969 struct slp_multi_dev *smdev = _slp_multi_dev;
1974 dev_info(dev, "not yet created smdev!\n");
1978 switch (smdev->ustate) {
1979 case USB_STATE_NOTATTACHED:
1980 state_str = "NOTATTACHED";
1983 case USB_STATE_CONFIGURED:
1984 state_str = "CONFIGURED";
1988 dev_err(dev, "not supported usb_state(%d) for uevent\n",
1993 ret = add_uevent_var(env, "USB_STATE=%s", state_str);
1995 dev_err(dev, "failed to add uevent USB_STATE\n");
2000 static int __init slp_multi_init(void)
2004 slp_multi_class = class_create(THIS_MODULE, "usb_mode");
2005 if (IS_ERR(slp_multi_class)) {
2006 pr_err("failed to create slp_multi class --> %ld\n",
2007 PTR_ERR(slp_multi_class));
2008 return PTR_ERR(slp_multi_class);
2011 slp_multi_class->dev_uevent = slp_multi_uevent;
2013 err = class_create_file(slp_multi_class, &class_attr_version.attr);
2015 pr_err("usb_mode: can't create sysfs version file\n");
2019 err = platform_driver_register(&slp_multi_driver);
2021 pr_err("usb_mode: can't register driver\n");
2028 class_remove_file(slp_multi_class, &class_attr_version.attr);
2030 class_destroy(slp_multi_class);
2034 late_initcall(slp_multi_init);
2036 static void __exit slp_multi_exit(void)
2038 platform_driver_unregister(&slp_multi_driver);
2039 class_remove_file(slp_multi_class, &class_attr_version.attr);
2040 class_destroy(slp_multi_class);
2042 module_exit(slp_multi_exit);