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