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.6 2007/06/28 06:32:31 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 USB_DECLARE_DRIVER(atausb
);
146 DRIVER_MODULE(atausb
, uhub
, atausb_driver
, atausb_devclass
, 0, 0);
147 MODULE_VERSION(atausb
, 1);
150 atausb_match(device_t dev
)
152 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
153 usb_interface_descriptor_t
*id
;
155 if (uaa
->iface
== NULL
)
158 id
= usbd_get_interface_descriptor(uaa
->iface
);
159 if (!id
|| id
->bInterfaceClass
!= UICLASS_MASS
)
162 switch (id
->bInterfaceSubClass
) {
163 case UISUBCLASS_QIC157
:
165 case UISUBCLASS_SCSI
:
166 case UISUBCLASS_SFF8020I
:
167 case UISUBCLASS_SFF8070I
:
169 switch (id
->bInterfaceProtocol
) {
170 case UIPROTO_MASS_CBI
:
171 case UIPROTO_MASS_CBI_I
:
172 case UIPROTO_MASS_BBB
:
173 case UIPROTO_MASS_BBB_OLD
:
174 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO
;
176 return UMATCH_IFACECLASS_IFACESUBCLASS
;
180 return UMATCH_IFACECLASS
;
185 atausb_attach(device_t dev
)
187 struct atausb_softc
*sc
= device_get_softc(dev
);
188 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
189 usb_interface_descriptor_t
*id
;
190 usb_endpoint_descriptor_t
*ed
;
191 usbd_device_handle udev
;
192 usb_device_request_t request
;
193 char devinfo
[1024], *proto
, *subclass
;
198 usbd_devinfo(uaa
->device
, 0, devinfo
);
199 device_set_desc_copy(dev
, devinfo
);
200 sc
->bulkin
= sc
->bulkout
= sc
->bulkirq
= -1;
201 sc
->bulkin_pipe
= sc
->bulkout_pipe
= sc
->bulkirq_pipe
= NULL
;
202 sc
->iface
= uaa
->iface
;
203 sc
->ifaceno
= uaa
->ifaceno
;
206 sc
->locked_ch
= NULL
;
207 sc
->restart_ch
= NULL
;
208 spin_init(&sc
->locked_mtx
);
210 id
= usbd_get_interface_descriptor(sc
->iface
);
211 switch (id
->bInterfaceProtocol
) {
212 case UIPROTO_MASS_BBB
:
213 case UIPROTO_MASS_BBB_OLD
:
216 case UIPROTO_MASS_CBI
:
219 case UIPROTO_MASS_CBI_I
:
220 proto
= "CBI with CCI";
225 switch (id
->bInterfaceSubClass
) {
229 case UISUBCLASS_QIC157
:
230 case UISUBCLASS_SFF8020I
:
231 case UISUBCLASS_SFF8070I
:
234 case UISUBCLASS_SCSI
:
241 subclass
= "Unknown";
243 device_printf(dev
, "using %s over %s\n", subclass
, proto
);
244 if (strcmp(proto
, "Bulk-Only") ||
245 (strcmp(subclass
, "ATAPI") && strcmp(subclass
, "SCSI")))
248 for (i
= 0 ; i
< id
->bNumEndpoints
; i
++) {
249 if (!(ed
= usbd_interface2endpoint_descriptor(sc
->iface
, i
))) {
250 device_printf(sc
->dev
, "could not read endpoint descriptor\n");
253 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
254 (ed
->bmAttributes
& UE_XFERTYPE
) == UE_BULK
) {
255 sc
->bulkin
= ed
->bEndpointAddress
;
257 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
258 (ed
->bmAttributes
& UE_XFERTYPE
) == UE_BULK
) {
259 sc
->bulkout
= ed
->bEndpointAddress
;
261 if (id
->bInterfaceProtocol
== UIPROTO_MASS_CBI_I
&&
262 UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
263 (ed
->bmAttributes
& UE_XFERTYPE
) == UE_INTERRUPT
) {
264 sc
->bulkirq
= ed
->bEndpointAddress
;
268 /* check whether we found at least the endpoints we need */
269 if (!sc
->bulkin
|| !sc
->bulkout
) {
270 device_printf(sc
->dev
, "needed endpoints not found (%d,%d)\n",
271 sc
->bulkin
, sc
->bulkout
);
277 if (usbd_open_pipe(sc
->iface
, sc
->bulkout
,
278 USBD_EXCLUSIVE_USE
, &sc
->bulkout_pipe
)) {
279 device_printf(sc
->dev
, "cannot open bulkout pipe (%d)\n", sc
->bulkout
);
283 if (usbd_open_pipe(sc
->iface
, sc
->bulkin
,
284 USBD_EXCLUSIVE_USE
, &sc
->bulkin_pipe
)) {
285 device_printf(sc
->dev
, "cannot open bulkin pipe (%d)\n", sc
->bulkin
);
289 if (id
->bInterfaceProtocol
== UIPROTO_MASS_CBI_I
) {
290 if (usbd_open_pipe(sc
->iface
, sc
->bulkirq
,
291 USBD_EXCLUSIVE_USE
, &sc
->bulkirq_pipe
)) {
292 device_printf(sc
->dev
, "cannot open bulkirq pipe (%d)\n",
298 sc
->state
= ATAUSB_S_ATTACH
;
300 /* alloc needed number of transfer handles */
301 for (i
= 0; i
< ATAUSB_T_MAX
; i
++) {
302 sc
->transfer
[i
] = usbd_alloc_xfer(uaa
->device
);
303 if (!sc
->transfer
[i
]) {
304 device_printf(sc
->dev
, "out of memory\n");
310 /* driver is ready to process requests here */
311 sc
->state
= ATAUSB_S_IDLE
;
313 /* get number of devices so we can add matching channels */
314 usbd_interface2device_handle(sc
->iface
, &udev
);
315 request
.bmRequestType
= UT_READ_CLASS_INTERFACE
;
316 request
.bRequest
= 0xfe; /* GET_MAX_LUN; */
317 USETW(request
.wValue
, 0);
318 USETW(request
.wIndex
, sc
->ifaceno
);
319 USETW(request
.wLength
, sizeof(maxlun
));
320 switch ((err
= usbd_do_request(udev
, &request
, &maxlun
))) {
321 case USBD_NORMAL_COMPLETION
:
323 device_printf(sc
->dev
, "maxlun=%d\n", maxlun
);
328 device_printf(sc
->dev
, "get maxlun not supported %s\n",
332 /* ata channels are children to this USB control device */
333 for (i
= 0; i
<= sc
->maxlun
; i
++) {
334 /* XXX TGEN devclass_find_free_unit() implementation */
336 while (freeunit
< devclass_get_maxunit(ata_devclass
) &&
337 devclass_get_device(ata_devclass
, freeunit
) != NULL
)
339 if (!device_add_child(sc
->dev
, "ata", freeunit
)) {
340 device_printf(sc
->dev
, "failed to attach ata child device\n");
345 bus_generic_attach(sc
->dev
);
350 atausb_detach(device_t dev
)
352 struct atausb_softc
*sc
= device_get_softc(dev
);
353 usbd_device_handle udev
;
357 /* signal that device is going away */
358 sc
->state
= ATAUSB_S_DETACH
;
360 /* abort all the pipes in case there are active transfers */
361 usbd_interface2device_handle(sc
->iface
, &udev
);
362 usbd_abort_pipe(udev
->default_pipe
);
363 if (sc
->bulkout_pipe
)
364 usbd_abort_pipe(sc
->bulkout_pipe
);
366 usbd_abort_pipe(sc
->bulkin_pipe
);
367 if (sc
->bulkirq_pipe
)
368 usbd_abort_pipe(sc
->bulkirq_pipe
);
370 /* detach & delete all children */
371 if (!device_get_children(dev
, &children
, &nchildren
)) {
372 for (i
= 0; i
< nchildren
; i
++)
373 device_delete_child(dev
, children
[i
]);
374 kfree(children
, M_TEMP
);
377 /* free the transfers */
378 for (i
= 0; i
< ATAUSB_T_MAX
; i
++)
380 usbd_free_xfer(sc
->transfer
[i
]);
382 /* remove all the pipes */
383 if (sc
->bulkout_pipe
)
384 usbd_close_pipe(sc
->bulkout_pipe
);
386 usbd_close_pipe(sc
->bulkin_pipe
);
387 if (sc
->bulkirq_pipe
)
388 usbd_close_pipe(sc
->bulkirq_pipe
);
390 spin_uninit(&sc
->locked_mtx
);
396 * Generic USB transfer routines
399 atausb_start(struct atausb_softc
*sc
, usbd_pipe_handle pipe
,
400 void *buffer
, int buflen
, int flags
, usbd_xfer_handle xfer
)
404 if (sc
->state
== ATAUSB_S_DETACH
)
405 return USBD_NOT_STARTED
;
407 usbd_setup_xfer(xfer
, pipe
, (void *)sc
, buffer
, buflen
, flags
,
408 sc
->timeout
, atausb_bbb_finish
);
409 err
= usbd_transfer(xfer
);
410 if (err
&& (err
!= USBD_IN_PROGRESS
)) {
412 device_printf(sc
->dev
, "failed to setup transfer, %s\n",
416 return USBD_NORMAL_COMPLETION
;
420 atausb_ctl_start(struct atausb_softc
*sc
, usbd_device_handle udev
,
421 usb_device_request_t
*req
, void *buffer
, int buflen
, int flags
,
422 usbd_xfer_handle xfer
)
426 if (sc
->state
== ATAUSB_S_DETACH
)
427 return USBD_NOT_STARTED
;
429 usbd_setup_default_xfer(xfer
, udev
, (void *)sc
, sc
->timeout
, req
,
430 buffer
, buflen
, flags
, atausb_bbb_finish
);
431 err
= usbd_transfer(xfer
);
432 if (err
&& (err
!= USBD_IN_PROGRESS
)) {
434 device_printf(sc
->dev
, "failed to setup ctl transfer, %s\n",
438 return USBD_NORMAL_COMPLETION
;
442 atausb_clear_stall(struct atausb_softc
*sc
, u_int8_t endpt
,
443 usbd_pipe_handle pipe
, int state
, usbd_xfer_handle xfer
)
445 usbd_device_handle udev
;
448 device_printf(sc
->dev
, "clear endpoint 0x%02x stall\n", endpt
);
449 usbd_interface2device_handle(sc
->iface
, &udev
);
451 usbd_clear_endpoint_toggle(pipe
);
452 sc
->usb_request
.bmRequestType
= UT_WRITE_ENDPOINT
;
453 sc
->usb_request
.bRequest
= UR_CLEAR_FEATURE
;
454 USETW(sc
->usb_request
.wValue
, UF_ENDPOINT_HALT
);
455 USETW(sc
->usb_request
.wIndex
, endpt
);
456 USETW(sc
->usb_request
.wLength
, 0);
457 atausb_ctl_start(sc
, udev
, &sc
->usb_request
, NULL
, 0, 0, xfer
);
462 * Bulk-Only transport part
465 atausb_bbb_reset(struct atausb_softc
*sc
)
467 usbd_device_handle udev
;
470 device_printf(sc
->dev
, "Bulk Reset\n");
472 sc
->state
= ATAUSB_S_BBB_RESET1
;
473 usbd_interface2device_handle(sc
->iface
, &udev
);
474 sc
->usb_request
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
475 sc
->usb_request
.bRequest
= 0xff; /* bulk-only reset */
476 USETW(sc
->usb_request
.wValue
, 0);
477 USETW(sc
->usb_request
.wIndex
, sc
->ifaceno
);
478 USETW(sc
->usb_request
.wLength
, 0);
479 atausb_ctl_start(sc
, udev
, &sc
->usb_request
, NULL
,
480 0, 0, sc
->transfer
[ATAUSB_T_BBB_RESET1
]);
484 atausb_bbb_start(struct ata_request
*request
)
486 struct atausb_softc
*sc
=
487 device_get_softc(device_get_parent(request
->parent
));
488 struct ata_channel
*ch
= device_get_softc(request
->parent
);
490 sc
->timeout
= (request
->timeout
* 1000) + 5000;
491 USETDW(sc
->cbw
.signature
, CBWSIGNATURE
);
492 USETDW(sc
->cbw
.tag
, UGETDW(sc
->cbw
.tag
) + 1);
493 USETDW(sc
->cbw
.transfer_length
, request
->bytecount
);
494 sc
->cbw
.flags
= (request
->flags
& ATA_R_READ
) ? CBWFLAGS_IN
: CBWFLAGS_OUT
;
495 sc
->cbw
.lun
= ch
->unit
;
497 bzero(sc
->cbw
.cdb
, 16);
498 bcopy(request
->u
.atapi
.ccb
, sc
->cbw
.cdb
, 12); /* XXX SOS */
499 sc
->state
= ATAUSB_S_BBB_COMMAND
;
500 if (atausb_start(sc
, sc
->bulkout_pipe
, &sc
->cbw
, sizeof(struct bbb_cbw
),
501 0, sc
->transfer
[ATAUSB_T_BBB_CBW
])) {
502 request
->result
= EIO
;
504 device_printf(request
->dev
, "cannot setup USB transfer\n");
505 atausb_bbb_reset(sc
);
506 return ATA_OP_FINISHED
;
508 return ATA_OP_CONTINUES
;
512 atausb_bbb_finish(usbd_xfer_handle xfer
, usbd_private_handle priv
,
515 struct atausb_softc
*sc
= (struct atausb_softc
*)priv
;
516 struct ata_request
*request
= sc
->ata_request
;
517 usbd_xfer_handle next_xfer
;
519 /* device_printf(sc->dev, "BBB state %d: %s\n", sc->state,
520 usbd_errstr(err)); */
522 if (sc
->state
== ATAUSB_S_DETACH
) {
523 device_printf(sc
->dev
, "WARNING - device has been removed\n");
528 case ATAUSB_S_BBB_COMMAND
: /* command transport phase */
531 device_printf(sc
->dev
, "failed to send CBW\n");
532 request
->result
= EIO
;
533 atausb_bbb_reset(sc
);
537 /* next is data transport phase, setup transfer */
538 sc
->state
= ATAUSB_S_BBB_DATA
;
539 if (request
->flags
& ATA_R_READ
) {
540 if (atausb_start(sc
, sc
->bulkin_pipe
,
541 request
->data
, request
->bytecount
,
543 sc
->transfer
[ATAUSB_T_BBB_DATA
])) {
544 request
->result
= EIO
;
545 atausb_bbb_reset(sc
);
549 if (request
->flags
& ATA_R_WRITE
) {
550 if (atausb_start(sc
, sc
->bulkout_pipe
,
551 request
->data
, request
->bytecount
,
552 0, sc
->transfer
[ATAUSB_T_BBB_DATA
])) {
553 request
->result
= EIO
;
554 atausb_bbb_reset(sc
);
560 case ATAUSB_S_BBB_DATA
: /* data transport phase */
561 if (request
->flags
& (ATA_R_READ
| ATA_R_WRITE
)) {
562 usbd_get_xfer_status(xfer
, NULL
, NULL
, &request
->donecount
, NULL
);
565 device_printf(sc
->dev
, "data %s count %d failed: %s\n",
566 (request
->flags
& ATA_R_READ
?"read":"write"),
567 request
->bytecount
, usbd_errstr(err
));
568 if (err
== USBD_STALLED
) {
569 atausb_clear_stall(sc
,
570 (request
->flags
& ATA_R_READ
?
571 sc
->bulkin
: sc
->bulkout
),
572 (request
->flags
& ATA_R_READ
?
573 sc
->bulkin_pipe
: sc
->bulkout_pipe
),
575 sc
->transfer
[ATAUSB_T_BBB_DCLEAR
]);
578 request
->result
= EIO
;
579 atausb_bbb_reset(sc
);
586 case ATAUSB_S_BBB_DCLEAR
: /* stall clear after data phase */
587 case ATAUSB_S_BBB_SCLEAR
: /* stall clear after status phase */
590 device_printf(sc
->dev
, "bulk%s stall clear failed %s\n",
591 (request
->flags
& ATA_R_READ
? "in" : "out"),
593 request
->result
= EIO
;
594 atausb_bbb_reset(sc
);
598 if (sc
->state
== ATAUSB_S_BBB_COMMAND
||
599 sc
->state
== ATAUSB_S_BBB_DATA
||
600 sc
->state
== ATAUSB_S_BBB_DCLEAR
) {
601 /* first attempt on status transport phase setup transfer */
602 sc
->state
= ATAUSB_S_BBB_STATUS1
;
603 next_xfer
= sc
->transfer
[ATAUSB_T_BBB_CSW1
];
606 /* second attempt of fetching status */
607 sc
->state
= ATAUSB_S_BBB_STATUS2
;
608 next_xfer
= sc
->transfer
[ATAUSB_T_BBB_CSW2
];
610 if (atausb_start(sc
, sc
->bulkin_pipe
, &sc
->csw
, sizeof(struct bbb_csw
),
611 USBD_SHORT_XFER_OK
, next_xfer
)) {
612 request
->result
= EIO
;
613 atausb_bbb_reset(sc
);
617 case ATAUSB_S_BBB_STATUS1
: /* status transfer first attempt */
618 case ATAUSB_S_BBB_STATUS2
: /* status transfer second attempt */
621 device_printf(sc
->dev
, "cannot get CSW, %s%s\n",
623 sc
->state
== ATAUSB_S_BBB_STATUS1
? ", retry":"");
624 if (sc
->state
== ATAUSB_S_BBB_STATUS1
) {
625 atausb_clear_stall(sc
, sc
->bulkin
, sc
->bulkin_pipe
,
627 sc
->transfer
[ATAUSB_T_BBB_SCLEAR
]);
630 request
->result
= EIO
;
631 atausb_bbb_reset(sc
);
636 int residue
= UGETDW(sc
->csw
.residue
);
639 (request
->bytecount
- request
->donecount
))
640 residue
= request
->bytecount
- request
->donecount
;
642 /* check CSW and handle eventual error */
643 if (UGETDW(sc
->csw
.signature
) != CSWSIGNATURE
) {
645 device_printf(sc
->dev
, "bad CSW signature 0x%08x != 0x%08x\n",
646 UGETDW(sc
->csw
.signature
), CSWSIGNATURE
);
647 request
->result
= EIO
;
648 atausb_bbb_reset(sc
);
651 else if (UGETDW(sc
->csw
.tag
) != UGETDW(sc
->cbw
.tag
)) {
653 device_printf(sc
->dev
, "bad CSW tag %d != %d\n",
654 UGETDW(sc
->csw
.tag
), UGETDW(sc
->cbw
.tag
));
655 request
->result
= EIO
;
656 atausb_bbb_reset(sc
);
659 else if (sc
->csw
.status
> CSWSTATUS_PHASE
) {
661 device_printf(sc
->dev
, "bad CSW status %d > %d\n",
662 sc
->csw
.status
, CSWSTATUS_PHASE
);
663 request
->result
= EIO
;
664 atausb_bbb_reset(sc
);
667 else if (sc
->csw
.status
== CSWSTATUS_PHASE
) {
669 device_printf(sc
->dev
, "phase error residue = %d\n", residue
);
670 request
->result
= EIO
;
671 atausb_bbb_reset(sc
);
674 else if (request
->donecount
> request
->bytecount
) {
676 device_printf(sc
->dev
, "buffer overrun %d > %d",
677 request
->donecount
, request
->bytecount
);
678 request
->result
= EIO
;
679 atausb_bbb_reset(sc
);
682 else if (sc
->csw
.status
== CSWSTATUS_FAILED
) {
684 device_printf(sc
->dev
, "CSWSTATUS_FAILED\n");
685 request
->error
= ATA_E_ATAPI_SENSE_MASK
;
686 sc
->state
= ATAUSB_S_IDLE
;
687 ata_interrupt(device_get_softc(request
->parent
));
691 sc
->state
= ATAUSB_S_IDLE
;
692 ata_interrupt(device_get_softc(request
->parent
));
697 case ATAUSB_S_BBB_RESET1
:
700 device_printf(sc
->dev
,
701 "BBB reset failure: %s\n", usbd_errstr(err
));
702 atausb_clear_stall(sc
, sc
->bulkin
, sc
->bulkin_pipe
,
704 sc
->transfer
[ATAUSB_T_BBB_RESET2
]);
707 case ATAUSB_S_BBB_RESET2
:
710 device_printf(sc
->dev
, "BBB bulkin clear stall failure: %s\n",
712 atausb_clear_stall(sc
, sc
->bulkout
, sc
->bulkout_pipe
,
714 sc
->transfer
[ATAUSB_T_BBB_RESET3
]);
717 case ATAUSB_S_BBB_RESET3
:
720 device_printf(sc
->dev
, "BBB bulk-out clear stall failure: %s\n",
722 sc
->state
= ATAUSB_S_IDLE
;
725 request
->result
= ENXIO
;
727 request
->result
= EIO
;
728 ata_interrupt(device_get_softc(request
->parent
));
734 device_printf(sc
->dev
, "unknown state %d", sc
->state
);
742 struct atapi_inquiry
{
743 u_int8_t device_type
;
744 u_int8_t device_modifier
;
746 u_int8_t response_format
;
748 u_int8_t reserved
[2];
751 u_int8_t product
[16];
752 u_int8_t revision
[4];
753 /* u_int8_t crap[60]; */
757 ata_usbchannel_begin_transaction(struct ata_request
*request
)
759 struct atausb_softc
*sc
=
760 device_get_softc(device_get_parent(request
->parent
));
763 device_printf(request
->dev
, "begin_transaction %s\n",
764 ata_cmd2str(request
));
766 /* sanity just in case */
767 if (sc
->state
!= ATAUSB_S_IDLE
) {
768 kprintf("begin is busy (%d)\n", sc
->state
);
769 request
->result
= EBUSY
;
770 return ATA_OP_FINISHED
;
773 /* XXX SOS convert the request into the format used, only BBB for now*/
774 sc
->ata_request
= request
;
776 /* ATA/ATAPI IDENTIFY needs special treatment */
777 if (!(request
->flags
& ATA_R_ATAPI
)) {
778 if (request
->u
.ata
.command
!= ATA_ATAPI_IDENTIFY
) {
779 device_printf(request
->dev
,"%s unsupported\n",ata_cmd2str(request
));
780 request
->result
= EIO
;
781 return ATA_OP_FINISHED
;
783 request
->flags
|= ATA_R_ATAPI
;
784 bzero(request
->u
.atapi
.ccb
, 16);
785 request
->u
.atapi
.ccb
[0] = ATAPI_INQUIRY
;
786 request
->u
.atapi
.ccb
[4] = 255; /* sizeof(struct atapi_inquiry); */
787 request
->data
+= 256; /* arbitrary offset into ata_param */
788 request
->bytecount
= 255; /* sizeof(struct atapi_inquiry); */
790 return atausb_bbb_start(request
);
794 ata_usbchannel_end_transaction(struct ata_request
*request
)
797 device_printf(request
->dev
, "end_transaction %s\n",
798 ata_cmd2str(request
));
800 /* XXX SOS convert the request from the format used, only BBB for now*/
802 /* ATA/ATAPI IDENTIFY needs special treatment */
803 if ((request
->flags
& ATA_R_ATAPI
) &&
804 (request
->u
.atapi
.ccb
[0] == ATAPI_INQUIRY
)) {
805 struct ata_device
*atadev
= device_get_softc(request
->dev
);
806 struct atapi_inquiry
*inquiry
= (struct atapi_inquiry
*)request
->data
;
809 /* convert inquiry data into simple ata_param like format */
810 atadev
->param
.config
= ATA_PROTO_ATAPI
| ATA_PROTO_ATAPI_12
;
811 atadev
->param
.config
|= (inquiry
->device_type
& 0x1f) << 8;
812 bzero(atadev
->param
.model
, sizeof(atadev
->param
.model
));
813 strncpy(atadev
->param
.model
, inquiry
->vendor
, 8);
814 strcpy(atadev
->param
.model
, " ");
815 strncpy(atadev
->param
.model
, inquiry
->product
, 16);
816 ptr
= (u_int16_t
*)(atadev
->param
.model
+ sizeof(atadev
->param
.model
));
817 while (--ptr
>= (u_int16_t
*)atadev
->param
.model
)
819 strncpy(atadev
->param
.revision
, inquiry
->revision
, 4);
820 ptr
=(u_int16_t
*)(atadev
->param
.revision
+sizeof(atadev
->param
.revision
));
821 while (--ptr
>= (u_int16_t
*)atadev
->param
.revision
)
825 return ATA_OP_FINISHED
;
829 ata_usbchannel_probe(device_t dev
)
831 struct ata_channel
*ch
= device_get_softc(dev
);
836 /* take care of green memory */
837 bzero(ch
, sizeof(struct ata_channel
));
839 /* find channel number on this controller */
840 device_get_children(device_get_parent(dev
), &children
, &count
);
841 for (i
= 0; i
< count
; i
++) {
842 if (children
[i
] == dev
)
845 kfree(children
, M_TEMP
);
847 ksprintf(buffer
, "USB lun %d", ch
->unit
);
848 device_set_desc_copy(dev
, buffer
);
854 ata_usbchannel_attach(device_t dev
)
856 struct ata_channel
*ch
= device_get_softc(dev
);
858 /* initialize the softc basics */
860 ch
->state
= ATA_IDLE
;
861 ch
->hw
.begin_transaction
= ata_usbchannel_begin_transaction
;
862 ch
->hw
.end_transaction
= ata_usbchannel_end_transaction
;
863 ch
->hw
.status
= NULL
;
864 ch
->hw
.command
= NULL
;
865 spin_init(&ch
->state_mtx
);
866 spin_init(&ch
->queue_mtx
);
867 TAILQ_INIT(&ch
->ata_queue
);
869 /* XXX SOS reset the controller HW, the channel and device(s) */
870 /* ATA_RESET(dev); */
872 /* probe and attach device on this channel */
873 ch
->devices
= ATA_ATAPI_MASTER
;
879 ata_usbchannel_detach(device_t dev
)
881 struct ata_channel
*ch
= device_get_softc(dev
);
885 /* detach & delete all children */
886 if (!device_get_children(dev
, &children
, &nchildren
)) {
887 for (i
= 0; i
< nchildren
; i
++)
889 device_delete_child(dev
, children
[i
]);
890 kfree(children
, M_TEMP
);
892 spin_uninit(&ch
->state_mtx
);
893 spin_uninit(&ch
->queue_mtx
);
898 ata_usbchannel_setmode(device_t parent
, device_t dev
)
900 struct atausb_softc
*sc
= device_get_softc(GRANDPARENT(dev
));
901 struct ata_device
*atadev
= device_get_softc(dev
);
902 usbd_device_handle udev
;
904 usbd_interface2device_handle(sc
->iface
, &udev
);
905 if (usbd_get_speed(udev
) == USB_SPEED_HIGH
)
906 atadev
->mode
= ATA_USB2
;
908 atadev
->mode
= ATA_USB1
;
912 ata_usbchannel_locking(device_t dev
, int flags
)
914 struct atausb_softc
*sc
= device_get_softc(device_get_parent(dev
));
915 struct ata_channel
*ch
= device_get_softc(dev
);
919 spin_lock_wr(&sc
->locked_mtx
);
922 if (sc
->locked_ch
== NULL
)
924 if (sc
->locked_ch
!= ch
)
929 if (sc
->locked_ch
== ch
) {
930 sc
->locked_ch
= NULL
;
931 if (sc
->restart_ch
) {
933 sc
->restart_ch
= NULL
;
934 spin_unlock_wr(&sc
->locked_mtx
);
945 res
= sc
->locked_ch
->unit
;
946 spin_unlock_wr(&sc
->locked_mtx
);
950 static device_method_t ata_usbchannel_methods
[] = {
951 /* device interface */
952 DEVMETHOD(device_probe
, ata_usbchannel_probe
),
953 DEVMETHOD(device_attach
, ata_usbchannel_attach
),
954 DEVMETHOD(device_detach
, ata_usbchannel_detach
),
957 DEVMETHOD(ata_setmode
, ata_usbchannel_setmode
),
958 DEVMETHOD(ata_locking
, ata_usbchannel_locking
),
959 /* DEVMETHOD(ata_reset, ata_usbchannel_reset), */
964 static driver_t ata_usbchannel_driver
= {
966 ata_usbchannel_methods
,
967 sizeof(struct ata_channel
),
970 DRIVER_MODULE(ata
, atausb
, ata_usbchannel_driver
, ata_devclass
, 0, 0);
971 MODULE_DEPEND(atausb
, ata
, 1, 1, 1);