blob: 297c183b8abf38649b5bbeaa8408b5c30b21263f [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
705 if (req->busy) {
706 req->req.status = -EBUSY;
707 spin_unlock_irqrestore(&ui->lock, flags);
708 dev_err(&ui->pdev->dev,
709 "usb_ept_queue_xfer() tried to queue busy request\n");
710 return -EBUSY;
711 }
712
713 if (!atomic_read(&ui->configured) && (ept->num != 0)) {
714 req->req.status = -ESHUTDOWN;
715 spin_unlock_irqrestore(&ui->lock, flags);
716 if (printk_ratelimit())
717 dev_err(&ui->pdev->dev,
718 "%s: called while offline\n", __func__);
719 return -ESHUTDOWN;
720 }
721
722 if (ui->usb_state == USB_STATE_SUSPENDED) {
723 if (!atomic_read(&ui->remote_wakeup)) {
724 req->req.status = -EAGAIN;
725 spin_unlock_irqrestore(&ui->lock, flags);
726 if (printk_ratelimit())
727 dev_err(&ui->pdev->dev,
728 "%s: cannot queue as bus is suspended "
729 "ept #%d %s max:%d head:%p bit:%d\n",
730 __func__, ept->num,
731 (ept->flags & EPT_FLAG_IN) ? "in" : "out",
732 ept->ep.maxpacket, ept->head, ept->bit);
733
734 return -EAGAIN;
735 }
736
737 wake_lock(&ui->wlock);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700738 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 schedule_delayed_work(&ui->rw_work, REMOTE_WAKEUP_DELAY);
740 }
741
742 req->busy = 1;
743 req->live = 0;
744 req->next = 0;
745 req->req.status = -EBUSY;
746
747 req->dma = dma_map_single(NULL, req->req.buf, length,
748 (ept->flags & EPT_FLAG_IN) ?
749 DMA_TO_DEVICE : DMA_FROM_DEVICE);
750
751
752 /* Add the new request to the end of the queue */
753 last = ept->last;
754 if (last) {
755 /* Already requests in the queue. add us to the
756 * end, but let the completion interrupt actually
757 * start things going, to avoid hw issues
758 */
759 last->next = req;
760 req->prev = last;
761
762 } else {
763 /* queue was empty -- kick the hardware */
764 ept->req = req;
765 req->prev = NULL;
766 usb_ept_start(ept);
767 }
768 ept->last = req;
769
770 spin_unlock_irqrestore(&ui->lock, flags);
771 return 0;
772}
773
774/* --- endpoint 0 handling --- */
775
776static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
777{
778 struct msm_request *r = to_msm_request(req);
779 struct msm_endpoint *ept = to_msm_endpoint(ep);
780 struct usb_info *ui = ept->ui;
781
782 req->complete = r->gadget_complete;
783 r->gadget_complete = 0;
784 if (req->complete)
785 req->complete(&ui->ep0in.ep, req);
786}
787
788static void ep0_status_complete(struct usb_ep *ep, struct usb_request *_req)
789{
790 struct usb_request *req = _req->context;
791 struct msm_request *r;
792 struct msm_endpoint *ept;
793 struct usb_info *ui;
794
795 pr_debug("%s:\n", __func__);
796 if (!req)
797 return;
798
799 r = to_msm_request(req);
800 ept = to_msm_endpoint(ep);
801 ui = ept->ui;
802 _req->context = 0;
803
804 req->complete = r->gadget_complete;
805 req->zero = 0;
806 r->gadget_complete = 0;
807 if (req->complete)
808 req->complete(&ui->ep0in.ep, req);
809
810}
811
812static void ep0_status_phase(struct usb_ep *ep, struct usb_request *req)
813{
814 struct msm_endpoint *ept = to_msm_endpoint(ep);
815 struct usb_info *ui = ept->ui;
816
817 pr_debug("%s:\n", __func__);
818
819 req->length = 0;
820 req->complete = ep0_status_complete;
821
822 /* status phase */
823 if (atomic_read(&ui->ep0_dir) == USB_DIR_IN)
824 usb_ept_queue_xfer(&ui->ep0out, req);
825 else
826 usb_ept_queue_xfer(&ui->ep0in, req);
827}
828
829static void ep0in_send_zero_leng_pkt(struct msm_endpoint *ept)
830{
831 struct usb_info *ui = ept->ui;
832 struct usb_request *req = ui->setup_req;
833
834 pr_debug("%s:\n", __func__);
835
836 req->length = 0;
837 req->complete = ep0_status_phase;
838 usb_ept_queue_xfer(&ui->ep0in, req);
839}
840
841static void ep0_queue_ack_complete(struct usb_ep *ep,
842 struct usb_request *_req)
843{
844 struct msm_endpoint *ept = to_msm_endpoint(ep);
845 struct usb_info *ui = ept->ui;
846 struct usb_request *req = ui->setup_req;
847
848 pr_debug("%s: _req:%p actual:%d length:%d zero:%d\n",
849 __func__, _req, _req->actual,
850 _req->length, _req->zero);
851
852 /* queue up the receive of the ACK response from the host */
853 if (_req->status == 0 && _req->actual == _req->length) {
854 req->context = _req;
855 if (atomic_read(&ui->ep0_dir) == USB_DIR_IN) {
856 if (_req->zero && _req->length &&
857 !(_req->length % ep->maxpacket)) {
858 ep0in_send_zero_leng_pkt(&ui->ep0in);
859 return;
860 }
861 }
862 ep0_status_phase(ep, req);
863 } else
864 ep0_complete(ep, _req);
865}
866
867static void ep0_setup_ack_complete(struct usb_ep *ep, struct usb_request *req)
868{
869 struct msm_endpoint *ept = to_msm_endpoint(ep);
870 struct usb_info *ui = ept->ui;
871 unsigned int temp;
872 int test_mode = atomic_read(&ui->test_mode);
873
874 if (!test_mode)
875 return;
876
877 switch (test_mode) {
878 case J_TEST:
879 dev_info(&ui->pdev->dev, "usb electrical test mode: (J)\n");
880 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
881 writel(temp | PORTSC_PTC_J_STATE, USB_PORTSC);
882 break;
883
884 case K_TEST:
885 dev_info(&ui->pdev->dev, "usb electrical test mode: (K)\n");
886 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
887 writel(temp | PORTSC_PTC_K_STATE, USB_PORTSC);
888 break;
889
890 case SE0_NAK_TEST:
891 dev_info(&ui->pdev->dev,
892 "usb electrical test mode: (SE0-NAK)\n");
893 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
894 writel(temp | PORTSC_PTC_SE0_NAK, USB_PORTSC);
895 break;
896
897 case TST_PKT_TEST:
898 dev_info(&ui->pdev->dev,
899 "usb electrical test mode: (TEST_PKT)\n");
900 temp = readl(USB_PORTSC) & (~PORTSC_PTC);
901 writel(temp | PORTSC_PTC_TST_PKT, USB_PORTSC);
902 break;
903 }
904}
905
906static void ep0_setup_ack(struct usb_info *ui)
907{
908 struct usb_request *req = ui->setup_req;
909 req->length = 0;
910 req->complete = ep0_setup_ack_complete;
911 usb_ept_queue_xfer(&ui->ep0in, req);
912}
913
914static void ep0_setup_stall(struct usb_info *ui)
915{
916 writel((1<<16) | (1<<0), USB_ENDPTCTRL(0));
917}
918
919static void ep0_setup_send(struct usb_info *ui, unsigned length)
920{
921 struct usb_request *req = ui->setup_req;
922 struct msm_request *r = to_msm_request(req);
923 struct msm_endpoint *ept = &ui->ep0in;
924
925 req->length = length;
926 req->complete = ep0_queue_ack_complete;
927 r->gadget_complete = 0;
928 usb_ept_queue_xfer(ept, req);
929}
930
931static void handle_setup(struct usb_info *ui)
932{
933 struct usb_ctrlrequest ctl;
934 struct usb_request *req = ui->setup_req;
935 int ret;
936#ifdef CONFIG_USB_OTG
937 u8 hnp;
938 unsigned long flags;
939#endif
Vijayavardhan Vennapusa09bbcb32011-08-30 02:13:26 +0530940 /* USB hardware sometimes generate interrupt before
941 * 8 bytes of SETUP packet are written to system memory.
942 * This results in fetching wrong setup_data sometimes.
943 * TODO: Remove below workaround of adding 1us delay once
944 * it gets fixed in hardware.
945 */
946 udelay(10);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947
948 memcpy(&ctl, ui->ep0out.head->setup_data, sizeof(ctl));
949 /* Ensure buffer is read before acknowledging to h/w */
950 mb();
951
952 writel(EPT_RX(0), USB_ENDPTSETUPSTAT);
953
954 if (ctl.bRequestType & USB_DIR_IN)
955 atomic_set(&ui->ep0_dir, USB_DIR_IN);
956 else
957 atomic_set(&ui->ep0_dir, USB_DIR_OUT);
958
959 /* any pending ep0 transactions must be canceled */
960 flush_endpoint(&ui->ep0out);
961 flush_endpoint(&ui->ep0in);
962
963 dev_dbg(&ui->pdev->dev,
964 "setup: type=%02x req=%02x val=%04x idx=%04x len=%04x\n",
965 ctl.bRequestType, ctl.bRequest, ctl.wValue,
966 ctl.wIndex, ctl.wLength);
967
968 if ((ctl.bRequestType & (USB_DIR_IN | USB_TYPE_MASK)) ==
969 (USB_DIR_IN | USB_TYPE_STANDARD)) {
970 if (ctl.bRequest == USB_REQ_GET_STATUS) {
971 /* OTG supplement Rev 2.0 introduces another device
972 * GET_STATUS request for HNP polling with length = 1.
973 */
974 u8 len = 2;
975 switch (ctl.bRequestType & USB_RECIP_MASK) {
976 case USB_RECIP_ENDPOINT:
977 {
978 struct msm_endpoint *ept;
979 unsigned num =
980 ctl.wIndex & USB_ENDPOINT_NUMBER_MASK;
981 u16 temp = 0;
982
983 if (num == 0) {
984 memset(req->buf, 0, 2);
985 break;
986 }
987 if (ctl.wIndex & USB_ENDPOINT_DIR_MASK)
988 num += 16;
989 ept = &ui->ep0out + num;
990 temp = usb_ep_get_stall(ept);
991 temp = temp << USB_ENDPOINT_HALT;
992 memcpy(req->buf, &temp, 2);
993 break;
994 }
995 case USB_RECIP_DEVICE:
996 {
997 u16 temp = 0;
998
999 if (ctl.wIndex == OTG_STATUS_SELECTOR) {
1000#ifdef CONFIG_USB_OTG
1001 spin_lock_irqsave(&ui->lock, flags);
1002 hnp = (ui->gadget.host_request <<
1003 HOST_REQUEST_FLAG);
1004 ui->hnp_avail = 1;
1005 spin_unlock_irqrestore(&ui->lock,
1006 flags);
1007 memcpy(req->buf, &hnp, 1);
1008 len = 1;
1009#else
1010 goto stall;
1011#endif
1012 } else {
1013 temp = (atomic_read(&ui->self_powered)
1014 << USB_DEVICE_SELF_POWERED);
1015 temp |= (atomic_read(&ui->remote_wakeup)
1016 << USB_DEVICE_REMOTE_WAKEUP);
1017 memcpy(req->buf, &temp, 2);
1018 }
1019 break;
1020 }
1021 case USB_RECIP_INTERFACE:
1022 memset(req->buf, 0, 2);
1023 break;
1024 default:
1025 goto stall;
1026 }
1027 ep0_setup_send(ui, len);
1028 return;
1029 }
1030 }
1031 if (ctl.bRequestType ==
1032 (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)) {
1033 if ((ctl.bRequest == USB_REQ_CLEAR_FEATURE) ||
1034 (ctl.bRequest == USB_REQ_SET_FEATURE)) {
1035 if ((ctl.wValue == 0) && (ctl.wLength == 0)) {
1036 unsigned num = ctl.wIndex & 0x0f;
1037
1038 if (num != 0) {
1039 struct msm_endpoint *ept;
1040
1041 if (ctl.wIndex & 0x80)
1042 num += 16;
1043 ept = &ui->ep0out + num;
1044
1045 if (ept->wedged)
1046 goto ack;
1047 if (ctl.bRequest == USB_REQ_SET_FEATURE)
1048 usb_ept_set_halt(&ept->ep, 1);
1049 else
1050 usb_ept_set_halt(&ept->ep, 0);
1051 }
1052 goto ack;
1053 }
1054 }
1055 }
1056 if (ctl.bRequestType == (USB_DIR_OUT | USB_TYPE_STANDARD)) {
1057 if (ctl.bRequest == USB_REQ_SET_CONFIGURATION) {
1058 atomic_set(&ui->configured, !!ctl.wValue);
1059 msm_hsusb_set_state(USB_STATE_CONFIGURED);
1060 } else if (ctl.bRequest == USB_REQ_SET_ADDRESS) {
1061 /*
1062 * Gadget speed should be set when PCI interrupt
1063 * occurs. But sometimes, PCI interrupt is not
1064 * occuring after reset. Hence update the gadget
1065 * speed here.
1066 */
1067 if (ui->gadget.speed == USB_SPEED_UNKNOWN) {
1068 dev_info(&ui->pdev->dev,
1069 "PCI intr missed"
1070 "set speed explictly\n");
1071 msm_hsusb_set_speed(ui);
1072 }
1073 msm_hsusb_set_state(USB_STATE_ADDRESS);
1074
1075 /* write address delayed (will take effect
1076 ** after the next IN txn)
1077 */
1078 writel((ctl.wValue << 25) | (1 << 24), USB_DEVICEADDR);
1079 goto ack;
1080 } else if (ctl.bRequest == USB_REQ_SET_FEATURE) {
1081 switch (ctl.wValue) {
1082 case USB_DEVICE_TEST_MODE:
1083 switch (ctl.wIndex) {
1084 case J_TEST:
1085 case K_TEST:
1086 case SE0_NAK_TEST:
1087 case TST_PKT_TEST:
1088 atomic_set(&ui->test_mode, ctl.wIndex);
1089 goto ack;
1090 }
1091 goto stall;
1092 case USB_DEVICE_REMOTE_WAKEUP:
1093 atomic_set(&ui->remote_wakeup, 1);
1094 goto ack;
1095#ifdef CONFIG_USB_OTG
1096 case USB_DEVICE_B_HNP_ENABLE:
1097 ui->gadget.b_hnp_enable = 1;
1098 goto ack;
1099 case USB_DEVICE_A_HNP_SUPPORT:
1100 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1101 /* B-devices compliant to OTG spec
1102 * Rev 2.0 are not required to
1103 * suppport these features.
1104 */
1105 goto stall;
1106#endif
1107 }
1108 } else if ((ctl.bRequest == USB_REQ_CLEAR_FEATURE) &&
1109 (ctl.wValue == USB_DEVICE_REMOTE_WAKEUP)) {
1110 atomic_set(&ui->remote_wakeup, 0);
1111 goto ack;
1112 }
1113 }
1114
1115 /* delegate if we get here */
1116 if (ui->driver) {
1117 ret = ui->driver->setup(&ui->gadget, &ctl);
1118 if (ret >= 0)
1119 return;
1120 }
1121
1122stall:
1123 /* stall ep0 on error */
1124 ep0_setup_stall(ui);
1125 return;
1126
1127ack:
1128 ep0_setup_ack(ui);
1129}
1130
1131static void handle_endpoint(struct usb_info *ui, unsigned bit)
1132{
1133 struct msm_endpoint *ept = ui->ept + bit;
1134 struct msm_request *req;
1135 unsigned long flags;
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +05301136 int req_dequeue = 1;
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05301137 int dtd_update_fail_count_chk = 10;
1138 int check_bit = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139 unsigned info;
1140
1141 /*
1142 INFO("handle_endpoint() %d %s req=%p(%08x)\n",
1143 ept->num, (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1144 ept->req, ept->req ? ept->req->item_dma : 0);
1145 */
1146
1147 /* expire all requests that are no longer active */
1148 spin_lock_irqsave(&ui->lock, flags);
1149 while ((req = ept->req)) {
1150 /* if we've processed all live requests, time to
1151 * restart the hardware on the next non-live request
1152 */
1153 if (!req->live) {
1154 usb_ept_start(ept);
1155 break;
1156 }
1157
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +05301158dequeue:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 /* clean speculative fetches on req->item->info */
1160 dma_coherent_post_ops();
1161 info = req->item->info;
1162 /* if the transaction is still in-flight, stop here */
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +05301163 if (info & INFO_ACTIVE) {
1164 if (req_dequeue) {
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +05301165 ui->dTD_update_fail_count++;
1166 ept->dTD_update_fail_count++;
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05301167 udelay(1);
1168 if (!dtd_update_fail_count_chk--) {
1169 req_dequeue = 0;
1170 check_bit = 1;
1171 }
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +05301172 goto dequeue;
1173 } else {
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05301174 if (check_bit) {
1175 pr_debug("%s: Delay Workaround Failed\n",
1176 __func__);
1177 check_bit = 0;
1178 ui->dTD_workaround_fail_count++;
1179 ept->dTD_workaround_fail_count++;
1180 }
Chiranjeevi Velempatifbd057d2012-05-14 16:11:35 +05301181 break;
1182 }
1183 }
1184 req_dequeue = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185
Vijayavardhan Vennapusafd8df252011-12-08 16:19:37 +05301186 del_timer(&ept->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187 /* advance ept queue to the next request */
1188 ept->req = req->next;
1189 if (ept->req == 0)
1190 ept->last = 0;
1191
1192 dma_unmap_single(NULL, req->dma, req->req.length,
1193 (ept->flags & EPT_FLAG_IN) ?
1194 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1195
1196 if (info & (INFO_HALTED | INFO_BUFFER_ERROR | INFO_TXN_ERROR)) {
1197 /* XXX pass on more specific error code */
1198 req->req.status = -EIO;
1199 req->req.actual = 0;
1200 dev_err(&ui->pdev->dev,
1201 "ept %d %s error. info=%08x\n",
1202 ept->num,
1203 (ept->flags & EPT_FLAG_IN) ? "in" : "out",
1204 info);
1205 } else {
1206 req->req.status = 0;
1207 req->req.actual =
1208 req->req.length - ((info >> 16) & 0x7FFF);
1209 }
1210 req->busy = 0;
1211 req->live = 0;
1212
1213 if (req->req.complete) {
1214 spin_unlock_irqrestore(&ui->lock, flags);
1215 req->req.complete(&ept->ep, &req->req);
1216 spin_lock_irqsave(&ui->lock, flags);
1217 }
1218 }
1219 spin_unlock_irqrestore(&ui->lock, flags);
1220}
1221
1222static void flush_endpoint_hw(struct usb_info *ui, unsigned bits)
1223{
1224 /* flush endpoint, canceling transactions
1225 ** - this can take a "large amount of time" (per databook)
1226 ** - the flush can fail in some cases, thus we check STAT
1227 ** and repeat if we're still operating
1228 ** (does the fact that this doesn't use the tripwire matter?!)
1229 */
1230 do {
1231 writel(bits, USB_ENDPTFLUSH);
1232 while (readl(USB_ENDPTFLUSH) & bits)
1233 udelay(100);
1234 } while (readl(USB_ENDPTSTAT) & bits);
1235}
1236
1237static void flush_endpoint_sw(struct msm_endpoint *ept)
1238{
1239 struct usb_info *ui = ept->ui;
1240 struct msm_request *req, *next_req = NULL;
1241 unsigned long flags;
1242
1243 /* inactive endpoints have nothing to do here */
1244 if (ept->ep.maxpacket == 0)
1245 return;
1246
1247 /* put the queue head in a sane state */
1248 ept->head->info = 0;
1249 ept->head->next = TERMINATE;
1250
1251 /* cancel any pending requests */
1252 spin_lock_irqsave(&ui->lock, flags);
1253 req = ept->req;
1254 ept->req = 0;
1255 ept->last = 0;
1256 while (req != 0) {
1257 req->busy = 0;
1258 req->live = 0;
1259 req->req.status = -ESHUTDOWN;
1260 req->req.actual = 0;
1261
1262 /* Gadget driver may free the request in completion
1263 * handler. So keep a copy of next req pointer
1264 * before calling completion handler.
1265 */
1266 next_req = req->next;
1267 if (req->req.complete) {
1268 spin_unlock_irqrestore(&ui->lock, flags);
1269 req->req.complete(&ept->ep, &req->req);
1270 spin_lock_irqsave(&ui->lock, flags);
1271 }
1272 req = next_req;
1273 }
1274 spin_unlock_irqrestore(&ui->lock, flags);
1275}
1276
1277static void flush_endpoint(struct msm_endpoint *ept)
1278{
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301279 del_timer(&ept->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280 flush_endpoint_hw(ept->ui, (1 << ept->bit));
1281 flush_endpoint_sw(ept);
1282}
1283
1284static irqreturn_t usb_interrupt(int irq, void *data)
1285{
1286 struct usb_info *ui = data;
1287 unsigned n;
1288 unsigned long flags;
1289
1290 n = readl(USB_USBSTS);
1291 writel(n, USB_USBSTS);
1292
1293 /* somehow we got an IRQ while in the reset sequence: ignore it */
1294 if (!atomic_read(&ui->running))
1295 return IRQ_HANDLED;
1296
1297 if (n & STS_PCI) {
1298 msm_hsusb_set_speed(ui);
1299 if (atomic_read(&ui->configured)) {
1300 wake_lock(&ui->wlock);
1301
1302 spin_lock_irqsave(&ui->lock, flags);
1303 ui->usb_state = USB_STATE_CONFIGURED;
1304 ui->flags = USB_FLAG_CONFIGURED;
1305 spin_unlock_irqrestore(&ui->lock, flags);
1306
1307 ui->driver->resume(&ui->gadget);
1308 schedule_work(&ui->work);
1309 } else {
1310 msm_hsusb_set_state(USB_STATE_DEFAULT);
1311 }
1312
1313#ifdef CONFIG_USB_OTG
1314 /* notify otg to clear A_BIDL_ADIS timer */
1315 if (ui->gadget.is_a_peripheral)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001316 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317#endif
1318 }
1319
1320 if (n & STS_URI) {
1321 dev_dbg(&ui->pdev->dev, "reset\n");
1322 spin_lock_irqsave(&ui->lock, flags);
1323 ui->gadget.speed = USB_SPEED_UNKNOWN;
1324 spin_unlock_irqrestore(&ui->lock, flags);
1325#ifdef CONFIG_USB_OTG
1326 /* notify otg to clear A_BIDL_ADIS timer */
1327 if (ui->gadget.is_a_peripheral)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001328 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 spin_lock_irqsave(&ui->lock, flags);
1330 /* Host request is persistent across reset */
1331 ui->gadget.b_hnp_enable = 0;
1332 ui->hnp_avail = 0;
1333 spin_unlock_irqrestore(&ui->lock, flags);
1334#endif
1335 msm_hsusb_set_state(USB_STATE_DEFAULT);
1336 atomic_set(&ui->remote_wakeup, 0);
1337 if (!ui->gadget.is_a_peripheral)
1338 schedule_delayed_work(&ui->chg_stop, 0);
1339
1340 writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
1341 writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
1342 writel(0xffffffff, USB_ENDPTFLUSH);
1343 writel(0, USB_ENDPTCTRL(1));
1344
1345 wake_lock(&ui->wlock);
1346 if (atomic_read(&ui->configured)) {
1347 /* marking us offline will cause ept queue attempts
1348 ** to fail
1349 */
1350 atomic_set(&ui->configured, 0);
1351 /* Defer sending offline uevent to userspace */
1352 atomic_set(&ui->offline_pending, 1);
1353
1354 /* XXX: we can't seem to detect going offline,
1355 * XXX: so deconfigure on reset for the time being
1356 */
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05301357 dev_dbg(&ui->pdev->dev,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358 "usb: notify offline\n");
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05301359 ui->driver->disconnect(&ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360 /* cancel pending ep0 transactions */
1361 flush_endpoint(&ui->ep0out);
1362 flush_endpoint(&ui->ep0in);
1363
1364 }
1365 /* Start phy stuck timer */
1366 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1367 mod_timer(&phy_status_timer, PHY_STATUS_CHECK_DELAY);
1368 }
1369
1370 if (n & STS_SLI) {
1371 dev_dbg(&ui->pdev->dev, "suspend\n");
1372
1373 spin_lock_irqsave(&ui->lock, flags);
1374 ui->usb_state = USB_STATE_SUSPENDED;
1375 ui->flags = USB_FLAG_SUSPEND;
1376 spin_unlock_irqrestore(&ui->lock, flags);
1377
1378 ui->driver->suspend(&ui->gadget);
1379 schedule_work(&ui->work);
1380#ifdef CONFIG_USB_OTG
1381 /* notify otg for
1382 * 1. kicking A_BIDL_ADIS timer in case of A-peripheral
1383 * 2. disabling pull-up and kicking B_ASE0_RST timer
1384 */
1385 if (ui->gadget.b_hnp_enable || ui->gadget.is_a_peripheral)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001386 usb_phy_set_suspend(ui->xceiv, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001387#endif
1388 }
1389
1390 if (n & STS_UI) {
1391 n = readl(USB_ENDPTSETUPSTAT);
1392 if (n & EPT_RX(0))
1393 handle_setup(ui);
1394
1395 n = readl(USB_ENDPTCOMPLETE);
1396 writel(n, USB_ENDPTCOMPLETE);
1397 while (n) {
1398 unsigned bit = __ffs(n);
1399 handle_endpoint(ui, bit);
1400 n = n & (~(1 << bit));
1401 }
1402 }
1403 return IRQ_HANDLED;
1404}
1405
1406static void usb_prepare(struct usb_info *ui)
1407{
1408 spin_lock_init(&ui->lock);
1409
1410 memset(ui->buf, 0, 4096);
1411 ui->head = (void *) (ui->buf + 0);
1412
1413 /* only important for reset/reinit */
1414 memset(ui->ept, 0, sizeof(ui->ept));
1415 ui->next_item = 0;
1416 ui->next_ifc_num = 0;
1417
1418 init_endpoints(ui);
1419
1420 ui->ep0in.ep.maxpacket = 64;
1421 ui->ep0out.ep.maxpacket = 64;
1422
1423 ui->setup_req =
1424 usb_ept_alloc_req(&ui->ep0in, SETUP_BUF_SIZE, GFP_KERNEL);
1425
1426 INIT_WORK(&ui->work, usb_do_work);
1427 INIT_DELAYED_WORK(&ui->chg_det, usb_chg_detect);
1428 INIT_DELAYED_WORK(&ui->chg_stop, usb_chg_stop);
1429 INIT_DELAYED_WORK(&ui->rw_work, usb_do_remote_wakeup);
1430 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1431 INIT_WORK(&ui->phy_status_check, usb_phy_stuck_recover);
1432}
1433
1434static void usb_reset(struct usb_info *ui)
1435{
1436 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1437
1438 dev_dbg(&ui->pdev->dev, "reset controller\n");
1439
1440 atomic_set(&ui->running, 0);
1441
1442 /*
1443 * PHY reset takes minimum 100 msec. Hence reset only link
1444 * during HNP. Reset PHY and link in B-peripheral mode.
1445 */
1446 if (ui->gadget.is_a_peripheral)
1447 otg->reset(ui->xceiv, 0);
1448 else
1449 otg->reset(ui->xceiv, 1);
1450
1451 /* set usb controller interrupt threshold to zero*/
1452 writel((readl(USB_USBCMD) & ~USBCMD_ITC_MASK) | USBCMD_ITC(0),
1453 USB_USBCMD);
1454
1455 writel(ui->dma, USB_ENDPOINTLISTADDR);
1456
1457 configure_endpoints(ui);
1458
1459 /* marking us offline will cause ept queue attempts to fail */
1460 atomic_set(&ui->configured, 0);
1461
1462 if (ui->driver) {
1463 dev_dbg(&ui->pdev->dev, "usb: notify offline\n");
1464 ui->driver->disconnect(&ui->gadget);
1465 }
1466
1467 /* cancel pending ep0 transactions */
1468 flush_endpoint(&ui->ep0out);
1469 flush_endpoint(&ui->ep0in);
1470
1471 /* enable interrupts */
1472 writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR);
1473
1474 /* Ensure that h/w RESET is completed before returning */
1475 mb();
1476
1477 atomic_set(&ui->running, 1);
1478}
1479
1480static void usb_start(struct usb_info *ui)
1481{
1482 unsigned long flags;
1483
1484 spin_lock_irqsave(&ui->lock, flags);
1485 ui->flags |= USB_FLAG_START;
1486 schedule_work(&ui->work);
1487 spin_unlock_irqrestore(&ui->lock, flags);
1488}
1489
1490static int usb_free(struct usb_info *ui, int ret)
1491{
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03001492 if (ret)
1493 dev_dbg(&ui->pdev->dev, "usb_free(%d)\n", ret);
1494
1495 usb_del_gadget_udc(&ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001496
1497 if (ui->xceiv)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001498 usb_put_transceiver(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001499
1500 if (ui->irq)
1501 free_irq(ui->irq, 0);
1502 if (ui->pool)
1503 dma_pool_destroy(ui->pool);
1504 if (ui->dma)
1505 dma_free_coherent(&ui->pdev->dev, 4096, ui->buf, ui->dma);
1506 kfree(ui);
1507 return ret;
1508}
1509
1510static void usb_do_work_check_vbus(struct usb_info *ui)
1511{
1512 unsigned long iflags;
1513
1514 spin_lock_irqsave(&ui->lock, iflags);
1515 if (is_usb_online(ui))
1516 ui->flags |= USB_FLAG_VBUS_ONLINE;
1517 else
1518 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1519 spin_unlock_irqrestore(&ui->lock, iflags);
1520}
1521
1522static void usb_do_work(struct work_struct *w)
1523{
1524 struct usb_info *ui = container_of(w, struct usb_info, work);
1525 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1526 unsigned long iflags;
1527 unsigned flags, _vbus;
1528
1529 for (;;) {
1530 spin_lock_irqsave(&ui->lock, iflags);
1531 flags = ui->flags;
1532 ui->flags = 0;
1533 _vbus = is_usb_online(ui);
1534 spin_unlock_irqrestore(&ui->lock, iflags);
1535
1536 /* give up if we have nothing to do */
1537 if (flags == 0)
1538 break;
1539
1540 switch (ui->state) {
1541 case USB_STATE_IDLE:
1542 if (flags & USB_FLAG_START) {
1543 int ret;
1544
1545 if (!_vbus) {
1546 ui->state = USB_STATE_OFFLINE;
1547 break;
1548 }
1549
1550 pm_runtime_get_noresume(&ui->pdev->dev);
1551 pm_runtime_resume(&ui->pdev->dev);
1552 dev_dbg(&ui->pdev->dev,
1553 "msm72k_udc: IDLE -> ONLINE\n");
1554 usb_reset(ui);
1555 ret = request_irq(otg->irq, usb_interrupt,
1556 IRQF_SHARED,
1557 ui->pdev->name, ui);
1558 /* FIXME: should we call BUG_ON when
1559 * requst irq fails
1560 */
1561 if (ret) {
1562 dev_err(&ui->pdev->dev,
1563 "hsusb: peripheral: request irq"
1564 " failed:(%d)", ret);
1565 break;
1566 }
1567 ui->irq = otg->irq;
1568 ui->state = USB_STATE_ONLINE;
1569 usb_do_work_check_vbus(ui);
1570
1571 if (!atomic_read(&ui->softconnect))
1572 break;
1573
1574 msm72k_pullup_internal(&ui->gadget, 1);
1575
1576 if (!ui->gadget.is_a_peripheral)
1577 schedule_delayed_work(
1578 &ui->chg_det,
1579 USB_CHG_DET_DELAY);
1580
1581 }
1582 break;
1583 case USB_STATE_ONLINE:
1584 if (atomic_read(&ui->offline_pending)) {
1585 switch_set_state(&ui->sdev, 0);
1586 atomic_set(&ui->offline_pending, 0);
1587 }
1588
1589 /* If at any point when we were online, we received
1590 * the signal to go offline, we must honor it
1591 */
1592 if (flags & USB_FLAG_VBUS_OFFLINE) {
1593
1594 ui->chg_current = 0;
1595 /* wait incase chg_detect is running */
1596 if (!ui->gadget.is_a_peripheral)
1597 cancel_delayed_work_sync(&ui->chg_det);
1598
1599 dev_dbg(&ui->pdev->dev,
1600 "msm72k_udc: ONLINE -> OFFLINE\n");
1601
1602 atomic_set(&ui->running, 0);
1603 atomic_set(&ui->remote_wakeup, 0);
1604 atomic_set(&ui->configured, 0);
1605
1606 if (ui->driver) {
1607 dev_dbg(&ui->pdev->dev,
1608 "usb: notify offline\n");
1609 ui->driver->disconnect(&ui->gadget);
1610 }
1611 /* cancel pending ep0 transactions */
1612 flush_endpoint(&ui->ep0out);
1613 flush_endpoint(&ui->ep0in);
1614
1615 /* synchronize with irq context */
1616 spin_lock_irqsave(&ui->lock, iflags);
1617#ifdef CONFIG_USB_OTG
1618 ui->gadget.host_request = 0;
1619 ui->gadget.b_hnp_enable = 0;
1620 ui->hnp_avail = 0;
1621#endif
1622 msm72k_pullup_internal(&ui->gadget, 0);
1623 spin_unlock_irqrestore(&ui->lock, iflags);
1624
1625
1626 /* if charger is initialized to known type
1627 * we must let modem know about charger
1628 * disconnection
1629 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001630 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631
1632 if (ui->irq) {
1633 free_irq(ui->irq, ui);
1634 ui->irq = 0;
1635 }
1636
1637
1638 switch_set_state(&ui->sdev, 0);
1639
1640 ui->state = USB_STATE_OFFLINE;
1641 usb_do_work_check_vbus(ui);
1642 pm_runtime_put_noidle(&ui->pdev->dev);
1643 pm_runtime_suspend(&ui->pdev->dev);
1644 wake_unlock(&ui->wlock);
1645 break;
1646 }
1647 if (flags & USB_FLAG_SUSPEND) {
1648 int maxpower = usb_get_max_power(ui);
1649
1650 if (maxpower < 0)
1651 break;
1652
Steve Mucklef132c6c2012-06-06 18:30:57 -07001653 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001654 /* To support TCXO during bus suspend
1655 * This might be dummy check since bus suspend
1656 * is not implemented as of now
1657 * */
1658 if (release_wlocks)
1659 wake_unlock(&ui->wlock);
1660
1661 /* TBD: Initiate LPM at usb bus suspend */
1662 break;
1663 }
1664 if (flags & USB_FLAG_CONFIGURED) {
1665 int maxpower = usb_get_max_power(ui);
1666
1667 /* We may come here even when no configuration
1668 * is selected. Send online/offline event
1669 * accordingly.
1670 */
1671 switch_set_state(&ui->sdev,
1672 atomic_read(&ui->configured));
1673
1674 if (maxpower < 0)
1675 break;
1676
1677 ui->chg_current = maxpower;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001678 usb_phy_set_power(ui->xceiv, maxpower);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679 break;
1680 }
1681 if (flags & USB_FLAG_RESET) {
1682 dev_dbg(&ui->pdev->dev,
1683 "msm72k_udc: ONLINE -> RESET\n");
1684 msm72k_pullup_internal(&ui->gadget, 0);
1685 usb_reset(ui);
1686 msm72k_pullup_internal(&ui->gadget, 1);
1687 dev_dbg(&ui->pdev->dev,
1688 "msm72k_udc: RESET -> ONLINE\n");
1689 break;
1690 }
1691 break;
1692 case USB_STATE_OFFLINE:
1693 /* If we were signaled to go online and vbus is still
1694 * present when we received the signal, go online.
1695 */
1696 if ((flags & USB_FLAG_VBUS_ONLINE) && _vbus) {
1697 int ret;
1698
1699 pm_runtime_get_noresume(&ui->pdev->dev);
1700 pm_runtime_resume(&ui->pdev->dev);
1701 dev_dbg(&ui->pdev->dev,
1702 "msm72k_udc: OFFLINE -> ONLINE\n");
1703
1704 usb_reset(ui);
1705 ui->state = USB_STATE_ONLINE;
1706 usb_do_work_check_vbus(ui);
1707 ret = request_irq(otg->irq, usb_interrupt,
1708 IRQF_SHARED,
1709 ui->pdev->name, ui);
1710 /* FIXME: should we call BUG_ON when
1711 * requst irq fails
1712 */
1713 if (ret) {
1714 dev_err(&ui->pdev->dev,
1715 "hsusb: peripheral: request irq"
1716 " failed:(%d)", ret);
1717 break;
1718 }
1719 ui->irq = otg->irq;
1720 enable_irq_wake(otg->irq);
1721
1722 if (!atomic_read(&ui->softconnect))
1723 break;
1724 msm72k_pullup_internal(&ui->gadget, 1);
1725
1726 if (!ui->gadget.is_a_peripheral)
1727 schedule_delayed_work(
1728 &ui->chg_det,
1729 USB_CHG_DET_DELAY);
1730 }
1731 break;
1732 }
1733 }
1734}
1735
1736/* FIXME - the callers of this function should use a gadget API instead.
1737 * This is called from htc_battery.c and board-halibut.c
1738 * WARNING - this can get called before this driver is initialized.
1739 */
1740void msm_hsusb_set_vbus_state(int online)
1741{
1742 unsigned long flags;
1743 struct usb_info *ui = the_usb_info;
1744
1745 if (!ui) {
1746 pr_err("%s called before driver initialized\n", __func__);
1747 return;
1748 }
1749
1750 spin_lock_irqsave(&ui->lock, flags);
1751
1752 if (is_usb_online(ui) == online)
1753 goto out;
1754
1755 if (online) {
1756 ui->usb_state = USB_STATE_POWERED;
1757 ui->flags |= USB_FLAG_VBUS_ONLINE;
1758 } else {
1759 ui->gadget.speed = USB_SPEED_UNKNOWN;
1760 ui->usb_state = USB_STATE_NOTATTACHED;
1761 ui->flags |= USB_FLAG_VBUS_OFFLINE;
Chiranjeevi Velempati18dda792012-07-16 17:02:32 +05301762 ui->chg_type_retry_cnt = 0;
1763 ui->proprietary_chg = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 }
1765 if (in_interrupt()) {
1766 schedule_work(&ui->work);
1767 } else {
1768 spin_unlock_irqrestore(&ui->lock, flags);
1769 usb_do_work(&ui->work);
1770 return;
1771 }
1772out:
1773 spin_unlock_irqrestore(&ui->lock, flags);
1774}
1775
1776#if defined(CONFIG_DEBUG_FS)
1777
1778void usb_function_reenumerate(void)
1779{
1780 struct usb_info *ui = the_usb_info;
1781
1782 /* disable and re-enable the D+ pullup */
1783 dev_dbg(&ui->pdev->dev, "disable pullup\n");
1784 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
1785
1786 msleep(10);
1787
1788 dev_dbg(&ui->pdev->dev, "enable pullup\n");
1789 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
1790}
1791
1792static char debug_buffer[PAGE_SIZE];
1793
1794static ssize_t debug_read_status(struct file *file, char __user *ubuf,
1795 size_t count, loff_t *ppos)
1796{
1797 struct usb_info *ui = file->private_data;
1798 char *buf = debug_buffer;
1799 unsigned long flags;
1800 struct msm_endpoint *ept;
1801 struct msm_request *req;
1802 int n;
1803 int i = 0;
1804
1805 spin_lock_irqsave(&ui->lock, flags);
1806
1807 i += scnprintf(buf + i, PAGE_SIZE - i,
1808 "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
1809 readl(USB_ENDPTSETUPSTAT),
1810 readl(USB_ENDPTPRIME),
1811 readl(USB_ENDPTSTAT),
1812 readl(USB_ENDPTCOMPLETE));
1813 i += scnprintf(buf + i, PAGE_SIZE - i,
1814 "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
1815 readl(USB_USBCMD),
1816 readl(USB_USBSTS),
1817 readl(USB_USBINTR),
1818 readl(USB_PORTSC));
1819
1820
1821 for (n = 0; n < 32; n++) {
1822 ept = ui->ept + n;
1823 if (ept->ep.maxpacket == 0)
1824 continue;
1825
1826 i += scnprintf(buf + i, PAGE_SIZE - i,
1827 "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
1828 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1829 ept->head->config, ept->head->active,
1830 ept->head->next, ept->head->info);
1831
1832 for (req = ept->req; req; req = req->next)
1833 i += scnprintf(buf + i, PAGE_SIZE - i,
1834 " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
1835 req->item_dma, req->item->next,
1836 req->item->info, req->item->page0,
1837 req->busy ? 'B' : ' ',
1838 req->live ? 'L' : ' ');
1839 }
1840
1841 i += scnprintf(buf + i, PAGE_SIZE - i,
1842 "phy failure count: %d\n", ui->phy_fail_count);
1843
1844 spin_unlock_irqrestore(&ui->lock, flags);
1845
1846 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
1847}
1848
1849static ssize_t debug_write_reset(struct file *file, const char __user *buf,
1850 size_t count, loff_t *ppos)
1851{
1852 struct usb_info *ui = file->private_data;
1853 unsigned long flags;
1854
1855 spin_lock_irqsave(&ui->lock, flags);
1856 ui->flags |= USB_FLAG_RESET;
1857 schedule_work(&ui->work);
1858 spin_unlock_irqrestore(&ui->lock, flags);
1859
1860 return count;
1861}
1862
1863static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
1864 size_t count, loff_t *ppos)
1865{
1866 usb_function_reenumerate();
1867 return count;
1868}
1869
1870static int debug_open(struct inode *inode, struct file *file)
1871{
1872 file->private_data = inode->i_private;
1873 return 0;
1874}
1875
1876const struct file_operations debug_stat_ops = {
1877 .open = debug_open,
1878 .read = debug_read_status,
1879};
1880
1881const struct file_operations debug_reset_ops = {
1882 .open = debug_open,
1883 .write = debug_write_reset,
1884};
1885
1886const struct file_operations debug_cycle_ops = {
1887 .open = debug_open,
1888 .write = debug_write_cycle,
1889};
1890
1891static ssize_t debug_read_release_wlocks(struct file *file, char __user *ubuf,
1892 size_t count, loff_t *ppos)
1893{
1894 char kbuf[10];
1895 size_t c = 0;
1896
1897 memset(kbuf, 0, 10);
1898
1899 c = scnprintf(kbuf, 10, "%d", release_wlocks);
1900
1901 if (copy_to_user(ubuf, kbuf, c))
1902 return -EFAULT;
1903
1904 return c;
1905}
1906static ssize_t debug_write_release_wlocks(struct file *file,
1907 const char __user *buf, size_t count, loff_t *ppos)
1908{
1909 char kbuf[10];
1910 long temp;
1911
1912 memset(kbuf, 0, 10);
1913
1914 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1915 return -EFAULT;
1916
1917 if (strict_strtol(kbuf, 10, &temp))
1918 return -EINVAL;
1919
1920 if (temp)
1921 release_wlocks = 1;
1922 else
1923 release_wlocks = 0;
1924
1925 return count;
1926}
1927static int debug_wake_lock_open(struct inode *inode, struct file *file)
1928{
1929 file->private_data = inode->i_private;
1930 return 0;
1931}
1932const struct file_operations debug_wlocks_ops = {
1933 .open = debug_wake_lock_open,
1934 .read = debug_read_release_wlocks,
1935 .write = debug_write_release_wlocks,
1936};
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301937
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301938static ssize_t debug_reprime_ep(struct file *file, const char __user *ubuf,
1939 size_t count, loff_t *ppos)
1940{
1941 struct usb_info *ui = file->private_data;
1942 struct msm_endpoint *ept;
1943 char kbuf[10];
1944 unsigned int ep_num, dir;
1945 unsigned long flags;
1946 unsigned n, i;
1947
1948 memset(kbuf, 0, 10);
1949
1950 if (copy_from_user(kbuf, ubuf, count > 10 ? 10 : count))
1951 return -EFAULT;
1952
1953 if (sscanf(kbuf, "%u %u", &ep_num, &dir) != 2)
1954 return -EINVAL;
1955
1956 if (dir)
1957 i = ep_num + 16;
1958 else
1959 i = ep_num;
1960
1961 spin_lock_irqsave(&ui->lock, flags);
1962 ept = ui->ept + i;
1963 n = 1 << ept->bit;
1964
1965 if ((readl_relaxed(USB_ENDPTPRIME) & n))
1966 goto out;
1967
1968 if (readl_relaxed(USB_ENDPTSTAT) & n)
1969 goto out;
1970
1971 /* clear speculative loads on item->info */
1972 rmb();
1973 if (ept->req && (ept->req->item->info & INFO_ACTIVE)) {
1974 pr_err("%s(): ept%d%s prime failed. ept: config: %x"
1975 "active: %x next: %x info: %x\n",
1976 __func__, ept->num,
1977 ept->flags & EPT_FLAG_IN ? "in" : "out",
1978 ept->head->config, ept->head->active,
1979 ept->head->next, ept->head->info);
1980 writel_relaxed(n, USB_ENDPTPRIME);
1981 }
1982out:
1983 spin_unlock_irqrestore(&ui->lock, flags);
1984
1985 return count;
1986}
1987
1988static char buffer[512];
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301989static ssize_t debug_prime_fail_read(struct file *file, char __user *ubuf,
1990 size_t count, loff_t *ppos)
1991{
1992 struct usb_info *ui = file->private_data;
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301993 char *buf = buffer;
1994 unsigned long flags;
1995 struct msm_endpoint *ept;
1996 int n;
1997 int i = 0;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301998
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301999 spin_lock_irqsave(&ui->lock, flags);
2000 for (n = 0; n < 32; n++) {
2001 ept = ui->ept + n;
2002 if (ept->ep.maxpacket == 0)
2003 continue;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302004
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302005 i += scnprintf(buf + i, PAGE_SIZE - i,
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302006 "ept%d %s false_prime_count=%lu prime_fail_count=%d "
2007 "dtd_fail_count=%lu "
2008 "dTD_workaround_fail_count=%lu\n",
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302009 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
2010 ept->false_prime_fail_count,
2011 ept->actual_prime_fail_count,
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302012 ept->dTD_update_fail_count,
2013 ept->dTD_workaround_fail_count);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302014 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302015
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302016 i += scnprintf(buf + i, PAGE_SIZE - i,
2017 "dTD_update_fail count: %lu\n",
2018 ui->dTD_update_fail_count);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302019
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302020 i += scnprintf(buf + i, PAGE_SIZE - i,
2021 "prime_fail count: %d\n", ui->prime_fail_count);
2022
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302023 i += scnprintf(buf + i, PAGE_SIZE - i,
2024 "dtd_workaround_fail count: %lu\n",
2025 ui->dTD_workaround_fail_count);
2026
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302027 spin_unlock_irqrestore(&ui->lock, flags);
2028
2029 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302030}
2031
2032static int debug_prime_fail_open(struct inode *inode, struct file *file)
2033{
2034 file->private_data = inode->i_private;
2035 return 0;
2036}
2037
2038const struct file_operations prime_fail_ops = {
2039 .open = debug_prime_fail_open,
2040 .read = debug_prime_fail_read,
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302041 .write = debug_reprime_ep,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302042};
2043
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044static void usb_debugfs_init(struct usb_info *ui)
2045{
2046 struct dentry *dent;
2047 dent = debugfs_create_dir(dev_name(&ui->pdev->dev), 0);
2048 if (IS_ERR(dent))
2049 return;
2050
2051 debugfs_create_file("status", 0444, dent, ui, &debug_stat_ops);
2052 debugfs_create_file("reset", 0222, dent, ui, &debug_reset_ops);
2053 debugfs_create_file("cycle", 0222, dent, ui, &debug_cycle_ops);
2054 debugfs_create_file("release_wlocks", 0666, dent, ui,
2055 &debug_wlocks_ops);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302056 debugfs_create_file("prime_fail_countt", 0666, dent, ui,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302057 &prime_fail_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058}
2059#else
2060static void usb_debugfs_init(struct usb_info *ui) {}
2061#endif
2062
2063static int
2064msm72k_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
2065{
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302066 struct msm_endpoint *ept;
2067 unsigned char ep_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002068
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302069 if (_ep == NULL || desc == NULL)
2070 return -EINVAL;
2071
2072 ept = to_msm_endpoint(_ep);
2073 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002074 _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2075 config_ept(ept);
2076 ept->wedged = 0;
2077 usb_ept_enable(ept, 1, ep_type);
2078 return 0;
2079}
2080
2081static int msm72k_disable(struct usb_ep *_ep)
2082{
2083 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2084
2085 usb_ept_enable(ept, 0, 0);
2086 flush_endpoint(ept);
Pavankumar Kondeti2c968782012-04-12 15:16:39 +05302087 /*
2088 * Clear descriptors here. Otherwise previous descriptors
2089 * will be used by function drivers upon next enumeration.
2090 */
2091 _ep->desc = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002092 return 0;
2093}
2094
2095static struct usb_request *
2096msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
2097{
2098 return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
2099}
2100
2101static void
2102msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
2103{
2104 struct msm_request *req = to_msm_request(_req);
2105 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2106 struct usb_info *ui = ept->ui;
2107
2108 /* request should not be busy */
2109 BUG_ON(req->busy);
2110 if (req->alloced)
2111 kfree(req->req.buf);
2112 dma_pool_free(ui->pool, req->item, req->item_dma);
2113 kfree(req);
2114}
2115
2116static int
2117msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
2118{
2119 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2120 struct usb_info *ui = ep->ui;
2121
2122 if (ep == &ui->ep0in) {
2123 struct msm_request *r = to_msm_request(req);
2124 if (!req->length)
2125 goto ep_queue_done;
2126 r->gadget_complete = req->complete;
2127 /* ep0_queue_ack_complete queue a receive for ACK before
2128 ** calling req->complete
2129 */
2130 req->complete = ep0_queue_ack_complete;
2131 if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
2132 ep = &ui->ep0out;
2133 goto ep_queue_done;
2134 }
2135
2136ep_queue_done:
2137 return usb_ept_queue_xfer(ep, req);
2138}
2139
2140static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2141{
2142 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2143 struct msm_request *req = to_msm_request(_req);
2144 struct usb_info *ui = ep->ui;
2145
2146 struct msm_request *temp_req;
2147 unsigned long flags;
2148
2149 if (!(ui && req && ep->req))
2150 return -EINVAL;
2151
2152 spin_lock_irqsave(&ui->lock, flags);
2153 if (!req->busy) {
2154 dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
2155 spin_unlock_irqrestore(&ui->lock, flags);
2156 return -EINVAL;
2157 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302158 del_timer(&ep->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159 /* Stop the transfer */
2160 do {
2161 writel((1 << ep->bit), USB_ENDPTFLUSH);
2162 while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
2163 udelay(100);
2164 } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
2165
2166 req->req.status = 0;
2167 req->busy = 0;
2168
2169 if (ep->req == req) {
2170 ep->req = req->next;
2171 ep->head->next = req->item->next;
2172 } else {
2173 req->prev->next = req->next;
2174 if (req->next)
2175 req->next->prev = req->prev;
2176 req->prev->item->next = req->item->next;
2177 }
2178
2179 if (!req->next)
2180 ep->last = req->prev;
2181
2182 /* initialize request to default */
2183 req->item->next = TERMINATE;
2184 req->item->info = 0;
2185 req->live = 0;
2186 dma_unmap_single(NULL, req->dma, req->req.length,
2187 (ep->flags & EPT_FLAG_IN) ?
2188 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2189
2190 if (req->req.complete) {
2191 req->req.status = -ECONNRESET;
2192 spin_unlock_irqrestore(&ui->lock, flags);
2193 req->req.complete(&ep->ep, &req->req);
2194 spin_lock_irqsave(&ui->lock, flags);
2195 }
2196
2197 if (!req->live) {
2198 /* Reprime the endpoint for the remaining transfers */
2199 for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
2200 temp_req->live = 0;
2201 if (ep->req)
2202 usb_ept_start(ep);
2203 spin_unlock_irqrestore(&ui->lock, flags);
2204 return 0;
2205 }
2206 spin_unlock_irqrestore(&ui->lock, flags);
2207 return 0;
2208}
2209
2210static int
2211usb_ept_set_halt(struct usb_ep *_ep, int value)
2212{
2213 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2214 struct usb_info *ui = ept->ui;
2215 unsigned int in = ept->flags & EPT_FLAG_IN;
2216 unsigned int n;
2217 unsigned long flags;
2218
2219 spin_lock_irqsave(&ui->lock, flags);
2220
2221 n = readl(USB_ENDPTCTRL(ept->num));
2222
2223 if (in) {
2224 if (value)
2225 n |= CTRL_TXS;
2226 else {
2227 n &= ~CTRL_TXS;
2228 n |= CTRL_TXR;
2229 }
2230 } else {
2231 if (value)
2232 n |= CTRL_RXS;
2233 else {
2234 n &= ~CTRL_RXS;
2235 n |= CTRL_RXR;
2236 }
2237 }
2238 writel(n, USB_ENDPTCTRL(ept->num));
2239 if (!value)
2240 ept->wedged = 0;
2241 spin_unlock_irqrestore(&ui->lock, flags);
2242
2243 return 0;
2244}
2245
2246static int
2247msm72k_set_halt(struct usb_ep *_ep, int value)
2248{
2249 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2250 unsigned int in = ept->flags & EPT_FLAG_IN;
2251
2252 if (value && in && ept->req)
2253 return -EAGAIN;
2254
2255 usb_ept_set_halt(_ep, value);
2256
2257 return 0;
2258}
2259
2260static int
2261msm72k_fifo_status(struct usb_ep *_ep)
2262{
2263 return -EOPNOTSUPP;
2264}
2265
2266static void
2267msm72k_fifo_flush(struct usb_ep *_ep)
2268{
2269 flush_endpoint(to_msm_endpoint(_ep));
2270}
2271static int msm72k_set_wedge(struct usb_ep *_ep)
2272{
2273 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2274
2275 if (ept->num == 0)
2276 return -EINVAL;
2277
2278 ept->wedged = 1;
2279
2280 return msm72k_set_halt(_ep, 1);
2281}
2282
2283static const struct usb_ep_ops msm72k_ep_ops = {
2284 .enable = msm72k_enable,
2285 .disable = msm72k_disable,
2286
2287 .alloc_request = msm72k_alloc_request,
2288 .free_request = msm72k_free_request,
2289
2290 .queue = msm72k_queue,
2291 .dequeue = msm72k_dequeue,
2292
2293 .set_halt = msm72k_set_halt,
2294 .set_wedge = msm72k_set_wedge,
2295 .fifo_status = msm72k_fifo_status,
2296 .fifo_flush = msm72k_fifo_flush,
2297};
2298
2299static int msm72k_get_frame(struct usb_gadget *_gadget)
2300{
2301 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2302
2303 /* frame number is in bits 13:3 */
2304 return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
2305}
2306
2307/* VBUS reporting logically comes from a transceiver */
2308static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
2309{
2310 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2311 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2312
2313 if (is_active || atomic_read(&otg->chg_type)
2314 == USB_CHG_TYPE__WALLCHARGER)
2315 wake_lock(&ui->wlock);
2316
2317 msm_hsusb_set_vbus_state(is_active);
2318 return 0;
2319}
2320
2321/* SW workarounds
2322Issue #1 - USB Spoof Disconnect Failure
2323Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2324SW workaround - Making opmode non-driving and SuspendM set in function
2325 register of SMSC phy
2326*/
2327/* drivers may have software control over D+ pullup */
2328static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
2329{
2330 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2331 unsigned long flags;
2332
2333 if (is_active) {
2334 spin_lock_irqsave(&ui->lock, flags);
2335 if (is_usb_online(ui) && ui->driver)
2336 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2337 spin_unlock_irqrestore(&ui->lock, flags);
2338 } else {
2339 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2340 /* S/W workaround, Issue#1 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002341 usb_phy_io_write(ui->xceiv, 0x48, 0x04);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002342 }
2343
2344 /* Ensure pull-up operation is completed before returning */
2345 mb();
2346
2347 return 0;
2348}
2349
2350static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
2351{
2352 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302353 struct msm_otg *otg = to_msm_otg(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002354 unsigned long flags;
2355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356 atomic_set(&ui->softconnect, is_active);
2357
2358 spin_lock_irqsave(&ui->lock, flags);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302359 if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL ||
2360 atomic_read(&otg->chg_type) == USB_CHG_TYPE__WALLCHARGER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002361 spin_unlock_irqrestore(&ui->lock, flags);
2362 return 0;
2363 }
2364 spin_unlock_irqrestore(&ui->lock, flags);
2365
2366 msm72k_pullup_internal(_gadget, is_active);
2367
2368 if (is_active && !ui->gadget.is_a_peripheral)
2369 schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
2370
2371 return 0;
2372}
2373
2374static int msm72k_wakeup(struct usb_gadget *_gadget)
2375{
2376 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2377 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2378
2379 if (!atomic_read(&ui->remote_wakeup)) {
2380 dev_err(&ui->pdev->dev,
2381 "%s: remote wakeup not supported\n", __func__);
2382 return -ENOTSUPP;
2383 }
2384
2385 if (!atomic_read(&ui->configured)) {
2386 dev_err(&ui->pdev->dev,
2387 "%s: device is not configured\n", __func__);
2388 return -ENODEV;
2389 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002390 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002391
2392 disable_irq(otg->irq);
2393
2394 if (!is_usb_active())
2395 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
2396
2397 /* Ensure that USB port is resumed before enabling the IRQ */
2398 mb();
2399
2400 enable_irq(otg->irq);
2401
2402 return 0;
2403}
2404
2405/* when Gadget is configured, it will indicate how much power
2406 * can be pulled from vbus, as specified in configuiration descriptor
2407 */
2408static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2409{
2410 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2411 unsigned long flags;
2412
2413
2414 spin_lock_irqsave(&ui->lock, flags);
2415 ui->b_max_pow = mA;
2416 ui->flags = USB_FLAG_CONFIGURED;
2417 spin_unlock_irqrestore(&ui->lock, flags);
2418
2419 schedule_work(&ui->work);
2420
2421 return 0;
2422}
2423
2424static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
2425{
2426 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2427 unsigned long flags;
2428 int ret = 0;
2429
2430 spin_lock_irqsave(&ui->lock, flags);
2431 if (set) {
2432 if (ui->pdata && ui->pdata->self_powered)
2433 atomic_set(&ui->self_powered, 1);
2434 else
2435 ret = -EOPNOTSUPP;
2436 } else {
2437 /* We can always work as a bus powered device */
2438 atomic_set(&ui->self_powered, 0);
2439 }
2440 spin_unlock_irqrestore(&ui->lock, flags);
2441
2442 return ret;
2443
2444}
2445
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002446static int msm72k_gadget_start(struct usb_gadget_driver *driver,
2447 int (*bind)(struct usb_gadget *));
2448static int msm72k_gadget_stop(struct usb_gadget_driver *driver);
2449
2450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002451static const struct usb_gadget_ops msm72k_ops = {
2452 .get_frame = msm72k_get_frame,
2453 .vbus_session = msm72k_udc_vbus_session,
2454 .vbus_draw = msm72k_udc_vbus_draw,
2455 .pullup = msm72k_pullup,
2456 .wakeup = msm72k_wakeup,
2457 .set_selfpowered = msm72k_set_selfpowered,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002458 .start = msm72k_gadget_start,
2459 .stop = msm72k_gadget_stop
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002460};
2461
2462static void usb_do_remote_wakeup(struct work_struct *w)
2463{
2464 struct usb_info *ui = the_usb_info;
2465
2466 msm72k_wakeup(&ui->gadget);
2467}
2468
2469static ssize_t usb_remote_wakeup(struct device *dev,
2470 struct device_attribute *attr, const char *buf, size_t count)
2471{
2472 struct usb_info *ui = the_usb_info;
2473
2474 msm72k_wakeup(&ui->gadget);
2475
2476 return count;
2477}
2478
2479static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
2480 char *buf)
2481{
2482 size_t i;
2483 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
2484 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
2485 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
2486 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
2487 "USB_STATE_SUSPENDED"
2488 };
2489
2490 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
2491 return i;
2492}
2493
2494static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
2495 char *buf)
2496{
2497 struct usb_info *ui = the_usb_info;
2498 size_t i;
2499 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
2500 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
2501
2502 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
2503 return i;
2504}
2505
2506static ssize_t store_usb_chg_current(struct device *dev,
2507 struct device_attribute *attr, const char *buf, size_t count)
2508{
2509 struct usb_info *ui = the_usb_info;
2510 unsigned long mA;
2511
2512 if (ui->gadget.is_a_peripheral)
2513 return -EINVAL;
2514
2515 if (strict_strtoul(buf, 10, &mA))
2516 return -EINVAL;
2517
2518 ui->chg_current = mA;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002519 usb_phy_set_power(ui->xceiv, mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520
2521 return count;
2522}
2523
2524static ssize_t show_usb_chg_current(struct device *dev,
2525 struct device_attribute *attr, char *buf)
2526{
2527 struct usb_info *ui = the_usb_info;
2528 size_t count;
2529
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302530 count = snprintf(buf, PAGE_SIZE, "%d", ui->chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531
2532 return count;
2533}
2534
2535static ssize_t show_usb_chg_type(struct device *dev,
2536 struct device_attribute *attr, char *buf)
2537{
2538 struct usb_info *ui = the_usb_info;
2539 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2540 size_t count;
2541 char *chg_type[] = {"STD DOWNSTREAM PORT",
2542 "CARKIT",
2543 "DEDICATED CHARGER",
2544 "INVALID"};
2545
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302546 count = snprintf(buf, PAGE_SIZE, "%s",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002547 chg_type[atomic_read(&otg->chg_type)]);
2548
2549 return count;
2550}
2551static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
2552static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
2553static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
2554static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
2555static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
2556 show_usb_chg_current, store_usb_chg_current);
2557
2558#ifdef CONFIG_USB_OTG
2559static ssize_t store_host_req(struct device *dev,
2560 struct device_attribute *attr, const char *buf, size_t count)
2561{
2562 struct usb_info *ui = the_usb_info;
2563 unsigned long val, flags;
2564
2565 if (strict_strtoul(buf, 10, &val))
2566 return -EINVAL;
2567
2568 dev_dbg(&ui->pdev->dev, "%s host request\n",
2569 val ? "set" : "clear");
2570
2571 spin_lock_irqsave(&ui->lock, flags);
2572 if (ui->hnp_avail)
2573 ui->gadget.host_request = !!val;
2574 spin_unlock_irqrestore(&ui->lock, flags);
2575
2576 return count;
2577}
2578static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
2579
2580/* How do we notify user space about HNP availability?
2581 * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
2582 * Introduce hnp_avail flag and set when HNP polling request arrives.
2583 * The expectation is that user space checks hnp availability before
2584 * requesting host role via above sysfs node.
2585 */
2586static ssize_t show_host_avail(struct device *dev,
2587 struct device_attribute *attr, char *buf)
2588{
2589 struct usb_info *ui = the_usb_info;
2590 size_t count;
2591 unsigned long flags;
2592
2593 spin_lock_irqsave(&ui->lock, flags);
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302594 count = snprintf(buf, PAGE_SIZE, "%d\n", ui->hnp_avail);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 spin_unlock_irqrestore(&ui->lock, flags);
2596
2597 return count;
2598}
2599static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
2600
2601static struct attribute *otg_attrs[] = {
2602 &dev_attr_host_request.attr,
2603 &dev_attr_host_avail.attr,
2604 NULL,
2605};
2606
2607static struct attribute_group otg_attr_grp = {
2608 .name = "otg",
2609 .attrs = otg_attrs,
2610};
2611#endif
2612
2613static int msm72k_probe(struct platform_device *pdev)
2614{
2615 struct usb_info *ui;
2616 struct msm_otg *otg;
2617 int retval;
2618
2619 dev_dbg(&pdev->dev, "msm72k_probe\n");
2620 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
2621 if (!ui)
2622 return -ENOMEM;
2623
2624 ui->pdev = pdev;
2625 ui->pdata = pdev->dev.platform_data;
2626
2627 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
2628 if (!ui->buf)
2629 return usb_free(ui, -ENOMEM);
2630
2631 ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
2632 if (!ui->pool)
2633 return usb_free(ui, -ENOMEM);
2634
Steve Mucklef132c6c2012-06-06 18:30:57 -07002635 ui->xceiv = usb_get_transceiver();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 if (!ui->xceiv)
2637 return usb_free(ui, -ENODEV);
2638
2639 otg = to_msm_otg(ui->xceiv);
2640 ui->addr = otg->regs;
2641
2642 ui->gadget.ops = &msm72k_ops;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002643 ui->gadget.max_speed = USB_SPEED_HIGH;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002644 device_initialize(&ui->gadget.dev);
2645 dev_set_name(&ui->gadget.dev, "gadget");
2646 ui->gadget.dev.parent = &pdev->dev;
2647 ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
2648
2649#ifdef CONFIG_USB_OTG
2650 ui->gadget.is_otg = 1;
2651#endif
2652
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002653 retval = usb_add_gadget_udc(&pdev->dev, &ui->gadget);
2654 if (retval)
2655 return usb_free(ui, retval);
2656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002657 ui->sdev.name = DRIVER_NAME;
2658 ui->sdev.print_name = print_switch_name;
2659 ui->sdev.print_state = print_switch_state;
2660
2661 retval = switch_dev_register(&ui->sdev);
2662 if (retval)
2663 return usb_free(ui, retval);
2664
2665 the_usb_info = ui;
2666
2667 wake_lock_init(&ui->wlock,
2668 WAKE_LOCK_SUSPEND, "usb_bus_active");
2669
2670 usb_debugfs_init(ui);
2671
2672 usb_prepare(ui);
2673
2674#ifdef CONFIG_USB_OTG
2675 retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
2676 if (retval) {
2677 dev_err(&ui->pdev->dev,
2678 "failed to create otg sysfs directory:"
2679 "err:(%d)\n", retval);
2680 }
2681#endif
2682
Steve Mucklef132c6c2012-06-06 18:30:57 -07002683 retval = otg_set_peripheral(ui->xceiv->otg, &ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002684 if (retval) {
2685 dev_err(&ui->pdev->dev,
2686 "%s: Cannot bind the transceiver, retval:(%d)\n",
2687 __func__, retval);
2688 switch_dev_unregister(&ui->sdev);
2689 wake_lock_destroy(&ui->wlock);
2690 return usb_free(ui, retval);
2691 }
2692
2693 pm_runtime_enable(&pdev->dev);
2694
2695 /* Setup phy stuck timer */
2696 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
2697 setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
2698 return 0;
2699}
2700
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002701static int msm72k_gadget_start(struct usb_gadget_driver *driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002702 int (*bind)(struct usb_gadget *))
2703{
2704 struct usb_info *ui = the_usb_info;
2705 int retval, n;
2706
2707 if (!driver
Steve Mucklef132c6c2012-06-06 18:30:57 -07002708 || driver->max_speed < USB_SPEED_FULL
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002709 || !bind
2710 || !driver->disconnect
2711 || !driver->setup)
2712 return -EINVAL;
2713 if (!ui)
2714 return -ENODEV;
2715 if (ui->driver)
2716 return -EBUSY;
2717
2718 /* first hook up the driver ... */
2719 ui->driver = driver;
2720 ui->gadget.dev.driver = &driver->driver;
2721 ui->gadget.name = driver_name;
2722 INIT_LIST_HEAD(&ui->gadget.ep_list);
2723 ui->gadget.ep0 = &ui->ep0in.ep;
2724 INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
2725 ui->gadget.speed = USB_SPEED_UNKNOWN;
2726 atomic_set(&ui->softconnect, 1);
2727
2728 for (n = 1; n < 16; n++) {
2729 struct msm_endpoint *ept = ui->ept + n;
2730 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2731 ept->ep.maxpacket = 512;
2732 }
2733 for (n = 17; n < 32; n++) {
2734 struct msm_endpoint *ept = ui->ept + n;
2735 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2736 ept->ep.maxpacket = 512;
2737 }
2738
2739 retval = device_add(&ui->gadget.dev);
2740 if (retval)
2741 goto fail;
2742
2743 retval = bind(&ui->gadget);
2744 if (retval) {
2745 dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
2746 driver->driver.name, retval);
2747 device_del(&ui->gadget.dev);
2748 goto fail;
2749 }
2750
2751 retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
2752 if (retval != 0)
2753 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2754 "(wakeup) error: (%d)\n", retval);
2755 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
2756 if (retval != 0)
2757 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2758 " (usb_state) error: (%d)\n", retval);
2759
2760 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
2761 if (retval != 0)
2762 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2763 " (usb_speed) error: (%d)\n", retval);
2764
2765 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
2766 if (retval != 0)
2767 dev_err(&ui->pdev->dev,
2768 "failed to create sysfs entry(chg_type): err:(%d)\n",
2769 retval);
2770 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
2771 if (retval != 0)
2772 dev_err(&ui->pdev->dev,
2773 "failed to create sysfs entry(chg_current):"
2774 "err:(%d)\n", retval);
2775
2776 dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
2777 driver->driver.name);
2778 usb_start(ui);
2779
2780 return 0;
2781
2782fail:
2783 ui->driver = NULL;
2784 ui->gadget.dev.driver = NULL;
2785 return retval;
2786}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002787
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002788static int msm72k_gadget_stop(struct usb_gadget_driver *driver)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002789{
2790 struct usb_info *dev = the_usb_info;
2791
2792 if (!dev)
2793 return -ENODEV;
2794 if (!driver || driver != dev->driver || !driver->unbind)
2795 return -EINVAL;
2796
2797 msm72k_pullup_internal(&dev->gadget, 0);
2798 if (dev->irq) {
2799 free_irq(dev->irq, dev);
2800 dev->irq = 0;
2801 }
2802 dev->state = USB_STATE_IDLE;
2803 atomic_set(&dev->configured, 0);
2804 switch_set_state(&dev->sdev, 0);
2805 /* cancel pending ep0 transactions */
2806 flush_endpoint(&dev->ep0out);
2807 flush_endpoint(&dev->ep0in);
2808
2809 device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
2810 device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
2811 device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
2812 device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
2813 device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
2814 driver->disconnect(&dev->gadget);
2815 driver->unbind(&dev->gadget);
2816 dev->gadget.dev.driver = NULL;
2817 dev->driver = NULL;
2818
2819 device_del(&dev->gadget.dev);
2820
2821 dev_dbg(&dev->pdev->dev,
2822 "unregistered gadget driver '%s'\n", driver->driver.name);
2823 return 0;
2824}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002825
2826
2827static int msm72k_udc_runtime_suspend(struct device *dev)
2828{
2829 dev_dbg(dev, "pm_runtime: suspending...\n");
2830 return 0;
2831}
2832
2833static int msm72k_udc_runtime_resume(struct device *dev)
2834{
2835 dev_dbg(dev, "pm_runtime: resuming...\n");
2836 return 0;
2837}
2838
2839static int msm72k_udc_runtime_idle(struct device *dev)
2840{
2841 dev_dbg(dev, "pm_runtime: idling...\n");
2842 return 0;
2843}
2844
2845static struct dev_pm_ops msm72k_udc_dev_pm_ops = {
2846 .runtime_suspend = msm72k_udc_runtime_suspend,
2847 .runtime_resume = msm72k_udc_runtime_resume,
2848 .runtime_idle = msm72k_udc_runtime_idle
2849};
2850
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002851static int __exit msm72k_remove(struct platform_device *pdev)
2852{
2853 struct usb_info *ui = container_of(&pdev, struct usb_info, pdev);
2854
2855 return usb_free(ui, 0);
2856}
2857
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002858static struct platform_driver usb_driver = {
2859 .probe = msm72k_probe,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002860 .remove = msm72k_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002861 .driver = { .name = "msm_hsusb",
2862 .pm = &msm72k_udc_dev_pm_ops, },
2863};
2864
2865static int __init init(void)
2866{
2867 return platform_driver_register(&usb_driver);
2868}
2869module_init(init);
2870
2871static void __exit cleanup(void)
2872{
2873 platform_driver_unregister(&usb_driver);
2874}
2875module_exit(cleanup);
2876
2877MODULE_DESCRIPTION(DRIVER_DESC);
2878MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
2879MODULE_LICENSE("GPL");