3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * The following file contains code that will detect USB autoinstall
31 * TODO: Potentially we could add code to automatically detect USB
32 * mass storage quirks for not supported SCSI commands!
35 #include <sys/stdint.h>
36 #include <sys/stddef.h>
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/linker_set.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR usb_debug
61 #include <dev/usb/usb_busdma.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_transfer.h>
64 #include <dev/usb/usb_msctest.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_device.h>
68 #include <dev/usb/usb_request.h>
69 #include <dev/usb/usb_util.h>
70 #include <dev/usb/quirk/usb_quirk.h>
88 #define SCSI_INQ_LEN 0x24
89 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
90 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 };
91 static uint8_t scsi_rezero_init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
92 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 };
93 static uint8_t scsi_ztestor_eject[] = { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01,
94 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 };
95 static uint8_t scsi_cmotech_eject[] = { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43,
97 static uint8_t scsi_huawei_eject[] = { 0x11, 0x06, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00 };
100 static uint8_t scsi_tct_eject[] = { 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 };
102 #define BULK_SIZE 64 /* dummy */
103 #define ERR_CSW_FAILED -1
105 /* Command Block Wrapper */
107 uDWord dCBWSignature;
108 #define CBWSIGNATURE 0x43425355
110 uDWord dCBWDataTransferLength;
112 #define CBWFLAGS_OUT 0x00
113 #define CBWFLAGS_IN 0x80
116 #define CBWCDBLENGTH 16
117 uByte CBWCDB[CBWCDBLENGTH];
120 /* Command Status Wrapper */
122 uDWord dCSWSignature;
123 #define CSWSIGNATURE 0x53425355
125 uDWord dCSWDataResidue;
127 #define CSWSTATUS_GOOD 0x0
128 #define CSWSTATUS_FAILED 0x1
129 #define CSWSTATUS_PHASE 0x2
132 struct bbb_transfer {
138 struct usb_xfer *xfer[ST_MAX];
142 usb_size_t data_len; /* bytes */
143 usb_size_t data_rem; /* bytes */
144 usb_timeout_t data_timeout; /* ms */
145 usb_frlength_t actlen; /* bytes */
147 uint8_t cmd_len; /* bytes */
157 static usb_callback_t bbb_command_callback;
158 static usb_callback_t bbb_data_read_callback;
159 static usb_callback_t bbb_data_rd_cs_callback;
160 static usb_callback_t bbb_data_write_callback;
161 static usb_callback_t bbb_data_wr_cs_callback;
162 static usb_callback_t bbb_status_callback;
164 static void bbb_done(struct bbb_transfer *, int);
165 static void bbb_transfer_start(struct bbb_transfer *, uint8_t);
166 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t,
168 static uint8_t bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t,
169 void *, size_t, void *, size_t, usb_timeout_t);
170 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t);
171 static void bbb_detach(struct bbb_transfer *);
173 static const struct usb_config bbb_config[ST_MAX] = {
177 .endpoint = UE_ADDR_ANY,
178 .direction = UE_DIR_OUT,
179 .bufsize = sizeof(struct bbb_cbw),
180 .callback = &bbb_command_callback,
181 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
186 .endpoint = UE_ADDR_ANY,
187 .direction = UE_DIR_IN,
188 .bufsize = BULK_SIZE,
189 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
190 .callback = &bbb_data_read_callback,
191 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
196 .endpoint = 0x00, /* Control pipe */
197 .direction = UE_DIR_ANY,
198 .bufsize = sizeof(struct usb_device_request),
199 .callback = &bbb_data_rd_cs_callback,
200 .timeout = 1 * USB_MS_HZ, /* 1 second */
205 .endpoint = UE_ADDR_ANY,
206 .direction = UE_DIR_OUT,
207 .bufsize = BULK_SIZE,
208 .flags = {.proxy_buffer = 1,},
209 .callback = &bbb_data_write_callback,
210 .timeout = 4 * USB_MS_HZ, /* 4 seconds */
215 .endpoint = 0x00, /* Control pipe */
216 .direction = UE_DIR_ANY,
217 .bufsize = sizeof(struct usb_device_request),
218 .callback = &bbb_data_wr_cs_callback,
219 .timeout = 1 * USB_MS_HZ, /* 1 second */
224 .endpoint = UE_ADDR_ANY,
225 .direction = UE_DIR_IN,
226 .bufsize = sizeof(struct bbb_csw),
227 .flags = {.short_xfer_ok = 1,},
228 .callback = &bbb_status_callback,
229 .timeout = 1 * USB_MS_HZ, /* 1 second */
234 bbb_done(struct bbb_transfer *sc, int error)
238 sc->state = ST_COMMAND;
244 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index)
246 sc->state = xfer_index;
247 usbd_transfer_start(sc->xfer[xfer_index]);
251 bbb_data_clear_stall_callback(struct usb_xfer *xfer,
252 uint8_t next_xfer, uint8_t stall_xfer)
254 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
256 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
257 switch (USB_GET_STATE(xfer)) {
259 case USB_ST_TRANSFERRED:
260 bbb_transfer_start(sc, next_xfer);
263 bbb_done(sc, USB_ERR_STALLED);
270 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
272 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
275 switch (USB_GET_STATE(xfer)) {
276 case USB_ST_TRANSFERRED:
278 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD :
279 (sc->dir == DIR_OUT) ? ST_DATA_WR :
285 tag = UGETDW(sc->cbw.dCBWTag) + 1;
286 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
287 USETDW(sc->cbw.dCBWTag, tag);
288 USETDW(sc->cbw.dCBWDataTransferLength, (uint32_t)sc->data_len);
289 sc->cbw.bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT);
290 sc->cbw.bCBWLUN = sc->lun;
291 sc->cbw.bCDBLength = sc->cmd_len;
292 if (sc->cbw.bCDBLength > sizeof(sc->cbw.CBWCDB)) {
293 sc->cbw.bCDBLength = sizeof(sc->cbw.CBWCDB);
294 DPRINTFN(0, "Truncating long command\n");
296 usbd_xfer_set_frame_data(xfer, 0, &sc->cbw, sizeof(sc->cbw));
297 usbd_transfer_submit(xfer);
307 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
309 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
310 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
313 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
315 switch (USB_GET_STATE(xfer)) {
316 case USB_ST_TRANSFERRED:
317 sc->data_rem -= actlen;
318 sc->data_ptr += actlen;
319 sc->actlen += actlen;
321 if (actlen < sumlen) {
326 DPRINTF("max_bulk=%d, data_rem=%d\n",
327 max_bulk, sc->data_rem);
329 if (sc->data_rem == 0) {
330 bbb_transfer_start(sc, ST_STATUS);
333 if (max_bulk > sc->data_rem) {
334 max_bulk = sc->data_rem;
336 usbd_xfer_set_timeout(xfer, sc->data_timeout);
337 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
338 usbd_transfer_submit(xfer);
342 if (error == USB_ERR_CANCELLED) {
345 bbb_transfer_start(sc, ST_DATA_RD_CS);
352 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
354 bbb_data_clear_stall_callback(xfer, ST_STATUS,
359 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
361 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
362 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
365 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
367 switch (USB_GET_STATE(xfer)) {
368 case USB_ST_TRANSFERRED:
369 sc->data_rem -= actlen;
370 sc->data_ptr += actlen;
371 sc->actlen += actlen;
373 if (actlen < sumlen) {
378 DPRINTF("max_bulk=%d, data_rem=%d\n",
379 max_bulk, sc->data_rem);
381 if (sc->data_rem == 0) {
382 bbb_transfer_start(sc, ST_STATUS);
385 if (max_bulk > sc->data_rem) {
386 max_bulk = sc->data_rem;
388 usbd_xfer_set_timeout(xfer, sc->data_timeout);
389 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
390 usbd_transfer_submit(xfer);
394 if (error == USB_ERR_CANCELLED) {
397 bbb_transfer_start(sc, ST_DATA_WR_CS);
405 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
407 bbb_data_clear_stall_callback(xfer, ST_STATUS,
412 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
414 struct bbb_transfer *sc = usbd_xfer_softc(xfer);
417 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
419 switch (USB_GET_STATE(xfer)) {
420 case USB_ST_TRANSFERRED:
422 /* very simple status check */
424 if (actlen < sizeof(sc->csw)) {
425 bbb_done(sc, USB_ERR_SHORT_XFER);
426 } else if (sc->csw.bCSWStatus == CSWSTATUS_GOOD) {
427 bbb_done(sc, 0); /* success */
429 bbb_done(sc, ERR_CSW_FAILED); /* error */
434 usbd_xfer_set_frame_data(xfer, 0, &sc->csw, sizeof(sc->csw));
435 usbd_transfer_submit(xfer);
439 DPRINTF("Failed to read CSW: %s, try %d\n",
440 usbd_errstr(error), sc->status_try);
442 if (error == USB_ERR_CANCELLED || sc->status_try) {
446 bbb_transfer_start(sc, ST_DATA_RD_CS);
452 /*------------------------------------------------------------------------*
453 * bbb_command_start - execute a SCSI command synchronously
458 *------------------------------------------------------------------------*/
460 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun,
461 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len,
462 usb_timeout_t data_timeout)
465 sc->dir = data_len ? dir : DIR_NONE;
466 sc->data_ptr = data_ptr;
467 sc->data_len = data_len;
468 sc->data_rem = data_len;
469 sc->data_timeout = (data_timeout + USB_MS_HZ);
471 sc->cmd_len = cmd_len;
472 bzero(&sc->cbw.CBWCDB, sizeof(sc->cbw.CBWCDB));
473 bcopy(cmd_ptr, &sc->cbw.CBWCDB, cmd_len);
474 DPRINTFN(1, "SCSI cmd = %*D\n", (int)cmd_len, &sc->cbw.CBWCDB, ":");
477 usbd_transfer_start(sc->xfer[sc->state]);
479 while (usbd_transfer_pending(sc->xfer[sc->state])) {
480 cv_wait(&sc->cv, &sc->mtx);
482 mtx_unlock(&sc->mtx);
486 static struct bbb_transfer *
487 bbb_attach(struct usb_device *udev, uint8_t iface_index)
489 struct usb_interface *iface;
490 struct usb_interface_descriptor *id;
491 struct bbb_transfer *sc;
494 iface = usbd_get_iface(udev, iface_index);
499 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
502 switch (id->bInterfaceSubClass) {
503 case UISUBCLASS_SCSI:
505 case UISUBCLASS_SFF8020I:
506 case UISUBCLASS_SFF8070I:
512 switch (id->bInterfaceProtocol) {
513 case UIPROTO_MASS_BBB_OLD:
514 case UIPROTO_MASS_BBB:
520 sc = malloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO);
521 mtx_init(&sc->mtx, "USB autoinstall", NULL, MTX_DEF);
522 cv_init(&sc->cv, "WBBB");
524 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, bbb_config,
525 ST_MAX, sc, &sc->mtx);
534 bbb_detach(struct bbb_transfer *sc)
536 usbd_transfer_unsetup(sc->xfer, ST_MAX);
537 mtx_destroy(&sc->mtx);
542 /*------------------------------------------------------------------------*
546 * 1: This interface is an auto install disk (CD-ROM)
547 * 0: Not an auto install disk.
548 *------------------------------------------------------------------------*/
550 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index)
552 struct bbb_transfer *sc;
554 uint8_t timeout, is_cdrom;
557 sc = bbb_attach(udev, iface_index);
562 timeout = 4; /* tries */
564 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
565 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
568 if (err == 0 && sc->actlen > 0) {
569 sid_type = sc->buffer[0] & 0x1F;
570 if (sid_type == 0x05)
573 } else if (err != ERR_CSW_FAILED)
574 break; /* non retryable error */
575 usb_pause_mtx(NULL, hz);
582 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method)
584 struct bbb_transfer *sc;
587 sc = bbb_attach(udev, iface_index);
589 return (USB_ERR_INVAL);
593 case MSC_EJECT_STOPUNIT:
594 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
595 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
597 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err));
598 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
599 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit),
602 case MSC_EJECT_REZERO:
603 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
604 &scsi_rezero_init, sizeof(scsi_rezero_init),
607 case MSC_EJECT_ZTESTOR:
608 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
609 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject),
612 case MSC_EJECT_CMOTECH:
613 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
614 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject),
617 case MSC_EJECT_HUAWEI:
618 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
619 &scsi_huawei_eject, sizeof(scsi_huawei_eject),
624 * TCTMobile needs DIR_IN flag. To get it, we
625 * supply a dummy data with the command.
627 err = bbb_command_start(sc, DIR_IN, 0, &sc->buffer,
628 sizeof(sc->buffer), &scsi_tct_eject,
629 sizeof(scsi_tct_eject), USB_MS_HZ);
632 printf("usb_msc_eject: unknown eject method (%d)\n", method);
635 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err));