blob: eebd9d426781aa1db0c86bf728545cc7ef89e582 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* drivers/usb/function/msm_hsusb.c
2 *
3 * Driver for HighSpeed USB Client Controller in MSM7K
4 *
5 * Copyright (C) 2007 Google, Inc.
6 * Copyright (c) 2008-2009, Code Aurora Forum. All rights reserved.
7 * Author: Brian Swetland <swetland@google.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/list.h>
23
24#include <linux/delay.h>
25#include <linux/interrupt.h>
26#include <linux/dma-mapping.h>
27#include <linux/dmapool.h>
28#include <linux/platform_device.h>
29#include <linux/debugfs.h>
30#include <linux/workqueue.h>
31#include <linux/clk.h>
32#include <linux/spinlock.h>
33#include <linux/switch.h>
34
35#include <linux/usb/ch9.h>
36#include <linux/io.h>
37
38#include <asm/mach-types.h>
39#include <mach/vreg.h>
40#include <mach/board.h>
41#include <mach/msm_hsusb.h>
42#include <mach/rpc_hsusb.h>
43#include <mach/rpc_pmapp.h>
44#include <mach/gpio.h>
45#include <mach/msm_hsusb_hw.h>
46#include <mach/msm_otg.h>
47#include <linux/wakelock.h>
48#include <linux/pm_qos_params.h>
49#include <mach/clk.h>
50
51#define MSM_USB_BASE ((unsigned) ui->addr)
52
53#include "usb_function.h"
54
55#define EPT_FLAG_IN 0x0001
56#define USB_DIR_MASK USB_DIR_IN
57#define SETUP_BUF_SIZE 4096
58
59/* IDs for string descriptors */
60#define STRING_LANGUAGE_ID 0
61#define STRING_SERIAL 1
62#define STRING_PRODUCT 2
63#define STRING_MANUFACTURER 3
64
65#define LANGUAGE_ID 0x0409 /* en-US */
66#define SOC_ROC_2_0 0x10002 /* ROC 2.0 */
67
68#define TRUE 1
69#define FALSE 0
70#define USB_LINK_RESET_TIMEOUT (msecs_to_jiffies(10))
71#define USB_CHG_DET_DELAY msecs_to_jiffies(1000)
72
73#define is_phy_45nm() (PHY_MODEL(ui->phy_info) == USB_PHY_MODEL_45NM)
74#define is_phy_external() (PHY_TYPE(ui->phy_info) == USB_PHY_EXTERNAL)
75
76static int pid = 0x9018;
77
78struct usb_fi_ept {
79 struct usb_endpoint *ept;
80 struct usb_endpoint_descriptor desc;
81};
82
83struct usb_function_info {
84 struct list_head list;
85 unsigned enabled;
86 struct usb_function *func;
87};
88
89struct msm_request {
90 struct usb_request req;
91
92 struct usb_info *ui;
93 struct msm_request *next;
94
95 unsigned busy:1;
96 unsigned live:1;
97 unsigned alloced:1;
98 unsigned dead:1;
99
100 dma_addr_t dma;
101
102 struct ept_queue_item *item;
103 dma_addr_t item_dma;
104};
105static unsigned char str_lang_desc[] = {4,
106 USB_DT_STRING,
107 (unsigned char)LANGUAGE_ID,
108 (unsigned char)(LANGUAGE_ID >> 8)};
109
110#define to_msm_request(r) container_of(r, struct msm_request, req)
111static int usb_hw_reset(struct usb_info *ui);
112static void usb_vbus_online(struct usb_info *);
113static void usb_vbus_offline(struct usb_info *ui);
114static void usb_lpm_exit(struct usb_info *ui);
115static void usb_lpm_wakeup_phy(struct work_struct *);
116static void usb_exit(void);
117static int usb_is_online(struct usb_info *ui);
118static void usb_do_work(struct work_struct *w);
119static int usb_lpm_enter(struct usb_info *ui);
120int (*usb_lpm_config_gpio)(int);
121static void usb_enable_pullup(struct usb_info *ui);
122static void usb_disable_pullup(struct usb_info *ui);
123
124static struct workqueue_struct *usb_work;
125static void usb_chg_stop(struct work_struct *w);
126
127#define USB_STATE_IDLE 0
128#define USB_STATE_ONLINE 1
129#define USB_STATE_OFFLINE 2
130
131#define USB_FLAG_START 0x0001
132#define USB_FLAG_VBUS_ONLINE 0x0002
133#define USB_FLAG_VBUS_OFFLINE 0x0004
134#define USB_FLAG_RESET 0x0008
135#define USB_FLAG_SUSPEND 0x0010
136#define USB_FLAG_CONFIGURE 0x0020
137#define USB_FLAG_RESUME 0x0040
138#define USB_FLAG_REG_OTG 0x0080
139
140#define USB_MSC_ONLY_FUNC_MAP 0x10
141#define DRIVER_NAME "msm_hsusb_peripheral"
142
143struct lpm_info {
144 struct work_struct wakeup_phy;
145};
146
147enum charger_type {
148 USB_CHG_TYPE__SDP,
149 USB_CHG_TYPE__CARKIT,
150 USB_CHG_TYPE__WALLCHARGER,
151 USB_CHG_TYPE__INVALID
152};
153
154struct usb_info {
155 /* lock for register/queue/device state changes */
156 spinlock_t lock;
157
158 /* single request used for handling setup transactions */
159 struct usb_request *setup_req;
160 struct usb_request *ep0out_req;
161
162 struct platform_device *pdev;
163 struct msm_hsusb_platform_data *pdata;
164 int irq;
165 int gpio_irq[2];
166 void *addr;
167
168 unsigned state;
169 unsigned flags;
170
171 unsigned online;
172 unsigned running;
173 unsigned bound;
174
175 struct dma_pool *pool;
176
177 /* dma page to back the queue heads and items */
178 unsigned char *buf;
179 dma_addr_t dma;
180
181 struct ept_queue_head *head;
182
183 /* used for allocation */
184 unsigned next_item;
185 unsigned next_ifc_num;
186 unsigned stopped:1;
187 unsigned remote_wakeup:1;
188 unsigned configured:1;
189 unsigned selfpowered:1;
190 unsigned iad:1;
191 unsigned char maxpower;
192 enum usb_device_speed speed;
193 unsigned phy_info;
194
195 /* endpoints are ordered based on their status bits,
196 ** so they are OUT0, OUT1, ... OUT15, IN0, IN1, ... IN15
197 */
198 struct usb_endpoint ept[32];
199
200 struct delayed_work work;
201 struct delayed_work chg_legacy_det;
202 unsigned phy_status;
203 unsigned phy_fail_count;
204 struct usb_composition *composition;
205
206 struct usb_function_info **func;
207 unsigned num_funcs;
208 struct usb_function_map *functions_map;
209
210#define MAX_INTERFACE_NUM 15
211 struct usb_function *func2ifc_map[MAX_INTERFACE_NUM];
212
213#define ep0out ept[0]
214#define ep0in ept[16]
215
216 struct clk *clk;
217 struct clk *pclk;
218 struct clk *cclk;
219 unsigned int clk_enabled;
220
221 struct vreg *vreg;
222 unsigned int vreg_enabled;
223
224 unsigned in_lpm;
225 struct lpm_info li;
226
227 enum charger_type chg_type;
228 struct work_struct chg_stop;
229#define MAX_STRDESC_NUM 100
230 char **strdesc;
231 int strdesc_index;
232
233 u16 test_mode;
234 struct wake_lock wlock;
235 struct msm_otg_transceiver *xceiv;
236 int active;
237 enum usb_device_state usb_state;
238 int vbus_sn_notif;
239 struct switch_dev sdev;
240};
241static struct usb_info *the_usb_info;
242
243static unsigned short usb_validate_product_id(unsigned short pid);
244static unsigned short usb_get_product_id(unsigned long enabled_functions);
245static void usb_switch_composition(unsigned short pid);
246static unsigned short usb_set_composition(unsigned short pid);
247static void usb_configure_device_descriptor(struct usb_info *ui);
248static void usb_uninit(struct usb_info *ui);
249
250static unsigned ulpi_read(struct usb_info *ui, unsigned reg);
251static int ulpi_write(struct usb_info *ui, unsigned val, unsigned reg);
252
253
254
255struct usb_device_descriptor desc_device = {
256 .bLength = USB_DT_DEVICE_SIZE,
257 .bDescriptorType = USB_DT_DEVICE,
258 .bcdUSB = 0x0200,
259 .bDeviceClass = 0,
260 .bDeviceSubClass = 0,
261 .bDeviceProtocol = 0,
262 .bMaxPacketSize0 = 64,
263 /* the following fields are filled in by usb_probe */
264 .idVendor = 0,
265 .idProduct = 0,
266 .bcdDevice = 0,
267 .iManufacturer = 0,
268 .iProduct = 0,
269 .iSerialNumber = 0,
270 .bNumConfigurations = 1,
271};
272
273static void flush_endpoint(struct usb_endpoint *ept);
274static void msm_hsusb_suspend_locks_acquire(struct usb_info *, int);
275
276static ssize_t print_switch_name(struct switch_dev *sdev, char *buf)
277{
278 return sprintf(buf, "%s\n", DRIVER_NAME);
279}
280
281static ssize_t print_switch_state(struct switch_dev *sdev, char *buf)
282{
283 struct usb_info *ui = the_usb_info;
284
285 return sprintf(buf, "%s\n", (ui->online ? "online" : "offline"));
286}
287
288#define USB_WALLCHARGER_CHG_CURRENT 1800
289static int usb_get_max_power(struct usb_info *ui)
290{
291 unsigned long flags;
292 enum charger_type temp;
293 int suspended;
294 int configured;
295
296 spin_lock_irqsave(&ui->lock, flags);
297 temp = ui->chg_type;
298 suspended = ui->usb_state == USB_STATE_SUSPENDED ? 1 : 0;
299 configured = ui->configured;
300 spin_unlock_irqrestore(&ui->lock, flags);
301
302 if (temp == USB_CHG_TYPE__INVALID)
303 return -ENODEV;
304
305 if (temp == USB_CHG_TYPE__WALLCHARGER)
306 return USB_WALLCHARGER_CHG_CURRENT;
307
308 if (suspended || !configured)
309 return 0;
310
311 return ui->maxpower * 2;
312}
313
314static void usb_chg_legacy_detect(struct work_struct *w)
315{
316 struct usb_info *ui = the_usb_info;
317 unsigned long flags;
318 enum charger_type temp = USB_CHG_TYPE__INVALID;
319 int maxpower;
320 int ret = 0;
321
322 spin_lock_irqsave(&ui->lock, flags);
323
324 if (ui->usb_state == USB_STATE_NOTATTACHED) {
325 ret = -ENODEV;
326 goto chg_legacy_det_out;
327 }
328
329 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
330 ui->chg_type = temp = USB_CHG_TYPE__WALLCHARGER;
331 goto chg_legacy_det_out;
332 }
333
334 ui->chg_type = temp = USB_CHG_TYPE__SDP;
335chg_legacy_det_out:
336 spin_unlock_irqrestore(&ui->lock, flags);
337
338 if (ret)
339 return;
340
341 msm_chg_usb_charger_connected(temp);
342 maxpower = usb_get_max_power(ui);
343 if (maxpower > 0)
344 msm_chg_usb_i_is_available(maxpower);
345
346 /* USB driver prevents idle and suspend power collapse(pc)
347 * while usb cable is connected. But when dedicated charger is
348 * connected, driver can vote for idle and suspend pc. In order
349 * to allow pc, driver has to initiate low power mode which it
350 * cannot do as phy cannot be accessed when dedicated charger
351 * is connected due to phy lockup issues. Just to allow idle &
352 * suspend pc when dedicated charger is connected, release the
353 * wakelock, set driver latency to default and act as if we are
354 * in low power mode so that, driver will re-acquire wakelocks
355 * for any sub-sequent usb interrupts.
356 */
357 if (temp == USB_CHG_TYPE__WALLCHARGER) {
358 pr_info("\n%s: WALL-CHARGER\n", __func__);
359 spin_lock_irqsave(&ui->lock, flags);
360 if (ui->usb_state == USB_STATE_NOTATTACHED) {
361 spin_unlock_irqrestore(&ui->lock, flags);
362 return;
363 }
364 ui->in_lpm = 1;
365 spin_unlock_irqrestore(&ui->lock, flags);
366
367 msm_hsusb_suspend_locks_acquire(ui, 0);
368 } else
369 pr_info("\n%s: Standard Downstream Port\n", __func__);
370}
371
372int usb_msm_get_next_strdesc_id(char *str)
373{
374 struct usb_info *ui = the_usb_info;
375 unsigned id;
376 unsigned long flags;
377 int len;
378
379 len = strlen(str);
380 if (!len) {
381 printk(KERN_ERR "usb next_strdesc_id(); null string\n");
382 return -EPERM;
383 }
384 /* for null character */
385 len = len + 1;
386
387 spin_lock_irqsave(&ui->lock, flags);
388
389 id = ui->strdesc_index;
390 if (id >= MAX_STRDESC_NUM) {
391 id = -EPERM;
392 printk(KERN_ERR "reached max strdesc number\n");
393 goto get_strd_id_exit;
394 }
395
396 ui->strdesc[id] = kmalloc(len, GFP_ATOMIC);
397 if (ui->strdesc[id]) {
398 memcpy(ui->strdesc[id], str, len);
399 ui->strdesc_index++;
400 } else {
401 id = -EPERM;
402 printk(KERN_ERR "usb next_strdesc_id(); Out of memory:(%s)\n",
403 str);
404 }
405
406get_strd_id_exit:
407 spin_unlock_irqrestore(&ui->lock, flags);
408 return id;
409}
410EXPORT_SYMBOL(usb_msm_get_next_strdesc_id);
411
412
413inline int usb_msm_is_iad(void)
414{
415 return the_usb_info->iad;
416}
417EXPORT_SYMBOL(usb_msm_is_iad);
418
419inline void usb_msm_enable_iad(void)
420{
421 the_usb_info->iad = 1;
422}
423EXPORT_SYMBOL(usb_msm_enable_iad);
424
425int usb_msm_get_speed()
426{
427 return the_usb_info->speed;
428}
429EXPORT_SYMBOL(usb_msm_get_speed);
430
431int usb_msm_get_next_ifc_number(struct usb_function *driver)
432{
433 struct usb_info *ui = the_usb_info;
434 int ifc_num = -1;
435 unsigned long flags;
436 int i;
437
438 spin_lock_irqsave(&ui->lock, flags);
439 for (i = 0; i < ui->pdata->num_functions; i++) {
440 if (strcmp(ui->functions_map[i].name, driver->name))
441 continue;
442 if (!(ui->composition->functions & (1 << i)))
443 continue;
444 ifc_num = ui->next_ifc_num++;
445 ui->func2ifc_map[ifc_num] = driver;
446 break;
447 }
448 spin_unlock_irqrestore(&ui->lock, flags);
449 return ifc_num;
450}
451EXPORT_SYMBOL(usb_msm_get_next_ifc_number);
452
453static inline int usb_msm_get_selfpowered(void)
454{
455 struct usb_info *ui = the_usb_info;
456
457 return ui->selfpowered;
458}
459static inline int usb_msm_get_remotewakeup(void)
460{
461 struct usb_info *ui = the_usb_info;
462
463 return ui->remote_wakeup;
464}
465
466static void usb_clk_enable(struct usb_info *ui)
467{
468 if (!ui->clk_enabled) {
469 clk_enable(ui->pclk);
470 if (ui->cclk)
471 clk_enable(ui->cclk);
472 ui->clk_enabled = 1;
473 }
474}
475
476static void usb_clk_disable(struct usb_info *ui)
477{
478 if (ui->clk_enabled) {
479 clk_disable(ui->pclk);
480 if (ui->cclk)
481 clk_disable(ui->cclk);
482 ui->clk_enabled = 0;
483 }
484}
485
486static void usb_vreg_enable(struct usb_info *ui)
487{
488 if (ui->vreg && !IS_ERR(ui->vreg) && !ui->vreg_enabled) {
489 vreg_enable(ui->vreg);
490 ui->vreg_enabled = 1;
491 }
492}
493
494static void usb_vreg_disable(struct usb_info *ui)
495{
496 if (ui->vreg && !IS_ERR(ui->vreg) && ui->vreg_enabled) {
497 vreg_disable(ui->vreg);
498 ui->vreg_enabled = 0;
499 }
500}
501
502static unsigned ulpi_read(struct usb_info *ui, unsigned reg)
503{
504 unsigned timeout = 100000;
505
506 /* initiate read operation */
507 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
508 USB_ULPI_VIEWPORT);
509
510 /* wait for completion */
511 while ((readl(USB_ULPI_VIEWPORT) & ULPI_RUN) && (--timeout)) ;
512
513 if (timeout == 0) {
514 printk(KERN_ERR "ulpi_read: timeout %08x\n",
515 readl(USB_ULPI_VIEWPORT));
516 return 0xffffffff;
517 }
518 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
519}
520
521static int ulpi_write(struct usb_info *ui, unsigned val, unsigned reg)
522{
523 unsigned timeout = 10000;
524
525 /* initiate write operation */
526 writel(ULPI_RUN | ULPI_WRITE |
527 ULPI_ADDR(reg) | ULPI_DATA(val),
528 USB_ULPI_VIEWPORT);
529
530 /* wait for completion */
531 while((readl(USB_ULPI_VIEWPORT) & ULPI_RUN) && (--timeout)) ;
532
533 if (timeout == 0) {
534 printk(KERN_ERR "ulpi_write: timeout\n");
535 return -1;
536 }
537
538 return 0;
539}
540
541static void msm_hsusb_suspend_locks_acquire(struct usb_info *ui, int acquire)
542{
543 if (acquire) {
544 wake_lock(&ui->wlock);
545 pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY,
546 DRIVER_NAME, ui->pdata->swfi_latency);
547 /* targets like 7x30 have introduced core clock
548 * to remove the dependency on max axi frequency
549 */
550 if (!ui->cclk)
551 pm_qos_update_requirement(PM_QOS_SYSTEM_BUS_FREQ,
552 DRIVER_NAME, MSM_AXI_MAX_FREQ);
553 } else {
554 wake_lock_timeout(&ui->wlock, HZ / 2);
555 pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY,
556 DRIVER_NAME,
557 PM_QOS_DEFAULT_VALUE);
558 if (!ui->cclk)
559 pm_qos_update_requirement(PM_QOS_SYSTEM_BUS_FREQ,
560 DRIVER_NAME, PM_QOS_DEFAULT_VALUE);
561 }
562}
563
564static void msm_hsusb_suspend_locks_init(struct usb_info *ui, int init)
565{
566 if (init) {
567 wake_lock_init(&ui->wlock, WAKE_LOCK_SUSPEND,
568 "usb_bus_active");
569 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
570 DRIVER_NAME,
571 PM_QOS_DEFAULT_VALUE);
572 pm_qos_add_requirement(PM_QOS_SYSTEM_BUS_FREQ,
573 DRIVER_NAME, PM_QOS_DEFAULT_VALUE);
574 } else {
575 wake_lock_destroy(&ui->wlock);
576 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY, DRIVER_NAME);
577 pm_qos_remove_requirement(PM_QOS_SYSTEM_BUS_FREQ, DRIVER_NAME);
578 }
579}
580
581static void init_endpoints(struct usb_info *ui)
582{
583 unsigned n;
584
585 for (n = 0; n < 32; n++) {
586 struct usb_endpoint *ept = ui->ept + n;
587
588 ept->ui = ui;
589 ept->bit = n;
590 ept->num = n & 15;
591 ept->alloced = 0;
592
593 if (ept->bit > 15) {
594 /* IN endpoint */
595 ept->head = ui->head + (ept->num << 1) + 1;
596 ept->flags = EPT_FLAG_IN;
597 } else {
598 /* OUT endpoint */
599 ept->head = ui->head + (ept->num << 1);
600 ept->flags = 0;
601 }
602 }
603}
604
605void usb_configure_endpoint(struct usb_endpoint *ep,
606 struct usb_endpoint_descriptor *ep_desc)
607{
608 unsigned cfg = 0;
609 unsigned long flags;
610 struct usb_info *ui = ep->ui;
611
612 if (!ui)
613 return;
614 spin_lock_irqsave(&ui->lock, flags);
615
616 if (ep_desc) {
617 ep->max_pkt = ep_desc->wMaxPacketSize;
618 ep->ep_descriptor = ep_desc;
619 }
620
621 if (!ep->max_pkt) {
622 printk(KERN_ERR "cannot configure zero length max pkt\n");
623 goto cfg_ept_end;
624 }
625
626 cfg = CONFIG_MAX_PKT(ep->max_pkt) | CONFIG_ZLT;
627 /* ep0 out needs interrupt-on-setup */
628 if (ep->bit == 0)
629 cfg |= CONFIG_IOS;
630 ep->head->config = cfg;
631 ep->head->next = TERMINATE;
632
633 pr_debug("ept #%d %s max:%d head:%p bit:%d\n",
634 ep->num,
635 (ep->flags & EPT_FLAG_IN) ? "in" : "out",
636 ep->max_pkt, ep->head, ep->bit);
637
638cfg_ept_end:
639 spin_unlock_irqrestore(&ui->lock, flags);
640}
641EXPORT_SYMBOL(usb_configure_endpoint);
642
643#define NUM_EPTS 15 /* number of in or out non-ctrl endpoints */
644struct usb_endpoint *usb_alloc_endpoint(unsigned direction)
645{
646 struct usb_info *ui = the_usb_info;
647 struct usb_endpoint *ept = NULL;
648 int i;
649 unsigned long flags;
650
651 spin_lock_irqsave(&ui->lock, flags);
652 if (direction & USB_DIR_IN)
653 ept = (&ui->ep0in);
654 else
655 ept = (&ui->ep0out);
656
657 for (i = 0; i < NUM_EPTS; i++) {
658 ept++;
659 if (!ept->alloced) {
660 ept->alloced = 1;
661 ept->ui = ui;
662 spin_unlock_irqrestore(&ui->lock, flags);
663 return ept;
664 }
665 }
666 spin_unlock_irqrestore(&ui->lock, flags);
667
668 return NULL;
669}
670EXPORT_SYMBOL(usb_alloc_endpoint);
671
672int usb_free_endpoint(struct usb_endpoint *ept)
673{
674 struct usb_info *ui = the_usb_info;
675 unsigned long flags;
676
677 if (!ept)
678 return -EINVAL;
679 spin_lock_irqsave(&ui->lock, flags);
680 ept->alloced = 0;
681 ept->ui = 0;
682 spin_unlock_irqrestore(&ui->lock, flags);
683
684 return 0;
685}
686EXPORT_SYMBOL(usb_free_endpoint);
687
688struct usb_request *usb_ept_alloc_req(struct usb_endpoint *ept,
689 unsigned bufsize)
690{
691 struct usb_info *ui = ept->ui;
692 struct msm_request *req;
693
694 if (!ui)
695 return NULL;
696
697 req = kzalloc(sizeof(*req), GFP_ATOMIC);
698 if (!req)
699 goto fail1;
700
701 req->item = dma_pool_alloc(ui->pool, GFP_ATOMIC, &req->item_dma);
702 if (!req->item)
703 goto fail2;
704
705 if (bufsize) {
706 req->req.buf = kmalloc(bufsize, GFP_ATOMIC);
707 if (!req->req.buf)
708 goto fail3;
709 req->alloced = 1;
710 }
711
712 return &req->req;
713
714fail3:
715 dma_pool_free(ui->pool, req->item, req->item_dma);
716fail2:
717 kfree(req);
718fail1:
719 return NULL;
720}
721EXPORT_SYMBOL(usb_ept_alloc_req);
722
723static void do_free_req(struct usb_info *ui, struct msm_request *req)
724{
725 if (req->alloced)
726 kfree(req->req.buf);
727
728 dma_pool_free(ui->pool, req->item, req->item_dma);
729 kfree(req);
730}
731
732void usb_ept_free_req(struct usb_endpoint *ept, struct usb_request *_req)
733{
734 struct msm_request *req, *temp_req, *prev_req;
735 struct usb_info *ui;
736 unsigned long flags;
737 int dead = 0;
738 if (!ept || !_req)
739 return;
740
741 ui = ept->ui;
742 if (!ui)
743 return;
744
745 req = to_msm_request(_req);
746 spin_lock_irqsave(&ui->lock, flags);
747 /* defer freeing resources if request is still busy */
748 if (req->busy)
749 dead = req->dead = 1;
750 spin_unlock_irqrestore(&ui->lock, flags);
751
752 /* if req->dead, then we will clean up when the request finishes */
753 if (!dead) {
754 temp_req = ept->req;
755 prev_req = temp_req;
756 while (temp_req != NULL) {
757 if (req == temp_req && ept->req != temp_req)
758 prev_req->next = temp_req->next;
759
760 prev_req = temp_req;
761 temp_req = temp_req->next;
762 }
763 if (ept->req == req)
764 ept->req = req->next;
765 req->req.complete = NULL;
766 do_free_req(ui, req);
767 } else
768 pr_err("%s: req is busy, can't free req\n", __func__);
769}
770EXPORT_SYMBOL(usb_ept_free_req);
771
772void usb_ept_enable(struct usb_endpoint *ept, int yes)
773{
774 struct usb_info *ui;
775 int in;
776 unsigned n;
777 unsigned char xfer;
778
779 if (!ept || !ept->ui)
780 return;
781 ui = ept->ui;
782 in = ept->flags & EPT_FLAG_IN;
783 if (!ept->ep_descriptor)
784 return;
785
786 if (ui->in_lpm) {
787 pr_err("%s: controller is in lpm, cannot proceed\n", __func__);
788 return;
789 }
790
791 xfer = ept->ep_descriptor->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
792
793 n = readl(USB_ENDPTCTRL(ept->num));
794
795 if (in) {
796 if (xfer == USB_ENDPOINT_XFER_BULK)
797 n = (n & (~CTRL_TXT_MASK)) | CTRL_TXT_BULK;
798 else if (xfer == USB_ENDPOINT_XFER_INT)
799 n = (n & (~CTRL_TXT_MASK)) | CTRL_TXT_INT;
800 if (yes)
801 n |= CTRL_TXE | CTRL_TXR;
802 else
803 n &= (~CTRL_TXE);
804 } else {
805 if (xfer == USB_ENDPOINT_XFER_BULK)
806 n = (n & (~CTRL_RXT_MASK)) | CTRL_RXT_BULK;
807 else if (xfer == USB_ENDPOINT_XFER_INT)
808 n = (n & (~CTRL_RXT_MASK)) | CTRL_RXT_INT;
809 if (yes)
810 n |= CTRL_RXE | CTRL_RXR;
811 else
812 n &= ~(CTRL_RXE);
813 }
814 /* complete all the updates to ept->head before enabling endpoint*/
815 dma_coherent_pre_ops();
816 writel(n, USB_ENDPTCTRL(ept->num));
817}
818EXPORT_SYMBOL(usb_ept_enable);
819
820static void usb_ept_start(struct usb_endpoint *ept)
821{
822 struct usb_info *ui = ept->ui;
823 struct msm_request *req = ept->req;
824
825 BUG_ON(req->live);
826
827 /* link the hw queue head to the request's transaction item */
828 ept->head->next = req->item_dma;
829 ept->head->info = 0;
830
831 /* memory barrier to flush the data before priming endpoint*/
832 dma_coherent_pre_ops();
833 /* start the endpoint */
834 writel(1 << ept->bit, USB_ENDPTPRIME);
835
836 /* mark this chain of requests as live */
837 while (req) {
838 req->live = 1;
839 if (req->item->next == TERMINATE)
840 break;
841 req = req->next;
842 }
843}
844
845int usb_ept_queue_xfer(struct usb_endpoint *ept, struct usb_request *_req)
846{
847 unsigned long flags;
848 struct msm_request *req = to_msm_request(_req);
849 struct msm_request *last;
850 struct usb_info *ui = ept->ui;
851 struct ept_queue_item *item = req->item;
852 unsigned length = req->req.length;
853
854 if (length > 0x4000)
855 return -EMSGSIZE;
856
857 if (ui->in_lpm) {
858 req->req.status = usb_remote_wakeup();
859 if (req->req.status) {
860 pr_debug("%s:RWakeup generation failed, EP = %x\n",
861 __func__, ept->bit);
862 return req->req.status;
863 }
864 }
865
866 spin_lock_irqsave(&ui->lock, flags);
867
868 if (req->busy) {
869 req->req.status = -EBUSY;
870 spin_unlock_irqrestore(&ui->lock, flags);
871 printk(KERN_INFO
872 "usb_ept_queue_xfer() tried to queue busy request\n");
873 return -EBUSY;
874 }
875
876 if (!ui->online && (ept->num != 0)) {
877 req->req.status = -ENODEV;
878 spin_unlock_irqrestore(&ui->lock, flags);
879 printk(KERN_INFO "usb_ept_queue_xfer() tried to queue request"
880 "while offline; ept->bit: %x\n", ept->bit);
881 return -ENODEV;
882 }
883
884 req->busy = 1;
885 req->live = 0;
886 req->next = 0;
887 req->req.status = -EBUSY;
888
889 req->dma = dma_map_single(NULL, req->req.buf, length,
890 (ept->flags & EPT_FLAG_IN) ?
891 DMA_TO_DEVICE : DMA_FROM_DEVICE);
892
893 /* prepare the transaction descriptor item for the hardware */
894 item->next = TERMINATE;
895 item->info = INFO_BYTES(length) | INFO_IOC | INFO_ACTIVE;
896 item->page0 = req->dma;
897 item->page1 = (req->dma + 0x1000) & 0xfffff000;
898 item->page2 = (req->dma + 0x2000) & 0xfffff000;
899 item->page3 = (req->dma + 0x3000) & 0xfffff000;
900
901 /* Add the new request to the end of the queue */
902 last = ept->last;
903 if (last) {
904 /* Already requests in the queue. add us to the
905 * end, but let the completion interrupt actually
906 * start things going, to avoid hw issues
907 */
908 last->next = req;
909
910 /* only modify the hw transaction next pointer if
911 * that request is not live
912 */
913 if (!last->live)
914 last->item->next = req->item_dma;
915 } else {
916 /* queue was empty -- kick the hardware */
917 ept->req = req;
918 usb_ept_start(ept);
919 }
920 ept->last = req;
921
922 spin_unlock_irqrestore(&ui->lock, flags);
923 return 0;
924}
925EXPORT_SYMBOL(usb_ept_queue_xfer);
926
927int usb_ept_flush(struct usb_endpoint *ept)
928{
929 printk("usb_ept_flush \n");
930 flush_endpoint(ept);
931 return 0;
932}
933
934int usb_ept_get_max_packet(struct usb_endpoint *ept)
935{
936 return ept->max_pkt;
937}
938EXPORT_SYMBOL(usb_ept_get_max_packet);
939
940int usb_remote_wakeup(void)
941{
942 struct usb_info *ui = the_usb_info;
943 unsigned long flags;
944
945 spin_lock_irqsave(&ui->lock, flags);
946 if (!ui->remote_wakeup) {
947 spin_unlock_irqrestore(&ui->lock, flags);
948 pr_err("%s: remote wakeup not supported\n", __func__);
949 return -ENOTSUPP;
950 }
951
952 if (!ui->online) {
953 spin_unlock_irqrestore(&ui->lock, flags);
954 pr_err("%s: device is not configured\n", __func__);
955 return -ENODEV;
956 }
957
958 if (ui->in_lpm)
959 usb_lpm_exit(ui);
960 spin_unlock_irqrestore(&ui->lock, flags);
961
962 /* if usb_lpm_exit is unable to set PHCD,
963 * it would initiate workthread to set the PHCD
964 */
965 if (cancel_work_sync(&ui->li.wakeup_phy))
966 usb_lpm_wakeup_phy(NULL);
967
968 spin_lock_irqsave(&ui->lock, flags);
969 if (ui->in_lpm) {
970 spin_unlock_irqrestore(&ui->lock, flags);
971 pr_err("%s: cannot bring controller out of lpm\n", __func__);
972 return -ENODEV;
973 }
974
975 if (!usb_is_online(ui)) {
976 pr_debug("%s: enabling force resume\n", __func__);
977 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
978 } else
979 pr_debug("%s: controller seems to be out of suspend already\n",
980 __func__);
981 spin_unlock_irqrestore(&ui->lock, flags);
982
983 return 0;
984}
985EXPORT_SYMBOL(usb_remote_wakeup);
986
987/* --- endpoint 0 handling --- */
988
989static void set_configuration(struct usb_info *ui, int yes)
990{
991 unsigned i;
992
993 ui->online = !!yes;
994
995 for (i = 0; i < ui->num_funcs; i++) {
996 struct usb_function_info *fi = ui->func[i];
997 if (!fi || !(ui->composition->functions & (1 << i)))
998 continue;
999 if (fi->func->configure)
1000 fi->func->configure(yes, fi->func->context);
1001 }
1002}
1003
1004static void ep0out_complete(struct usb_endpoint *ept, struct usb_request *req)
1005{
1006 req->complete = 0;
1007}
1008
1009static void ep0in_complete(struct usb_endpoint *ept, struct usb_request *req)
1010{
1011 /* queue up the receive of the ACK response from the host */
1012 if (req->status == 0) {
1013 struct usb_info *ui = ept->ui;
1014 req->length = 0;
1015 req->complete = ep0out_complete;
1016 usb_ept_queue_xfer(&ui->ep0out, req);
1017 }
1018}
1019
1020static void ep0in_complete_sendzero(
1021 struct usb_endpoint *ept, struct usb_request *req)
1022{
1023 if (req->status == 0) {
1024 struct usb_info *ui = ept->ui;
1025 req->length = 0;
1026 req->complete = ep0in_complete;
1027 usb_ept_queue_xfer(&ui->ep0in, req);
1028 }
1029}
1030
1031static void ep0_status_complete(
1032 struct usb_endpoint *ept, struct usb_request *req)
1033{
1034 struct usb_info *ui = ept->ui;
1035 unsigned int i;
1036
1037 if (!ui->test_mode)
1038 return;
1039
1040 switch (ui->test_mode) {
1041 case J_TEST:
1042 pr_info("usb electrical test mode: (J)\n");
1043 i = readl(USB_PORTSC) & (~PORTSC_PTC);
1044 writel(i | PORTSC_PTC_J_STATE, USB_PORTSC);
1045 break;
1046
1047 case K_TEST:
1048 pr_info("usb electrical test mode: (K)\n");
1049 i = readl(USB_PORTSC) & (~PORTSC_PTC);
1050 writel(i | PORTSC_PTC_K_STATE, USB_PORTSC);
1051 break;
1052
1053 case SE0_NAK_TEST:
1054 pr_info("usb electrical test mode: (SE0-NAK)\n");
1055 i = readl(USB_PORTSC) & (~PORTSC_PTC);
1056 writel(i | PORTSC_PTC_SE0_NAK, USB_PORTSC);
1057 break;
1058
1059 case TST_PKT_TEST:
1060 pr_info("usb electrical test mode: (TEST_PKT)\n");
1061 i = readl(USB_PORTSC) & (~PORTSC_PTC);
1062 writel(i | PORTSC_PTC_TST_PKT, USB_PORTSC);
1063 break;
1064 default:
1065 pr_err("usb:%s: undefined test mode: (%x)\n",
1066 __func__, ui->test_mode);
1067 }
1068
1069}
1070
1071static void ep0_setup_ack(struct usb_info *ui)
1072{
1073 struct usb_request *req = ui->setup_req;
1074 req->length = 0;
1075 req->complete = ep0_status_complete;
1076 usb_ept_queue_xfer(&ui->ep0in, req);
1077}
1078
1079static void ep0_setup_stall(struct usb_info *ui)
1080{
1081 writel((1<<16) | (1<<0), USB_ENDPTCTRL(0));
1082}
1083
1084static void ep0_setup_receive(struct usb_info *ui, unsigned len)
1085{
1086 ui->ep0out_req->length = len;
1087 usb_ept_queue_xfer(&ui->ep0out, ui->ep0out_req);
1088}
1089
1090static void ep0_setup_send(struct usb_info *ui, unsigned wlen)
1091{
1092 struct usb_request *req = ui->setup_req;
1093 struct usb_endpoint *ept = &ui->ep0in;
1094
1095 /* never send more data than the host requested */
1096 if (req->length > wlen)
1097 req->length = wlen;
1098
1099 /* if we are sending a short response that ends on
1100 * a packet boundary, we'll need to send a zero length
1101 * packet as well.
1102 */
1103 if ((req->length != wlen) && ((req->length & 63) == 0)) {
1104 req->complete = ep0in_complete_sendzero;
1105 } else {
1106 req->complete = ep0in_complete;
1107 }
1108
1109 usb_ept_queue_xfer(ept, req);
1110}
1111
1112
1113static int usb_find_descriptor(struct usb_info *ui, struct usb_ctrlrequest *ctl,
1114 struct usb_request *req);
1115
1116static void handle_setup(struct usb_info *ui)
1117{
1118 struct usb_ctrlrequest ctl;
1119
1120 memcpy(&ctl, ui->ep0out.head->setup_data, sizeof(ctl));
1121 writel(EPT_RX(0), USB_ENDPTSETUPSTAT);
1122
1123 /* any pending ep0 transactions must be canceled */
1124 flush_endpoint(&ui->ep0out);
1125 flush_endpoint(&ui->ep0in);
1126
1127 /* let functions handle vendor and class requests */
1128 if ((ctl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) {
1129 struct usb_function *func;
1130
1131 /* Send stall if received interface number is invalid */
1132 if (ctl.wIndex >= ui->next_ifc_num)
1133 goto stall;
1134
1135 func = ui->func2ifc_map[ctl.wIndex];
1136 if (func && func->setup) {
1137 if (ctl.bRequestType & USB_DIR_IN) {
1138 struct usb_request *req = ui->setup_req;
1139 int ret = func->setup(&ctl,
1140 req->buf, SETUP_BUF_SIZE,
1141 func->context);
1142 if (ret >= 0) {
1143 req->length = ret;
1144 ep0_setup_send(ui, ctl.wLength);
1145 return;
1146 }
1147 } else {
1148 int ret = func->setup(&ctl, NULL, 0,
1149 func->context);
1150 if (ret == 0) {
1151 ep0_setup_ack(ui);
1152 return;
1153 } else if (ret > 0) {
1154 ep0_setup_receive(ui, ret);
1155 return;
1156 }
1157 }
1158 }
1159 goto stall;
1160 return;
1161 }
1162
1163 switch (ctl.bRequest) {
1164 case USB_REQ_GET_STATUS:
1165 {
1166 struct usb_request *req = ui->setup_req;
1167 if ((ctl.bRequestType & (USB_DIR_MASK)) != (USB_DIR_IN))
1168 break;
1169 if (ctl.wLength != 2)
1170 break;
1171 req->length = 2;
1172 switch (ctl.bRequestType & USB_RECIP_MASK) {
1173 case USB_RECIP_ENDPOINT:
1174 {
1175 unsigned num = ctl.wIndex & USB_ENDPOINT_NUMBER_MASK;
1176 struct usb_endpoint *ept;
1177
1178 if (num == 0)
1179 break;
1180 if (ctl.wIndex & USB_ENDPOINT_DIR_MASK)
1181 num += 16;
1182 ept = ui->ept + num;
1183 memcpy(req->buf, &ept->ept_halted, 2);
1184 break;
1185 }
1186
1187 case USB_RECIP_DEVICE:
1188 {
1189 unsigned short temp = 0;
1190 if (usb_msm_get_selfpowered())
1191 temp = 1 << USB_DEVICE_SELF_POWERED;
1192 if (usb_msm_get_remotewakeup())
1193 temp |= 1 << USB_DEVICE_REMOTE_WAKEUP;
1194 memcpy(req->buf, &temp, 2);
1195 break;
1196 }
1197
1198 case USB_RECIP_INTERFACE:
1199 memset(req->buf, 0, 2);
1200 break;
1201 default:
1202 printk(KERN_ERR "Unreconginized recipient\n");
1203 break;
1204 }
1205
1206 ep0_setup_send(ui, 2);
1207 return;
1208 }
1209
1210 case USB_REQ_GET_DESCRIPTOR:
1211 {
1212 struct usb_request *req;
1213
1214 if ((ctl.bRequestType & (USB_DIR_MASK)) != (USB_DIR_IN))
1215 break;
1216
1217 req = ui->setup_req;
1218 if (!usb_find_descriptor(ui, &ctl, req)) {
1219 if (req->length > ctl.wLength)
1220 req->length = ctl.wLength;
1221 ep0_setup_send(ui, ctl.wLength);
1222 return;
1223 }
1224 break;
1225 }
1226
1227 case USB_REQ_SET_FEATURE:
1228 if ((ctl.bRequestType & (USB_DIR_MASK)) != (USB_DIR_OUT))
1229 break;
1230 if (ctl.wLength != 0)
1231 break;
1232 switch (ctl.bRequestType & USB_RECIP_MASK) {
1233 case USB_RECIP_DEVICE:
1234 if (ctl.wValue == USB_DEVICE_REMOTE_WAKEUP) {
1235 ui->remote_wakeup = 1;
1236 ep0_setup_ack(ui);
1237 return;
1238 } else if (ctl.wValue == USB_DEVICE_TEST_MODE) {
1239 if (ctl.wIndex & 0x0f)
1240 break;
1241 ui->test_mode = ctl.wIndex;
1242 ep0_setup_ack(ui);
1243 return;
1244 }
1245 break;
1246
1247 case USB_RECIP_ENDPOINT:
1248 {
1249 unsigned num = ctl.wIndex & USB_ENDPOINT_NUMBER_MASK;
1250 if ((num == 0) || (ctl.wValue != 0))
1251 break;
1252 if (ctl.wIndex & USB_ENDPOINT_DIR_MASK)
1253 num += 16;
1254 usb_ept_set_halt(ui->ept + num);
1255 ep0_setup_ack(ui);
1256 return;
1257 }
1258
1259 default:
1260 pr_err("usb: %s: set_feature: unrecognized recipient\n",
1261 __func__);
1262 break;
1263 }
1264 break;
1265
1266 case USB_REQ_CLEAR_FEATURE:
1267 {
1268 if ((ctl.bRequestType & (USB_DIR_MASK)) != (USB_DIR_OUT))
1269 break;
1270 if (ctl.wLength != 0)
1271 break;
1272
1273 switch (ctl.bRequestType & USB_RECIP_MASK) {
1274 case USB_RECIP_DEVICE:
1275 if (ctl.wValue != USB_DEVICE_REMOTE_WAKEUP)
1276 break;
1277 ui->remote_wakeup = 0;
1278 ep0_setup_ack(ui);
1279 return;
1280 case USB_RECIP_ENDPOINT:
1281 {
1282 unsigned num;
1283 if (ctl.wValue != USB_ENDPOINT_HALT)
1284 break;
1285 num = ctl.wIndex & USB_ENDPOINT_NUMBER_MASK;
1286 if (num != 0) {
1287 if (ctl.wIndex & USB_ENDPOINT_DIR_MASK)
1288 num += 16;
1289 usb_ept_clear_halt(ui->ept + num);
1290 }
1291 ep0_setup_ack(ui);
1292 return;
1293 }
1294 default:
1295 pr_info("unsupported clear feature command\n");
1296 pr_info("Request-type:(%08x) wValue:(%08x) "
1297 "wIndex:(%08x) wLength:(%08x)\n",
1298 ctl.bRequestType, ctl.wValue,
1299 ctl.wIndex, ctl.wLength);
1300 break;
1301 }
1302 break;
1303 }
1304
1305 case USB_REQ_SET_INTERFACE:
1306 if ((ctl.bRequestType & (USB_DIR_MASK | USB_RECIP_MASK))
1307 != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1308 break;
1309 if (ui->func2ifc_map[ctl.wIndex]->set_interface) {
1310 ui->func2ifc_map[ctl.wIndex]->set_interface(ctl.wIndex,
1311 ctl.wValue,
1312 ui->func2ifc_map[ctl.wIndex]->context);
1313 ep0_setup_ack(ui);
1314 return;
1315 }
1316 break;
1317 case USB_REQ_GET_INTERFACE:
1318 {
1319 struct usb_function *f;
1320 struct usb_request *req = ui->setup_req;
1321 int ifc_num = ctl.wIndex;
1322 int ret = 0;
1323
1324 if ((ctl.bRequestType & (USB_DIR_MASK | USB_RECIP_MASK))
1325 != (USB_DIR_IN | USB_RECIP_INTERFACE))
1326 break;
1327
1328 f = ui->func2ifc_map[ifc_num];
1329 if (!f->get_interface)
1330 break;
1331 ret = f->get_interface(ifc_num,
1332 ui->func2ifc_map[ifc_num]->context);
1333 if (ret < 0)
1334 break;
1335 req->length = ctl.wLength;
1336 memcpy(req->buf, &ret, req->length);
1337 ep0_setup_send(ui, ctl.wLength);
1338 return;
1339 }
1340 case USB_REQ_SET_CONFIGURATION:
1341 if ((ctl.bRequestType & USB_DIR_MASK) != USB_DIR_OUT)
1342 break;
1343 ui->configured = ctl.wValue;
1344 pr_info("hsusb set_configuration wValue = %d usbcmd = %x\n",
1345 ctl.wValue, readl(USB_USBCMD));
1346 set_configuration(ui, ctl.wValue);
1347 ep0_setup_ack(ui);
1348 ui->flags = USB_FLAG_CONFIGURE;
1349 if (ui->configured)
1350 ui->usb_state = USB_STATE_CONFIGURED;
1351 queue_delayed_work(usb_work, &ui->work, 0);
1352 return;
1353
1354 case USB_REQ_GET_CONFIGURATION:
1355 {
1356 unsigned conf;
1357 struct usb_request *req = ui->setup_req;
1358 req->length = 1;
1359 conf = ui->configured;
1360 memcpy(req->buf, &conf, req->length);
1361 ep0_setup_send(ui, ctl.wLength);
1362 return;
1363 }
1364
1365 case USB_REQ_SET_ADDRESS:
1366 if ((ctl.bRequestType & (USB_DIR_MASK | USB_RECIP_MASK))
1367 != (USB_DIR_OUT | USB_RECIP_DEVICE))
1368 break;
1369 ui->usb_state = USB_STATE_ADDRESS;
1370 writel((ctl.wValue << 25) | (1 << 24), USB_DEVICEADDR);
1371 ep0_setup_ack(ui);
1372 return;
1373 }
1374
1375stall:
1376 ep0_setup_stall(ui);
1377 return;
1378
1379}
1380
1381static void handle_endpoint(struct usb_info *ui, unsigned bit)
1382{
1383 struct usb_endpoint *ept = ui->ept + bit;
1384 struct msm_request *req;
1385 unsigned long flags;
1386 unsigned info;
1387
1388#if 0
1389 printk(KERN_INFO "handle_endpoint() %d %s req=%p(%08x)\n",
1390 ept->num, (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1391 ept->req, ept->req ? ept->req->item_dma : 0);
1392#endif
1393 if (!ept) {
1394 pr_err("%s: ept is null: ep bit = %d\n", __func__, bit);
1395 return;
1396 }
1397
1398 /* expire all requests that are no longer active */
1399 spin_lock_irqsave(&ui->lock, flags);
1400 while ((req = ept->req)) {
1401 /* clean speculative fetches on req->item->info */
1402 dma_coherent_post_ops();
1403 info = req->item->info;
1404
1405 /* if we've processed all live requests, time to
1406 * restart the hardware on the next non-live request
1407 */
1408 if (!req->live) {
1409 usb_ept_start(ept);
1410 break;
1411 }
1412
1413 /* if the transaction is still in-flight, stop here */
1414 if (info & INFO_ACTIVE)
1415 break;
1416
1417 /* advance ept queue to the next request */
1418 ept->req = req->next;
1419 if (ept->req == 0)
1420 ept->last = 0;
1421
1422 dma_unmap_single(NULL, req->dma, req->req.length,
1423 (ept->flags & EPT_FLAG_IN) ?
1424 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1425
1426 if (info & (INFO_HALTED | INFO_BUFFER_ERROR | INFO_TXN_ERROR)) {
1427 /* XXX pass on more specific error code */
1428 req->req.status = -EIO;
1429 req->req.actual = 0;
1430 printk(KERN_INFO "hsusb: ept %d %s error. info=%08x\n",
1431 ept->num,
1432 (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1433 info);
1434 } else {
1435 req->req.status = 0;
1436 req->req.actual = req->req.length - ((info >> 16) & 0x7FFF);
1437 }
1438 req->busy = 0;
1439 req->live = 0;
1440 if (req->dead)
1441 do_free_req(ui, req);
1442
1443 if (req->req.complete) {
1444 spin_unlock_irqrestore(&ui->lock, flags);
1445 req->req.complete(ept, &req->req);
1446 spin_lock_irqsave(&ui->lock, flags);
1447 }
1448 }
1449 spin_unlock_irqrestore(&ui->lock, flags);
1450}
1451
1452static void flush_endpoint_hw(struct usb_info *ui, unsigned bits)
1453{
1454 /* flush endpoint, canceling transactions
1455 ** - this can take a "large amount of time" (per databook)
1456 ** - the flush can fail in some cases, thus we check STAT
1457 ** and repeat if we're still operating
1458 ** (does the fact that this doesn't use the tripwire matter?!)
1459 */
1460
1461 if (ui->in_lpm) {
1462 pr_err("%s: controller is in lpm, cannot proceed\n", __func__);
1463 return;
1464 }
1465
1466 do {
1467 writel(bits, USB_ENDPTFLUSH);
1468 while (readl(USB_ENDPTFLUSH) & bits)
1469 udelay(100);
1470 } while (readl(USB_ENDPTSTAT) & bits);
1471}
1472
1473static void flush_endpoint_sw(struct usb_endpoint *ept)
1474{
1475 struct usb_info *ui = ept->ui;
1476 struct msm_request *req, *next;
1477 unsigned long flags;
1478
1479 /* inactive endpoints have nothing to do here */
1480 if (!ui || !ept->alloced || !ept->max_pkt)
1481 return;
1482
1483 /* put the queue head in a sane state */
1484 ept->head->info = 0;
1485 ept->head->next = TERMINATE;
1486
1487 /* cancel any pending requests */
1488 spin_lock_irqsave(&ui->lock, flags);
1489 req = ept->req;
1490 ept->req = 0;
1491 ept->last = 0;
1492 while (req != 0) {
1493 next = req->next;
1494
1495 req->busy = 0;
1496 req->live = 0;
1497 req->req.status = -ENODEV;
1498 req->req.actual = 0;
1499 if (req->req.complete) {
1500 spin_unlock_irqrestore(&ui->lock, flags);
1501 req->req.complete(ept, &req->req);
1502 spin_lock_irqsave(&ui->lock, flags);
1503 }
1504 if (req->dead)
1505 do_free_req(ui, req);
1506 req = req->next;
1507 }
1508 spin_unlock_irqrestore(&ui->lock, flags);
1509}
1510
1511static void flush_endpoint(struct usb_endpoint *ept)
1512{
1513 if (!ept->ui)
1514 return;
1515
1516 flush_endpoint_hw(ept->ui, (1 << ept->bit));
1517 flush_endpoint_sw(ept);
1518}
1519
1520static void flush_all_endpoints(struct usb_info *ui)
1521{
1522 unsigned n;
1523
1524 flush_endpoint_hw(ui, 0xffffffff);
1525
1526 for (n = 0; n < 32; n++)
1527 flush_endpoint_sw(ui->ept + n);
1528}
1529
1530#define HW_DELAY_FOR_LPM msecs_to_jiffies(1000)
1531#define DELAY_FOR_USB_VBUS_STABILIZE msecs_to_jiffies(500)
1532static irqreturn_t usb_interrupt(int irq, void *data)
1533{
1534 struct usb_info *ui = data;
1535 unsigned n;
1536 unsigned speed;
1537
1538 if (!ui->active)
1539 return IRQ_HANDLED;
1540
1541 if (ui->in_lpm) {
1542 usb_lpm_exit(ui);
1543 return IRQ_HANDLED;
1544 }
1545
1546 n = readl(USB_USBSTS);
1547 writel(n, USB_USBSTS);
1548
1549 /* somehow we got an IRQ while in the reset sequence: ignore it */
1550 if (ui->running == 0) {
1551 pr_err("%s: ui->running is zero\n", __func__);
1552 return IRQ_HANDLED;
1553 }
1554
1555 if (n & STS_PCI) {
1556 if (!(readl(USB_PORTSC) & PORTSC_PORT_RESET)) {
1557 speed = (readl(USB_PORTSC) & PORTSC_PORT_SPEED_MASK);
1558 switch (speed) {
1559 case PORTSC_PORT_SPEED_HIGH:
1560 pr_info("hsusb resume: speed = HIGH\n");
1561 ui->speed = USB_SPEED_HIGH;
1562 break;
1563
1564 case PORTSC_PORT_SPEED_FULL:
1565 pr_info("hsusb resume: speed = FULL\n");
1566 ui->speed = USB_SPEED_FULL;
1567 break;
1568
1569 default:
1570 pr_err("hsusb resume: Unknown Speed\n");
1571 ui->speed = USB_SPEED_UNKNOWN;
1572 break;
1573 }
1574 }
1575
1576 /* pci interrutpt would also be generated when resuming
1577 * from bus suspend, following check would avoid kick
1578 * starting usb main thread in case of pci interrupts
1579 * during enumeration
1580 */
1581 if (ui->configured && ui->chg_type == USB_CHG_TYPE__SDP) {
1582 ui->usb_state = USB_STATE_CONFIGURED;
1583 ui->flags = USB_FLAG_RESUME;
1584 queue_delayed_work(usb_work, &ui->work, 0);
1585 }
1586 }
1587
1588 if (n & STS_URI) {
1589 pr_info("hsusb reset interrupt\n");
1590 ui->usb_state = USB_STATE_DEFAULT;
1591 ui->configured = 0;
1592 schedule_work(&ui->chg_stop);
1593
1594 writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
1595 writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
1596 writel(0xffffffff, USB_ENDPTFLUSH);
1597 writel(0, USB_ENDPTCTRL(1));
1598
1599 if (ui->online != 0) {
1600 /* marking us offline will cause ept queue attempts to fail */
1601 ui->online = 0;
1602
1603 flush_all_endpoints(ui);
1604
1605 /* XXX: we can't seem to detect going offline, so deconfigure
1606 * XXX: on reset for the time being
1607 */
1608 set_configuration(ui, 0);
1609 }
1610 }
1611
1612 if (n & STS_SLI) {
1613 pr_info("hsusb suspend interrupt\n");
1614 ui->usb_state = USB_STATE_SUSPENDED;
1615
1616 /* stop usb charging */
1617 schedule_work(&ui->chg_stop);
1618 }
1619
1620 if (n & STS_UI) {
1621 n = readl(USB_ENDPTSETUPSTAT);
1622 if (n & EPT_RX(0))
1623 handle_setup(ui);
1624
1625 n = readl(USB_ENDPTCOMPLETE);
1626 writel(n, USB_ENDPTCOMPLETE);
1627 while (n) {
1628 unsigned bit = __ffs(n);
1629 handle_endpoint(ui, bit);
1630 n = n & (~(1 << bit));
1631 }
1632 }
1633
1634 n = readl(USB_OTGSC);
1635 writel(n, USB_OTGSC);
1636
1637 if (n & OTGSC_BSVIS) {
1638 /*Verify B Session Valid Bit to verify vbus status*/
1639 if (B_SESSION_VALID & n) {
1640 pr_info("usb cable connected\n");
1641 ui->usb_state = USB_STATE_POWERED;
1642 ui->flags = USB_FLAG_VBUS_ONLINE;
1643 /* Wait for 100ms to stabilize VBUS before initializing
1644 * USB and detecting charger type
1645 */
1646 queue_delayed_work(usb_work, &ui->work, 0);
1647 } else {
1648 int i;
1649
1650 usb_disable_pullup(ui);
1651
1652 printk(KERN_INFO "usb cable disconnected\n");
1653 ui->usb_state = USB_STATE_NOTATTACHED;
1654 ui->configured = 0;
1655 for (i = 0; i < ui->num_funcs; i++) {
1656 struct usb_function_info *fi = ui->func[i];
1657 if (!fi ||
1658 !(ui->composition->functions & (1 << i)))
1659 continue;
1660 if (fi->func->disconnect)
1661 fi->func->disconnect
1662 (fi->func->context);
1663 }
1664 ui->flags = USB_FLAG_VBUS_OFFLINE;
1665 queue_delayed_work(usb_work, &ui->work, 0);
1666 }
1667 }
1668
1669 return IRQ_HANDLED;
1670}
1671
1672static void usb_prepare(struct usb_info *ui)
1673{
1674 memset(ui->buf, 0, 4096);
1675 ui->head = (void *) (ui->buf + 0);
1676
1677 /* only important for reset/reinit */
1678 memset(ui->ept, 0, sizeof(ui->ept));
1679 ui->next_item = 0;
1680 ui->speed = USB_SPEED_UNKNOWN;
1681
1682 init_endpoints(ui);
1683
1684 ui->ep0in.max_pkt = 64;
1685 ui->ep0in.ui = ui;
1686 ui->ep0in.alloced = 1;
1687 ui->ep0out.max_pkt = 64;
1688 ui->ep0out.ui = ui;
1689 ui->ep0out.alloced = 1;
1690
1691 ui->setup_req = usb_ept_alloc_req(&ui->ep0in, SETUP_BUF_SIZE);
1692 ui->ep0out_req = usb_ept_alloc_req(&ui->ep0out, ui->ep0out.max_pkt);
1693
1694 INIT_WORK(&ui->chg_stop, usb_chg_stop);
1695 INIT_WORK(&ui->li.wakeup_phy, usb_lpm_wakeup_phy);
1696 INIT_DELAYED_WORK(&ui->work, usb_do_work);
1697 INIT_DELAYED_WORK(&ui->chg_legacy_det, usb_chg_legacy_detect);
1698}
1699
1700static int usb_is_online(struct usb_info *ui)
1701{
1702 /* continue lpm if bus is suspended or disconnected or stopped*/
1703 if (((readl(USB_PORTSC) & PORTSC_SUSP) == PORTSC_SUSP) ||
1704 ((readl(USB_PORTSC) & PORTSC_CCS) == 0) ||
1705 ((readl(USB_USBCMD) & USBCMD_RS) == 0))
1706 return 0;
1707
1708 pr_debug("usb is online\n");
1709 pr_debug("usbcmd:(%08x) usbsts:(%08x) portsc:(%08x)\n",
1710 readl(USB_USBCMD),
1711 readl(USB_USBSTS),
1712 readl(USB_PORTSC));
1713 return -1;
1714}
1715
1716static int usb_wakeup_phy(struct usb_info *ui)
1717{
1718 int i;
1719
1720 writel(readl(USB_USBCMD) & ~ULPI_STP_CTRL, USB_USBCMD);
1721
1722 /* some circuits automatically clear PHCD bit */
1723 for (i = 0; i < 5 && (readl(USB_PORTSC) & PORTSC_PHCD); i++) {
1724 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1725 msleep(1);
1726 }
1727
1728 if ((readl(USB_PORTSC) & PORTSC_PHCD)) {
1729 pr_err("%s: cannot clear phcd bit\n", __func__);
1730 return -1;
1731 }
1732
1733 return 0;
1734}
1735
1736static int usb_suspend_phy(struct usb_info *ui)
1737{
1738 int i;
1739 unsigned long flags;
1740
1741 if (usb_is_online(ui))
1742 return -1;
1743
1744 /* spec talks about following bits in LPM for external phy.
1745 * But they are ignored because
1746 * 1. disabling interface protection circuit: by disabling
1747 * interface protection curcuit we cannot come out
1748 * of lpm as async interrupts would be disabled
1749 * 2. setting the suspendM bit: this bit would be set by usb
1750 * controller once we set phcd bit.
1751 */
1752 switch (PHY_TYPE(ui->phy_info)) {
1753 case USB_PHY_INTEGRATED:
1754 if (!is_phy_45nm())
1755 ulpi_read(ui, 0x14);
1756
1757 /* turn on/off otg comparators */
1758 if (ui->vbus_sn_notif &&
1759 ui->usb_state == USB_STATE_NOTATTACHED)
1760 ulpi_write(ui, 0x00, 0x30);
1761 else
1762 ulpi_write(ui, 0x01, 0x30);
1763
1764 if (!is_phy_45nm())
1765 ulpi_write(ui, 0x08, 0x09);
1766
1767 break;
1768
1769 case USB_PHY_UNDEFINED:
1770 pr_err("%s: undefined phy type\n", __func__);
1771 return -1;
1772 }
1773
1774 /* loop for large amount of time */
1775 for (i = 0; i < 500; i++) {
1776 spin_lock_irqsave(&ui->lock, flags);
1777 if (usb_is_online(ui)) {
1778 spin_unlock_irqrestore(&ui->lock, flags);
1779 return -1;
1780 }
1781 /* set phy to be in lpm */
1782 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1783 spin_unlock_irqrestore(&ui->lock, flags);
1784
1785 msleep(1);
1786 if (readl(USB_PORTSC) & PORTSC_PHCD)
1787 goto blk_stp_sig;
1788 }
1789
1790 if (!(readl(USB_PORTSC) & PORTSC_PHCD)) {
1791 pr_err("unable to set phcd of portsc reg\n");
1792 pr_err("Reset HW link and phy to recover from phcd error\n");
1793 usb_hw_reset(ui);
1794 return -1;
1795 }
1796
1797 /* we have to set this bit again to work-around h/w bug */
1798 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1799
1800blk_stp_sig:
1801 /* block the stop signal */
1802 writel(readl(USB_USBCMD) | ULPI_STP_CTRL, USB_USBCMD);
1803
1804 return 0;
1805}
1806
1807/* SW workarounds
1808Issue#2 - Integrated PHY Calibration
1809Symptom - Electrical compliance failure in eye-diagram tests
1810SW workaround - Try to raise amplitude to 400mV
1811
1812Issue#3 - AHB Posted Writes
1813Symptom - USB stability
1814SW workaround - This programs xtor ON, BURST disabled and
1815 unspecified length of INCR burst enabled
1816*/
1817static int usb_hw_reset(struct usb_info *ui)
1818{
1819 unsigned i;
1820 struct msm_hsusb_platform_data *pdata;
1821 unsigned long timeout;
1822 unsigned val = 0;
1823
1824 pdata = ui->pdev->dev.platform_data;
1825
1826 clk_enable(ui->clk);
1827 /* reset the phy before resetting link */
1828 if (readl(USB_PORTSC) & PORTSC_PHCD)
1829 usb_wakeup_phy(ui);
1830 /* rpc call for phy_reset */
1831 if (ui->pdata->phy_reset)
1832 ui->pdata->phy_reset(ui->addr);
1833 else
1834 msm_hsusb_phy_reset();
1835 /* Give some delay to settle phy after reset */
1836 msleep(100);
1837
1838 /* RESET */
1839 writel(USBCMD_RESET, USB_USBCMD);
1840 timeout = jiffies + USB_LINK_RESET_TIMEOUT;
1841 while (readl(USB_USBCMD) & USBCMD_RESET) {
1842 if (time_after(jiffies, timeout)) {
1843 dev_err(&ui->pdev->dev, "usb link reset timeout\n");
1844 break;
1845 }
1846 msleep(1);
1847 }
1848
1849 /* select DEVICE mode with SDIS active */
1850 writel((USBMODE_SDIS | USBMODE_DEVICE), USB_USBMODE);
1851 msleep(1);
1852
1853 /* select ULPI phy */
1854 i = (readl(USB_PORTSC) & ~PORTSC_PTS);
1855 writel(i | PORTSC_PTS_ULPI, USB_PORTSC);
1856 /* set usb controller interrupt latency to zero*/
1857 writel((readl(USB_USBCMD) & ~USBCMD_ITC_MASK) | USBCMD_ITC(0),
1858 USB_USBCMD);
1859
1860 /* If the target is 7x01 and roc version is > 1.2, set
1861 * the AHB mode to 2 for maximum performance, else set
1862 * it to 1, to bypass the AHB transactor for stability.
1863 */
1864 if (PHY_TYPE(ui->phy_info) == USB_PHY_EXTERNAL) {
1865 if (pdata->soc_version >= SOC_ROC_2_0)
1866 writel(0x02, USB_ROC_AHB_MODE);
1867 else
1868 writel(0x01, USB_ROC_AHB_MODE);
1869 } else {
1870 unsigned cfg_val;
1871
1872 /* Raise amplitude to 400mV
1873 * SW workaround, Issue#2
1874 */
1875 cfg_val = ulpi_read(ui, ULPI_CONFIG_REG);
1876 cfg_val |= ULPI_AMPLITUDE_MAX;
1877 ulpi_write(ui, cfg_val, ULPI_CONFIG_REG);
1878
1879 writel(0x0, USB_AHB_BURST);
1880 writel(0x00, USB_AHB_MODE);
1881 }
1882
1883 /* TBD: do we have to add DpRise, ChargerRise and
1884 * IdFloatRise for 45nm
1885 */
1886 /* Disable VbusValid and SessionEnd comparators */
1887 val = ULPI_VBUS_VALID | ULPI_SESS_END;
1888
1889 /* enable id interrupt only when transceiver is available */
1890 if (ui->xceiv)
1891 writel(readl(USB_OTGSC) | OTGSC_BSVIE | OTGSC_IDIE, USB_OTGSC);
1892 else {
1893 writel((readl(USB_OTGSC) | OTGSC_BSVIE) & ~OTGSC_IDPU,
1894 USB_OTGSC);
1895 ulpi_write(ui, ULPI_IDPU, ULPI_OTG_CTRL_CLR);
1896 val |= ULPI_HOST_DISCONNECT | ULPI_ID_GND;
1897 }
1898 ulpi_write(ui, val, ULPI_INT_RISE_CLR);
1899 ulpi_write(ui, val, ULPI_INT_FALL_CLR);
1900
1901 /* we are just setting the pointer in the hwblock. Since the
1902 * endpoint isnt enabled the hw block doenst read the contents
1903 * of ui->dma - so we dont need a barrier here
1904 * */
1905 writel(ui->dma, USB_ENDPOINTLISTADDR);
1906
1907 clk_disable(ui->clk);
1908
1909 return 0;
1910}
1911
1912static void usb_reset(struct usb_info *ui)
1913{
1914 unsigned long flags;
1915
1916 spin_lock_irqsave(&ui->lock, flags);
1917 ui->running = 0;
1918 spin_unlock_irqrestore(&ui->lock, flags);
1919
1920#if 0
1921 /* we should flush and shutdown cleanly if already running */
1922 writel(0xffffffff, USB_ENDPTFLUSH);
1923 msleep(2);
1924#endif
1925
1926 if (usb_hw_reset(ui)) {
1927 pr_info("%s: h/w reset failed\n", __func__);
1928 return;
1929 }
1930
1931 usb_configure_endpoint(&ui->ep0in, NULL);
1932 usb_configure_endpoint(&ui->ep0out, NULL);
1933
1934 /* marking us offline will cause ept queue attempts to fail */
1935 ui->online = 0;
1936
1937 /* terminate any pending transactions */
1938 flush_all_endpoints(ui);
1939
1940 set_configuration(ui, 0);
1941
1942 spin_lock_irqsave(&ui->lock, flags);
1943 ui->running = 1;
1944 spin_unlock_irqrestore(&ui->lock, flags);
1945}
1946
1947static void usb_enable(void *handle, int enable)
1948{
1949 struct usb_info *ui = handle;
1950 unsigned long flags;
1951 spin_lock_irqsave(&ui->lock, flags);
1952
1953 if (enable) {
1954 ui->flags |= USB_FLAG_RESET;
1955 ui->active = 1;
1956 spin_unlock_irqrestore(&ui->lock, flags);
1957 usb_do_work(&ui->work.work);
1958 } else {
1959 ui->active = 0;
1960 spin_unlock_irqrestore(&ui->lock, flags);
1961 usb_clk_disable(ui);
1962 msm_hsusb_suspend_locks_acquire(ui, 0);
1963 }
1964}
1965
1966static struct msm_otg_ops dcd_ops = {
1967 .request = usb_enable,
1968};
1969
1970void usb_start(struct usb_info *ui)
1971{
1972 int i, ret;
1973
1974 for (i = 0; i < ui->num_funcs; i++) {
1975 struct usb_function_info *fi = ui->func[i];
1976 if (!fi || !(ui->composition->functions & (1<<i)))
1977 continue;
1978 if (fi->enabled) {
1979 pr_info("usb_bind_func() (%s)\n", fi->func->name);
1980 fi->func->bind(fi->func->context);
1981 }
1982 }
1983
1984 ui->clk_enabled = 0;
1985 ui->vreg_enabled = 0;
1986
1987 ui->xceiv = msm_otg_get_transceiver();
1988 if (ui->xceiv) {
1989 ui->flags = USB_FLAG_REG_OTG;
1990 queue_delayed_work(usb_work, &ui->work, 0);
1991 } else {
1992 /*Initialize pm app RPC */
1993 ret = msm_pm_app_rpc_init();
1994 if (ret) {
1995 pr_err("%s: pm_app_rpc connect failed\n", __func__);
1996 goto out;
1997 }
1998 pr_info("%s: pm_app_rpc connect success\n", __func__);
1999
2000 ret = msm_pm_app_register_vbus_sn(&msm_hsusb_set_vbus_state);
2001 if (ret) {
2002 pr_err("%s:PMIC VBUS SN notif not supported\n", \
2003 __func__);
2004 msm_pm_app_rpc_deinit();
2005 goto out;
2006 }
2007 pr_info("%s:PMIC VBUS SN notif supported\n", \
2008 __func__);
2009
2010 ret = msm_pm_app_enable_usb_ldo(1);
2011 if (ret) {
2012 pr_err("%s: unable to turn on internal LDO", \
2013 __func__);
2014 msm_pm_app_unregister_vbus_sn(
2015 &msm_hsusb_set_vbus_state);
2016 msm_pm_app_rpc_deinit();
2017 goto out;
2018 }
2019 ui->vbus_sn_notif = 1;
2020out:
2021 ui->active = 1;
2022 ui->flags |= (USB_FLAG_START | USB_FLAG_RESET);
2023 queue_delayed_work(usb_work, &ui->work, 0);
2024 }
2025
2026}
2027
2028static LIST_HEAD(usb_function_list);
2029static DEFINE_MUTEX(usb_function_list_lock);
2030
2031
2032static struct usb_function_info *usb_find_function(const char *name)
2033{
2034 struct list_head *entry;
2035 list_for_each(entry, &usb_function_list) {
2036 struct usb_function_info *fi =
2037 list_entry(entry, struct usb_function_info, list);
2038 if (fi) {
2039 if (!strcmp(name, fi->func->name))
2040 return fi;
2041 }
2042 }
2043
2044 return NULL;
2045}
2046
2047static void usb_try_to_bind(void)
2048{
2049 struct usb_info *ui = the_usb_info;
2050 unsigned long enabled_functions = 0;
2051 int i;
2052
2053 if (!ui || ui->bound || !ui->pdev || !ui->composition)
2054 return;
2055
2056 for (i = 0; i < ui->num_funcs; i++) {
2057 if (ui->func[i])
2058 enabled_functions |= (1 << i);
2059 }
2060 if ((enabled_functions & ui->composition->functions)
2061 != ui->composition->functions)
2062 return;
2063
2064 usb_set_composition(ui->composition->product_id);
2065 usb_configure_device_descriptor(ui);
2066
2067 /* we have found all the needed functions */
2068 ui->bound = 1;
2069 printk(KERN_INFO "msm_hsusb: functions bound. starting.\n");
2070 usb_start(ui);
2071}
2072
2073static int usb_get_function_index(const char *name)
2074{
2075 struct usb_info *ui = the_usb_info;
2076 int i;
2077
2078 for (i = 0; i < ui->num_funcs; i++) {
2079 if (!strcmp(name, ui->functions_map[i].name))
2080 return i;
2081 }
2082 return -1;
2083}
2084
2085int usb_function_register(struct usb_function *driver)
2086{
2087 struct usb_info *ui = the_usb_info;
2088 struct usb_function_info *fi;
2089 int ret = 0;
2090 int index;
2091
2092 mutex_lock(&usb_function_list_lock);
2093
2094 index = usb_get_function_index(driver->name);
2095 if (index < 0) {
2096 pr_err("%s: unsupported function = %s\n",
2097 __func__, driver->name);
2098 ret = -EINVAL;
2099 goto fail;
2100 }
2101
2102 fi = kzalloc(sizeof(*fi), GFP_KERNEL);
2103 if (!fi) {
2104 ret = -ENOMEM;
2105 goto fail;
2106 }
2107
2108 fi->func = driver;
2109 list_add(&fi->list, &usb_function_list);
2110 ui->func[index] = fi;
2111 fi->func->ep0_out_req = ui->ep0out_req;
2112 fi->func->ep0_in_req = ui->setup_req;
2113 fi->func->ep0_out = &ui->ep0out;
2114 fi->func->ep0_in = &ui->ep0in;
2115 pr_info("%s: name = '%s', map = %d\n", __func__, driver->name, index);
2116
2117 usb_try_to_bind();
2118fail:
2119 mutex_unlock(&usb_function_list_lock);
2120 return ret;
2121}
2122EXPORT_SYMBOL(usb_function_register);
2123
2124static unsigned short usb_validate_product_id(unsigned short pid)
2125{
2126 struct usb_info *ui = the_usb_info;
2127 int i;
2128
2129 if (!ui || !ui->pdata)
2130 return -1;
2131
2132 /* set idProduct based on which functions are enabled */
2133 for (i = 0; i < ui->pdata->num_compositions; i++) {
2134 if (ui->pdata->compositions[i].product_id == pid)
2135 break;
2136 }
2137
2138 if (i < ui->pdata->num_compositions) {
2139 struct usb_composition *comp = &ui->pdata->compositions[i];
2140 for (i = 0; i < ui->num_funcs; i++) {
2141 if (comp->functions & (1 << i)) {
2142 if (!ui->func[i]) {
2143 pr_err("%s: func(%d) not available\n",
2144 __func__, i);
2145 return 0;
2146 }
2147 }
2148 }
2149 return comp->product_id;
2150 } else
2151 pr_err("%s: Product id (%x) is not supported\n", __func__, pid);
2152 return 0;
2153}
2154
2155static unsigned short usb_get_product_id(unsigned long enabled_functions)
2156{
2157 struct usb_info *ui = the_usb_info;
2158 int i;
2159
2160 if (!(ui && ui->pdata))
2161 return -1;
2162
2163 /* set idProduct based on which functions are enabled */
2164 for (i = 0; i < ui->pdata->num_compositions; i++) {
2165 if (ui->pdata->compositions[i].functions == enabled_functions)
2166 return ui->pdata->compositions[i].product_id;
2167 }
2168 return 0;
2169}
2170
2171static void usb_uninit(struct usb_info *ui)
2172{
2173 int i;
2174
2175 for (i = 0; i < ui->strdesc_index; i++)
2176 kfree(ui->strdesc[i]);
2177 ui->strdesc_index = 1;
2178 ui->next_ifc_num = 0;
2179}
2180
2181static unsigned short usb_set_composition(unsigned short pid)
2182{
2183 struct usb_info *ui = the_usb_info;
2184 int i;
2185
2186 if (!(ui && ui->pdata))
2187 return 0;
2188
2189 /* Retrieve product id on enabled functions */
2190 for (i = 0; i < ui->pdata->num_compositions; i++) {
2191 if (ui->pdata->compositions[i].product_id == pid) {
2192 ui->composition = &ui->pdata->compositions[i];
2193 for (i = 0; i < ui->num_funcs; i++) {
2194 struct usb_function_info *fi = ui->func[i];
2195 if (ui->func && fi && fi->func) {
2196 fi->enabled = (ui->composition->
2197 functions >> i) & 1;
2198 }
2199 }
2200 pr_info("%s: composition set to product id = %x\n",
2201 __func__, ui->composition->product_id);
2202 return ui->composition->product_id;
2203 }
2204 }
2205 pr_err("%s: product id (%x) not supported\n", __func__, pid);
2206 return 0;
2207}
2208
2209static void usb_switch_composition(unsigned short pid)
2210{
2211 struct usb_info *ui = the_usb_info;
2212 int i;
2213 unsigned long flags;
2214
2215
2216 if (!ui->active)
2217 return;
2218 if (!usb_validate_product_id(pid))
2219 return;
2220
2221 disable_irq(ui->irq);
2222 if (cancel_delayed_work_sync(&ui->work))
2223 pr_info("%s: Removed work successfully\n", __func__);
2224 if (ui->running) {
2225 spin_lock_irqsave(&ui->lock, flags);
2226 ui->running = 0;
2227 ui->online = 0;
2228 ui->bound = 0;
2229 spin_unlock_irqrestore(&ui->lock, flags);
2230 /* we should come out of lpm to access registers */
2231 if (ui->in_lpm) {
2232 if (PHY_TYPE(ui->phy_info) == USB_PHY_EXTERNAL) {
2233 disable_irq(ui->gpio_irq[0]);
2234 disable_irq(ui->gpio_irq[1]);
2235 }
2236
2237 if (ui->usb_state == USB_STATE_NOTATTACHED
2238 && ui->vbus_sn_notif)
2239 msm_pm_app_enable_usb_ldo(1);
2240
2241 usb_lpm_exit(ui);
2242 if (cancel_work_sync(&ui->li.wakeup_phy))
2243 usb_lpm_wakeup_phy(NULL);
2244 ui->in_lpm = 0;
2245 }
2246 /* disable usb and session valid interrupts */
2247 writel(0, USB_USBINTR);
2248 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
2249
2250 /* stop the controller */
2251 usb_disable_pullup(ui);
2252 ui->usb_state = USB_STATE_NOTATTACHED;
2253 switch_set_state(&ui->sdev, 0);
2254 /* Before starting again, wait for 300ms
2255 * to make sure host detects soft disconnection
2256 **/
2257 msleep(300);
2258 }
2259
2260 for (i = 0; i < ui->num_funcs; i++) {
2261 struct usb_function_info *fi = ui->func[i];
2262 if (!fi || !fi->func || !fi->enabled)
2263 continue;
2264 if (fi->func->configure)
2265 fi->func->configure(0, fi->func->context);
2266 if (fi->func->unbind)
2267 fi->func->unbind(fi->func->context);
2268 }
2269
2270 usb_uninit(ui);
2271 usb_set_composition(pid);
2272 usb_configure_device_descriptor(ui);
2273
2274 /* initialize functions */
2275 for (i = 0; i < ui->num_funcs; i++) {
2276 struct usb_function_info *fi = ui->func[i];
2277 if (!fi || !(ui->composition->functions & (1 << i)))
2278 continue;
2279 if (fi->enabled) {
2280 if (fi->func->bind)
2281 fi->func->bind(fi->func->context);
2282 }
2283 }
2284
2285 ui->bound = 1;
2286 ui->flags = USB_FLAG_RESET;
2287 queue_delayed_work(usb_work, &ui->work, 0);
2288 enable_irq(ui->irq);
2289}
2290
2291void usb_function_enable(const char *function, int enable)
2292{
2293 struct usb_function_info *fi;
2294 struct usb_info *ui = the_usb_info;
2295 unsigned long functions_mask;
2296 int curr_enable;
2297 unsigned short pid;
2298 int i;
2299
2300 if (!ui)
2301 return;
2302
2303 pr_info("%s: name = %s, enable = %d\n", __func__, function, enable);
2304
2305 fi = usb_find_function(function);
2306 if (!fi) {
2307 pr_err("%s: function (%s) not registered with DCD\n",
2308 __func__, function);
2309 return;
2310 }
2311 if (fi->enabled == enable) {
2312 pr_err("%s: function (%s) state is same\n",
2313 __func__, function);
2314 return;
2315 }
2316 functions_mask = 0;
2317 curr_enable = fi->enabled;
2318 fi->enabled = enable;
2319 for (i = 0; i < ui->num_funcs; i++) {
2320 struct usb_function_info *fi = ui->func[i];
2321 if (fi && fi->enabled)
2322 functions_mask |= (1 << i);
2323 }
2324
2325 pid = usb_get_product_id(functions_mask);
2326 if (!pid) {
2327 fi->enabled = curr_enable;
2328 pr_err("%s: mask (%lx) not matching with any products\n",
2329 __func__, functions_mask);
2330 pr_err("%s: continuing with current composition\n", __func__);
2331 return;
2332 }
2333 usb_switch_composition(pid);
2334}
2335EXPORT_SYMBOL(usb_function_enable);
2336
2337static int usb_free(struct usb_info *ui, int ret)
2338{
2339 disable_irq_wake(ui->irq);
2340 free_irq(ui->irq, ui);
2341 if (ui->gpio_irq[0])
2342 free_irq(ui->gpio_irq[0], NULL);
2343 if (ui->gpio_irq[1])
2344 free_irq(ui->gpio_irq[1], NULL);
2345
2346 dma_pool_destroy(ui->pool);
2347 dma_free_coherent(&ui->pdev->dev, 4096, ui->buf, ui->dma);
2348 kfree(ui->func);
2349 kfree(ui->strdesc);
2350 iounmap(ui->addr);
2351 clk_put(ui->clk);
2352 clk_put(ui->pclk);
2353 clk_put(ui->cclk);
2354 msm_hsusb_suspend_locks_init(ui, 0);
2355 kfree(ui);
2356
2357 return ret;
2358}
2359
2360static int usb_vbus_is_on(struct usb_info *ui)
2361{
2362 unsigned tmp;
2363
2364 /* disable session valid raising and falling interrupts */
2365 ulpi_write(ui, ULPI_SESSION_VALID_RAISE, ULPI_USBINTR_ENABLE_RASING_C);
2366 ulpi_write(ui, ULPI_SESSION_VALID_FALL, ULPI_USBINTR_ENABLE_FALLING_C);
2367
2368 tmp = ulpi_read(ui, ULPI_USBINTR_STATUS);
2369
2370 /* enable session valid raising and falling interrupts */
2371 ulpi_write(ui, ULPI_SESSION_VALID_RAISE, ULPI_USBINTR_ENABLE_RASING_S);
2372 ulpi_write(ui, ULPI_SESSION_VALID_FALL, ULPI_USBINTR_ENABLE_FALLING_S);
2373
2374 if (tmp & (1 << 2))
2375 return 1;
2376 return 0;
2377}
2378static void usb_do_work(struct work_struct *w)
2379{
2380 struct usb_info *ui = container_of(w, struct usb_info, work.work);
2381 unsigned long iflags;
2382 unsigned long flags, ret;
2383
2384 for (;;) {
2385 spin_lock_irqsave(&ui->lock, iflags);
2386 flags = ui->flags;
2387 ui->flags = 0;
2388 spin_unlock_irqrestore(&ui->lock, iflags);
2389
2390 /* give up if we have nothing to do */
2391 if (flags == 0)
2392 break;
2393
2394 switch (ui->state) {
2395 case USB_STATE_IDLE:
2396 if (flags & USB_FLAG_REG_OTG) {
2397 dcd_ops.handle = (void *) ui;
2398 ret = ui->xceiv->set_peripheral(ui->xceiv,
2399 &dcd_ops);
2400 if (ret)
2401 pr_err("%s: Can't register peripheral"
2402 "driver with OTG", __func__);
2403 break;
2404 }
2405 if ((flags & USB_FLAG_START) ||
2406 (flags & USB_FLAG_RESET)) {
2407 disable_irq(ui->irq);
2408 if (ui->vbus_sn_notif)
2409 msm_pm_app_enable_usb_ldo(1);
2410 usb_clk_enable(ui);
2411 usb_vreg_enable(ui);
2412 usb_vbus_online(ui);
2413
2414 /* if VBUS is present move to ONLINE state
2415 * otherwise move to OFFLINE state
2416 */
2417 if (usb_vbus_is_on(ui)) {
2418 ui->usb_state = USB_STATE_POWERED;
2419 msm_hsusb_suspend_locks_acquire(ui, 1);
2420 ui->state = USB_STATE_ONLINE;
2421 usb_enable_pullup(ui);
2422 schedule_delayed_work(
2423 &ui->chg_legacy_det,
2424 USB_CHG_DET_DELAY);
2425 pr_info("hsusb: IDLE -> ONLINE\n");
2426 } else {
2427 ui->usb_state = USB_STATE_NOTATTACHED;
2428 ui->state = USB_STATE_OFFLINE;
2429
2430 msleep(500);
2431 usb_lpm_enter(ui);
2432 pr_info("hsusb: IDLE -> OFFLINE\n");
2433 if (ui->vbus_sn_notif)
2434 msm_pm_app_enable_usb_ldo(0);
2435 }
2436 enable_irq(ui->irq);
2437 break;
2438 }
2439 goto reset;
2440
2441 case USB_STATE_ONLINE:
2442 /* If at any point when we were online, we received
2443 * the signal to go offline, we must honor it
2444 */
2445 if (flags & USB_FLAG_VBUS_OFFLINE) {
2446 enum charger_type temp;
2447 unsigned long f;
2448
2449 cancel_delayed_work_sync(&ui->chg_legacy_det);
2450
2451 spin_lock_irqsave(&ui->lock, f);
2452 temp = ui->chg_type;
2453 ui->chg_type = USB_CHG_TYPE__INVALID;
2454 spin_unlock_irqrestore(&ui->lock, f);
2455
2456 if (temp != USB_CHG_TYPE__INVALID) {
2457 /* re-acquire wakelock and restore axi
2458 * freq if they have been reduced by
2459 * charger work item
2460 */
2461 msm_hsusb_suspend_locks_acquire(ui, 1);
2462
2463 msm_chg_usb_i_is_not_available();
2464 msm_chg_usb_charger_disconnected();
2465 }
2466
2467 /* reset usb core and usb phy */
2468 disable_irq(ui->irq);
2469 if (ui->in_lpm)
2470 usb_lpm_exit(ui);
2471 usb_vbus_offline(ui);
2472 usb_lpm_enter(ui);
2473 if ((ui->vbus_sn_notif) &&
2474 (ui->usb_state == USB_STATE_NOTATTACHED))
2475 msm_pm_app_enable_usb_ldo(0);
2476 ui->state = USB_STATE_OFFLINE;
2477 enable_irq(ui->irq);
2478 switch_set_state(&ui->sdev, 0);
2479 pr_info("hsusb: ONLINE -> OFFLINE\n");
2480 break;
2481 }
2482 if (flags & USB_FLAG_SUSPEND) {
2483 ui->usb_state = USB_STATE_SUSPENDED;
2484 usb_lpm_enter(ui);
2485 msm_hsusb_suspend_locks_acquire(ui, 1);
2486 break;
2487 }
2488 if ((flags & USB_FLAG_RESUME) ||
2489 (flags & USB_FLAG_CONFIGURE)) {
2490 int maxpower = usb_get_max_power(ui);
2491
2492 if (maxpower > 0)
2493 msm_chg_usb_i_is_available(maxpower);
2494
2495 if (flags & USB_FLAG_CONFIGURE)
2496 switch_set_state(&ui->sdev, 1);
2497
2498 break;
2499 }
2500 goto reset;
2501
2502 case USB_STATE_OFFLINE:
2503 /* If we were signaled to go online and vbus is still
2504 * present when we received the signal, go online.
2505 */
2506 if ((flags & USB_FLAG_VBUS_ONLINE)) {
2507 msm_hsusb_suspend_locks_acquire(ui, 1);
2508 disable_irq(ui->irq);
2509 ui->state = USB_STATE_ONLINE;
2510 if (ui->in_lpm)
2511 usb_lpm_exit(ui);
2512 usb_vbus_online(ui);
2513 if (!(B_SESSION_VALID & readl(USB_OTGSC))) {
2514 writel(((readl(USB_OTGSC) &
2515 ~OTGSC_INTR_STS_MASK) |
2516 OTGSC_BSVIS), USB_OTGSC);
2517 enable_irq(ui->irq);
2518 goto reset;
2519 }
2520 usb_enable_pullup(ui);
2521 schedule_delayed_work(
2522 &ui->chg_legacy_det,
2523 USB_CHG_DET_DELAY);
2524 pr_info("hsusb: OFFLINE -> ONLINE\n");
2525 enable_irq(ui->irq);
2526 break;
2527 }
2528 if (flags & USB_FLAG_SUSPEND) {
2529 usb_lpm_enter(ui);
2530 wake_unlock(&ui->wlock);
2531 break;
2532 }
2533 default:
2534reset:
2535 /* For RESET or any unknown flag in a particular state
2536 * go to IDLE state and reset HW to bring to known state
2537 */
2538 ui->flags = USB_FLAG_RESET;
2539 ui->state = USB_STATE_IDLE;
2540 }
2541 }
2542}
2543
2544void msm_hsusb_set_vbus_state(int online)
2545{
2546 struct usb_info *ui = the_usb_info;
2547
2548 if (ui && online) {
2549 msm_pm_app_enable_usb_ldo(1);
2550 usb_lpm_exit(ui);
2551 /* Turn on PHY comparators */
2552 if (!(ulpi_read(ui, 0x30) & 0x01))
2553 ulpi_write(ui, 0x01, 0x30);
2554 }
2555}
2556
2557static irqreturn_t usb_lpm_gpio_isr(int irq, void *data)
2558{
2559 disable_irq(irq);
2560
2561 return IRQ_HANDLED;
2562}
2563
2564static void usb_lpm_exit(struct usb_info *ui)
2565{
2566 if (ui->in_lpm == 0)
2567 return;
2568
2569 if (usb_lpm_config_gpio)
2570 usb_lpm_config_gpio(0);
2571
2572 wake_lock(&ui->wlock);
2573 usb_clk_enable(ui);
2574 usb_vreg_enable(ui);
2575
2576 writel(readl(USB_USBCMD) & ~ASYNC_INTR_CTRL, USB_USBCMD);
2577 writel(readl(USB_USBCMD) & ~ULPI_STP_CTRL, USB_USBCMD);
2578
2579 if (readl(USB_PORTSC) & PORTSC_PHCD) {
2580 disable_irq(ui->irq);
2581 schedule_work(&ui->li.wakeup_phy);
2582 } else {
2583 ui->in_lpm = 0;
2584 if (ui->xceiv)
2585 ui->xceiv->set_suspend(ui->xceiv, 0);
2586 }
2587 pr_info("%s(): USB exited from low power mode\n", __func__);
2588}
2589
2590static int usb_lpm_enter(struct usb_info *ui)
2591{
2592 unsigned long flags;
2593 unsigned connected;
2594
2595 spin_lock_irqsave(&ui->lock, flags);
2596 if (ui->in_lpm) {
2597 spin_unlock_irqrestore(&ui->lock, flags);
2598 pr_debug("already in lpm, nothing to do\n");
2599 return 0;
2600 }
2601
2602 if (usb_is_online(ui)) {
2603 spin_unlock_irqrestore(&ui->lock, flags);
2604 pr_info("%s: lpm procedure aborted\n", __func__);
2605 return -1;
2606 }
2607
2608 ui->in_lpm = 1;
2609 if (ui->xceiv)
2610 ui->xceiv->set_suspend(ui->xceiv, 1);
2611 disable_irq(ui->irq);
2612 spin_unlock_irqrestore(&ui->lock, flags);
2613
2614 if (usb_suspend_phy(ui)) {
2615 ui->in_lpm = 0;
2616 ui->flags = USB_FLAG_RESET;
2617 enable_irq(ui->irq);
2618 pr_err("%s: phy suspend failed, lpm procedure aborted\n",
2619 __func__);
2620 return -1;
2621 }
2622
2623 if ((B_SESSION_VALID & readl(USB_OTGSC)) &&
2624 (ui->usb_state == USB_STATE_NOTATTACHED)) {
2625 ui->in_lpm = 0;
2626 writel(((readl(USB_OTGSC) & ~OTGSC_INTR_STS_MASK) |
2627 OTGSC_BSVIS), USB_OTGSC);
2628 ui->flags = USB_FLAG_VBUS_ONLINE;
2629 ui->usb_state = USB_STATE_POWERED;
2630 usb_wakeup_phy(ui);
2631 enable_irq(ui->irq);
2632 return -1;
2633 }
2634
2635 /* enable async interrupt */
2636 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL, USB_USBCMD);
2637 connected = readl(USB_USBCMD) & USBCMD_RS;
2638
2639 usb_vreg_disable(ui);
2640 usb_clk_disable(ui);
2641
2642 if (usb_lpm_config_gpio) {
2643 if (usb_lpm_config_gpio(1)) {
2644 spin_lock_irqsave(&ui->lock, flags);
2645 usb_lpm_exit(ui);
2646 spin_unlock_irqrestore(&ui->lock, flags);
2647 enable_irq(ui->irq);
2648 return -1;
2649 }
2650 enable_irq(ui->gpio_irq[0]);
2651 enable_irq(ui->gpio_irq[1]);
2652 }
2653
2654 enable_irq(ui->irq);
2655 msm_hsusb_suspend_locks_acquire(ui, 0);
2656 pr_info("%s: usb in low power mode\n", __func__);
2657 return 0;
2658}
2659
2660static void usb_enable_pullup(struct usb_info *ui)
2661{
2662 disable_irq(ui->irq);
2663 writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR);
2664 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2665 enable_irq(ui->irq);
2666}
2667
2668/* SW workarounds
2669Issue #1 - USB Spoof Disconnect Failure
2670Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2671SW workaround - Making opmode non-driving and SuspendM set in function
2672 register of SMSC phy
2673*/
2674static void usb_disable_pullup(struct usb_info *ui)
2675{
2676 disable_irq(ui->irq);
2677 writel(readl(USB_USBINTR) & ~(STS_URI | STS_SLI | STS_UI | STS_PCI),
2678 USB_USBINTR);
2679 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2680
2681 /* S/W workaround, Issue#1 */
2682 if (!is_phy_external() && !is_phy_45nm())
2683 ulpi_write(ui, 0x48, 0x04);
2684
2685 enable_irq(ui->irq);
2686}
2687
2688static void usb_chg_stop(struct work_struct *w)
2689{
2690 struct usb_info *ui = the_usb_info;
2691 enum charger_type temp;
2692 unsigned long flags;
2693
2694 spin_lock_irqsave(&ui->lock, flags);
2695 temp = ui->chg_type;
2696 spin_unlock_irqrestore(&ui->lock, flags);
2697
2698 if (temp == USB_CHG_TYPE__SDP)
2699 msm_chg_usb_i_is_not_available();
2700}
2701
2702static void usb_vbus_online(struct usb_info *ui)
2703{
2704 if (ui->in_lpm) {
2705 if (usb_lpm_config_gpio)
2706 usb_lpm_config_gpio(0);
2707 usb_vreg_enable(ui);
2708 usb_clk_enable(ui);
2709 usb_wakeup_phy(ui);
2710 ui->in_lpm = 0;
2711 }
2712
2713 usb_reset(ui);
2714}
2715
2716static void usb_vbus_offline(struct usb_info *ui)
2717{
2718 unsigned long timeout;
2719 unsigned val = 0;
2720
2721 if (ui->online != 0) {
2722 ui->online = 0;
2723 flush_all_endpoints(ui);
2724 set_configuration(ui, 0);
2725 }
2726
2727 /* reset h/w at cable disconnetion becasuse
2728 * of h/w bugs and to flush any resource that
2729 * h/w might be holding
2730 */
2731 clk_enable(ui->clk);
2732
2733 if (readl(USB_PORTSC) & PORTSC_PHCD)
2734 usb_wakeup_phy(ui);
2735
2736 if (ui->pdata->phy_reset)
2737 ui->pdata->phy_reset(ui->addr);
2738 else
2739 msm_hsusb_phy_reset();
2740 /* Give some delay to settle phy after reset */
2741 msleep(100);
2742
2743 writel(USBCMD_RESET, USB_USBCMD);
2744 timeout = jiffies + USB_LINK_RESET_TIMEOUT;
2745 while (readl(USB_USBCMD) & USBCMD_RESET) {
2746 if (time_after(jiffies, timeout)) {
2747 dev_err(&ui->pdev->dev, "usb link reset timeout\n");
2748 break;
2749 }
2750 msleep(1);
2751 }
2752
2753 /* Disable VbusValid and SessionEnd comparators */
2754 val = ULPI_VBUS_VALID | ULPI_SESS_END;
2755
2756 /* enable id interrupt only when transceiver is available */
2757 if (ui->xceiv)
2758 writel(readl(USB_OTGSC) | OTGSC_BSVIE | OTGSC_IDIE, USB_OTGSC);
2759 else {
2760 writel((readl(USB_OTGSC) | OTGSC_BSVIE) & ~OTGSC_IDPU,
2761 USB_OTGSC);
2762 ulpi_write(ui, ULPI_IDPU, ULPI_OTG_CTRL_CLR);
2763 val |= ULPI_HOST_DISCONNECT | ULPI_ID_GND;
2764 }
2765 ulpi_write(ui, val, ULPI_INT_RISE_CLR);
2766 ulpi_write(ui, val, ULPI_INT_FALL_CLR);
2767
2768 clk_disable(ui->clk);
2769}
2770
2771static void usb_lpm_wakeup_phy(struct work_struct *w)
2772{
2773 struct usb_info *ui = the_usb_info;
2774 unsigned long flags;
2775
2776 if (usb_wakeup_phy(ui)) {
2777 pr_err("fatal error: cannot bring phy out of lpm\n");
2778 pr_err("%s: resetting controller\n", __func__);
2779
2780 spin_lock_irqsave(&ui->lock, flags);
2781 usb_disable_pullup(ui);
2782 ui->flags = USB_FLAG_RESET;
2783 queue_delayed_work(usb_work, &ui->work, 0);
2784 enable_irq(ui->irq);
2785 spin_unlock_irqrestore(&ui->lock, flags);
2786 return;
2787 }
2788
2789 ui->in_lpm = 0;
2790 if (ui->xceiv)
2791 ui->xceiv->set_suspend(ui->xceiv, 0);
2792 enable_irq(ui->irq);
2793}
2794
2795void usb_function_reenumerate(void)
2796{
2797 struct usb_info *ui = the_usb_info;
2798
2799 /* disable and re-enable the D+ pullup */
2800 pr_info("hsusb: disable pullup\n");
2801 usb_disable_pullup(ui);
2802
2803 msleep(10);
2804
2805 pr_info("hsusb: enable pullup\n");
2806 usb_enable_pullup(ui);
2807}
2808
2809#if defined(CONFIG_DEBUG_FS)
2810static char debug_buffer[PAGE_SIZE];
2811
2812static ssize_t debug_read_status(struct file *file, char __user *ubuf,
2813 size_t count, loff_t *ppos)
2814{
2815 struct usb_info *ui = file->private_data;
2816 char *buf = debug_buffer;
2817 unsigned long flags;
2818 struct usb_endpoint *ept;
2819 struct msm_request *req;
2820 int n;
2821 int i = 0;
2822
2823 spin_lock_irqsave(&ui->lock, flags);
2824
2825 i += scnprintf(buf + i, PAGE_SIZE - i,
2826 "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
2827 readl(USB_ENDPTSETUPSTAT),
2828 readl(USB_ENDPTPRIME),
2829 readl(USB_ENDPTSTAT),
2830 readl(USB_ENDPTCOMPLETE));
2831 i += scnprintf(buf + i, PAGE_SIZE - i,
2832 "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
2833 readl(USB_USBCMD),
2834 readl(USB_USBSTS),
2835 readl(USB_USBINTR),
2836 readl(USB_PORTSC));
2837
2838
2839 for (n = 0; n < 32; n++) {
2840 ept = ui->ept + n;
2841 if (ept->max_pkt == 0)
2842 continue;
2843
2844 i += scnprintf(buf + i, PAGE_SIZE - i,
2845 "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
2846 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
2847 ept->head->config, ept->head->active,
2848 ept->head->next, ept->head->info);
2849
2850 for (req = ept->req; req; req = req->next)
2851 i += scnprintf(buf + i, PAGE_SIZE - i,
2852 " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
2853 req->item_dma, req->item->next,
2854 req->item->info, req->item->page0,
2855 req->busy ? 'B' : ' ',
2856 req->live ? 'L' : ' '
2857 );
2858 }
2859
2860 i += scnprintf(buf + i, PAGE_SIZE - i,
2861 "phy failure count: %d\n", ui->phy_fail_count);
2862
2863 spin_unlock_irqrestore(&ui->lock, flags);
2864
2865 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
2866}
2867
2868
2869static ssize_t debug_write_reset(struct file *file, const char __user *buf,
2870 size_t count, loff_t *ppos)
2871{
2872 struct usb_info *ui = file->private_data;
2873 unsigned long flags;
2874
2875 spin_lock_irqsave(&ui->lock, flags);
2876 ui->flags |= USB_FLAG_RESET;
2877 queue_delayed_work(usb_work, &ui->work, 0);
2878 spin_unlock_irqrestore(&ui->lock, flags);
2879
2880 return count;
2881}
2882
2883
2884static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
2885 size_t count, loff_t *ppos)
2886{
2887 usb_function_reenumerate();
2888 return count;
2889}
2890
2891static int debug_open(struct inode *inode, struct file *file)
2892{
2893 file->private_data = inode->i_private;
2894 return 0;
2895}
2896
2897const struct file_operations debug_stat_ops = {
2898 .open = debug_open,
2899 .read = debug_read_status,
2900};
2901
2902
2903
2904const struct file_operations debug_reset_ops = {
2905 .open = debug_open,
2906 .write = debug_write_reset,
2907};
2908
2909const struct file_operations debug_cycle_ops = {
2910 .open = debug_open,
2911 .write = debug_write_cycle,
2912};
2913
2914static struct dentry *debugfs_dent;
2915static struct dentry *debugfs_status;
2916static struct dentry *debugfs_reset;
2917static struct dentry *debugfs_cycle;
2918static void usb_debugfs_init(struct usb_info *ui)
2919{
2920 debugfs_dent = debugfs_create_dir("usb", 0);
2921 if (IS_ERR(debugfs_dent))
2922 return;
2923
2924 debugfs_status = debugfs_create_file("status", 0444,
2925 debugfs_dent, ui, &debug_stat_ops);
2926 debugfs_reset = debugfs_create_file("reset", 0222,
2927 debugfs_dent, ui, &debug_reset_ops);
2928 debugfs_cycle = debugfs_create_file("cycle", 0222,
2929 debugfs_dent, ui, &debug_cycle_ops);
2930}
2931
2932static void usb_debugfs_uninit(void)
2933{
2934 debugfs_remove(debugfs_status);
2935 debugfs_remove(debugfs_reset);
2936 debugfs_remove(debugfs_cycle);
2937 debugfs_remove(debugfs_dent);
2938}
2939
2940#else
2941static void usb_debugfs_init(struct usb_info *ui) {}
2942static void usb_debugfs_uninit(void) {}
2943#endif
2944
2945static void usb_configure_device_descriptor(struct usb_info *ui)
2946{
2947 desc_device.idVendor = ui->pdata->vendor_id;
2948 desc_device.idProduct = ui->composition->product_id;
2949 desc_device.bcdDevice = ui->pdata->version;
2950
2951 if (ui->pdata->serial_number)
2952 desc_device.iSerialNumber =
2953 usb_msm_get_next_strdesc_id(ui->pdata->serial_number);
2954 if (ui->pdata->product_name)
2955 desc_device.iProduct =
2956 usb_msm_get_next_strdesc_id(ui->pdata->product_name);
2957 if (ui->pdata->manufacturer_name)
2958 desc_device.iManufacturer =
2959 usb_msm_get_next_strdesc_id(
2960 ui->pdata->manufacturer_name);
2961
2962 /* Send Serial number to A9 for software download */
2963 if (ui->pdata->serial_number) {
2964 msm_hsusb_is_serial_num_null(FALSE);
2965 msm_hsusb_send_serial_number(ui->pdata->serial_number);
2966 } else
2967 msm_hsusb_is_serial_num_null(TRUE);
2968
2969 msm_hsusb_send_productID(desc_device.idProduct);
2970
2971}
2972static ssize_t msm_hsusb_store_func_enable(struct device *dev,
2973 struct device_attribute *attr,
2974 const char *buf, size_t size)
2975{
2976 char name[20];
2977 int enable = 0;
2978 int i;
2979
2980 for (i = 0; buf[i] != 0; i++) {
2981 if (buf[i] == '=')
2982 break;
2983 name[i] = buf[i];
2984 }
2985 name[i++] = 0;
2986 if (buf[i] == '0' || buf[i] == '1')
2987 enable = buf[i] - '0';
2988 else
2989 return size;
2990
2991 pr_info("%s: name = %s, enable = %d\n", __func__, name, enable);
2992 usb_function_enable(name, enable);
2993 return size;
2994}
2995static ssize_t msm_hsusb_show_compswitch(struct device *dev,
2996 struct device_attribute *attr,
2997 char *buf)
2998{
2999 struct usb_info *ui = the_usb_info;
3000 int i;
3001
3002 if (ui->composition)
3003 i = scnprintf(buf, PAGE_SIZE,
3004 "composition product id = %x\n",
3005 ui->composition->product_id);
3006 else
3007 i = scnprintf(buf, PAGE_SIZE,
3008 "composition product id = 0\n");
3009 return i;
3010}
3011
3012static ssize_t msm_hsusb_store_compswitch(struct device *dev,
3013 struct device_attribute *attr,
3014 const char *buf, size_t size)
3015{
3016 unsigned long pid;
3017
3018 if (!strict_strtoul(buf, 16, &pid)) {
3019 pr_info("%s: Requested New Product id = %lx\n", __func__, pid);
3020 usb_switch_composition((unsigned short)pid);
3021 } else
3022 pr_info("%s: strict_strtoul conversion failed\n", __func__);
3023
3024 return size;
3025}
3026static ssize_t msm_hsusb_store_autoresume(struct device *dev,
3027 struct device_attribute *attr,
3028 const char *buf, size_t size)
3029{
3030 usb_remote_wakeup();
3031
3032 return size;
3033}
3034
3035static ssize_t msm_hsusb_show_state(struct device *dev,
3036 struct device_attribute *attr,
3037 char *buf)
3038{
3039 struct usb_info *ui = the_usb_info;
3040 int i;
3041 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
3042 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
3043 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
3044 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
3045 "USB_STATE_SUSPENDED"
3046 };
3047
3048 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[ui->usb_state]);
3049 return i;
3050}
3051
3052static ssize_t msm_hsusb_show_lpm(struct device *dev,
3053 struct device_attribute *attr,
3054 char *buf)
3055{
3056 struct usb_info *ui = the_usb_info;
3057 int i;
3058
3059 i = scnprintf(buf, PAGE_SIZE, "%d\n", ui->in_lpm);
3060 return i;
3061}
3062
3063static ssize_t msm_hsusb_show_speed(struct device *dev,
3064 struct device_attribute *attr,
3065 char *buf)
3066{
3067 struct usb_info *ui = the_usb_info;
3068 int i;
3069 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
3070 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
3071
3072 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->speed]);
3073 return i;
3074}
3075
3076static DEVICE_ATTR(composition, 0664,
3077 msm_hsusb_show_compswitch, msm_hsusb_store_compswitch);
3078static DEVICE_ATTR(func_enable, S_IWUSR,
3079 NULL, msm_hsusb_store_func_enable);
3080static DEVICE_ATTR(autoresume, 0222,
3081 NULL, msm_hsusb_store_autoresume);
3082static DEVICE_ATTR(state, 0664, msm_hsusb_show_state, NULL);
3083static DEVICE_ATTR(lpm, 0664, msm_hsusb_show_lpm, NULL);
3084static DEVICE_ATTR(speed, 0664, msm_hsusb_show_speed, NULL);
3085
3086static struct attribute *msm_hsusb_attrs[] = {
3087 &dev_attr_composition.attr,
3088 &dev_attr_func_enable.attr,
3089 &dev_attr_autoresume.attr,
3090 &dev_attr_state.attr,
3091 &dev_attr_lpm.attr,
3092 &dev_attr_speed.attr,
3093 NULL,
3094};
3095static struct attribute_group msm_hsusb_attr_grp = {
3096 .attrs = msm_hsusb_attrs,
3097};
3098
3099#define msm_hsusb_func_attr(function, index) \
3100static ssize_t show_##function(struct device *dev, \
3101 struct device_attribute *attr, char *buf) \
3102{ \
3103 struct usb_info *ui = the_usb_info; \
3104 struct usb_function_info *fi = ui->func[index]; \
3105 \
3106 return sprintf(buf, "%d", fi->enabled); \
3107 \
3108} \
3109 \
3110static DEVICE_ATTR(function, S_IRUGO, show_##function, NULL);
3111
3112msm_hsusb_func_attr(diag, 0);
3113msm_hsusb_func_attr(adb, 1);
3114msm_hsusb_func_attr(modem, 2);
3115msm_hsusb_func_attr(nmea, 3);
3116msm_hsusb_func_attr(mass_storage, 4);
3117msm_hsusb_func_attr(ethernet, 5);
3118msm_hsusb_func_attr(rmnet, 6);
3119
3120static struct attribute *msm_hsusb_func_attrs[] = {
3121 &dev_attr_diag.attr,
3122 &dev_attr_adb.attr,
3123 &dev_attr_modem.attr,
3124 &dev_attr_nmea.attr,
3125 &dev_attr_mass_storage.attr,
3126 &dev_attr_ethernet.attr,
3127 &dev_attr_rmnet.attr,
3128 NULL,
3129};
3130
3131static struct attribute_group msm_hsusb_func_attr_grp = {
3132 .name = "functions",
3133 .attrs = msm_hsusb_func_attrs,
3134};
3135
3136static int __init usb_probe(struct platform_device *pdev)
3137{
3138 struct resource *res;
3139 struct usb_info *ui;
3140 int irq;
3141 int ulpi_irq1 = 0;
3142 int ulpi_irq2 = 0;
3143 int i;
3144 int ret = 0;
3145
3146 if (!pdev || !pdev->dev.platform_data) {
3147 pr_err("%s:pdev or platform data is null\n", __func__);
3148 return -ENODEV;
3149 }
3150
3151 irq = platform_get_irq(pdev, 0);
3152 if (irq < 0) {
3153 pr_err("%s: failed to get irq num from platform_get_irq\n",
3154 __func__);
3155 return -ENODEV;
3156 }
3157
3158
3159 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3160 if (!res) {
3161 pr_err("%s: failed to get mem resource\n", __func__);
3162 return -ENODEV;
3163 }
3164
3165 ret = sysfs_create_group(&pdev->dev.kobj, &msm_hsusb_attr_grp);
3166 if (ret) {
3167 pr_err("%s: unable to create sysfs group\n", __func__);
3168 return ret;
3169 }
3170
3171 usb_work = create_singlethread_workqueue("usb_work");
3172 if (!usb_work) {
3173 pr_err("%s: unable to create work queue\n", __func__);
3174 ret = -ENOMEM;
3175 goto free_sysfs_grp;
3176 }
3177
3178 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
3179 if (!ui) {
3180 pr_err("%s: unable to allocate memory for ui\n", __func__);
3181 ret = -ENOMEM;
3182 goto free_workqueue;
3183 }
3184
3185 ui->pdev = pdev;
3186 ui->pdata = pdev->dev.platform_data;
3187
3188 for (i = 0; i < ui->pdata->num_compositions; i++)
3189 if (ui->pdata->compositions[i].product_id == pid) {
3190 ui->composition = &ui->pdata->compositions[i];
3191 break;
3192 }
3193 if (!ui->composition) {
3194 pr_err("%s: unable to find the composition with pid:(%d)\n",
3195 __func__, pid);
3196 ret = -ENODEV;
3197 goto free_ui;
3198 }
3199
3200 ui->phy_info = ui->pdata->phy_info;
3201 if (ui->phy_info == USB_PHY_UNDEFINED) {
3202 pr_err("undefined phy_info: (%d)\n", ui->phy_info);
3203 ret = -ENOMEM;
3204 goto free_ui;
3205 }
3206
3207 /* zero is reserved for language id */
3208 ui->strdesc_index = 1;
3209 ui->strdesc = kzalloc(sizeof(char *) * MAX_STRDESC_NUM, GFP_KERNEL);
3210 if (!ui->strdesc) {
3211 pr_err("%s: unable allocate mem for string descriptors\n",
3212 __func__);
3213 ret = -ENOMEM;
3214 goto free_ui;
3215 }
3216
3217 ui->num_funcs = ui->pdata->num_functions;
3218 ui->func = kzalloc(sizeof(struct usb_function *) * ui->num_funcs,
3219 GFP_KERNEL);
3220 if (!ui->func) {
3221 pr_err("%s: unable allocate mem for functions\n", __func__);
3222 ret = -ENOMEM;
3223 goto free_str_desc;
3224 }
3225
3226 ret = sysfs_create_group(&pdev->dev.kobj, &msm_hsusb_func_attr_grp);
3227 if (ret) {
3228 pr_err("%s: unable to create functions sysfs group\n",
3229 __func__);
3230 goto free_func;
3231 }
3232
3233 ui->addr = ioremap(res->start, resource_size(res));
3234 if (!ui->addr) {
3235 pr_err("%s: unable ioremap\n", __func__);
3236 ret = -ENOMEM;
3237 goto free_func_sysfs_grp;
3238 }
3239
3240 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
3241 if (!ui->buf) {
3242 pr_err("%s: failed allocate dma coherent memory\n", __func__);
3243 ret = -ENOMEM;
3244 goto free_iounmap;
3245 }
3246
3247 ui->pool = dma_pool_create("hsusb", NULL, 32, 32, 0);
3248 if (!ui->pool) {
3249 pr_err("%s: unable to allocate dma pool\n", __func__);
3250 ret = -ENOMEM;
3251 goto free_dma_coherent;
3252 }
3253
3254 ui->clk = clk_get(&pdev->dev, "usb_hs_clk");
3255 if (IS_ERR(ui->clk)) {
3256 pr_err("%s: unable get usb_hs_clk\n", __func__);
3257 ret = PTR_ERR(ui->clk);
3258 goto free_dma_pool;
3259 }
3260
3261 ui->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
3262 if (IS_ERR(ui->pclk)) {
3263 pr_err("%s: unable get usb_hs_pclk\n", __func__);
3264 ret = PTR_ERR(ui->pclk);
3265 goto free_hs_clk;
3266 }
3267
3268 if (ui->pdata->core_clk) {
3269 ui->cclk = clk_get(&pdev->dev, "usb_hs_core_clk");
3270 if (IS_ERR(ui->cclk)) {
3271 pr_err("%s: unable get usb_hs_core_clk\n", __func__);
3272 ret = PTR_ERR(ui->cclk);
3273 goto free_hs_pclk;
3274 }
3275 }
3276
3277 if (ui->pdata->vreg5v_required) {
3278 ui->vreg = vreg_get(NULL, "boost");
3279 if (IS_ERR(ui->vreg)) {
3280 pr_err("%s: vreg get failed\n", __func__);
3281 ui->vreg = NULL;
3282 ret = PTR_ERR(ui->vreg);
3283 goto free_hs_cclk;
3284 }
3285 }
3286
3287 /* disable interrupts before requesting irq */
3288 usb_clk_enable(ui);
3289 writel(0, USB_USBINTR);
3290 writel(readl(USB_OTGSC) & ~OTGSC_INTR_MASK, USB_OTGSC);
3291 usb_clk_disable(ui);
3292
3293 ret = request_irq(irq, usb_interrupt, IRQF_SHARED, pdev->name, ui);
3294 if (ret) {
3295 pr_err("%s: request_irq failed\n", __func__);
3296 goto free_vreg5v;
3297 }
3298 ui->irq = irq;
3299
3300 if (ui->pdata->config_gpio) {
3301 usb_lpm_config_gpio = ui->pdata->config_gpio;
3302
3303 ulpi_irq1 = platform_get_irq_byname(pdev, "vbus_interrupt");
3304 if (ulpi_irq1 < 0) {
3305 pr_err("%s: failed to get vbus gpio interrupt\n",
3306 __func__);
3307 return -ENODEV;
3308 }
3309
3310 ulpi_irq2 = platform_get_irq_byname(pdev, "id_interrupt");
3311 if (ulpi_irq2 < 0) {
3312 pr_err("%s: failed to get id gpio interrupt\n",
3313 __func__);
3314 return -ENODEV;
3315 }
3316
3317 ret = request_irq(ulpi_irq1,
3318 &usb_lpm_gpio_isr,
3319 IRQF_TRIGGER_HIGH,
3320 "vbus_interrupt", NULL);
3321 if (ret) {
3322 pr_err("%s: failed to request vbus interrupt:(%d)\n",
3323 __func__, ulpi_irq1);
3324 goto free_irq;
3325 }
3326
3327 ret = request_irq(ulpi_irq2,
3328 &usb_lpm_gpio_isr,
3329 IRQF_TRIGGER_RISING,
3330 "usb_ulpi_data3", NULL);
3331 if (ret) {
3332 pr_err("%s: failed to request irq ulpi_data_3:(%d)\n",
3333 __func__, ulpi_irq2);
3334 goto free_ulpi_irq1;
3335 }
3336
3337 ui->gpio_irq[0] = ulpi_irq1;
3338 ui->gpio_irq[1] = ulpi_irq2;
3339 }
3340
3341 ui->sdev.name = DRIVER_NAME;
3342 ui->sdev.print_name = print_switch_name;
3343 ui->sdev.print_state = print_switch_state;
3344
3345 ret = switch_dev_register(&ui->sdev);
3346 if (ret < 0) {
3347 pr_err("%s(): switch_dev_register failed ret = %d\n",
3348 __func__, ret);
3349 goto free_ulpi_irq2;
3350 }
3351
3352 the_usb_info = ui;
3353 ui->functions_map = ui->pdata->function_map;
3354 ui->selfpowered = 0;
3355 ui->remote_wakeup = 0;
3356 ui->maxpower = 0xFA;
3357 ui->chg_type = USB_CHG_TYPE__INVALID;
3358 /* to allow swfi latency, driver latency
3359 * must be above listed swfi latency
3360 */
3361 ui->pdata->swfi_latency += 1;
3362
3363 spin_lock_init(&ui->lock);
3364 msm_hsusb_suspend_locks_init(ui, 1);
3365 enable_irq_wake(irq);
3366
3367 /* memory barrier initialization in non-interrupt context */
3368 dmb();
3369
3370 usb_debugfs_init(ui);
3371 usb_prepare(ui);
3372
3373 pr_info("%s: io=%p, irq=%d, dma=%p(%x)\n",
3374 __func__, ui->addr, ui->irq, ui->buf, ui->dma);
3375 return 0;
3376
3377free_ulpi_irq2:
3378 free_irq(ulpi_irq2, NULL);
3379free_ulpi_irq1:
3380 free_irq(ulpi_irq1, NULL);
3381free_irq:
3382 free_irq(ui->irq, ui);
3383free_vreg5v:
3384 if (ui->pdata->vreg5v_required)
3385 vreg_put(ui->vreg);
3386free_hs_cclk:
3387 clk_put(ui->cclk);
3388free_hs_pclk:
3389 clk_put(ui->pclk);
3390free_hs_clk:
3391 clk_put(ui->clk);
3392free_dma_pool:
3393 dma_pool_destroy(ui->pool);
3394free_dma_coherent:
3395 dma_free_coherent(&pdev->dev, 4096, ui->buf, ui->dma);
3396free_iounmap:
3397 iounmap(ui->addr);
3398free_func_sysfs_grp:
3399 sysfs_remove_group(&pdev->dev.kobj, &msm_hsusb_func_attr_grp);
3400free_func:
3401 kfree(ui->func);
3402free_str_desc:
3403 kfree(ui->strdesc);
3404free_ui:
3405 kfree(ui);
3406free_workqueue:
3407 destroy_workqueue(usb_work);
3408free_sysfs_grp:
3409 sysfs_remove_group(&pdev->dev.kobj, &msm_hsusb_attr_grp);
3410
3411 return ret;
3412}
3413
3414#ifdef CONFIG_PM
3415static int usb_platform_suspend(struct platform_device *pdev,
3416 pm_message_t state)
3417{
3418 struct usb_info *ui = the_usb_info;
3419 unsigned long flags;
3420 int ret = 0;
3421
3422 spin_lock_irqsave(&ui->lock, flags);
3423
3424 if (!ui->active) {
3425 spin_unlock_irqrestore(&ui->lock, flags);
3426 pr_info("%s: peripheral mode is not active"
3427 "nothing to be done\n", __func__);
3428 return 0;
3429 }
3430
3431 if (ui->in_lpm) {
3432 spin_unlock_irqrestore(&ui->lock, flags);
3433 pr_info("%s: we are already in lpm, nothing to be done\n",
3434 __func__);
3435 return 0;
3436 }
3437 spin_unlock_irqrestore(&ui->lock, flags);
3438
3439 ret = usb_lpm_enter(ui);
3440 if (ret)
3441 pr_err("%s: failed to enter lpm\n", __func__);
3442
3443 return ret;
3444}
3445#endif
3446
3447static struct platform_driver usb_driver = {
3448 .probe = usb_probe,
3449#ifdef CONFIG_PM
3450 .suspend = usb_platform_suspend,
3451#endif
3452 .driver = { .name = DRIVER_NAME, },
3453};
3454
3455static int __init usb_module_init(void)
3456{
3457 /* rpc connect for phy_reset */
3458 msm_hsusb_rpc_connect();
3459 /* rpc connect for charging */
3460 msm_chg_rpc_connect();
3461
3462 return platform_driver_register(&usb_driver);
3463}
3464
3465static void free_usb_info(void)
3466{
3467 struct usb_info *ui = the_usb_info;
3468 unsigned long flags;
3469 int i;
3470 if (ui) {
3471 INIT_LIST_HEAD(&usb_function_list);
3472
3473 for (i = 0; i < ui->num_funcs; i++)
3474 kfree(ui->func[i]);
3475 ui->num_funcs = 0;
3476 usb_uninit(ui);
3477 kfree(ui->strdesc);
3478 usb_ept_free_req(&ui->ep0in, ui->setup_req);
3479 if (ui->ept[0].ui == ui)
3480 flush_all_endpoints(ui);
3481 spin_lock_irqsave(&ui->lock, flags);
3482 usb_clk_disable(ui);
3483 usb_vreg_disable(ui);
3484 spin_unlock_irqrestore(&ui->lock, flags);
3485 usb_free(ui, 0);
3486 the_usb_info = NULL;
3487 }
3488}
3489static void usb_exit(void)
3490{
3491 struct usb_info *ui = the_usb_info;
3492 /* free the dev state structure */
3493 if (!ui)
3494 return;
3495
3496 if (ui->xceiv) {
3497 ui->xceiv->set_peripheral(ui->xceiv, NULL);
3498 msm_otg_put_transceiver(ui->xceiv);
3499 }
3500
3501 cancel_work_sync(&ui->li.wakeup_phy);
3502
3503 destroy_workqueue(usb_work);
3504 /* free the usb_info structure */
3505 free_usb_info();
3506 switch_dev_unregister(&ui->sdev);
3507 sysfs_remove_group(&ui->pdev->dev.kobj, &msm_hsusb_func_attr_grp);
3508 sysfs_remove_group(&ui->pdev->dev.kobj, &msm_hsusb_attr_grp);
3509 usb_debugfs_uninit();
3510 platform_driver_unregister(&usb_driver);
3511 msm_hsusb_rpc_close();
3512 msm_chg_rpc_close();
3513 msm_pm_app_unregister_vbus_sn(&msm_hsusb_set_vbus_state);
3514 msm_pm_app_rpc_deinit();
3515}
3516
3517static void __exit usb_module_exit(void)
3518{
3519 usb_exit();
3520}
3521
3522module_param(pid, int, 0);
3523MODULE_PARM_DESC(pid, "Product ID of the desired composition");
3524
3525module_init(usb_module_init);
3526module_exit(usb_module_exit);
3527
3528static void copy_string_descriptor(char *string, char *buffer)
3529{
3530 int length, i;
3531
3532 if (string) {
3533 length = strlen(string);
3534 buffer[0] = 2 * length + 2;
3535 buffer[1] = USB_DT_STRING;
3536 for (i = 0; i < length; i++) {
3537 buffer[2 * i + 2] = string[i];
3538 buffer[2 * i + 3] = 0;
3539 }
3540 }
3541}
3542static int get_qualifier_descriptor(struct usb_qualifier_descriptor *dq)
3543{
3544 struct usb_qualifier_descriptor *dev_qualifier = dq;
3545 dev_qualifier->bLength = sizeof(struct usb_qualifier_descriptor),
3546 dev_qualifier->bDescriptorType = USB_DT_DEVICE_QUALIFIER,
3547 dev_qualifier->bcdUSB = __constant_cpu_to_le16(0x0200),
3548 dev_qualifier->bDeviceClass = USB_CLASS_PER_INTERFACE,
3549 dev_qualifier->bDeviceSubClass = 0;
3550 dev_qualifier->bDeviceProtocol = 0;
3551 dev_qualifier->bMaxPacketSize0 = 64;
3552 dev_qualifier->bNumConfigurations = 1;
3553 dev_qualifier->bRESERVED = 0;
3554 return sizeof(struct usb_qualifier_descriptor);
3555}
3556
3557static int usb_fill_descriptors(void *ptr,
3558 struct usb_descriptor_header **descriptors)
3559{
3560 unsigned char *buf = ptr;
3561 struct usb_descriptor_header *item = descriptors[0];
3562 unsigned cnt = 0;
3563
3564 while (NULL != item) {
3565 unsigned len = item->bLength;
3566 memcpy(buf, item, len);
3567 buf += len;
3568 cnt++;
3569 item = descriptors[cnt];
3570 }
3571
3572 return buf-(u8 *)ptr;
3573}
3574
3575static int usb_find_descriptor(struct usb_info *ui, struct usb_ctrlrequest *ctl,
3576 struct usb_request *req)
3577{
3578 int i;
3579 unsigned short id = ctl->wValue;
3580 unsigned short type = id >> 8;
3581 id &= 0xff;
3582
3583 if ((type == USB_DT_DEVICE) && (id == 0)) {
3584 req->length = sizeof(desc_device);
3585 if (usb_msm_is_iad()) {
3586 desc_device.bDeviceClass = 0xEF;
3587 desc_device.bDeviceSubClass = 0x02;
3588 desc_device.bDeviceProtocol = 0x01;
3589 }
3590 memcpy(req->buf, &desc_device, req->length);
3591 return 0;
3592 }
3593 if ((type == USB_DT_DEVICE_QUALIFIER) && (id == 0)) {
3594 struct usb_qualifier_descriptor dq;
3595 req->length = get_qualifier_descriptor(&dq);
3596 if (usb_msm_is_iad()) {
3597 dq.bDeviceClass = 0xEF;
3598 dq.bDeviceSubClass = 0x02;
3599 dq.bDeviceProtocol = 0x01;
3600 }
3601 memcpy(req->buf, &dq, req->length);
3602 return 0;
3603 }
3604
3605 if ((type == USB_DT_OTHER_SPEED_CONFIG) && (id == 0))
3606 goto get_config;
3607
3608 if ((type == USB_DT_CONFIG) && (id == 0)) {
3609 struct usb_config_descriptor cfg;
3610 unsigned ifc_count = 0;
3611 char *ptr, *start;
3612get_config:
3613 ifc_count = 0;
3614 start = req->buf;
3615 ptr = start + USB_DT_CONFIG_SIZE;
3616 ifc_count = ui->next_ifc_num;
3617
3618 for (i = 0; i < ui->num_funcs; i++) {
3619 struct usb_function_info *fi = ui->func[i];
3620 struct usb_descriptor_header **dh = NULL;
3621
3622 if (!fi || !(ui->composition->functions & (1 << i)))
3623 continue;
3624 switch (ui->speed) {
3625 case USB_SPEED_HIGH:
3626 if (type == USB_DT_OTHER_SPEED_CONFIG)
3627 dh = fi->func->fs_descriptors;
3628 else
3629 dh = fi->func->hs_descriptors;
3630 break;
3631
3632 case USB_SPEED_FULL:
3633 if (type == USB_DT_OTHER_SPEED_CONFIG)
3634 dh = fi->func->hs_descriptors;
3635 else
3636 dh = fi->func->fs_descriptors;
3637 break;
3638
3639 default:
3640 printk(KERN_ERR "Unsupported speed(%x)\n",
3641 ui->speed);
3642 return -1;
3643 }
3644 ptr += usb_fill_descriptors(ptr, dh);
3645 }
3646
3647#define USB_REMOTE_WAKEUP_SUPPORT 1
3648 cfg.bLength = USB_DT_CONFIG_SIZE;
3649 if (type == USB_DT_OTHER_SPEED_CONFIG)
3650 cfg.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
3651 else
3652 cfg.bDescriptorType = USB_DT_CONFIG;
3653 cfg.wTotalLength = ptr - start;
3654 cfg.bNumInterfaces = ifc_count;
3655 cfg.bConfigurationValue = 1;
3656 cfg.iConfiguration = 0;
3657 cfg.bmAttributes = USB_CONFIG_ATT_ONE |
3658 ui->selfpowered << USB_CONFIG_ATT_SELFPOWER_POS |
3659 USB_REMOTE_WAKEUP_SUPPORT << USB_CONFIG_ATT_WAKEUP_POS;
3660 cfg.bMaxPower = ui->maxpower;
3661
3662 memcpy(start, &cfg, USB_DT_CONFIG_SIZE);
3663
3664 req->length = ptr - start;
3665 return 0;
3666 }
3667
3668 if (type == USB_DT_STRING) {
3669 char *buffer = req->buf;
3670
3671 buffer[0] = 0;
3672 if (id > ui->strdesc_index)
3673 return -1;
3674 if (id == STRING_LANGUAGE_ID)
3675 memcpy(buffer, str_lang_desc, str_lang_desc[0]);
3676 else
3677 copy_string_descriptor(ui->strdesc[id], buffer);
3678
3679 if (buffer[0]) {
3680 req->length = buffer[0];
3681 return 0;
3682 } else
3683 return -1;
3684 }
3685 return -1;
3686}
3687
3688/*****Gadget Framework Functions***/
3689struct device *usb_get_device(void)
3690{
3691 if (the_usb_info) {
3692 if (the_usb_info->pdev)
3693 return &(the_usb_info->pdev->dev);
3694 }
3695 return NULL;
3696}
3697EXPORT_SYMBOL(usb_get_device);
3698
3699int usb_ept_cancel_xfer(struct usb_endpoint *ept, struct usb_request *_req)
3700{
3701 struct usb_info *ui = the_usb_info;
3702 struct msm_request *req = to_msm_request(_req);
3703 struct msm_request *temp_req, *prev_req;
3704 unsigned long flags;
3705
3706 if (!(ui && req && ept->req))
3707 return -EINVAL;
3708
3709 spin_lock_irqsave(&ui->lock, flags);
3710 if (req->busy) {
3711 req->req.status = 0;
3712 req->busy = 0;
3713
3714 /* See if the request is the first request in the ept queue */
3715 if (ept->req == req) {
3716 /* Stop the transfer */
3717 do {
3718 writel((1 << ept->bit), USB_ENDPTFLUSH);
3719 while (readl(USB_ENDPTFLUSH) & (1 << ept->bit))
3720 udelay(100);
3721 } while (readl(USB_ENDPTSTAT) & (1 << ept->bit));
3722 if (!req->next)
3723 ept->last = NULL;
3724 ept->req = req->next;
3725 ept->head->next = req->item->next;
3726 goto cancel_req;
3727 }
3728 /* Request could be in the middle of ept queue */
3729 prev_req = temp_req = ept->req;
3730 do {
3731 if (req == temp_req) {
3732 if (req->live) {
3733 /* Stop the transfer */
3734 do {
3735 writel((1 << ept->bit),
3736 USB_ENDPTFLUSH);
3737 while (readl(USB_ENDPTFLUSH) &
3738 (1 << ept->bit))
3739 udelay(100);
3740 } while (readl(USB_ENDPTSTAT) &
3741 (1 << ept->bit));
3742 }
3743 prev_req->next = temp_req->next;
3744 prev_req->item->next = temp_req->item->next;
3745 if (!req->next)
3746 ept->last = prev_req;
3747 goto cancel_req;
3748 }
3749 prev_req = temp_req;
3750 temp_req = temp_req->next;
3751 } while (temp_req != NULL);
3752 goto error;
3753cancel_req:
3754 if (req->live) {
3755 /* prepare the transaction descriptor item for the hardware */
3756 req->item->next = TERMINATE;
3757 req->item->info = 0;
3758 req->live = 0;
3759 dma_unmap_single(NULL, req->dma, req->req.length,
3760 (ept->flags & EPT_FLAG_IN) ?
3761 DMA_TO_DEVICE : DMA_FROM_DEVICE);
3762 /* Reprime the endpoint for the remaining transfers */
3763 if (ept->req) {
3764 temp_req = ept->req;
3765 while (temp_req != NULL) {
3766 temp_req->live = 0;
3767 temp_req = temp_req->next;
3768 }
3769 usb_ept_start(ept);
3770 }
3771 } else
3772 dma_unmap_single(NULL, req->dma, req->req.length,
3773 (ept->flags & EPT_FLAG_IN) ?
3774 DMA_TO_DEVICE : DMA_FROM_DEVICE);
3775 spin_unlock_irqrestore(&ui->lock, flags);
3776 return 0;
3777 }
3778error:
3779 spin_unlock_irqrestore(&ui->lock, flags);
3780 return -EINVAL;
3781}
3782EXPORT_SYMBOL(usb_ept_cancel_xfer);
3783
3784int usb_ept_set_halt(struct usb_endpoint *ept)
3785{
3786 struct usb_info *ui = ept->ui;
3787 int in = ept->flags & EPT_FLAG_IN;
3788 unsigned n;
3789
3790 if (ui->in_lpm) {
3791 pr_err("%s: controller is in lpm, cannot proceed\n", __func__);
3792 return -1;
3793 }
3794
3795 ept->ept_halted = 1;
3796
3797 n = readl(USB_ENDPTCTRL(ept->num));
3798
3799 if (in)
3800 n |= CTRL_TXS;
3801 else
3802 n |= CTRL_RXS;
3803
3804 writel(n, USB_ENDPTCTRL(ept->num));
3805
3806 return 0;
3807}
3808EXPORT_SYMBOL(usb_ept_set_halt);
3809
3810int usb_ept_clear_halt(struct usb_endpoint *ept)
3811{
3812 struct usb_info *ui = ept->ui;
3813 int in = ept->flags & EPT_FLAG_IN;
3814 unsigned n;
3815
3816 if (ui->in_lpm) {
3817 pr_err("%s: controller is in lpm, cannot proceed\n", __func__);
3818 return -1;
3819 }
3820
3821 if (ept->ept_halted)
3822 ept->ept_halted = 0;
3823
3824 n = readl(USB_ENDPTCTRL(ept->num));
3825
3826 /*clear stall bit and set data toggle bit*/
3827 if (in) {
3828 n &= (~CTRL_TXS);
3829 n |= (CTRL_TXR);
3830 } else {
3831 n &= ~(CTRL_RXS);
3832 n |= (CTRL_RXR);
3833 }
3834
3835 writel(n, USB_ENDPTCTRL(ept->num));
3836
3837 return 0;
3838}
3839EXPORT_SYMBOL(usb_ept_clear_halt);
3840
3841int usb_ept_is_stalled(struct usb_endpoint *ept)
3842{
3843 struct usb_info *ui = ept->ui;
3844 int in = ept->flags & EPT_FLAG_IN;
3845 unsigned n;
3846
3847 n = readl(USB_ENDPTCTRL(ept->num));
3848
3849 if (in && (n & CTRL_TXS))
3850 return 1;
3851 else if (n & CTRL_RXS)
3852 return 1;
3853 return 0;
3854}
3855
3856void usb_ept_fifo_flush(struct usb_endpoint *ept)
3857{
3858 flush_endpoint(ept);
3859}
3860EXPORT_SYMBOL(usb_ept_fifo_flush);
3861
3862struct usb_function *usb_ept_get_function(struct usb_endpoint *ept)
3863{
3864 return NULL;
3865}
3866EXPORT_SYMBOL(usb_ept_get_function);
3867
3868
3869void usb_free_endpoint_all_req(struct usb_endpoint *ep)
3870{
3871 struct msm_request *temp;
3872 struct msm_request *req;
3873 if (!ep)
3874 return;
3875 req = ep->req;
3876 while (req) {
3877 temp = req->next;
3878 req->busy = 0;
3879 if (&req->req)
3880 usb_ept_free_req(ep, &req->req);
3881 req = temp;
3882 }
3883}
3884EXPORT_SYMBOL(usb_free_endpoint_all_req);
3885
3886int usb_function_unregister(struct usb_function *func)
3887{
3888 struct usb_info *ui = the_usb_info;
3889 int i;
3890 struct usb_function_info *fi;
3891 unsigned long flags;
3892
3893 if (!func)
3894 return -EINVAL;
3895
3896 fi = usb_find_function(func->name);
3897 if (!fi)
3898 return -EINVAL;
3899
3900 if (ui->running) {
3901 disable_irq(ui->irq);
3902 spin_lock_irqsave(&ui->lock, flags);
3903 ui->running = 0;
3904 ui->online = 0;
3905 ui->bound = 0;
3906 spin_unlock_irqrestore(&ui->lock, flags);
3907 usb_uninit(ui);
3908 /* we should come out of lpm to access registers */
3909 if (ui->in_lpm) {
3910 if (PHY_TYPE(ui->phy_info) == USB_PHY_EXTERNAL) {
3911 disable_irq(ui->gpio_irq[0]);
3912 disable_irq(ui->gpio_irq[1]);
3913 }
3914 usb_lpm_exit(ui);
3915 if (cancel_work_sync(&ui->li.wakeup_phy))
3916 usb_lpm_wakeup_phy(NULL);
3917 ui->in_lpm = 0;
3918 }
3919 /* disable usb and session valid interrupts */
3920 writel(0, USB_USBINTR);
3921 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
3922
3923 /* stop the controller */
3924 usb_disable_pullup(ui);
3925 msleep(100);
3926 enable_irq(ui->irq);
3927 }
3928
3929 pr_info("%s: func->name = %s\n", __func__, func->name);
3930
3931 ui->composition = NULL;
3932
3933 if (func->configure)
3934 func->configure(0, func->context);
3935 if (func->unbind)
3936 func->unbind(func->context);
3937
3938 list_del(&fi->list);
3939 for (i = 0; i < ui->num_funcs; i++)
3940 if (fi == ui->func[i])
3941 ui->func[i] = NULL;
3942 kfree(fi);
3943 return 0;
3944}
3945EXPORT_SYMBOL(usb_function_unregister);
3946
3947MODULE_LICENSE("GPL");
3948