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