blob: 24ba619d8e880bedab58ef8cee3ee11e5a1a6687 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Driver for HighSpeed USB Client Controller in MSM7K
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
6 * Author: Mike Lockwood <lockwood@android.com>
7 * 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
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24
25#include <linux/delay.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/dma-mapping.h>
29#include <linux/dmapool.h>
30#include <linux/platform_device.h>
31#include <linux/debugfs.h>
32#include <linux/workqueue.h>
33#include <linux/switch.h>
34#include <linux/pm_runtime.h>
35
36#include <mach/msm72k_otg.h>
37#include <linux/io.h>
38
39#include <asm/mach-types.h>
40
41#include <mach/board.h>
42#include <mach/msm_hsusb.h>
43#include <linux/device.h>
44#include <mach/msm_hsusb_hw.h>
45#include <mach/clk.h>
46#include <linux/uaccess.h>
47#include <linux/wakelock.h>
48
49static const char driver_name[] = "msm72k_udc";
50
51/* #define DEBUG */
52/* #define VERBOSE */
53
54#define MSM_USB_BASE ((unsigned) ui->addr)
55
56#define DRIVER_DESC "MSM 72K USB Peripheral Controller"
57#define DRIVER_NAME "MSM72K_UDC"
58
59#define EPT_FLAG_IN 0x0001
60
61#define SETUP_BUF_SIZE 8
62
63
64static const char *const ep_name[] = {
65 "ep0out", "ep1out", "ep2out", "ep3out",
66 "ep4out", "ep5out", "ep6out", "ep7out",
67 "ep8out", "ep9out", "ep10out", "ep11out",
68 "ep12out", "ep13out", "ep14out", "ep15out",
69 "ep0in", "ep1in", "ep2in", "ep3in",
70 "ep4in", "ep5in", "ep6in", "ep7in",
71 "ep8in", "ep9in", "ep10in", "ep11in",
72 "ep12in", "ep13in", "ep14in", "ep15in"
73};
74
75/*To release the wakelock from debugfs*/
76static int release_wlocks;
77
78struct msm_request {
79 struct usb_request req;
80
81 /* saved copy of req.complete */
82 void (*gadget_complete)(struct usb_ep *ep,
83 struct usb_request *req);
84
85
86 struct usb_info *ui;
87 struct msm_request *next;
88 struct msm_request *prev;
89
90 unsigned busy:1;
91 unsigned live:1;
92 unsigned alloced:1;
93
94 dma_addr_t dma;
95 dma_addr_t item_dma;
96
97 struct ept_queue_item *item;
98};
99
100#define to_msm_request(r) container_of(r, struct msm_request, req)
101#define to_msm_endpoint(r) container_of(r, struct msm_endpoint, ep)
102#define to_msm_otg(xceiv) container_of(xceiv, struct msm_otg, otg)
103#define is_b_sess_vld() ((OTGSC_BSV & readl(USB_OTGSC)) ? 1 : 0)
104#define is_usb_online(ui) (ui->usb_state != USB_STATE_NOTATTACHED)
105
106struct msm_endpoint {
107 struct usb_ep ep;
108 struct usb_info *ui;
109 struct msm_request *req; /* head of pending requests */
110 struct msm_request *last;
111 unsigned flags;
112
113 /* bit number (0-31) in various status registers
114 ** as well as the index into the usb_info's array
115 ** of all endpoints
116 */
117 unsigned char bit;
118 unsigned char num;
119
120 unsigned wedged:1;
121 /* pointers to DMA transfer list area */
122 /* these are allocated from the usb_info dma space */
123 struct ept_queue_head *head;
124};
125
126/* PHY status check timer to monitor phy stuck up on reset */
127static struct timer_list phy_status_timer;
128
129static void usb_do_work(struct work_struct *w);
130static void usb_do_remote_wakeup(struct work_struct *w);
131
132
133#define USB_STATE_IDLE 0
134#define USB_STATE_ONLINE 1
135#define USB_STATE_OFFLINE 2
136
137#define USB_FLAG_START 0x0001
138#define USB_FLAG_VBUS_ONLINE 0x0002
139#define USB_FLAG_VBUS_OFFLINE 0x0004
140#define USB_FLAG_RESET 0x0008
141#define USB_FLAG_SUSPEND 0x0010
142#define USB_FLAG_CONFIGURED 0x0020
143
144#define USB_CHG_DET_DELAY msecs_to_jiffies(1000)
145#define REMOTE_WAKEUP_DELAY msecs_to_jiffies(1000)
146#define PHY_STATUS_CHECK_DELAY (jiffies + msecs_to_jiffies(1000))
147
148struct usb_info {
149 /* lock for register/queue/device state changes */
150 spinlock_t lock;
151
152 /* single request used for handling setup transactions */
153 struct usb_request *setup_req;
154
155 struct platform_device *pdev;
156 int irq;
157 void *addr;
158
159 unsigned state;
160 unsigned flags;
161
162 atomic_t configured;
163 atomic_t running;
164
165 struct dma_pool *pool;
166
167 /* dma page to back the queue heads and items */
168 unsigned char *buf;
169 dma_addr_t dma;
170
171 struct ept_queue_head *head;
172
173 /* used for allocation */
174 unsigned next_item;
175 unsigned next_ifc_num;
176
177 /* endpoints are ordered based on their status bits,
178 ** so they are OUT0, OUT1, ... OUT15, IN0, IN1, ... IN15
179 */
180 struct msm_endpoint ept[32];
181
182
183 /* max power requested by selected configuration */
184 unsigned b_max_pow;
185 unsigned chg_current;
186 struct delayed_work chg_det;
187 struct delayed_work chg_stop;
188 struct msm_hsusb_gadget_platform_data *pdata;
189 struct work_struct phy_status_check;
190
191 struct work_struct work;
192 unsigned phy_status;
193 unsigned phy_fail_count;
194
195 struct usb_gadget gadget;
196 struct usb_gadget_driver *driver;
197 struct switch_dev sdev;
198
199#define ep0out ept[0]
200#define ep0in ept[16]
201
202 atomic_t ep0_dir;
203 atomic_t test_mode;
204 atomic_t offline_pending;
205 atomic_t softconnect;
206#ifdef CONFIG_USB_OTG
207 u8 hnp_avail;
208#endif
209
210 atomic_t remote_wakeup;
211 atomic_t self_powered;
212 struct delayed_work rw_work;
213
214 struct otg_transceiver *xceiv;
215 enum usb_device_state usb_state;
216 struct wake_lock wlock;
217};
218
219static const struct usb_ep_ops msm72k_ep_ops;
220static struct usb_info *the_usb_info;
221
222static int msm72k_wakeup(struct usb_gadget *_gadget);
223static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active);
224static int msm72k_set_halt(struct usb_ep *_ep, int value);
225static void flush_endpoint(struct msm_endpoint *ept);
226static void usb_reset(struct usb_info *ui);
227static int usb_ept_set_halt(struct usb_ep *_ep, int value);
228
229static void msm_hsusb_set_speed(struct usb_info *ui)
230{
231 unsigned long flags;
232
233 spin_lock_irqsave(&ui->lock, flags);
234 switch (readl(USB_PORTSC) & PORTSC_PSPD_MASK) {
235 case PORTSC_PSPD_FS:
236 dev_dbg(&ui->pdev->dev, "portchange USB_SPEED_FULL\n");
237 ui->gadget.speed = USB_SPEED_FULL;
238 break;
239 case PORTSC_PSPD_LS:
240 dev_dbg(&ui->pdev->dev, "portchange USB_SPEED_LOW\n");
241 ui->gadget.speed = USB_SPEED_LOW;
242 break;
243 case PORTSC_PSPD_HS:
244 dev_dbg(&ui->pdev->dev, "portchange USB_SPEED_HIGH\n");
245 ui->gadget.speed = USB_SPEED_HIGH;
246 break;
247 }
248 spin_unlock_irqrestore(&ui->lock, flags);
249}
250
251static void msm_hsusb_set_state(enum usb_device_state state)
252{
253 unsigned long flags;
254
255 spin_lock_irqsave(&the_usb_info->lock, flags);
256 the_usb_info->usb_state = state;
257 spin_unlock_irqrestore(&the_usb_info->lock, flags);
258}
259
260static enum usb_device_state msm_hsusb_get_state(void)
261{
262 unsigned long flags;
263 enum usb_device_state state;
264
265 spin_lock_irqsave(&the_usb_info->lock, flags);
266 state = the_usb_info->usb_state;
267 spin_unlock_irqrestore(&the_usb_info->lock, flags);
268
269 return state;
270}
271
272static ssize_t print_switch_name(struct switch_dev *sdev, char *buf)
273{
274 return sprintf(buf, "%s\n", DRIVER_NAME);
275}
276
277static ssize_t print_switch_state(struct switch_dev *sdev, char *buf)
278{
279 return sprintf(buf, "%s\n", sdev->state ? "online" : "offline");
280}
281
282static inline enum chg_type usb_get_chg_type(struct usb_info *ui)
283{
284 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS)
285 return USB_CHG_TYPE__WALLCHARGER;
286 else
287 return USB_CHG_TYPE__SDP;
288}
289
290#define USB_WALLCHARGER_CHG_CURRENT 1800
291static int usb_get_max_power(struct usb_info *ui)
292{
293 struct msm_otg *otg = to_msm_otg(ui->xceiv);
294 unsigned long flags;
295 enum chg_type temp;
296 int suspended;
297 int configured;
298 unsigned bmaxpow;
299
300 if (ui->gadget.is_a_peripheral)
301 return -EINVAL;
302
303 temp = atomic_read(&otg->chg_type);
304 spin_lock_irqsave(&ui->lock, flags);
305 suspended = ui->usb_state == USB_STATE_SUSPENDED ? 1 : 0;
306 configured = atomic_read(&ui->configured);
307 bmaxpow = ui->b_max_pow;
308 spin_unlock_irqrestore(&ui->lock, flags);
309
310 if (temp == USB_CHG_TYPE__INVALID)
311 return -ENODEV;
312
313 if (temp == USB_CHG_TYPE__WALLCHARGER)
314 return USB_WALLCHARGER_CHG_CURRENT;
315
316 if (suspended || !configured)
317 return 0;
318
319 return bmaxpow;
320}
321
322static int usb_phy_stuck_check(struct usb_info *ui)
323{
324 /*
325 * write some value (0xAA) into scratch reg (0x16) and read it back,
326 * If the read value is same as written value, means PHY is normal
327 * otherwise, PHY seems to have stuck.
328 */
329
330 if (otg_io_write(ui->xceiv, 0xAA, 0x16) == -1) {
331 dev_dbg(&ui->pdev->dev,
332 "%s(): ulpi write timeout\n", __func__);
333 return -EIO;
334 }
335
336 if (otg_io_read(ui->xceiv, 0x16) != 0xAA) {
337 dev_dbg(&ui->pdev->dev,
338 "%s(): read value is incorrect\n", __func__);
339 return -EIO;
340 }
341
342 return 0;
343}
344
345/*
346 * This function checks the phy status by reading/writing to the
347 * phy scratch register. If the phy is stuck resets the HW
348 * */
349static void usb_phy_stuck_recover(struct work_struct *w)
350{
351 struct usb_info *ui = the_usb_info;
352 struct msm_otg *otg = to_msm_otg(ui->xceiv);
353 unsigned long flags;
354
355 spin_lock_irqsave(&ui->lock, flags);
356 if (ui->gadget.speed != USB_SPEED_UNKNOWN ||
357 ui->usb_state == USB_STATE_NOTATTACHED ||
358 ui->driver == NULL) {
359 spin_unlock_irqrestore(&ui->lock, flags);
360 return;
361 }
362 spin_unlock_irqrestore(&ui->lock, flags);
363
364 disable_irq(otg->irq);
365 if (usb_phy_stuck_check(ui)) {
366#ifdef CONFIG_USB_MSM_ACA
367 del_timer_sync(&otg->id_timer);
368#endif
369 ui->phy_fail_count++;
370 dev_err(&ui->pdev->dev,
371 "%s():PHY stuck, resetting HW\n", __func__);
372 /*
373 * PHY seems to have stuck,
374 * reset the PHY and HW link to recover the PHY
375 */
376 usb_reset(ui);
377#ifdef CONFIG_USB_MSM_ACA
378 mod_timer(&otg->id_timer, jiffies +
379 msecs_to_jiffies(OTG_ID_POLL_MS));
380#endif
381 msm72k_pullup_internal(&ui->gadget, 1);
382 }
383 enable_irq(otg->irq);
384}
385
386static void usb_phy_status_check_timer(unsigned long data)
387{
388 struct usb_info *ui = the_usb_info;
389
390 schedule_work(&ui->phy_status_check);
391}
392
393static void usb_chg_stop(struct work_struct *w)
394{
395 struct usb_info *ui = container_of(w, struct usb_info, chg_stop.work);
396 struct msm_otg *otg = to_msm_otg(ui->xceiv);
397 enum chg_type temp;
398
399 temp = atomic_read(&otg->chg_type);
400
401 if (temp == USB_CHG_TYPE__SDP)
402 otg_set_power(ui->xceiv, 0);
403}
404
405static void usb_chg_detect(struct work_struct *w)
406{
407 struct usb_info *ui = container_of(w, struct usb_info, chg_det.work);
408 struct msm_otg *otg = to_msm_otg(ui->xceiv);
409 enum chg_type temp = USB_CHG_TYPE__INVALID;
410 unsigned long flags;
411 int maxpower;
412
413 spin_lock_irqsave(&ui->lock, flags);
414 if (ui->usb_state == USB_STATE_NOTATTACHED) {
415 spin_unlock_irqrestore(&ui->lock, flags);
416 return;
417 }
418
419 temp = usb_get_chg_type(ui);
420 spin_unlock_irqrestore(&ui->lock, flags);
421
422 atomic_set(&otg->chg_type, temp);
423 maxpower = usb_get_max_power(ui);
424 if (maxpower > 0)
425 otg_set_power(ui->xceiv, maxpower);
426
427 /* USB driver prevents idle and suspend power collapse(pc)
428 * while USB cable is connected. But when dedicated charger is
429 * connected, driver can vote for idle and suspend pc.
430 * OTG driver handles idle pc as part of above otg_set_power call
431 * when wallcharger is attached. To allow suspend pc, release the
432 * wakelock which will be re-acquired for any sub-sequent usb interrupts
433 * */
434 if (temp == USB_CHG_TYPE__WALLCHARGER) {
435 pm_runtime_put_sync(&ui->pdev->dev);
436 wake_unlock(&ui->wlock);
437 }
438}
439
440static int usb_ep_get_stall(struct msm_endpoint *ept)
441{
442 unsigned int n;
443 struct usb_info *ui = ept->ui;
444
445 n = readl(USB_ENDPTCTRL(ept->num));
446 if (ept->flags & EPT_FLAG_IN)
447 return (CTRL_TXS & n) ? 1 : 0;
448 else
449 return (CTRL_RXS & n) ? 1 : 0;
450}
451
452static void init_endpoints(struct usb_info *ui)
453{
454 unsigned n;
455
456 for (n = 0; n < 32; n++) {
457 struct msm_endpoint *ept = ui->ept + n;
458
459 ept->ui = ui;
460 ept->bit = n;
461 ept->num = n & 15;
462 ept->ep.name = ep_name[n];
463 ept->ep.ops = &msm72k_ep_ops;
464
465 if (ept->bit > 15) {
466 /* IN endpoint */
467 ept->head = ui->head + (ept->num << 1) + 1;
468 ept->flags = EPT_FLAG_IN;
469 } else {
470 /* OUT endpoint */
471 ept->head = ui->head + (ept->num << 1);
472 ept->flags = 0;
473 }
474
475 }
476}
477
478static void config_ept(struct msm_endpoint *ept)
479{
480 struct usb_info *ui = ept->ui;
481 unsigned cfg = CONFIG_MAX_PKT(ept->ep.maxpacket) | CONFIG_ZLT;
482
483 /* ep0 out needs interrupt-on-setup */
484 if (ept->bit == 0)
485 cfg |= CONFIG_IOS;
486
487 ept->head->config = cfg;
488 ept->head->next = TERMINATE;
489
490 if (ept->ep.maxpacket)
491 dev_dbg(&ui->pdev->dev,
492 "ept #%d %s max:%d head:%p bit:%d\n",
493 ept->num,
494 (ept->flags & EPT_FLAG_IN) ? "in" : "out",
495 ept->ep.maxpacket, ept->head, ept->bit);
496}
497
498static void configure_endpoints(struct usb_info *ui)
499{
500 unsigned n;
501
502 for (n = 0; n < 32; n++)
503 config_ept(ui->ept + n);
504}
505
506struct usb_request *usb_ept_alloc_req(struct msm_endpoint *ept,
507 unsigned bufsize, gfp_t gfp_flags)
508{
509 struct usb_info *ui = ept->ui;
510 struct msm_request *req;
511
512 req = kzalloc(sizeof(*req), gfp_flags);
513 if (!req)
514 goto fail1;
515
516 req->item = dma_pool_alloc(ui->pool, gfp_flags, &req->item_dma);
517 if (!req->item)
518 goto fail2;
519
520 if (bufsize) {
521 req->req.buf = kmalloc(bufsize, gfp_flags);
522 if (!req->req.buf)
523 goto fail3;
524 req->alloced = 1;
525 }
526
527 return &req->req;
528
529fail3:
530 dma_pool_free(ui->pool, req->item, req->item_dma);
531fail2:
532 kfree(req);
533fail1:
534 return 0;
535}
536
537static void usb_ept_enable(struct msm_endpoint *ept, int yes,
538 unsigned char ep_type)
539{
540 struct usb_info *ui = ept->ui;
541 int in = ept->flags & EPT_FLAG_IN;
542 unsigned n;
543
544 n = readl(USB_ENDPTCTRL(ept->num));
545
546 if (in) {
547 if (yes) {
548 n = (n & (~CTRL_TXT_MASK)) |
549 (ep_type << CTRL_TXT_EP_TYPE_SHIFT);
550 n |= CTRL_TXE | CTRL_TXR;
551 } else
552 n &= (~CTRL_TXE);
553 } else {
554 if (yes) {
555 n = (n & (~CTRL_RXT_MASK)) |
556 (ep_type << CTRL_RXT_EP_TYPE_SHIFT);
557 n |= CTRL_RXE | CTRL_RXR;
558 } else
559 n &= ~(CTRL_RXE);
560 }
561 /* complete all the updates to ept->head before enabling endpoint*/
562 mb();
563 writel(n, USB_ENDPTCTRL(ept->num));
564
565 /* Ensure endpoint is enabled before returning */
566 mb();
567
568 dev_dbg(&ui->pdev->dev, "ept %d %s %s\n",
569 ept->num, in ? "in" : "out", yes ? "enabled" : "disabled");
570}
571
572static void usb_ept_start(struct msm_endpoint *ept)
573{
574 struct usb_info *ui = ept->ui;
575 struct msm_request *req = ept->req;
576 struct msm_request *f_req = ept->req;
577 unsigned n = 1 << ept->bit;
578 unsigned info;
579 int reprime_cnt = 0;
580
581 BUG_ON(req->live);
582
583 while (req) {
584 req->live = 1;
585 /* prepare the transaction descriptor item for the hardware */
586 req->item->info =
587 INFO_BYTES(req->req.length) | INFO_IOC | INFO_ACTIVE;
588 req->item->page0 = req->dma;
589 req->item->page1 = (req->dma + 0x1000) & 0xfffff000;
590 req->item->page2 = (req->dma + 0x2000) & 0xfffff000;
591 req->item->page3 = (req->dma + 0x3000) & 0xfffff000;
592
593 if (req->next == NULL) {
594 req->item->next = TERMINATE;
595 break;
596 }
597 req->item->next = req->next->item_dma;
598 req = req->next;
599 }
600
601 rmb();
602 /* link the hw queue head to the request's transaction item */
603 ept->head->next = ept->req->item_dma;
604 ept->head->info = 0;
605
606reprime_ept:
607 /* flush buffers before priming ept */
608 mb();
609 /* during high throughput testing it is observed that
610 * ept stat bit is not set even thoguh all the data
611 * structures are updated properly and ept prime bit
612 * is set. To workaround the issue, use dTD INFO bit
613 * to make decision on re-prime or not.
614 */
615 writel_relaxed(n, USB_ENDPTPRIME);
616 /* busy wait till endptprime gets clear */
617 while ((readl_relaxed(USB_ENDPTPRIME) & n))
618 ;
619 if (readl_relaxed(USB_ENDPTSTAT) & n)
620 return;
621
622 rmb();
623 info = f_req->item->info;
624 if (info & INFO_ACTIVE) {
625 if (reprime_cnt++ < 3)
626 goto reprime_ept;
627 else
628 pr_err("%s(): ept%d%s prime failed. ept: config: %x"
629 "active: %x next: %x info: %x\n"
630 " req@ %x next: %x info: %x\n",
631 __func__, ept->num,
632 ept->flags & EPT_FLAG_IN ? "in" : "out",
633 ept->head->config, ept->head->active,
634 ept->head->next, ept->head->info,
635 f_req->item_dma, f_req->item->next, info);
636 }
637}
638
639int usb_ept_queue_xfer(struct msm_endpoint *ept, struct usb_request *_req)
640{
641 unsigned long flags;
642 struct msm_request *req = to_msm_request(_req);
643 struct msm_request *last;
644 struct usb_info *ui = ept->ui;
645 unsigned length = req->req.length;
646
647 if (length > 0x4000)
648 return -EMSGSIZE;
649
650 spin_lock_irqsave(&ui->lock, flags);
651
652 if (req->busy) {
653 req->req.status = -EBUSY;
654 spin_unlock_irqrestore(&ui->lock, flags);
655 dev_err(&ui->pdev->dev,
656 "usb_ept_queue_xfer() tried to queue busy request\n");
657 return -EBUSY;
658 }
659
660 if (!atomic_read(&ui->configured) && (ept->num != 0)) {
661 req->req.status = -ESHUTDOWN;
662 spin_unlock_irqrestore(&ui->lock, flags);
663 if (printk_ratelimit())
664 dev_err(&ui->pdev->dev,
665 "%s: called while offline\n", __func__);
666 return -ESHUTDOWN;
667 }
668
669 if (ui->usb_state == USB_STATE_SUSPENDED) {
670 if (!atomic_read(&ui->remote_wakeup)) {
671 req->req.status = -EAGAIN;
672 spin_unlock_irqrestore(&ui->lock, flags);
673 if (printk_ratelimit())
674 dev_err(&ui->pdev->dev,
675 "%s: cannot queue as bus is suspended "
676 "ept #%d %s max:%d head:%p bit:%d\n",
677 __func__, ept->num,
678 (ept->flags & EPT_FLAG_IN) ? "in" : "out",
679 ept->ep.maxpacket, ept->head, ept->bit);
680
681 return -EAGAIN;
682 }
683
684 wake_lock(&ui->wlock);
685 otg_set_suspend(ui->xceiv, 0);
686 schedule_delayed_work(&ui->rw_work, REMOTE_WAKEUP_DELAY);
687 }
688
689 req->busy = 1;
690 req->live = 0;
691 req->next = 0;
692 req->req.status = -EBUSY;
693
694 req->dma = dma_map_single(NULL, req->req.buf, length,
695 (ept->flags & EPT_FLAG_IN) ?
696 DMA_TO_DEVICE : DMA_FROM_DEVICE);
697
698
699 /* Add the new request to the end of the queue */
700 last = ept->last;
701 if (last) {
702 /* Already requests in the queue. add us to the
703 * end, but let the completion interrupt actually
704 * start things going, to avoid hw issues
705 */
706 last->next = req;
707 req->prev = last;
708
709 } else {
710 /* queue was empty -- kick the hardware */
711 ept->req = req;
712 req->prev = NULL;
713 usb_ept_start(ept);
714 }
715 ept->last = req;
716
717 spin_unlock_irqrestore(&ui->lock, flags);
718 return 0;
719}
720
721/* --- endpoint 0 handling --- */
722
723static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
724{
725 struct msm_request *r = to_msm_request(req);
726 struct msm_endpoint *ept = to_msm_endpoint(ep);
727 struct usb_info *ui = ept->ui;
728
729 req->complete = r->gadget_complete;
730 r->gadget_complete = 0;
731 if (req->complete)
732 req->complete(&ui->ep0in.ep, req);
733}
734
735static void ep0_status_complete(struct usb_ep *ep, struct usb_request *_req)
736{
737 struct usb_request *req = _req->context;
738 struct msm_request *r;
739 struct msm_endpoint *ept;
740 struct usb_info *ui;
741
742 pr_debug("%s:\n", __func__);
743 if (!req)
744 return;
745
746 r = to_msm_request(req);
747 ept = to_msm_endpoint(ep);
748 ui = ept->ui;
749 _req->context = 0;
750
751 req->complete = r->gadget_complete;
752 req->zero = 0;
753 r->gadget_complete = 0;
754 if (req->complete)
755 req->complete(&ui->ep0in.ep, req);
756
757}
758
759static void ep0_status_phase(struct usb_ep *ep, struct usb_request *req)
760{
761 struct msm_endpoint *ept = to_msm_endpoint(ep);
762 struct usb_info *ui = ept->ui;
763
764 pr_debug("%s:\n", __func__);
765
766 req->length = 0;
767 req->complete = ep0_status_complete;
768
769 /* status phase */
770 if (atomic_read(&ui->ep0_dir) == USB_DIR_IN)
771 usb_ept_queue_xfer(&ui->ep0out, req);
772 else
773 usb_ept_queue_xfer(&ui->ep0in, req);
774}
775
776static void ep0in_send_zero_leng_pkt(struct msm_endpoint *ept)
777{
778 struct usb_info *ui = ept->ui;
779 struct usb_request *req = ui->setup_req;
780
781 pr_debug("%s:\n", __func__);
782
783 req->length = 0;
784 req->complete = ep0_status_phase;
785 usb_ept_queue_xfer(&ui->ep0in, req);
786}
787
788static void ep0_queue_ack_complete(struct usb_ep *ep,
789 struct usb_request *_req)
790{
791 struct msm_endpoint *ept = to_msm_endpoint(ep);
792 struct usb_info *ui = ept->ui;
793 struct usb_request *req = ui->setup_req;
794
795 pr_debug("%s: _req:%p actual:%d length:%d zero:%d\n",
796 __func__, _req, _req->actual,
797 _req->length, _req->zero);
798
799 /* queue up the receive of the ACK response from the host */
800 if (_req->status == 0 && _req->actual == _req->length) {
801 req->context = _req;
802 if (atomic_read(&ui->ep0_dir) == USB_DIR_IN) {
803 if (_req->zero && _req->length &&
804 !(_req->length % ep->maxpacket)) {
805 ep0in_send_zero_leng_pkt(&ui->ep0in);
806 return;
807 }
808 }
809 ep0_status_phase(ep, req);
810 } else
811 ep0_complete(ep, _req);
812}
813
814static void ep0_setup_ack_complete(struct usb_ep *ep, struct usb_request *req)
815{
816 struct msm_endpoint *ept = to_msm_endpoint(ep);
817 struct usb_info *ui = ept->ui;
818 unsigned int temp;
819 int test_mode = atomic_read(&ui->test_mode);
820
821 if (!test_mode)
822 return;
823
824 switch (test_mode) {
825 case J_TEST:
826 dev_info(&ui->pdev->dev, "usb electrical test mode: (J)\n");
827 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
828 writel(temp | PORTSC_PTC_J_STATE, USB_PORTSC);
829 break;
830
831 case K_TEST:
832 dev_info(&ui->pdev->dev, "usb electrical test mode: (K)\n");
833 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
834 writel(temp | PORTSC_PTC_K_STATE, USB_PORTSC);
835 break;
836
837 case SE0_NAK_TEST:
838 dev_info(&ui->pdev->dev,
839 "usb electrical test mode: (SE0-NAK)\n");
840 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
841 writel(temp | PORTSC_PTC_SE0_NAK, USB_PORTSC);
842 break;
843
844 case TST_PKT_TEST:
845 dev_info(&ui->pdev->dev,
846 "usb electrical test mode: (TEST_PKT)\n");
847 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
848 writel(temp | PORTSC_PTC_TST_PKT, USB_PORTSC);
849 break;
850 }
851}
852
853static void ep0_setup_ack(struct usb_info *ui)
854{
855 struct usb_request *req = ui->setup_req;
856 req->length = 0;
857 req->complete = ep0_setup_ack_complete;
858 usb_ept_queue_xfer(&ui->ep0in, req);
859}
860
861static void ep0_setup_stall(struct usb_info *ui)
862{
863 writel((1<<16) | (1<<0), USB_ENDPTCTRL(0));
864}
865
866static void ep0_setup_send(struct usb_info *ui, unsigned length)
867{
868 struct usb_request *req = ui->setup_req;
869 struct msm_request *r = to_msm_request(req);
870 struct msm_endpoint *ept = &ui->ep0in;
871
872 req->length = length;
873 req->complete = ep0_queue_ack_complete;
874 r->gadget_complete = 0;
875 usb_ept_queue_xfer(ept, req);
876}
877
878static void handle_setup(struct usb_info *ui)
879{
880 struct usb_ctrlrequest ctl;
881 struct usb_request *req = ui->setup_req;
882 int ret;
883#ifdef CONFIG_USB_OTG
884 u8 hnp;
885 unsigned long flags;
886#endif
887
888 memcpy(&ctl, ui->ep0out.head->setup_data, sizeof(ctl));
889 /* Ensure buffer is read before acknowledging to h/w */
890 mb();
891
892 writel(EPT_RX(0), USB_ENDPTSETUPSTAT);
893
894 if (ctl.bRequestType & USB_DIR_IN)
895 atomic_set(&ui->ep0_dir, USB_DIR_IN);
896 else
897 atomic_set(&ui->ep0_dir, USB_DIR_OUT);
898
899 /* any pending ep0 transactions must be canceled */
900 flush_endpoint(&ui->ep0out);
901 flush_endpoint(&ui->ep0in);
902
903 dev_dbg(&ui->pdev->dev,
904 "setup: type=%02x req=%02x val=%04x idx=%04x len=%04x\n",
905 ctl.bRequestType, ctl.bRequest, ctl.wValue,
906 ctl.wIndex, ctl.wLength);
907
908 if ((ctl.bRequestType & (USB_DIR_IN | USB_TYPE_MASK)) ==
909 (USB_DIR_IN | USB_TYPE_STANDARD)) {
910 if (ctl.bRequest == USB_REQ_GET_STATUS) {
911 /* OTG supplement Rev 2.0 introduces another device
912 * GET_STATUS request for HNP polling with length = 1.
913 */
914 u8 len = 2;
915 switch (ctl.bRequestType & USB_RECIP_MASK) {
916 case USB_RECIP_ENDPOINT:
917 {
918 struct msm_endpoint *ept;
919 unsigned num =
920 ctl.wIndex & USB_ENDPOINT_NUMBER_MASK;
921 u16 temp = 0;
922
923 if (num == 0) {
924 memset(req->buf, 0, 2);
925 break;
926 }
927 if (ctl.wIndex & USB_ENDPOINT_DIR_MASK)
928 num += 16;
929 ept = &ui->ep0out + num;
930 temp = usb_ep_get_stall(ept);
931 temp = temp << USB_ENDPOINT_HALT;
932 memcpy(req->buf, &temp, 2);
933 break;
934 }
935 case USB_RECIP_DEVICE:
936 {
937 u16 temp = 0;
938
939 if (ctl.wIndex == OTG_STATUS_SELECTOR) {
940#ifdef CONFIG_USB_OTG
941 spin_lock_irqsave(&ui->lock, flags);
942 hnp = (ui->gadget.host_request <<
943 HOST_REQUEST_FLAG);
944 ui->hnp_avail = 1;
945 spin_unlock_irqrestore(&ui->lock,
946 flags);
947 memcpy(req->buf, &hnp, 1);
948 len = 1;
949#else
950 goto stall;
951#endif
952 } else {
953 temp = (atomic_read(&ui->self_powered)
954 << USB_DEVICE_SELF_POWERED);
955 temp |= (atomic_read(&ui->remote_wakeup)
956 << USB_DEVICE_REMOTE_WAKEUP);
957 memcpy(req->buf, &temp, 2);
958 }
959 break;
960 }
961 case USB_RECIP_INTERFACE:
962 memset(req->buf, 0, 2);
963 break;
964 default:
965 goto stall;
966 }
967 ep0_setup_send(ui, len);
968 return;
969 }
970 }
971 if (ctl.bRequestType ==
972 (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)) {
973 if ((ctl.bRequest == USB_REQ_CLEAR_FEATURE) ||
974 (ctl.bRequest == USB_REQ_SET_FEATURE)) {
975 if ((ctl.wValue == 0) && (ctl.wLength == 0)) {
976 unsigned num = ctl.wIndex & 0x0f;
977
978 if (num != 0) {
979 struct msm_endpoint *ept;
980
981 if (ctl.wIndex & 0x80)
982 num += 16;
983 ept = &ui->ep0out + num;
984
985 if (ept->wedged)
986 goto ack;
987 if (ctl.bRequest == USB_REQ_SET_FEATURE)
988 usb_ept_set_halt(&ept->ep, 1);
989 else
990 usb_ept_set_halt(&ept->ep, 0);
991 }
992 goto ack;
993 }
994 }
995 }
996 if (ctl.bRequestType == (USB_DIR_OUT | USB_TYPE_STANDARD)) {
997 if (ctl.bRequest == USB_REQ_SET_CONFIGURATION) {
998 atomic_set(&ui->configured, !!ctl.wValue);
999 msm_hsusb_set_state(USB_STATE_CONFIGURED);
1000 } else if (ctl.bRequest == USB_REQ_SET_ADDRESS) {
1001 /*
1002 * Gadget speed should be set when PCI interrupt
1003 * occurs. But sometimes, PCI interrupt is not
1004 * occuring after reset. Hence update the gadget
1005 * speed here.
1006 */
1007 if (ui->gadget.speed == USB_SPEED_UNKNOWN) {
1008 dev_info(&ui->pdev->dev,
1009 "PCI intr missed"
1010 "set speed explictly\n");
1011 msm_hsusb_set_speed(ui);
1012 }
1013 msm_hsusb_set_state(USB_STATE_ADDRESS);
1014
1015 /* write address delayed (will take effect
1016 ** after the next IN txn)
1017 */
1018 writel((ctl.wValue << 25) | (1 << 24), USB_DEVICEADDR);
1019 goto ack;
1020 } else if (ctl.bRequest == USB_REQ_SET_FEATURE) {
1021 switch (ctl.wValue) {
1022 case USB_DEVICE_TEST_MODE:
1023 switch (ctl.wIndex) {
1024 case J_TEST:
1025 case K_TEST:
1026 case SE0_NAK_TEST:
1027 case TST_PKT_TEST:
1028 atomic_set(&ui->test_mode, ctl.wIndex);
1029 goto ack;
1030 }
1031 goto stall;
1032 case USB_DEVICE_REMOTE_WAKEUP:
1033 atomic_set(&ui->remote_wakeup, 1);
1034 goto ack;
1035#ifdef CONFIG_USB_OTG
1036 case USB_DEVICE_B_HNP_ENABLE:
1037 ui->gadget.b_hnp_enable = 1;
1038 goto ack;
1039 case USB_DEVICE_A_HNP_SUPPORT:
1040 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1041 /* B-devices compliant to OTG spec
1042 * Rev 2.0 are not required to
1043 * suppport these features.
1044 */
1045 goto stall;
1046#endif
1047 }
1048 } else if ((ctl.bRequest == USB_REQ_CLEAR_FEATURE) &&
1049 (ctl.wValue == USB_DEVICE_REMOTE_WAKEUP)) {
1050 atomic_set(&ui->remote_wakeup, 0);
1051 goto ack;
1052 }
1053 }
1054
1055 /* delegate if we get here */
1056 if (ui->driver) {
1057 ret = ui->driver->setup(&ui->gadget, &ctl);
1058 if (ret >= 0)
1059 return;
1060 }
1061
1062stall:
1063 /* stall ep0 on error */
1064 ep0_setup_stall(ui);
1065 return;
1066
1067ack:
1068 ep0_setup_ack(ui);
1069}
1070
1071static void handle_endpoint(struct usb_info *ui, unsigned bit)
1072{
1073 struct msm_endpoint *ept = ui->ept + bit;
1074 struct msm_request *req;
1075 unsigned long flags;
1076 unsigned info;
1077
1078 /*
1079 INFO("handle_endpoint() %d %s req=%p(%08x)\n",
1080 ept->num, (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1081 ept->req, ept->req ? ept->req->item_dma : 0);
1082 */
1083
1084 /* expire all requests that are no longer active */
1085 spin_lock_irqsave(&ui->lock, flags);
1086 while ((req = ept->req)) {
1087 /* if we've processed all live requests, time to
1088 * restart the hardware on the next non-live request
1089 */
1090 if (!req->live) {
1091 usb_ept_start(ept);
1092 break;
1093 }
1094
1095 /* clean speculative fetches on req->item->info */
1096 dma_coherent_post_ops();
1097 info = req->item->info;
1098 /* if the transaction is still in-flight, stop here */
1099 if (info & INFO_ACTIVE)
1100 break;
1101
1102 /* advance ept queue to the next request */
1103 ept->req = req->next;
1104 if (ept->req == 0)
1105 ept->last = 0;
1106
1107 dma_unmap_single(NULL, req->dma, req->req.length,
1108 (ept->flags & EPT_FLAG_IN) ?
1109 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1110
1111 if (info & (INFO_HALTED | INFO_BUFFER_ERROR | INFO_TXN_ERROR)) {
1112 /* XXX pass on more specific error code */
1113 req->req.status = -EIO;
1114 req->req.actual = 0;
1115 dev_err(&ui->pdev->dev,
1116 "ept %d %s error. info=%08x\n",
1117 ept->num,
1118 (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1119 info);
1120 } else {
1121 req->req.status = 0;
1122 req->req.actual =
1123 req->req.length - ((info >> 16) & 0x7FFF);
1124 }
1125 req->busy = 0;
1126 req->live = 0;
1127
1128 if (req->req.complete) {
1129 spin_unlock_irqrestore(&ui->lock, flags);
1130 req->req.complete(&ept->ep, &req->req);
1131 spin_lock_irqsave(&ui->lock, flags);
1132 }
1133 }
1134 spin_unlock_irqrestore(&ui->lock, flags);
1135}
1136
1137static void flush_endpoint_hw(struct usb_info *ui, unsigned bits)
1138{
1139 /* flush endpoint, canceling transactions
1140 ** - this can take a "large amount of time" (per databook)
1141 ** - the flush can fail in some cases, thus we check STAT
1142 ** and repeat if we're still operating
1143 ** (does the fact that this doesn't use the tripwire matter?!)
1144 */
1145 do {
1146 writel(bits, USB_ENDPTFLUSH);
1147 while (readl(USB_ENDPTFLUSH) & bits)
1148 udelay(100);
1149 } while (readl(USB_ENDPTSTAT) & bits);
1150}
1151
1152static void flush_endpoint_sw(struct msm_endpoint *ept)
1153{
1154 struct usb_info *ui = ept->ui;
1155 struct msm_request *req, *next_req = NULL;
1156 unsigned long flags;
1157
1158 /* inactive endpoints have nothing to do here */
1159 if (ept->ep.maxpacket == 0)
1160 return;
1161
1162 /* put the queue head in a sane state */
1163 ept->head->info = 0;
1164 ept->head->next = TERMINATE;
1165
1166 /* cancel any pending requests */
1167 spin_lock_irqsave(&ui->lock, flags);
1168 req = ept->req;
1169 ept->req = 0;
1170 ept->last = 0;
1171 while (req != 0) {
1172 req->busy = 0;
1173 req->live = 0;
1174 req->req.status = -ESHUTDOWN;
1175 req->req.actual = 0;
1176
1177 /* Gadget driver may free the request in completion
1178 * handler. So keep a copy of next req pointer
1179 * before calling completion handler.
1180 */
1181 next_req = req->next;
1182 if (req->req.complete) {
1183 spin_unlock_irqrestore(&ui->lock, flags);
1184 req->req.complete(&ept->ep, &req->req);
1185 spin_lock_irqsave(&ui->lock, flags);
1186 }
1187 req = next_req;
1188 }
1189 spin_unlock_irqrestore(&ui->lock, flags);
1190}
1191
1192static void flush_endpoint(struct msm_endpoint *ept)
1193{
1194 flush_endpoint_hw(ept->ui, (1 << ept->bit));
1195 flush_endpoint_sw(ept);
1196}
1197
1198static irqreturn_t usb_interrupt(int irq, void *data)
1199{
1200 struct usb_info *ui = data;
1201 unsigned n;
1202 unsigned long flags;
1203
1204 n = readl(USB_USBSTS);
1205 writel(n, USB_USBSTS);
1206
1207 /* somehow we got an IRQ while in the reset sequence: ignore it */
1208 if (!atomic_read(&ui->running))
1209 return IRQ_HANDLED;
1210
1211 if (n & STS_PCI) {
1212 msm_hsusb_set_speed(ui);
1213 if (atomic_read(&ui->configured)) {
1214 wake_lock(&ui->wlock);
1215
1216 spin_lock_irqsave(&ui->lock, flags);
1217 ui->usb_state = USB_STATE_CONFIGURED;
1218 ui->flags = USB_FLAG_CONFIGURED;
1219 spin_unlock_irqrestore(&ui->lock, flags);
1220
1221 ui->driver->resume(&ui->gadget);
1222 schedule_work(&ui->work);
1223 } else {
1224 msm_hsusb_set_state(USB_STATE_DEFAULT);
1225 }
1226
1227#ifdef CONFIG_USB_OTG
1228 /* notify otg to clear A_BIDL_ADIS timer */
1229 if (ui->gadget.is_a_peripheral)
1230 otg_set_suspend(ui->xceiv, 0);
1231#endif
1232 }
1233
1234 if (n & STS_URI) {
1235 dev_dbg(&ui->pdev->dev, "reset\n");
1236 spin_lock_irqsave(&ui->lock, flags);
1237 ui->gadget.speed = USB_SPEED_UNKNOWN;
1238 spin_unlock_irqrestore(&ui->lock, flags);
1239#ifdef CONFIG_USB_OTG
1240 /* notify otg to clear A_BIDL_ADIS timer */
1241 if (ui->gadget.is_a_peripheral)
1242 otg_set_suspend(ui->xceiv, 0);
1243 spin_lock_irqsave(&ui->lock, flags);
1244 /* Host request is persistent across reset */
1245 ui->gadget.b_hnp_enable = 0;
1246 ui->hnp_avail = 0;
1247 spin_unlock_irqrestore(&ui->lock, flags);
1248#endif
1249 msm_hsusb_set_state(USB_STATE_DEFAULT);
1250 atomic_set(&ui->remote_wakeup, 0);
1251 if (!ui->gadget.is_a_peripheral)
1252 schedule_delayed_work(&ui->chg_stop, 0);
1253
1254 writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
1255 writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
1256 writel(0xffffffff, USB_ENDPTFLUSH);
1257 writel(0, USB_ENDPTCTRL(1));
1258
1259 wake_lock(&ui->wlock);
1260 if (atomic_read(&ui->configured)) {
1261 /* marking us offline will cause ept queue attempts
1262 ** to fail
1263 */
1264 atomic_set(&ui->configured, 0);
1265 /* Defer sending offline uevent to userspace */
1266 atomic_set(&ui->offline_pending, 1);
1267
1268 /* XXX: we can't seem to detect going offline,
1269 * XXX: so deconfigure on reset for the time being
1270 */
1271 if (ui->driver) {
1272 dev_dbg(&ui->pdev->dev,
1273 "usb: notify offline\n");
1274 ui->driver->disconnect(&ui->gadget);
1275 }
1276 /* cancel pending ep0 transactions */
1277 flush_endpoint(&ui->ep0out);
1278 flush_endpoint(&ui->ep0in);
1279
1280 }
1281 /* Start phy stuck timer */
1282 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1283 mod_timer(&phy_status_timer, PHY_STATUS_CHECK_DELAY);
1284 }
1285
1286 if (n & STS_SLI) {
1287 dev_dbg(&ui->pdev->dev, "suspend\n");
1288
1289 spin_lock_irqsave(&ui->lock, flags);
1290 ui->usb_state = USB_STATE_SUSPENDED;
1291 ui->flags = USB_FLAG_SUSPEND;
1292 spin_unlock_irqrestore(&ui->lock, flags);
1293
1294 ui->driver->suspend(&ui->gadget);
1295 schedule_work(&ui->work);
1296#ifdef CONFIG_USB_OTG
1297 /* notify otg for
1298 * 1. kicking A_BIDL_ADIS timer in case of A-peripheral
1299 * 2. disabling pull-up and kicking B_ASE0_RST timer
1300 */
1301 if (ui->gadget.b_hnp_enable || ui->gadget.is_a_peripheral)
1302 otg_set_suspend(ui->xceiv, 1);
1303#endif
1304 }
1305
1306 if (n & STS_UI) {
1307 n = readl(USB_ENDPTSETUPSTAT);
1308 if (n & EPT_RX(0))
1309 handle_setup(ui);
1310
1311 n = readl(USB_ENDPTCOMPLETE);
1312 writel(n, USB_ENDPTCOMPLETE);
1313 while (n) {
1314 unsigned bit = __ffs(n);
1315 handle_endpoint(ui, bit);
1316 n = n & (~(1 << bit));
1317 }
1318 }
1319 return IRQ_HANDLED;
1320}
1321
1322static void usb_prepare(struct usb_info *ui)
1323{
1324 spin_lock_init(&ui->lock);
1325
1326 memset(ui->buf, 0, 4096);
1327 ui->head = (void *) (ui->buf + 0);
1328
1329 /* only important for reset/reinit */
1330 memset(ui->ept, 0, sizeof(ui->ept));
1331 ui->next_item = 0;
1332 ui->next_ifc_num = 0;
1333
1334 init_endpoints(ui);
1335
1336 ui->ep0in.ep.maxpacket = 64;
1337 ui->ep0out.ep.maxpacket = 64;
1338
1339 ui->setup_req =
1340 usb_ept_alloc_req(&ui->ep0in, SETUP_BUF_SIZE, GFP_KERNEL);
1341
1342 INIT_WORK(&ui->work, usb_do_work);
1343 INIT_DELAYED_WORK(&ui->chg_det, usb_chg_detect);
1344 INIT_DELAYED_WORK(&ui->chg_stop, usb_chg_stop);
1345 INIT_DELAYED_WORK(&ui->rw_work, usb_do_remote_wakeup);
1346 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1347 INIT_WORK(&ui->phy_status_check, usb_phy_stuck_recover);
1348}
1349
1350static void usb_reset(struct usb_info *ui)
1351{
1352 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1353
1354 dev_dbg(&ui->pdev->dev, "reset controller\n");
1355
1356 atomic_set(&ui->running, 0);
1357
1358 /*
1359 * PHY reset takes minimum 100 msec. Hence reset only link
1360 * during HNP. Reset PHY and link in B-peripheral mode.
1361 */
1362 if (ui->gadget.is_a_peripheral)
1363 otg->reset(ui->xceiv, 0);
1364 else
1365 otg->reset(ui->xceiv, 1);
1366
1367 /* set usb controller interrupt threshold to zero*/
1368 writel((readl(USB_USBCMD) & ~USBCMD_ITC_MASK) | USBCMD_ITC(0),
1369 USB_USBCMD);
1370
1371 writel(ui->dma, USB_ENDPOINTLISTADDR);
1372
1373 configure_endpoints(ui);
1374
1375 /* marking us offline will cause ept queue attempts to fail */
1376 atomic_set(&ui->configured, 0);
1377
1378 if (ui->driver) {
1379 dev_dbg(&ui->pdev->dev, "usb: notify offline\n");
1380 ui->driver->disconnect(&ui->gadget);
1381 }
1382
1383 /* cancel pending ep0 transactions */
1384 flush_endpoint(&ui->ep0out);
1385 flush_endpoint(&ui->ep0in);
1386
1387 /* enable interrupts */
1388 writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR);
1389
1390 /* Ensure that h/w RESET is completed before returning */
1391 mb();
1392
1393 atomic_set(&ui->running, 1);
1394}
1395
1396static void usb_start(struct usb_info *ui)
1397{
1398 unsigned long flags;
1399
1400 spin_lock_irqsave(&ui->lock, flags);
1401 ui->flags |= USB_FLAG_START;
1402 schedule_work(&ui->work);
1403 spin_unlock_irqrestore(&ui->lock, flags);
1404}
1405
1406static int usb_free(struct usb_info *ui, int ret)
1407{
1408 dev_dbg(&ui->pdev->dev, "usb_free(%d)\n", ret);
1409
1410 if (ui->xceiv)
1411 otg_put_transceiver(ui->xceiv);
1412
1413 if (ui->irq)
1414 free_irq(ui->irq, 0);
1415 if (ui->pool)
1416 dma_pool_destroy(ui->pool);
1417 if (ui->dma)
1418 dma_free_coherent(&ui->pdev->dev, 4096, ui->buf, ui->dma);
1419 kfree(ui);
1420 return ret;
1421}
1422
1423static void usb_do_work_check_vbus(struct usb_info *ui)
1424{
1425 unsigned long iflags;
1426
1427 spin_lock_irqsave(&ui->lock, iflags);
1428 if (is_usb_online(ui))
1429 ui->flags |= USB_FLAG_VBUS_ONLINE;
1430 else
1431 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1432 spin_unlock_irqrestore(&ui->lock, iflags);
1433}
1434
1435static void usb_do_work(struct work_struct *w)
1436{
1437 struct usb_info *ui = container_of(w, struct usb_info, work);
1438 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1439 unsigned long iflags;
1440 unsigned flags, _vbus;
1441
1442 for (;;) {
1443 spin_lock_irqsave(&ui->lock, iflags);
1444 flags = ui->flags;
1445 ui->flags = 0;
1446 _vbus = is_usb_online(ui);
1447 spin_unlock_irqrestore(&ui->lock, iflags);
1448
1449 /* give up if we have nothing to do */
1450 if (flags == 0)
1451 break;
1452
1453 switch (ui->state) {
1454 case USB_STATE_IDLE:
1455 if (flags & USB_FLAG_START) {
1456 int ret;
1457
1458 if (!_vbus) {
1459 ui->state = USB_STATE_OFFLINE;
1460 break;
1461 }
1462
1463 pm_runtime_get_noresume(&ui->pdev->dev);
1464 pm_runtime_resume(&ui->pdev->dev);
1465 dev_dbg(&ui->pdev->dev,
1466 "msm72k_udc: IDLE -> ONLINE\n");
1467 usb_reset(ui);
1468 ret = request_irq(otg->irq, usb_interrupt,
1469 IRQF_SHARED,
1470 ui->pdev->name, ui);
1471 /* FIXME: should we call BUG_ON when
1472 * requst irq fails
1473 */
1474 if (ret) {
1475 dev_err(&ui->pdev->dev,
1476 "hsusb: peripheral: request irq"
1477 " failed:(%d)", ret);
1478 break;
1479 }
1480 ui->irq = otg->irq;
1481 ui->state = USB_STATE_ONLINE;
1482 usb_do_work_check_vbus(ui);
1483
1484 if (!atomic_read(&ui->softconnect))
1485 break;
1486
1487 msm72k_pullup_internal(&ui->gadget, 1);
1488
1489 if (!ui->gadget.is_a_peripheral)
1490 schedule_delayed_work(
1491 &ui->chg_det,
1492 USB_CHG_DET_DELAY);
1493
1494 }
1495 break;
1496 case USB_STATE_ONLINE:
1497 if (atomic_read(&ui->offline_pending)) {
1498 switch_set_state(&ui->sdev, 0);
1499 atomic_set(&ui->offline_pending, 0);
1500 }
1501
1502 /* If at any point when we were online, we received
1503 * the signal to go offline, we must honor it
1504 */
1505 if (flags & USB_FLAG_VBUS_OFFLINE) {
1506
1507 ui->chg_current = 0;
1508 /* wait incase chg_detect is running */
1509 if (!ui->gadget.is_a_peripheral)
1510 cancel_delayed_work_sync(&ui->chg_det);
1511
1512 dev_dbg(&ui->pdev->dev,
1513 "msm72k_udc: ONLINE -> OFFLINE\n");
1514
1515 atomic_set(&ui->running, 0);
1516 atomic_set(&ui->remote_wakeup, 0);
1517 atomic_set(&ui->configured, 0);
1518
1519 if (ui->driver) {
1520 dev_dbg(&ui->pdev->dev,
1521 "usb: notify offline\n");
1522 ui->driver->disconnect(&ui->gadget);
1523 }
1524 /* cancel pending ep0 transactions */
1525 flush_endpoint(&ui->ep0out);
1526 flush_endpoint(&ui->ep0in);
1527
1528 /* synchronize with irq context */
1529 spin_lock_irqsave(&ui->lock, iflags);
1530#ifdef CONFIG_USB_OTG
1531 ui->gadget.host_request = 0;
1532 ui->gadget.b_hnp_enable = 0;
1533 ui->hnp_avail = 0;
1534#endif
1535 msm72k_pullup_internal(&ui->gadget, 0);
1536 spin_unlock_irqrestore(&ui->lock, iflags);
1537
1538
1539 /* if charger is initialized to known type
1540 * we must let modem know about charger
1541 * disconnection
1542 */
1543 otg_set_power(ui->xceiv, 0);
1544
1545 if (ui->irq) {
1546 free_irq(ui->irq, ui);
1547 ui->irq = 0;
1548 }
1549
1550
1551 switch_set_state(&ui->sdev, 0);
1552
1553 ui->state = USB_STATE_OFFLINE;
1554 usb_do_work_check_vbus(ui);
1555 pm_runtime_put_noidle(&ui->pdev->dev);
1556 pm_runtime_suspend(&ui->pdev->dev);
1557 wake_unlock(&ui->wlock);
1558 break;
1559 }
1560 if (flags & USB_FLAG_SUSPEND) {
1561 int maxpower = usb_get_max_power(ui);
1562
1563 if (maxpower < 0)
1564 break;
1565
1566 otg_set_power(ui->xceiv, 0);
1567 /* To support TCXO during bus suspend
1568 * This might be dummy check since bus suspend
1569 * is not implemented as of now
1570 * */
1571 if (release_wlocks)
1572 wake_unlock(&ui->wlock);
1573
1574 /* TBD: Initiate LPM at usb bus suspend */
1575 break;
1576 }
1577 if (flags & USB_FLAG_CONFIGURED) {
1578 int maxpower = usb_get_max_power(ui);
1579
1580 /* We may come here even when no configuration
1581 * is selected. Send online/offline event
1582 * accordingly.
1583 */
1584 switch_set_state(&ui->sdev,
1585 atomic_read(&ui->configured));
1586
1587 if (maxpower < 0)
1588 break;
1589
1590 ui->chg_current = maxpower;
1591 otg_set_power(ui->xceiv, maxpower);
1592 break;
1593 }
1594 if (flags & USB_FLAG_RESET) {
1595 dev_dbg(&ui->pdev->dev,
1596 "msm72k_udc: ONLINE -> RESET\n");
1597 msm72k_pullup_internal(&ui->gadget, 0);
1598 usb_reset(ui);
1599 msm72k_pullup_internal(&ui->gadget, 1);
1600 dev_dbg(&ui->pdev->dev,
1601 "msm72k_udc: RESET -> ONLINE\n");
1602 break;
1603 }
1604 break;
1605 case USB_STATE_OFFLINE:
1606 /* If we were signaled to go online and vbus is still
1607 * present when we received the signal, go online.
1608 */
1609 if ((flags & USB_FLAG_VBUS_ONLINE) && _vbus) {
1610 int ret;
1611
1612 pm_runtime_get_noresume(&ui->pdev->dev);
1613 pm_runtime_resume(&ui->pdev->dev);
1614 dev_dbg(&ui->pdev->dev,
1615 "msm72k_udc: OFFLINE -> ONLINE\n");
1616
1617 usb_reset(ui);
1618 ui->state = USB_STATE_ONLINE;
1619 usb_do_work_check_vbus(ui);
1620 ret = request_irq(otg->irq, usb_interrupt,
1621 IRQF_SHARED,
1622 ui->pdev->name, ui);
1623 /* FIXME: should we call BUG_ON when
1624 * requst irq fails
1625 */
1626 if (ret) {
1627 dev_err(&ui->pdev->dev,
1628 "hsusb: peripheral: request irq"
1629 " failed:(%d)", ret);
1630 break;
1631 }
1632 ui->irq = otg->irq;
1633 enable_irq_wake(otg->irq);
1634
1635 if (!atomic_read(&ui->softconnect))
1636 break;
1637 msm72k_pullup_internal(&ui->gadget, 1);
1638
1639 if (!ui->gadget.is_a_peripheral)
1640 schedule_delayed_work(
1641 &ui->chg_det,
1642 USB_CHG_DET_DELAY);
1643 }
1644 break;
1645 }
1646 }
1647}
1648
1649/* FIXME - the callers of this function should use a gadget API instead.
1650 * This is called from htc_battery.c and board-halibut.c
1651 * WARNING - this can get called before this driver is initialized.
1652 */
1653void msm_hsusb_set_vbus_state(int online)
1654{
1655 unsigned long flags;
1656 struct usb_info *ui = the_usb_info;
1657
1658 if (!ui) {
1659 pr_err("%s called before driver initialized\n", __func__);
1660 return;
1661 }
1662
1663 spin_lock_irqsave(&ui->lock, flags);
1664
1665 if (is_usb_online(ui) == online)
1666 goto out;
1667
1668 if (online) {
1669 ui->usb_state = USB_STATE_POWERED;
1670 ui->flags |= USB_FLAG_VBUS_ONLINE;
1671 } else {
1672 ui->gadget.speed = USB_SPEED_UNKNOWN;
1673 ui->usb_state = USB_STATE_NOTATTACHED;
1674 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1675 }
1676 if (in_interrupt()) {
1677 schedule_work(&ui->work);
1678 } else {
1679 spin_unlock_irqrestore(&ui->lock, flags);
1680 usb_do_work(&ui->work);
1681 return;
1682 }
1683out:
1684 spin_unlock_irqrestore(&ui->lock, flags);
1685}
1686
1687#if defined(CONFIG_DEBUG_FS)
1688
1689void usb_function_reenumerate(void)
1690{
1691 struct usb_info *ui = the_usb_info;
1692
1693 /* disable and re-enable the D+ pullup */
1694 dev_dbg(&ui->pdev->dev, "disable pullup\n");
1695 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
1696
1697 msleep(10);
1698
1699 dev_dbg(&ui->pdev->dev, "enable pullup\n");
1700 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
1701}
1702
1703static char debug_buffer[PAGE_SIZE];
1704
1705static ssize_t debug_read_status(struct file *file, char __user *ubuf,
1706 size_t count, loff_t *ppos)
1707{
1708 struct usb_info *ui = file->private_data;
1709 char *buf = debug_buffer;
1710 unsigned long flags;
1711 struct msm_endpoint *ept;
1712 struct msm_request *req;
1713 int n;
1714 int i = 0;
1715
1716 spin_lock_irqsave(&ui->lock, flags);
1717
1718 i += scnprintf(buf + i, PAGE_SIZE - i,
1719 "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
1720 readl(USB_ENDPTSETUPSTAT),
1721 readl(USB_ENDPTPRIME),
1722 readl(USB_ENDPTSTAT),
1723 readl(USB_ENDPTCOMPLETE));
1724 i += scnprintf(buf + i, PAGE_SIZE - i,
1725 "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
1726 readl(USB_USBCMD),
1727 readl(USB_USBSTS),
1728 readl(USB_USBINTR),
1729 readl(USB_PORTSC));
1730
1731
1732 for (n = 0; n < 32; n++) {
1733 ept = ui->ept + n;
1734 if (ept->ep.maxpacket == 0)
1735 continue;
1736
1737 i += scnprintf(buf + i, PAGE_SIZE - i,
1738 "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
1739 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1740 ept->head->config, ept->head->active,
1741 ept->head->next, ept->head->info);
1742
1743 for (req = ept->req; req; req = req->next)
1744 i += scnprintf(buf + i, PAGE_SIZE - i,
1745 " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
1746 req->item_dma, req->item->next,
1747 req->item->info, req->item->page0,
1748 req->busy ? 'B' : ' ',
1749 req->live ? 'L' : ' ');
1750 }
1751
1752 i += scnprintf(buf + i, PAGE_SIZE - i,
1753 "phy failure count: %d\n", ui->phy_fail_count);
1754
1755 spin_unlock_irqrestore(&ui->lock, flags);
1756
1757 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
1758}
1759
1760static ssize_t debug_write_reset(struct file *file, const char __user *buf,
1761 size_t count, loff_t *ppos)
1762{
1763 struct usb_info *ui = file->private_data;
1764 unsigned long flags;
1765
1766 spin_lock_irqsave(&ui->lock, flags);
1767 ui->flags |= USB_FLAG_RESET;
1768 schedule_work(&ui->work);
1769 spin_unlock_irqrestore(&ui->lock, flags);
1770
1771 return count;
1772}
1773
1774static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
1775 size_t count, loff_t *ppos)
1776{
1777 usb_function_reenumerate();
1778 return count;
1779}
1780
1781static int debug_open(struct inode *inode, struct file *file)
1782{
1783 file->private_data = inode->i_private;
1784 return 0;
1785}
1786
1787const struct file_operations debug_stat_ops = {
1788 .open = debug_open,
1789 .read = debug_read_status,
1790};
1791
1792const struct file_operations debug_reset_ops = {
1793 .open = debug_open,
1794 .write = debug_write_reset,
1795};
1796
1797const struct file_operations debug_cycle_ops = {
1798 .open = debug_open,
1799 .write = debug_write_cycle,
1800};
1801
1802static ssize_t debug_read_release_wlocks(struct file *file, char __user *ubuf,
1803 size_t count, loff_t *ppos)
1804{
1805 char kbuf[10];
1806 size_t c = 0;
1807
1808 memset(kbuf, 0, 10);
1809
1810 c = scnprintf(kbuf, 10, "%d", release_wlocks);
1811
1812 if (copy_to_user(ubuf, kbuf, c))
1813 return -EFAULT;
1814
1815 return c;
1816}
1817static ssize_t debug_write_release_wlocks(struct file *file,
1818 const char __user *buf, size_t count, loff_t *ppos)
1819{
1820 char kbuf[10];
1821 long temp;
1822
1823 memset(kbuf, 0, 10);
1824
1825 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1826 return -EFAULT;
1827
1828 if (strict_strtol(kbuf, 10, &temp))
1829 return -EINVAL;
1830
1831 if (temp)
1832 release_wlocks = 1;
1833 else
1834 release_wlocks = 0;
1835
1836 return count;
1837}
1838static int debug_wake_lock_open(struct inode *inode, struct file *file)
1839{
1840 file->private_data = inode->i_private;
1841 return 0;
1842}
1843const struct file_operations debug_wlocks_ops = {
1844 .open = debug_wake_lock_open,
1845 .read = debug_read_release_wlocks,
1846 .write = debug_write_release_wlocks,
1847};
1848static void usb_debugfs_init(struct usb_info *ui)
1849{
1850 struct dentry *dent;
1851 dent = debugfs_create_dir(dev_name(&ui->pdev->dev), 0);
1852 if (IS_ERR(dent))
1853 return;
1854
1855 debugfs_create_file("status", 0444, dent, ui, &debug_stat_ops);
1856 debugfs_create_file("reset", 0222, dent, ui, &debug_reset_ops);
1857 debugfs_create_file("cycle", 0222, dent, ui, &debug_cycle_ops);
1858 debugfs_create_file("release_wlocks", 0666, dent, ui,
1859 &debug_wlocks_ops);
1860}
1861#else
1862static void usb_debugfs_init(struct usb_info *ui) {}
1863#endif
1864
1865static int
1866msm72k_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
1867{
1868 struct msm_endpoint *ept = to_msm_endpoint(_ep);
1869 unsigned char ep_type =
1870 desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1871
1872 _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1873 config_ept(ept);
1874 ept->wedged = 0;
1875 usb_ept_enable(ept, 1, ep_type);
1876 return 0;
1877}
1878
1879static int msm72k_disable(struct usb_ep *_ep)
1880{
1881 struct msm_endpoint *ept = to_msm_endpoint(_ep);
1882
1883 usb_ept_enable(ept, 0, 0);
1884 flush_endpoint(ept);
1885 return 0;
1886}
1887
1888static struct usb_request *
1889msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
1890{
1891 return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
1892}
1893
1894static void
1895msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
1896{
1897 struct msm_request *req = to_msm_request(_req);
1898 struct msm_endpoint *ept = to_msm_endpoint(_ep);
1899 struct usb_info *ui = ept->ui;
1900
1901 /* request should not be busy */
1902 BUG_ON(req->busy);
1903 if (req->alloced)
1904 kfree(req->req.buf);
1905 dma_pool_free(ui->pool, req->item, req->item_dma);
1906 kfree(req);
1907}
1908
1909static int
1910msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
1911{
1912 struct msm_endpoint *ep = to_msm_endpoint(_ep);
1913 struct usb_info *ui = ep->ui;
1914
1915 if (ep == &ui->ep0in) {
1916 struct msm_request *r = to_msm_request(req);
1917 if (!req->length)
1918 goto ep_queue_done;
1919 r->gadget_complete = req->complete;
1920 /* ep0_queue_ack_complete queue a receive for ACK before
1921 ** calling req->complete
1922 */
1923 req->complete = ep0_queue_ack_complete;
1924 if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
1925 ep = &ui->ep0out;
1926 goto ep_queue_done;
1927 }
1928
1929ep_queue_done:
1930 return usb_ept_queue_xfer(ep, req);
1931}
1932
1933static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1934{
1935 struct msm_endpoint *ep = to_msm_endpoint(_ep);
1936 struct msm_request *req = to_msm_request(_req);
1937 struct usb_info *ui = ep->ui;
1938
1939 struct msm_request *temp_req;
1940 unsigned long flags;
1941
1942 if (!(ui && req && ep->req))
1943 return -EINVAL;
1944
1945 spin_lock_irqsave(&ui->lock, flags);
1946 if (!req->busy) {
1947 dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
1948 spin_unlock_irqrestore(&ui->lock, flags);
1949 return -EINVAL;
1950 }
1951 /* Stop the transfer */
1952 do {
1953 writel((1 << ep->bit), USB_ENDPTFLUSH);
1954 while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
1955 udelay(100);
1956 } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
1957
1958 req->req.status = 0;
1959 req->busy = 0;
1960
1961 if (ep->req == req) {
1962 ep->req = req->next;
1963 ep->head->next = req->item->next;
1964 } else {
1965 req->prev->next = req->next;
1966 if (req->next)
1967 req->next->prev = req->prev;
1968 req->prev->item->next = req->item->next;
1969 }
1970
1971 if (!req->next)
1972 ep->last = req->prev;
1973
1974 /* initialize request to default */
1975 req->item->next = TERMINATE;
1976 req->item->info = 0;
1977 req->live = 0;
1978 dma_unmap_single(NULL, req->dma, req->req.length,
1979 (ep->flags & EPT_FLAG_IN) ?
1980 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1981
1982 if (req->req.complete) {
1983 req->req.status = -ECONNRESET;
1984 spin_unlock_irqrestore(&ui->lock, flags);
1985 req->req.complete(&ep->ep, &req->req);
1986 spin_lock_irqsave(&ui->lock, flags);
1987 }
1988
1989 if (!req->live) {
1990 /* Reprime the endpoint for the remaining transfers */
1991 for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
1992 temp_req->live = 0;
1993 if (ep->req)
1994 usb_ept_start(ep);
1995 spin_unlock_irqrestore(&ui->lock, flags);
1996 return 0;
1997 }
1998 spin_unlock_irqrestore(&ui->lock, flags);
1999 return 0;
2000}
2001
2002static int
2003usb_ept_set_halt(struct usb_ep *_ep, int value)
2004{
2005 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2006 struct usb_info *ui = ept->ui;
2007 unsigned int in = ept->flags & EPT_FLAG_IN;
2008 unsigned int n;
2009 unsigned long flags;
2010
2011 spin_lock_irqsave(&ui->lock, flags);
2012
2013 n = readl(USB_ENDPTCTRL(ept->num));
2014
2015 if (in) {
2016 if (value)
2017 n |= CTRL_TXS;
2018 else {
2019 n &= ~CTRL_TXS;
2020 n |= CTRL_TXR;
2021 }
2022 } else {
2023 if (value)
2024 n |= CTRL_RXS;
2025 else {
2026 n &= ~CTRL_RXS;
2027 n |= CTRL_RXR;
2028 }
2029 }
2030 writel(n, USB_ENDPTCTRL(ept->num));
2031 if (!value)
2032 ept->wedged = 0;
2033 spin_unlock_irqrestore(&ui->lock, flags);
2034
2035 return 0;
2036}
2037
2038static int
2039msm72k_set_halt(struct usb_ep *_ep, int value)
2040{
2041 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2042 unsigned int in = ept->flags & EPT_FLAG_IN;
2043
2044 if (value && in && ept->req)
2045 return -EAGAIN;
2046
2047 usb_ept_set_halt(_ep, value);
2048
2049 return 0;
2050}
2051
2052static int
2053msm72k_fifo_status(struct usb_ep *_ep)
2054{
2055 return -EOPNOTSUPP;
2056}
2057
2058static void
2059msm72k_fifo_flush(struct usb_ep *_ep)
2060{
2061 flush_endpoint(to_msm_endpoint(_ep));
2062}
2063static int msm72k_set_wedge(struct usb_ep *_ep)
2064{
2065 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2066
2067 if (ept->num == 0)
2068 return -EINVAL;
2069
2070 ept->wedged = 1;
2071
2072 return msm72k_set_halt(_ep, 1);
2073}
2074
2075static const struct usb_ep_ops msm72k_ep_ops = {
2076 .enable = msm72k_enable,
2077 .disable = msm72k_disable,
2078
2079 .alloc_request = msm72k_alloc_request,
2080 .free_request = msm72k_free_request,
2081
2082 .queue = msm72k_queue,
2083 .dequeue = msm72k_dequeue,
2084
2085 .set_halt = msm72k_set_halt,
2086 .set_wedge = msm72k_set_wedge,
2087 .fifo_status = msm72k_fifo_status,
2088 .fifo_flush = msm72k_fifo_flush,
2089};
2090
2091static int msm72k_get_frame(struct usb_gadget *_gadget)
2092{
2093 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2094
2095 /* frame number is in bits 13:3 */
2096 return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
2097}
2098
2099/* VBUS reporting logically comes from a transceiver */
2100static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
2101{
2102 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2103 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2104
2105 if (is_active || atomic_read(&otg->chg_type)
2106 == USB_CHG_TYPE__WALLCHARGER)
2107 wake_lock(&ui->wlock);
2108
2109 msm_hsusb_set_vbus_state(is_active);
2110 return 0;
2111}
2112
2113/* SW workarounds
2114Issue #1 - USB Spoof Disconnect Failure
2115Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2116SW workaround - Making opmode non-driving and SuspendM set in function
2117 register of SMSC phy
2118*/
2119/* drivers may have software control over D+ pullup */
2120static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
2121{
2122 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2123 unsigned long flags;
2124
2125 if (is_active) {
2126 spin_lock_irqsave(&ui->lock, flags);
2127 if (is_usb_online(ui) && ui->driver)
2128 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2129 spin_unlock_irqrestore(&ui->lock, flags);
2130 } else {
2131 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2132 /* S/W workaround, Issue#1 */
2133 otg_io_write(ui->xceiv, 0x48, 0x04);
2134 }
2135
2136 /* Ensure pull-up operation is completed before returning */
2137 mb();
2138
2139 return 0;
2140}
2141
2142static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
2143{
2144 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2145 unsigned long flags;
2146
2147
2148 atomic_set(&ui->softconnect, is_active);
2149
2150 spin_lock_irqsave(&ui->lock, flags);
2151 if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL) {
2152 spin_unlock_irqrestore(&ui->lock, flags);
2153 return 0;
2154 }
2155 spin_unlock_irqrestore(&ui->lock, flags);
2156
2157 msm72k_pullup_internal(_gadget, is_active);
2158
2159 if (is_active && !ui->gadget.is_a_peripheral)
2160 schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
2161
2162 return 0;
2163}
2164
2165static int msm72k_wakeup(struct usb_gadget *_gadget)
2166{
2167 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2168 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2169
2170 if (!atomic_read(&ui->remote_wakeup)) {
2171 dev_err(&ui->pdev->dev,
2172 "%s: remote wakeup not supported\n", __func__);
2173 return -ENOTSUPP;
2174 }
2175
2176 if (!atomic_read(&ui->configured)) {
2177 dev_err(&ui->pdev->dev,
2178 "%s: device is not configured\n", __func__);
2179 return -ENODEV;
2180 }
2181 otg_set_suspend(ui->xceiv, 0);
2182
2183 disable_irq(otg->irq);
2184
2185 if (!is_usb_active())
2186 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
2187
2188 /* Ensure that USB port is resumed before enabling the IRQ */
2189 mb();
2190
2191 enable_irq(otg->irq);
2192
2193 return 0;
2194}
2195
2196/* when Gadget is configured, it will indicate how much power
2197 * can be pulled from vbus, as specified in configuiration descriptor
2198 */
2199static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2200{
2201 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2202 unsigned long flags;
2203
2204
2205 spin_lock_irqsave(&ui->lock, flags);
2206 ui->b_max_pow = mA;
2207 ui->flags = USB_FLAG_CONFIGURED;
2208 spin_unlock_irqrestore(&ui->lock, flags);
2209
2210 schedule_work(&ui->work);
2211
2212 return 0;
2213}
2214
2215static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
2216{
2217 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2218 unsigned long flags;
2219 int ret = 0;
2220
2221 spin_lock_irqsave(&ui->lock, flags);
2222 if (set) {
2223 if (ui->pdata && ui->pdata->self_powered)
2224 atomic_set(&ui->self_powered, 1);
2225 else
2226 ret = -EOPNOTSUPP;
2227 } else {
2228 /* We can always work as a bus powered device */
2229 atomic_set(&ui->self_powered, 0);
2230 }
2231 spin_unlock_irqrestore(&ui->lock, flags);
2232
2233 return ret;
2234
2235}
2236
2237static const struct usb_gadget_ops msm72k_ops = {
2238 .get_frame = msm72k_get_frame,
2239 .vbus_session = msm72k_udc_vbus_session,
2240 .vbus_draw = msm72k_udc_vbus_draw,
2241 .pullup = msm72k_pullup,
2242 .wakeup = msm72k_wakeup,
2243 .set_selfpowered = msm72k_set_selfpowered,
2244};
2245
2246static void usb_do_remote_wakeup(struct work_struct *w)
2247{
2248 struct usb_info *ui = the_usb_info;
2249
2250 msm72k_wakeup(&ui->gadget);
2251}
2252
2253static ssize_t usb_remote_wakeup(struct device *dev,
2254 struct device_attribute *attr, const char *buf, size_t count)
2255{
2256 struct usb_info *ui = the_usb_info;
2257
2258 msm72k_wakeup(&ui->gadget);
2259
2260 return count;
2261}
2262
2263static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
2264 char *buf)
2265{
2266 size_t i;
2267 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
2268 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
2269 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
2270 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
2271 "USB_STATE_SUSPENDED"
2272 };
2273
2274 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
2275 return i;
2276}
2277
2278static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
2279 char *buf)
2280{
2281 struct usb_info *ui = the_usb_info;
2282 size_t i;
2283 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
2284 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
2285
2286 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
2287 return i;
2288}
2289
2290static ssize_t store_usb_chg_current(struct device *dev,
2291 struct device_attribute *attr, const char *buf, size_t count)
2292{
2293 struct usb_info *ui = the_usb_info;
2294 unsigned long mA;
2295
2296 if (ui->gadget.is_a_peripheral)
2297 return -EINVAL;
2298
2299 if (strict_strtoul(buf, 10, &mA))
2300 return -EINVAL;
2301
2302 ui->chg_current = mA;
2303 otg_set_power(ui->xceiv, mA);
2304
2305 return count;
2306}
2307
2308static ssize_t show_usb_chg_current(struct device *dev,
2309 struct device_attribute *attr, char *buf)
2310{
2311 struct usb_info *ui = the_usb_info;
2312 size_t count;
2313
2314 count = sprintf(buf, "%d", ui->chg_current);
2315
2316 return count;
2317}
2318
2319static ssize_t show_usb_chg_type(struct device *dev,
2320 struct device_attribute *attr, char *buf)
2321{
2322 struct usb_info *ui = the_usb_info;
2323 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2324 size_t count;
2325 char *chg_type[] = {"STD DOWNSTREAM PORT",
2326 "CARKIT",
2327 "DEDICATED CHARGER",
2328 "INVALID"};
2329
2330 count = sprintf(buf, "%s",
2331 chg_type[atomic_read(&otg->chg_type)]);
2332
2333 return count;
2334}
2335static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
2336static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
2337static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
2338static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
2339static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
2340 show_usb_chg_current, store_usb_chg_current);
2341
2342#ifdef CONFIG_USB_OTG
2343static ssize_t store_host_req(struct device *dev,
2344 struct device_attribute *attr, const char *buf, size_t count)
2345{
2346 struct usb_info *ui = the_usb_info;
2347 unsigned long val, flags;
2348
2349 if (strict_strtoul(buf, 10, &val))
2350 return -EINVAL;
2351
2352 dev_dbg(&ui->pdev->dev, "%s host request\n",
2353 val ? "set" : "clear");
2354
2355 spin_lock_irqsave(&ui->lock, flags);
2356 if (ui->hnp_avail)
2357 ui->gadget.host_request = !!val;
2358 spin_unlock_irqrestore(&ui->lock, flags);
2359
2360 return count;
2361}
2362static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
2363
2364/* How do we notify user space about HNP availability?
2365 * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
2366 * Introduce hnp_avail flag and set when HNP polling request arrives.
2367 * The expectation is that user space checks hnp availability before
2368 * requesting host role via above sysfs node.
2369 */
2370static ssize_t show_host_avail(struct device *dev,
2371 struct device_attribute *attr, char *buf)
2372{
2373 struct usb_info *ui = the_usb_info;
2374 size_t count;
2375 unsigned long flags;
2376
2377 spin_lock_irqsave(&ui->lock, flags);
2378 count = sprintf(buf, "%d\n", ui->hnp_avail);
2379 spin_unlock_irqrestore(&ui->lock, flags);
2380
2381 return count;
2382}
2383static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
2384
2385static struct attribute *otg_attrs[] = {
2386 &dev_attr_host_request.attr,
2387 &dev_attr_host_avail.attr,
2388 NULL,
2389};
2390
2391static struct attribute_group otg_attr_grp = {
2392 .name = "otg",
2393 .attrs = otg_attrs,
2394};
2395#endif
2396
2397static int msm72k_probe(struct platform_device *pdev)
2398{
2399 struct usb_info *ui;
2400 struct msm_otg *otg;
2401 int retval;
2402
2403 dev_dbg(&pdev->dev, "msm72k_probe\n");
2404 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
2405 if (!ui)
2406 return -ENOMEM;
2407
2408 ui->pdev = pdev;
2409 ui->pdata = pdev->dev.platform_data;
2410
2411 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
2412 if (!ui->buf)
2413 return usb_free(ui, -ENOMEM);
2414
2415 ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
2416 if (!ui->pool)
2417 return usb_free(ui, -ENOMEM);
2418
2419 ui->xceiv = otg_get_transceiver();
2420 if (!ui->xceiv)
2421 return usb_free(ui, -ENODEV);
2422
2423 otg = to_msm_otg(ui->xceiv);
2424 ui->addr = otg->regs;
2425
2426 ui->gadget.ops = &msm72k_ops;
2427 ui->gadget.is_dualspeed = 1;
2428 device_initialize(&ui->gadget.dev);
2429 dev_set_name(&ui->gadget.dev, "gadget");
2430 ui->gadget.dev.parent = &pdev->dev;
2431 ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
2432
2433#ifdef CONFIG_USB_OTG
2434 ui->gadget.is_otg = 1;
2435#endif
2436
2437 ui->sdev.name = DRIVER_NAME;
2438 ui->sdev.print_name = print_switch_name;
2439 ui->sdev.print_state = print_switch_state;
2440
2441 retval = switch_dev_register(&ui->sdev);
2442 if (retval)
2443 return usb_free(ui, retval);
2444
2445 the_usb_info = ui;
2446
2447 wake_lock_init(&ui->wlock,
2448 WAKE_LOCK_SUSPEND, "usb_bus_active");
2449
2450 usb_debugfs_init(ui);
2451
2452 usb_prepare(ui);
2453
2454#ifdef CONFIG_USB_OTG
2455 retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
2456 if (retval) {
2457 dev_err(&ui->pdev->dev,
2458 "failed to create otg sysfs directory:"
2459 "err:(%d)\n", retval);
2460 }
2461#endif
2462
2463 retval = otg_set_peripheral(ui->xceiv, &ui->gadget);
2464 if (retval) {
2465 dev_err(&ui->pdev->dev,
2466 "%s: Cannot bind the transceiver, retval:(%d)\n",
2467 __func__, retval);
2468 switch_dev_unregister(&ui->sdev);
2469 wake_lock_destroy(&ui->wlock);
2470 return usb_free(ui, retval);
2471 }
2472
2473 pm_runtime_enable(&pdev->dev);
2474
2475 /* Setup phy stuck timer */
2476 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
2477 setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
2478 return 0;
2479}
2480
2481int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2482 int (*bind)(struct usb_gadget *))
2483{
2484 struct usb_info *ui = the_usb_info;
2485 int retval, n;
2486
2487 if (!driver
2488 || driver->speed < USB_SPEED_FULL
2489 || !bind
2490 || !driver->disconnect
2491 || !driver->setup)
2492 return -EINVAL;
2493 if (!ui)
2494 return -ENODEV;
2495 if (ui->driver)
2496 return -EBUSY;
2497
2498 /* first hook up the driver ... */
2499 ui->driver = driver;
2500 ui->gadget.dev.driver = &driver->driver;
2501 ui->gadget.name = driver_name;
2502 INIT_LIST_HEAD(&ui->gadget.ep_list);
2503 ui->gadget.ep0 = &ui->ep0in.ep;
2504 INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
2505 ui->gadget.speed = USB_SPEED_UNKNOWN;
2506 atomic_set(&ui->softconnect, 1);
2507
2508 for (n = 1; n < 16; n++) {
2509 struct msm_endpoint *ept = ui->ept + n;
2510 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2511 ept->ep.maxpacket = 512;
2512 }
2513 for (n = 17; n < 32; n++) {
2514 struct msm_endpoint *ept = ui->ept + n;
2515 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2516 ept->ep.maxpacket = 512;
2517 }
2518
2519 retval = device_add(&ui->gadget.dev);
2520 if (retval)
2521 goto fail;
2522
2523 retval = bind(&ui->gadget);
2524 if (retval) {
2525 dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
2526 driver->driver.name, retval);
2527 device_del(&ui->gadget.dev);
2528 goto fail;
2529 }
2530
2531 retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
2532 if (retval != 0)
2533 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2534 "(wakeup) error: (%d)\n", retval);
2535 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
2536 if (retval != 0)
2537 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2538 " (usb_state) error: (%d)\n", retval);
2539
2540 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
2541 if (retval != 0)
2542 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2543 " (usb_speed) error: (%d)\n", retval);
2544
2545 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
2546 if (retval != 0)
2547 dev_err(&ui->pdev->dev,
2548 "failed to create sysfs entry(chg_type): err:(%d)\n",
2549 retval);
2550 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
2551 if (retval != 0)
2552 dev_err(&ui->pdev->dev,
2553 "failed to create sysfs entry(chg_current):"
2554 "err:(%d)\n", retval);
2555
2556 dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
2557 driver->driver.name);
2558 usb_start(ui);
2559
2560 return 0;
2561
2562fail:
2563 ui->driver = NULL;
2564 ui->gadget.dev.driver = NULL;
2565 return retval;
2566}
2567EXPORT_SYMBOL(usb_gadget_probe_driver);
2568
2569int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2570{
2571 struct usb_info *dev = the_usb_info;
2572
2573 if (!dev)
2574 return -ENODEV;
2575 if (!driver || driver != dev->driver || !driver->unbind)
2576 return -EINVAL;
2577
2578 msm72k_pullup_internal(&dev->gadget, 0);
2579 if (dev->irq) {
2580 free_irq(dev->irq, dev);
2581 dev->irq = 0;
2582 }
2583 dev->state = USB_STATE_IDLE;
2584 atomic_set(&dev->configured, 0);
2585 switch_set_state(&dev->sdev, 0);
2586 /* cancel pending ep0 transactions */
2587 flush_endpoint(&dev->ep0out);
2588 flush_endpoint(&dev->ep0in);
2589
2590 device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
2591 device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
2592 device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
2593 device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
2594 device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
2595 driver->disconnect(&dev->gadget);
2596 driver->unbind(&dev->gadget);
2597 dev->gadget.dev.driver = NULL;
2598 dev->driver = NULL;
2599
2600 device_del(&dev->gadget.dev);
2601
2602 dev_dbg(&dev->pdev->dev,
2603 "unregistered gadget driver '%s'\n", driver->driver.name);
2604 return 0;
2605}
2606EXPORT_SYMBOL(usb_gadget_unregister_driver);
2607
2608
2609static int msm72k_udc_runtime_suspend(struct device *dev)
2610{
2611 dev_dbg(dev, "pm_runtime: suspending...\n");
2612 return 0;
2613}
2614
2615static int msm72k_udc_runtime_resume(struct device *dev)
2616{
2617 dev_dbg(dev, "pm_runtime: resuming...\n");
2618 return 0;
2619}
2620
2621static int msm72k_udc_runtime_idle(struct device *dev)
2622{
2623 dev_dbg(dev, "pm_runtime: idling...\n");
2624 return 0;
2625}
2626
2627static struct dev_pm_ops msm72k_udc_dev_pm_ops = {
2628 .runtime_suspend = msm72k_udc_runtime_suspend,
2629 .runtime_resume = msm72k_udc_runtime_resume,
2630 .runtime_idle = msm72k_udc_runtime_idle
2631};
2632
2633static struct platform_driver usb_driver = {
2634 .probe = msm72k_probe,
2635 .driver = { .name = "msm_hsusb",
2636 .pm = &msm72k_udc_dev_pm_ops, },
2637};
2638
2639static int __init init(void)
2640{
2641 return platform_driver_register(&usb_driver);
2642}
2643module_init(init);
2644
2645static void __exit cleanup(void)
2646{
2647 platform_driver_unregister(&usb_driver);
2648}
2649module_exit(cleanup);
2650
2651MODULE_DESCRIPTION(DRIVER_DESC);
2652MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
2653MODULE_LICENSE("GPL");