Nuke device_ptr_t, USBBASEDEVICE, USBDEVNAME(), USBDEVUNIT(), USBGETSOFTC(),
[dragonfly.git] / sys / dev / disk / nata / ata-usb.c
blobe9743985979cc87a943b42bb3dcdfccf4b1f4ac5
1 /*-
2 * Copyright (c) 2006 Søren Schmidt <sos@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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 $
30 #include "opt_ata.h"
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/libkern.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/nata.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>
46 #include "ata-all.h"
47 #include "ata_if.h"
49 /* Command Block Wrapper */
50 struct bbb_cbw {
51 u_int8_t signature[4];
52 #define CBWSIGNATURE 0x43425355
54 u_int8_t tag[4];
55 u_int8_t transfer_length[4];
56 u_int8_t flags;
57 #define CBWFLAGS_OUT 0x00
58 #define CBWFLAGS_IN 0x80
60 u_int8_t lun;
61 u_int8_t length;
62 #define CBWCDBLENGTH 16
64 u_int8_t cdb[CBWCDBLENGTH];
67 /* Command Status Wrapper */
68 struct bbb_csw {
69 u_int8_t signature[4];
70 #define CSWSIGNATURE 0x53425355
72 u_int8_t tag[4];
73 u_int8_t residue[4];
74 u_int8_t status;
75 #define CSWSTATUS_GOOD 0x0
76 #define CSWSTATUS_FAILED 0x1
77 #define CSWSTATUS_PHASE 0x2
80 /* USB-ATA 'controller' softc */
81 struct atausb_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 */
86 u_int8_t bulkout;
87 u_int8_t bulkirq;
88 usbd_pipe_handle bulkin_pipe; /* pipe handle's */
89 usbd_pipe_handle bulkout_pipe;
90 usbd_pipe_handle bulkirq_pipe;
91 int maxlun;
92 int timeout;
93 struct ata_request *ata_request;
94 usb_device_request_t usb_request;
95 struct bbb_cbw cbw;
96 struct bbb_csw csw;
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];
110 int state;
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;
131 /* prototypes*/
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);
143 * USB frontend part
145 USB_DECLARE_DRIVER(atausb);
146 DRIVER_MODULE(atausb, uhub, atausb_driver, atausb_devclass, 0, 0);
147 MODULE_VERSION(atausb, 1);
149 static int
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)
156 return UMATCH_NONE;
158 id = usbd_get_interface_descriptor(uaa->iface);
159 if (!id || id->bInterfaceClass != UICLASS_MASS)
160 return UMATCH_NONE;
162 switch (id->bInterfaceSubClass) {
163 case UISUBCLASS_QIC157:
164 case UISUBCLASS_RBC:
165 case UISUBCLASS_SCSI:
166 case UISUBCLASS_SFF8020I:
167 case UISUBCLASS_SFF8070I:
168 case UISUBCLASS_UFI:
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;
175 default:
176 return UMATCH_IFACECLASS_IFACESUBCLASS;
178 break;
179 default:
180 return UMATCH_IFACECLASS;
184 static int
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;
194 u_int8_t maxlun;
195 int err, i;
197 sc->dev = dev;
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;
204 sc->maxlun = 0;
205 sc->timeout = 5000;
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:
214 proto = "Bulk-Only";
215 break;
216 case UIPROTO_MASS_CBI:
217 proto = "CBI";
218 break;
219 case UIPROTO_MASS_CBI_I:
220 proto = "CBI with CCI";
221 break;
222 default:
223 proto = "Unknown";
225 switch (id->bInterfaceSubClass) {
226 case UISUBCLASS_RBC:
227 subclass = "RBC";
228 break;
229 case UISUBCLASS_QIC157:
230 case UISUBCLASS_SFF8020I:
231 case UISUBCLASS_SFF8070I:
232 subclass = "ATAPI";
233 break;
234 case UISUBCLASS_SCSI:
235 subclass = "SCSI";
236 break;
237 case UISUBCLASS_UFI:
238 subclass = "UFI";
239 break;
240 default:
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")))
246 return ENXIO;
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");
251 return ENXIO;
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);
272 atausb_detach(dev);
273 return ENXIO;
276 /* open the pipes */
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);
280 atausb_detach(dev);
281 return ENXIO;
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);
286 atausb_detach(dev);
287 return ENXIO;
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",
293 sc->bulkirq);
294 atausb_detach(dev);
295 return ENXIO;
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");
305 atausb_detach(dev);
306 return ENXIO;
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:
322 if (bootverbose)
323 device_printf(sc->dev, "maxlun=%d\n", maxlun);
324 sc->maxlun = maxlun;
325 break;
326 default:
327 if (bootverbose)
328 device_printf(sc->dev, "get maxlun not supported %s\n",
329 usbd_errstr(err));
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 */
335 int freeunit = 2;
336 while (freeunit < devclass_get_maxunit(ata_devclass) &&
337 devclass_get_device(ata_devclass, freeunit) != NULL)
338 freeunit++;
339 if (!device_add_child(sc->dev, "ata", freeunit)) {
340 device_printf(sc->dev, "failed to attach ata child device\n");
341 atausb_detach(dev);
342 return ENXIO;
345 bus_generic_attach(sc->dev);
346 return 0;
349 static int
350 atausb_detach(device_t dev)
352 struct atausb_softc *sc = device_get_softc(dev);
353 usbd_device_handle udev;
354 device_t *children;
355 int nchildren, i;
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);
365 if (sc->bulkin_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++)
379 if (sc->transfer[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);
385 if (sc->bulkin_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);
391 return 0;
396 * Generic USB transfer routines
398 static usbd_status
399 atausb_start(struct atausb_softc *sc, usbd_pipe_handle pipe,
400 void *buffer, int buflen, int flags, usbd_xfer_handle xfer)
402 usbd_status err;
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)) {
411 if (atausbdebug)
412 device_printf(sc->dev, "failed to setup transfer, %s\n",
413 usbd_errstr(err));
414 return err;
416 return USBD_NORMAL_COMPLETION;
419 static usbd_status
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)
424 usbd_status err;
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)) {
433 if (atausbdebug)
434 device_printf(sc->dev, "failed to setup ctl transfer, %s\n",
435 usbd_errstr(err));
436 return err;
438 return USBD_NORMAL_COMPLETION;
441 static void
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;
447 if (atausbdebug)
448 device_printf(sc->dev, "clear endpoint 0x%02x stall\n", endpt);
449 usbd_interface2device_handle(sc->iface, &udev);
450 sc->state = state;
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
464 static void
465 atausb_bbb_reset(struct atausb_softc *sc)
467 usbd_device_handle udev;
469 if (atausbdebug)
470 device_printf(sc->dev, "Bulk Reset\n");
471 sc->timeout = 5000;
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]);
483 static int
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;
496 sc->cbw.length = 16;
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;
503 if (atausbdebug)
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;
511 static void
512 atausb_bbb_finish(usbd_xfer_handle xfer, usbd_private_handle priv,
513 usbd_status err)
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");
524 return;
527 switch (sc->state) {
528 case ATAUSB_S_BBB_COMMAND: /* command transport phase */
529 if (err) {
530 if (atausbdebug)
531 device_printf(sc->dev, "failed to send CBW\n");
532 request->result = EIO;
533 atausb_bbb_reset(sc);
534 return;
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,
542 USBD_SHORT_XFER_OK,
543 sc->transfer[ATAUSB_T_BBB_DATA])) {
544 request->result = EIO;
545 atausb_bbb_reset(sc);
547 return;
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);
556 return;
558 /* FALLTHROUGH */
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);
563 if (err) {
564 if (atausbdebug)
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),
574 ATAUSB_S_BBB_DCLEAR,
575 sc->transfer[ATAUSB_T_BBB_DCLEAR]);
577 else {
578 request->result = EIO;
579 atausb_bbb_reset(sc);
581 return;
584 /* FALLTHROUGH */
586 case ATAUSB_S_BBB_DCLEAR: /* stall clear after data phase */
587 case ATAUSB_S_BBB_SCLEAR: /* stall clear after status phase */
588 if (err) {
589 if (atausbdebug)
590 device_printf(sc->dev, "bulk%s stall clear failed %s\n",
591 (request->flags & ATA_R_READ ? "in" : "out"),
592 usbd_errstr(err));
593 request->result = EIO;
594 atausb_bbb_reset(sc);
595 return;
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];
605 else {
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);
615 return;
617 case ATAUSB_S_BBB_STATUS1: /* status transfer first attempt */
618 case ATAUSB_S_BBB_STATUS2: /* status transfer second attempt */
619 if (err) {
620 if (atausbdebug)
621 device_printf(sc->dev, "cannot get CSW, %s%s\n",
622 usbd_errstr(err),
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,
626 ATAUSB_S_BBB_SCLEAR,
627 sc->transfer[ATAUSB_T_BBB_SCLEAR]);
629 else {
630 request->result = EIO;
631 atausb_bbb_reset(sc);
633 return;
636 int residue = UGETDW(sc->csw.residue);
638 if (!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) {
644 if (atausbdebug)
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);
649 return;
651 else if (UGETDW(sc->csw.tag) != UGETDW(sc->cbw.tag)) {
652 if (atausbdebug)
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);
657 return;
659 else if (sc->csw.status > CSWSTATUS_PHASE) {
660 if (atausbdebug)
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);
665 return;
667 else if (sc->csw.status == CSWSTATUS_PHASE) {
668 if (atausbdebug)
669 device_printf(sc->dev, "phase error residue = %d\n", residue);
670 request->result = EIO;
671 atausb_bbb_reset(sc);
672 return;
674 else if (request->donecount > request->bytecount) {
675 if (atausbdebug)
676 device_printf(sc->dev, "buffer overrun %d > %d",
677 request->donecount, request->bytecount);
678 request->result = EIO;
679 atausb_bbb_reset(sc);
680 return;
682 else if (sc->csw.status == CSWSTATUS_FAILED) {
683 if (atausbdebug)
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));
688 return;
690 else {
691 sc->state = ATAUSB_S_IDLE;
692 ata_interrupt(device_get_softc(request->parent));
693 return;
695 /* NOT REACHED */
697 case ATAUSB_S_BBB_RESET1:
698 if (err)
699 if (atausbdebug)
700 device_printf(sc->dev,
701 "BBB reset failure: %s\n", usbd_errstr(err));
702 atausb_clear_stall(sc, sc->bulkin, sc->bulkin_pipe,
703 ATAUSB_S_BBB_RESET2,
704 sc->transfer[ATAUSB_T_BBB_RESET2]);
705 return;
707 case ATAUSB_S_BBB_RESET2:
708 if (err)
709 if (atausbdebug)
710 device_printf(sc->dev, "BBB bulkin clear stall failure: %s\n",
711 usbd_errstr(err));
712 atausb_clear_stall(sc, sc->bulkout, sc->bulkout_pipe,
713 ATAUSB_S_BBB_RESET3,
714 sc->transfer[ATAUSB_T_BBB_RESET3]);
715 return;
717 case ATAUSB_S_BBB_RESET3:
718 if (err)
719 if (atausbdebug)
720 device_printf(sc->dev, "BBB bulk-out clear stall failure: %s\n",
721 usbd_errstr(err));
722 sc->state = ATAUSB_S_IDLE;
723 if (request) {
724 if (err)
725 request->result = ENXIO;
726 else
727 request->result = EIO;
728 ata_interrupt(device_get_softc(request->parent));
730 return;
732 default:
733 if (atausbdebug)
734 device_printf(sc->dev, "unknown state %d", sc->state);
740 * ATA backend part
742 struct atapi_inquiry {
743 u_int8_t device_type;
744 u_int8_t device_modifier;
745 u_int8_t version;
746 u_int8_t response_format;
747 u_int8_t length;
748 u_int8_t reserved[2];
749 u_int8_t flags;
750 u_int8_t vendor[8];
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));
762 if (atausbdebug > 1)
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)
796 if (atausbdebug > 1)
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;
807 u_int16_t *ptr;
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)
818 *ptr = ntohs(*ptr);
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)
822 *ptr = ntohs(*ptr);
823 request->result = 0;
825 return ATA_OP_FINISHED;
828 static int
829 ata_usbchannel_probe(device_t dev)
831 struct ata_channel *ch = device_get_softc(dev);
832 device_t *children;
833 int count, i;
834 char buffer[32];
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)
843 ch->unit = i;
845 kfree(children, M_TEMP);
847 ksprintf(buffer, "USB lun %d", ch->unit);
848 device_set_desc_copy(dev, buffer);
850 return 0;
853 static int
854 ata_usbchannel_attach(device_t dev)
856 struct ata_channel *ch = device_get_softc(dev);
858 /* initialize the softc basics */
859 ch->dev = dev;
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;
874 ata_identify(dev);
875 return 0;
878 static int
879 ata_usbchannel_detach(device_t dev)
881 struct ata_channel *ch = device_get_softc(dev);
882 device_t *children;
883 int nchildren, i;
885 /* detach & delete all children */
886 if (!device_get_children(dev, &children, &nchildren)) {
887 for (i = 0; i < nchildren; i++)
888 if (children[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);
894 return 0;
897 static void
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;
907 else
908 atadev->mode = ATA_USB1;
911 static int
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);
916 int res = -1;
919 spin_lock_wr(&sc->locked_mtx);
920 switch (flags) {
921 case ATA_LF_LOCK:
922 if (sc->locked_ch == NULL)
923 sc->locked_ch = ch;
924 if (sc->locked_ch != ch)
925 sc->restart_ch = ch;
926 break;
928 case ATA_LF_UNLOCK:
929 if (sc->locked_ch == ch) {
930 sc->locked_ch = NULL;
931 if (sc->restart_ch) {
932 ch = sc->restart_ch;
933 sc->restart_ch = NULL;
934 spin_unlock_wr(&sc->locked_mtx);
935 ata_start(ch->dev);
936 return res;
939 break;
941 case ATA_LF_WHICH:
942 break;
944 if (sc->locked_ch)
945 res = sc->locked_ch->unit;
946 spin_unlock_wr(&sc->locked_mtx);
947 return res;
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),
956 /* ATA methods */
957 DEVMETHOD(ata_setmode, ata_usbchannel_setmode),
958 DEVMETHOD(ata_locking, ata_usbchannel_locking),
959 /* DEVMETHOD(ata_reset, ata_usbchannel_reset), */
961 { 0, 0 }
964 static driver_t ata_usbchannel_driver = {
965 "ata",
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);