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