Deleted Added
full compact
umass.c (190189) umass.c (190263)
1#include <sys/cdefs.h>
1#include <sys/cdefs.h>
2__FBSDID("$FreeBSD: head/sys/dev/usb/storage/umass.c 190189 2009-03-20 22:17:27Z thompsa $");
2__FBSDID("$FreeBSD: head/sys/dev/usb/storage/umass.c 190263 2009-03-22 13:17:46Z imp $");
3
4/*-
5 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
6 * Nick Hibma <n_hibma@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
3
4/*-
5 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
6 * Nick Hibma <n_hibma@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: head/sys/dev/usb/storage/umass.c 190189 2009-03-20 22:17:27Z thompsa $
30 * $FreeBSD: head/sys/dev/usb/storage/umass.c 190263 2009-03-22 13:17:46Z imp $
31 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
32 */
33
34/* Also already merged from NetBSD:
35 * $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
36 * $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
37 * $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
38 * $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
39 */
40
41/*
42 * Universal Serial Bus Mass Storage Class specs:
43 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
44 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
45 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
46 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
47 */
48
49/*
50 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
51 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
52 */
53
54/*
55 * The driver handles 3 Wire Protocols
56 * - Command/Bulk/Interrupt (CBI)
57 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
58 * - Mass Storage Bulk-Only (BBB)
59 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
60 *
61 * Over these wire protocols it handles the following command protocols
62 * - SCSI
63 * - UFI (floppy command set)
64 * - 8070i (ATAPI)
65 *
66 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
67 * sc->sc_transform method is used to convert the commands into the appropriate
68 * format (if at all necessary). For example, UFI requires all commands to be
69 * 12 bytes in length amongst other things.
70 *
71 * The source code below is marked and can be split into a number of pieces
72 * (in this order):
73 *
74 * - probe/attach/detach
75 * - generic transfer routines
76 * - BBB
77 * - CBI
78 * - CBI_I (in addition to functions from CBI)
79 * - CAM (Common Access Method)
80 * - SCSI
81 * - UFI
82 * - 8070i (ATAPI)
83 *
84 * The protocols are implemented using a state machine, for the transfers as
85 * well as for the resets. The state machine is contained in umass_t_*_callback.
86 * The state machine is started through either umass_command_start() or
87 * umass_reset().
88 *
89 * The reason for doing this is a) CAM performs a lot better this way and b) it
90 * avoids using tsleep from interrupt context (for example after a failed
91 * transfer).
92 */
93
94/*
95 * The SCSI related part of this driver has been derived from the
96 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org).
97 *
98 * The CAM layer uses so called actions which are messages sent to the host
99 * adapter for completion. The actions come in through umass_cam_action. The
100 * appropriate block of routines is called depending on the transport protocol
101 * in use. When the transfer has finished, these routines call
102 * umass_cam_cb again to complete the CAM command.
103 */
104
105#include "usbdevs.h"
106#include <dev/usb/usb.h>
107#include <dev/usb/usb_mfunc.h>
108#include <dev/usb/usb_error.h>
109
110#include <dev/usb/usb_core.h>
111#include <dev/usb/usb_util.h>
112#include <dev/usb/usb_busdma.h>
113#include <dev/usb/usb_request.h>
114#include <dev/usb/usb_debug.h>
115#include <dev/usb/usb_process.h>
116#include <dev/usb/usb_transfer.h>
117
118#include <cam/cam.h>
119#include <cam/cam_ccb.h>
120#include <cam/cam_sim.h>
121#include <cam/cam_xpt_sim.h>
122#include <cam/scsi/scsi_all.h>
123#include <cam/scsi/scsi_da.h>
124
125#include <cam/cam_periph.h>
126
127#if 1
128/* this enables loading of virtual buffers into DMA */
129#define UMASS_USB_FLAGS .ext_buffer=1,
130#else
131#define UMASS_USB_FLAGS
132#endif
133
134#if USB_DEBUG
135#define DIF(m, x) \
136 do { \
137 if (umass_debug & (m)) { x ; } \
138 } while (0)
139
140#define DPRINTF(sc, m, fmt, ...) \
141 do { \
142 if (umass_debug & (m)) { \
143 printf("%s:%s: " fmt, \
144 (sc) ? (const char *)(sc)->sc_name : \
145 (const char *)"umassX", \
146 __FUNCTION__ ,## __VA_ARGS__); \
147 } \
148 } while (0)
149
150#define UDMASS_GEN 0x00010000 /* general */
151#define UDMASS_SCSI 0x00020000 /* scsi */
152#define UDMASS_UFI 0x00040000 /* ufi command set */
153#define UDMASS_ATAPI 0x00080000 /* 8070i command set */
154#define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
155#define UDMASS_USB 0x00100000 /* USB general */
156#define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
157#define UDMASS_CBI 0x00400000 /* CBI transfers */
158#define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
159#define UDMASS_ALL 0xffff0000 /* all of the above */
160static int umass_debug = 0;
161
162SYSCTL_NODE(_hw_usb2, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
163SYSCTL_INT(_hw_usb2_umass, OID_AUTO, debug, CTLFLAG_RW,
164 &umass_debug, 0, "umass debug level");
165#else
166#define DIF(...) do { } while (0)
167#define DPRINTF(...) do { } while (0)
168#endif
169
170#define UMASS_GONE ((struct umass_softc *)1)
171
172#define UMASS_BULK_SIZE (1 << 17)
173#define UMASS_CBI_DIAGNOSTIC_CMDLEN 12 /* bytes */
174#define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN) /* bytes */
175
176/* USB transfer definitions */
177
178#define UMASS_T_BBB_RESET1 0 /* Bulk-Only */
179#define UMASS_T_BBB_RESET2 1
180#define UMASS_T_BBB_RESET3 2
181#define UMASS_T_BBB_COMMAND 3
182#define UMASS_T_BBB_DATA_READ 4
183#define UMASS_T_BBB_DATA_RD_CS 5
184#define UMASS_T_BBB_DATA_WRITE 6
185#define UMASS_T_BBB_DATA_WR_CS 7
186#define UMASS_T_BBB_STATUS 8
187#define UMASS_T_BBB_MAX 9
188
189#define UMASS_T_CBI_RESET1 0 /* CBI */
190#define UMASS_T_CBI_RESET2 1
191#define UMASS_T_CBI_RESET3 2
192#define UMASS_T_CBI_COMMAND 3
193#define UMASS_T_CBI_DATA_READ 4
194#define UMASS_T_CBI_DATA_RD_CS 5
195#define UMASS_T_CBI_DATA_WRITE 6
196#define UMASS_T_CBI_DATA_WR_CS 7
197#define UMASS_T_CBI_STATUS 8
198#define UMASS_T_CBI_RESET4 9
199#define UMASS_T_CBI_MAX 10
200
201#define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
202
203/* Generic definitions */
204
205/* Direction for transfer */
206#define DIR_NONE 0
207#define DIR_IN 1
208#define DIR_OUT 2
209
210/* device name */
211#define DEVNAME "umass"
212#define DEVNAME_SIM "umass-sim"
213
214/* Approximate maximum transfer speeds (assumes 33% overhead). */
215#define UMASS_FULL_TRANSFER_SPEED 1000
216#define UMASS_HIGH_TRANSFER_SPEED 40000
217#define UMASS_FLOPPY_TRANSFER_SPEED 20
218
219#define UMASS_TIMEOUT 5000 /* ms */
220
221/* CAM specific definitions */
222
223#define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
224#define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
225
226/* Bulk-Only features */
227
228#define UR_BBB_RESET 0xff /* Bulk-Only reset */
229#define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
230
231/* Command Block Wrapper */
232typedef struct {
233 uDWord dCBWSignature;
234#define CBWSIGNATURE 0x43425355
235 uDWord dCBWTag;
236 uDWord dCBWDataTransferLength;
237 uByte bCBWFlags;
238#define CBWFLAGS_OUT 0x00
239#define CBWFLAGS_IN 0x80
240 uByte bCBWLUN;
241 uByte bCDBLength;
242#define CBWCDBLENGTH 16
243 uByte CBWCDB[CBWCDBLENGTH];
244} __packed umass_bbb_cbw_t;
245
246#define UMASS_BBB_CBW_SIZE 31
247
248/* Command Status Wrapper */
249typedef struct {
250 uDWord dCSWSignature;
251#define CSWSIGNATURE 0x53425355
252#define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355
253#define CSWSIGNATURE_OLYMPUS_C1 0x55425355
254 uDWord dCSWTag;
255 uDWord dCSWDataResidue;
256 uByte bCSWStatus;
257#define CSWSTATUS_GOOD 0x0
258#define CSWSTATUS_FAILED 0x1
259#define CSWSTATUS_PHASE 0x2
260} __packed umass_bbb_csw_t;
261
262#define UMASS_BBB_CSW_SIZE 13
263
264/* CBI features */
265
266#define UR_CBI_ADSC 0x00
267
268typedef union {
269 struct {
270 uint8_t type;
271#define IDB_TYPE_CCI 0x00
272 uint8_t value;
273#define IDB_VALUE_PASS 0x00
274#define IDB_VALUE_FAIL 0x01
275#define IDB_VALUE_PHASE 0x02
276#define IDB_VALUE_PERSISTENT 0x03
277#define IDB_VALUE_STATUS_MASK 0x03
278 } __packed common;
279
280 struct {
281 uint8_t asc;
282 uint8_t ascq;
283 } __packed ufi;
284} __packed umass_cbi_sbl_t;
285
286struct umass_softc; /* see below */
287
288typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
289 uint32_t residue, uint8_t status);
290
291#define STATUS_CMD_OK 0 /* everything ok */
292#define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
293#define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
294#define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
295
296typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
297 uint8_t cmd_len);
298
299struct umass_devdescr {
300 uint32_t vid;
301#define VID_WILDCARD 0xffffffff
302#define VID_EOT 0xfffffffe
303 uint32_t pid;
304#define PID_WILDCARD 0xffffffff
305#define PID_EOT 0xfffffffe
306 uint32_t rid;
307#define RID_WILDCARD 0xffffffff
308#define RID_EOT 0xfffffffe
309
310 /* wire and command protocol */
311 uint16_t proto;
312#define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
313#define UMASS_PROTO_CBI 0x0002
314#define UMASS_PROTO_CBI_I 0x0004
315#define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
316#define UMASS_PROTO_SCSI 0x0100 /* command protocol */
317#define UMASS_PROTO_ATAPI 0x0200
318#define UMASS_PROTO_UFI 0x0400
319#define UMASS_PROTO_RBC 0x0800
320#define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
321
322 /* Device specific quirks */
323 uint16_t quirks;
324#define NO_QUIRKS 0x0000
325 /*
326 * The drive does not support Test Unit Ready. Convert to Start Unit
327 */
328#define NO_TEST_UNIT_READY 0x0001
329 /*
330 * The drive does not reset the Unit Attention state after REQUEST
331 * SENSE has been sent. The INQUIRY command does not reset the UA
332 * either, and so CAM runs in circles trying to retrieve the initial
333 * INQUIRY data.
334 */
335#define RS_NO_CLEAR_UA 0x0002
336 /* The drive does not support START STOP. */
337#define NO_START_STOP 0x0004
338 /* Don't ask for full inquiry data (255b). */
339#define FORCE_SHORT_INQUIRY 0x0008
340 /* Needs to be initialised the Shuttle way */
341#define SHUTTLE_INIT 0x0010
342 /* Drive needs to be switched to alternate iface 1 */
343#define ALT_IFACE_1 0x0020
344 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
345#define FLOPPY_SPEED 0x0040
346 /* The device can't count and gets the residue of transfers wrong */
347#define IGNORE_RESIDUE 0x0080
348 /* No GetMaxLun call */
349#define NO_GETMAXLUN 0x0100
350 /* The device uses a weird CSWSIGNATURE. */
351#define WRONG_CSWSIG 0x0200
352 /* Device cannot handle INQUIRY so fake a generic response */
353#define NO_INQUIRY 0x0400
354 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
355#define NO_INQUIRY_EVPD 0x0800
356 /* Pad all RBC requests to 12 bytes. */
357#define RBC_PAD_TO_12 0x1000
358 /*
359 * Device reports number of sectors from READ_CAPACITY, not max
360 * sector number.
361 */
362#define READ_CAPACITY_OFFBY1 0x2000
363 /*
364 * Device cannot handle a SCSI synchronize cache command. Normally
365 * this quirk would be handled in the cam layer, but for IDE bridges
366 * we need to associate the quirk with the bridge and not the
367 * underlying disk device. This is handled by faking a success
368 * result.
369 */
370#define NO_SYNCHRONIZE_CACHE 0x4000
371};
372
373static const struct umass_devdescr umass_devdescr[] = {
374 {USB_VENDOR_ASAHIOPTICAL, PID_WILDCARD, RID_WILDCARD,
375 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
376 RS_NO_CLEAR_UA
377 },
378 {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_ATTACHE, RID_WILDCARD,
379 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
380 IGNORE_RESIDUE
381 },
382 {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_A256MB, RID_WILDCARD,
383 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
384 IGNORE_RESIDUE
385 },
386 {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_DISKPRO512, RID_WILDCARD,
387 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
388 IGNORE_RESIDUE
389 },
390 {USB_VENDOR_ADDONICS2, USB_PRODUCT_ADDONICS2_CABLE_205, RID_WILDCARD,
391 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
392 NO_QUIRKS
393 },
394 {USB_VENDOR_AIPTEK, USB_PRODUCT_AIPTEK_POCKETCAM3M, RID_WILDCARD,
395 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
396 NO_QUIRKS
397 },
398 {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_UMCR_9361, RID_WILDCARD,
399 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
400 NO_GETMAXLUN
401 },
402 {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_TRANSCEND, RID_WILDCARD,
403 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
404 NO_GETMAXLUN
405 },
406 {USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO230, RID_WILDCARD,
407 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
408 NO_INQUIRY
409 },
410 {USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO330, RID_WILDCARD,
411 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
412 NO_INQUIRY
413 },
414 {USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2SCSI, RID_WILDCARD,
415 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
416 NO_QUIRKS
417 },
418 {USB_VENDOR_CASIO, USB_PRODUCT_CASIO_QV_DIGICAM, RID_WILDCARD,
419 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
420 NO_INQUIRY
421 },
422 {USB_VENDOR_CCYU, USB_PRODUCT_CCYU_ED1064, RID_WILDCARD,
423 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
424 NO_QUIRKS
425 },
426 {USB_VENDOR_CENTURY, USB_PRODUCT_CENTURY_EX35QUAT, RID_WILDCARD,
427 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
428 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
429 },
430 {USB_VENDOR_DESKNOTE, USB_PRODUCT_DESKNOTE_UCR_61S2B, RID_WILDCARD,
431 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
432 NO_QUIRKS
433 },
434 {USB_VENDOR_DMI, USB_PRODUCT_DMI_CFSM_RW, RID_WILDCARD,
435 UMASS_PROTO_SCSI,
436 NO_GETMAXLUN
437 },
438 {USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_875DC, RID_WILDCARD,
439 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
440 NO_INQUIRY
441 },
442 {USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_895, RID_WILDCARD,
443 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
444 NO_GETMAXLUN
445 },
446 {USB_VENDOR_FEIYA, USB_PRODUCT_FEIYA_5IN1, RID_WILDCARD,
447 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
448 NO_QUIRKS
449 },
450 {USB_VENDOR_FREECOM, USB_PRODUCT_FREECOM_DVD, RID_WILDCARD,
451 UMASS_PROTO_SCSI,
452 NO_QUIRKS
453 },
454 {USB_VENDOR_FUJIPHOTO, USB_PRODUCT_FUJIPHOTO_MASS0100, RID_WILDCARD,
455 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
456 RS_NO_CLEAR_UA
457 },
458 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE, RID_WILDCARD,
459 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
460 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
461 | NO_SYNCHRONIZE_CACHE
462 },
463 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE_2, RID_WILDCARD,
464 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
465 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
466 },
467 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB, RID_WILDCARD,
468 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
469 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
470 },
471 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB_2, RID_WILDCARD,
472 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
473 WRONG_CSWSIG
474 },
475 {USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FG, RID_WILDCARD,
476 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
477 NO_QUIRKS
478 },
479 {USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FGSM, RID_WILDCARD,
480 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
481 NO_QUIRKS
482 },
483 {USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_DZ_MV100A, RID_WILDCARD,
484 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
485 NO_GETMAXLUN
486 },
487 {USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_USB, RID_WILDCARD,
488 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
489 NO_INQUIRY
490 },
491 {USB_VENDOR_HP, USB_PRODUCT_HP_CDW4E, RID_WILDCARD,
492 UMASS_PROTO_ATAPI,
493 NO_QUIRKS
494 },
495 {USB_VENDOR_HP, USB_PRODUCT_HP_CDW8200, RID_WILDCARD,
496 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
497 NO_TEST_UNIT_READY | NO_START_STOP
498 },
499 {USB_VENDOR_IMAGINATION, USB_PRODUCT_IMAGINATION_DBX1, RID_WILDCARD,
500 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
501 WRONG_CSWSIG
502 },
503 {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_USBCABLE, RID_WILDCARD,
504 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
505 NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
506 },
507 {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_ATAPI, RID_WILDCARD,
508 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
509 NO_QUIRKS
510 },
511 {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_STORAGE_V2, RID_WILDCARD,
512 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
513 NO_QUIRKS
514 },
515 {USB_VENDOR_IODATA, USB_PRODUCT_IODATA_IU_CD2, RID_WILDCARD,
516 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
517 NO_QUIRKS
518 },
519 {USB_VENDOR_IODATA, USB_PRODUCT_IODATA_DVR_UEH8, RID_WILDCARD,
520 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
521 NO_QUIRKS
522 },
523 {USB_VENDOR_IOMEGA, USB_PRODUCT_IOMEGA_ZIP100, RID_WILDCARD,
524 /*
525 * XXX This is not correct as there are Zip drives that use
526 * ATAPI.
527 */
528 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
529 NO_TEST_UNIT_READY
530 },
531 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_L3, RID_WILDCARD,
532 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
533 NO_INQUIRY
534 },
535 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S3X, RID_WILDCARD,
536 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
537 NO_INQUIRY
538 },
539 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S4, RID_WILDCARD,
540 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
541 NO_INQUIRY
542 },
543 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S5, RID_WILDCARD,
544 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
545 NO_INQUIRY
546 },
547 {USB_VENDOR_LACIE, USB_PRODUCT_LACIE_HD, RID_WILDCARD,
548 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
549 NO_QUIRKS
550 },
551 {USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_CF_READER, RID_WILDCARD,
552 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
553 NO_INQUIRY
554 },
555 {USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_JUMPSHOT, RID_WILDCARD,
556 UMASS_PROTO_SCSI,
557 NO_QUIRKS
558 },
559 {USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443SU2, RID_WILDCARD,
560 UMASS_PROTO_SCSI,
561 NO_QUIRKS
562 },
563 {USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443U2, RID_WILDCARD,
564 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
565 NO_QUIRKS
566 },
567 {USB_VENDOR_MELCO, USB_PRODUCT_MELCO_DUBPXXG, RID_WILDCARD,
568 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
569 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
570 },
571 {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_DPCM, RID_WILDCARD,
572 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
573 NO_TEST_UNIT_READY | NO_START_STOP
574 },
575 {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIDB25, RID_WILDCARD,
576 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
577 NO_QUIRKS
578 },
579 {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIHD50, RID_WILDCARD,
580 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
581 NO_QUIRKS
582 },
583 {USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_E223, RID_WILDCARD,
584 UMASS_PROTO_SCSI,
585 NO_QUIRKS
586 },
587 {USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_F300, RID_WILDCARD,
588 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
589 NO_QUIRKS
590 },
591 {USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_CDRRW, RID_WILDCARD,
592 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
593 NO_QUIRKS
594 },
595 {USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_FDD, RID_WILDCARD,
596 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
597 NO_GETMAXLUN
598 },
599 {USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_E398, RID_WILDCARD,
600 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
601 FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
602 },
603 {USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY, RID_WILDCARD,
604 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
605 IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
606 },
607 {USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY2, RID_WILDCARD,
608 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
609 NO_QUIRKS
610 },
611 {USB_VENDOR_MYSON, USB_PRODUCT_MYSON_HEDEN, RID_WILDCARD,
612 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
31 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
32 */
33
34/* Also already merged from NetBSD:
35 * $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
36 * $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
37 * $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
38 * $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
39 */
40
41/*
42 * Universal Serial Bus Mass Storage Class specs:
43 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
44 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
45 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
46 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
47 */
48
49/*
50 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
51 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
52 */
53
54/*
55 * The driver handles 3 Wire Protocols
56 * - Command/Bulk/Interrupt (CBI)
57 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
58 * - Mass Storage Bulk-Only (BBB)
59 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
60 *
61 * Over these wire protocols it handles the following command protocols
62 * - SCSI
63 * - UFI (floppy command set)
64 * - 8070i (ATAPI)
65 *
66 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
67 * sc->sc_transform method is used to convert the commands into the appropriate
68 * format (if at all necessary). For example, UFI requires all commands to be
69 * 12 bytes in length amongst other things.
70 *
71 * The source code below is marked and can be split into a number of pieces
72 * (in this order):
73 *
74 * - probe/attach/detach
75 * - generic transfer routines
76 * - BBB
77 * - CBI
78 * - CBI_I (in addition to functions from CBI)
79 * - CAM (Common Access Method)
80 * - SCSI
81 * - UFI
82 * - 8070i (ATAPI)
83 *
84 * The protocols are implemented using a state machine, for the transfers as
85 * well as for the resets. The state machine is contained in umass_t_*_callback.
86 * The state machine is started through either umass_command_start() or
87 * umass_reset().
88 *
89 * The reason for doing this is a) CAM performs a lot better this way and b) it
90 * avoids using tsleep from interrupt context (for example after a failed
91 * transfer).
92 */
93
94/*
95 * The SCSI related part of this driver has been derived from the
96 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org).
97 *
98 * The CAM layer uses so called actions which are messages sent to the host
99 * adapter for completion. The actions come in through umass_cam_action. The
100 * appropriate block of routines is called depending on the transport protocol
101 * in use. When the transfer has finished, these routines call
102 * umass_cam_cb again to complete the CAM command.
103 */
104
105#include "usbdevs.h"
106#include <dev/usb/usb.h>
107#include <dev/usb/usb_mfunc.h>
108#include <dev/usb/usb_error.h>
109
110#include <dev/usb/usb_core.h>
111#include <dev/usb/usb_util.h>
112#include <dev/usb/usb_busdma.h>
113#include <dev/usb/usb_request.h>
114#include <dev/usb/usb_debug.h>
115#include <dev/usb/usb_process.h>
116#include <dev/usb/usb_transfer.h>
117
118#include <cam/cam.h>
119#include <cam/cam_ccb.h>
120#include <cam/cam_sim.h>
121#include <cam/cam_xpt_sim.h>
122#include <cam/scsi/scsi_all.h>
123#include <cam/scsi/scsi_da.h>
124
125#include <cam/cam_periph.h>
126
127#if 1
128/* this enables loading of virtual buffers into DMA */
129#define UMASS_USB_FLAGS .ext_buffer=1,
130#else
131#define UMASS_USB_FLAGS
132#endif
133
134#if USB_DEBUG
135#define DIF(m, x) \
136 do { \
137 if (umass_debug & (m)) { x ; } \
138 } while (0)
139
140#define DPRINTF(sc, m, fmt, ...) \
141 do { \
142 if (umass_debug & (m)) { \
143 printf("%s:%s: " fmt, \
144 (sc) ? (const char *)(sc)->sc_name : \
145 (const char *)"umassX", \
146 __FUNCTION__ ,## __VA_ARGS__); \
147 } \
148 } while (0)
149
150#define UDMASS_GEN 0x00010000 /* general */
151#define UDMASS_SCSI 0x00020000 /* scsi */
152#define UDMASS_UFI 0x00040000 /* ufi command set */
153#define UDMASS_ATAPI 0x00080000 /* 8070i command set */
154#define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
155#define UDMASS_USB 0x00100000 /* USB general */
156#define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
157#define UDMASS_CBI 0x00400000 /* CBI transfers */
158#define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
159#define UDMASS_ALL 0xffff0000 /* all of the above */
160static int umass_debug = 0;
161
162SYSCTL_NODE(_hw_usb2, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
163SYSCTL_INT(_hw_usb2_umass, OID_AUTO, debug, CTLFLAG_RW,
164 &umass_debug, 0, "umass debug level");
165#else
166#define DIF(...) do { } while (0)
167#define DPRINTF(...) do { } while (0)
168#endif
169
170#define UMASS_GONE ((struct umass_softc *)1)
171
172#define UMASS_BULK_SIZE (1 << 17)
173#define UMASS_CBI_DIAGNOSTIC_CMDLEN 12 /* bytes */
174#define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN) /* bytes */
175
176/* USB transfer definitions */
177
178#define UMASS_T_BBB_RESET1 0 /* Bulk-Only */
179#define UMASS_T_BBB_RESET2 1
180#define UMASS_T_BBB_RESET3 2
181#define UMASS_T_BBB_COMMAND 3
182#define UMASS_T_BBB_DATA_READ 4
183#define UMASS_T_BBB_DATA_RD_CS 5
184#define UMASS_T_BBB_DATA_WRITE 6
185#define UMASS_T_BBB_DATA_WR_CS 7
186#define UMASS_T_BBB_STATUS 8
187#define UMASS_T_BBB_MAX 9
188
189#define UMASS_T_CBI_RESET1 0 /* CBI */
190#define UMASS_T_CBI_RESET2 1
191#define UMASS_T_CBI_RESET3 2
192#define UMASS_T_CBI_COMMAND 3
193#define UMASS_T_CBI_DATA_READ 4
194#define UMASS_T_CBI_DATA_RD_CS 5
195#define UMASS_T_CBI_DATA_WRITE 6
196#define UMASS_T_CBI_DATA_WR_CS 7
197#define UMASS_T_CBI_STATUS 8
198#define UMASS_T_CBI_RESET4 9
199#define UMASS_T_CBI_MAX 10
200
201#define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
202
203/* Generic definitions */
204
205/* Direction for transfer */
206#define DIR_NONE 0
207#define DIR_IN 1
208#define DIR_OUT 2
209
210/* device name */
211#define DEVNAME "umass"
212#define DEVNAME_SIM "umass-sim"
213
214/* Approximate maximum transfer speeds (assumes 33% overhead). */
215#define UMASS_FULL_TRANSFER_SPEED 1000
216#define UMASS_HIGH_TRANSFER_SPEED 40000
217#define UMASS_FLOPPY_TRANSFER_SPEED 20
218
219#define UMASS_TIMEOUT 5000 /* ms */
220
221/* CAM specific definitions */
222
223#define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
224#define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
225
226/* Bulk-Only features */
227
228#define UR_BBB_RESET 0xff /* Bulk-Only reset */
229#define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
230
231/* Command Block Wrapper */
232typedef struct {
233 uDWord dCBWSignature;
234#define CBWSIGNATURE 0x43425355
235 uDWord dCBWTag;
236 uDWord dCBWDataTransferLength;
237 uByte bCBWFlags;
238#define CBWFLAGS_OUT 0x00
239#define CBWFLAGS_IN 0x80
240 uByte bCBWLUN;
241 uByte bCDBLength;
242#define CBWCDBLENGTH 16
243 uByte CBWCDB[CBWCDBLENGTH];
244} __packed umass_bbb_cbw_t;
245
246#define UMASS_BBB_CBW_SIZE 31
247
248/* Command Status Wrapper */
249typedef struct {
250 uDWord dCSWSignature;
251#define CSWSIGNATURE 0x53425355
252#define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355
253#define CSWSIGNATURE_OLYMPUS_C1 0x55425355
254 uDWord dCSWTag;
255 uDWord dCSWDataResidue;
256 uByte bCSWStatus;
257#define CSWSTATUS_GOOD 0x0
258#define CSWSTATUS_FAILED 0x1
259#define CSWSTATUS_PHASE 0x2
260} __packed umass_bbb_csw_t;
261
262#define UMASS_BBB_CSW_SIZE 13
263
264/* CBI features */
265
266#define UR_CBI_ADSC 0x00
267
268typedef union {
269 struct {
270 uint8_t type;
271#define IDB_TYPE_CCI 0x00
272 uint8_t value;
273#define IDB_VALUE_PASS 0x00
274#define IDB_VALUE_FAIL 0x01
275#define IDB_VALUE_PHASE 0x02
276#define IDB_VALUE_PERSISTENT 0x03
277#define IDB_VALUE_STATUS_MASK 0x03
278 } __packed common;
279
280 struct {
281 uint8_t asc;
282 uint8_t ascq;
283 } __packed ufi;
284} __packed umass_cbi_sbl_t;
285
286struct umass_softc; /* see below */
287
288typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
289 uint32_t residue, uint8_t status);
290
291#define STATUS_CMD_OK 0 /* everything ok */
292#define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
293#define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
294#define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
295
296typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
297 uint8_t cmd_len);
298
299struct umass_devdescr {
300 uint32_t vid;
301#define VID_WILDCARD 0xffffffff
302#define VID_EOT 0xfffffffe
303 uint32_t pid;
304#define PID_WILDCARD 0xffffffff
305#define PID_EOT 0xfffffffe
306 uint32_t rid;
307#define RID_WILDCARD 0xffffffff
308#define RID_EOT 0xfffffffe
309
310 /* wire and command protocol */
311 uint16_t proto;
312#define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
313#define UMASS_PROTO_CBI 0x0002
314#define UMASS_PROTO_CBI_I 0x0004
315#define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
316#define UMASS_PROTO_SCSI 0x0100 /* command protocol */
317#define UMASS_PROTO_ATAPI 0x0200
318#define UMASS_PROTO_UFI 0x0400
319#define UMASS_PROTO_RBC 0x0800
320#define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
321
322 /* Device specific quirks */
323 uint16_t quirks;
324#define NO_QUIRKS 0x0000
325 /*
326 * The drive does not support Test Unit Ready. Convert to Start Unit
327 */
328#define NO_TEST_UNIT_READY 0x0001
329 /*
330 * The drive does not reset the Unit Attention state after REQUEST
331 * SENSE has been sent. The INQUIRY command does not reset the UA
332 * either, and so CAM runs in circles trying to retrieve the initial
333 * INQUIRY data.
334 */
335#define RS_NO_CLEAR_UA 0x0002
336 /* The drive does not support START STOP. */
337#define NO_START_STOP 0x0004
338 /* Don't ask for full inquiry data (255b). */
339#define FORCE_SHORT_INQUIRY 0x0008
340 /* Needs to be initialised the Shuttle way */
341#define SHUTTLE_INIT 0x0010
342 /* Drive needs to be switched to alternate iface 1 */
343#define ALT_IFACE_1 0x0020
344 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
345#define FLOPPY_SPEED 0x0040
346 /* The device can't count and gets the residue of transfers wrong */
347#define IGNORE_RESIDUE 0x0080
348 /* No GetMaxLun call */
349#define NO_GETMAXLUN 0x0100
350 /* The device uses a weird CSWSIGNATURE. */
351#define WRONG_CSWSIG 0x0200
352 /* Device cannot handle INQUIRY so fake a generic response */
353#define NO_INQUIRY 0x0400
354 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
355#define NO_INQUIRY_EVPD 0x0800
356 /* Pad all RBC requests to 12 bytes. */
357#define RBC_PAD_TO_12 0x1000
358 /*
359 * Device reports number of sectors from READ_CAPACITY, not max
360 * sector number.
361 */
362#define READ_CAPACITY_OFFBY1 0x2000
363 /*
364 * Device cannot handle a SCSI synchronize cache command. Normally
365 * this quirk would be handled in the cam layer, but for IDE bridges
366 * we need to associate the quirk with the bridge and not the
367 * underlying disk device. This is handled by faking a success
368 * result.
369 */
370#define NO_SYNCHRONIZE_CACHE 0x4000
371};
372
373static const struct umass_devdescr umass_devdescr[] = {
374 {USB_VENDOR_ASAHIOPTICAL, PID_WILDCARD, RID_WILDCARD,
375 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
376 RS_NO_CLEAR_UA
377 },
378 {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_ATTACHE, RID_WILDCARD,
379 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
380 IGNORE_RESIDUE
381 },
382 {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_A256MB, RID_WILDCARD,
383 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
384 IGNORE_RESIDUE
385 },
386 {USB_VENDOR_ADDON, USB_PRODUCT_ADDON_DISKPRO512, RID_WILDCARD,
387 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
388 IGNORE_RESIDUE
389 },
390 {USB_VENDOR_ADDONICS2, USB_PRODUCT_ADDONICS2_CABLE_205, RID_WILDCARD,
391 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
392 NO_QUIRKS
393 },
394 {USB_VENDOR_AIPTEK, USB_PRODUCT_AIPTEK_POCKETCAM3M, RID_WILDCARD,
395 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
396 NO_QUIRKS
397 },
398 {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_UMCR_9361, RID_WILDCARD,
399 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
400 NO_GETMAXLUN
401 },
402 {USB_VENDOR_ALCOR, USB_PRODUCT_ALCOR_TRANSCEND, RID_WILDCARD,
403 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
404 NO_GETMAXLUN
405 },
406 {USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO230, RID_WILDCARD,
407 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
408 NO_INQUIRY
409 },
410 {USB_VENDOR_ASAHIOPTICAL, USB_PRODUCT_ASAHIOPTICAL_OPTIO330, RID_WILDCARD,
411 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
412 NO_INQUIRY
413 },
414 {USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2SCSI, RID_WILDCARD,
415 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
416 NO_QUIRKS
417 },
418 {USB_VENDOR_CASIO, USB_PRODUCT_CASIO_QV_DIGICAM, RID_WILDCARD,
419 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
420 NO_INQUIRY
421 },
422 {USB_VENDOR_CCYU, USB_PRODUCT_CCYU_ED1064, RID_WILDCARD,
423 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
424 NO_QUIRKS
425 },
426 {USB_VENDOR_CENTURY, USB_PRODUCT_CENTURY_EX35QUAT, RID_WILDCARD,
427 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
428 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
429 },
430 {USB_VENDOR_DESKNOTE, USB_PRODUCT_DESKNOTE_UCR_61S2B, RID_WILDCARD,
431 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
432 NO_QUIRKS
433 },
434 {USB_VENDOR_DMI, USB_PRODUCT_DMI_CFSM_RW, RID_WILDCARD,
435 UMASS_PROTO_SCSI,
436 NO_GETMAXLUN
437 },
438 {USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_875DC, RID_WILDCARD,
439 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
440 NO_INQUIRY
441 },
442 {USB_VENDOR_EPSON, USB_PRODUCT_EPSON_STYLUS_895, RID_WILDCARD,
443 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
444 NO_GETMAXLUN
445 },
446 {USB_VENDOR_FEIYA, USB_PRODUCT_FEIYA_5IN1, RID_WILDCARD,
447 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
448 NO_QUIRKS
449 },
450 {USB_VENDOR_FREECOM, USB_PRODUCT_FREECOM_DVD, RID_WILDCARD,
451 UMASS_PROTO_SCSI,
452 NO_QUIRKS
453 },
454 {USB_VENDOR_FUJIPHOTO, USB_PRODUCT_FUJIPHOTO_MASS0100, RID_WILDCARD,
455 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
456 RS_NO_CLEAR_UA
457 },
458 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE, RID_WILDCARD,
459 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
460 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
461 | NO_SYNCHRONIZE_CACHE
462 },
463 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB2IDE_2, RID_WILDCARD,
464 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
465 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
466 },
467 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB, RID_WILDCARD,
468 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
469 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
470 },
471 {USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL641USB_2, RID_WILDCARD,
472 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
473 WRONG_CSWSIG
474 },
475 {USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FG, RID_WILDCARD,
476 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
477 NO_QUIRKS
478 },
479 {USB_VENDOR_HAGIWARA, USB_PRODUCT_HAGIWARA_FGSM, RID_WILDCARD,
480 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
481 NO_QUIRKS
482 },
483 {USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_DZ_MV100A, RID_WILDCARD,
484 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
485 NO_GETMAXLUN
486 },
487 {USB_VENDOR_HITACHI, USB_PRODUCT_HITACHI_DVDCAM_USB, RID_WILDCARD,
488 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
489 NO_INQUIRY
490 },
491 {USB_VENDOR_HP, USB_PRODUCT_HP_CDW4E, RID_WILDCARD,
492 UMASS_PROTO_ATAPI,
493 NO_QUIRKS
494 },
495 {USB_VENDOR_HP, USB_PRODUCT_HP_CDW8200, RID_WILDCARD,
496 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
497 NO_TEST_UNIT_READY | NO_START_STOP
498 },
499 {USB_VENDOR_IMAGINATION, USB_PRODUCT_IMAGINATION_DBX1, RID_WILDCARD,
500 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
501 WRONG_CSWSIG
502 },
503 {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_USBCABLE, RID_WILDCARD,
504 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
505 NO_TEST_UNIT_READY | NO_START_STOP | ALT_IFACE_1
506 },
507 {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_ATAPI, RID_WILDCARD,
508 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
509 NO_QUIRKS
510 },
511 {USB_VENDOR_INSYSTEM, USB_PRODUCT_INSYSTEM_STORAGE_V2, RID_WILDCARD,
512 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
513 NO_QUIRKS
514 },
515 {USB_VENDOR_IODATA, USB_PRODUCT_IODATA_IU_CD2, RID_WILDCARD,
516 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
517 NO_QUIRKS
518 },
519 {USB_VENDOR_IODATA, USB_PRODUCT_IODATA_DVR_UEH8, RID_WILDCARD,
520 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
521 NO_QUIRKS
522 },
523 {USB_VENDOR_IOMEGA, USB_PRODUCT_IOMEGA_ZIP100, RID_WILDCARD,
524 /*
525 * XXX This is not correct as there are Zip drives that use
526 * ATAPI.
527 */
528 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
529 NO_TEST_UNIT_READY
530 },
531 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_L3, RID_WILDCARD,
532 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
533 NO_INQUIRY
534 },
535 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S3X, RID_WILDCARD,
536 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
537 NO_INQUIRY
538 },
539 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S4, RID_WILDCARD,
540 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
541 NO_INQUIRY
542 },
543 {USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_FINECAM_S5, RID_WILDCARD,
544 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
545 NO_INQUIRY
546 },
547 {USB_VENDOR_LACIE, USB_PRODUCT_LACIE_HD, RID_WILDCARD,
548 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
549 NO_QUIRKS
550 },
551 {USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_CF_READER, RID_WILDCARD,
552 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
553 NO_INQUIRY
554 },
555 {USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_JUMPSHOT, RID_WILDCARD,
556 UMASS_PROTO_SCSI,
557 NO_QUIRKS
558 },
559 {USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443SU2, RID_WILDCARD,
560 UMASS_PROTO_SCSI,
561 NO_QUIRKS
562 },
563 {USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LDR_H443U2, RID_WILDCARD,
564 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
565 NO_QUIRKS
566 },
567 {USB_VENDOR_MELCO, USB_PRODUCT_MELCO_DUBPXXG, RID_WILDCARD,
568 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
569 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
570 },
571 {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_DPCM, RID_WILDCARD,
572 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
573 NO_TEST_UNIT_READY | NO_START_STOP
574 },
575 {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIDB25, RID_WILDCARD,
576 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
577 NO_QUIRKS
578 },
579 {USB_VENDOR_MICROTECH, USB_PRODUCT_MICROTECH_SCSIHD50, RID_WILDCARD,
580 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
581 NO_QUIRKS
582 },
583 {USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_E223, RID_WILDCARD,
584 UMASS_PROTO_SCSI,
585 NO_QUIRKS
586 },
587 {USB_VENDOR_MINOLTA, USB_PRODUCT_MINOLTA_F300, RID_WILDCARD,
588 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
589 NO_QUIRKS
590 },
591 {USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_CDRRW, RID_WILDCARD,
592 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
593 NO_QUIRKS
594 },
595 {USB_VENDOR_MITSUMI, USB_PRODUCT_MITSUMI_FDD, RID_WILDCARD,
596 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
597 NO_GETMAXLUN
598 },
599 {USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_E398, RID_WILDCARD,
600 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
601 FORCE_SHORT_INQUIRY | NO_INQUIRY_EVPD | NO_GETMAXLUN
602 },
603 {USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY, RID_WILDCARD,
604 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
605 IGNORE_RESIDUE | NO_GETMAXLUN | RS_NO_CLEAR_UA
606 },
607 {USB_VENDOR_MSYSTEMS, USB_PRODUCT_MSYSTEMS_DISKONKEY2, RID_WILDCARD,
608 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
609 NO_QUIRKS
610 },
611 {USB_VENDOR_MYSON, USB_PRODUCT_MYSON_HEDEN, RID_WILDCARD,
612 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
613 NO_INQUIRY | IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
613 IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
614 },
615 {USB_VENDOR_MYSON, USB_PRODUCT_MYSON_STARREADER, RID_WILDCARD,
616 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
617 NO_SYNCHRONIZE_CACHE
618 },
619 {USB_VENDOR_NEODIO, USB_PRODUCT_NEODIO_ND3260, RID_WILDCARD,
620 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
621 FORCE_SHORT_INQUIRY
622 },
623 {USB_VENDOR_NETAC, USB_PRODUCT_NETAC_CF_CARD, RID_WILDCARD,
624 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
625 NO_INQUIRY
626 },
627 {USB_VENDOR_NETAC, USB_PRODUCT_NETAC_ONLYDISK, RID_WILDCARD,
628 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
629 IGNORE_RESIDUE
630 },
631 {USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_CLIK_40, RID_WILDCARD,
632 UMASS_PROTO_ATAPI,
633 NO_INQUIRY
634 },
635 {USB_VENDOR_NIKON, USB_PRODUCT_NIKON_D300, RID_WILDCARD,
636 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
637 NO_QUIRKS
638 },
639 {USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C1, RID_WILDCARD,
640 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
641 WRONG_CSWSIG
642 },
643 {USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C700, RID_WILDCARD,
644 UMASS_PROTO_SCSI,
645 NO_GETMAXLUN
646 },
647 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_SDS_HOTFIND_D, RID_WILDCARD,
648 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
649 NO_GETMAXLUN | NO_SYNCHRONIZE_CACHE
650 },
651 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFMS_RW, RID_WILDCARD,
652 UMASS_PROTO_SCSI,
653 NO_QUIRKS
654 },
655 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_COMBO, RID_WILDCARD,
656 UMASS_PROTO_SCSI,
657 NO_QUIRKS
658 },
659 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER, RID_WILDCARD,
660 UMASS_PROTO_SCSI,
661 NO_QUIRKS
662 },
663 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER2, RID_WILDCARD,
664 UMASS_PROTO_SCSI,
665 NO_QUIRKS
666 },
667 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDCFE_B_CF_READER, RID_WILDCARD,
668 UMASS_PROTO_SCSI,
669 NO_QUIRKS
670 },
671 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDSM_B_READER, RID_WILDCARD,
672 UMASS_PROTO_SCSI,
673 NO_INQUIRY
674 },
675 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_READER, RID_WILDCARD,
676 UMASS_PROTO_SCSI,
677 NO_QUIRKS
678 },
679 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_UCF100, RID_WILDCARD,
680 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
681 NO_INQUIRY | NO_GETMAXLUN
682 },
683 {USB_VENDOR_ONSPEC2, USB_PRODUCT_ONSPEC2_IMAGEMATE_SDDR55, RID_WILDCARD,
684 UMASS_PROTO_SCSI,
685 NO_GETMAXLUN
686 },
687 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXL840AN, RID_WILDCARD,
688 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
689 NO_GETMAXLUN
690 },
691 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB20AN, RID_WILDCARD,
692 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
693 NO_QUIRKS
694 },
695 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB35AN, RID_WILDCARD,
696 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
697 NO_QUIRKS
698 },
699 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_LS120CAM, RID_WILDCARD,
700 UMASS_PROTO_UFI,
701 NO_QUIRKS
702 },
703 {USB_VENDOR_PLEXTOR, USB_PRODUCT_PLEXTOR_40_12_40U, RID_WILDCARD,
704 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
705 NO_TEST_UNIT_READY
706 },
707 {USB_VENDOR_PNY, USB_PRODUCT_PNY_ATTACHE2, RID_WILDCARD,
708 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
709 IGNORE_RESIDUE | NO_START_STOP
710 },
711 {USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_YP_U2, RID_WILDCARD,
712 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
713 SHUTTLE_INIT | NO_GETMAXLUN
714 },
715 {USB_VENDOR_SAMSUNG_TECHWIN, USB_PRODUCT_SAMSUNG_TECHWIN_DIGIMAX_410, RID_WILDCARD,
716 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
717 NO_INQUIRY
718 },
719 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR05A, RID_WILDCARD,
720 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
721 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
722 },
723 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR09, RID_WILDCARD,
724 UMASS_PROTO_SCSI,
725 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
726 },
727 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR12, RID_WILDCARD,
728 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
729 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
730 },
731 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ2_256, RID_WILDCARD,
732 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
733 IGNORE_RESIDUE
734 },
735 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_128, RID_WILDCARD,
736 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
737 IGNORE_RESIDUE
738 },
739 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_256, RID_WILDCARD,
740 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
741 IGNORE_RESIDUE
742 },
743 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR31, RID_WILDCARD,
744 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
745 READ_CAPACITY_OFFBY1
746 },
747 {USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_SL11R, RID_WILDCARD,
748 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
749 NO_INQUIRY
750 },
751 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSB, RID_WILDCARD,
752 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
753 NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
754 },
755 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CDRW, RID_WILDCARD,
756 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
757 NO_QUIRKS
758 },
759 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CF, RID_WILDCARD,
760 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
761 NO_QUIRKS
762 },
763 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBATAPI, RID_WILDCARD,
764 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
765 NO_QUIRKS
766 },
767 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBCFSM, RID_WILDCARD,
768 UMASS_PROTO_SCSI,
769 NO_QUIRKS
770 },
771 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSCSI, RID_WILDCARD,
772 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
773 NO_QUIRKS
774 },
775 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_HIFD, RID_WILDCARD,
776 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
777 NO_GETMAXLUN
778 },
779 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_SDDR09, RID_WILDCARD,
780 UMASS_PROTO_SCSI,
781 NO_GETMAXLUN
782 },
783 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_ZIOMMC, RID_WILDCARD,
784 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
785 NO_GETMAXLUN
786 },
787 {USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_I_BEAD100, RID_WILDCARD,
788 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
789 SHUTTLE_INIT
790 },
791 {USB_VENDOR_SIIG, USB_PRODUCT_SIIG_WINTERREADER, RID_WILDCARD,
792 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
793 IGNORE_RESIDUE
794 },
795 {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_MD_7425, RID_WILDCARD,
796 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
797 NO_INQUIRY
798 },
799 {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_SX_520Z, RID_WILDCARD,
800 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
801 NO_INQUIRY
802 },
803 {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, 0x0500,
804 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
805 RBC_PAD_TO_12
806 },
807 {USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40_MS, RID_WILDCARD,
808 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
809 NO_INQUIRY
810 },
811 {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0500,
812 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
813 RBC_PAD_TO_12
814 },
815 {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0600,
816 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
817 RBC_PAD_TO_12
818 },
819 {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, RID_WILDCARD,
820 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
821 NO_QUIRKS
822 },
823 {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, RID_WILDCARD,
824 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
825 NO_QUIRKS
826 },
827 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSC, RID_WILDCARD,
828 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
829 NO_QUIRKS
830 },
831 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_MSC_U03, RID_WILDCARD,
832 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
833 NO_GETMAXLUN
834 },
835 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_NW_MS7, RID_WILDCARD,
836 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
837 NO_GETMAXLUN
838 },
839 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_PEG_N760C, RID_WILDCARD,
840 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
841 NO_INQUIRY
842 },
843 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSACUS1, RID_WILDCARD,
844 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
845 NO_GETMAXLUN
846 },
847 {USB_VENDOR_SONY, USB_PRODUCT_SONY_PORTABLE_HDD_V2, RID_WILDCARD,
848 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
849 NO_QUIRKS
850 },
851 {USB_VENDOR_SUPERTOP, USB_PRODUCT_SUPERTOP_IDE, RID_WILDCARD,
852 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
853 IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
854 },
855 {USB_VENDOR_TAUGA, USB_PRODUCT_TAUGA_CAMERAMATE, RID_WILDCARD,
856 UMASS_PROTO_SCSI,
857 NO_QUIRKS
858 },
859 {USB_VENDOR_TEAC, USB_PRODUCT_TEAC_FD05PUB, RID_WILDCARD,
860 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
861 NO_QUIRKS
862 },
863 {USB_VENDOR_TREK, USB_PRODUCT_TREK_MEMKEY, RID_WILDCARD,
864 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
865 NO_INQUIRY
866 },
867 {USB_VENDOR_TREK, USB_PRODUCT_TREK_THUMBDRIVE_8MB, RID_WILDCARD,
868 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
869 IGNORE_RESIDUE
870 },
871 {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_C3310, RID_WILDCARD,
872 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
873 NO_QUIRKS
874 },
875 {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_MP3, RID_WILDCARD,
876 UMASS_PROTO_RBC,
877 NO_QUIRKS
878 },
879 {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_T33520, RID_WILDCARD,
880 UMASS_PROTO_SCSI,
881 NO_QUIRKS
882 },
883 {USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_MDIV, RID_WILDCARD,
884 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
885 NO_QUIRKS
886 },
887 {USB_VENDOR_VIA, USB_PRODUCT_VIA_USB2IDEBRIDGE, RID_WILDCARD,
888 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
889 NO_SYNCHRONIZE_CACHE
890 },
891 {USB_VENDOR_VIVITAR, USB_PRODUCT_VIVITAR_35XX, RID_WILDCARD,
892 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
893 NO_INQUIRY
894 },
895 {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_COMBO, RID_WILDCARD,
896 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
897 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
898 },
899 {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_EXTHDD, RID_WILDCARD,
900 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
901 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
902 },
903 {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_MYBOOK, RID_WILDCARD,
904 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
905 NO_INQUIRY_EVPD
906 },
907 {USB_VENDOR_WINMAXGROUP, USB_PRODUCT_WINMAXGROUP_FLASH64MC, RID_WILDCARD,
908 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
909 NO_INQUIRY
910 },
911 {USB_VENDOR_YANO, USB_PRODUCT_YANO_FW800HD, RID_WILDCARD,
912 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
913 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
914 },
915 {USB_VENDOR_YANO, USB_PRODUCT_YANO_U640MO, RID_WILDCARD,
916 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
917 FORCE_SHORT_INQUIRY
918 },
919 {USB_VENDOR_YEDATA, USB_PRODUCT_YEDATA_FLASHBUSTERU, RID_WILDCARD,
920 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
921 NO_GETMAXLUN
922 },
923 {USB_VENDOR_ZORAN, USB_PRODUCT_ZORAN_EX20DSC, RID_WILDCARD,
924 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
925 NO_QUIRKS
926 },
927 {USB_VENDOR_MEIZU, USB_PRODUCT_MEIZU_M6_SL, RID_WILDCARD,
928 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
929 NO_INQUIRY | NO_SYNCHRONIZE_CACHE
930 },
931 {VID_EOT, PID_EOT, RID_EOT, 0, 0}
932};
933
934struct umass_softc {
935
936 struct scsi_sense cam_scsi_sense;
937 struct scsi_test_unit_ready cam_scsi_test_unit_ready;
938 struct mtx sc_mtx;
939 struct {
940 uint8_t *data_ptr;
941 union ccb *ccb;
942 umass_callback_t *callback;
943
944 uint32_t data_len; /* bytes */
945 uint32_t data_rem; /* bytes */
946 uint32_t data_timeout; /* ms */
947 uint32_t actlen; /* bytes */
948
949 uint8_t cmd_data[UMASS_MAX_CMDLEN];
950 uint8_t cmd_len; /* bytes */
951 uint8_t dir;
952 uint8_t lun;
953 } sc_transfer;
954
955 /* Bulk specific variables for transfers in progress */
956 umass_bbb_cbw_t cbw; /* command block wrapper */
957 umass_bbb_csw_t csw; /* command status wrapper */
958
959 /* CBI specific variables for transfers in progress */
960 umass_cbi_sbl_t sbl; /* status block */
961
962 device_t sc_dev;
963 struct usb2_device *sc_udev;
964 struct cam_sim *sc_sim; /* SCSI Interface Module */
965 struct usb2_xfer *sc_xfer[UMASS_T_MAX];
966
967 /*
968 * The command transform function is used to convert the SCSI
969 * commands into their derivatives, like UFI, ATAPI, and friends.
970 */
971 umass_transform_t *sc_transform;
972
973 uint32_t sc_unit;
974
975 uint16_t sc_proto; /* wire and cmd protocol */
976 uint16_t sc_quirks; /* they got it almost right */
977
978 uint8_t sc_name[16];
979 uint8_t sc_iface_no; /* interface number */
980 uint8_t sc_maxlun; /* maximum LUN number, inclusive */
981 uint8_t sc_last_xfer_index;
982 uint8_t sc_status_try;
983};
984
985struct umass_probe_proto {
986 uint16_t quirks;
987 uint16_t proto;
988
989 int32_t error;
990};
991
992/* prototypes */
993
994static device_probe_t umass_probe;
995static device_attach_t umass_attach;
996static device_detach_t umass_detach;
997
998static usb2_callback_t umass_tr_error;
999static usb2_callback_t umass_t_bbb_reset1_callback;
1000static usb2_callback_t umass_t_bbb_reset2_callback;
1001static usb2_callback_t umass_t_bbb_reset3_callback;
1002static usb2_callback_t umass_t_bbb_command_callback;
1003static usb2_callback_t umass_t_bbb_data_read_callback;
1004static usb2_callback_t umass_t_bbb_data_rd_cs_callback;
1005static usb2_callback_t umass_t_bbb_data_write_callback;
1006static usb2_callback_t umass_t_bbb_data_wr_cs_callback;
1007static usb2_callback_t umass_t_bbb_status_callback;
1008static usb2_callback_t umass_t_cbi_reset1_callback;
1009static usb2_callback_t umass_t_cbi_reset2_callback;
1010static usb2_callback_t umass_t_cbi_reset3_callback;
1011static usb2_callback_t umass_t_cbi_reset4_callback;
1012static usb2_callback_t umass_t_cbi_command_callback;
1013static usb2_callback_t umass_t_cbi_data_read_callback;
1014static usb2_callback_t umass_t_cbi_data_rd_cs_callback;
1015static usb2_callback_t umass_t_cbi_data_write_callback;
1016static usb2_callback_t umass_t_cbi_data_wr_cs_callback;
1017static usb2_callback_t umass_t_cbi_status_callback;
1018
1019static void umass_cancel_ccb(struct umass_softc *);
1020static void umass_init_shuttle(struct umass_softc *);
1021static void umass_reset(struct umass_softc *);
1022static void umass_t_bbb_data_clear_stall_callback(struct usb2_xfer *,
1023 uint8_t, uint8_t);
1024static void umass_command_start(struct umass_softc *, uint8_t, void *,
1025 uint32_t, uint32_t, umass_callback_t *, union ccb *);
1026static uint8_t umass_bbb_get_max_lun(struct umass_softc *);
1027static void umass_cbi_start_status(struct umass_softc *);
1028static void umass_t_cbi_data_clear_stall_callback(struct usb2_xfer *,
1029 uint8_t, uint8_t);
1030static int umass_cam_attach_sim(struct umass_softc *);
1031static void umass_cam_rescan_callback(struct cam_periph *, union ccb *);
1032static void umass_cam_rescan(struct umass_softc *);
1033static void umass_cam_attach(struct umass_softc *);
1034static void umass_cam_detach_sim(struct umass_softc *);
1035static void umass_cam_action(struct cam_sim *, union ccb *);
1036static void umass_cam_poll(struct cam_sim *);
1037static void umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
1038 uint8_t);
1039static void umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
1040 uint8_t);
1041static void umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
1042 uint8_t);
1043static uint8_t umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
1044static uint8_t umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
1045static uint8_t umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
1046static uint8_t umass_atapi_transform(struct umass_softc *, uint8_t *,
1047 uint8_t);
1048static uint8_t umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
1049static uint8_t umass_std_transform(struct umass_softc *, union ccb *, uint8_t
1050 *, uint8_t);
1051
1052#if USB_DEBUG
1053static void umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
1054static void umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
1055static void umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
1056static void umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
1057 uint32_t);
1058#endif
1059
1060struct usb2_config umass_bbb_config[UMASS_T_BBB_MAX] = {
1061
1062 [UMASS_T_BBB_RESET1] = {
1063 .type = UE_CONTROL,
1064 .endpoint = 0x00, /* Control pipe */
1065 .direction = UE_DIR_ANY,
1066 .mh.bufsize = sizeof(struct usb2_device_request),
1067 .mh.callback = &umass_t_bbb_reset1_callback,
1068 .mh.timeout = 5000, /* 5 seconds */
1069 .mh.interval = 500, /* 500 milliseconds */
1070 },
1071
1072 [UMASS_T_BBB_RESET2] = {
1073 .type = UE_CONTROL,
1074 .endpoint = 0x00, /* Control pipe */
1075 .direction = UE_DIR_ANY,
1076 .mh.bufsize = sizeof(struct usb2_device_request),
1077 .mh.callback = &umass_t_bbb_reset2_callback,
1078 .mh.timeout = 5000, /* 5 seconds */
1079 .mh.interval = 50, /* 50 milliseconds */
1080 },
1081
1082 [UMASS_T_BBB_RESET3] = {
1083 .type = UE_CONTROL,
1084 .endpoint = 0x00, /* Control pipe */
1085 .direction = UE_DIR_ANY,
1086 .mh.bufsize = sizeof(struct usb2_device_request),
1087 .mh.callback = &umass_t_bbb_reset3_callback,
1088 .mh.timeout = 5000, /* 5 seconds */
1089 .mh.interval = 50, /* 50 milliseconds */
1090 },
1091
1092 [UMASS_T_BBB_COMMAND] = {
1093 .type = UE_BULK,
1094 .endpoint = UE_ADDR_ANY,
1095 .direction = UE_DIR_OUT,
1096 .mh.bufsize = sizeof(umass_bbb_cbw_t),
1097 .mh.callback = &umass_t_bbb_command_callback,
1098 .mh.timeout = 5000, /* 5 seconds */
1099 },
1100
1101 [UMASS_T_BBB_DATA_READ] = {
1102 .type = UE_BULK,
1103 .endpoint = UE_ADDR_ANY,
1104 .direction = UE_DIR_IN,
1105 .mh.bufsize = UMASS_BULK_SIZE,
1106 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1107 .mh.callback = &umass_t_bbb_data_read_callback,
1108 .mh.timeout = 0, /* overwritten later */
1109 },
1110
1111 [UMASS_T_BBB_DATA_RD_CS] = {
1112 .type = UE_CONTROL,
1113 .endpoint = 0x00, /* Control pipe */
1114 .direction = UE_DIR_ANY,
1115 .mh.bufsize = sizeof(struct usb2_device_request),
1116 .mh.callback = &umass_t_bbb_data_rd_cs_callback,
1117 .mh.timeout = 5000, /* 5 seconds */
1118 },
1119
1120 [UMASS_T_BBB_DATA_WRITE] = {
1121 .type = UE_BULK,
1122 .endpoint = UE_ADDR_ANY,
1123 .direction = UE_DIR_OUT,
1124 .mh.bufsize = UMASS_BULK_SIZE,
1125 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1126 .mh.callback = &umass_t_bbb_data_write_callback,
1127 .mh.timeout = 0, /* overwritten later */
1128 },
1129
1130 [UMASS_T_BBB_DATA_WR_CS] = {
1131 .type = UE_CONTROL,
1132 .endpoint = 0x00, /* Control pipe */
1133 .direction = UE_DIR_ANY,
1134 .mh.bufsize = sizeof(struct usb2_device_request),
1135 .mh.callback = &umass_t_bbb_data_wr_cs_callback,
1136 .mh.timeout = 5000, /* 5 seconds */
1137 },
1138
1139 [UMASS_T_BBB_STATUS] = {
1140 .type = UE_BULK,
1141 .endpoint = UE_ADDR_ANY,
1142 .direction = UE_DIR_IN,
1143 .mh.bufsize = sizeof(umass_bbb_csw_t),
1144 .mh.flags = {.short_xfer_ok = 1,},
1145 .mh.callback = &umass_t_bbb_status_callback,
1146 .mh.timeout = 5000, /* ms */
1147 },
1148};
1149
1150struct usb2_config umass_cbi_config[UMASS_T_CBI_MAX] = {
1151
1152 [UMASS_T_CBI_RESET1] = {
1153 .type = UE_CONTROL,
1154 .endpoint = 0x00, /* Control pipe */
1155 .direction = UE_DIR_ANY,
1156 .mh.bufsize = (sizeof(struct usb2_device_request) +
1157 UMASS_CBI_DIAGNOSTIC_CMDLEN),
1158 .mh.callback = &umass_t_cbi_reset1_callback,
1159 .mh.timeout = 5000, /* 5 seconds */
1160 .mh.interval = 500, /* 500 milliseconds */
1161 },
1162
1163 [UMASS_T_CBI_RESET2] = {
1164 .type = UE_CONTROL,
1165 .endpoint = 0x00, /* Control pipe */
1166 .direction = UE_DIR_ANY,
1167 .mh.bufsize = sizeof(struct usb2_device_request),
1168 .mh.callback = &umass_t_cbi_reset2_callback,
1169 .mh.timeout = 5000, /* 5 seconds */
1170 .mh.interval = 50, /* 50 milliseconds */
1171 },
1172
1173 [UMASS_T_CBI_RESET3] = {
1174 .type = UE_CONTROL,
1175 .endpoint = 0x00, /* Control pipe */
1176 .direction = UE_DIR_ANY,
1177 .mh.bufsize = sizeof(struct usb2_device_request),
1178 .mh.callback = &umass_t_cbi_reset3_callback,
1179 .mh.timeout = 5000, /* 5 seconds */
1180 .mh.interval = 50, /* 50 milliseconds */
1181 },
1182
1183 [UMASS_T_CBI_COMMAND] = {
1184 .type = UE_CONTROL,
1185 .endpoint = 0x00, /* Control pipe */
1186 .direction = UE_DIR_ANY,
1187 .mh.bufsize = (sizeof(struct usb2_device_request) +
1188 UMASS_MAX_CMDLEN),
1189 .mh.callback = &umass_t_cbi_command_callback,
1190 .mh.timeout = 5000, /* 5 seconds */
1191 },
1192
1193 [UMASS_T_CBI_DATA_READ] = {
1194 .type = UE_BULK,
1195 .endpoint = UE_ADDR_ANY,
1196 .direction = UE_DIR_IN,
1197 .mh.bufsize = UMASS_BULK_SIZE,
1198 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1199 .mh.callback = &umass_t_cbi_data_read_callback,
1200 .mh.timeout = 0, /* overwritten later */
1201 },
1202
1203 [UMASS_T_CBI_DATA_RD_CS] = {
1204 .type = UE_CONTROL,
1205 .endpoint = 0x00, /* Control pipe */
1206 .direction = UE_DIR_ANY,
1207 .mh.bufsize = sizeof(struct usb2_device_request),
1208 .mh.callback = &umass_t_cbi_data_rd_cs_callback,
1209 .mh.timeout = 5000, /* 5 seconds */
1210 },
1211
1212 [UMASS_T_CBI_DATA_WRITE] = {
1213 .type = UE_BULK,
1214 .endpoint = UE_ADDR_ANY,
1215 .direction = UE_DIR_OUT,
1216 .mh.bufsize = UMASS_BULK_SIZE,
1217 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1218 .mh.callback = &umass_t_cbi_data_write_callback,
1219 .mh.timeout = 0, /* overwritten later */
1220 },
1221
1222 [UMASS_T_CBI_DATA_WR_CS] = {
1223 .type = UE_CONTROL,
1224 .endpoint = 0x00, /* Control pipe */
1225 .direction = UE_DIR_ANY,
1226 .mh.bufsize = sizeof(struct usb2_device_request),
1227 .mh.callback = &umass_t_cbi_data_wr_cs_callback,
1228 .mh.timeout = 5000, /* 5 seconds */
1229 },
1230
1231 [UMASS_T_CBI_STATUS] = {
1232 .type = UE_INTERRUPT,
1233 .endpoint = UE_ADDR_ANY,
1234 .direction = UE_DIR_IN,
1235 .mh.flags = {.short_xfer_ok = 1,},
1236 .mh.bufsize = sizeof(umass_cbi_sbl_t),
1237 .mh.callback = &umass_t_cbi_status_callback,
1238 .mh.timeout = 5000, /* ms */
1239 },
1240
1241 [UMASS_T_CBI_RESET4] = {
1242 .type = UE_CONTROL,
1243 .endpoint = 0x00, /* Control pipe */
1244 .direction = UE_DIR_ANY,
1245 .mh.bufsize = sizeof(struct usb2_device_request),
1246 .mh.callback = &umass_t_cbi_reset4_callback,
1247 .mh.timeout = 5000, /* ms */
1248 },
1249};
1250
1251/* If device cannot return valid inquiry data, fake it */
1252static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1253 0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
1254 /* additional_length */ 31, 0, 0, 0
1255};
1256
1257#define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
1258#define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
1259
1260static devclass_t umass_devclass;
1261
1262static device_method_t umass_methods[] = {
1263 /* Device interface */
1264 DEVMETHOD(device_probe, umass_probe),
1265 DEVMETHOD(device_attach, umass_attach),
1266 DEVMETHOD(device_detach, umass_detach),
1267 {0, 0}
1268};
1269
1270static driver_t umass_driver = {
1271 .name = "umass",
1272 .methods = umass_methods,
1273 .size = sizeof(struct umass_softc),
1274};
1275
1276DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
1277MODULE_DEPEND(umass, usb, 1, 1, 1);
1278MODULE_DEPEND(umass, cam, 1, 1, 1);
1279
1280/*
1281 * USB device probe/attach/detach
1282 */
1283
1284/*
1285 * Match the device we are seeing with the
1286 * devices supported.
1287 */
1288static struct umass_probe_proto
1289umass_probe_proto(device_t dev, struct usb2_attach_arg *uaa)
1290{
1291 const struct umass_devdescr *udd = umass_devdescr;
1292 struct usb2_interface_descriptor *id;
1293 struct umass_probe_proto ret;
1294
1295 bzero(&ret, sizeof(ret));
1296
1297 /*
1298 * An entry specifically for Y-E Data devices as they don't fit in
1299 * the device description table.
1300 */
1301 if ((uaa->info.idVendor == USB_VENDOR_YEDATA) &&
1302 (uaa->info.idProduct == USB_PRODUCT_YEDATA_FLASHBUSTERU)) {
1303
1304 /*
1305 * Revisions < 1.28 do not handle the interrupt endpoint
1306 * very well.
1307 */
1308 if (uaa->info.bcdDevice < 0x128) {
1309 ret.proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1310 } else {
1311 ret.proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1312 }
1313
1314 /*
1315 * Revisions < 1.28 do not have the TEST UNIT READY command
1316 * Revisions == 1.28 have a broken TEST UNIT READY
1317 */
1318 if (uaa->info.bcdDevice <= 0x128) {
1319 ret.quirks |= NO_TEST_UNIT_READY;
1320 }
1321 ret.quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1322 ret.error = 0;
1323 goto done;
1324 }
1325 /*
1326 * Check the list of supported devices for a match. While looking,
1327 * check for wildcarded and fully matched. First match wins.
1328 */
1329 for (; udd->vid != VID_EOT; udd++) {
1330 if ((udd->vid == VID_WILDCARD) &&
1331 (udd->pid == PID_WILDCARD) &&
1332 (udd->rid == RID_WILDCARD)) {
1333 device_printf(dev, "ignoring invalid "
1334 "wildcard quirk\n");
1335 continue;
1336 }
1337 if (((udd->vid == uaa->info.idVendor) ||
1338 (udd->vid == VID_WILDCARD)) &&
1339 ((udd->pid == uaa->info.idProduct) ||
1340 (udd->pid == PID_WILDCARD))) {
1341 if (udd->rid == RID_WILDCARD) {
1342 ret.proto = udd->proto;
1343 ret.quirks = udd->quirks;
1344 ret.error = 0;
1345 goto done;
1346 } else if (udd->rid == uaa->info.bcdDevice) {
1347 ret.proto = udd->proto;
1348 ret.quirks = udd->quirks;
1349 ret.error = 0;
1350 goto done;
1351 } /* else RID does not match */
1352 }
1353 }
1354
1355 /* Check for a standards compliant device */
1356 id = usb2_get_interface_descriptor(uaa->iface);
1357 if ((id == NULL) ||
1358 (id->bInterfaceClass != UICLASS_MASS)) {
1359 ret.error = ENXIO;
1360 goto done;
1361 }
1362 switch (id->bInterfaceSubClass) {
1363 case UISUBCLASS_SCSI:
1364 ret.proto |= UMASS_PROTO_SCSI;
1365 break;
1366 case UISUBCLASS_UFI:
1367 ret.proto |= UMASS_PROTO_UFI;
1368 break;
1369 case UISUBCLASS_RBC:
1370 ret.proto |= UMASS_PROTO_RBC;
1371 break;
1372 case UISUBCLASS_SFF8020I:
1373 case UISUBCLASS_SFF8070I:
1374 ret.proto |= UMASS_PROTO_ATAPI;
1375 break;
1376 default:
1377 device_printf(dev, "unsupported command "
1378 "protocol %d\n", id->bInterfaceSubClass);
1379 ret.error = ENXIO;
1380 goto done;
1381 }
1382
1383 switch (id->bInterfaceProtocol) {
1384 case UIPROTO_MASS_CBI:
1385 ret.proto |= UMASS_PROTO_CBI;
1386 break;
1387 case UIPROTO_MASS_CBI_I:
1388 ret.proto |= UMASS_PROTO_CBI_I;
1389 break;
1390 case UIPROTO_MASS_BBB_OLD:
1391 case UIPROTO_MASS_BBB:
1392 ret.proto |= UMASS_PROTO_BBB;
1393 break;
1394 default:
1395 device_printf(dev, "unsupported wire "
1396 "protocol %d\n", id->bInterfaceProtocol);
1397 ret.error = ENXIO;
1398 goto done;
1399 }
1400
1401 ret.error = 0;
1402done:
1403 return (ret);
1404}
1405
1406static int
1407umass_probe(device_t dev)
1408{
1409 struct usb2_attach_arg *uaa = device_get_ivars(dev);
1410 struct umass_probe_proto temp;
1411
1412 if (uaa->usb2_mode != USB_MODE_HOST) {
1413 return (ENXIO);
1414 }
1415 if (uaa->use_generic == 0) {
1416 /* give other drivers a try first */
1417 return (ENXIO);
1418 }
1419 temp = umass_probe_proto(dev, uaa);
1420
1421 return (temp.error);
1422}
1423
1424static int
1425umass_attach(device_t dev)
1426{
1427 struct umass_softc *sc = device_get_softc(dev);
1428 struct usb2_attach_arg *uaa = device_get_ivars(dev);
1429 struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
1430 struct usb2_interface_descriptor *id;
1431 int32_t err;
1432
1433 /*
1434 * NOTE: the softc struct is bzero-ed in device_set_driver.
1435 * We can safely call umass_detach without specifically
1436 * initializing the struct.
1437 */
1438
1439 sc->sc_dev = dev;
1440 sc->sc_udev = uaa->device;
1441 sc->sc_proto = temp.proto;
1442 sc->sc_quirks = temp.quirks;
1443 sc->sc_unit = device_get_unit(dev);
1444
1445 snprintf(sc->sc_name, sizeof(sc->sc_name),
1446 "%s", device_get_nameunit(dev));
1447
1448 device_set_usb2_desc(dev);
1449
1450 mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
1451 NULL, MTX_DEF | MTX_RECURSE);
1452
1453 /* get interface index */
1454
1455 id = usb2_get_interface_descriptor(uaa->iface);
1456 if (id == NULL) {
1457 device_printf(dev, "failed to get "
1458 "interface number\n");
1459 goto detach;
1460 }
1461 sc->sc_iface_no = id->bInterfaceNumber;
1462
1463#if USB_DEBUG
1464 device_printf(dev, " ");
1465
1466 switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
1467 case UMASS_PROTO_SCSI:
1468 printf("SCSI");
1469 break;
1470 case UMASS_PROTO_ATAPI:
1471 printf("8070i (ATAPI)");
1472 break;
1473 case UMASS_PROTO_UFI:
1474 printf("UFI");
1475 break;
1476 case UMASS_PROTO_RBC:
1477 printf("RBC");
1478 break;
1479 default:
1480 printf("(unknown 0x%02x)",
1481 sc->sc_proto & UMASS_PROTO_COMMAND);
1482 break;
1483 }
1484
1485 printf(" over ");
1486
1487 switch (sc->sc_proto & UMASS_PROTO_WIRE) {
1488 case UMASS_PROTO_BBB:
1489 printf("Bulk-Only");
1490 break;
1491 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
1492 printf("CBI");
1493 break;
1494 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1495 printf("CBI with CCI");
1496 break;
1497 default:
1498 printf("(unknown 0x%02x)",
1499 sc->sc_proto & UMASS_PROTO_WIRE);
1500 }
1501
1502 printf("; quirks = 0x%04x\n", sc->sc_quirks);
1503#endif
1504
1505 if (sc->sc_quirks & ALT_IFACE_1) {
1506 err = usb2_set_alt_interface_index
1507 (uaa->device, uaa->info.bIfaceIndex, 1);
1508
1509 if (err) {
1510 DPRINTF(sc, UDMASS_USB, "could not switch to "
1511 "Alt Interface 1\n");
1512 goto detach;
1513 }
1514 }
1515 /* allocate all required USB transfers */
1516
1517 if (sc->sc_proto & UMASS_PROTO_BBB) {
1518
1519 err = usb2_transfer_setup(uaa->device,
1520 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
1521 UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
1522
1523 /* skip reset first time */
1524 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1525
1526 } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
1527
1528 err = usb2_transfer_setup(uaa->device,
1529 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
1530 (sc->sc_proto & UMASS_PROTO_CBI_I) ?
1531 UMASS_T_CBI_MAX : (UMASS_T_CBI_MAX - 2), sc,
1532 &sc->sc_mtx);
1533
1534 /* skip reset first time */
1535 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1536
1537 } else {
1538 err = USB_ERR_INVAL;
1539 }
1540
1541 if (err) {
1542 device_printf(dev, "could not setup required "
1543 "transfers, %s\n", usb2_errstr(err));
1544 goto detach;
1545 }
1546 sc->sc_transform =
1547 (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
1548 (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
1549 (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
1550 (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1551 &umass_no_transform;
1552
1553 /* from here onwards the device can be used. */
1554
1555 if (sc->sc_quirks & SHUTTLE_INIT) {
1556 umass_init_shuttle(sc);
1557 }
1558 /* get the maximum LUN supported by the device */
1559
1560 if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1561 !(sc->sc_quirks & NO_GETMAXLUN))
1562 sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1563 else
1564 sc->sc_maxlun = 0;
1565
1566 /* Prepare the SCSI command block */
1567 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1568 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1569
1570 /*
1571 * some devices need a delay after that the configuration value is
1572 * set to function properly:
1573 */
1574 usb2_pause_mtx(NULL, hz);
1575
1576 /* register the SIM */
1577 err = umass_cam_attach_sim(sc);
1578 if (err) {
1579 goto detach;
1580 }
1581 /* scan the SIM */
1582 umass_cam_attach(sc);
1583
1584 DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1585
1586 return (0); /* success */
1587
1588detach:
1589 umass_detach(dev);
1590 return (ENXIO); /* failure */
1591}
1592
1593static int
1594umass_detach(device_t dev)
1595{
1596 struct umass_softc *sc = device_get_softc(dev);
1597
1598 DPRINTF(sc, UDMASS_USB, "\n");
1599
1600 /* teardown our statemachine */
1601
1602 usb2_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1603
1604#if (__FreeBSD_version >= 700037)
1605 mtx_lock(&sc->sc_mtx);
1606#endif
1607 umass_cam_detach_sim(sc);
1608
1609#if (__FreeBSD_version >= 700037)
1610 mtx_unlock(&sc->sc_mtx);
1611#endif
1612
1613 return (0); /* success */
1614}
1615
1616static void
1617umass_init_shuttle(struct umass_softc *sc)
1618{
1619 struct usb2_device_request req;
1620 usb2_error_t err;
1621 uint8_t status[2] = {0, 0};
1622
1623 /*
1624 * The Linux driver does this, but no one can tell us what the
1625 * command does.
1626 */
1627 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1628 req.bRequest = 1; /* XXX unknown command */
1629 USETW(req.wValue, 0);
1630 req.wIndex[0] = sc->sc_iface_no;
1631 req.wIndex[1] = 0;
1632 USETW(req.wLength, sizeof(status));
1633 err = usb2_do_request(sc->sc_udev, NULL, &req, &status);
1634
1635 DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1636 status[0], status[1]);
1637}
1638
1639/*
1640 * Generic functions to handle transfers
1641 */
1642
1643static void
1644umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1645{
1646 DPRINTF(sc, UDMASS_GEN, "transfer index = "
1647 "%d\n", xfer_index);
1648
1649 if (sc->sc_xfer[xfer_index]) {
1650 sc->sc_last_xfer_index = xfer_index;
1651 usb2_transfer_start(sc->sc_xfer[xfer_index]);
1652 } else {
1653 umass_cancel_ccb(sc);
1654 }
1655}
1656
1657static void
1658umass_reset(struct umass_softc *sc)
1659{
1660 DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1661
1662 /*
1663 * stop the last transfer, if not already stopped:
1664 */
1665 usb2_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1666 umass_transfer_start(sc, 0);
1667}
1668
1669static void
1670umass_cancel_ccb(struct umass_softc *sc)
1671{
1672 union ccb *ccb;
1673
1674 mtx_assert(&sc->sc_mtx, MA_OWNED);
1675
1676 ccb = sc->sc_transfer.ccb;
1677 sc->sc_transfer.ccb = NULL;
1678 sc->sc_last_xfer_index = 0;
1679
1680 if (ccb) {
1681 (sc->sc_transfer.callback)
1682 (sc, ccb, (sc->sc_transfer.data_len -
1683 sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1684 }
1685}
1686
1687static void
1688umass_tr_error(struct usb2_xfer *xfer)
1689{
1690 struct umass_softc *sc = xfer->priv_sc;
1691
1692 if (xfer->error != USB_ERR_CANCELLED) {
1693
1694 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1695 "reset\n", usb2_errstr(xfer->error));
1696 }
1697 umass_cancel_ccb(sc);
1698}
1699
1700/*
1701 * BBB protocol specific functions
1702 */
1703
1704static void
1705umass_t_bbb_reset1_callback(struct usb2_xfer *xfer)
1706{
1707 struct umass_softc *sc = xfer->priv_sc;
1708 struct usb2_device_request req;
1709
1710 switch (USB_GET_STATE(xfer)) {
1711 case USB_ST_TRANSFERRED:
1712 umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1713 return;
1714
1715 case USB_ST_SETUP:
1716 /*
1717 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1718 *
1719 * For Reset Recovery the host shall issue in the following order:
1720 * a) a Bulk-Only Mass Storage Reset
1721 * b) a Clear Feature HALT to the Bulk-In endpoint
1722 * c) a Clear Feature HALT to the Bulk-Out endpoint
1723 *
1724 * This is done in 3 steps, using 3 transfers:
1725 * UMASS_T_BBB_RESET1
1726 * UMASS_T_BBB_RESET2
1727 * UMASS_T_BBB_RESET3
1728 */
1729
1730 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1731
1732 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1733 req.bRequest = UR_BBB_RESET; /* bulk only reset */
1734 USETW(req.wValue, 0);
1735 req.wIndex[0] = sc->sc_iface_no;
1736 req.wIndex[1] = 0;
1737 USETW(req.wLength, 0);
1738
1739 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
1740
1741 xfer->frlengths[0] = sizeof(req);
1742 xfer->nframes = 1;
1743 usb2_start_hardware(xfer);
1744 return;
1745
1746 default: /* Error */
1747 umass_tr_error(xfer);
1748 return;
1749
1750 }
1751}
1752
1753static void
1754umass_t_bbb_reset2_callback(struct usb2_xfer *xfer)
1755{
1756 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1757 UMASS_T_BBB_DATA_READ);
1758}
1759
1760static void
1761umass_t_bbb_reset3_callback(struct usb2_xfer *xfer)
1762{
1763 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1764 UMASS_T_BBB_DATA_WRITE);
1765}
1766
1767static void
1768umass_t_bbb_data_clear_stall_callback(struct usb2_xfer *xfer,
1769 uint8_t next_xfer,
1770 uint8_t stall_xfer)
1771{
1772 struct umass_softc *sc = xfer->priv_sc;
1773
1774 switch (USB_GET_STATE(xfer)) {
1775 case USB_ST_TRANSFERRED:
1776tr_transferred:
1777 umass_transfer_start(sc, next_xfer);
1778 return;
1779
1780 case USB_ST_SETUP:
1781 if (usb2_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1782 goto tr_transferred;
1783 }
1784 return;
1785
1786 default: /* Error */
1787 umass_tr_error(xfer);
1788 return;
1789
1790 }
1791}
1792
1793static void
1794umass_t_bbb_command_callback(struct usb2_xfer *xfer)
1795{
1796 struct umass_softc *sc = xfer->priv_sc;
1797 union ccb *ccb = sc->sc_transfer.ccb;
1798 uint32_t tag;
1799
1800 switch (USB_GET_STATE(xfer)) {
1801 case USB_ST_TRANSFERRED:
1802 umass_transfer_start
1803 (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1804 (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1805 UMASS_T_BBB_STATUS));
1806 return;
1807
1808 case USB_ST_SETUP:
1809
1810 sc->sc_status_try = 0;
1811
1812 if (ccb) {
1813
1814 /*
1815 * the initial value is not important,
1816 * as long as the values are unique:
1817 */
1818 tag = UGETDW(sc->cbw.dCBWTag) + 1;
1819
1820 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1821 USETDW(sc->cbw.dCBWTag, tag);
1822
1823 /*
1824 * dCBWDataTransferLength:
1825 * This field indicates the number of bytes of data that the host
1826 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1827 * the Direction bit) during the execution of this command. If this
1828 * field is set to 0, the device will expect that no data will be
1829 * transferred IN or OUT during this command, regardless of the value
1830 * of the Direction bit defined in dCBWFlags.
1831 */
1832 USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1833
1834 /*
1835 * dCBWFlags:
1836 * The bits of the Flags field are defined as follows:
1837 * Bits 0-6 reserved
1838 * Bit 7 Direction - this bit shall be ignored if the
1839 * dCBWDataTransferLength field is zero.
1840 * 0 = data Out from host to device
1841 * 1 = data In from device to host
1842 */
1843 sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1844 CBWFLAGS_IN : CBWFLAGS_OUT);
1845 sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1846
1847 if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1848 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1849 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1850 }
1851 sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1852
1853 bcopy(sc->sc_transfer.cmd_data, sc->cbw.CBWCDB,
1854 sc->sc_transfer.cmd_len);
1855
1856 bzero(sc->sc_transfer.cmd_data + sc->sc_transfer.cmd_len,
1857 sizeof(sc->cbw.CBWCDB) - sc->sc_transfer.cmd_len);
1858
1859 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1860
1861 usb2_copy_in(xfer->frbuffers, 0, &sc->cbw, sizeof(sc->cbw));
1862
1863 xfer->frlengths[0] = sizeof(sc->cbw);
1864 usb2_start_hardware(xfer);
1865 }
1866 return;
1867
1868 default: /* Error */
1869 umass_tr_error(xfer);
1870 return;
1871
1872 }
1873}
1874
1875static void
1876umass_t_bbb_data_read_callback(struct usb2_xfer *xfer)
1877{
1878 struct umass_softc *sc = xfer->priv_sc;
1879 uint32_t max_bulk = xfer->max_data_length;
1880
1881 switch (USB_GET_STATE(xfer)) {
1882 case USB_ST_TRANSFERRED:
1883 if (!xfer->flags.ext_buffer) {
1884 usb2_copy_out(xfer->frbuffers, 0,
1885 sc->sc_transfer.data_ptr, xfer->actlen);
1886 }
1887 sc->sc_transfer.data_rem -= xfer->actlen;
1888 sc->sc_transfer.data_ptr += xfer->actlen;
1889 sc->sc_transfer.actlen += xfer->actlen;
1890
1891 if (xfer->actlen < xfer->sumlen) {
1892 /* short transfer */
1893 sc->sc_transfer.data_rem = 0;
1894 }
1895 case USB_ST_SETUP:
1896 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1897 max_bulk, sc->sc_transfer.data_rem);
1898
1899 if (sc->sc_transfer.data_rem == 0) {
1900 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1901 return;
1902 }
1903 if (max_bulk > sc->sc_transfer.data_rem) {
1904 max_bulk = sc->sc_transfer.data_rem;
1905 }
1906 xfer->timeout = sc->sc_transfer.data_timeout;
1907 xfer->frlengths[0] = max_bulk;
1908
1909 if (xfer->flags.ext_buffer) {
1910 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
1911 }
1912 usb2_start_hardware(xfer);
1913 return;
1914
1915 default: /* Error */
1916 if (xfer->error == USB_ERR_CANCELLED) {
1917 umass_tr_error(xfer);
1918 } else {
1919 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1920 }
1921 return;
1922
1923 }
1924}
1925
1926static void
1927umass_t_bbb_data_rd_cs_callback(struct usb2_xfer *xfer)
1928{
1929 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1930 UMASS_T_BBB_DATA_READ);
1931}
1932
1933static void
1934umass_t_bbb_data_write_callback(struct usb2_xfer *xfer)
1935{
1936 struct umass_softc *sc = xfer->priv_sc;
1937 uint32_t max_bulk = xfer->max_data_length;
1938
1939 switch (USB_GET_STATE(xfer)) {
1940 case USB_ST_TRANSFERRED:
1941 sc->sc_transfer.data_rem -= xfer->actlen;
1942 sc->sc_transfer.data_ptr += xfer->actlen;
1943 sc->sc_transfer.actlen += xfer->actlen;
1944
1945 if (xfer->actlen < xfer->sumlen) {
1946 /* short transfer */
1947 sc->sc_transfer.data_rem = 0;
1948 }
1949 case USB_ST_SETUP:
1950 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1951 max_bulk, sc->sc_transfer.data_rem);
1952
1953 if (sc->sc_transfer.data_rem == 0) {
1954 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1955 return;
1956 }
1957 if (max_bulk > sc->sc_transfer.data_rem) {
1958 max_bulk = sc->sc_transfer.data_rem;
1959 }
1960 xfer->timeout = sc->sc_transfer.data_timeout;
1961 xfer->frlengths[0] = max_bulk;
1962
1963 if (xfer->flags.ext_buffer) {
1964 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
1965 } else {
1966 usb2_copy_in(xfer->frbuffers, 0,
1967 sc->sc_transfer.data_ptr, max_bulk);
1968 }
1969
1970 usb2_start_hardware(xfer);
1971 return;
1972
1973 default: /* Error */
1974 if (xfer->error == USB_ERR_CANCELLED) {
1975 umass_tr_error(xfer);
1976 } else {
1977 umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
1978 }
1979 return;
1980
1981 }
1982}
1983
1984static void
1985umass_t_bbb_data_wr_cs_callback(struct usb2_xfer *xfer)
1986{
1987 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1988 UMASS_T_BBB_DATA_WRITE);
1989}
1990
1991static void
1992umass_t_bbb_status_callback(struct usb2_xfer *xfer)
1993{
1994 struct umass_softc *sc = xfer->priv_sc;
1995 union ccb *ccb = sc->sc_transfer.ccb;
1996 uint32_t residue;
1997
1998 switch (USB_GET_STATE(xfer)) {
1999 case USB_ST_TRANSFERRED:
2000
2001 /*
2002 * Do a full reset if there is something wrong with the CSW:
2003 */
2004 sc->sc_status_try = 1;
2005
2006 /* Zero missing parts of the CSW: */
2007
2008 if (xfer->actlen < sizeof(sc->csw)) {
2009 bzero(&sc->csw, sizeof(sc->csw));
2010 }
2011 usb2_copy_out(xfer->frbuffers, 0, &sc->csw, xfer->actlen);
2012
2013 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2014
2015 residue = UGETDW(sc->csw.dCSWDataResidue);
2016
2017 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
2018 residue = (sc->sc_transfer.data_len -
2019 sc->sc_transfer.actlen);
2020 }
2021 if (residue > sc->sc_transfer.data_len) {
2022 DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
2023 "to %d bytes\n", residue, sc->sc_transfer.data_len);
2024 residue = sc->sc_transfer.data_len;
2025 }
2026 /* translate weird command-status signatures: */
2027 if (sc->sc_quirks & WRONG_CSWSIG) {
2028
2029 uint32_t temp = UGETDW(sc->csw.dCSWSignature);
2030
2031 if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
2032 (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
2033 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2034 }
2035 }
2036 /* check CSW and handle eventual error */
2037 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2038 DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
2039 UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
2040 /*
2041 * Invalid CSW: Wrong signature or wrong tag might
2042 * indicate that we lost synchronization. Reset the
2043 * device.
2044 */
2045 goto tr_error;
2046 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
2047 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
2048 "0x%08x\n", UGETDW(sc->csw.dCSWTag),
2049 UGETDW(sc->cbw.dCBWTag));
2050 goto tr_error;
2051 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2052 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
2053 sc->csw.bCSWStatus, CSWSTATUS_PHASE);
2054 goto tr_error;
2055 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2056 DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
2057 "%d\n", residue);
2058 goto tr_error;
2059 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
2060 DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
2061 sc->sc_transfer.actlen, sc->sc_transfer.data_len);
2062 goto tr_error;
2063 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2064 DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
2065 "%d\n", residue);
2066
2067 sc->sc_transfer.ccb = NULL;
2068
2069 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2070
2071 (sc->sc_transfer.callback)
2072 (sc, ccb, residue, STATUS_CMD_FAILED);
2073 } else {
2074 sc->sc_transfer.ccb = NULL;
2075
2076 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2077
2078 (sc->sc_transfer.callback)
2079 (sc, ccb, residue, STATUS_CMD_OK);
2080 }
2081 return;
2082
2083 case USB_ST_SETUP:
2084 xfer->frlengths[0] = xfer->max_data_length;
2085 usb2_start_hardware(xfer);
2086 return;
2087
2088 default:
2089tr_error:
2090 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
2091 usb2_errstr(xfer->error), sc->sc_status_try);
2092
2093 if ((xfer->error == USB_ERR_CANCELLED) ||
2094 (sc->sc_status_try)) {
2095 umass_tr_error(xfer);
2096 } else {
2097 sc->sc_status_try = 1;
2098 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
2099 }
2100 return;
2101
2102 }
2103}
2104
2105static void
2106umass_command_start(struct umass_softc *sc, uint8_t dir,
2107 void *data_ptr, uint32_t data_len,
2108 uint32_t data_timeout, umass_callback_t *callback,
2109 union ccb *ccb)
2110{
2111 sc->sc_transfer.lun = ccb->ccb_h.target_lun;
2112
2113 /*
2114 * NOTE: assumes that "sc->sc_transfer.cmd_data" and
2115 * "sc->sc_transfer.cmd_len" has been properly
2116 * initialized.
2117 */
2118
2119 sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
2120 sc->sc_transfer.data_ptr = data_ptr;
2121 sc->sc_transfer.data_len = data_len;
2122 sc->sc_transfer.data_rem = data_len;
2123 sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
2124
2125 sc->sc_transfer.actlen = 0;
2126 sc->sc_transfer.callback = callback;
2127 sc->sc_transfer.ccb = ccb;
2128
2129 if (sc->sc_xfer[sc->sc_last_xfer_index]) {
2130 usb2_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
2131 } else {
2132 ccb->ccb_h.status = CAM_TID_INVALID;
2133 xpt_done(ccb);
2134 }
2135}
2136
2137static uint8_t
2138umass_bbb_get_max_lun(struct umass_softc *sc)
2139{
2140 struct usb2_device_request req;
2141 usb2_error_t err;
2142 uint8_t buf = 0;
2143
2144 /* The Get Max Lun command is a class-specific request. */
2145 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2146 req.bRequest = UR_BBB_GET_MAX_LUN;
2147 USETW(req.wValue, 0);
2148 req.wIndex[0] = sc->sc_iface_no;
2149 req.wIndex[1] = 0;
2150 USETW(req.wLength, 1);
2151
2152 err = usb2_do_request(sc->sc_udev, NULL, &req, &buf);
2153 if (err) {
2154 buf = 0;
2155
2156 /* Device doesn't support Get Max Lun request. */
2157 printf("%s: Get Max Lun not supported (%s)\n",
2158 sc->sc_name, usb2_errstr(err));
2159 }
2160 return (buf);
2161}
2162
2163/*
2164 * Command/Bulk/Interrupt (CBI) specific functions
2165 */
2166
2167static void
2168umass_cbi_start_status(struct umass_softc *sc)
2169{
2170 if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
2171 umass_transfer_start(sc, UMASS_T_CBI_STATUS);
2172 } else {
2173 union ccb *ccb = sc->sc_transfer.ccb;
2174
2175 sc->sc_transfer.ccb = NULL;
2176
2177 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2178
2179 (sc->sc_transfer.callback)
2180 (sc, ccb, (sc->sc_transfer.data_len -
2181 sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
2182 }
2183}
2184
2185static void
2186umass_t_cbi_reset1_callback(struct usb2_xfer *xfer)
2187{
2188 struct umass_softc *sc = xfer->priv_sc;
2189 struct usb2_device_request req;
2190 uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
2191
2192 uint8_t i;
2193
2194 switch (USB_GET_STATE(xfer)) {
2195 case USB_ST_TRANSFERRED:
2196 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
2197 return;
2198
2199 case USB_ST_SETUP:
2200 /*
2201 * Command Block Reset Protocol
2202 *
2203 * First send a reset request to the device. Then clear
2204 * any possibly stalled bulk endpoints.
2205 *
2206 * This is done in 3 steps, using 3 transfers:
2207 * UMASS_T_CBI_RESET1
2208 * UMASS_T_CBI_RESET2
2209 * UMASS_T_CBI_RESET3
2210 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
2211 */
2212
2213 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
2214
2215 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2216 req.bRequest = UR_CBI_ADSC;
2217 USETW(req.wValue, 0);
2218 req.wIndex[0] = sc->sc_iface_no;
2219 req.wIndex[1] = 0;
2220 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
2221
2222 /*
2223 * The 0x1d code is the SEND DIAGNOSTIC command. To
2224 * distinguish between the two, the last 10 bytes of the CBL
2225 * is filled with 0xff (section 2.2 of the CBI
2226 * specification)
2227 */
2228 buf[0] = 0x1d; /* Command Block Reset */
2229 buf[1] = 0x04;
2230
2231 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
2232 buf[i] = 0xff;
2233 }
2234
2235 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
2236 usb2_copy_in(xfer->frbuffers + 1, 0, buf, sizeof(buf));
2237
2238 xfer->frlengths[0] = sizeof(req);
2239 xfer->frlengths[1] = sizeof(buf);
2240 xfer->nframes = 2;
2241 usb2_start_hardware(xfer);
2242 return;
2243
2244 default: /* Error */
2245 umass_tr_error(xfer);
2246 return;
2247
2248 }
2249}
2250
2251static void
2252umass_t_cbi_reset2_callback(struct usb2_xfer *xfer)
2253{
2254 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
2255 UMASS_T_CBI_DATA_READ);
2256}
2257
2258static void
2259umass_t_cbi_reset3_callback(struct usb2_xfer *xfer)
2260{
2261 struct umass_softc *sc = xfer->priv_sc;
2262
2263 umass_t_cbi_data_clear_stall_callback
2264 (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
2265 sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
2266 UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
2267 UMASS_T_CBI_DATA_WRITE);
2268}
2269
2270static void
2271umass_t_cbi_reset4_callback(struct usb2_xfer *xfer)
2272{
2273 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
2274 UMASS_T_CBI_STATUS);
2275}
2276
2277static void
2278umass_t_cbi_data_clear_stall_callback(struct usb2_xfer *xfer,
2279 uint8_t next_xfer,
2280 uint8_t stall_xfer)
2281{
2282 struct umass_softc *sc = xfer->priv_sc;
2283
2284 switch (USB_GET_STATE(xfer)) {
2285 case USB_ST_TRANSFERRED:
2286tr_transferred:
2287 if (next_xfer == UMASS_T_CBI_STATUS) {
2288 umass_cbi_start_status(sc);
2289 } else {
2290 umass_transfer_start(sc, next_xfer);
2291 }
2292 return;
2293
2294 case USB_ST_SETUP:
2295 if (usb2_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
2296 goto tr_transferred; /* should not happen */
2297 }
2298 return;
2299
2300 default: /* Error */
2301 umass_tr_error(xfer);
2302 return;
2303
2304 }
2305}
2306
2307static void
2308umass_t_cbi_command_callback(struct usb2_xfer *xfer)
2309{
2310 struct umass_softc *sc = xfer->priv_sc;
2311 union ccb *ccb = sc->sc_transfer.ccb;
2312 struct usb2_device_request req;
2313
2314 switch (USB_GET_STATE(xfer)) {
2315 case USB_ST_TRANSFERRED:
2316
2317 if (sc->sc_transfer.dir == DIR_NONE) {
2318 umass_cbi_start_status(sc);
2319 } else {
2320 umass_transfer_start
2321 (sc, (sc->sc_transfer.dir == DIR_IN) ?
2322 UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
2323 }
2324 return;
2325
2326 case USB_ST_SETUP:
2327
2328 if (ccb) {
2329
2330 /*
2331 * do a CBI transfer with cmd_len bytes from
2332 * cmd_data, possibly a data phase of data_len
2333 * bytes from/to the device and finally a status
2334 * read phase.
2335 */
2336
2337 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2338 req.bRequest = UR_CBI_ADSC;
2339 USETW(req.wValue, 0);
2340 req.wIndex[0] = sc->sc_iface_no;
2341 req.wIndex[1] = 0;
2342 req.wLength[0] = sc->sc_transfer.cmd_len;
2343 req.wLength[1] = 0;
2344
2345 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
2346 usb2_copy_in(xfer->frbuffers + 1, 0, sc->sc_transfer.cmd_data,
2347 sc->sc_transfer.cmd_len);
2348
2349 xfer->frlengths[0] = sizeof(req);
2350 xfer->frlengths[1] = sc->sc_transfer.cmd_len;
2351 xfer->nframes = xfer->frlengths[1] ? 2 : 1;
2352
2353 DIF(UDMASS_CBI,
2354 umass_cbi_dump_cmd(sc,
2355 sc->sc_transfer.cmd_data,
2356 sc->sc_transfer.cmd_len));
2357
2358 usb2_start_hardware(xfer);
2359 }
2360 return;
2361
2362 default: /* Error */
2363 umass_tr_error(xfer);
2364 return;
2365
2366 }
2367}
2368
2369static void
2370umass_t_cbi_data_read_callback(struct usb2_xfer *xfer)
2371{
2372 struct umass_softc *sc = xfer->priv_sc;
2373 uint32_t max_bulk = xfer->max_data_length;
2374
2375 switch (USB_GET_STATE(xfer)) {
2376 case USB_ST_TRANSFERRED:
2377 if (!xfer->flags.ext_buffer) {
2378 usb2_copy_out(xfer->frbuffers, 0,
2379 sc->sc_transfer.data_ptr, xfer->actlen);
2380 }
2381 sc->sc_transfer.data_rem -= xfer->actlen;
2382 sc->sc_transfer.data_ptr += xfer->actlen;
2383 sc->sc_transfer.actlen += xfer->actlen;
2384
2385 if (xfer->actlen < xfer->sumlen) {
2386 /* short transfer */
2387 sc->sc_transfer.data_rem = 0;
2388 }
2389 case USB_ST_SETUP:
2390 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2391 max_bulk, sc->sc_transfer.data_rem);
2392
2393 if (sc->sc_transfer.data_rem == 0) {
2394 umass_cbi_start_status(sc);
2395 return;
2396 }
2397 if (max_bulk > sc->sc_transfer.data_rem) {
2398 max_bulk = sc->sc_transfer.data_rem;
2399 }
2400 xfer->timeout = sc->sc_transfer.data_timeout;
2401
2402 if (xfer->flags.ext_buffer) {
2403 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
2404 }
2405 xfer->frlengths[0] = max_bulk;
2406 usb2_start_hardware(xfer);
2407 return;
2408
2409 default: /* Error */
2410 if ((xfer->error == USB_ERR_CANCELLED) ||
2411 (sc->sc_transfer.callback != &umass_cam_cb)) {
2412 umass_tr_error(xfer);
2413 } else {
2414 umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
2415 }
2416 return;
2417
2418 }
2419}
2420
2421static void
2422umass_t_cbi_data_rd_cs_callback(struct usb2_xfer *xfer)
2423{
2424 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2425 UMASS_T_CBI_DATA_READ);
2426}
2427
2428static void
2429umass_t_cbi_data_write_callback(struct usb2_xfer *xfer)
2430{
2431 struct umass_softc *sc = xfer->priv_sc;
2432 uint32_t max_bulk = xfer->max_data_length;
2433
2434 switch (USB_GET_STATE(xfer)) {
2435 case USB_ST_TRANSFERRED:
2436 sc->sc_transfer.data_rem -= xfer->actlen;
2437 sc->sc_transfer.data_ptr += xfer->actlen;
2438 sc->sc_transfer.actlen += xfer->actlen;
2439
2440 if (xfer->actlen < xfer->sumlen) {
2441 /* short transfer */
2442 sc->sc_transfer.data_rem = 0;
2443 }
2444 case USB_ST_SETUP:
2445 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2446 max_bulk, sc->sc_transfer.data_rem);
2447
2448 if (sc->sc_transfer.data_rem == 0) {
2449 umass_cbi_start_status(sc);
2450 return;
2451 }
2452 if (max_bulk > sc->sc_transfer.data_rem) {
2453 max_bulk = sc->sc_transfer.data_rem;
2454 }
2455 xfer->timeout = sc->sc_transfer.data_timeout;
2456
2457 if (xfer->flags.ext_buffer) {
2458 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
2459 } else {
2460 usb2_copy_in(xfer->frbuffers, 0,
2461 sc->sc_transfer.data_ptr, max_bulk);
2462 }
2463
2464 xfer->frlengths[0] = max_bulk;
2465 usb2_start_hardware(xfer);
2466 return;
2467
2468 default: /* Error */
2469 if ((xfer->error == USB_ERR_CANCELLED) ||
2470 (sc->sc_transfer.callback != &umass_cam_cb)) {
2471 umass_tr_error(xfer);
2472 } else {
2473 umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
2474 }
2475 return;
2476
2477 }
2478}
2479
2480static void
2481umass_t_cbi_data_wr_cs_callback(struct usb2_xfer *xfer)
2482{
2483 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2484 UMASS_T_CBI_DATA_WRITE);
2485}
2486
2487static void
2488umass_t_cbi_status_callback(struct usb2_xfer *xfer)
2489{
2490 struct umass_softc *sc = xfer->priv_sc;
2491 union ccb *ccb = sc->sc_transfer.ccb;
2492 uint32_t residue;
2493 uint8_t status;
2494
2495 switch (USB_GET_STATE(xfer)) {
2496 case USB_ST_TRANSFERRED:
2497
2498 if (xfer->actlen < sizeof(sc->sbl)) {
2499 goto tr_setup;
2500 }
2501 usb2_copy_out(xfer->frbuffers, 0, &sc->sbl, sizeof(sc->sbl));
2502
2503 residue = (sc->sc_transfer.data_len -
2504 sc->sc_transfer.actlen);
2505
2506 /* dissect the information in the buffer */
2507
2508 if (sc->sc_proto & UMASS_PROTO_UFI) {
2509
2510 /*
2511 * Section 3.4.3.1.3 specifies that the UFI command
2512 * protocol returns an ASC and ASCQ in the interrupt
2513 * data block.
2514 */
2515
2516 DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2517 "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2518 sc->sbl.ufi.ascq);
2519
2520 status = (((sc->sbl.ufi.asc == 0) &&
2521 (sc->sbl.ufi.ascq == 0)) ?
2522 STATUS_CMD_OK : STATUS_CMD_FAILED);
2523
2524 sc->sc_transfer.ccb = NULL;
2525
2526 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2527
2528 (sc->sc_transfer.callback)
2529 (sc, ccb, residue, status);
2530
2531 return;
2532
2533 } else {
2534
2535 /* Command Interrupt Data Block */
2536
2537 DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2538 sc->sbl.common.type, sc->sbl.common.value);
2539
2540 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2541
2542 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2543
2544 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2545 (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2546 (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2547 STATUS_WIRE_FAILED);
2548
2549 sc->sc_transfer.ccb = NULL;
2550
2551 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2552
2553 (sc->sc_transfer.callback)
2554 (sc, ccb, residue, status);
2555
2556 return;
2557 }
2558 }
2559
2560 /* fallthrough */
2561
2562 case USB_ST_SETUP:
2563tr_setup:
2564 xfer->frlengths[0] = xfer->max_data_length;
2565 usb2_start_hardware(xfer);
2566 return;
2567
2568 default: /* Error */
2569 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2570 usb2_errstr(xfer->error));
2571 umass_tr_error(xfer);
2572 return;
2573
2574 }
2575}
2576
2577/*
2578 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2579 */
2580
2581static int
2582umass_cam_attach_sim(struct umass_softc *sc)
2583{
2584 struct cam_devq *devq; /* Per device Queue */
2585
2586 /*
2587 * A HBA is attached to the CAM layer.
2588 *
2589 * The CAM layer will then after a while start probing for devices on
2590 * the bus. The number of SIMs is limited to one.
2591 */
2592
2593 devq = cam_simq_alloc(1 /* maximum openings */ );
2594 if (devq == NULL) {
2595 return (ENOMEM);
2596 }
2597 sc->sc_sim = cam_sim_alloc
2598 (&umass_cam_action, &umass_cam_poll,
2599 DEVNAME_SIM,
2600 sc /* priv */ ,
2601 sc->sc_unit /* unit number */ ,
2602#if (__FreeBSD_version >= 700037)
2603 &sc->sc_mtx /* mutex */ ,
2604#endif
2605 1 /* maximum device openings */ ,
2606 0 /* maximum tagged device openings */ ,
2607 devq);
2608
2609 if (sc->sc_sim == NULL) {
2610 cam_simq_free(devq);
2611 return (ENOMEM);
2612 }
2613
2614#if (__FreeBSD_version >= 700037)
2615 mtx_lock(&sc->sc_mtx);
2616#endif
2617
2618#if (__FreeBSD_version >= 700048)
2619 if (xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit) != CAM_SUCCESS) {
2620 mtx_unlock(&sc->sc_mtx);
2621 return (ENOMEM);
2622 }
2623#else
2624 if (xpt_bus_register(sc->sc_sim, sc->sc_unit) != CAM_SUCCESS) {
2625#if (__FreeBSD_version >= 700037)
2626 mtx_unlock(&sc->sc_mtx);
2627#endif
2628 return (ENOMEM);
2629 }
2630#endif
2631
2632#if (__FreeBSD_version >= 700037)
2633 mtx_unlock(&sc->sc_mtx);
2634#endif
2635 return (0);
2636}
2637
2638static void
2639umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2640{
2641#if USB_DEBUG
2642 struct umass_softc *sc = NULL;
2643
2644 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2645 DPRINTF(sc, UDMASS_SCSI, "%s:%d Rescan failed, 0x%04x\n",
2646 periph->periph_name, periph->unit_number,
2647 ccb->ccb_h.status);
2648 } else {
2649 DPRINTF(sc, UDMASS_SCSI, "%s%d: Rescan succeeded\n",
2650 periph->periph_name, periph->unit_number);
2651 }
2652#endif
2653
2654 xpt_free_path(ccb->ccb_h.path);
2655 free(ccb, M_USBDEV);
2656}
2657
2658static void
2659umass_cam_rescan(struct umass_softc *sc)
2660{
2661 struct cam_path *path;
2662 union ccb *ccb;
2663
2664 DPRINTF(sc, UDMASS_SCSI, "scbus%d: scanning for %d:%d:%d\n",
2665 cam_sim_path(sc->sc_sim),
2666 cam_sim_path(sc->sc_sim),
2667 sc->sc_unit, CAM_LUN_WILDCARD);
2668
2669 ccb = malloc(sizeof(*ccb), M_USBDEV, M_WAITOK | M_ZERO);
2670
2671 if (ccb == NULL) {
2672 return;
2673 }
2674#if (__FreeBSD_version >= 700037)
2675 mtx_lock(&sc->sc_mtx);
2676#endif
2677
2678 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->sc_sim),
2679 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2680 != CAM_REQ_CMP) {
2681#if (__FreeBSD_version >= 700037)
2682 mtx_unlock(&sc->sc_mtx);
2683#endif
2684 free(ccb, M_USBDEV);
2685 return;
2686 }
2687 xpt_setup_ccb(&ccb->ccb_h, path, 5 /* priority (low) */ );
2688 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2689 ccb->ccb_h.cbfcnp = &umass_cam_rescan_callback;
2690 ccb->crcn.flags = CAM_FLAG_NONE;
2691 xpt_action(ccb);
2692
2693#if (__FreeBSD_version >= 700037)
2694 mtx_unlock(&sc->sc_mtx);
2695#endif
2696
2697 /* The scan is in progress now. */
2698}
2699
2700static void
2701umass_cam_attach(struct umass_softc *sc)
2702{
2703#ifndef USB_DEBUG
2704 if (bootverbose)
2705#endif
2706 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2707 sc->sc_name, cam_sim_path(sc->sc_sim),
2708 sc->sc_unit, CAM_LUN_WILDCARD,
2709 cam_sim_path(sc->sc_sim));
2710
2711 if (!cold) {
2712 /*
2713 * Notify CAM of the new device after a short delay. Any
2714 * failure is benign, as the user can still do it by hand
2715 * (camcontrol rescan <busno>). Only do this if we are not
2716 * booting, because CAM does a scan after booting has
2717 * completed, when interrupts have been enabled.
2718 */
2719
2720 /* scan the new sim */
2721 umass_cam_rescan(sc);
2722 }
2723}
2724
2725/* umass_cam_detach
2726 * detach from the CAM layer
2727 */
2728
2729static void
2730umass_cam_detach_sim(struct umass_softc *sc)
2731{
2732 if (sc->sc_sim != NULL) {
2733 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2734 /* accessing the softc is not possible after this */
2735 sc->sc_sim->softc = UMASS_GONE;
2736 cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2737 } else {
2738 panic("%s: CAM layer is busy!\n",
2739 sc->sc_name);
2740 }
2741 sc->sc_sim = NULL;
2742 }
2743}
2744
2745/* umass_cam_action
2746 * CAM requests for action come through here
2747 */
2748
2749static void
2750umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2751{
2752 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2753
2754 if (sc == UMASS_GONE) {
2755 ccb->ccb_h.status = CAM_TID_INVALID;
2756 xpt_done(ccb);
2757 return;
2758 }
2759 if (sc) {
2760#if (__FreeBSD_version < 700037)
2761 mtx_lock(&sc->sc_mtx);
2762#endif
2763 }
2764 /*
2765 * Verify, depending on the operation to perform, that we either got
2766 * a valid sc, because an existing target was referenced, or
2767 * otherwise the SIM is addressed.
2768 *
2769 * This avoids bombing out at a printf and does give the CAM layer some
2770 * sensible feedback on errors.
2771 */
2772 switch (ccb->ccb_h.func_code) {
2773 case XPT_SCSI_IO:
2774 case XPT_RESET_DEV:
2775 case XPT_GET_TRAN_SETTINGS:
2776 case XPT_SET_TRAN_SETTINGS:
2777 case XPT_CALC_GEOMETRY:
2778 /* the opcodes requiring a target. These should never occur. */
2779 if (sc == NULL) {
2780 DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: "
2781 "Invalid target (target needed)\n",
2782 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2783 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2784 ccb->ccb_h.func_code);
2785
2786 ccb->ccb_h.status = CAM_TID_INVALID;
2787 xpt_done(ccb);
2788 goto done;
2789 }
2790 break;
2791 case XPT_PATH_INQ:
2792 case XPT_NOOP:
2793 /*
2794 * The opcodes sometimes aimed at a target (sc is valid),
2795 * sometimes aimed at the SIM (sc is invalid and target is
2796 * CAM_TARGET_WILDCARD)
2797 */
2798 if ((sc == NULL) &&
2799 (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) {
2800 DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: "
2801 "Invalid target (no wildcard)\n",
2802 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2803 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2804 ccb->ccb_h.func_code);
2805
2806 ccb->ccb_h.status = CAM_TID_INVALID;
2807 xpt_done(ccb);
2808 goto done;
2809 }
2810 break;
2811 default:
2812 /* XXX Hm, we should check the input parameters */
2813 break;
2814 }
2815
2816 /* Perform the requested action */
2817 switch (ccb->ccb_h.func_code) {
2818 case XPT_SCSI_IO:
2819 {
2820 uint8_t *cmd;
2821 uint8_t dir;
2822
2823 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2824 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2825 } else {
2826 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2827 }
2828
2829 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2830 "cmd: 0x%02x, flags: 0x%02x, "
2831 "%db cmd/%db data/%db sense\n",
2832 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2833 ccb->ccb_h.target_lun, cmd[0],
2834 ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2835 ccb->csio.dxfer_len, ccb->csio.sense_len);
2836
2837 if (sc->sc_transfer.ccb) {
2838 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2839 "I/O in progress, deferring\n",
2840 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2841 ccb->ccb_h.target_lun);
2842 ccb->ccb_h.status = CAM_SCSI_BUSY;
2843 xpt_done(ccb);
2844 goto done;
2845 }
2846 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2847 case CAM_DIR_IN:
2848 dir = DIR_IN;
2849 break;
2850 case CAM_DIR_OUT:
2851 dir = DIR_OUT;
2852 DIF(UDMASS_SCSI,
2853 umass_dump_buffer(sc, ccb->csio.data_ptr,
2854 ccb->csio.dxfer_len, 48));
2855 break;
2856 default:
2857 dir = DIR_NONE;
2858 }
2859
2860 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2861
2862 /*
2863 * sc->sc_transform will convert the command to the
2864 * command format needed by the specific command set
2865 * and return the converted command in
2866 * "sc->sc_transfer.cmd_data"
2867 */
2868 if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2869
2870 if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2871
2872 /*
2873 * Handle EVPD inquiry for broken devices first
2874 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2875 */
2876 if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2877 (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
2878 struct scsi_sense_data *sense;
2879
2880 sense = &ccb->csio.sense_data;
2881 bzero(sense, sizeof(*sense));
2882 sense->error_code = SSD_CURRENT_ERROR;
2883 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2884 sense->add_sense_code = 0x24;
2885 sense->extra_len = 10;
2886 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2887 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2888 CAM_AUTOSNS_VALID;
2889 xpt_done(ccb);
2890 goto done;
2891 }
2892 /*
2893 * Return fake inquiry data for
2894 * broken devices
2895 */
2896 if (sc->sc_quirks & NO_INQUIRY) {
2897 memcpy(ccb->csio.data_ptr, &fake_inq_data,
2898 sizeof(fake_inq_data));
2899 ccb->csio.scsi_status = SCSI_STATUS_OK;
2900 ccb->ccb_h.status = CAM_REQ_CMP;
2901 xpt_done(ccb);
2902 goto done;
2903 }
2904 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2905 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
2906 }
2907 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
2908 if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
2909 ccb->csio.scsi_status = SCSI_STATUS_OK;
2910 ccb->ccb_h.status = CAM_REQ_CMP;
2911 xpt_done(ccb);
2912 goto done;
2913 }
2914 }
2915 umass_command_start(sc, dir, ccb->csio.data_ptr,
2916 ccb->csio.dxfer_len,
2917 ccb->ccb_h.timeout,
2918 &umass_cam_cb, ccb);
2919 }
2920 break;
2921 }
2922 case XPT_PATH_INQ:
2923 {
2924 struct ccb_pathinq *cpi = &ccb->cpi;
2925
2926 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
2927 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2928 ccb->ccb_h.target_lun);
2929
2930 /* host specific information */
2931 cpi->version_num = 1;
2932 cpi->hba_inquiry = 0;
2933 cpi->target_sprt = 0;
2934 cpi->hba_misc = PIM_NO_6_BYTE;
2935 cpi->hba_eng_cnt = 0;
2936 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2937 cpi->initiator_id = UMASS_SCSIID_HOST;
2938 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2939 strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2940 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2941 cpi->unit_number = cam_sim_unit(sim);
2942 cpi->bus_id = sc->sc_unit;
2943#if (__FreeBSD_version >= 700025)
2944 cpi->protocol = PROTO_SCSI;
2945 cpi->protocol_version = SCSI_REV_2;
2946 cpi->transport = XPORT_USB;
2947 cpi->transport_version = 0;
2948#endif
2949 if (sc == NULL) {
2950 cpi->base_transfer_speed = 0;
2951 cpi->max_lun = 0;
2952 } else {
2953 if (sc->sc_quirks & FLOPPY_SPEED) {
2954 cpi->base_transfer_speed =
2955 UMASS_FLOPPY_TRANSFER_SPEED;
2956 } else if (usb2_get_speed(sc->sc_udev) ==
2957 USB_SPEED_HIGH) {
2958 cpi->base_transfer_speed =
2959 UMASS_HIGH_TRANSFER_SPEED;
2960 } else {
2961 cpi->base_transfer_speed =
2962 UMASS_FULL_TRANSFER_SPEED;
2963 }
2964 cpi->max_lun = sc->sc_maxlun;
2965 }
2966
2967 cpi->ccb_h.status = CAM_REQ_CMP;
2968 xpt_done(ccb);
2969 break;
2970 }
2971 case XPT_RESET_DEV:
2972 {
2973 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
2974 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2975 ccb->ccb_h.target_lun);
2976
2977 umass_reset(sc);
2978
2979 ccb->ccb_h.status = CAM_REQ_CMP;
2980 xpt_done(ccb);
2981 break;
2982 }
2983 case XPT_GET_TRAN_SETTINGS:
2984 {
2985 struct ccb_trans_settings *cts = &ccb->cts;
2986
2987 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2988 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2989 ccb->ccb_h.target_lun);
2990
2991#if (__FreeBSD_version >= 700025)
2992 cts->protocol = PROTO_SCSI;
2993 cts->protocol_version = SCSI_REV_2;
2994 cts->transport = XPORT_USB;
2995 cts->transport_version = 0;
2996 cts->xport_specific.valid = 0;
2997#else
2998 cts->valid = 0;
2999 cts->flags = 0; /* no disconnection, tagging */
3000#endif
3001 ccb->ccb_h.status = CAM_REQ_CMP;
3002 xpt_done(ccb);
3003 break;
3004 }
3005 case XPT_SET_TRAN_SETTINGS:
3006 {
3007 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3008 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3009 ccb->ccb_h.target_lun);
3010
3011 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3012 xpt_done(ccb);
3013 break;
3014 }
3015 case XPT_CALC_GEOMETRY:
3016 {
3017 cam_calc_geometry(&ccb->ccg, /* extended */ 1);
3018 xpt_done(ccb);
3019 break;
3020 }
3021 case XPT_NOOP:
3022 {
3023 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
3024 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3025 ccb->ccb_h.target_lun);
3026
3027 ccb->ccb_h.status = CAM_REQ_CMP;
3028 xpt_done(ccb);
3029 break;
3030 }
3031 default:
3032 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
3033 "Not implemented\n",
3034 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3035 ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
3036
3037 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3038 xpt_done(ccb);
3039 break;
3040 }
3041
3042done:
3043#if (__FreeBSD_version < 700037)
3044 if (sc) {
3045 mtx_unlock(&sc->sc_mtx);
3046 }
3047#endif
3048 return;
3049}
3050
3051static void
3052umass_cam_poll(struct cam_sim *sim)
3053{
3054 struct umass_softc *sc = (struct umass_softc *)sim->softc;
3055
3056 if (sc == UMASS_GONE)
3057 return;
3058
3059 DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
3060
3061 usb2_do_poll(sc->sc_xfer, UMASS_T_MAX);
3062}
3063
3064
3065/* umass_cam_cb
3066 * finalise a completed CAM command
3067 */
3068
3069static void
3070umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3071 uint8_t status)
3072{
3073 ccb->csio.resid = residue;
3074
3075 switch (status) {
3076 case STATUS_CMD_OK:
3077 ccb->ccb_h.status = CAM_REQ_CMP;
3078 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
3079 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3080 (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3081 struct scsi_read_capacity_data *rcap;
3082 uint32_t maxsector;
3083
3084 rcap = (void *)(ccb->csio.data_ptr);
3085 maxsector = scsi_4btoul(rcap->addr) - 1;
3086 scsi_ulto4b(maxsector, rcap->addr);
3087 }
3088 xpt_done(ccb);
3089 break;
3090
3091 case STATUS_CMD_UNKNOWN:
3092 case STATUS_CMD_FAILED:
3093
3094 /* fetch sense data */
3095
3096 /* the rest of the command was filled in at attach */
3097 sc->cam_scsi_sense.length = ccb->csio.sense_len;
3098
3099 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
3100 "sense data\n", ccb->csio.sense_len);
3101
3102 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
3103 sizeof(sc->cam_scsi_sense))) {
3104
3105 if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
3106 (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
3107 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
3108 }
3109 umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
3110 ccb->csio.sense_len, ccb->ccb_h.timeout,
3111 &umass_cam_sense_cb, ccb);
3112 }
3113 break;
3114
3115 default:
3116 /*
3117 * the wire protocol failed and will have recovered
3118 * (hopefully). We return an error to CAM and let CAM retry
3119 * the command if necessary.
3120 */
3121 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3122 xpt_done(ccb);
3123 break;
3124 }
3125}
3126
3127/*
3128 * Finalise a completed autosense operation
3129 */
3130static void
3131umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3132 uint8_t status)
3133{
3134 uint8_t *cmd;
3135 uint8_t key;
3136
3137 switch (status) {
3138 case STATUS_CMD_OK:
3139 case STATUS_CMD_UNKNOWN:
3140 case STATUS_CMD_FAILED:
3141
3142 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
3143 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
3144 } else {
3145 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
3146 }
3147
3148 key = (ccb->csio.sense_data.flags & SSD_KEY);
3149
3150 /*
3151 * Getting sense data always succeeds (apart from wire
3152 * failures):
3153 */
3154 if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3155 (cmd[0] == INQUIRY) &&
3156 (key == SSD_KEY_UNIT_ATTENTION)) {
3157 /*
3158 * Ignore unit attention errors in the case where
3159 * the Unit Attention state is not cleared on
3160 * REQUEST SENSE. They will appear again at the next
3161 * command.
3162 */
3163 ccb->ccb_h.status = CAM_REQ_CMP;
3164 } else if (key == SSD_KEY_NO_SENSE) {
3165 /*
3166 * No problem after all (in the case of CBI without
3167 * CCI)
3168 */
3169 ccb->ccb_h.status = CAM_REQ_CMP;
3170 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3171 (cmd[0] == READ_CAPACITY) &&
3172 (key == SSD_KEY_UNIT_ATTENTION)) {
3173 /*
3174 * Some devices do not clear the unit attention error
3175 * on request sense. We insert a test unit ready
3176 * command to make sure we clear the unit attention
3177 * condition, then allow the retry to proceed as
3178 * usual.
3179 */
3180
3181 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3182 | CAM_AUTOSNS_VALID;
3183 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3184
3185#if 0
3186 DELAY(300000);
3187#endif
3188 DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
3189 "TEST_UNIT_READY\n");
3190
3191 /* the rest of the command was filled in at attach */
3192
3193 if (umass_std_transform(sc, ccb,
3194 &sc->cam_scsi_test_unit_ready.opcode,
3195 sizeof(sc->cam_scsi_test_unit_ready))) {
3196 umass_command_start(sc, DIR_NONE, NULL, 0,
3197 ccb->ccb_h.timeout,
3198 &umass_cam_quirk_cb, ccb);
3199 }
3200 break;
3201 } else {
3202 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3203 | CAM_AUTOSNS_VALID;
3204 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3205 }
3206 xpt_done(ccb);
3207 break;
3208
3209 default:
3210 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
3211 "status %d\n", status);
3212 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3213 xpt_done(ccb);
3214 }
3215}
3216
3217/*
3218 * This completion code just handles the fact that we sent a test-unit-ready
3219 * after having previously failed a READ CAPACITY with CHECK_COND. Even
3220 * though this command succeeded, we have to tell CAM to retry.
3221 */
3222static void
3223umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3224 uint8_t status)
3225{
3226 DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
3227 "returned status %d\n", status);
3228
3229 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3230 | CAM_AUTOSNS_VALID;
3231 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3232 xpt_done(ccb);
3233}
3234
3235/*
3236 * SCSI specific functions
3237 */
3238
3239static uint8_t
3240umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3241 uint8_t cmd_len)
3242{
3243 if ((cmd_len == 0) ||
3244 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3245 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3246 "length: %d bytes\n", cmd_len);
3247 return (0); /* failure */
3248 }
3249 sc->sc_transfer.cmd_len = cmd_len;
3250
3251 switch (cmd_ptr[0]) {
3252 case TEST_UNIT_READY:
3253 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3254 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3255 "to START_UNIT\n");
3256 bzero(sc->sc_transfer.cmd_data, cmd_len);
3257 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3258 sc->sc_transfer.cmd_data[4] = SSS_START;
3259 return (1);
3260 }
3261 break;
3262
3263 case INQUIRY:
3264 /*
3265 * some drives wedge when asked for full inquiry
3266 * information.
3267 */
3268 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3269 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3270 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3271 return (1);
3272 }
3273 break;
3274 }
3275
3276 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3277 return (1);
3278}
3279
3280static uint8_t
3281umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
3282{
3283 if ((cmd_len == 0) ||
3284 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3285 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3286 "length: %d bytes\n", cmd_len);
3287 return (0); /* failure */
3288 }
3289 switch (cmd_ptr[0]) {
3290 /* these commands are defined in RBC: */
3291 case READ_10:
3292 case READ_CAPACITY:
3293 case START_STOP_UNIT:
3294 case SYNCHRONIZE_CACHE:
3295 case WRITE_10:
3296 case 0x2f: /* VERIFY_10 is absent from
3297 * scsi_all.h??? */
3298 case INQUIRY:
3299 case MODE_SELECT_10:
3300 case MODE_SENSE_10:
3301 case TEST_UNIT_READY:
3302 case WRITE_BUFFER:
3303 /*
3304 * The following commands are not listed in my copy of the
3305 * RBC specs. CAM however seems to want those, and at least
3306 * the Sony DSC device appears to support those as well
3307 */
3308 case REQUEST_SENSE:
3309 case PREVENT_ALLOW:
3310
3311 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3312
3313 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
3314 bzero(sc->sc_transfer.cmd_data + cmd_len, 12 - cmd_len);
3315 cmd_len = 12;
3316 }
3317 sc->sc_transfer.cmd_len = cmd_len;
3318 return (1); /* sucess */
3319
3320 /* All other commands are not legal in RBC */
3321 default:
3322 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
3323 "command 0x%02x\n", cmd_ptr[0]);
3324 return (0); /* failure */
3325 }
3326}
3327
3328static uint8_t
3329umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3330 uint8_t cmd_len)
3331{
3332 if ((cmd_len == 0) ||
3333 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3334 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3335 "length: %d bytes\n", cmd_len);
3336 return (0); /* failure */
3337 }
3338 /* An UFI command is always 12 bytes in length */
3339 sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
3340
3341 /* Zero the command data */
3342 bzero(sc->sc_transfer.cmd_data, UFI_COMMAND_LENGTH);
3343
3344 switch (cmd_ptr[0]) {
3345 /*
3346 * Commands of which the format has been verified. They
3347 * should work. Copy the command into the (zeroed out)
3348 * destination buffer.
3349 */
3350 case TEST_UNIT_READY:
3351 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3352 /*
3353 * Some devices do not support this command. Start
3354 * Stop Unit should give the same results
3355 */
3356 DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
3357 "to START_UNIT\n");
3358
3359 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3360 sc->sc_transfer.cmd_data[4] = SSS_START;
3361 return (1);
3362 }
3363 break;
3364
3365 case REZERO_UNIT:
3366 case REQUEST_SENSE:
3367 case FORMAT_UNIT:
3368 case INQUIRY:
3369 case START_STOP_UNIT:
3370 case SEND_DIAGNOSTIC:
3371 case PREVENT_ALLOW:
3372 case READ_CAPACITY:
3373 case READ_10:
3374 case WRITE_10:
3375 case POSITION_TO_ELEMENT: /* SEEK_10 */
3376 case WRITE_AND_VERIFY:
3377 case VERIFY:
3378 case MODE_SELECT_10:
3379 case MODE_SENSE_10:
3380 case READ_12:
3381 case WRITE_12:
3382 case READ_FORMAT_CAPACITIES:
3383 break;
3384
3385 /*
3386 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
3387 * required for UFI devices, so it is appropriate to fake
3388 * success.
3389 */
3390 case SYNCHRONIZE_CACHE:
3391 return (2);
3392
3393 default:
3394 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
3395 "command 0x%02x\n", cmd_ptr[0]);
3396 return (0); /* failure */
3397 }
3398
3399 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3400 return (1); /* success */
3401}
3402
3403/*
3404 * 8070i (ATAPI) specific functions
3405 */
3406static uint8_t
3407umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3408 uint8_t cmd_len)
3409{
3410 if ((cmd_len == 0) ||
3411 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3412 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3413 "length: %d bytes\n", cmd_len);
3414 return (0); /* failure */
3415 }
3416 /* An ATAPI command is always 12 bytes in length. */
3417 sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
3418
3419 /* Zero the command data */
3420 bzero(sc->sc_transfer.cmd_data, ATAPI_COMMAND_LENGTH);
3421
3422 switch (cmd_ptr[0]) {
3423 /*
3424 * Commands of which the format has been verified. They
3425 * should work. Copy the command into the destination
3426 * buffer.
3427 */
3428 case INQUIRY:
3429 /*
3430 * some drives wedge when asked for full inquiry
3431 * information.
3432 */
3433 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3434 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3435
3436 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3437 return (1);
3438 }
3439 break;
3440
3441 case TEST_UNIT_READY:
3442 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3443 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3444 "to START_UNIT\n");
3445 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3446 sc->sc_transfer.cmd_data[4] = SSS_START;
3447 return (1);
3448 }
3449 break;
3450
3451 case REZERO_UNIT:
3452 case REQUEST_SENSE:
3453 case START_STOP_UNIT:
3454 case SEND_DIAGNOSTIC:
3455 case PREVENT_ALLOW:
3456 case READ_CAPACITY:
3457 case READ_10:
3458 case WRITE_10:
3459 case POSITION_TO_ELEMENT: /* SEEK_10 */
3460 case SYNCHRONIZE_CACHE:
3461 case MODE_SELECT_10:
3462 case MODE_SENSE_10:
3463 case READ_BUFFER:
3464 case 0x42: /* READ_SUBCHANNEL */
3465 case 0x43: /* READ_TOC */
3466 case 0x44: /* READ_HEADER */
3467 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */
3468 case 0x48: /* PLAY_TRACK */
3469 case 0x49: /* PLAY_TRACK_REL */
3470 case 0x4b: /* PAUSE */
3471 case 0x51: /* READ_DISK_INFO */
3472 case 0x52: /* READ_TRACK_INFO */
3473 case 0x54: /* SEND_OPC */
3474 case 0x59: /* READ_MASTER_CUE */
3475 case 0x5b: /* CLOSE_TR_SESSION */
3476 case 0x5c: /* READ_BUFFER_CAP */
3477 case 0x5d: /* SEND_CUE_SHEET */
3478 case 0xa1: /* BLANK */
3479 case 0xa5: /* PLAY_12 */
3480 case 0xa6: /* EXCHANGE_MEDIUM */
3481 case 0xad: /* READ_DVD_STRUCTURE */
3482 case 0xbb: /* SET_CD_SPEED */
3483 case 0xe5: /* READ_TRACK_INFO_PHILIPS */
3484 break;;
3485
3486 case READ_12:
3487 case WRITE_12:
3488 default:
3489 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
3490 "command 0x%02x - trying anyway\n",
3491 cmd_ptr[0]);
3492 break;;
3493 }
3494
3495 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3496 return (1); /* success */
3497}
3498
3499static uint8_t
3500umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
3501 uint8_t cmdlen)
3502{
3503 return (0); /* failure */
3504}
3505
3506static uint8_t
3507umass_std_transform(struct umass_softc *sc, union ccb *ccb,
3508 uint8_t *cmd, uint8_t cmdlen)
3509{
3510 uint8_t retval;
3511
3512 retval = (sc->sc_transform) (sc, cmd, cmdlen);
3513
3514 if (retval == 2) {
3515 ccb->ccb_h.status = CAM_REQ_CMP;
3516 xpt_done(ccb);
3517 return (0);
3518 } else if (retval == 0) {
3519 ccb->ccb_h.status = CAM_REQ_INVALID;
3520 xpt_done(ccb);
3521 return (0);
3522 }
3523 /* Command should be executed */
3524 return (1);
3525}
3526
3527#if USB_DEBUG
3528static void
3529umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3530{
3531 uint8_t *c = cbw->CBWCDB;
3532
3533 uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
3534 uint32_t tag = UGETDW(cbw->dCBWTag);
3535
3536 uint8_t clen = cbw->bCDBLength;
3537 uint8_t flags = cbw->bCBWFlags;
3538 uint8_t lun = cbw->bCBWLUN;
3539
3540 DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
3541 "(0x%02x%02x%02x%02x%02x%02x%s), "
3542 "data = %db, lun = %d, dir = %s\n",
3543 tag, clen,
3544 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
3545 dlen, lun, (flags == CBWFLAGS_IN ? "in" :
3546 (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
3547}
3548
3549static void
3550umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3551{
3552 uint32_t sig = UGETDW(csw->dCSWSignature);
3553 uint32_t tag = UGETDW(csw->dCSWTag);
3554 uint32_t res = UGETDW(csw->dCSWDataResidue);
3555 uint8_t status = csw->bCSWStatus;
3556
3557 DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
3558 "res = %d, status = 0x%02x (%s)\n",
3559 tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
3560 tag, res,
3561 status, (status == CSWSTATUS_GOOD ? "good" :
3562 (status == CSWSTATUS_FAILED ? "failed" :
3563 (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
3564}
3565
3566static void
3567umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
3568{
3569 uint8_t *c = cmd;
3570 uint8_t dir = sc->sc_transfer.dir;
3571
3572 DPRINTF(sc, UDMASS_BBB, "cmd = %db "
3573 "(0x%02x%02x%02x%02x%02x%02x%s), "
3574 "data = %db, dir = %s\n",
3575 cmdlen,
3576 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
3577 sc->sc_transfer.data_len,
3578 (dir == DIR_IN ? "in" :
3579 (dir == DIR_OUT ? "out" :
3580 (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3581}
3582
3583static void
3584umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3585 uint32_t printlen)
3586{
3587 uint32_t i, j;
3588 char s1[40];
3589 char s2[40];
3590 char s3[5];
3591
3592 s1[0] = '\0';
3593 s3[0] = '\0';
3594
3595 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3596 for (i = 0; (i < buflen) && (i < printlen); i++) {
3597 j = i % 16;
3598 if (j == 0 && i != 0) {
3599 DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3600 s1, s2);
3601 s2[0] = '\0';
3602 }
3603 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3604 }
3605 if (buflen > printlen)
3606 sprintf(s3, " ...");
3607 DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",
3608 s1, s2, s3);
3609}
3610
3611#endif
614 },
615 {USB_VENDOR_MYSON, USB_PRODUCT_MYSON_STARREADER, RID_WILDCARD,
616 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
617 NO_SYNCHRONIZE_CACHE
618 },
619 {USB_VENDOR_NEODIO, USB_PRODUCT_NEODIO_ND3260, RID_WILDCARD,
620 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
621 FORCE_SHORT_INQUIRY
622 },
623 {USB_VENDOR_NETAC, USB_PRODUCT_NETAC_CF_CARD, RID_WILDCARD,
624 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
625 NO_INQUIRY
626 },
627 {USB_VENDOR_NETAC, USB_PRODUCT_NETAC_ONLYDISK, RID_WILDCARD,
628 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
629 IGNORE_RESIDUE
630 },
631 {USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_CLIK_40, RID_WILDCARD,
632 UMASS_PROTO_ATAPI,
633 NO_INQUIRY
634 },
635 {USB_VENDOR_NIKON, USB_PRODUCT_NIKON_D300, RID_WILDCARD,
636 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
637 NO_QUIRKS
638 },
639 {USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C1, RID_WILDCARD,
640 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
641 WRONG_CSWSIG
642 },
643 {USB_VENDOR_OLYMPUS, USB_PRODUCT_OLYMPUS_C700, RID_WILDCARD,
644 UMASS_PROTO_SCSI,
645 NO_GETMAXLUN
646 },
647 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_SDS_HOTFIND_D, RID_WILDCARD,
648 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
649 NO_GETMAXLUN | NO_SYNCHRONIZE_CACHE
650 },
651 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFMS_RW, RID_WILDCARD,
652 UMASS_PROTO_SCSI,
653 NO_QUIRKS
654 },
655 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_COMBO, RID_WILDCARD,
656 UMASS_PROTO_SCSI,
657 NO_QUIRKS
658 },
659 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER, RID_WILDCARD,
660 UMASS_PROTO_SCSI,
661 NO_QUIRKS
662 },
663 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_CFSM_READER2, RID_WILDCARD,
664 UMASS_PROTO_SCSI,
665 NO_QUIRKS
666 },
667 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDCFE_B_CF_READER, RID_WILDCARD,
668 UMASS_PROTO_SCSI,
669 NO_QUIRKS
670 },
671 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_MDSM_B_READER, RID_WILDCARD,
672 UMASS_PROTO_SCSI,
673 NO_INQUIRY
674 },
675 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_READER, RID_WILDCARD,
676 UMASS_PROTO_SCSI,
677 NO_QUIRKS
678 },
679 {USB_VENDOR_ONSPEC, USB_PRODUCT_ONSPEC_UCF100, RID_WILDCARD,
680 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
681 NO_INQUIRY | NO_GETMAXLUN
682 },
683 {USB_VENDOR_ONSPEC2, USB_PRODUCT_ONSPEC2_IMAGEMATE_SDDR55, RID_WILDCARD,
684 UMASS_PROTO_SCSI,
685 NO_GETMAXLUN
686 },
687 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXL840AN, RID_WILDCARD,
688 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
689 NO_GETMAXLUN
690 },
691 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB20AN, RID_WILDCARD,
692 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
693 NO_QUIRKS
694 },
695 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_KXLCB35AN, RID_WILDCARD,
696 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
697 NO_QUIRKS
698 },
699 {USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_LS120CAM, RID_WILDCARD,
700 UMASS_PROTO_UFI,
701 NO_QUIRKS
702 },
703 {USB_VENDOR_PLEXTOR, USB_PRODUCT_PLEXTOR_40_12_40U, RID_WILDCARD,
704 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
705 NO_TEST_UNIT_READY
706 },
707 {USB_VENDOR_PNY, USB_PRODUCT_PNY_ATTACHE2, RID_WILDCARD,
708 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
709 IGNORE_RESIDUE | NO_START_STOP
710 },
711 {USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_YP_U2, RID_WILDCARD,
712 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
713 SHUTTLE_INIT | NO_GETMAXLUN
714 },
715 {USB_VENDOR_SAMSUNG_TECHWIN, USB_PRODUCT_SAMSUNG_TECHWIN_DIGIMAX_410, RID_WILDCARD,
716 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
717 NO_INQUIRY
718 },
719 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR05A, RID_WILDCARD,
720 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
721 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
722 },
723 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR09, RID_WILDCARD,
724 UMASS_PROTO_SCSI,
725 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
726 },
727 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR12, RID_WILDCARD,
728 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
729 READ_CAPACITY_OFFBY1 | NO_GETMAXLUN
730 },
731 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ2_256, RID_WILDCARD,
732 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
733 IGNORE_RESIDUE
734 },
735 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_128, RID_WILDCARD,
736 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
737 IGNORE_RESIDUE
738 },
739 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDCZ4_256, RID_WILDCARD,
740 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
741 IGNORE_RESIDUE
742 },
743 {USB_VENDOR_SANDISK, USB_PRODUCT_SANDISK_SDDR31, RID_WILDCARD,
744 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
745 READ_CAPACITY_OFFBY1
746 },
747 {USB_VENDOR_SCANLOGIC, USB_PRODUCT_SCANLOGIC_SL11R, RID_WILDCARD,
748 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
749 NO_INQUIRY
750 },
751 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSB, RID_WILDCARD,
752 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
753 NO_TEST_UNIT_READY | NO_START_STOP | SHUTTLE_INIT
754 },
755 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CDRW, RID_WILDCARD,
756 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
757 NO_QUIRKS
758 },
759 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_CF, RID_WILDCARD,
760 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
761 NO_QUIRKS
762 },
763 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBATAPI, RID_WILDCARD,
764 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
765 NO_QUIRKS
766 },
767 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSBCFSM, RID_WILDCARD,
768 UMASS_PROTO_SCSI,
769 NO_QUIRKS
770 },
771 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_EUSCSI, RID_WILDCARD,
772 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
773 NO_QUIRKS
774 },
775 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_HIFD, RID_WILDCARD,
776 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
777 NO_GETMAXLUN
778 },
779 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_SDDR09, RID_WILDCARD,
780 UMASS_PROTO_SCSI,
781 NO_GETMAXLUN
782 },
783 {USB_VENDOR_SHUTTLE, USB_PRODUCT_SHUTTLE_ZIOMMC, RID_WILDCARD,
784 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
785 NO_GETMAXLUN
786 },
787 {USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_I_BEAD100, RID_WILDCARD,
788 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
789 SHUTTLE_INIT
790 },
791 {USB_VENDOR_SIIG, USB_PRODUCT_SIIG_WINTERREADER, RID_WILDCARD,
792 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
793 IGNORE_RESIDUE
794 },
795 {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_MD_7425, RID_WILDCARD,
796 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
797 NO_INQUIRY
798 },
799 {USB_VENDOR_SKANHEX, USB_PRODUCT_SKANHEX_SX_520Z, RID_WILDCARD,
800 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
801 NO_INQUIRY
802 },
803 {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, 0x0500,
804 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
805 RBC_PAD_TO_12
806 },
807 {USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40_MS, RID_WILDCARD,
808 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
809 NO_INQUIRY
810 },
811 {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0500,
812 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
813 RBC_PAD_TO_12
814 },
815 {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, 0x0600,
816 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
817 RBC_PAD_TO_12
818 },
819 {USB_VENDOR_SONY, USB_PRODUCT_SONY_DSC, RID_WILDCARD,
820 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
821 NO_QUIRKS
822 },
823 {USB_VENDOR_SONY, USB_PRODUCT_SONY_HANDYCAM, RID_WILDCARD,
824 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
825 NO_QUIRKS
826 },
827 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSC, RID_WILDCARD,
828 UMASS_PROTO_RBC | UMASS_PROTO_CBI,
829 NO_QUIRKS
830 },
831 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_MSC_U03, RID_WILDCARD,
832 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
833 NO_GETMAXLUN
834 },
835 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_NW_MS7, RID_WILDCARD,
836 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
837 NO_GETMAXLUN
838 },
839 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MS_PEG_N760C, RID_WILDCARD,
840 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
841 NO_INQUIRY
842 },
843 {USB_VENDOR_SONY, USB_PRODUCT_SONY_MSACUS1, RID_WILDCARD,
844 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
845 NO_GETMAXLUN
846 },
847 {USB_VENDOR_SONY, USB_PRODUCT_SONY_PORTABLE_HDD_V2, RID_WILDCARD,
848 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
849 NO_QUIRKS
850 },
851 {USB_VENDOR_SUPERTOP, USB_PRODUCT_SUPERTOP_IDE, RID_WILDCARD,
852 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
853 IGNORE_RESIDUE | NO_SYNCHRONIZE_CACHE
854 },
855 {USB_VENDOR_TAUGA, USB_PRODUCT_TAUGA_CAMERAMATE, RID_WILDCARD,
856 UMASS_PROTO_SCSI,
857 NO_QUIRKS
858 },
859 {USB_VENDOR_TEAC, USB_PRODUCT_TEAC_FD05PUB, RID_WILDCARD,
860 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
861 NO_QUIRKS
862 },
863 {USB_VENDOR_TREK, USB_PRODUCT_TREK_MEMKEY, RID_WILDCARD,
864 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
865 NO_INQUIRY
866 },
867 {USB_VENDOR_TREK, USB_PRODUCT_TREK_THUMBDRIVE_8MB, RID_WILDCARD,
868 UMASS_PROTO_ATAPI | UMASS_PROTO_BBB,
869 IGNORE_RESIDUE
870 },
871 {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_C3310, RID_WILDCARD,
872 UMASS_PROTO_UFI | UMASS_PROTO_CBI,
873 NO_QUIRKS
874 },
875 {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_MP3, RID_WILDCARD,
876 UMASS_PROTO_RBC,
877 NO_QUIRKS
878 },
879 {USB_VENDOR_TRUMPION, USB_PRODUCT_TRUMPION_T33520, RID_WILDCARD,
880 UMASS_PROTO_SCSI,
881 NO_QUIRKS
882 },
883 {USB_VENDOR_TWINMOS, USB_PRODUCT_TWINMOS_MDIV, RID_WILDCARD,
884 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
885 NO_QUIRKS
886 },
887 {USB_VENDOR_VIA, USB_PRODUCT_VIA_USB2IDEBRIDGE, RID_WILDCARD,
888 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
889 NO_SYNCHRONIZE_CACHE
890 },
891 {USB_VENDOR_VIVITAR, USB_PRODUCT_VIVITAR_35XX, RID_WILDCARD,
892 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
893 NO_INQUIRY
894 },
895 {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_COMBO, RID_WILDCARD,
896 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
897 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
898 },
899 {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_EXTHDD, RID_WILDCARD,
900 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
901 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
902 },
903 {USB_VENDOR_WESTERN, USB_PRODUCT_WESTERN_MYBOOK, RID_WILDCARD,
904 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
905 NO_INQUIRY_EVPD
906 },
907 {USB_VENDOR_WINMAXGROUP, USB_PRODUCT_WINMAXGROUP_FLASH64MC, RID_WILDCARD,
908 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
909 NO_INQUIRY
910 },
911 {USB_VENDOR_YANO, USB_PRODUCT_YANO_FW800HD, RID_WILDCARD,
912 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
913 FORCE_SHORT_INQUIRY | NO_START_STOP | IGNORE_RESIDUE
914 },
915 {USB_VENDOR_YANO, USB_PRODUCT_YANO_U640MO, RID_WILDCARD,
916 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I,
917 FORCE_SHORT_INQUIRY
918 },
919 {USB_VENDOR_YEDATA, USB_PRODUCT_YEDATA_FLASHBUSTERU, RID_WILDCARD,
920 UMASS_PROTO_SCSI | UMASS_PROTO_CBI,
921 NO_GETMAXLUN
922 },
923 {USB_VENDOR_ZORAN, USB_PRODUCT_ZORAN_EX20DSC, RID_WILDCARD,
924 UMASS_PROTO_ATAPI | UMASS_PROTO_CBI,
925 NO_QUIRKS
926 },
927 {USB_VENDOR_MEIZU, USB_PRODUCT_MEIZU_M6_SL, RID_WILDCARD,
928 UMASS_PROTO_SCSI | UMASS_PROTO_BBB,
929 NO_INQUIRY | NO_SYNCHRONIZE_CACHE
930 },
931 {VID_EOT, PID_EOT, RID_EOT, 0, 0}
932};
933
934struct umass_softc {
935
936 struct scsi_sense cam_scsi_sense;
937 struct scsi_test_unit_ready cam_scsi_test_unit_ready;
938 struct mtx sc_mtx;
939 struct {
940 uint8_t *data_ptr;
941 union ccb *ccb;
942 umass_callback_t *callback;
943
944 uint32_t data_len; /* bytes */
945 uint32_t data_rem; /* bytes */
946 uint32_t data_timeout; /* ms */
947 uint32_t actlen; /* bytes */
948
949 uint8_t cmd_data[UMASS_MAX_CMDLEN];
950 uint8_t cmd_len; /* bytes */
951 uint8_t dir;
952 uint8_t lun;
953 } sc_transfer;
954
955 /* Bulk specific variables for transfers in progress */
956 umass_bbb_cbw_t cbw; /* command block wrapper */
957 umass_bbb_csw_t csw; /* command status wrapper */
958
959 /* CBI specific variables for transfers in progress */
960 umass_cbi_sbl_t sbl; /* status block */
961
962 device_t sc_dev;
963 struct usb2_device *sc_udev;
964 struct cam_sim *sc_sim; /* SCSI Interface Module */
965 struct usb2_xfer *sc_xfer[UMASS_T_MAX];
966
967 /*
968 * The command transform function is used to convert the SCSI
969 * commands into their derivatives, like UFI, ATAPI, and friends.
970 */
971 umass_transform_t *sc_transform;
972
973 uint32_t sc_unit;
974
975 uint16_t sc_proto; /* wire and cmd protocol */
976 uint16_t sc_quirks; /* they got it almost right */
977
978 uint8_t sc_name[16];
979 uint8_t sc_iface_no; /* interface number */
980 uint8_t sc_maxlun; /* maximum LUN number, inclusive */
981 uint8_t sc_last_xfer_index;
982 uint8_t sc_status_try;
983};
984
985struct umass_probe_proto {
986 uint16_t quirks;
987 uint16_t proto;
988
989 int32_t error;
990};
991
992/* prototypes */
993
994static device_probe_t umass_probe;
995static device_attach_t umass_attach;
996static device_detach_t umass_detach;
997
998static usb2_callback_t umass_tr_error;
999static usb2_callback_t umass_t_bbb_reset1_callback;
1000static usb2_callback_t umass_t_bbb_reset2_callback;
1001static usb2_callback_t umass_t_bbb_reset3_callback;
1002static usb2_callback_t umass_t_bbb_command_callback;
1003static usb2_callback_t umass_t_bbb_data_read_callback;
1004static usb2_callback_t umass_t_bbb_data_rd_cs_callback;
1005static usb2_callback_t umass_t_bbb_data_write_callback;
1006static usb2_callback_t umass_t_bbb_data_wr_cs_callback;
1007static usb2_callback_t umass_t_bbb_status_callback;
1008static usb2_callback_t umass_t_cbi_reset1_callback;
1009static usb2_callback_t umass_t_cbi_reset2_callback;
1010static usb2_callback_t umass_t_cbi_reset3_callback;
1011static usb2_callback_t umass_t_cbi_reset4_callback;
1012static usb2_callback_t umass_t_cbi_command_callback;
1013static usb2_callback_t umass_t_cbi_data_read_callback;
1014static usb2_callback_t umass_t_cbi_data_rd_cs_callback;
1015static usb2_callback_t umass_t_cbi_data_write_callback;
1016static usb2_callback_t umass_t_cbi_data_wr_cs_callback;
1017static usb2_callback_t umass_t_cbi_status_callback;
1018
1019static void umass_cancel_ccb(struct umass_softc *);
1020static void umass_init_shuttle(struct umass_softc *);
1021static void umass_reset(struct umass_softc *);
1022static void umass_t_bbb_data_clear_stall_callback(struct usb2_xfer *,
1023 uint8_t, uint8_t);
1024static void umass_command_start(struct umass_softc *, uint8_t, void *,
1025 uint32_t, uint32_t, umass_callback_t *, union ccb *);
1026static uint8_t umass_bbb_get_max_lun(struct umass_softc *);
1027static void umass_cbi_start_status(struct umass_softc *);
1028static void umass_t_cbi_data_clear_stall_callback(struct usb2_xfer *,
1029 uint8_t, uint8_t);
1030static int umass_cam_attach_sim(struct umass_softc *);
1031static void umass_cam_rescan_callback(struct cam_periph *, union ccb *);
1032static void umass_cam_rescan(struct umass_softc *);
1033static void umass_cam_attach(struct umass_softc *);
1034static void umass_cam_detach_sim(struct umass_softc *);
1035static void umass_cam_action(struct cam_sim *, union ccb *);
1036static void umass_cam_poll(struct cam_sim *);
1037static void umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
1038 uint8_t);
1039static void umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
1040 uint8_t);
1041static void umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
1042 uint8_t);
1043static uint8_t umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
1044static uint8_t umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
1045static uint8_t umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
1046static uint8_t umass_atapi_transform(struct umass_softc *, uint8_t *,
1047 uint8_t);
1048static uint8_t umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
1049static uint8_t umass_std_transform(struct umass_softc *, union ccb *, uint8_t
1050 *, uint8_t);
1051
1052#if USB_DEBUG
1053static void umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
1054static void umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
1055static void umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
1056static void umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
1057 uint32_t);
1058#endif
1059
1060struct usb2_config umass_bbb_config[UMASS_T_BBB_MAX] = {
1061
1062 [UMASS_T_BBB_RESET1] = {
1063 .type = UE_CONTROL,
1064 .endpoint = 0x00, /* Control pipe */
1065 .direction = UE_DIR_ANY,
1066 .mh.bufsize = sizeof(struct usb2_device_request),
1067 .mh.callback = &umass_t_bbb_reset1_callback,
1068 .mh.timeout = 5000, /* 5 seconds */
1069 .mh.interval = 500, /* 500 milliseconds */
1070 },
1071
1072 [UMASS_T_BBB_RESET2] = {
1073 .type = UE_CONTROL,
1074 .endpoint = 0x00, /* Control pipe */
1075 .direction = UE_DIR_ANY,
1076 .mh.bufsize = sizeof(struct usb2_device_request),
1077 .mh.callback = &umass_t_bbb_reset2_callback,
1078 .mh.timeout = 5000, /* 5 seconds */
1079 .mh.interval = 50, /* 50 milliseconds */
1080 },
1081
1082 [UMASS_T_BBB_RESET3] = {
1083 .type = UE_CONTROL,
1084 .endpoint = 0x00, /* Control pipe */
1085 .direction = UE_DIR_ANY,
1086 .mh.bufsize = sizeof(struct usb2_device_request),
1087 .mh.callback = &umass_t_bbb_reset3_callback,
1088 .mh.timeout = 5000, /* 5 seconds */
1089 .mh.interval = 50, /* 50 milliseconds */
1090 },
1091
1092 [UMASS_T_BBB_COMMAND] = {
1093 .type = UE_BULK,
1094 .endpoint = UE_ADDR_ANY,
1095 .direction = UE_DIR_OUT,
1096 .mh.bufsize = sizeof(umass_bbb_cbw_t),
1097 .mh.callback = &umass_t_bbb_command_callback,
1098 .mh.timeout = 5000, /* 5 seconds */
1099 },
1100
1101 [UMASS_T_BBB_DATA_READ] = {
1102 .type = UE_BULK,
1103 .endpoint = UE_ADDR_ANY,
1104 .direction = UE_DIR_IN,
1105 .mh.bufsize = UMASS_BULK_SIZE,
1106 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1107 .mh.callback = &umass_t_bbb_data_read_callback,
1108 .mh.timeout = 0, /* overwritten later */
1109 },
1110
1111 [UMASS_T_BBB_DATA_RD_CS] = {
1112 .type = UE_CONTROL,
1113 .endpoint = 0x00, /* Control pipe */
1114 .direction = UE_DIR_ANY,
1115 .mh.bufsize = sizeof(struct usb2_device_request),
1116 .mh.callback = &umass_t_bbb_data_rd_cs_callback,
1117 .mh.timeout = 5000, /* 5 seconds */
1118 },
1119
1120 [UMASS_T_BBB_DATA_WRITE] = {
1121 .type = UE_BULK,
1122 .endpoint = UE_ADDR_ANY,
1123 .direction = UE_DIR_OUT,
1124 .mh.bufsize = UMASS_BULK_SIZE,
1125 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1126 .mh.callback = &umass_t_bbb_data_write_callback,
1127 .mh.timeout = 0, /* overwritten later */
1128 },
1129
1130 [UMASS_T_BBB_DATA_WR_CS] = {
1131 .type = UE_CONTROL,
1132 .endpoint = 0x00, /* Control pipe */
1133 .direction = UE_DIR_ANY,
1134 .mh.bufsize = sizeof(struct usb2_device_request),
1135 .mh.callback = &umass_t_bbb_data_wr_cs_callback,
1136 .mh.timeout = 5000, /* 5 seconds */
1137 },
1138
1139 [UMASS_T_BBB_STATUS] = {
1140 .type = UE_BULK,
1141 .endpoint = UE_ADDR_ANY,
1142 .direction = UE_DIR_IN,
1143 .mh.bufsize = sizeof(umass_bbb_csw_t),
1144 .mh.flags = {.short_xfer_ok = 1,},
1145 .mh.callback = &umass_t_bbb_status_callback,
1146 .mh.timeout = 5000, /* ms */
1147 },
1148};
1149
1150struct usb2_config umass_cbi_config[UMASS_T_CBI_MAX] = {
1151
1152 [UMASS_T_CBI_RESET1] = {
1153 .type = UE_CONTROL,
1154 .endpoint = 0x00, /* Control pipe */
1155 .direction = UE_DIR_ANY,
1156 .mh.bufsize = (sizeof(struct usb2_device_request) +
1157 UMASS_CBI_DIAGNOSTIC_CMDLEN),
1158 .mh.callback = &umass_t_cbi_reset1_callback,
1159 .mh.timeout = 5000, /* 5 seconds */
1160 .mh.interval = 500, /* 500 milliseconds */
1161 },
1162
1163 [UMASS_T_CBI_RESET2] = {
1164 .type = UE_CONTROL,
1165 .endpoint = 0x00, /* Control pipe */
1166 .direction = UE_DIR_ANY,
1167 .mh.bufsize = sizeof(struct usb2_device_request),
1168 .mh.callback = &umass_t_cbi_reset2_callback,
1169 .mh.timeout = 5000, /* 5 seconds */
1170 .mh.interval = 50, /* 50 milliseconds */
1171 },
1172
1173 [UMASS_T_CBI_RESET3] = {
1174 .type = UE_CONTROL,
1175 .endpoint = 0x00, /* Control pipe */
1176 .direction = UE_DIR_ANY,
1177 .mh.bufsize = sizeof(struct usb2_device_request),
1178 .mh.callback = &umass_t_cbi_reset3_callback,
1179 .mh.timeout = 5000, /* 5 seconds */
1180 .mh.interval = 50, /* 50 milliseconds */
1181 },
1182
1183 [UMASS_T_CBI_COMMAND] = {
1184 .type = UE_CONTROL,
1185 .endpoint = 0x00, /* Control pipe */
1186 .direction = UE_DIR_ANY,
1187 .mh.bufsize = (sizeof(struct usb2_device_request) +
1188 UMASS_MAX_CMDLEN),
1189 .mh.callback = &umass_t_cbi_command_callback,
1190 .mh.timeout = 5000, /* 5 seconds */
1191 },
1192
1193 [UMASS_T_CBI_DATA_READ] = {
1194 .type = UE_BULK,
1195 .endpoint = UE_ADDR_ANY,
1196 .direction = UE_DIR_IN,
1197 .mh.bufsize = UMASS_BULK_SIZE,
1198 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1199 .mh.callback = &umass_t_cbi_data_read_callback,
1200 .mh.timeout = 0, /* overwritten later */
1201 },
1202
1203 [UMASS_T_CBI_DATA_RD_CS] = {
1204 .type = UE_CONTROL,
1205 .endpoint = 0x00, /* Control pipe */
1206 .direction = UE_DIR_ANY,
1207 .mh.bufsize = sizeof(struct usb2_device_request),
1208 .mh.callback = &umass_t_cbi_data_rd_cs_callback,
1209 .mh.timeout = 5000, /* 5 seconds */
1210 },
1211
1212 [UMASS_T_CBI_DATA_WRITE] = {
1213 .type = UE_BULK,
1214 .endpoint = UE_ADDR_ANY,
1215 .direction = UE_DIR_OUT,
1216 .mh.bufsize = UMASS_BULK_SIZE,
1217 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
1218 .mh.callback = &umass_t_cbi_data_write_callback,
1219 .mh.timeout = 0, /* overwritten later */
1220 },
1221
1222 [UMASS_T_CBI_DATA_WR_CS] = {
1223 .type = UE_CONTROL,
1224 .endpoint = 0x00, /* Control pipe */
1225 .direction = UE_DIR_ANY,
1226 .mh.bufsize = sizeof(struct usb2_device_request),
1227 .mh.callback = &umass_t_cbi_data_wr_cs_callback,
1228 .mh.timeout = 5000, /* 5 seconds */
1229 },
1230
1231 [UMASS_T_CBI_STATUS] = {
1232 .type = UE_INTERRUPT,
1233 .endpoint = UE_ADDR_ANY,
1234 .direction = UE_DIR_IN,
1235 .mh.flags = {.short_xfer_ok = 1,},
1236 .mh.bufsize = sizeof(umass_cbi_sbl_t),
1237 .mh.callback = &umass_t_cbi_status_callback,
1238 .mh.timeout = 5000, /* ms */
1239 },
1240
1241 [UMASS_T_CBI_RESET4] = {
1242 .type = UE_CONTROL,
1243 .endpoint = 0x00, /* Control pipe */
1244 .direction = UE_DIR_ANY,
1245 .mh.bufsize = sizeof(struct usb2_device_request),
1246 .mh.callback = &umass_t_cbi_reset4_callback,
1247 .mh.timeout = 5000, /* ms */
1248 },
1249};
1250
1251/* If device cannot return valid inquiry data, fake it */
1252static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
1253 0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
1254 /* additional_length */ 31, 0, 0, 0
1255};
1256
1257#define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
1258#define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
1259
1260static devclass_t umass_devclass;
1261
1262static device_method_t umass_methods[] = {
1263 /* Device interface */
1264 DEVMETHOD(device_probe, umass_probe),
1265 DEVMETHOD(device_attach, umass_attach),
1266 DEVMETHOD(device_detach, umass_detach),
1267 {0, 0}
1268};
1269
1270static driver_t umass_driver = {
1271 .name = "umass",
1272 .methods = umass_methods,
1273 .size = sizeof(struct umass_softc),
1274};
1275
1276DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
1277MODULE_DEPEND(umass, usb, 1, 1, 1);
1278MODULE_DEPEND(umass, cam, 1, 1, 1);
1279
1280/*
1281 * USB device probe/attach/detach
1282 */
1283
1284/*
1285 * Match the device we are seeing with the
1286 * devices supported.
1287 */
1288static struct umass_probe_proto
1289umass_probe_proto(device_t dev, struct usb2_attach_arg *uaa)
1290{
1291 const struct umass_devdescr *udd = umass_devdescr;
1292 struct usb2_interface_descriptor *id;
1293 struct umass_probe_proto ret;
1294
1295 bzero(&ret, sizeof(ret));
1296
1297 /*
1298 * An entry specifically for Y-E Data devices as they don't fit in
1299 * the device description table.
1300 */
1301 if ((uaa->info.idVendor == USB_VENDOR_YEDATA) &&
1302 (uaa->info.idProduct == USB_PRODUCT_YEDATA_FLASHBUSTERU)) {
1303
1304 /*
1305 * Revisions < 1.28 do not handle the interrupt endpoint
1306 * very well.
1307 */
1308 if (uaa->info.bcdDevice < 0x128) {
1309 ret.proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI;
1310 } else {
1311 ret.proto = UMASS_PROTO_UFI | UMASS_PROTO_CBI_I;
1312 }
1313
1314 /*
1315 * Revisions < 1.28 do not have the TEST UNIT READY command
1316 * Revisions == 1.28 have a broken TEST UNIT READY
1317 */
1318 if (uaa->info.bcdDevice <= 0x128) {
1319 ret.quirks |= NO_TEST_UNIT_READY;
1320 }
1321 ret.quirks |= RS_NO_CLEAR_UA | FLOPPY_SPEED;
1322 ret.error = 0;
1323 goto done;
1324 }
1325 /*
1326 * Check the list of supported devices for a match. While looking,
1327 * check for wildcarded and fully matched. First match wins.
1328 */
1329 for (; udd->vid != VID_EOT; udd++) {
1330 if ((udd->vid == VID_WILDCARD) &&
1331 (udd->pid == PID_WILDCARD) &&
1332 (udd->rid == RID_WILDCARD)) {
1333 device_printf(dev, "ignoring invalid "
1334 "wildcard quirk\n");
1335 continue;
1336 }
1337 if (((udd->vid == uaa->info.idVendor) ||
1338 (udd->vid == VID_WILDCARD)) &&
1339 ((udd->pid == uaa->info.idProduct) ||
1340 (udd->pid == PID_WILDCARD))) {
1341 if (udd->rid == RID_WILDCARD) {
1342 ret.proto = udd->proto;
1343 ret.quirks = udd->quirks;
1344 ret.error = 0;
1345 goto done;
1346 } else if (udd->rid == uaa->info.bcdDevice) {
1347 ret.proto = udd->proto;
1348 ret.quirks = udd->quirks;
1349 ret.error = 0;
1350 goto done;
1351 } /* else RID does not match */
1352 }
1353 }
1354
1355 /* Check for a standards compliant device */
1356 id = usb2_get_interface_descriptor(uaa->iface);
1357 if ((id == NULL) ||
1358 (id->bInterfaceClass != UICLASS_MASS)) {
1359 ret.error = ENXIO;
1360 goto done;
1361 }
1362 switch (id->bInterfaceSubClass) {
1363 case UISUBCLASS_SCSI:
1364 ret.proto |= UMASS_PROTO_SCSI;
1365 break;
1366 case UISUBCLASS_UFI:
1367 ret.proto |= UMASS_PROTO_UFI;
1368 break;
1369 case UISUBCLASS_RBC:
1370 ret.proto |= UMASS_PROTO_RBC;
1371 break;
1372 case UISUBCLASS_SFF8020I:
1373 case UISUBCLASS_SFF8070I:
1374 ret.proto |= UMASS_PROTO_ATAPI;
1375 break;
1376 default:
1377 device_printf(dev, "unsupported command "
1378 "protocol %d\n", id->bInterfaceSubClass);
1379 ret.error = ENXIO;
1380 goto done;
1381 }
1382
1383 switch (id->bInterfaceProtocol) {
1384 case UIPROTO_MASS_CBI:
1385 ret.proto |= UMASS_PROTO_CBI;
1386 break;
1387 case UIPROTO_MASS_CBI_I:
1388 ret.proto |= UMASS_PROTO_CBI_I;
1389 break;
1390 case UIPROTO_MASS_BBB_OLD:
1391 case UIPROTO_MASS_BBB:
1392 ret.proto |= UMASS_PROTO_BBB;
1393 break;
1394 default:
1395 device_printf(dev, "unsupported wire "
1396 "protocol %d\n", id->bInterfaceProtocol);
1397 ret.error = ENXIO;
1398 goto done;
1399 }
1400
1401 ret.error = 0;
1402done:
1403 return (ret);
1404}
1405
1406static int
1407umass_probe(device_t dev)
1408{
1409 struct usb2_attach_arg *uaa = device_get_ivars(dev);
1410 struct umass_probe_proto temp;
1411
1412 if (uaa->usb2_mode != USB_MODE_HOST) {
1413 return (ENXIO);
1414 }
1415 if (uaa->use_generic == 0) {
1416 /* give other drivers a try first */
1417 return (ENXIO);
1418 }
1419 temp = umass_probe_proto(dev, uaa);
1420
1421 return (temp.error);
1422}
1423
1424static int
1425umass_attach(device_t dev)
1426{
1427 struct umass_softc *sc = device_get_softc(dev);
1428 struct usb2_attach_arg *uaa = device_get_ivars(dev);
1429 struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
1430 struct usb2_interface_descriptor *id;
1431 int32_t err;
1432
1433 /*
1434 * NOTE: the softc struct is bzero-ed in device_set_driver.
1435 * We can safely call umass_detach without specifically
1436 * initializing the struct.
1437 */
1438
1439 sc->sc_dev = dev;
1440 sc->sc_udev = uaa->device;
1441 sc->sc_proto = temp.proto;
1442 sc->sc_quirks = temp.quirks;
1443 sc->sc_unit = device_get_unit(dev);
1444
1445 snprintf(sc->sc_name, sizeof(sc->sc_name),
1446 "%s", device_get_nameunit(dev));
1447
1448 device_set_usb2_desc(dev);
1449
1450 mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
1451 NULL, MTX_DEF | MTX_RECURSE);
1452
1453 /* get interface index */
1454
1455 id = usb2_get_interface_descriptor(uaa->iface);
1456 if (id == NULL) {
1457 device_printf(dev, "failed to get "
1458 "interface number\n");
1459 goto detach;
1460 }
1461 sc->sc_iface_no = id->bInterfaceNumber;
1462
1463#if USB_DEBUG
1464 device_printf(dev, " ");
1465
1466 switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
1467 case UMASS_PROTO_SCSI:
1468 printf("SCSI");
1469 break;
1470 case UMASS_PROTO_ATAPI:
1471 printf("8070i (ATAPI)");
1472 break;
1473 case UMASS_PROTO_UFI:
1474 printf("UFI");
1475 break;
1476 case UMASS_PROTO_RBC:
1477 printf("RBC");
1478 break;
1479 default:
1480 printf("(unknown 0x%02x)",
1481 sc->sc_proto & UMASS_PROTO_COMMAND);
1482 break;
1483 }
1484
1485 printf(" over ");
1486
1487 switch (sc->sc_proto & UMASS_PROTO_WIRE) {
1488 case UMASS_PROTO_BBB:
1489 printf("Bulk-Only");
1490 break;
1491 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
1492 printf("CBI");
1493 break;
1494 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
1495 printf("CBI with CCI");
1496 break;
1497 default:
1498 printf("(unknown 0x%02x)",
1499 sc->sc_proto & UMASS_PROTO_WIRE);
1500 }
1501
1502 printf("; quirks = 0x%04x\n", sc->sc_quirks);
1503#endif
1504
1505 if (sc->sc_quirks & ALT_IFACE_1) {
1506 err = usb2_set_alt_interface_index
1507 (uaa->device, uaa->info.bIfaceIndex, 1);
1508
1509 if (err) {
1510 DPRINTF(sc, UDMASS_USB, "could not switch to "
1511 "Alt Interface 1\n");
1512 goto detach;
1513 }
1514 }
1515 /* allocate all required USB transfers */
1516
1517 if (sc->sc_proto & UMASS_PROTO_BBB) {
1518
1519 err = usb2_transfer_setup(uaa->device,
1520 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
1521 UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
1522
1523 /* skip reset first time */
1524 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1525
1526 } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
1527
1528 err = usb2_transfer_setup(uaa->device,
1529 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
1530 (sc->sc_proto & UMASS_PROTO_CBI_I) ?
1531 UMASS_T_CBI_MAX : (UMASS_T_CBI_MAX - 2), sc,
1532 &sc->sc_mtx);
1533
1534 /* skip reset first time */
1535 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1536
1537 } else {
1538 err = USB_ERR_INVAL;
1539 }
1540
1541 if (err) {
1542 device_printf(dev, "could not setup required "
1543 "transfers, %s\n", usb2_errstr(err));
1544 goto detach;
1545 }
1546 sc->sc_transform =
1547 (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
1548 (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
1549 (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
1550 (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1551 &umass_no_transform;
1552
1553 /* from here onwards the device can be used. */
1554
1555 if (sc->sc_quirks & SHUTTLE_INIT) {
1556 umass_init_shuttle(sc);
1557 }
1558 /* get the maximum LUN supported by the device */
1559
1560 if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1561 !(sc->sc_quirks & NO_GETMAXLUN))
1562 sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1563 else
1564 sc->sc_maxlun = 0;
1565
1566 /* Prepare the SCSI command block */
1567 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1568 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1569
1570 /*
1571 * some devices need a delay after that the configuration value is
1572 * set to function properly:
1573 */
1574 usb2_pause_mtx(NULL, hz);
1575
1576 /* register the SIM */
1577 err = umass_cam_attach_sim(sc);
1578 if (err) {
1579 goto detach;
1580 }
1581 /* scan the SIM */
1582 umass_cam_attach(sc);
1583
1584 DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1585
1586 return (0); /* success */
1587
1588detach:
1589 umass_detach(dev);
1590 return (ENXIO); /* failure */
1591}
1592
1593static int
1594umass_detach(device_t dev)
1595{
1596 struct umass_softc *sc = device_get_softc(dev);
1597
1598 DPRINTF(sc, UDMASS_USB, "\n");
1599
1600 /* teardown our statemachine */
1601
1602 usb2_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1603
1604#if (__FreeBSD_version >= 700037)
1605 mtx_lock(&sc->sc_mtx);
1606#endif
1607 umass_cam_detach_sim(sc);
1608
1609#if (__FreeBSD_version >= 700037)
1610 mtx_unlock(&sc->sc_mtx);
1611#endif
1612
1613 return (0); /* success */
1614}
1615
1616static void
1617umass_init_shuttle(struct umass_softc *sc)
1618{
1619 struct usb2_device_request req;
1620 usb2_error_t err;
1621 uint8_t status[2] = {0, 0};
1622
1623 /*
1624 * The Linux driver does this, but no one can tell us what the
1625 * command does.
1626 */
1627 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1628 req.bRequest = 1; /* XXX unknown command */
1629 USETW(req.wValue, 0);
1630 req.wIndex[0] = sc->sc_iface_no;
1631 req.wIndex[1] = 0;
1632 USETW(req.wLength, sizeof(status));
1633 err = usb2_do_request(sc->sc_udev, NULL, &req, &status);
1634
1635 DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1636 status[0], status[1]);
1637}
1638
1639/*
1640 * Generic functions to handle transfers
1641 */
1642
1643static void
1644umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1645{
1646 DPRINTF(sc, UDMASS_GEN, "transfer index = "
1647 "%d\n", xfer_index);
1648
1649 if (sc->sc_xfer[xfer_index]) {
1650 sc->sc_last_xfer_index = xfer_index;
1651 usb2_transfer_start(sc->sc_xfer[xfer_index]);
1652 } else {
1653 umass_cancel_ccb(sc);
1654 }
1655}
1656
1657static void
1658umass_reset(struct umass_softc *sc)
1659{
1660 DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1661
1662 /*
1663 * stop the last transfer, if not already stopped:
1664 */
1665 usb2_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1666 umass_transfer_start(sc, 0);
1667}
1668
1669static void
1670umass_cancel_ccb(struct umass_softc *sc)
1671{
1672 union ccb *ccb;
1673
1674 mtx_assert(&sc->sc_mtx, MA_OWNED);
1675
1676 ccb = sc->sc_transfer.ccb;
1677 sc->sc_transfer.ccb = NULL;
1678 sc->sc_last_xfer_index = 0;
1679
1680 if (ccb) {
1681 (sc->sc_transfer.callback)
1682 (sc, ccb, (sc->sc_transfer.data_len -
1683 sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1684 }
1685}
1686
1687static void
1688umass_tr_error(struct usb2_xfer *xfer)
1689{
1690 struct umass_softc *sc = xfer->priv_sc;
1691
1692 if (xfer->error != USB_ERR_CANCELLED) {
1693
1694 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1695 "reset\n", usb2_errstr(xfer->error));
1696 }
1697 umass_cancel_ccb(sc);
1698}
1699
1700/*
1701 * BBB protocol specific functions
1702 */
1703
1704static void
1705umass_t_bbb_reset1_callback(struct usb2_xfer *xfer)
1706{
1707 struct umass_softc *sc = xfer->priv_sc;
1708 struct usb2_device_request req;
1709
1710 switch (USB_GET_STATE(xfer)) {
1711 case USB_ST_TRANSFERRED:
1712 umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1713 return;
1714
1715 case USB_ST_SETUP:
1716 /*
1717 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1718 *
1719 * For Reset Recovery the host shall issue in the following order:
1720 * a) a Bulk-Only Mass Storage Reset
1721 * b) a Clear Feature HALT to the Bulk-In endpoint
1722 * c) a Clear Feature HALT to the Bulk-Out endpoint
1723 *
1724 * This is done in 3 steps, using 3 transfers:
1725 * UMASS_T_BBB_RESET1
1726 * UMASS_T_BBB_RESET2
1727 * UMASS_T_BBB_RESET3
1728 */
1729
1730 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1731
1732 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1733 req.bRequest = UR_BBB_RESET; /* bulk only reset */
1734 USETW(req.wValue, 0);
1735 req.wIndex[0] = sc->sc_iface_no;
1736 req.wIndex[1] = 0;
1737 USETW(req.wLength, 0);
1738
1739 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
1740
1741 xfer->frlengths[0] = sizeof(req);
1742 xfer->nframes = 1;
1743 usb2_start_hardware(xfer);
1744 return;
1745
1746 default: /* Error */
1747 umass_tr_error(xfer);
1748 return;
1749
1750 }
1751}
1752
1753static void
1754umass_t_bbb_reset2_callback(struct usb2_xfer *xfer)
1755{
1756 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1757 UMASS_T_BBB_DATA_READ);
1758}
1759
1760static void
1761umass_t_bbb_reset3_callback(struct usb2_xfer *xfer)
1762{
1763 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1764 UMASS_T_BBB_DATA_WRITE);
1765}
1766
1767static void
1768umass_t_bbb_data_clear_stall_callback(struct usb2_xfer *xfer,
1769 uint8_t next_xfer,
1770 uint8_t stall_xfer)
1771{
1772 struct umass_softc *sc = xfer->priv_sc;
1773
1774 switch (USB_GET_STATE(xfer)) {
1775 case USB_ST_TRANSFERRED:
1776tr_transferred:
1777 umass_transfer_start(sc, next_xfer);
1778 return;
1779
1780 case USB_ST_SETUP:
1781 if (usb2_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1782 goto tr_transferred;
1783 }
1784 return;
1785
1786 default: /* Error */
1787 umass_tr_error(xfer);
1788 return;
1789
1790 }
1791}
1792
1793static void
1794umass_t_bbb_command_callback(struct usb2_xfer *xfer)
1795{
1796 struct umass_softc *sc = xfer->priv_sc;
1797 union ccb *ccb = sc->sc_transfer.ccb;
1798 uint32_t tag;
1799
1800 switch (USB_GET_STATE(xfer)) {
1801 case USB_ST_TRANSFERRED:
1802 umass_transfer_start
1803 (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1804 (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1805 UMASS_T_BBB_STATUS));
1806 return;
1807
1808 case USB_ST_SETUP:
1809
1810 sc->sc_status_try = 0;
1811
1812 if (ccb) {
1813
1814 /*
1815 * the initial value is not important,
1816 * as long as the values are unique:
1817 */
1818 tag = UGETDW(sc->cbw.dCBWTag) + 1;
1819
1820 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1821 USETDW(sc->cbw.dCBWTag, tag);
1822
1823 /*
1824 * dCBWDataTransferLength:
1825 * This field indicates the number of bytes of data that the host
1826 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1827 * the Direction bit) during the execution of this command. If this
1828 * field is set to 0, the device will expect that no data will be
1829 * transferred IN or OUT during this command, regardless of the value
1830 * of the Direction bit defined in dCBWFlags.
1831 */
1832 USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1833
1834 /*
1835 * dCBWFlags:
1836 * The bits of the Flags field are defined as follows:
1837 * Bits 0-6 reserved
1838 * Bit 7 Direction - this bit shall be ignored if the
1839 * dCBWDataTransferLength field is zero.
1840 * 0 = data Out from host to device
1841 * 1 = data In from device to host
1842 */
1843 sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1844 CBWFLAGS_IN : CBWFLAGS_OUT);
1845 sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1846
1847 if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1848 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1849 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1850 }
1851 sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1852
1853 bcopy(sc->sc_transfer.cmd_data, sc->cbw.CBWCDB,
1854 sc->sc_transfer.cmd_len);
1855
1856 bzero(sc->sc_transfer.cmd_data + sc->sc_transfer.cmd_len,
1857 sizeof(sc->cbw.CBWCDB) - sc->sc_transfer.cmd_len);
1858
1859 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1860
1861 usb2_copy_in(xfer->frbuffers, 0, &sc->cbw, sizeof(sc->cbw));
1862
1863 xfer->frlengths[0] = sizeof(sc->cbw);
1864 usb2_start_hardware(xfer);
1865 }
1866 return;
1867
1868 default: /* Error */
1869 umass_tr_error(xfer);
1870 return;
1871
1872 }
1873}
1874
1875static void
1876umass_t_bbb_data_read_callback(struct usb2_xfer *xfer)
1877{
1878 struct umass_softc *sc = xfer->priv_sc;
1879 uint32_t max_bulk = xfer->max_data_length;
1880
1881 switch (USB_GET_STATE(xfer)) {
1882 case USB_ST_TRANSFERRED:
1883 if (!xfer->flags.ext_buffer) {
1884 usb2_copy_out(xfer->frbuffers, 0,
1885 sc->sc_transfer.data_ptr, xfer->actlen);
1886 }
1887 sc->sc_transfer.data_rem -= xfer->actlen;
1888 sc->sc_transfer.data_ptr += xfer->actlen;
1889 sc->sc_transfer.actlen += xfer->actlen;
1890
1891 if (xfer->actlen < xfer->sumlen) {
1892 /* short transfer */
1893 sc->sc_transfer.data_rem = 0;
1894 }
1895 case USB_ST_SETUP:
1896 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1897 max_bulk, sc->sc_transfer.data_rem);
1898
1899 if (sc->sc_transfer.data_rem == 0) {
1900 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1901 return;
1902 }
1903 if (max_bulk > sc->sc_transfer.data_rem) {
1904 max_bulk = sc->sc_transfer.data_rem;
1905 }
1906 xfer->timeout = sc->sc_transfer.data_timeout;
1907 xfer->frlengths[0] = max_bulk;
1908
1909 if (xfer->flags.ext_buffer) {
1910 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
1911 }
1912 usb2_start_hardware(xfer);
1913 return;
1914
1915 default: /* Error */
1916 if (xfer->error == USB_ERR_CANCELLED) {
1917 umass_tr_error(xfer);
1918 } else {
1919 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1920 }
1921 return;
1922
1923 }
1924}
1925
1926static void
1927umass_t_bbb_data_rd_cs_callback(struct usb2_xfer *xfer)
1928{
1929 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1930 UMASS_T_BBB_DATA_READ);
1931}
1932
1933static void
1934umass_t_bbb_data_write_callback(struct usb2_xfer *xfer)
1935{
1936 struct umass_softc *sc = xfer->priv_sc;
1937 uint32_t max_bulk = xfer->max_data_length;
1938
1939 switch (USB_GET_STATE(xfer)) {
1940 case USB_ST_TRANSFERRED:
1941 sc->sc_transfer.data_rem -= xfer->actlen;
1942 sc->sc_transfer.data_ptr += xfer->actlen;
1943 sc->sc_transfer.actlen += xfer->actlen;
1944
1945 if (xfer->actlen < xfer->sumlen) {
1946 /* short transfer */
1947 sc->sc_transfer.data_rem = 0;
1948 }
1949 case USB_ST_SETUP:
1950 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1951 max_bulk, sc->sc_transfer.data_rem);
1952
1953 if (sc->sc_transfer.data_rem == 0) {
1954 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1955 return;
1956 }
1957 if (max_bulk > sc->sc_transfer.data_rem) {
1958 max_bulk = sc->sc_transfer.data_rem;
1959 }
1960 xfer->timeout = sc->sc_transfer.data_timeout;
1961 xfer->frlengths[0] = max_bulk;
1962
1963 if (xfer->flags.ext_buffer) {
1964 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
1965 } else {
1966 usb2_copy_in(xfer->frbuffers, 0,
1967 sc->sc_transfer.data_ptr, max_bulk);
1968 }
1969
1970 usb2_start_hardware(xfer);
1971 return;
1972
1973 default: /* Error */
1974 if (xfer->error == USB_ERR_CANCELLED) {
1975 umass_tr_error(xfer);
1976 } else {
1977 umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
1978 }
1979 return;
1980
1981 }
1982}
1983
1984static void
1985umass_t_bbb_data_wr_cs_callback(struct usb2_xfer *xfer)
1986{
1987 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1988 UMASS_T_BBB_DATA_WRITE);
1989}
1990
1991static void
1992umass_t_bbb_status_callback(struct usb2_xfer *xfer)
1993{
1994 struct umass_softc *sc = xfer->priv_sc;
1995 union ccb *ccb = sc->sc_transfer.ccb;
1996 uint32_t residue;
1997
1998 switch (USB_GET_STATE(xfer)) {
1999 case USB_ST_TRANSFERRED:
2000
2001 /*
2002 * Do a full reset if there is something wrong with the CSW:
2003 */
2004 sc->sc_status_try = 1;
2005
2006 /* Zero missing parts of the CSW: */
2007
2008 if (xfer->actlen < sizeof(sc->csw)) {
2009 bzero(&sc->csw, sizeof(sc->csw));
2010 }
2011 usb2_copy_out(xfer->frbuffers, 0, &sc->csw, xfer->actlen);
2012
2013 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
2014
2015 residue = UGETDW(sc->csw.dCSWDataResidue);
2016
2017 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
2018 residue = (sc->sc_transfer.data_len -
2019 sc->sc_transfer.actlen);
2020 }
2021 if (residue > sc->sc_transfer.data_len) {
2022 DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
2023 "to %d bytes\n", residue, sc->sc_transfer.data_len);
2024 residue = sc->sc_transfer.data_len;
2025 }
2026 /* translate weird command-status signatures: */
2027 if (sc->sc_quirks & WRONG_CSWSIG) {
2028
2029 uint32_t temp = UGETDW(sc->csw.dCSWSignature);
2030
2031 if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
2032 (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
2033 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
2034 }
2035 }
2036 /* check CSW and handle eventual error */
2037 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
2038 DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
2039 UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
2040 /*
2041 * Invalid CSW: Wrong signature or wrong tag might
2042 * indicate that we lost synchronization. Reset the
2043 * device.
2044 */
2045 goto tr_error;
2046 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
2047 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
2048 "0x%08x\n", UGETDW(sc->csw.dCSWTag),
2049 UGETDW(sc->cbw.dCBWTag));
2050 goto tr_error;
2051 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
2052 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
2053 sc->csw.bCSWStatus, CSWSTATUS_PHASE);
2054 goto tr_error;
2055 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
2056 DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
2057 "%d\n", residue);
2058 goto tr_error;
2059 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
2060 DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
2061 sc->sc_transfer.actlen, sc->sc_transfer.data_len);
2062 goto tr_error;
2063 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
2064 DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
2065 "%d\n", residue);
2066
2067 sc->sc_transfer.ccb = NULL;
2068
2069 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2070
2071 (sc->sc_transfer.callback)
2072 (sc, ccb, residue, STATUS_CMD_FAILED);
2073 } else {
2074 sc->sc_transfer.ccb = NULL;
2075
2076 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
2077
2078 (sc->sc_transfer.callback)
2079 (sc, ccb, residue, STATUS_CMD_OK);
2080 }
2081 return;
2082
2083 case USB_ST_SETUP:
2084 xfer->frlengths[0] = xfer->max_data_length;
2085 usb2_start_hardware(xfer);
2086 return;
2087
2088 default:
2089tr_error:
2090 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
2091 usb2_errstr(xfer->error), sc->sc_status_try);
2092
2093 if ((xfer->error == USB_ERR_CANCELLED) ||
2094 (sc->sc_status_try)) {
2095 umass_tr_error(xfer);
2096 } else {
2097 sc->sc_status_try = 1;
2098 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
2099 }
2100 return;
2101
2102 }
2103}
2104
2105static void
2106umass_command_start(struct umass_softc *sc, uint8_t dir,
2107 void *data_ptr, uint32_t data_len,
2108 uint32_t data_timeout, umass_callback_t *callback,
2109 union ccb *ccb)
2110{
2111 sc->sc_transfer.lun = ccb->ccb_h.target_lun;
2112
2113 /*
2114 * NOTE: assumes that "sc->sc_transfer.cmd_data" and
2115 * "sc->sc_transfer.cmd_len" has been properly
2116 * initialized.
2117 */
2118
2119 sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
2120 sc->sc_transfer.data_ptr = data_ptr;
2121 sc->sc_transfer.data_len = data_len;
2122 sc->sc_transfer.data_rem = data_len;
2123 sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
2124
2125 sc->sc_transfer.actlen = 0;
2126 sc->sc_transfer.callback = callback;
2127 sc->sc_transfer.ccb = ccb;
2128
2129 if (sc->sc_xfer[sc->sc_last_xfer_index]) {
2130 usb2_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
2131 } else {
2132 ccb->ccb_h.status = CAM_TID_INVALID;
2133 xpt_done(ccb);
2134 }
2135}
2136
2137static uint8_t
2138umass_bbb_get_max_lun(struct umass_softc *sc)
2139{
2140 struct usb2_device_request req;
2141 usb2_error_t err;
2142 uint8_t buf = 0;
2143
2144 /* The Get Max Lun command is a class-specific request. */
2145 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2146 req.bRequest = UR_BBB_GET_MAX_LUN;
2147 USETW(req.wValue, 0);
2148 req.wIndex[0] = sc->sc_iface_no;
2149 req.wIndex[1] = 0;
2150 USETW(req.wLength, 1);
2151
2152 err = usb2_do_request(sc->sc_udev, NULL, &req, &buf);
2153 if (err) {
2154 buf = 0;
2155
2156 /* Device doesn't support Get Max Lun request. */
2157 printf("%s: Get Max Lun not supported (%s)\n",
2158 sc->sc_name, usb2_errstr(err));
2159 }
2160 return (buf);
2161}
2162
2163/*
2164 * Command/Bulk/Interrupt (CBI) specific functions
2165 */
2166
2167static void
2168umass_cbi_start_status(struct umass_softc *sc)
2169{
2170 if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
2171 umass_transfer_start(sc, UMASS_T_CBI_STATUS);
2172 } else {
2173 union ccb *ccb = sc->sc_transfer.ccb;
2174
2175 sc->sc_transfer.ccb = NULL;
2176
2177 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2178
2179 (sc->sc_transfer.callback)
2180 (sc, ccb, (sc->sc_transfer.data_len -
2181 sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
2182 }
2183}
2184
2185static void
2186umass_t_cbi_reset1_callback(struct usb2_xfer *xfer)
2187{
2188 struct umass_softc *sc = xfer->priv_sc;
2189 struct usb2_device_request req;
2190 uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
2191
2192 uint8_t i;
2193
2194 switch (USB_GET_STATE(xfer)) {
2195 case USB_ST_TRANSFERRED:
2196 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
2197 return;
2198
2199 case USB_ST_SETUP:
2200 /*
2201 * Command Block Reset Protocol
2202 *
2203 * First send a reset request to the device. Then clear
2204 * any possibly stalled bulk endpoints.
2205 *
2206 * This is done in 3 steps, using 3 transfers:
2207 * UMASS_T_CBI_RESET1
2208 * UMASS_T_CBI_RESET2
2209 * UMASS_T_CBI_RESET3
2210 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
2211 */
2212
2213 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
2214
2215 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2216 req.bRequest = UR_CBI_ADSC;
2217 USETW(req.wValue, 0);
2218 req.wIndex[0] = sc->sc_iface_no;
2219 req.wIndex[1] = 0;
2220 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
2221
2222 /*
2223 * The 0x1d code is the SEND DIAGNOSTIC command. To
2224 * distinguish between the two, the last 10 bytes of the CBL
2225 * is filled with 0xff (section 2.2 of the CBI
2226 * specification)
2227 */
2228 buf[0] = 0x1d; /* Command Block Reset */
2229 buf[1] = 0x04;
2230
2231 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
2232 buf[i] = 0xff;
2233 }
2234
2235 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
2236 usb2_copy_in(xfer->frbuffers + 1, 0, buf, sizeof(buf));
2237
2238 xfer->frlengths[0] = sizeof(req);
2239 xfer->frlengths[1] = sizeof(buf);
2240 xfer->nframes = 2;
2241 usb2_start_hardware(xfer);
2242 return;
2243
2244 default: /* Error */
2245 umass_tr_error(xfer);
2246 return;
2247
2248 }
2249}
2250
2251static void
2252umass_t_cbi_reset2_callback(struct usb2_xfer *xfer)
2253{
2254 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
2255 UMASS_T_CBI_DATA_READ);
2256}
2257
2258static void
2259umass_t_cbi_reset3_callback(struct usb2_xfer *xfer)
2260{
2261 struct umass_softc *sc = xfer->priv_sc;
2262
2263 umass_t_cbi_data_clear_stall_callback
2264 (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
2265 sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
2266 UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
2267 UMASS_T_CBI_DATA_WRITE);
2268}
2269
2270static void
2271umass_t_cbi_reset4_callback(struct usb2_xfer *xfer)
2272{
2273 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
2274 UMASS_T_CBI_STATUS);
2275}
2276
2277static void
2278umass_t_cbi_data_clear_stall_callback(struct usb2_xfer *xfer,
2279 uint8_t next_xfer,
2280 uint8_t stall_xfer)
2281{
2282 struct umass_softc *sc = xfer->priv_sc;
2283
2284 switch (USB_GET_STATE(xfer)) {
2285 case USB_ST_TRANSFERRED:
2286tr_transferred:
2287 if (next_xfer == UMASS_T_CBI_STATUS) {
2288 umass_cbi_start_status(sc);
2289 } else {
2290 umass_transfer_start(sc, next_xfer);
2291 }
2292 return;
2293
2294 case USB_ST_SETUP:
2295 if (usb2_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
2296 goto tr_transferred; /* should not happen */
2297 }
2298 return;
2299
2300 default: /* Error */
2301 umass_tr_error(xfer);
2302 return;
2303
2304 }
2305}
2306
2307static void
2308umass_t_cbi_command_callback(struct usb2_xfer *xfer)
2309{
2310 struct umass_softc *sc = xfer->priv_sc;
2311 union ccb *ccb = sc->sc_transfer.ccb;
2312 struct usb2_device_request req;
2313
2314 switch (USB_GET_STATE(xfer)) {
2315 case USB_ST_TRANSFERRED:
2316
2317 if (sc->sc_transfer.dir == DIR_NONE) {
2318 umass_cbi_start_status(sc);
2319 } else {
2320 umass_transfer_start
2321 (sc, (sc->sc_transfer.dir == DIR_IN) ?
2322 UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
2323 }
2324 return;
2325
2326 case USB_ST_SETUP:
2327
2328 if (ccb) {
2329
2330 /*
2331 * do a CBI transfer with cmd_len bytes from
2332 * cmd_data, possibly a data phase of data_len
2333 * bytes from/to the device and finally a status
2334 * read phase.
2335 */
2336
2337 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2338 req.bRequest = UR_CBI_ADSC;
2339 USETW(req.wValue, 0);
2340 req.wIndex[0] = sc->sc_iface_no;
2341 req.wIndex[1] = 0;
2342 req.wLength[0] = sc->sc_transfer.cmd_len;
2343 req.wLength[1] = 0;
2344
2345 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
2346 usb2_copy_in(xfer->frbuffers + 1, 0, sc->sc_transfer.cmd_data,
2347 sc->sc_transfer.cmd_len);
2348
2349 xfer->frlengths[0] = sizeof(req);
2350 xfer->frlengths[1] = sc->sc_transfer.cmd_len;
2351 xfer->nframes = xfer->frlengths[1] ? 2 : 1;
2352
2353 DIF(UDMASS_CBI,
2354 umass_cbi_dump_cmd(sc,
2355 sc->sc_transfer.cmd_data,
2356 sc->sc_transfer.cmd_len));
2357
2358 usb2_start_hardware(xfer);
2359 }
2360 return;
2361
2362 default: /* Error */
2363 umass_tr_error(xfer);
2364 return;
2365
2366 }
2367}
2368
2369static void
2370umass_t_cbi_data_read_callback(struct usb2_xfer *xfer)
2371{
2372 struct umass_softc *sc = xfer->priv_sc;
2373 uint32_t max_bulk = xfer->max_data_length;
2374
2375 switch (USB_GET_STATE(xfer)) {
2376 case USB_ST_TRANSFERRED:
2377 if (!xfer->flags.ext_buffer) {
2378 usb2_copy_out(xfer->frbuffers, 0,
2379 sc->sc_transfer.data_ptr, xfer->actlen);
2380 }
2381 sc->sc_transfer.data_rem -= xfer->actlen;
2382 sc->sc_transfer.data_ptr += xfer->actlen;
2383 sc->sc_transfer.actlen += xfer->actlen;
2384
2385 if (xfer->actlen < xfer->sumlen) {
2386 /* short transfer */
2387 sc->sc_transfer.data_rem = 0;
2388 }
2389 case USB_ST_SETUP:
2390 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2391 max_bulk, sc->sc_transfer.data_rem);
2392
2393 if (sc->sc_transfer.data_rem == 0) {
2394 umass_cbi_start_status(sc);
2395 return;
2396 }
2397 if (max_bulk > sc->sc_transfer.data_rem) {
2398 max_bulk = sc->sc_transfer.data_rem;
2399 }
2400 xfer->timeout = sc->sc_transfer.data_timeout;
2401
2402 if (xfer->flags.ext_buffer) {
2403 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
2404 }
2405 xfer->frlengths[0] = max_bulk;
2406 usb2_start_hardware(xfer);
2407 return;
2408
2409 default: /* Error */
2410 if ((xfer->error == USB_ERR_CANCELLED) ||
2411 (sc->sc_transfer.callback != &umass_cam_cb)) {
2412 umass_tr_error(xfer);
2413 } else {
2414 umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
2415 }
2416 return;
2417
2418 }
2419}
2420
2421static void
2422umass_t_cbi_data_rd_cs_callback(struct usb2_xfer *xfer)
2423{
2424 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2425 UMASS_T_CBI_DATA_READ);
2426}
2427
2428static void
2429umass_t_cbi_data_write_callback(struct usb2_xfer *xfer)
2430{
2431 struct umass_softc *sc = xfer->priv_sc;
2432 uint32_t max_bulk = xfer->max_data_length;
2433
2434 switch (USB_GET_STATE(xfer)) {
2435 case USB_ST_TRANSFERRED:
2436 sc->sc_transfer.data_rem -= xfer->actlen;
2437 sc->sc_transfer.data_ptr += xfer->actlen;
2438 sc->sc_transfer.actlen += xfer->actlen;
2439
2440 if (xfer->actlen < xfer->sumlen) {
2441 /* short transfer */
2442 sc->sc_transfer.data_rem = 0;
2443 }
2444 case USB_ST_SETUP:
2445 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
2446 max_bulk, sc->sc_transfer.data_rem);
2447
2448 if (sc->sc_transfer.data_rem == 0) {
2449 umass_cbi_start_status(sc);
2450 return;
2451 }
2452 if (max_bulk > sc->sc_transfer.data_rem) {
2453 max_bulk = sc->sc_transfer.data_rem;
2454 }
2455 xfer->timeout = sc->sc_transfer.data_timeout;
2456
2457 if (xfer->flags.ext_buffer) {
2458 usb2_set_frame_data(xfer, sc->sc_transfer.data_ptr, 0);
2459 } else {
2460 usb2_copy_in(xfer->frbuffers, 0,
2461 sc->sc_transfer.data_ptr, max_bulk);
2462 }
2463
2464 xfer->frlengths[0] = max_bulk;
2465 usb2_start_hardware(xfer);
2466 return;
2467
2468 default: /* Error */
2469 if ((xfer->error == USB_ERR_CANCELLED) ||
2470 (sc->sc_transfer.callback != &umass_cam_cb)) {
2471 umass_tr_error(xfer);
2472 } else {
2473 umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
2474 }
2475 return;
2476
2477 }
2478}
2479
2480static void
2481umass_t_cbi_data_wr_cs_callback(struct usb2_xfer *xfer)
2482{
2483 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2484 UMASS_T_CBI_DATA_WRITE);
2485}
2486
2487static void
2488umass_t_cbi_status_callback(struct usb2_xfer *xfer)
2489{
2490 struct umass_softc *sc = xfer->priv_sc;
2491 union ccb *ccb = sc->sc_transfer.ccb;
2492 uint32_t residue;
2493 uint8_t status;
2494
2495 switch (USB_GET_STATE(xfer)) {
2496 case USB_ST_TRANSFERRED:
2497
2498 if (xfer->actlen < sizeof(sc->sbl)) {
2499 goto tr_setup;
2500 }
2501 usb2_copy_out(xfer->frbuffers, 0, &sc->sbl, sizeof(sc->sbl));
2502
2503 residue = (sc->sc_transfer.data_len -
2504 sc->sc_transfer.actlen);
2505
2506 /* dissect the information in the buffer */
2507
2508 if (sc->sc_proto & UMASS_PROTO_UFI) {
2509
2510 /*
2511 * Section 3.4.3.1.3 specifies that the UFI command
2512 * protocol returns an ASC and ASCQ in the interrupt
2513 * data block.
2514 */
2515
2516 DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2517 "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2518 sc->sbl.ufi.ascq);
2519
2520 status = (((sc->sbl.ufi.asc == 0) &&
2521 (sc->sbl.ufi.ascq == 0)) ?
2522 STATUS_CMD_OK : STATUS_CMD_FAILED);
2523
2524 sc->sc_transfer.ccb = NULL;
2525
2526 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2527
2528 (sc->sc_transfer.callback)
2529 (sc, ccb, residue, status);
2530
2531 return;
2532
2533 } else {
2534
2535 /* Command Interrupt Data Block */
2536
2537 DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2538 sc->sbl.common.type, sc->sbl.common.value);
2539
2540 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2541
2542 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2543
2544 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2545 (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2546 (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2547 STATUS_WIRE_FAILED);
2548
2549 sc->sc_transfer.ccb = NULL;
2550
2551 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2552
2553 (sc->sc_transfer.callback)
2554 (sc, ccb, residue, status);
2555
2556 return;
2557 }
2558 }
2559
2560 /* fallthrough */
2561
2562 case USB_ST_SETUP:
2563tr_setup:
2564 xfer->frlengths[0] = xfer->max_data_length;
2565 usb2_start_hardware(xfer);
2566 return;
2567
2568 default: /* Error */
2569 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2570 usb2_errstr(xfer->error));
2571 umass_tr_error(xfer);
2572 return;
2573
2574 }
2575}
2576
2577/*
2578 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2579 */
2580
2581static int
2582umass_cam_attach_sim(struct umass_softc *sc)
2583{
2584 struct cam_devq *devq; /* Per device Queue */
2585
2586 /*
2587 * A HBA is attached to the CAM layer.
2588 *
2589 * The CAM layer will then after a while start probing for devices on
2590 * the bus. The number of SIMs is limited to one.
2591 */
2592
2593 devq = cam_simq_alloc(1 /* maximum openings */ );
2594 if (devq == NULL) {
2595 return (ENOMEM);
2596 }
2597 sc->sc_sim = cam_sim_alloc
2598 (&umass_cam_action, &umass_cam_poll,
2599 DEVNAME_SIM,
2600 sc /* priv */ ,
2601 sc->sc_unit /* unit number */ ,
2602#if (__FreeBSD_version >= 700037)
2603 &sc->sc_mtx /* mutex */ ,
2604#endif
2605 1 /* maximum device openings */ ,
2606 0 /* maximum tagged device openings */ ,
2607 devq);
2608
2609 if (sc->sc_sim == NULL) {
2610 cam_simq_free(devq);
2611 return (ENOMEM);
2612 }
2613
2614#if (__FreeBSD_version >= 700037)
2615 mtx_lock(&sc->sc_mtx);
2616#endif
2617
2618#if (__FreeBSD_version >= 700048)
2619 if (xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit) != CAM_SUCCESS) {
2620 mtx_unlock(&sc->sc_mtx);
2621 return (ENOMEM);
2622 }
2623#else
2624 if (xpt_bus_register(sc->sc_sim, sc->sc_unit) != CAM_SUCCESS) {
2625#if (__FreeBSD_version >= 700037)
2626 mtx_unlock(&sc->sc_mtx);
2627#endif
2628 return (ENOMEM);
2629 }
2630#endif
2631
2632#if (__FreeBSD_version >= 700037)
2633 mtx_unlock(&sc->sc_mtx);
2634#endif
2635 return (0);
2636}
2637
2638static void
2639umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2640{
2641#if USB_DEBUG
2642 struct umass_softc *sc = NULL;
2643
2644 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2645 DPRINTF(sc, UDMASS_SCSI, "%s:%d Rescan failed, 0x%04x\n",
2646 periph->periph_name, periph->unit_number,
2647 ccb->ccb_h.status);
2648 } else {
2649 DPRINTF(sc, UDMASS_SCSI, "%s%d: Rescan succeeded\n",
2650 periph->periph_name, periph->unit_number);
2651 }
2652#endif
2653
2654 xpt_free_path(ccb->ccb_h.path);
2655 free(ccb, M_USBDEV);
2656}
2657
2658static void
2659umass_cam_rescan(struct umass_softc *sc)
2660{
2661 struct cam_path *path;
2662 union ccb *ccb;
2663
2664 DPRINTF(sc, UDMASS_SCSI, "scbus%d: scanning for %d:%d:%d\n",
2665 cam_sim_path(sc->sc_sim),
2666 cam_sim_path(sc->sc_sim),
2667 sc->sc_unit, CAM_LUN_WILDCARD);
2668
2669 ccb = malloc(sizeof(*ccb), M_USBDEV, M_WAITOK | M_ZERO);
2670
2671 if (ccb == NULL) {
2672 return;
2673 }
2674#if (__FreeBSD_version >= 700037)
2675 mtx_lock(&sc->sc_mtx);
2676#endif
2677
2678 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->sc_sim),
2679 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
2680 != CAM_REQ_CMP) {
2681#if (__FreeBSD_version >= 700037)
2682 mtx_unlock(&sc->sc_mtx);
2683#endif
2684 free(ccb, M_USBDEV);
2685 return;
2686 }
2687 xpt_setup_ccb(&ccb->ccb_h, path, 5 /* priority (low) */ );
2688 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2689 ccb->ccb_h.cbfcnp = &umass_cam_rescan_callback;
2690 ccb->crcn.flags = CAM_FLAG_NONE;
2691 xpt_action(ccb);
2692
2693#if (__FreeBSD_version >= 700037)
2694 mtx_unlock(&sc->sc_mtx);
2695#endif
2696
2697 /* The scan is in progress now. */
2698}
2699
2700static void
2701umass_cam_attach(struct umass_softc *sc)
2702{
2703#ifndef USB_DEBUG
2704 if (bootverbose)
2705#endif
2706 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2707 sc->sc_name, cam_sim_path(sc->sc_sim),
2708 sc->sc_unit, CAM_LUN_WILDCARD,
2709 cam_sim_path(sc->sc_sim));
2710
2711 if (!cold) {
2712 /*
2713 * Notify CAM of the new device after a short delay. Any
2714 * failure is benign, as the user can still do it by hand
2715 * (camcontrol rescan <busno>). Only do this if we are not
2716 * booting, because CAM does a scan after booting has
2717 * completed, when interrupts have been enabled.
2718 */
2719
2720 /* scan the new sim */
2721 umass_cam_rescan(sc);
2722 }
2723}
2724
2725/* umass_cam_detach
2726 * detach from the CAM layer
2727 */
2728
2729static void
2730umass_cam_detach_sim(struct umass_softc *sc)
2731{
2732 if (sc->sc_sim != NULL) {
2733 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2734 /* accessing the softc is not possible after this */
2735 sc->sc_sim->softc = UMASS_GONE;
2736 cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2737 } else {
2738 panic("%s: CAM layer is busy!\n",
2739 sc->sc_name);
2740 }
2741 sc->sc_sim = NULL;
2742 }
2743}
2744
2745/* umass_cam_action
2746 * CAM requests for action come through here
2747 */
2748
2749static void
2750umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2751{
2752 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2753
2754 if (sc == UMASS_GONE) {
2755 ccb->ccb_h.status = CAM_TID_INVALID;
2756 xpt_done(ccb);
2757 return;
2758 }
2759 if (sc) {
2760#if (__FreeBSD_version < 700037)
2761 mtx_lock(&sc->sc_mtx);
2762#endif
2763 }
2764 /*
2765 * Verify, depending on the operation to perform, that we either got
2766 * a valid sc, because an existing target was referenced, or
2767 * otherwise the SIM is addressed.
2768 *
2769 * This avoids bombing out at a printf and does give the CAM layer some
2770 * sensible feedback on errors.
2771 */
2772 switch (ccb->ccb_h.func_code) {
2773 case XPT_SCSI_IO:
2774 case XPT_RESET_DEV:
2775 case XPT_GET_TRAN_SETTINGS:
2776 case XPT_SET_TRAN_SETTINGS:
2777 case XPT_CALC_GEOMETRY:
2778 /* the opcodes requiring a target. These should never occur. */
2779 if (sc == NULL) {
2780 DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: "
2781 "Invalid target (target needed)\n",
2782 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2783 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2784 ccb->ccb_h.func_code);
2785
2786 ccb->ccb_h.status = CAM_TID_INVALID;
2787 xpt_done(ccb);
2788 goto done;
2789 }
2790 break;
2791 case XPT_PATH_INQ:
2792 case XPT_NOOP:
2793 /*
2794 * The opcodes sometimes aimed at a target (sc is valid),
2795 * sometimes aimed at the SIM (sc is invalid and target is
2796 * CAM_TARGET_WILDCARD)
2797 */
2798 if ((sc == NULL) &&
2799 (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) {
2800 DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: "
2801 "Invalid target (no wildcard)\n",
2802 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2803 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2804 ccb->ccb_h.func_code);
2805
2806 ccb->ccb_h.status = CAM_TID_INVALID;
2807 xpt_done(ccb);
2808 goto done;
2809 }
2810 break;
2811 default:
2812 /* XXX Hm, we should check the input parameters */
2813 break;
2814 }
2815
2816 /* Perform the requested action */
2817 switch (ccb->ccb_h.func_code) {
2818 case XPT_SCSI_IO:
2819 {
2820 uint8_t *cmd;
2821 uint8_t dir;
2822
2823 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2824 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2825 } else {
2826 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2827 }
2828
2829 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2830 "cmd: 0x%02x, flags: 0x%02x, "
2831 "%db cmd/%db data/%db sense\n",
2832 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2833 ccb->ccb_h.target_lun, cmd[0],
2834 ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2835 ccb->csio.dxfer_len, ccb->csio.sense_len);
2836
2837 if (sc->sc_transfer.ccb) {
2838 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2839 "I/O in progress, deferring\n",
2840 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2841 ccb->ccb_h.target_lun);
2842 ccb->ccb_h.status = CAM_SCSI_BUSY;
2843 xpt_done(ccb);
2844 goto done;
2845 }
2846 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2847 case CAM_DIR_IN:
2848 dir = DIR_IN;
2849 break;
2850 case CAM_DIR_OUT:
2851 dir = DIR_OUT;
2852 DIF(UDMASS_SCSI,
2853 umass_dump_buffer(sc, ccb->csio.data_ptr,
2854 ccb->csio.dxfer_len, 48));
2855 break;
2856 default:
2857 dir = DIR_NONE;
2858 }
2859
2860 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2861
2862 /*
2863 * sc->sc_transform will convert the command to the
2864 * command format needed by the specific command set
2865 * and return the converted command in
2866 * "sc->sc_transfer.cmd_data"
2867 */
2868 if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2869
2870 if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2871
2872 /*
2873 * Handle EVPD inquiry for broken devices first
2874 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2875 */
2876 if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2877 (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
2878 struct scsi_sense_data *sense;
2879
2880 sense = &ccb->csio.sense_data;
2881 bzero(sense, sizeof(*sense));
2882 sense->error_code = SSD_CURRENT_ERROR;
2883 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2884 sense->add_sense_code = 0x24;
2885 sense->extra_len = 10;
2886 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2887 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2888 CAM_AUTOSNS_VALID;
2889 xpt_done(ccb);
2890 goto done;
2891 }
2892 /*
2893 * Return fake inquiry data for
2894 * broken devices
2895 */
2896 if (sc->sc_quirks & NO_INQUIRY) {
2897 memcpy(ccb->csio.data_ptr, &fake_inq_data,
2898 sizeof(fake_inq_data));
2899 ccb->csio.scsi_status = SCSI_STATUS_OK;
2900 ccb->ccb_h.status = CAM_REQ_CMP;
2901 xpt_done(ccb);
2902 goto done;
2903 }
2904 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2905 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
2906 }
2907 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
2908 if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
2909 ccb->csio.scsi_status = SCSI_STATUS_OK;
2910 ccb->ccb_h.status = CAM_REQ_CMP;
2911 xpt_done(ccb);
2912 goto done;
2913 }
2914 }
2915 umass_command_start(sc, dir, ccb->csio.data_ptr,
2916 ccb->csio.dxfer_len,
2917 ccb->ccb_h.timeout,
2918 &umass_cam_cb, ccb);
2919 }
2920 break;
2921 }
2922 case XPT_PATH_INQ:
2923 {
2924 struct ccb_pathinq *cpi = &ccb->cpi;
2925
2926 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
2927 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2928 ccb->ccb_h.target_lun);
2929
2930 /* host specific information */
2931 cpi->version_num = 1;
2932 cpi->hba_inquiry = 0;
2933 cpi->target_sprt = 0;
2934 cpi->hba_misc = PIM_NO_6_BYTE;
2935 cpi->hba_eng_cnt = 0;
2936 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2937 cpi->initiator_id = UMASS_SCSIID_HOST;
2938 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2939 strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2940 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2941 cpi->unit_number = cam_sim_unit(sim);
2942 cpi->bus_id = sc->sc_unit;
2943#if (__FreeBSD_version >= 700025)
2944 cpi->protocol = PROTO_SCSI;
2945 cpi->protocol_version = SCSI_REV_2;
2946 cpi->transport = XPORT_USB;
2947 cpi->transport_version = 0;
2948#endif
2949 if (sc == NULL) {
2950 cpi->base_transfer_speed = 0;
2951 cpi->max_lun = 0;
2952 } else {
2953 if (sc->sc_quirks & FLOPPY_SPEED) {
2954 cpi->base_transfer_speed =
2955 UMASS_FLOPPY_TRANSFER_SPEED;
2956 } else if (usb2_get_speed(sc->sc_udev) ==
2957 USB_SPEED_HIGH) {
2958 cpi->base_transfer_speed =
2959 UMASS_HIGH_TRANSFER_SPEED;
2960 } else {
2961 cpi->base_transfer_speed =
2962 UMASS_FULL_TRANSFER_SPEED;
2963 }
2964 cpi->max_lun = sc->sc_maxlun;
2965 }
2966
2967 cpi->ccb_h.status = CAM_REQ_CMP;
2968 xpt_done(ccb);
2969 break;
2970 }
2971 case XPT_RESET_DEV:
2972 {
2973 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
2974 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2975 ccb->ccb_h.target_lun);
2976
2977 umass_reset(sc);
2978
2979 ccb->ccb_h.status = CAM_REQ_CMP;
2980 xpt_done(ccb);
2981 break;
2982 }
2983 case XPT_GET_TRAN_SETTINGS:
2984 {
2985 struct ccb_trans_settings *cts = &ccb->cts;
2986
2987 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2988 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2989 ccb->ccb_h.target_lun);
2990
2991#if (__FreeBSD_version >= 700025)
2992 cts->protocol = PROTO_SCSI;
2993 cts->protocol_version = SCSI_REV_2;
2994 cts->transport = XPORT_USB;
2995 cts->transport_version = 0;
2996 cts->xport_specific.valid = 0;
2997#else
2998 cts->valid = 0;
2999 cts->flags = 0; /* no disconnection, tagging */
3000#endif
3001 ccb->ccb_h.status = CAM_REQ_CMP;
3002 xpt_done(ccb);
3003 break;
3004 }
3005 case XPT_SET_TRAN_SETTINGS:
3006 {
3007 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
3008 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
3009 ccb->ccb_h.target_lun);
3010
3011 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3012 xpt_done(ccb);
3013 break;
3014 }
3015 case XPT_CALC_GEOMETRY:
3016 {
3017 cam_calc_geometry(&ccb->ccg, /* extended */ 1);
3018 xpt_done(ccb);
3019 break;
3020 }
3021 case XPT_NOOP:
3022 {
3023 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
3024 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3025 ccb->ccb_h.target_lun);
3026
3027 ccb->ccb_h.status = CAM_REQ_CMP;
3028 xpt_done(ccb);
3029 break;
3030 }
3031 default:
3032 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
3033 "Not implemented\n",
3034 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
3035 ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
3036
3037 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3038 xpt_done(ccb);
3039 break;
3040 }
3041
3042done:
3043#if (__FreeBSD_version < 700037)
3044 if (sc) {
3045 mtx_unlock(&sc->sc_mtx);
3046 }
3047#endif
3048 return;
3049}
3050
3051static void
3052umass_cam_poll(struct cam_sim *sim)
3053{
3054 struct umass_softc *sc = (struct umass_softc *)sim->softc;
3055
3056 if (sc == UMASS_GONE)
3057 return;
3058
3059 DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
3060
3061 usb2_do_poll(sc->sc_xfer, UMASS_T_MAX);
3062}
3063
3064
3065/* umass_cam_cb
3066 * finalise a completed CAM command
3067 */
3068
3069static void
3070umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3071 uint8_t status)
3072{
3073 ccb->csio.resid = residue;
3074
3075 switch (status) {
3076 case STATUS_CMD_OK:
3077 ccb->ccb_h.status = CAM_REQ_CMP;
3078 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
3079 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
3080 (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
3081 struct scsi_read_capacity_data *rcap;
3082 uint32_t maxsector;
3083
3084 rcap = (void *)(ccb->csio.data_ptr);
3085 maxsector = scsi_4btoul(rcap->addr) - 1;
3086 scsi_ulto4b(maxsector, rcap->addr);
3087 }
3088 xpt_done(ccb);
3089 break;
3090
3091 case STATUS_CMD_UNKNOWN:
3092 case STATUS_CMD_FAILED:
3093
3094 /* fetch sense data */
3095
3096 /* the rest of the command was filled in at attach */
3097 sc->cam_scsi_sense.length = ccb->csio.sense_len;
3098
3099 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
3100 "sense data\n", ccb->csio.sense_len);
3101
3102 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
3103 sizeof(sc->cam_scsi_sense))) {
3104
3105 if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
3106 (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
3107 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
3108 }
3109 umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
3110 ccb->csio.sense_len, ccb->ccb_h.timeout,
3111 &umass_cam_sense_cb, ccb);
3112 }
3113 break;
3114
3115 default:
3116 /*
3117 * the wire protocol failed and will have recovered
3118 * (hopefully). We return an error to CAM and let CAM retry
3119 * the command if necessary.
3120 */
3121 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3122 xpt_done(ccb);
3123 break;
3124 }
3125}
3126
3127/*
3128 * Finalise a completed autosense operation
3129 */
3130static void
3131umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3132 uint8_t status)
3133{
3134 uint8_t *cmd;
3135 uint8_t key;
3136
3137 switch (status) {
3138 case STATUS_CMD_OK:
3139 case STATUS_CMD_UNKNOWN:
3140 case STATUS_CMD_FAILED:
3141
3142 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
3143 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
3144 } else {
3145 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
3146 }
3147
3148 key = (ccb->csio.sense_data.flags & SSD_KEY);
3149
3150 /*
3151 * Getting sense data always succeeds (apart from wire
3152 * failures):
3153 */
3154 if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3155 (cmd[0] == INQUIRY) &&
3156 (key == SSD_KEY_UNIT_ATTENTION)) {
3157 /*
3158 * Ignore unit attention errors in the case where
3159 * the Unit Attention state is not cleared on
3160 * REQUEST SENSE. They will appear again at the next
3161 * command.
3162 */
3163 ccb->ccb_h.status = CAM_REQ_CMP;
3164 } else if (key == SSD_KEY_NO_SENSE) {
3165 /*
3166 * No problem after all (in the case of CBI without
3167 * CCI)
3168 */
3169 ccb->ccb_h.status = CAM_REQ_CMP;
3170 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
3171 (cmd[0] == READ_CAPACITY) &&
3172 (key == SSD_KEY_UNIT_ATTENTION)) {
3173 /*
3174 * Some devices do not clear the unit attention error
3175 * on request sense. We insert a test unit ready
3176 * command to make sure we clear the unit attention
3177 * condition, then allow the retry to proceed as
3178 * usual.
3179 */
3180
3181 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3182 | CAM_AUTOSNS_VALID;
3183 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3184
3185#if 0
3186 DELAY(300000);
3187#endif
3188 DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
3189 "TEST_UNIT_READY\n");
3190
3191 /* the rest of the command was filled in at attach */
3192
3193 if (umass_std_transform(sc, ccb,
3194 &sc->cam_scsi_test_unit_ready.opcode,
3195 sizeof(sc->cam_scsi_test_unit_ready))) {
3196 umass_command_start(sc, DIR_NONE, NULL, 0,
3197 ccb->ccb_h.timeout,
3198 &umass_cam_quirk_cb, ccb);
3199 }
3200 break;
3201 } else {
3202 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3203 | CAM_AUTOSNS_VALID;
3204 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3205 }
3206 xpt_done(ccb);
3207 break;
3208
3209 default:
3210 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
3211 "status %d\n", status);
3212 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
3213 xpt_done(ccb);
3214 }
3215}
3216
3217/*
3218 * This completion code just handles the fact that we sent a test-unit-ready
3219 * after having previously failed a READ CAPACITY with CHECK_COND. Even
3220 * though this command succeeded, we have to tell CAM to retry.
3221 */
3222static void
3223umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
3224 uint8_t status)
3225{
3226 DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
3227 "returned status %d\n", status);
3228
3229 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
3230 | CAM_AUTOSNS_VALID;
3231 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
3232 xpt_done(ccb);
3233}
3234
3235/*
3236 * SCSI specific functions
3237 */
3238
3239static uint8_t
3240umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3241 uint8_t cmd_len)
3242{
3243 if ((cmd_len == 0) ||
3244 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3245 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3246 "length: %d bytes\n", cmd_len);
3247 return (0); /* failure */
3248 }
3249 sc->sc_transfer.cmd_len = cmd_len;
3250
3251 switch (cmd_ptr[0]) {
3252 case TEST_UNIT_READY:
3253 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3254 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3255 "to START_UNIT\n");
3256 bzero(sc->sc_transfer.cmd_data, cmd_len);
3257 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3258 sc->sc_transfer.cmd_data[4] = SSS_START;
3259 return (1);
3260 }
3261 break;
3262
3263 case INQUIRY:
3264 /*
3265 * some drives wedge when asked for full inquiry
3266 * information.
3267 */
3268 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3269 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3270 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3271 return (1);
3272 }
3273 break;
3274 }
3275
3276 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3277 return (1);
3278}
3279
3280static uint8_t
3281umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
3282{
3283 if ((cmd_len == 0) ||
3284 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3285 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3286 "length: %d bytes\n", cmd_len);
3287 return (0); /* failure */
3288 }
3289 switch (cmd_ptr[0]) {
3290 /* these commands are defined in RBC: */
3291 case READ_10:
3292 case READ_CAPACITY:
3293 case START_STOP_UNIT:
3294 case SYNCHRONIZE_CACHE:
3295 case WRITE_10:
3296 case 0x2f: /* VERIFY_10 is absent from
3297 * scsi_all.h??? */
3298 case INQUIRY:
3299 case MODE_SELECT_10:
3300 case MODE_SENSE_10:
3301 case TEST_UNIT_READY:
3302 case WRITE_BUFFER:
3303 /*
3304 * The following commands are not listed in my copy of the
3305 * RBC specs. CAM however seems to want those, and at least
3306 * the Sony DSC device appears to support those as well
3307 */
3308 case REQUEST_SENSE:
3309 case PREVENT_ALLOW:
3310
3311 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3312
3313 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
3314 bzero(sc->sc_transfer.cmd_data + cmd_len, 12 - cmd_len);
3315 cmd_len = 12;
3316 }
3317 sc->sc_transfer.cmd_len = cmd_len;
3318 return (1); /* sucess */
3319
3320 /* All other commands are not legal in RBC */
3321 default:
3322 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
3323 "command 0x%02x\n", cmd_ptr[0]);
3324 return (0); /* failure */
3325 }
3326}
3327
3328static uint8_t
3329umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3330 uint8_t cmd_len)
3331{
3332 if ((cmd_len == 0) ||
3333 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3334 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3335 "length: %d bytes\n", cmd_len);
3336 return (0); /* failure */
3337 }
3338 /* An UFI command is always 12 bytes in length */
3339 sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
3340
3341 /* Zero the command data */
3342 bzero(sc->sc_transfer.cmd_data, UFI_COMMAND_LENGTH);
3343
3344 switch (cmd_ptr[0]) {
3345 /*
3346 * Commands of which the format has been verified. They
3347 * should work. Copy the command into the (zeroed out)
3348 * destination buffer.
3349 */
3350 case TEST_UNIT_READY:
3351 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3352 /*
3353 * Some devices do not support this command. Start
3354 * Stop Unit should give the same results
3355 */
3356 DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
3357 "to START_UNIT\n");
3358
3359 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3360 sc->sc_transfer.cmd_data[4] = SSS_START;
3361 return (1);
3362 }
3363 break;
3364
3365 case REZERO_UNIT:
3366 case REQUEST_SENSE:
3367 case FORMAT_UNIT:
3368 case INQUIRY:
3369 case START_STOP_UNIT:
3370 case SEND_DIAGNOSTIC:
3371 case PREVENT_ALLOW:
3372 case READ_CAPACITY:
3373 case READ_10:
3374 case WRITE_10:
3375 case POSITION_TO_ELEMENT: /* SEEK_10 */
3376 case WRITE_AND_VERIFY:
3377 case VERIFY:
3378 case MODE_SELECT_10:
3379 case MODE_SENSE_10:
3380 case READ_12:
3381 case WRITE_12:
3382 case READ_FORMAT_CAPACITIES:
3383 break;
3384
3385 /*
3386 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
3387 * required for UFI devices, so it is appropriate to fake
3388 * success.
3389 */
3390 case SYNCHRONIZE_CACHE:
3391 return (2);
3392
3393 default:
3394 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
3395 "command 0x%02x\n", cmd_ptr[0]);
3396 return (0); /* failure */
3397 }
3398
3399 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3400 return (1); /* success */
3401}
3402
3403/*
3404 * 8070i (ATAPI) specific functions
3405 */
3406static uint8_t
3407umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
3408 uint8_t cmd_len)
3409{
3410 if ((cmd_len == 0) ||
3411 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
3412 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
3413 "length: %d bytes\n", cmd_len);
3414 return (0); /* failure */
3415 }
3416 /* An ATAPI command is always 12 bytes in length. */
3417 sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
3418
3419 /* Zero the command data */
3420 bzero(sc->sc_transfer.cmd_data, ATAPI_COMMAND_LENGTH);
3421
3422 switch (cmd_ptr[0]) {
3423 /*
3424 * Commands of which the format has been verified. They
3425 * should work. Copy the command into the destination
3426 * buffer.
3427 */
3428 case INQUIRY:
3429 /*
3430 * some drives wedge when asked for full inquiry
3431 * information.
3432 */
3433 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
3434 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3435
3436 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
3437 return (1);
3438 }
3439 break;
3440
3441 case TEST_UNIT_READY:
3442 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
3443 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
3444 "to START_UNIT\n");
3445 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
3446 sc->sc_transfer.cmd_data[4] = SSS_START;
3447 return (1);
3448 }
3449 break;
3450
3451 case REZERO_UNIT:
3452 case REQUEST_SENSE:
3453 case START_STOP_UNIT:
3454 case SEND_DIAGNOSTIC:
3455 case PREVENT_ALLOW:
3456 case READ_CAPACITY:
3457 case READ_10:
3458 case WRITE_10:
3459 case POSITION_TO_ELEMENT: /* SEEK_10 */
3460 case SYNCHRONIZE_CACHE:
3461 case MODE_SELECT_10:
3462 case MODE_SENSE_10:
3463 case READ_BUFFER:
3464 case 0x42: /* READ_SUBCHANNEL */
3465 case 0x43: /* READ_TOC */
3466 case 0x44: /* READ_HEADER */
3467 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */
3468 case 0x48: /* PLAY_TRACK */
3469 case 0x49: /* PLAY_TRACK_REL */
3470 case 0x4b: /* PAUSE */
3471 case 0x51: /* READ_DISK_INFO */
3472 case 0x52: /* READ_TRACK_INFO */
3473 case 0x54: /* SEND_OPC */
3474 case 0x59: /* READ_MASTER_CUE */
3475 case 0x5b: /* CLOSE_TR_SESSION */
3476 case 0x5c: /* READ_BUFFER_CAP */
3477 case 0x5d: /* SEND_CUE_SHEET */
3478 case 0xa1: /* BLANK */
3479 case 0xa5: /* PLAY_12 */
3480 case 0xa6: /* EXCHANGE_MEDIUM */
3481 case 0xad: /* READ_DVD_STRUCTURE */
3482 case 0xbb: /* SET_CD_SPEED */
3483 case 0xe5: /* READ_TRACK_INFO_PHILIPS */
3484 break;;
3485
3486 case READ_12:
3487 case WRITE_12:
3488 default:
3489 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
3490 "command 0x%02x - trying anyway\n",
3491 cmd_ptr[0]);
3492 break;;
3493 }
3494
3495 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
3496 return (1); /* success */
3497}
3498
3499static uint8_t
3500umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
3501 uint8_t cmdlen)
3502{
3503 return (0); /* failure */
3504}
3505
3506static uint8_t
3507umass_std_transform(struct umass_softc *sc, union ccb *ccb,
3508 uint8_t *cmd, uint8_t cmdlen)
3509{
3510 uint8_t retval;
3511
3512 retval = (sc->sc_transform) (sc, cmd, cmdlen);
3513
3514 if (retval == 2) {
3515 ccb->ccb_h.status = CAM_REQ_CMP;
3516 xpt_done(ccb);
3517 return (0);
3518 } else if (retval == 0) {
3519 ccb->ccb_h.status = CAM_REQ_INVALID;
3520 xpt_done(ccb);
3521 return (0);
3522 }
3523 /* Command should be executed */
3524 return (1);
3525}
3526
3527#if USB_DEBUG
3528static void
3529umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3530{
3531 uint8_t *c = cbw->CBWCDB;
3532
3533 uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
3534 uint32_t tag = UGETDW(cbw->dCBWTag);
3535
3536 uint8_t clen = cbw->bCDBLength;
3537 uint8_t flags = cbw->bCBWFlags;
3538 uint8_t lun = cbw->bCBWLUN;
3539
3540 DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
3541 "(0x%02x%02x%02x%02x%02x%02x%s), "
3542 "data = %db, lun = %d, dir = %s\n",
3543 tag, clen,
3544 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
3545 dlen, lun, (flags == CBWFLAGS_IN ? "in" :
3546 (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
3547}
3548
3549static void
3550umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3551{
3552 uint32_t sig = UGETDW(csw->dCSWSignature);
3553 uint32_t tag = UGETDW(csw->dCSWTag);
3554 uint32_t res = UGETDW(csw->dCSWDataResidue);
3555 uint8_t status = csw->bCSWStatus;
3556
3557 DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
3558 "res = %d, status = 0x%02x (%s)\n",
3559 tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
3560 tag, res,
3561 status, (status == CSWSTATUS_GOOD ? "good" :
3562 (status == CSWSTATUS_FAILED ? "failed" :
3563 (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
3564}
3565
3566static void
3567umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
3568{
3569 uint8_t *c = cmd;
3570 uint8_t dir = sc->sc_transfer.dir;
3571
3572 DPRINTF(sc, UDMASS_BBB, "cmd = %db "
3573 "(0x%02x%02x%02x%02x%02x%02x%s), "
3574 "data = %db, dir = %s\n",
3575 cmdlen,
3576 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
3577 sc->sc_transfer.data_len,
3578 (dir == DIR_IN ? "in" :
3579 (dir == DIR_OUT ? "out" :
3580 (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3581}
3582
3583static void
3584umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3585 uint32_t printlen)
3586{
3587 uint32_t i, j;
3588 char s1[40];
3589 char s2[40];
3590 char s3[5];
3591
3592 s1[0] = '\0';
3593 s3[0] = '\0';
3594
3595 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3596 for (i = 0; (i < buflen) && (i < printlen); i++) {
3597 j = i % 16;
3598 if (j == 0 && i != 0) {
3599 DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3600 s1, s2);
3601 s2[0] = '\0';
3602 }
3603 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3604 }
3605 if (buflen > printlen)
3606 sprintf(s3, " ...");
3607 DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",
3608 s1, s2, s3);
3609}
3610
3611#endif