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