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