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