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