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