2 * Copyright (c) 2006 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/sys/dev/ata/ata-usb.c,v 1.4 2006/03/31 08:09:05 sos Exp $
27 * $DragonFly: src/sys/dev/disk/nata/ata-usb.c,v 1.7 2007/07/02 23:52:04 hasso Exp $
32 #include <sys/param.h>
34 #include <sys/libkern.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/spinlock.h>
39 #include <sys/spinlock2.h>
41 #include <bus/usb/usb.h>
42 #include <bus/usb/usbdi.h>
43 #include <bus/usb/usbdi_util.h>
44 #include <bus/usb/usbdivar.h>
49 /* Command Block Wrapper */
51 u_int8_t signature[4];
52 #define CBWSIGNATURE 0x43425355
55 u_int8_t transfer_length[4];
57 #define CBWFLAGS_OUT 0x00
58 #define CBWFLAGS_IN 0x80
62 #define CBWCDBLENGTH 16
64 u_int8_t cdb[CBWCDBLENGTH];
67 /* Command Status Wrapper */
69 u_int8_t signature[4];
70 #define CSWSIGNATURE 0x53425355
75 #define CSWSTATUS_GOOD 0x0
76 #define CSWSTATUS_FAILED 0x1
77 #define CSWSTATUS_PHASE 0x2
80 /* USB-ATA 'controller' softc */
82 device_t dev; /* base device */
83 usbd_interface_handle iface; /* interface */
84 int ifaceno; /* interface number */
85 u_int8_t bulkin; /* endpoint address's */
88 usbd_pipe_handle bulkin_pipe; /* pipe handle's */
89 usbd_pipe_handle bulkout_pipe;
90 usbd_pipe_handle bulkirq_pipe;
93 struct ata_request *ata_request;
94 usb_device_request_t usb_request;
98 #define ATAUSB_T_BBB_CBW 0
99 #define ATAUSB_T_BBB_DATA 1
100 #define ATAUSB_T_BBB_DCLEAR 2
101 #define ATAUSB_T_BBB_CSW1 3
102 #define ATAUSB_T_BBB_CSW2 4
103 #define ATAUSB_T_BBB_SCLEAR 5
104 #define ATAUSB_T_BBB_RESET1 6
105 #define ATAUSB_T_BBB_RESET2 7
106 #define ATAUSB_T_BBB_RESET3 8
107 #define ATAUSB_T_MAX 9
108 usbd_xfer_handle transfer[ATAUSB_T_MAX];
111 #define ATAUSB_S_ATTACH 0
112 #define ATAUSB_S_IDLE 1
113 #define ATAUSB_S_BBB_COMMAND 2
114 #define ATAUSB_S_BBB_DATA 3
115 #define ATAUSB_S_BBB_DCLEAR 4
116 #define ATAUSB_S_BBB_STATUS1 5
117 #define ATAUSB_S_BBB_SCLEAR 6
118 #define ATAUSB_S_BBB_STATUS2 7
119 #define ATAUSB_S_BBB_RESET1 8
120 #define ATAUSB_S_BBB_RESET2 9
121 #define ATAUSB_S_BBB_RESET3 10
122 #define ATAUSB_S_DETACH 11
124 struct spinlock locked_mtx;
125 struct ata_channel *locked_ch;
126 struct ata_channel *restart_ch;
129 static int atausbdebug = 0;
132 static usbd_status atausb_start(struct atausb_softc *sc, usbd_pipe_handle pipe, void *buffer, int buflen, int flags, usbd_xfer_handle xfer);
133 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);
134 static void atausb_clear_stall(struct atausb_softc *sc, u_int8_t endpt, usbd_pipe_handle pipe, int state, usbd_xfer_handle xfer);
135 static void atausb_bbb_reset(struct atausb_softc *sc);
136 static int atausb_bbb_start(struct ata_request *request);
137 static void atausb_bbb_finish(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status err);
138 int ata_usbchannel_begin_transaction(struct ata_request *request);
139 int ata_usbchannel_end_transaction(struct ata_request *request);
145 static device_probe_t atausb_match;
146 static device_attach_t atausb_attach;
147 static device_detach_t atausb_detach;
149 static devclass_t atausb_devclass;
151 static kobj_method_t atausb_methods[] = {
152 DEVMETHOD(device_probe, atausb_match),
153 DEVMETHOD(device_attach, atausb_attach),
154 DEVMETHOD(device_detach, atausb_detach),
158 static driver_t atausb_driver = {
161 sizeof(struct atausb_softc)
164 MODULE_DEPEND(atausb, usb, 1, 1, 1);
165 DRIVER_MODULE(atausb, uhub, atausb_driver, atausb_devclass, 0, 0);
166 MODULE_VERSION(atausb, 1);
169 atausb_match(device_t dev)
171 struct usb_attach_arg *uaa = device_get_ivars(dev);
172 usb_interface_descriptor_t *id;
174 if (uaa->iface == NULL)
177 id = usbd_get_interface_descriptor(uaa->iface);
178 if (!id || id->bInterfaceClass != UICLASS_MASS)
181 switch (id->bInterfaceSubClass) {
182 case UISUBCLASS_QIC157:
184 case UISUBCLASS_SCSI:
185 case UISUBCLASS_SFF8020I:
186 case UISUBCLASS_SFF8070I:
188 switch (id->bInterfaceProtocol) {
189 case UIPROTO_MASS_CBI:
190 case UIPROTO_MASS_CBI_I:
191 case UIPROTO_MASS_BBB:
192 case UIPROTO_MASS_BBB_OLD:
193 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
195 return UMATCH_IFACECLASS_IFACESUBCLASS;
199 return UMATCH_IFACECLASS;
204 atausb_attach(device_t dev)
206 struct atausb_softc *sc = device_get_softc(dev);
207 struct usb_attach_arg *uaa = device_get_ivars(dev);
208 usb_interface_descriptor_t *id;
209 usb_endpoint_descriptor_t *ed;
210 usbd_device_handle udev;
211 usb_device_request_t request;
212 char devinfo[1024], *proto, *subclass;
217 usbd_devinfo(uaa->device, 0, devinfo);
218 device_set_desc_copy(dev, devinfo);
219 sc->bulkin = sc->bulkout = sc->bulkirq = -1;
220 sc->bulkin_pipe = sc->bulkout_pipe= sc->bulkirq_pipe = NULL;
221 sc->iface = uaa->iface;
222 sc->ifaceno = uaa->ifaceno;
225 sc->locked_ch = NULL;
226 sc->restart_ch = NULL;
227 spin_init(&sc->locked_mtx);
229 id = usbd_get_interface_descriptor(sc->iface);
230 switch (id->bInterfaceProtocol) {
231 case UIPROTO_MASS_BBB:
232 case UIPROTO_MASS_BBB_OLD:
235 case UIPROTO_MASS_CBI:
238 case UIPROTO_MASS_CBI_I:
239 proto = "CBI with CCI";
244 switch (id->bInterfaceSubClass) {
248 case UISUBCLASS_QIC157:
249 case UISUBCLASS_SFF8020I:
250 case UISUBCLASS_SFF8070I:
253 case UISUBCLASS_SCSI:
260 subclass = "Unknown";
262 device_printf(dev, "using %s over %s\n", subclass, proto);
263 if (strcmp(proto, "Bulk-Only") ||
264 (strcmp(subclass, "ATAPI") && strcmp(subclass, "SCSI")))
267 for (i = 0 ; i < id->bNumEndpoints ; i++) {
268 if (!(ed = usbd_interface2endpoint_descriptor(sc->iface, i))) {
269 device_printf(sc->dev, "could not read endpoint descriptor\n");
272 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
273 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
274 sc->bulkin = ed->bEndpointAddress;
276 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
277 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
278 sc->bulkout = ed->bEndpointAddress;
280 if (id->bInterfaceProtocol == UIPROTO_MASS_CBI_I &&
281 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
282 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
283 sc->bulkirq = ed->bEndpointAddress;
287 /* check whether we found at least the endpoints we need */
288 if (!sc->bulkin || !sc->bulkout) {
289 device_printf(sc->dev, "needed endpoints not found (%d,%d)\n",
290 sc->bulkin, sc->bulkout);
296 if (usbd_open_pipe(sc->iface, sc->bulkout,
297 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe)) {
298 device_printf(sc->dev, "cannot open bulkout pipe (%d)\n", sc->bulkout);
302 if (usbd_open_pipe(sc->iface, sc->bulkin,
303 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe)) {
304 device_printf(sc->dev, "cannot open bulkin pipe (%d)\n", sc->bulkin);
308 if (id->bInterfaceProtocol == UIPROTO_MASS_CBI_I) {
309 if (usbd_open_pipe(sc->iface, sc->bulkirq,
310 USBD_EXCLUSIVE_USE, &sc->bulkirq_pipe)) {
311 device_printf(sc->dev, "cannot open bulkirq pipe (%d)\n",
317 sc->state = ATAUSB_S_ATTACH;
319 /* alloc needed number of transfer handles */
320 for (i = 0; i < ATAUSB_T_MAX; i++) {
321 sc->transfer[i] = usbd_alloc_xfer(uaa->device);
322 if (!sc->transfer[i]) {
323 device_printf(sc->dev, "out of memory\n");
329 /* driver is ready to process requests here */
330 sc->state = ATAUSB_S_IDLE;
332 /* get number of devices so we can add matching channels */
333 usbd_interface2device_handle(sc->iface, &udev);
334 request.bmRequestType = UT_READ_CLASS_INTERFACE;
335 request.bRequest = 0xfe; /* GET_MAX_LUN; */
336 USETW(request.wValue, 0);
337 USETW(request.wIndex, sc->ifaceno);
338 USETW(request.wLength, sizeof(maxlun));
339 switch ((err = usbd_do_request(udev, &request, &maxlun))) {
340 case USBD_NORMAL_COMPLETION:
342 device_printf(sc->dev, "maxlun=%d\n", maxlun);
347 device_printf(sc->dev, "get maxlun not supported %s\n",
351 /* ata channels are children to this USB control device */
352 for (i = 0; i <= sc->maxlun; i++) {
353 /* XXX TGEN devclass_find_free_unit() implementation */
355 while (freeunit < devclass_get_maxunit(ata_devclass) &&
356 devclass_get_device(ata_devclass, freeunit) != NULL)
358 if (!device_add_child(sc->dev, "ata", freeunit)) {
359 device_printf(sc->dev, "failed to attach ata child device\n");
364 bus_generic_attach(sc->dev);
369 atausb_detach(device_t dev)
371 struct atausb_softc *sc = device_get_softc(dev);
372 usbd_device_handle udev;
376 /* signal that device is going away */
377 sc->state = ATAUSB_S_DETACH;
379 /* abort all the pipes in case there are active transfers */
380 usbd_interface2device_handle(sc->iface, &udev);
381 usbd_abort_pipe(udev->default_pipe);
382 if (sc->bulkout_pipe)
383 usbd_abort_pipe(sc->bulkout_pipe);
385 usbd_abort_pipe(sc->bulkin_pipe);
386 if (sc->bulkirq_pipe)
387 usbd_abort_pipe(sc->bulkirq_pipe);
389 /* detach & delete all children */
390 if (!device_get_children(dev, &children, &nchildren)) {
391 for (i = 0; i < nchildren; i++)
392 device_delete_child(dev, children[i]);
393 kfree(children, M_TEMP);
396 /* free the transfers */
397 for (i = 0; i < ATAUSB_T_MAX; i++)
399 usbd_free_xfer(sc->transfer[i]);
401 /* remove all the pipes */
402 if (sc->bulkout_pipe)
403 usbd_close_pipe(sc->bulkout_pipe);
405 usbd_close_pipe(sc->bulkin_pipe);
406 if (sc->bulkirq_pipe)
407 usbd_close_pipe(sc->bulkirq_pipe);
409 spin_uninit(&sc->locked_mtx);
415 * Generic USB transfer routines
418 atausb_start(struct atausb_softc *sc, usbd_pipe_handle pipe,
419 void *buffer, int buflen, int flags, usbd_xfer_handle xfer)
423 if (sc->state == ATAUSB_S_DETACH)
424 return USBD_NOT_STARTED;
426 usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen, flags,
427 sc->timeout, atausb_bbb_finish);
428 err = usbd_transfer(xfer);
429 if (err && (err != USBD_IN_PROGRESS)) {
431 device_printf(sc->dev, "failed to setup transfer, %s\n",
435 return USBD_NORMAL_COMPLETION;
439 atausb_ctl_start(struct atausb_softc *sc, usbd_device_handle udev,
440 usb_device_request_t *req, void *buffer, int buflen, int flags,
441 usbd_xfer_handle xfer)
445 if (sc->state == ATAUSB_S_DETACH)
446 return USBD_NOT_STARTED;
448 usbd_setup_default_xfer(xfer, udev, (void *)sc, sc->timeout, req,
449 buffer, buflen, flags, atausb_bbb_finish);
450 err = usbd_transfer(xfer);
451 if (err && (err != USBD_IN_PROGRESS)) {
453 device_printf(sc->dev, "failed to setup ctl transfer, %s\n",
457 return USBD_NORMAL_COMPLETION;
461 atausb_clear_stall(struct atausb_softc *sc, u_int8_t endpt,
462 usbd_pipe_handle pipe, int state, usbd_xfer_handle xfer)
464 usbd_device_handle udev;
467 device_printf(sc->dev, "clear endpoint 0x%02x stall\n", endpt);
468 usbd_interface2device_handle(sc->iface, &udev);
470 usbd_clear_endpoint_toggle(pipe);
471 sc->usb_request.bmRequestType = UT_WRITE_ENDPOINT;
472 sc->usb_request.bRequest = UR_CLEAR_FEATURE;
473 USETW(sc->usb_request.wValue, UF_ENDPOINT_HALT);
474 USETW(sc->usb_request.wIndex, endpt);
475 USETW(sc->usb_request.wLength, 0);
476 atausb_ctl_start(sc, udev, &sc->usb_request, NULL, 0, 0, xfer);
481 * Bulk-Only transport part
484 atausb_bbb_reset(struct atausb_softc *sc)
486 usbd_device_handle udev;
489 device_printf(sc->dev, "Bulk Reset\n");
491 sc->state = ATAUSB_S_BBB_RESET1;
492 usbd_interface2device_handle(sc->iface, &udev);
493 sc->usb_request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
494 sc->usb_request.bRequest = 0xff; /* bulk-only reset */
495 USETW(sc->usb_request.wValue, 0);
496 USETW(sc->usb_request.wIndex, sc->ifaceno);
497 USETW(sc->usb_request.wLength, 0);
498 atausb_ctl_start(sc, udev, &sc->usb_request, NULL,
499 0, 0, sc->transfer[ATAUSB_T_BBB_RESET1]);
503 atausb_bbb_start(struct ata_request *request)
505 struct atausb_softc *sc =
506 device_get_softc(device_get_parent(request->parent));
507 struct ata_channel *ch = device_get_softc(request->parent);
509 sc->timeout = (request->timeout * 1000) + 5000;
510 USETDW(sc->cbw.signature, CBWSIGNATURE);
511 USETDW(sc->cbw.tag, UGETDW(sc->cbw.tag) + 1);
512 USETDW(sc->cbw.transfer_length, request->bytecount);
513 sc->cbw.flags = (request->flags & ATA_R_READ) ? CBWFLAGS_IN : CBWFLAGS_OUT;
514 sc->cbw.lun = ch->unit;
516 bzero(sc->cbw.cdb, 16);
517 bcopy(request->u.atapi.ccb, sc->cbw.cdb, 12); /* XXX SOS */
518 sc->state = ATAUSB_S_BBB_COMMAND;
519 if (atausb_start(sc, sc->bulkout_pipe, &sc->cbw, sizeof(struct bbb_cbw),
520 0, sc->transfer[ATAUSB_T_BBB_CBW])) {
521 request->result = EIO;
523 device_printf(request->dev, "cannot setup USB transfer\n");
524 atausb_bbb_reset(sc);
525 return ATA_OP_FINISHED;
527 return ATA_OP_CONTINUES;
531 atausb_bbb_finish(usbd_xfer_handle xfer, usbd_private_handle priv,
534 struct atausb_softc *sc = (struct atausb_softc *)priv;
535 struct ata_request *request = sc->ata_request;
536 usbd_xfer_handle next_xfer;
538 /* device_printf(sc->dev, "BBB state %d: %s\n", sc->state,
539 usbd_errstr(err)); */
541 if (sc->state == ATAUSB_S_DETACH) {
542 device_printf(sc->dev, "WARNING - device has been removed\n");
547 case ATAUSB_S_BBB_COMMAND: /* command transport phase */
550 device_printf(sc->dev, "failed to send CBW\n");
551 request->result = EIO;
552 atausb_bbb_reset(sc);
556 /* next is data transport phase, setup transfer */
557 sc->state = ATAUSB_S_BBB_DATA;
558 if (request->flags & ATA_R_READ) {
559 if (atausb_start(sc, sc->bulkin_pipe,
560 request->data, request->bytecount,
562 sc->transfer[ATAUSB_T_BBB_DATA])) {
563 request->result = EIO;
564 atausb_bbb_reset(sc);
568 if (request->flags & ATA_R_WRITE) {
569 if (atausb_start(sc, sc->bulkout_pipe,
570 request->data, request->bytecount,
571 0, sc->transfer[ATAUSB_T_BBB_DATA])) {
572 request->result = EIO;
573 atausb_bbb_reset(sc);
579 case ATAUSB_S_BBB_DATA: /* data transport phase */
580 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
581 usbd_get_xfer_status(xfer, NULL, NULL, &request->donecount, NULL);
584 device_printf(sc->dev, "data %s count %d failed: %s\n",
585 (request->flags & ATA_R_READ?"read":"write"),
586 request->bytecount, usbd_errstr(err));
587 if (err == USBD_STALLED) {
588 atausb_clear_stall(sc,
589 (request->flags & ATA_R_READ ?
590 sc->bulkin : sc->bulkout),
591 (request->flags & ATA_R_READ ?
592 sc->bulkin_pipe : sc->bulkout_pipe),
594 sc->transfer[ATAUSB_T_BBB_DCLEAR]);
597 request->result = EIO;
598 atausb_bbb_reset(sc);
605 case ATAUSB_S_BBB_DCLEAR: /* stall clear after data phase */
606 case ATAUSB_S_BBB_SCLEAR: /* stall clear after status phase */
609 device_printf(sc->dev, "bulk%s stall clear failed %s\n",
610 (request->flags & ATA_R_READ ? "in" : "out"),
612 request->result = EIO;
613 atausb_bbb_reset(sc);
617 if (sc->state == ATAUSB_S_BBB_COMMAND ||
618 sc->state == ATAUSB_S_BBB_DATA ||
619 sc->state == ATAUSB_S_BBB_DCLEAR) {
620 /* first attempt on status transport phase setup transfer */
621 sc->state = ATAUSB_S_BBB_STATUS1;
622 next_xfer = sc->transfer[ATAUSB_T_BBB_CSW1];
625 /* second attempt of fetching status */
626 sc->state = ATAUSB_S_BBB_STATUS2;
627 next_xfer = sc->transfer[ATAUSB_T_BBB_CSW2];
629 if (atausb_start(sc, sc->bulkin_pipe, &sc->csw, sizeof(struct bbb_csw),
630 USBD_SHORT_XFER_OK, next_xfer)) {
631 request->result = EIO;
632 atausb_bbb_reset(sc);
636 case ATAUSB_S_BBB_STATUS1: /* status transfer first attempt */
637 case ATAUSB_S_BBB_STATUS2: /* status transfer second attempt */
640 device_printf(sc->dev, "cannot get CSW, %s%s\n",
642 sc->state == ATAUSB_S_BBB_STATUS1 ? ", retry":"");
643 if (sc->state == ATAUSB_S_BBB_STATUS1) {
644 atausb_clear_stall(sc, sc->bulkin, sc->bulkin_pipe,
646 sc->transfer[ATAUSB_T_BBB_SCLEAR]);
649 request->result = EIO;
650 atausb_bbb_reset(sc);
655 int residue = UGETDW(sc->csw.residue);
658 (request->bytecount - request->donecount))
659 residue = request->bytecount - request->donecount;
661 /* check CSW and handle eventual error */
662 if (UGETDW(sc->csw.signature) != CSWSIGNATURE) {
664 device_printf(sc->dev, "bad CSW signature 0x%08x != 0x%08x\n",
665 UGETDW(sc->csw.signature), CSWSIGNATURE);
666 request->result = EIO;
667 atausb_bbb_reset(sc);
670 else if (UGETDW(sc->csw.tag) != UGETDW(sc->cbw.tag)) {
672 device_printf(sc->dev, "bad CSW tag %d != %d\n",
673 UGETDW(sc->csw.tag), UGETDW(sc->cbw.tag));
674 request->result = EIO;
675 atausb_bbb_reset(sc);
678 else if (sc->csw.status > CSWSTATUS_PHASE) {
680 device_printf(sc->dev, "bad CSW status %d > %d\n",
681 sc->csw.status, CSWSTATUS_PHASE);
682 request->result = EIO;
683 atausb_bbb_reset(sc);
686 else if (sc->csw.status == CSWSTATUS_PHASE) {
688 device_printf(sc->dev, "phase error residue = %d\n", residue);
689 request->result = EIO;
690 atausb_bbb_reset(sc);
693 else if (request->donecount > request->bytecount) {
695 device_printf(sc->dev, "buffer overrun %d > %d",
696 request->donecount, request->bytecount);
697 request->result = EIO;
698 atausb_bbb_reset(sc);
701 else if (sc->csw.status == CSWSTATUS_FAILED) {
703 device_printf(sc->dev, "CSWSTATUS_FAILED\n");
704 request->error = ATA_E_ATAPI_SENSE_MASK ;
705 sc->state = ATAUSB_S_IDLE;
706 ata_interrupt(device_get_softc(request->parent));
710 sc->state = ATAUSB_S_IDLE;
711 ata_interrupt(device_get_softc(request->parent));
716 case ATAUSB_S_BBB_RESET1:
719 device_printf(sc->dev,
720 "BBB reset failure: %s\n", usbd_errstr(err));
721 atausb_clear_stall(sc, sc->bulkin, sc->bulkin_pipe,
723 sc->transfer[ATAUSB_T_BBB_RESET2]);
726 case ATAUSB_S_BBB_RESET2:
729 device_printf(sc->dev, "BBB bulkin clear stall failure: %s\n",
731 atausb_clear_stall(sc, sc->bulkout, sc->bulkout_pipe,
733 sc->transfer[ATAUSB_T_BBB_RESET3]);
736 case ATAUSB_S_BBB_RESET3:
739 device_printf(sc->dev, "BBB bulk-out clear stall failure: %s\n",
741 sc->state = ATAUSB_S_IDLE;
744 request->result = ENXIO;
746 request->result = EIO;
747 ata_interrupt(device_get_softc(request->parent));
753 device_printf(sc->dev, "unknown state %d", sc->state);
761 struct atapi_inquiry {
762 u_int8_t device_type;
763 u_int8_t device_modifier;
765 u_int8_t response_format;
767 u_int8_t reserved[2];
770 u_int8_t product[16];
771 u_int8_t revision[4];
772 /* u_int8_t crap[60]; */
776 ata_usbchannel_begin_transaction(struct ata_request *request)
778 struct atausb_softc *sc =
779 device_get_softc(device_get_parent(request->parent));
782 device_printf(request->dev, "begin_transaction %s\n",
783 ata_cmd2str(request));
785 /* sanity just in case */
786 if (sc->state != ATAUSB_S_IDLE) {
787 kprintf("begin is busy (%d)\n", sc->state);
788 request->result = EBUSY;
789 return ATA_OP_FINISHED;
792 /* XXX SOS convert the request into the format used, only BBB for now*/
793 sc->ata_request = request;
795 /* ATA/ATAPI IDENTIFY needs special treatment */
796 if (!(request->flags & ATA_R_ATAPI)) {
797 if (request->u.ata.command != ATA_ATAPI_IDENTIFY) {
798 device_printf(request->dev,"%s unsupported\n",ata_cmd2str(request));
799 request->result = EIO;
800 return ATA_OP_FINISHED;
802 request->flags |= ATA_R_ATAPI;
803 bzero(request->u.atapi.ccb, 16);
804 request->u.atapi.ccb[0] = ATAPI_INQUIRY;
805 request->u.atapi.ccb[4] = 255; /* sizeof(struct atapi_inquiry); */
806 request->data += 256; /* arbitrary offset into ata_param */
807 request->bytecount = 255; /* sizeof(struct atapi_inquiry); */
809 return atausb_bbb_start(request);
813 ata_usbchannel_end_transaction(struct ata_request *request)
816 device_printf(request->dev, "end_transaction %s\n",
817 ata_cmd2str(request));
819 /* XXX SOS convert the request from the format used, only BBB for now*/
821 /* ATA/ATAPI IDENTIFY needs special treatment */
822 if ((request->flags & ATA_R_ATAPI) &&
823 (request->u.atapi.ccb[0] == ATAPI_INQUIRY)) {
824 struct ata_device *atadev = device_get_softc(request->dev);
825 struct atapi_inquiry *inquiry = (struct atapi_inquiry *)request->data;
828 /* convert inquiry data into simple ata_param like format */
829 atadev->param.config = ATA_PROTO_ATAPI | ATA_PROTO_ATAPI_12;
830 atadev->param.config |= (inquiry->device_type & 0x1f) << 8;
831 bzero(atadev->param.model, sizeof(atadev->param.model));
832 strncpy(atadev->param.model, inquiry->vendor, 8);
833 strcpy(atadev->param.model, " ");
834 strncpy(atadev->param.model, inquiry->product, 16);
835 ptr = (u_int16_t*)(atadev->param.model + sizeof(atadev->param.model));
836 while (--ptr >= (u_int16_t*)atadev->param.model)
838 strncpy(atadev->param.revision, inquiry->revision, 4);
839 ptr=(u_int16_t*)(atadev->param.revision+sizeof(atadev->param.revision));
840 while (--ptr >= (u_int16_t*)atadev->param.revision)
844 return ATA_OP_FINISHED;
848 ata_usbchannel_probe(device_t dev)
850 struct ata_channel *ch = device_get_softc(dev);
855 /* take care of green memory */
856 bzero(ch, sizeof(struct ata_channel));
858 /* find channel number on this controller */
859 device_get_children(device_get_parent(dev), &children, &count);
860 for (i = 0; i < count; i++) {
861 if (children[i] == dev)
864 kfree(children, M_TEMP);
866 ksprintf(buffer, "USB lun %d", ch->unit);
867 device_set_desc_copy(dev, buffer);
873 ata_usbchannel_attach(device_t dev)
875 struct ata_channel *ch = device_get_softc(dev);
877 /* initialize the softc basics */
879 ch->state = ATA_IDLE;
880 ch->hw.begin_transaction = ata_usbchannel_begin_transaction;
881 ch->hw.end_transaction = ata_usbchannel_end_transaction;
882 ch->hw.status = NULL;
883 ch->hw.command = NULL;
884 spin_init(&ch->state_mtx);
885 spin_init(&ch->queue_mtx);
886 TAILQ_INIT(&ch->ata_queue);
888 /* XXX SOS reset the controller HW, the channel and device(s) */
889 /* ATA_RESET(dev); */
891 /* probe and attach device on this channel */
892 ch->devices = ATA_ATAPI_MASTER;
898 ata_usbchannel_detach(device_t dev)
900 struct ata_channel *ch = device_get_softc(dev);
904 /* detach & delete all children */
905 if (!device_get_children(dev, &children, &nchildren)) {
906 for (i = 0; i < nchildren; i++)
908 device_delete_child(dev, children[i]);
909 kfree(children, M_TEMP);
911 spin_uninit(&ch->state_mtx);
912 spin_uninit(&ch->queue_mtx);
917 ata_usbchannel_setmode(device_t parent, device_t dev)
919 struct atausb_softc *sc = device_get_softc(GRANDPARENT(dev));
920 struct ata_device *atadev = device_get_softc(dev);
921 usbd_device_handle udev;
923 usbd_interface2device_handle(sc->iface, &udev);
924 if (usbd_get_speed(udev) == USB_SPEED_HIGH)
925 atadev->mode = ATA_USB2;
927 atadev->mode = ATA_USB1;
931 ata_usbchannel_locking(device_t dev, int flags)
933 struct atausb_softc *sc = device_get_softc(device_get_parent(dev));
934 struct ata_channel *ch = device_get_softc(dev);
938 spin_lock_wr(&sc->locked_mtx);
941 if (sc->locked_ch == NULL)
943 if (sc->locked_ch != ch)
948 if (sc->locked_ch == ch) {
949 sc->locked_ch = NULL;
950 if (sc->restart_ch) {
952 sc->restart_ch = NULL;
953 spin_unlock_wr(&sc->locked_mtx);
964 res = sc->locked_ch->unit;
965 spin_unlock_wr(&sc->locked_mtx);
969 static device_method_t ata_usbchannel_methods[] = {
970 /* device interface */
971 DEVMETHOD(device_probe, ata_usbchannel_probe),
972 DEVMETHOD(device_attach, ata_usbchannel_attach),
973 DEVMETHOD(device_detach, ata_usbchannel_detach),
976 DEVMETHOD(ata_setmode, ata_usbchannel_setmode),
977 DEVMETHOD(ata_locking, ata_usbchannel_locking),
978 /* DEVMETHOD(ata_reset, ata_usbchannel_reset), */
983 static driver_t ata_usbchannel_driver = {
985 ata_usbchannel_methods,
986 sizeof(struct ata_channel),
989 DRIVER_MODULE(ata, atausb, ata_usbchannel_driver, ata_devclass, 0, 0);
990 MODULE_DEPEND(atausb, ata, 1, 1, 1);