blob: 35ea49798f487bedc7dcb4d30525fdb4781aa70f [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;
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +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
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +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 */
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +05301137 if (info & INFO_ACTIVE) {
1138 if (req_dequeue) {
1139 req_dequeue = 0;
1140 ui->dTD_update_fail_count++;
1141 ept->dTD_update_fail_count++;
1142 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{
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03001456 if (ret)
1457 dev_dbg(&ui->pdev->dev, "usb_free(%d)\n", ret);
1458
1459 usb_del_gadget_udc(&ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460
1461 if (ui->xceiv)
1462 otg_put_transceiver(ui->xceiv);
1463
1464 if (ui->irq)
1465 free_irq(ui->irq, 0);
1466 if (ui->pool)
1467 dma_pool_destroy(ui->pool);
1468 if (ui->dma)
1469 dma_free_coherent(&ui->pdev->dev, 4096, ui->buf, ui->dma);
1470 kfree(ui);
1471 return ret;
1472}
1473
1474static void usb_do_work_check_vbus(struct usb_info *ui)
1475{
1476 unsigned long iflags;
1477
1478 spin_lock_irqsave(&ui->lock, iflags);
1479 if (is_usb_online(ui))
1480 ui->flags |= USB_FLAG_VBUS_ONLINE;
1481 else
1482 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1483 spin_unlock_irqrestore(&ui->lock, iflags);
1484}
1485
1486static void usb_do_work(struct work_struct *w)
1487{
1488 struct usb_info *ui = container_of(w, struct usb_info, work);
1489 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1490 unsigned long iflags;
1491 unsigned flags, _vbus;
1492
1493 for (;;) {
1494 spin_lock_irqsave(&ui->lock, iflags);
1495 flags = ui->flags;
1496 ui->flags = 0;
1497 _vbus = is_usb_online(ui);
1498 spin_unlock_irqrestore(&ui->lock, iflags);
1499
1500 /* give up if we have nothing to do */
1501 if (flags == 0)
1502 break;
1503
1504 switch (ui->state) {
1505 case USB_STATE_IDLE:
1506 if (flags & USB_FLAG_START) {
1507 int ret;
1508
1509 if (!_vbus) {
1510 ui->state = USB_STATE_OFFLINE;
1511 break;
1512 }
1513
1514 pm_runtime_get_noresume(&ui->pdev->dev);
1515 pm_runtime_resume(&ui->pdev->dev);
1516 dev_dbg(&ui->pdev->dev,
1517 "msm72k_udc: IDLE -> ONLINE\n");
1518 usb_reset(ui);
1519 ret = request_irq(otg->irq, usb_interrupt,
1520 IRQF_SHARED,
1521 ui->pdev->name, ui);
1522 /* FIXME: should we call BUG_ON when
1523 * requst irq fails
1524 */
1525 if (ret) {
1526 dev_err(&ui->pdev->dev,
1527 "hsusb: peripheral: request irq"
1528 " failed:(%d)", ret);
1529 break;
1530 }
1531 ui->irq = otg->irq;
1532 ui->state = USB_STATE_ONLINE;
1533 usb_do_work_check_vbus(ui);
1534
1535 if (!atomic_read(&ui->softconnect))
1536 break;
1537
1538 msm72k_pullup_internal(&ui->gadget, 1);
1539
1540 if (!ui->gadget.is_a_peripheral)
1541 schedule_delayed_work(
1542 &ui->chg_det,
1543 USB_CHG_DET_DELAY);
1544
1545 }
1546 break;
1547 case USB_STATE_ONLINE:
1548 if (atomic_read(&ui->offline_pending)) {
1549 switch_set_state(&ui->sdev, 0);
1550 atomic_set(&ui->offline_pending, 0);
1551 }
1552
1553 /* If at any point when we were online, we received
1554 * the signal to go offline, we must honor it
1555 */
1556 if (flags & USB_FLAG_VBUS_OFFLINE) {
1557
1558 ui->chg_current = 0;
1559 /* wait incase chg_detect is running */
1560 if (!ui->gadget.is_a_peripheral)
1561 cancel_delayed_work_sync(&ui->chg_det);
1562
1563 dev_dbg(&ui->pdev->dev,
1564 "msm72k_udc: ONLINE -> OFFLINE\n");
1565
1566 atomic_set(&ui->running, 0);
1567 atomic_set(&ui->remote_wakeup, 0);
1568 atomic_set(&ui->configured, 0);
1569
1570 if (ui->driver) {
1571 dev_dbg(&ui->pdev->dev,
1572 "usb: notify offline\n");
1573 ui->driver->disconnect(&ui->gadget);
1574 }
1575 /* cancel pending ep0 transactions */
1576 flush_endpoint(&ui->ep0out);
1577 flush_endpoint(&ui->ep0in);
1578
1579 /* synchronize with irq context */
1580 spin_lock_irqsave(&ui->lock, iflags);
1581#ifdef CONFIG_USB_OTG
1582 ui->gadget.host_request = 0;
1583 ui->gadget.b_hnp_enable = 0;
1584 ui->hnp_avail = 0;
1585#endif
1586 msm72k_pullup_internal(&ui->gadget, 0);
1587 spin_unlock_irqrestore(&ui->lock, iflags);
1588
1589
1590 /* if charger is initialized to known type
1591 * we must let modem know about charger
1592 * disconnection
1593 */
1594 otg_set_power(ui->xceiv, 0);
1595
1596 if (ui->irq) {
1597 free_irq(ui->irq, ui);
1598 ui->irq = 0;
1599 }
1600
1601
1602 switch_set_state(&ui->sdev, 0);
1603
1604 ui->state = USB_STATE_OFFLINE;
1605 usb_do_work_check_vbus(ui);
1606 pm_runtime_put_noidle(&ui->pdev->dev);
1607 pm_runtime_suspend(&ui->pdev->dev);
1608 wake_unlock(&ui->wlock);
1609 break;
1610 }
1611 if (flags & USB_FLAG_SUSPEND) {
1612 int maxpower = usb_get_max_power(ui);
1613
1614 if (maxpower < 0)
1615 break;
1616
1617 otg_set_power(ui->xceiv, 0);
1618 /* To support TCXO during bus suspend
1619 * This might be dummy check since bus suspend
1620 * is not implemented as of now
1621 * */
1622 if (release_wlocks)
1623 wake_unlock(&ui->wlock);
1624
1625 /* TBD: Initiate LPM at usb bus suspend */
1626 break;
1627 }
1628 if (flags & USB_FLAG_CONFIGURED) {
1629 int maxpower = usb_get_max_power(ui);
1630
1631 /* We may come here even when no configuration
1632 * is selected. Send online/offline event
1633 * accordingly.
1634 */
1635 switch_set_state(&ui->sdev,
1636 atomic_read(&ui->configured));
1637
1638 if (maxpower < 0)
1639 break;
1640
1641 ui->chg_current = maxpower;
1642 otg_set_power(ui->xceiv, maxpower);
1643 break;
1644 }
1645 if (flags & USB_FLAG_RESET) {
1646 dev_dbg(&ui->pdev->dev,
1647 "msm72k_udc: ONLINE -> RESET\n");
1648 msm72k_pullup_internal(&ui->gadget, 0);
1649 usb_reset(ui);
1650 msm72k_pullup_internal(&ui->gadget, 1);
1651 dev_dbg(&ui->pdev->dev,
1652 "msm72k_udc: RESET -> ONLINE\n");
1653 break;
1654 }
1655 break;
1656 case USB_STATE_OFFLINE:
1657 /* If we were signaled to go online and vbus is still
1658 * present when we received the signal, go online.
1659 */
1660 if ((flags & USB_FLAG_VBUS_ONLINE) && _vbus) {
1661 int ret;
1662
1663 pm_runtime_get_noresume(&ui->pdev->dev);
1664 pm_runtime_resume(&ui->pdev->dev);
1665 dev_dbg(&ui->pdev->dev,
1666 "msm72k_udc: OFFLINE -> ONLINE\n");
1667
1668 usb_reset(ui);
1669 ui->state = USB_STATE_ONLINE;
1670 usb_do_work_check_vbus(ui);
1671 ret = request_irq(otg->irq, usb_interrupt,
1672 IRQF_SHARED,
1673 ui->pdev->name, ui);
1674 /* FIXME: should we call BUG_ON when
1675 * requst irq fails
1676 */
1677 if (ret) {
1678 dev_err(&ui->pdev->dev,
1679 "hsusb: peripheral: request irq"
1680 " failed:(%d)", ret);
1681 break;
1682 }
1683 ui->irq = otg->irq;
1684 enable_irq_wake(otg->irq);
1685
1686 if (!atomic_read(&ui->softconnect))
1687 break;
1688 msm72k_pullup_internal(&ui->gadget, 1);
1689
1690 if (!ui->gadget.is_a_peripheral)
1691 schedule_delayed_work(
1692 &ui->chg_det,
1693 USB_CHG_DET_DELAY);
1694 }
1695 break;
1696 }
1697 }
1698}
1699
1700/* FIXME - the callers of this function should use a gadget API instead.
1701 * This is called from htc_battery.c and board-halibut.c
1702 * WARNING - this can get called before this driver is initialized.
1703 */
1704void msm_hsusb_set_vbus_state(int online)
1705{
1706 unsigned long flags;
1707 struct usb_info *ui = the_usb_info;
1708
1709 if (!ui) {
1710 pr_err("%s called before driver initialized\n", __func__);
1711 return;
1712 }
1713
1714 spin_lock_irqsave(&ui->lock, flags);
1715
1716 if (is_usb_online(ui) == online)
1717 goto out;
1718
1719 if (online) {
1720 ui->usb_state = USB_STATE_POWERED;
1721 ui->flags |= USB_FLAG_VBUS_ONLINE;
1722 } else {
1723 ui->gadget.speed = USB_SPEED_UNKNOWN;
1724 ui->usb_state = USB_STATE_NOTATTACHED;
1725 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1726 }
1727 if (in_interrupt()) {
1728 schedule_work(&ui->work);
1729 } else {
1730 spin_unlock_irqrestore(&ui->lock, flags);
1731 usb_do_work(&ui->work);
1732 return;
1733 }
1734out:
1735 spin_unlock_irqrestore(&ui->lock, flags);
1736}
1737
1738#if defined(CONFIG_DEBUG_FS)
1739
1740void usb_function_reenumerate(void)
1741{
1742 struct usb_info *ui = the_usb_info;
1743
1744 /* disable and re-enable the D+ pullup */
1745 dev_dbg(&ui->pdev->dev, "disable pullup\n");
1746 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
1747
1748 msleep(10);
1749
1750 dev_dbg(&ui->pdev->dev, "enable pullup\n");
1751 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
1752}
1753
1754static char debug_buffer[PAGE_SIZE];
1755
1756static ssize_t debug_read_status(struct file *file, char __user *ubuf,
1757 size_t count, loff_t *ppos)
1758{
1759 struct usb_info *ui = file->private_data;
1760 char *buf = debug_buffer;
1761 unsigned long flags;
1762 struct msm_endpoint *ept;
1763 struct msm_request *req;
1764 int n;
1765 int i = 0;
1766
1767 spin_lock_irqsave(&ui->lock, flags);
1768
1769 i += scnprintf(buf + i, PAGE_SIZE - i,
1770 "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
1771 readl(USB_ENDPTSETUPSTAT),
1772 readl(USB_ENDPTPRIME),
1773 readl(USB_ENDPTSTAT),
1774 readl(USB_ENDPTCOMPLETE));
1775 i += scnprintf(buf + i, PAGE_SIZE - i,
1776 "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
1777 readl(USB_USBCMD),
1778 readl(USB_USBSTS),
1779 readl(USB_USBINTR),
1780 readl(USB_PORTSC));
1781
1782
1783 for (n = 0; n < 32; n++) {
1784 ept = ui->ept + n;
1785 if (ept->ep.maxpacket == 0)
1786 continue;
1787
1788 i += scnprintf(buf + i, PAGE_SIZE - i,
1789 "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
1790 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1791 ept->head->config, ept->head->active,
1792 ept->head->next, ept->head->info);
1793
1794 for (req = ept->req; req; req = req->next)
1795 i += scnprintf(buf + i, PAGE_SIZE - i,
1796 " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
1797 req->item_dma, req->item->next,
1798 req->item->info, req->item->page0,
1799 req->busy ? 'B' : ' ',
1800 req->live ? 'L' : ' ');
1801 }
1802
1803 i += scnprintf(buf + i, PAGE_SIZE - i,
1804 "phy failure count: %d\n", ui->phy_fail_count);
1805
1806 spin_unlock_irqrestore(&ui->lock, flags);
1807
1808 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
1809}
1810
1811static ssize_t debug_write_reset(struct file *file, const char __user *buf,
1812 size_t count, loff_t *ppos)
1813{
1814 struct usb_info *ui = file->private_data;
1815 unsigned long flags;
1816
1817 spin_lock_irqsave(&ui->lock, flags);
1818 ui->flags |= USB_FLAG_RESET;
1819 schedule_work(&ui->work);
1820 spin_unlock_irqrestore(&ui->lock, flags);
1821
1822 return count;
1823}
1824
1825static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
1826 size_t count, loff_t *ppos)
1827{
1828 usb_function_reenumerate();
1829 return count;
1830}
1831
1832static int debug_open(struct inode *inode, struct file *file)
1833{
1834 file->private_data = inode->i_private;
1835 return 0;
1836}
1837
1838const struct file_operations debug_stat_ops = {
1839 .open = debug_open,
1840 .read = debug_read_status,
1841};
1842
1843const struct file_operations debug_reset_ops = {
1844 .open = debug_open,
1845 .write = debug_write_reset,
1846};
1847
1848const struct file_operations debug_cycle_ops = {
1849 .open = debug_open,
1850 .write = debug_write_cycle,
1851};
1852
1853static ssize_t debug_read_release_wlocks(struct file *file, char __user *ubuf,
1854 size_t count, loff_t *ppos)
1855{
1856 char kbuf[10];
1857 size_t c = 0;
1858
1859 memset(kbuf, 0, 10);
1860
1861 c = scnprintf(kbuf, 10, "%d", release_wlocks);
1862
1863 if (copy_to_user(ubuf, kbuf, c))
1864 return -EFAULT;
1865
1866 return c;
1867}
1868static ssize_t debug_write_release_wlocks(struct file *file,
1869 const char __user *buf, size_t count, loff_t *ppos)
1870{
1871 char kbuf[10];
1872 long temp;
1873
1874 memset(kbuf, 0, 10);
1875
1876 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1877 return -EFAULT;
1878
1879 if (strict_strtol(kbuf, 10, &temp))
1880 return -EINVAL;
1881
1882 if (temp)
1883 release_wlocks = 1;
1884 else
1885 release_wlocks = 0;
1886
1887 return count;
1888}
1889static int debug_wake_lock_open(struct inode *inode, struct file *file)
1890{
1891 file->private_data = inode->i_private;
1892 return 0;
1893}
1894const struct file_operations debug_wlocks_ops = {
1895 .open = debug_wake_lock_open,
1896 .read = debug_read_release_wlocks,
1897 .write = debug_write_release_wlocks,
1898};
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301899
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301900static ssize_t debug_reprime_ep(struct file *file, const char __user *ubuf,
1901 size_t count, loff_t *ppos)
1902{
1903 struct usb_info *ui = file->private_data;
1904 struct msm_endpoint *ept;
1905 char kbuf[10];
1906 unsigned int ep_num, dir;
1907 unsigned long flags;
1908 unsigned n, i;
1909
1910 memset(kbuf, 0, 10);
1911
1912 if (copy_from_user(kbuf, ubuf, count > 10 ? 10 : count))
1913 return -EFAULT;
1914
1915 if (sscanf(kbuf, "%u %u", &ep_num, &dir) != 2)
1916 return -EINVAL;
1917
1918 if (dir)
1919 i = ep_num + 16;
1920 else
1921 i = ep_num;
1922
1923 spin_lock_irqsave(&ui->lock, flags);
1924 ept = ui->ept + i;
1925 n = 1 << ept->bit;
1926
1927 if ((readl_relaxed(USB_ENDPTPRIME) & n))
1928 goto out;
1929
1930 if (readl_relaxed(USB_ENDPTSTAT) & n)
1931 goto out;
1932
1933 /* clear speculative loads on item->info */
1934 rmb();
1935 if (ept->req && (ept->req->item->info & INFO_ACTIVE)) {
1936 pr_err("%s(): ept%d%s prime failed. ept: config: %x"
1937 "active: %x next: %x info: %x\n",
1938 __func__, ept->num,
1939 ept->flags & EPT_FLAG_IN ? "in" : "out",
1940 ept->head->config, ept->head->active,
1941 ept->head->next, ept->head->info);
1942 writel_relaxed(n, USB_ENDPTPRIME);
1943 }
1944out:
1945 spin_unlock_irqrestore(&ui->lock, flags);
1946
1947 return count;
1948}
1949
1950static char buffer[512];
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301951static ssize_t debug_prime_fail_read(struct file *file, char __user *ubuf,
1952 size_t count, loff_t *ppos)
1953{
1954 struct usb_info *ui = file->private_data;
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301955 char *buf = buffer;
1956 unsigned long flags;
1957 struct msm_endpoint *ept;
1958 int n;
1959 int i = 0;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301960
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301961 spin_lock_irqsave(&ui->lock, flags);
1962 for (n = 0; n < 32; n++) {
1963 ept = ui->ept + n;
1964 if (ept->ep.maxpacket == 0)
1965 continue;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301966
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301967 i += scnprintf(buf + i, PAGE_SIZE - i,
1968 "ept%d %s false_prime_count=%lu prime_fail_count=%d dtd_fail_count=%lu\n",
1969 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1970 ept->false_prime_fail_count,
1971 ept->actual_prime_fail_count,
1972 ept->dTD_update_fail_count);
1973 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301974
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301975 i += scnprintf(buf + i, PAGE_SIZE - i,
1976 "dTD_update_fail count: %lu\n",
1977 ui->dTD_update_fail_count);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301978
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301979 i += scnprintf(buf + i, PAGE_SIZE - i,
1980 "prime_fail count: %d\n", ui->prime_fail_count);
1981
1982 spin_unlock_irqrestore(&ui->lock, flags);
1983
1984 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301985}
1986
1987static int debug_prime_fail_open(struct inode *inode, struct file *file)
1988{
1989 file->private_data = inode->i_private;
1990 return 0;
1991}
1992
1993const struct file_operations prime_fail_ops = {
1994 .open = debug_prime_fail_open,
1995 .read = debug_prime_fail_read,
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301996 .write = debug_reprime_ep,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301997};
1998
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001999static void usb_debugfs_init(struct usb_info *ui)
2000{
2001 struct dentry *dent;
2002 dent = debugfs_create_dir(dev_name(&ui->pdev->dev), 0);
2003 if (IS_ERR(dent))
2004 return;
2005
2006 debugfs_create_file("status", 0444, dent, ui, &debug_stat_ops);
2007 debugfs_create_file("reset", 0222, dent, ui, &debug_reset_ops);
2008 debugfs_create_file("cycle", 0222, dent, ui, &debug_cycle_ops);
2009 debugfs_create_file("release_wlocks", 0666, dent, ui,
2010 &debug_wlocks_ops);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302011 debugfs_create_file("prime_fail_countt", 0666, dent, ui,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302012 &prime_fail_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002013}
2014#else
2015static void usb_debugfs_init(struct usb_info *ui) {}
2016#endif
2017
2018static int
2019msm72k_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
2020{
2021 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2022 unsigned char ep_type =
2023 desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2024
2025 _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2026 config_ept(ept);
2027 ept->wedged = 0;
2028 usb_ept_enable(ept, 1, ep_type);
2029 return 0;
2030}
2031
2032static int msm72k_disable(struct usb_ep *_ep)
2033{
2034 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2035
2036 usb_ept_enable(ept, 0, 0);
2037 flush_endpoint(ept);
Pavankumar Kondeti2c968782012-04-12 15:16:39 +05302038 /*
2039 * Clear descriptors here. Otherwise previous descriptors
2040 * will be used by function drivers upon next enumeration.
2041 */
2042 _ep->desc = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002043 return 0;
2044}
2045
2046static struct usb_request *
2047msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
2048{
2049 return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
2050}
2051
2052static void
2053msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
2054{
2055 struct msm_request *req = to_msm_request(_req);
2056 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2057 struct usb_info *ui = ept->ui;
2058
2059 /* request should not be busy */
2060 BUG_ON(req->busy);
2061 if (req->alloced)
2062 kfree(req->req.buf);
2063 dma_pool_free(ui->pool, req->item, req->item_dma);
2064 kfree(req);
2065}
2066
2067static int
2068msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
2069{
2070 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2071 struct usb_info *ui = ep->ui;
2072
2073 if (ep == &ui->ep0in) {
2074 struct msm_request *r = to_msm_request(req);
2075 if (!req->length)
2076 goto ep_queue_done;
2077 r->gadget_complete = req->complete;
2078 /* ep0_queue_ack_complete queue a receive for ACK before
2079 ** calling req->complete
2080 */
2081 req->complete = ep0_queue_ack_complete;
2082 if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
2083 ep = &ui->ep0out;
2084 goto ep_queue_done;
2085 }
2086
2087ep_queue_done:
2088 return usb_ept_queue_xfer(ep, req);
2089}
2090
2091static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2092{
2093 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2094 struct msm_request *req = to_msm_request(_req);
2095 struct usb_info *ui = ep->ui;
2096
2097 struct msm_request *temp_req;
2098 unsigned long flags;
2099
2100 if (!(ui && req && ep->req))
2101 return -EINVAL;
2102
2103 spin_lock_irqsave(&ui->lock, flags);
2104 if (!req->busy) {
2105 dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
2106 spin_unlock_irqrestore(&ui->lock, flags);
2107 return -EINVAL;
2108 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302109 del_timer(&ep->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002110 /* Stop the transfer */
2111 do {
2112 writel((1 << ep->bit), USB_ENDPTFLUSH);
2113 while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
2114 udelay(100);
2115 } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
2116
2117 req->req.status = 0;
2118 req->busy = 0;
2119
2120 if (ep->req == req) {
2121 ep->req = req->next;
2122 ep->head->next = req->item->next;
2123 } else {
2124 req->prev->next = req->next;
2125 if (req->next)
2126 req->next->prev = req->prev;
2127 req->prev->item->next = req->item->next;
2128 }
2129
2130 if (!req->next)
2131 ep->last = req->prev;
2132
2133 /* initialize request to default */
2134 req->item->next = TERMINATE;
2135 req->item->info = 0;
2136 req->live = 0;
2137 dma_unmap_single(NULL, req->dma, req->req.length,
2138 (ep->flags & EPT_FLAG_IN) ?
2139 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2140
2141 if (req->req.complete) {
2142 req->req.status = -ECONNRESET;
2143 spin_unlock_irqrestore(&ui->lock, flags);
2144 req->req.complete(&ep->ep, &req->req);
2145 spin_lock_irqsave(&ui->lock, flags);
2146 }
2147
2148 if (!req->live) {
2149 /* Reprime the endpoint for the remaining transfers */
2150 for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
2151 temp_req->live = 0;
2152 if (ep->req)
2153 usb_ept_start(ep);
2154 spin_unlock_irqrestore(&ui->lock, flags);
2155 return 0;
2156 }
2157 spin_unlock_irqrestore(&ui->lock, flags);
2158 return 0;
2159}
2160
2161static int
2162usb_ept_set_halt(struct usb_ep *_ep, int value)
2163{
2164 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2165 struct usb_info *ui = ept->ui;
2166 unsigned int in = ept->flags & EPT_FLAG_IN;
2167 unsigned int n;
2168 unsigned long flags;
2169
2170 spin_lock_irqsave(&ui->lock, flags);
2171
2172 n = readl(USB_ENDPTCTRL(ept->num));
2173
2174 if (in) {
2175 if (value)
2176 n |= CTRL_TXS;
2177 else {
2178 n &= ~CTRL_TXS;
2179 n |= CTRL_TXR;
2180 }
2181 } else {
2182 if (value)
2183 n |= CTRL_RXS;
2184 else {
2185 n &= ~CTRL_RXS;
2186 n |= CTRL_RXR;
2187 }
2188 }
2189 writel(n, USB_ENDPTCTRL(ept->num));
2190 if (!value)
2191 ept->wedged = 0;
2192 spin_unlock_irqrestore(&ui->lock, flags);
2193
2194 return 0;
2195}
2196
2197static int
2198msm72k_set_halt(struct usb_ep *_ep, int value)
2199{
2200 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2201 unsigned int in = ept->flags & EPT_FLAG_IN;
2202
2203 if (value && in && ept->req)
2204 return -EAGAIN;
2205
2206 usb_ept_set_halt(_ep, value);
2207
2208 return 0;
2209}
2210
2211static int
2212msm72k_fifo_status(struct usb_ep *_ep)
2213{
2214 return -EOPNOTSUPP;
2215}
2216
2217static void
2218msm72k_fifo_flush(struct usb_ep *_ep)
2219{
2220 flush_endpoint(to_msm_endpoint(_ep));
2221}
2222static int msm72k_set_wedge(struct usb_ep *_ep)
2223{
2224 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2225
2226 if (ept->num == 0)
2227 return -EINVAL;
2228
2229 ept->wedged = 1;
2230
2231 return msm72k_set_halt(_ep, 1);
2232}
2233
2234static const struct usb_ep_ops msm72k_ep_ops = {
2235 .enable = msm72k_enable,
2236 .disable = msm72k_disable,
2237
2238 .alloc_request = msm72k_alloc_request,
2239 .free_request = msm72k_free_request,
2240
2241 .queue = msm72k_queue,
2242 .dequeue = msm72k_dequeue,
2243
2244 .set_halt = msm72k_set_halt,
2245 .set_wedge = msm72k_set_wedge,
2246 .fifo_status = msm72k_fifo_status,
2247 .fifo_flush = msm72k_fifo_flush,
2248};
2249
2250static int msm72k_get_frame(struct usb_gadget *_gadget)
2251{
2252 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2253
2254 /* frame number is in bits 13:3 */
2255 return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
2256}
2257
2258/* VBUS reporting logically comes from a transceiver */
2259static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
2260{
2261 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2262 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2263
2264 if (is_active || atomic_read(&otg->chg_type)
2265 == USB_CHG_TYPE__WALLCHARGER)
2266 wake_lock(&ui->wlock);
2267
2268 msm_hsusb_set_vbus_state(is_active);
2269 return 0;
2270}
2271
2272/* SW workarounds
2273Issue #1 - USB Spoof Disconnect Failure
2274Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2275SW workaround - Making opmode non-driving and SuspendM set in function
2276 register of SMSC phy
2277*/
2278/* drivers may have software control over D+ pullup */
2279static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
2280{
2281 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2282 unsigned long flags;
2283
2284 if (is_active) {
2285 spin_lock_irqsave(&ui->lock, flags);
2286 if (is_usb_online(ui) && ui->driver)
2287 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2288 spin_unlock_irqrestore(&ui->lock, flags);
2289 } else {
2290 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2291 /* S/W workaround, Issue#1 */
2292 otg_io_write(ui->xceiv, 0x48, 0x04);
2293 }
2294
2295 /* Ensure pull-up operation is completed before returning */
2296 mb();
2297
2298 return 0;
2299}
2300
2301static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
2302{
2303 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2304 unsigned long flags;
2305
2306
2307 atomic_set(&ui->softconnect, is_active);
2308
2309 spin_lock_irqsave(&ui->lock, flags);
2310 if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL) {
2311 spin_unlock_irqrestore(&ui->lock, flags);
2312 return 0;
2313 }
2314 spin_unlock_irqrestore(&ui->lock, flags);
2315
2316 msm72k_pullup_internal(_gadget, is_active);
2317
2318 if (is_active && !ui->gadget.is_a_peripheral)
2319 schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
2320
2321 return 0;
2322}
2323
2324static int msm72k_wakeup(struct usb_gadget *_gadget)
2325{
2326 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2327 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2328
2329 if (!atomic_read(&ui->remote_wakeup)) {
2330 dev_err(&ui->pdev->dev,
2331 "%s: remote wakeup not supported\n", __func__);
2332 return -ENOTSUPP;
2333 }
2334
2335 if (!atomic_read(&ui->configured)) {
2336 dev_err(&ui->pdev->dev,
2337 "%s: device is not configured\n", __func__);
2338 return -ENODEV;
2339 }
2340 otg_set_suspend(ui->xceiv, 0);
2341
2342 disable_irq(otg->irq);
2343
2344 if (!is_usb_active())
2345 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
2346
2347 /* Ensure that USB port is resumed before enabling the IRQ */
2348 mb();
2349
2350 enable_irq(otg->irq);
2351
2352 return 0;
2353}
2354
2355/* when Gadget is configured, it will indicate how much power
2356 * can be pulled from vbus, as specified in configuiration descriptor
2357 */
2358static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2359{
2360 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2361 unsigned long flags;
2362
2363
2364 spin_lock_irqsave(&ui->lock, flags);
2365 ui->b_max_pow = mA;
2366 ui->flags = USB_FLAG_CONFIGURED;
2367 spin_unlock_irqrestore(&ui->lock, flags);
2368
2369 schedule_work(&ui->work);
2370
2371 return 0;
2372}
2373
2374static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
2375{
2376 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2377 unsigned long flags;
2378 int ret = 0;
2379
2380 spin_lock_irqsave(&ui->lock, flags);
2381 if (set) {
2382 if (ui->pdata && ui->pdata->self_powered)
2383 atomic_set(&ui->self_powered, 1);
2384 else
2385 ret = -EOPNOTSUPP;
2386 } else {
2387 /* We can always work as a bus powered device */
2388 atomic_set(&ui->self_powered, 0);
2389 }
2390 spin_unlock_irqrestore(&ui->lock, flags);
2391
2392 return ret;
2393
2394}
2395
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002396static int msm72k_gadget_start(struct usb_gadget_driver *driver,
2397 int (*bind)(struct usb_gadget *));
2398static int msm72k_gadget_stop(struct usb_gadget_driver *driver);
2399
2400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401static const struct usb_gadget_ops msm72k_ops = {
2402 .get_frame = msm72k_get_frame,
2403 .vbus_session = msm72k_udc_vbus_session,
2404 .vbus_draw = msm72k_udc_vbus_draw,
2405 .pullup = msm72k_pullup,
2406 .wakeup = msm72k_wakeup,
2407 .set_selfpowered = msm72k_set_selfpowered,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002408 .start = msm72k_gadget_start,
2409 .stop = msm72k_gadget_stop
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410};
2411
2412static void usb_do_remote_wakeup(struct work_struct *w)
2413{
2414 struct usb_info *ui = the_usb_info;
2415
2416 msm72k_wakeup(&ui->gadget);
2417}
2418
2419static ssize_t usb_remote_wakeup(struct device *dev,
2420 struct device_attribute *attr, const char *buf, size_t count)
2421{
2422 struct usb_info *ui = the_usb_info;
2423
2424 msm72k_wakeup(&ui->gadget);
2425
2426 return count;
2427}
2428
2429static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
2430 char *buf)
2431{
2432 size_t i;
2433 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
2434 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
2435 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
2436 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
2437 "USB_STATE_SUSPENDED"
2438 };
2439
2440 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
2441 return i;
2442}
2443
2444static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
2445 char *buf)
2446{
2447 struct usb_info *ui = the_usb_info;
2448 size_t i;
2449 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
2450 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
2451
2452 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
2453 return i;
2454}
2455
2456static ssize_t store_usb_chg_current(struct device *dev,
2457 struct device_attribute *attr, const char *buf, size_t count)
2458{
2459 struct usb_info *ui = the_usb_info;
2460 unsigned long mA;
2461
2462 if (ui->gadget.is_a_peripheral)
2463 return -EINVAL;
2464
2465 if (strict_strtoul(buf, 10, &mA))
2466 return -EINVAL;
2467
2468 ui->chg_current = mA;
2469 otg_set_power(ui->xceiv, mA);
2470
2471 return count;
2472}
2473
2474static ssize_t show_usb_chg_current(struct device *dev,
2475 struct device_attribute *attr, char *buf)
2476{
2477 struct usb_info *ui = the_usb_info;
2478 size_t count;
2479
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302480 count = snprintf(buf, PAGE_SIZE, "%d", ui->chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481
2482 return count;
2483}
2484
2485static ssize_t show_usb_chg_type(struct device *dev,
2486 struct device_attribute *attr, char *buf)
2487{
2488 struct usb_info *ui = the_usb_info;
2489 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2490 size_t count;
2491 char *chg_type[] = {"STD DOWNSTREAM PORT",
2492 "CARKIT",
2493 "DEDICATED CHARGER",
2494 "INVALID"};
2495
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302496 count = snprintf(buf, PAGE_SIZE, "%s",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 chg_type[atomic_read(&otg->chg_type)]);
2498
2499 return count;
2500}
2501static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
2502static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
2503static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
2504static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
2505static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
2506 show_usb_chg_current, store_usb_chg_current);
2507
2508#ifdef CONFIG_USB_OTG
2509static ssize_t store_host_req(struct device *dev,
2510 struct device_attribute *attr, const char *buf, size_t count)
2511{
2512 struct usb_info *ui = the_usb_info;
2513 unsigned long val, flags;
2514
2515 if (strict_strtoul(buf, 10, &val))
2516 return -EINVAL;
2517
2518 dev_dbg(&ui->pdev->dev, "%s host request\n",
2519 val ? "set" : "clear");
2520
2521 spin_lock_irqsave(&ui->lock, flags);
2522 if (ui->hnp_avail)
2523 ui->gadget.host_request = !!val;
2524 spin_unlock_irqrestore(&ui->lock, flags);
2525
2526 return count;
2527}
2528static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
2529
2530/* How do we notify user space about HNP availability?
2531 * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
2532 * Introduce hnp_avail flag and set when HNP polling request arrives.
2533 * The expectation is that user space checks hnp availability before
2534 * requesting host role via above sysfs node.
2535 */
2536static ssize_t show_host_avail(struct device *dev,
2537 struct device_attribute *attr, char *buf)
2538{
2539 struct usb_info *ui = the_usb_info;
2540 size_t count;
2541 unsigned long flags;
2542
2543 spin_lock_irqsave(&ui->lock, flags);
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302544 count = snprintf(buf, PAGE_SIZE, "%d\n", ui->hnp_avail);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002545 spin_unlock_irqrestore(&ui->lock, flags);
2546
2547 return count;
2548}
2549static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
2550
2551static struct attribute *otg_attrs[] = {
2552 &dev_attr_host_request.attr,
2553 &dev_attr_host_avail.attr,
2554 NULL,
2555};
2556
2557static struct attribute_group otg_attr_grp = {
2558 .name = "otg",
2559 .attrs = otg_attrs,
2560};
2561#endif
2562
2563static int msm72k_probe(struct platform_device *pdev)
2564{
2565 struct usb_info *ui;
2566 struct msm_otg *otg;
2567 int retval;
2568
2569 dev_dbg(&pdev->dev, "msm72k_probe\n");
2570 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
2571 if (!ui)
2572 return -ENOMEM;
2573
2574 ui->pdev = pdev;
2575 ui->pdata = pdev->dev.platform_data;
2576
2577 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
2578 if (!ui->buf)
2579 return usb_free(ui, -ENOMEM);
2580
2581 ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
2582 if (!ui->pool)
2583 return usb_free(ui, -ENOMEM);
2584
2585 ui->xceiv = otg_get_transceiver();
2586 if (!ui->xceiv)
2587 return usb_free(ui, -ENODEV);
2588
2589 otg = to_msm_otg(ui->xceiv);
2590 ui->addr = otg->regs;
2591
2592 ui->gadget.ops = &msm72k_ops;
2593 ui->gadget.is_dualspeed = 1;
2594 device_initialize(&ui->gadget.dev);
2595 dev_set_name(&ui->gadget.dev, "gadget");
2596 ui->gadget.dev.parent = &pdev->dev;
2597 ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
2598
2599#ifdef CONFIG_USB_OTG
2600 ui->gadget.is_otg = 1;
2601#endif
2602
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002603 retval = usb_add_gadget_udc(&pdev->dev, &ui->gadget);
2604 if (retval)
2605 return usb_free(ui, retval);
2606
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002607 ui->sdev.name = DRIVER_NAME;
2608 ui->sdev.print_name = print_switch_name;
2609 ui->sdev.print_state = print_switch_state;
2610
2611 retval = switch_dev_register(&ui->sdev);
2612 if (retval)
2613 return usb_free(ui, retval);
2614
2615 the_usb_info = ui;
2616
2617 wake_lock_init(&ui->wlock,
2618 WAKE_LOCK_SUSPEND, "usb_bus_active");
2619
2620 usb_debugfs_init(ui);
2621
2622 usb_prepare(ui);
2623
2624#ifdef CONFIG_USB_OTG
2625 retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
2626 if (retval) {
2627 dev_err(&ui->pdev->dev,
2628 "failed to create otg sysfs directory:"
2629 "err:(%d)\n", retval);
2630 }
2631#endif
2632
2633 retval = otg_set_peripheral(ui->xceiv, &ui->gadget);
2634 if (retval) {
2635 dev_err(&ui->pdev->dev,
2636 "%s: Cannot bind the transceiver, retval:(%d)\n",
2637 __func__, retval);
2638 switch_dev_unregister(&ui->sdev);
2639 wake_lock_destroy(&ui->wlock);
2640 return usb_free(ui, retval);
2641 }
2642
2643 pm_runtime_enable(&pdev->dev);
2644
2645 /* Setup phy stuck timer */
2646 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
2647 setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
2648 return 0;
2649}
2650
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002651static int msm72k_gadget_start(struct usb_gadget_driver *driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002652 int (*bind)(struct usb_gadget *))
2653{
2654 struct usb_info *ui = the_usb_info;
2655 int retval, n;
2656
2657 if (!driver
2658 || driver->speed < USB_SPEED_FULL
2659 || !bind
2660 || !driver->disconnect
2661 || !driver->setup)
2662 return -EINVAL;
2663 if (!ui)
2664 return -ENODEV;
2665 if (ui->driver)
2666 return -EBUSY;
2667
2668 /* first hook up the driver ... */
2669 ui->driver = driver;
2670 ui->gadget.dev.driver = &driver->driver;
2671 ui->gadget.name = driver_name;
2672 INIT_LIST_HEAD(&ui->gadget.ep_list);
2673 ui->gadget.ep0 = &ui->ep0in.ep;
2674 INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
2675 ui->gadget.speed = USB_SPEED_UNKNOWN;
2676 atomic_set(&ui->softconnect, 1);
2677
2678 for (n = 1; n < 16; n++) {
2679 struct msm_endpoint *ept = ui->ept + n;
2680 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2681 ept->ep.maxpacket = 512;
2682 }
2683 for (n = 17; n < 32; n++) {
2684 struct msm_endpoint *ept = ui->ept + n;
2685 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2686 ept->ep.maxpacket = 512;
2687 }
2688
2689 retval = device_add(&ui->gadget.dev);
2690 if (retval)
2691 goto fail;
2692
2693 retval = bind(&ui->gadget);
2694 if (retval) {
2695 dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
2696 driver->driver.name, retval);
2697 device_del(&ui->gadget.dev);
2698 goto fail;
2699 }
2700
2701 retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
2702 if (retval != 0)
2703 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2704 "(wakeup) error: (%d)\n", retval);
2705 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
2706 if (retval != 0)
2707 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2708 " (usb_state) error: (%d)\n", retval);
2709
2710 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
2711 if (retval != 0)
2712 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2713 " (usb_speed) error: (%d)\n", retval);
2714
2715 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
2716 if (retval != 0)
2717 dev_err(&ui->pdev->dev,
2718 "failed to create sysfs entry(chg_type): err:(%d)\n",
2719 retval);
2720 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
2721 if (retval != 0)
2722 dev_err(&ui->pdev->dev,
2723 "failed to create sysfs entry(chg_current):"
2724 "err:(%d)\n", retval);
2725
2726 dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
2727 driver->driver.name);
2728 usb_start(ui);
2729
2730 return 0;
2731
2732fail:
2733 ui->driver = NULL;
2734 ui->gadget.dev.driver = NULL;
2735 return retval;
2736}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002737
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002738static int msm72k_gadget_stop(struct usb_gadget_driver *driver)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002739{
2740 struct usb_info *dev = the_usb_info;
2741
2742 if (!dev)
2743 return -ENODEV;
2744 if (!driver || driver != dev->driver || !driver->unbind)
2745 return -EINVAL;
2746
2747 msm72k_pullup_internal(&dev->gadget, 0);
2748 if (dev->irq) {
2749 free_irq(dev->irq, dev);
2750 dev->irq = 0;
2751 }
2752 dev->state = USB_STATE_IDLE;
2753 atomic_set(&dev->configured, 0);
2754 switch_set_state(&dev->sdev, 0);
2755 /* cancel pending ep0 transactions */
2756 flush_endpoint(&dev->ep0out);
2757 flush_endpoint(&dev->ep0in);
2758
2759 device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
2760 device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
2761 device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
2762 device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
2763 device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
2764 driver->disconnect(&dev->gadget);
2765 driver->unbind(&dev->gadget);
2766 dev->gadget.dev.driver = NULL;
2767 dev->driver = NULL;
2768
2769 device_del(&dev->gadget.dev);
2770
2771 dev_dbg(&dev->pdev->dev,
2772 "unregistered gadget driver '%s'\n", driver->driver.name);
2773 return 0;
2774}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002775
2776
2777static int msm72k_udc_runtime_suspend(struct device *dev)
2778{
2779 dev_dbg(dev, "pm_runtime: suspending...\n");
2780 return 0;
2781}
2782
2783static int msm72k_udc_runtime_resume(struct device *dev)
2784{
2785 dev_dbg(dev, "pm_runtime: resuming...\n");
2786 return 0;
2787}
2788
2789static int msm72k_udc_runtime_idle(struct device *dev)
2790{
2791 dev_dbg(dev, "pm_runtime: idling...\n");
2792 return 0;
2793}
2794
2795static struct dev_pm_ops msm72k_udc_dev_pm_ops = {
2796 .runtime_suspend = msm72k_udc_runtime_suspend,
2797 .runtime_resume = msm72k_udc_runtime_resume,
2798 .runtime_idle = msm72k_udc_runtime_idle
2799};
2800
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002801static int __exit msm72k_remove(struct platform_device *pdev)
2802{
2803 struct usb_info *ui = container_of(&pdev, struct usb_info, pdev);
2804
2805 return usb_free(ui, 0);
2806}
2807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002808static struct platform_driver usb_driver = {
2809 .probe = msm72k_probe,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002810 .remove = msm72k_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002811 .driver = { .name = "msm_hsusb",
2812 .pm = &msm72k_udc_dev_pm_ops, },
2813};
2814
2815static int __init init(void)
2816{
2817 return platform_driver_register(&usb_driver);
2818}
2819module_init(init);
2820
2821static void __exit cleanup(void)
2822{
2823 platform_driver_unregister(&usb_driver);
2824}
2825module_exit(cleanup);
2826
2827MODULE_DESCRIPTION(DRIVER_DESC);
2828MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
2829MODULE_LICENSE("GPL");