The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ata/ata-usb.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2006 - 2007 Søren Schmidt <sos@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer,
   10  *    without modification, immediately at the beginning of the file.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_ata.h"
   31 #include <sys/param.h>
   32 #include <sys/conf.h>
   33 #include <sys/systm.h>
   34 #include <sys/kernel.h>
   35 #include <sys/mutex.h>
   36 #include <sys/ata.h>
   37 #include <sys/bus.h>
   38 #include <sys/endian.h>
   39 #include <sys/sysctl.h>
   40 #include <sys/malloc.h>
   41 #include <sys/module.h>
   42 #include <sys/sema.h>
   43 #include <sys/taskqueue.h>
   44 #include <vm/uma.h>
   45 #include <machine/resource.h>
   46 #include <machine/bus.h>
   47 #include <sys/rman.h>
   48 #include <dev/usb/usb_port.h>
   49 #include <dev/usb/usb.h>
   50 #include <dev/usb/usbdi.h>
   51 #include <dev/usb/usbdi_util.h>
   52 #include <dev/usb/usbdivar.h>
   53 #include <dev/ata/ata-all.h>
   54 #include <ata_if.h>
   55 
   56 /* Command Block Wrapper */
   57 struct bbb_cbw {
   58     u_int8_t    signature[4];
   59 #define         CBWSIGNATURE            0x43425355
   60 
   61     u_int8_t    tag[4];
   62     u_int8_t    transfer_length[4];
   63     u_int8_t    flags;
   64 #define         CBWFLAGS_OUT            0x00
   65 #define         CBWFLAGS_IN             0x80
   66 
   67     u_int8_t    lun;
   68     u_int8_t    length;
   69 #define         CBWCDBLENGTH            16
   70 
   71     u_int8_t    cdb[CBWCDBLENGTH];
   72 };
   73 
   74 /* Command Status Wrapper */
   75 struct bbb_csw {
   76     u_int8_t    signature[4];
   77 #define         CSWSIGNATURE            0x53425355
   78 
   79     u_int8_t    tag[4];
   80     u_int8_t    residue[4];
   81     u_int8_t    status;
   82 #define         CSWSTATUS_GOOD          0x0
   83 #define         CSWSTATUS_FAILED        0x1
   84 #define         CSWSTATUS_PHASE         0x2
   85 };
   86 
   87 /* USB-ATA 'controller' softc */
   88 struct atausb_softc {
   89     device_t            dev;            /* base device */
   90     usbd_interface_handle iface;        /* interface */
   91     int                 ifaceno;        /* interface number */
   92     u_int8_t            bulkin;         /* endpoint address's */
   93     u_int8_t            bulkout;        
   94     u_int8_t            bulkirq;        
   95     usbd_pipe_handle    bulkin_pipe;    /* pipe handle's */
   96     usbd_pipe_handle    bulkout_pipe;
   97     usbd_pipe_handle    bulkirq_pipe;
   98     int                 maxlun;
   99     int                 timeout;
  100     struct ata_request  *ata_request;
  101     usb_device_request_t usb_request;
  102     struct bbb_cbw      cbw;
  103     struct bbb_csw      csw;
  104 
  105 #define ATAUSB_T_BBB_CBW                0
  106 #define ATAUSB_T_BBB_DATA               1
  107 #define ATAUSB_T_BBB_DCLEAR             2
  108 #define ATAUSB_T_BBB_CSW1               3
  109 #define ATAUSB_T_BBB_CSW2               4
  110 #define ATAUSB_T_BBB_SCLEAR             5
  111 #define ATAUSB_T_BBB_RESET1             6
  112 #define ATAUSB_T_BBB_RESET2             7
  113 #define ATAUSB_T_BBB_RESET3             8
  114 #define ATAUSB_T_MAX                    9
  115     usbd_xfer_handle    transfer[ATAUSB_T_MAX];
  116 
  117     int                 state;
  118 #define ATAUSB_S_ATTACH                 0
  119 #define ATAUSB_S_IDLE                   1
  120 #define ATAUSB_S_BBB_COMMAND            2
  121 #define ATAUSB_S_BBB_DATA               3
  122 #define ATAUSB_S_BBB_DCLEAR             4
  123 #define ATAUSB_S_BBB_STATUS1            5
  124 #define ATAUSB_S_BBB_SCLEAR             6
  125 #define ATAUSB_S_BBB_STATUS2            7
  126 #define ATAUSB_S_BBB_RESET1             8
  127 #define ATAUSB_S_BBB_RESET2             9
  128 #define ATAUSB_S_BBB_RESET3             10
  129 #define ATAUSB_S_DETACH                 11
  130 
  131     struct mtx          locked_mtx;
  132     struct ata_channel  *locked_ch;
  133     struct ata_channel  *restart_ch;
  134 };
  135 
  136 static int atausbdebug = 0;
  137 
  138 /* prototypes*/
  139 static usbd_status atausb_start(struct atausb_softc *sc, usbd_pipe_handle pipe, void *buffer, int buflen, int flags, usbd_xfer_handle xfer);
  140 static usbd_status atausb_ctl_start(struct atausb_softc *sc, usbd_device_handle udev, usb_device_request_t *req, void *buffer, int buflen, int flags, usbd_xfer_handle xfer);
  141 static void atausb_clear_stall(struct atausb_softc *sc, u_int8_t endpt, usbd_pipe_handle pipe, int state, usbd_xfer_handle xfer);
  142 static void atausb_bbb_reset(struct atausb_softc *sc);
  143 static int atausb_bbb_start(struct ata_request *request);
  144 static void atausb_bbb_finish(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status err);
  145 int ata_usbchannel_begin_transaction(struct ata_request *request);
  146 int ata_usbchannel_end_transaction(struct ata_request *request);
  147 
  148 
  149 /*
  150  * USB frontend part
  151  */
  152 USB_DECLARE_DRIVER(atausb);
  153 DRIVER_MODULE(atausb, uhub, atausb_driver, atausb_devclass, 0, 0);
  154 MODULE_VERSION(atausb, 1);
  155 
  156 static int
  157 atausb_match(device_t dev)
  158 {
  159     struct usb_attach_arg *uaa = device_get_ivars(dev);
  160     usb_interface_descriptor_t *id;
  161 
  162     if (uaa->iface == NULL)
  163         return UMATCH_NONE;
  164 
  165     id = usbd_get_interface_descriptor(uaa->iface);
  166     if (!id || id->bInterfaceClass != UICLASS_MASS)
  167         return UMATCH_NONE;
  168 
  169     switch (id->bInterfaceSubClass) {
  170     case UISUBCLASS_QIC157:
  171     case UISUBCLASS_RBC:
  172     case UISUBCLASS_SCSI:
  173     case UISUBCLASS_SFF8020I:
  174     case UISUBCLASS_SFF8070I:
  175     case UISUBCLASS_UFI:
  176         switch (id->bInterfaceProtocol) {
  177         case UIPROTO_MASS_CBI:
  178         case UIPROTO_MASS_CBI_I:
  179         case UIPROTO_MASS_BBB:
  180         case UIPROTO_MASS_BBB_OLD:
  181             return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
  182         default:
  183             return UMATCH_IFACECLASS_IFACESUBCLASS;
  184         }
  185         break;
  186     default:
  187         return UMATCH_IFACECLASS;
  188     }
  189 }
  190 
  191 static int
  192 atausb_attach(device_t dev)
  193 {
  194     struct atausb_softc *sc = device_get_softc(dev);
  195     struct usb_attach_arg *uaa = device_get_ivars(dev);
  196     usb_interface_descriptor_t *id;
  197     usb_endpoint_descriptor_t *ed;
  198     usbd_device_handle udev;
  199     usb_device_request_t request;
  200     char devinfo[1024], *proto, *subclass;
  201     u_int8_t maxlun;
  202     int err, i;
  203 
  204     sc->dev = dev;
  205     usbd_devinfo(uaa->device, 0, devinfo);
  206     device_set_desc_copy(dev, devinfo);
  207     sc->bulkin = sc->bulkout = sc->bulkirq = -1;
  208     sc->bulkin_pipe = sc->bulkout_pipe= sc->bulkirq_pipe = NULL;
  209     sc->iface = uaa->iface;
  210     sc->ifaceno = uaa->ifaceno;
  211     sc->maxlun = 0;
  212     sc->timeout = 5000;
  213     sc->locked_ch = NULL;
  214     sc->restart_ch = NULL;
  215     mtx_init(&sc->locked_mtx, "ATAUSB lock", NULL, MTX_DEF); 
  216 
  217     id = usbd_get_interface_descriptor(sc->iface);
  218     switch (id->bInterfaceProtocol) {
  219     case UIPROTO_MASS_BBB:
  220     case UIPROTO_MASS_BBB_OLD:
  221             proto = "Bulk-Only";
  222             break;
  223     case UIPROTO_MASS_CBI:
  224             proto = "CBI";
  225             break;
  226     case UIPROTO_MASS_CBI_I:
  227             proto = "CBI with CCI";
  228             break;
  229     default:
  230             proto = "Unknown";
  231     }
  232     switch (id->bInterfaceSubClass) {
  233     case UISUBCLASS_RBC:
  234             subclass = "RBC";
  235             break;
  236     case UISUBCLASS_QIC157:
  237     case UISUBCLASS_SFF8020I:
  238     case UISUBCLASS_SFF8070I:
  239             subclass = "ATAPI";
  240             break;
  241     case UISUBCLASS_SCSI:
  242             subclass = "SCSI";
  243             break;
  244     case UISUBCLASS_UFI:
  245             subclass = "UFI";
  246             break;
  247     default:
  248             subclass = "Unknown";
  249     }
  250     device_printf(dev, "using %s over %s\n", subclass, proto);
  251     if (strcmp(proto, "Bulk-Only") ||
  252         (strcmp(subclass, "ATAPI") && strcmp(subclass, "SCSI")))
  253         return ENXIO;
  254 
  255     for (i = 0 ; i < id->bNumEndpoints ; i++) {
  256         if (!(ed = usbd_interface2endpoint_descriptor(sc->iface, i))) {
  257             device_printf(sc->dev, "could not read endpoint descriptor\n");
  258             return ENXIO;
  259         }
  260         if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  261             (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
  262             sc->bulkin = ed->bEndpointAddress;
  263         }
  264         if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
  265                    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
  266             sc->bulkout = ed->bEndpointAddress;
  267         }
  268         if (id->bInterfaceProtocol == UIPROTO_MASS_CBI_I &&
  269                    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  270                    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
  271             sc->bulkirq = ed->bEndpointAddress;
  272         }
  273     }
  274 
  275     /* check whether we found at least the endpoints we need */
  276     if (!sc->bulkin || !sc->bulkout) {
  277         device_printf(sc->dev, "needed endpoints not found (%d,%d)\n",
  278                       sc->bulkin, sc->bulkout);
  279         atausb_detach(dev);
  280         return ENXIO;
  281     }
  282 
  283     /* open the pipes */
  284     if (usbd_open_pipe(sc->iface, sc->bulkout,
  285                        USBD_EXCLUSIVE_USE, &sc->bulkout_pipe)) {
  286         device_printf(sc->dev, "cannot open bulkout pipe (%d)\n", sc->bulkout);
  287         atausb_detach(dev);
  288         return ENXIO;
  289     }
  290     if (usbd_open_pipe(sc->iface, sc->bulkin,
  291                        USBD_EXCLUSIVE_USE, &sc->bulkin_pipe)) {
  292         device_printf(sc->dev, "cannot open bulkin pipe (%d)\n", sc->bulkin);
  293         atausb_detach(dev);
  294         return ENXIO;
  295     }
  296     if (id->bInterfaceProtocol == UIPROTO_MASS_CBI_I) {
  297         if (usbd_open_pipe(sc->iface, sc->bulkirq,
  298                            USBD_EXCLUSIVE_USE, &sc->bulkirq_pipe)) {
  299             device_printf(sc->dev, "cannot open bulkirq pipe (%d)\n",
  300                           sc->bulkirq);
  301             atausb_detach(dev);
  302             return ENXIO;
  303         }
  304     }
  305     sc->state = ATAUSB_S_ATTACH;
  306 
  307     /* alloc needed number of transfer handles */
  308     for (i = 0; i < ATAUSB_T_MAX; i++) {
  309         sc->transfer[i] = usbd_alloc_xfer(uaa->device);
  310         if (!sc->transfer[i]) {
  311             device_printf(sc->dev, "out of memory\n");
  312             atausb_detach(dev);
  313             return ENXIO;
  314         }
  315     }
  316 
  317     /* driver is ready to process requests here */
  318     sc->state = ATAUSB_S_IDLE;
  319 
  320     /* get number of devices so we can add matching channels */
  321     usbd_interface2device_handle(sc->iface, &udev);
  322     request.bmRequestType = UT_READ_CLASS_INTERFACE;
  323     request.bRequest = 0xfe; //GET_MAX_LUN;
  324     USETW(request.wValue, 0);
  325     USETW(request.wIndex, sc->ifaceno);
  326     USETW(request.wLength, sizeof(maxlun));
  327     switch ((err = usbd_do_request(udev, &request, &maxlun))) {
  328     case USBD_NORMAL_COMPLETION:
  329         if (bootverbose)
  330             device_printf(sc->dev, "maxlun=%d\n", maxlun);
  331         sc->maxlun = maxlun;
  332         break;
  333     default:
  334         if (bootverbose)
  335             device_printf(sc->dev, "get maxlun not supported %s\n",
  336         usbd_errstr(err));
  337     }
  338 
  339     /* ata channels are children to this USB control device */
  340     for (i = 0; i <= sc->maxlun; i++) {
  341         if (!device_add_child(sc->dev, "ata",
  342                               devclass_find_free_unit(ata_devclass, 2))) {
  343             device_printf(sc->dev, "failed to attach ata child device\n");
  344             atausb_detach(dev);
  345             return ENXIO;
  346         }
  347     }
  348     bus_generic_attach(sc->dev);
  349     return 0;
  350 }
  351 
  352 static int
  353 atausb_detach(device_t dev)
  354 {
  355     struct atausb_softc *sc = device_get_softc(dev);
  356     usbd_device_handle udev;
  357     device_t *children;
  358     int nchildren, i;
  359 
  360     /* signal that device is going away */
  361     sc->state = ATAUSB_S_DETACH;
  362 
  363     /* abort all the pipes in case there are active transfers */
  364     usbd_interface2device_handle(sc->iface, &udev);
  365     usbd_abort_default_pipe(udev);
  366     if (sc->bulkout_pipe)
  367         usbd_abort_pipe(sc->bulkout_pipe);
  368     if (sc->bulkin_pipe)
  369         usbd_abort_pipe(sc->bulkin_pipe);
  370     if (sc->bulkirq_pipe)
  371         usbd_abort_pipe(sc->bulkirq_pipe);
  372 
  373     /* detach & delete all children */
  374     if (!device_get_children(dev, &children, &nchildren)) {
  375         for (i = 0; i < nchildren; i++)
  376             device_delete_child(dev, children[i]);
  377         free(children, M_TEMP);
  378     }
  379 
  380     /* free the transfers */
  381     for (i = 0; i < ATAUSB_T_MAX; i++)
  382         if (sc->transfer[i])
  383             usbd_free_xfer(sc->transfer[i]);
  384 
  385     /* remove all the pipes */
  386     if (sc->bulkout_pipe)
  387         usbd_close_pipe(sc->bulkout_pipe);
  388     if (sc->bulkin_pipe)
  389         usbd_close_pipe(sc->bulkin_pipe);
  390     if (sc->bulkirq_pipe)
  391         usbd_close_pipe(sc->bulkirq_pipe);
  392 
  393     mtx_destroy(&sc->locked_mtx);
  394     return 0;
  395 }
  396 
  397 
  398 /*
  399  * Generic USB transfer routines 
  400  */
  401 static usbd_status
  402 atausb_start(struct atausb_softc *sc, usbd_pipe_handle pipe,
  403              void *buffer, int buflen, int flags, usbd_xfer_handle xfer)
  404 {
  405     usbd_status err;
  406 
  407     if (sc->state == ATAUSB_S_DETACH)
  408         return USBD_NOT_STARTED;
  409 
  410     usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen, flags,
  411                     sc->timeout, atausb_bbb_finish);
  412     err = usbd_transfer(xfer);
  413     if (err && (err != USBD_IN_PROGRESS)) {
  414         if (atausbdebug)
  415             device_printf(sc->dev, "failed to setup transfer, %s\n",
  416                           usbd_errstr(err));
  417         return err;
  418     }
  419     return USBD_NORMAL_COMPLETION;
  420 }
  421 
  422 static usbd_status
  423 atausb_ctl_start(struct atausb_softc *sc, usbd_device_handle udev,
  424                  usb_device_request_t *req, void *buffer, int buflen, int flags,
  425                  usbd_xfer_handle xfer)
  426 {
  427     usbd_status err;
  428 
  429     if (sc->state == ATAUSB_S_DETACH)
  430         return USBD_NOT_STARTED;
  431 
  432     usbd_setup_default_xfer(xfer, udev, (void *)sc, sc->timeout, req,
  433                             buffer, buflen, flags, atausb_bbb_finish);
  434     err = usbd_transfer(xfer);
  435     if (err && (err != USBD_IN_PROGRESS)) {
  436         if (atausbdebug)
  437             device_printf(sc->dev, "failed to setup ctl transfer, %s\n",
  438                           usbd_errstr(err));
  439         return err;
  440     }
  441     return USBD_NORMAL_COMPLETION;
  442 }
  443 
  444 static void
  445 atausb_clear_stall(struct atausb_softc *sc, u_int8_t endpt,
  446                    usbd_pipe_handle pipe, int state, usbd_xfer_handle xfer)
  447 {
  448     usbd_device_handle udev;
  449 
  450     if (atausbdebug)
  451         device_printf(sc->dev, "clear endpoint 0x%02x stall\n", endpt);
  452     usbd_interface2device_handle(sc->iface, &udev);
  453     sc->state = state;
  454     usbd_clear_endpoint_toggle(pipe);
  455     sc->usb_request.bmRequestType = UT_WRITE_ENDPOINT;
  456     sc->usb_request.bRequest = UR_CLEAR_FEATURE;
  457     USETW(sc->usb_request.wValue, UF_ENDPOINT_HALT);
  458     USETW(sc->usb_request.wIndex, endpt);
  459     USETW(sc->usb_request.wLength, 0);
  460     atausb_ctl_start(sc, udev, &sc->usb_request, NULL, 0, 0, xfer);
  461 }
  462 
  463 
  464 /*
  465  * Bulk-Only transport part
  466  */
  467 static void
  468 atausb_bbb_reset(struct atausb_softc *sc)
  469 {
  470     usbd_device_handle udev;
  471 
  472     if (atausbdebug)
  473         device_printf(sc->dev, "Bulk Reset\n");
  474     sc->timeout = 5000;
  475     sc->state = ATAUSB_S_BBB_RESET1;
  476     usbd_interface2device_handle(sc->iface, &udev);
  477     sc->usb_request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
  478     sc->usb_request.bRequest = 0xff; /* bulk-only reset */
  479     USETW(sc->usb_request.wValue, 0);
  480     USETW(sc->usb_request.wIndex, sc->ifaceno);
  481     USETW(sc->usb_request.wLength, 0);
  482     atausb_ctl_start(sc, udev, &sc->usb_request, NULL,
  483                      0, 0, sc->transfer[ATAUSB_T_BBB_RESET1]);
  484 }
  485 
  486 static int
  487 atausb_bbb_start(struct ata_request *request)
  488 {
  489     struct atausb_softc *sc = 
  490         device_get_softc(device_get_parent(request->parent));
  491     struct ata_channel *ch = device_get_softc(request->parent);
  492 
  493     sc->timeout = (request->timeout * 1000) + 5000;
  494     USETDW(sc->cbw.signature, CBWSIGNATURE);
  495     USETDW(sc->cbw.tag, UGETDW(sc->cbw.tag) + 1);
  496     USETDW(sc->cbw.transfer_length, request->bytecount);
  497     sc->cbw.flags = (request->flags & ATA_R_READ) ? CBWFLAGS_IN : CBWFLAGS_OUT;
  498     sc->cbw.lun = ch->unit;
  499     sc->cbw.length = 16;
  500     bzero(sc->cbw.cdb, 16);
  501     bcopy(request->u.atapi.ccb, sc->cbw.cdb, 12); /* XXX SOS */
  502     sc->state = ATAUSB_S_BBB_COMMAND;
  503     if (atausb_start(sc, sc->bulkout_pipe, &sc->cbw, sizeof(struct bbb_cbw),
  504                      0, sc->transfer[ATAUSB_T_BBB_CBW])) {
  505         request->result = EIO;
  506         if (atausbdebug)
  507             device_printf(request->dev, "cannot setup USB transfer\n");
  508         atausb_bbb_reset(sc);
  509         return ATA_OP_FINISHED;
  510     }
  511     return ATA_OP_CONTINUES;
  512 }
  513 
  514 static void
  515 atausb_bbb_finish(usbd_xfer_handle xfer, usbd_private_handle priv,
  516                  usbd_status err)
  517 {
  518     struct atausb_softc *sc = (struct atausb_softc *)priv;
  519     struct ata_request *request = sc->ata_request;
  520     usbd_xfer_handle next_xfer;
  521 
  522     //device_printf(sc->dev, "BBB state %d: %s\n", sc->state, usbd_errstr(err));
  523 
  524     if (sc->state == ATAUSB_S_DETACH) {
  525         device_printf(sc->dev, "WARNING - device has been removed\n");
  526         return;
  527     }
  528 
  529     switch (sc->state) {
  530     case ATAUSB_S_BBB_COMMAND:  /* command transport phase */
  531         if (err) {
  532             if (atausbdebug)
  533                 device_printf(sc->dev, "failed to send CBW\n");
  534             request->result = EIO;
  535             atausb_bbb_reset(sc);
  536             return;
  537         }
  538 
  539         /* next is data transport phase, setup transfer */
  540         sc->state = ATAUSB_S_BBB_DATA;
  541         if (request->flags & ATA_R_READ) {
  542             if (atausb_start(sc, sc->bulkin_pipe,
  543                              request->data, request->bytecount,
  544                              USBD_SHORT_XFER_OK,
  545                              sc->transfer[ATAUSB_T_BBB_DATA])) {
  546                 request->result = EIO;
  547                 atausb_bbb_reset(sc);
  548             }
  549             return;
  550         }
  551         if (request->flags & ATA_R_WRITE) {
  552             if (atausb_start(sc, sc->bulkout_pipe,
  553                              request->data, request->bytecount,
  554                              0, sc->transfer[ATAUSB_T_BBB_DATA])) {
  555                 request->result = EIO;
  556                 atausb_bbb_reset(sc);
  557             }
  558             return;
  559         }
  560         /* FALLTHROUGH */
  561 
  562     case ATAUSB_S_BBB_DATA:     /* data transport phase */
  563         if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
  564             usbd_get_xfer_status(xfer, NULL, NULL, &request->donecount, NULL);
  565             if (err) {
  566                 if (atausbdebug)
  567                     device_printf(sc->dev, "data %s count %d failed: %s\n",
  568                                   (request->flags & ATA_R_READ?"read":"write"),
  569                                   request->bytecount, usbd_errstr(err));
  570                 if (err == USBD_STALLED) {
  571                     atausb_clear_stall(sc,
  572                                        (request->flags & ATA_R_READ ?
  573                                         sc->bulkin : sc->bulkout),
  574                                        (request->flags & ATA_R_READ ?
  575                                         sc->bulkin_pipe : sc->bulkout_pipe),
  576                                        ATAUSB_S_BBB_DCLEAR,
  577                                        sc->transfer[ATAUSB_T_BBB_DCLEAR]);
  578                 }
  579                 else {
  580                     request->result = EIO;
  581                     atausb_bbb_reset(sc);
  582                 }
  583                 return;
  584             }
  585         }
  586         /* FALLTHROUGH */
  587 
  588     case ATAUSB_S_BBB_DCLEAR:   /* stall clear after data phase */
  589     case ATAUSB_S_BBB_SCLEAR:   /* stall clear after status phase */
  590         if (err) {
  591             if (atausbdebug)
  592                 device_printf(sc->dev, "bulk%s stall clear failed %s\n",
  593                               (request->flags & ATA_R_READ ? "in" : "out"),
  594                               usbd_errstr(err));
  595             request->result = EIO;
  596             atausb_bbb_reset(sc);
  597             return;
  598         }
  599 
  600         if (sc->state == ATAUSB_S_BBB_COMMAND ||
  601             sc->state == ATAUSB_S_BBB_DATA ||
  602             sc->state == ATAUSB_S_BBB_DCLEAR) {
  603             /* first attempt on status transport phase setup transfer */
  604             sc->state = ATAUSB_S_BBB_STATUS1;
  605             next_xfer = sc->transfer[ATAUSB_T_BBB_CSW1];
  606         }
  607         else {
  608             /* second attempt of fetching status */
  609             sc->state = ATAUSB_S_BBB_STATUS2;
  610             next_xfer = sc->transfer[ATAUSB_T_BBB_CSW2];
  611         }
  612         if (atausb_start(sc, sc->bulkin_pipe, &sc->csw, sizeof(struct bbb_csw),
  613                          USBD_SHORT_XFER_OK, next_xfer)) {
  614             request->result = EIO;
  615             atausb_bbb_reset(sc);
  616         }
  617         return;
  618 
  619     case ATAUSB_S_BBB_STATUS1:  /* status transfer first attempt */
  620     case ATAUSB_S_BBB_STATUS2:  /* status transfer second attempt */
  621         if (err) {
  622             if (atausbdebug)
  623                 device_printf(sc->dev, "cannot get CSW, %s%s\n",
  624                               usbd_errstr(err),
  625                               sc->state == ATAUSB_S_BBB_STATUS1 ? ", retry":"");
  626             if (sc->state == ATAUSB_S_BBB_STATUS1) {
  627                 atausb_clear_stall(sc, sc->bulkin, sc->bulkin_pipe,
  628                                    ATAUSB_S_BBB_SCLEAR,
  629                                    sc->transfer[ATAUSB_T_BBB_SCLEAR]);
  630             }
  631             else {
  632                 request->result = EIO;
  633                 atausb_bbb_reset(sc);
  634             }
  635             return;
  636         }
  637 
  638         int residue = UGETDW(sc->csw.residue);
  639 
  640         if (!residue &&
  641             (request->bytecount - request->donecount))
  642             residue = request->bytecount - request->donecount;
  643 
  644         /* check CSW and handle eventual error */
  645         if (UGETDW(sc->csw.signature) != CSWSIGNATURE) {
  646             if (atausbdebug)
  647                 device_printf(sc->dev, "bad CSW signature 0x%08x != 0x%08x\n",
  648                               UGETDW(sc->csw.signature), CSWSIGNATURE);
  649             request->result = EIO;
  650             atausb_bbb_reset(sc);
  651             return;
  652         }
  653         else if (UGETDW(sc->csw.tag) != UGETDW(sc->cbw.tag)) {
  654             if (atausbdebug)
  655                 device_printf(sc->dev, "bad CSW tag %d != %d\n",
  656                               UGETDW(sc->csw.tag), UGETDW(sc->cbw.tag));
  657             request->result = EIO;
  658             atausb_bbb_reset(sc);
  659             return;
  660         }
  661         else if (sc->csw.status > CSWSTATUS_PHASE) {
  662             if (atausbdebug)
  663                 device_printf(sc->dev, "bad CSW status %d > %d\n",
  664                               sc->csw.status, CSWSTATUS_PHASE);
  665             request->result = EIO;
  666             atausb_bbb_reset(sc);
  667             return;
  668         }
  669         else if (sc->csw.status == CSWSTATUS_PHASE) {
  670             if (atausbdebug)
  671                 device_printf(sc->dev, "phase error residue = %d\n", residue);
  672             request->result = EIO;
  673             atausb_bbb_reset(sc);
  674             return;
  675         }
  676         else if (request->donecount > request->bytecount) {
  677             if (atausbdebug)
  678                 device_printf(sc->dev, "buffer overrun %d > %d",
  679                              request->donecount, request->bytecount);
  680             request->result = EIO;
  681             atausb_bbb_reset(sc);
  682             return;
  683         }
  684         else if (sc->csw.status == CSWSTATUS_FAILED) {
  685             if (atausbdebug)
  686                 device_printf(sc->dev, "CSWSTATUS_FAILED\n");
  687             request->error = ATA_E_ATAPI_SENSE_MASK ;
  688             sc->state = ATAUSB_S_IDLE;
  689             ata_interrupt(device_get_softc(request->parent));
  690             return;
  691         }
  692         else {
  693             sc->state = ATAUSB_S_IDLE;
  694             ata_interrupt(device_get_softc(request->parent));
  695             return;
  696         }
  697         /* NOT REACHED */
  698 
  699     case ATAUSB_S_BBB_RESET1:
  700         if (err)
  701             if (atausbdebug)
  702                 device_printf(sc->dev,
  703                               "BBB reset failure: %s\n", usbd_errstr(err));
  704         atausb_clear_stall(sc, sc->bulkin, sc->bulkin_pipe,
  705                            ATAUSB_S_BBB_RESET2,
  706                            sc->transfer[ATAUSB_T_BBB_RESET2]);
  707         return;
  708 
  709     case ATAUSB_S_BBB_RESET2:
  710         if (err)
  711             if (atausbdebug)
  712                 device_printf(sc->dev, "BBB bulkin clear stall failure: %s\n",
  713                               usbd_errstr(err));
  714         atausb_clear_stall(sc, sc->bulkout, sc->bulkout_pipe,
  715                            ATAUSB_S_BBB_RESET3,
  716                            sc->transfer[ATAUSB_T_BBB_RESET3]);
  717         return;
  718 
  719     case ATAUSB_S_BBB_RESET3:
  720         if (err)
  721             if (atausbdebug)
  722                 device_printf(sc->dev, "BBB bulk-out clear stall failure: %s\n",
  723                               usbd_errstr(err));
  724         sc->state = ATAUSB_S_IDLE;
  725         if (request) {
  726             if (err)
  727                 request->result = ENXIO;
  728             else
  729                 request->result = EIO;
  730             ata_interrupt(device_get_softc(request->parent));
  731         }
  732         return;
  733 
  734     default:
  735         if (atausbdebug)
  736             device_printf(sc->dev, "unknown state %d", sc->state);
  737     }
  738 }
  739 
  740 
  741 /*
  742  * ATA backend part
  743  */
  744 struct atapi_inquiry {
  745     u_int8_t    device_type;
  746     u_int8_t    device_modifier;
  747     u_int8_t    version;
  748     u_int8_t    response_format;
  749     u_int8_t    length;
  750     u_int8_t    reserved[2];
  751     u_int8_t    flags;
  752     u_int8_t    vendor[8];
  753     u_int8_t    product[16];
  754     u_int8_t    revision[4];
  755     //u_int8_t    crap[60];
  756 };
  757 
  758 int
  759 ata_usbchannel_begin_transaction(struct ata_request *request)
  760 {
  761     struct atausb_softc *sc = 
  762         device_get_softc(device_get_parent(request->parent));
  763 
  764     if (atausbdebug > 1)
  765         device_printf(request->dev, "begin_transaction %s\n",
  766                       ata_cmd2str(request));
  767 
  768     /* sanity just in case */
  769     if (sc->state != ATAUSB_S_IDLE) {
  770         printf("begin is busy (%d)\n", sc->state);
  771         request->result = EBUSY;
  772         return ATA_OP_FINISHED;
  773     }
  774 
  775     /* XXX SOS convert the request into the format used, only BBB for now*/
  776     sc->ata_request = request;
  777 
  778     /* ATA/ATAPI IDENTIFY needs special treatment */
  779     if (!(request->flags & ATA_R_ATAPI)) {
  780         if (request->u.ata.command != ATA_ATAPI_IDENTIFY) {
  781             device_printf(request->dev,"%s unsupported\n",ata_cmd2str(request));
  782             request->result = EIO;
  783             return ATA_OP_FINISHED;
  784         }
  785         request->flags |= ATA_R_ATAPI;
  786         bzero(request->u.atapi.ccb, 16);
  787         request->u.atapi.ccb[0] = ATAPI_INQUIRY;
  788         request->u.atapi.ccb[4] =  255; //sizeof(struct atapi_inquiry);
  789         request->data += 256;   /* arbitrary offset into ata_param */
  790         request->bytecount = 255; //sizeof(struct atapi_inquiry);
  791     }
  792     return atausb_bbb_start(request);
  793 }
  794 
  795 int
  796 ata_usbchannel_end_transaction(struct ata_request *request)
  797 {
  798     if (atausbdebug > 1)
  799         device_printf(request->dev, "end_transaction %s\n",
  800                       ata_cmd2str(request));
  801     
  802     /* XXX SOS convert the request from the format used, only BBB for now*/
  803 
  804     /* ATA/ATAPI IDENTIFY needs special treatment */
  805     if ((request->flags & ATA_R_ATAPI) &&
  806         (request->u.atapi.ccb[0] == ATAPI_INQUIRY)) {
  807         struct ata_device *atadev = device_get_softc(request->dev);
  808         struct atapi_inquiry *inquiry = (struct atapi_inquiry *)request->data;
  809         u_int16_t *ptr;
  810 
  811         /* convert inquiry data into simple ata_param like format */
  812         atadev->param.config = ATA_PROTO_ATAPI | ATA_PROTO_ATAPI_12;
  813         atadev->param.config |= (inquiry->device_type & 0x1f) << 8;
  814         bzero(atadev->param.model, sizeof(atadev->param.model));
  815         strncpy(atadev->param.model, inquiry->vendor, 8);
  816         strcpy(atadev->param.model, "  ");
  817         strncpy(atadev->param.model, inquiry->product, 16);
  818         ptr = (u_int16_t*)(atadev->param.model + sizeof(atadev->param.model));
  819         while (--ptr >= (u_int16_t*)atadev->param.model)
  820             *ptr = ntohs(*ptr);
  821         strncpy(atadev->param.revision, inquiry->revision, 4);
  822         ptr=(u_int16_t*)(atadev->param.revision+sizeof(atadev->param.revision));
  823         while (--ptr >= (u_int16_t*)atadev->param.revision)
  824             *ptr = ntohs(*ptr);
  825         request->result = 0;
  826     }
  827     return ATA_OP_FINISHED;
  828 }
  829 
  830 static int
  831 ata_usbchannel_probe(device_t dev)
  832 {
  833     struct ata_channel *ch = device_get_softc(dev);
  834     device_t *children;
  835     int count, i;
  836     char buffer[32];
  837 
  838     /* take care of green memory */
  839     bzero(ch, sizeof(struct ata_channel));
  840 
  841     /* find channel number on this controller */
  842     device_get_children(device_get_parent(dev), &children, &count);
  843     for (i = 0; i < count; i++) {
  844         if (children[i] == dev)
  845             ch->unit = i;
  846     }
  847     free(children, M_TEMP);
  848 
  849     sprintf(buffer, "USB lun %d", ch->unit);
  850     device_set_desc_copy(dev, buffer);
  851 
  852     return 0;
  853 }
  854 
  855 static int
  856 ata_usbchannel_attach(device_t dev)
  857 {
  858     struct ata_channel *ch = device_get_softc(dev);
  859 
  860     /* initialize the softc basics */
  861     ch->dev = dev;
  862     ch->state = ATA_IDLE;
  863     ch->hw.begin_transaction = ata_usbchannel_begin_transaction;
  864     ch->hw.end_transaction = ata_usbchannel_end_transaction;
  865     ch->hw.status = NULL;
  866     ch->hw.command = NULL;
  867     bzero(&ch->state_mtx, sizeof(struct mtx));
  868     mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
  869     bzero(&ch->queue_mtx, sizeof(struct mtx));
  870     mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
  871     TAILQ_INIT(&ch->ata_queue);
  872 
  873     /* XXX SOS reset the controller HW, the channel and device(s) */
  874     //ATA_RESET(dev);
  875 
  876     /* probe and attach device on this channel */
  877     ch->devices = ATA_ATAPI_MASTER;
  878     if (!ata_delayed_attach)
  879         ata_identify(dev);
  880     return 0;
  881 }
  882 
  883 static int
  884 ata_usbchannel_detach(device_t dev)
  885 {
  886     struct ata_channel *ch = device_get_softc(dev);
  887     device_t *children;
  888     int nchildren, i;
  889 
  890     /* detach & delete all children */
  891     if (!device_get_children(dev, &children, &nchildren)) {
  892         for (i = 0; i < nchildren; i++)
  893             if (children[i])
  894                 device_delete_child(dev, children[i]);
  895         free(children, M_TEMP);
  896     }
  897     mtx_destroy(&ch->state_mtx);
  898     mtx_destroy(&ch->queue_mtx);
  899     return 0;
  900 }
  901 
  902 static void
  903 ata_usbchannel_setmode(device_t parent, device_t dev)
  904 {
  905     struct atausb_softc *sc = device_get_softc(GRANDPARENT(dev));
  906     struct ata_device *atadev = device_get_softc(dev);
  907     usbd_device_handle udev;
  908 
  909     usbd_interface2device_handle(sc->iface, &udev);
  910     if (usbd_get_speed(udev) == USB_SPEED_HIGH)
  911         atadev->mode = ATA_USB2;
  912     else
  913         atadev->mode = ATA_USB1;
  914 }
  915 
  916 static int
  917 ata_usbchannel_locking(device_t dev, int flags)
  918 {
  919     struct atausb_softc *sc = device_get_softc(device_get_parent(dev));
  920     struct ata_channel *ch = device_get_softc(dev);
  921     int res = -1;
  922 
  923 
  924     mtx_lock(&sc->locked_mtx);
  925     switch (flags) {
  926     case ATA_LF_LOCK:
  927         if (sc->locked_ch == NULL)
  928             sc->locked_ch = ch;
  929         if (sc->locked_ch != ch)
  930             sc->restart_ch = ch;
  931         break;
  932 
  933     case ATA_LF_UNLOCK:
  934         if (sc->locked_ch == ch) {
  935             sc->locked_ch = NULL;
  936             if (sc->restart_ch) {
  937                 ch = sc->restart_ch;
  938                 sc->restart_ch = NULL;
  939                 mtx_unlock(&sc->locked_mtx);
  940                 ata_start(ch->dev);
  941                 return res;
  942             }
  943         }
  944         break;
  945 
  946     case ATA_LF_WHICH:
  947         break;
  948     }
  949     if (sc->locked_ch)
  950         res = sc->locked_ch->unit;
  951     mtx_unlock(&sc->locked_mtx);
  952     return res;
  953 }
  954 
  955 static device_method_t ata_usbchannel_methods[] = {
  956     /* device interface */
  957     DEVMETHOD(device_probe,         ata_usbchannel_probe),
  958     DEVMETHOD(device_attach,        ata_usbchannel_attach),
  959     DEVMETHOD(device_detach,        ata_usbchannel_detach),
  960 
  961     /* ATA methods */
  962     DEVMETHOD(ata_setmode,        ata_usbchannel_setmode),
  963     DEVMETHOD(ata_locking,        ata_usbchannel_locking),
  964     //DEVMETHOD(ata_reset,        ata_usbchannel_reset),
  965 
  966     { 0, 0 }
  967 };
  968 
  969 static driver_t ata_usbchannel_driver = {
  970     "ata",
  971     ata_usbchannel_methods,
  972     sizeof(struct ata_channel),
  973 };
  974 
  975 DRIVER_MODULE(ata, atausb, ata_usbchannel_driver, ata_devclass, 0, 0);
  976 MODULE_DEPEND(atausb, ata, 1, 1, 1);

Cache object: a053bd678348e3c3b908635ceda38dd5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.