Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Renesas USB driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/usb.h> |
| 23 | #include <linux/usb/hcd.h> |
| 24 | #include "common.h" |
| 25 | |
| 26 | /* |
| 27 | *** HARDWARE LIMITATION *** |
| 28 | * |
| 29 | * 1) renesas_usbhs has a limited number of controllable devices. |
| 30 | * it can control only 9 devices in generally. |
| 31 | * see DEVADDn / DCPMAXP / PIPEMAXP. |
| 32 | * |
| 33 | * 2) renesas_usbhs pipe number is limited. |
| 34 | * the pipe will be re-used for each devices. |
| 35 | * so, software should control DATA0/1 sequence of each devices. |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * image of mod_host |
| 41 | * |
| 42 | * +--------+ |
| 43 | * | udev 0 | --> it is used when set address |
| 44 | * +--------+ |
| 45 | * |
| 46 | * +--------+ pipes are reused for each uep. |
| 47 | * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 48 | * +--------+ | | other device requested |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 49 | * +- [uep 1 (bulk)] --|---+ +--------------+ |
| 50 | * | +--------------> | pipe0 (dcp) | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 51 | * +- [uep 2 (bulk)] -@ | +--------------+ |
| 52 | * | | pipe1 (isoc) | |
| 53 | * +--------+ | +--------------+ |
| 54 | * | udev 2 |-+- [uep 0 (dcp) ] -@ +----------> | pipe2 (bulk) | |
| 55 | * +--------+ | +--------------+ |
| 56 | * +- [uep 1 (int) ] ----+ +------> | pipe3 (bulk) | |
| 57 | * | | +--------------+ |
| 58 | * +--------+ +-----|------> | pipe4 (int) | |
| 59 | * | udev 3 |-+- [uep 0 (dcp) ] -@ | +--------------+ |
| 60 | * +--------+ | | | .... | |
| 61 | * +- [uep 1 (bulk)] -@ | | .... | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 62 | * | | |
| 63 | * +- [uep 2 (bulk)]-----------+ |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 64 | * |
| 65 | * @ : uep requested free pipe, but all have been used. |
| 66 | * now it is waiting for free pipe |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 67 | */ |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * struct |
| 72 | */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 73 | struct usbhsh_request { |
| 74 | struct urb *urb; |
| 75 | struct usbhs_pkt pkt; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct usbhsh_device { |
| 79 | struct usb_device *usbv; |
| 80 | struct list_head ep_list_head; /* list of usbhsh_ep */ |
| 81 | }; |
| 82 | |
| 83 | struct usbhsh_ep { |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 84 | struct usbhs_pipe *pipe; /* attached pipe */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 85 | struct usbhsh_device *udev; /* attached udev */ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 86 | struct usb_host_endpoint *ep; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 87 | struct list_head ep_list; /* list to usbhsh_device */ |
Kuninori Morimoto | 7b332e5 | 2012-11-06 00:11:53 -0800 | [diff] [blame] | 88 | unsigned int counter; /* pipe attach counter */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */ |
| 92 | #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */ |
| 93 | struct usbhsh_hpriv { |
| 94 | struct usbhs_mod mod; |
| 95 | struct usbhs_pipe *dcp; |
| 96 | |
| 97 | struct usbhsh_device udev[USBHSH_DEVICE_MAX]; |
| 98 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 99 | u32 port_stat; /* USB_PORT_STAT_xxx */ |
| 100 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 101 | struct completion setup_ack_done; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | |
| 105 | static const char usbhsh_hcd_name[] = "renesas_usbhs host"; |
| 106 | |
| 107 | /* |
| 108 | * macro |
| 109 | */ |
| 110 | #define usbhsh_priv_to_hpriv(priv) \ |
| 111 | container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod) |
| 112 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 113 | #define __usbhsh_for_each_udev(start, pos, h, i) \ |
Kuninori Morimoto | 925403f | 2013-07-11 22:32:31 -0700 | [diff] [blame] | 114 | for ((i) = start; \ |
| 115 | ((i) < USBHSH_DEVICE_MAX) && ((pos) = (h)->udev + (i)); \ |
| 116 | (i)++) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 117 | |
| 118 | #define usbhsh_for_each_udev(pos, hpriv, i) \ |
| 119 | __usbhsh_for_each_udev(1, pos, hpriv, i) |
| 120 | |
| 121 | #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \ |
| 122 | __usbhsh_for_each_udev(0, pos, hpriv, i) |
| 123 | |
| 124 | #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv) |
| 125 | #define usbhsh_hcd_to_dev(h) ((h)->self.controller) |
| 126 | |
| 127 | #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv) |
| 128 | #define usbhsh_hpriv_to_dcp(h) ((h)->dcp) |
| 129 | #define usbhsh_hpriv_to_hcd(h) \ |
| 130 | container_of((void *)h, struct usb_hcd, hcd_priv) |
| 131 | |
| 132 | #define usbhsh_ep_to_uep(u) ((u)->hcpriv) |
| 133 | #define usbhsh_uep_to_pipe(u) ((u)->pipe) |
| 134 | #define usbhsh_uep_to_udev(u) ((u)->udev) |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 135 | #define usbhsh_uep_to_ep(u) ((u)->ep) |
| 136 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 137 | #define usbhsh_urb_to_ureq(u) ((u)->hcpriv) |
| 138 | #define usbhsh_urb_to_usbv(u) ((u)->dev) |
| 139 | |
| 140 | #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev) |
| 141 | |
| 142 | #define usbhsh_udev_to_usbv(h) ((h)->usbv) |
Kuninori Morimoto | ab14230 | 2011-10-31 00:48:00 -0700 | [diff] [blame] | 143 | #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 144 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 145 | #define usbhsh_pipe_to_uep(p) ((p)->mod_private) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 146 | |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 147 | #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent)) |
| 148 | #define usbhsh_device_hubport(d) ((d)->usbv->portnum) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 149 | #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev)) |
| 150 | #define usbhsh_device_nth(h, d) ((h)->udev + d) |
| 151 | #define usbhsh_device0(h) usbhsh_device_nth(h, 0) |
| 152 | |
| 153 | #define usbhsh_port_stat_init(h) ((h)->port_stat = 0) |
| 154 | #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s)) |
| 155 | #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s)) |
| 156 | #define usbhsh_port_stat_get(h) ((h)->port_stat) |
| 157 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 158 | #define usbhsh_pkt_to_ureq(p) \ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 159 | container_of((void *)p, struct usbhsh_request, pkt) |
| 160 | |
| 161 | /* |
| 162 | * req alloc/free |
| 163 | */ |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 164 | static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 165 | struct urb *urb, |
| 166 | gfp_t mem_flags) |
| 167 | { |
| 168 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 169 | |
Kuninori Morimoto | c5b963f | 2011-10-31 00:47:44 -0700 | [diff] [blame] | 170 | ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags); |
Wolfram Sang | 93b6cb4 | 2016-08-25 19:39:29 +0200 | [diff] [blame] | 171 | if (!ureq) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 172 | return NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 173 | |
| 174 | usbhs_pkt_init(&ureq->pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 175 | ureq->urb = urb; |
Kuninori Morimoto | fc9d5c7 | 2011-10-31 00:47:01 -0700 | [diff] [blame] | 176 | usbhsh_urb_to_ureq(urb) = ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 177 | |
| 178 | return ureq; |
| 179 | } |
| 180 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 181 | static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 182 | struct usbhsh_request *ureq) |
| 183 | { |
Kuninori Morimoto | fc9d5c7 | 2011-10-31 00:47:01 -0700 | [diff] [blame] | 184 | usbhsh_urb_to_ureq(ureq->urb) = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 185 | ureq->urb = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 186 | |
Kuninori Morimoto | c5b963f | 2011-10-31 00:47:44 -0700 | [diff] [blame] | 187 | kfree(ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 191 | * status |
| 192 | */ |
| 193 | static int usbhsh_is_running(struct usbhsh_hpriv *hpriv) |
| 194 | { |
| 195 | /* |
| 196 | * we can decide some device is attached or not |
| 197 | * by checking mod.irq_attch |
| 198 | * see |
| 199 | * usbhsh_irq_attch() |
| 200 | * usbhsh_irq_dtch() |
| 201 | */ |
| 202 | return (hpriv->mod.irq_attch == NULL); |
| 203 | } |
| 204 | |
| 205 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 206 | * pipe control |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 207 | */ |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 208 | static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv, |
| 209 | struct urb *urb, |
| 210 | struct usbhs_pkt *pkt) |
| 211 | { |
| 212 | int len = urb->actual_length; |
| 213 | int maxp = usb_endpoint_maxp(&urb->ep->desc); |
| 214 | int t = 0; |
| 215 | |
| 216 | /* DCP is out of sequence control */ |
| 217 | if (usb_pipecontrol(urb->pipe)) |
| 218 | return; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 219 | |
| 220 | /* |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 221 | * renesas_usbhs pipe has a limitation in a number. |
| 222 | * So, driver should re-use the limited pipe for each device/endpoint. |
| 223 | * DATA0/1 sequence should be saved for it. |
| 224 | * see [image of mod_host] |
| 225 | * [HARDWARE LIMITATION] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 226 | */ |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * next sequence depends on actual_length |
| 230 | * |
| 231 | * ex) actual_length = 1147, maxp = 512 |
| 232 | * data0 : 512 |
| 233 | * data1 : 512 |
| 234 | * data0 : 123 |
| 235 | * data1 is the next sequence |
| 236 | */ |
| 237 | t = len / maxp; |
| 238 | if (len % maxp) |
| 239 | t++; |
| 240 | if (pkt->zero) |
| 241 | t++; |
| 242 | t %= 2; |
| 243 | |
| 244 | if (t) |
| 245 | usb_dotoggle(urb->dev, |
| 246 | usb_pipeendpoint(urb->pipe), |
| 247 | usb_pipeout(urb->pipe)); |
| 248 | } |
| 249 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 250 | static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv, |
| 251 | struct urb *urb); |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 252 | |
| 253 | static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv, |
| 254 | struct urb *urb) |
| 255 | { |
| 256 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 257 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 258 | struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); |
| 259 | struct usbhs_pipe *pipe; |
| 260 | struct usb_endpoint_descriptor *desc = &urb->ep->desc; |
| 261 | struct device *dev = usbhs_priv_to_dev(priv); |
| 262 | unsigned long flags; |
| 263 | int dir_in_req = !!usb_pipein(urb->pipe); |
| 264 | int is_dcp = usb_endpoint_xfer_control(desc); |
| 265 | int i, dir_in; |
| 266 | int ret = -EBUSY; |
| 267 | |
| 268 | /******************** spin lock ********************/ |
| 269 | usbhs_lock(priv, flags); |
| 270 | |
Kuninori Morimoto | 7b332e5 | 2012-11-06 00:11:53 -0800 | [diff] [blame] | 271 | /* |
| 272 | * if uep has been attached to pipe, |
| 273 | * reuse it |
| 274 | */ |
| 275 | if (usbhsh_uep_to_pipe(uep)) { |
| 276 | ret = 0; |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 277 | goto usbhsh_pipe_attach_done; |
| 278 | } |
| 279 | |
| 280 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 281 | |
| 282 | /* check pipe type */ |
| 283 | if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc))) |
| 284 | continue; |
| 285 | |
| 286 | /* check pipe direction if normal pipe */ |
| 287 | if (!is_dcp) { |
| 288 | dir_in = !!usbhs_pipe_is_dir_in(pipe); |
| 289 | if (0 != (dir_in - dir_in_req)) |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | /* check pipe is free */ |
| 294 | if (usbhsh_pipe_to_uep(pipe)) |
| 295 | continue; |
| 296 | |
| 297 | /* |
| 298 | * attach pipe to uep |
| 299 | * |
| 300 | * usbhs_pipe_config_update() should be called after |
| 301 | * usbhs_set_device_config() |
| 302 | * see |
| 303 | * DCPMAXP/PIPEMAXP |
| 304 | */ |
| 305 | usbhsh_uep_to_pipe(uep) = pipe; |
| 306 | usbhsh_pipe_to_uep(pipe) = uep; |
| 307 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 308 | usbhs_pipe_config_update(pipe, |
| 309 | usbhsh_device_number(hpriv, udev), |
| 310 | usb_endpoint_num(desc), |
| 311 | usb_endpoint_maxp(desc)); |
| 312 | |
| 313 | dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__, |
| 314 | usbhsh_device_number(hpriv, udev), |
| 315 | usb_endpoint_num(desc), |
| 316 | usbhs_pipe_name(pipe), |
| 317 | dir_in_req ? "in" : "out"); |
| 318 | |
| 319 | ret = 0; |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | usbhsh_pipe_attach_done: |
Kuninori Morimoto | 7b332e5 | 2012-11-06 00:11:53 -0800 | [diff] [blame] | 324 | if (0 == ret) |
| 325 | uep->counter++; |
| 326 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 327 | usbhs_unlock(priv, flags); |
| 328 | /******************** spin unlock ******************/ |
| 329 | |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv, |
| 334 | struct usbhsh_ep *uep) |
| 335 | { |
| 336 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 337 | struct usbhs_pipe *pipe; |
| 338 | struct device *dev = usbhs_priv_to_dev(priv); |
| 339 | unsigned long flags; |
| 340 | |
Kuninori Morimoto | 4f053a2 | 2012-10-16 23:31:33 -0700 | [diff] [blame] | 341 | if (unlikely(!uep)) { |
| 342 | dev_err(dev, "no uep\n"); |
| 343 | return; |
| 344 | } |
| 345 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 346 | /******************** spin lock ********************/ |
| 347 | usbhs_lock(priv, flags); |
| 348 | |
| 349 | pipe = usbhsh_uep_to_pipe(uep); |
| 350 | |
| 351 | if (unlikely(!pipe)) { |
| 352 | dev_err(dev, "uep doens't have pipe\n"); |
Kuninori Morimoto | 7b332e5 | 2012-11-06 00:11:53 -0800 | [diff] [blame] | 353 | } else if (1 == uep->counter--) { /* last user */ |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 354 | struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep); |
| 355 | struct usbhsh_device *udev = usbhsh_uep_to_udev(uep); |
| 356 | |
| 357 | /* detach pipe from uep */ |
| 358 | usbhsh_uep_to_pipe(uep) = NULL; |
| 359 | usbhsh_pipe_to_uep(pipe) = NULL; |
| 360 | |
| 361 | dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__, |
| 362 | usbhsh_device_number(hpriv, udev), |
| 363 | usb_endpoint_num(&ep->desc), |
| 364 | usbhs_pipe_name(pipe)); |
| 365 | } |
| 366 | |
| 367 | usbhs_unlock(priv, flags); |
| 368 | /******************** spin unlock ******************/ |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * endpoint control |
| 373 | */ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 374 | static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv, |
| 375 | struct urb *urb, |
| 376 | gfp_t mem_flags) |
| 377 | { |
| 378 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 379 | struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); |
| 380 | struct usb_host_endpoint *ep = urb->ep; |
| 381 | struct usbhsh_ep *uep; |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 382 | struct device *dev = usbhs_priv_to_dev(priv); |
| 383 | struct usb_endpoint_descriptor *desc = &ep->desc; |
| 384 | unsigned long flags; |
| 385 | |
| 386 | uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags); |
Wolfram Sang | 93b6cb4 | 2016-08-25 19:39:29 +0200 | [diff] [blame] | 387 | if (!uep) |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 388 | return -ENOMEM; |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 389 | |
| 390 | /******************** spin lock ********************/ |
| 391 | usbhs_lock(priv, flags); |
| 392 | |
| 393 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 394 | * init endpoint |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 395 | */ |
Kuninori Morimoto | 7b332e5 | 2012-11-06 00:11:53 -0800 | [diff] [blame] | 396 | uep->counter = 0; |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 397 | INIT_LIST_HEAD(&uep->ep_list); |
| 398 | list_add_tail(&uep->ep_list, &udev->ep_list_head); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 399 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 400 | usbhsh_uep_to_udev(uep) = udev; |
| 401 | usbhsh_uep_to_ep(uep) = ep; |
| 402 | usbhsh_ep_to_uep(ep) = uep; |
| 403 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 404 | usbhs_unlock(priv, flags); |
| 405 | /******************** spin unlock ******************/ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 406 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 407 | dev_dbg(dev, "%s [%d-%d]\n", __func__, |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 408 | usbhsh_device_number(hpriv, udev), |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 409 | usb_endpoint_num(desc)); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv, |
| 415 | struct usb_host_endpoint *ep) |
| 416 | { |
| 417 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 418 | struct device *dev = usbhs_priv_to_dev(priv); |
| 419 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 420 | unsigned long flags; |
| 421 | |
| 422 | if (!uep) |
| 423 | return; |
| 424 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 425 | dev_dbg(dev, "%s [%d-%d]\n", __func__, |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 426 | usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)), |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 427 | usb_endpoint_num(&ep->desc)); |
| 428 | |
| 429 | if (usbhsh_uep_to_pipe(uep)) |
| 430 | usbhsh_pipe_detach(hpriv, uep); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 431 | |
| 432 | /******************** spin lock ********************/ |
| 433 | usbhs_lock(priv, flags); |
| 434 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 435 | /* remove this endpoint from udev */ |
| 436 | list_del_init(&uep->ep_list); |
| 437 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 438 | usbhsh_uep_to_udev(uep) = NULL; |
| 439 | usbhsh_uep_to_ep(uep) = NULL; |
| 440 | usbhsh_ep_to_uep(ep) = NULL; |
| 441 | |
| 442 | usbhs_unlock(priv, flags); |
| 443 | /******************** spin unlock ******************/ |
| 444 | |
| 445 | kfree(uep); |
| 446 | } |
| 447 | |
| 448 | static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv, |
| 449 | struct usbhsh_device *udev) |
| 450 | { |
| 451 | struct usbhsh_ep *uep, *next; |
| 452 | |
| 453 | list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list) |
| 454 | usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep)); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
| 458 | * device control |
| 459 | */ |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 460 | static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd, |
| 461 | struct usbhsh_device *udev) |
| 462 | { |
| 463 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); |
| 464 | |
| 465 | return hcd->self.root_hub == usbv->parent; |
| 466 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 467 | |
| 468 | static int usbhsh_device_has_endpoint(struct usbhsh_device *udev) |
| 469 | { |
| 470 | return !list_empty(&udev->ep_list_head); |
| 471 | } |
| 472 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 473 | static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv, |
| 474 | struct urb *urb) |
| 475 | { |
| 476 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 477 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); |
| 478 | |
| 479 | /* usbhsh_device_attach() is still not called */ |
| 480 | if (!udev) |
| 481 | return NULL; |
| 482 | |
| 483 | /* if it is device0, return it */ |
| 484 | if (0 == usb_pipedevice(urb->pipe)) |
| 485 | return usbhsh_device0(hpriv); |
| 486 | |
| 487 | /* return attached device */ |
| 488 | return udev; |
| 489 | } |
| 490 | |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 491 | static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 492 | struct urb *urb) |
| 493 | { |
| 494 | struct usbhsh_device *udev = NULL; |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 495 | struct usbhsh_device *udev0 = usbhsh_device0(hpriv); |
| 496 | struct usbhsh_device *pos; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 497 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 498 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
| 499 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 500 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 501 | unsigned long flags; |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 502 | u16 upphub, hubport; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 503 | int i; |
| 504 | |
| 505 | /* |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 506 | * This function should be called only while urb is pointing to device0. |
| 507 | * It will attach unused usbhsh_device to urb (usbv), |
| 508 | * and initialize device0. |
| 509 | * You can use usbhsh_device_get() to get "current" udev, |
| 510 | * and usbhsh_usbv_to_udev() is for "attached" udev. |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 511 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 512 | if (0 != usb_pipedevice(urb->pipe)) { |
| 513 | dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__); |
| 514 | return NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 517 | /******************** spin lock ********************/ |
| 518 | usbhs_lock(priv, flags); |
| 519 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 520 | /* |
| 521 | * find unused device |
| 522 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 523 | usbhsh_for_each_udev(pos, hpriv, i) { |
| 524 | if (usbhsh_udev_is_used(pos)) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 525 | continue; |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 526 | udev = pos; |
| 527 | break; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 530 | if (udev) { |
| 531 | /* |
| 532 | * usbhsh_usbv_to_udev() |
| 533 | * usbhsh_udev_to_usbv() |
| 534 | * will be enable |
| 535 | */ |
| 536 | dev_set_drvdata(&usbv->dev, udev); |
| 537 | udev->usbv = usbv; |
| 538 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 539 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 540 | usbhs_unlock(priv, flags); |
| 541 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 542 | |
Kuninori Morimoto | ab14230 | 2011-10-31 00:48:00 -0700 | [diff] [blame] | 543 | if (!udev) { |
| 544 | dev_err(dev, "no free usbhsh_device\n"); |
| 545 | return NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 548 | if (usbhsh_device_has_endpoint(udev)) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 549 | dev_warn(dev, "udev have old endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 550 | usbhsh_endpoint_detach_all(hpriv, udev); |
| 551 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 552 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 553 | if (usbhsh_device_has_endpoint(udev0)) { |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 554 | dev_warn(dev, "udev0 have old endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 555 | usbhsh_endpoint_detach_all(hpriv, udev0); |
| 556 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 557 | |
| 558 | /* uep will be attached */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 559 | INIT_LIST_HEAD(&udev0->ep_list_head); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 560 | INIT_LIST_HEAD(&udev->ep_list_head); |
| 561 | |
| 562 | /* |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 563 | * set device0 config |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 564 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 565 | usbhs_set_device_config(priv, |
| 566 | 0, 0, 0, usbv->speed); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 567 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 568 | /* |
| 569 | * set new device config |
| 570 | */ |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 571 | upphub = 0; |
| 572 | hubport = 0; |
| 573 | if (!usbhsh_connected_to_rhdev(hcd, udev)) { |
| 574 | /* if udev is not connected to rhdev, it means parent is Hub */ |
| 575 | struct usbhsh_device *parent = usbhsh_device_parent(udev); |
| 576 | |
| 577 | upphub = usbhsh_device_number(hpriv, parent); |
| 578 | hubport = usbhsh_device_hubport(udev); |
| 579 | |
Colin Ian King | 9038a03 | 2016-12-28 16:52:41 +0000 | [diff] [blame] | 580 | dev_dbg(dev, "%s connected to Hub [%d:%d](%p)\n", __func__, |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 581 | upphub, hubport, parent); |
| 582 | } |
| 583 | |
Kuninori Morimoto | 3dd4926 | 2011-10-31 00:47:13 -0700 | [diff] [blame] | 584 | usbhs_set_device_config(priv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 585 | usbhsh_device_number(hpriv, udev), |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 586 | upphub, hubport, usbv->speed); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 587 | |
| 588 | dev_dbg(dev, "%s [%d](%p)\n", __func__, |
| 589 | usbhsh_device_number(hpriv, udev), udev); |
| 590 | |
| 591 | return udev; |
| 592 | } |
| 593 | |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 594 | static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 595 | struct usbhsh_device *udev) |
| 596 | { |
| 597 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 598 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 599 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
| 600 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 601 | unsigned long flags; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 602 | |
| 603 | dev_dbg(dev, "%s [%d](%p)\n", __func__, |
| 604 | usbhsh_device_number(hpriv, udev), udev); |
| 605 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 606 | if (usbhsh_device_has_endpoint(udev)) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 607 | dev_warn(dev, "udev still have endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 608 | usbhsh_endpoint_detach_all(hpriv, udev); |
| 609 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 610 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 611 | /* |
| 612 | * There is nothing to do if it is device0. |
| 613 | * see |
| 614 | * usbhsh_device_attach() |
| 615 | * usbhsh_device_get() |
| 616 | */ |
| 617 | if (0 == usbhsh_device_number(hpriv, udev)) |
| 618 | return; |
| 619 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 620 | /******************** spin lock ********************/ |
| 621 | usbhs_lock(priv, flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * usbhsh_usbv_to_udev() |
| 625 | * usbhsh_udev_to_usbv() |
| 626 | * will be disable |
| 627 | */ |
| 628 | dev_set_drvdata(&usbv->dev, NULL); |
| 629 | udev->usbv = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 630 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 631 | usbhs_unlock(priv, flags); |
| 632 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
| 636 | * queue push/pop |
| 637 | */ |
| 638 | static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) |
| 639 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 640 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 641 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 642 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 643 | struct urb *urb = ureq->urb; |
| 644 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 6d0376f | 2011-12-08 18:31:11 -0800 | [diff] [blame] | 645 | int status = 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 646 | |
| 647 | dev_dbg(dev, "%s\n", __func__); |
| 648 | |
| 649 | if (!urb) { |
| 650 | dev_warn(dev, "pkt doesn't have urb\n"); |
| 651 | return; |
| 652 | } |
| 653 | |
Kuninori Morimoto | 6d0376f | 2011-12-08 18:31:11 -0800 | [diff] [blame] | 654 | if (!usbhsh_is_running(hpriv)) |
| 655 | status = -ESHUTDOWN; |
| 656 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 657 | urb->actual_length = pkt->actual; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 658 | |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 659 | usbhsh_endpoint_sequence_save(hpriv, urb, pkt); |
Kuninori Morimoto | e0b64ce | 2012-12-09 21:06:37 -0800 | [diff] [blame] | 660 | usbhsh_ureq_free(hpriv, ureq); |
| 661 | |
Kuninori Morimoto | d9b78f3 | 2011-12-15 01:53:18 -0800 | [diff] [blame] | 662 | usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep)); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 663 | |
| 664 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Kuninori Morimoto | 6d0376f | 2011-12-08 18:31:11 -0800 | [diff] [blame] | 665 | usb_hcd_giveback_urb(hcd, urb, status); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static int usbhsh_queue_push(struct usb_hcd *hcd, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 669 | struct urb *urb, |
| 670 | gfp_t mem_flags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 671 | { |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 672 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 673 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 674 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 675 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 676 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 677 | void *buf; |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 678 | int len, sequence; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 679 | |
| 680 | if (usb_pipeisoc(urb->pipe)) { |
| 681 | dev_err(dev, "pipe iso is not supported now\n"); |
| 682 | return -EIO; |
| 683 | } |
| 684 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 685 | /* this ureq will be freed on usbhsh_queue_done() */ |
| 686 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 687 | if (unlikely(!ureq)) { |
| 688 | dev_err(dev, "ureq alloc fail\n"); |
| 689 | return -ENOMEM; |
| 690 | } |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 691 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 692 | if (usb_pipein(urb->pipe)) |
Kuninori Morimoto | 87c2905 | 2012-10-29 00:45:24 -0700 | [diff] [blame] | 693 | pipe->handler = &usbhs_fifo_dma_pop_handler; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 694 | else |
Kuninori Morimoto | 87c2905 | 2012-10-29 00:45:24 -0700 | [diff] [blame] | 695 | pipe->handler = &usbhs_fifo_dma_push_handler; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 696 | |
| 697 | buf = (void *)(urb->transfer_buffer + urb->actual_length); |
| 698 | len = urb->transfer_buffer_length - urb->actual_length; |
| 699 | |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 700 | sequence = usb_gettoggle(urb->dev, |
| 701 | usb_pipeendpoint(urb->pipe), |
| 702 | usb_pipeout(urb->pipe)); |
| 703 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 704 | dev_dbg(dev, "%s\n", __func__); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 705 | usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 706 | buf, len, (urb->transfer_flags & URB_ZERO_PACKET), |
| 707 | sequence); |
| 708 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 709 | usbhs_pkt_start(pipe); |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
Kuninori Morimoto | 2d833fa | 2011-12-08 18:31:37 -0800 | [diff] [blame] | 714 | static void usbhsh_queue_force_pop(struct usbhs_priv *priv, |
| 715 | struct usbhs_pipe *pipe) |
| 716 | { |
| 717 | struct usbhs_pkt *pkt; |
| 718 | |
| 719 | while (1) { |
| 720 | pkt = usbhs_pkt_pop(pipe, NULL); |
| 721 | if (!pkt) |
| 722 | break; |
| 723 | |
| 724 | /* |
| 725 | * if all packet are gone, usbhsh_endpoint_disable() |
| 726 | * will be called. |
| 727 | * then, attached device/endpoint/pipe will be detached |
| 728 | */ |
| 729 | usbhsh_queue_done(priv, pkt); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv) |
| 734 | { |
| 735 | struct usbhs_pipe *pos; |
| 736 | int i; |
| 737 | |
| 738 | usbhs_for_each_pipe_with_dcp(pos, priv, i) |
| 739 | usbhsh_queue_force_pop(priv, pos); |
| 740 | } |
| 741 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 742 | /* |
| 743 | * DCP setup stage |
| 744 | */ |
| 745 | static int usbhsh_is_request_address(struct urb *urb) |
| 746 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 747 | struct usb_ctrlrequest *req; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 748 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 749 | req = (struct usb_ctrlrequest *)urb->setup_packet; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 750 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 751 | if ((DeviceOutRequest == req->bRequestType << 8) && |
| 752 | (USB_REQ_SET_ADDRESS == req->bRequest)) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 753 | return 1; |
| 754 | else |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv, |
| 759 | struct urb *urb, |
| 760 | struct usbhs_pipe *pipe) |
| 761 | { |
| 762 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 763 | struct usb_ctrlrequest req; |
| 764 | struct device *dev = usbhs_priv_to_dev(priv); |
| 765 | |
| 766 | /* |
| 767 | * wait setup packet ACK |
| 768 | * see |
| 769 | * usbhsh_irq_setup_ack() |
| 770 | * usbhsh_irq_setup_err() |
| 771 | */ |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 772 | init_completion(&hpriv->setup_ack_done); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 773 | |
| 774 | /* copy original request */ |
| 775 | memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest)); |
| 776 | |
| 777 | /* |
| 778 | * renesas_usbhs can not use original usb address. |
| 779 | * see HARDWARE LIMITATION. |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 780 | * modify usb address here to use attached device. |
| 781 | * see usbhsh_device_attach() |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 782 | */ |
| 783 | if (usbhsh_is_request_address(urb)) { |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 784 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 785 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); |
| 786 | |
| 787 | /* udev is a attached device */ |
| 788 | req.wValue = usbhsh_device_number(hpriv, udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 789 | dev_dbg(dev, "create new address - %d\n", req.wValue); |
| 790 | } |
| 791 | |
| 792 | /* set request */ |
| 793 | usbhs_usbreq_set_val(priv, &req); |
| 794 | |
| 795 | /* |
| 796 | * wait setup packet ACK |
| 797 | */ |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 798 | wait_for_completion(&hpriv->setup_ack_done); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 799 | |
| 800 | dev_dbg(dev, "%s done\n", __func__); |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * DCP data stage |
| 805 | */ |
| 806 | static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv, |
| 807 | struct usbhs_pkt *pkt) |
| 808 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 809 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 810 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 811 | |
| 812 | /* this ureq was connected to urb when usbhsh_urb_enqueue() */ |
| 813 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 814 | usbhsh_ureq_free(hpriv, ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 817 | static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv, |
| 818 | struct urb *urb, |
| 819 | struct usbhs_pipe *pipe, |
| 820 | gfp_t mem_flags) |
| 821 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 822 | { |
| 823 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 824 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 825 | /* this ureq will be freed on usbhsh_data_stage_packet_done() */ |
| 826 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 827 | if (unlikely(!ureq)) |
| 828 | return -ENOMEM; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 829 | |
| 830 | if (usb_pipein(urb->pipe)) |
| 831 | pipe->handler = &usbhs_dcp_data_stage_in_handler; |
| 832 | else |
| 833 | pipe->handler = &usbhs_dcp_data_stage_out_handler; |
| 834 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 835 | usbhs_pkt_push(pipe, &ureq->pkt, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 836 | usbhsh_data_stage_packet_done, |
| 837 | urb->transfer_buffer, |
| 838 | urb->transfer_buffer_length, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 839 | (urb->transfer_flags & URB_ZERO_PACKET), |
| 840 | -1); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 841 | |
| 842 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* |
| 846 | * DCP status stage |
| 847 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 848 | static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 849 | struct urb *urb, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 850 | struct usbhs_pipe *pipe, |
| 851 | gfp_t mem_flags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 852 | { |
| 853 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 854 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 855 | /* This ureq will be freed on usbhsh_queue_done() */ |
| 856 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 857 | if (unlikely(!ureq)) |
| 858 | return -ENOMEM; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 859 | |
| 860 | if (usb_pipein(urb->pipe)) |
| 861 | pipe->handler = &usbhs_dcp_status_stage_in_handler; |
| 862 | else |
| 863 | pipe->handler = &usbhs_dcp_status_stage_out_handler; |
| 864 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 865 | usbhs_pkt_push(pipe, &ureq->pkt, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 866 | usbhsh_queue_done, |
| 867 | NULL, |
| 868 | urb->transfer_buffer_length, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 869 | 0, -1); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 870 | |
| 871 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static int usbhsh_dcp_queue_push(struct usb_hcd *hcd, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 875 | struct urb *urb, |
| 876 | gfp_t mflags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 877 | { |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 878 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 879 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 880 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 881 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 882 | int ret; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 883 | |
| 884 | dev_dbg(dev, "%s\n", __func__); |
| 885 | |
| 886 | /* |
| 887 | * setup stage |
| 888 | * |
| 889 | * usbhsh_send_setup_stage_packet() wait SACK/SIGN |
| 890 | */ |
| 891 | usbhsh_setup_stage_packet_push(hpriv, urb, pipe); |
| 892 | |
| 893 | /* |
| 894 | * data stage |
| 895 | * |
| 896 | * It is pushed only when urb has buffer. |
| 897 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 898 | if (urb->transfer_buffer_length) { |
| 899 | ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags); |
| 900 | if (ret < 0) { |
| 901 | dev_err(dev, "data stage failed\n"); |
| 902 | return ret; |
| 903 | } |
| 904 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 905 | |
| 906 | /* |
| 907 | * status stage |
| 908 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 909 | ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags); |
| 910 | if (ret < 0) { |
| 911 | dev_err(dev, "status stage failed\n"); |
| 912 | return ret; |
| 913 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * start pushed packets |
| 917 | */ |
| 918 | usbhs_pkt_start(pipe); |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * dma map functions |
| 925 | */ |
Yoshihiro Shimoda | c3cdcac | 2016-04-18 16:53:41 +0900 | [diff] [blame] | 926 | static int usbhsh_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt, |
| 927 | int map) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 928 | { |
Kuninori Morimoto | 87c2905 | 2012-10-29 00:45:24 -0700 | [diff] [blame] | 929 | if (map) { |
| 930 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
| 931 | struct urb *urb = ureq->urb; |
| 932 | |
| 933 | /* it can not use scatter/gather */ |
| 934 | if (urb->num_sgs) |
| 935 | return -EINVAL; |
| 936 | |
| 937 | pkt->dma = urb->transfer_dma; |
| 938 | if (!pkt->dma) |
| 939 | return -EINVAL; |
| 940 | } |
| 941 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * for hc_driver |
| 947 | */ |
| 948 | static int usbhsh_host_start(struct usb_hcd *hcd) |
| 949 | { |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static void usbhsh_host_stop(struct usb_hcd *hcd) |
| 954 | { |
| 955 | } |
| 956 | |
| 957 | static int usbhsh_urb_enqueue(struct usb_hcd *hcd, |
| 958 | struct urb *urb, |
| 959 | gfp_t mem_flags) |
| 960 | { |
| 961 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 962 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 963 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 964 | struct usb_host_endpoint *ep = urb->ep; |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 965 | struct usbhsh_device *new_udev = NULL; |
Kuninori Morimoto | 73ef635 | 2011-10-24 02:24:49 -0700 | [diff] [blame] | 966 | int is_dir_in = usb_pipein(urb->pipe); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 967 | int ret; |
| 968 | |
Kuninori Morimoto | 73ef635 | 2011-10-24 02:24:49 -0700 | [diff] [blame] | 969 | dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 970 | |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 971 | if (!usbhsh_is_running(hpriv)) { |
| 972 | ret = -EIO; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 973 | dev_err(dev, "host is not running\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 974 | goto usbhsh_urb_enqueue_error_not_linked; |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 975 | } |
| 976 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 977 | ret = usb_hcd_link_urb_to_ep(hcd, urb); |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 978 | if (ret) { |
| 979 | dev_err(dev, "urb link failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 980 | goto usbhsh_urb_enqueue_error_not_linked; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 981 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 982 | |
| 983 | /* |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 984 | * attach udev if needed |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 985 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 986 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 987 | if (!usbhsh_device_get(hpriv, urb)) { |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 988 | new_udev = usbhsh_device_attach(hpriv, urb); |
Kuninori Morimoto | 37332ee | 2011-12-08 18:25:37 -0800 | [diff] [blame] | 989 | if (!new_udev) { |
| 990 | ret = -EIO; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 991 | dev_err(dev, "device attach failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 992 | goto usbhsh_urb_enqueue_error_not_linked; |
Kuninori Morimoto | 37332ee | 2011-12-08 18:25:37 -0800 | [diff] [blame] | 993 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 997 | * attach endpoint if needed |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 998 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 999 | */ |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 1000 | if (!usbhsh_ep_to_uep(ep)) { |
| 1001 | ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags); |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 1002 | if (ret < 0) { |
| 1003 | dev_err(dev, "endpoint attach failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1004 | goto usbhsh_urb_enqueue_error_free_device; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 1005 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1006 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1007 | |
| 1008 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 1009 | * attach pipe to endpoint |
| 1010 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1011 | */ |
Kuninori Morimoto | 7b332e5 | 2012-11-06 00:11:53 -0800 | [diff] [blame] | 1012 | ret = usbhsh_pipe_attach(hpriv, urb); |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 1013 | if (ret < 0) { |
| 1014 | dev_err(dev, "pipe attach failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1015 | goto usbhsh_urb_enqueue_error_free_endpoint; |
| 1016 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1017 | |
| 1018 | /* |
| 1019 | * push packet |
| 1020 | */ |
| 1021 | if (usb_pipecontrol(urb->pipe)) |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 1022 | ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1023 | else |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 1024 | ret = usbhsh_queue_push(hcd, urb, mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1025 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 1026 | return ret; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1027 | |
| 1028 | usbhsh_urb_enqueue_error_free_endpoint: |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 1029 | usbhsh_endpoint_detach(hpriv, ep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1030 | usbhsh_urb_enqueue_error_free_device: |
| 1031 | if (new_udev) |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 1032 | usbhsh_device_detach(hpriv, new_udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1033 | usbhsh_urb_enqueue_error_not_linked: |
| 1034 | |
| 1035 | dev_dbg(dev, "%s error\n", __func__); |
| 1036 | |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
| 1041 | { |
| 1042 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1043 | struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb); |
| 1044 | |
| 1045 | if (ureq) { |
Kuninori Morimoto | 5479654 | 2011-12-08 18:26:07 -0800 | [diff] [blame] | 1046 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1047 | struct usbhs_pkt *pkt = &ureq->pkt; |
| 1048 | |
| 1049 | usbhs_pkt_pop(pkt->pipe, pkt); |
| 1050 | usbhsh_queue_done(priv, pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | static void usbhsh_endpoint_disable(struct usb_hcd *hcd, |
| 1057 | struct usb_host_endpoint *ep) |
| 1058 | { |
| 1059 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); |
| 1060 | struct usbhsh_device *udev; |
| 1061 | struct usbhsh_hpriv *hpriv; |
| 1062 | |
| 1063 | /* |
| 1064 | * this function might be called manytimes by same hcd/ep |
Kuninori Morimoto | fca8ab7 | 2011-10-31 00:47:24 -0700 | [diff] [blame] | 1065 | * in-endpoint == out-endpoint if ep == dcp. |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1066 | */ |
| 1067 | if (!uep) |
| 1068 | return; |
| 1069 | |
| 1070 | udev = usbhsh_uep_to_udev(uep); |
| 1071 | hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1072 | |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 1073 | usbhsh_endpoint_detach(hpriv, ep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1074 | |
| 1075 | /* |
| 1076 | * if there is no endpoint, |
| 1077 | * free device |
| 1078 | */ |
| 1079 | if (!usbhsh_device_has_endpoint(udev)) |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 1080 | usbhsh_device_detach(hpriv, udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf) |
| 1084 | { |
| 1085 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1086 | int roothub_id = 1; /* only 1 root hub */ |
| 1087 | |
| 1088 | /* |
| 1089 | * does port stat was changed ? |
| 1090 | * check USB_PORT_STAT_C_xxx << 16 |
| 1091 | */ |
| 1092 | if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000) |
| 1093 | *buf = (1 << roothub_id); |
| 1094 | else |
| 1095 | *buf = 0; |
| 1096 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1097 | return !!(*buf); |
| 1098 | } |
| 1099 | |
| 1100 | static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv, |
| 1101 | u16 typeReq, u16 wValue, |
| 1102 | u16 wIndex, char *buf, u16 wLength) |
| 1103 | { |
| 1104 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1105 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1106 | |
| 1107 | switch (wValue) { |
| 1108 | case C_HUB_OVER_CURRENT: |
| 1109 | case C_HUB_LOCAL_POWER: |
| 1110 | dev_dbg(dev, "%s :: C_HUB_xx\n", __func__); |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | return -EPIPE; |
| 1115 | } |
| 1116 | |
| 1117 | static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv, |
| 1118 | u16 typeReq, u16 wValue, |
| 1119 | u16 wIndex, char *buf, u16 wLength) |
| 1120 | { |
| 1121 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1122 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1123 | int enable = (typeReq == SetPortFeature); |
| 1124 | int speed, i, timeout = 128; |
| 1125 | int roothub_id = 1; /* only 1 root hub */ |
| 1126 | |
| 1127 | /* common error */ |
| 1128 | if (wIndex > roothub_id || wLength != 0) |
| 1129 | return -EPIPE; |
| 1130 | |
| 1131 | /* check wValue */ |
| 1132 | switch (wValue) { |
| 1133 | case USB_PORT_FEAT_POWER: |
| 1134 | usbhs_vbus_ctrl(priv, enable); |
| 1135 | dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__); |
| 1136 | break; |
| 1137 | |
| 1138 | case USB_PORT_FEAT_ENABLE: |
| 1139 | case USB_PORT_FEAT_SUSPEND: |
| 1140 | case USB_PORT_FEAT_C_ENABLE: |
| 1141 | case USB_PORT_FEAT_C_SUSPEND: |
| 1142 | case USB_PORT_FEAT_C_CONNECTION: |
| 1143 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 1144 | case USB_PORT_FEAT_C_RESET: |
| 1145 | dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__); |
| 1146 | break; |
| 1147 | |
| 1148 | case USB_PORT_FEAT_RESET: |
| 1149 | if (!enable) |
| 1150 | break; |
| 1151 | |
| 1152 | usbhsh_port_stat_clear(hpriv, |
| 1153 | USB_PORT_STAT_HIGH_SPEED | |
| 1154 | USB_PORT_STAT_LOW_SPEED); |
| 1155 | |
Kuninori Morimoto | 2d833fa | 2011-12-08 18:31:37 -0800 | [diff] [blame] | 1156 | usbhsh_queue_force_pop_all(priv); |
| 1157 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1158 | usbhs_bus_send_reset(priv); |
| 1159 | msleep(20); |
| 1160 | usbhs_bus_send_sof_enable(priv); |
| 1161 | |
| 1162 | for (i = 0; i < timeout ; i++) { |
| 1163 | switch (usbhs_bus_get_speed(priv)) { |
| 1164 | case USB_SPEED_LOW: |
| 1165 | speed = USB_PORT_STAT_LOW_SPEED; |
| 1166 | goto got_usb_bus_speed; |
| 1167 | case USB_SPEED_HIGH: |
| 1168 | speed = USB_PORT_STAT_HIGH_SPEED; |
| 1169 | goto got_usb_bus_speed; |
| 1170 | case USB_SPEED_FULL: |
| 1171 | speed = 0; |
| 1172 | goto got_usb_bus_speed; |
| 1173 | } |
| 1174 | |
| 1175 | msleep(20); |
| 1176 | } |
| 1177 | return -EPIPE; |
| 1178 | |
| 1179 | got_usb_bus_speed: |
| 1180 | usbhsh_port_stat_set(hpriv, speed); |
| 1181 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE); |
| 1182 | |
| 1183 | dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n", |
| 1184 | __func__, speed); |
| 1185 | |
| 1186 | /* status change is not needed */ |
| 1187 | return 0; |
| 1188 | |
| 1189 | default: |
| 1190 | return -EPIPE; |
| 1191 | } |
| 1192 | |
| 1193 | /* set/clear status */ |
| 1194 | if (enable) |
| 1195 | usbhsh_port_stat_set(hpriv, (1 << wValue)); |
| 1196 | else |
| 1197 | usbhsh_port_stat_clear(hpriv, (1 << wValue)); |
| 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv, |
| 1203 | u16 typeReq, u16 wValue, |
| 1204 | u16 wIndex, char *buf, u16 wLength) |
| 1205 | { |
| 1206 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1207 | struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf; |
| 1208 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1209 | int roothub_id = 1; /* only 1 root hub */ |
| 1210 | |
| 1211 | switch (typeReq) { |
| 1212 | case GetHubStatus: |
| 1213 | dev_dbg(dev, "%s :: GetHubStatus\n", __func__); |
| 1214 | |
| 1215 | *buf = 0x00; |
| 1216 | break; |
| 1217 | |
| 1218 | case GetPortStatus: |
| 1219 | if (wIndex != roothub_id) |
| 1220 | return -EPIPE; |
| 1221 | |
| 1222 | dev_dbg(dev, "%s :: GetPortStatus\n", __func__); |
| 1223 | *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv)); |
| 1224 | break; |
| 1225 | |
| 1226 | case GetHubDescriptor: |
Sergei Shtylyov | b11373d | 2015-03-29 01:43:25 +0300 | [diff] [blame] | 1227 | desc->bDescriptorType = USB_DT_HUB; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1228 | desc->bHubContrCurrent = 0; |
| 1229 | desc->bNbrPorts = roothub_id; |
| 1230 | desc->bDescLength = 9; |
| 1231 | desc->bPwrOn2PwrGood = 0; |
Sergei Shtylyov | a1837d1 | 2015-01-19 01:59:09 +0300 | [diff] [blame] | 1232 | desc->wHubCharacteristics = |
| 1233 | cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1234 | desc->u.hs.DeviceRemovable[0] = (roothub_id << 1); |
| 1235 | desc->u.hs.DeviceRemovable[1] = ~0; |
| 1236 | dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__); |
| 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, |
| 1244 | u16 wIndex, char *buf, u16 wLength) |
| 1245 | { |
| 1246 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1247 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1248 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1249 | int ret = -EPIPE; |
| 1250 | |
| 1251 | switch (typeReq) { |
| 1252 | |
| 1253 | /* Hub Feature */ |
| 1254 | case ClearHubFeature: |
| 1255 | case SetHubFeature: |
| 1256 | ret = __usbhsh_hub_hub_feature(hpriv, typeReq, |
| 1257 | wValue, wIndex, buf, wLength); |
| 1258 | break; |
| 1259 | |
| 1260 | /* Port Feature */ |
| 1261 | case SetPortFeature: |
| 1262 | case ClearPortFeature: |
| 1263 | ret = __usbhsh_hub_port_feature(hpriv, typeReq, |
| 1264 | wValue, wIndex, buf, wLength); |
| 1265 | break; |
| 1266 | |
| 1267 | /* Get status */ |
| 1268 | case GetHubStatus: |
| 1269 | case GetPortStatus: |
| 1270 | case GetHubDescriptor: |
| 1271 | ret = __usbhsh_hub_get_status(hpriv, typeReq, |
| 1272 | wValue, wIndex, buf, wLength); |
| 1273 | break; |
| 1274 | } |
| 1275 | |
| 1276 | dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n", |
| 1277 | typeReq, ret, usbhsh_port_stat_get(hpriv)); |
| 1278 | |
| 1279 | return ret; |
| 1280 | } |
| 1281 | |
Kuninori Morimoto | e7ae64c | 2012-08-05 22:44:07 -0700 | [diff] [blame] | 1282 | static int usbhsh_bus_nop(struct usb_hcd *hcd) |
| 1283 | { |
| 1284 | /* nothing to do */ |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1288 | static struct hc_driver usbhsh_driver = { |
| 1289 | .description = usbhsh_hcd_name, |
| 1290 | .hcd_priv_size = sizeof(struct usbhsh_hpriv), |
| 1291 | |
| 1292 | /* |
| 1293 | * generic hardware linkage |
| 1294 | */ |
| 1295 | .flags = HCD_USB2, |
| 1296 | |
| 1297 | .start = usbhsh_host_start, |
| 1298 | .stop = usbhsh_host_stop, |
| 1299 | |
| 1300 | /* |
| 1301 | * managing i/o requests and associated device resources |
| 1302 | */ |
| 1303 | .urb_enqueue = usbhsh_urb_enqueue, |
| 1304 | .urb_dequeue = usbhsh_urb_dequeue, |
| 1305 | .endpoint_disable = usbhsh_endpoint_disable, |
| 1306 | |
| 1307 | /* |
| 1308 | * root hub |
| 1309 | */ |
| 1310 | .hub_status_data = usbhsh_hub_status_data, |
| 1311 | .hub_control = usbhsh_hub_control, |
Kuninori Morimoto | e7ae64c | 2012-08-05 22:44:07 -0700 | [diff] [blame] | 1312 | .bus_suspend = usbhsh_bus_nop, |
| 1313 | .bus_resume = usbhsh_bus_nop, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1314 | }; |
| 1315 | |
| 1316 | /* |
| 1317 | * interrupt functions |
| 1318 | */ |
| 1319 | static int usbhsh_irq_attch(struct usbhs_priv *priv, |
| 1320 | struct usbhs_irq_state *irq_state) |
| 1321 | { |
| 1322 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1323 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1324 | |
| 1325 | dev_dbg(dev, "device attached\n"); |
| 1326 | |
| 1327 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION); |
| 1328 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); |
| 1329 | |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1330 | /* |
| 1331 | * attch interrupt might happen infinitely on some device |
| 1332 | * (on self power USB hub ?) |
| 1333 | * disable it here. |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 1334 | * |
| 1335 | * usbhsh_is_running() becomes effective |
| 1336 | * according to this process. |
| 1337 | * see |
| 1338 | * usbhsh_is_running() |
| 1339 | * usbhsh_urb_enqueue() |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1340 | */ |
| 1341 | hpriv->mod.irq_attch = NULL; |
| 1342 | usbhs_irq_callback_update(priv, &hpriv->mod); |
| 1343 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static int usbhsh_irq_dtch(struct usbhs_priv *priv, |
| 1348 | struct usbhs_irq_state *irq_state) |
| 1349 | { |
| 1350 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1351 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1352 | |
| 1353 | dev_dbg(dev, "device detached\n"); |
| 1354 | |
| 1355 | usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION); |
| 1356 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); |
| 1357 | |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1358 | /* |
| 1359 | * enable attch interrupt again |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 1360 | * |
| 1361 | * usbhsh_is_running() becomes invalid |
| 1362 | * according to this process. |
| 1363 | * see |
| 1364 | * usbhsh_is_running() |
| 1365 | * usbhsh_urb_enqueue() |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1366 | */ |
| 1367 | hpriv->mod.irq_attch = usbhsh_irq_attch; |
| 1368 | usbhs_irq_callback_update(priv, &hpriv->mod); |
| 1369 | |
Kuninori Morimoto | 2d833fa | 2011-12-08 18:31:37 -0800 | [diff] [blame] | 1370 | /* |
| 1371 | * usbhsh_queue_force_pop_all() should be called |
| 1372 | * after usbhsh_is_running() becomes invalid. |
| 1373 | */ |
| 1374 | usbhsh_queue_force_pop_all(priv); |
| 1375 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static int usbhsh_irq_setup_ack(struct usbhs_priv *priv, |
| 1380 | struct usbhs_irq_state *irq_state) |
| 1381 | { |
| 1382 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1383 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1384 | |
| 1385 | dev_dbg(dev, "setup packet OK\n"); |
| 1386 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 1387 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1388 | |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | static int usbhsh_irq_setup_err(struct usbhs_priv *priv, |
| 1393 | struct usbhs_irq_state *irq_state) |
| 1394 | { |
| 1395 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1396 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1397 | |
| 1398 | dev_dbg(dev, "setup packet Err\n"); |
| 1399 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 1400 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1401 | |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * module start/stop |
| 1407 | */ |
| 1408 | static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv) |
| 1409 | { |
| 1410 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1411 | struct usbhs_pipe *pipe; |
Yoshihiro Shimoda | 51f141a | 2015-11-18 14:34:09 +0900 | [diff] [blame] | 1412 | struct renesas_usbhs_driver_pipe_config *pipe_configs = |
| 1413 | usbhs_get_dparam(priv, pipe_configs); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1414 | int pipe_size = usbhs_get_dparam(priv, pipe_size); |
| 1415 | int old_type, dir_in, i; |
| 1416 | |
| 1417 | /* init all pipe */ |
| 1418 | old_type = USB_ENDPOINT_XFER_CONTROL; |
| 1419 | for (i = 0; i < pipe_size; i++) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1420 | |
| 1421 | /* |
| 1422 | * data "output" will be finished as soon as possible, |
| 1423 | * but there is no guaranty at data "input" case. |
| 1424 | * |
| 1425 | * "input" needs "standby" pipe. |
| 1426 | * So, "input" direction pipe > "output" direction pipe |
| 1427 | * is good idea. |
| 1428 | * |
| 1429 | * 1st USB_ENDPOINT_XFER_xxx will be output direction, |
| 1430 | * and the other will be input direction here. |
| 1431 | * |
| 1432 | * ex) |
| 1433 | * ... |
| 1434 | * USB_ENDPOINT_XFER_ISOC -> dir out |
| 1435 | * USB_ENDPOINT_XFER_ISOC -> dir in |
| 1436 | * USB_ENDPOINT_XFER_BULK -> dir out |
| 1437 | * USB_ENDPOINT_XFER_BULK -> dir in |
| 1438 | * USB_ENDPOINT_XFER_BULK -> dir in |
| 1439 | * ... |
| 1440 | */ |
Yoshihiro Shimoda | 51f141a | 2015-11-18 14:34:09 +0900 | [diff] [blame] | 1441 | dir_in = (pipe_configs[i].type == old_type); |
| 1442 | old_type = pipe_configs[i].type; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1443 | |
Yoshihiro Shimoda | 51f141a | 2015-11-18 14:34:09 +0900 | [diff] [blame] | 1444 | if (USB_ENDPOINT_XFER_CONTROL == pipe_configs[i].type) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1445 | pipe = usbhs_dcp_malloc(priv); |
| 1446 | usbhsh_hpriv_to_dcp(hpriv) = pipe; |
| 1447 | } else { |
| 1448 | pipe = usbhs_pipe_malloc(priv, |
Yoshihiro Shimoda | 51f141a | 2015-11-18 14:34:09 +0900 | [diff] [blame] | 1449 | pipe_configs[i].type, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1450 | dir_in); |
| 1451 | } |
| 1452 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 1453 | pipe->mod_private = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | static int usbhsh_start(struct usbhs_priv *priv) |
| 1458 | { |
| 1459 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1460 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 1461 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 1462 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1463 | int ret; |
| 1464 | |
| 1465 | /* add hcd */ |
| 1466 | ret = usb_add_hcd(hcd, 0, 0); |
| 1467 | if (ret < 0) |
| 1468 | return 0; |
Peter Chen | 3c9740a | 2013-11-05 10:46:02 +0800 | [diff] [blame] | 1469 | device_wakeup_enable(hcd->self.controller); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1470 | |
| 1471 | /* |
| 1472 | * pipe initialize and enable DCP |
| 1473 | */ |
Yoshihiro Shimoda | cdeb794 | 2014-11-04 10:05:45 +0900 | [diff] [blame] | 1474 | usbhs_fifo_init(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1475 | usbhs_pipe_init(priv, |
| 1476 | usbhsh_dma_map_ctrl); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1477 | usbhsh_pipe_init_for_host(priv); |
| 1478 | |
| 1479 | /* |
| 1480 | * system config enble |
| 1481 | * - HI speed |
| 1482 | * - host |
| 1483 | * - usb module |
| 1484 | */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1485 | usbhs_sys_host_ctrl(priv, 1); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1486 | |
| 1487 | /* |
| 1488 | * enable irq callback |
| 1489 | */ |
| 1490 | mod->irq_attch = usbhsh_irq_attch; |
| 1491 | mod->irq_dtch = usbhsh_irq_dtch; |
| 1492 | mod->irq_sack = usbhsh_irq_setup_ack; |
| 1493 | mod->irq_sign = usbhsh_irq_setup_err; |
| 1494 | usbhs_irq_callback_update(priv, mod); |
| 1495 | |
| 1496 | dev_dbg(dev, "start host\n"); |
| 1497 | |
| 1498 | return ret; |
| 1499 | } |
| 1500 | |
| 1501 | static int usbhsh_stop(struct usbhs_priv *priv) |
| 1502 | { |
| 1503 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1504 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
Kuninori Morimoto | 146ee50 | 2011-10-24 02:25:07 -0700 | [diff] [blame] | 1505 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1506 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1507 | |
Kuninori Morimoto | 146ee50 | 2011-10-24 02:25:07 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * disable irq callback |
| 1510 | */ |
| 1511 | mod->irq_attch = NULL; |
| 1512 | mod->irq_dtch = NULL; |
| 1513 | mod->irq_sack = NULL; |
| 1514 | mod->irq_sign = NULL; |
| 1515 | usbhs_irq_callback_update(priv, mod); |
| 1516 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1517 | usb_remove_hcd(hcd); |
| 1518 | |
| 1519 | /* disable sys */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1520 | usbhs_sys_host_ctrl(priv, 0); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1521 | |
| 1522 | dev_dbg(dev, "quit host\n"); |
| 1523 | |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 1527 | int usbhs_mod_host_probe(struct usbhs_priv *priv) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1528 | { |
| 1529 | struct usbhsh_hpriv *hpriv; |
| 1530 | struct usb_hcd *hcd; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1531 | struct usbhsh_device *udev; |
| 1532 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1533 | int i; |
| 1534 | |
| 1535 | /* initialize hcd */ |
| 1536 | hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name); |
| 1537 | if (!hcd) { |
| 1538 | dev_err(dev, "Failed to create hcd\n"); |
| 1539 | return -ENOMEM; |
| 1540 | } |
Kuninori Morimoto | 1115b9e | 2011-12-08 18:24:47 -0800 | [diff] [blame] | 1541 | hcd->has_tt = 1; /* for low/full speed */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1542 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1543 | /* |
| 1544 | * CAUTION |
| 1545 | * |
| 1546 | * There is no guarantee that it is possible to access usb module here. |
| 1547 | * Don't accesses to it. |
| 1548 | * The accesse will be enable after "usbhsh_start" |
| 1549 | */ |
| 1550 | |
| 1551 | hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1552 | |
| 1553 | /* |
| 1554 | * register itself |
| 1555 | */ |
| 1556 | usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST); |
| 1557 | |
| 1558 | /* init hpriv */ |
| 1559 | hpriv->mod.name = "host"; |
| 1560 | hpriv->mod.start = usbhsh_start; |
| 1561 | hpriv->mod.stop = usbhsh_stop; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1562 | usbhsh_port_stat_init(hpriv); |
| 1563 | |
| 1564 | /* init all device */ |
| 1565 | usbhsh_for_each_udev_with_dev0(udev, hpriv, i) { |
| 1566 | udev->usbv = NULL; |
| 1567 | INIT_LIST_HEAD(&udev->ep_list_head); |
| 1568 | } |
| 1569 | |
| 1570 | dev_info(dev, "host probed\n"); |
| 1571 | |
| 1572 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 1575 | int usbhs_mod_host_remove(struct usbhs_priv *priv) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1576 | { |
| 1577 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1578 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 1579 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1580 | usb_put_hcd(hcd); |
| 1581 | |
| 1582 | return 0; |
| 1583 | } |