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