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