blob: a025d95eccf13b384da9b5a8beb6f8f83861f6ac [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)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700102#define to_msm_otg(xceiv) container_of(xceiv, struct msm_otg, phy)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103#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
Steve Mucklef132c6c2012-06-06 18:30:57 -0700222 struct usb_phy *xceiv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 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
Steve Mucklef132c6c2012-06-06 18:30:57 -0700339 if (usb_phy_io_write(ui->xceiv, 0xAA, 0x16) == -1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 dev_dbg(&ui->pdev->dev,
341 "%s(): ulpi write timeout\n", __func__);
342 return -EIO;
343 }
344
Steve Mucklef132c6c2012-06-06 18:30:57 -0700345 if (usb_phy_io_read(ui->xceiv, 0x16) != 0xAA) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 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)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700411 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412}
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)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700434 usb_phy_set_power(ui->xceiv, maxpower);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435
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.
Steve Mucklef132c6c2012-06-06 18:30:57 -0700439 * OTG driver handles idle pc as part of above usb_phy_set_power call
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 * 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);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700714 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 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)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001280 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281#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)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001292 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 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)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001350 usb_phy_set_suspend(ui->xceiv, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001351#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)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001462 usb_put_transceiver(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001463
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 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001594 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001595
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
Steve Mucklef132c6c2012-06-06 18:30:57 -07001617 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001618 /* 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;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001642 usb_phy_set_power(ui->xceiv, maxpower);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001643 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{
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302021 struct msm_endpoint *ept;
2022 unsigned char ep_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302024 if (_ep == NULL || desc == NULL)
2025 return -EINVAL;
2026
2027 ept = to_msm_endpoint(_ep);
2028 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002029 _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2030 config_ept(ept);
2031 ept->wedged = 0;
2032 usb_ept_enable(ept, 1, ep_type);
2033 return 0;
2034}
2035
2036static int msm72k_disable(struct usb_ep *_ep)
2037{
2038 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2039
2040 usb_ept_enable(ept, 0, 0);
2041 flush_endpoint(ept);
Pavankumar Kondeti2c968782012-04-12 15:16:39 +05302042 /*
2043 * Clear descriptors here. Otherwise previous descriptors
2044 * will be used by function drivers upon next enumeration.
2045 */
2046 _ep->desc = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047 return 0;
2048}
2049
2050static struct usb_request *
2051msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
2052{
2053 return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
2054}
2055
2056static void
2057msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
2058{
2059 struct msm_request *req = to_msm_request(_req);
2060 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2061 struct usb_info *ui = ept->ui;
2062
2063 /* request should not be busy */
2064 BUG_ON(req->busy);
2065 if (req->alloced)
2066 kfree(req->req.buf);
2067 dma_pool_free(ui->pool, req->item, req->item_dma);
2068 kfree(req);
2069}
2070
2071static int
2072msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
2073{
2074 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2075 struct usb_info *ui = ep->ui;
2076
2077 if (ep == &ui->ep0in) {
2078 struct msm_request *r = to_msm_request(req);
2079 if (!req->length)
2080 goto ep_queue_done;
2081 r->gadget_complete = req->complete;
2082 /* ep0_queue_ack_complete queue a receive for ACK before
2083 ** calling req->complete
2084 */
2085 req->complete = ep0_queue_ack_complete;
2086 if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
2087 ep = &ui->ep0out;
2088 goto ep_queue_done;
2089 }
2090
2091ep_queue_done:
2092 return usb_ept_queue_xfer(ep, req);
2093}
2094
2095static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2096{
2097 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2098 struct msm_request *req = to_msm_request(_req);
2099 struct usb_info *ui = ep->ui;
2100
2101 struct msm_request *temp_req;
2102 unsigned long flags;
2103
2104 if (!(ui && req && ep->req))
2105 return -EINVAL;
2106
2107 spin_lock_irqsave(&ui->lock, flags);
2108 if (!req->busy) {
2109 dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
2110 spin_unlock_irqrestore(&ui->lock, flags);
2111 return -EINVAL;
2112 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302113 del_timer(&ep->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002114 /* Stop the transfer */
2115 do {
2116 writel((1 << ep->bit), USB_ENDPTFLUSH);
2117 while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
2118 udelay(100);
2119 } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
2120
2121 req->req.status = 0;
2122 req->busy = 0;
2123
2124 if (ep->req == req) {
2125 ep->req = req->next;
2126 ep->head->next = req->item->next;
2127 } else {
2128 req->prev->next = req->next;
2129 if (req->next)
2130 req->next->prev = req->prev;
2131 req->prev->item->next = req->item->next;
2132 }
2133
2134 if (!req->next)
2135 ep->last = req->prev;
2136
2137 /* initialize request to default */
2138 req->item->next = TERMINATE;
2139 req->item->info = 0;
2140 req->live = 0;
2141 dma_unmap_single(NULL, req->dma, req->req.length,
2142 (ep->flags & EPT_FLAG_IN) ?
2143 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2144
2145 if (req->req.complete) {
2146 req->req.status = -ECONNRESET;
2147 spin_unlock_irqrestore(&ui->lock, flags);
2148 req->req.complete(&ep->ep, &req->req);
2149 spin_lock_irqsave(&ui->lock, flags);
2150 }
2151
2152 if (!req->live) {
2153 /* Reprime the endpoint for the remaining transfers */
2154 for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
2155 temp_req->live = 0;
2156 if (ep->req)
2157 usb_ept_start(ep);
2158 spin_unlock_irqrestore(&ui->lock, flags);
2159 return 0;
2160 }
2161 spin_unlock_irqrestore(&ui->lock, flags);
2162 return 0;
2163}
2164
2165static int
2166usb_ept_set_halt(struct usb_ep *_ep, int value)
2167{
2168 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2169 struct usb_info *ui = ept->ui;
2170 unsigned int in = ept->flags & EPT_FLAG_IN;
2171 unsigned int n;
2172 unsigned long flags;
2173
2174 spin_lock_irqsave(&ui->lock, flags);
2175
2176 n = readl(USB_ENDPTCTRL(ept->num));
2177
2178 if (in) {
2179 if (value)
2180 n |= CTRL_TXS;
2181 else {
2182 n &= ~CTRL_TXS;
2183 n |= CTRL_TXR;
2184 }
2185 } else {
2186 if (value)
2187 n |= CTRL_RXS;
2188 else {
2189 n &= ~CTRL_RXS;
2190 n |= CTRL_RXR;
2191 }
2192 }
2193 writel(n, USB_ENDPTCTRL(ept->num));
2194 if (!value)
2195 ept->wedged = 0;
2196 spin_unlock_irqrestore(&ui->lock, flags);
2197
2198 return 0;
2199}
2200
2201static int
2202msm72k_set_halt(struct usb_ep *_ep, int value)
2203{
2204 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2205 unsigned int in = ept->flags & EPT_FLAG_IN;
2206
2207 if (value && in && ept->req)
2208 return -EAGAIN;
2209
2210 usb_ept_set_halt(_ep, value);
2211
2212 return 0;
2213}
2214
2215static int
2216msm72k_fifo_status(struct usb_ep *_ep)
2217{
2218 return -EOPNOTSUPP;
2219}
2220
2221static void
2222msm72k_fifo_flush(struct usb_ep *_ep)
2223{
2224 flush_endpoint(to_msm_endpoint(_ep));
2225}
2226static int msm72k_set_wedge(struct usb_ep *_ep)
2227{
2228 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2229
2230 if (ept->num == 0)
2231 return -EINVAL;
2232
2233 ept->wedged = 1;
2234
2235 return msm72k_set_halt(_ep, 1);
2236}
2237
2238static const struct usb_ep_ops msm72k_ep_ops = {
2239 .enable = msm72k_enable,
2240 .disable = msm72k_disable,
2241
2242 .alloc_request = msm72k_alloc_request,
2243 .free_request = msm72k_free_request,
2244
2245 .queue = msm72k_queue,
2246 .dequeue = msm72k_dequeue,
2247
2248 .set_halt = msm72k_set_halt,
2249 .set_wedge = msm72k_set_wedge,
2250 .fifo_status = msm72k_fifo_status,
2251 .fifo_flush = msm72k_fifo_flush,
2252};
2253
2254static int msm72k_get_frame(struct usb_gadget *_gadget)
2255{
2256 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2257
2258 /* frame number is in bits 13:3 */
2259 return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
2260}
2261
2262/* VBUS reporting logically comes from a transceiver */
2263static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
2264{
2265 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2266 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2267
2268 if (is_active || atomic_read(&otg->chg_type)
2269 == USB_CHG_TYPE__WALLCHARGER)
2270 wake_lock(&ui->wlock);
2271
2272 msm_hsusb_set_vbus_state(is_active);
2273 return 0;
2274}
2275
2276/* SW workarounds
2277Issue #1 - USB Spoof Disconnect Failure
2278Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2279SW workaround - Making opmode non-driving and SuspendM set in function
2280 register of SMSC phy
2281*/
2282/* drivers may have software control over D+ pullup */
2283static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
2284{
2285 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2286 unsigned long flags;
2287
2288 if (is_active) {
2289 spin_lock_irqsave(&ui->lock, flags);
2290 if (is_usb_online(ui) && ui->driver)
2291 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2292 spin_unlock_irqrestore(&ui->lock, flags);
2293 } else {
2294 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2295 /* S/W workaround, Issue#1 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002296 usb_phy_io_write(ui->xceiv, 0x48, 0x04);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002297 }
2298
2299 /* Ensure pull-up operation is completed before returning */
2300 mb();
2301
2302 return 0;
2303}
2304
2305static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
2306{
2307 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302308 struct msm_otg *otg = to_msm_otg(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002309 unsigned long flags;
2310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002311 atomic_set(&ui->softconnect, is_active);
2312
2313 spin_lock_irqsave(&ui->lock, flags);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302314 if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL ||
2315 atomic_read(&otg->chg_type) == USB_CHG_TYPE__WALLCHARGER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002316 spin_unlock_irqrestore(&ui->lock, flags);
2317 return 0;
2318 }
2319 spin_unlock_irqrestore(&ui->lock, flags);
2320
2321 msm72k_pullup_internal(_gadget, is_active);
2322
2323 if (is_active && !ui->gadget.is_a_peripheral)
2324 schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
2325
2326 return 0;
2327}
2328
2329static int msm72k_wakeup(struct usb_gadget *_gadget)
2330{
2331 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2332 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2333
2334 if (!atomic_read(&ui->remote_wakeup)) {
2335 dev_err(&ui->pdev->dev,
2336 "%s: remote wakeup not supported\n", __func__);
2337 return -ENOTSUPP;
2338 }
2339
2340 if (!atomic_read(&ui->configured)) {
2341 dev_err(&ui->pdev->dev,
2342 "%s: device is not configured\n", __func__);
2343 return -ENODEV;
2344 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002345 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002346
2347 disable_irq(otg->irq);
2348
2349 if (!is_usb_active())
2350 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
2351
2352 /* Ensure that USB port is resumed before enabling the IRQ */
2353 mb();
2354
2355 enable_irq(otg->irq);
2356
2357 return 0;
2358}
2359
2360/* when Gadget is configured, it will indicate how much power
2361 * can be pulled from vbus, as specified in configuiration descriptor
2362 */
2363static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2364{
2365 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2366 unsigned long flags;
2367
2368
2369 spin_lock_irqsave(&ui->lock, flags);
2370 ui->b_max_pow = mA;
2371 ui->flags = USB_FLAG_CONFIGURED;
2372 spin_unlock_irqrestore(&ui->lock, flags);
2373
2374 schedule_work(&ui->work);
2375
2376 return 0;
2377}
2378
2379static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
2380{
2381 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2382 unsigned long flags;
2383 int ret = 0;
2384
2385 spin_lock_irqsave(&ui->lock, flags);
2386 if (set) {
2387 if (ui->pdata && ui->pdata->self_powered)
2388 atomic_set(&ui->self_powered, 1);
2389 else
2390 ret = -EOPNOTSUPP;
2391 } else {
2392 /* We can always work as a bus powered device */
2393 atomic_set(&ui->self_powered, 0);
2394 }
2395 spin_unlock_irqrestore(&ui->lock, flags);
2396
2397 return ret;
2398
2399}
2400
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002401static int msm72k_gadget_start(struct usb_gadget_driver *driver,
2402 int (*bind)(struct usb_gadget *));
2403static int msm72k_gadget_stop(struct usb_gadget_driver *driver);
2404
2405
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002406static const struct usb_gadget_ops msm72k_ops = {
2407 .get_frame = msm72k_get_frame,
2408 .vbus_session = msm72k_udc_vbus_session,
2409 .vbus_draw = msm72k_udc_vbus_draw,
2410 .pullup = msm72k_pullup,
2411 .wakeup = msm72k_wakeup,
2412 .set_selfpowered = msm72k_set_selfpowered,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002413 .start = msm72k_gadget_start,
2414 .stop = msm72k_gadget_stop
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002415};
2416
2417static void usb_do_remote_wakeup(struct work_struct *w)
2418{
2419 struct usb_info *ui = the_usb_info;
2420
2421 msm72k_wakeup(&ui->gadget);
2422}
2423
2424static ssize_t usb_remote_wakeup(struct device *dev,
2425 struct device_attribute *attr, const char *buf, size_t count)
2426{
2427 struct usb_info *ui = the_usb_info;
2428
2429 msm72k_wakeup(&ui->gadget);
2430
2431 return count;
2432}
2433
2434static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
2435 char *buf)
2436{
2437 size_t i;
2438 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
2439 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
2440 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
2441 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
2442 "USB_STATE_SUSPENDED"
2443 };
2444
2445 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
2446 return i;
2447}
2448
2449static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
2450 char *buf)
2451{
2452 struct usb_info *ui = the_usb_info;
2453 size_t i;
2454 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
2455 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
2456
2457 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
2458 return i;
2459}
2460
2461static ssize_t store_usb_chg_current(struct device *dev,
2462 struct device_attribute *attr, const char *buf, size_t count)
2463{
2464 struct usb_info *ui = the_usb_info;
2465 unsigned long mA;
2466
2467 if (ui->gadget.is_a_peripheral)
2468 return -EINVAL;
2469
2470 if (strict_strtoul(buf, 10, &mA))
2471 return -EINVAL;
2472
2473 ui->chg_current = mA;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002474 usb_phy_set_power(ui->xceiv, mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002475
2476 return count;
2477}
2478
2479static ssize_t show_usb_chg_current(struct device *dev,
2480 struct device_attribute *attr, char *buf)
2481{
2482 struct usb_info *ui = the_usb_info;
2483 size_t count;
2484
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302485 count = snprintf(buf, PAGE_SIZE, "%d", ui->chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486
2487 return count;
2488}
2489
2490static ssize_t show_usb_chg_type(struct device *dev,
2491 struct device_attribute *attr, char *buf)
2492{
2493 struct usb_info *ui = the_usb_info;
2494 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2495 size_t count;
2496 char *chg_type[] = {"STD DOWNSTREAM PORT",
2497 "CARKIT",
2498 "DEDICATED CHARGER",
2499 "INVALID"};
2500
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302501 count = snprintf(buf, PAGE_SIZE, "%s",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002502 chg_type[atomic_read(&otg->chg_type)]);
2503
2504 return count;
2505}
2506static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
2507static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
2508static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
2509static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
2510static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
2511 show_usb_chg_current, store_usb_chg_current);
2512
2513#ifdef CONFIG_USB_OTG
2514static ssize_t store_host_req(struct device *dev,
2515 struct device_attribute *attr, const char *buf, size_t count)
2516{
2517 struct usb_info *ui = the_usb_info;
2518 unsigned long val, flags;
2519
2520 if (strict_strtoul(buf, 10, &val))
2521 return -EINVAL;
2522
2523 dev_dbg(&ui->pdev->dev, "%s host request\n",
2524 val ? "set" : "clear");
2525
2526 spin_lock_irqsave(&ui->lock, flags);
2527 if (ui->hnp_avail)
2528 ui->gadget.host_request = !!val;
2529 spin_unlock_irqrestore(&ui->lock, flags);
2530
2531 return count;
2532}
2533static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
2534
2535/* How do we notify user space about HNP availability?
2536 * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
2537 * Introduce hnp_avail flag and set when HNP polling request arrives.
2538 * The expectation is that user space checks hnp availability before
2539 * requesting host role via above sysfs node.
2540 */
2541static ssize_t show_host_avail(struct device *dev,
2542 struct device_attribute *attr, char *buf)
2543{
2544 struct usb_info *ui = the_usb_info;
2545 size_t count;
2546 unsigned long flags;
2547
2548 spin_lock_irqsave(&ui->lock, flags);
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302549 count = snprintf(buf, PAGE_SIZE, "%d\n", ui->hnp_avail);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002550 spin_unlock_irqrestore(&ui->lock, flags);
2551
2552 return count;
2553}
2554static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
2555
2556static struct attribute *otg_attrs[] = {
2557 &dev_attr_host_request.attr,
2558 &dev_attr_host_avail.attr,
2559 NULL,
2560};
2561
2562static struct attribute_group otg_attr_grp = {
2563 .name = "otg",
2564 .attrs = otg_attrs,
2565};
2566#endif
2567
2568static int msm72k_probe(struct platform_device *pdev)
2569{
2570 struct usb_info *ui;
2571 struct msm_otg *otg;
2572 int retval;
2573
2574 dev_dbg(&pdev->dev, "msm72k_probe\n");
2575 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
2576 if (!ui)
2577 return -ENOMEM;
2578
2579 ui->pdev = pdev;
2580 ui->pdata = pdev->dev.platform_data;
2581
2582 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
2583 if (!ui->buf)
2584 return usb_free(ui, -ENOMEM);
2585
2586 ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
2587 if (!ui->pool)
2588 return usb_free(ui, -ENOMEM);
2589
Steve Mucklef132c6c2012-06-06 18:30:57 -07002590 ui->xceiv = usb_get_transceiver();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002591 if (!ui->xceiv)
2592 return usb_free(ui, -ENODEV);
2593
2594 otg = to_msm_otg(ui->xceiv);
2595 ui->addr = otg->regs;
2596
2597 ui->gadget.ops = &msm72k_ops;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002598 ui->gadget.max_speed = USB_SPEED_HIGH;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002599 device_initialize(&ui->gadget.dev);
2600 dev_set_name(&ui->gadget.dev, "gadget");
2601 ui->gadget.dev.parent = &pdev->dev;
2602 ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
2603
2604#ifdef CONFIG_USB_OTG
2605 ui->gadget.is_otg = 1;
2606#endif
2607
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002608 retval = usb_add_gadget_udc(&pdev->dev, &ui->gadget);
2609 if (retval)
2610 return usb_free(ui, retval);
2611
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002612 ui->sdev.name = DRIVER_NAME;
2613 ui->sdev.print_name = print_switch_name;
2614 ui->sdev.print_state = print_switch_state;
2615
2616 retval = switch_dev_register(&ui->sdev);
2617 if (retval)
2618 return usb_free(ui, retval);
2619
2620 the_usb_info = ui;
2621
2622 wake_lock_init(&ui->wlock,
2623 WAKE_LOCK_SUSPEND, "usb_bus_active");
2624
2625 usb_debugfs_init(ui);
2626
2627 usb_prepare(ui);
2628
2629#ifdef CONFIG_USB_OTG
2630 retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
2631 if (retval) {
2632 dev_err(&ui->pdev->dev,
2633 "failed to create otg sysfs directory:"
2634 "err:(%d)\n", retval);
2635 }
2636#endif
2637
Steve Mucklef132c6c2012-06-06 18:30:57 -07002638 retval = otg_set_peripheral(ui->xceiv->otg, &ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002639 if (retval) {
2640 dev_err(&ui->pdev->dev,
2641 "%s: Cannot bind the transceiver, retval:(%d)\n",
2642 __func__, retval);
2643 switch_dev_unregister(&ui->sdev);
2644 wake_lock_destroy(&ui->wlock);
2645 return usb_free(ui, retval);
2646 }
2647
2648 pm_runtime_enable(&pdev->dev);
2649
2650 /* Setup phy stuck timer */
2651 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
2652 setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
2653 return 0;
2654}
2655
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002656static int msm72k_gadget_start(struct usb_gadget_driver *driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002657 int (*bind)(struct usb_gadget *))
2658{
2659 struct usb_info *ui = the_usb_info;
2660 int retval, n;
2661
2662 if (!driver
Steve Mucklef132c6c2012-06-06 18:30:57 -07002663 || driver->max_speed < USB_SPEED_FULL
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002664 || !bind
2665 || !driver->disconnect
2666 || !driver->setup)
2667 return -EINVAL;
2668 if (!ui)
2669 return -ENODEV;
2670 if (ui->driver)
2671 return -EBUSY;
2672
2673 /* first hook up the driver ... */
2674 ui->driver = driver;
2675 ui->gadget.dev.driver = &driver->driver;
2676 ui->gadget.name = driver_name;
2677 INIT_LIST_HEAD(&ui->gadget.ep_list);
2678 ui->gadget.ep0 = &ui->ep0in.ep;
2679 INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
2680 ui->gadget.speed = USB_SPEED_UNKNOWN;
2681 atomic_set(&ui->softconnect, 1);
2682
2683 for (n = 1; n < 16; 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 for (n = 17; n < 32; n++) {
2689 struct msm_endpoint *ept = ui->ept + n;
2690 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2691 ept->ep.maxpacket = 512;
2692 }
2693
2694 retval = device_add(&ui->gadget.dev);
2695 if (retval)
2696 goto fail;
2697
2698 retval = bind(&ui->gadget);
2699 if (retval) {
2700 dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
2701 driver->driver.name, retval);
2702 device_del(&ui->gadget.dev);
2703 goto fail;
2704 }
2705
2706 retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
2707 if (retval != 0)
2708 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2709 "(wakeup) error: (%d)\n", retval);
2710 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
2711 if (retval != 0)
2712 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2713 " (usb_state) error: (%d)\n", retval);
2714
2715 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
2716 if (retval != 0)
2717 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2718 " (usb_speed) error: (%d)\n", retval);
2719
2720 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
2721 if (retval != 0)
2722 dev_err(&ui->pdev->dev,
2723 "failed to create sysfs entry(chg_type): err:(%d)\n",
2724 retval);
2725 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
2726 if (retval != 0)
2727 dev_err(&ui->pdev->dev,
2728 "failed to create sysfs entry(chg_current):"
2729 "err:(%d)\n", retval);
2730
2731 dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
2732 driver->driver.name);
2733 usb_start(ui);
2734
2735 return 0;
2736
2737fail:
2738 ui->driver = NULL;
2739 ui->gadget.dev.driver = NULL;
2740 return retval;
2741}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002742
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002743static int msm72k_gadget_stop(struct usb_gadget_driver *driver)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002744{
2745 struct usb_info *dev = the_usb_info;
2746
2747 if (!dev)
2748 return -ENODEV;
2749 if (!driver || driver != dev->driver || !driver->unbind)
2750 return -EINVAL;
2751
2752 msm72k_pullup_internal(&dev->gadget, 0);
2753 if (dev->irq) {
2754 free_irq(dev->irq, dev);
2755 dev->irq = 0;
2756 }
2757 dev->state = USB_STATE_IDLE;
2758 atomic_set(&dev->configured, 0);
2759 switch_set_state(&dev->sdev, 0);
2760 /* cancel pending ep0 transactions */
2761 flush_endpoint(&dev->ep0out);
2762 flush_endpoint(&dev->ep0in);
2763
2764 device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
2765 device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
2766 device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
2767 device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
2768 device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
2769 driver->disconnect(&dev->gadget);
2770 driver->unbind(&dev->gadget);
2771 dev->gadget.dev.driver = NULL;
2772 dev->driver = NULL;
2773
2774 device_del(&dev->gadget.dev);
2775
2776 dev_dbg(&dev->pdev->dev,
2777 "unregistered gadget driver '%s'\n", driver->driver.name);
2778 return 0;
2779}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002780
2781
2782static int msm72k_udc_runtime_suspend(struct device *dev)
2783{
2784 dev_dbg(dev, "pm_runtime: suspending...\n");
2785 return 0;
2786}
2787
2788static int msm72k_udc_runtime_resume(struct device *dev)
2789{
2790 dev_dbg(dev, "pm_runtime: resuming...\n");
2791 return 0;
2792}
2793
2794static int msm72k_udc_runtime_idle(struct device *dev)
2795{
2796 dev_dbg(dev, "pm_runtime: idling...\n");
2797 return 0;
2798}
2799
2800static struct dev_pm_ops msm72k_udc_dev_pm_ops = {
2801 .runtime_suspend = msm72k_udc_runtime_suspend,
2802 .runtime_resume = msm72k_udc_runtime_resume,
2803 .runtime_idle = msm72k_udc_runtime_idle
2804};
2805
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002806static int __exit msm72k_remove(struct platform_device *pdev)
2807{
2808 struct usb_info *ui = container_of(&pdev, struct usb_info, pdev);
2809
2810 return usb_free(ui, 0);
2811}
2812
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002813static struct platform_driver usb_driver = {
2814 .probe = msm72k_probe,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002815 .remove = msm72k_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002816 .driver = { .name = "msm_hsusb",
2817 .pm = &msm72k_udc_dev_pm_ops, },
2818};
2819
2820static int __init init(void)
2821{
2822 return platform_driver_register(&usb_driver);
2823}
2824module_init(init);
2825
2826static void __exit cleanup(void)
2827{
2828 platform_driver_unregister(&usb_driver);
2829}
2830module_exit(cleanup);
2831
2832MODULE_DESCRIPTION(DRIVER_DESC);
2833MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
2834MODULE_LICENSE("GPL");