packaging: install license for rpm package instead of license package
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / usb / gadget / slp.c
1 /*
2  * Gadget Driver for SLP based on Android
3  *
4  * Copyright (C) 2008 Google, Inc.
5  * Author: Mike Lockwood <lockwood@android.com>
6  * Modified : Yongsul Oh <yongsul96.oh@samsung.com>
7  *
8  * Heavily based on android.c
9  *
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.
13  *
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.
18  *
19  */
20
21 /* #define DEBUG */
22 /* #define VERBOSE_DEBUG */
23
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/fs.h>
27
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>
33
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>
40 #include <linux/of.h>
41 #include <linux/workqueue.h>
42 #include <linux/spinlock.h>
43
44 #include "gadget_chips.h"
45 #include "u_serial.h"
46
47 #include "f_sdb.c"
48 #include "f_mtp_slp.c"
49 #define USB_ETH_RNDIS y
50 #define USB_FRNDIS_INCLUDED y
51 #include "f_rndis.c"
52 #include "rndis.c"
53 #include "u_ether.c"
54 #ifdef CONFIG_USB_SPRD_DWC
55 #include "f_vserial.c"
56 #endif
57
58 #define USB_MODE_VERSION        "1.1"
59
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);
64
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;
68 #endif
69
70 /* Default vendor and product IDs, overridden by userspace */
71 #define VENDOR_ID               0x04E8  /* Samsung VID */
72 #define PRODUCT_ID              0x6860  /* KIES mode PID */
73
74 /* DM_PORT NUM : /dev/ttyGS* port number */
75 #define DM_PORT_NUM            1
76
77 struct slp_multi_func_data {
78         const char *name;
79         enum slp_multi_config_id usb_config_id;
80 };
81
82 struct slp_multi_usb_function {
83         char *name;
84         void *config;
85
86         struct device *dev;
87         char *dev_name;
88         struct device_attribute **attributes;
89
90         /* for slp_multi_dev.funcs_fconf */
91         struct list_head fconf_list;
92
93         /* for slp_multi_dev.funcs_sconf */
94         struct list_head sconf_list;
95
96         /* for slp_multi_dev.available_functions */
97         struct list_head available_list;
98
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 *);
109
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 *);
120 };
121
122 struct slp_multi_dev {
123         struct list_head available_functions;
124
125         /* for each configuration control */
126         struct list_head funcs_fconf;
127         struct list_head funcs_sconf;
128
129         struct usb_composite_dev *cdev;
130         struct device *dev;
131
132         bool enabled;
133         bool dual_config;
134
135         /* current USB state */
136         enum usb_device_state ustate;
137
138
139         /* to control DMA QOS */
140         char pm_qos[5];
141         s32 swfi_latency;
142         s32 curr_latency;
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;
149         spinlock_t evt_lock;
150 };
151
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);
157
158 /* string IDs are assigned dynamically */
159 #define STRING_MANUFACTURER_IDX         0
160 #define STRING_PRODUCT_IDX              1
161 #define STRING_SERIAL_IDX               2
162
163 static char manufacturer_string[256];
164 static char product_string[256];
165 static char serial_string[256];
166
167 /* String Table */
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,
172         {}                      /* end of list */
173 };
174
175 static struct usb_gadget_strings stringtab_dev = {
176         .language = 0x0409,     /* en-us */
177         .strings = strings_dev,
178 };
179
180 static struct usb_gadget_strings *slp_dev_strings[] = {
181         &stringtab_dev,
182         NULL,
183 };
184
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,
194 };
195
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 */
202 };
203
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 */
210 };
211
212 /*-------------------------------------------------------------------------*/
213 /* Supported functions initialization */
214
215 static int sdb_function_init(struct slp_multi_usb_function *f,
216                              struct usb_composite_dev *cdev)
217 {
218         return sdb_setup(cdev);
219 }
220
221 static void sdb_function_cleanup(struct slp_multi_usb_function *f)
222 {
223         sdb_cleanup();
224 }
225
226 static int sdb_function_bind_config(struct slp_multi_usb_function *f,
227                                     struct usb_configuration *c)
228 {
229         return sdb_bind_config(c);
230 }
231
232 static struct slp_multi_usb_function sdb_function = {
233         .name = "sdb",
234         .init = sdb_function_init,
235         .cleanup = sdb_function_cleanup,
236         .bind_config = sdb_function_bind_config,
237 };
238
239 struct acm_function_config {
240         int instances;
241         struct usb_function *f_acm[MAX_U_SERIAL_PORTS];
242         struct usb_function_instance *f_acm_inst[MAX_U_SERIAL_PORTS];
243 };
244
245 static int acm_function_init(struct slp_multi_usb_function *f,
246                              struct usb_composite_dev *cdev)
247 {
248         struct acm_function_config *config;
249         int status, i, j;
250
251         config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
252         if (!config)
253                 return -ENOMEM;
254         f->config = config;
255         config->instances = 1;
256
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;
262                 }
263         }
264
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;
270                 }
271         }
272         return 0;
273
274 err_usb_get_function:
275         while (j-- > 0)
276                 usb_put_function(config->f_acm[j]);
277
278 err_usb_get_instance:
279         while (i-- > 0)
280                 usb_put_function_instance(config->f_acm_inst[i]);
281
282         kfree(config);
283         return status;
284 }
285
286 static void acm_function_cleanup(struct slp_multi_usb_function *f)
287 {
288         int i;
289         struct acm_function_config *config = f->config;
290
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]);
294         }
295
296         kfree(config);
297         config = NULL;
298 }
299
300 static int acm_function_bind_config(struct slp_multi_usb_function *f,
301                                     struct usb_configuration *c)
302 {
303         int i;
304         int ret = 0;
305         struct acm_function_config *config = f->config;
306
307         for (i = 0; i < config->instances; i++) {
308                 ret = usb_add_function(c, config->f_acm[i]);
309                 if (ret) {
310                         dev_err(f->dev, "Could not bind acm%u config\n", i);
311                         goto err_usb_add_function;
312                 }
313         }
314
315         return 0;
316
317 err_usb_add_function:
318         while (i-- > 0)
319                 usb_remove_function(c, config->f_acm[i]);
320
321         return ret;
322 }
323
324 static ssize_t acm_instances_show(struct device *dev,
325                                   struct device_attribute *attr, char *buf)
326 {
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);
330 }
331
332 static ssize_t acm_instances_store(struct device *dev,
333                                    struct device_attribute *attr,
334                                    const char *buf, size_t size)
335 {
336         struct slp_multi_usb_function *f = dev_get_drvdata(dev);
337         struct acm_function_config *config = f->config;
338         int value;
339
340         sscanf(buf, "%d", &value);
341         if (value > MAX_U_SERIAL_PORTS)
342                 value = MAX_U_SERIAL_PORTS;
343         config->instances = value;
344         return size;
345 }
346
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 };
351
352 static struct slp_multi_usb_function acm_function = {
353         .name           = "acm",
354         .init           = acm_function_init,
355         .cleanup        = acm_function_cleanup,
356         .bind_config    = acm_function_bind_config,
357         .attributes     = acm_function_attributes,
358 };
359
360 static int
361 mtp_function_init(struct slp_multi_usb_function *f,
362                 struct usb_composite_dev *cdev)
363 {
364         return mtp_setup(cdev);
365 }
366
367 static void mtp_function_cleanup(struct slp_multi_usb_function *f)
368 {
369         mtp_cleanup();
370 }
371
372 static int
373 mtp_function_bind_config(struct slp_multi_usb_function *f,
374                 struct usb_configuration *c)
375 {
376         return mtp_bind_config(c);
377 }
378
379 static int mtp_function_ctrlrequest(struct slp_multi_usb_function *f,
380                                         struct usb_composite_dev *cdev,
381                                         const struct usb_ctrlrequest *c)
382 {
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] = {
389                 /* Header section */
390                 /* Upper 2byte of dwLength */
391                 0x00, 0x00,
392                 /* bcd Version */
393                 0x00, 0x01,
394                 /* wIndex, Extended compatID index */
395                 0x04, 0x00,
396                 /* bCount, we use only 1 function(MTP) */
397                 0x01,
398                 /* RESERVED */
399                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
400
401                 /* First Function section for MTP */
402                 /* bFirstInterfaceNumber,
403                  * we always use it by 0 for MTP
404                  */
405                 0x00,
406                 /* RESERVED, fixed value 1 */
407                 0x01,
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,
412                 /* RESERVED */
413                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
414         };
415
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
423                  */
424         case 0x54:
425         case 0x6F:
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);
430
431                 if (value >= 0) {
432                         req->length = value;
433                         req->zero = value < w_length;
434                         value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
435                         if (value < 0) {
436                                 req->status = 0;
437                                 cdev->req->complete(gadget->ep0, req);
438                         }
439                 }
440                 break;
441         }
442
443         return value;
444 }
445
446 static struct slp_multi_usb_function mtp_function = {
447         .name           = "mtp",
448         .init           = mtp_function_init,
449         .cleanup        = mtp_function_cleanup,
450         .bind_config    = mtp_function_bind_config,
451         .ctrlrequest    = mtp_function_ctrlrequest,
452 };
453
454 struct rndis_function_config {
455         u8 ethaddr[ETH_ALEN];
456         u32 vendorID;
457         char manufacturer[256];
458         bool wceis;
459         u8 rndis_string_defs0_id;
460         struct eth_dev *edev;
461 };
462 static char host_addr_string[18];
463
464 static int rndis_function_init(struct slp_multi_usb_function *f,
465                                struct usb_composite_dev *cdev)
466 {
467         struct rndis_function_config *config;
468         int status, i;
469
470         config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
471         if (!config)
472                 return -ENOMEM;
473
474         /* maybe allocate device-global string IDs */
475         if (rndis_string_defs[0].id == 0) {
476
477                 /* control interface label */
478                 status = usb_string_id(cdev);
479                 if (status < 0)
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;
484
485                 /* data interface label */
486                 status = usb_string_id(cdev);
487                 if (status < 0)
488                         goto rndis_init_error;
489                 rndis_string_defs[1].id = status;
490                 rndis_data_intf.iInterface = status;
491
492                 /* IAD iFunction label */
493                 status = usb_string_id(cdev);
494                 if (status < 0)
495                         goto rndis_init_error;
496                 rndis_string_defs[2].id = status;
497                 rndis_iad_descriptor.iFunction = status;
498         }
499
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];
504         }
505         config->ethaddr[0] &= 0xfe;     /* clear multicast bit */
506         config->ethaddr[0] |= 0x02;     /* set local assignment bit (IEEE802) */
507
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]);
513
514         f->config = config;
515         return 0;
516
517  rndis_init_error:
518         kfree(config);
519         return status;
520 }
521
522 static void rndis_function_cleanup(struct slp_multi_usb_function *f)
523 {
524         kfree(f->config);
525         f->config = NULL;
526 }
527
528 static int rndis_function_bind_config(struct slp_multi_usb_function *f,
529                                       struct usb_configuration *c)
530 {
531         int ret = -EINVAL;
532         struct rndis_function_config *rndis = f->config;
533
534         if (!rndis) {
535                 dev_err(f->dev, "error rndis_pdata is null\n");
536                 return ret;
537         }
538
539         rndis->edev = gether_setup_name(c->cdev->gadget, rndis->ethaddr,"usb");
540
541         if (IS_ERR(rndis->edev)) {
542                 dev_err(f->dev, "gether_setup failed\n");
543                 return ret;
544         }
545
546         if (rndis->wceis) {
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;
556         }
557
558         /* ... and setup RNDIS itself */
559         ret = rndis_init();
560         if (ret < 0) {
561                 dev_err(f->dev, "rndis_init failed(ret:%d)\n", ret);
562                 gether_cleanup(rndis->edev);
563                 return ret;
564         }
565
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
573          */
574         if (rndis_string_defs[0].id == 0)
575                 rndis_string_defs[0].id = rndis->rndis_string_defs0_id;
576
577         ret = rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
578                                  rndis->manufacturer, rndis->edev);
579         if (ret) {
580                 rndis_exit();
581                 gether_cleanup(rndis->edev);
582                 dev_err(f->dev, "rndis_bind_config failed(ret:%d)\n", ret);
583         }
584
585         return ret;
586 }
587
588 static void rndis_function_unbind_config(struct slp_multi_usb_function *f,
589                                          struct usb_configuration *c)
590 {
591         struct rndis_function_config *rndis = f->config;
592         gether_cleanup(rndis->edev);
593 }
594
595 static ssize_t rndis_manufacturer_show(struct device *dev,
596                                        struct device_attribute *attr, char *buf)
597 {
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);
601 }
602
603 static ssize_t rndis_manufacturer_store(struct device *dev,
604                                         struct device_attribute *attr,
605                                         const char *buf, size_t size)
606 {
607         struct slp_multi_usb_function *f = dev_get_drvdata(dev);
608         struct rndis_function_config *config = f->config;
609
610         if ((size >= sizeof(config->manufacturer)) ||
611                 (sscanf(buf, "%s", config->manufacturer) != 1))
612                 return -EINVAL;
613
614         return size;
615 }
616
617 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
618                    rndis_manufacturer_store);
619
620 static ssize_t rndis_wceis_show(struct device *dev,
621                                 struct device_attribute *attr, char *buf)
622 {
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);
626 }
627
628 static ssize_t rndis_wceis_store(struct device *dev,
629                                  struct device_attribute *attr, const char *buf,
630                                  size_t size)
631 {
632         struct slp_multi_usb_function *f = dev_get_drvdata(dev);
633         struct rndis_function_config *config = f->config;
634         int value;
635
636         if (sscanf(buf, "%d", &value) == 1) {
637                 config->wceis = value;
638                 return size;
639         }
640         return -EINVAL;
641 }
642
643 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
644                    rndis_wceis_store);
645
646 static ssize_t rndis_ethaddr_show(struct device *dev,
647                                   struct device_attribute *attr, char *buf)
648 {
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]);
654 }
655
656 static DEVICE_ATTR(ethaddr, S_IRUGO, rndis_ethaddr_show,
657                    NULL);
658
659 static ssize_t rndis_vendorID_show(struct device *dev,
660                                    struct device_attribute *attr, char *buf)
661 {
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);
665 }
666
667 static ssize_t rndis_vendorID_store(struct device *dev,
668                                     struct device_attribute *attr,
669                                     const char *buf, size_t size)
670 {
671         struct slp_multi_usb_function *f = dev_get_drvdata(dev);
672         struct rndis_function_config *config = f->config;
673         int value;
674
675         if (sscanf(buf, "%04x", &value) == 1) {
676                 config->vendorID = value;
677                 return size;
678         }
679         return -EINVAL;
680 }
681
682 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
683                    rndis_vendorID_store);
684
685 static struct device_attribute *rndis_function_attributes[] = {
686         &dev_attr_manufacturer,
687         &dev_attr_wceis,
688         &dev_attr_ethaddr,
689         &dev_attr_vendorID,
690         NULL
691 };
692
693 static struct slp_multi_usb_function rndis_function = {
694         .name = "rndis",
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,
700 };
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)
705 {
706         return vser_init();
707 }
708
709 static void vser_function_cleanup(struct slp_multi_usb_function *f)
710 {
711         vser_cleanup();
712 }
713
714 static int vser_function_bind_config(struct slp_multi_usb_function *f,
715                                                 struct usb_configuration *c)
716 {
717         return vser_bind_config(c);
718 }
719
720 static int vser_function_ctrlrequest(struct slp_multi_usb_function *f,
721                                                 struct usb_composite_dev *cdev,
722                                                 const struct usb_ctrlrequest *c)
723 {
724         return vser_setup(cdev, c);
725 }
726 static struct slp_multi_usb_function vser_function = {
727         .name           = "vser",
728         .init           = vser_function_init,
729         .cleanup        = vser_function_cleanup,
730         .bind_config    = vser_function_bind_config,
731         .ctrlrequest    = vser_function_ctrlrequest,
732 };
733
734 struct gser_function_config {
735         int instances_on;
736         struct usb_function *f_gser[GSER_PORT_MAX_COUNT];
737         struct usb_function_instance *f_gser_inst[GSER_PORT_MAX_COUNT];
738 };
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)
742 {
743         int i;
744         int ret;
745         struct gser_function_config *config;
746
747         config = kzalloc(sizeof(struct gser_function_config), GFP_KERNEL);
748         if (!config)
749                 return -ENOMEM;
750         f->config = config;
751
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;
757                 }
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;
762                 }
763         }
764         return 0;
765 err_usb_get_function_instance:
766         while (i-- > 0) {
767                 usb_put_function(config->f_gser[i]);
768 err_usb_get_function:
769                 usb_put_function_instance(config->f_gser_inst[i]);
770         }
771         return ret;
772 }
773
774 static void gser_function_cleanup(struct slp_multi_usb_function *f)
775 {
776         int i;
777         struct gser_function_config *config = f->config;
778
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]);
782         }
783         kfree(f->config);
784         f->config = NULL;
785 }
786
787 static int gser_function_bind_config(struct slp_multi_usb_function *f,
788                                                 struct usb_configuration *c)
789 {
790         int i;
791         int ret = 0;
792         struct gser_function_config *config = f->config;
793
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]);
797                 if (ret) {
798                         pr_err("Could not bind gser%u config\n", i);
799                         goto err_usb_add_function;
800                 }
801         }
802
803         return 0;
804
805 err_usb_add_function:
806         while (i-- > 0)
807                 usb_remove_function(c, config->f_gser[i]);
808         return ret;
809 }
810 /**
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
813  *the gser function;
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.
816  */
817 static void gser_function_unbind_config(struct slp_multi_usb_function *f,
818                                        struct usb_configuration *c)
819 {
820         int i;
821         struct gser_function_config *config = f->config;
822
823         for (i = 0; i < config->instances_on; i++)
824                 usb_remove_function(c, config->f_gser[i]);
825 }
826
827 static void gser_setup_complete(struct usb_ep *ep, struct usb_request *req)
828 {
829 }
830
831 static int gser_setup(struct usb_composite_dev *cdev, const struct usb_ctrlrequest *ctrl)
832 {
833         u16 w_length = le16_to_cpu(ctrl->wLength);
834         int value = -EOPNOTSUPP;
835
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) {
840                         case 0x22:
841                                 value = 0;
842                                 break;
843                 }
844         }
845
846         /* respond with data transfer or status phase? */
847         if (value >= 0) {
848                 int rc;
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);
853                 if (rc < 0)
854                         printk("%s setup response queue error\n", __func__);
855         }
856
857         if (value == -EOPNOTSUPP)
858                 VDBG(cdev,
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));
864         return value;
865 }
866
867 static int gser_function_ctrlrequest(struct slp_multi_usb_function *f,
868                                                 struct usb_composite_dev *cdev,
869                                                 const struct usb_ctrlrequest *c)
870 {
871         return gser_setup(cdev, c);
872 }
873 static ssize_t gser_port_store(struct device *dev,
874                 struct device_attribute *attr, char *buf)
875 {
876         int     count;
877
878         printk("%s %s\n",__func__,buf);
879         sscanf(buf, "%d", &count);
880         if(count > GSER_PORT_MAX_COUNT)
881                 return -1;
882         gser_port_count = count;
883         return count;
884 }
885 static ssize_t gser_port_show(struct device *dev,
886                 struct device_attribute *attr, char *buf)
887 {
888         /* print gser instance numbers */
889         return sprintf(buf, "%d \n",gser_port_count );
890 }
891 static DEVICE_ATTR(port_count, S_IRUGO | S_IWUSR, gser_port_show, gser_port_store);
892
893 static struct device_attribute *gser_function_attributes[] = {
894         &dev_attr_port_count,
895         NULL
896 };
897 static struct slp_multi_usb_function gser_function = {
898         .name           = "gser",
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,
905 };
906 #endif
907
908 /*-------------------------------------------------------------------------*/
909 /* Supported functions initialization */
910
911 static struct slp_multi_usb_function *supported_functions[] = {
912         &sdb_function,
913         &acm_function,
914         &mtp_function,
915         &rndis_function,
916 #ifdef CONFIG_USB_SPRD_DWC
917         &vser_function,
918         &gser_function,
919 #endif
920         NULL,
921 };
922
923 static void slp_multi_evt_emit(struct slp_multi_dev *smdev,
924                                 struct slp_multi_evt *evt)
925 {
926         int err;
927
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,
933                                         evt->qos);
934                         dev_info(smdev->dev, "PM QOS value changed to %s\n",
935                                 (evt->qos != PM_QOS_DEFAULT_VALUE) ?
936                                         "high" : "low");
937                 }
938                 break;
939
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);
946                         if (err < 0)
947                                 dev_err(smdev->dev, "can't send usb_state[%d]\n",
948                                 evt->ustate);
949                         else
950                                 dev_info(smdev->dev, "uevent usb_state [%s]\n",
951                                 (smdev->ustate == USB_STATE_CONFIGURED) ?
952                                 "CONFIGURED" : "OTHERS");
953                 }
954                 break;
955
956         default:
957                 dev_err(smdev->dev, "not supported type(%d)\n", evt->evt_type);
958                 break;
959         }
960 }
961
962 static void slp_multi_evt_thread(struct work_struct *data)
963 {
964         struct slp_multi_dev *smdev = _slp_multi_dev;
965         LIST_HEAD(evt_list);
966
967         mutex_lock(&smdev->evt_mutex);
968         if (!smdev->enabled)
969                 goto evt_thread_done;
970
971         while (1) {
972                 struct slp_multi_evt *evt;
973                 struct list_head *this, *tmp;
974                 unsigned long flags;
975
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);
979
980                 if (list_empty(&evt_list))
981                         break;
982
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);
987                         kfree(evt);
988                 }
989         }
990
991 evt_thread_done:
992         mutex_unlock(&smdev->evt_mutex);
993 }
994
995 static void slp_multi_evt_send(struct slp_multi_dev *smdev,
996                                 struct slp_multi_evt *evt)
997 {
998         unsigned long flags;
999
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);
1004 }
1005
1006 static void slp_multi_qos_evt(struct slp_multi_dev *smdev,
1007                                 s32 new_value)
1008 {
1009         struct slp_multi_evt *evt;
1010
1011         evt = kzalloc(sizeof(struct slp_multi_evt), GFP_ATOMIC);
1012         if (!evt) {
1013                 dev_err(smdev->dev, "can't queue qos(%d) by ENOMEM\n",
1014                         new_value);
1015                 return;
1016         }
1017
1018         evt->evt_type = SMDEV_EVT_QOS_CHANGE;
1019         INIT_LIST_HEAD(&evt->node);
1020         evt->qos = new_value;
1021
1022         slp_multi_evt_send(smdev, evt);
1023 }
1024
1025 static void slp_multi_state_evt(struct slp_multi_dev *smdev,
1026                                 enum usb_device_state new_state)
1027 {
1028         struct slp_multi_evt *evt;
1029
1030         evt = kzalloc(sizeof(struct slp_multi_evt), GFP_ATOMIC);
1031         if (!evt) {
1032                 dev_err(smdev->dev, "can't queue [%d] evt by ENOMEM\n",
1033                         new_state);
1034                 return;
1035         }
1036
1037         evt->evt_type = SMDEV_EVT_STATE_CHANGE;
1038         INIT_LIST_HEAD(&evt->node);
1039         evt->ustate = new_state;
1040
1041         slp_multi_evt_send(smdev, evt);
1042 }
1043
1044 static int slp_multi_init_functions(struct slp_multi_dev *smdev,
1045                                   struct usb_composite_dev *cdev)
1046 {
1047         struct slp_multi_usb_function *f;
1048         struct device_attribute **attrs;
1049         struct device_attribute *attr;
1050         int err = 0;
1051         int index = 0;
1052
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)) {
1058                         dev_err(smdev->dev,
1059                                 "Failed to create dev %s", f->dev_name);
1060                         err = PTR_ERR(f->dev);
1061                         goto init_func_err_create;
1062                 }
1063
1064                 if (f->init) {
1065                         err = f->init(f, cdev);
1066                         if (err) {
1067                                 dev_err(smdev->dev,
1068                                         "Failed to init %s", f->name);
1069                                 goto init_func_err_out;
1070                         }
1071                 }
1072
1073                 attrs = f->attributes;
1074                 if (attrs) {
1075                         while ((attr = *attrs++) && !err)
1076                                 err = device_create_file(f->dev, attr);
1077                 }
1078                 if (err) {
1079                         dev_err(f->dev, "Failed to create function %s attributes",
1080                                f->name);
1081                         goto init_func_err_out;
1082                 }
1083         }
1084         return 0;
1085
1086  init_func_err_out:
1087         device_destroy(slp_multi_class, f->dev->devt);
1088  init_func_err_create:
1089         kfree(f->dev_name);
1090         return err;
1091 }
1092
1093 static void slp_multi_cleanup_functions(struct slp_multi_dev *smdev)
1094 {
1095         struct slp_multi_usb_function *f;
1096
1097         list_for_each_entry(f, &smdev->available_functions, available_list) {
1098                 if (f->dev) {
1099                         device_destroy(slp_multi_class, f->dev->devt);
1100                         kfree(f->dev_name);
1101                 }
1102
1103                 if (f->cleanup)
1104                         f->cleanup(f);
1105         }
1106 }
1107
1108 static int
1109 slp_multi_bind_enabled_functions(struct slp_multi_dev *smdev,
1110                                struct usb_configuration *c)
1111 {
1112         struct slp_multi_usb_function *f;
1113         int ret;
1114
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",
1118                                 f->name);
1119                         ret = f->bind_config(f, c);
1120                         if (ret) {
1121                                 dev_err(smdev->dev, "%s bind_conf(1st) failed\n",
1122                                         f->name);
1123                                 return ret;
1124                         }
1125                 }
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",
1129                                 f->name);
1130                         ret = f->bind_config(f, c);
1131                         if (ret) {
1132                                 dev_err(smdev->dev, "%s bind_conf(2nd) failed\n",
1133                                         f->name);
1134                                 return ret;
1135                         }
1136                 }
1137         } else {
1138                 dev_err(smdev->dev, "Not supported configuraton(%d)\n",
1139                         c->bConfigurationValue);
1140                 return -EINVAL;
1141         }
1142         return 0;
1143 }
1144
1145 static void
1146 slp_multi_unbind_enabled_functions(struct slp_multi_dev *smdev,
1147                                  struct usb_configuration *c)
1148 {
1149         struct slp_multi_usb_function *f;
1150
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);
1155                 }
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);
1160                 }
1161         }
1162 }
1163
1164 #define ADD_FUNCS_LIST(head, member)    \
1165 static inline int add_##member(struct slp_multi_dev *smdev, char *name) \
1166 {       \
1167         struct slp_multi_usb_function *av_f, *en_f;     \
1168         \
1169         dev_dbg(smdev->dev, "usb: name=%s\n", name);    \
1170         list_for_each_entry(av_f, &smdev->available_functions,  \
1171                         available_list) {       \
1172                 if (!strcmp(name, av_f->name)) {        \
1173                         list_for_each_entry(en_f, &smdev->head, \
1174                                         member) {       \
1175                                 if (av_f == en_f) {     \
1176                                         dev_info(smdev->dev, \
1177                                                 "usb:%s already enabled!\n", \
1178                                                 name);  \
1179                                         return 0;       \
1180                                 }       \
1181                         }       \
1182                         list_add_tail(&av_f->member, &smdev->head);     \
1183                         return 0;       \
1184                 }       \
1185         }       \
1186         return -EINVAL; \
1187 }       \
1188 static ssize_t  show_##head(struct device *pdev,        \
1189                         struct device_attribute *attr, char *buf)       \
1190 {       \
1191         struct slp_multi_dev *smdev = dev_get_drvdata(pdev);    \
1192         struct slp_multi_usb_function *f;       \
1193         char *buff = buf;       \
1194         \
1195         list_for_each_entry(f, &smdev->head, member) {  \
1196                 dev_dbg(pdev, "usb: enabled_func=%s\n", \
1197                        f->name);        \
1198                 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);      \
1199         }       \
1200         if (buff != buf)        \
1201                 *(buff - 1) = '\n';     \
1202         \
1203         return buff - buf;      \
1204 }       \
1205 static ssize_t store_##head(struct device *pdev,        \
1206                 struct device_attribute *attr,  \
1207                 const char *buff, size_t size)  \
1208 {       \
1209         struct slp_multi_dev *smdev = dev_get_drvdata(pdev);    \
1210         char *name;     \
1211         char buf[256], *b;      \
1212         int err;        \
1213         \
1214         if (smdev->enabled) {   \
1215                 dev_info(pdev, "can't change usb functions"     \
1216                         "(already enabled)!!\n");       \
1217                 return -EBUSY;  \
1218         }       \
1219         \
1220         INIT_LIST_HEAD(&smdev->head);   \
1221         \
1222         dev_dbg(pdev, "usb: buff=%s\n", buff);  \
1223         strlcpy(buf, buff, sizeof(buf));        \
1224         b = strim(buf); \
1225         \
1226         while (b) {     \
1227                 name = strsep(&b, ","); \
1228                 if (name) {     \
1229                         err = add_##member(smdev, name);        \
1230                         if (err)        \
1231                                 dev_err(pdev, \
1232                                         "slp_multi_usb: Cannot enable '%s'", \
1233                                         name); \
1234                         else            \
1235                 dev_err(pdev, \
1236                     "slp_multi_usb: enable SUCCESS '%s'", \
1237                     name); \
1238                 }       \
1239         }       \
1240         \
1241         return size;    \
1242 }       \
1243 static DEVICE_ATTR(head, S_IRUGO | S_IWUSR, show_##head, store_##head);
1244
1245 ADD_FUNCS_LIST(funcs_fconf, fconf_list)
1246 ADD_FUNCS_LIST(funcs_sconf, sconf_list)
1247
1248 /*-------------------------------------------------------------------------*/
1249 /* /sys/class/usb_mode/usb%d/ interface */
1250
1251 static ssize_t pm_qos_show(struct device *pdev,
1252                            struct device_attribute *attr, char *buf)
1253 {
1254         struct slp_multi_dev *smdev = dev_get_drvdata(pdev);
1255
1256         return snprintf(buf, PAGE_SIZE, "%s\n", smdev->pm_qos);
1257 }
1258
1259 static ssize_t pm_qos_store(struct device *pdev,
1260                            struct device_attribute *attr,
1261                            const char *buff, size_t size)
1262 {
1263         struct slp_multi_dev *smdev = dev_get_drvdata(pdev);
1264
1265         if (smdev->enabled) {
1266                 dev_info(pdev, "Already usb enabled, can't change qos\n");
1267                 return -EBUSY;
1268         }
1269
1270         if (!(strncmp(buff, "high", 4)) || !(strncmp(buff, "low", 3))) {
1271                 dev_err(pdev, "not supported cmd, can't set it\n");
1272                 return -EINVAL;
1273         }
1274
1275         strlcpy(smdev->pm_qos, buff, sizeof(smdev->pm_qos));
1276         return size;
1277 }
1278
1279 static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR, pm_qos_show, pm_qos_store);
1280
1281 static ssize_t enable_show(struct device *pdev,
1282                            struct device_attribute *attr, char *buf)
1283 {
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);
1287 }
1288
1289 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1290                             const char *buff, size_t size)
1291 {
1292         struct slp_multi_dev *smdev = dev_get_drvdata(pdev);
1293         struct usb_composite_dev *cdev = smdev->cdev;
1294         struct list_head *this, *tmp;
1295         int enabled;
1296         int ret = 0;
1297
1298         if (sysfs_streq(buff, "1"))
1299                 enabled = 1;
1300         else if (sysfs_streq(buff, "0"))
1301                 enabled = 0;
1302         else {
1303                 dev_err(pdev, "Invalid cmd %c%c..", *buff, *(buff+1));
1304                 return -EINVAL;
1305         }
1306
1307         dev_dbg(pdev, "usb: %s enabled=%d, !smdev->enabled=%d\n",
1308                __func__, enabled, !smdev->enabled);
1309
1310         mutex_lock(&smdev->enable_lock);
1311
1312         if (enabled && !smdev->enabled) {
1313                 struct slp_multi_usb_function *f;
1314
1315 #if defined(CONFIG_USB_G_ANDROID_SAMSUNG_COMPOSITE) || defined(CONFIG_USB_G_SLP)
1316                 cdev->next_string_id = composite_string_index;
1317 #else
1318                 cdev->next_string_id = 0;
1319 #endif
1320                 /* update values in composite driver's
1321                  * copy of device descriptor
1322                  */
1323                 cdev->desc.idVendor = device_desc.idVendor;
1324                 cdev->desc.idProduct = device_desc.idProduct;
1325                 cdev->desc.bcdDevice = device_desc.bcdDevice;
1326
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);
1334                         }
1335                 }
1336
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);
1344                         }
1345                         smdev->dual_config = true;
1346                 }
1347                 strncpy(manufacturer_string, "SAMSUNG",
1348                                 sizeof(manufacturer_string) - 1);
1349                 strncpy(product_string, "SLP",
1350                                 sizeof(product_string) - 1);
1351
1352                 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1353                 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1354                 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1355
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",
1363                        __func__);
1364
1365                 ret = usb_add_config(cdev,
1366                                 &first_config_driver, slp_multi_bind_config);
1367                 if (ret < 0) {
1368                         dev_err(pdev,
1369                                 "usb_add_config fail-1st(%d)\n", ret);
1370                         smdev->dual_config = false;
1371                         goto done;
1372                 }
1373
1374                 if (smdev->dual_config) {
1375                         ret = usb_add_config(cdev, &second_config_driver,
1376                                        slp_multi_bind_config);
1377                         if (ret < 0) {
1378                                 dev_err(pdev,
1379                                         "usb_add_config fail-2nd(%d)\n", ret);
1380                                 smdev->dual_config = false;
1381                                 goto enable_conf_err;
1382                         }
1383                 }
1384
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");
1392                 }
1393
1394                 smdev->enabled = true;
1395
1396                 ret = usb_gadget_connect(cdev->gadget);
1397                 if (ret < 0) {
1398                         dev_err(pdev, "can't connected gadget(%d)\n", ret);
1399                         smdev->enabled = false;
1400                         goto enable_conf_err;
1401                 }
1402
1403         } else if (!enabled && smdev->enabled) {
1404                 usb_gadget_disconnect(cdev->gadget);
1405
1406                 smdev->enabled = false;
1407
1408                 /* Cancel pending control requests if it available */
1409                 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
1410
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;
1415
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;
1421
1422                                 evt = list_entry(this,
1423                                         struct slp_multi_evt, node);
1424                                 list_del(&evt->node);
1425                                 kfree(evt);
1426                         }
1427                 }
1428                 mutex_unlock(&smdev->evt_mutex);
1429
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");
1435                 }
1436
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);
1441                 }
1442
1443         } else {
1444                 dev_info(pdev, "slp_multi_usb: already %s\n",
1445                        smdev->enabled ? "enabled" : "disabled");
1446         }
1447
1448         goto done;
1449
1450 enable_conf_err:
1451         if (smdev->dual_config) {
1452                 usb_remove_config(cdev, &second_config_driver);
1453                 smdev->dual_config = false;
1454         }
1455         usb_remove_config(cdev, &first_config_driver);
1456
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;
1461
1462                         evt = list_entry(this, struct slp_multi_evt, node);
1463                         list_del(&evt->node);
1464                         kfree(evt);
1465                 }
1466         }
1467         mutex_unlock(&smdev->evt_mutex);
1468
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);
1473         }
1474
1475 done:
1476         mutex_unlock(&smdev->enable_lock);
1477         return (ret < 0 ? ret : size);
1478 }
1479
1480 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1481
1482 #define DESCRIPTOR_ATTR(field, format_string)                           \
1483 static ssize_t                                                          \
1484 field ## _show(struct device *dev, struct device_attribute *attr,       \
1485                 char *buf)                                              \
1486 {                                                                       \
1487         return snprintf(buf, PAGE_SIZE, format_string, device_desc.field);\
1488 }                                                                       \
1489 static ssize_t                                                          \
1490 field ## _store(struct device *dev, struct device_attribute *attr,      \
1491                 const char *buf, size_t size)   \
1492 {                                                                       \
1493         int value;      \
1494         if (sscanf(buf, format_string, &value) == 1) {                  \
1495                 device_desc.field = value;                              \
1496                 return size;                                            \
1497         }                                                               \
1498         return -EINVAL;                                                 \
1499 }                                                                       \
1500 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1501
1502 #define DESCRIPTOR_STRING_ATTR(field, buffer)   \
1503 static ssize_t  \
1504 field ## _show(struct device *dev, struct device_attribute *attr,       \
1505                 char *buf)      \
1506 {       \
1507         return snprintf(buf, PAGE_SIZE, "%s", buffer);  \
1508 }       \
1509 static ssize_t  \
1510 field ## _store(struct device *dev, struct device_attribute *attr,      \
1511                 const char *buf, size_t size)   \
1512 {       \
1513         if ((size >= sizeof(buffer)) || \
1514                 (sscanf(buf, "%s", buffer) != 1)) {     \
1515                 return -EINVAL; \
1516         }       \
1517         return size;    \
1518 }       \
1519 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1520
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)
1530
1531 static struct device_attribute *slp_multi_usb_attributes[] = {
1532         &dev_attr_idVendor,
1533         &dev_attr_idProduct,
1534         &dev_attr_bcdDevice,
1535         &dev_attr_bDeviceClass,
1536         &dev_attr_bDeviceSubClass,
1537         &dev_attr_bDeviceProtocol,
1538         &dev_attr_iManufacturer,
1539         &dev_attr_iProduct,
1540         &dev_attr_iSerial,
1541         &dev_attr_funcs_fconf,
1542         &dev_attr_funcs_sconf,
1543         &dev_attr_enable,
1544         &dev_attr_pm_qos,
1545         NULL
1546 };
1547
1548 /*-------------------------------------------------------------------------*/
1549 /* Composite driver */
1550
1551 static int slp_multi_bind_config(struct usb_configuration *c)
1552 {
1553         struct slp_multi_dev *smdev = _slp_multi_dev;
1554         int ret = 0;
1555
1556         ret = slp_multi_bind_enabled_functions(smdev, c);
1557
1558         return ret;
1559 }
1560
1561 static void slp_multi_unbind_config(struct usb_configuration *c)
1562 {
1563         struct slp_multi_dev *smdev = _slp_multi_dev;
1564
1565
1566         slp_multi_unbind_enabled_functions(smdev, c);
1567 }
1568
1569 static int slp_multi_bind(struct usb_composite_dev *cdev)
1570 {
1571         struct slp_multi_dev *smdev = _slp_multi_dev;
1572         struct usb_gadget *gadget = cdev->gadget;
1573         int id, ret;
1574
1575         dev_dbg(smdev->dev, "usb: %s disconnect\n", __func__);
1576         usb_gadget_disconnect(gadget);
1577
1578         /* Allocate string descriptor numbers ... note that string
1579          * contents can be overridden by the composite_dev glue.
1580          */
1581         id = usb_string_id(cdev);
1582         if (id < 0)
1583                 return id;
1584         strings_dev[STRING_MANUFACTURER_IDX].id = id;
1585         device_desc.iManufacturer = id;
1586
1587         id = usb_string_id(cdev);
1588         if (id < 0)
1589                 return id;
1590         strings_dev[STRING_PRODUCT_IDX].id = id;
1591         device_desc.iProduct = id;
1592
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);
1599
1600         id = usb_string_id(cdev);
1601         if (id < 0)
1602                 return id;
1603         strings_dev[STRING_SERIAL_IDX].id = id;
1604         device_desc.iSerialNumber = id;
1605
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;
1611 #endif
1612         ret = slp_multi_init_functions(smdev, cdev);
1613         if (ret)
1614                 return ret;
1615
1616         usb_gadget_set_selfpowered(gadget);
1617         smdev->cdev = cdev;
1618
1619         return 0;
1620 }
1621
1622 static int slp_multi_usb_unbind(struct usb_composite_dev *cdev)
1623 {
1624         struct slp_multi_dev *smdev = _slp_multi_dev;
1625         dev_dbg(smdev->dev, "usb: %s\n", __func__);
1626         slp_multi_cleanup_functions(smdev);
1627         return 0;
1628 }
1629
1630 static void slp_multi_usb_disconnect(struct usb_composite_dev *cdev)
1631 {
1632         struct slp_multi_dev *smdev = _slp_multi_dev;
1633         dev_dbg(smdev->dev, "usb:%s (%d)\n", __func__, __LINE__);
1634
1635         /* to prevent evt queuing during dis/enable control*/
1636         if (list_empty(&cdev->configs))
1637                 return;
1638
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);
1642         }
1643
1644         dev_info(smdev->dev, "queue disconnect evt\n");
1645         slp_multi_state_evt(smdev, USB_STATE_NOTATTACHED);
1646 }
1647
1648 static void slp_multi_usb_resume(struct usb_composite_dev *cdev)
1649 {
1650         struct slp_multi_dev *smdev = _slp_multi_dev;
1651
1652         dev_dbg(smdev->dev, "usb: %s\n", __func__);
1653
1654         /* to prevent evt queuing during dis/enable control*/
1655         if (list_empty(&cdev->configs))
1656                 return;
1657
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);
1661         }
1662 }
1663
1664 static void slp_multi_usb_suspend(struct usb_composite_dev *cdev)
1665 {
1666         struct slp_multi_dev *smdev = _slp_multi_dev;
1667
1668         dev_dbg(smdev->dev, "usb: %s\n", __func__);
1669
1670         /* to prevent evt queuing during dis/enable control*/
1671         if (list_empty(&cdev->configs))
1672                 return;
1673
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);
1677         }
1678 }
1679
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,
1690 };
1691
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);
1695
1696 static int
1697 slp_multi_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1698 {
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;
1704
1705         if (smdev && ((b_requestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)) {
1706                 struct usb_request *req = cdev->req;
1707
1708                 req->zero = 0;
1709                 req->length = 0;
1710                 gadget->ep0->driver_data = cdev;
1711
1712                 /* To check & report it to platform , we check it all */
1713                 list_for_each_entry(f, &smdev->available_functions,
1714                         available_list) {
1715                         if (f->ctrlrequest) {
1716                                 value = f->ctrlrequest(f, cdev, ctrl);
1717                                 if (value >= 0)
1718                                         break;
1719                         }
1720                 }
1721         }
1722
1723         if (value < 0)
1724                 value = composite_setup_func(gadget, ctrl);
1725
1726         if (smdev && (value >= 0) && (cdev->config)
1727                 && (ctrl->bRequest == USB_REQ_SET_CONFIGURATION))
1728                 slp_multi_state_evt(smdev, USB_STATE_CONFIGURED);
1729
1730         return value;
1731 }
1732
1733 static struct slp_multi_dev *slp_multi_create_device(struct device *pdev,
1734                                 struct slp_multi_platform_data *pdata)
1735 {
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;
1741         int i, err;
1742
1743         smdev = kzalloc(sizeof(*smdev), GFP_KERNEL);
1744         if (!smdev) {
1745                 dev_err(pdev, "usb_mode: can't alloc for smdev\n");
1746                 return ERR_PTR(-ENOMEM);
1747         }
1748
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);
1753
1754         mutex_init(&smdev->evt_mutex);
1755         mutex_init(&smdev->enable_lock);
1756         spin_lock_init(&smdev->evt_lock);
1757
1758         INIT_WORK(&smdev->evt_work, slp_multi_evt_thread);
1759
1760         smdev->ustate = USB_STATE_NOTATTACHED;
1761
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);
1767         }
1768
1769         smdev->dev = device_create(slp_multi_class, NULL,
1770                                   MKDEV(0, 0), NULL, "usb0");
1771         if (IS_ERR(smdev->dev)) {
1772                 kfree(smdev);
1773                 return ERR_PTR(-ENODEV);
1774         }
1775
1776         dev_set_drvdata(smdev->dev, smdev);
1777
1778         while ((attr = *attrs++)) {
1779                 err = device_create_file(smdev->dev, attr);
1780                 if (err) {
1781                         dev_set_drvdata(smdev->dev, NULL);
1782                         device_destroy(slp_multi_class, smdev->dev->devt);
1783                         return ERR_PTR(err);
1784                 }
1785         }
1786         return smdev;
1787 }
1788
1789 static void slp_multi_destroy_device(struct slp_multi_dev *smdev)
1790 {
1791         struct device_attribute **attrs = slp_multi_usb_attributes;
1792         struct device_attribute *attr;
1793         struct list_head *this, *tmp;
1794
1795
1796         while ((attr = *attrs++))
1797                 device_destroy(slp_multi_class, smdev->dev->devt);
1798
1799         cancel_work_sync(&smdev->evt_work);
1800
1801         if (!list_empty(&smdev->evt_list)) {
1802                 list_for_each_safe(this, tmp, &smdev->evt_list) {
1803                         struct slp_multi_evt *evt;
1804
1805                         evt = list_entry(this, struct slp_multi_evt, node);
1806                         list_del(&evt->node);
1807                         kfree(evt);
1808                 }
1809         }
1810
1811         dev_set_drvdata(smdev->dev, NULL);
1812
1813         device_unregister(smdev->dev);
1814 }
1815
1816 static int slp_multi_probe(struct platform_device *pdev)
1817 {
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;
1822         int i, err;
1823
1824         if (np) {
1825                 dev_err(&pdev->dev, "usb : %s device tree enabled\n", __func__);
1826                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1827                 if (!pdata) {
1828                         dev_err(&pdev->dev, "usb : %s unable to allocate platform"
1829                                 "data\n", __func__);
1830                         return -ENOMEM;
1831                 }
1832
1833                 if (of_find_property(np, "slp,slp_mult-nluns", NULL))
1834                         of_property_read_u32(np, "slp,slp_mult-nluns",
1835                                 &pdata->nluns);
1836
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);
1840
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");
1844
1845                         if (pdata->nfuncs < 0) {
1846                                 dev_err(&pdev->dev,
1847                                         "fail to count enable-funcs(%d)\n",
1848                                                 pdata->nfuncs);
1849                                 return -EINVAL;
1850                         }
1851
1852                         enable_funcs = devm_kzalloc(&pdev->dev,
1853                                 pdata->nfuncs * sizeof(**enable_funcs),
1854                                         GFP_KERNEL);
1855                         if (!enable_funcs) {
1856                                 dev_err(&pdev->dev, "usb : %s unable to allocate"
1857                                         "platform data\n", __func__);
1858                                 return -ENOMEM;
1859                         }
1860
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;
1866                 } else {
1867                         dev_err(&pdev->dev, "usb : %s There is no"
1868                                 "enable-functions\n", __func__);
1869                         return -EINVAL;
1870                 }
1871         } else {
1872                 pdata = pdev->dev.platform_data;
1873         }
1874
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);
1879         }
1880
1881         slp_multi_nluns = pdata->nluns;
1882         _slp_multi_dev = smdev;
1883
1884         err = usb_composite_probe(&slp_multi_composite);
1885         if (err) {
1886                 dev_err(&pdev->dev, "usb_mode:  %s can't probe composite\n", __func__);
1887                 goto err_comp_prb;
1888         }
1889
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));
1895         } else {
1896                 smdev->swfi_latency = PM_QOS_DEFAULT_VALUE;
1897                 strlcpy(smdev->pm_qos, "NONE", sizeof(smdev->pm_qos));
1898         }
1899         smdev->curr_latency = PM_QOS_DEFAULT_VALUE;
1900
1901         platform_set_drvdata(pdev, smdev);
1902
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;
1907
1908         dev_err(&pdev->dev, "usb : %s usb_mode driver, version:" USB_MODE_VERSION
1909                "," " init Ok\n", __func__);
1910
1911         return 0;
1912
1913  err_comp_prb:
1914         platform_set_drvdata(pdev, NULL);
1915         slp_multi_destroy_device(smdev);
1916
1917         return err;
1918 }
1919
1920 static int slp_multi_remove(struct platform_device *pdev)
1921 {
1922         struct slp_multi_platform_data *pdata = pdev->dev.platform_data;
1923         struct slp_multi_dev *smdev = platform_get_drvdata(pdev);
1924
1925         if (smdev->enabled) {
1926                 dev_info(&pdev->dev, "start remove usb_mode driver without disabled\n");
1927                 smdev->enabled = false;
1928         }
1929
1930         usb_composite_unregister(&slp_multi_composite);
1931
1932         platform_set_drvdata(pdev, NULL);
1933
1934         if (pdata->swfi_latency != PM_QOS_DEFAULT_VALUE)
1935                 pm_qos_remove_request(&smdev->pm_qos_req_dma);
1936
1937         slp_multi_destroy_device(smdev);
1938         kfree(_slp_multi_dev);
1939         _slp_multi_dev = NULL;
1940
1941         return 0;
1942 }
1943
1944 #ifdef CONFIG_OF
1945 static const struct of_device_id of_slp_multi_match[] = {
1946         {
1947                 .compatible = "slp,slp_multi",
1948         },
1949         { },
1950 };
1951 MODULE_DEVICE_TABLE(of, of_slp_multi_match);
1952 #endif
1953
1954 static struct platform_driver slp_multi_driver = {
1955         .probe = slp_multi_probe,
1956         .remove = slp_multi_remove,
1957         .driver = {
1958                    .name = "slp_multi",
1959                    .owner = THIS_MODULE,
1960                    .of_match_table = of_match_ptr(of_slp_multi_match),
1961         },
1962 };
1963
1964 static CLASS_ATTR_STRING(version, S_IRUSR | S_IRGRP | S_IROTH,
1965                          USB_MODE_VERSION);
1966
1967 static int slp_multi_uevent(struct device *dev, struct kobj_uevent_env *env)
1968 {
1969         struct slp_multi_dev *smdev = _slp_multi_dev;
1970         char *state_str;
1971         int ret;
1972
1973         if (!smdev) {
1974                 dev_info(dev, "not yet created smdev!\n");
1975                 return -ENODEV;
1976         }
1977
1978         switch (smdev->ustate) {
1979         case USB_STATE_NOTATTACHED:
1980                 state_str = "NOTATTACHED";
1981                 break;
1982
1983         case USB_STATE_CONFIGURED:
1984                 state_str = "CONFIGURED";
1985                 break;
1986
1987         default:
1988                 dev_err(dev, "not supported usb_state(%d) for uevent\n",
1989                         smdev->ustate);
1990                 return -EINVAL;
1991         }
1992
1993         ret = add_uevent_var(env, "USB_STATE=%s", state_str);
1994         if (ret)
1995                 dev_err(dev, "failed to add uevent USB_STATE\n");
1996
1997         return ret;
1998 }
1999
2000 static int __init slp_multi_init(void)
2001 {
2002         int err;
2003
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);
2009         }
2010
2011         slp_multi_class->dev_uevent = slp_multi_uevent;
2012
2013         err = class_create_file(slp_multi_class, &class_attr_version.attr);
2014         if (err) {
2015                 pr_err("usb_mode: can't create sysfs version file\n");
2016                 goto err_class;
2017         }
2018
2019         err = platform_driver_register(&slp_multi_driver);
2020         if (err) {
2021                 pr_err("usb_mode: can't register driver\n");
2022                 goto err_attr;
2023         }
2024
2025         return 0;
2026
2027 err_attr:
2028         class_remove_file(slp_multi_class, &class_attr_version.attr);
2029 err_class:
2030         class_destroy(slp_multi_class);
2031
2032         return err;
2033 }
2034 late_initcall(slp_multi_init);
2035
2036 static void __exit slp_multi_exit(void)
2037 {
2038         platform_driver_unregister(&slp_multi_driver);
2039         class_remove_file(slp_multi_class, &class_attr_version.attr);
2040         class_destroy(slp_multi_class);
2041 }
2042 module_exit(slp_multi_exit);