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