xref: /freebsd/sys/dev/usb/usb_msctest.c (revision 955c8cbb4960e6cf3602de144b1b9154a5092968)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008,2011 Hans Petter Selasky. All rights reserved.
4  *
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  * 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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * The following file contains code that will detect USB autoinstall
29  * disks.
30  *
31  * TODO: Potentially we could add code to automatically detect USB
32  * mass storage quirks for not supported SCSI commands!
33  */
34 
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>
42 #include <sys/bus.h>
43 #include <sys/module.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/sx.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
52 #include <sys/priv.h>
53 
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 
58 #define	USB_DEBUG_VAR usb_debug
59 
60 #include <dev/usb/usb_busdma.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_transfer.h>
63 #include <dev/usb/usb_msctest.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_device.h>
66 #include <dev/usb/usb_request.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/quirk/usb_quirk.h>
69 
70 enum {
71 	ST_COMMAND,
72 	ST_DATA_RD,
73 	ST_DATA_RD_CS,
74 	ST_DATA_WR,
75 	ST_DATA_WR_CS,
76 	ST_STATUS,
77 	ST_MAX,
78 };
79 
80 enum {
81 	DIR_IN,
82 	DIR_OUT,
83 	DIR_NONE,
84 };
85 
86 #define	SCSI_MAX_LEN	MAX(0x100, BULK_SIZE)
87 #define	SCSI_INQ_LEN	0x24
88 #define	SCSI_SENSE_LEN	0xFF
89 
90 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
91 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 };
92 static uint8_t scsi_rezero_init[] =     { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
93 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 };
94 static uint8_t scsi_ztestor_eject[] =   { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01,
95 					  0x01, 0x01, 0x01, 0x01, 0x00, 0x00 };
96 static uint8_t scsi_cmotech_eject[] =   { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43,
97 					  0x48, 0x47 };
98 static uint8_t scsi_huawei_eject[] =	{ 0x11, 0x06, 0x00, 0x00, 0x00, 0x00,
99 					  0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 					  0x00, 0x00, 0x00, 0x00 };
101 static uint8_t scsi_tct_eject[] =	{ 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 };
102 static uint8_t scsi_sync_cache[] =	{ 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
103 					  0x00, 0x00, 0x00, 0x00 };
104 static uint8_t scsi_request_sense[] =	{ 0x03, 0x00, 0x00, 0x00, 0x12, 0x00,
105 					  0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
106 static uint8_t scsi_read_capacity[] =	{ 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
107 					  0x00, 0x00, 0x00, 0x00 };
108 
109 #define	BULK_SIZE		64	/* dummy */
110 #define	ERR_CSW_FAILED		-1
111 
112 /* Command Block Wrapper */
113 struct bbb_cbw {
114 	uDWord	dCBWSignature;
115 #define	CBWSIGNATURE	0x43425355
116 	uDWord	dCBWTag;
117 	uDWord	dCBWDataTransferLength;
118 	uByte	bCBWFlags;
119 #define	CBWFLAGS_OUT	0x00
120 #define	CBWFLAGS_IN	0x80
121 	uByte	bCBWLUN;
122 	uByte	bCDBLength;
123 #define	CBWCDBLENGTH	16
124 	uByte	CBWCDB[CBWCDBLENGTH];
125 } __packed;
126 
127 /* Command Status Wrapper */
128 struct bbb_csw {
129 	uDWord	dCSWSignature;
130 #define	CSWSIGNATURE	0x53425355
131 	uDWord	dCSWTag;
132 	uDWord	dCSWDataResidue;
133 	uByte	bCSWStatus;
134 #define	CSWSTATUS_GOOD	0x0
135 #define	CSWSTATUS_FAILED	0x1
136 #define	CSWSTATUS_PHASE	0x2
137 } __packed;
138 
139 struct bbb_transfer {
140 	struct mtx mtx;
141 	struct cv cv;
142 	struct bbb_cbw *cbw;
143 	struct bbb_csw *csw;
144 
145 	struct usb_xfer *xfer[ST_MAX];
146 
147 	uint8_t *data_ptr;
148 
149 	usb_size_t data_len;		/* bytes */
150 	usb_size_t data_rem;		/* bytes */
151 	usb_timeout_t data_timeout;	/* ms */
152 	usb_frlength_t actlen;		/* bytes */
153 	usb_frlength_t buffer_size;    	/* bytes */
154 
155 	uint8_t	cmd_len;		/* bytes */
156 	uint8_t	dir;
157 	uint8_t	lun;
158 	uint8_t	state;
159 	uint8_t	status_try;
160 	int	error;
161 
162 	uint8_t	*buffer;
163 };
164 
165 static usb_callback_t bbb_command_callback;
166 static usb_callback_t bbb_data_read_callback;
167 static usb_callback_t bbb_data_rd_cs_callback;
168 static usb_callback_t bbb_data_write_callback;
169 static usb_callback_t bbb_data_wr_cs_callback;
170 static usb_callback_t bbb_status_callback;
171 
172 static void	bbb_done(struct bbb_transfer *, int);
173 static void	bbb_transfer_start(struct bbb_transfer *, uint8_t);
174 static void	bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t,
175 		    uint8_t);
176 static int	bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t,
177 		    void *, size_t, void *, size_t, usb_timeout_t);
178 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t);
179 static void	bbb_detach(struct bbb_transfer *);
180 
181 static const struct usb_config bbb_config[ST_MAX] = {
182 
183 	[ST_COMMAND] = {
184 		.type = UE_BULK,
185 		.endpoint = UE_ADDR_ANY,
186 		.direction = UE_DIR_OUT,
187 		.bufsize = sizeof(struct bbb_cbw),
188 		.callback = &bbb_command_callback,
189 		.timeout = 4 * USB_MS_HZ,	/* 4 seconds */
190 	},
191 
192 	[ST_DATA_RD] = {
193 		.type = UE_BULK,
194 		.endpoint = UE_ADDR_ANY,
195 		.direction = UE_DIR_IN,
196 		.bufsize = SCSI_MAX_LEN,
197 		.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
198 		.callback = &bbb_data_read_callback,
199 		.timeout = 4 * USB_MS_HZ,	/* 4 seconds */
200 	},
201 
202 	[ST_DATA_RD_CS] = {
203 		.type = UE_CONTROL,
204 		.endpoint = 0x00,	/* Control pipe */
205 		.direction = UE_DIR_ANY,
206 		.bufsize = sizeof(struct usb_device_request),
207 		.callback = &bbb_data_rd_cs_callback,
208 		.timeout = 1 * USB_MS_HZ,	/* 1 second  */
209 	},
210 
211 	[ST_DATA_WR] = {
212 		.type = UE_BULK,
213 		.endpoint = UE_ADDR_ANY,
214 		.direction = UE_DIR_OUT,
215 		.bufsize = SCSI_MAX_LEN,
216 		.flags = {.ext_buffer = 1,.proxy_buffer = 1,},
217 		.callback = &bbb_data_write_callback,
218 		.timeout = 4 * USB_MS_HZ,	/* 4 seconds */
219 	},
220 
221 	[ST_DATA_WR_CS] = {
222 		.type = UE_CONTROL,
223 		.endpoint = 0x00,	/* Control pipe */
224 		.direction = UE_DIR_ANY,
225 		.bufsize = sizeof(struct usb_device_request),
226 		.callback = &bbb_data_wr_cs_callback,
227 		.timeout = 1 * USB_MS_HZ,	/* 1 second  */
228 	},
229 
230 	[ST_STATUS] = {
231 		.type = UE_BULK,
232 		.endpoint = UE_ADDR_ANY,
233 		.direction = UE_DIR_IN,
234 		.bufsize = sizeof(struct bbb_csw),
235 		.flags = {.short_xfer_ok = 1,},
236 		.callback = &bbb_status_callback,
237 		.timeout = 1 * USB_MS_HZ,	/* 1 second  */
238 	},
239 };
240 
241 static void
242 bbb_done(struct bbb_transfer *sc, int error)
243 {
244 	sc->error = error;
245 	sc->state = ST_COMMAND;
246 	sc->status_try = 1;
247 	cv_signal(&sc->cv);
248 }
249 
250 static void
251 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index)
252 {
253 	sc->state = xfer_index;
254 	usbd_transfer_start(sc->xfer[xfer_index]);
255 }
256 
257 static void
258 bbb_data_clear_stall_callback(struct usb_xfer *xfer,
259     uint8_t next_xfer, uint8_t stall_xfer)
260 {
261 	struct bbb_transfer *sc = usbd_xfer_softc(xfer);
262 
263 	if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
264 		switch (USB_GET_STATE(xfer)) {
265 		case USB_ST_SETUP:
266 		case USB_ST_TRANSFERRED:
267 			bbb_transfer_start(sc, next_xfer);
268 			break;
269 		default:
270 			bbb_done(sc, USB_ERR_STALLED);
271 			break;
272 		}
273 	}
274 }
275 
276 static void
277 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
278 {
279 	struct bbb_transfer *sc = usbd_xfer_softc(xfer);
280 	uint32_t tag;
281 
282 	switch (USB_GET_STATE(xfer)) {
283 	case USB_ST_TRANSFERRED:
284 		bbb_transfer_start
285 		    (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD :
286 		    (sc->dir == DIR_OUT) ? ST_DATA_WR :
287 		    ST_STATUS));
288 		break;
289 
290 	case USB_ST_SETUP:
291 		sc->status_try = 0;
292 		tag = UGETDW(sc->cbw->dCBWTag) + 1;
293 		USETDW(sc->cbw->dCBWSignature, CBWSIGNATURE);
294 		USETDW(sc->cbw->dCBWTag, tag);
295 		USETDW(sc->cbw->dCBWDataTransferLength, (uint32_t)sc->data_len);
296 		sc->cbw->bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT);
297 		sc->cbw->bCBWLUN = sc->lun;
298 		sc->cbw->bCDBLength = sc->cmd_len;
299 		if (sc->cbw->bCDBLength > sizeof(sc->cbw->CBWCDB)) {
300 			sc->cbw->bCDBLength = sizeof(sc->cbw->CBWCDB);
301 			DPRINTFN(0, "Truncating long command\n");
302 		}
303 		usbd_xfer_set_frame_len(xfer, 0,
304 		    sizeof(struct bbb_cbw));
305 		usbd_transfer_submit(xfer);
306 		break;
307 
308 	default:			/* Error */
309 		bbb_done(sc, error);
310 		break;
311 	}
312 }
313 
314 static void
315 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
316 {
317 	struct bbb_transfer *sc = usbd_xfer_softc(xfer);
318 	usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
319 	int actlen, sumlen;
320 
321 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
322 
323 	switch (USB_GET_STATE(xfer)) {
324 	case USB_ST_TRANSFERRED:
325 		sc->data_rem -= actlen;
326 		sc->data_ptr += actlen;
327 		sc->actlen += actlen;
328 
329 		if (actlen < sumlen) {
330 			/* short transfer */
331 			sc->data_rem = 0;
332 		}
333 	case USB_ST_SETUP:
334 		DPRINTF("max_bulk=%d, data_rem=%d\n",
335 		    max_bulk, sc->data_rem);
336 
337 		if (sc->data_rem == 0) {
338 			bbb_transfer_start(sc, ST_STATUS);
339 			break;
340 		}
341 		if (max_bulk > sc->data_rem) {
342 			max_bulk = sc->data_rem;
343 		}
344 		usbd_xfer_set_timeout(xfer, sc->data_timeout);
345 		usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
346 		usbd_transfer_submit(xfer);
347 		break;
348 
349 	default:			/* Error */
350 		if (error == USB_ERR_CANCELLED) {
351 			bbb_done(sc, error);
352 		} else {
353 			bbb_transfer_start(sc, ST_DATA_RD_CS);
354 		}
355 		break;
356 	}
357 }
358 
359 static void
360 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
361 {
362 	bbb_data_clear_stall_callback(xfer, ST_STATUS,
363 	    ST_DATA_RD);
364 }
365 
366 static void
367 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
368 {
369 	struct bbb_transfer *sc = usbd_xfer_softc(xfer);
370 	usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
371 	int actlen, sumlen;
372 
373 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
374 
375 	switch (USB_GET_STATE(xfer)) {
376 	case USB_ST_TRANSFERRED:
377 		sc->data_rem -= actlen;
378 		sc->data_ptr += actlen;
379 		sc->actlen += actlen;
380 
381 		if (actlen < sumlen) {
382 			/* short transfer */
383 			sc->data_rem = 0;
384 		}
385 	case USB_ST_SETUP:
386 		DPRINTF("max_bulk=%d, data_rem=%d\n",
387 		    max_bulk, sc->data_rem);
388 
389 		if (sc->data_rem == 0) {
390 			bbb_transfer_start(sc, ST_STATUS);
391 			break;
392 		}
393 		if (max_bulk > sc->data_rem) {
394 			max_bulk = sc->data_rem;
395 		}
396 		usbd_xfer_set_timeout(xfer, sc->data_timeout);
397 		usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk);
398 		usbd_transfer_submit(xfer);
399 		break;
400 
401 	default:			/* Error */
402 		if (error == USB_ERR_CANCELLED) {
403 			bbb_done(sc, error);
404 		} else {
405 			bbb_transfer_start(sc, ST_DATA_WR_CS);
406 		}
407 		break;
408 	}
409 }
410 
411 static void
412 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
413 {
414 	bbb_data_clear_stall_callback(xfer, ST_STATUS,
415 	    ST_DATA_WR);
416 }
417 
418 static void
419 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
420 {
421 	struct bbb_transfer *sc = usbd_xfer_softc(xfer);
422 	int actlen;
423 	int sumlen;
424 
425 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
426 
427 	switch (USB_GET_STATE(xfer)) {
428 	case USB_ST_TRANSFERRED:
429 
430 		/* very simple status check */
431 
432 		if (actlen < (int)sizeof(struct bbb_csw)) {
433 			bbb_done(sc, USB_ERR_SHORT_XFER);
434 		} else if (sc->csw->bCSWStatus == CSWSTATUS_GOOD) {
435 			bbb_done(sc, 0);	/* success */
436 		} else {
437 			bbb_done(sc, ERR_CSW_FAILED);	/* error */
438 		}
439 		break;
440 
441 	case USB_ST_SETUP:
442 		usbd_xfer_set_frame_len(xfer, 0,
443 		    sizeof(struct bbb_csw));
444 		usbd_transfer_submit(xfer);
445 		break;
446 
447 	default:
448 		DPRINTF("Failed to read CSW: %s, try %d\n",
449 		    usbd_errstr(error), sc->status_try);
450 
451 		if (error == USB_ERR_CANCELLED || sc->status_try) {
452 			bbb_done(sc, error);
453 		} else {
454 			sc->status_try = 1;
455 			bbb_transfer_start(sc, ST_DATA_RD_CS);
456 		}
457 		break;
458 	}
459 }
460 
461 /*------------------------------------------------------------------------*
462  *	bbb_command_start - execute a SCSI command synchronously
463  *
464  * Return values
465  * 0: Success
466  * Else: Failure
467  *------------------------------------------------------------------------*/
468 static int
469 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun,
470     void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len,
471     usb_timeout_t data_timeout)
472 {
473 	sc->lun = lun;
474 	sc->dir = data_len ? dir : DIR_NONE;
475 	sc->data_ptr = data_ptr;
476 	sc->data_len = data_len;
477 	sc->data_rem = data_len;
478 	sc->data_timeout = (data_timeout + USB_MS_HZ);
479 	sc->actlen = 0;
480 	sc->cmd_len = cmd_len;
481 	memset(&sc->cbw->CBWCDB, 0, sizeof(sc->cbw->CBWCDB));
482 	memcpy(&sc->cbw->CBWCDB, cmd_ptr, cmd_len);
483 	DPRINTFN(1, "SCSI cmd = %*D\n", (int)cmd_len, (char *)sc->cbw->CBWCDB, ":");
484 
485 	mtx_lock(&sc->mtx);
486 	usbd_transfer_start(sc->xfer[sc->state]);
487 
488 	while (usbd_transfer_pending(sc->xfer[sc->state])) {
489 		cv_wait(&sc->cv, &sc->mtx);
490 	}
491 	mtx_unlock(&sc->mtx);
492 	return (sc->error);
493 }
494 
495 static struct bbb_transfer *
496 bbb_attach(struct usb_device *udev, uint8_t iface_index)
497 {
498 	struct usb_interface *iface;
499 	struct usb_interface_descriptor *id;
500 	struct bbb_transfer *sc;
501 	usb_error_t err;
502 	uint8_t do_unlock;
503 
504 	/* automatic locking */
505 	if (usbd_enum_is_locked(udev)) {
506 		do_unlock = 0;
507 	} else {
508 		do_unlock = 1;
509 		usbd_enum_lock(udev);
510 	}
511 
512 	/*
513 	 * Make sure any driver which is hooked up to this interface,
514 	 * like umass is gone:
515 	 */
516 	usb_detach_device(udev, iface_index, 0);
517 
518 	if (do_unlock)
519 		usbd_enum_unlock(udev);
520 
521 	iface = usbd_get_iface(udev, iface_index);
522 	if (iface == NULL)
523 		return (NULL);
524 
525 	id = iface->idesc;
526 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
527 		return (NULL);
528 
529 	switch (id->bInterfaceSubClass) {
530 	case UISUBCLASS_SCSI:
531 	case UISUBCLASS_UFI:
532 	case UISUBCLASS_SFF8020I:
533 	case UISUBCLASS_SFF8070I:
534 		break;
535 	default:
536 		return (NULL);
537 	}
538 
539 	switch (id->bInterfaceProtocol) {
540 	case UIPROTO_MASS_BBB_OLD:
541 	case UIPROTO_MASS_BBB:
542 		break;
543 	default:
544 		return (NULL);
545 	}
546 
547 	sc = malloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO);
548 	mtx_init(&sc->mtx, "USB autoinstall", NULL, MTX_DEF);
549 	cv_init(&sc->cv, "WBBB");
550 
551 	err = usbd_transfer_setup(udev, &iface_index, sc->xfer, bbb_config,
552 	    ST_MAX, sc, &sc->mtx);
553 	if (err) {
554 		bbb_detach(sc);
555 		return (NULL);
556 	}
557 	/* store pointer to DMA buffers */
558 	sc->buffer = usbd_xfer_get_frame_buffer(
559 	    sc->xfer[ST_DATA_RD], 0);
560 	sc->buffer_size =
561 	    usbd_xfer_max_len(sc->xfer[ST_DATA_RD]);
562 	sc->cbw = usbd_xfer_get_frame_buffer(
563 	    sc->xfer[ST_COMMAND], 0);
564 	sc->csw = usbd_xfer_get_frame_buffer(
565 	    sc->xfer[ST_STATUS], 0);
566 
567 	return (sc);
568 }
569 
570 static void
571 bbb_detach(struct bbb_transfer *sc)
572 {
573 	usbd_transfer_unsetup(sc->xfer, ST_MAX);
574 	mtx_destroy(&sc->mtx);
575 	cv_destroy(&sc->cv);
576 	free(sc, M_USB);
577 }
578 
579 /*------------------------------------------------------------------------*
580  *	usb_iface_is_cdrom
581  *
582  * Return values:
583  * 1: This interface is an auto install disk (CD-ROM)
584  * 0: Not an auto install disk.
585  *------------------------------------------------------------------------*/
586 int
587 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index)
588 {
589 	struct bbb_transfer *sc;
590 	uint8_t timeout;
591 	uint8_t is_cdrom;
592 	uint8_t sid_type;
593 	int err;
594 
595 	sc = bbb_attach(udev, iface_index);
596 	if (sc == NULL)
597 		return (0);
598 
599 	is_cdrom = 0;
600 	timeout = 4;	/* tries */
601 	while (--timeout) {
602 		err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
603 		    SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
604 		    USB_MS_HZ);
605 
606 		if (err == 0 && sc->actlen > 0) {
607 			sid_type = sc->buffer[0] & 0x1F;
608 			if (sid_type == 0x05)
609 				is_cdrom = 1;
610 			break;
611 		} else if (err != ERR_CSW_FAILED)
612 			break;	/* non retryable error */
613 		usb_pause_mtx(NULL, hz);
614 	}
615 	bbb_detach(sc);
616 	return (is_cdrom);
617 }
618 
619 static uint8_t
620 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index)
621 {
622 	struct usb_device_request req;
623 	usb_error_t err;
624 	uint8_t buf = 0;
625 
626 
627 	/* The Get Max Lun command is a class-specific request. */
628 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
629 	req.bRequest = 0xFE;		/* GET_MAX_LUN */
630 	USETW(req.wValue, 0);
631 	req.wIndex[0] = iface_index;
632 	req.wIndex[1] = 0;
633 	USETW(req.wLength, 1);
634 
635 	err = usbd_do_request(udev, NULL, &req, &buf);
636 	if (err)
637 		buf = 0;
638 
639 	return (buf);
640 }
641 
642 usb_error_t
643 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index)
644 {
645 	struct bbb_transfer *sc;
646 	uint8_t timeout;
647 	uint8_t is_no_direct;
648 	uint8_t sid_type;
649 	int err;
650 
651 	sc = bbb_attach(udev, iface_index);
652 	if (sc == NULL)
653 		return (0);
654 
655 	/*
656 	 * Some devices need a delay after that the configuration
657 	 * value is set to function properly:
658 	 */
659 	usb_pause_mtx(NULL, hz);
660 
661 	if (usb_msc_get_max_lun(udev, iface_index) == 0) {
662 		DPRINTF("Device has only got one LUN.\n");
663 		usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN);
664 	}
665 
666 	is_no_direct = 1;
667 	for (timeout = 4; timeout != 0; timeout--) {
668 		err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
669 		    SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
670 		    USB_MS_HZ);
671 
672 		if (err == 0 && sc->actlen > 0) {
673 			sid_type = sc->buffer[0] & 0x1F;
674 			if (sid_type == 0x00)
675 				is_no_direct = 0;
676 			break;
677 		} else if (err != ERR_CSW_FAILED) {
678 			DPRINTF("Device is not responding "
679 			    "properly to SCSI INQUIRY command.\n");
680 			goto error;	/* non retryable error */
681 		}
682 		usb_pause_mtx(NULL, hz);
683 	}
684 
685 	if (is_no_direct) {
686 		DPRINTF("Device is not direct access.\n");
687 		goto done;
688 	}
689 
690 	err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
691 	    &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
692 	    USB_MS_HZ);
693 
694 	if (err != 0) {
695 
696 		if (err != ERR_CSW_FAILED)
697 			goto error;
698 	}
699 	timeout = 1;
700 
701 retry_sync_cache:
702 	err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
703 	    &scsi_sync_cache, sizeof(scsi_sync_cache),
704 	    USB_MS_HZ);
705 
706 	if (err != 0) {
707 
708 		if (err != ERR_CSW_FAILED)
709 			goto error;
710 
711 		DPRINTF("Device doesn't handle synchronize cache\n");
712 
713 		usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
714 
715 	} else {
716 
717 		/*
718 		 * Certain Kingston memory sticks fail the first
719 		 * read capacity after a synchronize cache command
720 		 * has been issued. Disable the synchronize cache
721 		 * command for such devices.
722 		 */
723 
724 		err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
725 		    &scsi_read_capacity, sizeof(scsi_read_capacity),
726 		    USB_MS_HZ);
727 
728 		if (err != 0) {
729 			if (err != ERR_CSW_FAILED)
730 				goto error;
731 
732 			err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8,
733 			    &scsi_read_capacity, sizeof(scsi_read_capacity),
734 			    USB_MS_HZ);
735 
736 			if (err == 0) {
737 				if (timeout--)
738 					goto retry_sync_cache;
739 
740 				DPRINTF("Device most likely doesn't "
741 				    "handle synchronize cache\n");
742 
743 				usbd_add_dynamic_quirk(udev,
744 				    UQ_MSC_NO_SYNC_CACHE);
745 			} else {
746 				if (err != ERR_CSW_FAILED)
747 					goto error;
748 			}
749 		}
750 	}
751 
752 	/* clear sense status of any failed commands on the device */
753 
754 	err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
755 	    SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry),
756 	    USB_MS_HZ);
757 
758 	DPRINTF("Inquiry = %d\n", err);
759 
760 	if (err != 0) {
761 
762 		if (err != ERR_CSW_FAILED)
763 			goto error;
764 	}
765 
766 	err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
767 	    SCSI_SENSE_LEN, &scsi_request_sense,
768 	    sizeof(scsi_request_sense), USB_MS_HZ);
769 
770 	DPRINTF("Request sense = %d\n", err);
771 
772 	if (err != 0) {
773 
774 		if (err != ERR_CSW_FAILED)
775 			goto error;
776 	}
777 
778 done:
779 	bbb_detach(sc);
780 	return (0);
781 
782 error:
783  	bbb_detach(sc);
784 
785 	DPRINTF("Device did not respond, enabling all quirks\n");
786 
787 	usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE);
788 	usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY);
789 
790 	/* Need to re-enumerate the device */
791 	usbd_req_re_enumerate(udev, NULL);
792 
793 	return (USB_ERR_STALLED);
794 }
795 
796 usb_error_t
797 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method)
798 {
799 	struct bbb_transfer *sc;
800 	usb_error_t err;
801 
802 	sc = bbb_attach(udev, iface_index);
803 	if (sc == NULL)
804 		return (USB_ERR_INVAL);
805 
806 	err = 0;
807 	switch (method) {
808 	case MSC_EJECT_STOPUNIT:
809 		err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
810 		    &scsi_test_unit_ready, sizeof(scsi_test_unit_ready),
811 		    USB_MS_HZ);
812 		DPRINTF("Test unit ready status: %s\n", usbd_errstr(err));
813 		err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
814 		    &scsi_start_stop_unit, sizeof(scsi_start_stop_unit),
815 		    USB_MS_HZ);
816 		break;
817 	case MSC_EJECT_REZERO:
818 		err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
819 		    &scsi_rezero_init, sizeof(scsi_rezero_init),
820 		    USB_MS_HZ);
821 		break;
822 	case MSC_EJECT_ZTESTOR:
823 		err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
824 		    &scsi_ztestor_eject, sizeof(scsi_ztestor_eject),
825 		    USB_MS_HZ);
826 		break;
827 	case MSC_EJECT_CMOTECH:
828 		err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
829 		    &scsi_cmotech_eject, sizeof(scsi_cmotech_eject),
830 		    USB_MS_HZ);
831 		break;
832 	case MSC_EJECT_HUAWEI:
833 		err = bbb_command_start(sc, DIR_IN, 0, NULL, 0,
834 		    &scsi_huawei_eject, sizeof(scsi_huawei_eject),
835 		    USB_MS_HZ);
836 		break;
837 	case MSC_EJECT_TCT:
838 		/*
839 		 * TCTMobile needs DIR_IN flag. To get it, we
840 		 * supply a dummy data with the command.
841 		 */
842 		err = bbb_command_start(sc, DIR_IN, 0, sc->buffer,
843 		    sc->buffer_size, &scsi_tct_eject,
844 		    sizeof(scsi_tct_eject), USB_MS_HZ);
845 		break;
846 	default:
847 		printf("usb_msc_eject: unknown eject method (%d)\n", method);
848 		break;
849 	}
850 	DPRINTF("Eject CD command status: %s\n", usbd_errstr(err));
851 
852 	bbb_detach(sc);
853 	return (0);
854 }
855