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