usb: chipidea: big-endian support
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / chipidea / udc.c
1 /*
2 * udc.c - ChipIdea UDC driver
3 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/dmapool.h>
16 #include <linux/err.h>
17 #include <linux/irqreturn.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/otg.h>
24 #include <linux/usb/chipidea.h>
25
26 #include "ci.h"
27 #include "udc.h"
28 #include "bits.h"
29 #include "debug.h"
30
31 /* control endpoint description */
32 static const struct usb_endpoint_descriptor
33 ctrl_endpt_out_desc = {
34 .bLength = USB_DT_ENDPOINT_SIZE,
35 .bDescriptorType = USB_DT_ENDPOINT,
36
37 .bEndpointAddress = USB_DIR_OUT,
38 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
39 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
40 };
41
42 static const struct usb_endpoint_descriptor
43 ctrl_endpt_in_desc = {
44 .bLength = USB_DT_ENDPOINT_SIZE,
45 .bDescriptorType = USB_DT_ENDPOINT,
46
47 .bEndpointAddress = USB_DIR_IN,
48 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
49 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
50 };
51
52 /**
53 * hw_ep_bit: calculates the bit number
54 * @num: endpoint number
55 * @dir: endpoint direction
56 *
57 * This function returns bit number
58 */
59 static inline int hw_ep_bit(int num, int dir)
60 {
61 return num + (dir ? 16 : 0);
62 }
63
64 static inline int ep_to_bit(struct ci13xxx *ci, int n)
65 {
66 int fill = 16 - ci->hw_ep_max / 2;
67
68 if (n >= ci->hw_ep_max / 2)
69 n += fill;
70
71 return n;
72 }
73
74 /**
75 * hw_device_state: enables/disables interrupts (execute without interruption)
76 * @dma: 0 => disable, !0 => enable and set dma engine
77 *
78 * This function returns an error code
79 */
80 static int hw_device_state(struct ci13xxx *ci, u32 dma)
81 {
82 if (dma) {
83 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
84 /* interrupt, error, port change, reset, sleep/suspend */
85 hw_write(ci, OP_USBINTR, ~0,
86 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
87 } else {
88 hw_write(ci, OP_USBINTR, ~0, 0);
89 }
90 return 0;
91 }
92
93 /**
94 * hw_ep_flush: flush endpoint fifo (execute without interruption)
95 * @num: endpoint number
96 * @dir: endpoint direction
97 *
98 * This function returns an error code
99 */
100 static int hw_ep_flush(struct ci13xxx *ci, int num, int dir)
101 {
102 int n = hw_ep_bit(num, dir);
103
104 do {
105 /* flush any pending transfer */
106 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
107 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
108 cpu_relax();
109 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
110
111 return 0;
112 }
113
114 /**
115 * hw_ep_disable: disables endpoint (execute without interruption)
116 * @num: endpoint number
117 * @dir: endpoint direction
118 *
119 * This function returns an error code
120 */
121 static int hw_ep_disable(struct ci13xxx *ci, int num, int dir)
122 {
123 hw_ep_flush(ci, num, dir);
124 hw_write(ci, OP_ENDPTCTRL + num,
125 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
126 return 0;
127 }
128
129 /**
130 * hw_ep_enable: enables endpoint (execute without interruption)
131 * @num: endpoint number
132 * @dir: endpoint direction
133 * @type: endpoint type
134 *
135 * This function returns an error code
136 */
137 static int hw_ep_enable(struct ci13xxx *ci, int num, int dir, int type)
138 {
139 u32 mask, data;
140
141 if (dir) {
142 mask = ENDPTCTRL_TXT; /* type */
143 data = type << __ffs(mask);
144
145 mask |= ENDPTCTRL_TXS; /* unstall */
146 mask |= ENDPTCTRL_TXR; /* reset data toggle */
147 data |= ENDPTCTRL_TXR;
148 mask |= ENDPTCTRL_TXE; /* enable */
149 data |= ENDPTCTRL_TXE;
150 } else {
151 mask = ENDPTCTRL_RXT; /* type */
152 data = type << __ffs(mask);
153
154 mask |= ENDPTCTRL_RXS; /* unstall */
155 mask |= ENDPTCTRL_RXR; /* reset data toggle */
156 data |= ENDPTCTRL_RXR;
157 mask |= ENDPTCTRL_RXE; /* enable */
158 data |= ENDPTCTRL_RXE;
159 }
160 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
161 return 0;
162 }
163
164 /**
165 * hw_ep_get_halt: return endpoint halt status
166 * @num: endpoint number
167 * @dir: endpoint direction
168 *
169 * This function returns 1 if endpoint halted
170 */
171 static int hw_ep_get_halt(struct ci13xxx *ci, int num, int dir)
172 {
173 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
174
175 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
176 }
177
178 /**
179 * hw_test_and_clear_setup_status: test & clear setup status (execute without
180 * interruption)
181 * @n: endpoint number
182 *
183 * This function returns setup status
184 */
185 static int hw_test_and_clear_setup_status(struct ci13xxx *ci, int n)
186 {
187 n = ep_to_bit(ci, n);
188 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
189 }
190
191 /**
192 * hw_ep_prime: primes endpoint (execute without interruption)
193 * @num: endpoint number
194 * @dir: endpoint direction
195 * @is_ctrl: true if control endpoint
196 *
197 * This function returns an error code
198 */
199 static int hw_ep_prime(struct ci13xxx *ci, int num, int dir, int is_ctrl)
200 {
201 int n = hw_ep_bit(num, dir);
202
203 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
204 return -EAGAIN;
205
206 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
207
208 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
209 cpu_relax();
210 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
211 return -EAGAIN;
212
213 /* status shoult be tested according with manual but it doesn't work */
214 return 0;
215 }
216
217 /**
218 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
219 * without interruption)
220 * @num: endpoint number
221 * @dir: endpoint direction
222 * @value: true => stall, false => unstall
223 *
224 * This function returns an error code
225 */
226 static int hw_ep_set_halt(struct ci13xxx *ci, int num, int dir, int value)
227 {
228 if (value != 0 && value != 1)
229 return -EINVAL;
230
231 do {
232 enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
233 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
234 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
235
236 /* data toggle - reserved for EP0 but it's in ESS */
237 hw_write(ci, reg, mask_xs|mask_xr,
238 value ? mask_xs : mask_xr);
239 } while (value != hw_ep_get_halt(ci, num, dir));
240
241 return 0;
242 }
243
244 /**
245 * hw_is_port_high_speed: test if port is high speed
246 *
247 * This function returns true if high speed port
248 */
249 static int hw_port_is_high_speed(struct ci13xxx *ci)
250 {
251 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
252 hw_read(ci, OP_PORTSC, PORTSC_HSP);
253 }
254
255 /**
256 * hw_read_intr_enable: returns interrupt enable register
257 *
258 * This function returns register data
259 */
260 static u32 hw_read_intr_enable(struct ci13xxx *ci)
261 {
262 return hw_read(ci, OP_USBINTR, ~0);
263 }
264
265 /**
266 * hw_read_intr_status: returns interrupt status register
267 *
268 * This function returns register data
269 */
270 static u32 hw_read_intr_status(struct ci13xxx *ci)
271 {
272 return hw_read(ci, OP_USBSTS, ~0);
273 }
274
275 /**
276 * hw_test_and_clear_complete: test & clear complete status (execute without
277 * interruption)
278 * @n: endpoint number
279 *
280 * This function returns complete status
281 */
282 static int hw_test_and_clear_complete(struct ci13xxx *ci, int n)
283 {
284 n = ep_to_bit(ci, n);
285 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
286 }
287
288 /**
289 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
290 * without interruption)
291 *
292 * This function returns active interrutps
293 */
294 static u32 hw_test_and_clear_intr_active(struct ci13xxx *ci)
295 {
296 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
297
298 hw_write(ci, OP_USBSTS, ~0, reg);
299 return reg;
300 }
301
302 /**
303 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
304 * interruption)
305 *
306 * This function returns guard value
307 */
308 static int hw_test_and_clear_setup_guard(struct ci13xxx *ci)
309 {
310 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
311 }
312
313 /**
314 * hw_test_and_set_setup_guard: test & set setup guard (execute without
315 * interruption)
316 *
317 * This function returns guard value
318 */
319 static int hw_test_and_set_setup_guard(struct ci13xxx *ci)
320 {
321 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
322 }
323
324 /**
325 * hw_usb_set_address: configures USB address (execute without interruption)
326 * @value: new USB address
327 *
328 * This function explicitly sets the address, without the "USBADRA" (advance)
329 * feature, which is not supported by older versions of the controller.
330 */
331 static void hw_usb_set_address(struct ci13xxx *ci, u8 value)
332 {
333 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
334 value << __ffs(DEVICEADDR_USBADR));
335 }
336
337 /**
338 * hw_usb_reset: restart device after a bus reset (execute without
339 * interruption)
340 *
341 * This function returns an error code
342 */
343 static int hw_usb_reset(struct ci13xxx *ci)
344 {
345 hw_usb_set_address(ci, 0);
346
347 /* ESS flushes only at end?!? */
348 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
349
350 /* clear setup token semaphores */
351 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
352
353 /* clear complete status */
354 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
355
356 /* wait until all bits cleared */
357 while (hw_read(ci, OP_ENDPTPRIME, ~0))
358 udelay(10); /* not RTOS friendly */
359
360 /* reset all endpoints ? */
361
362 /* reset internal status and wait for further instructions
363 no need to verify the port reset status (ESS does it) */
364
365 return 0;
366 }
367
368 /******************************************************************************
369 * UTIL block
370 *****************************************************************************/
371 /**
372 * _usb_addr: calculates endpoint address from direction & number
373 * @ep: endpoint
374 */
375 static inline u8 _usb_addr(struct ci13xxx_ep *ep)
376 {
377 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
378 }
379
380 /**
381 * _hardware_queue: configures a request at hardware level
382 * @gadget: gadget
383 * @mEp: endpoint
384 *
385 * This function returns an error code
386 */
387 static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
388 {
389 struct ci13xxx *ci = mEp->ci;
390 unsigned i;
391 int ret = 0;
392 unsigned length = mReq->req.length;
393
394 /* don't queue twice */
395 if (mReq->req.status == -EALREADY)
396 return -EALREADY;
397
398 mReq->req.status = -EALREADY;
399
400 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
401 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
402 &mReq->zdma);
403 if (mReq->zptr == NULL)
404 return -ENOMEM;
405
406 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
407 mReq->zptr->next = cpu_to_le32(TD_TERMINATE);
408 mReq->zptr->token = cpu_to_le32(TD_STATUS_ACTIVE);
409 if (!mReq->req.no_interrupt)
410 mReq->zptr->token |= cpu_to_le32(TD_IOC);
411 }
412 ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
413 if (ret)
414 return ret;
415
416 /*
417 * TD configuration
418 * TODO - handle requests which spawns into several TDs
419 */
420 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
421 mReq->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
422 mReq->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
423 mReq->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
424 if (mReq->zptr) {
425 mReq->ptr->next = cpu_to_le32(mReq->zdma);
426 } else {
427 mReq->ptr->next = cpu_to_le32(TD_TERMINATE);
428 if (!mReq->req.no_interrupt)
429 mReq->ptr->token |= cpu_to_le32(TD_IOC);
430 }
431 mReq->ptr->page[0] = cpu_to_le32(mReq->req.dma);
432 for (i = 1; i < 5; i++) {
433 u32 page = mReq->req.dma + i * CI13XXX_PAGE_SIZE;
434 page &= ~TD_RESERVED_MASK;
435 mReq->ptr->page[i] = cpu_to_le32(page);
436 }
437
438 if (!list_empty(&mEp->qh.queue)) {
439 struct ci13xxx_req *mReqPrev;
440 int n = hw_ep_bit(mEp->num, mEp->dir);
441 int tmp_stat;
442 u32 next = mReq->dma & TD_ADDR_MASK;
443
444 mReqPrev = list_entry(mEp->qh.queue.prev,
445 struct ci13xxx_req, queue);
446 if (mReqPrev->zptr)
447 mReqPrev->zptr->next = cpu_to_le32(next);
448 else
449 mReqPrev->ptr->next = cpu_to_le32(next);
450 wmb();
451 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
452 goto done;
453 do {
454 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
455 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
456 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
457 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
458 if (tmp_stat)
459 goto done;
460 }
461
462 /* QH configuration */
463 mEp->qh.ptr->td.next = cpu_to_le32(mReq->dma); /* TERMINATE = 0 */
464 mEp->qh.ptr->td.token &= cpu_to_le32(~TD_STATUS); /* clear status */
465 mEp->qh.ptr->cap |= cpu_to_le32(QH_ZLT);
466
467 wmb(); /* synchronize before ep prime */
468
469 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
470 mEp->type == USB_ENDPOINT_XFER_CONTROL);
471 done:
472 return ret;
473 }
474
475 /**
476 * _hardware_dequeue: handles a request at hardware level
477 * @gadget: gadget
478 * @mEp: endpoint
479 *
480 * This function returns an error code
481 */
482 static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
483 {
484 if (mReq->req.status != -EALREADY)
485 return -EINVAL;
486
487 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->ptr->token) != 0)
488 return -EBUSY;
489
490 if (mReq->zptr) {
491 if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0)
492 return -EBUSY;
493 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
494 mReq->zptr = NULL;
495 }
496
497 mReq->req.status = 0;
498
499 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
500
501 mReq->req.status = le32_to_cpu(mReq->ptr->token) & TD_STATUS;
502 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
503 mReq->req.status = -1;
504 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
505 mReq->req.status = -1;
506 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
507 mReq->req.status = -1;
508
509 mReq->req.actual = le32_to_cpu(mReq->ptr->token) & TD_TOTAL_BYTES;
510 mReq->req.actual >>= __ffs(TD_TOTAL_BYTES);
511 mReq->req.actual = mReq->req.length - mReq->req.actual;
512 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
513
514 return mReq->req.actual;
515 }
516
517 /**
518 * _ep_nuke: dequeues all endpoint requests
519 * @mEp: endpoint
520 *
521 * This function returns an error code
522 * Caller must hold lock
523 */
524 static int _ep_nuke(struct ci13xxx_ep *mEp)
525 __releases(mEp->lock)
526 __acquires(mEp->lock)
527 {
528 if (mEp == NULL)
529 return -EINVAL;
530
531 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
532
533 while (!list_empty(&mEp->qh.queue)) {
534
535 /* pop oldest request */
536 struct ci13xxx_req *mReq = \
537 list_entry(mEp->qh.queue.next,
538 struct ci13xxx_req, queue);
539 list_del_init(&mReq->queue);
540 mReq->req.status = -ESHUTDOWN;
541
542 if (mReq->req.complete != NULL) {
543 spin_unlock(mEp->lock);
544 mReq->req.complete(&mEp->ep, &mReq->req);
545 spin_lock(mEp->lock);
546 }
547 }
548 return 0;
549 }
550
551 /**
552 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
553 * @gadget: gadget
554 *
555 * This function returns an error code
556 */
557 static int _gadget_stop_activity(struct usb_gadget *gadget)
558 {
559 struct usb_ep *ep;
560 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
561 unsigned long flags;
562
563 spin_lock_irqsave(&ci->lock, flags);
564 ci->gadget.speed = USB_SPEED_UNKNOWN;
565 ci->remote_wakeup = 0;
566 ci->suspended = 0;
567 spin_unlock_irqrestore(&ci->lock, flags);
568
569 /* flush all endpoints */
570 gadget_for_each_ep(ep, gadget) {
571 usb_ep_fifo_flush(ep);
572 }
573 usb_ep_fifo_flush(&ci->ep0out->ep);
574 usb_ep_fifo_flush(&ci->ep0in->ep);
575
576 if (ci->driver)
577 ci->driver->disconnect(gadget);
578
579 /* make sure to disable all endpoints */
580 gadget_for_each_ep(ep, gadget) {
581 usb_ep_disable(ep);
582 }
583
584 if (ci->status != NULL) {
585 usb_ep_free_request(&ci->ep0in->ep, ci->status);
586 ci->status = NULL;
587 }
588
589 return 0;
590 }
591
592 /******************************************************************************
593 * ISR block
594 *****************************************************************************/
595 /**
596 * isr_reset_handler: USB reset interrupt handler
597 * @ci: UDC device
598 *
599 * This function resets USB engine after a bus reset occurred
600 */
601 static void isr_reset_handler(struct ci13xxx *ci)
602 __releases(ci->lock)
603 __acquires(ci->lock)
604 {
605 int retval;
606
607 spin_unlock(&ci->lock);
608 retval = _gadget_stop_activity(&ci->gadget);
609 if (retval)
610 goto done;
611
612 retval = hw_usb_reset(ci);
613 if (retval)
614 goto done;
615
616 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
617 if (ci->status == NULL)
618 retval = -ENOMEM;
619
620 done:
621 spin_lock(&ci->lock);
622
623 if (retval)
624 dev_err(ci->dev, "error: %i\n", retval);
625 }
626
627 /**
628 * isr_get_status_complete: get_status request complete function
629 * @ep: endpoint
630 * @req: request handled
631 *
632 * Caller must release lock
633 */
634 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
635 {
636 if (ep == NULL || req == NULL)
637 return;
638
639 kfree(req->buf);
640 usb_ep_free_request(ep, req);
641 }
642
643 /**
644 * isr_get_status_response: get_status request response
645 * @ci: ci struct
646 * @setup: setup request packet
647 *
648 * This function returns an error code
649 */
650 static int isr_get_status_response(struct ci13xxx *ci,
651 struct usb_ctrlrequest *setup)
652 __releases(mEp->lock)
653 __acquires(mEp->lock)
654 {
655 struct ci13xxx_ep *mEp = ci->ep0in;
656 struct usb_request *req = NULL;
657 gfp_t gfp_flags = GFP_ATOMIC;
658 int dir, num, retval;
659
660 if (mEp == NULL || setup == NULL)
661 return -EINVAL;
662
663 spin_unlock(mEp->lock);
664 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
665 spin_lock(mEp->lock);
666 if (req == NULL)
667 return -ENOMEM;
668
669 req->complete = isr_get_status_complete;
670 req->length = 2;
671 req->buf = kzalloc(req->length, gfp_flags);
672 if (req->buf == NULL) {
673 retval = -ENOMEM;
674 goto err_free_req;
675 }
676
677 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
678 /* Assume that device is bus powered for now. */
679 *(u16 *)req->buf = ci->remote_wakeup << 1;
680 retval = 0;
681 } else if ((setup->bRequestType & USB_RECIP_MASK) \
682 == USB_RECIP_ENDPOINT) {
683 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
684 TX : RX;
685 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
686 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
687 }
688 /* else do nothing; reserved for future use */
689
690 spin_unlock(mEp->lock);
691 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
692 spin_lock(mEp->lock);
693 if (retval)
694 goto err_free_buf;
695
696 return 0;
697
698 err_free_buf:
699 kfree(req->buf);
700 err_free_req:
701 spin_unlock(mEp->lock);
702 usb_ep_free_request(&mEp->ep, req);
703 spin_lock(mEp->lock);
704 return retval;
705 }
706
707 /**
708 * isr_setup_status_complete: setup_status request complete function
709 * @ep: endpoint
710 * @req: request handled
711 *
712 * Caller must release lock. Put the port in test mode if test mode
713 * feature is selected.
714 */
715 static void
716 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
717 {
718 struct ci13xxx *ci = req->context;
719 unsigned long flags;
720
721 if (ci->setaddr) {
722 hw_usb_set_address(ci, ci->address);
723 ci->setaddr = false;
724 }
725
726 spin_lock_irqsave(&ci->lock, flags);
727 if (ci->test_mode)
728 hw_port_test_set(ci, ci->test_mode);
729 spin_unlock_irqrestore(&ci->lock, flags);
730 }
731
732 /**
733 * isr_setup_status_phase: queues the status phase of a setup transation
734 * @ci: ci struct
735 *
736 * This function returns an error code
737 */
738 static int isr_setup_status_phase(struct ci13xxx *ci)
739 __releases(mEp->lock)
740 __acquires(mEp->lock)
741 {
742 int retval;
743 struct ci13xxx_ep *mEp;
744
745 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
746 ci->status->context = ci;
747 ci->status->complete = isr_setup_status_complete;
748
749 spin_unlock(mEp->lock);
750 retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
751 spin_lock(mEp->lock);
752
753 return retval;
754 }
755
756 /**
757 * isr_tr_complete_low: transaction complete low level handler
758 * @mEp: endpoint
759 *
760 * This function returns an error code
761 * Caller must hold lock
762 */
763 static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
764 __releases(mEp->lock)
765 __acquires(mEp->lock)
766 {
767 struct ci13xxx_req *mReq, *mReqTemp;
768 struct ci13xxx_ep *mEpTemp = mEp;
769 int retval = 0;
770
771 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
772 queue) {
773 retval = _hardware_dequeue(mEp, mReq);
774 if (retval < 0)
775 break;
776 list_del_init(&mReq->queue);
777 if (mReq->req.complete != NULL) {
778 spin_unlock(mEp->lock);
779 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
780 mReq->req.length)
781 mEpTemp = mEp->ci->ep0in;
782 mReq->req.complete(&mEpTemp->ep, &mReq->req);
783 spin_lock(mEp->lock);
784 }
785 }
786
787 if (retval == -EBUSY)
788 retval = 0;
789
790 return retval;
791 }
792
793 /**
794 * isr_tr_complete_handler: transaction complete interrupt handler
795 * @ci: UDC descriptor
796 *
797 * This function handles traffic events
798 */
799 static void isr_tr_complete_handler(struct ci13xxx *ci)
800 __releases(ci->lock)
801 __acquires(ci->lock)
802 {
803 unsigned i;
804 u8 tmode = 0;
805
806 for (i = 0; i < ci->hw_ep_max; i++) {
807 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
808 int type, num, dir, err = -EINVAL;
809 struct usb_ctrlrequest req;
810
811 if (mEp->ep.desc == NULL)
812 continue; /* not configured */
813
814 if (hw_test_and_clear_complete(ci, i)) {
815 err = isr_tr_complete_low(mEp);
816 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
817 if (err > 0) /* needs status phase */
818 err = isr_setup_status_phase(ci);
819 if (err < 0) {
820 spin_unlock(&ci->lock);
821 if (usb_ep_set_halt(&mEp->ep))
822 dev_err(ci->dev,
823 "error: ep_set_halt\n");
824 spin_lock(&ci->lock);
825 }
826 }
827 }
828
829 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
830 !hw_test_and_clear_setup_status(ci, i))
831 continue;
832
833 if (i != 0) {
834 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
835 continue;
836 }
837
838 /*
839 * Flush data and handshake transactions of previous
840 * setup packet.
841 */
842 _ep_nuke(ci->ep0out);
843 _ep_nuke(ci->ep0in);
844
845 /* read_setup_packet */
846 do {
847 hw_test_and_set_setup_guard(ci);
848 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
849 } while (!hw_test_and_clear_setup_guard(ci));
850
851 type = req.bRequestType;
852
853 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
854
855 switch (req.bRequest) {
856 case USB_REQ_CLEAR_FEATURE:
857 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
858 le16_to_cpu(req.wValue) ==
859 USB_ENDPOINT_HALT) {
860 if (req.wLength != 0)
861 break;
862 num = le16_to_cpu(req.wIndex);
863 dir = num & USB_ENDPOINT_DIR_MASK;
864 num &= USB_ENDPOINT_NUMBER_MASK;
865 if (dir) /* TX */
866 num += ci->hw_ep_max/2;
867 if (!ci->ci13xxx_ep[num].wedge) {
868 spin_unlock(&ci->lock);
869 err = usb_ep_clear_halt(
870 &ci->ci13xxx_ep[num].ep);
871 spin_lock(&ci->lock);
872 if (err)
873 break;
874 }
875 err = isr_setup_status_phase(ci);
876 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
877 le16_to_cpu(req.wValue) ==
878 USB_DEVICE_REMOTE_WAKEUP) {
879 if (req.wLength != 0)
880 break;
881 ci->remote_wakeup = 0;
882 err = isr_setup_status_phase(ci);
883 } else {
884 goto delegate;
885 }
886 break;
887 case USB_REQ_GET_STATUS:
888 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
889 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
890 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
891 goto delegate;
892 if (le16_to_cpu(req.wLength) != 2 ||
893 le16_to_cpu(req.wValue) != 0)
894 break;
895 err = isr_get_status_response(ci, &req);
896 break;
897 case USB_REQ_SET_ADDRESS:
898 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
899 goto delegate;
900 if (le16_to_cpu(req.wLength) != 0 ||
901 le16_to_cpu(req.wIndex) != 0)
902 break;
903 ci->address = (u8)le16_to_cpu(req.wValue);
904 ci->setaddr = true;
905 err = isr_setup_status_phase(ci);
906 break;
907 case USB_REQ_SET_FEATURE:
908 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
909 le16_to_cpu(req.wValue) ==
910 USB_ENDPOINT_HALT) {
911 if (req.wLength != 0)
912 break;
913 num = le16_to_cpu(req.wIndex);
914 dir = num & USB_ENDPOINT_DIR_MASK;
915 num &= USB_ENDPOINT_NUMBER_MASK;
916 if (dir) /* TX */
917 num += ci->hw_ep_max/2;
918
919 spin_unlock(&ci->lock);
920 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
921 spin_lock(&ci->lock);
922 if (!err)
923 isr_setup_status_phase(ci);
924 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
925 if (req.wLength != 0)
926 break;
927 switch (le16_to_cpu(req.wValue)) {
928 case USB_DEVICE_REMOTE_WAKEUP:
929 ci->remote_wakeup = 1;
930 err = isr_setup_status_phase(ci);
931 break;
932 case USB_DEVICE_TEST_MODE:
933 tmode = le16_to_cpu(req.wIndex) >> 8;
934 switch (tmode) {
935 case TEST_J:
936 case TEST_K:
937 case TEST_SE0_NAK:
938 case TEST_PACKET:
939 case TEST_FORCE_EN:
940 ci->test_mode = tmode;
941 err = isr_setup_status_phase(
942 ci);
943 break;
944 default:
945 break;
946 }
947 default:
948 goto delegate;
949 }
950 } else {
951 goto delegate;
952 }
953 break;
954 default:
955 delegate:
956 if (req.wLength == 0) /* no data phase */
957 ci->ep0_dir = TX;
958
959 spin_unlock(&ci->lock);
960 err = ci->driver->setup(&ci->gadget, &req);
961 spin_lock(&ci->lock);
962 break;
963 }
964
965 if (err < 0) {
966 spin_unlock(&ci->lock);
967 if (usb_ep_set_halt(&mEp->ep))
968 dev_err(ci->dev, "error: ep_set_halt\n");
969 spin_lock(&ci->lock);
970 }
971 }
972 }
973
974 /******************************************************************************
975 * ENDPT block
976 *****************************************************************************/
977 /**
978 * ep_enable: configure endpoint, making it usable
979 *
980 * Check usb_ep_enable() at "usb_gadget.h" for details
981 */
982 static int ep_enable(struct usb_ep *ep,
983 const struct usb_endpoint_descriptor *desc)
984 {
985 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
986 int retval = 0;
987 unsigned long flags;
988
989 if (ep == NULL || desc == NULL)
990 return -EINVAL;
991
992 spin_lock_irqsave(mEp->lock, flags);
993
994 /* only internal SW should enable ctrl endpts */
995
996 mEp->ep.desc = desc;
997
998 if (!list_empty(&mEp->qh.queue))
999 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
1000
1001 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1002 mEp->num = usb_endpoint_num(desc);
1003 mEp->type = usb_endpoint_type(desc);
1004
1005 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
1006
1007 mEp->qh.ptr->cap = 0;
1008
1009 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1010 mEp->qh.ptr->cap |= cpu_to_le32(QH_IOS);
1011 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1012 mEp->qh.ptr->cap &= cpu_to_le32(~QH_MULT);
1013 else
1014 mEp->qh.ptr->cap &= cpu_to_le32(~QH_ZLT);
1015
1016 mEp->qh.ptr->cap |= cpu_to_le32((mEp->ep.maxpacket << __ffs(QH_MAX_PKT))
1017 & QH_MAX_PKT);
1018 mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
1019
1020 /*
1021 * Enable endpoints in the HW other than ep0 as ep0
1022 * is always enabled
1023 */
1024 if (mEp->num)
1025 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
1026
1027 spin_unlock_irqrestore(mEp->lock, flags);
1028 return retval;
1029 }
1030
1031 /**
1032 * ep_disable: endpoint is no longer usable
1033 *
1034 * Check usb_ep_disable() at "usb_gadget.h" for details
1035 */
1036 static int ep_disable(struct usb_ep *ep)
1037 {
1038 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1039 int direction, retval = 0;
1040 unsigned long flags;
1041
1042 if (ep == NULL)
1043 return -EINVAL;
1044 else if (mEp->ep.desc == NULL)
1045 return -EBUSY;
1046
1047 spin_lock_irqsave(mEp->lock, flags);
1048
1049 /* only internal SW should disable ctrl endpts */
1050
1051 direction = mEp->dir;
1052 do {
1053 retval |= _ep_nuke(mEp);
1054 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
1055
1056 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1057 mEp->dir = (mEp->dir == TX) ? RX : TX;
1058
1059 } while (mEp->dir != direction);
1060
1061 mEp->ep.desc = NULL;
1062
1063 spin_unlock_irqrestore(mEp->lock, flags);
1064 return retval;
1065 }
1066
1067 /**
1068 * ep_alloc_request: allocate a request object to use with this endpoint
1069 *
1070 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1071 */
1072 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1073 {
1074 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1075 struct ci13xxx_req *mReq = NULL;
1076
1077 if (ep == NULL)
1078 return NULL;
1079
1080 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1081 if (mReq != NULL) {
1082 INIT_LIST_HEAD(&mReq->queue);
1083
1084 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1085 &mReq->dma);
1086 if (mReq->ptr == NULL) {
1087 kfree(mReq);
1088 mReq = NULL;
1089 }
1090 }
1091
1092 return (mReq == NULL) ? NULL : &mReq->req;
1093 }
1094
1095 /**
1096 * ep_free_request: frees a request object
1097 *
1098 * Check usb_ep_free_request() at "usb_gadget.h" for details
1099 */
1100 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1101 {
1102 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1103 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1104 unsigned long flags;
1105
1106 if (ep == NULL || req == NULL) {
1107 return;
1108 } else if (!list_empty(&mReq->queue)) {
1109 dev_err(mEp->ci->dev, "freeing queued request\n");
1110 return;
1111 }
1112
1113 spin_lock_irqsave(mEp->lock, flags);
1114
1115 if (mReq->ptr)
1116 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1117 kfree(mReq);
1118
1119 spin_unlock_irqrestore(mEp->lock, flags);
1120 }
1121
1122 /**
1123 * ep_queue: queues (submits) an I/O request to an endpoint
1124 *
1125 * Check usb_ep_queue()* at usb_gadget.h" for details
1126 */
1127 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1128 gfp_t __maybe_unused gfp_flags)
1129 {
1130 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1131 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1132 struct ci13xxx *ci = mEp->ci;
1133 int retval = 0;
1134 unsigned long flags;
1135
1136 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
1137 return -EINVAL;
1138
1139 spin_lock_irqsave(mEp->lock, flags);
1140
1141 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1142 if (req->length)
1143 mEp = (ci->ep0_dir == RX) ?
1144 ci->ep0out : ci->ep0in;
1145 if (!list_empty(&mEp->qh.queue)) {
1146 _ep_nuke(mEp);
1147 retval = -EOVERFLOW;
1148 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
1149 _usb_addr(mEp));
1150 }
1151 }
1152
1153 /* first nuke then test link, e.g. previous status has not sent */
1154 if (!list_empty(&mReq->queue)) {
1155 retval = -EBUSY;
1156 dev_err(mEp->ci->dev, "request already in queue\n");
1157 goto done;
1158 }
1159
1160 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1161 req->length = 4 * CI13XXX_PAGE_SIZE;
1162 retval = -EMSGSIZE;
1163 dev_warn(mEp->ci->dev, "request length truncated\n");
1164 }
1165
1166 /* push request */
1167 mReq->req.status = -EINPROGRESS;
1168 mReq->req.actual = 0;
1169
1170 retval = _hardware_enqueue(mEp, mReq);
1171
1172 if (retval == -EALREADY)
1173 retval = 0;
1174 if (!retval)
1175 list_add_tail(&mReq->queue, &mEp->qh.queue);
1176
1177 done:
1178 spin_unlock_irqrestore(mEp->lock, flags);
1179 return retval;
1180 }
1181
1182 /**
1183 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1184 *
1185 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1186 */
1187 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1188 {
1189 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1190 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1191 unsigned long flags;
1192
1193 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
1194 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
1195 list_empty(&mEp->qh.queue))
1196 return -EINVAL;
1197
1198 spin_lock_irqsave(mEp->lock, flags);
1199
1200 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
1201
1202 /* pop request */
1203 list_del_init(&mReq->queue);
1204
1205 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
1206
1207 req->status = -ECONNRESET;
1208
1209 if (mReq->req.complete != NULL) {
1210 spin_unlock(mEp->lock);
1211 mReq->req.complete(&mEp->ep, &mReq->req);
1212 spin_lock(mEp->lock);
1213 }
1214
1215 spin_unlock_irqrestore(mEp->lock, flags);
1216 return 0;
1217 }
1218
1219 /**
1220 * ep_set_halt: sets the endpoint halt feature
1221 *
1222 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1223 */
1224 static int ep_set_halt(struct usb_ep *ep, int value)
1225 {
1226 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1227 int direction, retval = 0;
1228 unsigned long flags;
1229
1230 if (ep == NULL || mEp->ep.desc == NULL)
1231 return -EINVAL;
1232
1233 spin_lock_irqsave(mEp->lock, flags);
1234
1235 #ifndef STALL_IN
1236 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1237 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
1238 !list_empty(&mEp->qh.queue)) {
1239 spin_unlock_irqrestore(mEp->lock, flags);
1240 return -EAGAIN;
1241 }
1242 #endif
1243
1244 direction = mEp->dir;
1245 do {
1246 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
1247
1248 if (!value)
1249 mEp->wedge = 0;
1250
1251 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1252 mEp->dir = (mEp->dir == TX) ? RX : TX;
1253
1254 } while (mEp->dir != direction);
1255
1256 spin_unlock_irqrestore(mEp->lock, flags);
1257 return retval;
1258 }
1259
1260 /**
1261 * ep_set_wedge: sets the halt feature and ignores clear requests
1262 *
1263 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1264 */
1265 static int ep_set_wedge(struct usb_ep *ep)
1266 {
1267 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1268 unsigned long flags;
1269
1270 if (ep == NULL || mEp->ep.desc == NULL)
1271 return -EINVAL;
1272
1273 spin_lock_irqsave(mEp->lock, flags);
1274 mEp->wedge = 1;
1275 spin_unlock_irqrestore(mEp->lock, flags);
1276
1277 return usb_ep_set_halt(ep);
1278 }
1279
1280 /**
1281 * ep_fifo_flush: flushes contents of a fifo
1282 *
1283 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1284 */
1285 static void ep_fifo_flush(struct usb_ep *ep)
1286 {
1287 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1288 unsigned long flags;
1289
1290 if (ep == NULL) {
1291 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
1292 return;
1293 }
1294
1295 spin_lock_irqsave(mEp->lock, flags);
1296
1297 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
1298
1299 spin_unlock_irqrestore(mEp->lock, flags);
1300 }
1301
1302 /**
1303 * Endpoint-specific part of the API to the USB controller hardware
1304 * Check "usb_gadget.h" for details
1305 */
1306 static const struct usb_ep_ops usb_ep_ops = {
1307 .enable = ep_enable,
1308 .disable = ep_disable,
1309 .alloc_request = ep_alloc_request,
1310 .free_request = ep_free_request,
1311 .queue = ep_queue,
1312 .dequeue = ep_dequeue,
1313 .set_halt = ep_set_halt,
1314 .set_wedge = ep_set_wedge,
1315 .fifo_flush = ep_fifo_flush,
1316 };
1317
1318 /******************************************************************************
1319 * GADGET block
1320 *****************************************************************************/
1321 static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1322 {
1323 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1324 unsigned long flags;
1325 int gadget_ready = 0;
1326
1327 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
1328 return -EOPNOTSUPP;
1329
1330 spin_lock_irqsave(&ci->lock, flags);
1331 ci->vbus_active = is_active;
1332 if (ci->driver)
1333 gadget_ready = 1;
1334 spin_unlock_irqrestore(&ci->lock, flags);
1335
1336 if (gadget_ready) {
1337 if (is_active) {
1338 pm_runtime_get_sync(&_gadget->dev);
1339 hw_device_reset(ci, USBMODE_CM_DC);
1340 hw_device_state(ci, ci->ep0out->qh.dma);
1341 } else {
1342 hw_device_state(ci, 0);
1343 if (ci->platdata->notify_event)
1344 ci->platdata->notify_event(ci,
1345 CI13XXX_CONTROLLER_STOPPED_EVENT);
1346 _gadget_stop_activity(&ci->gadget);
1347 pm_runtime_put_sync(&_gadget->dev);
1348 }
1349 }
1350
1351 return 0;
1352 }
1353
1354 static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1355 {
1356 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1357 unsigned long flags;
1358 int ret = 0;
1359
1360 spin_lock_irqsave(&ci->lock, flags);
1361 if (!ci->remote_wakeup) {
1362 ret = -EOPNOTSUPP;
1363 goto out;
1364 }
1365 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1366 ret = -EINVAL;
1367 goto out;
1368 }
1369 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1370 out:
1371 spin_unlock_irqrestore(&ci->lock, flags);
1372 return ret;
1373 }
1374
1375 static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1376 {
1377 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1378
1379 if (ci->transceiver)
1380 return usb_phy_set_power(ci->transceiver, mA);
1381 return -ENOTSUPP;
1382 }
1383
1384 /* Change Data+ pullup status
1385 * this func is used by usb_gadget_connect/disconnet
1386 */
1387 static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on)
1388 {
1389 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1390
1391 if (is_on)
1392 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1393 else
1394 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1395
1396 return 0;
1397 }
1398
1399 static int ci13xxx_start(struct usb_gadget *gadget,
1400 struct usb_gadget_driver *driver);
1401 static int ci13xxx_stop(struct usb_gadget *gadget,
1402 struct usb_gadget_driver *driver);
1403 /**
1404 * Device operations part of the API to the USB controller hardware,
1405 * which don't involve endpoints (or i/o)
1406 * Check "usb_gadget.h" for details
1407 */
1408 static const struct usb_gadget_ops usb_gadget_ops = {
1409 .vbus_session = ci13xxx_vbus_session,
1410 .wakeup = ci13xxx_wakeup,
1411 .pullup = ci13xxx_pullup,
1412 .vbus_draw = ci13xxx_vbus_draw,
1413 .udc_start = ci13xxx_start,
1414 .udc_stop = ci13xxx_stop,
1415 };
1416
1417 static int init_eps(struct ci13xxx *ci)
1418 {
1419 int retval = 0, i, j;
1420
1421 for (i = 0; i < ci->hw_ep_max/2; i++)
1422 for (j = RX; j <= TX; j++) {
1423 int k = i + j * ci->hw_ep_max/2;
1424 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
1425
1426 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1427 (j == TX) ? "in" : "out");
1428
1429 mEp->ci = ci;
1430 mEp->lock = &ci->lock;
1431 mEp->td_pool = ci->td_pool;
1432
1433 mEp->ep.name = mEp->name;
1434 mEp->ep.ops = &usb_ep_ops;
1435 /*
1436 * for ep0: maxP defined in desc, for other
1437 * eps, maxP is set by epautoconfig() called
1438 * by gadget layer
1439 */
1440 mEp->ep.maxpacket = (unsigned short)~0;
1441
1442 INIT_LIST_HEAD(&mEp->qh.queue);
1443 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1444 &mEp->qh.dma);
1445 if (mEp->qh.ptr == NULL)
1446 retval = -ENOMEM;
1447 else
1448 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1449
1450 /*
1451 * set up shorthands for ep0 out and in endpoints,
1452 * don't add to gadget's ep_list
1453 */
1454 if (i == 0) {
1455 if (j == RX)
1456 ci->ep0out = mEp;
1457 else
1458 ci->ep0in = mEp;
1459
1460 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
1461 continue;
1462 }
1463
1464 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
1465 }
1466
1467 return retval;
1468 }
1469
1470 static void destroy_eps(struct ci13xxx *ci)
1471 {
1472 int i;
1473
1474 for (i = 0; i < ci->hw_ep_max; i++) {
1475 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1476
1477 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1478 }
1479 }
1480
1481 /**
1482 * ci13xxx_start: register a gadget driver
1483 * @gadget: our gadget
1484 * @driver: the driver being registered
1485 *
1486 * Interrupts are enabled here.
1487 */
1488 static int ci13xxx_start(struct usb_gadget *gadget,
1489 struct usb_gadget_driver *driver)
1490 {
1491 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
1492 unsigned long flags;
1493 int retval = -ENOMEM;
1494
1495 if (driver->disconnect == NULL)
1496 return -EINVAL;
1497
1498
1499 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1500 retval = usb_ep_enable(&ci->ep0out->ep);
1501 if (retval)
1502 return retval;
1503
1504 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1505 retval = usb_ep_enable(&ci->ep0in->ep);
1506 if (retval)
1507 return retval;
1508 spin_lock_irqsave(&ci->lock, flags);
1509
1510 ci->driver = driver;
1511 pm_runtime_get_sync(&ci->gadget.dev);
1512 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1513 if (ci->vbus_active) {
1514 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
1515 hw_device_reset(ci, USBMODE_CM_DC);
1516 } else {
1517 pm_runtime_put_sync(&ci->gadget.dev);
1518 goto done;
1519 }
1520 }
1521
1522 retval = hw_device_state(ci, ci->ep0out->qh.dma);
1523 if (retval)
1524 pm_runtime_put_sync(&ci->gadget.dev);
1525
1526 done:
1527 spin_unlock_irqrestore(&ci->lock, flags);
1528 return retval;
1529 }
1530
1531 /**
1532 * ci13xxx_stop: unregister a gadget driver
1533 */
1534 static int ci13xxx_stop(struct usb_gadget *gadget,
1535 struct usb_gadget_driver *driver)
1536 {
1537 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
1538 unsigned long flags;
1539
1540 spin_lock_irqsave(&ci->lock, flags);
1541
1542 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1543 ci->vbus_active) {
1544 hw_device_state(ci, 0);
1545 if (ci->platdata->notify_event)
1546 ci->platdata->notify_event(ci,
1547 CI13XXX_CONTROLLER_STOPPED_EVENT);
1548 ci->driver = NULL;
1549 spin_unlock_irqrestore(&ci->lock, flags);
1550 _gadget_stop_activity(&ci->gadget);
1551 spin_lock_irqsave(&ci->lock, flags);
1552 pm_runtime_put(&ci->gadget.dev);
1553 }
1554
1555 spin_unlock_irqrestore(&ci->lock, flags);
1556
1557 return 0;
1558 }
1559
1560 /******************************************************************************
1561 * BUS block
1562 *****************************************************************************/
1563 /**
1564 * udc_irq: ci interrupt handler
1565 *
1566 * This function returns IRQ_HANDLED if the IRQ has been handled
1567 * It locks access to registers
1568 */
1569 static irqreturn_t udc_irq(struct ci13xxx *ci)
1570 {
1571 irqreturn_t retval;
1572 u32 intr;
1573
1574 if (ci == NULL)
1575 return IRQ_HANDLED;
1576
1577 spin_lock(&ci->lock);
1578
1579 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1580 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1581 USBMODE_CM_DC) {
1582 spin_unlock(&ci->lock);
1583 return IRQ_NONE;
1584 }
1585 }
1586 intr = hw_test_and_clear_intr_active(ci);
1587
1588 if (intr) {
1589 /* order defines priority - do NOT change it */
1590 if (USBi_URI & intr)
1591 isr_reset_handler(ci);
1592
1593 if (USBi_PCI & intr) {
1594 ci->gadget.speed = hw_port_is_high_speed(ci) ?
1595 USB_SPEED_HIGH : USB_SPEED_FULL;
1596 if (ci->suspended && ci->driver->resume) {
1597 spin_unlock(&ci->lock);
1598 ci->driver->resume(&ci->gadget);
1599 spin_lock(&ci->lock);
1600 ci->suspended = 0;
1601 }
1602 }
1603
1604 if (USBi_UI & intr)
1605 isr_tr_complete_handler(ci);
1606
1607 if (USBi_SLI & intr) {
1608 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1609 ci->driver->suspend) {
1610 ci->suspended = 1;
1611 spin_unlock(&ci->lock);
1612 ci->driver->suspend(&ci->gadget);
1613 spin_lock(&ci->lock);
1614 }
1615 }
1616 retval = IRQ_HANDLED;
1617 } else {
1618 retval = IRQ_NONE;
1619 }
1620 spin_unlock(&ci->lock);
1621
1622 return retval;
1623 }
1624
1625 /**
1626 * udc_release: driver release function
1627 * @dev: device
1628 *
1629 * Currently does nothing
1630 */
1631 static void udc_release(struct device *dev)
1632 {
1633 }
1634
1635 /**
1636 * udc_start: initialize gadget role
1637 * @ci: chipidea controller
1638 */
1639 static int udc_start(struct ci13xxx *ci)
1640 {
1641 struct device *dev = ci->dev;
1642 int retval = 0;
1643
1644 spin_lock_init(&ci->lock);
1645
1646 ci->gadget.ops = &usb_gadget_ops;
1647 ci->gadget.speed = USB_SPEED_UNKNOWN;
1648 ci->gadget.max_speed = USB_SPEED_HIGH;
1649 ci->gadget.is_otg = 0;
1650 ci->gadget.name = ci->platdata->name;
1651
1652 INIT_LIST_HEAD(&ci->gadget.ep_list);
1653
1654 dev_set_name(&ci->gadget.dev, "gadget");
1655 ci->gadget.dev.dma_mask = dev->dma_mask;
1656 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1657 ci->gadget.dev.parent = dev;
1658 ci->gadget.dev.release = udc_release;
1659
1660 /* alloc resources */
1661 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
1662 sizeof(struct ci13xxx_qh),
1663 64, CI13XXX_PAGE_SIZE);
1664 if (ci->qh_pool == NULL)
1665 return -ENOMEM;
1666
1667 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
1668 sizeof(struct ci13xxx_td),
1669 64, CI13XXX_PAGE_SIZE);
1670 if (ci->td_pool == NULL) {
1671 retval = -ENOMEM;
1672 goto free_qh_pool;
1673 }
1674
1675 retval = init_eps(ci);
1676 if (retval)
1677 goto free_pools;
1678
1679 ci->gadget.ep0 = &ci->ep0in->ep;
1680
1681 if (ci->global_phy)
1682 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
1683
1684 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1685 if (ci->transceiver == NULL) {
1686 retval = -ENODEV;
1687 goto destroy_eps;
1688 }
1689 }
1690
1691 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1692 retval = hw_device_reset(ci, USBMODE_CM_DC);
1693 if (retval)
1694 goto put_transceiver;
1695 }
1696
1697 retval = device_register(&ci->gadget.dev);
1698 if (retval) {
1699 put_device(&ci->gadget.dev);
1700 goto put_transceiver;
1701 }
1702
1703 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1704 retval = otg_set_peripheral(ci->transceiver->otg,
1705 &ci->gadget);
1706 if (retval)
1707 goto unreg_device;
1708 }
1709
1710 retval = usb_add_gadget_udc(dev, &ci->gadget);
1711 if (retval)
1712 goto remove_trans;
1713
1714 pm_runtime_no_callbacks(&ci->gadget.dev);
1715 pm_runtime_enable(&ci->gadget.dev);
1716
1717 return retval;
1718
1719 remove_trans:
1720 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1721 otg_set_peripheral(ci->transceiver->otg, NULL);
1722 if (ci->global_phy)
1723 usb_put_phy(ci->transceiver);
1724 }
1725
1726 dev_err(dev, "error = %i\n", retval);
1727 unreg_device:
1728 device_unregister(&ci->gadget.dev);
1729 put_transceiver:
1730 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
1731 usb_put_phy(ci->transceiver);
1732 destroy_eps:
1733 destroy_eps(ci);
1734 free_pools:
1735 dma_pool_destroy(ci->td_pool);
1736 free_qh_pool:
1737 dma_pool_destroy(ci->qh_pool);
1738 return retval;
1739 }
1740
1741 /**
1742 * udc_remove: parent remove must call this to remove UDC
1743 *
1744 * No interrupts active, the IRQ has been released
1745 */
1746 static void udc_stop(struct ci13xxx *ci)
1747 {
1748 if (ci == NULL)
1749 return;
1750
1751 usb_del_gadget_udc(&ci->gadget);
1752
1753 destroy_eps(ci);
1754
1755 dma_pool_destroy(ci->td_pool);
1756 dma_pool_destroy(ci->qh_pool);
1757
1758 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1759 otg_set_peripheral(ci->transceiver->otg, NULL);
1760 if (ci->global_phy)
1761 usb_put_phy(ci->transceiver);
1762 }
1763 device_unregister(&ci->gadget.dev);
1764 /* my kobject is dynamic, I swear! */
1765 memset(&ci->gadget, 0, sizeof(ci->gadget));
1766 }
1767
1768 /**
1769 * ci_hdrc_gadget_init - initialize device related bits
1770 * ci: the controller
1771 *
1772 * This function enables the gadget role, if the device is "device capable".
1773 */
1774 int ci_hdrc_gadget_init(struct ci13xxx *ci)
1775 {
1776 struct ci_role_driver *rdrv;
1777
1778 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1779 return -ENXIO;
1780
1781 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1782 if (!rdrv)
1783 return -ENOMEM;
1784
1785 rdrv->start = udc_start;
1786 rdrv->stop = udc_stop;
1787 rdrv->irq = udc_irq;
1788 rdrv->name = "gadget";
1789 ci->roles[CI_ROLE_GADGET] = rdrv;
1790
1791 return 0;
1792 }