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