Make CAM_NEW_TRAN_CODE default.
[dragonfly.git] / sys / dev / usbmisc / umass / umass.c
blob8e53c842f0750ada3c4b11aef9f8ce677790b5c2
1 /*-
2 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
3 * Nick Hibma <n_hibma@freebsd.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
27 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
28 * $FreeBSD: src/sys/dev/usb/umass.c,v 1.96 2003/12/19 12:19:11 sanpei Exp $
29 * $DragonFly: src/sys/dev/usbmisc/umass/umass.c,v 1.37 2008/02/10 00:01:03 pavalos Exp $
33 * Universal Serial Bus Mass Storage Class specs:
34 * http://www.usb.org/developers/data/devclass/usbmassover_11.pdf
35 * http://www.usb.org/developers/data/devclass/usbmassbulk_10.pdf
36 * http://www.usb.org/developers/data/devclass/usbmass-cbi10.pdf
37 * http://www.usb.org/developers/data/devclass/usbmass-ufi10.pdf
41 * Ported to NetBSD by Lennart Augustsson <augustss@netbsd.org>.
42 * Parts of the code written my Jason R. Thorpe <thorpej@shagadelic.org>.
46 * The driver handles 3 Wire Protocols
47 * - Command/Bulk/Interrupt (CBI)
48 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
49 * - Mass Storage Bulk-Only (BBB)
50 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
52 * Over these wire protocols it handles the following command protocols
53 * - SCSI
54 * - UFI (floppy command set)
55 * - 8070i (ATAPI)
57 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
58 * sc->transform method is used to convert the commands into the appropriate
59 * format (if at all necessary). For example, UFI requires all commands to be
60 * 12 bytes in length amongst other things.
62 * The source code below is marked and can be split into a number of pieces
63 * (in this order):
65 * - probe/attach/detach
66 * - generic transfer routines
67 * - BBB
68 * - CBI
69 * - CBI_I (in addition to functions from CBI)
70 * - CAM (Common Access Method)
71 * - SCSI
72 * - UFI
73 * - 8070i (ATAPI)
75 * The protocols are implemented using a state machine, for the transfers as
76 * well as for the resets. The state machine is contained in umass_*_state.
77 * The state machine is started through either umass_*_transfer or
78 * umass_*_reset.
80 * The reason for doing this is a) CAM performs a lot better this way and b) it
81 * avoids using tsleep from interrupt context (for example after a failed
82 * transfer).
86 * The SCSI related part of this driver has been derived from the
87 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
89 * The CAM layer uses so called actions which are messages sent to the host
90 * adapter for completion. The actions come in through umass_cam_action. The
91 * appropriate block of routines is called depending on the transport protocol
92 * in use. When the transfer has finished, these routines call
93 * umass_cam_cb again to complete the CAM command.
97 * XXX Currently CBI with CCI is not supported because it bombs the system
98 * when the device is detached (low frequency interrupts are detached
99 * too late.
101 #undef CBI_I
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/module.h>
107 #include <sys/bus.h>
108 #include <sys/sysctl.h>
110 #include <bus/usb/usb.h>
111 #include <bus/usb/usbdi.h>
112 #include <bus/usb/usbdi_util.h>
114 #include <bus/cam/cam.h>
115 #include <bus/cam/cam_ccb.h>
116 #include <bus/cam/cam_sim.h>
117 #include <bus/cam/cam_xpt_sim.h>
118 #include <bus/cam/scsi/scsi_all.h>
119 #include <bus/cam/scsi/scsi_da.h>
120 #include <bus/cam/scsi/scsi_cd.h>
121 #include <bus/cam/scsi/scsi_ch.h>
122 #include <dev/disk/ata/atapi-all.h>
124 #include <bus/cam/cam_periph.h>
126 #ifdef USB_DEBUG
127 #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0)
128 #define DPRINTF(m, x) if (umassdebug & (m)) kprintf x
129 #define UDMASS_GEN 0x00010000 /* general */
130 #define UDMASS_SCSI 0x00020000 /* scsi */
131 #define UDMASS_UFI 0x00040000 /* ufi command set */
132 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
133 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
134 #define UDMASS_USB 0x00100000 /* USB general */
135 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
136 #define UDMASS_CBI 0x00400000 /* CBI transfers */
137 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
138 #define UDMASS_ALL 0xffff0000 /* all of the above */
139 int umassdebug = 0;
140 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
141 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
142 &umassdebug, 0, "umass debug level");
143 #else
144 #define DIF(m, x) /* nop */
145 #define DPRINTF(m, x) /* nop */
146 #endif
149 /* Generic definitions */
151 /* Direction for umass_*_transfer */
152 #define DIR_NONE 0
153 #define DIR_IN 1
154 #define DIR_OUT 2
156 /* device name */
157 #define DEVNAME "umass"
158 #define DEVNAME_SIM "umass-sim"
160 #define UMASS_MAX_TRANSFER_SIZE 65536
161 /* Approximate maximum transfer speeds (assumes 33% overhead). */
162 #define UMASS_FULL_TRANSFER_SPEED 1000
163 #define UMASS_HIGH_TRANSFER_SPEED 40000
164 #define UMASS_FLOPPY_TRANSFER_SPEED 20
166 #define UMASS_TIMEOUT 5000 /* msecs */
168 /* CAM specific definitions */
170 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
171 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
173 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
176 /* Bulk-Only features */
178 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
179 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
181 /* Command Block Wrapper */
182 typedef struct {
183 uDWord dCBWSignature;
184 # define CBWSIGNATURE 0x43425355
185 uDWord dCBWTag;
186 uDWord dCBWDataTransferLength;
187 uByte bCBWFlags;
188 # define CBWFLAGS_OUT 0x00
189 # define CBWFLAGS_IN 0x80
190 uByte bCBWLUN;
191 uByte bCDBLength;
192 # define CBWCDBLENGTH 16
193 uByte CBWCDB[CBWCDBLENGTH];
194 } umass_bbb_cbw_t;
195 #define UMASS_BBB_CBW_SIZE 31
197 /* Command Status Wrapper */
198 typedef struct {
199 uDWord dCSWSignature;
200 # define CSWSIGNATURE 0x53425355
201 # define CSWSIGNATURE_OLYMPUS_C1 0x55425355
202 uDWord dCSWTag;
203 uDWord dCSWDataResidue;
204 uByte bCSWStatus;
205 # define CSWSTATUS_GOOD 0x0
206 # define CSWSTATUS_FAILED 0x1
207 # define CSWSTATUS_PHASE 0x2
208 } umass_bbb_csw_t;
209 #define UMASS_BBB_CSW_SIZE 13
211 /* CBI features */
213 #define UR_CBI_ADSC 0x00
215 typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */
217 typedef union {
218 struct {
219 unsigned char type;
220 #define IDB_TYPE_CCI 0x00
221 unsigned char value;
222 #define IDB_VALUE_PASS 0x00
223 #define IDB_VALUE_FAIL 0x01
224 #define IDB_VALUE_PHASE 0x02
225 #define IDB_VALUE_PERSISTENT 0x03
226 #define IDB_VALUE_STATUS_MASK 0x03
227 } common;
229 struct {
230 unsigned char asc;
231 unsigned char ascq;
232 } ufi;
233 } umass_cbi_sbl_t;
237 struct umass_softc; /* see below */
239 typedef void (*transfer_cb_f) (struct umass_softc *sc, void *priv,
240 int residue, int status);
241 #define STATUS_CMD_OK 0 /* everything ok */
242 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
243 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
244 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
246 typedef void (*wire_reset_f) (struct umass_softc *sc, int status);
247 typedef void (*wire_transfer_f) (struct umass_softc *sc, int lun,
248 void *cmd, int cmdlen, void *data, int datalen,
249 int dir, u_int timeout, transfer_cb_f cb, void *priv);
250 typedef void (*wire_state_f) (usbd_xfer_handle xfer,
251 usbd_private_handle priv, usbd_status err);
253 typedef int (*command_transform_f) (struct umass_softc *sc,
254 unsigned char *cmd, int cmdlen,
255 unsigned char **rcmd, int *rcmdlen);
258 struct umass_devdescr_t {
259 u_int32_t vendor;
260 u_int32_t product;
261 u_int32_t release;
262 # define WILDCARD_ID 0xffffffff
263 # define EOT_ID 0xfffffffe
265 /* wire and command protocol */
266 u_int16_t proto;
267 # define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
268 # define UMASS_PROTO_CBI 0x0002
269 # define UMASS_PROTO_CBI_I 0x0004
270 # define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
271 # define UMASS_PROTO_SCSI 0x0100 /* command protocol */
272 # define UMASS_PROTO_ATAPI 0x0200
273 # define UMASS_PROTO_UFI 0x0400
274 # define UMASS_PROTO_RBC 0x0800
275 # define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
277 /* Device specific quirks */
278 u_int16_t quirks;
279 # define NO_QUIRKS 0x0000
280 /* The drive does not support Test Unit Ready. Convert to Start Unit
282 # define NO_TEST_UNIT_READY 0x0001
283 /* The drive does not reset the Unit Attention state after REQUEST
284 * SENSE has been sent. The INQUIRY command does not reset the UA
285 * either, and so CAM runs in circles trying to retrieve the initial
286 * INQUIRY data.
288 # define RS_NO_CLEAR_UA 0x0002
289 /* The drive does not support START STOP. */
290 # define NO_START_STOP 0x0004
291 /* Don't ask for full inquiry data (255b). */
292 # define FORCE_SHORT_INQUIRY 0x0008
293 /* Needs to be initialised the Shuttle way */
294 # define SHUTTLE_INIT 0x0010
295 /* Drive needs to be switched to alternate iface 1 */
296 # define ALT_IFACE_1 0x0020
297 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
298 # define FLOPPY_SPEED 0x0040
299 /* The device can't count and gets the residue of transfers wrong */
300 # define IGNORE_RESIDUE 0x0080
301 /* No GetMaxLun call */
302 # define NO_GETMAXLUN 0x0100
303 /* The device uses a weird CSWSIGNATURE. */
304 # define WRONG_CSWSIG 0x0200
305 /* Device cannot handle INQUIRY so fake a generic response */
306 # define NO_INQUIRY 0x0400
307 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
308 # define NO_INQUIRY_EVPD 0x0800
309 # define READ_CAPACITY_OFFBY1 0x2000
312 static struct umass_devdescr_t umass_devdescrs[] = {
313 /* Addonics Cable 205 */
314 { .vendor = 0x0bf6, .product = 0xa001, .release = WILDCARD_ID,
315 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
316 .quirks = NO_QUIRKS
318 /* Addonics USB 2.0 Flash */
319 { .vendor = 0x09df, .product = 0x1300, .release = WILDCARD_ID,
320 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
321 .quirks = IGNORE_RESIDUE
323 /* Addonics Attache 256MB USB */
324 { .vendor = 0x09df, .product = 0x1400, .release = WILDCARD_ID,
325 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
326 .quirks = IGNORE_RESIDUE
328 /* Addonics USB 2.0 Flash */
329 { .vendor = 0x09df, .product = 0x1420, .release = WILDCARD_ID,
330 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
331 .quirks = IGNORE_RESIDUE
333 /* AIPTEK PocketCAM 3Mega */
334 { .vendor = 0x08ca, .product = 0x2011, .release = WILDCARD_ID,
335 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
336 .quirks = NO_QUIRKS
338 /* All Asahi Optical products */
339 { .vendor = 0x0a17, .product = WILDCARD_ID, .release = WILDCARD_ID,
340 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
341 .quirks = RS_NO_CLEAR_UA
343 /* Belkin USB to SCSI */
344 { .vendor = 0x050d, .product = 0x0115, .release = WILDCARD_ID,
345 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
346 .quirks = NO_QUIRKS
348 /* CASIO QV DigiCam */
349 { .vendor = 0x07cf, .product = 0x1001, .release = WILDCARD_ID,
350 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
351 .quirks = NO_INQUIRY
353 /* CCYU EasyDisk ED1064 */
354 { .vendor = 0x1065, .product = 0x2136, .release = WILDCARD_ID,
355 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
356 .quirks = NO_QUIRKS
358 /* Century Century USB Disk */
359 { .vendor = 0x07f7, .product = 0x011e, .release = WILDCARD_ID,
360 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
361 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
363 /* Desknote UCR-61S2B */
364 { .vendor = 0x1019, .product = 0x0c55, .release = WILDCARD_ID,
365 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
366 .quirks = NO_QUIRKS
368 /* DMI CF/SM Reader/Writer */
369 { .vendor = 0x0c0b, .product = 0xa109, .release = WILDCARD_ID,
370 .proto = UMASS_PROTO_SCSI,
371 .quirks = NO_GETMAXLUN
373 /* Epson Stylus Photo 875DC */
374 { .vendor = 0x03f8, .product = 0x0601, .release = WILDCARD_ID,
375 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
376 .quirks = NO_INQUIRY
378 /* Epson Stylus Photo 895 */
379 { .vendor = 0x03f8, .product = 0x0602, .release = WILDCARD_ID,
380 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
381 .quirks = NO_GETMAXLUN
383 /* Feiya 5-in-1 Card Reader */
384 { .vendor = 0x090c, .product = 0x1132, .release = WILDCARD_ID,
385 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
386 .quirks = NO_QUIRKS
388 /* Freecom DVD drive */
389 { .vendor = 0x07ab, .product = 0xfc01, .release = WILDCARD_ID,
390 .proto = UMASS_PROTO_SCSI,
391 .quirks = NO_QUIRKS
393 /* Fujiphoto mass storage products */
394 { .vendor = 0x04cb, .product = 0x0100, .release = WILDCARD_ID,
395 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
396 .quirks = RS_NO_CLEAR_UA
398 /* Genesys GL641USB USB-IDE Bridge */
399 { .vendor = 0x05e3, .product = 0x0701, .release = WILDCARD_ID,
400 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
401 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
403 /* Genesys GL641USB USB-IDE Bridge */
404 { .vendor = 0x05e3, .product = 0x0702, .release = WILDCARD_ID,
405 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
406 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
408 /* Genesys GL641USB CompactFlash Card */
409 { .vendor = 0x05e3, .product = 0x0700, .release = WILDCARD_ID,
410 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
411 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
413 /* Genesys GL641USB 6-in-1 Card */
414 { .vendor = 0x05e3, .product = 0x0760, .release = WILDCARD_ID,
415 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
416 .quirks = WRONG_CSWSIG
418 /* Hagiwara FlashGate SmartMedia Card */
419 { .vendor = 0x0693, .product = 0x0002, .release = WILDCARD_ID,
420 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
421 .quirks = NO_QUIRKS
423 /* Hitachi DVDCAM USB HS Interface */
424 { .vendor = 0x04a4, .product = 0x001e, .release = WILDCARD_ID,
425 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
426 .quirks = NO_INQUIRY
428 /* Hitachi DVD-CAM DZ-MV100A Camcorder */
429 { .vendor = 0x04a4, .product = 0x0004, .release = WILDCARD_ID,
430 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
431 .quirks = NO_GETMAXLUN
433 /* Hewlett CD-Writer+ CD-4e */
434 { .vendor = 0x03f0, .product = 0x0307, .release = WILDCARD_ID,
435 .proto = UMASS_PROTO_ATAPI,
436 .quirks = NO_QUIRKS
438 /* Hewlett CD-Writer Plus 8200e */
439 { .vendor = 0x03f0, .product = 0x0207, .release = WILDCARD_ID,
440 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
441 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
443 /* Imagination DBX1 DSP core */
444 { .vendor = 0x149a, .product = 0x2107, .release = WILDCARD_ID,
445 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
446 .quirks = WRONG_CSWSIG
448 /* In-System ATAPI Adapter */
449 { .vendor = 0x05ab, .product = 0x0031, .release = WILDCARD_ID,
450 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
451 .quirks = NO_QUIRKS
453 /* In-System USB Storage Adapter */
454 { .vendor = 0x05ab, .product = 0x5701, .release = WILDCARD_ID,
455 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
456 .quirks = NO_QUIRKS
458 /* In-System USB cable */
459 { .vendor = 0x05ab, .product = 0x081a, .release = WILDCARD_ID,
460 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
461 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
463 /* I-O DVD Multi-plus unit */
464 { .vendor = 0x04bb, .product = 0x0204, .release = WILDCARD_ID,
465 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
466 .quirks = NO_QUIRKS
468 /* I-O DVD Multi-plus unit */
469 { .vendor = 0x04bb, .product = 0x0206, .release = WILDCARD_ID,
470 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
471 .quirks = NO_QUIRKS
473 /* Iomega Zip 100 */
474 { .vendor = 0x059b, .product = 0x0001, .release = WILDCARD_ID,
475 /* XXX This is not correct as there are Zip drives that use ATAPI. */
476 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
477 .quirks = NO_TEST_UNIT_READY
479 /* Kyocera Finecam L3 */
480 { .vendor = 0x0482, .product = 0x0105, .release = WILDCARD_ID,
481 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
482 .quirks = NO_INQUIRY
484 /* Kyocera Finecam S3x */
485 { .vendor = 0x0482, .product = 0x0100, .release = WILDCARD_ID,
486 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
487 .quirks = NO_INQUIRY
489 /* Kyocera Finecam S4 */
490 { .vendor = 0x0482, .product = 0x0101, .release = WILDCARD_ID,
491 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
492 .quirks = NO_INQUIRY
494 /* Kyocera Finecam S5 */
495 { .vendor = 0x0482, .product = 0x0103, .release = WILDCARD_ID,
496 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
497 .quirks = NO_INQUIRY
499 /* LaCie Hard Disk */
500 { .vendor = 0x059f, .product = 0xa601, .release = WILDCARD_ID,
501 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
502 .quirks = NO_QUIRKS
504 /* Lexar USB CF Reader */
505 { .vendor = 0x05dc, .product = 0xb002, .release = WILDCARD_ID,
506 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
507 .quirks = NO_INQUIRY
509 /* Lexar jumpSHOT CompactFlash Reader */
510 { .vendor = 0x05dc, .product = 0x0001, .release = WILDCARD_ID,
511 .proto = UMASS_PROTO_SCSI,
512 .quirks = NO_QUIRKS
514 /* Logitech DVD Multi-plus unit */
515 { .vendor = 0x046d, .product = 0x0033, .release = WILDCARD_ID,
516 .proto = UMASS_PROTO_SCSI,
517 .quirks = NO_QUIRKS
519 /* Logitech DVD Multi-plus unit LDR-H443U2 */
520 { .vendor = 0x0789, .product = 0x00b3, .release = WILDCARD_ID,
521 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
522 .quirks = NO_QUIRKS
524 /* Melco USB-IDE Bridge: DUB-PxxG */
525 { .vendor = 0x0411, .product = 0x001c, .release = WILDCARD_ID,
526 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
527 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
529 /* Microtech USB CameraMate */
530 { .vendor = 0x07af, .product = 0x0006, .release = WILDCARD_ID,
531 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
532 .quirks = NO_TEST_UNIT_READY | NO_START_STOP
534 /* Microtech USB-SCSI-DB25 */
535 { .vendor = 0x07af, .product = 0x0004, .release = WILDCARD_ID,
536 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
537 .quirks = NO_QUIRKS
539 /* Microtech USB-SCSI-HD50 */
540 { .vendor = 0x07af, .product = 0x0005, .release = WILDCARD_ID,
541 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
542 .quirks = NO_QUIRKS
544 /* Minolta Dimage E223 */
545 { .vendor = 0x0686, .product = 0x4017, .release = WILDCARD_ID,
546 .proto = UMASS_PROTO_SCSI,
547 .quirks = NO_QUIRKS
549 /* Minolta Dimage F300 */
550 { .vendor = 0x0686, .product = 0x4011, .release = WILDCARD_ID,
551 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
552 .quirks = NO_QUIRKS
554 /* Mitsumi CD-R/RW Drive */
555 { .vendor = 0x03ee, .product = 0x0000, .release = WILDCARD_ID,
556 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
557 .quirks = NO_QUIRKS
559 /* Mitsumi USB FDD */
560 { .vendor = 0x03ee, .product = 0x6901, .release = WILDCARD_ID,
561 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
562 .quirks = NO_GETMAXLUN
564 /* Motorola E398 Mobile Phone */
565 { .vendor = 0x22b8, .product = 0x4810, .release = WILDCARD_ID,
566 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
567 .quirks = FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
569 /* M-Systems DiskOnKey */
570 { .vendor = 0x08ec, .product = 0x0010, .release = WILDCARD_ID,
571 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
572 .quirks = IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
574 /* M-Systems DiskOnKey */
575 { .vendor = 0x08ec, .product = 0x0011, .release = WILDCARD_ID,
576 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
577 .quirks = NO_QUIRKS
579 /* Myson USB-IDE */
580 { .vendor = 0x04cf, .product = 0x8818, .release = WILDCARD_ID,
581 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
582 .quirks = NO_INQUIRY | IGNORE_RESIDUE
584 /* Neodio 8-in-1 Multi-format Flash */
585 { .vendor = 0x0aec, .product = 0x3260, .release = WILDCARD_ID,
586 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
587 .quirks = FORCE_SHORT_INQUIRY
589 /* Netac USB-CF-Card */
590 { .vendor = 0x0dd8, .product = 0x1060, .release = WILDCARD_ID,
591 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
592 .quirks = NO_INQUIRY
594 /* Netac OnlyDisk */
595 { .vendor = 0x0dd8, .product = 0x0003, .release = WILDCARD_ID,
596 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
597 .quirks = IGNORE_RESIDUE
599 /* NetChip USB Clik! 40 */
600 { .vendor = 0x0525, .product = 0xa140, .release = WILDCARD_ID,
601 .proto = UMASS_PROTO_ATAPI,
602 .quirks = NO_INQUIRY
604 /* Olympus C-1 Digital Camera */
605 { .vendor = 0x07b4, .product = 0x0102, .release = WILDCARD_ID,
606 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
607 .quirks = WRONG_CSWSIG
609 /* Olympus C-700 Ultra Zoom */
610 { .vendor = 0x07b4, .product = 0x0105, .release = WILDCARD_ID,
611 .proto = UMASS_PROTO_SCSI,
612 .quirks = NO_GETMAXLUN
614 /* OnSpec SIIG/Datafab Memory Stick+CF */
615 { .vendor = 0x07c4, .product = 0xa001, .release = WILDCARD_ID,
616 .proto = UMASS_PROTO_SCSI,
617 .quirks = NO_QUIRKS
619 /* OnSpec USB to CF */
620 { .vendor = 0x07c4, .product = 0xa109, .release = WILDCARD_ID,
621 .proto = UMASS_PROTO_SCSI,
622 .quirks = NO_QUIRKS
624 /* OnSpec PNY/Datafab CF+SM Reader */
625 { .vendor = 0x07c4, .product = 0xa005, .release = WILDCARD_ID,
626 .proto = UMASS_PROTO_SCSI,
627 .quirks = NO_QUIRKS
629 /* OnSpec Simple Tech/Datafab CF+SM */
630 { .vendor = 0x07c4, .product = 0xa006, .release = WILDCARD_ID,
631 .proto = UMASS_PROTO_SCSI,
632 .quirks = NO_QUIRKS
634 /* OnSpec MDCFE-B USB CF */
635 { .vendor = 0x07c4, .product = 0xa000, .release = WILDCARD_ID,
636 .proto = UMASS_PROTO_SCSI,
637 .quirks = NO_QUIRKS
639 /* OnSpec MDSM-B reader */
640 { .vendor = 0x07c4, .product = 0xa103, .release = WILDCARD_ID,
641 .proto = UMASS_PROTO_SCSI,
642 .quirks = NO_INQUIRY
644 /* OnSpec Datafab-based Reader */
645 { .vendor = 0x07c4, .product = 0xa003, .release = WILDCARD_ID,
646 .proto = UMASS_PROTO_SCSI,
647 .quirks = NO_QUIRKS
649 /* OnSpec FlashLink UCF-100 CompactFlash */
650 { .vendor = 0x07c4, .product = 0xa400, .release = WILDCARD_ID,
651 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
652 .quirks = NO_INQUIRY | NO_GETMAXLUN
654 /* OnSpec ImageMate SDDR55 */
655 { .vendor = 0x55aa, .product = 0xa103, .release = WILDCARD_ID,
656 .proto = UMASS_PROTO_SCSI,
657 .quirks = NO_GETMAXLUN
659 /* Panasonic CD-R Drive KXL-840AN */
660 { .vendor = 0x04da, .product = 0x0d01, .release = WILDCARD_ID,
661 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
662 .quirks = NO_GETMAXLUN
664 /* Panasonic CD-R Drive KXL-CB20AN */
665 { .vendor = 0x04da, .product = 0x0d0a, .release = WILDCARD_ID,
666 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
667 .quirks = NO_QUIRKS
669 /* Panasonic DVD-ROM & CD-R/RW */
670 { .vendor = 0x04da, .product = 0x0d0e, .release = WILDCARD_ID,
671 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
672 .quirks = NO_QUIRKS
674 /* Panasonic LS-120 Camera */
675 { .vendor = 0x04da, .product = 0x0901, .release = WILDCARD_ID,
676 .proto = UMASS_PROTO_UFI,
677 .quirks = NO_QUIRKS
679 /* Plextor PlexWriter 40/12/40U */
680 { .vendor = 0x093b, .product = 0x0011, .release = WILDCARD_ID,
681 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
682 .quirks = NO_TEST_UNIT_READY
684 /* PNY USB 2.0 Flash */
685 { .vendor = 0x154b, .product = 0x0010, .release = WILDCARD_ID,
686 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
687 .quirks = IGNORE_RESIDUE | NO_START_STOP
689 /* Pen USB 2.0 Flash Drive */
690 { .vendor = 0x0d7d, .product = 0x1300, .release = WILDCARD_ID,
691 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
692 .quirks = IGNORE_RESIDUE
694 /* Samsung YP-U2 MP3 Player */
695 { .vendor = 0x04e8, .product = 0x5050, .release = WILDCARD_ID,
696 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
697 .quirks = SHUTTLE_INIT | NO_GETMAXLUN
699 /* Samsung Digimax 410 */
700 { .vendor = 0x0839, .product = 0x000a, .release = WILDCARD_ID,
701 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
702 .quirks = NO_INQUIRY
704 /* SanDisk ImageMate SDDR-05a */
705 { .vendor = 0x0781, .product = 0x0001, .release = WILDCARD_ID,
706 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
707 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
709 /* SanDisk ImageMate SDDR-09 */
710 { .vendor = 0x0781, .product = 0x0200, .release = WILDCARD_ID,
711 .proto = UMASS_PROTO_SCSI,
712 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
714 /* SanDisk ImageMate SDDR-12 */
715 { .vendor = 0x0781, .product = 0x0100, .release = WILDCARD_ID,
716 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
717 .quirks = READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
719 /* SanDisk ImageMate SDDR-31 */
720 { .vendor = 0x0781, .product = 0x0002, .release = WILDCARD_ID,
721 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
722 .quirks = READ_CAPACITY_OFFBY1
724 /* SanDisk Cruzer Mini 256MB */
725 { .vendor = 0x0781, .product = 0x7104, .release = WILDCARD_ID,
726 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
727 .quirks = IGNORE_RESIDUE
729 /* SanDisk Cruzer Micro 128MB */
730 { .vendor = 0x0781, .product = 0x7112, .release = WILDCARD_ID,
731 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
732 .quirks = IGNORE_RESIDUE
734 /* SanDisk Cruzer Micro 256MB */
735 { .vendor = 0x0781, .product = 0x7113, .release = WILDCARD_ID,
736 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
737 .quirks = IGNORE_RESIDUE
739 /* ScanLogic SL11R IDE Adapter */
740 { .vendor = 0x04ce, .product = 0x0002, .release = WILDCARD_ID,
741 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
742 .quirks = NO_QUIRKS
744 /* Shuttle CD-RW Device */
745 { .vendor = 0x04e6, .product = 0x0101, .release = WILDCARD_ID,
746 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
747 .quirks = NO_QUIRKS
749 /* Shuttle eUSB CompactFlash Adapter */
750 { .vendor = 0x04e6, .product = 0x000a, .release = WILDCARD_ID,
751 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
752 .quirks = NO_QUIRKS
754 /* Shuttle E-USB Bridge */
755 { .vendor = 0x04e6, .product = 0x0001, .release = WILDCARD_ID,
756 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
757 .quirks = NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
759 /* Shuttle eUSB ATA/ATAPI Adapter */
760 { .vendor = 0x04e6, .product = 0x0009, .release = WILDCARD_ID,
761 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
762 .quirks = NO_QUIRKS
764 /* Shuttle eUSB SmartMedia / */
765 { .vendor = 0x04e6, .product = 0x0005, .release = WILDCARD_ID,
766 .proto = UMASS_PROTO_SCSI,
767 .quirks = NO_QUIRKS
769 /* Shuttle eUSCSI Bridge */
770 { .vendor = 0x04e6, .product = 0x0002, .release = WILDCARD_ID,
771 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
772 .quirks = NO_QUIRKS
774 /* Shuttle Sony Hifd */
775 { .vendor = 0x04e6, .product = 0x0007, .release = WILDCARD_ID,
776 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
777 .quirks = NO_GETMAXLUN
779 /* Shuttle ImageMate SDDR09 */
780 { .vendor = 0x04e6, .product = 0x0003, .release = WILDCARD_ID,
781 .proto = UMASS_PROTO_SCSI,
782 .quirks = NO_GETMAXLUN
784 /* Shuttle eUSB MultiMediaCard Adapter */
785 { .vendor = 0x04e6, .product = 0x0006, .release = WILDCARD_ID,
786 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
787 .quirks = NO_GETMAXLUN
789 /* Sigmatel i-Bead 100 MP3 */
790 { .vendor = 0x066f, .product = 0x8008, .release = WILDCARD_ID,
791 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
792 .quirks = SHUTTLE_INIT
794 /* SIIG WINTERREADER Reader */
795 { .vendor = 0x07cc, .product = 0x0330, .release = WILDCARD_ID,
796 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
797 .quirks = IGNORE_RESIDUE
799 /* Skanhex MD 7425 Camera */
800 { .vendor = 0x0d96, .product = 0x410a, .release = WILDCARD_ID,
801 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
802 .quirks = NO_INQUIRY
804 /* Skanhex SX 520z Camera */
805 { .vendor = 0x0d96, .product = 0x5200, .release = WILDCARD_ID,
806 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
807 .quirks = NO_INQUIRY
809 /* Sony Clie v4.0 */
810 { .vendor = 0x054c, .product = 0x006d, .release = WILDCARD_ID,
811 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
812 .quirks = NO_INQUIRY
814 /* Sony DSC cameras */
815 { .vendor = 0x054c, .product = 0x0010, .release = WILDCARD_ID,
816 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
817 .quirks = NO_QUIRKS
819 /* Sony Handycam */
820 { .vendor = 0x054c, .product = 0x002e, .release = WILDCARD_ID,
821 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
822 .quirks = NO_QUIRKS
824 /* Sony Memorystick MSC-U03 */
825 { .vendor = 0x054c, .product = 0x0069, .release = WILDCARD_ID,
826 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
827 .quirks = NO_GETMAXLUN
829 /* Sony Memorystick NW-MS7 */
830 { .vendor = 0x054c, .product = 0x0025, .release = WILDCARD_ID,
831 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
832 .quirks = NO_GETMAXLUN
834 /* Sony PEG N760c Memorystick */
835 { .vendor = 0x054c, .product = 0x0058, .release = WILDCARD_ID,
836 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
837 .quirks = NO_INQUIRY
839 /* Sony Memorystick MSAC-US1 */
840 { .vendor = 0x054c, .product = 0x002d, .release = WILDCARD_ID,
841 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
842 .quirks = NO_GETMAXLUN
844 /* Sony MSC memory stick */
845 { .vendor = 0x054c, .product = 0x0032, .release = WILDCARD_ID,
846 .proto = UMASS_PROTO_RBC | UMASS_PROTO_CBI,
847 .quirks = NO_QUIRKS
849 /* Sony Portable USB Harddrive */
850 { .vendor = 0x054c, .product = 0x002b, .release = WILDCARD_ID,
851 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
852 .quirks = NO_QUIRKS
854 /* Taugagreining CameraMate (DPCM_USB) */
855 { .vendor = 0x0436, .product = 0x0005, .release = WILDCARD_ID,
856 .proto = UMASS_PROTO_SCSI,
857 .quirks = NO_QUIRKS
859 /* TEAC FD-05PUB floppy */
860 { .vendor = 0x0644, .product = 0x0000, .release = WILDCARD_ID,
861 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
862 .quirks = NO_QUIRKS
864 /* Trek IBM USB Memory */
865 { .vendor = 0x0a16, .product = 0x8888, .release = WILDCARD_ID,
866 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
867 .quirks = NO_INQUIRY
869 /* Trek ThumbDrive_8MB */
870 { .vendor = 0x0a16, .product = 0x9988, .release = WILDCARD_ID,
871 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
872 .quirks = IGNORE_RESIDUE
874 /* Trumpion Comotron C3310 MP3 */
875 { .vendor = 0x090a, .product = 0x1100, .release = WILDCARD_ID,
876 .proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI,
877 .quirks = NO_QUIRKS
879 /* Trumpion MP3 player */
880 { .vendor = 0x090a, .product = 0x1200, .release = WILDCARD_ID,
881 .proto = UMASS_PROTO_RBC,
882 .quirks = NO_QUIRKS
884 /* Trumpion T33520 USB Flash */
885 { .vendor = 0x090a, .product = 0x1001, .release = WILDCARD_ID,
886 .proto = UMASS_PROTO_SCSI,
887 .quirks = NO_QUIRKS
889 /* TwinMOS Memory Disk IV */
890 { .vendor = 0x126f, .product = 0x1325, .release = WILDCARD_ID,
891 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
892 .quirks = NO_QUIRKS
894 /* Vivitar Vivicam 35Xx */
895 { .vendor = 0x0636, .product = 0x0003, .release = WILDCARD_ID,
896 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
897 .quirks = NO_INQUIRY
899 /* Western Firewire USB Combo */
900 { .vendor = 0x1058, .product = 0x0200, .release = WILDCARD_ID,
901 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
902 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
904 /* Western External HDD */
905 { .vendor = 0x1058, .product = 0x0400, .release = WILDCARD_ID,
906 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
907 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
909 /* Western MyBook External HDD */
910 { .vendor = 0x1058, .product = 0x0901, .release = WILDCARD_ID,
911 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
912 .quirks = NO_INQUIRY_EVPD
914 /* WinMaxGroup USB Flash Disk */
915 { .vendor = 0x0ed1, .product = 0x6660, .release = WILDCARD_ID,
916 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
917 .quirks = NO_INQUIRY
919 /* Yano METALWEAR-HDD */
920 { .vendor = 0x094f, .product = 0x05fc, .release = WILDCARD_ID,
921 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
922 .quirks = FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
924 /* Yano U640MO-03 */
925 { .vendor = 0x094f, .product = 0x0101, .release = WILDCARD_ID,
926 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
927 .quirks = FORCE_SHORT_INQUIRY
929 /* Y-E Flashbuster-U */
930 { .vendor = 0x057b, .product = 0x0000, .release = WILDCARD_ID,
931 .proto = UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
932 .quirks = NO_GETMAXLUN
934 /* Zoran Digital Camera EX-20 */
935 { .vendor = 0x0595, .product = 0x4343, .release = WILDCARD_ID,
936 .proto = UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
937 .quirks = NO_QUIRKS
939 { .vendor = EOT_ID, .product = EOT_ID, .release = EOT_ID,
940 .proto = 0, .quirks = 0 }
944 /* the per device structure */
945 struct umass_softc {
946 device_t sc_dev; /* base device */
947 usbd_device_handle sc_udev; /* USB device */
949 struct cam_sim *umass_sim; /* SCSI Interface Module */
951 unsigned char flags; /* various device flags */
952 # define UMASS_FLAGS_GONE 0x01 /* devices is no more */
954 u_int16_t proto; /* wire and cmd protocol */
955 u_int16_t quirks; /* they got it almost right */
957 usbd_interface_handle iface; /* Mass Storage interface */
958 int ifaceno; /* MS iface number */
960 u_int8_t bulkin; /* bulk-in Endpoint Address */
961 u_int8_t bulkout; /* bulk-out Endpoint Address */
962 u_int8_t intrin; /* intr-in Endp. (CBI) */
963 usbd_pipe_handle bulkin_pipe;
964 usbd_pipe_handle bulkout_pipe;
965 usbd_pipe_handle intrin_pipe;
967 /* Reset the device in a wire protocol specific way */
968 wire_reset_f reset;
970 /* The start of a wire transfer. It prepares the whole transfer (cmd,
971 * data, and status stage) and initiates it. It is up to the state
972 * machine (below) to handle the various stages and errors in these
974 wire_transfer_f transfer;
976 /* The state machine, handling the various states during a transfer */
977 wire_state_f state;
979 /* The command transform function is used to conver the SCSI commands
980 * into their derivatives, like UFI, ATAPI, and friends.
982 command_transform_f transform; /* command transform */
984 /* Bulk specific variables for transfers in progress */
985 umass_bbb_cbw_t cbw; /* command block wrapper */
986 umass_bbb_csw_t csw; /* command status wrapper*/
987 /* CBI specific variables for transfers in progress */
988 umass_cbi_cbl_t cbl; /* command block */
989 umass_cbi_sbl_t sbl; /* status block */
991 /* generic variables for transfers in progress */
992 /* ctrl transfer requests */
993 usb_device_request_t request;
995 /* xfer handles
996 * Most of our operations are initiated from interrupt context, so
997 * we need to avoid using the one that is in use. We want to avoid
998 * allocating them in the interrupt context as well.
1000 /* indices into array below */
1001 # define XFER_BBB_CBW 0 /* Bulk-Only */
1002 # define XFER_BBB_DATA 1
1003 # define XFER_BBB_DCLEAR 2
1004 # define XFER_BBB_CSW1 3
1005 # define XFER_BBB_CSW2 4
1006 # define XFER_BBB_SCLEAR 5
1007 # define XFER_BBB_RESET1 6
1008 # define XFER_BBB_RESET2 7
1009 # define XFER_BBB_RESET3 8
1011 # define XFER_CBI_CB 0 /* CBI */
1012 # define XFER_CBI_DATA 1
1013 # define XFER_CBI_STATUS 2
1014 # define XFER_CBI_DCLEAR 3
1015 # define XFER_CBI_SCLEAR 4
1016 # define XFER_CBI_RESET1 5
1017 # define XFER_CBI_RESET2 6
1018 # define XFER_CBI_RESET3 7
1020 # define XFER_NR 9 /* maximum number */
1022 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
1024 int transfer_dir; /* data direction */
1025 void *transfer_data; /* data buffer */
1026 int transfer_datalen; /* (maximum) length */
1027 int transfer_actlen; /* actual length */
1028 transfer_cb_f transfer_cb; /* callback */
1029 void *transfer_priv; /* for callback */
1030 int transfer_status;
1032 int transfer_state;
1033 # define TSTATE_ATTACH 0 /* in attach */
1034 # define TSTATE_IDLE 1
1035 # define TSTATE_BBB_COMMAND 2 /* CBW transfer */
1036 # define TSTATE_BBB_DATA 3 /* Data transfer */
1037 # define TSTATE_BBB_DCLEAR 4 /* clear endpt stall */
1038 # define TSTATE_BBB_STATUS1 5 /* clear endpt stall */
1039 # define TSTATE_BBB_SCLEAR 6 /* clear endpt stall */
1040 # define TSTATE_BBB_STATUS2 7 /* CSW transfer */
1041 # define TSTATE_BBB_RESET1 8 /* reset command */
1042 # define TSTATE_BBB_RESET2 9 /* in clear stall */
1043 # define TSTATE_BBB_RESET3 10 /* out clear stall */
1044 # define TSTATE_CBI_COMMAND 11 /* command transfer */
1045 # define TSTATE_CBI_DATA 12 /* data transfer */
1046 # define TSTATE_CBI_STATUS 13 /* status transfer */
1047 # define TSTATE_CBI_DCLEAR 14 /* clear ep stall */
1048 # define TSTATE_CBI_SCLEAR 15 /* clear ep stall */
1049 # define TSTATE_CBI_RESET1 16 /* reset command */
1050 # define TSTATE_CBI_RESET2 17 /* in clear stall */
1051 # define TSTATE_CBI_RESET3 18 /* out clear stall */
1052 # define TSTATE_STATES 19 /* # of states above */
1055 /* SCSI/CAM specific variables */
1056 unsigned char cam_scsi_command[CAM_MAX_CDBLEN];
1057 unsigned char cam_scsi_command2[CAM_MAX_CDBLEN];
1058 struct scsi_sense cam_scsi_sense;
1059 struct scsi_sense cam_scsi_test_unit_ready;
1060 int timeout; /* in msecs */
1062 int maxlun; /* maximum LUN number */
1063 struct callout rescan_timeout;
1066 #ifdef USB_DEBUG
1067 char *states[TSTATE_STATES+1] = {
1068 /* should be kept in sync with the list at transfer_state */
1069 "Attach",
1070 "Idle",
1071 "BBB CBW",
1072 "BBB Data",
1073 "BBB Data bulk-in/-out clear stall",
1074 "BBB CSW, 1st attempt",
1075 "BBB CSW bulk-in clear stall",
1076 "BBB CSW, 2nd attempt",
1077 "BBB Reset",
1078 "BBB bulk-in clear stall",
1079 "BBB bulk-out clear stall",
1080 "CBI Command",
1081 "CBI Data",
1082 "CBI Status",
1083 "CBI Data bulk-in/-out clear stall",
1084 "CBI Status intr-in clear stall",
1085 "CBI Reset",
1086 "CBI bulk-in clear stall",
1087 "CBI bulk-out clear stall",
1088 NULL
1090 #endif
1092 /* If device cannot return valid inquiry data, fake it */
1093 static uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1094 0, /*removable*/ 0x80, SCSI_REV_2, SCSI_REV_2,
1095 /*additional_length*/ 31, 0, 0, 0
1098 /* USB device probe/attach/detach functions */
1099 static device_probe_t umass_match;
1100 static device_attach_t umass_attach;
1101 static device_detach_t umass_detach;
1103 static devclass_t umass_devclass;
1105 static kobj_method_t umass_methods[] = {
1106 DEVMETHOD(device_probe, umass_match),
1107 DEVMETHOD(device_attach, umass_attach),
1108 DEVMETHOD(device_detach, umass_detach),
1109 {0,0},
1110 {0,0}
1113 static driver_t umass_driver = {
1114 "umass",
1115 umass_methods,
1116 sizeof(struct umass_softc)
1119 MODULE_DEPEND(umass, usb, 1, 1, 1);
1121 static int umass_match_proto (struct umass_softc *sc,
1122 usbd_interface_handle iface,
1123 usbd_device_handle udev);
1125 /* quirk functions */
1126 static void umass_init_shuttle (struct umass_softc *sc);
1128 /* generic transfer functions */
1129 static usbd_status umass_setup_transfer (struct umass_softc *sc,
1130 usbd_pipe_handle pipe,
1131 void *buffer, int buflen, int flags,
1132 usbd_xfer_handle xfer);
1133 static usbd_status umass_setup_ctrl_transfer (struct umass_softc *sc,
1134 usbd_device_handle udev,
1135 usb_device_request_t *req,
1136 void *buffer, int buflen, int flags,
1137 usbd_xfer_handle xfer);
1138 static void umass_clear_endpoint_stall (struct umass_softc *sc,
1139 u_int8_t endpt, usbd_pipe_handle pipe,
1140 int state, usbd_xfer_handle xfer);
1141 static void umass_reset (struct umass_softc *sc,
1142 transfer_cb_f cb, void *priv);
1144 /* Bulk-Only related functions */
1145 static void umass_bbb_reset (struct umass_softc *sc, int status);
1146 static void umass_bbb_transfer (struct umass_softc *sc, int lun,
1147 void *cmd, int cmdlen,
1148 void *data, int datalen, int dir, u_int timeout,
1149 transfer_cb_f cb, void *priv);
1150 static void umass_bbb_state (usbd_xfer_handle xfer,
1151 usbd_private_handle priv,
1152 usbd_status err);
1153 static int umass_bbb_get_max_lun
1154 (struct umass_softc *sc);
1156 /* CBI related functions */
1157 static int umass_cbi_adsc (struct umass_softc *sc,
1158 char *buffer, int buflen,
1159 usbd_xfer_handle xfer);
1160 static void umass_cbi_reset (struct umass_softc *sc, int status);
1161 static void umass_cbi_transfer (struct umass_softc *sc, int lun,
1162 void *cmd, int cmdlen,
1163 void *data, int datalen, int dir, u_int timeout,
1164 transfer_cb_f cb, void *priv);
1165 static void umass_cbi_state (usbd_xfer_handle xfer,
1166 usbd_private_handle priv, usbd_status err);
1168 /* CAM related functions */
1169 static void umass_cam_action (struct cam_sim *sim, union ccb *ccb);
1170 static void umass_cam_poll (struct cam_sim *sim);
1172 static void umass_cam_cb (struct umass_softc *sc, void *priv,
1173 int residue, int status);
1174 static void umass_cam_sense_cb (struct umass_softc *sc, void *priv,
1175 int residue, int status);
1176 static void umass_cam_quirk_cb (struct umass_softc *sc, void *priv,
1177 int residue, int status);
1179 static void umass_cam_rescan_callback
1180 (struct cam_periph *periph,union ccb *ccb);
1181 static void umass_cam_rescan (void *addr);
1183 static int umass_cam_attach_sim (struct umass_softc *sc);
1184 static int umass_cam_attach (struct umass_softc *sc);
1185 static int umass_cam_detach_sim (struct umass_softc *sc);
1188 /* SCSI specific functions */
1189 static int umass_scsi_transform (struct umass_softc *sc,
1190 unsigned char *cmd, int cmdlen,
1191 unsigned char **rcmd, int *rcmdlen);
1193 /* UFI specific functions */
1194 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
1195 static int umass_ufi_transform (struct umass_softc *sc,
1196 unsigned char *cmd, int cmdlen,
1197 unsigned char **rcmd, int *rcmdlen);
1199 /* ATAPI (8070i) specific functions */
1200 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
1201 static int umass_atapi_transform (struct umass_softc *sc,
1202 unsigned char *cmd, int cmdlen,
1203 unsigned char **rcmd, int *rcmdlen);
1205 /* RBC specific functions */
1206 static int umass_rbc_transform (struct umass_softc *sc,
1207 unsigned char *cmd, int cmdlen,
1208 unsigned char **rcmd, int *rcmdlen);
1210 #ifdef USB_DEBUG
1211 /* General debugging functions */
1212 static void umass_bbb_dump_cbw (struct umass_softc *sc, umass_bbb_cbw_t *cbw);
1213 static void umass_bbb_dump_csw (struct umass_softc *sc, umass_bbb_csw_t *csw);
1214 static void umass_cbi_dump_cmd (struct umass_softc *sc, void *cmd, int cmdlen);
1215 static void umass_dump_buffer (struct umass_softc *sc, u_int8_t *buffer,
1216 int buflen, int printlen);
1217 #endif
1219 MODULE_DEPEND(umass, cam, 1,1,1);
1222 * USB device probe/attach/detach
1226 * Match the device we are seeing with the devices supported. Fill in the
1227 * description in the softc accordingly. This function is called from both
1228 * probe and attach.
1231 static int
1232 umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface,
1233 usbd_device_handle udev)
1235 usb_device_descriptor_t *dd;
1236 usb_interface_descriptor_t *id;
1237 int i;
1238 int found = 0;
1240 sc->sc_udev = udev;
1241 sc->proto = 0;
1242 sc->quirks = 0;
1244 dd = usbd_get_device_descriptor(udev);
1246 /* An entry specifically for Y-E Data devices as they don't fit in the
1247 * device description table.
1249 if (UGETW(dd->idVendor) == 0x057b && UGETW(dd->idProduct) == 0x0000) {
1251 /* Revisions < 1.28 do not handle the inerrupt endpoint
1252 * very well.
1254 if (UGETW(dd->bcdDevice) < 0x128) {
1255 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1256 } else {
1257 sc->proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1261 * Revisions < 1.28 do not have the TEST UNIT READY command
1262 * Revisions == 1.28 have a broken TEST UNIT READY
1264 if (UGETW(dd->bcdDevice) <= 0x128)
1265 sc->quirks |= NO_TEST_UNIT_READY;
1267 sc->quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1268 return(UMATCH_VENDOR_PRODUCT);
1271 /* Check the list of supported devices for a match. While looking,
1272 * check for wildcarded and fully matched. First match wins.
1274 for (i = 0; umass_devdescrs[i].vendor != EOT_ID && !found; i++) {
1275 if (umass_devdescrs[i].vendor == WILDCARD_ID &&
1276 umass_devdescrs[i].product == WILDCARD_ID &&
1277 umass_devdescrs[i].release == WILDCARD_ID) {
1278 kprintf("umass: ignoring invalid wildcard quirk\n");
1279 continue;
1281 if ((umass_devdescrs[i].vendor == UGETW(dd->idVendor) ||
1282 umass_devdescrs[i].vendor == WILDCARD_ID)
1283 && (umass_devdescrs[i].product == UGETW(dd->idProduct) ||
1284 umass_devdescrs[i].product == WILDCARD_ID)) {
1285 if (umass_devdescrs[i].release == WILDCARD_ID) {
1286 sc->proto = umass_devdescrs[i].proto;
1287 sc->quirks = umass_devdescrs[i].quirks;
1288 return (UMATCH_VENDOR_PRODUCT);
1289 } else if (umass_devdescrs[i].release ==
1290 UGETW(dd->bcdDevice)) {
1291 sc->proto = umass_devdescrs[i].proto;
1292 sc->quirks = umass_devdescrs[i].quirks;
1293 return (UMATCH_VENDOR_PRODUCT_REV);
1294 } /* else RID does not match */
1298 /* Check for a standards compliant device */
1300 id = usbd_get_interface_descriptor(iface);
1301 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
1302 return(UMATCH_NONE);
1304 switch (id->bInterfaceSubClass) {
1305 case UISUBCLASS_SCSI:
1306 sc->proto |= UMASS_PROTO_SCSI;
1307 break;
1308 case UISUBCLASS_UFI:
1309 sc->proto |= UMASS_PROTO_UFI;
1310 break;
1311 case UISUBCLASS_RBC:
1312 sc->proto |= UMASS_PROTO_RBC;
1313 break;
1314 case UISUBCLASS_SFF8020I:
1315 case UISUBCLASS_SFF8070I:
1316 sc->proto |= UMASS_PROTO_ATAPI;
1317 break;
1318 default:
1319 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
1320 device_get_nameunit(sc->sc_dev), id->bInterfaceSubClass));
1321 return(UMATCH_NONE);
1324 switch (id->bInterfaceProtocol) {
1325 case UIPROTO_MASS_CBI:
1326 sc->proto |= UMASS_PROTO_CBI;
1327 break;
1328 case UIPROTO_MASS_CBI_I:
1329 sc->proto |= UMASS_PROTO_CBI_I;
1330 break;
1331 case UIPROTO_MASS_BBB_OLD:
1332 case UIPROTO_MASS_BBB:
1333 sc->proto |= UMASS_PROTO_BBB;
1334 break;
1335 default:
1336 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
1337 device_get_nameunit(sc->sc_dev), id->bInterfaceProtocol));
1338 return(UMATCH_NONE);
1341 return(UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
1344 static int
1345 umass_match(device_t self)
1347 struct usb_attach_arg *uaa = device_get_ivars(self);
1348 struct umass_softc *sc = device_get_softc(self);
1350 sc->sc_dev = self;
1352 if (uaa->iface == NULL)
1353 return(UMATCH_NONE);
1355 return(umass_match_proto(sc, uaa->iface, uaa->device));
1358 static int
1359 umass_attach(device_t self)
1361 struct umass_softc *sc = device_get_softc(self);
1362 struct usb_attach_arg *uaa = device_get_ivars(self);
1363 usb_interface_descriptor_t *id;
1364 usb_endpoint_descriptor_t *ed;
1365 int i;
1366 int err;
1369 * the softc struct is bzero-ed in device_set_driver. We can safely
1370 * call umass_detach without specifically initialising the struct.
1373 sc->sc_dev = self;
1375 sc->iface = uaa->iface;
1376 sc->ifaceno = uaa->ifaceno;
1378 /* initialise the proto and drive values in the umass_softc (again) */
1379 (void) umass_match_proto(sc, sc->iface, uaa->device);
1381 id = usbd_get_interface_descriptor(sc->iface);
1382 #ifdef USB_DEBUG
1383 kprintf("%s: ", device_get_nameunit(sc->sc_dev));
1384 switch (sc->proto&UMASS_PROTO_COMMAND) {
1385 case UMASS_PROTO_SCSI:
1386 kprintf("SCSI");
1387 break;
1388 case UMASS_PROTO_ATAPI:
1389 kprintf("8070i (ATAPI)");
1390 break;
1391 case UMASS_PROTO_UFI:
1392 kprintf("UFI");
1393 break;
1394 case UMASS_PROTO_RBC:
1395 kprintf("RBC");
1396 break;
1397 default:
1398 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_COMMAND);
1399 break;
1401 kprintf(" over ");
1402 switch (sc->proto&UMASS_PROTO_WIRE) {
1403 case UMASS_PROTO_BBB:
1404 kprintf("Bulk-Only");
1405 break;
1406 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
1407 kprintf("CBI");
1408 break;
1409 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1410 kprintf("CBI with CCI");
1411 #ifndef CBI_I
1412 kprintf(" (using CBI)");
1413 #endif
1414 break;
1415 default:
1416 kprintf("(unknown 0x%02x)", sc->proto&UMASS_PROTO_WIRE);
1418 kprintf("; quirks = 0x%04x\n", sc->quirks);
1419 #endif
1421 #ifndef CBI_I
1422 if (sc->proto & UMASS_PROTO_CBI_I) {
1423 /* See beginning of file for comment on the use of CBI with CCI */
1424 sc->proto = (sc->proto & ~UMASS_PROTO_CBI_I) | UMASS_PROTO_CBI;
1426 #endif
1428 if (sc->quirks & ALT_IFACE_1) {
1429 err = usbd_set_interface(0, 1);
1430 if (err) {
1431 DPRINTF(UDMASS_USB, ("%s: could not switch to "
1432 "Alt Interface %d\n",
1433 device_get_nameunit(sc->sc_dev), 1));
1434 umass_detach(self);
1435 return ENXIO;
1440 * In addition to the Control endpoint the following endpoints
1441 * are required:
1442 * a) bulk-in endpoint.
1443 * b) bulk-out endpoint.
1444 * and for Control/Bulk/Interrupt with CCI (CBI_I)
1445 * c) intr-in
1447 * The endpoint addresses are not fixed, so we have to read them
1448 * from the device descriptors of the current interface.
1450 for (i = 0 ; i < id->bNumEndpoints ; i++) {
1451 ed = usbd_interface2endpoint_descriptor(sc->iface, i);
1452 if (!ed) {
1453 kprintf("%s: could not read endpoint descriptor\n",
1454 device_get_nameunit(sc->sc_dev));
1455 return ENXIO;
1457 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1458 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1459 sc->bulkin = ed->bEndpointAddress;
1460 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
1461 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
1462 sc->bulkout = ed->bEndpointAddress;
1463 } else if (sc->proto & UMASS_PROTO_CBI_I
1464 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
1465 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
1466 sc->intrin = ed->bEndpointAddress;
1467 #ifdef USB_DEBUG
1468 if (UGETW(ed->wMaxPacketSize) > 2) {
1469 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
1470 device_get_nameunit(sc->sc_dev),
1471 UGETW(ed->wMaxPacketSize)));
1473 #endif
1477 /* check whether we found all the endpoints we need */
1478 if (!sc->bulkin || !sc->bulkout
1479 || (sc->proto & UMASS_PROTO_CBI_I && !sc->intrin) ) {
1480 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
1481 device_get_nameunit(sc->sc_dev),
1482 sc->bulkin, sc->bulkout, sc->intrin));
1483 umass_detach(self);
1484 return ENXIO;
1487 /* Open the bulk-in and -out pipe */
1488 err = usbd_open_pipe(sc->iface, sc->bulkout,
1489 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
1490 if (err) {
1491 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
1492 device_get_nameunit(sc->sc_dev), sc->bulkout));
1493 umass_detach(self);
1494 return ENXIO;
1496 err = usbd_open_pipe(sc->iface, sc->bulkin,
1497 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
1498 if (err) {
1499 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
1500 device_get_nameunit(sc->sc_dev), sc->bulkin));
1501 umass_detach(self);
1502 return ENXIO;
1504 /* Open the intr-in pipe if the protocol is CBI with CCI.
1505 * Note: early versions of the Zip drive do have an interrupt pipe, but
1506 * this pipe is unused
1508 * We do not open the interrupt pipe as an interrupt pipe, but as a
1509 * normal bulk endpoint. We send an IN transfer down the wire at the
1510 * appropriate time, because we know exactly when to expect data on
1511 * that endpoint. This saves bandwidth, but more important, makes the
1512 * code for handling the data on that endpoint simpler. No data
1513 * arriving concurently.
1515 if (sc->proto & UMASS_PROTO_CBI_I) {
1516 err = usbd_open_pipe(sc->iface, sc->intrin,
1517 USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
1518 if (err) {
1519 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
1520 device_get_nameunit(sc->sc_dev), sc->intrin));
1521 umass_detach(self);
1522 return ENXIO;
1526 /* initialisation of generic part */
1527 sc->transfer_state = TSTATE_ATTACH;
1529 /* request a sufficient number of xfer handles */
1530 for (i = 0; i < XFER_NR; i++) {
1531 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
1532 if (!sc->transfer_xfer[i]) {
1533 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
1534 device_get_nameunit(sc->sc_dev)));
1535 umass_detach(self);
1536 return ENXIO;
1540 /* Initialise the wire protocol specific methods */
1541 if (sc->proto & UMASS_PROTO_BBB) {
1542 sc->reset = umass_bbb_reset;
1543 sc->transfer = umass_bbb_transfer;
1544 sc->state = umass_bbb_state;
1545 } else if (sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I)) {
1546 sc->reset = umass_cbi_reset;
1547 sc->transfer = umass_cbi_transfer;
1548 sc->state = umass_cbi_state;
1549 #ifdef USB_DEBUG
1550 } else {
1551 panic("%s:%d: Unknown proto 0x%02x",
1552 __FILE__, __LINE__, sc->proto);
1553 #endif
1556 if (sc->proto & UMASS_PROTO_SCSI)
1557 sc->transform = umass_scsi_transform;
1558 else if (sc->proto & UMASS_PROTO_UFI)
1559 sc->transform = umass_ufi_transform;
1560 else if (sc->proto & UMASS_PROTO_ATAPI)
1561 sc->transform = umass_atapi_transform;
1562 else if (sc->proto & UMASS_PROTO_RBC)
1563 sc->transform = umass_rbc_transform;
1564 #ifdef USB_DEBUG
1565 else
1566 panic("No transformation defined for command proto 0x%02x",
1567 sc->proto & UMASS_PROTO_COMMAND);
1568 #endif
1570 /* From here onwards the device can be used. */
1572 if (sc->quirks & SHUTTLE_INIT)
1573 umass_init_shuttle(sc);
1575 /* Get the maximum LUN supported by the device.
1577 if (((sc->proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1578 !(sc->quirks & NO_GETMAXLUN))
1579 sc->maxlun = umass_bbb_get_max_lun(sc);
1580 else
1581 sc->maxlun = 0;
1583 if ((sc->proto & UMASS_PROTO_SCSI) ||
1584 (sc->proto & UMASS_PROTO_ATAPI) ||
1585 (sc->proto & UMASS_PROTO_UFI) ||
1586 (sc->proto & UMASS_PROTO_RBC)) {
1587 /* Prepare the SCSI command block */
1588 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1589 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1591 /* register the SIM */
1592 err = umass_cam_attach_sim(sc);
1593 if (err) {
1594 umass_detach(self);
1595 return ENXIO;
1597 /* scan the new sim */
1598 err = umass_cam_attach(sc);
1599 if (err) {
1600 umass_cam_detach_sim(sc);
1601 umass_detach(self);
1602 return ENXIO;
1604 } else {
1605 panic("%s:%d: Unknown proto 0x%02x",
1606 __FILE__, __LINE__, sc->proto);
1609 sc->transfer_state = TSTATE_IDLE;
1610 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", device_get_nameunit(sc->sc_dev)));
1612 return 0;
1615 static int
1616 umass_detach(device_t self)
1618 struct umass_softc *sc = device_get_softc(self);
1619 int err = 0;
1620 int i;
1621 int to;
1623 DPRINTF(UDMASS_USB, ("%s: detached\n", device_get_nameunit(sc->sc_dev)));
1626 * Set UMASS_FLAGS_GONE to prevent any new transfers from being
1627 * queued, and abort any transfers in progress to ensure that
1628 * pending requests (e.g. from CAM's bus scan) are terminated.
1630 sc->flags |= UMASS_FLAGS_GONE;
1632 if (sc->bulkout_pipe)
1633 usbd_abort_pipe(sc->bulkout_pipe);
1634 if (sc->bulkin_pipe)
1635 usbd_abort_pipe(sc->bulkin_pipe);
1636 if (sc->intrin_pipe)
1637 usbd_abort_pipe(sc->intrin_pipe);
1640 * Wait until we go idle to make sure that all of our xfer requests
1641 * have finished. We could be in the middle of a BBB reset (which
1642 * would not be effected by the pipe aborts above).
1644 to = hz;
1645 while (sc->transfer_state != TSTATE_IDLE) {
1646 kprintf("%s: state %d waiting for idle\n",
1647 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1648 tsleep(sc, 0, "umassidl", to);
1649 if (to >= hz * 10) {
1650 kprintf("%s: state %d giving up!\n",
1651 device_get_nameunit(sc->sc_dev), sc->transfer_state);
1652 break;
1654 to += hz;
1657 if ((sc->proto & UMASS_PROTO_SCSI) ||
1658 (sc->proto & UMASS_PROTO_ATAPI) ||
1659 (sc->proto & UMASS_PROTO_UFI) ||
1660 (sc->proto & UMASS_PROTO_RBC)) {
1661 /* detach the SCSI host controller (SIM) */
1662 err = umass_cam_detach_sim(sc);
1665 for (i = 0; i < XFER_NR; i++) {
1666 if (sc->transfer_xfer[i])
1667 usbd_free_xfer(sc->transfer_xfer[i]);
1670 /* remove all the pipes */
1671 if (sc->bulkout_pipe)
1672 usbd_close_pipe(sc->bulkout_pipe);
1673 if (sc->bulkin_pipe)
1674 usbd_close_pipe(sc->bulkin_pipe);
1675 if (sc->intrin_pipe)
1676 usbd_close_pipe(sc->intrin_pipe);
1678 return(err);
1681 static void
1682 umass_init_shuttle(struct umass_softc *sc)
1684 usb_device_request_t req;
1685 u_char status[2];
1687 /* The Linux driver does this, but no one can tell us what the
1688 * command does.
1690 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1691 req.bRequest = 1; /* XXX unknown command */
1692 USETW(req.wValue, 0);
1693 USETW(req.wIndex, sc->ifaceno);
1694 USETW(req.wLength, sizeof status);
1695 (void) usbd_do_request(sc->sc_udev, &req, &status);
1697 DPRINTF(UDMASS_GEN, ("%s: Shuttle init returned 0x%02x%02x\n",
1698 device_get_nameunit(sc->sc_dev), status[0], status[1]));
1702 * Generic functions to handle transfers
1705 static usbd_status
1706 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1707 void *buffer, int buflen, int flags,
1708 usbd_xfer_handle xfer)
1710 usbd_status err;
1712 /* Initialiase a USB transfer and then schedule it */
1714 (void) usbd_setup_xfer(xfer, pipe, (void *) sc, buffer, buflen, flags,
1715 sc->timeout, sc->state);
1717 err = usbd_transfer(xfer);
1718 if (err && err != USBD_IN_PROGRESS) {
1719 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1720 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1721 return(err);
1724 return (USBD_NORMAL_COMPLETION);
1728 static usbd_status
1729 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle udev,
1730 usb_device_request_t *req,
1731 void *buffer, int buflen, int flags,
1732 usbd_xfer_handle xfer)
1734 usbd_status err;
1736 /* Initialiase a USB control transfer and then schedule it */
1738 (void) usbd_setup_default_xfer(xfer, udev, (void *) sc,
1739 UMASS_TIMEOUT, req, buffer, buflen, flags, sc->state);
1741 err = usbd_transfer(xfer);
1742 if (err && err != USBD_IN_PROGRESS) {
1743 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1744 device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
1746 /* do not reset, as this would make us loop */
1747 return(err);
1750 return (USBD_NORMAL_COMPLETION);
1753 static void
1754 umass_clear_endpoint_stall(struct umass_softc *sc,
1755 u_int8_t endpt, usbd_pipe_handle pipe,
1756 int state, usbd_xfer_handle xfer)
1758 usbd_device_handle udev;
1760 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1761 device_get_nameunit(sc->sc_dev), endpt));
1763 usbd_interface2device_handle(sc->iface, &udev);
1765 sc->transfer_state = state;
1767 usbd_clear_endpoint_toggle(pipe);
1769 sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1770 sc->request.bRequest = UR_CLEAR_FEATURE;
1771 USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1772 USETW(sc->request.wIndex, endpt);
1773 USETW(sc->request.wLength, 0);
1774 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0, xfer);
1777 static void
1778 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1780 sc->transfer_cb = cb;
1781 sc->transfer_priv = priv;
1783 /* The reset is a forced reset, so no error (yet) */
1784 sc->reset(sc, STATUS_CMD_OK);
1788 * Bulk protocol specific functions
1791 static void
1792 umass_bbb_reset(struct umass_softc *sc, int status)
1794 usbd_device_handle udev;
1796 KASSERT(sc->proto & UMASS_PROTO_BBB,
1797 ("%s: umass_bbb_reset: wrong sc->proto 0x%02x\n",
1798 device_get_nameunit(sc->sc_dev), sc->proto));
1801 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1803 * For Reset Recovery the host shall issue in the following order:
1804 * a) a Bulk-Only Mass Storage Reset
1805 * b) a Clear Feature HALT to the Bulk-In endpoint
1806 * c) a Clear Feature HALT to the Bulk-Out endpoint
1808 * This is done in 3 steps, states:
1809 * TSTATE_BBB_RESET1
1810 * TSTATE_BBB_RESET2
1811 * TSTATE_BBB_RESET3
1813 * If the reset doesn't succeed, the device should be port reset.
1816 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1817 device_get_nameunit(sc->sc_dev)));
1819 sc->transfer_state = TSTATE_BBB_RESET1;
1820 sc->transfer_status = status;
1822 usbd_interface2device_handle(sc->iface, &udev);
1824 /* reset is a class specific interface write */
1825 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1826 sc->request.bRequest = UR_BBB_RESET;
1827 USETW(sc->request.wValue, 0);
1828 USETW(sc->request.wIndex, sc->ifaceno);
1829 USETW(sc->request.wLength, 0);
1830 umass_setup_ctrl_transfer(sc, udev, &sc->request, NULL, 0, 0,
1831 sc->transfer_xfer[XFER_BBB_RESET1]);
1834 static void
1835 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1836 void *data, int datalen, int dir, u_int timeout,
1837 transfer_cb_f cb, void *priv)
1839 KASSERT(sc->proto & UMASS_PROTO_BBB,
1840 ("%s: umass_bbb_transfer: wrong sc->proto 0x%02x\n",
1841 device_get_nameunit(sc->sc_dev), sc->proto));
1842 /* Be a little generous. */
1843 sc->timeout = timeout + UMASS_TIMEOUT;
1846 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1847 * a data phase of datalen bytes from/to the device and finally a
1848 * csw read phase.
1849 * If the data direction was inbound a maximum of datalen bytes
1850 * is stored in the buffer pointed to by data.
1852 * umass_bbb_transfer initialises the transfer and lets the state
1853 * machine in umass_bbb_state handle the completion. It uses the
1854 * following states:
1855 * TSTATE_BBB_COMMAND
1856 * -> TSTATE_BBB_DATA
1857 * -> TSTATE_BBB_STATUS
1858 * -> TSTATE_BBB_STATUS2
1859 * -> TSTATE_BBB_IDLE
1861 * An error in any of those states will invoke
1862 * umass_bbb_reset.
1865 /* check the given arguments */
1866 KASSERT(datalen == 0 || data != NULL,
1867 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
1868 KASSERT(cmdlen <= CBWCDBLENGTH,
1869 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1870 device_get_nameunit(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1871 KASSERT(dir == DIR_NONE || datalen > 0,
1872 ("%s: datalen == 0 while direction is not NONE\n",
1873 device_get_nameunit(sc->sc_dev)));
1874 KASSERT(datalen == 0 || dir != DIR_NONE,
1875 ("%s: direction is NONE while datalen is not zero\n",
1876 device_get_nameunit(sc->sc_dev)));
1877 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1878 ("%s: CBW struct does not have the right size (%ld vs. %d)\n",
1879 device_get_nameunit(sc->sc_dev),
1880 (long)sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1881 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1882 ("%s: CSW struct does not have the right size (%ld vs. %d)\n",
1883 device_get_nameunit(sc->sc_dev),
1884 (long)sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1887 * Determine the direction of the data transfer and the length.
1889 * dCBWDataTransferLength (datalen) :
1890 * This field indicates the number of bytes of data that the host
1891 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1892 * the Direction bit) during the execution of this command. If this
1893 * field is set to 0, the device will expect that no data will be
1894 * transferred IN or OUT during this command, regardless of the value
1895 * of the Direction bit defined in dCBWFlags.
1897 * dCBWFlags (dir) :
1898 * The bits of the Flags field are defined as follows:
1899 * Bits 0-6 reserved
1900 * Bit 7 Direction - this bit shall be ignored if the
1901 * dCBWDataTransferLength field is zero.
1902 * 0 = data Out from host to device
1903 * 1 = data In from device to host
1906 /* Fill in the Command Block Wrapper
1907 * We fill in all the fields, so there is no need to bzero it first.
1909 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1910 /* We don't care about the initial value, as long as the values are unique */
1911 USETDW(sc->cbw.dCBWTag, UGETDW(sc->cbw.dCBWTag) + 1);
1912 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1913 /* DIR_NONE is treated as DIR_OUT (0x00) */
1914 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1915 sc->cbw.bCBWLUN = lun;
1916 sc->cbw.bCDBLength = cmdlen;
1917 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1919 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1921 /* store the details for the data transfer phase */
1922 sc->transfer_dir = dir;
1923 sc->transfer_data = data;
1924 sc->transfer_datalen = datalen;
1925 sc->transfer_actlen = 0;
1926 sc->transfer_cb = cb;
1927 sc->transfer_priv = priv;
1928 sc->transfer_status = STATUS_CMD_OK;
1930 /* move from idle to the command state */
1931 sc->transfer_state = TSTATE_BBB_COMMAND;
1933 /* Send the CBW from host to device via bulk-out endpoint. */
1934 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1935 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1936 sc->transfer_xfer[XFER_BBB_CBW])) {
1937 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1942 static void
1943 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1944 usbd_status err)
1946 struct umass_softc *sc = (struct umass_softc *) priv;
1947 usbd_xfer_handle next_xfer;
1949 KASSERT(sc->proto & UMASS_PROTO_BBB,
1950 ("%s: umass_bbb_state: wrong sc->proto 0x%02x\n",
1951 device_get_nameunit(sc->sc_dev), sc->proto));
1954 * State handling for BBB transfers.
1956 * The subroutine is rather long. It steps through the states given in
1957 * Annex A of the Bulk-Only specification.
1958 * Each state first does the error handling of the previous transfer
1959 * and then prepares the next transfer.
1960 * Each transfer is done asynchroneously so after the request/transfer
1961 * has been submitted you will find a 'return;'.
1964 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1965 device_get_nameunit(sc->sc_dev), sc->transfer_state,
1966 states[sc->transfer_state], xfer, usbd_errstr(err)));
1968 switch (sc->transfer_state) {
1970 /***** Bulk Transfer *****/
1971 case TSTATE_BBB_COMMAND:
1972 /* Command transport phase, error handling */
1973 if (err) {
1974 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1975 device_get_nameunit(sc->sc_dev)));
1976 /* If the device detects that the CBW is invalid, then
1977 * the device may STALL both bulk endpoints and require
1978 * a Bulk-Reset
1980 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1981 return;
1984 /* Data transport phase, setup transfer */
1985 sc->transfer_state = TSTATE_BBB_DATA;
1986 if (sc->transfer_dir == DIR_IN) {
1987 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1988 sc->transfer_data, sc->transfer_datalen,
1989 USBD_SHORT_XFER_OK,
1990 sc->transfer_xfer[XFER_BBB_DATA]))
1991 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1993 return;
1994 } else if (sc->transfer_dir == DIR_OUT) {
1995 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1996 sc->transfer_data, sc->transfer_datalen,
1997 0, /* fixed length transfer */
1998 sc->transfer_xfer[XFER_BBB_DATA]))
1999 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2001 return;
2002 } else {
2003 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
2004 device_get_nameunit(sc->sc_dev)));
2007 /* FALLTHROUGH if no data phase, err == 0 */
2008 case TSTATE_BBB_DATA:
2009 /* Command transport phase, error handling (ignored if no data
2010 * phase (fallthrough from previous state)) */
2011 if (sc->transfer_dir != DIR_NONE) {
2012 /* retrieve the length of the transfer that was done */
2013 usbd_get_xfer_status(xfer, NULL, NULL,
2014 &sc->transfer_actlen, NULL);
2016 if (err) {
2017 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
2018 "%s\n", device_get_nameunit(sc->sc_dev),
2019 (sc->transfer_dir == DIR_IN?"in":"out"),
2020 sc->transfer_datalen,usbd_errstr(err)));
2022 if (err == USBD_STALLED) {
2023 umass_clear_endpoint_stall(sc,
2024 (sc->transfer_dir == DIR_IN?
2025 sc->bulkin:sc->bulkout),
2026 (sc->transfer_dir == DIR_IN?
2027 sc->bulkin_pipe:sc->bulkout_pipe),
2028 TSTATE_BBB_DCLEAR,
2029 sc->transfer_xfer[XFER_BBB_DCLEAR]);
2030 return;
2031 } else {
2032 /* Unless the error is a pipe stall the
2033 * error is fatal.
2035 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
2036 return;
2041 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
2042 umass_dump_buffer(sc, sc->transfer_data,
2043 sc->transfer_datalen, 48));
2047 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
2048 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
2049 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
2050 /* Reading of CSW after bulk stall condition in data phase
2051 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
2052 * reading CSW (TSTATE_BBB_SCLEAR).
2053 * In the case of no data phase or successfull data phase,
2054 * err == 0 and the following if block is passed.
2056 if (err) { /* should not occur */
2057 /* try the transfer below, even if clear stall failed */
2058 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
2059 ", %s\n", device_get_nameunit(sc->sc_dev),
2060 (sc->transfer_dir == DIR_IN? "in":"out"),
2061 usbd_errstr(err)));
2062 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2063 return;
2066 /* Status transport phase, setup transfer */
2067 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
2068 sc->transfer_state == TSTATE_BBB_DATA ||
2069 sc->transfer_state == TSTATE_BBB_DCLEAR) {
2070 /* After no data phase, successfull data phase and
2071 * after clearing bulk-in/-out stall condition
2073 sc->transfer_state = TSTATE_BBB_STATUS1;
2074 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
2075 } else {
2076 /* After first attempt of fetching CSW */
2077 sc->transfer_state = TSTATE_BBB_STATUS2;
2078 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
2081 /* Read the Command Status Wrapper via bulk-in endpoint. */
2082 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2083 &sc->csw, UMASS_BBB_CSW_SIZE, 0,
2084 next_xfer)) {
2085 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2086 return;
2089 return;
2090 case TSTATE_BBB_STATUS1: /* first attempt */
2091 case TSTATE_BBB_STATUS2: /* second attempt */
2092 /* Status transfer, error handling */
2094 int Residue;
2095 if (err) {
2096 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
2097 device_get_nameunit(sc->sc_dev), usbd_errstr(err),
2098 (sc->transfer_state == TSTATE_BBB_STATUS1?
2099 ", retrying":"")));
2101 /* If this was the first attempt at fetching the CSW
2102 * retry it, otherwise fail.
2104 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
2105 umass_clear_endpoint_stall(sc,
2106 sc->bulkin, sc->bulkin_pipe,
2107 TSTATE_BBB_SCLEAR,
2108 sc->transfer_xfer[XFER_BBB_SCLEAR]);
2109 return;
2110 } else {
2111 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2112 return;
2116 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2118 /* Translate weird command-status signatures. */
2119 if ((sc->quirks & WRONG_CSWSIG) &&
2120 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
2121 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2123 Residue = UGETDW(sc->csw.dCSWDataResidue);
2124 if (Residue == 0 &&
2125 sc->transfer_datalen - sc->transfer_actlen != 0)
2126 Residue = sc->transfer_datalen - sc->transfer_actlen;
2128 /* Check CSW and handle any error */
2129 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2130 /* Invalid CSW: Wrong signature or wrong tag might
2131 * indicate that the device is confused -> reset it.
2133 kprintf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
2134 device_get_nameunit(sc->sc_dev),
2135 UGETDW(sc->csw.dCSWSignature),
2136 CSWSIGNATURE);
2138 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2139 return;
2140 } else if (UGETDW(sc->csw.dCSWTag)
2141 != UGETDW(sc->cbw.dCBWTag)) {
2142 kprintf("%s: Invalid CSW: tag %d should be %d\n",
2143 device_get_nameunit(sc->sc_dev),
2144 UGETDW(sc->csw.dCSWTag),
2145 UGETDW(sc->cbw.dCBWTag));
2147 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2148 return;
2150 /* CSW is valid here */
2151 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2152 kprintf("%s: Invalid CSW: status %d > %d\n",
2153 device_get_nameunit(sc->sc_dev),
2154 sc->csw.bCSWStatus,
2155 CSWSTATUS_PHASE);
2157 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2158 return;
2159 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2160 kprintf("%s: Phase Error, residue = %d\n",
2161 device_get_nameunit(sc->sc_dev), Residue);
2163 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
2164 return;
2166 } else if (sc->transfer_actlen > sc->transfer_datalen) {
2167 /* Buffer overrun! Don't let this go by unnoticed */
2168 panic("%s: transferred %db instead of %db",
2169 device_get_nameunit(sc->sc_dev),
2170 sc->transfer_actlen, sc->transfer_datalen);
2172 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2173 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
2174 device_get_nameunit(sc->sc_dev), Residue));
2176 /* SCSI command failed but transfer was succesful */
2177 sc->transfer_state = TSTATE_IDLE;
2178 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2179 STATUS_CMD_FAILED);
2180 return;
2182 } else { /* success */
2183 sc->transfer_state = TSTATE_IDLE;
2184 sc->transfer_cb(sc, sc->transfer_priv, Residue,
2185 STATUS_CMD_OK);
2187 return;
2191 /***** Bulk Reset *****/
2192 case TSTATE_BBB_RESET1:
2193 if (err)
2194 kprintf("%s: BBB reset failed, %s\n",
2195 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2197 umass_clear_endpoint_stall(sc,
2198 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
2199 sc->transfer_xfer[XFER_BBB_RESET2]);
2201 return;
2202 case TSTATE_BBB_RESET2:
2203 if (err) /* should not occur */
2204 kprintf("%s: BBB bulk-in clear stall failed, %s\n",
2205 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2206 /* no error recovery, otherwise we end up in a loop */
2208 umass_clear_endpoint_stall(sc,
2209 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
2210 sc->transfer_xfer[XFER_BBB_RESET3]);
2212 return;
2213 case TSTATE_BBB_RESET3:
2214 if (err) /* should not occur */
2215 kprintf("%s: BBB bulk-out clear stall failed, %s\n",
2216 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2217 /* no error recovery, otherwise we end up in a loop */
2219 sc->transfer_state = TSTATE_IDLE;
2220 if (sc->transfer_priv) {
2221 sc->transfer_cb(sc, sc->transfer_priv,
2222 sc->transfer_datalen,
2223 sc->transfer_status);
2226 return;
2228 /***** Default *****/
2229 default:
2230 panic("%s: Unknown state %d",
2231 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2235 static int
2236 umass_bbb_get_max_lun(struct umass_softc *sc)
2238 usbd_device_handle udev;
2239 usb_device_request_t req;
2240 usbd_status err;
2241 usb_interface_descriptor_t *id;
2242 int maxlun = 0;
2243 u_int8_t buf = 0;
2245 usbd_interface2device_handle(sc->iface, &udev);
2246 id = usbd_get_interface_descriptor(sc->iface);
2248 /* The Get Max Lun command is a class-specific request. */
2249 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2250 req.bRequest = UR_BBB_GET_MAX_LUN;
2251 USETW(req.wValue, 0);
2252 USETW(req.wIndex, id->bInterfaceNumber);
2253 USETW(req.wLength, 1);
2255 err = usbd_do_request(udev, &req, &buf);
2256 switch (err) {
2257 case USBD_NORMAL_COMPLETION:
2258 maxlun = buf;
2259 DPRINTF(UDMASS_BBB, ("%s: Max Lun is %d\n",
2260 device_get_nameunit(sc->sc_dev), maxlun));
2261 break;
2262 case USBD_STALLED:
2263 case USBD_SHORT_XFER:
2264 default:
2265 /* Device doesn't support Get Max Lun request. */
2266 kprintf("%s: Get Max Lun not supported (%s)\n",
2267 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2268 /* XXX Should we port_reset the device? */
2269 break;
2272 return(maxlun);
2276 * Command/Bulk/Interrupt (CBI) specific functions
2279 static int
2280 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
2281 usbd_xfer_handle xfer)
2283 usbd_device_handle udev;
2285 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2286 ("%s: umass_cbi_adsc: wrong sc->proto 0x%02x\n",
2287 device_get_nameunit(sc->sc_dev), sc->proto));
2289 usbd_interface2device_handle(sc->iface, &udev);
2291 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2292 sc->request.bRequest = UR_CBI_ADSC;
2293 USETW(sc->request.wValue, 0);
2294 USETW(sc->request.wIndex, sc->ifaceno);
2295 USETW(sc->request.wLength, buflen);
2296 return umass_setup_ctrl_transfer(sc, udev, &sc->request, buffer,
2297 buflen, 0, xfer);
2301 static void
2302 umass_cbi_reset(struct umass_softc *sc, int status)
2304 int i;
2305 # define SEND_DIAGNOSTIC_CMDLEN 12
2307 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2308 ("%s: umass_cbi_reset: wrong sc->proto 0x%02x\n",
2309 device_get_nameunit(sc->sc_dev), sc->proto));
2312 * Command Block Reset Protocol
2314 * First send a reset request to the device. Then clear
2315 * any possibly stalled bulk endpoints.
2317 * This is done in 3 steps, states:
2318 * TSTATE_CBI_RESET1
2319 * TSTATE_CBI_RESET2
2320 * TSTATE_CBI_RESET3
2322 * If the reset doesn't succeed, the device should be port reset.
2325 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
2326 device_get_nameunit(sc->sc_dev)));
2328 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
2329 ("%s: CBL struct is too small (%ld < %d)\n",
2330 device_get_nameunit(sc->sc_dev),
2331 (long)sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
2333 sc->transfer_state = TSTATE_CBI_RESET1;
2334 sc->transfer_status = status;
2336 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
2337 * the two the last 10 bytes of the cbl is filled with 0xff (section
2338 * 2.2 of the CBI spec).
2340 sc->cbl[0] = 0x1d; /* Command Block Reset */
2341 sc->cbl[1] = 0x04;
2342 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
2343 sc->cbl[i] = 0xff;
2345 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
2346 sc->transfer_xfer[XFER_CBI_RESET1]);
2347 /* XXX if the command fails we should reset the port on the bub */
2350 static void
2351 umass_cbi_transfer(struct umass_softc *sc, int lun,
2352 void *cmd, int cmdlen, void *data, int datalen, int dir,
2353 u_int timeout, transfer_cb_f cb, void *priv)
2355 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2356 ("%s: umass_cbi_transfer: wrong sc->proto 0x%02x\n",
2357 device_get_nameunit(sc->sc_dev), sc->proto));
2358 /* Be a little generous. */
2359 sc->timeout = timeout + UMASS_TIMEOUT;
2362 * Do a CBI transfer with cmdlen bytes from cmd, possibly
2363 * a data phase of datalen bytes from/to the device and finally a
2364 * csw read phase.
2365 * If the data direction was inbound a maximum of datalen bytes
2366 * is stored in the buffer pointed to by data.
2368 * umass_cbi_transfer initialises the transfer and lets the state
2369 * machine in umass_cbi_state handle the completion. It uses the
2370 * following states:
2371 * TSTATE_CBI_COMMAND
2372 * -> XXX fill in
2374 * An error in any of those states will invoke
2375 * umass_cbi_reset.
2378 /* check the given arguments */
2379 KASSERT(datalen == 0 || data != NULL,
2380 ("%s: datalen > 0, but no buffer",device_get_nameunit(sc->sc_dev)));
2381 KASSERT(datalen == 0 || dir != DIR_NONE,
2382 ("%s: direction is NONE while datalen is not zero\n",
2383 device_get_nameunit(sc->sc_dev)));
2385 /* store the details for the data transfer phase */
2386 sc->transfer_dir = dir;
2387 sc->transfer_data = data;
2388 sc->transfer_datalen = datalen;
2389 sc->transfer_actlen = 0;
2390 sc->transfer_cb = cb;
2391 sc->transfer_priv = priv;
2392 sc->transfer_status = STATUS_CMD_OK;
2394 /* move from idle to the command state */
2395 sc->transfer_state = TSTATE_CBI_COMMAND;
2397 DIF(UDMASS_CBI, umass_cbi_dump_cmd(sc, cmd, cmdlen));
2399 /* Send the Command Block from host to device via control endpoint. */
2400 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
2401 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2404 static void
2405 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
2406 usbd_status err)
2408 struct umass_softc *sc = (struct umass_softc *) priv;
2410 KASSERT(sc->proto & (UMASS_PROTO_CBI|UMASS_PROTO_CBI_I),
2411 ("%s: umass_cbi_state: wrong sc->proto 0x%02x\n",
2412 device_get_nameunit(sc->sc_dev), sc->proto));
2415 * State handling for CBI transfers.
2418 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
2419 device_get_nameunit(sc->sc_dev), sc->transfer_state,
2420 states[sc->transfer_state], xfer, usbd_errstr(err)));
2422 switch (sc->transfer_state) {
2424 /***** CBI Transfer *****/
2425 case TSTATE_CBI_COMMAND:
2426 if (err == USBD_STALLED) {
2427 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
2428 device_get_nameunit(sc->sc_dev)));
2429 /* Status transport by control pipe (section 2.3.2.1).
2430 * The command contained in the command block failed.
2432 * The control pipe has already been unstalled by the
2433 * USB stack.
2434 * Section 2.4.3.1.1 states that the bulk in endpoints
2435 * should not be stalled at this point.
2438 sc->transfer_state = TSTATE_IDLE;
2439 sc->transfer_cb(sc, sc->transfer_priv,
2440 sc->transfer_datalen,
2441 STATUS_CMD_FAILED);
2443 return;
2444 } else if (err) {
2445 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
2446 device_get_nameunit(sc->sc_dev)));
2447 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2449 return;
2452 sc->transfer_state = TSTATE_CBI_DATA;
2453 if (sc->transfer_dir == DIR_IN) {
2454 if (umass_setup_transfer(sc, sc->bulkin_pipe,
2455 sc->transfer_data, sc->transfer_datalen,
2456 USBD_SHORT_XFER_OK,
2457 sc->transfer_xfer[XFER_CBI_DATA]))
2458 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2460 } else if (sc->transfer_dir == DIR_OUT) {
2461 if (umass_setup_transfer(sc, sc->bulkout_pipe,
2462 sc->transfer_data, sc->transfer_datalen,
2463 0, /* fixed length transfer */
2464 sc->transfer_xfer[XFER_CBI_DATA]))
2465 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2467 } else if (sc->proto & UMASS_PROTO_CBI_I) {
2468 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2469 device_get_nameunit(sc->sc_dev)));
2470 sc->transfer_state = TSTATE_CBI_STATUS;
2471 if (umass_setup_transfer(sc, sc->intrin_pipe,
2472 &sc->sbl, sizeof(sc->sbl),
2473 0, /* fixed length transfer */
2474 sc->transfer_xfer[XFER_CBI_STATUS])){
2475 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2477 } else {
2478 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
2479 device_get_nameunit(sc->sc_dev)));
2480 /* No command completion interrupt. Request
2481 * sense data.
2483 sc->transfer_state = TSTATE_IDLE;
2484 sc->transfer_cb(sc, sc->transfer_priv,
2485 0, STATUS_CMD_UNKNOWN);
2488 return;
2490 case TSTATE_CBI_DATA:
2491 /* retrieve the length of the transfer that was done */
2492 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
2494 if (err) {
2495 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
2496 "%s\n", device_get_nameunit(sc->sc_dev),
2497 (sc->transfer_dir == DIR_IN?"in":"out"),
2498 sc->transfer_datalen,usbd_errstr(err)));
2500 if (err == USBD_STALLED) {
2501 umass_clear_endpoint_stall(sc,
2502 sc->bulkin, sc->bulkin_pipe,
2503 TSTATE_CBI_DCLEAR,
2504 sc->transfer_xfer[XFER_CBI_DCLEAR]);
2505 } else {
2506 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2508 return;
2511 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2512 umass_dump_buffer(sc, sc->transfer_data,
2513 sc->transfer_actlen, 48));
2515 if (sc->proto & UMASS_PROTO_CBI_I) {
2516 sc->transfer_state = TSTATE_CBI_STATUS;
2517 if (umass_setup_transfer(sc, sc->intrin_pipe,
2518 &sc->sbl, sizeof(sc->sbl),
2519 0, /* fixed length transfer */
2520 sc->transfer_xfer[XFER_CBI_STATUS])){
2521 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2523 } else {
2524 /* No command completion interrupt. Request
2525 * sense to get status of command.
2527 sc->transfer_state = TSTATE_IDLE;
2528 sc->transfer_cb(sc, sc->transfer_priv,
2529 sc->transfer_datalen - sc->transfer_actlen,
2530 STATUS_CMD_UNKNOWN);
2532 return;
2534 case TSTATE_CBI_STATUS:
2535 if (err) {
2536 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2537 device_get_nameunit(sc->sc_dev)));
2538 /* Status transport by interrupt pipe (section 2.3.2.2).
2541 if (err == USBD_STALLED) {
2542 umass_clear_endpoint_stall(sc,
2543 sc->intrin, sc->intrin_pipe,
2544 TSTATE_CBI_SCLEAR,
2545 sc->transfer_xfer[XFER_CBI_SCLEAR]);
2546 } else {
2547 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2549 return;
2552 /* Dissect the information in the buffer */
2554 if (sc->proto & UMASS_PROTO_UFI) {
2555 int status;
2557 /* Section 3.4.3.1.3 specifies that the UFI command
2558 * protocol returns an ASC and ASCQ in the interrupt
2559 * data block.
2562 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2563 "ASCQ = 0x%02x\n",
2564 device_get_nameunit(sc->sc_dev),
2565 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2567 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2568 status = STATUS_CMD_OK;
2569 else
2570 status = STATUS_CMD_FAILED;
2572 sc->transfer_state = TSTATE_IDLE;
2573 sc->transfer_cb(sc, sc->transfer_priv,
2574 sc->transfer_datalen - sc->transfer_actlen,
2575 status);
2576 } else {
2577 /* Command Interrupt Data Block */
2578 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2579 device_get_nameunit(sc->sc_dev),
2580 sc->sbl.common.type, sc->sbl.common.value));
2582 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2583 int err;
2585 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2586 == IDB_VALUE_PASS) {
2587 err = STATUS_CMD_OK;
2588 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2589 == IDB_VALUE_FAIL ||
2590 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2591 == IDB_VALUE_PERSISTENT) {
2592 err = STATUS_CMD_FAILED;
2593 } else {
2594 err = STATUS_WIRE_FAILED;
2597 sc->transfer_state = TSTATE_IDLE;
2598 sc->transfer_cb(sc, sc->transfer_priv,
2599 sc->transfer_datalen-sc->transfer_actlen,
2600 err);
2603 return;
2605 case TSTATE_CBI_DCLEAR:
2606 if (err) { /* should not occur */
2607 kprintf("%s: CBI bulk-in/out stall clear failed, %s\n",
2608 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2609 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2612 sc->transfer_state = TSTATE_IDLE;
2613 sc->transfer_cb(sc, sc->transfer_priv,
2614 sc->transfer_datalen,
2615 STATUS_CMD_FAILED);
2616 return;
2618 case TSTATE_CBI_SCLEAR:
2619 if (err) /* should not occur */
2620 kprintf("%s: CBI intr-in stall clear failed, %s\n",
2621 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2623 /* Something really bad is going on. Reset the device */
2624 umass_cbi_reset(sc, STATUS_CMD_FAILED);
2625 return;
2627 /***** CBI Reset *****/
2628 case TSTATE_CBI_RESET1:
2629 if (err)
2630 kprintf("%s: CBI reset failed, %s\n",
2631 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2633 umass_clear_endpoint_stall(sc,
2634 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2635 sc->transfer_xfer[XFER_CBI_RESET2]);
2637 return;
2638 case TSTATE_CBI_RESET2:
2639 if (err) /* should not occur */
2640 kprintf("%s: CBI bulk-in stall clear failed, %s\n",
2641 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2642 /* no error recovery, otherwise we end up in a loop */
2644 umass_clear_endpoint_stall(sc,
2645 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2646 sc->transfer_xfer[XFER_CBI_RESET3]);
2648 return;
2649 case TSTATE_CBI_RESET3:
2650 if (err) /* should not occur */
2651 kprintf("%s: CBI bulk-out stall clear failed, %s\n",
2652 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
2653 /* no error recovery, otherwise we end up in a loop */
2655 sc->transfer_state = TSTATE_IDLE;
2656 if (sc->transfer_priv) {
2657 sc->transfer_cb(sc, sc->transfer_priv,
2658 sc->transfer_datalen,
2659 sc->transfer_status);
2662 return;
2665 /***** Default *****/
2666 default:
2667 panic("%s: Unknown state %d",
2668 device_get_nameunit(sc->sc_dev), sc->transfer_state);
2676 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2679 static int
2680 umass_cam_attach_sim(struct umass_softc *sc)
2682 struct cam_devq *devq; /* Per device Queue */
2684 /* A HBA is attached to the CAM layer.
2686 * The CAM layer will then after a while start probing for
2687 * devices on the bus. The number of SIMs is limited to one.
2690 callout_init(&sc->rescan_timeout);
2691 devq = cam_simq_alloc(1 /*maximum openings*/);
2692 if (devq == NULL)
2693 return(ENOMEM);
2695 sc->umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll,
2696 DEVNAME_SIM,
2697 sc /*priv*/,
2698 device_get_unit(sc->sc_dev) /*unit number*/,
2699 1 /*maximum device openings*/,
2700 0 /*maximum tagged device openings*/,
2701 devq);
2702 cam_simq_release(devq);
2703 if (sc->umass_sim == NULL)
2704 return(ENOMEM);
2707 * If we could not register the bus we must immediately free the
2708 * sim so we do not attempt to deregister a bus later on that we
2709 * had not registered.
2711 if (xpt_bus_register(sc->umass_sim, device_get_unit(sc->sc_dev)) !=
2712 CAM_SUCCESS) {
2713 cam_sim_free(sc->umass_sim);
2714 sc->umass_sim = NULL;
2715 return(ENOMEM);
2718 return(0);
2721 static void
2722 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2724 #ifdef USB_DEBUG
2725 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2726 DPRINTF(UDMASS_SCSI, ("%s:%d Rescan failed, 0x%04x\n",
2727 periph->periph_name, periph->unit_number,
2728 ccb->ccb_h.status));
2729 } else {
2730 DPRINTF(UDMASS_SCSI, ("%s%d: Rescan succeeded\n",
2731 periph->periph_name, periph->unit_number));
2733 #endif
2735 xpt_free_path(ccb->ccb_h.path);
2736 kfree(ccb, M_USBDEV);
2739 static void
2740 umass_cam_rescan(void *addr)
2742 struct umass_softc *sc = (struct umass_softc *) addr;
2743 struct cam_path *path;
2744 union ccb *ccb;
2746 ccb = kmalloc(sizeof(union ccb), M_USBDEV, M_INTWAIT|M_ZERO);
2748 DPRINTF(UDMASS_SCSI, ("scbus%d: scanning for %s:%d:%d:%d\n",
2749 cam_sim_path(sc->umass_sim),
2750 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2751 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD));
2753 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->umass_sim),
2754 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2755 != CAM_REQ_CMP) {
2756 kfree(ccb, M_USBDEV);
2757 return;
2760 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2761 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2762 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2763 ccb->crcn.flags = CAM_FLAG_NONE;
2764 xpt_action(ccb);
2766 /* The scan is in progress now. */
2769 static int
2770 umass_cam_attach(struct umass_softc *sc)
2772 #ifndef USB_DEBUG
2773 if (bootverbose)
2774 #endif
2775 kprintf("%s:%d:%d:%d: Attached to scbus%d\n",
2776 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2777 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD,
2778 cam_sim_path(sc->umass_sim));
2780 if (!cold) {
2782 * Notify CAM of the new device after a 0.2 second delay. Any
2783 * failure is benign, as the user can still do it by hand
2784 * (camcontrol rescan <busno>). Only do this if we are not
2785 * booting, because CAM does a scan after booting has
2786 * completed, when interrupts have been enabled.
2788 callout_reset(&sc->rescan_timeout, MS_TO_TICKS(200),
2789 umass_cam_rescan, sc);
2792 return(0); /* always succesfull */
2795 /* umass_cam_detach
2796 * detach from the CAM layer
2799 static int
2800 umass_cam_detach_sim(struct umass_softc *sc)
2802 callout_stop(&sc->rescan_timeout);
2803 if (sc->umass_sim) {
2804 xpt_bus_deregister(cam_sim_path(sc->umass_sim));
2805 cam_sim_free(sc->umass_sim);
2807 sc->umass_sim = NULL;
2810 return(0);
2813 /* umass_cam_action
2814 * CAM requests for action come through here
2817 static void
2818 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2820 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2822 /* The softc is still there, but marked as going away. umass_cam_detach
2823 * has not yet notified CAM of the lost device however.
2825 if (sc && (sc->flags & UMASS_FLAGS_GONE)) {
2826 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2827 "Invalid target (gone)\n",
2828 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2829 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2830 ccb->ccb_h.func_code));
2831 ccb->ccb_h.status = CAM_TID_INVALID;
2832 xpt_done(ccb);
2833 return;
2836 /* Verify, depending on the operation to perform, that we either got a
2837 * valid sc, because an existing target was referenced, or otherwise
2838 * the SIM is addressed.
2840 * This avoids bombing out at a kprintf and does give the CAM layer some
2841 * sensible feedback on errors.
2843 switch (ccb->ccb_h.func_code) {
2844 case XPT_SCSI_IO:
2845 case XPT_RESET_DEV:
2846 case XPT_GET_TRAN_SETTINGS:
2847 case XPT_SET_TRAN_SETTINGS:
2848 case XPT_CALC_GEOMETRY:
2849 /* the opcodes requiring a target. These should never occur. */
2850 if (sc == NULL) {
2851 kprintf("%s:%d:%d:%d:func_code 0x%04x: "
2852 "Invalid target (target needed)\n",
2853 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2854 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2855 ccb->ccb_h.func_code);
2857 ccb->ccb_h.status = CAM_TID_INVALID;
2858 xpt_done(ccb);
2859 return;
2861 break;
2862 case XPT_PATH_INQ:
2863 case XPT_NOOP:
2864 /* The opcodes sometimes aimed at a target (sc is valid),
2865 * sometimes aimed at the SIM (sc is invalid and target is
2866 * CAM_TARGET_WILDCARD)
2868 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2869 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2870 "Invalid target (no wildcard)\n",
2871 DEVNAME_SIM, cam_sim_path(sc->umass_sim),
2872 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2873 ccb->ccb_h.func_code));
2875 ccb->ccb_h.status = CAM_TID_INVALID;
2876 xpt_done(ccb);
2877 return;
2879 break;
2880 default:
2881 /* XXX Hm, we should check the input parameters */
2882 break;
2885 /* Perform the requested action */
2886 switch (ccb->ccb_h.func_code) {
2887 case XPT_SCSI_IO:
2889 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2890 int dir;
2891 unsigned char *cmd;
2892 int cmdlen;
2893 unsigned char *rcmd;
2894 int rcmdlen;
2896 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2897 "cmd: 0x%02x, flags: 0x%02x, "
2898 "%db cmd/%db data/%db sense\n",
2899 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2900 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2901 csio->cdb_io.cdb_bytes[0],
2902 ccb->ccb_h.flags & CAM_DIR_MASK,
2903 csio->cdb_len, csio->dxfer_len,
2904 csio->sense_len));
2906 /* clear the end of the buffer to make sure we don't send out
2907 * garbage.
2909 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2910 == CAM_DIR_OUT)
2911 umass_dump_buffer(sc, csio->data_ptr,
2912 csio->dxfer_len, 48));
2914 if (sc->transfer_state != TSTATE_IDLE) {
2915 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2916 "I/O in progress, deferring (state %d, %s)\n",
2917 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
2918 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2919 sc->transfer_state,states[sc->transfer_state]));
2920 ccb->ccb_h.status = CAM_SCSI_BUSY;
2921 xpt_done(ccb);
2922 return;
2925 switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2926 case CAM_DIR_IN:
2927 dir = DIR_IN;
2928 break;
2929 case CAM_DIR_OUT:
2930 dir = DIR_OUT;
2931 break;
2932 default:
2933 dir = DIR_NONE;
2936 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2939 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2940 cmd = (unsigned char *) csio->cdb_io.cdb_ptr;
2941 } else {
2942 cmd = (unsigned char *) &csio->cdb_io.cdb_bytes;
2944 cmdlen = csio->cdb_len;
2945 rcmd = (unsigned char *) &sc->cam_scsi_command;
2946 rcmdlen = sizeof(sc->cam_scsi_command);
2948 /* sc->transform will convert the command to the command
2949 * (format) needed by the specific command set and return
2950 * the converted command in a buffer pointed to be rcmd.
2951 * We pass in a buffer, but if the command does not
2952 * have to be transformed it returns a ptr to the original
2953 * buffer (see umass_scsi_transform).
2956 if (sc->transform(sc, cmd, cmdlen, &rcmd, &rcmdlen)) {
2958 * Handle EVPD inquiry for broken devices first
2959 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2961 if ((sc->quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2962 rcmd[0] == INQUIRY && (rcmd[1] & SI_EVPD)) {
2963 struct scsi_sense_data *sense;
2965 sense = &ccb->csio.sense_data;
2966 bzero(sense, sizeof(*sense));
2967 sense->error_code = SSD_CURRENT_ERROR;
2968 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2969 sense->add_sense_code = 0x24;
2970 sense->extra_len = 10;
2971 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2972 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2973 CAM_AUTOSNS_VALID;
2974 xpt_done(ccb);
2975 return;
2977 /* Return fake inquiry data for broken devices */
2978 if ((sc->quirks & NO_INQUIRY) && rcmd[0] == INQUIRY) {
2979 struct ccb_scsiio *csio = &ccb->csio;
2981 memcpy(csio->data_ptr, &fake_inq_data,
2982 sizeof(fake_inq_data));
2983 csio->scsi_status = SCSI_STATUS_OK;
2984 ccb->ccb_h.status = CAM_REQ_CMP;
2985 xpt_done(ccb);
2986 return;
2988 if ((sc->quirks & FORCE_SHORT_INQUIRY) &&
2989 rcmd[0] == INQUIRY) {
2990 csio->dxfer_len = SHORT_INQUIRY_LENGTH;
2992 sc->transfer(sc, ccb->ccb_h.target_lun, rcmd, rcmdlen,
2993 csio->data_ptr,
2994 csio->dxfer_len, dir, ccb->ccb_h.timeout,
2995 umass_cam_cb, (void *) ccb);
2996 } else {
2997 ccb->ccb_h.status = CAM_REQ_INVALID;
2998 xpt_done(ccb);
3001 break;
3003 case XPT_PATH_INQ:
3005 struct ccb_pathinq *cpi = &ccb->cpi;
3007 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
3008 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
3009 cam_sim_path(sc->umass_sim),
3010 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3012 /* host specific information */
3013 cpi->version_num = 1;
3014 cpi->hba_inquiry = 0;
3015 cpi->target_sprt = 0;
3016 cpi->hba_misc = PIM_NO_6_BYTE;
3017 cpi->hba_eng_cnt = 0;
3018 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
3019 cpi->initiator_id = UMASS_SCSIID_HOST;
3020 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3021 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
3022 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3023 cpi->unit_number = cam_sim_unit(sim);
3024 cpi->bus_id = device_get_unit(sc->sc_dev);
3026 if (sc == NULL) {
3027 cpi->base_transfer_speed = 0;
3028 cpi->max_lun = 0;
3029 } else {
3030 if (sc->quirks & FLOPPY_SPEED) {
3031 cpi->base_transfer_speed =
3032 UMASS_FLOPPY_TRANSFER_SPEED;
3033 } else if (usbd_get_speed(sc->sc_udev) ==
3034 USB_SPEED_HIGH) {
3035 cpi->base_transfer_speed =
3036 UMASS_HIGH_TRANSFER_SPEED;
3037 } else {
3038 cpi->base_transfer_speed =
3039 UMASS_FULL_TRANSFER_SPEED;
3041 cpi->max_lun = sc->maxlun;
3044 cpi->ccb_h.status = CAM_REQ_CMP;
3045 xpt_done(ccb);
3046 break;
3048 case XPT_RESET_DEV:
3050 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
3051 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3052 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3054 ccb->ccb_h.status = CAM_REQ_INPROG;
3055 umass_reset(sc, umass_cam_cb, (void *) ccb);
3056 break;
3058 case XPT_GET_TRAN_SETTINGS:
3060 struct ccb_trans_settings *cts = &ccb->cts;
3061 cts->protocol = PROTO_SCSI;
3062 cts->protocol_version = SCSI_REV_2;
3063 cts->transport = XPORT_USB;
3064 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
3065 cts->xport_specific.valid = 0;
3067 ccb->ccb_h.status = CAM_REQ_CMP;
3068 xpt_done(ccb);
3069 break;
3071 case XPT_SET_TRAN_SETTINGS:
3073 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3074 device_get_nameunit(sc->sc_dev), cam_sim_path(sc->umass_sim),
3075 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3077 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3078 xpt_done(ccb);
3079 break;
3081 case XPT_CALC_GEOMETRY:
3083 cam_calc_geometry(&ccb->ccg, /*extended*/1);
3084 xpt_done(ccb);
3085 break;
3087 case XPT_NOOP:
3089 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
3090 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
3091 cam_sim_path(sc->umass_sim),
3092 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
3094 ccb->ccb_h.status = CAM_REQ_CMP;
3095 xpt_done(ccb);
3096 break;
3098 default:
3099 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
3100 "Not implemented\n",
3101 (sc == NULL? DEVNAME_SIM:device_get_nameunit(sc->sc_dev)),
3102 cam_sim_path(sc->umass_sim),
3103 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
3104 ccb->ccb_h.func_code));
3106 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3107 xpt_done(ccb);
3108 break;
3112 static void
3113 umass_cam_poll(struct cam_sim *sim)
3115 struct umass_softc *sc = (struct umass_softc *) sim->softc;
3117 KKASSERT(sc != NULL);
3119 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
3120 device_get_nameunit(sc->sc_dev)));
3122 usbd_set_polling(sc->sc_udev, 1);
3123 usbd_dopoll(sc->iface);
3124 usbd_set_polling(sc->sc_udev, 0);
3128 /* umass_cam_cb
3129 * finalise a completed CAM command
3132 static void
3133 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
3135 union ccb *ccb = (union ccb *) priv;
3136 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3138 csio->resid = residue;
3140 switch (status) {
3141 case STATUS_CMD_OK:
3142 ccb->ccb_h.status = CAM_REQ_CMP;
3143 if ((sc->quirks & READ_CAPACITY_OFFBY1) &&
3144 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3145 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3146 struct scsi_read_capacity_data *rcap;
3147 uint32_t maxsector;
3149 rcap = (struct scsi_read_capacity_data *)csio->data_ptr;
3150 maxsector = scsi_4btoul(rcap->addr) - 1;
3151 scsi_ulto4b(maxsector, rcap->addr);
3153 xpt_done(ccb);
3154 break;
3156 case STATUS_CMD_UNKNOWN:
3157 case STATUS_CMD_FAILED:
3158 switch (ccb->ccb_h.func_code) {
3159 case XPT_SCSI_IO:
3161 unsigned char *rcmd;
3162 int rcmdlen;
3164 /* fetch sense data */
3165 /* the rest of the command was filled in at attach */
3166 sc->cam_scsi_sense.length = csio->sense_len;
3168 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
3169 device_get_nameunit(sc->sc_dev), csio->sense_len));
3171 rcmd = (unsigned char *) &sc->cam_scsi_command;
3172 rcmdlen = sizeof(sc->cam_scsi_command);
3174 if (sc->transform(sc,
3175 (unsigned char *) &sc->cam_scsi_sense,
3176 sizeof(sc->cam_scsi_sense),
3177 &rcmd, &rcmdlen)) {
3178 if ((sc->quirks & FORCE_SHORT_INQUIRY) && (rcmd[0] == INQUIRY)) {
3179 csio->sense_len = SHORT_INQUIRY_LENGTH;
3181 sc->transfer(sc, ccb->ccb_h.target_lun,
3182 rcmd, rcmdlen,
3183 &csio->sense_data,
3184 csio->sense_len, DIR_IN, ccb->ccb_h.timeout,
3185 umass_cam_sense_cb, (void *) ccb);
3186 } else {
3187 panic("transform(REQUEST_SENSE) failed");
3189 break;
3191 case XPT_RESET_DEV: /* Reset failed */
3192 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3193 xpt_done(ccb);
3194 break;
3195 default:
3196 panic("umass_cam_cb called for func_code %d",
3197 ccb->ccb_h.func_code);
3199 break;
3201 case STATUS_WIRE_FAILED:
3202 /* the wire protocol failed and will have recovered
3203 * (hopefully). We return an error to CAM and let CAM retry
3204 * the command if necessary.
3206 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3207 xpt_done(ccb);
3208 break;
3209 default:
3210 panic("%s: Unknown status %d in umass_cam_cb",
3211 device_get_nameunit(sc->sc_dev), status);
3215 /* Finalise a completed autosense operation
3217 static void
3218 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
3220 union ccb *ccb = (union ccb *) priv;
3221 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
3222 unsigned char *rcmd;
3223 int rcmdlen;
3225 switch (status) {
3226 case STATUS_CMD_OK:
3227 case STATUS_CMD_UNKNOWN:
3228 case STATUS_CMD_FAILED:
3229 /* Getting sense data always succeeds (apart from wire
3230 * failures).
3232 if ((sc->quirks & RS_NO_CLEAR_UA)
3233 && csio->cdb_io.cdb_bytes[0] == INQUIRY
3234 && (csio->sense_data.flags & SSD_KEY)
3235 == SSD_KEY_UNIT_ATTENTION) {
3236 /* Ignore unit attention errors in the case where
3237 * the Unit Attention state is not cleared on
3238 * REQUEST SENSE. They will appear again at the next
3239 * command.
3241 ccb->ccb_h.status = CAM_REQ_CMP;
3242 } else if ((csio->sense_data.flags & SSD_KEY)
3243 == SSD_KEY_NO_SENSE) {
3244 /* No problem after all (in the case of CBI without
3245 * CCI)
3247 ccb->ccb_h.status = CAM_REQ_CMP;
3248 } else if ((sc->quirks & RS_NO_CLEAR_UA) &&
3249 (csio->cdb_io.cdb_bytes[0] == READ_CAPACITY) &&
3250 ((csio->sense_data.flags & SSD_KEY)
3251 == SSD_KEY_UNIT_ATTENTION)) {
3253 * Some devices do not clear the unit attention error
3254 * on request sense. We insert a test unit ready
3255 * command to make sure we clear the unit attention
3256 * condition, then allow the retry to proceed as
3257 * usual.
3260 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3261 | CAM_AUTOSNS_VALID;
3262 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3264 #if 0
3265 DELAY(300000);
3266 #endif
3268 DPRINTF(UDMASS_SCSI,("%s: Doing a sneaky"
3269 "TEST_UNIT_READY\n",
3270 device_get_nameunit(sc->sc_dev)));
3272 /* the rest of the command was filled in at attach */
3274 rcmd = (unsigned char *) &sc->cam_scsi_command2;
3275 rcmdlen = sizeof(sc->cam_scsi_command2);
3277 if (sc->transform(sc,
3278 (unsigned char *)
3279 &sc->cam_scsi_test_unit_ready,
3280 sizeof(sc->cam_scsi_test_unit_ready),
3281 &rcmd, &rcmdlen)) {
3282 sc->transfer(sc, ccb->ccb_h.target_lun,
3283 rcmd, rcmdlen,
3284 NULL, 0, DIR_NONE, ccb->ccb_h.timeout,
3285 umass_cam_quirk_cb, (void *) ccb);
3286 } else {
3287 panic("transform(TEST_UNIT_READY) failed");
3289 break;
3290 } else {
3291 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3292 | CAM_AUTOSNS_VALID;
3293 csio->scsi_status = SCSI_STATUS_CHECK_COND;
3295 xpt_done(ccb);
3296 break;
3298 default:
3299 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
3300 device_get_nameunit(sc->sc_dev), status));
3301 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3302 xpt_done(ccb);
3307 * This completion code just handles the fact that we sent a test-unit-ready
3308 * after having previously failed a READ CAPACITY with CHECK_COND. Even
3309 * though this command succeeded, we have to tell CAM to retry.
3311 static void
3312 umass_cam_quirk_cb(struct umass_softc *sc, void *priv, int residue, int status)
3314 union ccb *ccb = (union ccb *) priv;
3316 DPRINTF(UDMASS_SCSI, ("%s: Test unit ready returned status %d\n",
3317 device_get_nameunit(sc->sc_dev), status));
3318 #if 0
3319 ccb->ccb_h.status = CAM_REQ_CMP;
3320 #endif
3321 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3322 | CAM_AUTOSNS_VALID;
3323 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3324 xpt_done(ccb);
3327 static int
3328 umass_driver_load(module_t mod, int what, void *arg)
3330 switch (what) {
3331 case MOD_UNLOAD:
3332 case MOD_LOAD:
3333 default:
3334 return(usbd_driver_load(mod, what, arg));
3339 * SCSI specific functions
3342 static int
3343 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3344 unsigned char **rcmd, int *rcmdlen)
3346 switch (cmd[0]) {
3347 case TEST_UNIT_READY:
3348 if (sc->quirks & NO_TEST_UNIT_READY) {
3349 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3350 ("rcmdlen = %d < %ld, buffer too small",
3351 *rcmdlen,
3352 (long)sizeof(struct scsi_start_stop_unit)));
3353 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3354 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3355 memset(*rcmd, 0, *rcmdlen);
3356 (*rcmd)[0] = START_STOP_UNIT;
3357 (*rcmd)[4] = SSS_START;
3358 return 1;
3360 /* fallthrough */
3361 case INQUIRY:
3362 /* some drives wedge when asked for full inquiry information. */
3363 if (sc->quirks & FORCE_SHORT_INQUIRY) {
3364 memcpy(*rcmd, cmd, cmdlen);
3365 *rcmdlen = cmdlen;
3366 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3367 return 1;
3369 /* fallthrough */
3370 default:
3371 *rcmd = cmd; /* We don't need to copy it */
3372 *rcmdlen = cmdlen;
3375 return 1;
3377 /* RBC specific functions */
3378 static int
3379 umass_rbc_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3380 unsigned char **rcmd, int *rcmdlen)
3382 switch (cmd[0]) {
3383 /* these commands are defined in RBC: */
3384 case READ_10:
3385 case READ_CAPACITY:
3386 case START_STOP_UNIT:
3387 case SYNCHRONIZE_CACHE:
3388 case WRITE_10:
3389 case 0x2f: /* VERIFY_10 is absent from scsi_all.h??? */
3390 case INQUIRY:
3391 case MODE_SELECT_10:
3392 case MODE_SENSE_10:
3393 case TEST_UNIT_READY:
3394 case WRITE_BUFFER:
3395 /* The following commands are not listed in my copy of the RBC specs.
3396 * CAM however seems to want those, and at least the Sony DSC device
3397 * appears to support those as well */
3398 case REQUEST_SENSE:
3399 case PREVENT_ALLOW:
3400 *rcmd = cmd; /* We don't need to copy it */
3401 *rcmdlen = cmdlen;
3402 return 1;
3403 /* All other commands are not legal in RBC */
3404 default:
3405 kprintf("%s: Unsupported RBC command 0x%02x",
3406 device_get_nameunit(sc->sc_dev), cmd[0]);
3407 kprintf("\n");
3408 return 0; /* failure */
3413 * UFI specific functions
3415 static int
3416 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3417 unsigned char **rcmd, int *rcmdlen)
3419 /* A UFI command is always 12 bytes in length */
3420 KASSERT(*rcmdlen >= UFI_COMMAND_LENGTH,
3421 ("rcmdlen = %d < %d, buffer too small",
3422 *rcmdlen, UFI_COMMAND_LENGTH));
3424 *rcmdlen = UFI_COMMAND_LENGTH;
3425 memset(*rcmd, 0, UFI_COMMAND_LENGTH);
3427 switch (cmd[0]) {
3428 /* Commands of which the format has been verified. They should work.
3429 * Copy the command into the (zeroed out) destination buffer.
3431 case TEST_UNIT_READY:
3432 if (sc->quirks & NO_TEST_UNIT_READY) {
3433 /* Some devices do not support this command.
3434 * Start Stop Unit should give the same results
3436 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
3437 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3438 (*rcmd)[0] = START_STOP_UNIT;
3439 (*rcmd)[4] = SSS_START;
3440 } else {
3441 memcpy(*rcmd, cmd, cmdlen);
3443 return 1;
3445 case REZERO_UNIT:
3446 case REQUEST_SENSE:
3447 case INQUIRY:
3448 case START_STOP_UNIT:
3449 case SEND_DIAGNOSTIC:
3450 case PREVENT_ALLOW:
3451 case READ_CAPACITY:
3452 case READ_10:
3453 case WRITE_10:
3454 case POSITION_TO_ELEMENT: /* SEEK_10 */
3455 case MODE_SELECT_10:
3456 case MODE_SENSE_10:
3457 case READ_12:
3458 case WRITE_12:
3459 memcpy(*rcmd, cmd, cmdlen);
3460 return 1;
3462 /* Other UFI commands: FORMAT_UNIT, READ_FORMAT_CAPACITY,
3463 * VERIFY, WRITE_AND_VERIFY.
3464 * These should be checked whether they somehow can be made to fit.
3467 default:
3468 kprintf("%s: Unsupported UFI command 0x%02x\n",
3469 device_get_nameunit(sc->sc_dev), cmd[0]);
3470 return 0; /* failure */
3475 * 8070i (ATAPI) specific functions
3477 static int
3478 umass_atapi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
3479 unsigned char **rcmd, int *rcmdlen)
3481 /* An ATAPI command is always 12 bytes in length. */
3482 KASSERT(*rcmdlen >= ATAPI_COMMAND_LENGTH,
3483 ("rcmdlen = %d < %d, buffer too small",
3484 *rcmdlen, ATAPI_COMMAND_LENGTH));
3486 *rcmdlen = ATAPI_COMMAND_LENGTH;
3487 memset(*rcmd, 0, ATAPI_COMMAND_LENGTH);
3489 switch (cmd[0]) {
3490 /* Commands of which the format has been verified. They should work.
3491 * Copy the command into the (zeroed out) destination buffer.
3493 case INQUIRY:
3494 memcpy(*rcmd, cmd, cmdlen);
3495 /* some drives wedge when asked for full inquiry information. */
3496 if (sc->quirks & FORCE_SHORT_INQUIRY)
3497 (*rcmd)[4] = SHORT_INQUIRY_LENGTH;
3498 return 1;
3500 case TEST_UNIT_READY:
3501 if (sc->quirks & NO_TEST_UNIT_READY) {
3502 KASSERT(*rcmdlen >= sizeof(struct scsi_start_stop_unit),
3503 ("rcmdlen = %d < %ld, buffer too small",
3504 *rcmdlen,
3505 (long)sizeof(struct scsi_start_stop_unit)));
3506 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
3507 "to START_UNIT\n", device_get_nameunit(sc->sc_dev)));
3508 memset(*rcmd, 0, *rcmdlen);
3509 (*rcmd)[0] = START_STOP_UNIT;
3510 (*rcmd)[4] = SSS_START;
3511 return 1;
3513 /* fallthrough */
3514 default:
3516 * All commands are passed through, very likely it will just work
3517 * regardless whether we know these commands or not.
3519 memcpy(*rcmd, cmd, cmdlen);
3520 return 1;
3525 /* (even the comment is missing) */
3527 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0);
3531 #ifdef USB_DEBUG
3532 static void
3533 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3535 int clen = cbw->bCDBLength;
3536 int dlen = UGETDW(cbw->dCBWDataTransferLength);
3537 u_int8_t *c = cbw->CBWCDB;
3538 int tag = UGETDW(cbw->dCBWTag);
3539 int flags = cbw->bCBWFlags;
3541 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
3542 "(0x%02x%02x%02x%02x%02x%02x%s), "
3543 "data = %db, dir = %s\n",
3544 device_get_nameunit(sc->sc_dev), tag, clen,
3545 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
3546 dlen, (flags == CBWFLAGS_IN? "in":
3547 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
3550 static void
3551 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3553 int sig = UGETDW(csw->dCSWSignature);
3554 int tag = UGETW(csw->dCSWTag);
3555 int res = UGETDW(csw->dCSWDataResidue);
3556 int status = csw->bCSWStatus;
3558 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
3559 "res = %d, status = 0x%02x (%s)\n", device_get_nameunit(sc->sc_dev),
3560 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
3561 tag, res,
3562 status, (status == CSWSTATUS_GOOD? "good":
3563 (status == CSWSTATUS_FAILED? "failed":
3564 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
3567 static void
3568 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, int cmdlen)
3570 u_int8_t *c = cmd;
3571 int dir = sc->transfer_dir;
3573 DPRINTF(UDMASS_BBB, ("%s: cmd = %db "
3574 "(0x%02x%02x%02x%02x%02x%02x%s), "
3575 "data = %db, dir = %s\n",
3576 device_get_nameunit(sc->sc_dev), cmdlen,
3577 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6? "...":""),
3578 sc->transfer_datalen,
3579 (dir == DIR_IN? "in":
3580 (dir == DIR_OUT? "out":
3581 (dir == DIR_NONE? "no data phase": "<invalid>")))));
3584 static void
3585 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
3586 int printlen)
3588 int i, j;
3589 char s1[40];
3590 char s2[40];
3591 char s3[5];
3593 s1[0] = '\0';
3594 s3[0] = '\0';
3596 ksprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3597 for (i = 0; i < buflen && i < printlen; i++) {
3598 j = i % 16;
3599 if (j == 0 && i != 0) {
3600 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3601 device_get_nameunit(sc->sc_dev), s1, s2));
3602 s2[0] = '\0';
3604 ksprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3606 if (buflen > printlen)
3607 ksprintf(s3, " ...");
3608 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3609 device_get_nameunit(sc->sc_dev), s1, s2, s3));
3611 #endif