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 $
32 * Universal Serial Bus Mass Storage Class specs:
33 * http://www.usb.org/developers/data/devclass/usbmassover_11.pdf
34 * http://www.usb.org/developers/data/devclass/usbmassbulk_10.pdf
35 * http://www.usb.org/developers/data/devclass/usbmass-cbi10.pdf
36 * http://www.usb.org/developers/data/devclass/usbmass-ufi10.pdf
40 * Ported to NetBSD by Lennart Augustsson <augustss@netbsd.org>.
41 * Parts of the code written my Jason R. Thorpe <thorpej@shagadelic.org>.
45 * The driver handles 3 Wire Protocols
46 * - Command/Bulk/Interrupt (CBI)
47 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
48 * - Mass Storage Bulk-Only (BBB)
49 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
51 * Over these wire protocols it handles the following command protocols
53 * - UFI (floppy command set)
56 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
57 * sc->transform method is used to convert the commands into the appropriate
58 * format (if at all necessary). For example, UFI requires all commands to be
59 * 12 bytes in length amongst other things.
61 * The source code below is marked and can be split into a number of pieces
64 * - probe/attach/detach
65 * - generic transfer routines
68 * - CBI_I (in addition to functions from CBI)
69 * - CAM (Common Access Method)
74 * The protocols are implemented using a state machine, for the transfers as
75 * well as for the resets. The state machine is contained in umass_*_state.
76 * The state machine is started through either umass_*_transfer or
79 * The reason for doing this is a) CAM performs a lot better this way and b) it
80 * avoids using tsleep from interrupt context (for example after a failed
85 * The SCSI related part of this driver has been derived from the
86 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
88 * The CAM layer uses so called actions which are messages sent to the host
89 * adapter for completion. The actions come in through umass_cam_action. The
90 * appropriate block of routines is called depending on the transport protocol
91 * in use. When the transfer has finished, these routines call
92 * umass_cam_cb again to complete the CAM command.
96 * XXX Currently CBI with CCI is not supported because it bombs the system
97 * when the device is detached (low frequency interrupts are detached
102 #include <sys/param.h>
103 #include <sys/systm.h>
104 #include <sys/kernel.h>
105 #include <sys/module.h>
107 #include <sys/sysctl.h>
109 #include <bus/usb/usb.h>
110 #include <bus/usb/usbdi.h>
111 #include <bus/usb/usbdi_util.h>
113 #include <bus/cam/cam.h>
114 #include <bus/cam/cam_ccb.h>
115 #include <bus/cam/cam_sim.h>
116 #include <bus/cam/cam_xpt_sim.h>
117 #include <bus/cam/scsi/scsi_all.h>
118 #include <bus/cam/scsi/scsi_da.h>
119 #include <bus/cam/scsi/scsi_cd.h>
120 #include <bus/cam/scsi/scsi_ch.h>
122 #include <bus/cam/cam_periph.h>
125 #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0)
126 #define DPRINTF(m, x) if (umassdebug & (m)) kprintf x
127 #define UDMASS_GEN 0x00010000 /* general */
128 #define UDMASS_SCSI 0x00020000 /* scsi */
129 #define UDMASS_UFI 0x00040000 /* ufi command set */
130 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
131 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
132 #define UDMASS_USB 0x00100000 /* USB general */
133 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
134 #define UDMASS_CBI 0x00400000 /* CBI transfers */
135 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
136 #define UDMASS_ALL 0xffff0000 /* all of the above */
138 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
139 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
140 &umassdebug, 0, "umass debug level");
142 #define DIF(m, x) /* nop */
143 #define DPRINTF(m, x) /* nop */
147 /* Generic definitions */
149 /* Direction for umass_*_transfer */
155 #define DEVNAME "umass"
156 #define DEVNAME_SIM "umass-sim"
158 #define UMASS_MAX_TRANSFER_SIZE 65536
159 /* Approximate maximum transfer speeds (assumes 33% overhead). */
160 #define UMASS_FULL_TRANSFER_SPEED 1000
161 #define UMASS_HIGH_TRANSFER_SPEED 40000
162 #define UMASS_FLOPPY_TRANSFER_SPEED 20
164 #define UMASS_TIMEOUT 5000 /* msecs */
166 /* CAM specific definitions */
168 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
169 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
171 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
174 /* Bulk-Only features */
176 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
177 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
179 /* Command Block Wrapper */
181 uDWord dCBWSignature;
182 # define CBWSIGNATURE 0x43425355
184 uDWord dCBWDataTransferLength;
186 # define CBWFLAGS_OUT 0x00
187 # define CBWFLAGS_IN 0x80
190 # define CBWCDBLENGTH 16
191 uByte CBWCDB[CBWCDBLENGTH];
193 #define UMASS_BBB_CBW_SIZE 31
195 /* Command Status Wrapper */
197 uDWord dCSWSignature;
198 # define CSWSIGNATURE 0x53425355
199 # define CSWSIGNATURE_OLYMPUS_C1 0x55425355
201 uDWord dCSWDataResidue;
203 # define CSWSTATUS_GOOD 0x0
204 # define CSWSTATUS_FAILED 0x1
205 # define CSWSTATUS_PHASE 0x2
207 #define UMASS_BBB_CSW_SIZE 13
211 #define UR_CBI_ADSC 0x00
213 typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */
218 #define IDB_TYPE_CCI 0x00
220 #define IDB_VALUE_PASS 0x00
221 #define IDB_VALUE_FAIL 0x01
222 #define IDB_VALUE_PHASE 0x02
223 #define IDB_VALUE_PERSISTENT 0x03
224 #define IDB_VALUE_STATUS_MASK 0x03
235 struct umass_softc; /* see below */
237 typedef void (*transfer_cb_f) (struct umass_softc *sc, void *priv,
238 int residue, int status);
239 #define STATUS_CMD_OK 0 /* everything ok */
240 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
241 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
242 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
244 typedef void (*wire_reset_f) (struct umass_softc *sc, int status);
245 typedef void (*wire_transfer_f) (struct umass_softc *sc, int lun,
246 void *cmd, int cmdlen, void *data, int datalen,
247 int dir, u_int timeout, transfer_cb_f cb, void *priv);
248 typedef void (*wire_state_f) (usbd_xfer_handle xfer,
249 usbd_private_handle priv, usbd_status err);
251 typedef int (*command_transform_f) (struct umass_softc *sc,
252 unsigned char *cmd, int cmdlen,
253 unsigned char **rcmd, int *rcmdlen);
256 struct umass_devdescr_t {
260 # define WILDCARD_ID 0xffffffff
261 # define EOT_ID 0xfffffffe
263 /* wire and command protocol */
265 # define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
266 # define UMASS_PROTO_CBI 0x0002
267 # define UMASS_PROTO_CBI_I 0x0004
268 # define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
269 # define UMASS_PROTO_SCSI 0x0100 /* command protocol */
270 # define UMASS_PROTO_ATAPI 0x0200
271 # define UMASS_PROTO_UFI 0x0400
272 # define UMASS_PROTO_RBC 0x0800
273 # define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
275 /* Device specific quirks */
277 # define NO_QUIRKS 0x0000
278 /* The drive does not support Test Unit Ready. Convert to Start Unit
280 # define NO_TEST_UNIT_READY 0x0001
281 /* The drive does not reset the Unit Attention state after REQUEST
282 * SENSE has been sent. The INQUIRY command does not reset the UA
283 * either, and so CAM runs in circles trying to retrieve the initial
286 # define RS_NO_CLEAR_UA 0x0002
287 /* The drive does not support START STOP. */
288 # define NO_START_STOP 0x0004
289 /* Don't ask for full inquiry data (255b). */
290 # define FORCE_SHORT_INQUIRY 0x0008
291 /* Needs to be initialised the Shuttle way */
292 # define SHUTTLE_INIT 0x0010
293 /* Drive needs to be switched to alternate iface 1 */
294 # define ALT_IFACE_1 0x0020
295 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
296 # define FLOPPY_SPEED 0x0040
297 /* The device can't count and gets the residue of transfers wrong */
298 # define IGNORE_RESIDUE 0x0080
299 /* No GetMaxLun call */
300 # define NO_GETMAXLUN 0x0100
301 /* The device uses a weird CSWSIGNATURE. */
302 # define WRONG_CSWSIG 0x0200
303 /* Device cannot handle INQUIRY so fake a generic response */
304 # define NO_INQUIRY 0x0400
305 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
306 # define NO_INQUIRY_EVPD 0x0800
307 # define READ_CAPACITY_OFFBY1 0x2000
310 static struct umass_devdescr_t umass_devdescrs[] = {
311 /* Addonics Cable 205 */
312 { .vendor = 0x0bf6, .product = 0xa001, .release = WILDCARD_ID,
313 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
316 /* Addonics USB 2.0 Flash */
317 { .vendor = 0x09df, .product = 0x1300, .release = WILDCARD_ID,
318 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
319 .quirks = IGNORE_RESIDUE
321 /* Addonics Attache 256MB USB */
322 { .vendor = 0x09df, .product = 0x1400, .release = WILDCARD_ID,
323 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
324 .quirks = IGNORE_RESIDUE
326 /* Addonics USB 2.0 Flash */
327 { .vendor = 0x09df, .product = 0x1420, .release = WILDCARD_ID,
328 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
329 .quirks = IGNORE_RESIDUE
331 /* AIPTEK PocketCAM 3Mega */
332 { .vendor = 0x08ca, .product = 0x2011, .release = WILDCARD_ID,
333 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
336 /* All Asahi Optical products */
337 { .vendor = 0x0a17, .product = WILDCARD_ID, .release = WILDCARD_ID,
338 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
339 .quirks = RS_NO_CLEAR_UA
341 /* Belkin USB to SCSI */
342 { .vendor = 0x050d, .product = 0x0115, .release = WILDCARD_ID,
343 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
346 /* CASIO QV DigiCam */
347 { .vendor = 0x07cf, .product = 0x1001, .release = WILDCARD_ID,
348 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
351 /* CCYU EasyDisk ED1064 */
352 { .vendor = 0x1065, .product = 0x2136, .release = WILDCARD_ID,
353 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
356 /* Century Century USB Disk */
357 { .vendor = 0x07f7, .product = 0x011e, .release = WILDCARD_ID,
358 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
359 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
361 /* Desknote UCR-61S2B */
362 { .vendor = 0x1019, .product = 0x0c55, .release = WILDCARD_ID,
363 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
366 /* DMI CF/SM Reader/Writer */
367 { .vendor = 0x0c0b, .product = 0xa109, .release = WILDCARD_ID,
368 .proto = UMASS_PROTO_SCSI,
369 .quirks = NO_GETMAXLUN
371 /* Epson Stylus Photo 875DC */
372 { .vendor = 0x03f8, .product = 0x0601, .release = WILDCARD_ID,
373 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
376 /* Epson Stylus Photo 895 */
377 { .vendor = 0x03f8, .product = 0x0602, .release = WILDCARD_ID,
378 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
379 .quirks = NO_GETMAXLUN
381 /* Feiya 5-in-1 Card Reader */
382 { .vendor = 0x090c, .product = 0x1132, .release = WILDCARD_ID,
383 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
386 /* Freecom DVD drive */
387 { .vendor = 0x07ab, .product = 0xfc01, .release = WILDCARD_ID,
388 .proto = UMASS_PROTO_SCSI,
391 /* Fujiphoto mass storage products */
392 { .vendor = 0x04cb, .product = 0x0100, .release = WILDCARD_ID,
393 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
394 .quirks = RS_NO_CLEAR_UA
396 /* Genesys GL641USB USB-IDE Bridge */
397 { .vendor = 0x05e3, .product = 0x0701, .release = WILDCARD_ID,
398 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
399 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
401 /* Genesys GL641USB USB-IDE Bridge */
402 { .vendor = 0x05e3, .product = 0x0702, .release = WILDCARD_ID,
403 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
404 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
406 /* Genesys GL641USB CompactFlash Card */
407 { .vendor = 0x05e3, .product = 0x0700, .release = WILDCARD_ID,
408 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
409 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
411 /* Genesys GL641USB 6-in-1 Card */
412 { .vendor = 0x05e3, .product = 0x0760, .release = WILDCARD_ID,
413 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
414 .quirks = WRONG_CSWSIG
416 /* Hagiwara FlashGate SmartMedia Card */
417 { .vendor = 0x0693, .product = 0x0002, .release = WILDCARD_ID,
418 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
421 /* Hitachi DVDCAM USB HS Interface */
422 { .vendor = 0x04a4, .product = 0x001e, .release = WILDCARD_ID,
423 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
426 /* Hitachi DVD-CAM DZ-MV100A Camcorder */
427 { .vendor = 0x04a4, .product = 0x0004, .release = WILDCARD_ID,
428 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
429 .quirks = NO_GETMAXLUN
431 /* Hewlett CD-Writer+ CD-4e */
432 { .vendor = 0x03f0, .product = 0x0307, .release = WILDCARD_ID,
433 .proto = UMASS_PROTO_ATAPI,
436 /* Hewlett CD-Writer Plus 8200e */
437 { .vendor = 0x03f0, .product = 0x0207, .release = WILDCARD_ID,
438 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
439 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
441 /* Imagination DBX1 DSP core */
442 { .vendor = 0x149a, .product = 0x2107, .release = WILDCARD_ID,
443 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
444 .quirks = WRONG_CSWSIG
446 /* In-System ATAPI Adapter */
447 { .vendor = 0x05ab, .product = 0x0031, .release = WILDCARD_ID,
448 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
451 /* In-System USB Storage Adapter */
452 { .vendor = 0x05ab, .product = 0x5701, .release = WILDCARD_ID,
453 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
456 /* In-System USB cable */
457 { .vendor = 0x05ab, .product = 0x081a, .release = WILDCARD_ID,
458 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
459 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
461 /* I-O DVD Multi-plus unit */
462 { .vendor = 0x04bb, .product = 0x0204, .release = WILDCARD_ID,
463 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
466 /* I-O DVD Multi-plus unit */
467 { .vendor = 0x04bb, .product = 0x0206, .release = WILDCARD_ID,
468 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
472 { .vendor = 0x059b, .product = 0x0001, .release = WILDCARD_ID,
473 /* XXX This is not correct as there are Zip drives that use ATAPI. */
474 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
475 .quirks = NO_TEST_UNIT_READY
477 /* Kyocera Finecam L3 */
478 { .vendor = 0x0482, .product = 0x0105, .release = WILDCARD_ID,
479 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
482 /* Kyocera Finecam S3x */
483 { .vendor = 0x0482, .product = 0x0100, .release = WILDCARD_ID,
484 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
487 /* Kyocera Finecam S4 */
488 { .vendor = 0x0482, .product = 0x0101, .release = WILDCARD_ID,
489 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
492 /* Kyocera Finecam S5 */
493 { .vendor = 0x0482, .product = 0x0103, .release = WILDCARD_ID,
494 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
497 /* LaCie Hard Disk */
498 { .vendor = 0x059f, .product = 0xa601, .release = WILDCARD_ID,
499 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
502 /* Lexar USB CF Reader */
503 { .vendor = 0x05dc, .product = 0xb002, .release = WILDCARD_ID,
504 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
507 /* Lexar jumpSHOT CompactFlash Reader */
508 { .vendor = 0x05dc, .product = 0x0001, .release = WILDCARD_ID,
509 .proto = UMASS_PROTO_SCSI,
512 /* Logitech DVD Multi-plus unit */
513 { .vendor = 0x046d, .product = 0x0033, .release = WILDCARD_ID,
514 .proto = UMASS_PROTO_SCSI,
517 /* Logitech DVD Multi-plus unit LDR-H443U2 */
518 { .vendor = 0x0789, .product = 0x00b3, .release = WILDCARD_ID,
519 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
522 /* Melco USB-IDE Bridge: DUB-PxxG */
523 { .vendor = 0x0411, .product = 0x001c, .release = WILDCARD_ID,
524 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
525 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
527 /* Microtech USB CameraMate */
528 { .vendor = 0x07af, .product = 0x0006, .release = WILDCARD_ID,
529 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
530 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
532 /* Microtech USB-SCSI-DB25 */
533 { .vendor = 0x07af, .product = 0x0004, .release = WILDCARD_ID,
534 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
537 /* Microtech USB-SCSI-HD50 */
538 { .vendor = 0x07af, .product = 0x0005, .release = WILDCARD_ID,
539 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
542 /* Minolta Dimage E223 */
543 { .vendor = 0x0686, .product = 0x4017, .release = WILDCARD_ID,
544 .proto = UMASS_PROTO_SCSI,
547 /* Minolta Dimage F300 */
548 { .vendor = 0x0686, .product = 0x4011, .release = WILDCARD_ID,
549 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
552 /* Mitsumi CD-R/RW Drive */
553 { .vendor = 0x03ee, .product = 0x0000, .release = WILDCARD_ID,
554 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
557 /* Mitsumi USB FDD */
558 { .vendor = 0x03ee, .product = 0x6901, .release = WILDCARD_ID,
559 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
560 .quirks = NO_GETMAXLUN
562 /* Motorola E398 Mobile Phone */
563 { .vendor = 0x22b8, .product = 0x4810, .release = WILDCARD_ID,
564 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
565 .quirks = FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
567 /* M-Systems DiskOnKey */
568 { .vendor = 0x08ec, .product = 0x0010, .release = WILDCARD_ID,
569 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
570 .quirks = IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
572 /* M-Systems DiskOnKey */
573 { .vendor = 0x08ec, .product = 0x0011, .release = WILDCARD_ID,
574 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
578 { .vendor = 0x04cf, .product = 0x8818, .release = WILDCARD_ID,
579 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
580 .quirks = NO_INQUIRY | IGNORE_RESIDUE
582 /* Neodio 8-in-1 Multi-format Flash */
583 { .vendor = 0x0aec, .product = 0x3260, .release = WILDCARD_ID,
584 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
585 .quirks = FORCE_SHORT_INQUIRY
587 /* Netac USB-CF-Card */
588 { .vendor = 0x0dd8, .product = 0x1060, .release = WILDCARD_ID,
589 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
593 { .vendor = 0x0dd8, .product = 0x0003, .release = WILDCARD_ID,
594 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
595 .quirks = IGNORE_RESIDUE
597 /* NetChip USB Clik! 40 */
598 { .vendor = 0x0525, .product = 0xa140, .release = WILDCARD_ID,
599 .proto = UMASS_PROTO_ATAPI,
602 /* Olympus C-1 Digital Camera */
603 { .vendor = 0x07b4, .product = 0x0102, .release = WILDCARD_ID,
604 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
605 .quirks = WRONG_CSWSIG
607 /* Olympus C-700 Ultra Zoom */
608 { .vendor = 0x07b4, .product = 0x0105, .release = WILDCARD_ID,
609 .proto = UMASS_PROTO_SCSI,
610 .quirks = NO_GETMAXLUN
612 /* OnSpec SIIG/Datafab Memory Stick+CF */
613 { .vendor = 0x07c4, .product = 0xa001, .release = WILDCARD_ID,
614 .proto = UMASS_PROTO_SCSI,
617 /* OnSpec USB to CF */
618 { .vendor = 0x07c4, .product = 0xa109, .release = WILDCARD_ID,
619 .proto = UMASS_PROTO_SCSI,
622 /* OnSpec PNY/Datafab CF+SM Reader */
623 { .vendor = 0x07c4, .product = 0xa005, .release = WILDCARD_ID,
624 .proto = UMASS_PROTO_SCSI,
627 /* OnSpec Simple Tech/Datafab CF+SM */
628 { .vendor = 0x07c4, .product = 0xa006, .release = WILDCARD_ID,
629 .proto = UMASS_PROTO_SCSI,
632 /* OnSpec MDCFE-B USB CF */
633 { .vendor = 0x07c4, .product = 0xa000, .release = WILDCARD_ID,
634 .proto = UMASS_PROTO_SCSI,
637 /* OnSpec MDSM-B reader */
638 { .vendor = 0x07c4, .product = 0xa103, .release = WILDCARD_ID,
639 .proto = UMASS_PROTO_SCSI,
642 /* OnSpec Datafab-based Reader */
643 { .vendor = 0x07c4, .product = 0xa003, .release = WILDCARD_ID,
644 .proto = UMASS_PROTO_SCSI,
647 /* OnSpec FlashLink UCF-100 CompactFlash */
648 { .vendor = 0x07c4, .product = 0xa400, .release = WILDCARD_ID,
649 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
650 .quirks = NO_INQUIRY | NO_GETMAXLUN
652 /* OnSpec ImageMate SDDR55 */
653 { .vendor = 0x55aa, .product = 0xa103, .release = WILDCARD_ID,
654 .proto = UMASS_PROTO_SCSI,
655 .quirks = NO_GETMAXLUN
657 /* Panasonic CD-R Drive KXL-840AN */
658 { .vendor = 0x04da, .product = 0x0d01, .release = WILDCARD_ID,
659 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
660 .quirks = NO_GETMAXLUN
662 /* Panasonic CD-R Drive KXL-CB20AN */
663 { .vendor = 0x04da, .product = 0x0d0a, .release = WILDCARD_ID,
664 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
667 /* Panasonic DVD-ROM & CD-R/RW */
668 { .vendor = 0x04da, .product = 0x0d0e, .release = WILDCARD_ID,
669 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
672 /* Panasonic LS-120 Camera */
673 { .vendor = 0x04da, .product = 0x0901, .release = WILDCARD_ID,
674 .proto = UMASS_PROTO_UFI,
677 /* Plextor PlexWriter 40/12/40U */
678 { .vendor = 0x093b, .product = 0x0011, .release = WILDCARD_ID,
679 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
680 .quirks = NO_TEST_UNIT_READY
682 /* PNY USB 2.0 Flash */
683 { .vendor = 0x154b, .product = 0x0010, .release = WILDCARD_ID,
684 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
685 .quirks = IGNORE_RESIDUE | NO_START_STOP
687 /* Pen USB 2.0 Flash Drive */
688 { .vendor = 0x0d7d, .product = 0x1300, .release = WILDCARD_ID,
689 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
690 .quirks = IGNORE_RESIDUE
692 /* Samsung YP-U2 MP3 Player */
693 { .vendor = 0x04e8, .product = 0x5050, .release = WILDCARD_ID,
694 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
695 .quirks = SHUTTLE_INIT | NO_GETMAXLUN
697 /* Samsung Digimax 410 */
698 { .vendor = 0x0839, .product = 0x000a, .release = WILDCARD_ID,
699 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
702 /* SanDisk ImageMate SDDR-05a */
703 { .vendor = 0x0781, .product = 0x0001, .release = WILDCARD_ID,
704 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
705 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
707 /* SanDisk ImageMate SDDR-09 */
708 { .vendor = 0x0781, .product = 0x0200, .release = WILDCARD_ID,
709 .proto = UMASS_PROTO_SCSI,
710 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
712 /* SanDisk ImageMate SDDR-12 */
713 { .vendor = 0x0781, .product = 0x0100, .release = WILDCARD_ID,
714 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
715 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
717 /* SanDisk ImageMate SDDR-31 */
718 { .vendor = 0x0781, .product = 0x0002, .release = WILDCARD_ID,
719 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
720 .quirks = READ_CAPACITY_OFFBY1
722 /* SanDisk Cruzer Mini 256MB */
723 { .vendor = 0x0781, .product = 0x7104, .release = WILDCARD_ID,
724 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
725 .quirks = IGNORE_RESIDUE
727 /* SanDisk Cruzer Micro 128MB */
728 { .vendor = 0x0781, .product = 0x7112, .release = WILDCARD_ID,
729 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
730 .quirks = IGNORE_RESIDUE
732 /* SanDisk Cruzer Micro 256MB */
733 { .vendor = 0x0781, .product = 0x7113, .release = WILDCARD_ID,
734 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
735 .quirks = IGNORE_RESIDUE
737 /* ScanLogic SL11R IDE Adapter */
738 { .vendor = 0x04ce, .product = 0x0002, .release = WILDCARD_ID,
739 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
742 /* Shuttle CD-RW Device */
743 { .vendor = 0x04e6, .product = 0x0101, .release = WILDCARD_ID,
744 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
747 /* Shuttle eUSB CompactFlash Adapter */
748 { .vendor = 0x04e6, .product = 0x000a, .release = WILDCARD_ID,
749 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
752 /* Shuttle E-USB Bridge */
753 { .vendor = 0x04e6, .product = 0x0001, .release = WILDCARD_ID,
754 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
755 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
757 /* Shuttle eUSB ATA/ATAPI Adapter */
758 { .vendor = 0x04e6, .product = 0x0009, .release = WILDCARD_ID,
759 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
762 /* Shuttle eUSB SmartMedia / */
763 { .vendor = 0x04e6, .product = 0x0005, .release = WILDCARD_ID,
764 .proto = UMASS_PROTO_SCSI,
767 /* Shuttle eUSCSI Bridge */
768 { .vendor = 0x04e6, .product = 0x0002, .release = WILDCARD_ID,
769 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
772 /* Shuttle Sony Hifd */
773 { .vendor = 0x04e6, .product = 0x0007, .release = WILDCARD_ID,
774 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
775 .quirks = NO_GETMAXLUN
777 /* Shuttle ImageMate SDDR09 */
778 { .vendor = 0x04e6, .product = 0x0003, .release = WILDCARD_ID,
779 .proto = UMASS_PROTO_SCSI,
780 .quirks = NO_GETMAXLUN
782 /* Shuttle eUSB MultiMediaCard Adapter */
783 { .vendor = 0x04e6, .product = 0x0006, .release = WILDCARD_ID,
784 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
785 .quirks = NO_GETMAXLUN
787 /* Sigmatel i-Bead 100 MP3 */
788 { .vendor = 0x066f, .product = 0x8008, .release = WILDCARD_ID,
789 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
790 .quirks = SHUTTLE_INIT
792 /* SIIG WINTERREADER Reader */
793 { .vendor = 0x07cc, .product = 0x0330, .release = WILDCARD_ID,
794 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
795 .quirks = IGNORE_RESIDUE
797 /* Skanhex MD 7425 Camera */
798 { .vendor = 0x0d96, .product = 0x410a, .release = WILDCARD_ID,
799 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
802 /* Skanhex SX 520z Camera */
803 { .vendor = 0x0d96, .product = 0x5200, .release = WILDCARD_ID,
804 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
808 { .vendor = 0x054c, .product = 0x006d, .release = WILDCARD_ID,
809 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
812 /* Sony DSC cameras */
813 { .vendor = 0x054c, .product = 0x0010, .release = WILDCARD_ID,
814 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
817 { .vendor = 0x054c, .product = 0x02f7, .release = WILDCARD_ID,
818 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
822 { .vendor = 0x054c, .product = 0x002e, .release = WILDCARD_ID,
823 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
826 /* Sony Memorystick MSC-U03 */
827 { .vendor = 0x054c, .product = 0x0069, .release = WILDCARD_ID,
828 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
829 .quirks = NO_GETMAXLUN
831 /* Sony Memorystick NW-MS7 */
832 { .vendor = 0x054c, .product = 0x0025, .release = WILDCARD_ID,
833 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
834 .quirks = NO_GETMAXLUN
836 /* Sony PEG N760c Memorystick */
837 { .vendor = 0x054c, .product = 0x0058, .release = WILDCARD_ID,
838 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
841 /* Sony Memorystick MSAC-US1 */
842 { .vendor = 0x054c, .product = 0x002d, .release = WILDCARD_ID,
843 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
844 .quirks = NO_GETMAXLUN
846 /* Sony MSC memory stick */
847 { .vendor = 0x054c, .product = 0x0032, .release = WILDCARD_ID,
848 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
851 /* Sony Portable USB Harddrive */
852 { .vendor = 0x054c, .product = 0x002b, .release = WILDCARD_ID,
853 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
856 /* Taugagreining CameraMate (DPCM_USB) */
857 { .vendor = 0x0436, .product = 0x0005, .release = WILDCARD_ID,
858 .proto = UMASS_PROTO_SCSI,
861 /* TEAC FD-05PUB floppy */
862 { .vendor = 0x0644, .product = 0x0000, .release = WILDCARD_ID,
863 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
866 /* Trek IBM USB Memory */
867 { .vendor = 0x0a16, .product = 0x8888, .release = WILDCARD_ID,
868 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
871 /* Trek ThumbDrive_8MB */
872 { .vendor = 0x0a16, .product = 0x9988, .release = WILDCARD_ID,
873 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
874 .quirks = IGNORE_RESIDUE
876 /* Trumpion Comotron C3310 MP3 */
877 { .vendor = 0x090a, .product = 0x1100, .release = WILDCARD_ID,
878 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
881 /* Trumpion MP3 player */
882 { .vendor = 0x090a, .product = 0x1200, .release = WILDCARD_ID,
883 .proto = UMASS_PROTO_RBC,
886 /* Trumpion T33520 USB Flash */
887 { .vendor = 0x090a, .product = 0x1001, .release = WILDCARD_ID,
888 .proto = UMASS_PROTO_SCSI,
891 /* TwinMOS Memory Disk IV */
892 { .vendor = 0x126f, .product = 0x1325, .release = WILDCARD_ID,
893 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
896 /* Vivitar Vivicam 35Xx */
897 { .vendor = 0x0636, .product = 0x0003, .release = WILDCARD_ID,
898 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
901 /* Western Firewire USB Combo */
902 { .vendor = 0x1058, .product = 0x0200, .release = WILDCARD_ID,
903 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
904 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
906 /* Western External USB HHD My Passport */
907 { .vendor = 0x1058, .product = 0x0704, .release = WILDCARD_ID,
908 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
909 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
911 /* Western External HDD */
912 { .vendor = 0x1058, .product = 0x0400, .release = WILDCARD_ID,
913 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
914 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
916 /* Western MyBook External HDD */
917 { .vendor = 0x1058, .product = 0x0901, .release = WILDCARD_ID,
918 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
919 .quirks = NO_INQUIRY_EVPD
921 /* WinMaxGroup USB Flash Disk */
922 { .vendor = 0x0ed1, .product = 0x6660, .release = WILDCARD_ID,
923 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
926 /* Yano METALWEAR-HDD */
927 { .vendor = 0x094f, .product = 0x05fc, .release = WILDCARD_ID,
928 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
929 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
932 { .vendor = 0x094f, .product = 0x0101, .release = WILDCARD_ID,
933 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
934 .quirks = FORCE_SHORT_INQUIRY
936 /* Y-E Flashbuster-U */
937 { .vendor = 0x057b, .product = 0x0000, .release = WILDCARD_ID,
938 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
939 .quirks = NO_GETMAXLUN
941 /* Zoran Digital Camera EX-20 */
942 { .vendor = 0x0595, .product = 0x4343, .release = WILDCARD_ID,
943 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
946 { .vendor = EOT_ID, .product = EOT_ID, .release = EOT_ID,
947 .proto = 0, .quirks = 0 }
951 /* the per device structure */
953 device_t sc_dev; /* base device */
954 usbd_device_handle sc_udev; /* USB device */
956 struct cam_sim *umass_sim; /* SCSI Interface Module */
958 unsigned char flags; /* various device flags */
959 # define UMASS_FLAGS_GONE 0x01 /* devices is no more */
961 u_int16_t proto; /* wire and cmd protocol */
962 u_int16_t quirks; /* they got it almost right */
964 usbd_interface_handle iface; /* Mass Storage interface */
965 int ifaceno; /* MS iface number */
967 u_int8_t bulkin; /* bulk-in Endpoint Address */
968 u_int8_t bulkout; /* bulk-out Endpoint Address */
969 u_int8_t intrin; /* intr-in Endp. (CBI) */
970 usbd_pipe_handle bulkin_pipe;
971 usbd_pipe_handle bulkout_pipe;
972 usbd_pipe_handle intrin_pipe;
974 /* Reset the device in a wire protocol specific way */
977 /* The start of a wire transfer. It prepares the whole transfer (cmd,
978 * data, and status stage) and initiates it. It is up to the state
979 * machine (below) to handle the various stages and errors in these
981 wire_transfer_f transfer;
983 /* The state machine, handling the various states during a transfer */
986 /* The command transform function is used to conver the SCSI commands
987 * into their derivatives, like UFI, ATAPI, and friends.
989 command_transform_f transform; /* command transform */
991 /* Bulk specific variables for transfers in progress */
992 umass_bbb_cbw_t cbw; /* command block wrapper */
993 umass_bbb_csw_t csw; /* command status wrapper*/
994 /* CBI specific variables for transfers in progress */
995 umass_cbi_cbl_t cbl; /* command block */
996 umass_cbi_sbl_t sbl; /* status block */
998 /* generic variables for transfers in progress */
999 /* ctrl transfer requests */
1000 usb_device_request_t request;
1003 * Most of our operations are initiated from interrupt context, so
1004 * we need to avoid using the one that is in use. We want to avoid
1005 * allocating them in the interrupt context as well.
1007 /* indices into array below */
1008 # define XFER_BBB_CBW 0 /* Bulk-Only */
1009 # define XFER_BBB_DATA 1
1010 # define XFER_BBB_DCLEAR 2
1011 # define XFER_BBB_CSW1 3
1012 # define XFER_BBB_CSW2 4
1013 # define XFER_BBB_SCLEAR 5
1014 # define XFER_BBB_RESET1 6
1015 # define XFER_BBB_RESET2 7
1016 # define XFER_BBB_RESET3 8
1018 # define XFER_CBI_CB 0 /* CBI */
1019 # define XFER_CBI_DATA 1
1020 # define XFER_CBI_STATUS 2
1021 # define XFER_CBI_DCLEAR 3
1022 # define XFER_CBI_SCLEAR 4
1023 # define XFER_CBI_RESET1 5
1024 # define XFER_CBI_RESET2 6
1025 # define XFER_CBI_RESET3 7
1027 # define XFER_NR 9 /* maximum number */
1029 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
1031 int transfer_dir; /* data direction */
1032 void *transfer_data; /* data buffer */
1033 int transfer_datalen; /* (maximum) length */
1034 int transfer_actlen; /* actual length */
1035 transfer_cb_f transfer_cb; /* callback */
1036 void *transfer_priv; /* for callback */
1037 int transfer_status;
1040 # define TSTATE_ATTACH 0 /* in attach */
1041 # define TSTATE_IDLE 1
1042 # define TSTATE_BBB_COMMAND 2 /* CBW transfer */
1043 # define TSTATE_BBB_DATA 3 /* Data transfer */
1044 # define TSTATE_BBB_DCLEAR 4 /* clear endpt stall */
1045 # define TSTATE_BBB_STATUS1 5 /* clear endpt stall */
1046 # define TSTATE_BBB_SCLEAR 6 /* clear endpt stall */
1047 # define TSTATE_BBB_STATUS2 7 /* CSW transfer */
1048 # define TSTATE_BBB_RESET1 8 /* reset command */
1049 # define TSTATE_BBB_RESET2 9 /* in clear stall */
1050 # define TSTATE_BBB_RESET3 10 /* out clear stall */
1051 # define TSTATE_CBI_COMMAND 11 /* command transfer */
1052 # define TSTATE_CBI_DATA 12 /* data transfer */
1053 # define TSTATE_CBI_STATUS 13 /* status transfer */
1054 # define TSTATE_CBI_DCLEAR 14 /* clear ep stall */
1055 # define TSTATE_CBI_SCLEAR 15 /* clear ep stall */
1056 # define TSTATE_CBI_RESET1 16 /* reset command */
1057 # define TSTATE_CBI_RESET2 17 /* in clear stall */
1058 # define TSTATE_CBI_RESET3 18 /* out clear stall */
1059 # define TSTATE_STATES 19 /* # of states above */
1062 /* SCSI/CAM specific variables */
1063 unsigned char cam_scsi_command[CAM_MAX_CDBLEN];
1064 unsigned char cam_scsi_command2[CAM_MAX_CDBLEN];
1065 struct scsi_sense cam_scsi_sense;
1066 struct scsi_sense cam_scsi_test_unit_ready;
1067 int timeout; /* in msecs */
1069 int maxlun; /* maximum LUN number */
1070 struct callout rescan_timeout;
1074 char *states[TSTATE_STATES+1] = {
1075 /* should be kept in sync with the list at transfer_state */
1080 "BBB Data bulk-in/-out clear stall",
1081 "BBB CSW, 1st attempt",
1082 "BBB CSW bulk-in clear stall",
1083 "BBB CSW, 2nd attempt",
1085 "BBB bulk-in clear stall",
1086 "BBB bulk-out clear stall",
1090 "CBI Data bulk-in/-out clear stall",
1091 "CBI Status intr-in clear stall",
1093 "CBI bulk-in clear stall",
1094 "CBI bulk-out clear stall",
1099 /* If device cannot return valid inquiry data, fake it */
1100 static uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1101 0, /*removable*/ 0x80, SCSI_REV_2, SCSI_REV_2,
1102 /*additional_length*/ 31, 0, 0, 0
1105 /* USB device probe/attach/detach functions */
1106 static device_probe_t umass_match;
1107 static device_attach_t umass_attach;
1108 static device_detach_t umass_detach;
1110 static devclass_t umass_devclass;
1112 static kobj_method_t umass_methods[] = {
1113 DEVMETHOD(device_probe, umass_match),
1114 DEVMETHOD(device_attach, umass_attach),
1115 DEVMETHOD(device_detach, umass_detach),
1119 static driver_t umass_driver = {
1122 sizeof(struct umass_softc)
1125 MODULE_DEPEND(umass, usb, 1, 1, 1);
1127 static int umass_match_proto (struct umass_softc *sc,
1128 usbd_interface_handle iface,
1129 usbd_device_handle udev);
1131 /* quirk functions */
1132 static void umass_init_shuttle (struct umass_softc *sc);
1134 /* generic transfer functions */
1135 static usbd_status umass_setup_transfer (struct umass_softc *sc,
1136 usbd_pipe_handle pipe,
1137 void *buffer, int buflen, int flags,
1138 usbd_xfer_handle xfer);
1139 static usbd_status umass_setup_ctrl_transfer (struct umass_softc *sc,
1140 usbd_device_handle udev,
1141 usb_device_request_t *req,
1142 void *buffer, int buflen, int flags,
1143 usbd_xfer_handle xfer);
1144 static void umass_clear_endpoint_stall (struct umass_softc *sc,
1145 u_int8_t endpt, usbd_pipe_handle pipe,
1146 int state, usbd_xfer_handle xfer);
1147 static void umass_reset (struct umass_softc *sc,
1148 transfer_cb_f cb, void *priv);
1150 /* Bulk-Only related functions */
1151 static void umass_bbb_reset (struct umass_softc *sc, int status);
1152 static void umass_bbb_transfer (struct umass_softc *sc, int lun,
1153 void *cmd, int cmdlen,
1154 void *data, int datalen, int dir, u_int timeout,
1155 transfer_cb_f cb, void *priv);
1156 static void umass_bbb_state (usbd_xfer_handle xfer,
1157 usbd_private_handle priv,
1159 static int umass_bbb_get_max_lun
1160 (struct umass_softc *sc);
1162 /* CBI related functions */
1163 static int umass_cbi_adsc (struct umass_softc *sc,
1164 char *buffer, int buflen,
1165 usbd_xfer_handle xfer);
1166 static void umass_cbi_reset (struct umass_softc *sc, int status);
1167 static void umass_cbi_transfer (struct umass_softc *sc, int lun,
1168 void *cmd, int cmdlen,
1169 void *data, int datalen, int dir, u_int timeout,
1170 transfer_cb_f cb, void *priv);
1171 static void umass_cbi_state (usbd_xfer_handle xfer,
1172 usbd_private_handle priv, usbd_status err);
1174 /* CAM related functions */
1175 static void umass_cam_action (struct cam_sim *sim, union ccb *ccb);
1176 static void umass_cam_poll (struct cam_sim *sim);
1178 static void umass_cam_cb (struct umass_softc *sc, void *priv,
1179 int residue, int status);
1180 static void umass_cam_sense_cb (struct umass_softc *sc, void *priv,
1181 int residue, int status);
1182 static void umass_cam_quirk_cb (struct umass_softc *sc, void *priv,
1183 int residue, int status);
1185 static void umass_cam_rescan_callback
1186 (struct cam_periph *periph,union ccb *ccb);
1187 static void umass_cam_rescan (void *addr);
1189 static int umass_cam_attach_sim (struct umass_softc *sc);
1190 static int umass_cam_attach (struct umass_softc *sc);
1191 static int umass_cam_detach_sim (struct umass_softc *sc);
1194 /* SCSI specific functions */
1195 static int umass_scsi_transform (struct umass_softc *sc,
1196 unsigned char *cmd, int cmdlen,
1197 unsigned char **rcmd, int *rcmdlen);
1199 /* UFI specific functions */
1200 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
1201 static int umass_ufi_transform (struct umass_softc *sc,
1202 unsigned char *cmd, int cmdlen,
1203 unsigned char **rcmd, int *rcmdlen);
1205 /* ATAPI (8070i) specific functions */
1206 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
1207 static int umass_atapi_transform (struct umass_softc *sc,
1208 unsigned char *cmd, int cmdlen,
1209 unsigned char **rcmd, int *rcmdlen);
1211 /* RBC specific functions */
1212 static int umass_rbc_transform (struct umass_softc *sc,
1213 unsigned char *cmd, int cmdlen,
1214 unsigned char **rcmd, int *rcmdlen);
1217 /* General debugging functions */
1218 static void umass_bbb_dump_cbw (struct umass_softc *sc, umass_bbb_cbw_t *cbw);
1219 static void umass_bbb_dump_csw (struct umass_softc *sc, umass_bbb_csw_t *csw);
1220 static void umass_cbi_dump_cmd (struct umass_softc *sc, void *cmd, int cmdlen);
1221 static void umass_dump_buffer (struct umass_softc *sc, u_int8_t *buffer,
1222 int buflen, int printlen);
1225 MODULE_DEPEND(umass, cam, 1,1,1);
1228 * USB device probe/attach/detach
1232 * Match the device we are seeing with the devices supported. Fill in the
1233 * description in the softc accordingly. This function is called from both
1238 umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface,
1239 usbd_device_handle udev)
1241 usb_device_descriptor_t *dd;
1242 usb_interface_descriptor_t *id;
1250 dd = usbd_get_device_descriptor(udev);
1252 /* An entry specifically for Y-E Data devices as they don't fit in the
1253 * device description table.
1255 if (UGETW(dd->idVendor) == 0x057b && UGETW(dd->idProduct) == 0x0000) {
1257 /* Revisions < 1.28 do not handle the inerrupt endpoint
1260 if (UGETW(dd->bcdDevice) < 0x128) {
1261 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1263 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1267 * Revisions < 1.28 do not have the TEST UNIT READY command
1268 * Revisions == 1.28 have a broken TEST UNIT READY
1270 if (UGETW(dd->bcdDevice) <= 0x128)
1271 sc->quirks |= NO_TEST_UNIT_READY;
1273 sc->quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1274 return(UMATCH_VENDOR_PRODUCT);
1277 /* Check the list of supported devices for a match. While looking,
1278 * check for wildcarded and fully matched. First match wins.
1280 for (i = 0; umass_devdescrs[i].vendor != EOT_ID && !found; i++) {
1281 if (umass_devdescrs[i].vendor == WILDCARD_ID &&
1282 umass_devdescrs[i].product == WILDCARD_ID &&
1283 umass_devdescrs[i].release == WILDCARD_ID) {
1284 kprintf("umass: ignoring invalid wildcard quirk\n");
1287 if ((umass_devdescrs[i].vendor == UGETW(dd->idVendor) ||
1288 umass_devdescrs[i].vendor == WILDCARD_ID)
1289 && (umass_devdescrs[i].product == UGETW(dd->idProduct) ||
1290 umass_devdescrs[i].product == WILDCARD_ID)) {
1291 if (umass_devdescrs[i].release == WILDCARD_ID) {
1292 sc->proto = umass_devdescrs[i].proto;
1293 sc->quirks = umass_devdescrs[i].quirks;
1294 return (UMATCH_VENDOR_PRODUCT);
1295 } else if (umass_devdescrs[i].release ==
1296 UGETW(dd->bcdDevice)) {
1297 sc->proto = umass_devdescrs[i].proto;
1298 sc->quirks = umass_devdescrs[i].quirks;
1299 return (UMATCH_VENDOR_PRODUCT_REV);
1300 } /* else RID does not match */
1304 /* Check for a standards compliant device */
1306 id = usbd_get_interface_descriptor(iface);
1307 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
1308 return(UMATCH_NONE);
1310 switch (id->bInterfaceSubClass) {
1311 case UISUBCLASS_SCSI:
1312 sc->proto |= UMASS_PROTO_SCSI;
1314 case UISUBCLASS_UFI:
1315 sc->proto |= UMASS_PROTO_UFI;
1317 case UISUBCLASS_RBC:
1318 sc->proto |= UMASS_PROTO_RBC;
1320 case UISUBCLASS_SFF8020I:
1321 case UISUBCLASS_SFF8070I:
1322 sc->proto |= UMASS_PROTO_ATAPI;
1325 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
1326 device_get_nameunit(sc->sc_dev), id->bInterfaceSubClass));
1327 return(UMATCH_NONE);
1330 switch (id->bInterfaceProtocol) {
1331 case UIPROTO_MASS_CBI:
1332 sc->proto |= UMASS_PROTO_CBI;
1334 case UIPROTO_MASS_CBI_I:
1335 sc->proto |= UMASS_PROTO_CBI_I;
1337 case UIPROTO_MASS_BBB_OLD:
1338 case UIPROTO_MASS_BBB:
1339 sc->proto |= UMASS_PROTO_BBB;
1342 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
1343 device_get_nameunit(sc->sc_dev), id->bInterfaceProtocol));
1344 return(UMATCH_NONE);
1347 return(UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
1351 umass_match(device_t self)
1353 struct usb_attach_arg *uaa = device_get_ivars(self);
1354 struct umass_softc *sc = device_get_softc(self);
1358 if (uaa->iface == NULL)
1359 return(UMATCH_NONE);
1361 return(umass_match_proto(sc, uaa->iface, uaa->device));
1365 umass_attach(device_t self)
1367 struct umass_softc *sc = device_get_softc(self);
1368 struct usb_attach_arg *uaa = device_get_ivars(self);
1369 usb_interface_descriptor_t *id;
1370 usb_endpoint_descriptor_t *ed;
1375 * the softc struct is bzero-ed in device_set_driver. We can safely
1376 * call umass_detach without specifically initialising the struct.
1381 sc->iface = uaa->iface;
1382 sc->ifaceno = uaa->ifaceno;
1384 /* initialise the proto and drive values in the umass_softc (again) */
1385 (void) umass_match_proto(sc, sc->iface, uaa->device);
1387 id = usbd_get_interface_descriptor(sc->iface);
1389 kprintf("%s: ", device_get_nameunit(sc->sc_dev));
1390 switch (sc->proto&UMASS_PROTO_COMMAND) {
1391 case UMASS_PROTO_SCSI:
1394 case UMASS_PROTO_ATAPI:
1395 kprintf("8070i (ATAPI)");
1397 case UMASS_PROTO_UFI:
1400 case UMASS_PROTO_RBC:
1404 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_COMMAND);
1408 switch (sc->proto&UMASS_PROTO_WIRE) {
1409 case UMASS_PROTO_BBB:
1410 kprintf("Bulk-Only");
1412 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
1415 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1416 kprintf("CBI with CCI");
1418 kprintf(" (using CBI)");
1422 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_WIRE);
1424 kprintf("; quirks = 0x%04x\n", sc->quirks);
1428 if (sc->proto & UMASS_PROTO_CBI_I) {
1429 /* See beginning of file for comment on the use of CBI with CCI */
1430 sc->proto = (sc->proto & ~UMASS_PROTO_CBI_I) | UMASS_PROTO_CBI;
1434 if (sc->quirks & ALT_IFACE_1) {
1435 err = usbd_set_interface(0, 1);
1437 DPRINTF(UDMASS_USB, ("%s: could not switch to "
1438 "Alt Interface %d\n",
1439 device_get_nameunit(sc->sc_dev), 1));
1446 * In addition to the Control endpoint the following endpoints
1448 * a) bulk-in endpoint.
1449 * b) bulk-out endpoint.
1450 * and for Control/Bulk/Interrupt with CCI (CBI_I)
1453 * The endpoint addresses are not fixed, so we have to read them
1454 * from the device descriptors of the current interface.
1456 for (i = 0 ; i < id->bNumEndpoints ; i++) {
1457 ed = usbd_interface2endpoint_descriptor(sc->iface, i);
1459 kprintf("%s: could not read endpoint descriptor\n",
1460 device_get_nameunit(sc->sc_dev));
1463 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1464 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1465 sc->bulkin = ed->bEndpointAddress;
1466 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
1467 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1468 sc->bulkout = ed->bEndpointAddress;
1469 } else if (sc->proto & UMASS_PROTO_CBI_I
1470 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1471 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
1472 sc->intrin = ed->bEndpointAddress;
1474 if (UGETW(ed->wMaxPacketSize) > 2) {
1475 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
1476 device_get_nameunit(sc->sc_dev),
1477 UGETW(ed->wMaxPacketSize)));
1483 /* check whether we found all the endpoints we need */
1484 if (!sc->bulkin || !sc->bulkout
1485 || (sc->proto & UMASS_PROTO_CBI_I && !sc->intrin) ) {
1486 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
1487 device_get_nameunit(sc->sc_dev),
1488 sc->bulkin, sc->bulkout, sc->intrin));
1493 /* Open the bulk-in and -out pipe */
1494 err = usbd_open_pipe(sc->iface, sc->bulkout,
1495 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
1497 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
1498 device_get_nameunit(sc->sc_dev), sc->bulkout));
1502 err = usbd_open_pipe(sc->iface, sc->bulkin,
1503 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
1505 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
1506 device_get_nameunit(sc->sc_dev), sc->bulkin));
1510 /* Open the intr-in pipe if the protocol is CBI with CCI.
1511 * Note: early versions of the Zip drive do have an interrupt pipe, but
1512 * this pipe is unused
1514 * We do not open the interrupt pipe as an interrupt pipe, but as a
1515 * normal bulk endpoint. We send an IN transfer down the wire at the
1516 * appropriate time, because we know exactly when to expect data on
1517 * that endpoint. This saves bandwidth, but more important, makes the
1518 * code for handling the data on that endpoint simpler. No data
1519 * arriving concurently.
1521 if (sc->proto & UMASS_PROTO_CBI_I) {
1522 err = usbd_open_pipe(sc->iface, sc->intrin,
1523 USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
1525 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
1526 device_get_nameunit(sc->sc_dev), sc->intrin));
1532 /* initialisation of generic part */
1533 sc->transfer_state = TSTATE_ATTACH;
1535 /* request a sufficient number of xfer handles */
1536 for (i = 0; i < XFER_NR; i++) {
1537 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
1538 if (!sc->transfer_xfer[i]) {
1539 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
1540 device_get_nameunit(sc->sc_dev)));
1547 * Preallocate buffers to avoid auto-allocation from an interrupt
1550 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_DATA],
1552 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_CBW],
1553 UMASS_BBB_CBW_SIZE);
1554 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_CSW1],
1555 UMASS_BBB_CSW_SIZE);
1556 usbd_alloc_buffer(sc->transfer_xfer[XFER_BBB_CSW2],
1557 UMASS_BBB_CSW_SIZE);
1558 usbd_alloc_buffer(sc->transfer_xfer[XFER_CBI_DATA],
1561 /* Initialise the wire protocol specific methods */
1562 if (sc->proto & UMASS_PROTO_BBB) {
1563 sc->reset = umass_bbb_reset;
1564 sc->transfer = umass_bbb_transfer;
1565 sc->state = umass_bbb_state;
1566 } else if (sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I)) {
1567 sc->reset = umass_cbi_reset;
1568 sc->transfer = umass_cbi_transfer;
1569 sc->state = umass_cbi_state;
1572 panic("%s:%d: Unknown proto 0x%02x",
1573 __FILE__, __LINE__, sc->proto);
1577 if (sc->proto & UMASS_PROTO_SCSI)
1578 sc->transform = umass_scsi_transform;
1579 else if (sc->proto & UMASS_PROTO_UFI)
1580 sc->transform = umass_ufi_transform;
1581 else if (sc->proto & UMASS_PROTO_ATAPI)
1582 sc->transform = umass_atapi_transform;
1583 else if (sc->proto & UMASS_PROTO_RBC)
1584 sc->transform = umass_rbc_transform;
1587 panic("No transformation defined for command proto 0x%02x",
1588 sc->proto & UMASS_PROTO_COMMAND);
1591 /* From here onwards the device can be used. */
1593 if (sc->quirks & SHUTTLE_INIT)
1594 umass_init_shuttle(sc);
1596 /* Get the maximum LUN supported by the device.
1598 if (((sc->proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1599 !(sc->quirks & NO_GETMAXLUN))
1600 sc->maxlun = umass_bbb_get_max_lun(sc);
1604 if ((sc->proto & UMASS_PROTO_SCSI) ||
1605 (sc->proto & UMASS_PROTO_ATAPI) ||
1606 (sc->proto & UMASS_PROTO_UFI) ||
1607 (sc->proto & UMASS_PROTO_RBC)) {
1608 /* Prepare the SCSI command block */
1609 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1610 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1612 /* register the SIM */
1613 err = umass_cam_attach_sim(sc);
1618 /* scan the new sim */
1619 err = umass_cam_attach(sc);
1621 umass_cam_detach_sim(sc);
1626 panic("%s:%d: Unknown proto 0x%02x",
1627 __FILE__, __LINE__, sc->proto);
1630 sc->transfer_state = TSTATE_IDLE;
1631 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", device_get_nameunit(sc->sc_dev)));
1637 umass_detach(device_t self)
1639 struct umass_softc *sc = device_get_softc(self);
1644 DPRINTF(UDMASS_USB, ("%s: detached\n", device_get_nameunit(sc->sc_dev)));
1647 * Set UMASS_FLAGS_GONE to prevent any new transfers from being
1648 * queued, and abort any transfers in progress to ensure that
1649 * pending requests (e.g. from CAM's bus scan) are terminated.
1651 sc->flags |= UMASS_FLAGS_GONE;
1653 if (sc->bulkout_pipe)
1654 usbd_abort_pipe(sc->bulkout_pipe);
1655 if (sc->bulkin_pipe)
1656 usbd_abort_pipe(sc->bulkin_pipe);
1657 if (sc->intrin_pipe)
1658 usbd_abort_pipe(sc->intrin_pipe);
1661 * Wait until we go idle to make sure that all of our xfer requests
1662 * have finished. We could be in the middle of a BBB reset (which
1663 * would not be effected by the pipe aborts above).
1666 while (sc->transfer_state != TSTATE_IDLE) {
1667 kprintf("%s: state %d waiting for idle\n",
1668 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1669 tsleep(sc, 0, "umassidl", to);
1670 if (to >= hz * 10) {
1671 kprintf("%s: state %d giving up!\n",
1672 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1678 if ((sc->proto & UMASS_PROTO_SCSI) ||
1679 (sc->proto & UMASS_PROTO_ATAPI) ||
1680 (sc->proto & UMASS_PROTO_UFI) ||
1681 (sc->proto & UMASS_PROTO_RBC)) {
1682 /* detach the SCSI host controller (SIM) */
1683 err = umass_cam_detach_sim(sc);
1686 for (i = 0; i < XFER_NR; i++) {
1687 if (sc->transfer_xfer[i])
1688 usbd_free_xfer(sc->transfer_xfer[i]);
1691 /* remove all the pipes */
1692 if (sc->bulkout_pipe)
1693 usbd_close_pipe(sc->bulkout_pipe);
1694 if (sc->bulkin_pipe)
1695 usbd_close_pipe(sc->bulkin_pipe);
1696 if (sc->intrin_pipe)
1697 usbd_close_pipe(sc->intrin_pipe);
1703 umass_init_shuttle(struct umass_softc *sc)
1705 usb_device_request_t req;
1708 /* The Linux driver does this, but no one can tell us what the
1711 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1712 req.bRequest = 1; /* XXX unknown command */
1713 USETW(req.wValue, 0);
1714 USETW(req.wIndex, sc->ifaceno);
1715 USETW(req.wLength, sizeof status);
1716 (void) usbd_do_request(sc->sc_udev, &req, &status);
1718 DPRINTF(UDMASS_GEN, ("%s: Shuttle init returned 0x%02x%02x\n",
1719 device_get_nameunit(sc->sc_dev), status[0], status[1]));
1723 * Generic functions to handle transfers
1727 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1728 void *buffer, int buflen, int flags,
1729 usbd_xfer_handle xfer)
1733 /* Initialiase a USB transfer and then schedule it */
1735 (void) usbd_setup_xfer(xfer, pipe, (void *) sc, buffer, buflen, flags,
1736 sc->timeout, sc->state);
1738 err = usbd_transfer(xfer);
1739 if (err && err != USBD_IN_PROGRESS) {
1740 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1741 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1745 return (USBD_NORMAL_COMPLETION);
1750 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle udev,
1751 usb_device_request_t *req,
1752 void *buffer, int buflen, int flags,
1753 usbd_xfer_handle xfer)
1757 /* Initialiase a USB control transfer and then schedule it */
1759 (void) usbd_setup_default_xfer(xfer, udev, (void *) sc,
1760 UMASS_TIMEOUT, req, buffer, buflen, flags, sc->state);
1762 err = usbd_transfer(xfer);
1763 if (err && err != USBD_IN_PROGRESS) {
1764 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1765 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1767 /* do not reset, as this would make us loop */
1771 return (USBD_NORMAL_COMPLETION);
1775 umass_clear_endpoint_stall(struct umass_softc *sc,
1776 u_int8_t endpt, usbd_pipe_handle pipe,
1777 int state, usbd_xfer_handle xfer)
1779 usbd_device_handle udev;
1781 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1782 device_get_nameunit(sc->sc_dev), endpt));
1784 usbd_interface2device_handle(sc->iface, &udev);
1786 sc->transfer_state = state;
1788 usbd_clear_endpoint_toggle(pipe);
1790 sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1791 sc->request.bRequest = UR_CLEAR_FEATURE;
1792 USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1793 USETW(sc->request.wIndex, endpt);
1794 USETW(sc->request.wLength, 0);
1795 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0, xfer);
1799 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1801 sc->transfer_cb = cb;
1802 sc->transfer_priv = priv;
1804 /* The reset is a forced reset, so no error (yet) */
1805 sc->reset(sc, STATUS_CMD_OK);
1809 * Bulk protocol specific functions
1813 umass_bbb_reset(struct umass_softc *sc, int status)
1815 usbd_device_handle udev;
1817 KASSERT(sc->proto & UMASS_PROTO_BBB,
1818 ("%s: umass_bbb_reset: wrong sc->proto 0x%02x",
1819 device_get_nameunit(sc->sc_dev), sc->proto));
1822 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1824 * For Reset Recovery the host shall issue in the following order:
1825 * a) a Bulk-Only Mass Storage Reset
1826 * b) a Clear Feature HALT to the Bulk-In endpoint
1827 * c) a Clear Feature HALT to the Bulk-Out endpoint
1829 * This is done in 3 steps, states:
1834 * If the reset doesn't succeed, the device should be port reset.
1837 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1838 device_get_nameunit(sc->sc_dev)));
1840 sc->transfer_state = TSTATE_BBB_RESET1;
1841 sc->transfer_status = status;
1843 usbd_interface2device_handle(sc->iface, &udev);
1845 /* reset is a class specific interface write */
1846 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1847 sc->request.bRequest = UR_BBB_RESET;
1848 USETW(sc->request.wValue, 0);
1849 USETW(sc->request.wIndex, sc->ifaceno);
1850 USETW(sc->request.wLength, 0);
1851 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0,
1852 sc->transfer_xfer[XFER_BBB_RESET1]);
1856 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1857 void *data, int datalen, int dir, u_int timeout,
1858 transfer_cb_f cb, void *priv)
1860 KASSERT(sc->proto & UMASS_PROTO_BBB,
1861 ("%s: umass_bbb_transfer: wrong sc->proto 0x%02x",
1862 device_get_nameunit(sc->sc_dev), sc->proto));
1863 /* Be a little generous. */
1864 sc->timeout = timeout + UMASS_TIMEOUT;
1867 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1868 * a data phase of datalen bytes from/to the device and finally a
1870 * If the data direction was inbound a maximum of datalen bytes
1871 * is stored in the buffer pointed to by data.
1873 * umass_bbb_transfer initialises the transfer and lets the state
1874 * machine in umass_bbb_state handle the completion. It uses the
1876 * TSTATE_BBB_COMMAND
1877 * -> TSTATE_BBB_DATA
1878 * -> TSTATE_BBB_STATUS
1879 * -> TSTATE_BBB_STATUS2
1880 * -> TSTATE_BBB_IDLE
1882 * An error in any of those states will invoke
1886 /* check the given arguments */
1887 KASSERT(datalen == 0 || data != NULL,
1888 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
1889 KASSERT(cmdlen <= CBWCDBLENGTH,
1890 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1891 device_get_nameunit(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1892 KASSERT(dir == DIR_NONE || datalen > 0,
1893 ("%s: datalen == 0 while direction is not NONE",
1894 device_get_nameunit(sc->sc_dev)));
1895 KASSERT(datalen == 0 || dir != DIR_NONE,
1896 ("%s: direction is NONE while datalen is not zero",
1897 device_get_nameunit(sc->sc_dev)));
1898 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1899 ("%s: CBW struct does not have the right size (%ld vs. %d)",
1900 device_get_nameunit(sc->sc_dev),
1901 (long)sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1902 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1903 ("%s: CSW struct does not have the right size (%ld vs. %d)",
1904 device_get_nameunit(sc->sc_dev),
1905 (long)sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1908 * Determine the direction of the data transfer and the length.
1910 * dCBWDataTransferLength (datalen) :
1911 * This field indicates the number of bytes of data that the host
1912 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1913 * the Direction bit) during the execution of this command. If this
1914 * field is set to 0, the device will expect that no data will be
1915 * transferred IN or OUT during this command, regardless of the value
1916 * of the Direction bit defined in dCBWFlags.
1919 * The bits of the Flags field are defined as follows:
1921 * Bit 7 Direction - this bit shall be ignored if the
1922 * dCBWDataTransferLength field is zero.
1923 * 0 = data Out from host to device
1924 * 1 = data In from device to host
1927 /* Fill in the Command Block Wrapper
1928 * We fill in all the fields, so there is no need to bzero it first.
1930 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1931 /* We don't care about the initial value, as long as the values are unique */
1932 USETDW(sc->cbw.dCBWTag, UGETDW(sc->cbw.dCBWTag) + 1);
1933 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1934 /* DIR_NONE is treated as DIR_OUT (0x00) */
1935 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1936 sc->cbw.bCBWLUN = lun;
1937 sc->cbw.bCDBLength = cmdlen;
1938 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1940 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1942 /* store the details for the data transfer phase */
1943 sc->transfer_dir = dir;
1944 sc->transfer_data = data;
1945 sc->transfer_datalen = datalen;
1946 sc->transfer_actlen = 0;
1947 sc->transfer_cb = cb;
1948 sc->transfer_priv = priv;
1949 sc->transfer_status = STATUS_CMD_OK;
1951 /* move from idle to the command state */
1952 sc->transfer_state = TSTATE_BBB_COMMAND;
1954 /* Send the CBW from host to device via bulk-out endpoint. */
1955 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1956 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1957 sc->transfer_xfer[XFER_BBB_CBW])) {
1958 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1964 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1967 struct umass_softc *sc = (struct umass_softc *) priv;
1968 usbd_xfer_handle next_xfer;
1970 KASSERT(sc->proto & UMASS_PROTO_BBB,
1971 ("%s: umass_bbb_state: wrong sc->proto 0x%02x",
1972 device_get_nameunit(sc->sc_dev), sc->proto));
1975 * State handling for BBB transfers.
1977 * The subroutine is rather long. It steps through the states given in
1978 * Annex A of the Bulk-Only specification.
1979 * Each state first does the error handling of the previous transfer
1980 * and then prepares the next transfer.
1981 * Each transfer is done asynchroneously so after the request/transfer
1982 * has been submitted you will find a 'return;'.
1985 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1986 device_get_nameunit(sc->sc_dev), sc->transfer_state,
1987 states[sc->transfer_state], xfer, usbd_errstr(err)));
1989 switch (sc->transfer_state) {
1991 /***** Bulk Transfer *****/
1992 case TSTATE_BBB_COMMAND:
1993 /* Command transport phase, error handling */
1995 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1996 device_get_nameunit(sc->sc_dev)));
1997 /* If the device detects that the CBW is invalid, then
1998 * the device may STALL both bulk endpoints and require
2001 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2005 /* Data transport phase, setup transfer */
2006 sc->transfer_state = TSTATE_BBB_DATA;
2007 if (sc->transfer_dir == DIR_IN) {
2008 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2009 sc->transfer_data, sc->transfer_datalen,
2011 sc->transfer_xfer[XFER_BBB_DATA]))
2012 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2015 } else if (sc->transfer_dir == DIR_OUT) {
2016 if (umass_setup_transfer(sc, sc->bulkout_pipe,
2017 sc->transfer_data, sc->transfer_datalen,
2018 0, /* fixed length transfer */
2019 sc->transfer_xfer[XFER_BBB_DATA]))
2020 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2024 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
2025 device_get_nameunit(sc->sc_dev)));
2028 /* FALLTHROUGH if no data phase, err == 0 */
2029 case TSTATE_BBB_DATA:
2030 /* Command transport phase, error handling (ignored if no data
2031 * phase (fallthrough from previous state)) */
2032 if (sc->transfer_dir != DIR_NONE) {
2033 /* retrieve the length of the transfer that was done */
2034 usbd_get_xfer_status(xfer, NULL, NULL,
2035 &sc->transfer_actlen, NULL);
2038 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
2039 "%s\n", device_get_nameunit(sc->sc_dev),
2040 (sc->transfer_dir == DIR_IN?"in":"out"),
2041 sc->transfer_datalen,usbd_errstr(err)));
2043 if (err == USBD_STALLED) {
2044 umass_clear_endpoint_stall(sc,
2045 (sc->transfer_dir == DIR_IN?
2046 sc->bulkin:sc->bulkout),
2047 (sc->transfer_dir == DIR_IN?
2048 sc->bulkin_pipe:sc->bulkout_pipe),
2050 sc->transfer_xfer[XFER_BBB_DCLEAR]);
2053 /* Unless the error is a pipe stall the
2056 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
2062 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
2063 umass_dump_buffer(sc, sc->transfer_data,
2064 sc->transfer_datalen, 48));
2068 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
2069 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
2070 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
2071 /* Reading of CSW after bulk stall condition in data phase
2072 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
2073 * reading CSW (TSTATE_BBB_SCLEAR).
2074 * In the case of no data phase or successfull data phase,
2075 * err == 0 and the following if block is passed.
2077 if (err) { /* should not occur */
2078 /* try the transfer below, even if clear stall failed */
2079 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
2080 ", %s\n", device_get_nameunit(sc->sc_dev),
2081 (sc->transfer_dir == DIR_IN? "in":"out"),
2083 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2087 /* Status transport phase, setup transfer */
2088 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
2089 sc->transfer_state == TSTATE_BBB_DATA ||
2090 sc->transfer_state == TSTATE_BBB_DCLEAR) {
2091 /* After no data phase, successfull data phase and
2092 * after clearing bulk-in/-out stall condition
2094 sc->transfer_state = TSTATE_BBB_STATUS1;
2095 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
2097 /* After first attempt of fetching CSW */
2098 sc->transfer_state = TSTATE_BBB_STATUS2;
2099 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
2102 /* Read the Command Status Wrapper via bulk-in endpoint. */
2103 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2104 &sc->csw, UMASS_BBB_CSW_SIZE, 0,
2106 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2111 case TSTATE_BBB_STATUS1: /* first attempt */
2112 case TSTATE_BBB_STATUS2: /* second attempt */
2113 /* Status transfer, error handling */
2117 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
2118 device_get_nameunit(sc->sc_dev), usbd_errstr(err),
2119 (sc->transfer_state == TSTATE_BBB_STATUS1?
2122 /* If this was the first attempt at fetching the CSW
2123 * retry it, otherwise fail.
2125 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
2126 umass_clear_endpoint_stall(sc,
2127 sc->bulkin, sc->bulkin_pipe,
2129 sc->transfer_xfer[XFER_BBB_SCLEAR]);
2132 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2137 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2139 /* Translate weird command-status signatures. */
2140 if ((sc->quirks & WRONG_CSWSIG) &&
2141 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
2142 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2144 Residue = UGETDW(sc->csw.dCSWDataResidue);
2146 sc->transfer_datalen - sc->transfer_actlen != 0)
2147 Residue = sc->transfer_datalen - sc->transfer_actlen;
2149 /* Check CSW and handle any error */
2150 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2151 /* Invalid CSW: Wrong signature or wrong tag might
2152 * indicate that the device is confused -> reset it.
2154 kprintf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
2155 device_get_nameunit(sc->sc_dev),
2156 UGETDW(sc->csw.dCSWSignature),
2159 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2161 } else if (UGETDW(sc->csw.dCSWTag)
2162 != UGETDW(sc->cbw.dCBWTag)) {
2163 kprintf("%s: Invalid CSW: tag %d should be %d\n",
2164 device_get_nameunit(sc->sc_dev),
2165 UGETDW(sc->csw.dCSWTag),
2166 UGETDW(sc->cbw.dCBWTag));
2168 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2171 /* CSW is valid here */
2172 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2173 kprintf("%s: Invalid CSW: status %d > %d\n",
2174 device_get_nameunit(sc->sc_dev),
2178 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2180 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2181 kprintf("%s: Phase Error, residue = %d\n",
2182 device_get_nameunit(sc->sc_dev), Residue);
2184 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2187 } else if (sc->transfer_actlen > sc->transfer_datalen) {
2188 /* Buffer overrun! Don't let this go by unnoticed */
2189 panic("%s: transferred %db instead of %db",
2190 device_get_nameunit(sc->sc_dev),
2191 sc->transfer_actlen, sc->transfer_datalen);
2193 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2194 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
2195 device_get_nameunit(sc->sc_dev), Residue));
2197 /* SCSI command failed but transfer was succesful */
2198 sc->transfer_state = TSTATE_IDLE;
2199 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2203 } else { /* success */
2204 sc->transfer_state = TSTATE_IDLE;
2205 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2212 /***** Bulk Reset *****/
2213 case TSTATE_BBB_RESET1:
2215 kprintf("%s: BBB reset failed, %s\n",
2216 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2218 umass_clear_endpoint_stall(sc,
2219 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
2220 sc->transfer_xfer[XFER_BBB_RESET2]);
2223 case TSTATE_BBB_RESET2:
2224 if (err) /* should not occur */
2225 kprintf("%s: BBB bulk-in clear stall failed, %s\n",
2226 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2227 /* no error recovery, otherwise we end up in a loop */
2229 umass_clear_endpoint_stall(sc,
2230 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
2231 sc->transfer_xfer[XFER_BBB_RESET3]);
2234 case TSTATE_BBB_RESET3:
2235 if (err) /* should not occur */
2236 kprintf("%s: BBB bulk-out clear stall failed, %s\n",
2237 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2238 /* no error recovery, otherwise we end up in a loop */
2240 sc->transfer_state = TSTATE_IDLE;
2241 if (sc->transfer_priv) {
2242 sc->transfer_cb(sc, sc->transfer_priv,
2243 sc->transfer_datalen,
2244 sc->transfer_status);
2249 /***** Default *****/
2251 panic("%s: Unknown state %d",
2252 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2257 umass_bbb_get_max_lun(struct umass_softc *sc)
2259 usbd_device_handle udev;
2260 usb_device_request_t req;
2262 usb_interface_descriptor_t *id;
2266 usbd_interface2device_handle(sc->iface, &udev);
2267 id = usbd_get_interface_descriptor(sc->iface);
2269 /* The Get Max Lun command is a class-specific request. */
2270 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2271 req.bRequest = UR_BBB_GET_MAX_LUN;
2272 USETW(req.wValue, 0);
2273 USETW(req.wIndex, id->bInterfaceNumber);
2274 USETW(req.wLength, 1);
2276 err = usbd_do_request(udev, &req, &buf);
2278 case USBD_NORMAL_COMPLETION:
2280 DPRINTF(UDMASS_BBB, ("%s: Max Lun is %d\n",
2281 device_get_nameunit(sc->sc_dev), maxlun));
2284 case USBD_SHORT_XFER:
2286 /* Device doesn't support Get Max Lun request. */
2287 kprintf("%s: Get Max Lun not supported (%s)\n",
2288 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2289 /* XXX Should we port_reset the device? */
2297 * Command/Bulk/Interrupt (CBI) specific functions
2301 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
2302 usbd_xfer_handle xfer)
2304 usbd_device_handle udev;
2306 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2307 ("%s: umass_cbi_adsc: wrong sc->proto 0x%02x",
2308 device_get_nameunit(sc->sc_dev), sc->proto));
2310 usbd_interface2device_handle(sc->iface, &udev);
2312 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2313 sc->request.bRequest = UR_CBI_ADSC;
2314 USETW(sc->request.wValue, 0);
2315 USETW(sc->request.wIndex, sc->ifaceno);
2316 USETW(sc->request.wLength, buflen);
2317 return umass_setup_ctrl_transfer(sc, udev, &sc->request, buffer,
2323 umass_cbi_reset(struct umass_softc *sc, int status)
2326 # define SEND_DIAGNOSTIC_CMDLEN 12
2328 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2329 ("%s: umass_cbi_reset: wrong sc->proto 0x%02x",
2330 device_get_nameunit(sc->sc_dev), sc->proto));
2333 * Command Block Reset Protocol
2335 * First send a reset request to the device. Then clear
2336 * any possibly stalled bulk endpoints.
2338 * This is done in 3 steps, states:
2343 * If the reset doesn't succeed, the device should be port reset.
2346 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
2347 device_get_nameunit(sc->sc_dev)));
2349 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
2350 ("%s: CBL struct is too small (%ld < %d)",
2351 device_get_nameunit(sc->sc_dev),
2352 (long)sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
2354 sc->transfer_state = TSTATE_CBI_RESET1;
2355 sc->transfer_status = status;
2357 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
2358 * the two the last 10 bytes of the cbl is filled with 0xff (section
2359 * 2.2 of the CBI spec).
2361 sc->cbl[0] = 0x1d; /* Command Block Reset */
2363 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
2366 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
2367 sc->transfer_xfer[XFER_CBI_RESET1]);
2368 /* XXX if the command fails we should reset the port on the bub */
2372 umass_cbi_transfer(struct umass_softc *sc, int lun,
2373 void *cmd, int cmdlen, void *data, int datalen, int dir,
2374 u_int timeout, transfer_cb_f cb, void *priv)
2376 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2377 ("%s: umass_cbi_transfer: wrong sc->proto 0x%02x",
2378 device_get_nameunit(sc->sc_dev), sc->proto));
2379 /* Be a little generous. */
2380 sc->timeout = timeout + UMASS_TIMEOUT;
2383 * Do a CBI transfer with cmdlen bytes from cmd, possibly
2384 * a data phase of datalen bytes from/to the device and finally a
2386 * If the data direction was inbound a maximum of datalen bytes
2387 * is stored in the buffer pointed to by data.
2389 * umass_cbi_transfer initialises the transfer and lets the state
2390 * machine in umass_cbi_state handle the completion. It uses the
2392 * TSTATE_CBI_COMMAND
2395 * An error in any of those states will invoke
2399 /* check the given arguments */
2400 KASSERT(datalen == 0 || data != NULL,
2401 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
2402 KASSERT(datalen == 0 || dir != DIR_NONE,
2403 ("%s: direction is NONE while datalen is not zero",
2404 device_get_nameunit(sc->sc_dev)));
2406 /* store the details for the data transfer phase */
2407 sc->transfer_dir = dir;
2408 sc->transfer_data = data;
2409 sc->transfer_datalen = datalen;
2410 sc->transfer_actlen = 0;
2411 sc->transfer_cb = cb;
2412 sc->transfer_priv = priv;
2413 sc->transfer_status = STATUS_CMD_OK;
2415 /* move from idle to the command state */
2416 sc->transfer_state = TSTATE_CBI_COMMAND;
2418 DIF(UDMASS_CBI, umass_cbi_dump_cmd(sc, cmd, cmdlen));
2420 /* Send the Command Block from host to device via control endpoint. */
2421 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
2422 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2426 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
2429 struct umass_softc *sc = (struct umass_softc *) priv;
2431 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2432 ("%s: umass_cbi_state: wrong sc->proto 0x%02x",
2433 device_get_nameunit(sc->sc_dev), sc->proto));
2436 * State handling for CBI transfers.
2439 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
2440 device_get_nameunit(sc->sc_dev), sc->transfer_state,
2441 states[sc->transfer_state], xfer, usbd_errstr(err)));
2443 switch (sc->transfer_state) {
2445 /***** CBI Transfer *****/
2446 case TSTATE_CBI_COMMAND:
2447 if (err == USBD_STALLED) {
2448 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
2449 device_get_nameunit(sc->sc_dev)));
2450 /* Status transport by control pipe (section 2.3.2.1).
2451 * The command contained in the command block failed.
2453 * The control pipe has already been unstalled by the
2455 * Section 2.4.3.1.1 states that the bulk in endpoints
2456 * should not be stalled at this point.
2459 sc->transfer_state = TSTATE_IDLE;
2460 sc->transfer_cb(sc, sc->transfer_priv,
2461 sc->transfer_datalen,
2466 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
2467 device_get_nameunit(sc->sc_dev)));
2468 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2473 sc->transfer_state = TSTATE_CBI_DATA;
2474 if (sc->transfer_dir == DIR_IN) {
2475 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2476 sc->transfer_data, sc->transfer_datalen,
2478 sc->transfer_xfer[XFER_CBI_DATA]))
2479 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2481 } else if (sc->transfer_dir == DIR_OUT) {
2482 if (umass_setup_transfer(sc, sc->bulkout_pipe,
2483 sc->transfer_data, sc->transfer_datalen,
2484 0, /* fixed length transfer */
2485 sc->transfer_xfer[XFER_CBI_DATA]))
2486 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2488 } else if (sc->proto & UMASS_PROTO_CBI_I) {
2489 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2490 device_get_nameunit(sc->sc_dev)));
2491 sc->transfer_state = TSTATE_CBI_STATUS;
2492 if (umass_setup_transfer(sc, sc->intrin_pipe,
2493 &sc->sbl, sizeof(sc->sbl),
2494 0, /* fixed length transfer */
2495 sc->transfer_xfer[XFER_CBI_STATUS])){
2496 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2499 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2500 device_get_nameunit(sc->sc_dev)));
2501 /* No command completion interrupt. Request
2504 sc->transfer_state = TSTATE_IDLE;
2505 sc->transfer_cb(sc, sc->transfer_priv,
2506 0, STATUS_CMD_UNKNOWN);
2511 case TSTATE_CBI_DATA:
2512 /* retrieve the length of the transfer that was done */
2513 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
2516 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
2517 "%s\n", device_get_nameunit(sc->sc_dev),
2518 (sc->transfer_dir == DIR_IN?"in":"out"),
2519 sc->transfer_datalen,usbd_errstr(err)));
2521 if (err == USBD_STALLED) {
2522 umass_clear_endpoint_stall(sc,
2523 sc->bulkin, sc->bulkin_pipe,
2525 sc->transfer_xfer[XFER_CBI_DCLEAR]);
2527 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2532 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2533 umass_dump_buffer(sc, sc->transfer_data,
2534 sc->transfer_actlen, 48));
2536 if (sc->proto & UMASS_PROTO_CBI_I) {
2537 sc->transfer_state = TSTATE_CBI_STATUS;
2538 if (umass_setup_transfer(sc, sc->intrin_pipe,
2539 &sc->sbl, sizeof(sc->sbl),
2540 0, /* fixed length transfer */
2541 sc->transfer_xfer[XFER_CBI_STATUS])){
2542 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2545 /* No command completion interrupt. Request
2546 * sense to get status of command.
2548 sc->transfer_state = TSTATE_IDLE;
2549 sc->transfer_cb(sc, sc->transfer_priv,
2550 sc->transfer_datalen - sc->transfer_actlen,
2551 STATUS_CMD_UNKNOWN);
2555 case TSTATE_CBI_STATUS:
2557 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2558 device_get_nameunit(sc->sc_dev)));
2559 /* Status transport by interrupt pipe (section 2.3.2.2).
2562 if (err == USBD_STALLED) {
2563 umass_clear_endpoint_stall(sc,
2564 sc->intrin, sc->intrin_pipe,
2566 sc->transfer_xfer[XFER_CBI_SCLEAR]);
2568 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2573 /* Dissect the information in the buffer */
2575 if (sc->proto & UMASS_PROTO_UFI) {
2578 /* Section 3.4.3.1.3 specifies that the UFI command
2579 * protocol returns an ASC and ASCQ in the interrupt
2583 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2585 device_get_nameunit(sc->sc_dev),
2586 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2588 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2589 status = STATUS_CMD_OK;
2591 status = STATUS_CMD_FAILED;
2593 sc->transfer_state = TSTATE_IDLE;
2594 sc->transfer_cb(sc, sc->transfer_priv,
2595 sc->transfer_datalen - sc->transfer_actlen,
2598 /* Command Interrupt Data Block */
2599 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2600 device_get_nameunit(sc->sc_dev),
2601 sc->sbl.common.type, sc->sbl.common.value));
2603 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2606 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2607 == IDB_VALUE_PASS) {
2608 err = STATUS_CMD_OK;
2609 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2610 == IDB_VALUE_FAIL ||
2611 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2612 == IDB_VALUE_PERSISTENT) {
2613 err = STATUS_CMD_FAILED;
2615 err = STATUS_WIRE_FAILED;
2618 sc->transfer_state = TSTATE_IDLE;
2619 sc->transfer_cb(sc, sc->transfer_priv,
2620 sc->transfer_datalen-sc->transfer_actlen,
2626 case TSTATE_CBI_DCLEAR:
2627 if (err) { /* should not occur */
2628 kprintf("%s: CBI bulk-in/out stall clear failed, %s\n",
2629 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2630 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2633 sc->transfer_state = TSTATE_IDLE;
2634 sc->transfer_cb(sc, sc->transfer_priv,
2635 sc->transfer_datalen,
2639 case TSTATE_CBI_SCLEAR:
2640 if (err) /* should not occur */
2641 kprintf("%s: CBI intr-in stall clear failed, %s\n",
2642 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2644 /* Something really bad is going on. Reset the device */
2645 umass_cbi_reset(sc, STATUS_CMD_FAILED);
2648 /***** CBI Reset *****/
2649 case TSTATE_CBI_RESET1:
2651 kprintf("%s: CBI reset failed, %s\n",
2652 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2654 umass_clear_endpoint_stall(sc,
2655 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2656 sc->transfer_xfer[XFER_CBI_RESET2]);
2659 case TSTATE_CBI_RESET2:
2660 if (err) /* should not occur */
2661 kprintf("%s: CBI bulk-in stall clear failed, %s\n",
2662 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2663 /* no error recovery, otherwise we end up in a loop */
2665 umass_clear_endpoint_stall(sc,
2666 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2667 sc->transfer_xfer[XFER_CBI_RESET3]);
2670 case TSTATE_CBI_RESET3:
2671 if (err) /* should not occur */
2672 kprintf("%s: CBI bulk-out stall clear failed, %s\n",
2673 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2674 /* no error recovery, otherwise we end up in a loop */
2676 sc->transfer_state = TSTATE_IDLE;
2677 if (sc->transfer_priv) {
2678 sc->transfer_cb(sc, sc->transfer_priv,
2679 sc->transfer_datalen,
2680 sc->transfer_status);
2686 /***** Default *****/
2688 panic("%s: Unknown state %d",
2689 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2697 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2701 umass_cam_attach_sim(struct umass_softc *sc)
2703 struct cam_devq *devq; /* Per device Queue */
2705 /* A HBA is attached to the CAM layer.
2707 * The CAM layer will then after a while start probing for
2708 * devices on the bus. The number of SIMs is limited to one.
2711 callout_init(&sc->rescan_timeout);
2712 devq = cam_simq_alloc(1 /*maximum openings*/);
2716 sc->umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll,
2719 device_get_unit(sc->sc_dev) /*unit number*/,
2721 1 /*maximum device openings*/,
2722 0 /*maximum tagged device openings*/,
2724 cam_simq_release(devq);
2725 if (sc->umass_sim == NULL)
2729 * If we could not register the bus we must immediately free the
2730 * sim so we do not attempt to deregister a bus later on that we
2731 * had not registered.
2733 if (xpt_bus_register(sc->umass_sim, device_get_unit(sc->sc_dev)) !=
2735 cam_sim_free(sc->umass_sim);
2736 sc->umass_sim = NULL;
2744 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2747 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2748 DPRINTF(UDMASS_SCSI, ("%s:%d Rescan failed, 0x%04x\n",
2749 periph->periph_name, periph->unit_number,
2750 ccb->ccb_h.status));
2752 DPRINTF(UDMASS_SCSI, ("%s%d: Rescan succeeded\n",
2753 periph->periph_name, periph->unit_number));
2757 xpt_free_path(ccb->ccb_h.path);
2758 kfree(ccb, M_USBDEV);
2762 * Rescan the SCSI bus to detect newly added devices. We use
2763 * an async rescan to avoid reentrancy issues.
2766 umass_cam_rescan(void *addr)
2768 struct umass_softc *sc = (struct umass_softc *) addr;
2769 struct cam_path *path;
2772 ccb = kmalloc(sizeof(union ccb), M_USBDEV, M_INTWAIT|M_ZERO);
2774 DPRINTF(UDMASS_SCSI, ("scbus%d: scanning for %s:%d:%d:%d\n",
2775 cam_sim_path(sc->umass_sim),
2776 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2777 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD));
2779 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->umass_sim),
2780 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2782 kfree(ccb, M_USBDEV);
2786 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2787 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2788 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2789 ccb->crcn.flags = CAM_FLAG_NONE;
2790 xpt_action_async(ccb);
2792 /* The scan is in progress now. */
2796 umass_cam_attach(struct umass_softc *sc)
2801 kprintf("%s:%d:%d:%d: Attached to scbus%d\n",
2802 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2803 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD,
2804 cam_sim_path(sc->umass_sim));
2808 * Notify CAM of the new device after a 0.2 second delay. Any
2809 * failure is benign, as the user can still do it by hand
2810 * (camcontrol rescan <busno>). Only do this if we are not
2811 * booting, because CAM does a scan after booting has
2812 * completed, when interrupts have been enabled.
2814 callout_reset(&sc->rescan_timeout, MS_TO_TICKS(200),
2815 umass_cam_rescan, sc);
2818 return(0); /* always succesfull */
2822 * detach from the CAM layer
2826 umass_cam_detach_sim(struct umass_softc *sc)
2828 callout_stop(&sc->rescan_timeout);
2829 if (sc->umass_sim) {
2830 xpt_bus_deregister(cam_sim_path(sc->umass_sim));
2831 cam_sim_free(sc->umass_sim);
2833 sc->umass_sim = NULL;
2840 * CAM requests for action come through here
2844 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2846 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2848 /* The softc is still there, but marked as going away. umass_cam_detach
2849 * has not yet notified CAM of the lost device however.
2851 if (sc && (sc->flags & UMASS_FLAGS_GONE)) {
2852 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2853 "Invalid target (gone)\n",
2854 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2855 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2856 ccb->ccb_h.func_code));
2857 ccb->ccb_h.status = CAM_TID_INVALID;
2862 /* Verify, depending on the operation to perform, that we either got a
2863 * valid sc, because an existing target was referenced, or otherwise
2864 * the SIM is addressed.
2866 * This avoids bombing out at a kprintf and does give the CAM layer some
2867 * sensible feedback on errors.
2869 switch (ccb->ccb_h.func_code) {
2872 case XPT_GET_TRAN_SETTINGS:
2873 case XPT_SET_TRAN_SETTINGS:
2874 case XPT_CALC_GEOMETRY:
2875 /* the opcodes requiring a target. These should never occur. */
2877 kprintf("%s:%d:%d:%d:func_code 0x%04x: "
2878 "Invalid target (target needed)\n",
2879 DEVNAME_SIM, 0, ccb->ccb_h.target_id,
2880 ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
2882 ccb->ccb_h.status = CAM_TID_INVALID;
2889 /* The opcodes sometimes aimed at a target (sc is valid),
2890 * sometimes aimed at the SIM (sc is invalid and target is
2891 * CAM_TARGET_WILDCARD)
2893 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2894 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2895 "Invalid target (no wildcard)\n",
2896 DEVNAME_SIM, 0, ccb->ccb_h.target_id,
2897 ccb->ccb_h.target_lun, ccb->ccb_h.func_code));
2899 ccb->ccb_h.status = CAM_TID_INVALID;
2905 /* XXX Hm, we should check the input parameters */
2909 /* Perform the requested action */
2910 switch (ccb->ccb_h.func_code) {
2913 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2917 unsigned char *rcmd;
2920 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2921 "cmd: 0x%02x, flags: 0x%02x, "
2922 "%db cmd/%db data/%db sense\n",
2923 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2924 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2925 csio->cdb_io.cdb_bytes[0],
2926 ccb->ccb_h.flags & CAM_DIR_MASK,
2927 csio->cdb_len, csio->dxfer_len,
2930 /* clear the end of the buffer to make sure we don't send out
2933 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2935 umass_dump_buffer(sc, csio->data_ptr,
2936 csio->dxfer_len, 48));
2938 if (sc->transfer_state != TSTATE_IDLE) {
2939 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2940 "I/O in progress, deferring (state %d, %s)\n",
2941 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2942 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2943 sc->transfer_state,states[sc->transfer_state]));
2944 ccb->ccb_h.status = CAM_SCSI_BUSY;
2949 switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2960 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2963 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2964 cmd = (unsigned char *) csio->cdb_io.cdb_ptr;
2966 cmd = (unsigned char *) &csio->cdb_io.cdb_bytes;
2968 cmdlen = csio->cdb_len;
2969 rcmd = (unsigned char *) &sc->cam_scsi_command;
2970 rcmdlen = sizeof(sc->cam_scsi_command);
2972 /* sc->transform will convert the command to the command
2973 * (format) needed by the specific command set and return
2974 * the converted command in a buffer pointed to be rcmd.
2975 * We pass in a buffer, but if the command does not
2976 * have to be transformed it returns a ptr to the original
2977 * buffer (see umass_scsi_transform).
2980 if (sc->transform(sc, cmd, cmdlen, &rcmd, &rcmdlen)) {
2982 * Handle EVPD inquiry for broken devices first
2983 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2985 if ((sc->quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2986 rcmd[0] == INQUIRY && (rcmd[1] & SI_EVPD)) {
2987 struct scsi_sense_data *sense;
2989 sense = &ccb->csio.sense_data;
2990 bzero(sense, sizeof(*sense));
2991 sense->error_code = SSD_CURRENT_ERROR;
2992 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2993 sense->add_sense_code = 0x24;
2994 sense->extra_len = 10;
2995 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2996 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
3001 /* Return fake inquiry data for broken devices */
3002 if ((sc->quirks & NO_INQUIRY) && rcmd[0] == INQUIRY) {
3003 struct ccb_scsiio *csio = &ccb->csio;
3005 memcpy(csio->data_ptr, &fake_inq_data,
3006 sizeof(fake_inq_data));
3007 csio->scsi_status = SCSI_STATUS_OK;
3008 ccb->ccb_h.status = CAM_REQ_CMP;
3012 if ((sc->quirks & FORCE_SHORT_INQUIRY) &&
3013 rcmd[0] == INQUIRY) {
3014 csio->dxfer_len = SHORT_INQUIRY_LENGTH;
3016 sc->transfer(sc, ccb->ccb_h.target_lun, rcmd, rcmdlen,
3018 csio->dxfer_len, dir, ccb->ccb_h.timeout,
3019 umass_cam_cb, (void *) ccb);
3021 ccb->ccb_h.status = CAM_REQ_INVALID;
3029 struct ccb_pathinq *cpi = &ccb->cpi;
3031 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
3032 (sc == NULL ? DEVNAME_SIM : device_get_nameunit(sc->sc_dev)),
3033 (sc == NULL ? 0 : cam_sim_path(sc->umass_sim)),
3034 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3036 /* host specific information */
3037 cpi->version_num = 1;
3038 cpi->hba_inquiry = 0;
3039 cpi->target_sprt = 0;
3040 cpi->hba_misc = PIM_NO_6_BYTE;
3041 cpi->hba_eng_cnt = 0;
3042 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
3043 cpi->initiator_id = UMASS_SCSIID_HOST;
3044 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3045 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
3046 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3047 cpi->unit_number = cam_sim_unit(sim);
3050 cpi->base_transfer_speed = 0;
3053 cpi->bus_id = device_get_unit(sc->sc_dev);
3055 if (sc->quirks & FLOPPY_SPEED) {
3056 cpi->base_transfer_speed =
3057 UMASS_FLOPPY_TRANSFER_SPEED;
3058 } else if (usbd_get_speed(sc->sc_udev) ==
3060 cpi->base_transfer_speed =
3061 UMASS_HIGH_TRANSFER_SPEED;
3063 cpi->base_transfer_speed =
3064 UMASS_FULL_TRANSFER_SPEED;
3066 cpi->max_lun = sc->maxlun;
3069 cpi->ccb_h.status = CAM_REQ_CMP;
3075 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
3076 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3077 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3079 ccb->ccb_h.status = CAM_REQ_INPROG;
3080 umass_reset(sc, umass_cam_cb, ccb);
3083 case XPT_GET_TRAN_SETTINGS:
3085 struct ccb_trans_settings *cts = &ccb->cts;
3086 cts->protocol = PROTO_SCSI;
3087 cts->protocol_version = SCSI_REV_2;
3088 cts->transport = XPORT_USB;
3089 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
3090 cts->xport_specific.valid = 0;
3092 ccb->ccb_h.status = CAM_REQ_CMP;
3096 case XPT_SET_TRAN_SETTINGS:
3098 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3099 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3100 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3102 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3106 case XPT_CALC_GEOMETRY:
3108 cam_calc_geometry(&ccb->ccg, /*extended*/1);
3114 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
3115 (sc == NULL ? DEVNAME_SIM : device_get_nameunit(sc->sc_dev)),
3116 (sc == NULL ? 0 : cam_sim_path(sc->umass_sim)),
3117 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3119 ccb->ccb_h.status = CAM_REQ_CMP;
3124 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
3125 "Not implemented\n",
3126 (sc == NULL ? DEVNAME_SIM : device_get_nameunit(sc->sc_dev)),
3127 (sc == NULL ? 0 : cam_sim_path(sc->umass_sim)),
3128 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
3129 ccb->ccb_h.func_code));
3131 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3138 umass_cam_poll(struct cam_sim *sim)
3140 struct umass_softc *sc = (struct umass_softc *) sim->softc;
3142 KKASSERT(sc != NULL);
3144 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
3145 device_get_nameunit(sc->sc_dev)));
3147 usbd_set_polling(sc->sc_udev, 1);
3148 usbd_dopoll(sc->iface);
3149 usbd_set_polling(sc->sc_udev, 0);
3154 * finalise a completed CAM command
3158 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
3160 union ccb *ccb = (union ccb *) priv;
3161 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3163 csio->resid = residue;
3167 ccb->ccb_h.status = CAM_REQ_CMP;
3168 if ((sc->quirks & READ_CAPACITY_OFFBY1) &&
3169 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3170 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3171 struct scsi_read_capacity_data *rcap;
3174 rcap = (struct scsi_read_capacity_data *)csio->data_ptr;
3175 maxsector = scsi_4btoul(rcap->addr) - 1;
3176 scsi_ulto4b(maxsector, rcap->addr);
3181 case STATUS_CMD_UNKNOWN:
3182 case STATUS_CMD_FAILED:
3183 switch (ccb->ccb_h.func_code) {
3186 unsigned char *rcmd;
3189 /* fetch sense data */
3190 /* the rest of the command was filled in at attach */
3191 sc->cam_scsi_sense.length = csio->sense_len;
3193 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
3194 device_get_nameunit(sc->sc_dev), csio->sense_len));
3196 rcmd = (unsigned char *) &sc->cam_scsi_command;
3197 rcmdlen = sizeof(sc->cam_scsi_command);
3199 if (sc->transform(sc,
3200 (unsigned char *) &sc->cam_scsi_sense,
3201 sizeof(sc->cam_scsi_sense),
3203 if ((sc->quirks & FORCE_SHORT_INQUIRY) && (rcmd[0] == INQUIRY)) {
3204 csio->sense_len = SHORT_INQUIRY_LENGTH;
3206 sc->transfer(sc, ccb->ccb_h.target_lun,
3209 csio->sense_len, DIR_IN, ccb->ccb_h.timeout,
3210 umass_cam_sense_cb, (void *) ccb);
3212 panic("transform(REQUEST_SENSE) failed");
3216 case XPT_RESET_DEV: /* Reset failed */
3217 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3221 panic("umass_cam_cb called for func_code %d",
3222 ccb->ccb_h.func_code);
3226 case STATUS_WIRE_FAILED:
3227 /* the wire protocol failed and will have recovered
3228 * (hopefully). We return an error to CAM and let CAM retry
3229 * the command if necessary.
3231 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3235 panic("%s: Unknown status %d in umass_cam_cb",
3236 device_get_nameunit(sc->sc_dev), status);
3240 /* Finalise a completed autosense operation
3243 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
3245 union ccb *ccb = (union ccb *) priv;
3246 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3247 unsigned char *rcmd;
3252 case STATUS_CMD_UNKNOWN:
3253 case STATUS_CMD_FAILED:
3254 /* Getting sense data always succeeds (apart from wire
3257 if ((sc->quirks & RS_NO_CLEAR_UA)
3258 && csio->cdb_io.cdb_bytes[0] == INQUIRY
3259 && (csio->sense_data.flags & SSD_KEY)
3260 == SSD_KEY_UNIT_ATTENTION) {
3261 /* Ignore unit attention errors in the case where
3262 * the Unit Attention state is not cleared on
3263 * REQUEST SENSE. They will appear again at the next
3266 ccb->ccb_h.status = CAM_REQ_CMP;
3267 } else if ((csio->sense_data.flags & SSD_KEY)
3268 == SSD_KEY_NO_SENSE) {
3269 /* No problem after all (in the case of CBI without
3272 ccb->ccb_h.status = CAM_REQ_CMP;
3273 } else if ((sc->quirks & RS_NO_CLEAR_UA) &&
3274 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY) &&
3275 ((csio->sense_data.flags & SSD_KEY)
3276 == SSD_KEY_UNIT_ATTENTION)) {
3278 * Some devices do not clear the unit attention error
3279 * on request sense. We insert a test unit ready
3280 * command to make sure we clear the unit attention
3281 * condition, then allow the retry to proceed as
3285 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3286 | CAM_AUTOSNS_VALID;
3287 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3293 DPRINTF(UDMASS_SCSI,("%s: Doing a sneaky"
3294 "TEST_UNIT_READY\n",
3295 device_get_nameunit(sc->sc_dev)));
3297 /* the rest of the command was filled in at attach */
3299 rcmd = (unsigned char *) &sc->cam_scsi_command2;
3300 rcmdlen = sizeof(sc->cam_scsi_command2);
3302 if (sc->transform(sc,
3304 &sc->cam_scsi_test_unit_ready,
3305 sizeof(sc->cam_scsi_test_unit_ready),
3307 sc->transfer(sc, ccb->ccb_h.target_lun,
3309 NULL, 0, DIR_NONE, ccb->ccb_h.timeout,
3310 umass_cam_quirk_cb, (void *) ccb);
3312 panic("transform(TEST_UNIT_READY) failed");
3316 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3317 | CAM_AUTOSNS_VALID;
3318 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3324 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
3325 device_get_nameunit(sc->sc_dev), status));
3326 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3332 * This completion code just handles the fact that we sent a test-unit-ready
3333 * after having previously failed a READ CAPACITY with CHECK_COND. Even
3334 * though this command succeeded, we have to tell CAM to retry.
3337 umass_cam_quirk_cb(struct umass_softc *sc, void *priv, int residue, int status)
3339 union ccb *ccb = (union ccb *) priv;
3341 DPRINTF(UDMASS_SCSI, ("%s: Test unit ready returned status %d\n",
3342 device_get_nameunit(sc->sc_dev), status));
3344 ccb->ccb_h.status = CAM_REQ_CMP;
3346 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3347 | CAM_AUTOSNS_VALID;
3348 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3353 umass_driver_load(module_t mod, int what, void *arg)
3359 return(usbd_driver_load(mod, what, arg));
3364 * SCSI specific functions
3368 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3369 unsigned char **rcmd, int *rcmdlen)
3372 case TEST_UNIT_READY:
3373 if (sc->quirks & NO_TEST_UNIT_READY) {
3374 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3375 ("rcmdlen = %d < %ld, buffer too small",
3377 (long)sizeof(struct scsi_start_stop_unit)));
3378 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3379 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3380 memset(*rcmd, 0, *rcmdlen);
3381 (*rcmd)[0] = START_STOP_UNIT;
3382 (*rcmd)[4] = SSS_START;
3387 /* some drives wedge when asked for full inquiry information. */
3388 if (sc->quirks & FORCE_SHORT_INQUIRY) {
3389 memcpy(*rcmd, cmd, cmdlen);
3391 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3396 *rcmd = cmd; /* We don't need to copy it */
3402 /* RBC specific functions */
3404 umass_rbc_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3405 unsigned char **rcmd, int *rcmdlen)
3408 /* these commands are defined in RBC: */
3411 case START_STOP_UNIT:
3412 case SYNCHRONIZE_CACHE:
3414 case 0x2f: /* VERIFY_10 is absent from scsi_all.h??? */
3416 case MODE_SELECT_10:
3418 case TEST_UNIT_READY:
3420 /* The following commands are not listed in my copy of the RBC specs.
3421 * CAM however seems to want those, and at least the Sony DSC device
3422 * appears to support those as well */
3425 *rcmd = cmd; /* We don't need to copy it */
3428 /* All other commands are not legal in RBC */
3430 kprintf("%s: Unsupported RBC command 0x%02x",
3431 device_get_nameunit(sc->sc_dev), cmd[0]);
3433 return 0; /* failure */
3438 * UFI specific functions
3441 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3442 unsigned char **rcmd, int *rcmdlen)
3444 /* A UFI command is always 12 bytes in length */
3445 KASSERT(*rcmdlen >= UFI_COMMAND_LENGTH,
3446 ("rcmdlen = %d < %d, buffer too small",
3447 *rcmdlen, UFI_COMMAND_LENGTH));
3449 *rcmdlen = UFI_COMMAND_LENGTH;
3450 memset(*rcmd, 0, UFI_COMMAND_LENGTH);
3453 /* Commands of which the format has been verified. They should work.
3454 * Copy the command into the (zeroed out) destination buffer.
3456 case TEST_UNIT_READY:
3457 if (sc->quirks & NO_TEST_UNIT_READY) {
3458 /* Some devices do not support this command.
3459 * Start Stop Unit should give the same results
3461 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
3462 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3463 (*rcmd)[0] = START_STOP_UNIT;
3464 (*rcmd)[4] = SSS_START;
3466 memcpy(*rcmd, cmd, cmdlen);
3473 case START_STOP_UNIT:
3474 case SEND_DIAGNOSTIC:
3479 case POSITION_TO_ELEMENT: /* SEEK_10 */
3480 case MODE_SELECT_10:
3484 memcpy(*rcmd, cmd, cmdlen);
3487 /* Other UFI commands: FORMAT_UNIT, READ_FORMAT_CAPACITY,
3488 * VERIFY, WRITE_AND_VERIFY.
3489 * These should be checked whether they somehow can be made to fit.
3493 kprintf("%s: Unsupported UFI command 0x%02x\n",
3494 device_get_nameunit(sc->sc_dev), cmd[0]);
3495 return 0; /* failure */
3500 * 8070i (ATAPI) specific functions
3503 umass_atapi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3504 unsigned char **rcmd, int *rcmdlen)
3506 /* An ATAPI command is always 12 bytes in length. */
3507 KASSERT(*rcmdlen >= ATAPI_COMMAND_LENGTH,
3508 ("rcmdlen = %d < %d, buffer too small",
3509 *rcmdlen, ATAPI_COMMAND_LENGTH));
3511 *rcmdlen = ATAPI_COMMAND_LENGTH;
3512 memset(*rcmd, 0, ATAPI_COMMAND_LENGTH);
3515 /* Commands of which the format has been verified. They should work.
3516 * Copy the command into the (zeroed out) destination buffer.
3519 memcpy(*rcmd, cmd, cmdlen);
3520 /* some drives wedge when asked for full inquiry information. */
3521 if (sc->quirks & FORCE_SHORT_INQUIRY)
3522 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3525 case TEST_UNIT_READY:
3526 if (sc->quirks & NO_TEST_UNIT_READY) {
3527 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3528 ("rcmdlen = %d < %ld, buffer too small",
3530 (long)sizeof(struct scsi_start_stop_unit)));
3531 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3532 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3533 memset(*rcmd, 0, *rcmdlen);
3534 (*rcmd)[0] = START_STOP_UNIT;
3535 (*rcmd)[4] = SSS_START;
3541 * All commands are passed through, very likely it will just work
3542 * regardless whether we know these commands or not.
3544 memcpy(*rcmd, cmd, cmdlen);
3550 /* (even the comment is missing) */
3552 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, NULL);
3558 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3560 int clen = cbw->bCDBLength;
3561 int dlen = UGETDW(cbw->dCBWDataTransferLength);
3562 u_int8_t *c = cbw->CBWCDB;
3563 int tag = UGETDW(cbw->dCBWTag);
3564 int flags = cbw->bCBWFlags;
3566 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
3567 "(0x%02x%02x%02x%02x%02x%02x%s), "
3568 "data = %db, dir = %s\n",
3569 device_get_nameunit(sc->sc_dev), tag, clen,
3570 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
3571 dlen, (flags == CBWFLAGS_IN? "in":
3572 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
3576 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3578 int sig = UGETDW(csw->dCSWSignature);
3579 int tag = UGETW(csw->dCSWTag);
3580 int res = UGETDW(csw->dCSWDataResidue);
3581 int status = csw->bCSWStatus;
3583 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
3584 "res = %d, status = 0x%02x (%s)\n", device_get_nameunit(sc->sc_dev),
3585 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
3587 status, (status == CSWSTATUS_GOOD? "good":
3588 (status == CSWSTATUS_FAILED? "failed":
3589 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
3593 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, int cmdlen)
3596 int dir = sc->transfer_dir;
3598 DPRINTF(UDMASS_BBB, ("%s: cmd = %db "
3599 "(0x%02x%02x%02x%02x%02x%02x%s), "
3600 "data = %db, dir = %s\n",
3601 device_get_nameunit(sc->sc_dev), cmdlen,
3602 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6? "...":""),
3603 sc->transfer_datalen,
3604 (dir == DIR_IN? "in":
3605 (dir == DIR_OUT? "out":
3606 (dir == DIR_NONE? "no data phase": "<invalid>")))));
3610 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
3621 ksprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3622 for (i = 0; i < buflen && i < printlen; i++) {
3624 if (j == 0 && i != 0) {
3625 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3626 device_get_nameunit(sc->sc_dev), s1, s2));
3629 ksprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3631 if (buflen > printlen)
3632 ksprintf(s3, " ...");
3633 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3634 device_get_nameunit(sc->sc_dev), s1, s2, s3));