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