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