1 /* $OpenBSD: sdmmc.c,v 1.14 2007/09/11 13:39:34 gilles Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Host controller independent SD/MMC bus driver based on information 21 * from SanDisk SD Card Product Manual Revision 2.2 (SanDisk), SDIO 22 * Simple Specification Version 1.0 (SDIO) and the Linux "mmc" driver. 23 */ 24 25 #include <sys/param.h> 26 #include <sys/device.h> 27 #include <sys/kernel.h> 28 #include <sys/kthread.h> 29 #include <sys/malloc.h> 30 #include <sys/proc.h> 31 #include <sys/systm.h> 32 33 #include <scsi/scsi_all.h> 34 #include <scsi/scsiconf.h> 35 36 #include <dev/sdmmc/sdmmc_ioreg.h> 37 #include <dev/sdmmc/sdmmc_scsi.h> 38 #include <dev/sdmmc/sdmmcchip.h> 39 #include <dev/sdmmc/sdmmcreg.h> 40 #include <dev/sdmmc/sdmmcvar.h> 41 42 #ifdef SDMMC_IOCTL 43 #include "bio.h" 44 #if NBIO < 1 45 #undef SDMMC_IOCTL 46 #endif 47 #include <dev/biovar.h> 48 #endif 49 50 int sdmmc_match(struct device *, void *, void *); 51 void sdmmc_attach(struct device *, struct device *, void *); 52 int sdmmc_detach(struct device *, int); 53 void sdmmc_create_thread(void *); 54 void sdmmc_task_thread(void *); 55 void sdmmc_discover_task(void *); 56 void sdmmc_card_attach(struct sdmmc_softc *); 57 void sdmmc_card_detach(struct sdmmc_softc *, int); 58 int sdmmc_enable(struct sdmmc_softc *); 59 void sdmmc_disable(struct sdmmc_softc *); 60 int sdmmc_scan(struct sdmmc_softc *); 61 int sdmmc_init(struct sdmmc_softc *); 62 int sdmmc_set_bus_width(struct sdmmc_function *); 63 #ifdef SDMMC_IOCTL 64 int sdmmc_ioctl(struct device *, u_long, caddr_t); 65 #endif 66 67 #define DEVNAME(sc) SDMMCDEVNAME(sc) 68 69 #ifdef SDMMC_DEBUG 70 int sdmmcdebug = 0; 71 extern int sdhcdebug; /* XXX should have a sdmmc_chip_debug() function */ 72 void sdmmc_dump_command(struct sdmmc_softc *, struct sdmmc_command *); 73 #define DPRINTF(n,s) do { if ((n) <= sdmmcdebug) printf s; } while (0) 74 #else 75 #define DPRINTF(n,s) do {} while (0) 76 #endif 77 78 struct cfattach sdmmc_ca = { 79 sizeof(struct sdmmc_softc), sdmmc_match, sdmmc_attach, sdmmc_detach 80 }; 81 82 struct cfdriver sdmmc_cd = { 83 NULL, "sdmmc", DV_DULL 84 }; 85 86 int 87 sdmmc_match(struct device *parent, void *match, void *aux) 88 { 89 struct cfdata *cf = match; 90 struct sdmmcbus_attach_args *saa = aux; 91 92 return strcmp(saa->saa_busname, cf->cf_driver->cd_name) == 0; 93 } 94 95 void 96 sdmmc_attach(struct device *parent, struct device *self, void *aux) 97 { 98 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 99 struct sdmmcbus_attach_args *saa = aux; 100 101 printf("\n"); 102 103 sc->sct = saa->sct; 104 sc->sch = saa->sch; 105 106 SIMPLEQ_INIT(&sc->sf_head); 107 TAILQ_INIT(&sc->sc_tskq); 108 TAILQ_INIT(&sc->sc_intrq); 109 sdmmc_init_task(&sc->sc_discover_task, sdmmc_discover_task, sc); 110 sdmmc_init_task(&sc->sc_intr_task, sdmmc_intr_task, sc); 111 lockinit(&sc->sc_lock, PRIBIO, DEVNAME(sc), 0, LK_CANRECURSE); 112 113 #ifdef SDMMC_IOCTL 114 if (bio_register(self, sdmmc_ioctl) != 0) 115 printf("%s: unable to register ioctl\n", DEVNAME(sc)); 116 #endif 117 118 /* 119 * Create the event thread that will attach and detach cards 120 * and perform other lengthy operations. 121 */ 122 #ifdef DO_CONFIG_PENDING 123 config_pending_incr(); 124 #endif 125 kthread_create_deferred(sdmmc_create_thread, sc); 126 } 127 128 int 129 sdmmc_detach(struct device *self, int flags) 130 { 131 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 132 133 sc->sc_dying = 1; 134 while (sc->sc_task_thread != NULL) { 135 wakeup(&sc->sc_tskq); 136 tsleep(sc, PWAIT, "mmcdie", 0); 137 } 138 return 0; 139 } 140 141 void 142 sdmmc_create_thread(void *arg) 143 { 144 struct sdmmc_softc *sc = arg; 145 146 if (kthread_create(sdmmc_task_thread, sc, &sc->sc_task_thread, 147 "%s", DEVNAME(sc)) != 0) 148 printf("%s: can't create task thread\n", DEVNAME(sc)); 149 150 #ifdef DO_CONFIG_PENDING 151 config_pending_decr(); 152 #endif 153 } 154 155 void 156 sdmmc_task_thread(void *arg) 157 { 158 struct sdmmc_softc *sc = arg; 159 struct sdmmc_task *task; 160 int s; 161 162 sdmmc_needs_discover(&sc->sc_dev); 163 164 s = splsdmmc(); 165 while (!sc->sc_dying) { 166 for (task = TAILQ_FIRST(&sc->sc_tskq); task != NULL; 167 task = TAILQ_FIRST(&sc->sc_tskq)) { 168 splx(s); 169 sdmmc_del_task(task); 170 task->func(task->arg); 171 s = splsdmmc(); 172 } 173 tsleep(&sc->sc_tskq, PWAIT, "mmctsk", 0); 174 } 175 splx(s); 176 177 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) 178 sdmmc_card_detach(sc, DETACH_FORCE); 179 180 sc->sc_task_thread = NULL; 181 wakeup(sc); 182 kthread_exit(0); 183 } 184 185 void 186 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task) 187 { 188 int s; 189 190 s = splsdmmc(); 191 TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next); 192 task->onqueue = 1; 193 task->sc = sc; 194 wakeup(&sc->sc_tskq); 195 splx(s); 196 } 197 198 void 199 sdmmc_del_task(struct sdmmc_task *task) 200 { 201 struct sdmmc_softc *sc = task->sc; 202 int s; 203 204 if (sc == NULL) 205 return; 206 207 s = splsdmmc(); 208 task->sc = NULL; 209 task->onqueue = 0; 210 TAILQ_REMOVE(&sc->sc_tskq, task, next); 211 splx(s); 212 } 213 214 void 215 sdmmc_needs_discover(struct device *self) 216 { 217 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 218 219 if (!sdmmc_task_pending(&sc->sc_discover_task)) 220 sdmmc_add_task(sc, &sc->sc_discover_task); 221 } 222 223 void 224 sdmmc_discover_task(void *arg) 225 { 226 struct sdmmc_softc *sc = arg; 227 228 if (sdmmc_chip_card_detect(sc->sct, sc->sch)) { 229 if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) { 230 SET(sc->sc_flags, SMF_CARD_PRESENT); 231 sdmmc_card_attach(sc); 232 } 233 } else { 234 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) { 235 CLR(sc->sc_flags, SMF_CARD_PRESENT); 236 sdmmc_card_detach(sc, DETACH_FORCE); 237 } 238 } 239 } 240 241 /* 242 * Called from process context when a card is present. 243 */ 244 void 245 sdmmc_card_attach(struct sdmmc_softc *sc) 246 { 247 DPRINTF(1,("%s: attach card\n", DEVNAME(sc))); 248 249 SDMMC_LOCK(sc); 250 CLR(sc->sc_flags, SMF_CARD_ATTACHED); 251 252 /* 253 * Power up the card (or card stack). 254 */ 255 if (sdmmc_enable(sc) != 0) { 256 printf("%s: can't enable card\n", DEVNAME(sc)); 257 goto err; 258 } 259 260 /* 261 * Scan for I/O functions and memory cards on the bus, 262 * allocating a sdmmc_function structure for each. 263 */ 264 if (sdmmc_scan(sc) != 0) { 265 printf("%s: no functions\n", DEVNAME(sc)); 266 goto err; 267 } 268 269 /* 270 * Initialize the I/O functions and memory cards. 271 */ 272 if (sdmmc_init(sc) != 0) { 273 printf("%s: init failed\n", DEVNAME(sc)); 274 goto err; 275 } 276 277 /* Attach SCSI emulation for memory cards. */ 278 if (ISSET(sc->sc_flags, SMF_MEM_MODE)) 279 sdmmc_scsi_attach(sc); 280 281 /* Attach I/O function drivers. */ 282 if (ISSET(sc->sc_flags, SMF_IO_MODE)) 283 sdmmc_io_attach(sc); 284 285 SET(sc->sc_flags, SMF_CARD_ATTACHED); 286 SDMMC_UNLOCK(sc); 287 return; 288 err: 289 sdmmc_card_detach(sc, DETACH_FORCE); 290 SDMMC_UNLOCK(sc); 291 } 292 293 /* 294 * Called from process context with DETACH_* flags from <sys/device.h> 295 * when cards are gone. 296 */ 297 void 298 sdmmc_card_detach(struct sdmmc_softc *sc, int flags) 299 { 300 struct sdmmc_function *sf, *sfnext; 301 302 DPRINTF(1,("%s: detach card\n", DEVNAME(sc))); 303 304 if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) { 305 /* Detach I/O function drivers. */ 306 if (ISSET(sc->sc_flags, SMF_IO_MODE)) 307 sdmmc_io_detach(sc); 308 309 /* Detach the SCSI emulation for memory cards. */ 310 if (ISSET(sc->sc_flags, SMF_MEM_MODE)) 311 sdmmc_scsi_detach(sc); 312 313 CLR(sc->sc_flags, SMF_CARD_ATTACHED); 314 } 315 316 /* Power down. */ 317 sdmmc_disable(sc); 318 319 /* Free all sdmmc_function structures. */ 320 for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) { 321 sfnext = SIMPLEQ_NEXT(sf, sf_list); 322 sdmmc_function_free(sf); 323 } 324 SIMPLEQ_INIT(&sc->sf_head); 325 sc->sc_function_count = 0; 326 sc->sc_fn0 = NULL; 327 } 328 329 int 330 sdmmc_enable(struct sdmmc_softc *sc) 331 { 332 u_int32_t host_ocr; 333 int error; 334 335 /* 336 * Calculate the equivalent of the card OCR from the host 337 * capabilities and select the maximum supported bus voltage. 338 */ 339 host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch); 340 error = sdmmc_chip_bus_power(sc->sct, sc->sch, host_ocr); 341 if (error != 0) { 342 printf("%s: can't supply bus power\n", DEVNAME(sc)); 343 goto err; 344 } 345 346 /* 347 * Select the minimum clock frequency. 348 */ 349 error = sdmmc_chip_bus_clock(sc->sct, sc->sch, SDMMC_SDCLK_400KHZ); 350 if (error != 0) { 351 printf("%s: can't supply clock\n", DEVNAME(sc)); 352 goto err; 353 } 354 355 /* XXX wait for card to power up */ 356 sdmmc_delay(100000); 357 358 /* Initialize SD I/O card function(s). */ 359 if ((error = sdmmc_io_enable(sc)) != 0) 360 goto err; 361 362 /* Initialize SD/MMC memory card(s). */ 363 if (ISSET(sc->sc_flags, SMF_MEM_MODE) && 364 (error = sdmmc_mem_enable(sc)) != 0) 365 goto err; 366 367 /* XXX respect host and card capabilities */ 368 if (ISSET(sc->sc_flags, SMF_SD_MODE)) 369 (void)sdmmc_chip_bus_clock(sc->sct, sc->sch, 370 SDMMC_SDCLK_25MHZ); 371 372 err: 373 if (error != 0) 374 sdmmc_disable(sc); 375 return error; 376 } 377 378 void 379 sdmmc_disable(struct sdmmc_softc *sc) 380 { 381 /* XXX complete commands if card is still present. */ 382 383 /* Make sure no card is still selected. */ 384 (void)sdmmc_select_card(sc, NULL); 385 386 /* Turn off bus power and clock. */ 387 (void)sdmmc_chip_bus_clock(sc->sct, sc->sch, SDMMC_SDCLK_OFF); 388 (void)sdmmc_chip_bus_power(sc->sct, sc->sch, 0); 389 } 390 391 /* 392 * Set the lowest bus voltage supported by the card and the host. 393 */ 394 int 395 sdmmc_set_bus_power(struct sdmmc_softc *sc, u_int32_t host_ocr, 396 u_int32_t card_ocr) 397 { 398 u_int32_t bit; 399 400 /* Mask off unsupported voltage levels and select the lowest. */ 401 DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr)); 402 host_ocr &= card_ocr; 403 for (bit = 4; bit < 23; bit++) { 404 if (ISSET(host_ocr, 1<<bit)) { 405 host_ocr &= 3<<bit; 406 break; 407 } 408 } 409 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr)); 410 411 if (host_ocr == 0 || 412 sdmmc_chip_bus_power(sc->sct, sc->sch, host_ocr) != 0) 413 return 1; 414 return 0; 415 } 416 417 struct sdmmc_function * 418 sdmmc_function_alloc(struct sdmmc_softc *sc) 419 { 420 struct sdmmc_function *sf; 421 422 sf = (struct sdmmc_function *)malloc(sizeof *sf, M_DEVBUF, 423 M_WAITOK | M_ZERO); 424 sf->sc = sc; 425 sf->number = -1; 426 sf->cis.manufacturer = SDMMC_VENDOR_INVALID; 427 sf->cis.product = SDMMC_PRODUCT_INVALID; 428 sf->cis.function = SDMMC_FUNCTION_INVALID; 429 return sf; 430 } 431 432 void 433 sdmmc_function_free(struct sdmmc_function *sf) 434 { 435 free(sf, M_DEVBUF); 436 } 437 438 /* 439 * Scan for I/O functions and memory cards on the bus, allocating a 440 * sdmmc_function structure for each. 441 */ 442 int 443 sdmmc_scan(struct sdmmc_softc *sc) 444 { 445 /* Scan for I/O functions. */ 446 if (ISSET(sc->sc_flags, SMF_IO_MODE)) 447 sdmmc_io_scan(sc); 448 449 /* Scan for memory cards on the bus. */ 450 if (ISSET(sc->sc_flags, SMF_MEM_MODE)) 451 sdmmc_mem_scan(sc); 452 453 /* There should be at least one function now. */ 454 if (SIMPLEQ_EMPTY(&sc->sf_head)) { 455 printf("%s: can't identify card\n", DEVNAME(sc)); 456 return 1; 457 } 458 return 0; 459 } 460 461 /* 462 * Initialize all the distinguished functions of the card, be it I/O 463 * or memory functions. 464 */ 465 int 466 sdmmc_init(struct sdmmc_softc *sc) 467 { 468 struct sdmmc_function *sf; 469 470 /* Initialize all identified card functions. */ 471 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) { 472 if (ISSET(sc->sc_flags, SMF_IO_MODE) && 473 sdmmc_io_init(sc, sf) != 0) 474 printf("%s: i/o init failed\n", DEVNAME(sc)); 475 476 if (ISSET(sc->sc_flags, SMF_MEM_MODE) && 477 sdmmc_mem_init(sc, sf) != 0) 478 printf("%s: mem init failed\n", DEVNAME(sc)); 479 } 480 481 /* Any good functions left after initialization? */ 482 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) { 483 if (!ISSET(sf->flags, SFF_ERROR)) 484 return 0; 485 } 486 /* No, we should probably power down the card. */ 487 return 1; 488 } 489 490 void 491 sdmmc_delay(u_int usecs) 492 { 493 int ticks = usecs / (1000000 / hz); 494 495 if (ticks > 0) 496 tsleep(&sdmmc_delay, PWAIT, "mmcdly", ticks); 497 else 498 delay(usecs); 499 } 500 501 int 502 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 503 { 504 struct sdmmc_command acmd; 505 int error; 506 507 SDMMC_LOCK(sc); 508 509 bzero(&acmd, sizeof acmd); 510 acmd.c_opcode = MMC_APP_CMD; 511 acmd.c_arg = 0; 512 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 513 514 error = sdmmc_mmc_command(sc, &acmd); 515 if (error != 0) { 516 SDMMC_UNLOCK(sc); 517 return error; 518 } 519 520 if (!ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) { 521 /* Card does not support application commands. */ 522 SDMMC_UNLOCK(sc); 523 return ENODEV; 524 } 525 526 error = sdmmc_mmc_command(sc, cmd); 527 SDMMC_UNLOCK(sc); 528 return error; 529 } 530 531 /* 532 * Execute MMC command and data transfers. All interactions with the 533 * host controller to complete the command happen in the context of 534 * the current process. 535 */ 536 int 537 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 538 { 539 int error; 540 541 SDMMC_LOCK(sc); 542 543 sdmmc_chip_exec_command(sc->sct, sc->sch, cmd); 544 545 #ifdef SDMMC_DEBUG 546 sdmmc_dump_command(sc, cmd); 547 #endif 548 549 error = cmd->c_error; 550 wakeup(cmd); 551 552 SDMMC_UNLOCK(sc); 553 return error; 554 } 555 556 /* 557 * Send the "GO IDLE STATE" command. 558 */ 559 void 560 sdmmc_go_idle_state(struct sdmmc_softc *sc) 561 { 562 struct sdmmc_command cmd; 563 564 bzero(&cmd, sizeof cmd); 565 cmd.c_opcode = MMC_GO_IDLE_STATE; 566 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0; 567 568 (void)sdmmc_mmc_command(sc, &cmd); 569 } 570 571 /* 572 * Retrieve (SD) or set (MMC) the relative card address (RCA). 573 */ 574 int 575 sdmmc_set_relative_addr(struct sdmmc_softc *sc, 576 struct sdmmc_function *sf) 577 { 578 struct sdmmc_command cmd; 579 580 bzero(&cmd, sizeof cmd); 581 582 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 583 cmd.c_opcode = SD_SEND_RELATIVE_ADDR; 584 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6; 585 } else { 586 cmd.c_opcode = MMC_SET_RELATIVE_ADDR; 587 cmd.c_arg = MMC_ARG_RCA(sf->rca); 588 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 589 } 590 591 if (sdmmc_mmc_command(sc, &cmd) != 0) 592 return 1; 593 594 if (ISSET(sc->sc_flags, SMF_SD_MODE)) 595 sf->rca = SD_R6_RCA(cmd.c_resp); 596 return 0; 597 } 598 599 /* 600 * Switch card and host to the maximum supported bus width. 601 */ 602 int 603 sdmmc_set_bus_width(struct sdmmc_function *sf) 604 { 605 struct sdmmc_softc *sc = sf->sc; 606 struct sdmmc_command cmd; 607 int error; 608 609 SDMMC_LOCK(sc); 610 611 if (!ISSET(sc->sc_flags, SMF_SD_MODE)) { 612 SDMMC_UNLOCK(sc); 613 return EOPNOTSUPP; 614 } 615 616 if ((error = sdmmc_select_card(sc, sf)) != 0) { 617 SDMMC_UNLOCK(sc); 618 return error; 619 } 620 621 bzero(&cmd, sizeof cmd); 622 cmd.c_opcode = SD_APP_SET_BUS_WIDTH; 623 cmd.c_arg = SD_ARG_BUS_WIDTH_4; 624 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 625 error = sdmmc_app_command(sc, &cmd); 626 SDMMC_UNLOCK(sc); 627 return error; 628 } 629 630 int 631 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf) 632 { 633 struct sdmmc_command cmd; 634 int error; 635 636 if (sc->sc_card == sf || (sf && sc->sc_card && 637 sc->sc_card->rca == sf->rca)) { 638 sc->sc_card = sf; 639 return 0; 640 } 641 642 bzero(&cmd, sizeof cmd); 643 cmd.c_opcode = MMC_SELECT_CARD; 644 cmd.c_arg = sf == NULL ? 0 : MMC_ARG_RCA(sf->rca); 645 cmd.c_flags = SCF_CMD_AC | (sf == NULL ? SCF_RSP_R0 : SCF_RSP_R1); 646 error = sdmmc_mmc_command(sc, &cmd); 647 if (error == 0 || sf == NULL) 648 sc->sc_card = sf; 649 return error; 650 } 651 652 #ifdef SDMMC_IOCTL 653 int 654 sdmmc_ioctl(struct device *self, u_long request, caddr_t addr) 655 { 656 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 657 struct sdmmc_command *ucmd; 658 struct sdmmc_command cmd; 659 void *data; 660 int error; 661 662 switch (request) { 663 #ifdef SDMMC_DEBUG 664 case SDIOCSETDEBUG: 665 sdmmcdebug = (((struct bio_sdmmc_debug *)addr)->debug) & 0xff; 666 sdhcdebug = (((struct bio_sdmmc_debug *)addr)->debug >> 8) & 0xff; 667 break; 668 #endif 669 670 case SDIOCEXECMMC: 671 case SDIOCEXECAPP: 672 ucmd = &((struct bio_sdmmc_command *)addr)->cmd; 673 674 /* Refuse to transfer more than 512K per command. */ 675 if (ucmd->c_datalen > 524288) 676 return ENOMEM; 677 678 /* Verify that the data buffer is safe to copy. */ 679 if ((ucmd->c_datalen > 0 && ucmd->c_data == NULL) || 680 (ucmd->c_datalen < 1 && ucmd->c_data != NULL) || 681 ucmd->c_datalen < 0) 682 return EINVAL; 683 684 bzero(&cmd, sizeof cmd); 685 cmd.c_opcode = ucmd->c_opcode; 686 cmd.c_arg = ucmd->c_arg; 687 cmd.c_flags = ucmd->c_flags; 688 cmd.c_blklen = ucmd->c_blklen; 689 690 if (ucmd->c_data) { 691 data = malloc(ucmd->c_datalen, M_TEMP, 692 M_WAITOK | M_CANFAIL); 693 if (data == NULL) 694 return ENOMEM; 695 if (copyin(ucmd->c_data, data, ucmd->c_datalen)) 696 return EFAULT; 697 698 cmd.c_data = data; 699 cmd.c_datalen = ucmd->c_datalen; 700 } 701 702 if (request == SDIOCEXECMMC) 703 error = sdmmc_mmc_command(sc, &cmd); 704 else 705 error = sdmmc_app_command(sc, &cmd); 706 if (error && !cmd.c_error) 707 cmd.c_error = error; 708 709 bcopy(&cmd.c_resp, ucmd->c_resp, sizeof cmd.c_resp); 710 ucmd->c_flags = cmd.c_flags; 711 ucmd->c_error = cmd.c_error; 712 713 if (ucmd->c_data && copyout(data, ucmd->c_data, 714 ucmd->c_datalen)) 715 return EFAULT; 716 717 if (ucmd->c_data) 718 free(data, M_TEMP); 719 break; 720 721 default: 722 return ENOTTY; 723 } 724 return 0; 725 } 726 #endif 727 728 #ifdef SDMMC_DEBUG 729 void 730 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 731 { 732 int i; 733 734 DPRINTF(1,("%s: cmd %u arg=%#x data=%#x dlen=%d flags=%#x " 735 "proc=\"%s\" (error %d)\n", DEVNAME(sc), cmd->c_opcode, 736 cmd->c_arg, cmd->c_data, cmd->c_datalen, cmd->c_flags, 737 curproc ? curproc->p_comm : "", cmd->c_error)); 738 739 if (cmd->c_error || sdmmcdebug < 1) 740 return; 741 742 printf("%s: resp=", DEVNAME(sc)); 743 if (ISSET(cmd->c_flags, SCF_RSP_136)) 744 for (i = 0; i < sizeof cmd->c_resp; i++) 745 printf("%02x ", ((u_char *)cmd->c_resp)[i]); 746 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) 747 for (i = 0; i < 4; i++) 748 printf("%02x ", ((u_char *)cmd->c_resp)[i]); 749 printf("\n"); 750 } 751 #endif 752