blob: 887a10ca5790b8072870ea3448cc02b13ee9b592 [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 Velempati2a9c1cb2012-12-21 18:26:23 +05305 * Copyright (c) 2009-2013, The Linux Foundation. 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) {
Chiranjeevi Velempati2a9c1cb2012-12-21 18:26:23 +05301652 /* Disable and acknowledge all
1653 * USB interrupts before freeing
1654 * irq, so that no USB spurious
1655 * interrupt occurs during USB cable
1656 * disconnect which may lead to
1657 * IRQ nobody cared error.
1658 */
1659 writel_relaxed(0, USB_USBINTR);
1660 writel_relaxed(readl_relaxed(USB_USBSTS)
1661 , USB_USBSTS);
1662 /* Ensure that above STOREs are
1663 * completed before enabling
1664 * interrupts */
1665 wmb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666 free_irq(ui->irq, ui);
1667 ui->irq = 0;
1668 }
1669
1670
1671 switch_set_state(&ui->sdev, 0);
1672
1673 ui->state = USB_STATE_OFFLINE;
1674 usb_do_work_check_vbus(ui);
1675 pm_runtime_put_noidle(&ui->pdev->dev);
1676 pm_runtime_suspend(&ui->pdev->dev);
1677 wake_unlock(&ui->wlock);
1678 break;
1679 }
1680 if (flags & USB_FLAG_SUSPEND) {
1681 int maxpower = usb_get_max_power(ui);
1682
1683 if (maxpower < 0)
1684 break;
1685
Steve Mucklef132c6c2012-06-06 18:30:57 -07001686 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001687 /* To support TCXO during bus suspend
1688 * This might be dummy check since bus suspend
1689 * is not implemented as of now
1690 * */
1691 if (release_wlocks)
1692 wake_unlock(&ui->wlock);
1693
1694 /* TBD: Initiate LPM at usb bus suspend */
1695 break;
1696 }
1697 if (flags & USB_FLAG_CONFIGURED) {
1698 int maxpower = usb_get_max_power(ui);
1699
1700 /* We may come here even when no configuration
1701 * is selected. Send online/offline event
1702 * accordingly.
1703 */
1704 switch_set_state(&ui->sdev,
1705 atomic_read(&ui->configured));
1706
1707 if (maxpower < 0)
1708 break;
1709
1710 ui->chg_current = maxpower;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001711 usb_phy_set_power(ui->xceiv, maxpower);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001712 break;
1713 }
1714 if (flags & USB_FLAG_RESET) {
1715 dev_dbg(&ui->pdev->dev,
1716 "msm72k_udc: ONLINE -> RESET\n");
1717 msm72k_pullup_internal(&ui->gadget, 0);
1718 usb_reset(ui);
1719 msm72k_pullup_internal(&ui->gadget, 1);
1720 dev_dbg(&ui->pdev->dev,
1721 "msm72k_udc: RESET -> ONLINE\n");
1722 break;
1723 }
1724 break;
1725 case USB_STATE_OFFLINE:
1726 /* If we were signaled to go online and vbus is still
1727 * present when we received the signal, go online.
1728 */
1729 if ((flags & USB_FLAG_VBUS_ONLINE) && _vbus) {
1730 int ret;
1731
1732 pm_runtime_get_noresume(&ui->pdev->dev);
1733 pm_runtime_resume(&ui->pdev->dev);
1734 dev_dbg(&ui->pdev->dev,
1735 "msm72k_udc: OFFLINE -> ONLINE\n");
1736
1737 usb_reset(ui);
1738 ui->state = USB_STATE_ONLINE;
1739 usb_do_work_check_vbus(ui);
1740 ret = request_irq(otg->irq, usb_interrupt,
1741 IRQF_SHARED,
1742 ui->pdev->name, ui);
1743 /* FIXME: should we call BUG_ON when
1744 * requst irq fails
1745 */
1746 if (ret) {
1747 dev_err(&ui->pdev->dev,
1748 "hsusb: peripheral: request irq"
1749 " failed:(%d)", ret);
1750 break;
1751 }
1752 ui->irq = otg->irq;
1753 enable_irq_wake(otg->irq);
1754
1755 if (!atomic_read(&ui->softconnect))
1756 break;
1757 msm72k_pullup_internal(&ui->gadget, 1);
1758
1759 if (!ui->gadget.is_a_peripheral)
1760 schedule_delayed_work(
1761 &ui->chg_det,
1762 USB_CHG_DET_DELAY);
1763 }
1764 break;
1765 }
1766 }
1767}
1768
1769/* FIXME - the callers of this function should use a gadget API instead.
1770 * This is called from htc_battery.c and board-halibut.c
1771 * WARNING - this can get called before this driver is initialized.
1772 */
1773void msm_hsusb_set_vbus_state(int online)
1774{
1775 unsigned long flags;
1776 struct usb_info *ui = the_usb_info;
1777
1778 if (!ui) {
1779 pr_err("%s called before driver initialized\n", __func__);
1780 return;
1781 }
1782
1783 spin_lock_irqsave(&ui->lock, flags);
1784
1785 if (is_usb_online(ui) == online)
1786 goto out;
1787
1788 if (online) {
1789 ui->usb_state = USB_STATE_POWERED;
1790 ui->flags |= USB_FLAG_VBUS_ONLINE;
1791 } else {
1792 ui->gadget.speed = USB_SPEED_UNKNOWN;
1793 ui->usb_state = USB_STATE_NOTATTACHED;
1794 ui->flags |= USB_FLAG_VBUS_OFFLINE;
Chiranjeevi Velempati18dda792012-07-16 17:02:32 +05301795 ui->chg_type_retry_cnt = 0;
1796 ui->proprietary_chg = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 }
1798 if (in_interrupt()) {
1799 schedule_work(&ui->work);
1800 } else {
1801 spin_unlock_irqrestore(&ui->lock, flags);
1802 usb_do_work(&ui->work);
1803 return;
1804 }
1805out:
1806 spin_unlock_irqrestore(&ui->lock, flags);
1807}
1808
1809#if defined(CONFIG_DEBUG_FS)
1810
1811void usb_function_reenumerate(void)
1812{
1813 struct usb_info *ui = the_usb_info;
1814
1815 /* disable and re-enable the D+ pullup */
1816 dev_dbg(&ui->pdev->dev, "disable pullup\n");
1817 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
1818
1819 msleep(10);
1820
1821 dev_dbg(&ui->pdev->dev, "enable pullup\n");
1822 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
1823}
1824
1825static char debug_buffer[PAGE_SIZE];
1826
1827static ssize_t debug_read_status(struct file *file, char __user *ubuf,
1828 size_t count, loff_t *ppos)
1829{
1830 struct usb_info *ui = file->private_data;
1831 char *buf = debug_buffer;
1832 unsigned long flags;
1833 struct msm_endpoint *ept;
1834 struct msm_request *req;
1835 int n;
1836 int i = 0;
1837
1838 spin_lock_irqsave(&ui->lock, flags);
1839
1840 i += scnprintf(buf + i, PAGE_SIZE - i,
1841 "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
1842 readl(USB_ENDPTSETUPSTAT),
1843 readl(USB_ENDPTPRIME),
1844 readl(USB_ENDPTSTAT),
1845 readl(USB_ENDPTCOMPLETE));
1846 i += scnprintf(buf + i, PAGE_SIZE - i,
1847 "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
1848 readl(USB_USBCMD),
1849 readl(USB_USBSTS),
1850 readl(USB_USBINTR),
1851 readl(USB_PORTSC));
1852
1853
1854 for (n = 0; n < 32; n++) {
1855 ept = ui->ept + n;
1856 if (ept->ep.maxpacket == 0)
1857 continue;
1858
1859 i += scnprintf(buf + i, PAGE_SIZE - i,
1860 "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
1861 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1862 ept->head->config, ept->head->active,
1863 ept->head->next, ept->head->info);
1864
1865 for (req = ept->req; req; req = req->next)
1866 i += scnprintf(buf + i, PAGE_SIZE - i,
1867 " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
1868 req->item_dma, req->item->next,
1869 req->item->info, req->item->page0,
1870 req->busy ? 'B' : ' ',
1871 req->live ? 'L' : ' ');
1872 }
1873
1874 i += scnprintf(buf + i, PAGE_SIZE - i,
1875 "phy failure count: %d\n", ui->phy_fail_count);
1876
1877 spin_unlock_irqrestore(&ui->lock, flags);
1878
1879 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
1880}
1881
1882static ssize_t debug_write_reset(struct file *file, const char __user *buf,
1883 size_t count, loff_t *ppos)
1884{
1885 struct usb_info *ui = file->private_data;
1886 unsigned long flags;
1887
1888 spin_lock_irqsave(&ui->lock, flags);
1889 ui->flags |= USB_FLAG_RESET;
1890 schedule_work(&ui->work);
1891 spin_unlock_irqrestore(&ui->lock, flags);
1892
1893 return count;
1894}
1895
1896static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
1897 size_t count, loff_t *ppos)
1898{
1899 usb_function_reenumerate();
1900 return count;
1901}
1902
1903static int debug_open(struct inode *inode, struct file *file)
1904{
1905 file->private_data = inode->i_private;
1906 return 0;
1907}
1908
1909const struct file_operations debug_stat_ops = {
1910 .open = debug_open,
1911 .read = debug_read_status,
1912};
1913
1914const struct file_operations debug_reset_ops = {
1915 .open = debug_open,
1916 .write = debug_write_reset,
1917};
1918
1919const struct file_operations debug_cycle_ops = {
1920 .open = debug_open,
1921 .write = debug_write_cycle,
1922};
1923
1924static ssize_t debug_read_release_wlocks(struct file *file, char __user *ubuf,
1925 size_t count, loff_t *ppos)
1926{
1927 char kbuf[10];
1928 size_t c = 0;
1929
1930 memset(kbuf, 0, 10);
1931
1932 c = scnprintf(kbuf, 10, "%d", release_wlocks);
1933
1934 if (copy_to_user(ubuf, kbuf, c))
1935 return -EFAULT;
1936
1937 return c;
1938}
1939static ssize_t debug_write_release_wlocks(struct file *file,
1940 const char __user *buf, size_t count, loff_t *ppos)
1941{
1942 char kbuf[10];
1943 long temp;
1944
1945 memset(kbuf, 0, 10);
1946
1947 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1948 return -EFAULT;
1949
1950 if (strict_strtol(kbuf, 10, &temp))
1951 return -EINVAL;
1952
1953 if (temp)
1954 release_wlocks = 1;
1955 else
1956 release_wlocks = 0;
1957
1958 return count;
1959}
1960static int debug_wake_lock_open(struct inode *inode, struct file *file)
1961{
1962 file->private_data = inode->i_private;
1963 return 0;
1964}
1965const struct file_operations debug_wlocks_ops = {
1966 .open = debug_wake_lock_open,
1967 .read = debug_read_release_wlocks,
1968 .write = debug_write_release_wlocks,
1969};
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301970
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301971static ssize_t debug_reprime_ep(struct file *file, const char __user *ubuf,
1972 size_t count, loff_t *ppos)
1973{
1974 struct usb_info *ui = file->private_data;
1975 struct msm_endpoint *ept;
1976 char kbuf[10];
1977 unsigned int ep_num, dir;
1978 unsigned long flags;
1979 unsigned n, i;
1980
1981 memset(kbuf, 0, 10);
1982
1983 if (copy_from_user(kbuf, ubuf, count > 10 ? 10 : count))
1984 return -EFAULT;
1985
1986 if (sscanf(kbuf, "%u %u", &ep_num, &dir) != 2)
1987 return -EINVAL;
1988
1989 if (dir)
1990 i = ep_num + 16;
1991 else
1992 i = ep_num;
1993
1994 spin_lock_irqsave(&ui->lock, flags);
1995 ept = ui->ept + i;
1996 n = 1 << ept->bit;
1997
1998 if ((readl_relaxed(USB_ENDPTPRIME) & n))
1999 goto out;
2000
2001 if (readl_relaxed(USB_ENDPTSTAT) & n)
2002 goto out;
2003
2004 /* clear speculative loads on item->info */
2005 rmb();
2006 if (ept->req && (ept->req->item->info & INFO_ACTIVE)) {
2007 pr_err("%s(): ept%d%s prime failed. ept: config: %x"
2008 "active: %x next: %x info: %x\n",
2009 __func__, ept->num,
2010 ept->flags & EPT_FLAG_IN ? "in" : "out",
2011 ept->head->config, ept->head->active,
2012 ept->head->next, ept->head->info);
2013 writel_relaxed(n, USB_ENDPTPRIME);
2014 }
2015out:
2016 spin_unlock_irqrestore(&ui->lock, flags);
2017
2018 return count;
2019}
2020
2021static char buffer[512];
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302022static ssize_t debug_prime_fail_read(struct file *file, char __user *ubuf,
2023 size_t count, loff_t *ppos)
2024{
2025 struct usb_info *ui = file->private_data;
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302026 char *buf = buffer;
2027 unsigned long flags;
2028 struct msm_endpoint *ept;
2029 int n;
2030 int i = 0;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302031
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302032 spin_lock_irqsave(&ui->lock, flags);
2033 for (n = 0; n < 32; n++) {
2034 ept = ui->ept + n;
2035 if (ept->ep.maxpacket == 0)
2036 continue;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302037
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302038 i += scnprintf(buf + i, PAGE_SIZE - i,
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302039 "ept%d %s false_prime_count=%lu prime_fail_count=%d "
2040 "dtd_fail_count=%lu "
2041 "dTD_workaround_fail_count=%lu\n",
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302042 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
2043 ept->false_prime_fail_count,
2044 ept->actual_prime_fail_count,
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302045 ept->dTD_update_fail_count,
2046 ept->dTD_workaround_fail_count);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302047 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302048
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302049 i += scnprintf(buf + i, PAGE_SIZE - i,
2050 "dTD_update_fail count: %lu\n",
2051 ui->dTD_update_fail_count);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302052
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302053 i += scnprintf(buf + i, PAGE_SIZE - i,
2054 "prime_fail count: %d\n", ui->prime_fail_count);
2055
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302056 i += scnprintf(buf + i, PAGE_SIZE - i,
2057 "dtd_workaround_fail count: %lu\n",
2058 ui->dTD_workaround_fail_count);
2059
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302060 spin_unlock_irqrestore(&ui->lock, flags);
2061
2062 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302063}
2064
2065static int debug_prime_fail_open(struct inode *inode, struct file *file)
2066{
2067 file->private_data = inode->i_private;
2068 return 0;
2069}
2070
2071const struct file_operations prime_fail_ops = {
2072 .open = debug_prime_fail_open,
2073 .read = debug_prime_fail_read,
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302074 .write = debug_reprime_ep,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302075};
2076
Rajkumar Raghupathyc0e14a42012-09-17 16:35:47 +05302077static ssize_t debug_prop_chg_write(struct file *file,
2078 const char __user *buf, size_t count, loff_t *ppos)
2079{
2080 struct usb_info *ui = file->private_data;
2081 char kbuf[2];
2082
2083 memset(kbuf, 0, sizeof(kbuf));
2084
2085 if (copy_from_user(kbuf, buf, sizeof(kbuf)))
2086 return -EFAULT;
2087
2088 if (!strncmp(kbuf, "1", 1))
2089 ui->pdata->prop_chg = 1;
2090 else
2091 ui->pdata->prop_chg = 0;
2092
2093 return count;
2094}
2095
2096static ssize_t debug_prop_chg_read(struct file *file, char __user *ubuf,
2097 size_t count, loff_t *ppos)
2098{
2099 struct usb_info *ui = file->private_data;
2100 char kbuf[2];
2101 size_t c = 0;
2102
2103 memset(kbuf, 0, sizeof(kbuf));
2104
2105 c = scnprintf(kbuf, sizeof(kbuf), "%d\n", ui->pdata->prop_chg);
2106
2107 if (copy_to_user(ubuf, kbuf, c))
2108 return -EFAULT;
2109
2110 return simple_read_from_buffer(ubuf, count, ppos, kbuf, c);
2111}
2112
2113static int debug_prop_chg_open(struct inode *inode, struct file *file)
2114{
2115 file->private_data = inode->i_private;
2116 return 0;
2117}
2118
2119const struct file_operations debug_prop_chg_ops = {
2120 .open = debug_prop_chg_open,
2121 .read = debug_prop_chg_read,
2122 .write = debug_prop_chg_write,
2123};
2124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002125static void usb_debugfs_init(struct usb_info *ui)
2126{
2127 struct dentry *dent;
2128 dent = debugfs_create_dir(dev_name(&ui->pdev->dev), 0);
2129 if (IS_ERR(dent))
2130 return;
2131
2132 debugfs_create_file("status", 0444, dent, ui, &debug_stat_ops);
2133 debugfs_create_file("reset", 0222, dent, ui, &debug_reset_ops);
2134 debugfs_create_file("cycle", 0222, dent, ui, &debug_cycle_ops);
2135 debugfs_create_file("release_wlocks", 0666, dent, ui,
2136 &debug_wlocks_ops);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302137 debugfs_create_file("prime_fail_countt", 0666, dent, ui,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302138 &prime_fail_ops);
Rajkumar Raghupathyc0e14a42012-09-17 16:35:47 +05302139 debugfs_create_file("proprietary_chg", 0666, dent, ui,
2140 &debug_prop_chg_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002141}
2142#else
2143static void usb_debugfs_init(struct usb_info *ui) {}
2144#endif
2145
2146static int
2147msm72k_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
2148{
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302149 struct msm_endpoint *ept;
2150 unsigned char ep_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302152 if (_ep == NULL || desc == NULL)
2153 return -EINVAL;
2154
2155 ept = to_msm_endpoint(_ep);
2156 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157 _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2158 config_ept(ept);
2159 ept->wedged = 0;
2160 usb_ept_enable(ept, 1, ep_type);
2161 return 0;
2162}
2163
2164static int msm72k_disable(struct usb_ep *_ep)
2165{
2166 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2167
2168 usb_ept_enable(ept, 0, 0);
2169 flush_endpoint(ept);
Pavankumar Kondeti2c968782012-04-12 15:16:39 +05302170 /*
2171 * Clear descriptors here. Otherwise previous descriptors
2172 * will be used by function drivers upon next enumeration.
2173 */
2174 _ep->desc = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002175 return 0;
2176}
2177
2178static struct usb_request *
2179msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
2180{
2181 return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
2182}
2183
2184static void
2185msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
2186{
2187 struct msm_request *req = to_msm_request(_req);
2188 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2189 struct usb_info *ui = ept->ui;
2190
2191 /* request should not be busy */
2192 BUG_ON(req->busy);
2193 if (req->alloced)
2194 kfree(req->req.buf);
2195 dma_pool_free(ui->pool, req->item, req->item_dma);
2196 kfree(req);
2197}
2198
2199static int
2200msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
2201{
2202 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2203 struct usb_info *ui = ep->ui;
2204
2205 if (ep == &ui->ep0in) {
2206 struct msm_request *r = to_msm_request(req);
2207 if (!req->length)
2208 goto ep_queue_done;
2209 r->gadget_complete = req->complete;
2210 /* ep0_queue_ack_complete queue a receive for ACK before
2211 ** calling req->complete
2212 */
2213 req->complete = ep0_queue_ack_complete;
2214 if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
2215 ep = &ui->ep0out;
2216 goto ep_queue_done;
2217 }
2218
2219ep_queue_done:
2220 return usb_ept_queue_xfer(ep, req);
2221}
2222
2223static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2224{
2225 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2226 struct msm_request *req = to_msm_request(_req);
2227 struct usb_info *ui = ep->ui;
2228
2229 struct msm_request *temp_req;
2230 unsigned long flags;
2231
2232 if (!(ui && req && ep->req))
2233 return -EINVAL;
2234
2235 spin_lock_irqsave(&ui->lock, flags);
2236 if (!req->busy) {
2237 dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
2238 spin_unlock_irqrestore(&ui->lock, flags);
2239 return -EINVAL;
2240 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302241 del_timer(&ep->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002242 /* Stop the transfer */
2243 do {
2244 writel((1 << ep->bit), USB_ENDPTFLUSH);
2245 while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
2246 udelay(100);
2247 } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
2248
2249 req->req.status = 0;
2250 req->busy = 0;
2251
2252 if (ep->req == req) {
2253 ep->req = req->next;
2254 ep->head->next = req->item->next;
2255 } else {
2256 req->prev->next = req->next;
2257 if (req->next)
2258 req->next->prev = req->prev;
2259 req->prev->item->next = req->item->next;
2260 }
2261
2262 if (!req->next)
2263 ep->last = req->prev;
2264
2265 /* initialize request to default */
2266 req->item->next = TERMINATE;
2267 req->item->info = 0;
2268 req->live = 0;
2269 dma_unmap_single(NULL, req->dma, req->req.length,
2270 (ep->flags & EPT_FLAG_IN) ?
2271 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2272
2273 if (req->req.complete) {
2274 req->req.status = -ECONNRESET;
2275 spin_unlock_irqrestore(&ui->lock, flags);
2276 req->req.complete(&ep->ep, &req->req);
2277 spin_lock_irqsave(&ui->lock, flags);
2278 }
2279
2280 if (!req->live) {
2281 /* Reprime the endpoint for the remaining transfers */
2282 for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
2283 temp_req->live = 0;
2284 if (ep->req)
2285 usb_ept_start(ep);
2286 spin_unlock_irqrestore(&ui->lock, flags);
2287 return 0;
2288 }
2289 spin_unlock_irqrestore(&ui->lock, flags);
2290 return 0;
2291}
2292
2293static int
2294usb_ept_set_halt(struct usb_ep *_ep, int value)
2295{
2296 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2297 struct usb_info *ui = ept->ui;
2298 unsigned int in = ept->flags & EPT_FLAG_IN;
2299 unsigned int n;
2300 unsigned long flags;
2301
2302 spin_lock_irqsave(&ui->lock, flags);
2303
2304 n = readl(USB_ENDPTCTRL(ept->num));
2305
2306 if (in) {
2307 if (value)
2308 n |= CTRL_TXS;
2309 else {
2310 n &= ~CTRL_TXS;
2311 n |= CTRL_TXR;
2312 }
2313 } else {
2314 if (value)
2315 n |= CTRL_RXS;
2316 else {
2317 n &= ~CTRL_RXS;
2318 n |= CTRL_RXR;
2319 }
2320 }
2321 writel(n, USB_ENDPTCTRL(ept->num));
2322 if (!value)
2323 ept->wedged = 0;
2324 spin_unlock_irqrestore(&ui->lock, flags);
2325
2326 return 0;
2327}
2328
2329static int
2330msm72k_set_halt(struct usb_ep *_ep, int value)
2331{
2332 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2333 unsigned int in = ept->flags & EPT_FLAG_IN;
2334
2335 if (value && in && ept->req)
2336 return -EAGAIN;
2337
2338 usb_ept_set_halt(_ep, value);
2339
2340 return 0;
2341}
2342
2343static int
2344msm72k_fifo_status(struct usb_ep *_ep)
2345{
2346 return -EOPNOTSUPP;
2347}
2348
2349static void
2350msm72k_fifo_flush(struct usb_ep *_ep)
2351{
2352 flush_endpoint(to_msm_endpoint(_ep));
2353}
2354static int msm72k_set_wedge(struct usb_ep *_ep)
2355{
2356 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2357
2358 if (ept->num == 0)
2359 return -EINVAL;
2360
2361 ept->wedged = 1;
2362
2363 return msm72k_set_halt(_ep, 1);
2364}
2365
2366static const struct usb_ep_ops msm72k_ep_ops = {
2367 .enable = msm72k_enable,
2368 .disable = msm72k_disable,
2369
2370 .alloc_request = msm72k_alloc_request,
2371 .free_request = msm72k_free_request,
2372
2373 .queue = msm72k_queue,
2374 .dequeue = msm72k_dequeue,
2375
2376 .set_halt = msm72k_set_halt,
2377 .set_wedge = msm72k_set_wedge,
2378 .fifo_status = msm72k_fifo_status,
2379 .fifo_flush = msm72k_fifo_flush,
2380};
2381
2382static int msm72k_get_frame(struct usb_gadget *_gadget)
2383{
2384 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2385
2386 /* frame number is in bits 13:3 */
2387 return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
2388}
2389
2390/* VBUS reporting logically comes from a transceiver */
2391static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
2392{
2393 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2394 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2395
2396 if (is_active || atomic_read(&otg->chg_type)
2397 == USB_CHG_TYPE__WALLCHARGER)
2398 wake_lock(&ui->wlock);
2399
2400 msm_hsusb_set_vbus_state(is_active);
2401 return 0;
2402}
2403
2404/* SW workarounds
2405Issue #1 - USB Spoof Disconnect Failure
2406Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2407SW workaround - Making opmode non-driving and SuspendM set in function
2408 register of SMSC phy
2409*/
2410/* drivers may have software control over D+ pullup */
2411static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
2412{
2413 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2414 unsigned long flags;
2415
2416 if (is_active) {
2417 spin_lock_irqsave(&ui->lock, flags);
2418 if (is_usb_online(ui) && ui->driver)
2419 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2420 spin_unlock_irqrestore(&ui->lock, flags);
2421 } else {
2422 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2423 /* S/W workaround, Issue#1 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002424 usb_phy_io_write(ui->xceiv, 0x48, 0x04);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002425 }
2426
2427 /* Ensure pull-up operation is completed before returning */
2428 mb();
2429
2430 return 0;
2431}
2432
2433static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
2434{
2435 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302436 struct msm_otg *otg = to_msm_otg(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002437 unsigned long flags;
2438
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002439 atomic_set(&ui->softconnect, is_active);
2440
2441 spin_lock_irqsave(&ui->lock, flags);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302442 if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL ||
2443 atomic_read(&otg->chg_type) == USB_CHG_TYPE__WALLCHARGER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002444 spin_unlock_irqrestore(&ui->lock, flags);
2445 return 0;
2446 }
2447 spin_unlock_irqrestore(&ui->lock, flags);
2448
2449 msm72k_pullup_internal(_gadget, is_active);
2450
2451 if (is_active && !ui->gadget.is_a_peripheral)
2452 schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
2453
2454 return 0;
2455}
2456
2457static int msm72k_wakeup(struct usb_gadget *_gadget)
2458{
2459 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2460 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2461
2462 if (!atomic_read(&ui->remote_wakeup)) {
2463 dev_err(&ui->pdev->dev,
2464 "%s: remote wakeup not supported\n", __func__);
2465 return -ENOTSUPP;
2466 }
2467
2468 if (!atomic_read(&ui->configured)) {
2469 dev_err(&ui->pdev->dev,
2470 "%s: device is not configured\n", __func__);
2471 return -ENODEV;
2472 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002473 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002474
2475 disable_irq(otg->irq);
2476
2477 if (!is_usb_active())
2478 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
2479
2480 /* Ensure that USB port is resumed before enabling the IRQ */
2481 mb();
2482
2483 enable_irq(otg->irq);
2484
2485 return 0;
2486}
2487
2488/* when Gadget is configured, it will indicate how much power
2489 * can be pulled from vbus, as specified in configuiration descriptor
2490 */
2491static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2492{
2493 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2494 unsigned long flags;
2495
2496
2497 spin_lock_irqsave(&ui->lock, flags);
2498 ui->b_max_pow = mA;
2499 ui->flags = USB_FLAG_CONFIGURED;
2500 spin_unlock_irqrestore(&ui->lock, flags);
2501
2502 schedule_work(&ui->work);
2503
2504 return 0;
2505}
2506
2507static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
2508{
2509 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2510 unsigned long flags;
2511 int ret = 0;
2512
2513 spin_lock_irqsave(&ui->lock, flags);
2514 if (set) {
2515 if (ui->pdata && ui->pdata->self_powered)
2516 atomic_set(&ui->self_powered, 1);
2517 else
2518 ret = -EOPNOTSUPP;
2519 } else {
2520 /* We can always work as a bus powered device */
2521 atomic_set(&ui->self_powered, 0);
2522 }
2523 spin_unlock_irqrestore(&ui->lock, flags);
2524
2525 return ret;
2526
2527}
2528
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002529static int msm72k_gadget_start(struct usb_gadget_driver *driver,
2530 int (*bind)(struct usb_gadget *));
2531static int msm72k_gadget_stop(struct usb_gadget_driver *driver);
2532
2533
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002534static const struct usb_gadget_ops msm72k_ops = {
2535 .get_frame = msm72k_get_frame,
2536 .vbus_session = msm72k_udc_vbus_session,
2537 .vbus_draw = msm72k_udc_vbus_draw,
2538 .pullup = msm72k_pullup,
2539 .wakeup = msm72k_wakeup,
2540 .set_selfpowered = msm72k_set_selfpowered,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002541 .start = msm72k_gadget_start,
2542 .stop = msm72k_gadget_stop
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002543};
2544
2545static void usb_do_remote_wakeup(struct work_struct *w)
2546{
2547 struct usb_info *ui = the_usb_info;
2548
2549 msm72k_wakeup(&ui->gadget);
2550}
2551
2552static ssize_t usb_remote_wakeup(struct device *dev,
2553 struct device_attribute *attr, const char *buf, size_t count)
2554{
2555 struct usb_info *ui = the_usb_info;
2556
2557 msm72k_wakeup(&ui->gadget);
2558
2559 return count;
2560}
2561
2562static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
2563 char *buf)
2564{
2565 size_t i;
2566 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
2567 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
2568 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
2569 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
2570 "USB_STATE_SUSPENDED"
2571 };
2572
2573 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
2574 return i;
2575}
2576
2577static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
2578 char *buf)
2579{
2580 struct usb_info *ui = the_usb_info;
2581 size_t i;
2582 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
2583 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
2584
2585 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
2586 return i;
2587}
2588
2589static ssize_t store_usb_chg_current(struct device *dev,
2590 struct device_attribute *attr, const char *buf, size_t count)
2591{
2592 struct usb_info *ui = the_usb_info;
2593 unsigned long mA;
2594
2595 if (ui->gadget.is_a_peripheral)
2596 return -EINVAL;
2597
2598 if (strict_strtoul(buf, 10, &mA))
2599 return -EINVAL;
2600
2601 ui->chg_current = mA;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002602 usb_phy_set_power(ui->xceiv, mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002603
2604 return count;
2605}
2606
2607static ssize_t show_usb_chg_current(struct device *dev,
2608 struct device_attribute *attr, char *buf)
2609{
2610 struct usb_info *ui = the_usb_info;
2611 size_t count;
2612
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302613 count = snprintf(buf, PAGE_SIZE, "%d", ui->chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002614
2615 return count;
2616}
2617
2618static ssize_t show_usb_chg_type(struct device *dev,
2619 struct device_attribute *attr, char *buf)
2620{
2621 struct usb_info *ui = the_usb_info;
2622 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2623 size_t count;
2624 char *chg_type[] = {"STD DOWNSTREAM PORT",
2625 "CARKIT",
2626 "DEDICATED CHARGER",
2627 "INVALID"};
2628
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302629 count = snprintf(buf, PAGE_SIZE, "%s",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002630 chg_type[atomic_read(&otg->chg_type)]);
2631
2632 return count;
2633}
2634static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
2635static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
2636static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
2637static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
2638static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
2639 show_usb_chg_current, store_usb_chg_current);
2640
2641#ifdef CONFIG_USB_OTG
2642static ssize_t store_host_req(struct device *dev,
2643 struct device_attribute *attr, const char *buf, size_t count)
2644{
2645 struct usb_info *ui = the_usb_info;
2646 unsigned long val, flags;
2647
2648 if (strict_strtoul(buf, 10, &val))
2649 return -EINVAL;
2650
2651 dev_dbg(&ui->pdev->dev, "%s host request\n",
2652 val ? "set" : "clear");
2653
2654 spin_lock_irqsave(&ui->lock, flags);
2655 if (ui->hnp_avail)
2656 ui->gadget.host_request = !!val;
2657 spin_unlock_irqrestore(&ui->lock, flags);
2658
2659 return count;
2660}
2661static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
2662
2663/* How do we notify user space about HNP availability?
2664 * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
2665 * Introduce hnp_avail flag and set when HNP polling request arrives.
2666 * The expectation is that user space checks hnp availability before
2667 * requesting host role via above sysfs node.
2668 */
2669static ssize_t show_host_avail(struct device *dev,
2670 struct device_attribute *attr, char *buf)
2671{
2672 struct usb_info *ui = the_usb_info;
2673 size_t count;
2674 unsigned long flags;
2675
2676 spin_lock_irqsave(&ui->lock, flags);
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302677 count = snprintf(buf, PAGE_SIZE, "%d\n", ui->hnp_avail);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002678 spin_unlock_irqrestore(&ui->lock, flags);
2679
2680 return count;
2681}
2682static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
2683
2684static struct attribute *otg_attrs[] = {
2685 &dev_attr_host_request.attr,
2686 &dev_attr_host_avail.attr,
2687 NULL,
2688};
2689
2690static struct attribute_group otg_attr_grp = {
2691 .name = "otg",
2692 .attrs = otg_attrs,
2693};
2694#endif
2695
2696static int msm72k_probe(struct platform_device *pdev)
2697{
2698 struct usb_info *ui;
2699 struct msm_otg *otg;
2700 int retval;
2701
2702 dev_dbg(&pdev->dev, "msm72k_probe\n");
2703 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
2704 if (!ui)
2705 return -ENOMEM;
2706
2707 ui->pdev = pdev;
2708 ui->pdata = pdev->dev.platform_data;
2709
2710 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
2711 if (!ui->buf)
2712 return usb_free(ui, -ENOMEM);
2713
2714 ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
2715 if (!ui->pool)
2716 return usb_free(ui, -ENOMEM);
2717
Steve Mucklef132c6c2012-06-06 18:30:57 -07002718 ui->xceiv = usb_get_transceiver();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002719 if (!ui->xceiv)
2720 return usb_free(ui, -ENODEV);
2721
2722 otg = to_msm_otg(ui->xceiv);
2723 ui->addr = otg->regs;
2724
2725 ui->gadget.ops = &msm72k_ops;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002726 ui->gadget.max_speed = USB_SPEED_HIGH;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002727 device_initialize(&ui->gadget.dev);
2728 dev_set_name(&ui->gadget.dev, "gadget");
2729 ui->gadget.dev.parent = &pdev->dev;
2730 ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
2731
2732#ifdef CONFIG_USB_OTG
2733 ui->gadget.is_otg = 1;
2734#endif
2735
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002736 retval = usb_add_gadget_udc(&pdev->dev, &ui->gadget);
2737 if (retval)
2738 return usb_free(ui, retval);
2739
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002740 ui->sdev.name = DRIVER_NAME;
2741 ui->sdev.print_name = print_switch_name;
2742 ui->sdev.print_state = print_switch_state;
2743
2744 retval = switch_dev_register(&ui->sdev);
2745 if (retval)
2746 return usb_free(ui, retval);
2747
2748 the_usb_info = ui;
2749
2750 wake_lock_init(&ui->wlock,
2751 WAKE_LOCK_SUSPEND, "usb_bus_active");
2752
2753 usb_debugfs_init(ui);
2754
2755 usb_prepare(ui);
2756
2757#ifdef CONFIG_USB_OTG
2758 retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
2759 if (retval) {
2760 dev_err(&ui->pdev->dev,
2761 "failed to create otg sysfs directory:"
2762 "err:(%d)\n", retval);
2763 }
2764#endif
2765
Steve Mucklef132c6c2012-06-06 18:30:57 -07002766 retval = otg_set_peripheral(ui->xceiv->otg, &ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002767 if (retval) {
2768 dev_err(&ui->pdev->dev,
2769 "%s: Cannot bind the transceiver, retval:(%d)\n",
2770 __func__, retval);
2771 switch_dev_unregister(&ui->sdev);
2772 wake_lock_destroy(&ui->wlock);
2773 return usb_free(ui, retval);
2774 }
2775
2776 pm_runtime_enable(&pdev->dev);
2777
2778 /* Setup phy stuck timer */
2779 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
2780 setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
2781 return 0;
2782}
2783
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002784static int msm72k_gadget_start(struct usb_gadget_driver *driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002785 int (*bind)(struct usb_gadget *))
2786{
2787 struct usb_info *ui = the_usb_info;
2788 int retval, n;
2789
2790 if (!driver
Steve Mucklef132c6c2012-06-06 18:30:57 -07002791 || driver->max_speed < USB_SPEED_FULL
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002792 || !bind
2793 || !driver->disconnect
2794 || !driver->setup)
2795 return -EINVAL;
2796 if (!ui)
2797 return -ENODEV;
2798 if (ui->driver)
2799 return -EBUSY;
2800
2801 /* first hook up the driver ... */
2802 ui->driver = driver;
2803 ui->gadget.dev.driver = &driver->driver;
2804 ui->gadget.name = driver_name;
2805 INIT_LIST_HEAD(&ui->gadget.ep_list);
2806 ui->gadget.ep0 = &ui->ep0in.ep;
2807 INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
2808 ui->gadget.speed = USB_SPEED_UNKNOWN;
2809 atomic_set(&ui->softconnect, 1);
2810
2811 for (n = 1; n < 16; n++) {
2812 struct msm_endpoint *ept = ui->ept + n;
2813 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2814 ept->ep.maxpacket = 512;
2815 }
2816 for (n = 17; n < 32; n++) {
2817 struct msm_endpoint *ept = ui->ept + n;
2818 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2819 ept->ep.maxpacket = 512;
2820 }
2821
2822 retval = device_add(&ui->gadget.dev);
2823 if (retval)
2824 goto fail;
2825
2826 retval = bind(&ui->gadget);
2827 if (retval) {
2828 dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
2829 driver->driver.name, retval);
2830 device_del(&ui->gadget.dev);
2831 goto fail;
2832 }
2833
2834 retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
2835 if (retval != 0)
2836 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2837 "(wakeup) error: (%d)\n", retval);
2838 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
2839 if (retval != 0)
2840 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2841 " (usb_state) error: (%d)\n", retval);
2842
2843 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
2844 if (retval != 0)
2845 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2846 " (usb_speed) error: (%d)\n", retval);
2847
2848 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
2849 if (retval != 0)
2850 dev_err(&ui->pdev->dev,
2851 "failed to create sysfs entry(chg_type): err:(%d)\n",
2852 retval);
2853 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
2854 if (retval != 0)
2855 dev_err(&ui->pdev->dev,
2856 "failed to create sysfs entry(chg_current):"
2857 "err:(%d)\n", retval);
2858
2859 dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
2860 driver->driver.name);
2861 usb_start(ui);
2862
2863 return 0;
2864
2865fail:
2866 ui->driver = NULL;
2867 ui->gadget.dev.driver = NULL;
2868 return retval;
2869}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002870
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002871static int msm72k_gadget_stop(struct usb_gadget_driver *driver)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002872{
2873 struct usb_info *dev = the_usb_info;
2874
2875 if (!dev)
2876 return -ENODEV;
2877 if (!driver || driver != dev->driver || !driver->unbind)
2878 return -EINVAL;
2879
2880 msm72k_pullup_internal(&dev->gadget, 0);
2881 if (dev->irq) {
2882 free_irq(dev->irq, dev);
2883 dev->irq = 0;
2884 }
2885 dev->state = USB_STATE_IDLE;
2886 atomic_set(&dev->configured, 0);
2887 switch_set_state(&dev->sdev, 0);
2888 /* cancel pending ep0 transactions */
2889 flush_endpoint(&dev->ep0out);
2890 flush_endpoint(&dev->ep0in);
2891
2892 device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
2893 device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
2894 device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
2895 device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
2896 device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
2897 driver->disconnect(&dev->gadget);
2898 driver->unbind(&dev->gadget);
2899 dev->gadget.dev.driver = NULL;
2900 dev->driver = NULL;
2901
2902 device_del(&dev->gadget.dev);
2903
2904 dev_dbg(&dev->pdev->dev,
2905 "unregistered gadget driver '%s'\n", driver->driver.name);
2906 return 0;
2907}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002908
2909
2910static int msm72k_udc_runtime_suspend(struct device *dev)
2911{
2912 dev_dbg(dev, "pm_runtime: suspending...\n");
2913 return 0;
2914}
2915
2916static int msm72k_udc_runtime_resume(struct device *dev)
2917{
2918 dev_dbg(dev, "pm_runtime: resuming...\n");
2919 return 0;
2920}
2921
2922static int msm72k_udc_runtime_idle(struct device *dev)
2923{
2924 dev_dbg(dev, "pm_runtime: idling...\n");
2925 return 0;
2926}
2927
2928static struct dev_pm_ops msm72k_udc_dev_pm_ops = {
2929 .runtime_suspend = msm72k_udc_runtime_suspend,
2930 .runtime_resume = msm72k_udc_runtime_resume,
2931 .runtime_idle = msm72k_udc_runtime_idle
2932};
2933
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002934static int __exit msm72k_remove(struct platform_device *pdev)
2935{
2936 struct usb_info *ui = container_of(&pdev, struct usb_info, pdev);
2937
2938 return usb_free(ui, 0);
2939}
2940
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002941static struct platform_driver usb_driver = {
2942 .probe = msm72k_probe,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002943 .remove = msm72k_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002944 .driver = { .name = "msm_hsusb",
2945 .pm = &msm72k_udc_dev_pm_ops, },
2946};
2947
2948static int __init init(void)
2949{
2950 return platform_driver_register(&usb_driver);
2951}
2952module_init(init);
2953
2954static void __exit cleanup(void)
2955{
2956 platform_driver_unregister(&usb_driver);
2957}
2958module_exit(cleanup);
2959
2960MODULE_DESCRIPTION(DRIVER_DESC);
2961MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
2962MODULE_LICENSE("GPL");