blob: 923be08597342489c8e2385c688c92f18827b179 [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;
Rajkumar Raghupathy4b06ee92013-01-17 15:10:36 +05301306 struct msm_otg *dev = to_msm_otg(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 unsigned n;
1308 unsigned long flags;
1309
Rajkumar Raghupathy4b06ee92013-01-17 15:10:36 +05301310 if (atomic_read(&dev->in_lpm))
1311 return IRQ_NONE;
1312
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 n = readl(USB_USBSTS);
1314 writel(n, USB_USBSTS);
1315
1316 /* somehow we got an IRQ while in the reset sequence: ignore it */
1317 if (!atomic_read(&ui->running))
1318 return IRQ_HANDLED;
1319
1320 if (n & STS_PCI) {
1321 msm_hsusb_set_speed(ui);
1322 if (atomic_read(&ui->configured)) {
1323 wake_lock(&ui->wlock);
1324
1325 spin_lock_irqsave(&ui->lock, flags);
1326 ui->usb_state = USB_STATE_CONFIGURED;
1327 ui->flags = USB_FLAG_CONFIGURED;
1328 spin_unlock_irqrestore(&ui->lock, flags);
1329
1330 ui->driver->resume(&ui->gadget);
1331 schedule_work(&ui->work);
1332 } else {
1333 msm_hsusb_set_state(USB_STATE_DEFAULT);
1334 }
1335
1336#ifdef CONFIG_USB_OTG
1337 /* notify otg to clear A_BIDL_ADIS timer */
1338 if (ui->gadget.is_a_peripheral)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001339 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001340#endif
1341 }
1342
1343 if (n & STS_URI) {
1344 dev_dbg(&ui->pdev->dev, "reset\n");
1345 spin_lock_irqsave(&ui->lock, flags);
1346 ui->gadget.speed = USB_SPEED_UNKNOWN;
1347 spin_unlock_irqrestore(&ui->lock, flags);
1348#ifdef CONFIG_USB_OTG
1349 /* notify otg to clear A_BIDL_ADIS timer */
1350 if (ui->gadget.is_a_peripheral)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001351 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352 spin_lock_irqsave(&ui->lock, flags);
1353 /* Host request is persistent across reset */
1354 ui->gadget.b_hnp_enable = 0;
1355 ui->hnp_avail = 0;
1356 spin_unlock_irqrestore(&ui->lock, flags);
1357#endif
1358 msm_hsusb_set_state(USB_STATE_DEFAULT);
1359 atomic_set(&ui->remote_wakeup, 0);
1360 if (!ui->gadget.is_a_peripheral)
1361 schedule_delayed_work(&ui->chg_stop, 0);
1362
1363 writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
1364 writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
1365 writel(0xffffffff, USB_ENDPTFLUSH);
1366 writel(0, USB_ENDPTCTRL(1));
1367
1368 wake_lock(&ui->wlock);
1369 if (atomic_read(&ui->configured)) {
1370 /* marking us offline will cause ept queue attempts
1371 ** to fail
1372 */
1373 atomic_set(&ui->configured, 0);
1374 /* Defer sending offline uevent to userspace */
1375 atomic_set(&ui->offline_pending, 1);
1376
1377 /* XXX: we can't seem to detect going offline,
1378 * XXX: so deconfigure on reset for the time being
1379 */
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05301380 dev_dbg(&ui->pdev->dev,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001381 "usb: notify offline\n");
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05301382 ui->driver->disconnect(&ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001383 /* cancel pending ep0 transactions */
1384 flush_endpoint(&ui->ep0out);
1385 flush_endpoint(&ui->ep0in);
1386
1387 }
1388 /* Start phy stuck timer */
1389 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1390 mod_timer(&phy_status_timer, PHY_STATUS_CHECK_DELAY);
1391 }
1392
1393 if (n & STS_SLI) {
1394 dev_dbg(&ui->pdev->dev, "suspend\n");
1395
1396 spin_lock_irqsave(&ui->lock, flags);
1397 ui->usb_state = USB_STATE_SUSPENDED;
1398 ui->flags = USB_FLAG_SUSPEND;
1399 spin_unlock_irqrestore(&ui->lock, flags);
1400
1401 ui->driver->suspend(&ui->gadget);
1402 schedule_work(&ui->work);
1403#ifdef CONFIG_USB_OTG
1404 /* notify otg for
1405 * 1. kicking A_BIDL_ADIS timer in case of A-peripheral
1406 * 2. disabling pull-up and kicking B_ASE0_RST timer
1407 */
1408 if (ui->gadget.b_hnp_enable || ui->gadget.is_a_peripheral)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001409 usb_phy_set_suspend(ui->xceiv, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410#endif
1411 }
1412
1413 if (n & STS_UI) {
1414 n = readl(USB_ENDPTSETUPSTAT);
1415 if (n & EPT_RX(0))
1416 handle_setup(ui);
1417
1418 n = readl(USB_ENDPTCOMPLETE);
1419 writel(n, USB_ENDPTCOMPLETE);
1420 while (n) {
1421 unsigned bit = __ffs(n);
1422 handle_endpoint(ui, bit);
1423 n = n & (~(1 << bit));
1424 }
1425 }
1426 return IRQ_HANDLED;
1427}
1428
1429static void usb_prepare(struct usb_info *ui)
1430{
1431 spin_lock_init(&ui->lock);
1432
1433 memset(ui->buf, 0, 4096);
1434 ui->head = (void *) (ui->buf + 0);
1435
1436 /* only important for reset/reinit */
1437 memset(ui->ept, 0, sizeof(ui->ept));
1438 ui->next_item = 0;
1439 ui->next_ifc_num = 0;
1440
1441 init_endpoints(ui);
1442
1443 ui->ep0in.ep.maxpacket = 64;
1444 ui->ep0out.ep.maxpacket = 64;
1445
1446 ui->setup_req =
1447 usb_ept_alloc_req(&ui->ep0in, SETUP_BUF_SIZE, GFP_KERNEL);
1448
1449 INIT_WORK(&ui->work, usb_do_work);
1450 INIT_DELAYED_WORK(&ui->chg_det, usb_chg_detect);
1451 INIT_DELAYED_WORK(&ui->chg_stop, usb_chg_stop);
1452 INIT_DELAYED_WORK(&ui->rw_work, usb_do_remote_wakeup);
1453 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
1454 INIT_WORK(&ui->phy_status_check, usb_phy_stuck_recover);
1455}
1456
1457static void usb_reset(struct usb_info *ui)
1458{
1459 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1460
1461 dev_dbg(&ui->pdev->dev, "reset controller\n");
1462
1463 atomic_set(&ui->running, 0);
1464
1465 /*
1466 * PHY reset takes minimum 100 msec. Hence reset only link
1467 * during HNP. Reset PHY and link in B-peripheral mode.
1468 */
1469 if (ui->gadget.is_a_peripheral)
1470 otg->reset(ui->xceiv, 0);
1471 else
1472 otg->reset(ui->xceiv, 1);
1473
1474 /* set usb controller interrupt threshold to zero*/
1475 writel((readl(USB_USBCMD) & ~USBCMD_ITC_MASK) | USBCMD_ITC(0),
1476 USB_USBCMD);
1477
1478 writel(ui->dma, USB_ENDPOINTLISTADDR);
1479
1480 configure_endpoints(ui);
1481
1482 /* marking us offline will cause ept queue attempts to fail */
1483 atomic_set(&ui->configured, 0);
1484
1485 if (ui->driver) {
1486 dev_dbg(&ui->pdev->dev, "usb: notify offline\n");
1487 ui->driver->disconnect(&ui->gadget);
1488 }
1489
1490 /* cancel pending ep0 transactions */
1491 flush_endpoint(&ui->ep0out);
1492 flush_endpoint(&ui->ep0in);
1493
1494 /* enable interrupts */
1495 writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR);
1496
1497 /* Ensure that h/w RESET is completed before returning */
1498 mb();
1499
1500 atomic_set(&ui->running, 1);
1501}
1502
1503static void usb_start(struct usb_info *ui)
1504{
1505 unsigned long flags;
1506
1507 spin_lock_irqsave(&ui->lock, flags);
1508 ui->flags |= USB_FLAG_START;
1509 schedule_work(&ui->work);
1510 spin_unlock_irqrestore(&ui->lock, flags);
1511}
1512
1513static int usb_free(struct usb_info *ui, int ret)
1514{
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03001515 if (ret)
1516 dev_dbg(&ui->pdev->dev, "usb_free(%d)\n", ret);
1517
1518 usb_del_gadget_udc(&ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001519
1520 if (ui->xceiv)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001521 usb_put_transceiver(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001522
1523 if (ui->irq)
1524 free_irq(ui->irq, 0);
1525 if (ui->pool)
1526 dma_pool_destroy(ui->pool);
1527 if (ui->dma)
1528 dma_free_coherent(&ui->pdev->dev, 4096, ui->buf, ui->dma);
1529 kfree(ui);
1530 return ret;
1531}
1532
1533static void usb_do_work_check_vbus(struct usb_info *ui)
1534{
1535 unsigned long iflags;
1536
1537 spin_lock_irqsave(&ui->lock, iflags);
1538 if (is_usb_online(ui))
1539 ui->flags |= USB_FLAG_VBUS_ONLINE;
1540 else
1541 ui->flags |= USB_FLAG_VBUS_OFFLINE;
1542 spin_unlock_irqrestore(&ui->lock, iflags);
1543}
1544
1545static void usb_do_work(struct work_struct *w)
1546{
1547 struct usb_info *ui = container_of(w, struct usb_info, work);
1548 struct msm_otg *otg = to_msm_otg(ui->xceiv);
1549 unsigned long iflags;
1550 unsigned flags, _vbus;
1551
1552 for (;;) {
1553 spin_lock_irqsave(&ui->lock, iflags);
1554 flags = ui->flags;
1555 ui->flags = 0;
1556 _vbus = is_usb_online(ui);
1557 spin_unlock_irqrestore(&ui->lock, iflags);
1558
1559 /* give up if we have nothing to do */
1560 if (flags == 0)
1561 break;
1562
1563 switch (ui->state) {
1564 case USB_STATE_IDLE:
1565 if (flags & USB_FLAG_START) {
1566 int ret;
1567
1568 if (!_vbus) {
1569 ui->state = USB_STATE_OFFLINE;
1570 break;
1571 }
1572
1573 pm_runtime_get_noresume(&ui->pdev->dev);
1574 pm_runtime_resume(&ui->pdev->dev);
1575 dev_dbg(&ui->pdev->dev,
1576 "msm72k_udc: IDLE -> ONLINE\n");
1577 usb_reset(ui);
1578 ret = request_irq(otg->irq, usb_interrupt,
1579 IRQF_SHARED,
1580 ui->pdev->name, ui);
1581 /* FIXME: should we call BUG_ON when
1582 * requst irq fails
1583 */
1584 if (ret) {
1585 dev_err(&ui->pdev->dev,
1586 "hsusb: peripheral: request irq"
1587 " failed:(%d)", ret);
1588 break;
1589 }
1590 ui->irq = otg->irq;
1591 ui->state = USB_STATE_ONLINE;
1592 usb_do_work_check_vbus(ui);
1593
1594 if (!atomic_read(&ui->softconnect))
1595 break;
1596
1597 msm72k_pullup_internal(&ui->gadget, 1);
1598
1599 if (!ui->gadget.is_a_peripheral)
1600 schedule_delayed_work(
1601 &ui->chg_det,
1602 USB_CHG_DET_DELAY);
1603
1604 }
1605 break;
1606 case USB_STATE_ONLINE:
1607 if (atomic_read(&ui->offline_pending)) {
1608 switch_set_state(&ui->sdev, 0);
1609 atomic_set(&ui->offline_pending, 0);
1610 }
1611
1612 /* If at any point when we were online, we received
1613 * the signal to go offline, we must honor it
1614 */
1615 if (flags & USB_FLAG_VBUS_OFFLINE) {
1616
1617 ui->chg_current = 0;
1618 /* wait incase chg_detect is running */
1619 if (!ui->gadget.is_a_peripheral)
1620 cancel_delayed_work_sync(&ui->chg_det);
1621
1622 dev_dbg(&ui->pdev->dev,
1623 "msm72k_udc: ONLINE -> OFFLINE\n");
1624
1625 atomic_set(&ui->running, 0);
1626 atomic_set(&ui->remote_wakeup, 0);
1627 atomic_set(&ui->configured, 0);
1628
1629 if (ui->driver) {
1630 dev_dbg(&ui->pdev->dev,
1631 "usb: notify offline\n");
1632 ui->driver->disconnect(&ui->gadget);
1633 }
1634 /* cancel pending ep0 transactions */
1635 flush_endpoint(&ui->ep0out);
1636 flush_endpoint(&ui->ep0in);
1637
1638 /* synchronize with irq context */
1639 spin_lock_irqsave(&ui->lock, iflags);
1640#ifdef CONFIG_USB_OTG
1641 ui->gadget.host_request = 0;
1642 ui->gadget.b_hnp_enable = 0;
1643 ui->hnp_avail = 0;
1644#endif
1645 msm72k_pullup_internal(&ui->gadget, 0);
1646 spin_unlock_irqrestore(&ui->lock, iflags);
1647
1648
1649 /* if charger is initialized to known type
1650 * we must let modem know about charger
1651 * disconnection
1652 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001653 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001654
1655 if (ui->irq) {
Chiranjeevi Velempati2a9c1cb2012-12-21 18:26:23 +05301656 /* Disable and acknowledge all
1657 * USB interrupts before freeing
1658 * irq, so that no USB spurious
1659 * interrupt occurs during USB cable
1660 * disconnect which may lead to
1661 * IRQ nobody cared error.
1662 */
1663 writel_relaxed(0, USB_USBINTR);
1664 writel_relaxed(readl_relaxed(USB_USBSTS)
1665 , USB_USBSTS);
1666 /* Ensure that above STOREs are
1667 * completed before enabling
1668 * interrupts */
1669 wmb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 free_irq(ui->irq, ui);
1671 ui->irq = 0;
1672 }
1673
1674
1675 switch_set_state(&ui->sdev, 0);
1676
1677 ui->state = USB_STATE_OFFLINE;
1678 usb_do_work_check_vbus(ui);
1679 pm_runtime_put_noidle(&ui->pdev->dev);
1680 pm_runtime_suspend(&ui->pdev->dev);
1681 wake_unlock(&ui->wlock);
1682 break;
1683 }
1684 if (flags & USB_FLAG_SUSPEND) {
1685 int maxpower = usb_get_max_power(ui);
1686
1687 if (maxpower < 0)
1688 break;
1689
Steve Mucklef132c6c2012-06-06 18:30:57 -07001690 usb_phy_set_power(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001691 /* To support TCXO during bus suspend
1692 * This might be dummy check since bus suspend
1693 * is not implemented as of now
1694 * */
1695 if (release_wlocks)
1696 wake_unlock(&ui->wlock);
1697
1698 /* TBD: Initiate LPM at usb bus suspend */
1699 break;
1700 }
1701 if (flags & USB_FLAG_CONFIGURED) {
1702 int maxpower = usb_get_max_power(ui);
1703
1704 /* We may come here even when no configuration
1705 * is selected. Send online/offline event
1706 * accordingly.
1707 */
1708 switch_set_state(&ui->sdev,
1709 atomic_read(&ui->configured));
1710
1711 if (maxpower < 0)
1712 break;
1713
1714 ui->chg_current = maxpower;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001715 usb_phy_set_power(ui->xceiv, maxpower);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001716 break;
1717 }
1718 if (flags & USB_FLAG_RESET) {
1719 dev_dbg(&ui->pdev->dev,
1720 "msm72k_udc: ONLINE -> RESET\n");
1721 msm72k_pullup_internal(&ui->gadget, 0);
1722 usb_reset(ui);
1723 msm72k_pullup_internal(&ui->gadget, 1);
1724 dev_dbg(&ui->pdev->dev,
1725 "msm72k_udc: RESET -> ONLINE\n");
1726 break;
1727 }
1728 break;
1729 case USB_STATE_OFFLINE:
1730 /* If we were signaled to go online and vbus is still
1731 * present when we received the signal, go online.
1732 */
1733 if ((flags & USB_FLAG_VBUS_ONLINE) && _vbus) {
1734 int ret;
1735
1736 pm_runtime_get_noresume(&ui->pdev->dev);
1737 pm_runtime_resume(&ui->pdev->dev);
1738 dev_dbg(&ui->pdev->dev,
1739 "msm72k_udc: OFFLINE -> ONLINE\n");
1740
1741 usb_reset(ui);
1742 ui->state = USB_STATE_ONLINE;
1743 usb_do_work_check_vbus(ui);
1744 ret = request_irq(otg->irq, usb_interrupt,
1745 IRQF_SHARED,
1746 ui->pdev->name, ui);
1747 /* FIXME: should we call BUG_ON when
1748 * requst irq fails
1749 */
1750 if (ret) {
1751 dev_err(&ui->pdev->dev,
1752 "hsusb: peripheral: request irq"
1753 " failed:(%d)", ret);
1754 break;
1755 }
1756 ui->irq = otg->irq;
1757 enable_irq_wake(otg->irq);
1758
1759 if (!atomic_read(&ui->softconnect))
1760 break;
1761 msm72k_pullup_internal(&ui->gadget, 1);
1762
1763 if (!ui->gadget.is_a_peripheral)
1764 schedule_delayed_work(
1765 &ui->chg_det,
1766 USB_CHG_DET_DELAY);
1767 }
1768 break;
1769 }
1770 }
1771}
1772
1773/* FIXME - the callers of this function should use a gadget API instead.
1774 * This is called from htc_battery.c and board-halibut.c
1775 * WARNING - this can get called before this driver is initialized.
1776 */
1777void msm_hsusb_set_vbus_state(int online)
1778{
1779 unsigned long flags;
1780 struct usb_info *ui = the_usb_info;
1781
1782 if (!ui) {
1783 pr_err("%s called before driver initialized\n", __func__);
1784 return;
1785 }
1786
1787 spin_lock_irqsave(&ui->lock, flags);
1788
1789 if (is_usb_online(ui) == online)
1790 goto out;
1791
1792 if (online) {
1793 ui->usb_state = USB_STATE_POWERED;
1794 ui->flags |= USB_FLAG_VBUS_ONLINE;
1795 } else {
1796 ui->gadget.speed = USB_SPEED_UNKNOWN;
1797 ui->usb_state = USB_STATE_NOTATTACHED;
1798 ui->flags |= USB_FLAG_VBUS_OFFLINE;
Chiranjeevi Velempati18dda792012-07-16 17:02:32 +05301799 ui->chg_type_retry_cnt = 0;
1800 ui->proprietary_chg = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801 }
1802 if (in_interrupt()) {
1803 schedule_work(&ui->work);
1804 } else {
1805 spin_unlock_irqrestore(&ui->lock, flags);
1806 usb_do_work(&ui->work);
1807 return;
1808 }
1809out:
1810 spin_unlock_irqrestore(&ui->lock, flags);
1811}
1812
1813#if defined(CONFIG_DEBUG_FS)
1814
1815void usb_function_reenumerate(void)
1816{
1817 struct usb_info *ui = the_usb_info;
1818
1819 /* disable and re-enable the D+ pullup */
1820 dev_dbg(&ui->pdev->dev, "disable pullup\n");
1821 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
1822
1823 msleep(10);
1824
1825 dev_dbg(&ui->pdev->dev, "enable pullup\n");
1826 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
1827}
1828
1829static char debug_buffer[PAGE_SIZE];
1830
1831static ssize_t debug_read_status(struct file *file, char __user *ubuf,
1832 size_t count, loff_t *ppos)
1833{
1834 struct usb_info *ui = file->private_data;
1835 char *buf = debug_buffer;
1836 unsigned long flags;
1837 struct msm_endpoint *ept;
1838 struct msm_request *req;
1839 int n;
1840 int i = 0;
1841
1842 spin_lock_irqsave(&ui->lock, flags);
1843
1844 i += scnprintf(buf + i, PAGE_SIZE - i,
1845 "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
1846 readl(USB_ENDPTSETUPSTAT),
1847 readl(USB_ENDPTPRIME),
1848 readl(USB_ENDPTSTAT),
1849 readl(USB_ENDPTCOMPLETE));
1850 i += scnprintf(buf + i, PAGE_SIZE - i,
1851 "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
1852 readl(USB_USBCMD),
1853 readl(USB_USBSTS),
1854 readl(USB_USBINTR),
1855 readl(USB_PORTSC));
1856
1857
1858 for (n = 0; n < 32; n++) {
1859 ept = ui->ept + n;
1860 if (ept->ep.maxpacket == 0)
1861 continue;
1862
1863 i += scnprintf(buf + i, PAGE_SIZE - i,
1864 "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
1865 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
1866 ept->head->config, ept->head->active,
1867 ept->head->next, ept->head->info);
1868
1869 for (req = ept->req; req; req = req->next)
1870 i += scnprintf(buf + i, PAGE_SIZE - i,
1871 " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
1872 req->item_dma, req->item->next,
1873 req->item->info, req->item->page0,
1874 req->busy ? 'B' : ' ',
1875 req->live ? 'L' : ' ');
1876 }
1877
1878 i += scnprintf(buf + i, PAGE_SIZE - i,
1879 "phy failure count: %d\n", ui->phy_fail_count);
1880
1881 spin_unlock_irqrestore(&ui->lock, flags);
1882
1883 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
1884}
1885
1886static ssize_t debug_write_reset(struct file *file, const char __user *buf,
1887 size_t count, loff_t *ppos)
1888{
1889 struct usb_info *ui = file->private_data;
1890 unsigned long flags;
1891
1892 spin_lock_irqsave(&ui->lock, flags);
1893 ui->flags |= USB_FLAG_RESET;
1894 schedule_work(&ui->work);
1895 spin_unlock_irqrestore(&ui->lock, flags);
1896
1897 return count;
1898}
1899
1900static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
1901 size_t count, loff_t *ppos)
1902{
1903 usb_function_reenumerate();
1904 return count;
1905}
1906
1907static int debug_open(struct inode *inode, struct file *file)
1908{
1909 file->private_data = inode->i_private;
1910 return 0;
1911}
1912
1913const struct file_operations debug_stat_ops = {
1914 .open = debug_open,
1915 .read = debug_read_status,
1916};
1917
1918const struct file_operations debug_reset_ops = {
1919 .open = debug_open,
1920 .write = debug_write_reset,
1921};
1922
1923const struct file_operations debug_cycle_ops = {
1924 .open = debug_open,
1925 .write = debug_write_cycle,
1926};
1927
1928static ssize_t debug_read_release_wlocks(struct file *file, char __user *ubuf,
1929 size_t count, loff_t *ppos)
1930{
1931 char kbuf[10];
1932 size_t c = 0;
1933
1934 memset(kbuf, 0, 10);
1935
1936 c = scnprintf(kbuf, 10, "%d", release_wlocks);
1937
1938 if (copy_to_user(ubuf, kbuf, c))
1939 return -EFAULT;
1940
1941 return c;
1942}
1943static ssize_t debug_write_release_wlocks(struct file *file,
1944 const char __user *buf, size_t count, loff_t *ppos)
1945{
1946 char kbuf[10];
1947 long temp;
1948
1949 memset(kbuf, 0, 10);
1950
1951 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1952 return -EFAULT;
1953
1954 if (strict_strtol(kbuf, 10, &temp))
1955 return -EINVAL;
1956
1957 if (temp)
1958 release_wlocks = 1;
1959 else
1960 release_wlocks = 0;
1961
1962 return count;
1963}
1964static int debug_wake_lock_open(struct inode *inode, struct file *file)
1965{
1966 file->private_data = inode->i_private;
1967 return 0;
1968}
1969const struct file_operations debug_wlocks_ops = {
1970 .open = debug_wake_lock_open,
1971 .read = debug_read_release_wlocks,
1972 .write = debug_write_release_wlocks,
1973};
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05301974
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05301975static ssize_t debug_reprime_ep(struct file *file, const char __user *ubuf,
1976 size_t count, loff_t *ppos)
1977{
1978 struct usb_info *ui = file->private_data;
1979 struct msm_endpoint *ept;
1980 char kbuf[10];
1981 unsigned int ep_num, dir;
1982 unsigned long flags;
1983 unsigned n, i;
1984
1985 memset(kbuf, 0, 10);
1986
1987 if (copy_from_user(kbuf, ubuf, count > 10 ? 10 : count))
1988 return -EFAULT;
1989
1990 if (sscanf(kbuf, "%u %u", &ep_num, &dir) != 2)
1991 return -EINVAL;
1992
1993 if (dir)
1994 i = ep_num + 16;
1995 else
1996 i = ep_num;
1997
1998 spin_lock_irqsave(&ui->lock, flags);
1999 ept = ui->ept + i;
2000 n = 1 << ept->bit;
2001
2002 if ((readl_relaxed(USB_ENDPTPRIME) & n))
2003 goto out;
2004
2005 if (readl_relaxed(USB_ENDPTSTAT) & n)
2006 goto out;
2007
2008 /* clear speculative loads on item->info */
2009 rmb();
2010 if (ept->req && (ept->req->item->info & INFO_ACTIVE)) {
2011 pr_err("%s(): ept%d%s prime failed. ept: config: %x"
2012 "active: %x next: %x info: %x\n",
2013 __func__, ept->num,
2014 ept->flags & EPT_FLAG_IN ? "in" : "out",
2015 ept->head->config, ept->head->active,
2016 ept->head->next, ept->head->info);
2017 writel_relaxed(n, USB_ENDPTPRIME);
2018 }
2019out:
2020 spin_unlock_irqrestore(&ui->lock, flags);
2021
2022 return count;
2023}
2024
2025static char buffer[512];
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302026static ssize_t debug_prime_fail_read(struct file *file, char __user *ubuf,
2027 size_t count, loff_t *ppos)
2028{
2029 struct usb_info *ui = file->private_data;
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302030 char *buf = buffer;
2031 unsigned long flags;
2032 struct msm_endpoint *ept;
2033 int n;
2034 int i = 0;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302035
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302036 spin_lock_irqsave(&ui->lock, flags);
2037 for (n = 0; n < 32; n++) {
2038 ept = ui->ept + n;
2039 if (ept->ep.maxpacket == 0)
2040 continue;
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302041
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302042 i += scnprintf(buf + i, PAGE_SIZE - i,
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302043 "ept%d %s false_prime_count=%lu prime_fail_count=%d "
2044 "dtd_fail_count=%lu "
2045 "dTD_workaround_fail_count=%lu\n",
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302046 ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
2047 ept->false_prime_fail_count,
2048 ept->actual_prime_fail_count,
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302049 ept->dTD_update_fail_count,
2050 ept->dTD_workaround_fail_count);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302051 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302052
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302053 i += scnprintf(buf + i, PAGE_SIZE - i,
2054 "dTD_update_fail count: %lu\n",
2055 ui->dTD_update_fail_count);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302056
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302057 i += scnprintf(buf + i, PAGE_SIZE - i,
2058 "prime_fail count: %d\n", ui->prime_fail_count);
2059
Rajkumar Raghupathyf17772e2012-06-19 11:27:12 +05302060 i += scnprintf(buf + i, PAGE_SIZE - i,
2061 "dtd_workaround_fail count: %lu\n",
2062 ui->dTD_workaround_fail_count);
2063
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302064 spin_unlock_irqrestore(&ui->lock, flags);
2065
2066 return simple_read_from_buffer(ubuf, count, ppos, buf, i);
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302067}
2068
2069static int debug_prime_fail_open(struct inode *inode, struct file *file)
2070{
2071 file->private_data = inode->i_private;
2072 return 0;
2073}
2074
2075const struct file_operations prime_fail_ops = {
2076 .open = debug_prime_fail_open,
2077 .read = debug_prime_fail_read,
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302078 .write = debug_reprime_ep,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302079};
2080
Rajkumar Raghupathyc0e14a42012-09-17 16:35:47 +05302081static ssize_t debug_prop_chg_write(struct file *file,
2082 const char __user *buf, size_t count, loff_t *ppos)
2083{
2084 struct usb_info *ui = file->private_data;
2085 char kbuf[2];
2086
2087 memset(kbuf, 0, sizeof(kbuf));
2088
2089 if (copy_from_user(kbuf, buf, sizeof(kbuf)))
2090 return -EFAULT;
2091
2092 if (!strncmp(kbuf, "1", 1))
2093 ui->pdata->prop_chg = 1;
2094 else
2095 ui->pdata->prop_chg = 0;
2096
2097 return count;
2098}
2099
2100static ssize_t debug_prop_chg_read(struct file *file, char __user *ubuf,
2101 size_t count, loff_t *ppos)
2102{
2103 struct usb_info *ui = file->private_data;
2104 char kbuf[2];
2105 size_t c = 0;
2106
2107 memset(kbuf, 0, sizeof(kbuf));
2108
2109 c = scnprintf(kbuf, sizeof(kbuf), "%d\n", ui->pdata->prop_chg);
2110
2111 if (copy_to_user(ubuf, kbuf, c))
2112 return -EFAULT;
2113
2114 return simple_read_from_buffer(ubuf, count, ppos, kbuf, c);
2115}
2116
2117static int debug_prop_chg_open(struct inode *inode, struct file *file)
2118{
2119 file->private_data = inode->i_private;
2120 return 0;
2121}
2122
2123const struct file_operations debug_prop_chg_ops = {
2124 .open = debug_prop_chg_open,
2125 .read = debug_prop_chg_read,
2126 .write = debug_prop_chg_write,
2127};
2128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129static void usb_debugfs_init(struct usb_info *ui)
2130{
2131 struct dentry *dent;
2132 dent = debugfs_create_dir(dev_name(&ui->pdev->dev), 0);
2133 if (IS_ERR(dent))
2134 return;
2135
2136 debugfs_create_file("status", 0444, dent, ui, &debug_stat_ops);
2137 debugfs_create_file("reset", 0222, dent, ui, &debug_reset_ops);
2138 debugfs_create_file("cycle", 0222, dent, ui, &debug_cycle_ops);
2139 debugfs_create_file("release_wlocks", 0666, dent, ui,
2140 &debug_wlocks_ops);
Vijayavardhan Vennapusa23ee2892011-12-12 15:53:07 +05302141 debugfs_create_file("prime_fail_countt", 0666, dent, ui,
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302142 &prime_fail_ops);
Rajkumar Raghupathyc0e14a42012-09-17 16:35:47 +05302143 debugfs_create_file("proprietary_chg", 0666, dent, ui,
2144 &debug_prop_chg_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002145}
2146#else
2147static void usb_debugfs_init(struct usb_info *ui) {}
2148#endif
2149
2150static int
2151msm72k_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
2152{
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302153 struct msm_endpoint *ept;
2154 unsigned char ep_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155
Rajkumar Raghupathye3a95f72012-05-29 20:05:47 +05302156 if (_ep == NULL || desc == NULL)
2157 return -EINVAL;
2158
2159 ept = to_msm_endpoint(_ep);
2160 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002161 _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2162 config_ept(ept);
2163 ept->wedged = 0;
2164 usb_ept_enable(ept, 1, ep_type);
2165 return 0;
2166}
2167
2168static int msm72k_disable(struct usb_ep *_ep)
2169{
2170 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2171
2172 usb_ept_enable(ept, 0, 0);
2173 flush_endpoint(ept);
Pavankumar Kondeti2c968782012-04-12 15:16:39 +05302174 /*
2175 * Clear descriptors here. Otherwise previous descriptors
2176 * will be used by function drivers upon next enumeration.
2177 */
2178 _ep->desc = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002179 return 0;
2180}
2181
2182static struct usb_request *
2183msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
2184{
2185 return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
2186}
2187
2188static void
2189msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
2190{
2191 struct msm_request *req = to_msm_request(_req);
2192 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2193 struct usb_info *ui = ept->ui;
2194
2195 /* request should not be busy */
2196 BUG_ON(req->busy);
2197 if (req->alloced)
2198 kfree(req->req.buf);
2199 dma_pool_free(ui->pool, req->item, req->item_dma);
2200 kfree(req);
2201}
2202
2203static int
2204msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
2205{
2206 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2207 struct usb_info *ui = ep->ui;
2208
2209 if (ep == &ui->ep0in) {
2210 struct msm_request *r = to_msm_request(req);
2211 if (!req->length)
2212 goto ep_queue_done;
2213 r->gadget_complete = req->complete;
2214 /* ep0_queue_ack_complete queue a receive for ACK before
2215 ** calling req->complete
2216 */
2217 req->complete = ep0_queue_ack_complete;
2218 if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
2219 ep = &ui->ep0out;
2220 goto ep_queue_done;
2221 }
2222
2223ep_queue_done:
2224 return usb_ept_queue_xfer(ep, req);
2225}
2226
2227static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2228{
2229 struct msm_endpoint *ep = to_msm_endpoint(_ep);
2230 struct msm_request *req = to_msm_request(_req);
2231 struct usb_info *ui = ep->ui;
2232
2233 struct msm_request *temp_req;
2234 unsigned long flags;
2235
2236 if (!(ui && req && ep->req))
2237 return -EINVAL;
2238
2239 spin_lock_irqsave(&ui->lock, flags);
2240 if (!req->busy) {
2241 dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
2242 spin_unlock_irqrestore(&ui->lock, flags);
2243 return -EINVAL;
2244 }
Pavankumar Kondeti9cb21752011-08-08 16:14:53 +05302245 del_timer(&ep->prime_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002246 /* Stop the transfer */
2247 do {
2248 writel((1 << ep->bit), USB_ENDPTFLUSH);
2249 while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
2250 udelay(100);
2251 } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
2252
2253 req->req.status = 0;
2254 req->busy = 0;
2255
2256 if (ep->req == req) {
2257 ep->req = req->next;
2258 ep->head->next = req->item->next;
2259 } else {
2260 req->prev->next = req->next;
2261 if (req->next)
2262 req->next->prev = req->prev;
2263 req->prev->item->next = req->item->next;
2264 }
2265
2266 if (!req->next)
2267 ep->last = req->prev;
2268
2269 /* initialize request to default */
2270 req->item->next = TERMINATE;
2271 req->item->info = 0;
2272 req->live = 0;
2273 dma_unmap_single(NULL, req->dma, req->req.length,
2274 (ep->flags & EPT_FLAG_IN) ?
2275 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2276
2277 if (req->req.complete) {
2278 req->req.status = -ECONNRESET;
2279 spin_unlock_irqrestore(&ui->lock, flags);
2280 req->req.complete(&ep->ep, &req->req);
2281 spin_lock_irqsave(&ui->lock, flags);
2282 }
2283
2284 if (!req->live) {
2285 /* Reprime the endpoint for the remaining transfers */
2286 for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
2287 temp_req->live = 0;
2288 if (ep->req)
2289 usb_ept_start(ep);
2290 spin_unlock_irqrestore(&ui->lock, flags);
2291 return 0;
2292 }
2293 spin_unlock_irqrestore(&ui->lock, flags);
2294 return 0;
2295}
2296
2297static int
2298usb_ept_set_halt(struct usb_ep *_ep, int value)
2299{
2300 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2301 struct usb_info *ui = ept->ui;
2302 unsigned int in = ept->flags & EPT_FLAG_IN;
2303 unsigned int n;
2304 unsigned long flags;
2305
2306 spin_lock_irqsave(&ui->lock, flags);
2307
2308 n = readl(USB_ENDPTCTRL(ept->num));
2309
2310 if (in) {
2311 if (value)
2312 n |= CTRL_TXS;
2313 else {
2314 n &= ~CTRL_TXS;
2315 n |= CTRL_TXR;
2316 }
2317 } else {
2318 if (value)
2319 n |= CTRL_RXS;
2320 else {
2321 n &= ~CTRL_RXS;
2322 n |= CTRL_RXR;
2323 }
2324 }
2325 writel(n, USB_ENDPTCTRL(ept->num));
2326 if (!value)
2327 ept->wedged = 0;
2328 spin_unlock_irqrestore(&ui->lock, flags);
2329
2330 return 0;
2331}
2332
2333static int
2334msm72k_set_halt(struct usb_ep *_ep, int value)
2335{
2336 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2337 unsigned int in = ept->flags & EPT_FLAG_IN;
2338
2339 if (value && in && ept->req)
2340 return -EAGAIN;
2341
2342 usb_ept_set_halt(_ep, value);
2343
2344 return 0;
2345}
2346
2347static int
2348msm72k_fifo_status(struct usb_ep *_ep)
2349{
2350 return -EOPNOTSUPP;
2351}
2352
2353static void
2354msm72k_fifo_flush(struct usb_ep *_ep)
2355{
2356 flush_endpoint(to_msm_endpoint(_ep));
2357}
2358static int msm72k_set_wedge(struct usb_ep *_ep)
2359{
2360 struct msm_endpoint *ept = to_msm_endpoint(_ep);
2361
2362 if (ept->num == 0)
2363 return -EINVAL;
2364
2365 ept->wedged = 1;
2366
2367 return msm72k_set_halt(_ep, 1);
2368}
2369
2370static const struct usb_ep_ops msm72k_ep_ops = {
2371 .enable = msm72k_enable,
2372 .disable = msm72k_disable,
2373
2374 .alloc_request = msm72k_alloc_request,
2375 .free_request = msm72k_free_request,
2376
2377 .queue = msm72k_queue,
2378 .dequeue = msm72k_dequeue,
2379
2380 .set_halt = msm72k_set_halt,
2381 .set_wedge = msm72k_set_wedge,
2382 .fifo_status = msm72k_fifo_status,
2383 .fifo_flush = msm72k_fifo_flush,
2384};
2385
2386static int msm72k_get_frame(struct usb_gadget *_gadget)
2387{
2388 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2389
2390 /* frame number is in bits 13:3 */
2391 return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
2392}
2393
2394/* VBUS reporting logically comes from a transceiver */
2395static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
2396{
2397 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2398 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2399
2400 if (is_active || atomic_read(&otg->chg_type)
2401 == USB_CHG_TYPE__WALLCHARGER)
2402 wake_lock(&ui->wlock);
2403
2404 msm_hsusb_set_vbus_state(is_active);
2405 return 0;
2406}
2407
2408/* SW workarounds
2409Issue #1 - USB Spoof Disconnect Failure
2410Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
2411SW workaround - Making opmode non-driving and SuspendM set in function
2412 register of SMSC phy
2413*/
2414/* drivers may have software control over D+ pullup */
2415static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
2416{
2417 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2418 unsigned long flags;
2419
2420 if (is_active) {
2421 spin_lock_irqsave(&ui->lock, flags);
2422 if (is_usb_online(ui) && ui->driver)
2423 writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
2424 spin_unlock_irqrestore(&ui->lock, flags);
2425 } else {
2426 writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
2427 /* S/W workaround, Issue#1 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002428 usb_phy_io_write(ui->xceiv, 0x48, 0x04);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002429 }
2430
2431 /* Ensure pull-up operation is completed before returning */
2432 mb();
2433
2434 return 0;
2435}
2436
2437static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
2438{
2439 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302440 struct msm_otg *otg = to_msm_otg(ui->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002441 unsigned long flags;
2442
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002443 atomic_set(&ui->softconnect, is_active);
2444
2445 spin_lock_irqsave(&ui->lock, flags);
Chiranjeevi Velempatif4fe1192012-05-14 09:39:08 +05302446 if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL ||
2447 atomic_read(&otg->chg_type) == USB_CHG_TYPE__WALLCHARGER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002448 spin_unlock_irqrestore(&ui->lock, flags);
2449 return 0;
2450 }
2451 spin_unlock_irqrestore(&ui->lock, flags);
2452
2453 msm72k_pullup_internal(_gadget, is_active);
2454
2455 if (is_active && !ui->gadget.is_a_peripheral)
2456 schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
2457
2458 return 0;
2459}
2460
2461static int msm72k_wakeup(struct usb_gadget *_gadget)
2462{
2463 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2464 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2465
2466 if (!atomic_read(&ui->remote_wakeup)) {
2467 dev_err(&ui->pdev->dev,
2468 "%s: remote wakeup not supported\n", __func__);
2469 return -ENOTSUPP;
2470 }
2471
2472 if (!atomic_read(&ui->configured)) {
2473 dev_err(&ui->pdev->dev,
2474 "%s: device is not configured\n", __func__);
2475 return -ENODEV;
2476 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002477 usb_phy_set_suspend(ui->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002478
2479 disable_irq(otg->irq);
2480
2481 if (!is_usb_active())
2482 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
2483
2484 /* Ensure that USB port is resumed before enabling the IRQ */
2485 mb();
2486
2487 enable_irq(otg->irq);
2488
2489 return 0;
2490}
2491
2492/* when Gadget is configured, it will indicate how much power
2493 * can be pulled from vbus, as specified in configuiration descriptor
2494 */
2495static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2496{
2497 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2498 unsigned long flags;
2499
2500
2501 spin_lock_irqsave(&ui->lock, flags);
2502 ui->b_max_pow = mA;
2503 ui->flags = USB_FLAG_CONFIGURED;
2504 spin_unlock_irqrestore(&ui->lock, flags);
2505
2506 schedule_work(&ui->work);
2507
2508 return 0;
2509}
2510
2511static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
2512{
2513 struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
2514 unsigned long flags;
2515 int ret = 0;
2516
2517 spin_lock_irqsave(&ui->lock, flags);
2518 if (set) {
2519 if (ui->pdata && ui->pdata->self_powered)
2520 atomic_set(&ui->self_powered, 1);
2521 else
2522 ret = -EOPNOTSUPP;
2523 } else {
2524 /* We can always work as a bus powered device */
2525 atomic_set(&ui->self_powered, 0);
2526 }
2527 spin_unlock_irqrestore(&ui->lock, flags);
2528
2529 return ret;
2530
2531}
2532
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002533static int msm72k_gadget_start(struct usb_gadget_driver *driver,
2534 int (*bind)(struct usb_gadget *));
2535static int msm72k_gadget_stop(struct usb_gadget_driver *driver);
2536
2537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002538static const struct usb_gadget_ops msm72k_ops = {
2539 .get_frame = msm72k_get_frame,
2540 .vbus_session = msm72k_udc_vbus_session,
2541 .vbus_draw = msm72k_udc_vbus_draw,
2542 .pullup = msm72k_pullup,
2543 .wakeup = msm72k_wakeup,
2544 .set_selfpowered = msm72k_set_selfpowered,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002545 .start = msm72k_gadget_start,
2546 .stop = msm72k_gadget_stop
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002547};
2548
2549static void usb_do_remote_wakeup(struct work_struct *w)
2550{
2551 struct usb_info *ui = the_usb_info;
2552
2553 msm72k_wakeup(&ui->gadget);
2554}
2555
2556static ssize_t usb_remote_wakeup(struct device *dev,
2557 struct device_attribute *attr, const char *buf, size_t count)
2558{
2559 struct usb_info *ui = the_usb_info;
2560
2561 msm72k_wakeup(&ui->gadget);
2562
2563 return count;
2564}
2565
2566static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
2567 char *buf)
2568{
2569 size_t i;
2570 char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
2571 "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
2572 "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
2573 "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
2574 "USB_STATE_SUSPENDED"
2575 };
2576
2577 i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
2578 return i;
2579}
2580
2581static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
2582 char *buf)
2583{
2584 struct usb_info *ui = the_usb_info;
2585 size_t i;
2586 char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
2587 "USB_SPEED_FULL", "USB_SPEED_HIGH"};
2588
2589 i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
2590 return i;
2591}
2592
2593static ssize_t store_usb_chg_current(struct device *dev,
2594 struct device_attribute *attr, const char *buf, size_t count)
2595{
2596 struct usb_info *ui = the_usb_info;
2597 unsigned long mA;
2598
2599 if (ui->gadget.is_a_peripheral)
2600 return -EINVAL;
2601
2602 if (strict_strtoul(buf, 10, &mA))
2603 return -EINVAL;
2604
2605 ui->chg_current = mA;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002606 usb_phy_set_power(ui->xceiv, mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002607
2608 return count;
2609}
2610
2611static ssize_t show_usb_chg_current(struct device *dev,
2612 struct device_attribute *attr, char *buf)
2613{
2614 struct usb_info *ui = the_usb_info;
2615 size_t count;
2616
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302617 count = snprintf(buf, PAGE_SIZE, "%d", ui->chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002618
2619 return count;
2620}
2621
2622static ssize_t show_usb_chg_type(struct device *dev,
2623 struct device_attribute *attr, char *buf)
2624{
2625 struct usb_info *ui = the_usb_info;
2626 struct msm_otg *otg = to_msm_otg(ui->xceiv);
2627 size_t count;
2628 char *chg_type[] = {"STD DOWNSTREAM PORT",
2629 "CARKIT",
2630 "DEDICATED CHARGER",
2631 "INVALID"};
2632
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302633 count = snprintf(buf, PAGE_SIZE, "%s",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002634 chg_type[atomic_read(&otg->chg_type)]);
2635
2636 return count;
2637}
2638static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
2639static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
2640static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
2641static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
2642static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
2643 show_usb_chg_current, store_usb_chg_current);
2644
2645#ifdef CONFIG_USB_OTG
2646static ssize_t store_host_req(struct device *dev,
2647 struct device_attribute *attr, const char *buf, size_t count)
2648{
2649 struct usb_info *ui = the_usb_info;
2650 unsigned long val, flags;
2651
2652 if (strict_strtoul(buf, 10, &val))
2653 return -EINVAL;
2654
2655 dev_dbg(&ui->pdev->dev, "%s host request\n",
2656 val ? "set" : "clear");
2657
2658 spin_lock_irqsave(&ui->lock, flags);
2659 if (ui->hnp_avail)
2660 ui->gadget.host_request = !!val;
2661 spin_unlock_irqrestore(&ui->lock, flags);
2662
2663 return count;
2664}
2665static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
2666
2667/* How do we notify user space about HNP availability?
2668 * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
2669 * Introduce hnp_avail flag and set when HNP polling request arrives.
2670 * The expectation is that user space checks hnp availability before
2671 * requesting host role via above sysfs node.
2672 */
2673static ssize_t show_host_avail(struct device *dev,
2674 struct device_attribute *attr, char *buf)
2675{
2676 struct usb_info *ui = the_usb_info;
2677 size_t count;
2678 unsigned long flags;
2679
2680 spin_lock_irqsave(&ui->lock, flags);
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302681 count = snprintf(buf, PAGE_SIZE, "%d\n", ui->hnp_avail);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002682 spin_unlock_irqrestore(&ui->lock, flags);
2683
2684 return count;
2685}
2686static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
2687
2688static struct attribute *otg_attrs[] = {
2689 &dev_attr_host_request.attr,
2690 &dev_attr_host_avail.attr,
2691 NULL,
2692};
2693
2694static struct attribute_group otg_attr_grp = {
2695 .name = "otg",
2696 .attrs = otg_attrs,
2697};
2698#endif
2699
2700static int msm72k_probe(struct platform_device *pdev)
2701{
2702 struct usb_info *ui;
2703 struct msm_otg *otg;
2704 int retval;
2705
2706 dev_dbg(&pdev->dev, "msm72k_probe\n");
2707 ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
2708 if (!ui)
2709 return -ENOMEM;
2710
2711 ui->pdev = pdev;
2712 ui->pdata = pdev->dev.platform_data;
2713
2714 ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
2715 if (!ui->buf)
2716 return usb_free(ui, -ENOMEM);
2717
2718 ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
2719 if (!ui->pool)
2720 return usb_free(ui, -ENOMEM);
2721
Steve Mucklef132c6c2012-06-06 18:30:57 -07002722 ui->xceiv = usb_get_transceiver();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002723 if (!ui->xceiv)
2724 return usb_free(ui, -ENODEV);
2725
2726 otg = to_msm_otg(ui->xceiv);
2727 ui->addr = otg->regs;
2728
2729 ui->gadget.ops = &msm72k_ops;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002730 ui->gadget.max_speed = USB_SPEED_HIGH;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002731 device_initialize(&ui->gadget.dev);
2732 dev_set_name(&ui->gadget.dev, "gadget");
2733 ui->gadget.dev.parent = &pdev->dev;
2734 ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
2735
2736#ifdef CONFIG_USB_OTG
2737 ui->gadget.is_otg = 1;
2738#endif
2739
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002740 retval = usb_add_gadget_udc(&pdev->dev, &ui->gadget);
2741 if (retval)
2742 return usb_free(ui, retval);
2743
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002744 ui->sdev.name = DRIVER_NAME;
2745 ui->sdev.print_name = print_switch_name;
2746 ui->sdev.print_state = print_switch_state;
2747
2748 retval = switch_dev_register(&ui->sdev);
2749 if (retval)
2750 return usb_free(ui, retval);
2751
2752 the_usb_info = ui;
2753
2754 wake_lock_init(&ui->wlock,
2755 WAKE_LOCK_SUSPEND, "usb_bus_active");
2756
2757 usb_debugfs_init(ui);
2758
2759 usb_prepare(ui);
2760
2761#ifdef CONFIG_USB_OTG
2762 retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
2763 if (retval) {
2764 dev_err(&ui->pdev->dev,
2765 "failed to create otg sysfs directory:"
2766 "err:(%d)\n", retval);
2767 }
2768#endif
2769
Steve Mucklef132c6c2012-06-06 18:30:57 -07002770 retval = otg_set_peripheral(ui->xceiv->otg, &ui->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002771 if (retval) {
2772 dev_err(&ui->pdev->dev,
2773 "%s: Cannot bind the transceiver, retval:(%d)\n",
2774 __func__, retval);
2775 switch_dev_unregister(&ui->sdev);
2776 wake_lock_destroy(&ui->wlock);
2777 return usb_free(ui, retval);
2778 }
2779
2780 pm_runtime_enable(&pdev->dev);
2781
2782 /* Setup phy stuck timer */
2783 if (ui->pdata && ui->pdata->is_phy_status_timer_on)
2784 setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
2785 return 0;
2786}
2787
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002788static int msm72k_gadget_start(struct usb_gadget_driver *driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002789 int (*bind)(struct usb_gadget *))
2790{
2791 struct usb_info *ui = the_usb_info;
2792 int retval, n;
2793
2794 if (!driver
Steve Mucklef132c6c2012-06-06 18:30:57 -07002795 || driver->max_speed < USB_SPEED_FULL
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002796 || !bind
2797 || !driver->disconnect
2798 || !driver->setup)
2799 return -EINVAL;
2800 if (!ui)
2801 return -ENODEV;
2802 if (ui->driver)
2803 return -EBUSY;
2804
2805 /* first hook up the driver ... */
2806 ui->driver = driver;
2807 ui->gadget.dev.driver = &driver->driver;
2808 ui->gadget.name = driver_name;
2809 INIT_LIST_HEAD(&ui->gadget.ep_list);
2810 ui->gadget.ep0 = &ui->ep0in.ep;
2811 INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
2812 ui->gadget.speed = USB_SPEED_UNKNOWN;
2813 atomic_set(&ui->softconnect, 1);
2814
2815 for (n = 1; n < 16; n++) {
2816 struct msm_endpoint *ept = ui->ept + n;
2817 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2818 ept->ep.maxpacket = 512;
2819 }
2820 for (n = 17; n < 32; n++) {
2821 struct msm_endpoint *ept = ui->ept + n;
2822 list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
2823 ept->ep.maxpacket = 512;
2824 }
2825
2826 retval = device_add(&ui->gadget.dev);
2827 if (retval)
2828 goto fail;
2829
2830 retval = bind(&ui->gadget);
2831 if (retval) {
2832 dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
2833 driver->driver.name, retval);
2834 device_del(&ui->gadget.dev);
2835 goto fail;
2836 }
2837
2838 retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
2839 if (retval != 0)
2840 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2841 "(wakeup) error: (%d)\n", retval);
2842 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
2843 if (retval != 0)
2844 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2845 " (usb_state) error: (%d)\n", retval);
2846
2847 retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
2848 if (retval != 0)
2849 dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
2850 " (usb_speed) error: (%d)\n", retval);
2851
2852 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
2853 if (retval != 0)
2854 dev_err(&ui->pdev->dev,
2855 "failed to create sysfs entry(chg_type): err:(%d)\n",
2856 retval);
2857 retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
2858 if (retval != 0)
2859 dev_err(&ui->pdev->dev,
2860 "failed to create sysfs entry(chg_current):"
2861 "err:(%d)\n", retval);
2862
2863 dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
2864 driver->driver.name);
2865 usb_start(ui);
2866
2867 return 0;
2868
2869fail:
2870 ui->driver = NULL;
2871 ui->gadget.dev.driver = NULL;
2872 return retval;
2873}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002874
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002875static int msm72k_gadget_stop(struct usb_gadget_driver *driver)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002876{
2877 struct usb_info *dev = the_usb_info;
2878
2879 if (!dev)
2880 return -ENODEV;
2881 if (!driver || driver != dev->driver || !driver->unbind)
2882 return -EINVAL;
2883
2884 msm72k_pullup_internal(&dev->gadget, 0);
2885 if (dev->irq) {
2886 free_irq(dev->irq, dev);
2887 dev->irq = 0;
2888 }
2889 dev->state = USB_STATE_IDLE;
2890 atomic_set(&dev->configured, 0);
2891 switch_set_state(&dev->sdev, 0);
2892 /* cancel pending ep0 transactions */
2893 flush_endpoint(&dev->ep0out);
2894 flush_endpoint(&dev->ep0in);
2895
2896 device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
2897 device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
2898 device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
2899 device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
2900 device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
2901 driver->disconnect(&dev->gadget);
2902 driver->unbind(&dev->gadget);
2903 dev->gadget.dev.driver = NULL;
2904 dev->driver = NULL;
2905
2906 device_del(&dev->gadget.dev);
2907
2908 dev_dbg(&dev->pdev->dev,
2909 "unregistered gadget driver '%s'\n", driver->driver.name);
2910 return 0;
2911}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002912
2913
2914static int msm72k_udc_runtime_suspend(struct device *dev)
2915{
2916 dev_dbg(dev, "pm_runtime: suspending...\n");
2917 return 0;
2918}
2919
2920static int msm72k_udc_runtime_resume(struct device *dev)
2921{
2922 dev_dbg(dev, "pm_runtime: resuming...\n");
2923 return 0;
2924}
2925
2926static int msm72k_udc_runtime_idle(struct device *dev)
2927{
2928 dev_dbg(dev, "pm_runtime: idling...\n");
2929 return 0;
2930}
2931
2932static struct dev_pm_ops msm72k_udc_dev_pm_ops = {
2933 .runtime_suspend = msm72k_udc_runtime_suspend,
2934 .runtime_resume = msm72k_udc_runtime_resume,
2935 .runtime_idle = msm72k_udc_runtime_idle
2936};
2937
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002938static int __exit msm72k_remove(struct platform_device *pdev)
2939{
2940 struct usb_info *ui = container_of(&pdev, struct usb_info, pdev);
2941
2942 return usb_free(ui, 0);
2943}
2944
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002945static struct platform_driver usb_driver = {
2946 .probe = msm72k_probe,
Sebastian Andrzej Siewiord23607a2011-06-28 16:33:47 +03002947 .remove = msm72k_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002948 .driver = { .name = "msm_hsusb",
2949 .pm = &msm72k_udc_dev_pm_ops, },
2950};
2951
2952static int __init init(void)
2953{
2954 return platform_driver_register(&usb_driver);
2955}
2956module_init(init);
2957
2958static void __exit cleanup(void)
2959{
2960 platform_driver_unregister(&usb_driver);
2961}
2962module_exit(cleanup);
2963
2964MODULE_DESCRIPTION(DRIVER_DESC);
2965MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
2966MODULE_LICENSE("GPL");