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