2 * Copyright (c) 2006 - 2007 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/mutex.h>
38 #include <sys/endian.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
43 #include <sys/taskqueue.h>
45 #include <machine/resource.h>
46 #include <machine/bus.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>
56 /* Command Block Wrapper */
58 u_int8_t signature[4];
59 #define CBWSIGNATURE 0x43425355
62 u_int8_t transfer_length[4];
64 #define CBWFLAGS_OUT 0x00
65 #define CBWFLAGS_IN 0x80
69 #define CBWCDBLENGTH 16
71 u_int8_t cdb[CBWCDBLENGTH];
74 /* Command Status Wrapper */
76 u_int8_t signature[4];
77 #define CSWSIGNATURE 0x53425355
82 #define CSWSTATUS_GOOD 0x0
83 #define CSWSTATUS_FAILED 0x1
84 #define CSWSTATUS_PHASE 0x2
87 /* USB-ATA 'controller' 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 */
95 usbd_pipe_handle bulkin_pipe; /* pipe handle's */
96 usbd_pipe_handle bulkout_pipe;
97 usbd_pipe_handle bulkirq_pipe;
100 struct ata_request *ata_request;
101 usb_device_request_t usb_request;
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];
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
131 struct mtx locked_mtx;
132 struct ata_channel *locked_ch;
133 struct ata_channel *restart_ch;
136 static int atausbdebug = 0;
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);
152 USB_DECLARE_DRIVER(atausb);
153 DRIVER_MODULE(atausb, uhub, atausb_driver, atausb_devclass, 0, 0);
154 MODULE_VERSION(atausb, 1);
157 atausb_match(device_t dev)
159 struct usb_attach_arg *uaa = device_get_ivars(dev);
160 usb_interface_descriptor_t *id;
162 if (uaa->iface == NULL)
165 id = usbd_get_interface_descriptor(uaa->iface);
166 if (!id || id->bInterfaceClass != UICLASS_MASS)
169 switch (id->bInterfaceSubClass) {
170 case UISUBCLASS_QIC157:
172 case UISUBCLASS_SCSI:
173 case UISUBCLASS_SFF8020I:
174 case UISUBCLASS_SFF8070I:
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;
183 return UMATCH_IFACECLASS_IFACESUBCLASS;
187 return UMATCH_IFACECLASS;
192 atausb_attach(device_t dev)
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;
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;
213 sc->locked_ch = NULL;
214 sc->restart_ch = NULL;
215 mtx_init(&sc->locked_mtx, "ATAUSB lock", NULL, MTX_DEF);
217 id = usbd_get_interface_descriptor(sc->iface);
218 switch (id->bInterfaceProtocol) {
219 case UIPROTO_MASS_BBB:
220 case UIPROTO_MASS_BBB_OLD:
223 case UIPROTO_MASS_CBI:
226 case UIPROTO_MASS_CBI_I:
227 proto = "CBI with CCI";
232 switch (id->bInterfaceSubClass) {
236 case UISUBCLASS_QIC157:
237 case UISUBCLASS_SFF8020I:
238 case UISUBCLASS_SFF8070I:
241 case UISUBCLASS_SCSI:
248 subclass = "Unknown";
250 device_printf(dev, "using %s over %s\n", subclass, proto);
251 if (strcmp(proto, "Bulk-Only") ||
252 (strcmp(subclass, "ATAPI") && strcmp(subclass, "SCSI")))
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");
260 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
261 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
262 sc->bulkin = ed->bEndpointAddress;
264 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
265 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
266 sc->bulkout = ed->bEndpointAddress;
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;
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);
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);
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);
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",
305 sc->state = ATAUSB_S_ATTACH;
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");
317 /* driver is ready to process requests here */
318 sc->state = ATAUSB_S_IDLE;
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:
330 device_printf(sc->dev, "maxlun=%d\n", maxlun);
335 device_printf(sc->dev, "get maxlun not supported %s\n",
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");
348 bus_generic_attach(sc->dev);
353 atausb_detach(device_t dev)
355 struct atausb_softc *sc = device_get_softc(dev);
356 usbd_device_handle udev;
360 /* signal that device is going away */
361 sc->state = ATAUSB_S_DETACH;
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);
369 usbd_abort_pipe(sc->bulkin_pipe);
370 if (sc->bulkirq_pipe)
371 usbd_abort_pipe(sc->bulkirq_pipe);
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);
380 /* free the transfers */
381 for (i = 0; i < ATAUSB_T_MAX; i++)
383 usbd_free_xfer(sc->transfer[i]);
385 /* remove all the pipes */
386 if (sc->bulkout_pipe)
387 usbd_close_pipe(sc->bulkout_pipe);
389 usbd_close_pipe(sc->bulkin_pipe);
390 if (sc->bulkirq_pipe)
391 usbd_close_pipe(sc->bulkirq_pipe);
393 mtx_destroy(&sc->locked_mtx);
399 * Generic USB transfer routines
402 atausb_start(struct atausb_softc *sc, usbd_pipe_handle pipe,
403 void *buffer, int buflen, int flags, usbd_xfer_handle xfer)
407 if (sc->state == ATAUSB_S_DETACH)
408 return USBD_NOT_STARTED;
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)) {
415 device_printf(sc->dev, "failed to setup transfer, %s\n",
419 return USBD_NORMAL_COMPLETION;
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)
429 if (sc->state == ATAUSB_S_DETACH)
430 return USBD_NOT_STARTED;
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)) {
437 device_printf(sc->dev, "failed to setup ctl transfer, %s\n",
441 return USBD_NORMAL_COMPLETION;
445 atausb_clear_stall(struct atausb_softc *sc, u_int8_t endpt,
446 usbd_pipe_handle pipe, int state, usbd_xfer_handle xfer)
448 usbd_device_handle udev;
451 device_printf(sc->dev, "clear endpoint 0x%02x stall\n", endpt);
452 usbd_interface2device_handle(sc->iface, &udev);
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);
465 * Bulk-Only transport part
468 atausb_bbb_reset(struct atausb_softc *sc)
470 usbd_device_handle udev;
473 device_printf(sc->dev, "Bulk Reset\n");
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]);
487 atausb_bbb_start(struct ata_request *request)
489 struct atausb_softc *sc =
490 device_get_softc(device_get_parent(request->parent));
491 struct ata_channel *ch = device_get_softc(request->parent);
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;
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;
507 device_printf(request->dev, "cannot setup USB transfer\n");
508 atausb_bbb_reset(sc);
509 return ATA_OP_FINISHED;
511 return ATA_OP_CONTINUES;
515 atausb_bbb_finish(usbd_xfer_handle xfer, usbd_private_handle priv,
518 struct atausb_softc *sc = (struct atausb_softc *)priv;
519 struct ata_request *request = sc->ata_request;
520 usbd_xfer_handle next_xfer;
522 //device_printf(sc->dev, "BBB state %d: %s\n", sc->state, usbd_errstr(err));
524 if (sc->state == ATAUSB_S_DETACH) {
525 device_printf(sc->dev, "WARNING - device has been removed\n");
530 case ATAUSB_S_BBB_COMMAND: /* command transport phase */
533 device_printf(sc->dev, "failed to send CBW\n");
534 request->result = EIO;
535 atausb_bbb_reset(sc);
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,
545 sc->transfer[ATAUSB_T_BBB_DATA])) {
546 request->result = EIO;
547 atausb_bbb_reset(sc);
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);
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);
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),
577 sc->transfer[ATAUSB_T_BBB_DCLEAR]);
580 request->result = EIO;
581 atausb_bbb_reset(sc);
588 case ATAUSB_S_BBB_DCLEAR: /* stall clear after data phase */
589 case ATAUSB_S_BBB_SCLEAR: /* stall clear after status phase */
592 device_printf(sc->dev, "bulk%s stall clear failed %s\n",
593 (request->flags & ATA_R_READ ? "in" : "out"),
595 request->result = EIO;
596 atausb_bbb_reset(sc);
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];
608 /* second attempt of fetching status */
609 sc->state = ATAUSB_S_BBB_STATUS2;
610 next_xfer = sc->transfer[ATAUSB_T_BBB_CSW2];
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);
619 case ATAUSB_S_BBB_STATUS1: /* status transfer first attempt */
620 case ATAUSB_S_BBB_STATUS2: /* status transfer second attempt */
623 device_printf(sc->dev, "cannot get CSW, %s%s\n",
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,
629 sc->transfer[ATAUSB_T_BBB_SCLEAR]);
632 request->result = EIO;
633 atausb_bbb_reset(sc);
638 int residue = UGETDW(sc->csw.residue);
641 (request->bytecount - request->donecount))
642 residue = request->bytecount - request->donecount;
644 /* check CSW and handle eventual error */
645 if (UGETDW(sc->csw.signature) != CSWSIGNATURE) {
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);
653 else if (UGETDW(sc->csw.tag) != UGETDW(sc->cbw.tag)) {
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);
661 else if (sc->csw.status > CSWSTATUS_PHASE) {
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);
669 else if (sc->csw.status == CSWSTATUS_PHASE) {
671 device_printf(sc->dev, "phase error residue = %d\n", residue);
672 request->result = EIO;
673 atausb_bbb_reset(sc);
676 else if (request->donecount > request->bytecount) {
678 device_printf(sc->dev, "buffer overrun %d > %d",
679 request->donecount, request->bytecount);
680 request->result = EIO;
681 atausb_bbb_reset(sc);
684 else if (sc->csw.status == CSWSTATUS_FAILED) {
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));
693 sc->state = ATAUSB_S_IDLE;
694 ata_interrupt(device_get_softc(request->parent));
699 case ATAUSB_S_BBB_RESET1:
702 device_printf(sc->dev,
703 "BBB reset failure: %s\n", usbd_errstr(err));
704 atausb_clear_stall(sc, sc->bulkin, sc->bulkin_pipe,
706 sc->transfer[ATAUSB_T_BBB_RESET2]);
709 case ATAUSB_S_BBB_RESET2:
712 device_printf(sc->dev, "BBB bulkin clear stall failure: %s\n",
714 atausb_clear_stall(sc, sc->bulkout, sc->bulkout_pipe,
716 sc->transfer[ATAUSB_T_BBB_RESET3]);
719 case ATAUSB_S_BBB_RESET3:
722 device_printf(sc->dev, "BBB bulk-out clear stall failure: %s\n",
724 sc->state = ATAUSB_S_IDLE;
727 request->result = ENXIO;
729 request->result = EIO;
730 ata_interrupt(device_get_softc(request->parent));
736 device_printf(sc->dev, "unknown state %d", sc->state);
744 struct atapi_inquiry {
745 u_int8_t device_type;
746 u_int8_t device_modifier;
748 u_int8_t response_format;
750 u_int8_t reserved[2];
753 u_int8_t product[16];
754 u_int8_t revision[4];
759 ata_usbchannel_begin_transaction(struct ata_request *request)
761 struct atausb_softc *sc =
762 device_get_softc(device_get_parent(request->parent));
765 device_printf(request->dev, "begin_transaction %s\n",
766 ata_cmd2str(request));
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;
775 /* XXX SOS convert the request into the format used, only BBB for now*/
776 sc->ata_request = request;
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;
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);
792 return atausb_bbb_start(request);
796 ata_usbchannel_end_transaction(struct ata_request *request)
799 device_printf(request->dev, "end_transaction %s\n",
800 ata_cmd2str(request));
802 /* XXX SOS convert the request from the format used, only BBB for now*/
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;
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)
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)
827 return ATA_OP_FINISHED;
831 ata_usbchannel_probe(device_t dev)
833 struct ata_channel *ch = device_get_softc(dev);
838 /* take care of green memory */
839 bzero(ch, sizeof(struct ata_channel));
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)
847 free(children, M_TEMP);
849 sprintf(buffer, "USB lun %d", ch->unit);
850 device_set_desc_copy(dev, buffer);
856 ata_usbchannel_attach(device_t dev)
858 struct ata_channel *ch = device_get_softc(dev);
860 /* initialize the softc basics */
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);
873 /* XXX SOS reset the controller HW, the channel and device(s) */
876 /* probe and attach device on this channel */
877 ch->devices = ATA_ATAPI_MASTER;
878 if (!ata_delayed_attach)
884 ata_usbchannel_detach(device_t dev)
886 struct ata_channel *ch = device_get_softc(dev);
890 /* detach & delete all children */
891 if (!device_get_children(dev, &children, &nchildren)) {
892 for (i = 0; i < nchildren; i++)
894 device_delete_child(dev, children[i]);
895 free(children, M_TEMP);
897 mtx_destroy(&ch->state_mtx);
898 mtx_destroy(&ch->queue_mtx);
903 ata_usbchannel_setmode(device_t parent, device_t dev)
905 struct atausb_softc *sc = device_get_softc(GRANDPARENT(dev));
906 struct ata_device *atadev = device_get_softc(dev);
907 usbd_device_handle udev;
909 usbd_interface2device_handle(sc->iface, &udev);
910 if (usbd_get_speed(udev) == USB_SPEED_HIGH)
911 atadev->mode = ATA_USB2;
913 atadev->mode = ATA_USB1;
917 ata_usbchannel_locking(device_t dev, int flags)
919 struct atausb_softc *sc = device_get_softc(device_get_parent(dev));
920 struct ata_channel *ch = device_get_softc(dev);
924 mtx_lock(&sc->locked_mtx);
927 if (sc->locked_ch == NULL)
929 if (sc->locked_ch != ch)
934 if (sc->locked_ch == ch) {
935 sc->locked_ch = NULL;
936 if (sc->restart_ch) {
938 sc->restart_ch = NULL;
939 mtx_unlock(&sc->locked_mtx);
950 res = sc->locked_ch->unit;
951 mtx_unlock(&sc->locked_mtx);
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),
962 DEVMETHOD(ata_setmode, ata_usbchannel_setmode),
963 DEVMETHOD(ata_locking, ata_usbchannel_locking),
964 //DEVMETHOD(ata_reset, ata_usbchannel_reset),
969 static driver_t ata_usbchannel_driver = {
971 ata_usbchannel_methods,
972 sizeof(struct ata_channel),
975 DRIVER_MODULE(ata, atausb, ata_usbchannel_driver, ata_devclass, 0, 0);
976 MODULE_DEPEND(atausb, ata, 1, 1, 1);