1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * udc.c - ChipIdea UDC driver
4 *
5 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
6 *
7 * Author: David Lopo
8 */
9
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dmapool.h>
13 #include <linux/err.h>
14 #include <linux/irqreturn.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/otg-fsm.h>
22 #include <linux/usb/chipidea.h>
23
24 #include "ci.h"
25 #include "udc.h"
26 #include "bits.h"
27 #include "otg.h"
28 #include "otg_fsm.h"
29 #include "trace.h"
30
31 /* control endpoint description */
32 static const struct usb_endpoint_descriptor
33 ctrl_endpt_out_desc = {
34 .bLength = USB_DT_ENDPOINT_SIZE,
35 .bDescriptorType = USB_DT_ENDPOINT,
36
37 .bEndpointAddress = USB_DIR_OUT,
38 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
39 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
40 };
41
42 static const struct usb_endpoint_descriptor
43 ctrl_endpt_in_desc = {
44 .bLength = USB_DT_ENDPOINT_SIZE,
45 .bDescriptorType = USB_DT_ENDPOINT,
46
47 .bEndpointAddress = USB_DIR_IN,
48 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
49 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
50 };
51
52 static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
53 struct td_node *node);
54 /**
55 * hw_ep_bit: calculates the bit number
56 * @num: endpoint number
57 * @dir: endpoint direction
58 *
59 * This function returns bit number
60 */
hw_ep_bit(int num,int dir)61 static inline int hw_ep_bit(int num, int dir)
62 {
63 return num + ((dir == TX) ? 16 : 0);
64 }
65
ep_to_bit(struct ci_hdrc * ci,int n)66 static inline int ep_to_bit(struct ci_hdrc *ci, int n)
67 {
68 int fill = 16 - ci->hw_ep_max / 2;
69
70 if (n >= ci->hw_ep_max / 2)
71 n += fill;
72
73 return n;
74 }
75
76 /**
77 * hw_device_state: enables/disables interrupts (execute without interruption)
78 * @ci: the controller
79 * @dma: 0 => disable, !0 => enable and set dma engine
80 *
81 * This function returns an error code
82 */
hw_device_state(struct ci_hdrc * ci,u32 dma)83 static int hw_device_state(struct ci_hdrc *ci, u32 dma)
84 {
85 if (dma) {
86 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
87 /* interrupt, error, port change, reset, sleep/suspend */
88 hw_write(ci, OP_USBINTR, ~0,
89 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI);
90 } else {
91 hw_write(ci, OP_USBINTR, ~0, 0);
92 }
93 return 0;
94 }
95
96 /**
97 * hw_ep_flush: flush endpoint fifo (execute without interruption)
98 * @ci: the controller
99 * @num: endpoint number
100 * @dir: endpoint direction
101 *
102 * This function returns an error code
103 */
hw_ep_flush(struct ci_hdrc * ci,int num,int dir)104 static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
105 {
106 int n = hw_ep_bit(num, dir);
107
108 do {
109 /* flush any pending transfer */
110 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
111 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
112 cpu_relax();
113 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
114
115 return 0;
116 }
117
118 /**
119 * hw_ep_disable: disables endpoint (execute without interruption)
120 * @ci: the controller
121 * @num: endpoint number
122 * @dir: endpoint direction
123 *
124 * This function returns an error code
125 */
hw_ep_disable(struct ci_hdrc * ci,int num,int dir)126 static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
127 {
128 hw_write(ci, OP_ENDPTCTRL + num,
129 (dir == TX) ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
130 return 0;
131 }
132
133 /**
134 * hw_ep_enable: enables endpoint (execute without interruption)
135 * @ci: the controller
136 * @num: endpoint number
137 * @dir: endpoint direction
138 * @type: endpoint type
139 *
140 * This function returns an error code
141 */
hw_ep_enable(struct ci_hdrc * ci,int num,int dir,int type)142 static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
143 {
144 u32 mask, data;
145
146 if (dir == TX) {
147 mask = ENDPTCTRL_TXT; /* type */
148 data = type << __ffs(mask);
149
150 mask |= ENDPTCTRL_TXS; /* unstall */
151 mask |= ENDPTCTRL_TXR; /* reset data toggle */
152 data |= ENDPTCTRL_TXR;
153 mask |= ENDPTCTRL_TXE; /* enable */
154 data |= ENDPTCTRL_TXE;
155 } else {
156 mask = ENDPTCTRL_RXT; /* type */
157 data = type << __ffs(mask);
158
159 mask |= ENDPTCTRL_RXS; /* unstall */
160 mask |= ENDPTCTRL_RXR; /* reset data toggle */
161 data |= ENDPTCTRL_RXR;
162 mask |= ENDPTCTRL_RXE; /* enable */
163 data |= ENDPTCTRL_RXE;
164 }
165 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
166 return 0;
167 }
168
169 /**
170 * hw_ep_get_halt: return endpoint halt status
171 * @ci: the controller
172 * @num: endpoint number
173 * @dir: endpoint direction
174 *
175 * This function returns 1 if endpoint halted
176 */
hw_ep_get_halt(struct ci_hdrc * ci,int num,int dir)177 static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
178 {
179 u32 mask = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
180
181 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
182 }
183
184 /**
185 * hw_ep_prime: primes endpoint (execute without interruption)
186 * @ci: the controller
187 * @num: endpoint number
188 * @dir: endpoint direction
189 * @is_ctrl: true if control endpoint
190 *
191 * This function returns an error code
192 */
hw_ep_prime(struct ci_hdrc * ci,int num,int dir,int is_ctrl)193 static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
194 {
195 int n = hw_ep_bit(num, dir);
196
197 /* Synchronize before ep prime */
198 wmb();
199
200 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
201 return -EAGAIN;
202
203 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
204
205 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
206 cpu_relax();
207 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
208 return -EAGAIN;
209
210 /* status shoult be tested according with manual but it doesn't work */
211 return 0;
212 }
213
214 /**
215 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
216 * without interruption)
217 * @ci: the controller
218 * @num: endpoint number
219 * @dir: endpoint direction
220 * @value: true => stall, false => unstall
221 *
222 * This function returns an error code
223 */
hw_ep_set_halt(struct ci_hdrc * ci,int num,int dir,int value)224 static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
225 {
226 if (value != 0 && value != 1)
227 return -EINVAL;
228
229 do {
230 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
231 u32 mask_xs = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
232 u32 mask_xr = (dir == TX) ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
233
234 /* data toggle - reserved for EP0 but it's in ESS */
235 hw_write(ci, reg, mask_xs|mask_xr,
236 value ? mask_xs : mask_xr);
237 } while (value != hw_ep_get_halt(ci, num, dir));
238
239 return 0;
240 }
241
242 /**
243 * hw_port_is_high_speed: test if port is high speed
244 * @ci: the controller
245 *
246 * This function returns true if high speed port
247 */
hw_port_is_high_speed(struct ci_hdrc * ci)248 static int hw_port_is_high_speed(struct ci_hdrc *ci)
249 {
250 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
251 hw_read(ci, OP_PORTSC, PORTSC_HSP);
252 }
253
254 /**
255 * hw_test_and_clear_complete: test & clear complete status (execute without
256 * interruption)
257 * @ci: the controller
258 * @n: endpoint number
259 *
260 * This function returns complete status
261 */
hw_test_and_clear_complete(struct ci_hdrc * ci,int n)262 static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
263 {
264 n = ep_to_bit(ci, n);
265 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
266 }
267
268 /**
269 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
270 * without interruption)
271 * @ci: the controller
272 *
273 * This function returns active interrutps
274 */
hw_test_and_clear_intr_active(struct ci_hdrc * ci)275 static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
276 {
277 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
278
279 hw_write(ci, OP_USBSTS, ~0, reg);
280 return reg;
281 }
282
283 /**
284 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
285 * interruption)
286 * @ci: the controller
287 *
288 * This function returns guard value
289 */
hw_test_and_clear_setup_guard(struct ci_hdrc * ci)290 static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
291 {
292 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
293 }
294
295 /**
296 * hw_test_and_set_setup_guard: test & set setup guard (execute without
297 * interruption)
298 * @ci: the controller
299 *
300 * This function returns guard value
301 */
hw_test_and_set_setup_guard(struct ci_hdrc * ci)302 static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
303 {
304 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
305 }
306
307 /**
308 * hw_usb_set_address: configures USB address (execute without interruption)
309 * @ci: the controller
310 * @value: new USB address
311 *
312 * This function explicitly sets the address, without the "USBADRA" (advance)
313 * feature, which is not supported by older versions of the controller.
314 */
hw_usb_set_address(struct ci_hdrc * ci,u8 value)315 static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
316 {
317 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
318 value << __ffs(DEVICEADDR_USBADR));
319 }
320
321 /**
322 * hw_usb_reset: restart device after a bus reset (execute without
323 * interruption)
324 * @ci: the controller
325 *
326 * This function returns an error code
327 */
hw_usb_reset(struct ci_hdrc * ci)328 static int hw_usb_reset(struct ci_hdrc *ci)
329 {
330 hw_usb_set_address(ci, 0);
331
332 /* ESS flushes only at end?!? */
333 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
334
335 /* clear setup token semaphores */
336 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
337
338 /* clear complete status */
339 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
340
341 /* wait until all bits cleared */
342 while (hw_read(ci, OP_ENDPTPRIME, ~0))
343 udelay(10); /* not RTOS friendly */
344
345 /* reset all endpoints ? */
346
347 /* reset internal status and wait for further instructions
348 no need to verify the port reset status (ESS does it) */
349
350 return 0;
351 }
352
353 /******************************************************************************
354 * UTIL block
355 *****************************************************************************/
356
add_td_to_list(struct ci_hw_ep * hwep,struct ci_hw_req * hwreq,unsigned int length,struct scatterlist * s)357 static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
358 unsigned int length, struct scatterlist *s)
359 {
360 int i;
361 u32 temp;
362 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
363 GFP_ATOMIC);
364
365 if (node == NULL)
366 return -ENOMEM;
367
368 node->ptr = dma_pool_zalloc(hwep->td_pool, GFP_ATOMIC, &node->dma);
369 if (node->ptr == NULL) {
370 kfree(node);
371 return -ENOMEM;
372 }
373
374 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
375 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
376 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
377 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
378 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
379
380 if (hwreq->req.length == 0
381 || hwreq->req.length % hwep->ep.maxpacket)
382 mul++;
383 node->ptr->token |= cpu_to_le32(mul << __ffs(TD_MULTO));
384 }
385
386 if (s) {
387 temp = (u32) (sg_dma_address(s) + hwreq->req.actual);
388 node->td_remaining_size = CI_MAX_BUF_SIZE - length;
389 } else {
390 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
391 }
392
393 if (length) {
394 node->ptr->page[0] = cpu_to_le32(temp);
395 for (i = 1; i < TD_PAGE_COUNT; i++) {
396 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
397 page &= ~TD_RESERVED_MASK;
398 node->ptr->page[i] = cpu_to_le32(page);
399 }
400 }
401
402 hwreq->req.actual += length;
403
404 if (!list_empty(&hwreq->tds)) {
405 /* get the last entry */
406 lastnode = list_entry(hwreq->tds.prev,
407 struct td_node, td);
408 lastnode->ptr->next = cpu_to_le32(node->dma);
409 }
410
411 INIT_LIST_HEAD(&node->td);
412 list_add_tail(&node->td, &hwreq->tds);
413
414 return 0;
415 }
416
417 /**
418 * _usb_addr: calculates endpoint address from direction & number
419 * @ep: endpoint
420 */
_usb_addr(struct ci_hw_ep * ep)421 static inline u8 _usb_addr(struct ci_hw_ep *ep)
422 {
423 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
424 }
425
prepare_td_for_non_sg(struct ci_hw_ep * hwep,struct ci_hw_req * hwreq)426 static int prepare_td_for_non_sg(struct ci_hw_ep *hwep,
427 struct ci_hw_req *hwreq)
428 {
429 unsigned int rest = hwreq->req.length;
430 int pages = TD_PAGE_COUNT;
431 int ret = 0;
432
433 if (rest == 0) {
434 ret = add_td_to_list(hwep, hwreq, 0, NULL);
435 if (ret < 0)
436 return ret;
437 }
438
439 /*
440 * The first buffer could be not page aligned.
441 * In that case we have to span into one extra td.
442 */
443 if (hwreq->req.dma % PAGE_SIZE)
444 pages--;
445
446 while (rest > 0) {
447 unsigned int count = min(hwreq->req.length - hwreq->req.actual,
448 (unsigned int)(pages * CI_HDRC_PAGE_SIZE));
449
450 ret = add_td_to_list(hwep, hwreq, count, NULL);
451 if (ret < 0)
452 return ret;
453
454 rest -= count;
455 }
456
457 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
458 && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
459 ret = add_td_to_list(hwep, hwreq, 0, NULL);
460 if (ret < 0)
461 return ret;
462 }
463
464 return ret;
465 }
466
prepare_td_per_sg(struct ci_hw_ep * hwep,struct ci_hw_req * hwreq,struct scatterlist * s)467 static int prepare_td_per_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
468 struct scatterlist *s)
469 {
470 unsigned int rest = sg_dma_len(s);
471 int ret = 0;
472
473 hwreq->req.actual = 0;
474 while (rest > 0) {
475 unsigned int count = min_t(unsigned int, rest,
476 CI_MAX_BUF_SIZE);
477
478 ret = add_td_to_list(hwep, hwreq, count, s);
479 if (ret < 0)
480 return ret;
481
482 rest -= count;
483 }
484
485 return ret;
486 }
487
ci_add_buffer_entry(struct td_node * node,struct scatterlist * s)488 static void ci_add_buffer_entry(struct td_node *node, struct scatterlist *s)
489 {
490 int empty_td_slot_index = (CI_MAX_BUF_SIZE - node->td_remaining_size)
491 / CI_HDRC_PAGE_SIZE;
492 int i;
493 u32 token;
494
495 token = le32_to_cpu(node->ptr->token) + (sg_dma_len(s) << __ffs(TD_TOTAL_BYTES));
496 node->ptr->token = cpu_to_le32(token);
497
498 for (i = empty_td_slot_index; i < TD_PAGE_COUNT; i++) {
499 u32 page = (u32) sg_dma_address(s) +
500 (i - empty_td_slot_index) * CI_HDRC_PAGE_SIZE;
501
502 page &= ~TD_RESERVED_MASK;
503 node->ptr->page[i] = cpu_to_le32(page);
504 }
505 }
506
prepare_td_for_sg(struct ci_hw_ep * hwep,struct ci_hw_req * hwreq)507 static int prepare_td_for_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
508 {
509 struct usb_request *req = &hwreq->req;
510 struct scatterlist *s = req->sg;
511 int ret = 0, i = 0;
512 struct td_node *node = NULL;
513
514 if (!s || req->zero || req->length == 0) {
515 dev_err(hwep->ci->dev, "not supported operation for sg\n");
516 return -EINVAL;
517 }
518
519 while (i++ < req->num_mapped_sgs) {
520 if (sg_dma_address(s) % PAGE_SIZE) {
521 dev_err(hwep->ci->dev, "not page aligned sg buffer\n");
522 return -EINVAL;
523 }
524
525 if (node && (node->td_remaining_size >= sg_dma_len(s))) {
526 ci_add_buffer_entry(node, s);
527 node->td_remaining_size -= sg_dma_len(s);
528 } else {
529 ret = prepare_td_per_sg(hwep, hwreq, s);
530 if (ret)
531 return ret;
532
533 node = list_entry(hwreq->tds.prev,
534 struct td_node, td);
535 }
536
537 s = sg_next(s);
538 }
539
540 return ret;
541 }
542
543 /**
544 * _hardware_enqueue: configures a request at hardware level
545 * @hwep: endpoint
546 * @hwreq: request
547 *
548 * This function returns an error code
549 */
_hardware_enqueue(struct ci_hw_ep * hwep,struct ci_hw_req * hwreq)550 static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
551 {
552 struct ci_hdrc *ci = hwep->ci;
553 int ret = 0;
554 struct td_node *firstnode, *lastnode;
555
556 /* don't queue twice */
557 if (hwreq->req.status == -EALREADY)
558 return -EALREADY;
559
560 hwreq->req.status = -EALREADY;
561
562 ret = usb_gadget_map_request_by_dev(ci->dev->parent,
563 &hwreq->req, hwep->dir);
564 if (ret)
565 return ret;
566
567 if (hwreq->req.num_mapped_sgs)
568 ret = prepare_td_for_sg(hwep, hwreq);
569 else
570 ret = prepare_td_for_non_sg(hwep, hwreq);
571
572 if (ret)
573 return ret;
574
575 lastnode = list_entry(hwreq->tds.prev,
576 struct td_node, td);
577
578 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
579 if (!hwreq->req.no_interrupt)
580 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
581
582 list_for_each_entry_safe(firstnode, lastnode, &hwreq->tds, td)
583 trace_ci_prepare_td(hwep, hwreq, firstnode);
584
585 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
586
587 wmb();
588
589 hwreq->req.actual = 0;
590 if (!list_empty(&hwep->qh.queue)) {
591 struct ci_hw_req *hwreqprev;
592 int n = hw_ep_bit(hwep->num, hwep->dir);
593 int tmp_stat;
594 struct td_node *prevlastnode;
595 u32 next = firstnode->dma & TD_ADDR_MASK;
596
597 hwreqprev = list_entry(hwep->qh.queue.prev,
598 struct ci_hw_req, queue);
599 prevlastnode = list_entry(hwreqprev->tds.prev,
600 struct td_node, td);
601
602 prevlastnode->ptr->next = cpu_to_le32(next);
603 wmb();
604
605 if (ci->rev == CI_REVISION_22) {
606 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
607 reprime_dtd(ci, hwep, prevlastnode);
608 }
609
610 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
611 goto done;
612 do {
613 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
614 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
615 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
616 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
617 if (tmp_stat)
618 goto done;
619 }
620
621 /* QH configuration */
622 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
623 hwep->qh.ptr->td.token &=
624 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
625
626 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
627 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
628
629 if (hwreq->req.length == 0
630 || hwreq->req.length % hwep->ep.maxpacket)
631 mul++;
632 hwep->qh.ptr->cap |= cpu_to_le32(mul << __ffs(QH_MULT));
633 }
634
635 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
636 hwep->type == USB_ENDPOINT_XFER_CONTROL);
637 done:
638 return ret;
639 }
640
641 /**
642 * free_pending_td: remove a pending request for the endpoint
643 * @hwep: endpoint
644 */
free_pending_td(struct ci_hw_ep * hwep)645 static void free_pending_td(struct ci_hw_ep *hwep)
646 {
647 struct td_node *pending = hwep->pending_td;
648
649 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
650 hwep->pending_td = NULL;
651 kfree(pending);
652 }
653
reprime_dtd(struct ci_hdrc * ci,struct ci_hw_ep * hwep,struct td_node * node)654 static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
655 struct td_node *node)
656 {
657 hwep->qh.ptr->td.next = cpu_to_le32(node->dma);
658 hwep->qh.ptr->td.token &=
659 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
660
661 return hw_ep_prime(ci, hwep->num, hwep->dir,
662 hwep->type == USB_ENDPOINT_XFER_CONTROL);
663 }
664
665 /**
666 * _hardware_dequeue: handles a request at hardware level
667 * @hwep: endpoint
668 * @hwreq: request
669 *
670 * This function returns an error code
671 */
_hardware_dequeue(struct ci_hw_ep * hwep,struct ci_hw_req * hwreq)672 static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
673 {
674 u32 tmptoken;
675 struct td_node *node, *tmpnode;
676 unsigned remaining_length;
677 unsigned actual = hwreq->req.length;
678 struct ci_hdrc *ci = hwep->ci;
679
680 if (hwreq->req.status != -EALREADY)
681 return -EINVAL;
682
683 hwreq->req.status = 0;
684
685 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
686 tmptoken = le32_to_cpu(node->ptr->token);
687 trace_ci_complete_td(hwep, hwreq, node);
688 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
689 int n = hw_ep_bit(hwep->num, hwep->dir);
690
691 if (ci->rev == CI_REVISION_24)
692 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
693 reprime_dtd(ci, hwep, node);
694 hwreq->req.status = -EALREADY;
695 return -EBUSY;
696 }
697
698 remaining_length = (tmptoken & TD_TOTAL_BYTES);
699 remaining_length >>= __ffs(TD_TOTAL_BYTES);
700 actual -= remaining_length;
701
702 hwreq->req.status = tmptoken & TD_STATUS;
703 if ((TD_STATUS_HALTED & hwreq->req.status)) {
704 hwreq->req.status = -EPIPE;
705 break;
706 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
707 hwreq->req.status = -EPROTO;
708 break;
709 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
710 hwreq->req.status = -EILSEQ;
711 break;
712 }
713
714 if (remaining_length) {
715 if (hwep->dir == TX) {
716 hwreq->req.status = -EPROTO;
717 break;
718 }
719 }
720 /*
721 * As the hardware could still address the freed td
722 * which will run the udc unusable, the cleanup of the
723 * td has to be delayed by one.
724 */
725 if (hwep->pending_td)
726 free_pending_td(hwep);
727
728 hwep->pending_td = node;
729 list_del_init(&node->td);
730 }
731
732 usb_gadget_unmap_request_by_dev(hwep->ci->dev->parent,
733 &hwreq->req, hwep->dir);
734
735 hwreq->req.actual += actual;
736
737 if (hwreq->req.status)
738 return hwreq->req.status;
739
740 return hwreq->req.actual;
741 }
742
743 /**
744 * _ep_nuke: dequeues all endpoint requests
745 * @hwep: endpoint
746 *
747 * This function returns an error code
748 * Caller must hold lock
749 */
_ep_nuke(struct ci_hw_ep * hwep)750 static int _ep_nuke(struct ci_hw_ep *hwep)
751 __releases(hwep->lock)
752 __acquires(hwep->lock)
753 {
754 struct td_node *node, *tmpnode;
755 if (hwep == NULL)
756 return -EINVAL;
757
758 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
759
760 while (!list_empty(&hwep->qh.queue)) {
761
762 /* pop oldest request */
763 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
764 struct ci_hw_req, queue);
765
766 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
767 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
768 list_del_init(&node->td);
769 node->ptr = NULL;
770 kfree(node);
771 }
772
773 list_del_init(&hwreq->queue);
774 hwreq->req.status = -ESHUTDOWN;
775
776 if (hwreq->req.complete != NULL) {
777 spin_unlock(hwep->lock);
778 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
779 spin_lock(hwep->lock);
780 }
781 }
782
783 if (hwep->pending_td)
784 free_pending_td(hwep);
785
786 return 0;
787 }
788
_ep_set_halt(struct usb_ep * ep,int value,bool check_transfer)789 static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
790 {
791 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
792 int direction, retval = 0;
793 unsigned long flags;
794
795 if (ep == NULL || hwep->ep.desc == NULL)
796 return -EINVAL;
797
798 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
799 return -EOPNOTSUPP;
800
801 spin_lock_irqsave(hwep->lock, flags);
802
803 if (value && hwep->dir == TX && check_transfer &&
804 !list_empty(&hwep->qh.queue) &&
805 !usb_endpoint_xfer_control(hwep->ep.desc)) {
806 spin_unlock_irqrestore(hwep->lock, flags);
807 return -EAGAIN;
808 }
809
810 direction = hwep->dir;
811 do {
812 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
813
814 if (!value)
815 hwep->wedge = 0;
816
817 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
818 hwep->dir = (hwep->dir == TX) ? RX : TX;
819
820 } while (hwep->dir != direction);
821
822 spin_unlock_irqrestore(hwep->lock, flags);
823 return retval;
824 }
825
826
827 /**
828 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
829 * @gadget: gadget
830 *
831 * This function returns an error code
832 */
_gadget_stop_activity(struct usb_gadget * gadget)833 static int _gadget_stop_activity(struct usb_gadget *gadget)
834 {
835 struct usb_ep *ep;
836 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
837 unsigned long flags;
838
839 /* flush all endpoints */
840 gadget_for_each_ep(ep, gadget) {
841 usb_ep_fifo_flush(ep);
842 }
843 usb_ep_fifo_flush(&ci->ep0out->ep);
844 usb_ep_fifo_flush(&ci->ep0in->ep);
845
846 /* make sure to disable all endpoints */
847 gadget_for_each_ep(ep, gadget) {
848 usb_ep_disable(ep);
849 }
850
851 if (ci->status != NULL) {
852 usb_ep_free_request(&ci->ep0in->ep, ci->status);
853 ci->status = NULL;
854 }
855
856 spin_lock_irqsave(&ci->lock, flags);
857 ci->gadget.speed = USB_SPEED_UNKNOWN;
858 ci->remote_wakeup = 0;
859 ci->suspended = 0;
860 spin_unlock_irqrestore(&ci->lock, flags);
861
862 return 0;
863 }
864
865 /******************************************************************************
866 * ISR block
867 *****************************************************************************/
868 /**
869 * isr_reset_handler: USB reset interrupt handler
870 * @ci: UDC device
871 *
872 * This function resets USB engine after a bus reset occurred
873 */
isr_reset_handler(struct ci_hdrc * ci)874 static void isr_reset_handler(struct ci_hdrc *ci)
875 __releases(ci->lock)
876 __acquires(ci->lock)
877 {
878 int retval;
879 u32 intr;
880
881 spin_unlock(&ci->lock);
882 if (ci->gadget.speed != USB_SPEED_UNKNOWN)
883 usb_gadget_udc_reset(&ci->gadget, ci->driver);
884
885 retval = _gadget_stop_activity(&ci->gadget);
886 if (retval)
887 goto done;
888
889 retval = hw_usb_reset(ci);
890 if (retval)
891 goto done;
892
893 /* clear SLI */
894 hw_write(ci, OP_USBSTS, USBi_SLI, USBi_SLI);
895 intr = hw_read(ci, OP_USBINTR, ~0);
896 hw_write(ci, OP_USBINTR, ~0, intr | USBi_SLI);
897
898 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
899 if (ci->status == NULL)
900 retval = -ENOMEM;
901
902 done:
903 spin_lock(&ci->lock);
904
905 if (retval)
906 dev_err(ci->dev, "error: %i\n", retval);
907 }
908
909 /**
910 * isr_get_status_complete: get_status request complete function
911 * @ep: endpoint
912 * @req: request handled
913 *
914 * Caller must release lock
915 */
isr_get_status_complete(struct usb_ep * ep,struct usb_request * req)916 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
917 {
918 if (ep == NULL || req == NULL)
919 return;
920
921 kfree(req->buf);
922 usb_ep_free_request(ep, req);
923 }
924
925 /**
926 * _ep_queue: queues (submits) an I/O request to an endpoint
927 * @ep: endpoint
928 * @req: request
929 * @gfp_flags: GFP flags (not used)
930 *
931 * Caller must hold lock
932 * This function returns an error code
933 */
_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t __maybe_unused gfp_flags)934 static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
935 gfp_t __maybe_unused gfp_flags)
936 {
937 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
938 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
939 struct ci_hdrc *ci = hwep->ci;
940 int retval = 0;
941
942 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
943 return -EINVAL;
944
945 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
946 if (req->length)
947 hwep = (ci->ep0_dir == RX) ?
948 ci->ep0out : ci->ep0in;
949 if (!list_empty(&hwep->qh.queue)) {
950 _ep_nuke(hwep);
951 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
952 _usb_addr(hwep));
953 }
954 }
955
956 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
957 hwreq->req.length > hwep->ep.mult * hwep->ep.maxpacket) {
958 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
959 return -EMSGSIZE;
960 }
961
962 if (ci->has_short_pkt_limit &&
963 hwreq->req.length > CI_MAX_REQ_SIZE) {
964 dev_err(hwep->ci->dev, "request length too big (max 16KB)\n");
965 return -EMSGSIZE;
966 }
967
968 /* first nuke then test link, e.g. previous status has not sent */
969 if (!list_empty(&hwreq->queue)) {
970 dev_err(hwep->ci->dev, "request already in queue\n");
971 return -EBUSY;
972 }
973
974 /* push request */
975 hwreq->req.status = -EINPROGRESS;
976 hwreq->req.actual = 0;
977
978 retval = _hardware_enqueue(hwep, hwreq);
979
980 if (retval == -EALREADY)
981 retval = 0;
982 if (!retval)
983 list_add_tail(&hwreq->queue, &hwep->qh.queue);
984
985 return retval;
986 }
987
988 /**
989 * isr_get_status_response: get_status request response
990 * @ci: ci struct
991 * @setup: setup request packet
992 *
993 * This function returns an error code
994 */
isr_get_status_response(struct ci_hdrc * ci,struct usb_ctrlrequest * setup)995 static int isr_get_status_response(struct ci_hdrc *ci,
996 struct usb_ctrlrequest *setup)
997 __releases(hwep->lock)
998 __acquires(hwep->lock)
999 {
1000 struct ci_hw_ep *hwep = ci->ep0in;
1001 struct usb_request *req = NULL;
1002 gfp_t gfp_flags = GFP_ATOMIC;
1003 int dir, num, retval;
1004
1005 if (hwep == NULL || setup == NULL)
1006 return -EINVAL;
1007
1008 spin_unlock(hwep->lock);
1009 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
1010 spin_lock(hwep->lock);
1011 if (req == NULL)
1012 return -ENOMEM;
1013
1014 req->complete = isr_get_status_complete;
1015 req->length = 2;
1016 req->buf = kzalloc(req->length, gfp_flags);
1017 if (req->buf == NULL) {
1018 retval = -ENOMEM;
1019 goto err_free_req;
1020 }
1021
1022 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1023 *(u16 *)req->buf = (ci->remote_wakeup << 1) |
1024 ci->gadget.is_selfpowered;
1025 } else if ((setup->bRequestType & USB_RECIP_MASK) \
1026 == USB_RECIP_ENDPOINT) {
1027 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
1028 TX : RX;
1029 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1030 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
1031 }
1032 /* else do nothing; reserved for future use */
1033
1034 retval = _ep_queue(&hwep->ep, req, gfp_flags);
1035 if (retval)
1036 goto err_free_buf;
1037
1038 return 0;
1039
1040 err_free_buf:
1041 kfree(req->buf);
1042 err_free_req:
1043 spin_unlock(hwep->lock);
1044 usb_ep_free_request(&hwep->ep, req);
1045 spin_lock(hwep->lock);
1046 return retval;
1047 }
1048
1049 /**
1050 * isr_setup_status_complete: setup_status request complete function
1051 * @ep: endpoint
1052 * @req: request handled
1053 *
1054 * Caller must release lock. Put the port in test mode if test mode
1055 * feature is selected.
1056 */
1057 static void
isr_setup_status_complete(struct usb_ep * ep,struct usb_request * req)1058 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
1059 {
1060 struct ci_hdrc *ci = req->context;
1061 unsigned long flags;
1062
1063 if (req->status < 0)
1064 return;
1065
1066 if (ci->setaddr) {
1067 hw_usb_set_address(ci, ci->address);
1068 ci->setaddr = false;
1069 if (ci->address)
1070 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
1071 }
1072
1073 spin_lock_irqsave(&ci->lock, flags);
1074 if (ci->test_mode)
1075 hw_port_test_set(ci, ci->test_mode);
1076 spin_unlock_irqrestore(&ci->lock, flags);
1077 }
1078
1079 /**
1080 * isr_setup_status_phase: queues the status phase of a setup transation
1081 * @ci: ci struct
1082 *
1083 * This function returns an error code
1084 */
isr_setup_status_phase(struct ci_hdrc * ci)1085 static int isr_setup_status_phase(struct ci_hdrc *ci)
1086 {
1087 struct ci_hw_ep *hwep;
1088
1089 /*
1090 * Unexpected USB controller behavior, caused by bad signal integrity
1091 * or ground reference problems, can lead to isr_setup_status_phase
1092 * being called with ci->status equal to NULL.
1093 * If this situation occurs, you should review your USB hardware design.
1094 */
1095 if (WARN_ON_ONCE(!ci->status))
1096 return -EPIPE;
1097
1098 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
1099 ci->status->context = ci;
1100 ci->status->complete = isr_setup_status_complete;
1101
1102 return _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
1103 }
1104
1105 /**
1106 * isr_tr_complete_low: transaction complete low level handler
1107 * @hwep: endpoint
1108 *
1109 * This function returns an error code
1110 * Caller must hold lock
1111 */
isr_tr_complete_low(struct ci_hw_ep * hwep)1112 static int isr_tr_complete_low(struct ci_hw_ep *hwep)
1113 __releases(hwep->lock)
1114 __acquires(hwep->lock)
1115 {
1116 struct ci_hw_req *hwreq, *hwreqtemp;
1117 struct ci_hw_ep *hweptemp = hwep;
1118 int retval = 0;
1119
1120 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
1121 queue) {
1122 retval = _hardware_dequeue(hwep, hwreq);
1123 if (retval < 0)
1124 break;
1125 list_del_init(&hwreq->queue);
1126 if (hwreq->req.complete != NULL) {
1127 spin_unlock(hwep->lock);
1128 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
1129 hwreq->req.length)
1130 hweptemp = hwep->ci->ep0in;
1131 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
1132 spin_lock(hwep->lock);
1133 }
1134 }
1135
1136 if (retval == -EBUSY)
1137 retval = 0;
1138
1139 return retval;
1140 }
1141
otg_a_alt_hnp_support(struct ci_hdrc * ci)1142 static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
1143 {
1144 dev_warn(&ci->gadget.dev,
1145 "connect the device to an alternate port if you want HNP\n");
1146 return isr_setup_status_phase(ci);
1147 }
1148
1149 /**
1150 * isr_setup_packet_handler: setup packet handler
1151 * @ci: UDC descriptor
1152 *
1153 * This function handles setup packet
1154 */
isr_setup_packet_handler(struct ci_hdrc * ci)1155 static void isr_setup_packet_handler(struct ci_hdrc *ci)
1156 __releases(ci->lock)
1157 __acquires(ci->lock)
1158 {
1159 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
1160 struct usb_ctrlrequest req;
1161 int type, num, dir, err = -EINVAL;
1162 u8 tmode = 0;
1163
1164 /*
1165 * Flush data and handshake transactions of previous
1166 * setup packet.
1167 */
1168 _ep_nuke(ci->ep0out);
1169 _ep_nuke(ci->ep0in);
1170
1171 /* read_setup_packet */
1172 do {
1173 hw_test_and_set_setup_guard(ci);
1174 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1175 } while (!hw_test_and_clear_setup_guard(ci));
1176
1177 type = req.bRequestType;
1178
1179 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1180
1181 switch (req.bRequest) {
1182 case USB_REQ_CLEAR_FEATURE:
1183 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1184 le16_to_cpu(req.wValue) ==
1185 USB_ENDPOINT_HALT) {
1186 if (req.wLength != 0)
1187 break;
1188 num = le16_to_cpu(req.wIndex);
1189 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1190 num &= USB_ENDPOINT_NUMBER_MASK;
1191 if (dir == TX)
1192 num += ci->hw_ep_max / 2;
1193 if (!ci->ci_hw_ep[num].wedge) {
1194 spin_unlock(&ci->lock);
1195 err = usb_ep_clear_halt(
1196 &ci->ci_hw_ep[num].ep);
1197 spin_lock(&ci->lock);
1198 if (err)
1199 break;
1200 }
1201 err = isr_setup_status_phase(ci);
1202 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1203 le16_to_cpu(req.wValue) ==
1204 USB_DEVICE_REMOTE_WAKEUP) {
1205 if (req.wLength != 0)
1206 break;
1207 ci->remote_wakeup = 0;
1208 err = isr_setup_status_phase(ci);
1209 } else {
1210 goto delegate;
1211 }
1212 break;
1213 case USB_REQ_GET_STATUS:
1214 if ((type != (USB_DIR_IN|USB_RECIP_DEVICE) ||
1215 le16_to_cpu(req.wIndex) == OTG_STS_SELECTOR) &&
1216 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1217 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1218 goto delegate;
1219 if (le16_to_cpu(req.wLength) != 2 ||
1220 le16_to_cpu(req.wValue) != 0)
1221 break;
1222 err = isr_get_status_response(ci, &req);
1223 break;
1224 case USB_REQ_SET_ADDRESS:
1225 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1226 goto delegate;
1227 if (le16_to_cpu(req.wLength) != 0 ||
1228 le16_to_cpu(req.wIndex) != 0)
1229 break;
1230 ci->address = (u8)le16_to_cpu(req.wValue);
1231 ci->setaddr = true;
1232 err = isr_setup_status_phase(ci);
1233 break;
1234 case USB_REQ_SET_FEATURE:
1235 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1236 le16_to_cpu(req.wValue) ==
1237 USB_ENDPOINT_HALT) {
1238 if (req.wLength != 0)
1239 break;
1240 num = le16_to_cpu(req.wIndex);
1241 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1242 num &= USB_ENDPOINT_NUMBER_MASK;
1243 if (dir == TX)
1244 num += ci->hw_ep_max / 2;
1245
1246 spin_unlock(&ci->lock);
1247 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
1248 spin_lock(&ci->lock);
1249 if (!err)
1250 isr_setup_status_phase(ci);
1251 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1252 if (req.wLength != 0)
1253 break;
1254 switch (le16_to_cpu(req.wValue)) {
1255 case USB_DEVICE_REMOTE_WAKEUP:
1256 ci->remote_wakeup = 1;
1257 err = isr_setup_status_phase(ci);
1258 break;
1259 case USB_DEVICE_TEST_MODE:
1260 tmode = le16_to_cpu(req.wIndex) >> 8;
1261 switch (tmode) {
1262 case USB_TEST_J:
1263 case USB_TEST_K:
1264 case USB_TEST_SE0_NAK:
1265 case USB_TEST_PACKET:
1266 case USB_TEST_FORCE_ENABLE:
1267 ci->test_mode = tmode;
1268 err = isr_setup_status_phase(
1269 ci);
1270 break;
1271 default:
1272 break;
1273 }
1274 break;
1275 case USB_DEVICE_B_HNP_ENABLE:
1276 if (ci_otg_is_fsm_mode(ci)) {
1277 ci->gadget.b_hnp_enable = 1;
1278 err = isr_setup_status_phase(
1279 ci);
1280 }
1281 break;
1282 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1283 if (ci_otg_is_fsm_mode(ci))
1284 err = otg_a_alt_hnp_support(ci);
1285 break;
1286 case USB_DEVICE_A_HNP_SUPPORT:
1287 if (ci_otg_is_fsm_mode(ci)) {
1288 ci->gadget.a_hnp_support = 1;
1289 err = isr_setup_status_phase(
1290 ci);
1291 }
1292 break;
1293 default:
1294 goto delegate;
1295 }
1296 } else {
1297 goto delegate;
1298 }
1299 break;
1300 default:
1301 delegate:
1302 if (req.wLength == 0) /* no data phase */
1303 ci->ep0_dir = TX;
1304
1305 spin_unlock(&ci->lock);
1306 err = ci->driver->setup(&ci->gadget, &req);
1307 spin_lock(&ci->lock);
1308 break;
1309 }
1310
1311 if (err < 0) {
1312 spin_unlock(&ci->lock);
1313 if (_ep_set_halt(&hwep->ep, 1, false))
1314 dev_err(ci->dev, "error: _ep_set_halt\n");
1315 spin_lock(&ci->lock);
1316 }
1317 }
1318
1319 /**
1320 * isr_tr_complete_handler: transaction complete interrupt handler
1321 * @ci: UDC descriptor
1322 *
1323 * This function handles traffic events
1324 */
isr_tr_complete_handler(struct ci_hdrc * ci)1325 static void isr_tr_complete_handler(struct ci_hdrc *ci)
1326 __releases(ci->lock)
1327 __acquires(ci->lock)
1328 {
1329 unsigned i;
1330 int err;
1331
1332 for (i = 0; i < ci->hw_ep_max; i++) {
1333 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1334
1335 if (hwep->ep.desc == NULL)
1336 continue; /* not configured */
1337
1338 if (hw_test_and_clear_complete(ci, i)) {
1339 err = isr_tr_complete_low(hwep);
1340 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1341 if (err > 0) /* needs status phase */
1342 err = isr_setup_status_phase(ci);
1343 if (err < 0) {
1344 spin_unlock(&ci->lock);
1345 if (_ep_set_halt(&hwep->ep, 1, false))
1346 dev_err(ci->dev,
1347 "error: _ep_set_halt\n");
1348 spin_lock(&ci->lock);
1349 }
1350 }
1351 }
1352
1353 /* Only handle setup packet below */
1354 if (i == 0 &&
1355 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1356 isr_setup_packet_handler(ci);
1357 }
1358 }
1359
1360 /******************************************************************************
1361 * ENDPT block
1362 *****************************************************************************/
1363 /*
1364 * ep_enable: configure endpoint, making it usable
1365 *
1366 * Check usb_ep_enable() at "usb_gadget.h" for details
1367 */
ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)1368 static int ep_enable(struct usb_ep *ep,
1369 const struct usb_endpoint_descriptor *desc)
1370 {
1371 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1372 int retval = 0;
1373 unsigned long flags;
1374 u32 cap = 0;
1375
1376 if (ep == NULL || desc == NULL)
1377 return -EINVAL;
1378
1379 spin_lock_irqsave(hwep->lock, flags);
1380
1381 /* only internal SW should enable ctrl endpts */
1382
1383 if (!list_empty(&hwep->qh.queue)) {
1384 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1385 spin_unlock_irqrestore(hwep->lock, flags);
1386 return -EBUSY;
1387 }
1388
1389 hwep->ep.desc = desc;
1390
1391 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1392 hwep->num = usb_endpoint_num(desc);
1393 hwep->type = usb_endpoint_type(desc);
1394
1395 hwep->ep.maxpacket = usb_endpoint_maxp(desc);
1396 hwep->ep.mult = usb_endpoint_maxp_mult(desc);
1397
1398 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1399 cap |= QH_IOS;
1400
1401 cap |= QH_ZLT;
1402 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
1403 /*
1404 * For ISO-TX, we set mult at QH as the largest value, and use
1405 * MultO at TD as real mult value.
1406 */
1407 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1408 cap |= 3 << __ffs(QH_MULT);
1409
1410 hwep->qh.ptr->cap = cpu_to_le32(cap);
1411
1412 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
1413
1414 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1415 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1416 retval = -EINVAL;
1417 }
1418
1419 /*
1420 * Enable endpoints in the HW other than ep0 as ep0
1421 * is always enabled
1422 */
1423 if (hwep->num)
1424 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1425 hwep->type);
1426
1427 spin_unlock_irqrestore(hwep->lock, flags);
1428 return retval;
1429 }
1430
1431 /*
1432 * ep_disable: endpoint is no longer usable
1433 *
1434 * Check usb_ep_disable() at "usb_gadget.h" for details
1435 */
ep_disable(struct usb_ep * ep)1436 static int ep_disable(struct usb_ep *ep)
1437 {
1438 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1439 int direction, retval = 0;
1440 unsigned long flags;
1441
1442 if (ep == NULL)
1443 return -EINVAL;
1444 else if (hwep->ep.desc == NULL)
1445 return -EBUSY;
1446
1447 spin_lock_irqsave(hwep->lock, flags);
1448 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1449 spin_unlock_irqrestore(hwep->lock, flags);
1450 return 0;
1451 }
1452
1453 /* only internal SW should disable ctrl endpts */
1454
1455 direction = hwep->dir;
1456 do {
1457 retval |= _ep_nuke(hwep);
1458 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
1459
1460 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1461 hwep->dir = (hwep->dir == TX) ? RX : TX;
1462
1463 } while (hwep->dir != direction);
1464
1465 hwep->ep.desc = NULL;
1466
1467 spin_unlock_irqrestore(hwep->lock, flags);
1468 return retval;
1469 }
1470
1471 /*
1472 * ep_alloc_request: allocate a request object to use with this endpoint
1473 *
1474 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1475 */
ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)1476 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1477 {
1478 struct ci_hw_req *hwreq;
1479
1480 if (ep == NULL)
1481 return NULL;
1482
1483 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
1484 if (hwreq != NULL) {
1485 INIT_LIST_HEAD(&hwreq->queue);
1486 INIT_LIST_HEAD(&hwreq->tds);
1487 }
1488
1489 return (hwreq == NULL) ? NULL : &hwreq->req;
1490 }
1491
1492 /*
1493 * ep_free_request: frees a request object
1494 *
1495 * Check usb_ep_free_request() at "usb_gadget.h" for details
1496 */
ep_free_request(struct usb_ep * ep,struct usb_request * req)1497 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1498 {
1499 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1500 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1501 struct td_node *node, *tmpnode;
1502 unsigned long flags;
1503
1504 if (ep == NULL || req == NULL) {
1505 return;
1506 } else if (!list_empty(&hwreq->queue)) {
1507 dev_err(hwep->ci->dev, "freeing queued request\n");
1508 return;
1509 }
1510
1511 spin_lock_irqsave(hwep->lock, flags);
1512
1513 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1514 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1515 list_del_init(&node->td);
1516 node->ptr = NULL;
1517 kfree(node);
1518 }
1519
1520 kfree(hwreq);
1521
1522 spin_unlock_irqrestore(hwep->lock, flags);
1523 }
1524
1525 /*
1526 * ep_queue: queues (submits) an I/O request to an endpoint
1527 *
1528 * Check usb_ep_queue()* at usb_gadget.h" for details
1529 */
ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t __maybe_unused gfp_flags)1530 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1531 gfp_t __maybe_unused gfp_flags)
1532 {
1533 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1534 int retval = 0;
1535 unsigned long flags;
1536
1537 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
1538 return -EINVAL;
1539
1540 spin_lock_irqsave(hwep->lock, flags);
1541 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1542 spin_unlock_irqrestore(hwep->lock, flags);
1543 return 0;
1544 }
1545 retval = _ep_queue(ep, req, gfp_flags);
1546 spin_unlock_irqrestore(hwep->lock, flags);
1547 return retval;
1548 }
1549
1550 /*
1551 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1552 *
1553 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1554 */
ep_dequeue(struct usb_ep * ep,struct usb_request * req)1555 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1556 {
1557 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1558 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1559 unsigned long flags;
1560 struct td_node *node, *tmpnode;
1561
1562 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1563 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1564 list_empty(&hwep->qh.queue))
1565 return -EINVAL;
1566
1567 spin_lock_irqsave(hwep->lock, flags);
1568 if (hwep->ci->gadget.speed != USB_SPEED_UNKNOWN)
1569 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1570
1571 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1572 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1573 list_del(&node->td);
1574 kfree(node);
1575 }
1576
1577 /* pop request */
1578 list_del_init(&hwreq->queue);
1579
1580 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
1581
1582 req->status = -ECONNRESET;
1583
1584 if (hwreq->req.complete != NULL) {
1585 spin_unlock(hwep->lock);
1586 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
1587 spin_lock(hwep->lock);
1588 }
1589
1590 spin_unlock_irqrestore(hwep->lock, flags);
1591 return 0;
1592 }
1593
1594 /*
1595 * ep_set_halt: sets the endpoint halt feature
1596 *
1597 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1598 */
ep_set_halt(struct usb_ep * ep,int value)1599 static int ep_set_halt(struct usb_ep *ep, int value)
1600 {
1601 return _ep_set_halt(ep, value, true);
1602 }
1603
1604 /*
1605 * ep_set_wedge: sets the halt feature and ignores clear requests
1606 *
1607 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1608 */
ep_set_wedge(struct usb_ep * ep)1609 static int ep_set_wedge(struct usb_ep *ep)
1610 {
1611 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1612 unsigned long flags;
1613
1614 if (ep == NULL || hwep->ep.desc == NULL)
1615 return -EINVAL;
1616
1617 spin_lock_irqsave(hwep->lock, flags);
1618 hwep->wedge = 1;
1619 spin_unlock_irqrestore(hwep->lock, flags);
1620
1621 return usb_ep_set_halt(ep);
1622 }
1623
1624 /*
1625 * ep_fifo_flush: flushes contents of a fifo
1626 *
1627 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1628 */
ep_fifo_flush(struct usb_ep * ep)1629 static void ep_fifo_flush(struct usb_ep *ep)
1630 {
1631 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1632 unsigned long flags;
1633
1634 if (ep == NULL) {
1635 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
1636 return;
1637 }
1638
1639 spin_lock_irqsave(hwep->lock, flags);
1640 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1641 spin_unlock_irqrestore(hwep->lock, flags);
1642 return;
1643 }
1644
1645 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1646
1647 spin_unlock_irqrestore(hwep->lock, flags);
1648 }
1649
1650 /*
1651 * Endpoint-specific part of the API to the USB controller hardware
1652 * Check "usb_gadget.h" for details
1653 */
1654 static const struct usb_ep_ops usb_ep_ops = {
1655 .enable = ep_enable,
1656 .disable = ep_disable,
1657 .alloc_request = ep_alloc_request,
1658 .free_request = ep_free_request,
1659 .queue = ep_queue,
1660 .dequeue = ep_dequeue,
1661 .set_halt = ep_set_halt,
1662 .set_wedge = ep_set_wedge,
1663 .fifo_flush = ep_fifo_flush,
1664 };
1665
1666 /******************************************************************************
1667 * GADGET block
1668 *****************************************************************************/
1669
ci_udc_get_frame(struct usb_gadget * _gadget)1670 static int ci_udc_get_frame(struct usb_gadget *_gadget)
1671 {
1672 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1673 unsigned long flags;
1674 int ret;
1675
1676 spin_lock_irqsave(&ci->lock, flags);
1677 ret = hw_read(ci, OP_FRINDEX, 0x3fff);
1678 spin_unlock_irqrestore(&ci->lock, flags);
1679 return ret >> 3;
1680 }
1681
1682 /*
1683 * ci_hdrc_gadget_connect: caller makes sure gadget driver is binded
1684 */
ci_hdrc_gadget_connect(struct usb_gadget * _gadget,int is_active)1685 static void ci_hdrc_gadget_connect(struct usb_gadget *_gadget, int is_active)
1686 {
1687 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1688
1689 if (is_active) {
1690 pm_runtime_get_sync(ci->dev);
1691 hw_device_reset(ci);
1692 spin_lock_irq(&ci->lock);
1693 if (ci->driver) {
1694 hw_device_state(ci, ci->ep0out->qh.dma);
1695 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
1696 spin_unlock_irq(&ci->lock);
1697 usb_udc_vbus_handler(_gadget, true);
1698 } else {
1699 spin_unlock_irq(&ci->lock);
1700 }
1701 } else {
1702 usb_udc_vbus_handler(_gadget, false);
1703 if (ci->driver)
1704 ci->driver->disconnect(&ci->gadget);
1705 hw_device_state(ci, 0);
1706 if (ci->platdata->notify_event)
1707 ci->platdata->notify_event(ci,
1708 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1709 _gadget_stop_activity(&ci->gadget);
1710 pm_runtime_put_sync(ci->dev);
1711 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
1712 }
1713 }
1714
ci_udc_vbus_session(struct usb_gadget * _gadget,int is_active)1715 static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1716 {
1717 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1718 unsigned long flags;
1719 int ret = 0;
1720
1721 spin_lock_irqsave(&ci->lock, flags);
1722 ci->vbus_active = is_active;
1723 spin_unlock_irqrestore(&ci->lock, flags);
1724
1725 if (ci->usb_phy)
1726 usb_phy_set_charger_state(ci->usb_phy, is_active ?
1727 USB_CHARGER_PRESENT : USB_CHARGER_ABSENT);
1728
1729 if (ci->platdata->notify_event)
1730 ret = ci->platdata->notify_event(ci,
1731 CI_HDRC_CONTROLLER_VBUS_EVENT);
1732
1733 if (ci->usb_phy) {
1734 if (is_active)
1735 usb_phy_set_event(ci->usb_phy, USB_EVENT_VBUS);
1736 else
1737 usb_phy_set_event(ci->usb_phy, USB_EVENT_NONE);
1738 }
1739
1740 if (ci->driver)
1741 ci_hdrc_gadget_connect(_gadget, is_active);
1742
1743 return ret;
1744 }
1745
ci_udc_wakeup(struct usb_gadget * _gadget)1746 static int ci_udc_wakeup(struct usb_gadget *_gadget)
1747 {
1748 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1749 unsigned long flags;
1750 int ret = 0;
1751
1752 spin_lock_irqsave(&ci->lock, flags);
1753 if (ci->gadget.speed == USB_SPEED_UNKNOWN) {
1754 spin_unlock_irqrestore(&ci->lock, flags);
1755 return 0;
1756 }
1757 if (!ci->remote_wakeup) {
1758 ret = -EOPNOTSUPP;
1759 goto out;
1760 }
1761 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1762 ret = -EINVAL;
1763 goto out;
1764 }
1765 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1766 out:
1767 spin_unlock_irqrestore(&ci->lock, flags);
1768 return ret;
1769 }
1770
ci_udc_vbus_draw(struct usb_gadget * _gadget,unsigned ma)1771 static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1772 {
1773 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1774
1775 if (ci->usb_phy)
1776 return usb_phy_set_power(ci->usb_phy, ma);
1777 return -ENOTSUPP;
1778 }
1779
ci_udc_selfpowered(struct usb_gadget * _gadget,int is_on)1780 static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
1781 {
1782 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1783 struct ci_hw_ep *hwep = ci->ep0in;
1784 unsigned long flags;
1785
1786 spin_lock_irqsave(hwep->lock, flags);
1787 _gadget->is_selfpowered = (is_on != 0);
1788 spin_unlock_irqrestore(hwep->lock, flags);
1789
1790 return 0;
1791 }
1792
1793 /* Change Data+ pullup status
1794 * this func is used by usb_gadget_connect/disconnect
1795 */
ci_udc_pullup(struct usb_gadget * _gadget,int is_on)1796 static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1797 {
1798 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1799
1800 /*
1801 * Data+ pullup controlled by OTG state machine in OTG fsm mode;
1802 * and don't touch Data+ in host mode for dual role config.
1803 */
1804 if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST)
1805 return 0;
1806
1807 pm_runtime_get_sync(ci->dev);
1808 if (is_on)
1809 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1810 else
1811 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1812 pm_runtime_put_sync(ci->dev);
1813
1814 return 0;
1815 }
1816
1817 static int ci_udc_start(struct usb_gadget *gadget,
1818 struct usb_gadget_driver *driver);
1819 static int ci_udc_stop(struct usb_gadget *gadget);
1820
1821 /* Match ISOC IN from the highest endpoint */
ci_udc_match_ep(struct usb_gadget * gadget,struct usb_endpoint_descriptor * desc,struct usb_ss_ep_comp_descriptor * comp_desc)1822 static struct usb_ep *ci_udc_match_ep(struct usb_gadget *gadget,
1823 struct usb_endpoint_descriptor *desc,
1824 struct usb_ss_ep_comp_descriptor *comp_desc)
1825 {
1826 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1827 struct usb_ep *ep;
1828
1829 if (usb_endpoint_xfer_isoc(desc) && usb_endpoint_dir_in(desc)) {
1830 list_for_each_entry_reverse(ep, &ci->gadget.ep_list, ep_list) {
1831 if (ep->caps.dir_in && !ep->claimed)
1832 return ep;
1833 }
1834 }
1835
1836 return NULL;
1837 }
1838
1839 /*
1840 * Device operations part of the API to the USB controller hardware,
1841 * which don't involve endpoints (or i/o)
1842 * Check "usb_gadget.h" for details
1843 */
1844 static const struct usb_gadget_ops usb_gadget_ops = {
1845 .get_frame = ci_udc_get_frame,
1846 .vbus_session = ci_udc_vbus_session,
1847 .wakeup = ci_udc_wakeup,
1848 .set_selfpowered = ci_udc_selfpowered,
1849 .pullup = ci_udc_pullup,
1850 .vbus_draw = ci_udc_vbus_draw,
1851 .udc_start = ci_udc_start,
1852 .udc_stop = ci_udc_stop,
1853 .match_ep = ci_udc_match_ep,
1854 };
1855
init_eps(struct ci_hdrc * ci)1856 static int init_eps(struct ci_hdrc *ci)
1857 {
1858 int retval = 0, i, j;
1859
1860 for (i = 0; i < ci->hw_ep_max/2; i++)
1861 for (j = RX; j <= TX; j++) {
1862 int k = i + j * ci->hw_ep_max/2;
1863 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
1864
1865 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
1866 (j == TX) ? "in" : "out");
1867
1868 hwep->ci = ci;
1869 hwep->lock = &ci->lock;
1870 hwep->td_pool = ci->td_pool;
1871
1872 hwep->ep.name = hwep->name;
1873 hwep->ep.ops = &usb_ep_ops;
1874
1875 if (i == 0) {
1876 hwep->ep.caps.type_control = true;
1877 } else {
1878 hwep->ep.caps.type_iso = true;
1879 hwep->ep.caps.type_bulk = true;
1880 hwep->ep.caps.type_int = true;
1881 }
1882
1883 if (j == TX)
1884 hwep->ep.caps.dir_in = true;
1885 else
1886 hwep->ep.caps.dir_out = true;
1887
1888 /*
1889 * for ep0: maxP defined in desc, for other
1890 * eps, maxP is set by epautoconfig() called
1891 * by gadget layer
1892 */
1893 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
1894
1895 INIT_LIST_HEAD(&hwep->qh.queue);
1896 hwep->qh.ptr = dma_pool_zalloc(ci->qh_pool, GFP_KERNEL,
1897 &hwep->qh.dma);
1898 if (hwep->qh.ptr == NULL)
1899 retval = -ENOMEM;
1900
1901 /*
1902 * set up shorthands for ep0 out and in endpoints,
1903 * don't add to gadget's ep_list
1904 */
1905 if (i == 0) {
1906 if (j == RX)
1907 ci->ep0out = hwep;
1908 else
1909 ci->ep0in = hwep;
1910
1911 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
1912 continue;
1913 }
1914
1915 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
1916 }
1917
1918 return retval;
1919 }
1920
destroy_eps(struct ci_hdrc * ci)1921 static void destroy_eps(struct ci_hdrc *ci)
1922 {
1923 int i;
1924
1925 for (i = 0; i < ci->hw_ep_max; i++) {
1926 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1927
1928 if (hwep->pending_td)
1929 free_pending_td(hwep);
1930 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
1931 }
1932 }
1933
1934 /**
1935 * ci_udc_start: register a gadget driver
1936 * @gadget: our gadget
1937 * @driver: the driver being registered
1938 *
1939 * Interrupts are enabled here.
1940 */
ci_udc_start(struct usb_gadget * gadget,struct usb_gadget_driver * driver)1941 static int ci_udc_start(struct usb_gadget *gadget,
1942 struct usb_gadget_driver *driver)
1943 {
1944 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1945 int retval;
1946
1947 if (driver->disconnect == NULL)
1948 return -EINVAL;
1949
1950 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1951 retval = usb_ep_enable(&ci->ep0out->ep);
1952 if (retval)
1953 return retval;
1954
1955 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1956 retval = usb_ep_enable(&ci->ep0in->ep);
1957 if (retval)
1958 return retval;
1959
1960 ci->driver = driver;
1961
1962 /* Start otg fsm for B-device */
1963 if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) {
1964 ci_hdrc_otg_fsm_start(ci);
1965 return retval;
1966 }
1967
1968 if (ci->vbus_active)
1969 ci_hdrc_gadget_connect(gadget, 1);
1970 else
1971 usb_udc_vbus_handler(&ci->gadget, false);
1972
1973 return retval;
1974 }
1975
ci_udc_stop_for_otg_fsm(struct ci_hdrc * ci)1976 static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci)
1977 {
1978 if (!ci_otg_is_fsm_mode(ci))
1979 return;
1980
1981 mutex_lock(&ci->fsm.lock);
1982 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
1983 ci->fsm.a_bidl_adis_tmout = 1;
1984 ci_hdrc_otg_fsm_start(ci);
1985 } else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
1986 ci->fsm.protocol = PROTO_UNDEF;
1987 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
1988 }
1989 mutex_unlock(&ci->fsm.lock);
1990 }
1991
1992 /*
1993 * ci_udc_stop: unregister a gadget driver
1994 */
ci_udc_stop(struct usb_gadget * gadget)1995 static int ci_udc_stop(struct usb_gadget *gadget)
1996 {
1997 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1998 unsigned long flags;
1999
2000 spin_lock_irqsave(&ci->lock, flags);
2001 ci->driver = NULL;
2002
2003 if (ci->vbus_active) {
2004 hw_device_state(ci, 0);
2005 spin_unlock_irqrestore(&ci->lock, flags);
2006 if (ci->platdata->notify_event)
2007 ci->platdata->notify_event(ci,
2008 CI_HDRC_CONTROLLER_STOPPED_EVENT);
2009 _gadget_stop_activity(&ci->gadget);
2010 spin_lock_irqsave(&ci->lock, flags);
2011 pm_runtime_put(ci->dev);
2012 }
2013
2014 spin_unlock_irqrestore(&ci->lock, flags);
2015
2016 ci_udc_stop_for_otg_fsm(ci);
2017 return 0;
2018 }
2019
2020 /******************************************************************************
2021 * BUS block
2022 *****************************************************************************/
2023 /*
2024 * udc_irq: ci interrupt handler
2025 *
2026 * This function returns IRQ_HANDLED if the IRQ has been handled
2027 * It locks access to registers
2028 */
udc_irq(struct ci_hdrc * ci)2029 static irqreturn_t udc_irq(struct ci_hdrc *ci)
2030 {
2031 irqreturn_t retval;
2032 u32 intr;
2033
2034 if (ci == NULL)
2035 return IRQ_HANDLED;
2036
2037 spin_lock(&ci->lock);
2038
2039 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
2040 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
2041 USBMODE_CM_DC) {
2042 spin_unlock(&ci->lock);
2043 return IRQ_NONE;
2044 }
2045 }
2046 intr = hw_test_and_clear_intr_active(ci);
2047
2048 if (intr) {
2049 /* order defines priority - do NOT change it */
2050 if (USBi_URI & intr)
2051 isr_reset_handler(ci);
2052
2053 if (USBi_PCI & intr) {
2054 ci->gadget.speed = hw_port_is_high_speed(ci) ?
2055 USB_SPEED_HIGH : USB_SPEED_FULL;
2056 if (ci->usb_phy)
2057 usb_phy_set_event(ci->usb_phy,
2058 USB_EVENT_ENUMERATED);
2059 if (ci->suspended) {
2060 if (ci->driver->resume) {
2061 spin_unlock(&ci->lock);
2062 ci->driver->resume(&ci->gadget);
2063 spin_lock(&ci->lock);
2064 }
2065 ci->suspended = 0;
2066 usb_gadget_set_state(&ci->gadget,
2067 ci->resume_state);
2068 }
2069 }
2070
2071 if ((USBi_UI | USBi_UEI) & intr)
2072 isr_tr_complete_handler(ci);
2073
2074 if ((USBi_SLI & intr) && !(ci->suspended)) {
2075 ci->suspended = 1;
2076 ci->resume_state = ci->gadget.state;
2077 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
2078 ci->driver->suspend) {
2079 spin_unlock(&ci->lock);
2080 ci->driver->suspend(&ci->gadget);
2081 spin_lock(&ci->lock);
2082 }
2083 usb_gadget_set_state(&ci->gadget,
2084 USB_STATE_SUSPENDED);
2085 }
2086 retval = IRQ_HANDLED;
2087 } else {
2088 retval = IRQ_NONE;
2089 }
2090 spin_unlock(&ci->lock);
2091
2092 return retval;
2093 }
2094
2095 /**
2096 * udc_start: initialize gadget role
2097 * @ci: chipidea controller
2098 */
udc_start(struct ci_hdrc * ci)2099 static int udc_start(struct ci_hdrc *ci)
2100 {
2101 struct device *dev = ci->dev;
2102 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
2103 int retval = 0;
2104
2105 ci->gadget.ops = &usb_gadget_ops;
2106 ci->gadget.speed = USB_SPEED_UNKNOWN;
2107 ci->gadget.max_speed = USB_SPEED_HIGH;
2108 ci->gadget.name = ci->platdata->name;
2109 ci->gadget.otg_caps = otg_caps;
2110 ci->gadget.sg_supported = 1;
2111 ci->gadget.irq = ci->irq;
2112
2113 if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA)
2114 ci->gadget.quirk_avoids_skb_reserve = 1;
2115
2116 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
2117 otg_caps->adp_support))
2118 ci->gadget.is_otg = 1;
2119
2120 INIT_LIST_HEAD(&ci->gadget.ep_list);
2121
2122 /* alloc resources */
2123 ci->qh_pool = dma_pool_create("ci_hw_qh", dev->parent,
2124 sizeof(struct ci_hw_qh),
2125 64, CI_HDRC_PAGE_SIZE);
2126 if (ci->qh_pool == NULL)
2127 return -ENOMEM;
2128
2129 ci->td_pool = dma_pool_create("ci_hw_td", dev->parent,
2130 sizeof(struct ci_hw_td),
2131 64, CI_HDRC_PAGE_SIZE);
2132 if (ci->td_pool == NULL) {
2133 retval = -ENOMEM;
2134 goto free_qh_pool;
2135 }
2136
2137 retval = init_eps(ci);
2138 if (retval)
2139 goto free_pools;
2140
2141 ci->gadget.ep0 = &ci->ep0in->ep;
2142
2143 retval = usb_add_gadget_udc(dev, &ci->gadget);
2144 if (retval)
2145 goto destroy_eps;
2146
2147 return retval;
2148
2149 destroy_eps:
2150 destroy_eps(ci);
2151 free_pools:
2152 dma_pool_destroy(ci->td_pool);
2153 free_qh_pool:
2154 dma_pool_destroy(ci->qh_pool);
2155 return retval;
2156 }
2157
2158 /*
2159 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
2160 *
2161 * No interrupts active, the IRQ has been released
2162 */
ci_hdrc_gadget_destroy(struct ci_hdrc * ci)2163 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
2164 {
2165 if (!ci->roles[CI_ROLE_GADGET])
2166 return;
2167
2168 usb_del_gadget_udc(&ci->gadget);
2169
2170 destroy_eps(ci);
2171
2172 dma_pool_destroy(ci->td_pool);
2173 dma_pool_destroy(ci->qh_pool);
2174 }
2175
udc_id_switch_for_device(struct ci_hdrc * ci)2176 static int udc_id_switch_for_device(struct ci_hdrc *ci)
2177 {
2178 if (ci->platdata->pins_device)
2179 pinctrl_select_state(ci->platdata->pctl,
2180 ci->platdata->pins_device);
2181
2182 if (ci->is_otg)
2183 /* Clear and enable BSV irq */
2184 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
2185 OTGSC_BSVIS | OTGSC_BSVIE);
2186
2187 return 0;
2188 }
2189
udc_id_switch_for_host(struct ci_hdrc * ci)2190 static void udc_id_switch_for_host(struct ci_hdrc *ci)
2191 {
2192 /*
2193 * host doesn't care B_SESSION_VALID event
2194 * so clear and disable BSV irq
2195 */
2196 if (ci->is_otg)
2197 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
2198
2199 ci->vbus_active = 0;
2200
2201 if (ci->platdata->pins_device && ci->platdata->pins_default)
2202 pinctrl_select_state(ci->platdata->pctl,
2203 ci->platdata->pins_default);
2204 }
2205
2206 #ifdef CONFIG_PM_SLEEP
udc_suspend(struct ci_hdrc * ci)2207 static void udc_suspend(struct ci_hdrc *ci)
2208 {
2209 /*
2210 * Set OP_ENDPTLISTADDR to be non-zero for
2211 * checking if controller resume from power lost
2212 * in non-host mode.
2213 */
2214 if (hw_read(ci, OP_ENDPTLISTADDR, ~0) == 0)
2215 hw_write(ci, OP_ENDPTLISTADDR, ~0, ~0);
2216 }
2217
udc_resume(struct ci_hdrc * ci,bool power_lost)2218 static void udc_resume(struct ci_hdrc *ci, bool power_lost)
2219 {
2220 if (power_lost) {
2221 if (ci->is_otg)
2222 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
2223 OTGSC_BSVIS | OTGSC_BSVIE);
2224 if (ci->vbus_active)
2225 usb_gadget_vbus_disconnect(&ci->gadget);
2226 }
2227
2228 /* Restore value 0 if it was set for power lost check */
2229 if (hw_read(ci, OP_ENDPTLISTADDR, ~0) == 0xFFFFFFFF)
2230 hw_write(ci, OP_ENDPTLISTADDR, ~0, 0);
2231 }
2232 #endif
2233
2234 /**
2235 * ci_hdrc_gadget_init - initialize device related bits
2236 * @ci: the controller
2237 *
2238 * This function initializes the gadget, if the device is "device capable".
2239 */
ci_hdrc_gadget_init(struct ci_hdrc * ci)2240 int ci_hdrc_gadget_init(struct ci_hdrc *ci)
2241 {
2242 struct ci_role_driver *rdrv;
2243 int ret;
2244
2245 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
2246 return -ENXIO;
2247
2248 rdrv = devm_kzalloc(ci->dev, sizeof(*rdrv), GFP_KERNEL);
2249 if (!rdrv)
2250 return -ENOMEM;
2251
2252 rdrv->start = udc_id_switch_for_device;
2253 rdrv->stop = udc_id_switch_for_host;
2254 #ifdef CONFIG_PM_SLEEP
2255 rdrv->suspend = udc_suspend;
2256 rdrv->resume = udc_resume;
2257 #endif
2258 rdrv->irq = udc_irq;
2259 rdrv->name = "gadget";
2260
2261 ret = udc_start(ci);
2262 if (!ret)
2263 ci->roles[CI_ROLE_GADGET] = rdrv;
2264
2265 return ret;
2266 }
2267