2 * udc.c - ChipIdea UDC driver
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
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.
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/dmapool.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29 #include <linux/usb/otg.h>
30 #include <linux/usb/chipidea.h>
37 /* control endpoint description */
38 static const struct usb_endpoint_descriptor
39 ctrl_endpt_out_desc = {
40 .bLength = USB_DT_ENDPOINT_SIZE,
41 .bDescriptorType = USB_DT_ENDPOINT,
43 .bEndpointAddress = USB_DIR_OUT,
44 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
45 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
48 static const struct usb_endpoint_descriptor
49 ctrl_endpt_in_desc = {
50 .bLength = USB_DT_ENDPOINT_SIZE,
51 .bDescriptorType = USB_DT_ENDPOINT,
53 .bEndpointAddress = USB_DIR_IN,
54 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
55 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
59 * hw_ep_bit: calculates the bit number
60 * @num: endpoint number
61 * @dir: endpoint direction
63 * This function returns bit number
65 static inline int hw_ep_bit(int num, int dir)
67 return num + (dir ? 16 : 0);
70 static inline int ep_to_bit(struct ci13xxx *ci, int n)
72 int fill = 16 - ci->hw_ep_max / 2;
74 if (n >= ci->hw_ep_max / 2)
81 * hw_device_state: enables/disables interrupts & starts/stops device (execute
82 * without interruption)
83 * @dma: 0 => disable, !0 => enable and set dma engine
85 * This function returns an error code
87 static int hw_device_state(struct ci13xxx *ci, u32 dma)
90 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
91 /* interrupt, error, port change, reset, sleep/suspend */
92 hw_write(ci, OP_USBINTR, ~0,
93 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
94 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
96 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
97 hw_write(ci, OP_USBINTR, ~0, 0);
103 * hw_ep_flush: flush endpoint fifo (execute without interruption)
104 * @num: endpoint number
105 * @dir: endpoint direction
107 * This function returns an error code
109 static int hw_ep_flush(struct ci13xxx *ci, int num, int dir)
111 int n = hw_ep_bit(num, dir);
114 /* flush any pending transfer */
115 hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n));
116 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
118 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
124 * hw_ep_disable: disables endpoint (execute without interruption)
125 * @num: endpoint number
126 * @dir: endpoint direction
128 * This function returns an error code
130 static int hw_ep_disable(struct ci13xxx *ci, int num, int dir)
132 hw_ep_flush(ci, num, dir);
133 hw_write(ci, OP_ENDPTCTRL + num,
134 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
139 * hw_ep_enable: enables endpoint (execute without interruption)
140 * @num: endpoint number
141 * @dir: endpoint direction
142 * @type: endpoint type
144 * This function returns an error code
146 static int hw_ep_enable(struct ci13xxx *ci, int num, int dir, int type)
151 mask = ENDPTCTRL_TXT; /* type */
152 data = type << ffs_nr(mask);
154 mask |= ENDPTCTRL_TXS; /* unstall */
155 mask |= ENDPTCTRL_TXR; /* reset data toggle */
156 data |= ENDPTCTRL_TXR;
157 mask |= ENDPTCTRL_TXE; /* enable */
158 data |= ENDPTCTRL_TXE;
160 mask = ENDPTCTRL_RXT; /* type */
161 data = type << ffs_nr(mask);
163 mask |= ENDPTCTRL_RXS; /* unstall */
164 mask |= ENDPTCTRL_RXR; /* reset data toggle */
165 data |= ENDPTCTRL_RXR;
166 mask |= ENDPTCTRL_RXE; /* enable */
167 data |= ENDPTCTRL_RXE;
169 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
174 * hw_ep_get_halt: return endpoint halt status
175 * @num: endpoint number
176 * @dir: endpoint direction
178 * This function returns 1 if endpoint halted
180 static int hw_ep_get_halt(struct ci13xxx *ci, int num, int dir)
182 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
184 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
188 * hw_test_and_clear_setup_status: test & clear setup status (execute without
190 * @n: endpoint number
192 * This function returns setup status
194 static int hw_test_and_clear_setup_status(struct ci13xxx *ci, int n)
196 n = ep_to_bit(ci, n);
197 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
201 * hw_ep_prime: primes endpoint (execute without interruption)
202 * @num: endpoint number
203 * @dir: endpoint direction
204 * @is_ctrl: true if control endpoint
206 * This function returns an error code
208 static int hw_ep_prime(struct ci13xxx *ci, int num, int dir, int is_ctrl)
210 int n = hw_ep_bit(num, dir);
212 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
215 hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n));
217 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
219 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
222 /* status shoult be tested according with manual but it doesn't work */
227 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
228 * without interruption)
229 * @num: endpoint number
230 * @dir: endpoint direction
231 * @value: true => stall, false => unstall
233 * This function returns an error code
235 static int hw_ep_set_halt(struct ci13xxx *ci, int num, int dir, int value)
237 if (value != 0 && value != 1)
241 enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
242 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
243 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
245 /* data toggle - reserved for EP0 but it's in ESS */
246 hw_write(ci, reg, mask_xs|mask_xr,
247 value ? mask_xs : mask_xr);
248 } while (value != hw_ep_get_halt(ci, num, dir));
254 * hw_is_port_high_speed: test if port is high speed
256 * This function returns true if high speed port
258 static int hw_port_is_high_speed(struct ci13xxx *ci)
260 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
261 hw_read(ci, OP_PORTSC, PORTSC_HSP);
265 * hw_read_intr_enable: returns interrupt enable register
267 * This function returns register data
269 static u32 hw_read_intr_enable(struct ci13xxx *ci)
271 return hw_read(ci, OP_USBINTR, ~0);
275 * hw_read_intr_status: returns interrupt status register
277 * This function returns register data
279 static u32 hw_read_intr_status(struct ci13xxx *ci)
281 return hw_read(ci, OP_USBSTS, ~0);
285 * hw_test_and_clear_complete: test & clear complete status (execute without
287 * @n: endpoint number
289 * This function returns complete status
291 static int hw_test_and_clear_complete(struct ci13xxx *ci, int n)
293 n = ep_to_bit(ci, n);
294 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
298 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
299 * without interruption)
301 * This function returns active interrutps
303 static u32 hw_test_and_clear_intr_active(struct ci13xxx *ci)
305 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
307 hw_write(ci, OP_USBSTS, ~0, reg);
312 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
315 * This function returns guard value
317 static int hw_test_and_clear_setup_guard(struct ci13xxx *ci)
319 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
323 * hw_test_and_set_setup_guard: test & set setup guard (execute without
326 * This function returns guard value
328 static int hw_test_and_set_setup_guard(struct ci13xxx *ci)
330 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
334 * hw_usb_set_address: configures USB address (execute without interruption)
335 * @value: new USB address
337 * This function explicitly sets the address, without the "USBADRA" (advance)
338 * feature, which is not supported by older versions of the controller.
340 static void hw_usb_set_address(struct ci13xxx *ci, u8 value)
342 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
343 value << ffs_nr(DEVICEADDR_USBADR));
347 * hw_usb_reset: restart device after a bus reset (execute without
350 * This function returns an error code
352 static int hw_usb_reset(struct ci13xxx *ci)
354 hw_usb_set_address(ci, 0);
356 /* ESS flushes only at end?!? */
357 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
359 /* clear setup token semaphores */
360 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
362 /* clear complete status */
363 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
365 /* wait until all bits cleared */
366 while (hw_read(ci, OP_ENDPTPRIME, ~0))
367 udelay(10); /* not RTOS friendly */
369 /* reset all endpoints ? */
371 /* reset internal status and wait for further instructions
372 no need to verify the port reset status (ESS does it) */
377 /******************************************************************************
379 *****************************************************************************/
381 * _usb_addr: calculates endpoint address from direction & number
384 static inline u8 _usb_addr(struct ci13xxx_ep *ep)
386 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
390 * _hardware_queue: configures a request at hardware level
394 * This function returns an error code
396 static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
398 struct ci13xxx *ci = mEp->ci;
401 unsigned length = mReq->req.length;
403 /* don't queue twice */
404 if (mReq->req.status == -EALREADY)
407 mReq->req.status = -EALREADY;
409 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
410 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
412 if (mReq->zptr == NULL)
415 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
416 mReq->zptr->next = TD_TERMINATE;
417 mReq->zptr->token = TD_STATUS_ACTIVE;
418 if (!mReq->req.no_interrupt)
419 mReq->zptr->token |= TD_IOC;
421 ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir);
427 * TODO - handle requests which spawns into several TDs
429 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
430 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
431 mReq->ptr->token &= TD_TOTAL_BYTES;
432 mReq->ptr->token |= TD_STATUS_ACTIVE;
434 mReq->ptr->next = mReq->zdma;
436 mReq->ptr->next = TD_TERMINATE;
437 if (!mReq->req.no_interrupt)
438 mReq->ptr->token |= TD_IOC;
440 mReq->ptr->page[0] = mReq->req.dma;
441 for (i = 1; i < 5; i++)
443 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
445 if (!list_empty(&mEp->qh.queue)) {
446 struct ci13xxx_req *mReqPrev;
447 int n = hw_ep_bit(mEp->num, mEp->dir);
450 mReqPrev = list_entry(mEp->qh.queue.prev,
451 struct ci13xxx_req, queue);
453 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
455 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
457 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
460 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
461 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
462 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
463 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
468 /* QH configuration */
469 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
470 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
471 mEp->qh.ptr->cap |= QH_ZLT;
473 wmb(); /* synchronize before ep prime */
475 ret = hw_ep_prime(ci, mEp->num, mEp->dir,
476 mEp->type == USB_ENDPOINT_XFER_CONTROL);
482 * _hardware_dequeue: handles a request at hardware level
486 * This function returns an error code
488 static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
490 if (mReq->req.status != -EALREADY)
493 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
497 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
499 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
503 mReq->req.status = 0;
505 usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir);
507 mReq->req.status = mReq->ptr->token & TD_STATUS;
508 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
509 mReq->req.status = -1;
510 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
511 mReq->req.status = -1;
512 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
513 mReq->req.status = -1;
515 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
516 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
517 mReq->req.actual = mReq->req.length - mReq->req.actual;
518 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
520 return mReq->req.actual;
524 * _ep_nuke: dequeues all endpoint requests
527 * This function returns an error code
528 * Caller must hold lock
530 static int _ep_nuke(struct ci13xxx_ep *mEp)
531 __releases(mEp->lock)
532 __acquires(mEp->lock)
537 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
539 while (!list_empty(&mEp->qh.queue)) {
541 /* pop oldest request */
542 struct ci13xxx_req *mReq = \
543 list_entry(mEp->qh.queue.next,
544 struct ci13xxx_req, queue);
545 list_del_init(&mReq->queue);
546 mReq->req.status = -ESHUTDOWN;
548 if (mReq->req.complete != NULL) {
549 spin_unlock(mEp->lock);
550 mReq->req.complete(&mEp->ep, &mReq->req);
551 spin_lock(mEp->lock);
558 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
561 * This function returns an error code
563 static int _gadget_stop_activity(struct usb_gadget *gadget)
566 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
569 spin_lock_irqsave(&ci->lock, flags);
570 ci->gadget.speed = USB_SPEED_UNKNOWN;
571 ci->remote_wakeup = 0;
573 spin_unlock_irqrestore(&ci->lock, flags);
575 /* flush all endpoints */
576 gadget_for_each_ep(ep, gadget) {
577 usb_ep_fifo_flush(ep);
579 usb_ep_fifo_flush(&ci->ep0out->ep);
580 usb_ep_fifo_flush(&ci->ep0in->ep);
583 ci->driver->disconnect(gadget);
585 /* make sure to disable all endpoints */
586 gadget_for_each_ep(ep, gadget) {
590 if (ci->status != NULL) {
591 usb_ep_free_request(&ci->ep0in->ep, ci->status);
598 /******************************************************************************
600 *****************************************************************************/
602 * isr_reset_handler: USB reset interrupt handler
605 * This function resets USB engine after a bus reset occurred
607 static void isr_reset_handler(struct ci13xxx *ci)
613 dbg_event(0xFF, "BUS RST", 0);
615 spin_unlock(&ci->lock);
616 retval = _gadget_stop_activity(&ci->gadget);
620 retval = hw_usb_reset(ci);
624 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
625 if (ci->status == NULL)
629 spin_lock(&ci->lock);
632 dev_err(ci->dev, "error: %i\n", retval);
636 * isr_get_status_complete: get_status request complete function
638 * @req: request handled
640 * Caller must release lock
642 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
644 if (ep == NULL || req == NULL)
648 usb_ep_free_request(ep, req);
652 * isr_get_status_response: get_status request response
654 * @setup: setup request packet
656 * This function returns an error code
658 static int isr_get_status_response(struct ci13xxx *ci,
659 struct usb_ctrlrequest *setup)
660 __releases(mEp->lock)
661 __acquires(mEp->lock)
663 struct ci13xxx_ep *mEp = ci->ep0in;
664 struct usb_request *req = NULL;
665 gfp_t gfp_flags = GFP_ATOMIC;
666 int dir, num, retval;
668 if (mEp == NULL || setup == NULL)
671 spin_unlock(mEp->lock);
672 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
673 spin_lock(mEp->lock);
677 req->complete = isr_get_status_complete;
679 req->buf = kzalloc(req->length, gfp_flags);
680 if (req->buf == NULL) {
685 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
686 /* Assume that device is bus powered for now. */
687 *(u16 *)req->buf = ci->remote_wakeup << 1;
689 } else if ((setup->bRequestType & USB_RECIP_MASK) \
690 == USB_RECIP_ENDPOINT) {
691 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
693 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
694 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
696 /* else do nothing; reserved for future use */
698 spin_unlock(mEp->lock);
699 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
700 spin_lock(mEp->lock);
709 spin_unlock(mEp->lock);
710 usb_ep_free_request(&mEp->ep, req);
711 spin_lock(mEp->lock);
716 * isr_setup_status_complete: setup_status request complete function
718 * @req: request handled
720 * Caller must release lock. Put the port in test mode if test mode
721 * feature is selected.
724 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
726 struct ci13xxx *ci = req->context;
730 hw_usb_set_address(ci, ci->address);
734 spin_lock_irqsave(&ci->lock, flags);
736 hw_port_test_set(ci, ci->test_mode);
737 spin_unlock_irqrestore(&ci->lock, flags);
741 * isr_setup_status_phase: queues the status phase of a setup transation
744 * This function returns an error code
746 static int isr_setup_status_phase(struct ci13xxx *ci)
747 __releases(mEp->lock)
748 __acquires(mEp->lock)
751 struct ci13xxx_ep *mEp;
753 mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
754 ci->status->context = ci;
755 ci->status->complete = isr_setup_status_complete;
757 spin_unlock(mEp->lock);
758 retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC);
759 spin_lock(mEp->lock);
765 * isr_tr_complete_low: transaction complete low level handler
768 * This function returns an error code
769 * Caller must hold lock
771 static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
772 __releases(mEp->lock)
773 __acquires(mEp->lock)
775 struct ci13xxx_req *mReq, *mReqTemp;
776 struct ci13xxx_ep *mEpTemp = mEp;
777 int uninitialized_var(retval);
779 if (list_empty(&mEp->qh.queue))
782 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
784 retval = _hardware_dequeue(mEp, mReq);
787 list_del_init(&mReq->queue);
788 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
789 if (mReq->req.complete != NULL) {
790 spin_unlock(mEp->lock);
791 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
793 mEpTemp = mEp->ci->ep0in;
794 mReq->req.complete(&mEpTemp->ep, &mReq->req);
795 spin_lock(mEp->lock);
799 if (retval == -EBUSY)
802 dbg_event(_usb_addr(mEp), "DONE", retval);
808 * isr_tr_complete_handler: transaction complete interrupt handler
809 * @ci: UDC descriptor
811 * This function handles traffic events
813 static void isr_tr_complete_handler(struct ci13xxx *ci)
820 for (i = 0; i < ci->hw_ep_max; i++) {
821 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
822 int type, num, dir, err = -EINVAL;
823 struct usb_ctrlrequest req;
825 if (mEp->ep.desc == NULL)
826 continue; /* not configured */
828 if (hw_test_and_clear_complete(ci, i)) {
829 err = isr_tr_complete_low(mEp);
830 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
831 if (err > 0) /* needs status phase */
832 err = isr_setup_status_phase(ci);
834 dbg_event(_usb_addr(mEp),
836 spin_unlock(&ci->lock);
837 if (usb_ep_set_halt(&mEp->ep))
839 "error: ep_set_halt\n");
840 spin_lock(&ci->lock);
845 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
846 !hw_test_and_clear_setup_status(ci, i))
850 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
855 * Flush data and handshake transactions of previous
858 _ep_nuke(ci->ep0out);
861 /* read_setup_packet */
863 hw_test_and_set_setup_guard(ci);
864 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
865 } while (!hw_test_and_clear_setup_guard(ci));
867 type = req.bRequestType;
869 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
871 dbg_setup(_usb_addr(mEp), &req);
873 switch (req.bRequest) {
874 case USB_REQ_CLEAR_FEATURE:
875 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
876 le16_to_cpu(req.wValue) ==
878 if (req.wLength != 0)
880 num = le16_to_cpu(req.wIndex);
881 dir = num & USB_ENDPOINT_DIR_MASK;
882 num &= USB_ENDPOINT_NUMBER_MASK;
884 num += ci->hw_ep_max/2;
885 if (!ci->ci13xxx_ep[num].wedge) {
886 spin_unlock(&ci->lock);
887 err = usb_ep_clear_halt(
888 &ci->ci13xxx_ep[num].ep);
889 spin_lock(&ci->lock);
893 err = isr_setup_status_phase(ci);
894 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
895 le16_to_cpu(req.wValue) ==
896 USB_DEVICE_REMOTE_WAKEUP) {
897 if (req.wLength != 0)
899 ci->remote_wakeup = 0;
900 err = isr_setup_status_phase(ci);
905 case USB_REQ_GET_STATUS:
906 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
907 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
908 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
910 if (le16_to_cpu(req.wLength) != 2 ||
911 le16_to_cpu(req.wValue) != 0)
913 err = isr_get_status_response(ci, &req);
915 case USB_REQ_SET_ADDRESS:
916 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
918 if (le16_to_cpu(req.wLength) != 0 ||
919 le16_to_cpu(req.wIndex) != 0)
921 ci->address = (u8)le16_to_cpu(req.wValue);
923 err = isr_setup_status_phase(ci);
925 case USB_REQ_SET_FEATURE:
926 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
927 le16_to_cpu(req.wValue) ==
929 if (req.wLength != 0)
931 num = le16_to_cpu(req.wIndex);
932 dir = num & USB_ENDPOINT_DIR_MASK;
933 num &= USB_ENDPOINT_NUMBER_MASK;
935 num += ci->hw_ep_max/2;
937 spin_unlock(&ci->lock);
938 err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep);
939 spin_lock(&ci->lock);
941 isr_setup_status_phase(ci);
942 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
943 if (req.wLength != 0)
945 switch (le16_to_cpu(req.wValue)) {
946 case USB_DEVICE_REMOTE_WAKEUP:
947 ci->remote_wakeup = 1;
948 err = isr_setup_status_phase(ci);
950 case USB_DEVICE_TEST_MODE:
951 tmode = le16_to_cpu(req.wIndex) >> 8;
958 ci->test_mode = tmode;
959 err = isr_setup_status_phase(
974 if (req.wLength == 0) /* no data phase */
977 spin_unlock(&ci->lock);
978 err = ci->driver->setup(&ci->gadget, &req);
979 spin_lock(&ci->lock);
984 dbg_event(_usb_addr(mEp), "ERROR", err);
986 spin_unlock(&ci->lock);
987 if (usb_ep_set_halt(&mEp->ep))
988 dev_err(ci->dev, "error: ep_set_halt\n");
989 spin_lock(&ci->lock);
994 /******************************************************************************
996 *****************************************************************************/
998 * ep_enable: configure endpoint, making it usable
1000 * Check usb_ep_enable() at "usb_gadget.h" for details
1002 static int ep_enable(struct usb_ep *ep,
1003 const struct usb_endpoint_descriptor *desc)
1005 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1007 unsigned long flags;
1009 if (ep == NULL || desc == NULL)
1012 spin_lock_irqsave(mEp->lock, flags);
1014 /* only internal SW should enable ctrl endpts */
1016 mEp->ep.desc = desc;
1018 if (!list_empty(&mEp->qh.queue))
1019 dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n");
1021 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1022 mEp->num = usb_endpoint_num(desc);
1023 mEp->type = usb_endpoint_type(desc);
1025 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
1027 dbg_event(_usb_addr(mEp), "ENABLE", 0);
1029 mEp->qh.ptr->cap = 0;
1031 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1032 mEp->qh.ptr->cap |= QH_IOS;
1033 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1034 mEp->qh.ptr->cap &= ~QH_MULT;
1036 mEp->qh.ptr->cap &= ~QH_ZLT;
1039 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
1040 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
1043 * Enable endpoints in the HW other than ep0 as ep0
1047 retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type);
1049 spin_unlock_irqrestore(mEp->lock, flags);
1054 * ep_disable: endpoint is no longer usable
1056 * Check usb_ep_disable() at "usb_gadget.h" for details
1058 static int ep_disable(struct usb_ep *ep)
1060 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1061 int direction, retval = 0;
1062 unsigned long flags;
1066 else if (mEp->ep.desc == NULL)
1069 spin_lock_irqsave(mEp->lock, flags);
1071 /* only internal SW should disable ctrl endpts */
1073 direction = mEp->dir;
1075 dbg_event(_usb_addr(mEp), "DISABLE", 0);
1077 retval |= _ep_nuke(mEp);
1078 retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir);
1080 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1081 mEp->dir = (mEp->dir == TX) ? RX : TX;
1083 } while (mEp->dir != direction);
1085 mEp->ep.desc = NULL;
1087 spin_unlock_irqrestore(mEp->lock, flags);
1092 * ep_alloc_request: allocate a request object to use with this endpoint
1094 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1096 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1098 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1099 struct ci13xxx_req *mReq = NULL;
1104 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
1106 INIT_LIST_HEAD(&mReq->queue);
1108 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
1110 if (mReq->ptr == NULL) {
1116 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
1118 return (mReq == NULL) ? NULL : &mReq->req;
1122 * ep_free_request: frees a request object
1124 * Check usb_ep_free_request() at "usb_gadget.h" for details
1126 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1128 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1129 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1130 unsigned long flags;
1132 if (ep == NULL || req == NULL) {
1134 } else if (!list_empty(&mReq->queue)) {
1135 dev_err(mEp->ci->dev, "freeing queued request\n");
1139 spin_lock_irqsave(mEp->lock, flags);
1142 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
1145 dbg_event(_usb_addr(mEp), "FREE", 0);
1147 spin_unlock_irqrestore(mEp->lock, flags);
1151 * ep_queue: queues (submits) an I/O request to an endpoint
1153 * Check usb_ep_queue()* at usb_gadget.h" for details
1155 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1156 gfp_t __maybe_unused gfp_flags)
1158 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1159 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1160 struct ci13xxx *ci = mEp->ci;
1162 unsigned long flags;
1164 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
1167 spin_lock_irqsave(mEp->lock, flags);
1169 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1171 mEp = (ci->ep0_dir == RX) ?
1172 ci->ep0out : ci->ep0in;
1173 if (!list_empty(&mEp->qh.queue)) {
1175 retval = -EOVERFLOW;
1176 dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n",
1181 /* first nuke then test link, e.g. previous status has not sent */
1182 if (!list_empty(&mReq->queue)) {
1184 dev_err(mEp->ci->dev, "request already in queue\n");
1188 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
1189 req->length = 4 * CI13XXX_PAGE_SIZE;
1191 dev_warn(mEp->ci->dev, "request length truncated\n");
1194 dbg_queue(_usb_addr(mEp), req, retval);
1197 mReq->req.status = -EINPROGRESS;
1198 mReq->req.actual = 0;
1200 retval = _hardware_enqueue(mEp, mReq);
1202 if (retval == -EALREADY) {
1203 dbg_event(_usb_addr(mEp), "QUEUE", retval);
1207 list_add_tail(&mReq->queue, &mEp->qh.queue);
1210 spin_unlock_irqrestore(mEp->lock, flags);
1215 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1217 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1219 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1221 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1222 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
1223 unsigned long flags;
1225 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
1226 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
1227 list_empty(&mEp->qh.queue))
1230 spin_lock_irqsave(mEp->lock, flags);
1232 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
1234 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
1237 list_del_init(&mReq->queue);
1239 usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir);
1241 req->status = -ECONNRESET;
1243 if (mReq->req.complete != NULL) {
1244 spin_unlock(mEp->lock);
1245 mReq->req.complete(&mEp->ep, &mReq->req);
1246 spin_lock(mEp->lock);
1249 spin_unlock_irqrestore(mEp->lock, flags);
1254 * ep_set_halt: sets the endpoint halt feature
1256 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1258 static int ep_set_halt(struct usb_ep *ep, int value)
1260 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1261 int direction, retval = 0;
1262 unsigned long flags;
1264 if (ep == NULL || mEp->ep.desc == NULL)
1267 spin_lock_irqsave(mEp->lock, flags);
1270 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1271 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
1272 !list_empty(&mEp->qh.queue)) {
1273 spin_unlock_irqrestore(mEp->lock, flags);
1278 direction = mEp->dir;
1280 dbg_event(_usb_addr(mEp), "HALT", value);
1281 retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value);
1286 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1287 mEp->dir = (mEp->dir == TX) ? RX : TX;
1289 } while (mEp->dir != direction);
1291 spin_unlock_irqrestore(mEp->lock, flags);
1296 * ep_set_wedge: sets the halt feature and ignores clear requests
1298 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1300 static int ep_set_wedge(struct usb_ep *ep)
1302 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1303 unsigned long flags;
1305 if (ep == NULL || mEp->ep.desc == NULL)
1308 spin_lock_irqsave(mEp->lock, flags);
1310 dbg_event(_usb_addr(mEp), "WEDGE", 0);
1313 spin_unlock_irqrestore(mEp->lock, flags);
1315 return usb_ep_set_halt(ep);
1319 * ep_fifo_flush: flushes contents of a fifo
1321 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1323 static void ep_fifo_flush(struct usb_ep *ep)
1325 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1326 unsigned long flags;
1329 dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp));
1333 spin_lock_irqsave(mEp->lock, flags);
1335 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
1336 hw_ep_flush(mEp->ci, mEp->num, mEp->dir);
1338 spin_unlock_irqrestore(mEp->lock, flags);
1342 * Endpoint-specific part of the API to the USB controller hardware
1343 * Check "usb_gadget.h" for details
1345 static const struct usb_ep_ops usb_ep_ops = {
1346 .enable = ep_enable,
1347 .disable = ep_disable,
1348 .alloc_request = ep_alloc_request,
1349 .free_request = ep_free_request,
1351 .dequeue = ep_dequeue,
1352 .set_halt = ep_set_halt,
1353 .set_wedge = ep_set_wedge,
1354 .fifo_flush = ep_fifo_flush,
1357 /******************************************************************************
1359 *****************************************************************************/
1360 static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
1362 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1363 unsigned long flags;
1364 int gadget_ready = 0;
1366 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS))
1369 spin_lock_irqsave(&ci->lock, flags);
1370 ci->vbus_active = is_active;
1373 spin_unlock_irqrestore(&ci->lock, flags);
1377 pm_runtime_get_sync(&_gadget->dev);
1378 hw_device_reset(ci, USBMODE_CM_DC);
1379 hw_device_state(ci, ci->ep0out->qh.dma);
1381 hw_device_state(ci, 0);
1382 if (ci->platdata->notify_event)
1383 ci->platdata->notify_event(ci,
1384 CI13XXX_CONTROLLER_STOPPED_EVENT);
1385 _gadget_stop_activity(&ci->gadget);
1386 pm_runtime_put_sync(&_gadget->dev);
1393 static int ci13xxx_wakeup(struct usb_gadget *_gadget)
1395 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1396 unsigned long flags;
1399 spin_lock_irqsave(&ci->lock, flags);
1400 if (!ci->remote_wakeup) {
1404 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1408 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1410 spin_unlock_irqrestore(&ci->lock, flags);
1414 static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1416 struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget);
1418 if (ci->transceiver)
1419 return usb_phy_set_power(ci->transceiver, mA);
1423 static int ci13xxx_start(struct usb_gadget *gadget,
1424 struct usb_gadget_driver *driver);
1425 static int ci13xxx_stop(struct usb_gadget *gadget,
1426 struct usb_gadget_driver *driver);
1428 * Device operations part of the API to the USB controller hardware,
1429 * which don't involve endpoints (or i/o)
1430 * Check "usb_gadget.h" for details
1432 static const struct usb_gadget_ops usb_gadget_ops = {
1433 .vbus_session = ci13xxx_vbus_session,
1434 .wakeup = ci13xxx_wakeup,
1435 .vbus_draw = ci13xxx_vbus_draw,
1436 .udc_start = ci13xxx_start,
1437 .udc_stop = ci13xxx_stop,
1440 static int init_eps(struct ci13xxx *ci)
1442 int retval = 0, i, j;
1444 for (i = 0; i < ci->hw_ep_max/2; i++)
1445 for (j = RX; j <= TX; j++) {
1446 int k = i + j * ci->hw_ep_max/2;
1447 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k];
1449 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
1450 (j == TX) ? "in" : "out");
1453 mEp->lock = &ci->lock;
1454 mEp->td_pool = ci->td_pool;
1456 mEp->ep.name = mEp->name;
1457 mEp->ep.ops = &usb_ep_ops;
1459 * for ep0: maxP defined in desc, for other
1460 * eps, maxP is set by epautoconfig() called
1463 mEp->ep.maxpacket = (unsigned short)~0;
1465 INIT_LIST_HEAD(&mEp->qh.queue);
1466 mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1468 if (mEp->qh.ptr == NULL)
1471 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
1474 * set up shorthands for ep0 out and in endpoints,
1475 * don't add to gadget's ep_list
1483 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
1487 list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list);
1494 * ci13xxx_start: register a gadget driver
1495 * @gadget: our gadget
1496 * @driver: the driver being registered
1498 * Interrupts are enabled here.
1500 static int ci13xxx_start(struct usb_gadget *gadget,
1501 struct usb_gadget_driver *driver)
1503 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
1504 unsigned long flags;
1505 int retval = -ENOMEM;
1507 if (driver->disconnect == NULL)
1511 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1512 retval = usb_ep_enable(&ci->ep0out->ep);
1516 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1517 retval = usb_ep_enable(&ci->ep0in->ep);
1520 spin_lock_irqsave(&ci->lock, flags);
1522 ci->driver = driver;
1523 pm_runtime_get_sync(&ci->gadget.dev);
1524 if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) {
1525 if (ci->vbus_active) {
1526 if (ci->platdata->flags & CI13XXX_REGS_SHARED)
1527 hw_device_reset(ci, USBMODE_CM_DC);
1529 pm_runtime_put_sync(&ci->gadget.dev);
1534 retval = hw_device_state(ci, ci->ep0out->qh.dma);
1536 pm_runtime_put_sync(&ci->gadget.dev);
1539 spin_unlock_irqrestore(&ci->lock, flags);
1544 * ci13xxx_stop: unregister a gadget driver
1546 static int ci13xxx_stop(struct usb_gadget *gadget,
1547 struct usb_gadget_driver *driver)
1549 struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget);
1550 unsigned long flags;
1552 spin_lock_irqsave(&ci->lock, flags);
1554 if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) ||
1556 hw_device_state(ci, 0);
1557 if (ci->platdata->notify_event)
1558 ci->platdata->notify_event(ci,
1559 CI13XXX_CONTROLLER_STOPPED_EVENT);
1561 spin_unlock_irqrestore(&ci->lock, flags);
1562 _gadget_stop_activity(&ci->gadget);
1563 spin_lock_irqsave(&ci->lock, flags);
1564 pm_runtime_put(&ci->gadget.dev);
1567 spin_unlock_irqrestore(&ci->lock, flags);
1572 /******************************************************************************
1574 *****************************************************************************/
1576 * udc_irq: ci interrupt handler
1578 * This function returns IRQ_HANDLED if the IRQ has been handled
1579 * It locks access to registers
1581 static irqreturn_t udc_irq(struct ci13xxx *ci)
1589 spin_lock(&ci->lock);
1591 if (ci->platdata->flags & CI13XXX_REGS_SHARED) {
1592 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1594 spin_unlock(&ci->lock);
1598 intr = hw_test_and_clear_intr_active(ci);
1599 dbg_interrupt(intr);
1602 /* order defines priority - do NOT change it */
1603 if (USBi_URI & intr)
1604 isr_reset_handler(ci);
1606 if (USBi_PCI & intr) {
1607 ci->gadget.speed = hw_port_is_high_speed(ci) ?
1608 USB_SPEED_HIGH : USB_SPEED_FULL;
1609 if (ci->suspended && ci->driver->resume) {
1610 spin_unlock(&ci->lock);
1611 ci->driver->resume(&ci->gadget);
1612 spin_lock(&ci->lock);
1618 isr_tr_complete_handler(ci);
1620 if (USBi_SLI & intr) {
1621 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1622 ci->driver->suspend) {
1624 spin_unlock(&ci->lock);
1625 ci->driver->suspend(&ci->gadget);
1626 spin_lock(&ci->lock);
1629 retval = IRQ_HANDLED;
1633 spin_unlock(&ci->lock);
1639 * udc_release: driver release function
1642 * Currently does nothing
1644 static void udc_release(struct device *dev)
1649 * udc_start: initialize gadget role
1650 * @ci: chipidea controller
1652 static int udc_start(struct ci13xxx *ci)
1654 struct device *dev = ci->dev;
1657 spin_lock_init(&ci->lock);
1659 ci->gadget.ops = &usb_gadget_ops;
1660 ci->gadget.speed = USB_SPEED_UNKNOWN;
1661 ci->gadget.max_speed = USB_SPEED_HIGH;
1662 ci->gadget.is_otg = 0;
1663 ci->gadget.name = ci->platdata->name;
1665 INIT_LIST_HEAD(&ci->gadget.ep_list);
1667 dev_set_name(&ci->gadget.dev, "gadget");
1668 ci->gadget.dev.dma_mask = dev->dma_mask;
1669 ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
1670 ci->gadget.dev.parent = dev;
1671 ci->gadget.dev.release = udc_release;
1673 /* alloc resources */
1674 ci->qh_pool = dma_pool_create("ci13xxx_qh", dev,
1675 sizeof(struct ci13xxx_qh),
1676 64, CI13XXX_PAGE_SIZE);
1677 if (ci->qh_pool == NULL)
1680 ci->td_pool = dma_pool_create("ci13xxx_td", dev,
1681 sizeof(struct ci13xxx_td),
1682 64, CI13XXX_PAGE_SIZE);
1683 if (ci->td_pool == NULL) {
1688 retval = init_eps(ci);
1692 ci->gadget.ep0 = &ci->ep0in->ep;
1695 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
1697 if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
1698 if (ci->transceiver == NULL) {
1704 if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) {
1705 retval = hw_device_reset(ci, USBMODE_CM_DC);
1707 goto put_transceiver;
1710 retval = device_register(&ci->gadget.dev);
1712 put_device(&ci->gadget.dev);
1713 goto put_transceiver;
1716 retval = dbg_create_files(&ci->gadget.dev);
1720 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1721 retval = otg_set_peripheral(ci->transceiver->otg,
1727 retval = usb_add_gadget_udc(dev, &ci->gadget);
1731 pm_runtime_no_callbacks(&ci->gadget.dev);
1732 pm_runtime_enable(&ci->gadget.dev);
1737 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1738 otg_set_peripheral(ci->transceiver->otg, &ci->gadget);
1740 usb_put_phy(ci->transceiver);
1743 dev_err(dev, "error = %i\n", retval);
1745 dbg_remove_files(&ci->gadget.dev);
1747 device_unregister(&ci->gadget.dev);
1749 if (!IS_ERR_OR_NULL(ci->transceiver) && ci->global_phy)
1750 usb_put_phy(ci->transceiver);
1752 dma_pool_destroy(ci->td_pool);
1754 dma_pool_destroy(ci->qh_pool);
1759 * udc_remove: parent remove must call this to remove UDC
1761 * No interrupts active, the IRQ has been released
1763 static void udc_stop(struct ci13xxx *ci)
1770 usb_del_gadget_udc(&ci->gadget);
1772 for (i = 0; i < ci->hw_ep_max; i++) {
1773 struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i];
1775 dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma);
1778 dma_pool_destroy(ci->td_pool);
1779 dma_pool_destroy(ci->qh_pool);
1781 if (!IS_ERR_OR_NULL(ci->transceiver)) {
1782 otg_set_peripheral(ci->transceiver->otg, NULL);
1784 usb_put_phy(ci->transceiver);
1786 dbg_remove_files(&ci->gadget.dev);
1787 device_unregister(&ci->gadget.dev);
1788 /* my kobject is dynamic, I swear! */
1789 memset(&ci->gadget, 0, sizeof(ci->gadget));
1793 * ci_hdrc_gadget_init - initialize device related bits
1794 * ci: the controller
1796 * This function enables the gadget role, if the device is "device capable".
1798 int ci_hdrc_gadget_init(struct ci13xxx *ci)
1800 struct ci_role_driver *rdrv;
1802 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1805 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1809 rdrv->start = udc_start;
1810 rdrv->stop = udc_stop;
1811 rdrv->irq = udc_irq;
1812 rdrv->name = "gadget";
1813 ci->roles[CI_ROLE_GADGET] = rdrv;