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