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