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