blob: e0fc12c055befba324b2c219c0531894b5fc66a3 [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.
Chiranjeevi Velempatiff7f5a52012-03-30 14:16:17 +05305 * Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 * 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;
1112 unsigned info;
1113
1114 /*
1115 INFO("handle_endpoint() %d %s req=%p(%08x)\n",
1116 ept->num, (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1117 ept->req, ept->req ? ept->req->item_dma : 0);
1118 */
1119
1120 /* expire all requests that are no longer active */
1121 spin_lock_irqsave(&ui->lock, flags);
1122 while ((req = ept->req)) {
1123 /* if we've processed all live requests, time to
1124 * restart the hardware on the next non-live request
1125 */
1126 if (!req->live) {
1127 usb_ept_start(ept);
1128 break;
1129 }
1130
1131 /* clean speculative fetches on req->item->info */
1132 dma_coherent_post_ops();
1133 info = req->item->info;
1134 /* if the transaction is still in-flight, stop here */
Chiranjeevi Velempatiff7f5a52012-03-30 14:16:17 +05301135 if (info & INFO_ACTIVE)
1136 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137
Vijayavardhan Vennapusafd8df252011-12-08 16:19:37 +05301138 del_timer(&ept->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139 /* advance ept queue to the next request */
1140 ept->req = req->next;
1141 if (ept->req == 0)
1142 ept->last = 0;
1143
1144 dma_unmap_single(NULL, req->dma, req->req.length,
1145 (ept->flags & EPT_FLAG_IN) ?
1146 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1147
1148 if (info & (INFO_HALTED | INFO_BUFFER_ERROR | INFO_TXN_ERROR)) {
1149 /* XXX pass on more specific error code */
1150 req->req.status = -EIO;
1151 req->req.actual = 0;
1152 dev_err(&ui->pdev->dev,
1153 "ept %d %s error. info=%08x\n",
1154 ept->num,
1155 (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1156 info);
1157 } else {
1158 req->req.status = 0;
1159 req->req.actual =
1160 req->req.length - ((info >> 16) & 0x7FFF);
1161 }
1162 req->busy = 0;
1163 req->live = 0;
1164
1165 if (req->req.complete) {
1166 spin_unlock_irqrestore(&ui->lock, flags);
1167 req->req.complete(&ept->ep, &req->req);
1168 spin_lock_irqsave(&ui->lock, flags);
1169 }
1170 }
1171 spin_unlock_irqrestore(&ui->lock, flags);
1172}
1173
1174static void flush_endpoint_hw(struct usb_info *ui, unsigned bits)
1175{
1176 /* flush endpoint, canceling transactions
1177 ** - this can take a "large amount of time" (per databook)
1178 ** - the flush can fail in some cases, thus we check STAT
1179 ** and repeat if we're still operating
1180 ** (does the fact that this doesn't use the tripwire matter?!)
1181 */
1182 do {
1183 writel(bits, USB_ENDPTFLUSH);
1184 while (readl(USB_ENDPTFLUSH) & bits)
1185 udelay(100);
1186 } while (readl(USB_ENDPTSTAT) & bits);
1187}
1188
1189static void flush_endpoint_sw(struct msm_endpoint *ept)
1190{
1191 struct usb_info *ui = ept->ui;
1192 struct msm_request *req, *next_req = NULL;
1193 unsigned long flags;
1194
1195 /* inactive endpoints have nothing to do here */
1196 if (ept->ep.maxpacket == 0)
1197 return;
1198
1199 /* put the queue head in a sane state */
1200 ept->head->info = 0;
1201 ept->head->next = TERMINATE;
1202
1203 /* cancel any pending requests */
1204 spin_lock_irqsave(&ui->lock, flags);
1205 req = ept->req;
1206 ept->req = 0;
1207 ept->last = 0;
1208 while (req != 0) {
1209 req->busy = 0;
1210 req->live = 0;
1211 req->req.status = -ESHUTDOWN;
1212 req->req.actual = 0;
1213
1214 /* Gadget driver may free the request in completion
1215 * handler. So keep a copy of next req pointer
1216 * before calling completion handler.
1217 */
1218 next_req = req->next;
1219 if (req->req.complete) {
1220 spin_unlock_irqrestore(&ui->lock, flags);
1221 req->req.complete(&ept->ep, &req->req);
1222 spin_lock_irqsave(&ui->lock, flags);
1223 }
1224 req = next_req;
1225 }
1226 spin_unlock_irqrestore(&ui->lock, flags);
1227}
1228
1229static void flush_endpoint(struct msm_endpoint *ept)
1230{
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301231 del_timer(&ept->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232 flush_endpoint_hw(ept->ui, (1 << ept->bit));
1233 flush_endpoint_sw(ept);
1234}
1235
1236static irqreturn_t usb_interrupt(int irq, void *data)
1237{
1238 struct usb_info *ui = data;
1239 unsigned n;
1240 unsigned long flags;
1241
1242 n = readl(USB_USBSTS);
1243 writel(n, USB_USBSTS);
1244
1245 /* somehow we got an IRQ while in the reset sequence: ignore it */
1246 if (!atomic_read(&ui->running))
1247 return IRQ_HANDLED;
1248
1249 if (n & STS_PCI) {
1250 msm_hsusb_set_speed(ui);
1251 if (atomic_read(&ui->configured)) {
1252 wake_lock(&ui->wlock);
1253
1254 spin_lock_irqsave(&ui->lock, flags);
1255 ui->usb_state = USB_STATE_CONFIGURED;
1256 ui->flags = USB_FLAG_CONFIGURED;
1257 spin_unlock_irqrestore(&ui->lock, flags);
1258
1259 ui->driver->resume(&ui->gadget);
1260 schedule_work(&ui->work);
1261 } else {
1262 msm_hsusb_set_state(USB_STATE_DEFAULT);
1263 }
1264
1265#ifdef CONFIG_USB_OTG
1266 /* notify otg to clear A_BIDL_ADIS timer */
1267 if (ui->gadget.is_a_peripheral)
1268 otg_set_suspend(ui->xceiv, 0);
1269#endif
1270 }
1271
1272 if (n & STS_URI) {
1273 dev_dbg(&ui->pdev->dev, "reset\n");
1274 spin_lock_irqsave(&ui->lock, flags);
1275 ui->gadget.speed = USB_SPEED_UNKNOWN;
1276 spin_unlock_irqrestore(&ui->lock, flags);
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 spin_lock_irqsave(&ui->lock, flags);
1282 /* Host request is persistent across reset */
1283 ui->gadget.b_hnp_enable = 0;
1284 ui->hnp_avail = 0;
1285 spin_unlock_irqrestore(&ui->lock, flags);
1286#endif
1287 msm_hsusb_set_state(USB_STATE_DEFAULT);
1288 atomic_set(&ui->remote_wakeup, 0);
1289 if (!ui->gadget.is_a_peripheral)
1290 schedule_delayed_work(&ui->chg_stop, 0);
1291
1292 writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
1293 writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
1294 writel(0xffffffff, USB_ENDPTFLUSH);
1295 writel(0, USB_ENDPTCTRL(1));
1296
1297 wake_lock(&ui->wlock);
1298 if (atomic_read(&ui->configured)) {
1299 /* marking us offline will cause ept queue attempts
1300 ** to fail
1301 */
1302 atomic_set(&ui->configured, 0);
1303 /* Defer sending offline uevent to userspace */
1304 atomic_set(&ui->offline_pending, 1);
1305
1306 /* XXX: we can't seem to detect going offline,
1307 * XXX: so deconfigure on reset for the time being
1308 */
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05301309 dev_dbg(&ui->pdev->dev,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310 "usb: notify offline\n");
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05301311 ui->driver->disconnect(&ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 /* cancel pending ep0 transactions */
1313 flush_endpoint(&ui->ep0out);
1314 flush_endpoint(&ui->ep0in);
1315
1316 }
1317 /* Start phy stuck timer */
1318 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1319 mod_timer(&phy_status_timer, PHY_STATUS_CHECK_DELAY);
1320 }
1321
1322 if (n & STS_SLI) {
1323 dev_dbg(&ui->pdev->dev, "suspend\n");
1324
1325 spin_lock_irqsave(&ui->lock, flags);
1326 ui->usb_state = USB_STATE_SUSPENDED;
1327 ui->flags = USB_FLAG_SUSPEND;
1328 spin_unlock_irqrestore(&ui->lock, flags);
1329
1330 ui->driver->suspend(&ui->gadget);
1331 schedule_work(&ui->work);
1332#ifdef CONFIG_USB_OTG
1333 /* notify otg for
1334 * 1. kicking A_BIDL_ADIS timer in case of A-peripheral
1335 * 2. disabling pull-up and kicking B_ASE0_RST timer
1336 */
1337 if (ui->gadget.b_hnp_enable || ui->gadget.is_a_peripheral)
1338 otg_set_suspend(ui->xceiv, 1);
1339#endif
1340 }
1341
1342 if (n & STS_UI) {
1343 n = readl(USB_ENDPTSETUPSTAT);
1344 if (n & EPT_RX(0))
1345 handle_setup(ui);
1346
1347 n = readl(USB_ENDPTCOMPLETE);
1348 writel(n, USB_ENDPTCOMPLETE);
1349 while (n) {
1350 unsigned bit = __ffs(n);
1351 handle_endpoint(ui, bit);
1352 n = n & (~(1 << bit));
1353 }
1354 }
1355 return IRQ_HANDLED;
1356}
1357
1358static void usb_prepare(struct usb_info *ui)
1359{
1360 spin_lock_init(&ui->lock);
1361
1362 memset(ui->buf, 0, 4096);
1363 ui->head = (void *) (ui->buf + 0);
1364
1365 /* only important for reset/reinit */
1366 memset(ui->ept, 0, sizeof(ui->ept));
1367 ui->next_item = 0;
1368 ui->next_ifc_num = 0;
1369
1370 init_endpoints(ui);
1371
1372 ui->ep0in.ep.maxpacket = 64;
1373 ui->ep0out.ep.maxpacket = 64;
1374
1375 ui->setup_req =
1376 usb_ept_alloc_req(&ui->ep0in, SETUP_BUF_SIZE, GFP_KERNEL);
1377
1378 INIT_WORK(&ui->work, usb_do_work);
1379 INIT_DELAYED_WORK(&ui->chg_det, usb_chg_detect);
1380 INIT_DELAYED_WORK(&ui->chg_stop, usb_chg_stop);
1381 INIT_DELAYED_WORK(&ui->rw_work, usb_do_remote_wakeup);
1382 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1383 INIT_WORK(&ui->phy_status_check, usb_phy_stuck_recover);
1384}
1385
1386static void usb_reset(struct usb_info *ui)
1387{
1388 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1389
1390 dev_dbg(&ui->pdev->dev, "reset controller\n");
1391
1392 atomic_set(&ui->running, 0);
1393
1394 /*
1395 * PHY reset takes minimum 100 msec. Hence reset only link
1396 * during HNP. Reset PHY and link in B-peripheral mode.
1397 */
1398 if (ui->gadget.is_a_peripheral)
1399 otg->reset(ui->xceiv, 0);
1400 else
1401 otg->reset(ui->xceiv, 1);
1402
1403 /* set usb controller interrupt threshold to zero*/
1404 writel((readl(USB_USBCMD) & ~USBCMD_ITC_MASK) | USBCMD_ITC(0),
1405 USB_USBCMD);
1406
1407 writel(ui->dma, USB_ENDPOINTLISTADDR);
1408
1409 configure_endpoints(ui);
1410
1411 /* marking us offline will cause ept queue attempts to fail */
1412 atomic_set(&ui->configured, 0);
1413
1414 if (ui->driver) {
1415 dev_dbg(&ui->pdev->dev, "usb: notify offline\n");
1416 ui->driver->disconnect(&ui->gadget);
1417 }
1418
1419 /* cancel pending ep0 transactions */
1420 flush_endpoint(&ui->ep0out);
1421 flush_endpoint(&ui->ep0in);
1422
1423 /* enable interrupts */
1424 writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR);
1425
1426 /* Ensure that h/w RESET is completed before returning */
1427 mb();
1428
1429 atomic_set(&ui->running, 1);
1430}
1431
1432static void usb_start(struct usb_info *ui)
1433{
1434 unsigned long flags;
1435
1436 spin_lock_irqsave(&ui->lock, flags);
1437 ui->flags |= USB_FLAG_START;
1438 schedule_work(&ui->work);
1439 spin_unlock_irqrestore(&ui->lock, flags);
1440}
1441
1442static int usb_free(struct usb_info *ui, int ret)
1443{
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03001444 if (ret)
1445 dev_dbg(&ui->pdev->dev, "usb_free(%d)\n", ret);
1446
1447 usb_del_gadget_udc(&ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448
1449 if (ui->xceiv)
1450 otg_put_transceiver(ui->xceiv);
1451
1452 if (ui->irq)
1453 free_irq(ui->irq, 0);
1454 if (ui->pool)
1455 dma_pool_destroy(ui->pool);
1456 if (ui->dma)
1457 dma_free_coherent(&ui->pdev->dev, 4096, ui->buf, ui->dma);
1458 kfree(ui);
1459 return ret;
1460}
1461
1462static void usb_do_work_check_vbus(struct usb_info *ui)
1463{
1464 unsigned long iflags;
1465
1466 spin_lock_irqsave(&ui->lock, iflags);
1467 if (is_usb_online(ui))
1468 ui->flags |= USB_FLAG_VBUS_ONLINE;
1469 else
1470 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1471 spin_unlock_irqrestore(&ui->lock, iflags);
1472}
1473
1474static void usb_do_work(struct work_struct *w)
1475{
1476 struct usb_info *ui = container_of(w, struct usb_info, work);
1477 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1478 unsigned long iflags;
1479 unsigned flags, _vbus;
1480
1481 for (;;) {
1482 spin_lock_irqsave(&ui->lock, iflags);
1483 flags = ui->flags;
1484 ui->flags = 0;
1485 _vbus = is_usb_online(ui);
1486 spin_unlock_irqrestore(&ui->lock, iflags);
1487
1488 /* give up if we have nothing to do */
1489 if (flags == 0)
1490 break;
1491
1492 switch (ui->state) {
1493 case USB_STATE_IDLE:
1494 if (flags & USB_FLAG_START) {
1495 int ret;
1496
1497 if (!_vbus) {
1498 ui->state = USB_STATE_OFFLINE;
1499 break;
1500 }
1501
1502 pm_runtime_get_noresume(&ui->pdev->dev);
1503 pm_runtime_resume(&ui->pdev->dev);
1504 dev_dbg(&ui->pdev->dev,
1505 "msm72k_udc: IDLE -> ONLINE\n");
1506 usb_reset(ui);
1507 ret = request_irq(otg->irq, usb_interrupt,
1508 IRQF_SHARED,
1509 ui->pdev->name, ui);
1510 /* FIXME: should we call BUG_ON when
1511 * requst irq fails
1512 */
1513 if (ret) {
1514 dev_err(&ui->pdev->dev,
1515 "hsusb: peripheral: request irq"
1516 " failed:(%d)", ret);
1517 break;
1518 }
1519 ui->irq = otg->irq;
1520 ui->state = USB_STATE_ONLINE;
1521 usb_do_work_check_vbus(ui);
1522
1523 if (!atomic_read(&ui->softconnect))
1524 break;
1525
1526 msm72k_pullup_internal(&ui->gadget, 1);
1527
1528 if (!ui->gadget.is_a_peripheral)
1529 schedule_delayed_work(
1530 &ui->chg_det,
1531 USB_CHG_DET_DELAY);
1532
1533 }
1534 break;
1535 case USB_STATE_ONLINE:
1536 if (atomic_read(&ui->offline_pending)) {
1537 switch_set_state(&ui->sdev, 0);
1538 atomic_set(&ui->offline_pending, 0);
1539 }
1540
1541 /* If at any point when we were online, we received
1542 * the signal to go offline, we must honor it
1543 */
1544 if (flags & USB_FLAG_VBUS_OFFLINE) {
1545
1546 ui->chg_current = 0;
1547 /* wait incase chg_detect is running */
1548 if (!ui->gadget.is_a_peripheral)
1549 cancel_delayed_work_sync(&ui->chg_det);
1550
1551 dev_dbg(&ui->pdev->dev,
1552 "msm72k_udc: ONLINE -> OFFLINE\n");
1553
1554 atomic_set(&ui->running, 0);
1555 atomic_set(&ui->remote_wakeup, 0);
1556 atomic_set(&ui->configured, 0);
1557
1558 if (ui->driver) {
1559 dev_dbg(&ui->pdev->dev,
1560 "usb: notify offline\n");
1561 ui->driver->disconnect(&ui->gadget);
1562 }
1563 /* cancel pending ep0 transactions */
1564 flush_endpoint(&ui->ep0out);
1565 flush_endpoint(&ui->ep0in);
1566
1567 /* synchronize with irq context */
1568 spin_lock_irqsave(&ui->lock, iflags);
1569#ifdef CONFIG_USB_OTG
1570 ui->gadget.host_request = 0;
1571 ui->gadget.b_hnp_enable = 0;
1572 ui->hnp_avail = 0;
1573#endif
1574 msm72k_pullup_internal(&ui->gadget, 0);
1575 spin_unlock_irqrestore(&ui->lock, iflags);
1576
1577
1578 /* if charger is initialized to known type
1579 * we must let modem know about charger
1580 * disconnection
1581 */
1582 otg_set_power(ui->xceiv, 0);
1583
1584 if (ui->irq) {
1585 free_irq(ui->irq, ui);
1586 ui->irq = 0;
1587 }
1588
1589
1590 switch_set_state(&ui->sdev, 0);
1591
1592 ui->state = USB_STATE_OFFLINE;
1593 usb_do_work_check_vbus(ui);
1594 pm_runtime_put_noidle(&ui->pdev->dev);
1595 pm_runtime_suspend(&ui->pdev->dev);
1596 wake_unlock(&ui->wlock);
1597 break;
1598 }
1599 if (flags & USB_FLAG_SUSPEND) {
1600 int maxpower = usb_get_max_power(ui);
1601
1602 if (maxpower < 0)
1603 break;
1604
1605 otg_set_power(ui->xceiv, 0);
1606 /* To support TCXO during bus suspend
1607 * This might be dummy check since bus suspend
1608 * is not implemented as of now
1609 * */
1610 if (release_wlocks)
1611 wake_unlock(&ui->wlock);
1612
1613 /* TBD: Initiate LPM at usb bus suspend */
1614 break;
1615 }
1616 if (flags & USB_FLAG_CONFIGURED) {
1617 int maxpower = usb_get_max_power(ui);
1618
1619 /* We may come here even when no configuration
1620 * is selected. Send online/offline event
1621 * accordingly.
1622 */
1623 switch_set_state(&ui->sdev,
1624 atomic_read(&ui->configured));
1625
1626 if (maxpower < 0)
1627 break;
1628
1629 ui->chg_current = maxpower;
1630 otg_set_power(ui->xceiv, maxpower);
1631 break;
1632 }
1633 if (flags & USB_FLAG_RESET) {
1634 dev_dbg(&ui->pdev->dev,
1635 "msm72k_udc: ONLINE -> RESET\n");
1636 msm72k_pullup_internal(&ui->gadget, 0);
1637 usb_reset(ui);
1638 msm72k_pullup_internal(&ui->gadget, 1);
1639 dev_dbg(&ui->pdev->dev,
1640 "msm72k_udc: RESET -> ONLINE\n");
1641 break;
1642 }
1643 break;
1644 case USB_STATE_OFFLINE:
1645 /* If we were signaled to go online and vbus is still
1646 * present when we received the signal, go online.
1647 */
1648 if ((flags & USB_FLAG_VBUS_ONLINE) && _vbus) {
1649 int ret;
1650
1651 pm_runtime_get_noresume(&ui->pdev->dev);
1652 pm_runtime_resume(&ui->pdev->dev);
1653 dev_dbg(&ui->pdev->dev,
1654 "msm72k_udc: OFFLINE -> ONLINE\n");
1655
1656 usb_reset(ui);
1657 ui->state = USB_STATE_ONLINE;
1658 usb_do_work_check_vbus(ui);
1659 ret = request_irq(otg->irq, usb_interrupt,
1660 IRQF_SHARED,
1661 ui->pdev->name, ui);
1662 /* FIXME: should we call BUG_ON when
1663 * requst irq fails
1664 */
1665 if (ret) {
1666 dev_err(&ui->pdev->dev,
1667 "hsusb: peripheral: request irq"
1668 " failed:(%d)", ret);
1669 break;
1670 }
1671 ui->irq = otg->irq;
1672 enable_irq_wake(otg->irq);
1673
1674 if (!atomic_read(&ui->softconnect))
1675 break;
1676 msm72k_pullup_internal(&ui->gadget, 1);
1677
1678 if (!ui->gadget.is_a_peripheral)
1679 schedule_delayed_work(
1680 &ui->chg_det,
1681 USB_CHG_DET_DELAY);
1682 }
1683 break;
1684 }
1685 }
1686}
1687
1688/* FIXME - the callers of this function should use a gadget API instead.
1689 * This is called from htc_battery.c and board-halibut.c
1690 * WARNING - this can get called before this driver is initialized.
1691 */
1692void msm_hsusb_set_vbus_state(int online)
1693{
1694 unsigned long flags;
1695 struct usb_info *ui = the_usb_info;
1696
1697 if (!ui) {
1698 pr_err("%s called before driver initialized\n", __func__);
1699 return;
1700 }
1701
1702 spin_lock_irqsave(&ui->lock, flags);
1703
1704 if (is_usb_online(ui) == online)
1705 goto out;
1706
1707 if (online) {
1708 ui->usb_state = USB_STATE_POWERED;
1709 ui->flags |= USB_FLAG_VBUS_ONLINE;
1710 } else {
1711 ui->gadget.speed = USB_SPEED_UNKNOWN;
1712 ui->usb_state = USB_STATE_NOTATTACHED;
1713 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1714 }
1715 if (in_interrupt()) {
1716 schedule_work(&ui->work);
1717 } else {
1718 spin_unlock_irqrestore(&ui->lock, flags);
1719 usb_do_work(&ui->work);
1720 return;
1721 }
1722out:
1723 spin_unlock_irqrestore(&ui->lock, flags);
1724}
1725
1726#if defined(CONFIG_DEBUG_FS)
1727
1728void usb_function_reenumerate(void)
1729{
1730 struct usb_info *ui = the_usb_info;
1731
1732 /* disable and re-enable the D+ pullup */
1733 dev_dbg(&ui->pdev->dev, "disable pullup\n");
1734 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
1735
1736 msleep(10);
1737
1738 dev_dbg(&ui->pdev->dev, "enable pullup\n");
1739 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
1740}
1741
1742static char debug_buffer[PAGE_SIZE];
1743
1744static ssize_t debug_read_status(struct file *file, char __user *ubuf,
1745 size_t count, loff_t *ppos)
1746{
1747 struct usb_info *ui = file->private_data;
1748 char *buf = debug_buffer;
1749 unsigned long flags;
1750 struct msm_endpoint *ept;
1751 struct msm_request *req;
1752 int n;
1753 int i = 0;
1754
1755 spin_lock_irqsave(&ui->lock, flags);
1756
1757 i += scnprintf(buf + i, PAGE_SIZE - i,
1758 "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
1759 readl(USB_ENDPTSETUPSTAT),
1760 readl(USB_ENDPTPRIME),
1761 readl(USB_ENDPTSTAT),
1762 readl(USB_ENDPTCOMPLETE));
1763 i += scnprintf(buf + i, PAGE_SIZE - i,
1764 "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
1765 readl(USB_USBCMD),
1766 readl(USB_USBSTS),
1767 readl(USB_USBINTR),
1768 readl(USB_PORTSC));
1769
1770
1771 for (n = 0; n < 32; n++) {
1772 ept = ui->ept + n;
1773 if (ept->ep.maxpacket == 0)
1774 continue;
1775
1776 i += scnprintf(buf + i, PAGE_SIZE - i,
1777 "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
1778 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1779 ept->head->config, ept->head->active,
1780 ept->head->next, ept->head->info);
1781
1782 for (req = ept->req; req; req = req->next)
1783 i += scnprintf(buf + i, PAGE_SIZE - i,
1784 " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
1785 req->item_dma, req->item->next,
1786 req->item->info, req->item->page0,
1787 req->busy ? 'B' : ' ',
1788 req->live ? 'L' : ' ');
1789 }
1790
1791 i += scnprintf(buf + i, PAGE_SIZE - i,
1792 "phy failure count: %d\n", ui->phy_fail_count);
1793
1794 spin_unlock_irqrestore(&ui->lock, flags);
1795
1796 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
1797}
1798
1799static ssize_t debug_write_reset(struct file *file, const char __user *buf,
1800 size_t count, loff_t *ppos)
1801{
1802 struct usb_info *ui = file->private_data;
1803 unsigned long flags;
1804
1805 spin_lock_irqsave(&ui->lock, flags);
1806 ui->flags |= USB_FLAG_RESET;
1807 schedule_work(&ui->work);
1808 spin_unlock_irqrestore(&ui->lock, flags);
1809
1810 return count;
1811}
1812
1813static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
1814 size_t count, loff_t *ppos)
1815{
1816 usb_function_reenumerate();
1817 return count;
1818}
1819
1820static int debug_open(struct inode *inode, struct file *file)
1821{
1822 file->private_data = inode->i_private;
1823 return 0;
1824}
1825
1826const struct file_operations debug_stat_ops = {
1827 .open = debug_open,
1828 .read = debug_read_status,
1829};
1830
1831const struct file_operations debug_reset_ops = {
1832 .open = debug_open,
1833 .write = debug_write_reset,
1834};
1835
1836const struct file_operations debug_cycle_ops = {
1837 .open = debug_open,
1838 .write = debug_write_cycle,
1839};
1840
1841static ssize_t debug_read_release_wlocks(struct file *file, char __user *ubuf,
1842 size_t count, loff_t *ppos)
1843{
1844 char kbuf[10];
1845 size_t c = 0;
1846
1847 memset(kbuf, 0, 10);
1848
1849 c = scnprintf(kbuf, 10, "%d", release_wlocks);
1850
1851 if (copy_to_user(ubuf, kbuf, c))
1852 return -EFAULT;
1853
1854 return c;
1855}
1856static ssize_t debug_write_release_wlocks(struct file *file,
1857 const char __user *buf, size_t count, loff_t *ppos)
1858{
1859 char kbuf[10];
1860 long temp;
1861
1862 memset(kbuf, 0, 10);
1863
1864 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1865 return -EFAULT;
1866
1867 if (strict_strtol(kbuf, 10, &temp))
1868 return -EINVAL;
1869
1870 if (temp)
1871 release_wlocks = 1;
1872 else
1873 release_wlocks = 0;
1874
1875 return count;
1876}
1877static int debug_wake_lock_open(struct inode *inode, struct file *file)
1878{
1879 file->private_data = inode->i_private;
1880 return 0;
1881}
1882const struct file_operations debug_wlocks_ops = {
1883 .open = debug_wake_lock_open,
1884 .read = debug_read_release_wlocks,
1885 .write = debug_write_release_wlocks,
1886};
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301887
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301888static ssize_t debug_reprime_ep(struct file *file, const char __user *ubuf,
1889 size_t count, loff_t *ppos)
1890{
1891 struct usb_info *ui = file->private_data;
1892 struct msm_endpoint *ept;
1893 char kbuf[10];
1894 unsigned int ep_num, dir;
1895 unsigned long flags;
1896 unsigned n, i;
1897
1898 memset(kbuf, 0, 10);
1899
1900 if (copy_from_user(kbuf, ubuf, count > 10 ? 10 : count))
1901 return -EFAULT;
1902
1903 if (sscanf(kbuf, "%u %u", &ep_num, &dir) != 2)
1904 return -EINVAL;
1905
1906 if (dir)
1907 i = ep_num + 16;
1908 else
1909 i = ep_num;
1910
1911 spin_lock_irqsave(&ui->lock, flags);
1912 ept = ui->ept + i;
1913 n = 1 << ept->bit;
1914
1915 if ((readl_relaxed(USB_ENDPTPRIME) & n))
1916 goto out;
1917
1918 if (readl_relaxed(USB_ENDPTSTAT) & n)
1919 goto out;
1920
1921 /* clear speculative loads on item->info */
1922 rmb();
1923 if (ept->req && (ept->req->item->info & INFO_ACTIVE)) {
1924 pr_err("%s(): ept%d%s prime failed. ept: config: %x"
1925 "active: %x next: %x info: %x\n",
1926 __func__, ept->num,
1927 ept->flags & EPT_FLAG_IN ? "in" : "out",
1928 ept->head->config, ept->head->active,
1929 ept->head->next, ept->head->info);
1930 writel_relaxed(n, USB_ENDPTPRIME);
1931 }
1932out:
1933 spin_unlock_irqrestore(&ui->lock, flags);
1934
1935 return count;
1936}
1937
1938static char buffer[512];
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301939static ssize_t debug_prime_fail_read(struct file *file, char __user *ubuf,
1940 size_t count, loff_t *ppos)
1941{
1942 struct usb_info *ui = file->private_data;
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301943 char *buf = buffer;
1944 unsigned long flags;
1945 struct msm_endpoint *ept;
1946 int n;
1947 int i = 0;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301948
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301949 spin_lock_irqsave(&ui->lock, flags);
1950 for (n = 0; n < 32; n++) {
1951 ept = ui->ept + n;
1952 if (ept->ep.maxpacket == 0)
1953 continue;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301954
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301955 i += scnprintf(buf + i, PAGE_SIZE - i,
1956 "ept%d %s false_prime_count=%lu prime_fail_count=%d dtd_fail_count=%lu\n",
1957 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1958 ept->false_prime_fail_count,
1959 ept->actual_prime_fail_count,
1960 ept->dTD_update_fail_count);
1961 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301962
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301963 i += scnprintf(buf + i, PAGE_SIZE - i,
1964 "dTD_update_fail count: %lu\n",
1965 ui->dTD_update_fail_count);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301966
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301967 i += scnprintf(buf + i, PAGE_SIZE - i,
1968 "prime_fail count: %d\n", ui->prime_fail_count);
1969
1970 spin_unlock_irqrestore(&ui->lock, flags);
1971
1972 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301973}
1974
1975static int debug_prime_fail_open(struct inode *inode, struct file *file)
1976{
1977 file->private_data = inode->i_private;
1978 return 0;
1979}
1980
1981const struct file_operations prime_fail_ops = {
1982 .open = debug_prime_fail_open,
1983 .read = debug_prime_fail_read,
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301984 .write = debug_reprime_ep,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301985};
1986
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001987static void usb_debugfs_init(struct usb_info *ui)
1988{
1989 struct dentry *dent;
1990 dent = debugfs_create_dir(dev_name(&ui->pdev->dev), 0);
1991 if (IS_ERR(dent))
1992 return;
1993
1994 debugfs_create_file("status", 0444, dent, ui, &debug_stat_ops);
1995 debugfs_create_file("reset", 0222, dent, ui, &debug_reset_ops);
1996 debugfs_create_file("cycle", 0222, dent, ui, &debug_cycle_ops);
1997 debugfs_create_file("release_wlocks", 0666, dent, ui,
1998 &debug_wlocks_ops);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301999 debugfs_create_file("prime_fail_countt", 0666, dent, ui,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302000 &prime_fail_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002001}
2002#else
2003static void usb_debugfs_init(struct usb_info *ui) {}
2004#endif
2005
2006static int
2007msm72k_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
2008{
2009 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2010 unsigned char ep_type =
2011 desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2012
2013 _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2014 config_ept(ept);
2015 ept->wedged = 0;
2016 usb_ept_enable(ept, 1, ep_type);
2017 return 0;
2018}
2019
2020static int msm72k_disable(struct usb_ep *_ep)
2021{
2022 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2023
2024 usb_ept_enable(ept, 0, 0);
2025 flush_endpoint(ept);
2026 return 0;
2027}
2028
2029static struct usb_request *
2030msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
2031{
2032 return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
2033}
2034
2035static void
2036msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
2037{
2038 struct msm_request *req = to_msm_request(_req);
2039 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2040 struct usb_info *ui = ept->ui;
2041
2042 /* request should not be busy */
2043 BUG_ON(req->busy);
2044 if (req->alloced)
2045 kfree(req->req.buf);
2046 dma_pool_free(ui->pool, req->item, req->item_dma);
2047 kfree(req);
2048}
2049
2050static int
2051msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
2052{
2053 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2054 struct usb_info *ui = ep->ui;
2055
2056 if (ep == &ui->ep0in) {
2057 struct msm_request *r = to_msm_request(req);
2058 if (!req->length)
2059 goto ep_queue_done;
2060 r->gadget_complete = req->complete;
2061 /* ep0_queue_ack_complete queue a receive for ACK before
2062 ** calling req->complete
2063 */
2064 req->complete = ep0_queue_ack_complete;
2065 if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
2066 ep = &ui->ep0out;
2067 goto ep_queue_done;
2068 }
2069
2070ep_queue_done:
2071 return usb_ept_queue_xfer(ep, req);
2072}
2073
2074static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2075{
2076 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2077 struct msm_request *req = to_msm_request(_req);
2078 struct usb_info *ui = ep->ui;
2079
2080 struct msm_request *temp_req;
2081 unsigned long flags;
2082
2083 if (!(ui && req && ep->req))
2084 return -EINVAL;
2085
2086 spin_lock_irqsave(&ui->lock, flags);
2087 if (!req->busy) {
2088 dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
2089 spin_unlock_irqrestore(&ui->lock, flags);
2090 return -EINVAL;
2091 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302092 del_timer(&ep->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093 /* Stop the transfer */
2094 do {
2095 writel((1 << ep->bit), USB_ENDPTFLUSH);
2096 while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
2097 udelay(100);
2098 } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
2099
2100 req->req.status = 0;
2101 req->busy = 0;
2102
2103 if (ep->req == req) {
2104 ep->req = req->next;
2105 ep->head->next = req->item->next;
2106 } else {
2107 req->prev->next = req->next;
2108 if (req->next)
2109 req->next->prev = req->prev;
2110 req->prev->item->next = req->item->next;
2111 }
2112
2113 if (!req->next)
2114 ep->last = req->prev;
2115
2116 /* initialize request to default */
2117 req->item->next = TERMINATE;
2118 req->item->info = 0;
2119 req->live = 0;
2120 dma_unmap_single(NULL, req->dma, req->req.length,
2121 (ep->flags & EPT_FLAG_IN) ?
2122 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2123
2124 if (req->req.complete) {
2125 req->req.status = -ECONNRESET;
2126 spin_unlock_irqrestore(&ui->lock, flags);
2127 req->req.complete(&ep->ep, &req->req);
2128 spin_lock_irqsave(&ui->lock, flags);
2129 }
2130
2131 if (!req->live) {
2132 /* Reprime the endpoint for the remaining transfers */
2133 for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
2134 temp_req->live = 0;
2135 if (ep->req)
2136 usb_ept_start(ep);
2137 spin_unlock_irqrestore(&ui->lock, flags);
2138 return 0;
2139 }
2140 spin_unlock_irqrestore(&ui->lock, flags);
2141 return 0;
2142}
2143
2144static int
2145usb_ept_set_halt(struct usb_ep *_ep, int value)
2146{
2147 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2148 struct usb_info *ui = ept->ui;
2149 unsigned int in = ept->flags & EPT_FLAG_IN;
2150 unsigned int n;
2151 unsigned long flags;
2152
2153 spin_lock_irqsave(&ui->lock, flags);
2154
2155 n = readl(USB_ENDPTCTRL(ept->num));
2156
2157 if (in) {
2158 if (value)
2159 n |= CTRL_TXS;
2160 else {
2161 n &= ~CTRL_TXS;
2162 n |= CTRL_TXR;
2163 }
2164 } else {
2165 if (value)
2166 n |= CTRL_RXS;
2167 else {
2168 n &= ~CTRL_RXS;
2169 n |= CTRL_RXR;
2170 }
2171 }
2172 writel(n, USB_ENDPTCTRL(ept->num));
2173 if (!value)
2174 ept->wedged = 0;
2175 spin_unlock_irqrestore(&ui->lock, flags);
2176
2177 return 0;
2178}
2179
2180static int
2181msm72k_set_halt(struct usb_ep *_ep, int value)
2182{
2183 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2184 unsigned int in = ept->flags & EPT_FLAG_IN;
2185
2186 if (value && in && ept->req)
2187 return -EAGAIN;
2188
2189 usb_ept_set_halt(_ep, value);
2190
2191 return 0;
2192}
2193
2194static int
2195msm72k_fifo_status(struct usb_ep *_ep)
2196{
2197 return -EOPNOTSUPP;
2198}
2199
2200static void
2201msm72k_fifo_flush(struct usb_ep *_ep)
2202{
2203 flush_endpoint(to_msm_endpoint(_ep));
2204}
2205static int msm72k_set_wedge(struct usb_ep *_ep)
2206{
2207 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2208
2209 if (ept->num == 0)
2210 return -EINVAL;
2211
2212 ept->wedged = 1;
2213
2214 return msm72k_set_halt(_ep, 1);
2215}
2216
2217static const struct usb_ep_ops msm72k_ep_ops = {
2218 .enable = msm72k_enable,
2219 .disable = msm72k_disable,
2220
2221 .alloc_request = msm72k_alloc_request,
2222 .free_request = msm72k_free_request,
2223
2224 .queue = msm72k_queue,
2225 .dequeue = msm72k_dequeue,
2226
2227 .set_halt = msm72k_set_halt,
2228 .set_wedge = msm72k_set_wedge,
2229 .fifo_status = msm72k_fifo_status,
2230 .fifo_flush = msm72k_fifo_flush,
2231};
2232
2233static int msm72k_get_frame(struct usb_gadget *_gadget)
2234{
2235 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2236
2237 /* frame number is in bits 13:3 */
2238 return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
2239}
2240
2241/* VBUS reporting logically comes from a transceiver */
2242static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
2243{
2244 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2245 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2246
2247 if (is_active || atomic_read(&otg->chg_type)
2248 == USB_CHG_TYPE__WALLCHARGER)
2249 wake_lock(&ui->wlock);
2250
2251 msm_hsusb_set_vbus_state(is_active);
2252 return 0;
2253}
2254
2255/* SW workarounds
2256Issue #1 - USB Spoof Disconnect Failure
2257Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2258SW workaround - Making opmode non-driving and SuspendM set in function
2259 register of SMSC phy
2260*/
2261/* drivers may have software control over D+ pullup */
2262static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
2263{
2264 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2265 unsigned long flags;
2266
2267 if (is_active) {
2268 spin_lock_irqsave(&ui->lock, flags);
2269 if (is_usb_online(ui) && ui->driver)
2270 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2271 spin_unlock_irqrestore(&ui->lock, flags);
2272 } else {
2273 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2274 /* S/W workaround, Issue#1 */
2275 otg_io_write(ui->xceiv, 0x48, 0x04);
2276 }
2277
2278 /* Ensure pull-up operation is completed before returning */
2279 mb();
2280
2281 return 0;
2282}
2283
2284static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
2285{
2286 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2287 unsigned long flags;
2288
2289
2290 atomic_set(&ui->softconnect, is_active);
2291
2292 spin_lock_irqsave(&ui->lock, flags);
2293 if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL) {
2294 spin_unlock_irqrestore(&ui->lock, flags);
2295 return 0;
2296 }
2297 spin_unlock_irqrestore(&ui->lock, flags);
2298
2299 msm72k_pullup_internal(_gadget, is_active);
2300
2301 if (is_active && !ui->gadget.is_a_peripheral)
2302 schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
2303
2304 return 0;
2305}
2306
2307static int msm72k_wakeup(struct usb_gadget *_gadget)
2308{
2309 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2310 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2311
2312 if (!atomic_read(&ui->remote_wakeup)) {
2313 dev_err(&ui->pdev->dev,
2314 "%s: remote wakeup not supported\n", __func__);
2315 return -ENOTSUPP;
2316 }
2317
2318 if (!atomic_read(&ui->configured)) {
2319 dev_err(&ui->pdev->dev,
2320 "%s: device is not configured\n", __func__);
2321 return -ENODEV;
2322 }
2323 otg_set_suspend(ui->xceiv, 0);
2324
2325 disable_irq(otg->irq);
2326
2327 if (!is_usb_active())
2328 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
2329
2330 /* Ensure that USB port is resumed before enabling the IRQ */
2331 mb();
2332
2333 enable_irq(otg->irq);
2334
2335 return 0;
2336}
2337
2338/* when Gadget is configured, it will indicate how much power
2339 * can be pulled from vbus, as specified in configuiration descriptor
2340 */
2341static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2342{
2343 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2344 unsigned long flags;
2345
2346
2347 spin_lock_irqsave(&ui->lock, flags);
2348 ui->b_max_pow = mA;
2349 ui->flags = USB_FLAG_CONFIGURED;
2350 spin_unlock_irqrestore(&ui->lock, flags);
2351
2352 schedule_work(&ui->work);
2353
2354 return 0;
2355}
2356
2357static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
2358{
2359 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2360 unsigned long flags;
2361 int ret = 0;
2362
2363 spin_lock_irqsave(&ui->lock, flags);
2364 if (set) {
2365 if (ui->pdata && ui->pdata->self_powered)
2366 atomic_set(&ui->self_powered, 1);
2367 else
2368 ret = -EOPNOTSUPP;
2369 } else {
2370 /* We can always work as a bus powered device */
2371 atomic_set(&ui->self_powered, 0);
2372 }
2373 spin_unlock_irqrestore(&ui->lock, flags);
2374
2375 return ret;
2376
2377}
2378
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002379static int msm72k_gadget_start(struct usb_gadget_driver *driver,
2380 int (*bind)(struct usb_gadget *));
2381static int msm72k_gadget_stop(struct usb_gadget_driver *driver);
2382
2383
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002384static const struct usb_gadget_ops msm72k_ops = {
2385 .get_frame = msm72k_get_frame,
2386 .vbus_session = msm72k_udc_vbus_session,
2387 .vbus_draw = msm72k_udc_vbus_draw,
2388 .pullup = msm72k_pullup,
2389 .wakeup = msm72k_wakeup,
2390 .set_selfpowered = msm72k_set_selfpowered,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002391 .start = msm72k_gadget_start,
2392 .stop = msm72k_gadget_stop
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002393};
2394
2395static void usb_do_remote_wakeup(struct work_struct *w)
2396{
2397 struct usb_info *ui = the_usb_info;
2398
2399 msm72k_wakeup(&ui->gadget);
2400}
2401
2402static ssize_t usb_remote_wakeup(struct device *dev,
2403 struct device_attribute *attr, const char *buf, size_t count)
2404{
2405 struct usb_info *ui = the_usb_info;
2406
2407 msm72k_wakeup(&ui->gadget);
2408
2409 return count;
2410}
2411
2412static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
2413 char *buf)
2414{
2415 size_t i;
2416 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
2417 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
2418 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
2419 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
2420 "USB_STATE_SUSPENDED"
2421 };
2422
2423 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
2424 return i;
2425}
2426
2427static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
2428 char *buf)
2429{
2430 struct usb_info *ui = the_usb_info;
2431 size_t i;
2432 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
2433 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
2434
2435 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
2436 return i;
2437}
2438
2439static ssize_t store_usb_chg_current(struct device *dev,
2440 struct device_attribute *attr, const char *buf, size_t count)
2441{
2442 struct usb_info *ui = the_usb_info;
2443 unsigned long mA;
2444
2445 if (ui->gadget.is_a_peripheral)
2446 return -EINVAL;
2447
2448 if (strict_strtoul(buf, 10, &mA))
2449 return -EINVAL;
2450
2451 ui->chg_current = mA;
2452 otg_set_power(ui->xceiv, mA);
2453
2454 return count;
2455}
2456
2457static ssize_t show_usb_chg_current(struct device *dev,
2458 struct device_attribute *attr, char *buf)
2459{
2460 struct usb_info *ui = the_usb_info;
2461 size_t count;
2462
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302463 count = snprintf(buf, PAGE_SIZE, "%d", ui->chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464
2465 return count;
2466}
2467
2468static ssize_t show_usb_chg_type(struct device *dev,
2469 struct device_attribute *attr, char *buf)
2470{
2471 struct usb_info *ui = the_usb_info;
2472 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2473 size_t count;
2474 char *chg_type[] = {"STD DOWNSTREAM PORT",
2475 "CARKIT",
2476 "DEDICATED CHARGER",
2477 "INVALID"};
2478
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302479 count = snprintf(buf, PAGE_SIZE, "%s",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002480 chg_type[atomic_read(&otg->chg_type)]);
2481
2482 return count;
2483}
2484static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
2485static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
2486static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
2487static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
2488static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
2489 show_usb_chg_current, store_usb_chg_current);
2490
2491#ifdef CONFIG_USB_OTG
2492static ssize_t store_host_req(struct device *dev,
2493 struct device_attribute *attr, const char *buf, size_t count)
2494{
2495 struct usb_info *ui = the_usb_info;
2496 unsigned long val, flags;
2497
2498 if (strict_strtoul(buf, 10, &val))
2499 return -EINVAL;
2500
2501 dev_dbg(&ui->pdev->dev, "%s host request\n",
2502 val ? "set" : "clear");
2503
2504 spin_lock_irqsave(&ui->lock, flags);
2505 if (ui->hnp_avail)
2506 ui->gadget.host_request = !!val;
2507 spin_unlock_irqrestore(&ui->lock, flags);
2508
2509 return count;
2510}
2511static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
2512
2513/* How do we notify user space about HNP availability?
2514 * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
2515 * Introduce hnp_avail flag and set when HNP polling request arrives.
2516 * The expectation is that user space checks hnp availability before
2517 * requesting host role via above sysfs node.
2518 */
2519static ssize_t show_host_avail(struct device *dev,
2520 struct device_attribute *attr, char *buf)
2521{
2522 struct usb_info *ui = the_usb_info;
2523 size_t count;
2524 unsigned long flags;
2525
2526 spin_lock_irqsave(&ui->lock, flags);
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302527 count = snprintf(buf, PAGE_SIZE, "%d\n", ui->hnp_avail);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528 spin_unlock_irqrestore(&ui->lock, flags);
2529
2530 return count;
2531}
2532static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
2533
2534static struct attribute *otg_attrs[] = {
2535 &dev_attr_host_request.attr,
2536 &dev_attr_host_avail.attr,
2537 NULL,
2538};
2539
2540static struct attribute_group otg_attr_grp = {
2541 .name = "otg",
2542 .attrs = otg_attrs,
2543};
2544#endif
2545
2546static int msm72k_probe(struct platform_device *pdev)
2547{
2548 struct usb_info *ui;
2549 struct msm_otg *otg;
2550 int retval;
2551
2552 dev_dbg(&pdev->dev, "msm72k_probe\n");
2553 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
2554 if (!ui)
2555 return -ENOMEM;
2556
2557 ui->pdev = pdev;
2558 ui->pdata = pdev->dev.platform_data;
2559
2560 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
2561 if (!ui->buf)
2562 return usb_free(ui, -ENOMEM);
2563
2564 ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
2565 if (!ui->pool)
2566 return usb_free(ui, -ENOMEM);
2567
2568 ui->xceiv = otg_get_transceiver();
2569 if (!ui->xceiv)
2570 return usb_free(ui, -ENODEV);
2571
2572 otg = to_msm_otg(ui->xceiv);
2573 ui->addr = otg->regs;
2574
2575 ui->gadget.ops = &msm72k_ops;
2576 ui->gadget.is_dualspeed = 1;
2577 device_initialize(&ui->gadget.dev);
2578 dev_set_name(&ui->gadget.dev, "gadget");
2579 ui->gadget.dev.parent = &pdev->dev;
2580 ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
2581
2582#ifdef CONFIG_USB_OTG
2583 ui->gadget.is_otg = 1;
2584#endif
2585
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002586 retval = usb_add_gadget_udc(&pdev->dev, &ui->gadget);
2587 if (retval)
2588 return usb_free(ui, retval);
2589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 ui->sdev.name = DRIVER_NAME;
2591 ui->sdev.print_name = print_switch_name;
2592 ui->sdev.print_state = print_switch_state;
2593
2594 retval = switch_dev_register(&ui->sdev);
2595 if (retval)
2596 return usb_free(ui, retval);
2597
2598 the_usb_info = ui;
2599
2600 wake_lock_init(&ui->wlock,
2601 WAKE_LOCK_SUSPEND, "usb_bus_active");
2602
2603 usb_debugfs_init(ui);
2604
2605 usb_prepare(ui);
2606
2607#ifdef CONFIG_USB_OTG
2608 retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
2609 if (retval) {
2610 dev_err(&ui->pdev->dev,
2611 "failed to create otg sysfs directory:"
2612 "err:(%d)\n", retval);
2613 }
2614#endif
2615
2616 retval = otg_set_peripheral(ui->xceiv, &ui->gadget);
2617 if (retval) {
2618 dev_err(&ui->pdev->dev,
2619 "%s: Cannot bind the transceiver, retval:(%d)\n",
2620 __func__, retval);
2621 switch_dev_unregister(&ui->sdev);
2622 wake_lock_destroy(&ui->wlock);
2623 return usb_free(ui, retval);
2624 }
2625
2626 pm_runtime_enable(&pdev->dev);
2627
2628 /* Setup phy stuck timer */
2629 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
2630 setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
2631 return 0;
2632}
2633
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002634static int msm72k_gadget_start(struct usb_gadget_driver *driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002635 int (*bind)(struct usb_gadget *))
2636{
2637 struct usb_info *ui = the_usb_info;
2638 int retval, n;
2639
2640 if (!driver
2641 || driver->speed < USB_SPEED_FULL
2642 || !bind
2643 || !driver->disconnect
2644 || !driver->setup)
2645 return -EINVAL;
2646 if (!ui)
2647 return -ENODEV;
2648 if (ui->driver)
2649 return -EBUSY;
2650
2651 /* first hook up the driver ... */
2652 ui->driver = driver;
2653 ui->gadget.dev.driver = &driver->driver;
2654 ui->gadget.name = driver_name;
2655 INIT_LIST_HEAD(&ui->gadget.ep_list);
2656 ui->gadget.ep0 = &ui->ep0in.ep;
2657 INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
2658 ui->gadget.speed = USB_SPEED_UNKNOWN;
2659 atomic_set(&ui->softconnect, 1);
2660
2661 for (n = 1; n < 16; n++) {
2662 struct msm_endpoint *ept = ui->ept + n;
2663 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2664 ept->ep.maxpacket = 512;
2665 }
2666 for (n = 17; n < 32; n++) {
2667 struct msm_endpoint *ept = ui->ept + n;
2668 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2669 ept->ep.maxpacket = 512;
2670 }
2671
2672 retval = device_add(&ui->gadget.dev);
2673 if (retval)
2674 goto fail;
2675
2676 retval = bind(&ui->gadget);
2677 if (retval) {
2678 dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
2679 driver->driver.name, retval);
2680 device_del(&ui->gadget.dev);
2681 goto fail;
2682 }
2683
2684 retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
2685 if (retval != 0)
2686 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2687 "(wakeup) error: (%d)\n", retval);
2688 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
2689 if (retval != 0)
2690 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2691 " (usb_state) error: (%d)\n", retval);
2692
2693 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
2694 if (retval != 0)
2695 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2696 " (usb_speed) error: (%d)\n", retval);
2697
2698 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
2699 if (retval != 0)
2700 dev_err(&ui->pdev->dev,
2701 "failed to create sysfs entry(chg_type): err:(%d)\n",
2702 retval);
2703 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
2704 if (retval != 0)
2705 dev_err(&ui->pdev->dev,
2706 "failed to create sysfs entry(chg_current):"
2707 "err:(%d)\n", retval);
2708
2709 dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
2710 driver->driver.name);
2711 usb_start(ui);
2712
2713 return 0;
2714
2715fail:
2716 ui->driver = NULL;
2717 ui->gadget.dev.driver = NULL;
2718 return retval;
2719}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002720
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002721static int msm72k_gadget_stop(struct usb_gadget_driver *driver)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002722{
2723 struct usb_info *dev = the_usb_info;
2724
2725 if (!dev)
2726 return -ENODEV;
2727 if (!driver || driver != dev->driver || !driver->unbind)
2728 return -EINVAL;
2729
2730 msm72k_pullup_internal(&dev->gadget, 0);
2731 if (dev->irq) {
2732 free_irq(dev->irq, dev);
2733 dev->irq = 0;
2734 }
2735 dev->state = USB_STATE_IDLE;
2736 atomic_set(&dev->configured, 0);
2737 switch_set_state(&dev->sdev, 0);
2738 /* cancel pending ep0 transactions */
2739 flush_endpoint(&dev->ep0out);
2740 flush_endpoint(&dev->ep0in);
2741
2742 device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
2743 device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
2744 device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
2745 device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
2746 device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
2747 driver->disconnect(&dev->gadget);
2748 driver->unbind(&dev->gadget);
2749 dev->gadget.dev.driver = NULL;
2750 dev->driver = NULL;
2751
2752 device_del(&dev->gadget.dev);
2753
2754 dev_dbg(&dev->pdev->dev,
2755 "unregistered gadget driver '%s'\n", driver->driver.name);
2756 return 0;
2757}
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
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002784static int __exit msm72k_remove(struct platform_device *pdev)
2785{
2786 struct usb_info *ui = container_of(&pdev, struct usb_info, pdev);
2787
2788 return usb_free(ui, 0);
2789}
2790
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002791static struct platform_driver usb_driver = {
2792 .probe = msm72k_probe,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002793 .remove = msm72k_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002794 .driver = { .name = "msm_hsusb",
2795 .pm = &msm72k_udc_dev_pm_ops, },
2796};
2797
2798static int __init init(void)
2799{
2800 return platform_driver_register(&usb_driver);
2801}
2802module_init(init);
2803
2804static void __exit cleanup(void)
2805{
2806 platform_driver_unregister(&usb_driver);
2807}
2808module_exit(cleanup);
2809
2810MODULE_DESCRIPTION(DRIVER_DESC);
2811MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
2812MODULE_LICENSE("GPL");