2 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
3 * Nick Hibma <n_hibma@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
28 * $FreeBSD: src/sys/dev/usb/umass.c,v 1.96 2003/12/19 12:19:11 sanpei Exp $
29 * $DragonFly: src/sys/dev/usbmisc/umass/umass.c,v 1.40 2008/08/16 21:42:19 thomas Exp $
33 * Universal Serial Bus Mass Storage Class specs:
34 * http://www.usb.org/developers/data/devclass/usbmassover_11.pdf
35 * http://www.usb.org/developers/data/devclass/usbmassbulk_10.pdf
36 * http://www.usb.org/developers/data/devclass/usbmass-cbi10.pdf
37 * http://www.usb.org/developers/data/devclass/usbmass-ufi10.pdf
41 * Ported to NetBSD by Lennart Augustsson <augustss@netbsd.org>.
42 * Parts of the code written my Jason R. Thorpe <thorpej@shagadelic.org>.
46 * The driver handles 3 Wire Protocols
47 * - Command/Bulk/Interrupt (CBI)
48 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
49 * - Mass Storage Bulk-Only (BBB)
50 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
52 * Over these wire protocols it handles the following command protocols
54 * - UFI (floppy command set)
57 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
58 * sc->transform method is used to convert the commands into the appropriate
59 * format (if at all necessary). For example, UFI requires all commands to be
60 * 12 bytes in length amongst other things.
62 * The source code below is marked and can be split into a number of pieces
65 * - probe/attach/detach
66 * - generic transfer routines
69 * - CBI_I (in addition to functions from CBI)
70 * - CAM (Common Access Method)
75 * The protocols are implemented using a state machine, for the transfers as
76 * well as for the resets. The state machine is contained in umass_*_state.
77 * The state machine is started through either umass_*_transfer or
80 * The reason for doing this is a) CAM performs a lot better this way and b) it
81 * avoids using tsleep from interrupt context (for example after a failed
86 * The SCSI related part of this driver has been derived from the
87 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
89 * The CAM layer uses so called actions which are messages sent to the host
90 * adapter for completion. The actions come in through umass_cam_action. The
91 * appropriate block of routines is called depending on the transport protocol
92 * in use. When the transfer has finished, these routines call
93 * umass_cam_cb again to complete the CAM command.
97 * XXX Currently CBI with CCI is not supported because it bombs the system
98 * when the device is detached (low frequency interrupts are detached
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/module.h>
108 #include <sys/sysctl.h>
110 #include <bus/usb/usb.h>
111 #include <bus/usb/usbdi.h>
112 #include <bus/usb/usbdi_util.h>
114 #include <bus/cam/cam.h>
115 #include <bus/cam/cam_ccb.h>
116 #include <bus/cam/cam_sim.h>
117 #include <bus/cam/cam_xpt_sim.h>
118 #include <bus/cam/scsi/scsi_all.h>
119 #include <bus/cam/scsi/scsi_da.h>
120 #include <bus/cam/scsi/scsi_cd.h>
121 #include <bus/cam/scsi/scsi_ch.h>
122 #include <dev/disk/ata/atapi-all.h>
124 #include <bus/cam/cam_periph.h>
127 #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0)
128 #define DPRINTF(m, x) if (umassdebug & (m)) kprintf x
129 #define UDMASS_GEN 0x00010000 /* general */
130 #define UDMASS_SCSI 0x00020000 /* scsi */
131 #define UDMASS_UFI 0x00040000 /* ufi command set */
132 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
133 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
134 #define UDMASS_USB 0x00100000 /* USB general */
135 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
136 #define UDMASS_CBI 0x00400000 /* CBI transfers */
137 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
138 #define UDMASS_ALL 0xffff0000 /* all of the above */
140 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
141 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
142 &umassdebug, 0, "umass debug level");
144 #define DIF(m, x) /* nop */
145 #define DPRINTF(m, x) /* nop */
149 /* Generic definitions */
151 /* Direction for umass_*_transfer */
157 #define DEVNAME "umass"
158 #define DEVNAME_SIM "umass-sim"
160 #define UMASS_MAX_TRANSFER_SIZE 65536
161 /* Approximate maximum transfer speeds (assumes 33% overhead). */
162 #define UMASS_FULL_TRANSFER_SPEED 1000
163 #define UMASS_HIGH_TRANSFER_SPEED 40000
164 #define UMASS_FLOPPY_TRANSFER_SPEED 20
166 #define UMASS_TIMEOUT 5000 /* msecs */
168 /* CAM specific definitions */
170 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
171 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
173 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
176 /* Bulk-Only features */
178 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
179 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
181 /* Command Block Wrapper */
183 uDWord dCBWSignature;
184 # define CBWSIGNATURE 0x43425355
186 uDWord dCBWDataTransferLength;
188 # define CBWFLAGS_OUT 0x00
189 # define CBWFLAGS_IN 0x80
192 # define CBWCDBLENGTH 16
193 uByte CBWCDB[CBWCDBLENGTH];
195 #define UMASS_BBB_CBW_SIZE 31
197 /* Command Status Wrapper */
199 uDWord dCSWSignature;
200 # define CSWSIGNATURE 0x53425355
201 # define CSWSIGNATURE_OLYMPUS_C1 0x55425355
203 uDWord dCSWDataResidue;
205 # define CSWSTATUS_GOOD 0x0
206 # define CSWSTATUS_FAILED 0x1
207 # define CSWSTATUS_PHASE 0x2
209 #define UMASS_BBB_CSW_SIZE 13
213 #define UR_CBI_ADSC 0x00
215 typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */
220 #define IDB_TYPE_CCI 0x00
222 #define IDB_VALUE_PASS 0x00
223 #define IDB_VALUE_FAIL 0x01
224 #define IDB_VALUE_PHASE 0x02
225 #define IDB_VALUE_PERSISTENT 0x03
226 #define IDB_VALUE_STATUS_MASK 0x03
237 struct umass_softc; /* see below */
239 typedef void (*transfer_cb_f) (struct umass_softc *sc, void *priv,
240 int residue, int status);
241 #define STATUS_CMD_OK 0 /* everything ok */
242 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
243 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
244 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
246 typedef void (*wire_reset_f) (struct umass_softc *sc, int status);
247 typedef void (*wire_transfer_f) (struct umass_softc *sc, int lun,
248 void *cmd, int cmdlen, void *data, int datalen,
249 int dir, u_int timeout, transfer_cb_f cb, void *priv);
250 typedef void (*wire_state_f) (usbd_xfer_handle xfer,
251 usbd_private_handle priv, usbd_status err);
253 typedef int (*command_transform_f) (struct umass_softc *sc,
254 unsigned char *cmd, int cmdlen,
255 unsigned char **rcmd, int *rcmdlen);
258 struct umass_devdescr_t {
262 # define WILDCARD_ID 0xffffffff
263 # define EOT_ID 0xfffffffe
265 /* wire and command protocol */
267 # define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
268 # define UMASS_PROTO_CBI 0x0002
269 # define UMASS_PROTO_CBI_I 0x0004
270 # define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
271 # define UMASS_PROTO_SCSI 0x0100 /* command protocol */
272 # define UMASS_PROTO_ATAPI 0x0200
273 # define UMASS_PROTO_UFI 0x0400
274 # define UMASS_PROTO_RBC 0x0800
275 # define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
277 /* Device specific quirks */
279 # define NO_QUIRKS 0x0000
280 /* The drive does not support Test Unit Ready. Convert to Start Unit
282 # define NO_TEST_UNIT_READY 0x0001
283 /* The drive does not reset the Unit Attention state after REQUEST
284 * SENSE has been sent. The INQUIRY command does not reset the UA
285 * either, and so CAM runs in circles trying to retrieve the initial
288 # define RS_NO_CLEAR_UA 0x0002
289 /* The drive does not support START STOP. */
290 # define NO_START_STOP 0x0004
291 /* Don't ask for full inquiry data (255b). */
292 # define FORCE_SHORT_INQUIRY 0x0008
293 /* Needs to be initialised the Shuttle way */
294 # define SHUTTLE_INIT 0x0010
295 /* Drive needs to be switched to alternate iface 1 */
296 # define ALT_IFACE_1 0x0020
297 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
298 # define FLOPPY_SPEED 0x0040
299 /* The device can't count and gets the residue of transfers wrong */
300 # define IGNORE_RESIDUE 0x0080
301 /* No GetMaxLun call */
302 # define NO_GETMAXLUN 0x0100
303 /* The device uses a weird CSWSIGNATURE. */
304 # define WRONG_CSWSIG 0x0200
305 /* Device cannot handle INQUIRY so fake a generic response */
306 # define NO_INQUIRY 0x0400
307 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
308 # define NO_INQUIRY_EVPD 0x0800
309 # define READ_CAPACITY_OFFBY1 0x2000
312 static struct umass_devdescr_t umass_devdescrs[] = {
313 /* Addonics Cable 205 */
314 { .vendor = 0x0bf6, .product = 0xa001, .release = WILDCARD_ID,
315 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
318 /* Addonics USB 2.0 Flash */
319 { .vendor = 0x09df, .product = 0x1300, .release = WILDCARD_ID,
320 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
321 .quirks = IGNORE_RESIDUE
323 /* Addonics Attache 256MB USB */
324 { .vendor = 0x09df, .product = 0x1400, .release = WILDCARD_ID,
325 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
326 .quirks = IGNORE_RESIDUE
328 /* Addonics USB 2.0 Flash */
329 { .vendor = 0x09df, .product = 0x1420, .release = WILDCARD_ID,
330 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
331 .quirks = IGNORE_RESIDUE
333 /* AIPTEK PocketCAM 3Mega */
334 { .vendor = 0x08ca, .product = 0x2011, .release = WILDCARD_ID,
335 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
338 /* All Asahi Optical products */
339 { .vendor = 0x0a17, .product = WILDCARD_ID, .release = WILDCARD_ID,
340 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
341 .quirks = RS_NO_CLEAR_UA
343 /* Belkin USB to SCSI */
344 { .vendor = 0x050d, .product = 0x0115, .release = WILDCARD_ID,
345 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
348 /* CASIO QV DigiCam */
349 { .vendor = 0x07cf, .product = 0x1001, .release = WILDCARD_ID,
350 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
353 /* CCYU EasyDisk ED1064 */
354 { .vendor = 0x1065, .product = 0x2136, .release = WILDCARD_ID,
355 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
358 /* Century Century USB Disk */
359 { .vendor = 0x07f7, .product = 0x011e, .release = WILDCARD_ID,
360 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
361 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
363 /* Desknote UCR-61S2B */
364 { .vendor = 0x1019, .product = 0x0c55, .release = WILDCARD_ID,
365 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
368 /* DMI CF/SM Reader/Writer */
369 { .vendor = 0x0c0b, .product = 0xa109, .release = WILDCARD_ID,
370 .proto = UMASS_PROTO_SCSI,
371 .quirks = NO_GETMAXLUN
373 /* Epson Stylus Photo 875DC */
374 { .vendor = 0x03f8, .product = 0x0601, .release = WILDCARD_ID,
375 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
378 /* Epson Stylus Photo 895 */
379 { .vendor = 0x03f8, .product = 0x0602, .release = WILDCARD_ID,
380 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
381 .quirks = NO_GETMAXLUN
383 /* Feiya 5-in-1 Card Reader */
384 { .vendor = 0x090c, .product = 0x1132, .release = WILDCARD_ID,
385 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
388 /* Freecom DVD drive */
389 { .vendor = 0x07ab, .product = 0xfc01, .release = WILDCARD_ID,
390 .proto = UMASS_PROTO_SCSI,
393 /* Fujiphoto mass storage products */
394 { .vendor = 0x04cb, .product = 0x0100, .release = WILDCARD_ID,
395 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
396 .quirks = RS_NO_CLEAR_UA
398 /* Genesys GL641USB USB-IDE Bridge */
399 { .vendor = 0x05e3, .product = 0x0701, .release = WILDCARD_ID,
400 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
401 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
403 /* Genesys GL641USB USB-IDE Bridge */
404 { .vendor = 0x05e3, .product = 0x0702, .release = WILDCARD_ID,
405 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
406 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
408 /* Genesys GL641USB CompactFlash Card */
409 { .vendor = 0x05e3, .product = 0x0700, .release = WILDCARD_ID,
410 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
411 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
413 /* Genesys GL641USB 6-in-1 Card */
414 { .vendor = 0x05e3, .product = 0x0760, .release = WILDCARD_ID,
415 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
416 .quirks = WRONG_CSWSIG
418 /* Hagiwara FlashGate SmartMedia Card */
419 { .vendor = 0x0693, .product = 0x0002, .release = WILDCARD_ID,
420 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
423 /* Hitachi DVDCAM USB HS Interface */
424 { .vendor = 0x04a4, .product = 0x001e, .release = WILDCARD_ID,
425 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
428 /* Hitachi DVD-CAM DZ-MV100A Camcorder */
429 { .vendor = 0x04a4, .product = 0x0004, .release = WILDCARD_ID,
430 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
431 .quirks = NO_GETMAXLUN
433 /* Hewlett CD-Writer+ CD-4e */
434 { .vendor = 0x03f0, .product = 0x0307, .release = WILDCARD_ID,
435 .proto = UMASS_PROTO_ATAPI,
438 /* Hewlett CD-Writer Plus 8200e */
439 { .vendor = 0x03f0, .product = 0x0207, .release = WILDCARD_ID,
440 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
441 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
443 /* Imagination DBX1 DSP core */
444 { .vendor = 0x149a, .product = 0x2107, .release = WILDCARD_ID,
445 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
446 .quirks = WRONG_CSWSIG
448 /* In-System ATAPI Adapter */
449 { .vendor = 0x05ab, .product = 0x0031, .release = WILDCARD_ID,
450 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
453 /* In-System USB Storage Adapter */
454 { .vendor = 0x05ab, .product = 0x5701, .release = WILDCARD_ID,
455 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
458 /* In-System USB cable */
459 { .vendor = 0x05ab, .product = 0x081a, .release = WILDCARD_ID,
460 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
461 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
463 /* I-O DVD Multi-plus unit */
464 { .vendor = 0x04bb, .product = 0x0204, .release = WILDCARD_ID,
465 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
468 /* I-O DVD Multi-plus unit */
469 { .vendor = 0x04bb, .product = 0x0206, .release = WILDCARD_ID,
470 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
474 { .vendor = 0x059b, .product = 0x0001, .release = WILDCARD_ID,
475 /* XXX This is not correct as there are Zip drives that use ATAPI. */
476 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
477 .quirks = NO_TEST_UNIT_READY
479 /* Kyocera Finecam L3 */
480 { .vendor = 0x0482, .product = 0x0105, .release = WILDCARD_ID,
481 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
484 /* Kyocera Finecam S3x */
485 { .vendor = 0x0482, .product = 0x0100, .release = WILDCARD_ID,
486 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
489 /* Kyocera Finecam S4 */
490 { .vendor = 0x0482, .product = 0x0101, .release = WILDCARD_ID,
491 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
494 /* Kyocera Finecam S5 */
495 { .vendor = 0x0482, .product = 0x0103, .release = WILDCARD_ID,
496 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
499 /* LaCie Hard Disk */
500 { .vendor = 0x059f, .product = 0xa601, .release = WILDCARD_ID,
501 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
504 /* Lexar USB CF Reader */
505 { .vendor = 0x05dc, .product = 0xb002, .release = WILDCARD_ID,
506 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
509 /* Lexar jumpSHOT CompactFlash Reader */
510 { .vendor = 0x05dc, .product = 0x0001, .release = WILDCARD_ID,
511 .proto = UMASS_PROTO_SCSI,
514 /* Logitech DVD Multi-plus unit */
515 { .vendor = 0x046d, .product = 0x0033, .release = WILDCARD_ID,
516 .proto = UMASS_PROTO_SCSI,
519 /* Logitech DVD Multi-plus unit LDR-H443U2 */
520 { .vendor = 0x0789, .product = 0x00b3, .release = WILDCARD_ID,
521 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
524 /* Melco USB-IDE Bridge: DUB-PxxG */
525 { .vendor = 0x0411, .product = 0x001c, .release = WILDCARD_ID,
526 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
527 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
529 /* Microtech USB CameraMate */
530 { .vendor = 0x07af, .product = 0x0006, .release = WILDCARD_ID,
531 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
532 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
534 /* Microtech USB-SCSI-DB25 */
535 { .vendor = 0x07af, .product = 0x0004, .release = WILDCARD_ID,
536 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
539 /* Microtech USB-SCSI-HD50 */
540 { .vendor = 0x07af, .product = 0x0005, .release = WILDCARD_ID,
541 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
544 /* Minolta Dimage E223 */
545 { .vendor = 0x0686, .product = 0x4017, .release = WILDCARD_ID,
546 .proto = UMASS_PROTO_SCSI,
549 /* Minolta Dimage F300 */
550 { .vendor = 0x0686, .product = 0x4011, .release = WILDCARD_ID,
551 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
554 /* Mitsumi CD-R/RW Drive */
555 { .vendor = 0x03ee, .product = 0x0000, .release = WILDCARD_ID,
556 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
559 /* Mitsumi USB FDD */
560 { .vendor = 0x03ee, .product = 0x6901, .release = WILDCARD_ID,
561 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
562 .quirks = NO_GETMAXLUN
564 /* Motorola E398 Mobile Phone */
565 { .vendor = 0x22b8, .product = 0x4810, .release = WILDCARD_ID,
566 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
567 .quirks = FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
569 /* M-Systems DiskOnKey */
570 { .vendor = 0x08ec, .product = 0x0010, .release = WILDCARD_ID,
571 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
572 .quirks = IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
574 /* M-Systems DiskOnKey */
575 { .vendor = 0x08ec, .product = 0x0011, .release = WILDCARD_ID,
576 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
580 { .vendor = 0x04cf, .product = 0x8818, .release = WILDCARD_ID,
581 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
582 .quirks = NO_INQUIRY | IGNORE_RESIDUE
584 /* Neodio 8-in-1 Multi-format Flash */
585 { .vendor = 0x0aec, .product = 0x3260, .release = WILDCARD_ID,
586 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
587 .quirks = FORCE_SHORT_INQUIRY
589 /* Netac USB-CF-Card */
590 { .vendor = 0x0dd8, .product = 0x1060, .release = WILDCARD_ID,
591 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
595 { .vendor = 0x0dd8, .product = 0x0003, .release = WILDCARD_ID,
596 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
597 .quirks = IGNORE_RESIDUE
599 /* NetChip USB Clik! 40 */
600 { .vendor = 0x0525, .product = 0xa140, .release = WILDCARD_ID,
601 .proto = UMASS_PROTO_ATAPI,
604 /* Olympus C-1 Digital Camera */
605 { .vendor = 0x07b4, .product = 0x0102, .release = WILDCARD_ID,
606 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
607 .quirks = WRONG_CSWSIG
609 /* Olympus C-700 Ultra Zoom */
610 { .vendor = 0x07b4, .product = 0x0105, .release = WILDCARD_ID,
611 .proto = UMASS_PROTO_SCSI,
612 .quirks = NO_GETMAXLUN
614 /* OnSpec SIIG/Datafab Memory Stick+CF */
615 { .vendor = 0x07c4, .product = 0xa001, .release = WILDCARD_ID,
616 .proto = UMASS_PROTO_SCSI,
619 /* OnSpec USB to CF */
620 { .vendor = 0x07c4, .product = 0xa109, .release = WILDCARD_ID,
621 .proto = UMASS_PROTO_SCSI,
624 /* OnSpec PNY/Datafab CF+SM Reader */
625 { .vendor = 0x07c4, .product = 0xa005, .release = WILDCARD_ID,
626 .proto = UMASS_PROTO_SCSI,
629 /* OnSpec Simple Tech/Datafab CF+SM */
630 { .vendor = 0x07c4, .product = 0xa006, .release = WILDCARD_ID,
631 .proto = UMASS_PROTO_SCSI,
634 /* OnSpec MDCFE-B USB CF */
635 { .vendor = 0x07c4, .product = 0xa000, .release = WILDCARD_ID,
636 .proto = UMASS_PROTO_SCSI,
639 /* OnSpec MDSM-B reader */
640 { .vendor = 0x07c4, .product = 0xa103, .release = WILDCARD_ID,
641 .proto = UMASS_PROTO_SCSI,
644 /* OnSpec Datafab-based Reader */
645 { .vendor = 0x07c4, .product = 0xa003, .release = WILDCARD_ID,
646 .proto = UMASS_PROTO_SCSI,
649 /* OnSpec FlashLink UCF-100 CompactFlash */
650 { .vendor = 0x07c4, .product = 0xa400, .release = WILDCARD_ID,
651 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
652 .quirks = NO_INQUIRY | NO_GETMAXLUN
654 /* OnSpec ImageMate SDDR55 */
655 { .vendor = 0x55aa, .product = 0xa103, .release = WILDCARD_ID,
656 .proto = UMASS_PROTO_SCSI,
657 .quirks = NO_GETMAXLUN
659 /* Panasonic CD-R Drive KXL-840AN */
660 { .vendor = 0x04da, .product = 0x0d01, .release = WILDCARD_ID,
661 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
662 .quirks = NO_GETMAXLUN
664 /* Panasonic CD-R Drive KXL-CB20AN */
665 { .vendor = 0x04da, .product = 0x0d0a, .release = WILDCARD_ID,
666 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
669 /* Panasonic DVD-ROM & CD-R/RW */
670 { .vendor = 0x04da, .product = 0x0d0e, .release = WILDCARD_ID,
671 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
674 /* Panasonic LS-120 Camera */
675 { .vendor = 0x04da, .product = 0x0901, .release = WILDCARD_ID,
676 .proto = UMASS_PROTO_UFI,
679 /* Plextor PlexWriter 40/12/40U */
680 { .vendor = 0x093b, .product = 0x0011, .release = WILDCARD_ID,
681 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
682 .quirks = NO_TEST_UNIT_READY
684 /* PNY USB 2.0 Flash */
685 { .vendor = 0x154b, .product = 0x0010, .release = WILDCARD_ID,
686 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
687 .quirks = IGNORE_RESIDUE | NO_START_STOP
689 /* Pen USB 2.0 Flash Drive */
690 { .vendor = 0x0d7d, .product = 0x1300, .release = WILDCARD_ID,
691 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
692 .quirks = IGNORE_RESIDUE
694 /* Samsung YP-U2 MP3 Player */
695 { .vendor = 0x04e8, .product = 0x5050, .release = WILDCARD_ID,
696 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
697 .quirks = SHUTTLE_INIT | NO_GETMAXLUN
699 /* Samsung Digimax 410 */
700 { .vendor = 0x0839, .product = 0x000a, .release = WILDCARD_ID,
701 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
704 /* SanDisk ImageMate SDDR-05a */
705 { .vendor = 0x0781, .product = 0x0001, .release = WILDCARD_ID,
706 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
707 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
709 /* SanDisk ImageMate SDDR-09 */
710 { .vendor = 0x0781, .product = 0x0200, .release = WILDCARD_ID,
711 .proto = UMASS_PROTO_SCSI,
712 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
714 /* SanDisk ImageMate SDDR-12 */
715 { .vendor = 0x0781, .product = 0x0100, .release = WILDCARD_ID,
716 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
717 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
719 /* SanDisk ImageMate SDDR-31 */
720 { .vendor = 0x0781, .product = 0x0002, .release = WILDCARD_ID,
721 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
722 .quirks = READ_CAPACITY_OFFBY1
724 /* SanDisk Cruzer Mini 256MB */
725 { .vendor = 0x0781, .product = 0x7104, .release = WILDCARD_ID,
726 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
727 .quirks = IGNORE_RESIDUE
729 /* SanDisk Cruzer Micro 128MB */
730 { .vendor = 0x0781, .product = 0x7112, .release = WILDCARD_ID,
731 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
732 .quirks = IGNORE_RESIDUE
734 /* SanDisk Cruzer Micro 256MB */
735 { .vendor = 0x0781, .product = 0x7113, .release = WILDCARD_ID,
736 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
737 .quirks = IGNORE_RESIDUE
739 /* ScanLogic SL11R IDE Adapter */
740 { .vendor = 0x04ce, .product = 0x0002, .release = WILDCARD_ID,
741 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
744 /* Shuttle CD-RW Device */
745 { .vendor = 0x04e6, .product = 0x0101, .release = WILDCARD_ID,
746 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
749 /* Shuttle eUSB CompactFlash Adapter */
750 { .vendor = 0x04e6, .product = 0x000a, .release = WILDCARD_ID,
751 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
754 /* Shuttle E-USB Bridge */
755 { .vendor = 0x04e6, .product = 0x0001, .release = WILDCARD_ID,
756 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
757 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
759 /* Shuttle eUSB ATA/ATAPI Adapter */
760 { .vendor = 0x04e6, .product = 0x0009, .release = WILDCARD_ID,
761 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
764 /* Shuttle eUSB SmartMedia / */
765 { .vendor = 0x04e6, .product = 0x0005, .release = WILDCARD_ID,
766 .proto = UMASS_PROTO_SCSI,
769 /* Shuttle eUSCSI Bridge */
770 { .vendor = 0x04e6, .product = 0x0002, .release = WILDCARD_ID,
771 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
774 /* Shuttle Sony Hifd */
775 { .vendor = 0x04e6, .product = 0x0007, .release = WILDCARD_ID,
776 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
777 .quirks = NO_GETMAXLUN
779 /* Shuttle ImageMate SDDR09 */
780 { .vendor = 0x04e6, .product = 0x0003, .release = WILDCARD_ID,
781 .proto = UMASS_PROTO_SCSI,
782 .quirks = NO_GETMAXLUN
784 /* Shuttle eUSB MultiMediaCard Adapter */
785 { .vendor = 0x04e6, .product = 0x0006, .release = WILDCARD_ID,
786 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
787 .quirks = NO_GETMAXLUN
789 /* Sigmatel i-Bead 100 MP3 */
790 { .vendor = 0x066f, .product = 0x8008, .release = WILDCARD_ID,
791 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
792 .quirks = SHUTTLE_INIT
794 /* SIIG WINTERREADER Reader */
795 { .vendor = 0x07cc, .product = 0x0330, .release = WILDCARD_ID,
796 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
797 .quirks = IGNORE_RESIDUE
799 /* Skanhex MD 7425 Camera */
800 { .vendor = 0x0d96, .product = 0x410a, .release = WILDCARD_ID,
801 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
804 /* Skanhex SX 520z Camera */
805 { .vendor = 0x0d96, .product = 0x5200, .release = WILDCARD_ID,
806 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
810 { .vendor = 0x054c, .product = 0x006d, .release = WILDCARD_ID,
811 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
814 /* Sony DSC cameras */
815 { .vendor = 0x054c, .product = 0x0010, .release = WILDCARD_ID,
816 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
820 { .vendor = 0x054c, .product = 0x002e, .release = WILDCARD_ID,
821 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
824 /* Sony Memorystick MSC-U03 */
825 { .vendor = 0x054c, .product = 0x0069, .release = WILDCARD_ID,
826 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
827 .quirks = NO_GETMAXLUN
829 /* Sony Memorystick NW-MS7 */
830 { .vendor = 0x054c, .product = 0x0025, .release = WILDCARD_ID,
831 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
832 .quirks = NO_GETMAXLUN
834 /* Sony PEG N760c Memorystick */
835 { .vendor = 0x054c, .product = 0x0058, .release = WILDCARD_ID,
836 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
839 /* Sony Memorystick MSAC-US1 */
840 { .vendor = 0x054c, .product = 0x002d, .release = WILDCARD_ID,
841 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
842 .quirks = NO_GETMAXLUN
844 /* Sony MSC memory stick */
845 { .vendor = 0x054c, .product = 0x0032, .release = WILDCARD_ID,
846 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
849 /* Sony Portable USB Harddrive */
850 { .vendor = 0x054c, .product = 0x002b, .release = WILDCARD_ID,
851 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
854 /* Taugagreining CameraMate (DPCM_USB) */
855 { .vendor = 0x0436, .product = 0x0005, .release = WILDCARD_ID,
856 .proto = UMASS_PROTO_SCSI,
859 /* TEAC FD-05PUB floppy */
860 { .vendor = 0x0644, .product = 0x0000, .release = WILDCARD_ID,
861 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
864 /* Trek IBM USB Memory */
865 { .vendor = 0x0a16, .product = 0x8888, .release = WILDCARD_ID,
866 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
869 /* Trek ThumbDrive_8MB */
870 { .vendor = 0x0a16, .product = 0x9988, .release = WILDCARD_ID,
871 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
872 .quirks = IGNORE_RESIDUE
874 /* Trumpion Comotron C3310 MP3 */
875 { .vendor = 0x090a, .product = 0x1100, .release = WILDCARD_ID,
876 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
879 /* Trumpion MP3 player */
880 { .vendor = 0x090a, .product = 0x1200, .release = WILDCARD_ID,
881 .proto = UMASS_PROTO_RBC,
884 /* Trumpion T33520 USB Flash */
885 { .vendor = 0x090a, .product = 0x1001, .release = WILDCARD_ID,
886 .proto = UMASS_PROTO_SCSI,
889 /* TwinMOS Memory Disk IV */
890 { .vendor = 0x126f, .product = 0x1325, .release = WILDCARD_ID,
891 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
894 /* Vivitar Vivicam 35Xx */
895 { .vendor = 0x0636, .product = 0x0003, .release = WILDCARD_ID,
896 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
899 /* Western Firewire USB Combo */
900 { .vendor = 0x1058, .product = 0x0200, .release = WILDCARD_ID,
901 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
902 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
904 /* Western External HDD */
905 { .vendor = 0x1058, .product = 0x0400, .release = WILDCARD_ID,
906 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
907 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
909 /* Western MyBook External HDD */
910 { .vendor = 0x1058, .product = 0x0901, .release = WILDCARD_ID,
911 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
912 .quirks = NO_INQUIRY_EVPD
914 /* WinMaxGroup USB Flash Disk */
915 { .vendor = 0x0ed1, .product = 0x6660, .release = WILDCARD_ID,
916 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
919 /* Yano METALWEAR-HDD */
920 { .vendor = 0x094f, .product = 0x05fc, .release = WILDCARD_ID,
921 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
922 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
925 { .vendor = 0x094f, .product = 0x0101, .release = WILDCARD_ID,
926 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
927 .quirks = FORCE_SHORT_INQUIRY
929 /* Y-E Flashbuster-U */
930 { .vendor = 0x057b, .product = 0x0000, .release = WILDCARD_ID,
931 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
932 .quirks = NO_GETMAXLUN
934 /* Zoran Digital Camera EX-20 */
935 { .vendor = 0x0595, .product = 0x4343, .release = WILDCARD_ID,
936 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
939 { .vendor = EOT_ID, .product = EOT_ID, .release = EOT_ID,
940 .proto = 0, .quirks = 0 }
944 /* the per device structure */
946 device_t sc_dev; /* base device */
947 usbd_device_handle sc_udev; /* USB device */
949 struct cam_sim *umass_sim; /* SCSI Interface Module */
951 unsigned char flags; /* various device flags */
952 # define UMASS_FLAGS_GONE 0x01 /* devices is no more */
954 u_int16_t proto; /* wire and cmd protocol */
955 u_int16_t quirks; /* they got it almost right */
957 usbd_interface_handle iface; /* Mass Storage interface */
958 int ifaceno; /* MS iface number */
960 u_int8_t bulkin; /* bulk-in Endpoint Address */
961 u_int8_t bulkout; /* bulk-out Endpoint Address */
962 u_int8_t intrin; /* intr-in Endp. (CBI) */
963 usbd_pipe_handle bulkin_pipe;
964 usbd_pipe_handle bulkout_pipe;
965 usbd_pipe_handle intrin_pipe;
967 /* Reset the device in a wire protocol specific way */
970 /* The start of a wire transfer. It prepares the whole transfer (cmd,
971 * data, and status stage) and initiates it. It is up to the state
972 * machine (below) to handle the various stages and errors in these
974 wire_transfer_f transfer;
976 /* The state machine, handling the various states during a transfer */
979 /* The command transform function is used to conver the SCSI commands
980 * into their derivatives, like UFI, ATAPI, and friends.
982 command_transform_f transform; /* command transform */
984 /* Bulk specific variables for transfers in progress */
985 umass_bbb_cbw_t cbw; /* command block wrapper */
986 umass_bbb_csw_t csw; /* command status wrapper*/
987 /* CBI specific variables for transfers in progress */
988 umass_cbi_cbl_t cbl; /* command block */
989 umass_cbi_sbl_t sbl; /* status block */
991 /* generic variables for transfers in progress */
992 /* ctrl transfer requests */
993 usb_device_request_t request;
996 * Most of our operations are initiated from interrupt context, so
997 * we need to avoid using the one that is in use. We want to avoid
998 * allocating them in the interrupt context as well.
1000 /* indices into array below */
1001 # define XFER_BBB_CBW 0 /* Bulk-Only */
1002 # define XFER_BBB_DATA 1
1003 # define XFER_BBB_DCLEAR 2
1004 # define XFER_BBB_CSW1 3
1005 # define XFER_BBB_CSW2 4
1006 # define XFER_BBB_SCLEAR 5
1007 # define XFER_BBB_RESET1 6
1008 # define XFER_BBB_RESET2 7
1009 # define XFER_BBB_RESET3 8
1011 # define XFER_CBI_CB 0 /* CBI */
1012 # define XFER_CBI_DATA 1
1013 # define XFER_CBI_STATUS 2
1014 # define XFER_CBI_DCLEAR 3
1015 # define XFER_CBI_SCLEAR 4
1016 # define XFER_CBI_RESET1 5
1017 # define XFER_CBI_RESET2 6
1018 # define XFER_CBI_RESET3 7
1020 # define XFER_NR 9 /* maximum number */
1022 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
1024 int transfer_dir; /* data direction */
1025 void *transfer_data; /* data buffer */
1026 int transfer_datalen; /* (maximum) length */
1027 int transfer_actlen; /* actual length */
1028 transfer_cb_f transfer_cb; /* callback */
1029 void *transfer_priv; /* for callback */
1030 int transfer_status;
1033 # define TSTATE_ATTACH 0 /* in attach */
1034 # define TSTATE_IDLE 1
1035 # define TSTATE_BBB_COMMAND 2 /* CBW transfer */
1036 # define TSTATE_BBB_DATA 3 /* Data transfer */
1037 # define TSTATE_BBB_DCLEAR 4 /* clear endpt stall */
1038 # define TSTATE_BBB_STATUS1 5 /* clear endpt stall */
1039 # define TSTATE_BBB_SCLEAR 6 /* clear endpt stall */
1040 # define TSTATE_BBB_STATUS2 7 /* CSW transfer */
1041 # define TSTATE_BBB_RESET1 8 /* reset command */
1042 # define TSTATE_BBB_RESET2 9 /* in clear stall */
1043 # define TSTATE_BBB_RESET3 10 /* out clear stall */
1044 # define TSTATE_CBI_COMMAND 11 /* command transfer */
1045 # define TSTATE_CBI_DATA 12 /* data transfer */
1046 # define TSTATE_CBI_STATUS 13 /* status transfer */
1047 # define TSTATE_CBI_DCLEAR 14 /* clear ep stall */
1048 # define TSTATE_CBI_SCLEAR 15 /* clear ep stall */
1049 # define TSTATE_CBI_RESET1 16 /* reset command */
1050 # define TSTATE_CBI_RESET2 17 /* in clear stall */
1051 # define TSTATE_CBI_RESET3 18 /* out clear stall */
1052 # define TSTATE_STATES 19 /* # of states above */
1055 /* SCSI/CAM specific variables */
1056 unsigned char cam_scsi_command[CAM_MAX_CDBLEN];
1057 unsigned char cam_scsi_command2[CAM_MAX_CDBLEN];
1058 struct scsi_sense cam_scsi_sense;
1059 struct scsi_sense cam_scsi_test_unit_ready;
1060 int timeout; /* in msecs */
1062 int maxlun; /* maximum LUN number */
1063 struct callout rescan_timeout;
1067 char *states[TSTATE_STATES+1] = {
1068 /* should be kept in sync with the list at transfer_state */
1073 "BBB Data bulk-in/-out clear stall",
1074 "BBB CSW, 1st attempt",
1075 "BBB CSW bulk-in clear stall",
1076 "BBB CSW, 2nd attempt",
1078 "BBB bulk-in clear stall",
1079 "BBB bulk-out clear stall",
1083 "CBI Data bulk-in/-out clear stall",
1084 "CBI Status intr-in clear stall",
1086 "CBI bulk-in clear stall",
1087 "CBI bulk-out clear stall",
1092 /* If device cannot return valid inquiry data, fake it */
1093 static uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1094 0, /*removable*/ 0x80, SCSI_REV_2, SCSI_REV_2,
1095 /*additional_length*/ 31, 0, 0, 0
1098 /* USB device probe/attach/detach functions */
1099 static device_probe_t umass_match;
1100 static device_attach_t umass_attach;
1101 static device_detach_t umass_detach;
1103 static devclass_t umass_devclass;
1105 static kobj_method_t umass_methods[] = {
1106 DEVMETHOD(device_probe, umass_match),
1107 DEVMETHOD(device_attach, umass_attach),
1108 DEVMETHOD(device_detach, umass_detach),
1113 static driver_t umass_driver = {
1116 sizeof(struct umass_softc)
1119 MODULE_DEPEND(umass, usb, 1, 1, 1);
1121 static int umass_match_proto (struct umass_softc *sc,
1122 usbd_interface_handle iface,
1123 usbd_device_handle udev);
1125 /* quirk functions */
1126 static void umass_init_shuttle (struct umass_softc *sc);
1128 /* generic transfer functions */
1129 static usbd_status umass_setup_transfer (struct umass_softc *sc,
1130 usbd_pipe_handle pipe,
1131 void *buffer, int buflen, int flags,
1132 usbd_xfer_handle xfer);
1133 static usbd_status umass_setup_ctrl_transfer (struct umass_softc *sc,
1134 usbd_device_handle udev,
1135 usb_device_request_t *req,
1136 void *buffer, int buflen, int flags,
1137 usbd_xfer_handle xfer);
1138 static void umass_clear_endpoint_stall (struct umass_softc *sc,
1139 u_int8_t endpt, usbd_pipe_handle pipe,
1140 int state, usbd_xfer_handle xfer);
1141 static void umass_reset (struct umass_softc *sc,
1142 transfer_cb_f cb, void *priv);
1144 /* Bulk-Only related functions */
1145 static void umass_bbb_reset (struct umass_softc *sc, int status);
1146 static void umass_bbb_transfer (struct umass_softc *sc, int lun,
1147 void *cmd, int cmdlen,
1148 void *data, int datalen, int dir, u_int timeout,
1149 transfer_cb_f cb, void *priv);
1150 static void umass_bbb_state (usbd_xfer_handle xfer,
1151 usbd_private_handle priv,
1153 static int umass_bbb_get_max_lun
1154 (struct umass_softc *sc);
1156 /* CBI related functions */
1157 static int umass_cbi_adsc (struct umass_softc *sc,
1158 char *buffer, int buflen,
1159 usbd_xfer_handle xfer);
1160 static void umass_cbi_reset (struct umass_softc *sc, int status);
1161 static void umass_cbi_transfer (struct umass_softc *sc, int lun,
1162 void *cmd, int cmdlen,
1163 void *data, int datalen, int dir, u_int timeout,
1164 transfer_cb_f cb, void *priv);
1165 static void umass_cbi_state (usbd_xfer_handle xfer,
1166 usbd_private_handle priv, usbd_status err);
1168 /* CAM related functions */
1169 static void umass_cam_action (struct cam_sim *sim, union ccb *ccb);
1170 static void umass_cam_poll (struct cam_sim *sim);
1172 static void umass_cam_cb (struct umass_softc *sc, void *priv,
1173 int residue, int status);
1174 static void umass_cam_sense_cb (struct umass_softc *sc, void *priv,
1175 int residue, int status);
1176 static void umass_cam_quirk_cb (struct umass_softc *sc, void *priv,
1177 int residue, int status);
1179 static void umass_cam_rescan_callback
1180 (struct cam_periph *periph,union ccb *ccb);
1181 static void umass_cam_rescan (void *addr);
1183 static int umass_cam_attach_sim (struct umass_softc *sc);
1184 static int umass_cam_attach (struct umass_softc *sc);
1185 static int umass_cam_detach_sim (struct umass_softc *sc);
1188 /* SCSI specific functions */
1189 static int umass_scsi_transform (struct umass_softc *sc,
1190 unsigned char *cmd, int cmdlen,
1191 unsigned char **rcmd, int *rcmdlen);
1193 /* UFI specific functions */
1194 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
1195 static int umass_ufi_transform (struct umass_softc *sc,
1196 unsigned char *cmd, int cmdlen,
1197 unsigned char **rcmd, int *rcmdlen);
1199 /* ATAPI (8070i) specific functions */
1200 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
1201 static int umass_atapi_transform (struct umass_softc *sc,
1202 unsigned char *cmd, int cmdlen,
1203 unsigned char **rcmd, int *rcmdlen);
1205 /* RBC specific functions */
1206 static int umass_rbc_transform (struct umass_softc *sc,
1207 unsigned char *cmd, int cmdlen,
1208 unsigned char **rcmd, int *rcmdlen);
1211 /* General debugging functions */
1212 static void umass_bbb_dump_cbw (struct umass_softc *sc, umass_bbb_cbw_t *cbw);
1213 static void umass_bbb_dump_csw (struct umass_softc *sc, umass_bbb_csw_t *csw);
1214 static void umass_cbi_dump_cmd (struct umass_softc *sc, void *cmd, int cmdlen);
1215 static void umass_dump_buffer (struct umass_softc *sc, u_int8_t *buffer,
1216 int buflen, int printlen);
1219 MODULE_DEPEND(umass, cam, 1,1,1);
1222 * USB device probe/attach/detach
1226 * Match the device we are seeing with the devices supported. Fill in the
1227 * description in the softc accordingly. This function is called from both
1232 umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface,
1233 usbd_device_handle udev)
1235 usb_device_descriptor_t *dd;
1236 usb_interface_descriptor_t *id;
1244 dd = usbd_get_device_descriptor(udev);
1246 /* An entry specifically for Y-E Data devices as they don't fit in the
1247 * device description table.
1249 if (UGETW(dd->idVendor) == 0x057b && UGETW(dd->idProduct) == 0x0000) {
1251 /* Revisions < 1.28 do not handle the inerrupt endpoint
1254 if (UGETW(dd->bcdDevice) < 0x128) {
1255 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1257 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1261 * Revisions < 1.28 do not have the TEST UNIT READY command
1262 * Revisions == 1.28 have a broken TEST UNIT READY
1264 if (UGETW(dd->bcdDevice) <= 0x128)
1265 sc->quirks |= NO_TEST_UNIT_READY;
1267 sc->quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1268 return(UMATCH_VENDOR_PRODUCT);
1271 /* Check the list of supported devices for a match. While looking,
1272 * check for wildcarded and fully matched. First match wins.
1274 for (i = 0; umass_devdescrs[i].vendor != EOT_ID && !found; i++) {
1275 if (umass_devdescrs[i].vendor == WILDCARD_ID &&
1276 umass_devdescrs[i].product == WILDCARD_ID &&
1277 umass_devdescrs[i].release == WILDCARD_ID) {
1278 kprintf("umass: ignoring invalid wildcard quirk\n");
1281 if ((umass_devdescrs[i].vendor == UGETW(dd->idVendor) ||
1282 umass_devdescrs[i].vendor == WILDCARD_ID)
1283 && (umass_devdescrs[i].product == UGETW(dd->idProduct) ||
1284 umass_devdescrs[i].product == WILDCARD_ID)) {
1285 if (umass_devdescrs[i].release == WILDCARD_ID) {
1286 sc->proto = umass_devdescrs[i].proto;
1287 sc->quirks = umass_devdescrs[i].quirks;
1288 return (UMATCH_VENDOR_PRODUCT);
1289 } else if (umass_devdescrs[i].release ==
1290 UGETW(dd->bcdDevice)) {
1291 sc->proto = umass_devdescrs[i].proto;
1292 sc->quirks = umass_devdescrs[i].quirks;
1293 return (UMATCH_VENDOR_PRODUCT_REV);
1294 } /* else RID does not match */
1298 /* Check for a standards compliant device */
1300 id = usbd_get_interface_descriptor(iface);
1301 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
1302 return(UMATCH_NONE);
1304 switch (id->bInterfaceSubClass) {
1305 case UISUBCLASS_SCSI:
1306 sc->proto |= UMASS_PROTO_SCSI;
1308 case UISUBCLASS_UFI:
1309 sc->proto |= UMASS_PROTO_UFI;
1311 case UISUBCLASS_RBC:
1312 sc->proto |= UMASS_PROTO_RBC;
1314 case UISUBCLASS_SFF8020I:
1315 case UISUBCLASS_SFF8070I:
1316 sc->proto |= UMASS_PROTO_ATAPI;
1319 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
1320 device_get_nameunit(sc->sc_dev), id->bInterfaceSubClass));
1321 return(UMATCH_NONE);
1324 switch (id->bInterfaceProtocol) {
1325 case UIPROTO_MASS_CBI:
1326 sc->proto |= UMASS_PROTO_CBI;
1328 case UIPROTO_MASS_CBI_I:
1329 sc->proto |= UMASS_PROTO_CBI_I;
1331 case UIPROTO_MASS_BBB_OLD:
1332 case UIPROTO_MASS_BBB:
1333 sc->proto |= UMASS_PROTO_BBB;
1336 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
1337 device_get_nameunit(sc->sc_dev), id->bInterfaceProtocol));
1338 return(UMATCH_NONE);
1341 return(UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
1345 umass_match(device_t self)
1347 struct usb_attach_arg *uaa = device_get_ivars(self);
1348 struct umass_softc *sc = device_get_softc(self);
1352 if (uaa->iface == NULL)
1353 return(UMATCH_NONE);
1355 return(umass_match_proto(sc, uaa->iface, uaa->device));
1359 umass_attach(device_t self)
1361 struct umass_softc *sc = device_get_softc(self);
1362 struct usb_attach_arg *uaa = device_get_ivars(self);
1363 usb_interface_descriptor_t *id;
1364 usb_endpoint_descriptor_t *ed;
1369 * the softc struct is bzero-ed in device_set_driver. We can safely
1370 * call umass_detach without specifically initialising the struct.
1375 sc->iface = uaa->iface;
1376 sc->ifaceno = uaa->ifaceno;
1378 /* initialise the proto and drive values in the umass_softc (again) */
1379 (void) umass_match_proto(sc, sc->iface, uaa->device);
1381 id = usbd_get_interface_descriptor(sc->iface);
1383 kprintf("%s: ", device_get_nameunit(sc->sc_dev));
1384 switch (sc->proto&UMASS_PROTO_COMMAND) {
1385 case UMASS_PROTO_SCSI:
1388 case UMASS_PROTO_ATAPI:
1389 kprintf("8070i (ATAPI)");
1391 case UMASS_PROTO_UFI:
1394 case UMASS_PROTO_RBC:
1398 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_COMMAND);
1402 switch (sc->proto&UMASS_PROTO_WIRE) {
1403 case UMASS_PROTO_BBB:
1404 kprintf("Bulk-Only");
1406 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
1409 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1410 kprintf("CBI with CCI");
1412 kprintf(" (using CBI)");
1416 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_WIRE);
1418 kprintf("; quirks = 0x%04x\n", sc->quirks);
1422 if (sc->proto & UMASS_PROTO_CBI_I) {
1423 /* See beginning of file for comment on the use of CBI with CCI */
1424 sc->proto = (sc->proto & ~UMASS_PROTO_CBI_I) | UMASS_PROTO_CBI;
1428 if (sc->quirks & ALT_IFACE_1) {
1429 err = usbd_set_interface(0, 1);
1431 DPRINTF(UDMASS_USB, ("%s: could not switch to "
1432 "Alt Interface %d\n",
1433 device_get_nameunit(sc->sc_dev), 1));
1440 * In addition to the Control endpoint the following endpoints
1442 * a) bulk-in endpoint.
1443 * b) bulk-out endpoint.
1444 * and for Control/Bulk/Interrupt with CCI (CBI_I)
1447 * The endpoint addresses are not fixed, so we have to read them
1448 * from the device descriptors of the current interface.
1450 for (i = 0 ; i < id->bNumEndpoints ; i++) {
1451 ed = usbd_interface2endpoint_descriptor(sc->iface, i);
1453 kprintf("%s: could not read endpoint descriptor\n",
1454 device_get_nameunit(sc->sc_dev));
1457 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1458 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1459 sc->bulkin = ed->bEndpointAddress;
1460 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
1461 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1462 sc->bulkout = ed->bEndpointAddress;
1463 } else if (sc->proto & UMASS_PROTO_CBI_I
1464 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1465 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
1466 sc->intrin = ed->bEndpointAddress;
1468 if (UGETW(ed->wMaxPacketSize) > 2) {
1469 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
1470 device_get_nameunit(sc->sc_dev),
1471 UGETW(ed->wMaxPacketSize)));
1477 /* check whether we found all the endpoints we need */
1478 if (!sc->bulkin || !sc->bulkout
1479 || (sc->proto & UMASS_PROTO_CBI_I && !sc->intrin) ) {
1480 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
1481 device_get_nameunit(sc->sc_dev),
1482 sc->bulkin, sc->bulkout, sc->intrin));
1487 /* Open the bulk-in and -out pipe */
1488 err = usbd_open_pipe(sc->iface, sc->bulkout,
1489 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
1491 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
1492 device_get_nameunit(sc->sc_dev), sc->bulkout));
1496 err = usbd_open_pipe(sc->iface, sc->bulkin,
1497 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
1499 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
1500 device_get_nameunit(sc->sc_dev), sc->bulkin));
1504 /* Open the intr-in pipe if the protocol is CBI with CCI.
1505 * Note: early versions of the Zip drive do have an interrupt pipe, but
1506 * this pipe is unused
1508 * We do not open the interrupt pipe as an interrupt pipe, but as a
1509 * normal bulk endpoint. We send an IN transfer down the wire at the
1510 * appropriate time, because we know exactly when to expect data on
1511 * that endpoint. This saves bandwidth, but more important, makes the
1512 * code for handling the data on that endpoint simpler. No data
1513 * arriving concurently.
1515 if (sc->proto & UMASS_PROTO_CBI_I) {
1516 err = usbd_open_pipe(sc->iface, sc->intrin,
1517 USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
1519 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
1520 device_get_nameunit(sc->sc_dev), sc->intrin));
1526 /* initialisation of generic part */
1527 sc->transfer_state = TSTATE_ATTACH;
1529 /* request a sufficient number of xfer handles */
1530 for (i = 0; i < XFER_NR; i++) {
1531 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
1532 if (!sc->transfer_xfer[i]) {
1533 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
1534 device_get_nameunit(sc->sc_dev)));
1541 * Preallocate buffers to avoid auto-allocation from an interrupt
1544 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_DATA],
1546 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_CBW],
1547 UMASS_BBB_CBW_SIZE);
1548 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_CSW1],
1549 UMASS_BBB_CSW_SIZE);
1550 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_CSW2],
1551 UMASS_BBB_CSW_SIZE);
1552 usbd_alloc_buffer(sc->transfer_xfer[XFER_CBI_DATA],
1555 /* Initialise the wire protocol specific methods */
1556 if (sc->proto & UMASS_PROTO_BBB) {
1557 sc->reset = umass_bbb_reset;
1558 sc->transfer = umass_bbb_transfer;
1559 sc->state = umass_bbb_state;
1560 } else if (sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I)) {
1561 sc->reset = umass_cbi_reset;
1562 sc->transfer = umass_cbi_transfer;
1563 sc->state = umass_cbi_state;
1566 panic("%s:%d: Unknown proto 0x%02x",
1567 __FILE__, __LINE__, sc->proto);
1571 if (sc->proto & UMASS_PROTO_SCSI)
1572 sc->transform = umass_scsi_transform;
1573 else if (sc->proto & UMASS_PROTO_UFI)
1574 sc->transform = umass_ufi_transform;
1575 else if (sc->proto & UMASS_PROTO_ATAPI)
1576 sc->transform = umass_atapi_transform;
1577 else if (sc->proto & UMASS_PROTO_RBC)
1578 sc->transform = umass_rbc_transform;
1581 panic("No transformation defined for command proto 0x%02x",
1582 sc->proto & UMASS_PROTO_COMMAND);
1585 /* From here onwards the device can be used. */
1587 if (sc->quirks & SHUTTLE_INIT)
1588 umass_init_shuttle(sc);
1590 /* Get the maximum LUN supported by the device.
1592 if (((sc->proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1593 !(sc->quirks & NO_GETMAXLUN))
1594 sc->maxlun = umass_bbb_get_max_lun(sc);
1598 if ((sc->proto & UMASS_PROTO_SCSI) ||
1599 (sc->proto & UMASS_PROTO_ATAPI) ||
1600 (sc->proto & UMASS_PROTO_UFI) ||
1601 (sc->proto & UMASS_PROTO_RBC)) {
1602 /* Prepare the SCSI command block */
1603 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1604 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1606 /* register the SIM */
1607 err = umass_cam_attach_sim(sc);
1612 /* scan the new sim */
1613 err = umass_cam_attach(sc);
1615 umass_cam_detach_sim(sc);
1620 panic("%s:%d: Unknown proto 0x%02x",
1621 __FILE__, __LINE__, sc->proto);
1624 sc->transfer_state = TSTATE_IDLE;
1625 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", device_get_nameunit(sc->sc_dev)));
1631 umass_detach(device_t self)
1633 struct umass_softc *sc = device_get_softc(self);
1638 DPRINTF(UDMASS_USB, ("%s: detached\n", device_get_nameunit(sc->sc_dev)));
1641 * Set UMASS_FLAGS_GONE to prevent any new transfers from being
1642 * queued, and abort any transfers in progress to ensure that
1643 * pending requests (e.g. from CAM's bus scan) are terminated.
1645 sc->flags |= UMASS_FLAGS_GONE;
1647 if (sc->bulkout_pipe)
1648 usbd_abort_pipe(sc->bulkout_pipe);
1649 if (sc->bulkin_pipe)
1650 usbd_abort_pipe(sc->bulkin_pipe);
1651 if (sc->intrin_pipe)
1652 usbd_abort_pipe(sc->intrin_pipe);
1655 * Wait until we go idle to make sure that all of our xfer requests
1656 * have finished. We could be in the middle of a BBB reset (which
1657 * would not be effected by the pipe aborts above).
1660 while (sc->transfer_state != TSTATE_IDLE) {
1661 kprintf("%s: state %d waiting for idle\n",
1662 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1663 tsleep(sc, 0, "umassidl", to);
1664 if (to >= hz * 10) {
1665 kprintf("%s: state %d giving up!\n",
1666 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1672 if ((sc->proto & UMASS_PROTO_SCSI) ||
1673 (sc->proto & UMASS_PROTO_ATAPI) ||
1674 (sc->proto & UMASS_PROTO_UFI) ||
1675 (sc->proto & UMASS_PROTO_RBC)) {
1676 /* detach the SCSI host controller (SIM) */
1677 err = umass_cam_detach_sim(sc);
1680 for (i = 0; i < XFER_NR; i++) {
1681 if (sc->transfer_xfer[i])
1682 usbd_free_xfer(sc->transfer_xfer[i]);
1685 /* remove all the pipes */
1686 if (sc->bulkout_pipe)
1687 usbd_close_pipe(sc->bulkout_pipe);
1688 if (sc->bulkin_pipe)
1689 usbd_close_pipe(sc->bulkin_pipe);
1690 if (sc->intrin_pipe)
1691 usbd_close_pipe(sc->intrin_pipe);
1697 umass_init_shuttle(struct umass_softc *sc)
1699 usb_device_request_t req;
1702 /* The Linux driver does this, but no one can tell us what the
1705 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1706 req.bRequest = 1; /* XXX unknown command */
1707 USETW(req.wValue, 0);
1708 USETW(req.wIndex, sc->ifaceno);
1709 USETW(req.wLength, sizeof status);
1710 (void) usbd_do_request(sc->sc_udev, &req, &status);
1712 DPRINTF(UDMASS_GEN, ("%s: Shuttle init returned 0x%02x%02x\n",
1713 device_get_nameunit(sc->sc_dev), status[0], status[1]));
1717 * Generic functions to handle transfers
1721 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1722 void *buffer, int buflen, int flags,
1723 usbd_xfer_handle xfer)
1727 /* Initialiase a USB transfer and then schedule it */
1729 (void) usbd_setup_xfer(xfer, pipe, (void *) sc, buffer, buflen, flags,
1730 sc->timeout, sc->state);
1732 err = usbd_transfer(xfer);
1733 if (err && err != USBD_IN_PROGRESS) {
1734 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1735 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1739 return (USBD_NORMAL_COMPLETION);
1744 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle udev,
1745 usb_device_request_t *req,
1746 void *buffer, int buflen, int flags,
1747 usbd_xfer_handle xfer)
1751 /* Initialiase a USB control transfer and then schedule it */
1753 (void) usbd_setup_default_xfer(xfer, udev, (void *) sc,
1754 UMASS_TIMEOUT, req, buffer, buflen, flags, sc->state);
1756 err = usbd_transfer(xfer);
1757 if (err && err != USBD_IN_PROGRESS) {
1758 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1759 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1761 /* do not reset, as this would make us loop */
1765 return (USBD_NORMAL_COMPLETION);
1769 umass_clear_endpoint_stall(struct umass_softc *sc,
1770 u_int8_t endpt, usbd_pipe_handle pipe,
1771 int state, usbd_xfer_handle xfer)
1773 usbd_device_handle udev;
1775 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1776 device_get_nameunit(sc->sc_dev), endpt));
1778 usbd_interface2device_handle(sc->iface, &udev);
1780 sc->transfer_state = state;
1782 usbd_clear_endpoint_toggle(pipe);
1784 sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1785 sc->request.bRequest = UR_CLEAR_FEATURE;
1786 USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1787 USETW(sc->request.wIndex, endpt);
1788 USETW(sc->request.wLength, 0);
1789 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0, xfer);
1793 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1795 sc->transfer_cb = cb;
1796 sc->transfer_priv = priv;
1798 /* The reset is a forced reset, so no error (yet) */
1799 sc->reset(sc, STATUS_CMD_OK);
1803 * Bulk protocol specific functions
1807 umass_bbb_reset(struct umass_softc *sc, int status)
1809 usbd_device_handle udev;
1811 KASSERT(sc->proto & UMASS_PROTO_BBB,
1812 ("%s: umass_bbb_reset: wrong sc->proto 0x%02x\n",
1813 device_get_nameunit(sc->sc_dev), sc->proto));
1816 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1818 * For Reset Recovery the host shall issue in the following order:
1819 * a) a Bulk-Only Mass Storage Reset
1820 * b) a Clear Feature HALT to the Bulk-In endpoint
1821 * c) a Clear Feature HALT to the Bulk-Out endpoint
1823 * This is done in 3 steps, states:
1828 * If the reset doesn't succeed, the device should be port reset.
1831 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1832 device_get_nameunit(sc->sc_dev)));
1834 sc->transfer_state = TSTATE_BBB_RESET1;
1835 sc->transfer_status = status;
1837 usbd_interface2device_handle(sc->iface, &udev);
1839 /* reset is a class specific interface write */
1840 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1841 sc->request.bRequest = UR_BBB_RESET;
1842 USETW(sc->request.wValue, 0);
1843 USETW(sc->request.wIndex, sc->ifaceno);
1844 USETW(sc->request.wLength, 0);
1845 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0,
1846 sc->transfer_xfer[XFER_BBB_RESET1]);
1850 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1851 void *data, int datalen, int dir, u_int timeout,
1852 transfer_cb_f cb, void *priv)
1854 KASSERT(sc->proto & UMASS_PROTO_BBB,
1855 ("%s: umass_bbb_transfer: wrong sc->proto 0x%02x\n",
1856 device_get_nameunit(sc->sc_dev), sc->proto));
1857 /* Be a little generous. */
1858 sc->timeout = timeout + UMASS_TIMEOUT;
1861 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1862 * a data phase of datalen bytes from/to the device and finally a
1864 * If the data direction was inbound a maximum of datalen bytes
1865 * is stored in the buffer pointed to by data.
1867 * umass_bbb_transfer initialises the transfer and lets the state
1868 * machine in umass_bbb_state handle the completion. It uses the
1870 * TSTATE_BBB_COMMAND
1871 * -> TSTATE_BBB_DATA
1872 * -> TSTATE_BBB_STATUS
1873 * -> TSTATE_BBB_STATUS2
1874 * -> TSTATE_BBB_IDLE
1876 * An error in any of those states will invoke
1880 /* check the given arguments */
1881 KASSERT(datalen == 0 || data != NULL,
1882 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
1883 KASSERT(cmdlen <= CBWCDBLENGTH,
1884 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1885 device_get_nameunit(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1886 KASSERT(dir == DIR_NONE || datalen > 0,
1887 ("%s: datalen == 0 while direction is not NONE\n",
1888 device_get_nameunit(sc->sc_dev)));
1889 KASSERT(datalen == 0 || dir != DIR_NONE,
1890 ("%s: direction is NONE while datalen is not zero\n",
1891 device_get_nameunit(sc->sc_dev)));
1892 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1893 ("%s: CBW struct does not have the right size (%ld vs. %d)\n",
1894 device_get_nameunit(sc->sc_dev),
1895 (long)sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1896 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1897 ("%s: CSW struct does not have the right size (%ld vs. %d)\n",
1898 device_get_nameunit(sc->sc_dev),
1899 (long)sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1902 * Determine the direction of the data transfer and the length.
1904 * dCBWDataTransferLength (datalen) :
1905 * This field indicates the number of bytes of data that the host
1906 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1907 * the Direction bit) during the execution of this command. If this
1908 * field is set to 0, the device will expect that no data will be
1909 * transferred IN or OUT during this command, regardless of the value
1910 * of the Direction bit defined in dCBWFlags.
1913 * The bits of the Flags field are defined as follows:
1915 * Bit 7 Direction - this bit shall be ignored if the
1916 * dCBWDataTransferLength field is zero.
1917 * 0 = data Out from host to device
1918 * 1 = data In from device to host
1921 /* Fill in the Command Block Wrapper
1922 * We fill in all the fields, so there is no need to bzero it first.
1924 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1925 /* We don't care about the initial value, as long as the values are unique */
1926 USETDW(sc->cbw.dCBWTag, UGETDW(sc->cbw.dCBWTag) + 1);
1927 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1928 /* DIR_NONE is treated as DIR_OUT (0x00) */
1929 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1930 sc->cbw.bCBWLUN = lun;
1931 sc->cbw.bCDBLength = cmdlen;
1932 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1934 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1936 /* store the details for the data transfer phase */
1937 sc->transfer_dir = dir;
1938 sc->transfer_data = data;
1939 sc->transfer_datalen = datalen;
1940 sc->transfer_actlen = 0;
1941 sc->transfer_cb = cb;
1942 sc->transfer_priv = priv;
1943 sc->transfer_status = STATUS_CMD_OK;
1945 /* move from idle to the command state */
1946 sc->transfer_state = TSTATE_BBB_COMMAND;
1948 /* Send the CBW from host to device via bulk-out endpoint. */
1949 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1950 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1951 sc->transfer_xfer[XFER_BBB_CBW])) {
1952 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1958 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1961 struct umass_softc *sc = (struct umass_softc *) priv;
1962 usbd_xfer_handle next_xfer;
1964 KASSERT(sc->proto & UMASS_PROTO_BBB,
1965 ("%s: umass_bbb_state: wrong sc->proto 0x%02x\n",
1966 device_get_nameunit(sc->sc_dev), sc->proto));
1969 * State handling for BBB transfers.
1971 * The subroutine is rather long. It steps through the states given in
1972 * Annex A of the Bulk-Only specification.
1973 * Each state first does the error handling of the previous transfer
1974 * and then prepares the next transfer.
1975 * Each transfer is done asynchroneously so after the request/transfer
1976 * has been submitted you will find a 'return;'.
1979 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1980 device_get_nameunit(sc->sc_dev), sc->transfer_state,
1981 states[sc->transfer_state], xfer, usbd_errstr(err)));
1983 switch (sc->transfer_state) {
1985 /***** Bulk Transfer *****/
1986 case TSTATE_BBB_COMMAND:
1987 /* Command transport phase, error handling */
1989 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1990 device_get_nameunit(sc->sc_dev)));
1991 /* If the device detects that the CBW is invalid, then
1992 * the device may STALL both bulk endpoints and require
1995 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1999 /* Data transport phase, setup transfer */
2000 sc->transfer_state = TSTATE_BBB_DATA;
2001 if (sc->transfer_dir == DIR_IN) {
2002 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2003 sc->transfer_data, sc->transfer_datalen,
2005 sc->transfer_xfer[XFER_BBB_DATA]))
2006 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2009 } else if (sc->transfer_dir == DIR_OUT) {
2010 if (umass_setup_transfer(sc, sc->bulkout_pipe,
2011 sc->transfer_data, sc->transfer_datalen,
2012 0, /* fixed length transfer */
2013 sc->transfer_xfer[XFER_BBB_DATA]))
2014 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2018 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
2019 device_get_nameunit(sc->sc_dev)));
2022 /* FALLTHROUGH if no data phase, err == 0 */
2023 case TSTATE_BBB_DATA:
2024 /* Command transport phase, error handling (ignored if no data
2025 * phase (fallthrough from previous state)) */
2026 if (sc->transfer_dir != DIR_NONE) {
2027 /* retrieve the length of the transfer that was done */
2028 usbd_get_xfer_status(xfer, NULL, NULL,
2029 &sc->transfer_actlen, NULL);
2032 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
2033 "%s\n", device_get_nameunit(sc->sc_dev),
2034 (sc->transfer_dir == DIR_IN?"in":"out"),
2035 sc->transfer_datalen,usbd_errstr(err)));
2037 if (err == USBD_STALLED) {
2038 umass_clear_endpoint_stall(sc,
2039 (sc->transfer_dir == DIR_IN?
2040 sc->bulkin:sc->bulkout),
2041 (sc->transfer_dir == DIR_IN?
2042 sc->bulkin_pipe:sc->bulkout_pipe),
2044 sc->transfer_xfer[XFER_BBB_DCLEAR]);
2047 /* Unless the error is a pipe stall the
2050 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
2056 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
2057 umass_dump_buffer(sc, sc->transfer_data,
2058 sc->transfer_datalen, 48));
2062 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
2063 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
2064 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
2065 /* Reading of CSW after bulk stall condition in data phase
2066 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
2067 * reading CSW (TSTATE_BBB_SCLEAR).
2068 * In the case of no data phase or successfull data phase,
2069 * err == 0 and the following if block is passed.
2071 if (err) { /* should not occur */
2072 /* try the transfer below, even if clear stall failed */
2073 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
2074 ", %s\n", device_get_nameunit(sc->sc_dev),
2075 (sc->transfer_dir == DIR_IN? "in":"out"),
2077 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2081 /* Status transport phase, setup transfer */
2082 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
2083 sc->transfer_state == TSTATE_BBB_DATA ||
2084 sc->transfer_state == TSTATE_BBB_DCLEAR) {
2085 /* After no data phase, successfull data phase and
2086 * after clearing bulk-in/-out stall condition
2088 sc->transfer_state = TSTATE_BBB_STATUS1;
2089 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
2091 /* After first attempt of fetching CSW */
2092 sc->transfer_state = TSTATE_BBB_STATUS2;
2093 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
2096 /* Read the Command Status Wrapper via bulk-in endpoint. */
2097 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2098 &sc->csw, UMASS_BBB_CSW_SIZE, 0,
2100 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2105 case TSTATE_BBB_STATUS1: /* first attempt */
2106 case TSTATE_BBB_STATUS2: /* second attempt */
2107 /* Status transfer, error handling */
2111 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
2112 device_get_nameunit(sc->sc_dev), usbd_errstr(err),
2113 (sc->transfer_state == TSTATE_BBB_STATUS1?
2116 /* If this was the first attempt at fetching the CSW
2117 * retry it, otherwise fail.
2119 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
2120 umass_clear_endpoint_stall(sc,
2121 sc->bulkin, sc->bulkin_pipe,
2123 sc->transfer_xfer[XFER_BBB_SCLEAR]);
2126 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2131 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2133 /* Translate weird command-status signatures. */
2134 if ((sc->quirks & WRONG_CSWSIG) &&
2135 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
2136 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2138 Residue = UGETDW(sc->csw.dCSWDataResidue);
2140 sc->transfer_datalen - sc->transfer_actlen != 0)
2141 Residue = sc->transfer_datalen - sc->transfer_actlen;
2143 /* Check CSW and handle any error */
2144 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2145 /* Invalid CSW: Wrong signature or wrong tag might
2146 * indicate that the device is confused -> reset it.
2148 kprintf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
2149 device_get_nameunit(sc->sc_dev),
2150 UGETDW(sc->csw.dCSWSignature),
2153 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2155 } else if (UGETDW(sc->csw.dCSWTag)
2156 != UGETDW(sc->cbw.dCBWTag)) {
2157 kprintf("%s: Invalid CSW: tag %d should be %d\n",
2158 device_get_nameunit(sc->sc_dev),
2159 UGETDW(sc->csw.dCSWTag),
2160 UGETDW(sc->cbw.dCBWTag));
2162 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2165 /* CSW is valid here */
2166 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2167 kprintf("%s: Invalid CSW: status %d > %d\n",
2168 device_get_nameunit(sc->sc_dev),
2172 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2174 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2175 kprintf("%s: Phase Error, residue = %d\n",
2176 device_get_nameunit(sc->sc_dev), Residue);
2178 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2181 } else if (sc->transfer_actlen > sc->transfer_datalen) {
2182 /* Buffer overrun! Don't let this go by unnoticed */
2183 panic("%s: transferred %db instead of %db",
2184 device_get_nameunit(sc->sc_dev),
2185 sc->transfer_actlen, sc->transfer_datalen);
2187 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2188 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
2189 device_get_nameunit(sc->sc_dev), Residue));
2191 /* SCSI command failed but transfer was succesful */
2192 sc->transfer_state = TSTATE_IDLE;
2193 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2197 } else { /* success */
2198 sc->transfer_state = TSTATE_IDLE;
2199 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2206 /***** Bulk Reset *****/
2207 case TSTATE_BBB_RESET1:
2209 kprintf("%s: BBB reset failed, %s\n",
2210 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2212 umass_clear_endpoint_stall(sc,
2213 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
2214 sc->transfer_xfer[XFER_BBB_RESET2]);
2217 case TSTATE_BBB_RESET2:
2218 if (err) /* should not occur */
2219 kprintf("%s: BBB bulk-in clear stall failed, %s\n",
2220 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2221 /* no error recovery, otherwise we end up in a loop */
2223 umass_clear_endpoint_stall(sc,
2224 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
2225 sc->transfer_xfer[XFER_BBB_RESET3]);
2228 case TSTATE_BBB_RESET3:
2229 if (err) /* should not occur */
2230 kprintf("%s: BBB bulk-out clear stall failed, %s\n",
2231 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2232 /* no error recovery, otherwise we end up in a loop */
2234 sc->transfer_state = TSTATE_IDLE;
2235 if (sc->transfer_priv) {
2236 sc->transfer_cb(sc, sc->transfer_priv,
2237 sc->transfer_datalen,
2238 sc->transfer_status);
2243 /***** Default *****/
2245 panic("%s: Unknown state %d",
2246 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2251 umass_bbb_get_max_lun(struct umass_softc *sc)
2253 usbd_device_handle udev;
2254 usb_device_request_t req;
2256 usb_interface_descriptor_t *id;
2260 usbd_interface2device_handle(sc->iface, &udev);
2261 id = usbd_get_interface_descriptor(sc->iface);
2263 /* The Get Max Lun command is a class-specific request. */
2264 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2265 req.bRequest = UR_BBB_GET_MAX_LUN;
2266 USETW(req.wValue, 0);
2267 USETW(req.wIndex, id->bInterfaceNumber);
2268 USETW(req.wLength, 1);
2270 err = usbd_do_request(udev, &req, &buf);
2272 case USBD_NORMAL_COMPLETION:
2274 DPRINTF(UDMASS_BBB, ("%s: Max Lun is %d\n",
2275 device_get_nameunit(sc->sc_dev), maxlun));
2278 case USBD_SHORT_XFER:
2280 /* Device doesn't support Get Max Lun request. */
2281 kprintf("%s: Get Max Lun not supported (%s)\n",
2282 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2283 /* XXX Should we port_reset the device? */
2291 * Command/Bulk/Interrupt (CBI) specific functions
2295 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
2296 usbd_xfer_handle xfer)
2298 usbd_device_handle udev;
2300 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2301 ("%s: umass_cbi_adsc: wrong sc->proto 0x%02x\n",
2302 device_get_nameunit(sc->sc_dev), sc->proto));
2304 usbd_interface2device_handle(sc->iface, &udev);
2306 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2307 sc->request.bRequest = UR_CBI_ADSC;
2308 USETW(sc->request.wValue, 0);
2309 USETW(sc->request.wIndex, sc->ifaceno);
2310 USETW(sc->request.wLength, buflen);
2311 return umass_setup_ctrl_transfer(sc, udev, &sc->request, buffer,
2317 umass_cbi_reset(struct umass_softc *sc, int status)
2320 # define SEND_DIAGNOSTIC_CMDLEN 12
2322 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2323 ("%s: umass_cbi_reset: wrong sc->proto 0x%02x\n",
2324 device_get_nameunit(sc->sc_dev), sc->proto));
2327 * Command Block Reset Protocol
2329 * First send a reset request to the device. Then clear
2330 * any possibly stalled bulk endpoints.
2332 * This is done in 3 steps, states:
2337 * If the reset doesn't succeed, the device should be port reset.
2340 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
2341 device_get_nameunit(sc->sc_dev)));
2343 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
2344 ("%s: CBL struct is too small (%ld < %d)\n",
2345 device_get_nameunit(sc->sc_dev),
2346 (long)sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
2348 sc->transfer_state = TSTATE_CBI_RESET1;
2349 sc->transfer_status = status;
2351 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
2352 * the two the last 10 bytes of the cbl is filled with 0xff (section
2353 * 2.2 of the CBI spec).
2355 sc->cbl[0] = 0x1d; /* Command Block Reset */
2357 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
2360 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
2361 sc->transfer_xfer[XFER_CBI_RESET1]);
2362 /* XXX if the command fails we should reset the port on the bub */
2366 umass_cbi_transfer(struct umass_softc *sc, int lun,
2367 void *cmd, int cmdlen, void *data, int datalen, int dir,
2368 u_int timeout, transfer_cb_f cb, void *priv)
2370 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2371 ("%s: umass_cbi_transfer: wrong sc->proto 0x%02x\n",
2372 device_get_nameunit(sc->sc_dev), sc->proto));
2373 /* Be a little generous. */
2374 sc->timeout = timeout + UMASS_TIMEOUT;
2377 * Do a CBI transfer with cmdlen bytes from cmd, possibly
2378 * a data phase of datalen bytes from/to the device and finally a
2380 * If the data direction was inbound a maximum of datalen bytes
2381 * is stored in the buffer pointed to by data.
2383 * umass_cbi_transfer initialises the transfer and lets the state
2384 * machine in umass_cbi_state handle the completion. It uses the
2386 * TSTATE_CBI_COMMAND
2389 * An error in any of those states will invoke
2393 /* check the given arguments */
2394 KASSERT(datalen == 0 || data != NULL,
2395 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
2396 KASSERT(datalen == 0 || dir != DIR_NONE,
2397 ("%s: direction is NONE while datalen is not zero\n",
2398 device_get_nameunit(sc->sc_dev)));
2400 /* store the details for the data transfer phase */
2401 sc->transfer_dir = dir;
2402 sc->transfer_data = data;
2403 sc->transfer_datalen = datalen;
2404 sc->transfer_actlen = 0;
2405 sc->transfer_cb = cb;
2406 sc->transfer_priv = priv;
2407 sc->transfer_status = STATUS_CMD_OK;
2409 /* move from idle to the command state */
2410 sc->transfer_state = TSTATE_CBI_COMMAND;
2412 DIF(UDMASS_CBI, umass_cbi_dump_cmd(sc, cmd, cmdlen));
2414 /* Send the Command Block from host to device via control endpoint. */
2415 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
2416 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2420 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
2423 struct umass_softc *sc = (struct umass_softc *) priv;
2425 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2426 ("%s: umass_cbi_state: wrong sc->proto 0x%02x\n",
2427 device_get_nameunit(sc->sc_dev), sc->proto));
2430 * State handling for CBI transfers.
2433 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
2434 device_get_nameunit(sc->sc_dev), sc->transfer_state,
2435 states[sc->transfer_state], xfer, usbd_errstr(err)));
2437 switch (sc->transfer_state) {
2439 /***** CBI Transfer *****/
2440 case TSTATE_CBI_COMMAND:
2441 if (err == USBD_STALLED) {
2442 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
2443 device_get_nameunit(sc->sc_dev)));
2444 /* Status transport by control pipe (section 2.3.2.1).
2445 * The command contained in the command block failed.
2447 * The control pipe has already been unstalled by the
2449 * Section 2.4.3.1.1 states that the bulk in endpoints
2450 * should not be stalled at this point.
2453 sc->transfer_state = TSTATE_IDLE;
2454 sc->transfer_cb(sc, sc->transfer_priv,
2455 sc->transfer_datalen,
2460 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
2461 device_get_nameunit(sc->sc_dev)));
2462 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2467 sc->transfer_state = TSTATE_CBI_DATA;
2468 if (sc->transfer_dir == DIR_IN) {
2469 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2470 sc->transfer_data, sc->transfer_datalen,
2472 sc->transfer_xfer[XFER_CBI_DATA]))
2473 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2475 } else if (sc->transfer_dir == DIR_OUT) {
2476 if (umass_setup_transfer(sc, sc->bulkout_pipe,
2477 sc->transfer_data, sc->transfer_datalen,
2478 0, /* fixed length transfer */
2479 sc->transfer_xfer[XFER_CBI_DATA]))
2480 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2482 } else if (sc->proto & UMASS_PROTO_CBI_I) {
2483 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2484 device_get_nameunit(sc->sc_dev)));
2485 sc->transfer_state = TSTATE_CBI_STATUS;
2486 if (umass_setup_transfer(sc, sc->intrin_pipe,
2487 &sc->sbl, sizeof(sc->sbl),
2488 0, /* fixed length transfer */
2489 sc->transfer_xfer[XFER_CBI_STATUS])){
2490 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2493 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2494 device_get_nameunit(sc->sc_dev)));
2495 /* No command completion interrupt. Request
2498 sc->transfer_state = TSTATE_IDLE;
2499 sc->transfer_cb(sc, sc->transfer_priv,
2500 0, STATUS_CMD_UNKNOWN);
2505 case TSTATE_CBI_DATA:
2506 /* retrieve the length of the transfer that was done */
2507 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
2510 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
2511 "%s\n", device_get_nameunit(sc->sc_dev),
2512 (sc->transfer_dir == DIR_IN?"in":"out"),
2513 sc->transfer_datalen,usbd_errstr(err)));
2515 if (err == USBD_STALLED) {
2516 umass_clear_endpoint_stall(sc,
2517 sc->bulkin, sc->bulkin_pipe,
2519 sc->transfer_xfer[XFER_CBI_DCLEAR]);
2521 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2526 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2527 umass_dump_buffer(sc, sc->transfer_data,
2528 sc->transfer_actlen, 48));
2530 if (sc->proto & UMASS_PROTO_CBI_I) {
2531 sc->transfer_state = TSTATE_CBI_STATUS;
2532 if (umass_setup_transfer(sc, sc->intrin_pipe,
2533 &sc->sbl, sizeof(sc->sbl),
2534 0, /* fixed length transfer */
2535 sc->transfer_xfer[XFER_CBI_STATUS])){
2536 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2539 /* No command completion interrupt. Request
2540 * sense to get status of command.
2542 sc->transfer_state = TSTATE_IDLE;
2543 sc->transfer_cb(sc, sc->transfer_priv,
2544 sc->transfer_datalen - sc->transfer_actlen,
2545 STATUS_CMD_UNKNOWN);
2549 case TSTATE_CBI_STATUS:
2551 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2552 device_get_nameunit(sc->sc_dev)));
2553 /* Status transport by interrupt pipe (section 2.3.2.2).
2556 if (err == USBD_STALLED) {
2557 umass_clear_endpoint_stall(sc,
2558 sc->intrin, sc->intrin_pipe,
2560 sc->transfer_xfer[XFER_CBI_SCLEAR]);
2562 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2567 /* Dissect the information in the buffer */
2569 if (sc->proto & UMASS_PROTO_UFI) {
2572 /* Section 3.4.3.1.3 specifies that the UFI command
2573 * protocol returns an ASC and ASCQ in the interrupt
2577 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2579 device_get_nameunit(sc->sc_dev),
2580 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2582 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2583 status = STATUS_CMD_OK;
2585 status = STATUS_CMD_FAILED;
2587 sc->transfer_state = TSTATE_IDLE;
2588 sc->transfer_cb(sc, sc->transfer_priv,
2589 sc->transfer_datalen - sc->transfer_actlen,
2592 /* Command Interrupt Data Block */
2593 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2594 device_get_nameunit(sc->sc_dev),
2595 sc->sbl.common.type, sc->sbl.common.value));
2597 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2600 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2601 == IDB_VALUE_PASS) {
2602 err = STATUS_CMD_OK;
2603 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2604 == IDB_VALUE_FAIL ||
2605 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2606 == IDB_VALUE_PERSISTENT) {
2607 err = STATUS_CMD_FAILED;
2609 err = STATUS_WIRE_FAILED;
2612 sc->transfer_state = TSTATE_IDLE;
2613 sc->transfer_cb(sc, sc->transfer_priv,
2614 sc->transfer_datalen-sc->transfer_actlen,
2620 case TSTATE_CBI_DCLEAR:
2621 if (err) { /* should not occur */
2622 kprintf("%s: CBI bulk-in/out stall clear failed, %s\n",
2623 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2624 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2627 sc->transfer_state = TSTATE_IDLE;
2628 sc->transfer_cb(sc, sc->transfer_priv,
2629 sc->transfer_datalen,
2633 case TSTATE_CBI_SCLEAR:
2634 if (err) /* should not occur */
2635 kprintf("%s: CBI intr-in stall clear failed, %s\n",
2636 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2638 /* Something really bad is going on. Reset the device */
2639 umass_cbi_reset(sc, STATUS_CMD_FAILED);
2642 /***** CBI Reset *****/
2643 case TSTATE_CBI_RESET1:
2645 kprintf("%s: CBI reset failed, %s\n",
2646 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2648 umass_clear_endpoint_stall(sc,
2649 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2650 sc->transfer_xfer[XFER_CBI_RESET2]);
2653 case TSTATE_CBI_RESET2:
2654 if (err) /* should not occur */
2655 kprintf("%s: CBI bulk-in stall clear failed, %s\n",
2656 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2657 /* no error recovery, otherwise we end up in a loop */
2659 umass_clear_endpoint_stall(sc,
2660 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2661 sc->transfer_xfer[XFER_CBI_RESET3]);
2664 case TSTATE_CBI_RESET3:
2665 if (err) /* should not occur */
2666 kprintf("%s: CBI bulk-out stall clear failed, %s\n",
2667 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2668 /* no error recovery, otherwise we end up in a loop */
2670 sc->transfer_state = TSTATE_IDLE;
2671 if (sc->transfer_priv) {
2672 sc->transfer_cb(sc, sc->transfer_priv,
2673 sc->transfer_datalen,
2674 sc->transfer_status);
2680 /***** Default *****/
2682 panic("%s: Unknown state %d",
2683 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2691 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2695 umass_cam_attach_sim(struct umass_softc *sc)
2697 struct cam_devq *devq; /* Per device Queue */
2699 /* A HBA is attached to the CAM layer.
2701 * The CAM layer will then after a while start probing for
2702 * devices on the bus. The number of SIMs is limited to one.
2705 callout_init(&sc->rescan_timeout);
2706 devq = cam_simq_alloc(1 /*maximum openings*/);
2710 sc->umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll,
2713 device_get_unit(sc->sc_dev) /*unit number*/,
2715 1 /*maximum device openings*/,
2716 0 /*maximum tagged device openings*/,
2718 cam_simq_release(devq);
2719 if (sc->umass_sim == NULL)
2723 * If we could not register the bus we must immediately free the
2724 * sim so we do not attempt to deregister a bus later on that we
2725 * had not registered.
2727 if (xpt_bus_register(sc->umass_sim, device_get_unit(sc->sc_dev)) !=
2729 cam_sim_free(sc->umass_sim);
2730 sc->umass_sim = NULL;
2738 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2741 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2742 DPRINTF(UDMASS_SCSI, ("%s:%d Rescan failed, 0x%04x\n",
2743 periph->periph_name, periph->unit_number,
2744 ccb->ccb_h.status));
2746 DPRINTF(UDMASS_SCSI, ("%s%d: Rescan succeeded\n",
2747 periph->periph_name, periph->unit_number));
2751 xpt_free_path(ccb->ccb_h.path);
2752 kfree(ccb, M_USBDEV);
2756 * Rescan the SCSI bus to detect newly added devices. We use
2757 * an async rescan to avoid reentrancy issues.
2760 umass_cam_rescan(void *addr)
2762 struct umass_softc *sc = (struct umass_softc *) addr;
2763 struct cam_path *path;
2766 ccb = kmalloc(sizeof(union ccb), M_USBDEV, M_INTWAIT|M_ZERO);
2768 DPRINTF(UDMASS_SCSI, ("scbus%d: scanning for %s:%d:%d:%d\n",
2769 cam_sim_path(sc->umass_sim),
2770 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2771 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD));
2773 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->umass_sim),
2774 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2776 kfree(ccb, M_USBDEV);
2780 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2781 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2782 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2783 ccb->crcn.flags = CAM_FLAG_NONE;
2784 xpt_action_async(ccb);
2786 /* The scan is in progress now. */
2790 umass_cam_attach(struct umass_softc *sc)
2795 kprintf("%s:%d:%d:%d: Attached to scbus%d\n",
2796 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2797 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD,
2798 cam_sim_path(sc->umass_sim));
2802 * Notify CAM of the new device after a 0.2 second delay. Any
2803 * failure is benign, as the user can still do it by hand
2804 * (camcontrol rescan <busno>). Only do this if we are not
2805 * booting, because CAM does a scan after booting has
2806 * completed, when interrupts have been enabled.
2808 callout_reset(&sc->rescan_timeout, MS_TO_TICKS(200),
2809 umass_cam_rescan, sc);
2812 return(0); /* always succesfull */
2816 * detach from the CAM layer
2820 umass_cam_detach_sim(struct umass_softc *sc)
2822 callout_stop(&sc->rescan_timeout);
2823 if (sc->umass_sim) {
2824 xpt_bus_deregister(cam_sim_path(sc->umass_sim));
2825 cam_sim_free(sc->umass_sim);
2827 sc->umass_sim = NULL;
2834 * CAM requests for action come through here
2838 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2840 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2842 /* The softc is still there, but marked as going away. umass_cam_detach
2843 * has not yet notified CAM of the lost device however.
2845 if (sc && (sc->flags & UMASS_FLAGS_GONE)) {
2846 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2847 "Invalid target (gone)\n",
2848 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2849 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2850 ccb->ccb_h.func_code));
2851 ccb->ccb_h.status = CAM_TID_INVALID;
2856 /* Verify, depending on the operation to perform, that we either got a
2857 * valid sc, because an existing target was referenced, or otherwise
2858 * the SIM is addressed.
2860 * This avoids bombing out at a kprintf and does give the CAM layer some
2861 * sensible feedback on errors.
2863 switch (ccb->ccb_h.func_code) {
2866 case XPT_GET_TRAN_SETTINGS:
2867 case XPT_SET_TRAN_SETTINGS:
2868 case XPT_CALC_GEOMETRY:
2869 /* the opcodes requiring a target. These should never occur. */
2871 kprintf("%s:%d:%d:%d:func_code 0x%04x: "
2872 "Invalid target (target needed)\n",
2873 DEVNAME_SIM, 0, ccb->ccb_h.target_id,
2874 ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
2876 ccb->ccb_h.status = CAM_TID_INVALID;
2883 /* The opcodes sometimes aimed at a target (sc is valid),
2884 * sometimes aimed at the SIM (sc is invalid and target is
2885 * CAM_TARGET_WILDCARD)
2887 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2888 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2889 "Invalid target (no wildcard)\n",
2890 DEVNAME_SIM, 0, ccb->ccb_h.target_id,
2891 ccb->ccb_h.target_lun, ccb->ccb_h.func_code));
2893 ccb->ccb_h.status = CAM_TID_INVALID;
2899 /* XXX Hm, we should check the input parameters */
2903 /* Perform the requested action */
2904 switch (ccb->ccb_h.func_code) {
2907 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2911 unsigned char *rcmd;
2914 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2915 "cmd: 0x%02x, flags: 0x%02x, "
2916 "%db cmd/%db data/%db sense\n",
2917 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2918 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2919 csio->cdb_io.cdb_bytes[0],
2920 ccb->ccb_h.flags & CAM_DIR_MASK,
2921 csio->cdb_len, csio->dxfer_len,
2924 /* clear the end of the buffer to make sure we don't send out
2927 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2929 umass_dump_buffer(sc, csio->data_ptr,
2930 csio->dxfer_len, 48));
2932 if (sc->transfer_state != TSTATE_IDLE) {
2933 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2934 "I/O in progress, deferring (state %d, %s)\n",
2935 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2936 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2937 sc->transfer_state,states[sc->transfer_state]));
2938 ccb->ccb_h.status = CAM_SCSI_BUSY;
2943 switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2954 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2957 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2958 cmd = (unsigned char *) csio->cdb_io.cdb_ptr;
2960 cmd = (unsigned char *) &csio->cdb_io.cdb_bytes;
2962 cmdlen = csio->cdb_len;
2963 rcmd = (unsigned char *) &sc->cam_scsi_command;
2964 rcmdlen = sizeof(sc->cam_scsi_command);
2966 /* sc->transform will convert the command to the command
2967 * (format) needed by the specific command set and return
2968 * the converted command in a buffer pointed to be rcmd.
2969 * We pass in a buffer, but if the command does not
2970 * have to be transformed it returns a ptr to the original
2971 * buffer (see umass_scsi_transform).
2974 if (sc->transform(sc, cmd, cmdlen, &rcmd, &rcmdlen)) {
2976 * Handle EVPD inquiry for broken devices first
2977 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2979 if ((sc->quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2980 rcmd[0] == INQUIRY && (rcmd[1] & SI_EVPD)) {
2981 struct scsi_sense_data *sense;
2983 sense = &ccb->csio.sense_data;
2984 bzero(sense, sizeof(*sense));
2985 sense->error_code = SSD_CURRENT_ERROR;
2986 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2987 sense->add_sense_code = 0x24;
2988 sense->extra_len = 10;
2989 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2990 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2995 /* Return fake inquiry data for broken devices */
2996 if ((sc->quirks & NO_INQUIRY) && rcmd[0] == INQUIRY) {
2997 struct ccb_scsiio *csio = &ccb->csio;
2999 memcpy(csio->data_ptr, &fake_inq_data,
3000 sizeof(fake_inq_data));
3001 csio->scsi_status = SCSI_STATUS_OK;
3002 ccb->ccb_h.status = CAM_REQ_CMP;
3006 if ((sc->quirks & FORCE_SHORT_INQUIRY) &&
3007 rcmd[0] == INQUIRY) {
3008 csio->dxfer_len = SHORT_INQUIRY_LENGTH;
3010 sc->transfer(sc, ccb->ccb_h.target_lun, rcmd, rcmdlen,
3012 csio->dxfer_len, dir, ccb->ccb_h.timeout,
3013 umass_cam_cb, (void *) ccb);
3015 ccb->ccb_h.status = CAM_REQ_INVALID;
3023 struct ccb_pathinq *cpi = &ccb->cpi;
3025 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
3026 (sc == NULL ? DEVNAME_SIM : device_get_nameunit(sc->sc_dev)),
3027 (sc == NULL ? 0 : cam_sim_path(sc->umass_sim)),
3028 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3030 /* host specific information */
3031 cpi->version_num = 1;
3032 cpi->hba_inquiry = 0;
3033 cpi->target_sprt = 0;
3034 cpi->hba_misc = PIM_NO_6_BYTE;
3035 cpi->hba_eng_cnt = 0;
3036 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
3037 cpi->initiator_id = UMASS_SCSIID_HOST;
3038 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3039 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
3040 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3041 cpi->unit_number = cam_sim_unit(sim);
3044 cpi->base_transfer_speed = 0;
3047 cpi->bus_id = device_get_unit(sc->sc_dev);
3049 if (sc->quirks & FLOPPY_SPEED) {
3050 cpi->base_transfer_speed =
3051 UMASS_FLOPPY_TRANSFER_SPEED;
3052 } else if (usbd_get_speed(sc->sc_udev) ==
3054 cpi->base_transfer_speed =
3055 UMASS_HIGH_TRANSFER_SPEED;
3057 cpi->base_transfer_speed =
3058 UMASS_FULL_TRANSFER_SPEED;
3060 cpi->max_lun = sc->maxlun;
3063 cpi->ccb_h.status = CAM_REQ_CMP;
3069 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
3070 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3071 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3073 ccb->ccb_h.status = CAM_REQ_INPROG;
3074 umass_reset(sc, umass_cam_cb, (void *) ccb);
3077 case XPT_GET_TRAN_SETTINGS:
3079 struct ccb_trans_settings *cts = &ccb->cts;
3080 cts->protocol = PROTO_SCSI;
3081 cts->protocol_version = SCSI_REV_2;
3082 cts->transport = XPORT_USB;
3083 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
3084 cts->xport_specific.valid = 0;
3086 ccb->ccb_h.status = CAM_REQ_CMP;
3090 case XPT_SET_TRAN_SETTINGS:
3092 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3093 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3094 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3096 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3100 case XPT_CALC_GEOMETRY:
3102 cam_calc_geometry(&ccb->ccg, /*extended*/1);
3108 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
3109 (sc == NULL ? DEVNAME_SIM : device_get_nameunit(sc->sc_dev)),
3110 (sc == NULL ? 0 : cam_sim_path(sc->umass_sim)),
3111 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3113 ccb->ccb_h.status = CAM_REQ_CMP;
3118 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
3119 "Not implemented\n",
3120 (sc == NULL ? DEVNAME_SIM : device_get_nameunit(sc->sc_dev)),
3121 (sc == NULL ? 0 : cam_sim_path(sc->umass_sim)),
3122 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
3123 ccb->ccb_h.func_code));
3125 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3132 umass_cam_poll(struct cam_sim *sim)
3134 struct umass_softc *sc = (struct umass_softc *) sim->softc;
3136 KKASSERT(sc != NULL);
3138 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
3139 device_get_nameunit(sc->sc_dev)));
3141 usbd_set_polling(sc->sc_udev, 1);
3142 usbd_dopoll(sc->iface);
3143 usbd_set_polling(sc->sc_udev, 0);
3148 * finalise a completed CAM command
3152 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
3154 union ccb *ccb = (union ccb *) priv;
3155 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3157 csio->resid = residue;
3161 ccb->ccb_h.status = CAM_REQ_CMP;
3162 if ((sc->quirks & READ_CAPACITY_OFFBY1) &&
3163 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3164 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3165 struct scsi_read_capacity_data *rcap;
3168 rcap = (struct scsi_read_capacity_data *)csio->data_ptr;
3169 maxsector = scsi_4btoul(rcap->addr) - 1;
3170 scsi_ulto4b(maxsector, rcap->addr);
3175 case STATUS_CMD_UNKNOWN:
3176 case STATUS_CMD_FAILED:
3177 switch (ccb->ccb_h.func_code) {
3180 unsigned char *rcmd;
3183 /* fetch sense data */
3184 /* the rest of the command was filled in at attach */
3185 sc->cam_scsi_sense.length = csio->sense_len;
3187 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
3188 device_get_nameunit(sc->sc_dev), csio->sense_len));
3190 rcmd = (unsigned char *) &sc->cam_scsi_command;
3191 rcmdlen = sizeof(sc->cam_scsi_command);
3193 if (sc->transform(sc,
3194 (unsigned char *) &sc->cam_scsi_sense,
3195 sizeof(sc->cam_scsi_sense),
3197 if ((sc->quirks & FORCE_SHORT_INQUIRY) && (rcmd[0] == INQUIRY)) {
3198 csio->sense_len = SHORT_INQUIRY_LENGTH;
3200 sc->transfer(sc, ccb->ccb_h.target_lun,
3203 csio->sense_len, DIR_IN, ccb->ccb_h.timeout,
3204 umass_cam_sense_cb, (void *) ccb);
3206 panic("transform(REQUEST_SENSE) failed");
3210 case XPT_RESET_DEV: /* Reset failed */
3211 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3215 panic("umass_cam_cb called for func_code %d",
3216 ccb->ccb_h.func_code);
3220 case STATUS_WIRE_FAILED:
3221 /* the wire protocol failed and will have recovered
3222 * (hopefully). We return an error to CAM and let CAM retry
3223 * the command if necessary.
3225 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3229 panic("%s: Unknown status %d in umass_cam_cb",
3230 device_get_nameunit(sc->sc_dev), status);
3234 /* Finalise a completed autosense operation
3237 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
3239 union ccb *ccb = (union ccb *) priv;
3240 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3241 unsigned char *rcmd;
3246 case STATUS_CMD_UNKNOWN:
3247 case STATUS_CMD_FAILED:
3248 /* Getting sense data always succeeds (apart from wire
3251 if ((sc->quirks & RS_NO_CLEAR_UA)
3252 && csio->cdb_io.cdb_bytes[0] == INQUIRY
3253 && (csio->sense_data.flags & SSD_KEY)
3254 == SSD_KEY_UNIT_ATTENTION) {
3255 /* Ignore unit attention errors in the case where
3256 * the Unit Attention state is not cleared on
3257 * REQUEST SENSE. They will appear again at the next
3260 ccb->ccb_h.status = CAM_REQ_CMP;
3261 } else if ((csio->sense_data.flags & SSD_KEY)
3262 == SSD_KEY_NO_SENSE) {
3263 /* No problem after all (in the case of CBI without
3266 ccb->ccb_h.status = CAM_REQ_CMP;
3267 } else if ((sc->quirks & RS_NO_CLEAR_UA) &&
3268 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY) &&
3269 ((csio->sense_data.flags & SSD_KEY)
3270 == SSD_KEY_UNIT_ATTENTION)) {
3272 * Some devices do not clear the unit attention error
3273 * on request sense. We insert a test unit ready
3274 * command to make sure we clear the unit attention
3275 * condition, then allow the retry to proceed as
3279 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3280 | CAM_AUTOSNS_VALID;
3281 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3287 DPRINTF(UDMASS_SCSI,("%s: Doing a sneaky"
3288 "TEST_UNIT_READY\n",
3289 device_get_nameunit(sc->sc_dev)));
3291 /* the rest of the command was filled in at attach */
3293 rcmd = (unsigned char *) &sc->cam_scsi_command2;
3294 rcmdlen = sizeof(sc->cam_scsi_command2);
3296 if (sc->transform(sc,
3298 &sc->cam_scsi_test_unit_ready,
3299 sizeof(sc->cam_scsi_test_unit_ready),
3301 sc->transfer(sc, ccb->ccb_h.target_lun,
3303 NULL, 0, DIR_NONE, ccb->ccb_h.timeout,
3304 umass_cam_quirk_cb, (void *) ccb);
3306 panic("transform(TEST_UNIT_READY) failed");
3310 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3311 | CAM_AUTOSNS_VALID;
3312 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3318 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
3319 device_get_nameunit(sc->sc_dev), status));
3320 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3326 * This completion code just handles the fact that we sent a test-unit-ready
3327 * after having previously failed a READ CAPACITY with CHECK_COND. Even
3328 * though this command succeeded, we have to tell CAM to retry.
3331 umass_cam_quirk_cb(struct umass_softc *sc, void *priv, int residue, int status)
3333 union ccb *ccb = (union ccb *) priv;
3335 DPRINTF(UDMASS_SCSI, ("%s: Test unit ready returned status %d\n",
3336 device_get_nameunit(sc->sc_dev), status));
3338 ccb->ccb_h.status = CAM_REQ_CMP;
3340 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3341 | CAM_AUTOSNS_VALID;
3342 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3347 umass_driver_load(module_t mod, int what, void *arg)
3353 return(usbd_driver_load(mod, what, arg));
3358 * SCSI specific functions
3362 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3363 unsigned char **rcmd, int *rcmdlen)
3366 case TEST_UNIT_READY:
3367 if (sc->quirks & NO_TEST_UNIT_READY) {
3368 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3369 ("rcmdlen = %d < %ld, buffer too small",
3371 (long)sizeof(struct scsi_start_stop_unit)));
3372 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3373 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3374 memset(*rcmd, 0, *rcmdlen);
3375 (*rcmd)[0] = START_STOP_UNIT;
3376 (*rcmd)[4] = SSS_START;
3381 /* some drives wedge when asked for full inquiry information. */
3382 if (sc->quirks & FORCE_SHORT_INQUIRY) {
3383 memcpy(*rcmd, cmd, cmdlen);
3385 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3390 *rcmd = cmd; /* We don't need to copy it */
3396 /* RBC specific functions */
3398 umass_rbc_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3399 unsigned char **rcmd, int *rcmdlen)
3402 /* these commands are defined in RBC: */
3405 case START_STOP_UNIT:
3406 case SYNCHRONIZE_CACHE:
3408 case 0x2f: /* VERIFY_10 is absent from scsi_all.h??? */
3410 case MODE_SELECT_10:
3412 case TEST_UNIT_READY:
3414 /* The following commands are not listed in my copy of the RBC specs.
3415 * CAM however seems to want those, and at least the Sony DSC device
3416 * appears to support those as well */
3419 *rcmd = cmd; /* We don't need to copy it */
3422 /* All other commands are not legal in RBC */
3424 kprintf("%s: Unsupported RBC command 0x%02x",
3425 device_get_nameunit(sc->sc_dev), cmd[0]);
3427 return 0; /* failure */
3432 * UFI specific functions
3435 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3436 unsigned char **rcmd, int *rcmdlen)
3438 /* A UFI command is always 12 bytes in length */
3439 KASSERT(*rcmdlen >= UFI_COMMAND_LENGTH,
3440 ("rcmdlen = %d < %d, buffer too small",
3441 *rcmdlen, UFI_COMMAND_LENGTH));
3443 *rcmdlen = UFI_COMMAND_LENGTH;
3444 memset(*rcmd, 0, UFI_COMMAND_LENGTH);
3447 /* Commands of which the format has been verified. They should work.
3448 * Copy the command into the (zeroed out) destination buffer.
3450 case TEST_UNIT_READY:
3451 if (sc->quirks & NO_TEST_UNIT_READY) {
3452 /* Some devices do not support this command.
3453 * Start Stop Unit should give the same results
3455 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
3456 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3457 (*rcmd)[0] = START_STOP_UNIT;
3458 (*rcmd)[4] = SSS_START;
3460 memcpy(*rcmd, cmd, cmdlen);
3467 case START_STOP_UNIT:
3468 case SEND_DIAGNOSTIC:
3473 case POSITION_TO_ELEMENT: /* SEEK_10 */
3474 case MODE_SELECT_10:
3478 memcpy(*rcmd, cmd, cmdlen);
3481 /* Other UFI commands: FORMAT_UNIT, READ_FORMAT_CAPACITY,
3482 * VERIFY, WRITE_AND_VERIFY.
3483 * These should be checked whether they somehow can be made to fit.
3487 kprintf("%s: Unsupported UFI command 0x%02x\n",
3488 device_get_nameunit(sc->sc_dev), cmd[0]);
3489 return 0; /* failure */
3494 * 8070i (ATAPI) specific functions
3497 umass_atapi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3498 unsigned char **rcmd, int *rcmdlen)
3500 /* An ATAPI command is always 12 bytes in length. */
3501 KASSERT(*rcmdlen >= ATAPI_COMMAND_LENGTH,
3502 ("rcmdlen = %d < %d, buffer too small",
3503 *rcmdlen, ATAPI_COMMAND_LENGTH));
3505 *rcmdlen = ATAPI_COMMAND_LENGTH;
3506 memset(*rcmd, 0, ATAPI_COMMAND_LENGTH);
3509 /* Commands of which the format has been verified. They should work.
3510 * Copy the command into the (zeroed out) destination buffer.
3513 memcpy(*rcmd, cmd, cmdlen);
3514 /* some drives wedge when asked for full inquiry information. */
3515 if (sc->quirks & FORCE_SHORT_INQUIRY)
3516 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3519 case TEST_UNIT_READY:
3520 if (sc->quirks & NO_TEST_UNIT_READY) {
3521 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3522 ("rcmdlen = %d < %ld, buffer too small",
3524 (long)sizeof(struct scsi_start_stop_unit)));
3525 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3526 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3527 memset(*rcmd, 0, *rcmdlen);
3528 (*rcmd)[0] = START_STOP_UNIT;
3529 (*rcmd)[4] = SSS_START;
3535 * All commands are passed through, very likely it will just work
3536 * regardless whether we know these commands or not.
3538 memcpy(*rcmd, cmd, cmdlen);
3544 /* (even the comment is missing) */
3546 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0);
3552 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3554 int clen = cbw->bCDBLength;
3555 int dlen = UGETDW(cbw->dCBWDataTransferLength);
3556 u_int8_t *c = cbw->CBWCDB;
3557 int tag = UGETDW(cbw->dCBWTag);
3558 int flags = cbw->bCBWFlags;
3560 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
3561 "(0x%02x%02x%02x%02x%02x%02x%s), "
3562 "data = %db, dir = %s\n",
3563 device_get_nameunit(sc->sc_dev), tag, clen,
3564 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
3565 dlen, (flags == CBWFLAGS_IN? "in":
3566 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
3570 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3572 int sig = UGETDW(csw->dCSWSignature);
3573 int tag = UGETW(csw->dCSWTag);
3574 int res = UGETDW(csw->dCSWDataResidue);
3575 int status = csw->bCSWStatus;
3577 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
3578 "res = %d, status = 0x%02x (%s)\n", device_get_nameunit(sc->sc_dev),
3579 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
3581 status, (status == CSWSTATUS_GOOD? "good":
3582 (status == CSWSTATUS_FAILED? "failed":
3583 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
3587 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, int cmdlen)
3590 int dir = sc->transfer_dir;
3592 DPRINTF(UDMASS_BBB, ("%s: cmd = %db "
3593 "(0x%02x%02x%02x%02x%02x%02x%s), "
3594 "data = %db, dir = %s\n",
3595 device_get_nameunit(sc->sc_dev), cmdlen,
3596 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6? "...":""),
3597 sc->transfer_datalen,
3598 (dir == DIR_IN? "in":
3599 (dir == DIR_OUT? "out":
3600 (dir == DIR_NONE? "no data phase": "<invalid>")))));
3604 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
3615 ksprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3616 for (i = 0; i < buflen && i < printlen; i++) {
3618 if (j == 0 && i != 0) {
3619 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3620 device_get_nameunit(sc->sc_dev), s1, s2));
3623 ksprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3625 if (buflen > printlen)
3626 ksprintf(s3, " ...");
3627 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3628 device_get_nameunit(sc->sc_dev), s1, s2, s3));