2 * Copyright (c) 2006 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Copyright (c) 2006 Hans Petter Selasky
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/linker_set.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
52 #include <machine/bus.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
61 #include <sys/taskqueue.h>
64 #include <dev/ata/ata-all.h>
67 #define ATAUSB_BULK_SIZE (1<<17)
69 /* Command Block Wrapper */
72 #define CBWSIGNATURE 0x43425355
75 uint8_t transfer_length[4];
77 #define CBWFLAGS_OUT 0x00
78 #define CBWFLAGS_IN 0x80
82 #define CBWCDBLENGTH 16
84 uint8_t cdb[CBWCDBLENGTH];
87 /* Command Status Wrapper */
90 #define CSWSIGNATURE 0x53425355
95 #define CSWSTATUS_GOOD 0x0
96 #define CSWSTATUS_FAILED 0x1
97 #define CSWSTATUS_PHASE 0x2
100 /* USB-ATA 'controller' softc */
101 struct atausb2_softc {
104 struct mtx locked_mtx;
106 struct ata_channel *locked_ch;
107 struct ata_channel *restart_ch;
108 struct ata_request *ata_request;
110 #define ATAUSB_T_BBB_RESET1 0
111 #define ATAUSB_T_BBB_RESET2 1
112 #define ATAUSB_T_BBB_RESET3 2
113 #define ATAUSB_T_BBB_COMMAND 3
114 #define ATAUSB_T_BBB_DATA_READ 4
115 #define ATAUSB_T_BBB_DATA_RD_CS 5
116 #define ATAUSB_T_BBB_DATA_WRITE 6
117 #define ATAUSB_T_BBB_DATA_WR_CS 7
118 #define ATAUSB_T_BBB_STATUS 8
119 #define ATAUSB_T_BBB_MAX 9
121 #define ATAUSB_T_MAX ATAUSB_T_BBB_MAX
123 struct usb_xfer *xfer[ATAUSB_T_MAX];
128 uint32_t ata_donecount;
129 uint32_t ata_bytecount;
131 uint8_t last_xfer_no;
133 uint8_t intr_stalled;
139 static const int atausbdebug = 0;
143 static device_probe_t atausb2_probe;
144 static device_attach_t atausb2_attach;
145 static device_detach_t atausb2_detach;
147 static usb_callback_t atausb2_t_bbb_reset1_callback;
148 static usb_callback_t atausb2_t_bbb_reset2_callback;
149 static usb_callback_t atausb2_t_bbb_reset3_callback;
150 static usb_callback_t atausb2_t_bbb_command_callback;
151 static usb_callback_t atausb2_t_bbb_data_read_callback;
152 static usb_callback_t atausb2_t_bbb_data_rd_cs_callback;
153 static usb_callback_t atausb2_t_bbb_data_write_callback;
154 static usb_callback_t atausb2_t_bbb_data_wr_cs_callback;
155 static usb_callback_t atausb2_t_bbb_status_callback;
156 static usb_callback_t atausb2_tr_error;
158 static void atausb2_cancel_request(struct atausb2_softc *sc);
159 static void atausb2_transfer_start(struct atausb2_softc *sc, uint8_t xfer_no);
160 static void atausb2_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
161 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error);
162 static int ata_usbchannel_begin_transaction(struct ata_request *request);
163 static int ata_usbchannel_end_transaction(struct ata_request *request);
165 static device_probe_t ata_usbchannel_probe;
166 static device_attach_t ata_usbchannel_attach;
167 static device_detach_t ata_usbchannel_detach;
169 static ata_setmode_t ata_usbchannel_setmode;
170 static ata_locking_t ata_usbchannel_locking;
176 struct usb_config atausb2_config[ATAUSB_T_BBB_MAX] = {
178 [ATAUSB_T_BBB_RESET1] = {
180 .endpoint = 0x00, /* Control pipe */
181 .direction = UE_DIR_ANY,
182 .bufsize = sizeof(struct usb_device_request),
184 .callback = &atausb2_t_bbb_reset1_callback,
185 .timeout = 5000, /* 5 seconds */
186 .interval = 500, /* 500 milliseconds */
189 [ATAUSB_T_BBB_RESET2] = {
191 .endpoint = 0x00, /* Control pipe */
192 .direction = UE_DIR_ANY,
193 .bufsize = sizeof(struct usb_device_request),
195 .callback = &atausb2_t_bbb_reset2_callback,
196 .timeout = 5000, /* 5 seconds */
197 .interval = 50, /* 50 milliseconds */
200 [ATAUSB_T_BBB_RESET3] = {
202 .endpoint = 0x00, /* Control pipe */
203 .direction = UE_DIR_ANY,
204 .bufsize = sizeof(struct usb_device_request),
206 .callback = &atausb2_t_bbb_reset3_callback,
207 .timeout = 5000, /* 5 seconds */
208 .interval = 50, /* 50 milliseconds */
211 [ATAUSB_T_BBB_COMMAND] = {
213 .endpoint = UE_ADDR_ANY,
214 .direction = UE_DIR_OUT,
215 .bufsize = sizeof(struct bbb_cbw),
217 .callback = &atausb2_t_bbb_command_callback,
218 .timeout = 5000, /* 5 seconds */
221 [ATAUSB_T_BBB_DATA_READ] = {
223 .endpoint = UE_ADDR_ANY,
224 .direction = UE_DIR_IN,
225 .bufsize = ATAUSB_BULK_SIZE,
226 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
227 .callback = &atausb2_t_bbb_data_read_callback,
228 .timeout = 0, /* overwritten later */
231 [ATAUSB_T_BBB_DATA_RD_CS] = {
233 .endpoint = 0x00, /* Control pipe */
234 .direction = UE_DIR_ANY,
235 .bufsize = sizeof(struct usb_device_request),
237 .callback = &atausb2_t_bbb_data_rd_cs_callback,
238 .timeout = 5000, /* 5 seconds */
241 [ATAUSB_T_BBB_DATA_WRITE] = {
243 .endpoint = UE_ADDR_ANY,
244 .direction = UE_DIR_OUT,
245 .bufsize = ATAUSB_BULK_SIZE,
246 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
247 .callback = &atausb2_t_bbb_data_write_callback,
248 .timeout = 0, /* overwritten later */
251 [ATAUSB_T_BBB_DATA_WR_CS] = {
253 .endpoint = 0x00, /* Control pipe */
254 .direction = UE_DIR_ANY,
255 .bufsize = sizeof(struct usb_device_request),
257 .callback = &atausb2_t_bbb_data_wr_cs_callback,
258 .timeout = 5000, /* 5 seconds */
261 [ATAUSB_T_BBB_STATUS] = {
263 .endpoint = UE_ADDR_ANY,
264 .direction = UE_DIR_IN,
265 .bufsize = sizeof(struct bbb_csw),
266 .flags = {.short_xfer_ok = 1,},
267 .callback = &atausb2_t_bbb_status_callback,
268 .timeout = 5000, /* ms */
272 static devclass_t atausb2_devclass;
274 static device_method_t atausb2_methods[] = {
275 DEVMETHOD(device_probe, atausb2_probe),
276 DEVMETHOD(device_attach, atausb2_attach),
277 DEVMETHOD(device_detach, atausb2_detach),
281 static driver_t atausb2_driver = {
283 .methods = atausb2_methods,
284 .size = sizeof(struct atausb2_softc),
287 DRIVER_MODULE(atausb, uhub, atausb2_driver, atausb2_devclass, 0, 0);
288 MODULE_DEPEND(atausb, usb, 1, 1, 1);
289 MODULE_VERSION(atausb, 1);
292 atausb2_probe(device_t dev)
294 struct usb_attach_arg *uaa = device_get_ivars(dev);
295 struct usb_interface_descriptor *id;
297 if (uaa->usb_mode != USB_MODE_HOST) {
300 if (uaa->use_generic == 0) {
301 /* give other drivers a try first */
304 id = usbd_get_interface_descriptor(uaa->iface);
305 if ((!id) || (id->bInterfaceClass != UICLASS_MASS)) {
308 switch (id->bInterfaceSubClass) {
309 case UISUBCLASS_QIC157:
311 case UISUBCLASS_SCSI:
312 case UISUBCLASS_SFF8020I:
313 case UISUBCLASS_SFF8070I:
315 switch (id->bInterfaceProtocol) {
316 case UIPROTO_MASS_CBI:
317 case UIPROTO_MASS_CBI_I:
318 case UIPROTO_MASS_BBB:
319 case UIPROTO_MASS_BBB_OLD:
331 atausb2_attach(device_t dev)
333 struct atausb2_softc *sc = device_get_softc(dev);
334 struct usb_attach_arg *uaa = device_get_ivars(dev);
335 struct usb_interface_descriptor *id;
336 const char *proto, *subclass;
337 struct usb_device_request request;
344 device_set_usb_desc(dev);
348 sc->locked_ch = NULL;
349 sc->restart_ch = NULL;
350 sc->usb2_speed = usbd_get_speed(uaa->device);
351 mtx_init(&sc->locked_mtx, "ATAUSB lock", NULL, (MTX_DEF | MTX_RECURSE));
353 id = usbd_get_interface_descriptor(uaa->iface);
354 switch (id->bInterfaceProtocol) {
355 case UIPROTO_MASS_BBB:
356 case UIPROTO_MASS_BBB_OLD:
359 case UIPROTO_MASS_CBI:
362 case UIPROTO_MASS_CBI_I:
363 proto = "CBI with CCI";
369 switch (id->bInterfaceSubClass) {
373 case UISUBCLASS_QIC157:
374 case UISUBCLASS_SFF8020I:
375 case UISUBCLASS_SFF8070I:
378 case UISUBCLASS_SCSI:
385 subclass = "Unknown";
388 has_intr = (id->bInterfaceProtocol == UIPROTO_MASS_CBI_I);
389 sc->iface_no = id->bInterfaceNumber;
391 device_printf(dev, "using %s over %s\n", subclass, proto);
392 if (strcmp(proto, "Bulk-Only") ||
393 (strcmp(subclass, "ATAPI") && strcmp(subclass, "SCSI"))) {
396 err = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
397 sc->xfer, atausb2_config, ATAUSB_T_BBB_MAX, sc,
400 /* skip reset first time */
401 sc->last_xfer_no = ATAUSB_T_BBB_COMMAND;
404 device_printf(sc->dev, "could not setup required "
405 "transfers, %s\n", usbd_errstr(err));
408 /* get number of devices so we can add matching channels */
409 request.bmRequestType = UT_READ_CLASS_INTERFACE;
410 request.bRequest = 0xfe; /* GET_MAX_LUN; */
411 USETW(request.wValue, 0);
412 USETW(request.wIndex, sc->iface_no);
413 USETW(request.wLength, sizeof(maxlun));
414 err = usbd_do_request(uaa->device, &Giant, &request, &maxlun);
418 device_printf(sc->dev, "get maxlun not supported %s\n",
424 device_printf(sc->dev, "maxlun=%d\n", sc->maxlun);
428 /* ata channels are children to this USB control device */
429 for (i = 0; i <= sc->maxlun; i++) {
430 if ((child = device_add_child(sc->dev, "ata",
431 devclass_find_free_unit(ata_devclass, 2))) == NULL) {
432 device_printf(sc->dev, "failed to add ata child device\n");
434 device_set_ivars(child, (void *)(intptr_t)i);
436 bus_generic_attach(sc->dev);
446 atausb2_detach(device_t dev)
448 struct atausb2_softc *sc = device_get_softc(dev);
452 /* teardown our statemachine */
454 usbd_transfer_unsetup(sc->xfer, ATAUSB_T_MAX);
456 /* detach & delete all children, if any */
458 if (!device_get_children(dev, &children, &nchildren)) {
459 for (i = 0; i < nchildren; i++) {
460 device_delete_child(dev, children[i]);
462 free(children, M_TEMP);
464 mtx_destroy(&sc->locked_mtx);
469 atausb2_transfer_start(struct atausb2_softc *sc, uint8_t xfer_no)
472 device_printf(sc->dev, "BBB transfer %d\n", xfer_no);
474 if (sc->xfer[xfer_no]) {
475 sc->last_xfer_no = xfer_no;
476 usbd_transfer_start(sc->xfer[xfer_no]);
478 atausb2_cancel_request(sc);
483 atausb2_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
485 struct atausb2_softc *sc = usbd_xfer_softc(xfer);
486 struct usb_device_request req;
487 struct usb_page_cache *pc;
489 switch (USB_GET_STATE(xfer)) {
490 case USB_ST_TRANSFERRED:
491 atausb2_transfer_start(sc, ATAUSB_T_BBB_RESET2);
495 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
496 req.bRequest = 0xff; /* bulk-only reset */
497 USETW(req.wValue, 0);
498 req.wIndex[0] = sc->iface_no;
500 USETW(req.wLength, 0);
502 pc = usbd_xfer_get_frame(xfer, 0);
503 usbd_copy_in(pc, 0, &req, sizeof(req));
505 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
506 usbd_xfer_set_frames(xfer, 1);
507 usbd_transfer_submit(xfer);
511 atausb2_tr_error(xfer, error);
518 atausb2_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
520 atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_RESET3,
521 ATAUSB_T_BBB_DATA_READ, error);
525 atausb2_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
527 atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_COMMAND,
528 ATAUSB_T_BBB_DATA_WRITE, error);
532 atausb2_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
533 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
535 struct atausb2_softc *sc = usbd_xfer_softc(xfer);
537 switch (USB_GET_STATE(xfer)) {
538 case USB_ST_TRANSFERRED:
540 atausb2_transfer_start(sc, next_xfer);
544 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
550 atausb2_tr_error(xfer, error);
557 atausb2_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
559 struct atausb2_softc *sc = usbd_xfer_softc(xfer);
560 struct ata_request *request = sc->ata_request;
561 struct ata_channel *ch;
562 struct usb_page_cache *pc;
565 switch (USB_GET_STATE(xfer)) {
566 case USB_ST_TRANSFERRED:
567 atausb2_transfer_start
568 (sc, ((request->flags & ATA_R_READ) ? ATAUSB_T_BBB_DATA_READ :
569 (request->flags & ATA_R_WRITE) ? ATAUSB_T_BBB_DATA_WRITE :
570 ATAUSB_T_BBB_STATUS));
578 ch = device_get_softc(request->parent);
580 sc->timeout = (request->timeout * 1000) + 5000;
582 tag = UGETDW(sc->cbw.tag) + 1;
584 USETDW(sc->cbw.signature, CBWSIGNATURE);
585 USETDW(sc->cbw.tag, tag);
586 USETDW(sc->cbw.transfer_length, request->bytecount);
587 sc->cbw.flags = (request->flags & ATA_R_READ) ? CBWFLAGS_IN : CBWFLAGS_OUT;
588 sc->cbw.lun = ch->unit;
590 bzero(sc->cbw.cdb, 16);
591 bcopy(request->u.atapi.ccb, sc->cbw.cdb, 12); /* XXX SOS */
593 pc = usbd_xfer_get_frame(xfer, 0);
594 usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
596 usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
597 usbd_transfer_submit(xfer);
602 atausb2_tr_error(xfer, error);
609 atausb2_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
611 struct atausb2_softc *sc = usbd_xfer_softc(xfer);
612 uint32_t max_bulk = usbd_xfer_max_len(xfer);
613 struct usb_page_cache *pc;
616 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
618 switch (USB_GET_STATE(xfer)) {
619 case USB_ST_TRANSFERRED:
621 pc = usbd_xfer_get_frame(xfer, 0);
622 usbd_copy_out(pc, 0, sc->ata_data, actlen);
624 sc->ata_bytecount -= actlen;
625 sc->ata_data += actlen;
626 sc->ata_donecount += actlen;
628 if (actlen < sumlen) {
630 sc->ata_bytecount = 0;
634 if (atausbdebug > 1) {
635 device_printf(sc->dev, "%s: max_bulk=%d, ata_bytecount=%d\n",
636 __FUNCTION__, max_bulk, sc->ata_bytecount);
638 if (sc->ata_bytecount == 0) {
639 atausb2_transfer_start(sc, ATAUSB_T_BBB_STATUS);
642 if (max_bulk > sc->ata_bytecount) {
643 max_bulk = sc->ata_bytecount;
645 usbd_xfer_set_timeout(xfer, sc->timeout);
646 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
648 usbd_transfer_submit(xfer);
652 if (error == USB_ERR_CANCELLED) {
653 atausb2_tr_error(xfer, error);
655 atausb2_transfer_start(sc, ATAUSB_T_BBB_DATA_RD_CS);
663 atausb2_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
665 atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_STATUS,
666 ATAUSB_T_BBB_DATA_READ, error);
670 atausb2_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
672 struct atausb2_softc *sc = usbd_xfer_softc(xfer);
673 struct usb_page_cache *pc;
674 uint32_t max_bulk = usbd_xfer_max_len(xfer);
677 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
679 switch (USB_GET_STATE(xfer)) {
680 case USB_ST_TRANSFERRED:
682 sc->ata_bytecount -= actlen;
683 sc->ata_data += actlen;
684 sc->ata_donecount += actlen;
688 if (atausbdebug > 1) {
689 device_printf(sc->dev, "%s: max_bulk=%d, ata_bytecount=%d\n",
690 __FUNCTION__, max_bulk, sc->ata_bytecount);
692 if (sc->ata_bytecount == 0) {
693 atausb2_transfer_start(sc, ATAUSB_T_BBB_STATUS);
696 if (max_bulk > sc->ata_bytecount) {
697 max_bulk = sc->ata_bytecount;
700 pc = usbd_xfer_get_frame(xfer, 0);
701 usbd_copy_in(pc, 0, sc->ata_data, max_bulk);
702 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
703 usbd_xfer_set_timeout(xfer, sc->timeout);
705 usbd_transfer_submit(xfer);
709 if (error == USB_ERR_CANCELLED) {
710 atausb2_tr_error(xfer, error);
712 atausb2_transfer_start(sc, ATAUSB_T_BBB_DATA_WR_CS);
720 atausb2_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
722 atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_STATUS,
723 ATAUSB_T_BBB_DATA_WRITE, error);
727 atausb2_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
729 struct atausb2_softc *sc = usbd_xfer_softc(xfer);
730 struct ata_request *request = sc->ata_request;
731 struct usb_page_cache *pc;
735 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
737 switch (USB_GET_STATE(xfer)) {
738 case USB_ST_TRANSFERRED:
740 if (actlen < sizeof(sc->csw)) {
741 bzero(&sc->csw, sizeof(sc->csw));
743 pc = usbd_xfer_get_frame(xfer, 0);
744 usbd_copy_out(pc, 0, &sc->csw, actlen);
746 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
747 request->donecount = sc->ata_donecount;
749 residue = UGETDW(sc->csw.residue);
752 residue = (request->bytecount - request->donecount);
754 if (residue > request->bytecount) {
756 device_printf(sc->dev, "truncating residue from %d "
757 "to %d bytes\n", residue,
760 residue = request->bytecount;
762 /* check CSW and handle eventual error */
763 if (UGETDW(sc->csw.signature) != CSWSIGNATURE) {
765 device_printf(sc->dev, "bad CSW signature 0x%08x != 0x%08x\n",
766 UGETDW(sc->csw.signature), CSWSIGNATURE);
769 } else if (UGETDW(sc->csw.tag) != UGETDW(sc->cbw.tag)) {
771 device_printf(sc->dev, "bad CSW tag %d != %d\n",
772 UGETDW(sc->csw.tag), UGETDW(sc->cbw.tag));
775 } else if (sc->csw.status > CSWSTATUS_PHASE) {
777 device_printf(sc->dev, "bad CSW status %d > %d\n",
778 sc->csw.status, CSWSTATUS_PHASE);
781 } else if (sc->csw.status == CSWSTATUS_PHASE) {
783 device_printf(sc->dev, "phase error residue = %d\n", residue);
786 } else if (request->donecount > request->bytecount) {
788 device_printf(sc->dev, "buffer overrun %d > %d\n",
789 request->donecount, request->bytecount);
792 } else if (sc->csw.status == CSWSTATUS_FAILED) {
794 device_printf(sc->dev, "CSWSTATUS_FAILED\n");
796 request->error = ATA_E_ATAPI_SENSE_MASK;
798 sc->last_xfer_no = ATAUSB_T_BBB_COMMAND;
800 sc->ata_request = NULL;
802 /* drop the USB transfer lock while doing the ATA interrupt */
803 mtx_unlock(&sc->locked_mtx);
805 ata_interrupt(device_get_softc(request->parent));
807 mtx_lock(&sc->locked_mtx);
811 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
812 usbd_transfer_submit(xfer);
817 if (error == USB_ERR_CANCELLED || sc->status_try) {
818 atausb2_tr_error(xfer, error);
821 atausb2_transfer_start(sc, ATAUSB_T_BBB_DATA_RD_CS);
829 atausb2_cancel_request(struct atausb2_softc *sc)
831 struct ata_request *request;
833 mtx_assert(&sc->locked_mtx, MA_OWNED);
835 request = sc->ata_request;
836 sc->ata_request = NULL;
837 sc->last_xfer_no = ATAUSB_T_BBB_RESET1;
840 request->error = ATA_E_ATAPI_SENSE_MASK;
842 mtx_unlock(&sc->locked_mtx);
844 ata_interrupt(device_get_softc(request->parent));
846 mtx_lock(&sc->locked_mtx);
851 atausb2_tr_error(struct usb_xfer *xfer, usb_error_t error)
853 struct atausb2_softc *sc = usbd_xfer_softc(xfer);
855 if (error != USB_ERR_CANCELLED) {
858 device_printf(sc->dev, "transfer failed, %s, in state %d "
859 "-> BULK reset\n", usbd_errstr(error),
863 atausb2_cancel_request(sc);
869 struct atapi_inquiry {
871 uint8_t device_modifier;
873 uint8_t response_format;
880 /* uint8_t crap[60]; */
884 ata_usbchannel_begin_transaction(struct ata_request *request)
886 struct atausb2_softc *sc =
887 device_get_softc(device_get_parent(request->parent));
890 if (atausbdebug > 1) {
891 device_printf(request->dev, "begin_transaction %s\n",
892 ata_cmd2str(request));
894 mtx_lock(&sc->locked_mtx);
896 /* sanity, just in case */
897 if (sc->ata_request) {
898 device_printf(request->dev, "begin is busy, "
899 "state = %d\n", sc->last_xfer_no);
900 request->result = EBUSY;
901 error = ATA_OP_FINISHED;
905 * XXX SOS convert the request into the format used, only BBB for
909 /* ATA/ATAPI IDENTIFY needs special treatment */
910 if (!(request->flags & ATA_R_ATAPI)) {
911 if (request->u.ata.command != ATA_ATAPI_IDENTIFY) {
912 device_printf(request->dev, "%s unsupported\n",
913 ata_cmd2str(request));
914 request->result = EIO;
915 error = ATA_OP_FINISHED;
918 request->flags |= ATA_R_ATAPI;
919 bzero(request->u.atapi.ccb, 16);
920 request->u.atapi.ccb[0] = ATAPI_INQUIRY;
921 request->u.atapi.ccb[4] = 255; /* sizeof(struct
923 request->data += 256; /* arbitrary offset into ata_param */
924 request->bytecount = 255; /* sizeof(struct
927 if (sc->xfer[sc->last_xfer_no]) {
929 sc->ata_request = request;
930 sc->ata_bytecount = request->bytecount;
931 sc->ata_data = request->data;
932 sc->ata_donecount = 0;
934 usbd_transfer_start(sc->xfer[sc->last_xfer_no]);
935 error = ATA_OP_CONTINUES;
937 request->result = EIO;
938 error = ATA_OP_FINISHED;
942 mtx_unlock(&sc->locked_mtx);
947 ata_usbchannel_end_transaction(struct ata_request *request)
949 if (atausbdebug > 1) {
950 device_printf(request->dev, "end_transaction %s\n",
951 ata_cmd2str(request));
954 * XXX SOS convert the request from the format used, only BBB for
958 /* ATA/ATAPI IDENTIFY needs special treatment */
959 if ((request->flags & ATA_R_ATAPI) &&
960 (request->u.atapi.ccb[0] == ATAPI_INQUIRY)) {
961 struct ata_device *atadev = device_get_softc(request->dev);
962 struct atapi_inquiry *inquiry = (struct atapi_inquiry *)request->data;
965 /* convert inquiry data into simple ata_param like format */
966 atadev->param.config = ATA_PROTO_ATAPI | ATA_PROTO_ATAPI_12;
967 atadev->param.config |= (inquiry->device_type & 0x1f) << 8;
968 bzero(atadev->param.model, sizeof(atadev->param.model));
969 strncpy(atadev->param.model, inquiry->vendor, 8);
970 strcpy(atadev->param.model, " ");
971 strncpy(atadev->param.model, inquiry->product, 16);
972 ptr = (uint16_t *)(atadev->param.model + sizeof(atadev->param.model));
973 while (--ptr >= (uint16_t *)atadev->param.model) {
976 strncpy(atadev->param.revision, inquiry->revision, 4);
977 ptr = (uint16_t *)(atadev->param.revision + sizeof(atadev->param.revision));
978 while (--ptr >= (uint16_t *)atadev->param.revision) {
983 return (ATA_OP_FINISHED);
987 ata_usbchannel_probe(device_t dev)
991 snprintf(buffer, sizeof(buffer), "USB lun %d",
992 (int)(intptr_t)device_get_ivars(dev));
993 device_set_desc_copy(dev, buffer);
999 ata_usbchannel_attach(device_t dev)
1001 struct ata_channel *ch = device_get_softc(dev);
1007 /* initialize the softc basics */
1009 ch->unit = (intptr_t)device_get_ivars(dev);
1010 ch->state = ATA_IDLE;
1011 ch->hw.begin_transaction = ata_usbchannel_begin_transaction;
1012 ch->hw.end_transaction = ata_usbchannel_end_transaction;
1013 ch->hw.status = NULL;
1014 ch->hw.command = NULL;
1015 bzero(&ch->state_mtx, sizeof(struct mtx));
1016 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
1017 bzero(&ch->queue_mtx, sizeof(struct mtx));
1018 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
1019 TAILQ_INIT(&ch->ata_queue);
1021 /* XXX SOS reset the controller HW, the channel and device(s) */
1022 /* ATA_RESET(dev); */
1024 /* probe and attach device on this channel */
1025 ch->devices = ATA_ATAPI_MASTER;
1026 if (!ata_delayed_attach) {
1033 ata_usbchannel_detach(device_t dev)
1035 struct ata_channel *ch = device_get_softc(dev);
1043 /* detach & delete all children */
1044 if (!device_get_children(dev, &children, &nchildren)) {
1045 for (i = 0; i < nchildren; i++)
1047 device_delete_child(dev, children[i]);
1048 free(children, M_TEMP);
1050 mtx_destroy(&ch->state_mtx);
1051 mtx_destroy(&ch->queue_mtx);
1056 ata_usbchannel_setmode(device_t parent, device_t dev)
1058 struct atausb2_softc *sc = device_get_softc(GRANDPARENT(dev));
1059 struct ata_device *atadev = device_get_softc(dev);
1061 if (sc->usb2_speed == USB_SPEED_HIGH)
1062 atadev->mode = ATA_USB2;
1064 atadev->mode = ATA_USB1;
1068 ata_usbchannel_locking(device_t dev, int flags)
1070 struct atausb2_softc *sc = device_get_softc(device_get_parent(dev));
1071 struct ata_channel *ch = device_get_softc(dev);
1074 mtx_lock(&sc->locked_mtx);
1077 if (sc->locked_ch == NULL)
1079 if (sc->locked_ch != ch)
1080 sc->restart_ch = ch;
1084 if (sc->locked_ch == ch) {
1085 sc->locked_ch = NULL;
1086 if (sc->restart_ch) {
1087 ch = sc->restart_ch;
1088 sc->restart_ch = NULL;
1089 mtx_unlock(&sc->locked_mtx);
1099 if (sc->locked_ch) {
1100 res = sc->locked_ch->unit;
1102 mtx_unlock(&sc->locked_mtx);
1106 static device_method_t ata_usbchannel_methods[] = {
1107 /* device interface */
1108 DEVMETHOD(device_probe, ata_usbchannel_probe),
1109 DEVMETHOD(device_attach, ata_usbchannel_attach),
1110 DEVMETHOD(device_detach, ata_usbchannel_detach),
1113 DEVMETHOD(ata_setmode, ata_usbchannel_setmode),
1114 DEVMETHOD(ata_locking, ata_usbchannel_locking),
1115 /* DEVMETHOD(ata_reset, ata_usbchannel_reset), */
1120 static driver_t ata_usbchannel_driver = {
1122 ata_usbchannel_methods,
1123 sizeof(struct ata_channel),
1126 DRIVER_MODULE(ata, atausb, ata_usbchannel_driver, ata_devclass, 0, 0);
1127 MODULE_DEPEND(atausb, ata, 1, 1, 1);