source: rtems-libbsd/freebsd/sys/dev/usb/storage/umass.c @ 7eeb079

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 7eeb079 was 7eeb079, checked in by Sebastian Huber <sebastian.huber@…>, on 02/02/15 at 13:27:13

Update to FreeBSD 9.3

  • Property mode set to 100644
File size: 79.8 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD$");
5
6/*-
7 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
8 *                    Nick Hibma <n_hibma@FreeBSD.org>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *      $FreeBSD$
33 *      $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
34 */
35
36/* Also already merged from NetBSD:
37 *      $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
38 *      $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
39 *      $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
40 *      $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
41 */
42
43/*
44 * Universal Serial Bus Mass Storage Class specs:
45 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
46 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
47 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
48 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
49 */
50
51/*
52 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
53 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
54 */
55
56/*
57 * The driver handles 3 Wire Protocols
58 * - Command/Bulk/Interrupt (CBI)
59 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
60 * - Mass Storage Bulk-Only (BBB)
61 *   (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
62 *
63 * Over these wire protocols it handles the following command protocols
64 * - SCSI
65 * - UFI (floppy command set)
66 * - 8070i (ATAPI)
67 *
68 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
69 * sc->sc_transform method is used to convert the commands into the appropriate
70 * format (if at all necessary). For example, UFI requires all commands to be
71 * 12 bytes in length amongst other things.
72 *
73 * The source code below is marked and can be split into a number of pieces
74 * (in this order):
75 *
76 * - probe/attach/detach
77 * - generic transfer routines
78 * - BBB
79 * - CBI
80 * - CBI_I (in addition to functions from CBI)
81 * - CAM (Common Access Method)
82 * - SCSI
83 * - UFI
84 * - 8070i (ATAPI)
85 *
86 * The protocols are implemented using a state machine, for the transfers as
87 * well as for the resets. The state machine is contained in umass_t_*_callback.
88 * The state machine is started through either umass_command_start() or
89 * umass_reset().
90 *
91 * The reason for doing this is a) CAM performs a lot better this way and b) it
92 * avoids using tsleep from interrupt context (for example after a failed
93 * transfer).
94 */
95
96/*
97 * The SCSI related part of this driver has been derived from the
98 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org).
99 *
100 * The CAM layer uses so called actions which are messages sent to the host
101 * adapter for completion. The actions come in through umass_cam_action. The
102 * appropriate block of routines is called depending on the transport protocol
103 * in use. When the transfer has finished, these routines call
104 * umass_cam_cb again to complete the CAM command.
105 */
106
107#include <sys/stdint.h>
108#include <sys/stddef.h>
109#include <rtems/bsd/sys/param.h>
110#include <sys/queue.h>
111#include <rtems/bsd/sys/types.h>
112#include <sys/systm.h>
113#include <sys/kernel.h>
114#include <sys/bus.h>
115#include <sys/module.h>
116#include <rtems/bsd/sys/lock.h>
117#include <sys/mutex.h>
118#include <sys/condvar.h>
119#include <sys/sysctl.h>
120#include <sys/sx.h>
121#include <rtems/bsd/sys/unistd.h>
122#include <sys/callout.h>
123#include <sys/malloc.h>
124#include <sys/priv.h>
125
126#include <dev/usb/usb.h>
127#include <dev/usb/usbdi.h>
128#include <dev/usb/usbdi_util.h>
129#include <rtems/bsd/local/usbdevs.h>
130
131#include <dev/usb/quirk/usb_quirk.h>
132
133#include <cam/cam.h>
134#include <cam/cam_ccb.h>
135#include <cam/cam_sim.h>
136#include <cam/cam_xpt_sim.h>
137#include <cam/scsi/scsi_all.h>
138#include <cam/scsi/scsi_da.h>
139
140#include <cam/cam_periph.h>
141
142#ifdef USB_DEBUG
143#define DIF(m, x)                               \
144  do {                                          \
145    if (umass_debug & (m)) { x ; }              \
146  } while (0)
147
148#define DPRINTF(sc, m, fmt, ...)                        \
149  do {                                                  \
150    if (umass_debug & (m)) {                            \
151        printf("%s:%s: " fmt,                           \
152               (sc) ? (const char *)(sc)->sc_name :     \
153               (const char *)"umassX",                  \
154                __FUNCTION__ ,## __VA_ARGS__);          \
155    }                                                   \
156  } while (0)
157
158#define UDMASS_GEN      0x00010000      /* general */
159#define UDMASS_SCSI     0x00020000      /* scsi */
160#define UDMASS_UFI      0x00040000      /* ufi command set */
161#define UDMASS_ATAPI    0x00080000      /* 8070i command set */
162#define UDMASS_CMD      (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
163#define UDMASS_USB      0x00100000      /* USB general */
164#define UDMASS_BBB      0x00200000      /* Bulk-Only transfers */
165#define UDMASS_CBI      0x00400000      /* CBI transfers */
166#define UDMASS_WIRE     (UDMASS_BBB|UDMASS_CBI)
167#define UDMASS_ALL      0xffff0000      /* all of the above */
168static int umass_debug;
169static int umass_throttle;
170
171static SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
172SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
173    &umass_debug, 0, "umass debug level");
174TUNABLE_INT("hw.usb.umass.debug", &umass_debug);
175SYSCTL_INT(_hw_usb_umass, OID_AUTO, throttle, CTLFLAG_RW | CTLFLAG_TUN,
176    &umass_throttle, 0, "Forced delay between commands in milliseconds");
177TUNABLE_INT("hw.usb.umass.throttle", &umass_throttle);
178#else
179#define DIF(...) do { } while (0)
180#define DPRINTF(...) do { } while (0)
181#endif
182
183#define UMASS_BULK_SIZE (1 << 17)
184#define UMASS_CBI_DIAGNOSTIC_CMDLEN 12  /* bytes */
185#define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN)        /* bytes */
186
187/* USB transfer definitions */
188
189#define UMASS_T_BBB_RESET1      0       /* Bulk-Only */
190#define UMASS_T_BBB_RESET2      1
191#define UMASS_T_BBB_RESET3      2
192#define UMASS_T_BBB_COMMAND     3
193#define UMASS_T_BBB_DATA_READ   4
194#define UMASS_T_BBB_DATA_RD_CS  5
195#define UMASS_T_BBB_DATA_WRITE  6
196#define UMASS_T_BBB_DATA_WR_CS  7
197#define UMASS_T_BBB_STATUS      8
198#define UMASS_T_BBB_MAX         9
199
200#define UMASS_T_CBI_RESET1      0       /* CBI */
201#define UMASS_T_CBI_RESET2      1
202#define UMASS_T_CBI_RESET3      2
203#define UMASS_T_CBI_COMMAND     3
204#define UMASS_T_CBI_DATA_READ   4
205#define UMASS_T_CBI_DATA_RD_CS  5
206#define UMASS_T_CBI_DATA_WRITE  6
207#define UMASS_T_CBI_DATA_WR_CS  7
208#define UMASS_T_CBI_STATUS      8
209#define UMASS_T_CBI_RESET4      9
210#define UMASS_T_CBI_MAX        10
211
212#define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
213
214/* Generic definitions */
215
216/* Direction for transfer */
217#define DIR_NONE        0
218#define DIR_IN          1
219#define DIR_OUT         2
220
221/* device name */
222#define DEVNAME         "umass"
223#define DEVNAME_SIM     "umass-sim"
224
225/* Approximate maximum transfer speeds (assumes 33% overhead). */
226#define UMASS_FULL_TRANSFER_SPEED       1000
227#define UMASS_HIGH_TRANSFER_SPEED       40000
228#define UMASS_SUPER_TRANSFER_SPEED      400000
229#define UMASS_FLOPPY_TRANSFER_SPEED     20
230
231#define UMASS_TIMEOUT                   5000    /* ms */
232
233/* CAM specific definitions */
234
235#define UMASS_SCSIID_MAX        1       /* maximum number of drives expected */
236#define UMASS_SCSIID_HOST       UMASS_SCSIID_MAX
237
238/* Bulk-Only features */
239
240#define UR_BBB_RESET            0xff    /* Bulk-Only reset */
241#define UR_BBB_GET_MAX_LUN      0xfe    /* Get maximum lun */
242
243/* Command Block Wrapper */
244typedef struct {
245        uDWord  dCBWSignature;
246#define CBWSIGNATURE    0x43425355
247        uDWord  dCBWTag;
248        uDWord  dCBWDataTransferLength;
249        uByte   bCBWFlags;
250#define CBWFLAGS_OUT    0x00
251#define CBWFLAGS_IN     0x80
252        uByte   bCBWLUN;
253        uByte   bCDBLength;
254#define CBWCDBLENGTH    16
255        uByte   CBWCDB[CBWCDBLENGTH];
256} __packed umass_bbb_cbw_t;
257
258#define UMASS_BBB_CBW_SIZE      31
259
260/* Command Status Wrapper */
261typedef struct {
262        uDWord  dCSWSignature;
263#define CSWSIGNATURE    0x53425355
264#define CSWSIGNATURE_IMAGINATION_DBX1   0x43425355
265#define CSWSIGNATURE_OLYMPUS_C1 0x55425355
266        uDWord  dCSWTag;
267        uDWord  dCSWDataResidue;
268        uByte   bCSWStatus;
269#define CSWSTATUS_GOOD  0x0
270#define CSWSTATUS_FAILED        0x1
271#define CSWSTATUS_PHASE 0x2
272} __packed umass_bbb_csw_t;
273
274#define UMASS_BBB_CSW_SIZE      13
275
276/* CBI features */
277
278#define UR_CBI_ADSC     0x00
279
280typedef union {
281        struct {
282                uint8_t type;
283#define IDB_TYPE_CCI            0x00
284                uint8_t value;
285#define IDB_VALUE_PASS          0x00
286#define IDB_VALUE_FAIL          0x01
287#define IDB_VALUE_PHASE         0x02
288#define IDB_VALUE_PERSISTENT    0x03
289#define IDB_VALUE_STATUS_MASK   0x03
290        } __packed common;
291
292        struct {
293                uint8_t asc;
294                uint8_t ascq;
295        } __packed ufi;
296} __packed umass_cbi_sbl_t;
297
298struct umass_softc;                     /* see below */
299
300typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
301        uint32_t residue, uint8_t status);
302
303#define STATUS_CMD_OK           0       /* everything ok */
304#define STATUS_CMD_UNKNOWN      1       /* will have to fetch sense */
305#define STATUS_CMD_FAILED       2       /* transfer was ok, command failed */
306#define STATUS_WIRE_FAILED      3       /* couldn't even get command across */
307
308typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
309        uint8_t cmd_len);
310
311/* Wire and command protocol */
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#define NO_QUIRKS               0x0000
324        /*
325         * The drive does not support Test Unit Ready. Convert to Start Unit
326         */
327#define NO_TEST_UNIT_READY      0x0001
328        /*
329         * The drive does not reset the Unit Attention state after REQUEST
330         * SENSE has been sent. The INQUIRY command does not reset the UA
331         * either, and so CAM runs in circles trying to retrieve the initial
332         * INQUIRY data.
333         */
334#define RS_NO_CLEAR_UA          0x0002
335        /* The drive does not support START STOP.  */
336#define NO_START_STOP           0x0004
337        /* Don't ask for full inquiry data (255b).  */
338#define FORCE_SHORT_INQUIRY     0x0008
339        /* Needs to be initialised the Shuttle way */
340#define SHUTTLE_INIT            0x0010
341        /* Drive needs to be switched to alternate iface 1 */
342#define ALT_IFACE_1             0x0020
343        /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
344#define FLOPPY_SPEED            0x0040
345        /* The device can't count and gets the residue of transfers wrong */
346#define IGNORE_RESIDUE          0x0080
347        /* No GetMaxLun call */
348#define NO_GETMAXLUN            0x0100
349        /* The device uses a weird CSWSIGNATURE. */
350#define WRONG_CSWSIG            0x0200
351        /* Device cannot handle INQUIRY so fake a generic response */
352#define NO_INQUIRY              0x0400
353        /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
354#define NO_INQUIRY_EVPD         0x0800
355        /* Pad all RBC requests to 12 bytes. */
356#define RBC_PAD_TO_12           0x1000
357        /*
358         * Device reports number of sectors from READ_CAPACITY, not max
359         * sector number.
360         */
361#define READ_CAPACITY_OFFBY1    0x2000
362        /*
363         * Device cannot handle a SCSI synchronize cache command.  Normally
364         * this quirk would be handled in the cam layer, but for IDE bridges
365         * we need to associate the quirk with the bridge and not the
366         * underlying disk device.  This is handled by faking a success
367         * result.
368         */
369#define NO_SYNCHRONIZE_CACHE    0x4000
370        /* Device does not support 'PREVENT/ALLOW MEDIUM REMOVAL'. */
371#define NO_PREVENT_ALLOW        0x8000
372
373struct umass_softc {
374
375        struct scsi_sense cam_scsi_sense;
376        struct scsi_test_unit_ready cam_scsi_test_unit_ready;
377        struct mtx sc_mtx;
378        struct {
379                uint8_t *data_ptr;
380                union ccb *ccb;
381                umass_callback_t *callback;
382
383                uint32_t data_len;      /* bytes */
384                uint32_t data_rem;      /* bytes */
385                uint32_t data_timeout;  /* ms */
386                uint32_t actlen;        /* bytes */
387
388                uint8_t cmd_data[UMASS_MAX_CMDLEN];
389                uint8_t cmd_len;        /* bytes */
390                uint8_t dir;
391                uint8_t lun;
392        }       sc_transfer;
393
394        /* Bulk specific variables for transfers in progress */
395        umass_bbb_cbw_t cbw;            /* command block wrapper */
396        umass_bbb_csw_t csw;            /* command status wrapper */
397
398        /* CBI specific variables for transfers in progress */
399        umass_cbi_sbl_t sbl;            /* status block */
400
401        device_t sc_dev;
402        struct usb_device *sc_udev;
403        struct cam_sim *sc_sim;         /* SCSI Interface Module */
404        struct usb_xfer *sc_xfer[UMASS_T_MAX];
405
406        /*
407         * The command transform function is used to convert the SCSI
408         * commands into their derivatives, like UFI, ATAPI, and friends.
409         */
410        umass_transform_t *sc_transform;
411
412        uint32_t sc_unit;
413        uint32_t sc_quirks;             /* they got it almost right */
414        uint32_t sc_proto;              /* wire and cmd protocol */
415
416        uint8_t sc_name[16];
417        uint8_t sc_iface_no;            /* interface number */
418        uint8_t sc_maxlun;              /* maximum LUN number, inclusive */
419        uint8_t sc_last_xfer_index;
420        uint8_t sc_status_try;
421};
422
423struct umass_probe_proto {
424        uint32_t quirks;
425        uint32_t proto;
426
427        int     error;
428};
429
430/* prototypes */
431
432static device_probe_t umass_probe;
433static device_attach_t umass_attach;
434static device_detach_t umass_detach;
435
436static usb_callback_t umass_tr_error;
437static usb_callback_t umass_t_bbb_reset1_callback;
438static usb_callback_t umass_t_bbb_reset2_callback;
439static usb_callback_t umass_t_bbb_reset3_callback;
440static usb_callback_t umass_t_bbb_command_callback;
441static usb_callback_t umass_t_bbb_data_read_callback;
442static usb_callback_t umass_t_bbb_data_rd_cs_callback;
443static usb_callback_t umass_t_bbb_data_write_callback;
444static usb_callback_t umass_t_bbb_data_wr_cs_callback;
445static usb_callback_t umass_t_bbb_status_callback;
446static usb_callback_t umass_t_cbi_reset1_callback;
447static usb_callback_t umass_t_cbi_reset2_callback;
448static usb_callback_t umass_t_cbi_reset3_callback;
449static usb_callback_t umass_t_cbi_reset4_callback;
450static usb_callback_t umass_t_cbi_command_callback;
451static usb_callback_t umass_t_cbi_data_read_callback;
452static usb_callback_t umass_t_cbi_data_rd_cs_callback;
453static usb_callback_t umass_t_cbi_data_write_callback;
454static usb_callback_t umass_t_cbi_data_wr_cs_callback;
455static usb_callback_t umass_t_cbi_status_callback;
456
457static void     umass_cancel_ccb(struct umass_softc *);
458static void     umass_init_shuttle(struct umass_softc *);
459static void     umass_reset(struct umass_softc *);
460static void     umass_t_bbb_data_clear_stall_callback(struct usb_xfer *,
461                    uint8_t, uint8_t, usb_error_t);
462static void     umass_command_start(struct umass_softc *, uint8_t, void *,
463                    uint32_t, uint32_t, umass_callback_t *, union ccb *);
464static uint8_t  umass_bbb_get_max_lun(struct umass_softc *);
465static void     umass_cbi_start_status(struct umass_softc *);
466static void     umass_t_cbi_data_clear_stall_callback(struct usb_xfer *,
467                    uint8_t, uint8_t, usb_error_t);
468static int      umass_cam_attach_sim(struct umass_softc *);
469#ifndef __rtems__
470static void     umass_cam_attach(struct umass_softc *);
471#endif /* __rtems__ */
472static void     umass_cam_detach_sim(struct umass_softc *);
473static void     umass_cam_action(struct cam_sim *, union ccb *);
474static void     umass_cam_poll(struct cam_sim *);
475static void     umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
476                    uint8_t);
477static void     umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
478                    uint8_t);
479static void     umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
480                    uint8_t);
481static uint8_t  umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
482static uint8_t  umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
483static uint8_t  umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
484static uint8_t  umass_atapi_transform(struct umass_softc *, uint8_t *,
485                    uint8_t);
486static uint8_t  umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
487static uint8_t  umass_std_transform(struct umass_softc *, union ccb *, uint8_t
488                    *, uint8_t);
489
490#ifdef USB_DEBUG
491static void     umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
492static void     umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
493static void     umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
494static void     umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
495                    uint32_t);
496#endif
497
498static struct usb_config umass_bbb_config[UMASS_T_BBB_MAX] = {
499
500        [UMASS_T_BBB_RESET1] = {
501                .type = UE_CONTROL,
502                .endpoint = 0x00,       /* Control pipe */
503                .direction = UE_DIR_ANY,
504                .bufsize = sizeof(struct usb_device_request),
505                .callback = &umass_t_bbb_reset1_callback,
506                .timeout = 5000,        /* 5 seconds */
507                .interval = 500,        /* 500 milliseconds */
508        },
509
510        [UMASS_T_BBB_RESET2] = {
511                .type = UE_CONTROL,
512                .endpoint = 0x00,       /* Control pipe */
513                .direction = UE_DIR_ANY,
514                .bufsize = sizeof(struct usb_device_request),
515                .callback = &umass_t_bbb_reset2_callback,
516                .timeout = 5000,        /* 5 seconds */
517                .interval = 50, /* 50 milliseconds */
518        },
519
520        [UMASS_T_BBB_RESET3] = {
521                .type = UE_CONTROL,
522                .endpoint = 0x00,       /* Control pipe */
523                .direction = UE_DIR_ANY,
524                .bufsize = sizeof(struct usb_device_request),
525                .callback = &umass_t_bbb_reset3_callback,
526                .timeout = 5000,        /* 5 seconds */
527                .interval = 50, /* 50 milliseconds */
528        },
529
530        [UMASS_T_BBB_COMMAND] = {
531                .type = UE_BULK,
532                .endpoint = UE_ADDR_ANY,
533                .direction = UE_DIR_OUT,
534                .bufsize = sizeof(umass_bbb_cbw_t),
535                .callback = &umass_t_bbb_command_callback,
536                .timeout = 5000,        /* 5 seconds */
537        },
538
539        [UMASS_T_BBB_DATA_READ] = {
540                .type = UE_BULK,
541                .endpoint = UE_ADDR_ANY,
542                .direction = UE_DIR_IN,
543                .bufsize = UMASS_BULK_SIZE,
544                .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
545                .callback = &umass_t_bbb_data_read_callback,
546                .timeout = 0,   /* overwritten later */
547        },
548
549        [UMASS_T_BBB_DATA_RD_CS] = {
550                .type = UE_CONTROL,
551                .endpoint = 0x00,       /* Control pipe */
552                .direction = UE_DIR_ANY,
553                .bufsize = sizeof(struct usb_device_request),
554                .callback = &umass_t_bbb_data_rd_cs_callback,
555                .timeout = 5000,        /* 5 seconds */
556        },
557
558        [UMASS_T_BBB_DATA_WRITE] = {
559                .type = UE_BULK,
560                .endpoint = UE_ADDR_ANY,
561                .direction = UE_DIR_OUT,
562                .bufsize = UMASS_BULK_SIZE,
563                .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
564                .callback = &umass_t_bbb_data_write_callback,
565                .timeout = 0,   /* overwritten later */
566        },
567
568        [UMASS_T_BBB_DATA_WR_CS] = {
569                .type = UE_CONTROL,
570                .endpoint = 0x00,       /* Control pipe */
571                .direction = UE_DIR_ANY,
572                .bufsize = sizeof(struct usb_device_request),
573                .callback = &umass_t_bbb_data_wr_cs_callback,
574                .timeout = 5000,        /* 5 seconds */
575        },
576
577        [UMASS_T_BBB_STATUS] = {
578                .type = UE_BULK,
579                .endpoint = UE_ADDR_ANY,
580                .direction = UE_DIR_IN,
581                .bufsize = sizeof(umass_bbb_csw_t),
582                .flags = {.short_xfer_ok = 1,},
583                .callback = &umass_t_bbb_status_callback,
584                .timeout = 5000,        /* ms */
585        },
586};
587
588static struct usb_config umass_cbi_config[UMASS_T_CBI_MAX] = {
589
590        [UMASS_T_CBI_RESET1] = {
591                .type = UE_CONTROL,
592                .endpoint = 0x00,       /* Control pipe */
593                .direction = UE_DIR_ANY,
594                .bufsize = (sizeof(struct usb_device_request) +
595                    UMASS_CBI_DIAGNOSTIC_CMDLEN),
596                .callback = &umass_t_cbi_reset1_callback,
597                .timeout = 5000,        /* 5 seconds */
598                .interval = 500,        /* 500 milliseconds */
599        },
600
601        [UMASS_T_CBI_RESET2] = {
602                .type = UE_CONTROL,
603                .endpoint = 0x00,       /* Control pipe */
604                .direction = UE_DIR_ANY,
605                .bufsize = sizeof(struct usb_device_request),
606                .callback = &umass_t_cbi_reset2_callback,
607                .timeout = 5000,        /* 5 seconds */
608                .interval = 50, /* 50 milliseconds */
609        },
610
611        [UMASS_T_CBI_RESET3] = {
612                .type = UE_CONTROL,
613                .endpoint = 0x00,       /* Control pipe */
614                .direction = UE_DIR_ANY,
615                .bufsize = sizeof(struct usb_device_request),
616                .callback = &umass_t_cbi_reset3_callback,
617                .timeout = 5000,        /* 5 seconds */
618                .interval = 50, /* 50 milliseconds */
619        },
620
621        [UMASS_T_CBI_COMMAND] = {
622                .type = UE_CONTROL,
623                .endpoint = 0x00,       /* Control pipe */
624                .direction = UE_DIR_ANY,
625                .bufsize = (sizeof(struct usb_device_request) +
626                    UMASS_MAX_CMDLEN),
627                .callback = &umass_t_cbi_command_callback,
628                .timeout = 5000,        /* 5 seconds */
629        },
630
631        [UMASS_T_CBI_DATA_READ] = {
632                .type = UE_BULK,
633                .endpoint = UE_ADDR_ANY,
634                .direction = UE_DIR_IN,
635                .bufsize = UMASS_BULK_SIZE,
636                .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
637                .callback = &umass_t_cbi_data_read_callback,
638                .timeout = 0,   /* overwritten later */
639        },
640
641        [UMASS_T_CBI_DATA_RD_CS] = {
642                .type = UE_CONTROL,
643                .endpoint = 0x00,       /* Control pipe */
644                .direction = UE_DIR_ANY,
645                .bufsize = sizeof(struct usb_device_request),
646                .callback = &umass_t_cbi_data_rd_cs_callback,
647                .timeout = 5000,        /* 5 seconds */
648        },
649
650        [UMASS_T_CBI_DATA_WRITE] = {
651                .type = UE_BULK,
652                .endpoint = UE_ADDR_ANY,
653                .direction = UE_DIR_OUT,
654                .bufsize = UMASS_BULK_SIZE,
655                .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,.ext_buffer=1,},
656                .callback = &umass_t_cbi_data_write_callback,
657                .timeout = 0,   /* overwritten later */
658        },
659
660        [UMASS_T_CBI_DATA_WR_CS] = {
661                .type = UE_CONTROL,
662                .endpoint = 0x00,       /* Control pipe */
663                .direction = UE_DIR_ANY,
664                .bufsize = sizeof(struct usb_device_request),
665                .callback = &umass_t_cbi_data_wr_cs_callback,
666                .timeout = 5000,        /* 5 seconds */
667        },
668
669        [UMASS_T_CBI_STATUS] = {
670                .type = UE_INTERRUPT,
671                .endpoint = UE_ADDR_ANY,
672                .direction = UE_DIR_IN,
673                .flags = {.short_xfer_ok = 1,.no_pipe_ok = 1,},
674                .bufsize = sizeof(umass_cbi_sbl_t),
675                .callback = &umass_t_cbi_status_callback,
676                .timeout = 5000,        /* ms */
677        },
678
679        [UMASS_T_CBI_RESET4] = {
680                .type = UE_CONTROL,
681                .endpoint = 0x00,       /* Control pipe */
682                .direction = UE_DIR_ANY,
683                .bufsize = sizeof(struct usb_device_request),
684                .callback = &umass_t_cbi_reset4_callback,
685                .timeout = 5000,        /* ms */
686        },
687};
688
689/* If device cannot return valid inquiry data, fake it */
690static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
691        0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
692         /* additional_length */ 31, 0, 0, 0
693};
694
695#define UFI_COMMAND_LENGTH      12      /* UFI commands are always 12 bytes */
696#define ATAPI_COMMAND_LENGTH    12      /* ATAPI commands are always 12 bytes */
697
698static devclass_t umass_devclass;
699
700static device_method_t umass_methods[] = {
701        /* Device interface */
702        DEVMETHOD(device_probe, umass_probe),
703        DEVMETHOD(device_attach, umass_attach),
704        DEVMETHOD(device_detach, umass_detach),
705        {0, 0}
706};
707
708static driver_t umass_driver = {
709        .name = "umass",
710        .methods = umass_methods,
711        .size = sizeof(struct umass_softc),
712};
713
714DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
715MODULE_DEPEND(umass, usb, 1, 1, 1);
716MODULE_DEPEND(umass, cam, 1, 1, 1);
717MODULE_VERSION(umass, 1);
718
719/*
720 * USB device probe/attach/detach
721 */
722
723static const STRUCT_USB_HOST_ID __used umass_devs[] = {
724        /* generic mass storage class */
725        {USB_IFACE_CLASS(UICLASS_MASS),},
726};
727
728static uint16_t
729umass_get_proto(struct usb_interface *iface)
730{
731        struct usb_interface_descriptor *id;
732        uint16_t retval;
733
734        retval = 0;
735
736        /* Check for a standards compliant device */
737        id = usbd_get_interface_descriptor(iface);
738        if ((id == NULL) ||
739            (id->bInterfaceClass != UICLASS_MASS)) {
740                goto done;
741        }
742        switch (id->bInterfaceSubClass) {
743        case UISUBCLASS_SCSI:
744                retval |= UMASS_PROTO_SCSI;
745                break;
746        case UISUBCLASS_UFI:
747                retval |= UMASS_PROTO_UFI;
748                break;
749        case UISUBCLASS_RBC:
750                retval |= UMASS_PROTO_RBC;
751                break;
752        case UISUBCLASS_SFF8020I:
753        case UISUBCLASS_SFF8070I:
754                retval |= UMASS_PROTO_ATAPI;
755                break;
756        default:
757                goto done;
758        }
759
760        switch (id->bInterfaceProtocol) {
761        case UIPROTO_MASS_CBI:
762                retval |= UMASS_PROTO_CBI;
763                break;
764        case UIPROTO_MASS_CBI_I:
765                retval |= UMASS_PROTO_CBI_I;
766                break;
767        case UIPROTO_MASS_BBB_OLD:
768        case UIPROTO_MASS_BBB:
769                retval |= UMASS_PROTO_BBB;
770                break;
771        default:
772                goto done;
773        }
774done:
775        return (retval);
776}
777
778/*
779 * Match the device we are seeing with the devices supported.
780 */
781static struct umass_probe_proto
782umass_probe_proto(device_t dev, struct usb_attach_arg *uaa)
783{
784        struct umass_probe_proto ret;
785        uint32_t quirks = NO_QUIRKS;
786        uint32_t proto = umass_get_proto(uaa->iface);
787
788        memset(&ret, 0, sizeof(ret));
789        ret.error = BUS_PROBE_GENERIC;
790
791        /* Search for protocol enforcement */
792
793        if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_BBB)) {
794                proto &= ~UMASS_PROTO_WIRE;
795                proto |= UMASS_PROTO_BBB;
796        } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI)) {
797                proto &= ~UMASS_PROTO_WIRE;
798                proto |= UMASS_PROTO_CBI;
799        } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI_I)) {
800                proto &= ~UMASS_PROTO_WIRE;
801                proto |= UMASS_PROTO_CBI_I;
802        }
803
804        if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_SCSI)) {
805                proto &= ~UMASS_PROTO_COMMAND;
806                proto |= UMASS_PROTO_SCSI;
807        } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_ATAPI)) {
808                proto &= ~UMASS_PROTO_COMMAND;
809                proto |= UMASS_PROTO_ATAPI;
810        } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_UFI)) {
811                proto &= ~UMASS_PROTO_COMMAND;
812                proto |= UMASS_PROTO_UFI;
813        } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_RBC)) {
814                proto &= ~UMASS_PROTO_COMMAND;
815                proto |= UMASS_PROTO_RBC;
816        }
817
818        /* Check if the protocol is invalid */
819
820        if ((proto & UMASS_PROTO_COMMAND) == 0) {
821                ret.error = ENXIO;
822                goto done;
823        }
824
825        if ((proto & UMASS_PROTO_WIRE) == 0) {
826                ret.error = ENXIO;
827                goto done;
828        }
829
830        /* Search for quirks */
831
832        if (usb_test_quirk(uaa, UQ_MSC_NO_TEST_UNIT_READY))
833                quirks |= NO_TEST_UNIT_READY;
834        if (usb_test_quirk(uaa, UQ_MSC_NO_RS_CLEAR_UA))
835                quirks |= RS_NO_CLEAR_UA;
836        if (usb_test_quirk(uaa, UQ_MSC_NO_START_STOP))
837                quirks |= NO_START_STOP;
838        if (usb_test_quirk(uaa, UQ_MSC_NO_GETMAXLUN))
839                quirks |= NO_GETMAXLUN;
840        if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY))
841                quirks |= NO_INQUIRY;
842        if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY_EVPD))
843                quirks |= NO_INQUIRY_EVPD;
844        if (usb_test_quirk(uaa, UQ_MSC_NO_PREVENT_ALLOW))
845                quirks |= NO_PREVENT_ALLOW;
846        if (usb_test_quirk(uaa, UQ_MSC_NO_SYNC_CACHE))
847                quirks |= NO_SYNCHRONIZE_CACHE;
848        if (usb_test_quirk(uaa, UQ_MSC_SHUTTLE_INIT))
849                quirks |= SHUTTLE_INIT;
850        if (usb_test_quirk(uaa, UQ_MSC_ALT_IFACE_1))
851                quirks |= ALT_IFACE_1;
852        if (usb_test_quirk(uaa, UQ_MSC_FLOPPY_SPEED))
853                quirks |= FLOPPY_SPEED;
854        if (usb_test_quirk(uaa, UQ_MSC_IGNORE_RESIDUE))
855                quirks |= IGNORE_RESIDUE;
856        if (usb_test_quirk(uaa, UQ_MSC_WRONG_CSWSIG))
857                quirks |= WRONG_CSWSIG;
858        if (usb_test_quirk(uaa, UQ_MSC_RBC_PAD_TO_12))
859                quirks |= RBC_PAD_TO_12;
860        if (usb_test_quirk(uaa, UQ_MSC_READ_CAP_OFFBY1))
861                quirks |= READ_CAPACITY_OFFBY1;
862        if (usb_test_quirk(uaa, UQ_MSC_FORCE_SHORT_INQ))
863                quirks |= FORCE_SHORT_INQUIRY;
864
865done:
866        ret.quirks = quirks;
867        ret.proto = proto;
868        return (ret);
869}
870
871static int
872umass_probe(device_t dev)
873{
874        struct usb_attach_arg *uaa = device_get_ivars(dev);
875        struct umass_probe_proto temp;
876
877        if (uaa->usb_mode != USB_MODE_HOST) {
878                return (ENXIO);
879        }
880        temp = umass_probe_proto(dev, uaa);
881
882        return (temp.error);
883}
884
885static int
886umass_attach(device_t dev)
887{
888        struct umass_softc *sc = device_get_softc(dev);
889        struct usb_attach_arg *uaa = device_get_ivars(dev);
890        struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
891        struct usb_interface_descriptor *id;
892        int err;
893
894        /*
895         * NOTE: the softc struct is cleared in device_set_driver.
896         * We can safely call umass_detach without specifically
897         * initializing the struct.
898         */
899
900        sc->sc_dev = dev;
901        sc->sc_udev = uaa->device;
902        sc->sc_proto = temp.proto;
903        sc->sc_quirks = temp.quirks;
904        sc->sc_unit = device_get_unit(dev);
905
906        snprintf(sc->sc_name, sizeof(sc->sc_name),
907            "%s", device_get_nameunit(dev));
908
909        device_set_usb_desc(dev);
910
911        mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
912            NULL, MTX_DEF | MTX_RECURSE);
913
914        /* get interface index */
915
916        id = usbd_get_interface_descriptor(uaa->iface);
917        if (id == NULL) {
918                device_printf(dev, "failed to get "
919                    "interface number\n");
920                goto detach;
921        }
922        sc->sc_iface_no = id->bInterfaceNumber;
923
924#ifdef USB_DEBUG
925        device_printf(dev, " ");
926
927        switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
928        case UMASS_PROTO_SCSI:
929                printf("SCSI");
930                break;
931        case UMASS_PROTO_ATAPI:
932                printf("8070i (ATAPI)");
933                break;
934        case UMASS_PROTO_UFI:
935                printf("UFI");
936                break;
937        case UMASS_PROTO_RBC:
938                printf("RBC");
939                break;
940        default:
941                printf("(unknown 0x%02x)",
942                    sc->sc_proto & UMASS_PROTO_COMMAND);
943                break;
944        }
945
946        printf(" over ");
947
948        switch (sc->sc_proto & UMASS_PROTO_WIRE) {
949        case UMASS_PROTO_BBB:
950                printf("Bulk-Only");
951                break;
952        case UMASS_PROTO_CBI:           /* uses Comand/Bulk pipes */
953                printf("CBI");
954                break;
955        case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
956                printf("CBI with CCI");
957                break;
958        default:
959                printf("(unknown 0x%02x)",
960                    sc->sc_proto & UMASS_PROTO_WIRE);
961        }
962
963        printf("; quirks = 0x%04x\n", sc->sc_quirks);
964#endif
965
966        if (sc->sc_quirks & ALT_IFACE_1) {
967                err = usbd_set_alt_interface_index
968                    (uaa->device, uaa->info.bIfaceIndex, 1);
969
970                if (err) {
971                        DPRINTF(sc, UDMASS_USB, "could not switch to "
972                            "Alt Interface 1\n");
973                        goto detach;
974                }
975        }
976        /* allocate all required USB transfers */
977
978        if (sc->sc_proto & UMASS_PROTO_BBB) {
979
980                err = usbd_transfer_setup(uaa->device,
981                    &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
982                    UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
983
984                /* skip reset first time */
985                sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
986
987        } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
988
989                err = usbd_transfer_setup(uaa->device,
990                    &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
991                    UMASS_T_CBI_MAX, sc, &sc->sc_mtx);
992
993                /* skip reset first time */
994                sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
995
996        } else {
997                err = USB_ERR_INVAL;
998        }
999
1000        if (err) {
1001                device_printf(dev, "could not setup required "
1002                    "transfers, %s\n", usbd_errstr(err));
1003                goto detach;
1004        }
1005#ifdef USB_DEBUG
1006        if (umass_throttle > 0) {
1007                uint8_t x;
1008                int iv;
1009
1010                iv = umass_throttle;
1011
1012                if (iv < 1)
1013                        iv = 1;
1014                else if (iv > 8000)
1015                        iv = 8000;
1016
1017                for (x = 0; x != UMASS_T_MAX; x++) {
1018                        if (sc->sc_xfer[x] != NULL)
1019                                usbd_xfer_set_interval(sc->sc_xfer[x], iv);
1020                }
1021        }
1022#endif
1023        sc->sc_transform =
1024            (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
1025            (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
1026            (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
1027            (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1028            &umass_no_transform;
1029
1030        /* from here onwards the device can be used. */
1031
1032        if (sc->sc_quirks & SHUTTLE_INIT) {
1033                umass_init_shuttle(sc);
1034        }
1035        /* get the maximum LUN supported by the device */
1036
1037        if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1038            !(sc->sc_quirks & NO_GETMAXLUN))
1039                sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1040        else
1041                sc->sc_maxlun = 0;
1042
1043        /* Prepare the SCSI command block */
1044        sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1045        sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1046
1047        /* register the SIM */
1048        err = umass_cam_attach_sim(sc);
1049        if (err) {
1050                goto detach;
1051        }
1052#ifndef __rtems__
1053        /* scan the SIM */
1054        umass_cam_attach(sc);
1055#endif /* __rtems__ */
1056
1057        DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1058
1059        return (0);                     /* success */
1060
1061detach:
1062        umass_detach(dev);
1063        return (ENXIO);                 /* failure */
1064}
1065
1066static int
1067umass_detach(device_t dev)
1068{
1069        struct umass_softc *sc = device_get_softc(dev);
1070
1071        DPRINTF(sc, UDMASS_USB, "\n");
1072
1073        /* teardown our statemachine */
1074
1075        usbd_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1076
1077        mtx_lock(&sc->sc_mtx);
1078
1079        /* cancel any leftover CCB's */
1080
1081        umass_cancel_ccb(sc);
1082
1083        umass_cam_detach_sim(sc);
1084
1085        mtx_unlock(&sc->sc_mtx);
1086
1087        mtx_destroy(&sc->sc_mtx);
1088
1089        return (0);                     /* success */
1090}
1091
1092static void
1093umass_init_shuttle(struct umass_softc *sc)
1094{
1095        struct usb_device_request req;
1096        usb_error_t err;
1097        uint8_t status[2] = {0, 0};
1098
1099        /*
1100         * The Linux driver does this, but no one can tell us what the
1101         * command does.
1102         */
1103        req.bmRequestType = UT_READ_VENDOR_DEVICE;
1104        req.bRequest = 1;               /* XXX unknown command */
1105        USETW(req.wValue, 0);
1106        req.wIndex[0] = sc->sc_iface_no;
1107        req.wIndex[1] = 0;
1108        USETW(req.wLength, sizeof(status));
1109        err = usbd_do_request(sc->sc_udev, NULL, &req, &status);
1110
1111        DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1112            status[0], status[1]);
1113}
1114
1115/*
1116 * Generic functions to handle transfers
1117 */
1118
1119static void
1120umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1121{
1122        DPRINTF(sc, UDMASS_GEN, "transfer index = "
1123            "%d\n", xfer_index);
1124
1125        if (sc->sc_xfer[xfer_index]) {
1126                sc->sc_last_xfer_index = xfer_index;
1127                usbd_transfer_start(sc->sc_xfer[xfer_index]);
1128        } else {
1129                umass_cancel_ccb(sc);
1130        }
1131}
1132
1133static void
1134umass_reset(struct umass_softc *sc)
1135{
1136        DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1137
1138        /*
1139         * stop the last transfer, if not already stopped:
1140         */
1141        usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1142        umass_transfer_start(sc, 0);
1143}
1144
1145static void
1146umass_cancel_ccb(struct umass_softc *sc)
1147{
1148        union ccb *ccb;
1149
1150        mtx_assert(&sc->sc_mtx, MA_OWNED);
1151
1152        ccb = sc->sc_transfer.ccb;
1153        sc->sc_transfer.ccb = NULL;
1154        sc->sc_last_xfer_index = 0;
1155
1156        if (ccb) {
1157                (sc->sc_transfer.callback)
1158                    (sc, ccb, (sc->sc_transfer.data_len -
1159                    sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1160        }
1161}
1162
1163static void
1164umass_tr_error(struct usb_xfer *xfer, usb_error_t error)
1165{
1166        struct umass_softc *sc = usbd_xfer_softc(xfer);
1167
1168        if (error != USB_ERR_CANCELLED) {
1169
1170                DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1171                    "reset\n", usbd_errstr(error));
1172        }
1173        umass_cancel_ccb(sc);
1174}
1175
1176/*
1177 * BBB protocol specific functions
1178 */
1179
1180static void
1181umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1182{
1183        struct umass_softc *sc = usbd_xfer_softc(xfer);
1184        struct usb_device_request req;
1185        struct usb_page_cache *pc;
1186
1187        switch (USB_GET_STATE(xfer)) {
1188        case USB_ST_TRANSFERRED:
1189                umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1190                return;
1191
1192        case USB_ST_SETUP:
1193                /*
1194                 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1195                 *
1196                 * For Reset Recovery the host shall issue in the following order:
1197                 * a) a Bulk-Only Mass Storage Reset
1198                 * b) a Clear Feature HALT to the Bulk-In endpoint
1199                 * c) a Clear Feature HALT to the Bulk-Out endpoint
1200                 *
1201                 * This is done in 3 steps, using 3 transfers:
1202                 * UMASS_T_BBB_RESET1
1203                 * UMASS_T_BBB_RESET2
1204                 * UMASS_T_BBB_RESET3
1205                 */
1206
1207                DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1208
1209                req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1210                req.bRequest = UR_BBB_RESET;    /* bulk only reset */
1211                USETW(req.wValue, 0);
1212                req.wIndex[0] = sc->sc_iface_no;
1213                req.wIndex[1] = 0;
1214                USETW(req.wLength, 0);
1215
1216                pc = usbd_xfer_get_frame(xfer, 0);
1217                usbd_copy_in(pc, 0, &req, sizeof(req));
1218
1219                usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1220                usbd_xfer_set_frames(xfer, 1);
1221                usbd_transfer_submit(xfer);
1222                return;
1223
1224        default:                        /* Error */
1225                umass_tr_error(xfer, error);
1226                return;
1227        }
1228}
1229
1230static void
1231umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1232{
1233        umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1234            UMASS_T_BBB_DATA_READ, error);
1235}
1236
1237static void
1238umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1239{
1240        umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1241            UMASS_T_BBB_DATA_WRITE, error);
1242}
1243
1244static void
1245umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
1246    uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1247{
1248        struct umass_softc *sc = usbd_xfer_softc(xfer);
1249
1250        switch (USB_GET_STATE(xfer)) {
1251        case USB_ST_TRANSFERRED:
1252tr_transferred:
1253                umass_transfer_start(sc, next_xfer);
1254                return;
1255
1256        case USB_ST_SETUP:
1257                if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1258                        goto tr_transferred;
1259                }
1260                return;
1261
1262        default:                        /* Error */
1263                umass_tr_error(xfer, error);
1264                return;
1265        }
1266}
1267
1268static void
1269umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
1270{
1271        struct umass_softc *sc = usbd_xfer_softc(xfer);
1272        union ccb *ccb = sc->sc_transfer.ccb;
1273        struct usb_page_cache *pc;
1274        uint32_t tag;
1275
1276        switch (USB_GET_STATE(xfer)) {
1277        case USB_ST_TRANSFERRED:
1278                umass_transfer_start
1279                    (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1280                    (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1281                    UMASS_T_BBB_STATUS));
1282                return;
1283
1284        case USB_ST_SETUP:
1285
1286                sc->sc_status_try = 0;
1287
1288                if (ccb) {
1289
1290                        /*
1291                         * the initial value is not important,
1292                         * as long as the values are unique:
1293                         */
1294                        tag = UGETDW(sc->cbw.dCBWTag) + 1;
1295
1296                        USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1297                        USETDW(sc->cbw.dCBWTag, tag);
1298
1299                        /*
1300                         * dCBWDataTransferLength:
1301                         *   This field indicates the number of bytes of data that the host
1302                         *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1303                         *   the Direction bit) during the execution of this command. If this
1304                         *   field is set to 0, the device will expect that no data will be
1305                         *   transferred IN or OUT during this command, regardless of the value
1306                         *   of the Direction bit defined in dCBWFlags.
1307                         */
1308                        USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1309
1310                        /*
1311                         * dCBWFlags:
1312                         *   The bits of the Flags field are defined as follows:
1313                         *     Bits 0-6  reserved
1314                         *     Bit  7    Direction - this bit shall be ignored if the
1315                         *                           dCBWDataTransferLength field is zero.
1316                         *               0 = data Out from host to device
1317                         *               1 = data In from device to host
1318                         */
1319                        sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1320                            CBWFLAGS_IN : CBWFLAGS_OUT);
1321                        sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1322
1323                        if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1324                                sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1325                                DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1326                        }
1327                        sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1328
1329                        /* copy SCSI command data */
1330                        memcpy(sc->cbw.CBWCDB, sc->sc_transfer.cmd_data,
1331                            sc->sc_transfer.cmd_len);
1332
1333                        /* clear remaining command area */
1334                        memset(sc->cbw.CBWCDB +
1335                            sc->sc_transfer.cmd_len, 0,
1336                            sizeof(sc->cbw.CBWCDB) -
1337                            sc->sc_transfer.cmd_len);
1338
1339                        DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1340
1341                        pc = usbd_xfer_get_frame(xfer, 0);
1342                        usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
1343                        usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
1344
1345                        usbd_transfer_submit(xfer);
1346                }
1347                return;
1348
1349        default:                        /* Error */
1350                umass_tr_error(xfer, error);
1351                return;
1352        }
1353}
1354
1355static void
1356umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1357{
1358        struct umass_softc *sc = usbd_xfer_softc(xfer);
1359        uint32_t max_bulk = usbd_xfer_max_len(xfer);
1360        int actlen, sumlen;
1361
1362        usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1363
1364        switch (USB_GET_STATE(xfer)) {
1365        case USB_ST_TRANSFERRED:
1366                sc->sc_transfer.data_rem -= actlen;
1367                sc->sc_transfer.data_ptr += actlen;
1368                sc->sc_transfer.actlen += actlen;
1369
1370                if (actlen < sumlen) {
1371                        /* short transfer */
1372                        sc->sc_transfer.data_rem = 0;
1373                }
1374        case USB_ST_SETUP:
1375                DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1376                    max_bulk, sc->sc_transfer.data_rem);
1377
1378                if (sc->sc_transfer.data_rem == 0) {
1379                        umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1380                        return;
1381                }
1382                if (max_bulk > sc->sc_transfer.data_rem) {
1383                        max_bulk = sc->sc_transfer.data_rem;
1384                }
1385                usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1386
1387                usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1388                    max_bulk);
1389
1390                usbd_transfer_submit(xfer);
1391                return;
1392
1393        default:                        /* Error */
1394                if (error == USB_ERR_CANCELLED) {
1395                        umass_tr_error(xfer, error);
1396                } else {
1397                        umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1398                }
1399                return;
1400        }
1401}
1402
1403static void
1404umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1405{
1406        umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1407            UMASS_T_BBB_DATA_READ, error);
1408}
1409
1410static void
1411umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1412{
1413        struct umass_softc *sc = usbd_xfer_softc(xfer);
1414        uint32_t max_bulk = usbd_xfer_max_len(xfer);
1415        int actlen, sumlen;
1416
1417        usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1418
1419        switch (USB_GET_STATE(xfer)) {
1420        case USB_ST_TRANSFERRED:
1421                sc->sc_transfer.data_rem -= actlen;
1422                sc->sc_transfer.data_ptr += actlen;
1423                sc->sc_transfer.actlen += actlen;
1424
1425                if (actlen < sumlen) {
1426                        /* short transfer */
1427                        sc->sc_transfer.data_rem = 0;
1428                }
1429        case USB_ST_SETUP:
1430                DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1431                    max_bulk, sc->sc_transfer.data_rem);
1432
1433                if (sc->sc_transfer.data_rem == 0) {
1434                        umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1435                        return;
1436                }
1437                if (max_bulk > sc->sc_transfer.data_rem) {
1438                        max_bulk = sc->sc_transfer.data_rem;
1439                }
1440                usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1441
1442                usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1443                    max_bulk);
1444
1445                usbd_transfer_submit(xfer);
1446                return;
1447
1448        default:                        /* Error */
1449                if (error == USB_ERR_CANCELLED) {
1450                        umass_tr_error(xfer, error);
1451                } else {
1452                        umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
1453                }
1454                return;
1455        }
1456}
1457
1458static void
1459umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1460{
1461        umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1462            UMASS_T_BBB_DATA_WRITE, error);
1463}
1464
1465static void
1466umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
1467{
1468        struct umass_softc *sc = usbd_xfer_softc(xfer);
1469        union ccb *ccb = sc->sc_transfer.ccb;
1470        struct usb_page_cache *pc;
1471        uint32_t residue;
1472        int actlen;
1473
1474        usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1475
1476        switch (USB_GET_STATE(xfer)) {
1477        case USB_ST_TRANSFERRED:
1478
1479                /*
1480                 * Do a full reset if there is something wrong with the CSW:
1481                 */
1482                sc->sc_status_try = 1;
1483
1484                /* Zero missing parts of the CSW: */
1485
1486                if (actlen < (int)sizeof(sc->csw))
1487                        memset(&sc->csw, 0, sizeof(sc->csw));
1488
1489                pc = usbd_xfer_get_frame(xfer, 0);
1490                usbd_copy_out(pc, 0, &sc->csw, actlen);
1491
1492                DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1493
1494                residue = UGETDW(sc->csw.dCSWDataResidue);
1495
1496                if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
1497                        residue = (sc->sc_transfer.data_len -
1498                            sc->sc_transfer.actlen);
1499                }
1500                if (residue > sc->sc_transfer.data_len) {
1501                        DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
1502                            "to %d bytes\n", residue, sc->sc_transfer.data_len);
1503                        residue = sc->sc_transfer.data_len;
1504                }
1505                /* translate weird command-status signatures: */
1506                if (sc->sc_quirks & WRONG_CSWSIG) {
1507
1508                        uint32_t temp = UGETDW(sc->csw.dCSWSignature);
1509
1510                        if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
1511                            (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
1512                                USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1513                        }
1514                }
1515                /* check CSW and handle eventual error */
1516                if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1517                        DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
1518                            UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
1519                        /*
1520                         * Invalid CSW: Wrong signature or wrong tag might
1521                         * indicate that we lost synchronization. Reset the
1522                         * device.
1523                         */
1524                        goto tr_error;
1525                } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
1526                        DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
1527                            "0x%08x\n", UGETDW(sc->csw.dCSWTag),
1528                            UGETDW(sc->cbw.dCBWTag));
1529                        goto tr_error;
1530                } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1531                        DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
1532                            sc->csw.bCSWStatus, CSWSTATUS_PHASE);
1533                        goto tr_error;
1534                } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1535                        DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
1536                            "%d\n", residue);
1537                        goto tr_error;
1538                } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
1539                        DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
1540                            sc->sc_transfer.actlen, sc->sc_transfer.data_len);
1541                        goto tr_error;
1542                } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1543                        DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
1544                            "%d\n", residue);
1545
1546                        sc->sc_transfer.ccb = NULL;
1547
1548                        sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1549
1550                        (sc->sc_transfer.callback)
1551                            (sc, ccb, residue, STATUS_CMD_FAILED);
1552                } else {
1553                        sc->sc_transfer.ccb = NULL;
1554
1555                        sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1556
1557                        (sc->sc_transfer.callback)
1558                            (sc, ccb, residue, STATUS_CMD_OK);
1559                }
1560                return;
1561
1562        case USB_ST_SETUP:
1563                usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1564                usbd_transfer_submit(xfer);
1565                return;
1566
1567        default:
1568tr_error:
1569                DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
1570                    usbd_errstr(error), sc->sc_status_try);
1571
1572                if ((error == USB_ERR_CANCELLED) ||
1573                    (sc->sc_status_try)) {
1574                        umass_tr_error(xfer, error);
1575                } else {
1576                        sc->sc_status_try = 1;
1577                        umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1578                }
1579                return;
1580        }
1581}
1582
1583static void
1584umass_command_start(struct umass_softc *sc, uint8_t dir,
1585    void *data_ptr, uint32_t data_len,
1586    uint32_t data_timeout, umass_callback_t *callback,
1587    union ccb *ccb)
1588{
1589        sc->sc_transfer.lun = ccb->ccb_h.target_lun;
1590
1591        /*
1592         * NOTE: assumes that "sc->sc_transfer.cmd_data" and
1593         * "sc->sc_transfer.cmd_len" has been properly
1594         * initialized.
1595         */
1596
1597        sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
1598        sc->sc_transfer.data_ptr = data_ptr;
1599        sc->sc_transfer.data_len = data_len;
1600        sc->sc_transfer.data_rem = data_len;
1601        sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
1602
1603        sc->sc_transfer.actlen = 0;
1604        sc->sc_transfer.callback = callback;
1605        sc->sc_transfer.ccb = ccb;
1606
1607        if (sc->sc_xfer[sc->sc_last_xfer_index]) {
1608                usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
1609        } else {
1610                umass_cancel_ccb(sc);
1611        }
1612}
1613
1614static uint8_t
1615umass_bbb_get_max_lun(struct umass_softc *sc)
1616{
1617        struct usb_device_request req;
1618        usb_error_t err;
1619        uint8_t buf = 0;
1620
1621        /* The Get Max Lun command is a class-specific request. */
1622        req.bmRequestType = UT_READ_CLASS_INTERFACE;
1623        req.bRequest = UR_BBB_GET_MAX_LUN;
1624        USETW(req.wValue, 0);
1625        req.wIndex[0] = sc->sc_iface_no;
1626        req.wIndex[1] = 0;
1627        USETW(req.wLength, 1);
1628
1629        err = usbd_do_request(sc->sc_udev, NULL, &req, &buf);
1630        if (err) {
1631                buf = 0;
1632
1633                /* Device doesn't support Get Max Lun request. */
1634                printf("%s: Get Max Lun not supported (%s)\n",
1635                    sc->sc_name, usbd_errstr(err));
1636        }
1637        return (buf);
1638}
1639
1640/*
1641 * Command/Bulk/Interrupt (CBI) specific functions
1642 */
1643
1644static void
1645umass_cbi_start_status(struct umass_softc *sc)
1646{
1647        if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
1648                umass_transfer_start(sc, UMASS_T_CBI_STATUS);
1649        } else {
1650                union ccb *ccb = sc->sc_transfer.ccb;
1651
1652                sc->sc_transfer.ccb = NULL;
1653
1654                sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1655
1656                (sc->sc_transfer.callback)
1657                    (sc, ccb, (sc->sc_transfer.data_len -
1658                    sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
1659        }
1660}
1661
1662static void
1663umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1664{
1665        struct umass_softc *sc = usbd_xfer_softc(xfer);
1666        struct usb_device_request req;
1667        struct usb_page_cache *pc;
1668        uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
1669
1670        uint8_t i;
1671
1672        switch (USB_GET_STATE(xfer)) {
1673        case USB_ST_TRANSFERRED:
1674                umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1675                break;
1676
1677        case USB_ST_SETUP:
1678                /*
1679                 * Command Block Reset Protocol
1680                 *
1681                 * First send a reset request to the device. Then clear
1682                 * any possibly stalled bulk endpoints.
1683                 *
1684                 * This is done in 3 steps, using 3 transfers:
1685                 * UMASS_T_CBI_RESET1
1686                 * UMASS_T_CBI_RESET2
1687                 * UMASS_T_CBI_RESET3
1688                 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
1689                 */
1690
1691                DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
1692
1693                req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1694                req.bRequest = UR_CBI_ADSC;
1695                USETW(req.wValue, 0);
1696                req.wIndex[0] = sc->sc_iface_no;
1697                req.wIndex[1] = 0;
1698                USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
1699
1700                /*
1701                 * The 0x1d code is the SEND DIAGNOSTIC command. To
1702                 * distinguish between the two, the last 10 bytes of the CBL
1703                 * is filled with 0xff (section 2.2 of the CBI
1704                 * specification)
1705                 */
1706                buf[0] = 0x1d;          /* Command Block Reset */
1707                buf[1] = 0x04;
1708
1709                for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
1710                        buf[i] = 0xff;
1711                }
1712
1713                pc = usbd_xfer_get_frame(xfer, 0);
1714                usbd_copy_in(pc, 0, &req, sizeof(req));
1715                pc = usbd_xfer_get_frame(xfer, 1);
1716                usbd_copy_in(pc, 0, buf, sizeof(buf));
1717
1718                usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1719                usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
1720                usbd_xfer_set_frames(xfer, 2);
1721                usbd_transfer_submit(xfer);
1722                break;
1723
1724        default:                        /* Error */
1725                if (error == USB_ERR_CANCELLED)
1726                        umass_tr_error(xfer, error);
1727                else
1728                        umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1729                break;
1730        }
1731}
1732
1733static void
1734umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1735{
1736        umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
1737            UMASS_T_CBI_DATA_READ, error);
1738}
1739
1740static void
1741umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1742{
1743        struct umass_softc *sc = usbd_xfer_softc(xfer);
1744
1745        umass_t_cbi_data_clear_stall_callback
1746            (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
1747            sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
1748            UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
1749            UMASS_T_CBI_DATA_WRITE, error);
1750}
1751
1752static void
1753umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error)
1754{
1755        umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
1756            UMASS_T_CBI_STATUS, error);
1757}
1758
1759static void
1760umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer,
1761    uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1762{
1763        struct umass_softc *sc = usbd_xfer_softc(xfer);
1764
1765        switch (USB_GET_STATE(xfer)) {
1766        case USB_ST_TRANSFERRED:
1767tr_transferred:
1768                if (next_xfer == UMASS_T_CBI_STATUS) {
1769                        umass_cbi_start_status(sc);
1770                } else {
1771                        umass_transfer_start(sc, next_xfer);
1772                }
1773                break;
1774
1775        case USB_ST_SETUP:
1776                if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1777                        goto tr_transferred;    /* should not happen */
1778                }
1779                break;
1780
1781        default:                        /* Error */
1782                umass_tr_error(xfer, error);
1783                break;
1784        }
1785}
1786
1787static void
1788umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error)
1789{
1790        struct umass_softc *sc = usbd_xfer_softc(xfer);
1791        union ccb *ccb = sc->sc_transfer.ccb;
1792        struct usb_device_request req;
1793        struct usb_page_cache *pc;
1794
1795        switch (USB_GET_STATE(xfer)) {
1796        case USB_ST_TRANSFERRED:
1797
1798                if (sc->sc_transfer.dir == DIR_NONE) {
1799                        umass_cbi_start_status(sc);
1800                } else {
1801                        umass_transfer_start
1802                            (sc, (sc->sc_transfer.dir == DIR_IN) ?
1803                            UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
1804                }
1805                break;
1806
1807        case USB_ST_SETUP:
1808
1809                if (ccb) {
1810
1811                        /*
1812                         * do a CBI transfer with cmd_len bytes from
1813                         * cmd_data, possibly a data phase of data_len
1814                         * bytes from/to the device and finally a status
1815                         * read phase.
1816                         */
1817
1818                        req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1819                        req.bRequest = UR_CBI_ADSC;
1820                        USETW(req.wValue, 0);
1821                        req.wIndex[0] = sc->sc_iface_no;
1822                        req.wIndex[1] = 0;
1823                        req.wLength[0] = sc->sc_transfer.cmd_len;
1824                        req.wLength[1] = 0;
1825
1826                        pc = usbd_xfer_get_frame(xfer, 0);
1827                        usbd_copy_in(pc, 0, &req, sizeof(req));
1828                        pc = usbd_xfer_get_frame(xfer, 1);
1829                        usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data,
1830                            sc->sc_transfer.cmd_len);
1831
1832                        usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1833                        usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len);
1834                        usbd_xfer_set_frames(xfer,
1835                            sc->sc_transfer.cmd_len ? 2 : 1);
1836
1837                        DIF(UDMASS_CBI,
1838                            umass_cbi_dump_cmd(sc,
1839                            sc->sc_transfer.cmd_data,
1840                            sc->sc_transfer.cmd_len));
1841
1842                        usbd_transfer_submit(xfer);
1843                }
1844                break;
1845
1846        default:                        /* Error */
1847                /*
1848                 * STALL on the control pipe can be result of the command error.
1849                 * Attempt to clear this STALL same as for bulk pipe also
1850                 * results in command completion interrupt, but ASC/ASCQ there
1851                 * look like not always valid, so don't bother about it.
1852                 */
1853                if ((error == USB_ERR_STALLED) ||
1854                    (sc->sc_transfer.callback == &umass_cam_cb)) {
1855                        sc->sc_transfer.ccb = NULL;
1856                        (sc->sc_transfer.callback)
1857                            (sc, ccb, sc->sc_transfer.data_len,
1858                            STATUS_CMD_UNKNOWN);
1859                } else {
1860                        umass_tr_error(xfer, error);
1861                        /* skip reset */
1862                        sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1863                }
1864                break;
1865        }
1866}
1867
1868static void
1869umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1870{
1871        struct umass_softc *sc = usbd_xfer_softc(xfer);
1872        uint32_t max_bulk = usbd_xfer_max_len(xfer);
1873        int actlen, sumlen;
1874
1875        usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1876
1877        switch (USB_GET_STATE(xfer)) {
1878        case USB_ST_TRANSFERRED:
1879                sc->sc_transfer.data_rem -= actlen;
1880                sc->sc_transfer.data_ptr += actlen;
1881                sc->sc_transfer.actlen += actlen;
1882
1883                if (actlen < sumlen) {
1884                        /* short transfer */
1885                        sc->sc_transfer.data_rem = 0;
1886                }
1887        case USB_ST_SETUP:
1888                DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1889                    max_bulk, sc->sc_transfer.data_rem);
1890
1891                if (sc->sc_transfer.data_rem == 0) {
1892                        umass_cbi_start_status(sc);
1893                        break;
1894                }
1895                if (max_bulk > sc->sc_transfer.data_rem) {
1896                        max_bulk = sc->sc_transfer.data_rem;
1897                }
1898                usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1899
1900                usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1901                    max_bulk);
1902
1903                usbd_transfer_submit(xfer);
1904                break;
1905
1906        default:                        /* Error */
1907                if ((error == USB_ERR_CANCELLED) ||
1908                    (sc->sc_transfer.callback != &umass_cam_cb)) {
1909                        umass_tr_error(xfer, error);
1910                } else {
1911                        umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
1912                }
1913                break;
1914        }
1915}
1916
1917static void
1918umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1919{
1920        umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1921            UMASS_T_CBI_DATA_READ, error);
1922}
1923
1924static void
1925umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1926{
1927        struct umass_softc *sc = usbd_xfer_softc(xfer);
1928        uint32_t max_bulk = usbd_xfer_max_len(xfer);
1929        int actlen, sumlen;
1930
1931        usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1932
1933        switch (USB_GET_STATE(xfer)) {
1934        case USB_ST_TRANSFERRED:
1935                sc->sc_transfer.data_rem -= actlen;
1936                sc->sc_transfer.data_ptr += actlen;
1937                sc->sc_transfer.actlen += actlen;
1938
1939                if (actlen < sumlen) {
1940                        /* short transfer */
1941                        sc->sc_transfer.data_rem = 0;
1942                }
1943        case USB_ST_SETUP:
1944                DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1945                    max_bulk, sc->sc_transfer.data_rem);
1946
1947                if (sc->sc_transfer.data_rem == 0) {
1948                        umass_cbi_start_status(sc);
1949                        break;
1950                }
1951                if (max_bulk > sc->sc_transfer.data_rem) {
1952                        max_bulk = sc->sc_transfer.data_rem;
1953                }
1954                usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1955
1956                usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1957                    max_bulk);
1958
1959                usbd_transfer_submit(xfer);
1960                break;
1961
1962        default:                        /* Error */
1963                if ((error == USB_ERR_CANCELLED) ||
1964                    (sc->sc_transfer.callback != &umass_cam_cb)) {
1965                        umass_tr_error(xfer, error);
1966                } else {
1967                        umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
1968                }
1969                break;
1970        }
1971}
1972
1973static void
1974umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1975{
1976        umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1977            UMASS_T_CBI_DATA_WRITE, error);
1978}
1979
1980static void
1981umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error)
1982{
1983        struct umass_softc *sc = usbd_xfer_softc(xfer);
1984        union ccb *ccb = sc->sc_transfer.ccb;
1985        struct usb_page_cache *pc;
1986        uint32_t residue;
1987        uint8_t status;
1988        int actlen;
1989
1990        usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1991
1992        switch (USB_GET_STATE(xfer)) {
1993        case USB_ST_TRANSFERRED:
1994
1995                if (actlen < (int)sizeof(sc->sbl)) {
1996                        goto tr_setup;
1997                }
1998                pc = usbd_xfer_get_frame(xfer, 0);
1999                usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl));
2000
2001                residue = (sc->sc_transfer.data_len -
2002                    sc->sc_transfer.actlen);
2003
2004                /* dissect the information in the buffer */
2005
2006                if (sc->sc_proto & UMASS_PROTO_UFI) {
2007
2008                        /*
2009                         * Section 3.4.3.1.3 specifies that the UFI command
2010                         * protocol returns an ASC and ASCQ in the interrupt
2011                         * data block.
2012                         */
2013
2014                        DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2015                            "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2016                            sc->sbl.ufi.ascq);
2017
2018                        status = (((sc->sbl.ufi.asc == 0) &&
2019                            (sc->sbl.ufi.ascq == 0)) ?
2020                            STATUS_CMD_OK : STATUS_CMD_FAILED);
2021
2022                        sc->sc_transfer.ccb = NULL;
2023
2024                        sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2025
2026                        (sc->sc_transfer.callback)
2027                            (sc, ccb, residue, status);
2028
2029                        break;
2030
2031                } else {
2032
2033                        /* Command Interrupt Data Block */
2034
2035                        DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2036                            sc->sbl.common.type, sc->sbl.common.value);
2037
2038                        if (sc->sbl.common.type == IDB_TYPE_CCI) {
2039
2040                                status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2041
2042                                status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2043                                    (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2044                                    (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2045                                    STATUS_WIRE_FAILED);
2046
2047                                sc->sc_transfer.ccb = NULL;
2048
2049                                sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2050
2051                                (sc->sc_transfer.callback)
2052                                    (sc, ccb, residue, status);
2053
2054                                break;
2055                        }
2056                }
2057
2058                /* fallthrough */
2059
2060        case USB_ST_SETUP:
2061tr_setup:
2062                usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2063                usbd_transfer_submit(xfer);
2064                break;
2065
2066        default:                        /* Error */
2067                DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2068                    usbd_errstr(error));
2069                umass_tr_error(xfer, error);
2070                break;
2071        }
2072}
2073
2074/*
2075 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2076 */
2077
2078static int
2079umass_cam_attach_sim(struct umass_softc *sc)
2080{
2081        struct cam_devq *devq;          /* Per device Queue */
2082
2083        /*
2084         * A HBA is attached to the CAM layer.
2085         *
2086         * The CAM layer will then after a while start probing for devices on
2087         * the bus. The number of SIMs is limited to one.
2088         */
2089
2090        devq = cam_simq_alloc(1 /* maximum openings */ );
2091        if (devq == NULL) {
2092                return (ENOMEM);
2093        }
2094        sc->sc_sim = cam_sim_alloc
2095            (&umass_cam_action, &umass_cam_poll,
2096            DEVNAME_SIM,
2097            sc /* priv */ ,
2098            sc->sc_unit /* unit number */ ,
2099            &sc->sc_mtx /* mutex */ ,
2100            1 /* maximum device openings */ ,
2101            0 /* maximum tagged device openings */ ,
2102            devq);
2103
2104        if (sc->sc_sim == NULL) {
2105                cam_simq_free(devq);
2106                return (ENOMEM);
2107        }
2108
2109        mtx_lock(&sc->sc_mtx);
2110
2111        if (xpt_bus_register(sc->sc_sim, sc->sc_dev,
2112            sc->sc_unit) != CAM_SUCCESS) {
2113                mtx_unlock(&sc->sc_mtx);
2114                return (ENOMEM);
2115        }
2116        mtx_unlock(&sc->sc_mtx);
2117
2118        return (0);
2119}
2120
2121#ifndef __rtems__
2122static void
2123umass_cam_attach(struct umass_softc *sc)
2124{
2125#ifndef USB_DEBUG
2126        if (bootverbose)
2127#endif
2128                printf("%s:%d:%d:%d: Attached to scbus%d\n",
2129                    sc->sc_name, cam_sim_path(sc->sc_sim),
2130                    sc->sc_unit, CAM_LUN_WILDCARD,
2131                    cam_sim_path(sc->sc_sim));
2132}
2133#endif /* __rtems__ */
2134
2135/* umass_cam_detach
2136 *      detach from the CAM layer
2137 */
2138
2139static void
2140umass_cam_detach_sim(struct umass_softc *sc)
2141{
2142        if (sc->sc_sim != NULL) {
2143                if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2144                        /* accessing the softc is not possible after this */
2145                        sc->sc_sim->softc = NULL;
2146                        cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2147                } else {
2148                        panic("%s: CAM layer is busy\n",
2149                            sc->sc_name);
2150                }
2151                sc->sc_sim = NULL;
2152        }
2153}
2154
2155/* umass_cam_action
2156 *      CAM requests for action come through here
2157 */
2158
2159static void
2160umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2161{
2162        struct umass_softc *sc = (struct umass_softc *)sim->softc;
2163
2164        if (sc == NULL) {
2165                ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2166                xpt_done(ccb);
2167                return;
2168        }
2169
2170        /* Perform the requested action */
2171        switch (ccb->ccb_h.func_code) {
2172        case XPT_SCSI_IO:
2173                {
2174                        uint8_t *cmd;
2175                        uint8_t dir;
2176
2177                        if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2178                                cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2179                        } else {
2180                                cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2181                        }
2182
2183                        DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2184                            "cmd: 0x%02x, flags: 0x%02x, "
2185                            "%db cmd/%db data/%db sense\n",
2186                            cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2187                            ccb->ccb_h.target_lun, cmd[0],
2188                            ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2189                            ccb->csio.dxfer_len, ccb->csio.sense_len);
2190
2191                        if (sc->sc_transfer.ccb) {
2192                                DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2193                                    "I/O in progress, deferring\n",
2194                                    cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2195                                    ccb->ccb_h.target_lun);
2196                                ccb->ccb_h.status = CAM_SCSI_BUSY;
2197                                xpt_done(ccb);
2198                                goto done;
2199                        }
2200                        switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2201                        case CAM_DIR_IN:
2202                                dir = DIR_IN;
2203                                break;
2204                        case CAM_DIR_OUT:
2205                                dir = DIR_OUT;
2206                                DIF(UDMASS_SCSI,
2207                                    umass_dump_buffer(sc, ccb->csio.data_ptr,
2208                                    ccb->csio.dxfer_len, 48));
2209                                break;
2210                        default:
2211                                dir = DIR_NONE;
2212                        }
2213
2214                        ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2215
2216                        /*
2217                         * sc->sc_transform will convert the command to the
2218                         * command format needed by the specific command set
2219                         * and return the converted command in
2220                         * "sc->sc_transfer.cmd_data"
2221                         */
2222                        if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2223
2224                                if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2225                                        const char *pserial;
2226
2227                                        pserial = usb_get_serial(sc->sc_udev);
2228
2229                                        /*
2230                                         * Umass devices don't generally report their serial numbers
2231                                         * in the usual SCSI way.  Emulate it here.
2232                                         */
2233                                        if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2234                                            (sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER) &&
2235                                            (pserial[0] != '\0')) {
2236                                                struct scsi_vpd_unit_serial_number *vpd_serial;
2237
2238                                                vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr;
2239                                                vpd_serial->length = strlen(pserial);
2240                                                if (vpd_serial->length > sizeof(vpd_serial->serial_num))
2241                                                        vpd_serial->length = sizeof(vpd_serial->serial_num);
2242                                                memcpy(vpd_serial->serial_num, pserial, vpd_serial->length);
2243                                                ccb->csio.scsi_status = SCSI_STATUS_OK;
2244                                                ccb->ccb_h.status = CAM_REQ_CMP;
2245                                                xpt_done(ccb);
2246                                                goto done;
2247                                        }
2248
2249                                        /*
2250                                         * Handle EVPD inquiry for broken devices first
2251                                         * NO_INQUIRY also implies NO_INQUIRY_EVPD
2252                                         */
2253                                        if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2254                                            (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
2255
2256                                                scsi_set_sense_data(&ccb->csio.sense_data,
2257                                                        /*sense_format*/ SSD_TYPE_NONE,
2258                                                        /*current_error*/ 1,
2259                                                        /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
2260                                                        /*asc*/ 0x24,
2261                                                        /*ascq*/ 0x00,
2262                                                        /*extra args*/ SSD_ELEM_NONE);
2263                                                ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2264                                                ccb->ccb_h.status =
2265                                                    CAM_SCSI_STATUS_ERROR |
2266                                                    CAM_AUTOSNS_VALID |
2267                                                    CAM_DEV_QFRZN;
2268                                                xpt_freeze_devq(ccb->ccb_h.path, 1);
2269                                                xpt_done(ccb);
2270                                                goto done;
2271                                        }
2272                                        /*
2273                                         * Return fake inquiry data for
2274                                         * broken devices
2275                                         */
2276                                        if (sc->sc_quirks & NO_INQUIRY) {
2277                                                memcpy(ccb->csio.data_ptr, &fake_inq_data,
2278                                                    sizeof(fake_inq_data));
2279                                                ccb->csio.scsi_status = SCSI_STATUS_OK;
2280                                                ccb->ccb_h.status = CAM_REQ_CMP;
2281                                                xpt_done(ccb);
2282                                                goto done;
2283                                        }
2284                                        if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2285                                                ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
2286                                        }
2287                                } else if (sc->sc_transfer.cmd_data[0] == PREVENT_ALLOW) {
2288                                        if (sc->sc_quirks & NO_PREVENT_ALLOW) {
2289                                                ccb->csio.scsi_status = SCSI_STATUS_OK;
2290                                                ccb->ccb_h.status = CAM_REQ_CMP;
2291                                                xpt_done(ccb);
2292                                                goto done;
2293                                        }
2294                                } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
2295                                        if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
2296                                                ccb->csio.scsi_status = SCSI_STATUS_OK;
2297                                                ccb->ccb_h.status = CAM_REQ_CMP;
2298                                                xpt_done(ccb);
2299                                                goto done;
2300                                        }
2301                                }
2302                                umass_command_start(sc, dir, ccb->csio.data_ptr,
2303                                    ccb->csio.dxfer_len,
2304                                    ccb->ccb_h.timeout,
2305                                    &umass_cam_cb, ccb);
2306                        }
2307                        break;
2308                }
2309        case XPT_PATH_INQ:
2310                {
2311                        struct ccb_pathinq *cpi = &ccb->cpi;
2312
2313                        DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
2314                            sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2315                            ccb->ccb_h.target_lun);
2316
2317                        /* host specific information */
2318                        cpi->version_num = 1;
2319                        cpi->hba_inquiry = 0;
2320                        cpi->target_sprt = 0;
2321                        cpi->hba_misc = PIM_NO_6_BYTE;
2322                        cpi->hba_eng_cnt = 0;
2323                        cpi->max_target = UMASS_SCSIID_MAX;     /* one target */
2324                        cpi->initiator_id = UMASS_SCSIID_HOST;
2325                        strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2326                        strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2327                        strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2328                        cpi->unit_number = cam_sim_unit(sim);
2329                        cpi->bus_id = sc->sc_unit;
2330                        cpi->protocol = PROTO_SCSI;
2331                        cpi->protocol_version = SCSI_REV_2;
2332                        cpi->transport = XPORT_USB;
2333                        cpi->transport_version = 0;
2334
2335                        if (sc == NULL) {
2336                                cpi->base_transfer_speed = 0;
2337                                cpi->max_lun = 0;
2338                        } else {
2339                                if (sc->sc_quirks & FLOPPY_SPEED) {
2340                                        cpi->base_transfer_speed =
2341                                            UMASS_FLOPPY_TRANSFER_SPEED;
2342                                } else {
2343                                        switch (usbd_get_speed(sc->sc_udev)) {
2344                                        case USB_SPEED_SUPER:
2345                                                cpi->base_transfer_speed =
2346                                                    UMASS_SUPER_TRANSFER_SPEED;
2347                                                cpi->maxio = MAXPHYS;
2348                                                break;
2349                                        case USB_SPEED_HIGH:
2350                                                cpi->base_transfer_speed =
2351                                                    UMASS_HIGH_TRANSFER_SPEED;
2352                                                break;
2353                                        default:
2354                                                cpi->base_transfer_speed =
2355                                                    UMASS_FULL_TRANSFER_SPEED;
2356                                                break;
2357                                        }
2358                                }
2359                                cpi->max_lun = sc->sc_maxlun;
2360                        }
2361
2362                        cpi->ccb_h.status = CAM_REQ_CMP;
2363                        xpt_done(ccb);
2364                        break;
2365                }
2366        case XPT_RESET_DEV:
2367                {
2368                        DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
2369                            cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2370                            ccb->ccb_h.target_lun);
2371
2372                        umass_reset(sc);
2373
2374                        ccb->ccb_h.status = CAM_REQ_CMP;
2375                        xpt_done(ccb);
2376                        break;
2377                }
2378        case XPT_GET_TRAN_SETTINGS:
2379                {
2380                        struct ccb_trans_settings *cts = &ccb->cts;
2381
2382                        DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2383                            cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2384                            ccb->ccb_h.target_lun);
2385
2386                        cts->protocol = PROTO_SCSI;
2387                        cts->protocol_version = SCSI_REV_2;
2388                        cts->transport = XPORT_USB;
2389                        cts->transport_version = 0;
2390                        cts->xport_specific.valid = 0;
2391
2392                        ccb->ccb_h.status = CAM_REQ_CMP;
2393                        xpt_done(ccb);
2394                        break;
2395                }
2396        case XPT_SET_TRAN_SETTINGS:
2397                {
2398                        DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2399                            cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2400                            ccb->ccb_h.target_lun);
2401
2402                        ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2403                        xpt_done(ccb);
2404                        break;
2405                }
2406#ifndef __rtems__
2407        case XPT_CALC_GEOMETRY:
2408                {
2409                        cam_calc_geometry(&ccb->ccg, /* extended */ 1);
2410                        xpt_done(ccb);
2411                        break;
2412                }
2413#endif /* __rtems__ */
2414        case XPT_NOOP:
2415                {
2416                        DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
2417                            sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2418                            ccb->ccb_h.target_lun);
2419
2420                        ccb->ccb_h.status = CAM_REQ_CMP;
2421                        xpt_done(ccb);
2422                        break;
2423                }
2424        default:
2425                DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
2426                    "Not implemented\n",
2427                    sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2428                    ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
2429
2430                ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2431                xpt_done(ccb);
2432                break;
2433        }
2434
2435done:
2436        return;
2437}
2438
2439static void
2440umass_cam_poll(struct cam_sim *sim)
2441{
2442        struct umass_softc *sc = (struct umass_softc *)sim->softc;
2443
2444        if (sc == NULL)
2445                return;
2446
2447        DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
2448
2449        usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX);
2450}
2451
2452
2453/* umass_cam_cb
2454 *      finalise a completed CAM command
2455 */
2456
2457static void
2458umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2459    uint8_t status)
2460{
2461        ccb->csio.resid = residue;
2462
2463        switch (status) {
2464        case STATUS_CMD_OK:
2465                ccb->ccb_h.status = CAM_REQ_CMP;
2466                if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
2467                    (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
2468                    (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
2469                        struct scsi_read_capacity_data *rcap;
2470                        uint32_t maxsector;
2471
2472                        rcap = (void *)(ccb->csio.data_ptr);
2473                        maxsector = scsi_4btoul(rcap->addr) - 1;
2474                        scsi_ulto4b(maxsector, rcap->addr);
2475                }
2476                /*
2477                 * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
2478                 * of pages supported by the device - otherwise, CAM
2479                 * will never ask us for the serial number if the
2480                 * device cannot handle that by itself.
2481                 */
2482                if (ccb->ccb_h.func_code == XPT_SCSI_IO &&
2483                    sc->sc_transfer.cmd_data[0] == INQUIRY &&
2484                    (sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2485                    sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST &&
2486                    (usb_get_serial(sc->sc_udev)[0] != '\0')) {
2487                        struct ccb_scsiio *csio;
2488                        struct scsi_vpd_supported_page_list *page_list;
2489
2490                        csio = &ccb->csio;
2491                        page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr;
2492                        if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) {
2493                                page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER;
2494                                page_list->length++;
2495                        }
2496                }
2497                xpt_done(ccb);
2498                break;
2499
2500        case STATUS_CMD_UNKNOWN:
2501        case STATUS_CMD_FAILED:
2502
2503                /* fetch sense data */
2504
2505                /* the rest of the command was filled in at attach */
2506                sc->cam_scsi_sense.length = ccb->csio.sense_len;
2507
2508                DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
2509                    "sense data\n", ccb->csio.sense_len);
2510
2511                if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
2512                    sizeof(sc->cam_scsi_sense))) {
2513
2514                        if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
2515                            (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
2516                                ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
2517                        }
2518                        umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
2519                            ccb->csio.sense_len, ccb->ccb_h.timeout,
2520                            &umass_cam_sense_cb, ccb);
2521                }
2522                break;
2523
2524        default:
2525                /*
2526                 * The wire protocol failed and will hopefully have
2527                 * recovered. We return an error to CAM and let CAM
2528                 * retry the command if necessary.
2529                 */
2530                xpt_freeze_devq(ccb->ccb_h.path, 1);
2531                ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
2532                xpt_done(ccb);
2533                break;
2534        }
2535}
2536
2537/*
2538 * Finalise a completed autosense operation
2539 */
2540static void
2541umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2542    uint8_t status)
2543{
2544        uint8_t *cmd;
2545
2546        switch (status) {
2547        case STATUS_CMD_OK:
2548        case STATUS_CMD_UNKNOWN:
2549        case STATUS_CMD_FAILED: {
2550                int key, sense_len;
2551
2552                ccb->csio.sense_resid = residue;
2553                sense_len = ccb->csio.sense_len - ccb->csio.sense_resid;
2554                key = scsi_get_sense_key(&ccb->csio.sense_data, sense_len,
2555                                         /*show_errors*/ 1);
2556
2557                if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2558                        cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2559                } else {
2560                        cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2561                }
2562
2563                /*
2564                 * Getting sense data always succeeds (apart from wire
2565                 * failures):
2566                 */
2567                if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2568                    (cmd[0] == INQUIRY) &&
2569                    (key == SSD_KEY_UNIT_ATTENTION)) {
2570                        /*
2571                         * Ignore unit attention errors in the case where
2572                         * the Unit Attention state is not cleared on
2573                         * REQUEST SENSE. They will appear again at the next
2574                         * command.
2575                         */
2576                        ccb->ccb_h.status = CAM_REQ_CMP;
2577                } else if (key == SSD_KEY_NO_SENSE) {
2578                        /*
2579                         * No problem after all (in the case of CBI without
2580                         * CCI)
2581                         */
2582                        ccb->ccb_h.status = CAM_REQ_CMP;
2583                } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2584                            (cmd[0] == READ_CAPACITY) &&
2585                    (key == SSD_KEY_UNIT_ATTENTION)) {
2586                        /*
2587                         * Some devices do not clear the unit attention error
2588                         * on request sense. We insert a test unit ready
2589                         * command to make sure we clear the unit attention
2590                         * condition, then allow the retry to proceed as
2591                         * usual.
2592                         */
2593
2594                        xpt_freeze_devq(ccb->ccb_h.path, 1);
2595                        ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2596                            | CAM_AUTOSNS_VALID | CAM_DEV_QFRZN;
2597                        ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2598
2599#if 0
2600                        DELAY(300000);
2601#endif
2602                        DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
2603                            "TEST_UNIT_READY\n");
2604
2605                        /* the rest of the command was filled in at attach */
2606
2607                        if ((sc->sc_transform)(sc,
2608                            &sc->cam_scsi_test_unit_ready.opcode,
2609                            sizeof(sc->cam_scsi_test_unit_ready)) == 1) {
2610                                umass_command_start(sc, DIR_NONE, NULL, 0,
2611                                    ccb->ccb_h.timeout,
2612                                    &umass_cam_quirk_cb, ccb);
2613                                break;
2614                        }
2615                } else {
2616                        xpt_freeze_devq(ccb->ccb_h.path, 1);
2617                        if (key >= 0) {
2618                                ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2619                                    | CAM_AUTOSNS_VALID | CAM_DEV_QFRZN;
2620                                ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2621                        } else
2622                                ccb->ccb_h.status = CAM_AUTOSENSE_FAIL
2623                                    | CAM_DEV_QFRZN;
2624                }
2625                xpt_done(ccb);
2626                break;
2627        }
2628        default:
2629                DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
2630                    "status %d\n", status);
2631                xpt_freeze_devq(ccb->ccb_h.path, 1);
2632                ccb->ccb_h.status = CAM_AUTOSENSE_FAIL | CAM_DEV_QFRZN;
2633                xpt_done(ccb);
2634        }
2635}
2636
2637/*
2638 * This completion code just handles the fact that we sent a test-unit-ready
2639 * after having previously failed a READ CAPACITY with CHECK_COND.  The CCB
2640 * status for CAM is already set earlier.
2641 */
2642static void
2643umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2644    uint8_t status)
2645{
2646        DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
2647            "returned status %d\n", status);
2648
2649        xpt_done(ccb);
2650}
2651
2652/*
2653 * SCSI specific functions
2654 */
2655
2656static uint8_t
2657umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2658    uint8_t cmd_len)
2659{
2660        if ((cmd_len == 0) ||
2661            (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2662                DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2663                    "length: %d bytes\n", cmd_len);
2664                return (0);             /* failure */
2665        }
2666        sc->sc_transfer.cmd_len = cmd_len;
2667
2668        switch (cmd_ptr[0]) {
2669        case TEST_UNIT_READY:
2670                if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2671                        DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2672                            "to START_UNIT\n");
2673                        memset(sc->sc_transfer.cmd_data, 0, cmd_len);
2674                        sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2675                        sc->sc_transfer.cmd_data[4] = SSS_START;
2676                        return (1);
2677                }
2678                break;
2679
2680        case INQUIRY:
2681                /*
2682                 * some drives wedge when asked for full inquiry
2683                 * information.
2684                 */
2685                if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2686                        memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2687                        sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2688                        return (1);
2689                }
2690                break;
2691        }
2692
2693        memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2694        return (1);
2695}
2696
2697static uint8_t
2698umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
2699{
2700        if ((cmd_len == 0) ||
2701            (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2702                DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2703                    "length: %d bytes\n", cmd_len);
2704                return (0);             /* failure */
2705        }
2706        switch (cmd_ptr[0]) {
2707                /* these commands are defined in RBC: */
2708        case READ_10:
2709        case READ_CAPACITY:
2710        case START_STOP_UNIT:
2711        case SYNCHRONIZE_CACHE:
2712        case WRITE_10:
2713        case 0x2f:                      /* VERIFY_10 is absent from
2714                                         * scsi_all.h??? */
2715        case INQUIRY:
2716        case MODE_SELECT_10:
2717        case MODE_SENSE_10:
2718        case TEST_UNIT_READY:
2719        case WRITE_BUFFER:
2720                /*
2721                 * The following commands are not listed in my copy of the
2722                 * RBC specs. CAM however seems to want those, and at least
2723                 * the Sony DSC device appears to support those as well
2724                 */
2725        case REQUEST_SENSE:
2726        case PREVENT_ALLOW:
2727
2728                memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2729
2730                if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
2731                        memset(sc->sc_transfer.cmd_data + cmd_len,
2732                            0, 12 - cmd_len);
2733                        cmd_len = 12;
2734                }
2735                sc->sc_transfer.cmd_len = cmd_len;
2736                return (1);             /* sucess */
2737
2738                /* All other commands are not legal in RBC */
2739        default:
2740                DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
2741                    "command 0x%02x\n", cmd_ptr[0]);
2742                return (0);             /* failure */
2743        }
2744}
2745
2746static uint8_t
2747umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2748    uint8_t cmd_len)
2749{
2750        if ((cmd_len == 0) ||
2751            (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2752                DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2753                    "length: %d bytes\n", cmd_len);
2754                return (0);             /* failure */
2755        }
2756        /* An UFI command is always 12 bytes in length */
2757        sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
2758
2759        /* Zero the command data */
2760        memset(sc->sc_transfer.cmd_data, 0, UFI_COMMAND_LENGTH);
2761
2762        switch (cmd_ptr[0]) {
2763                /*
2764                 * Commands of which the format has been verified. They
2765                 * should work. Copy the command into the (zeroed out)
2766                 * destination buffer.
2767                 */
2768        case TEST_UNIT_READY:
2769                if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2770                        /*
2771                         * Some devices do not support this command. Start
2772                         * Stop Unit should give the same results
2773                         */
2774                        DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
2775                            "to START_UNIT\n");
2776
2777                        sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2778                        sc->sc_transfer.cmd_data[4] = SSS_START;
2779                        return (1);
2780                }
2781                break;
2782
2783        case REZERO_UNIT:
2784        case REQUEST_SENSE:
2785        case FORMAT_UNIT:
2786        case INQUIRY:
2787        case START_STOP_UNIT:
2788        case SEND_DIAGNOSTIC:
2789        case PREVENT_ALLOW:
2790        case READ_CAPACITY:
2791        case READ_10:
2792        case WRITE_10:
2793        case POSITION_TO_ELEMENT:       /* SEEK_10 */
2794        case WRITE_AND_VERIFY:
2795        case VERIFY:
2796        case MODE_SELECT_10:
2797        case MODE_SENSE_10:
2798        case READ_12:
2799        case WRITE_12:
2800        case READ_FORMAT_CAPACITIES:
2801                break;
2802
2803                /*
2804                 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
2805                 * required for UFI devices, so it is appropriate to fake
2806                 * success.
2807                 */
2808        case SYNCHRONIZE_CACHE:
2809                return (2);
2810
2811        default:
2812                DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
2813                    "command 0x%02x\n", cmd_ptr[0]);
2814                return (0);             /* failure */
2815        }
2816
2817        memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2818        return (1);                     /* success */
2819}
2820
2821/*
2822 * 8070i (ATAPI) specific functions
2823 */
2824static uint8_t
2825umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2826    uint8_t cmd_len)
2827{
2828        if ((cmd_len == 0) ||
2829            (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2830                DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2831                    "length: %d bytes\n", cmd_len);
2832                return (0);             /* failure */
2833        }
2834        /* An ATAPI command is always 12 bytes in length. */
2835        sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
2836
2837        /* Zero the command data */
2838        memset(sc->sc_transfer.cmd_data, 0, ATAPI_COMMAND_LENGTH);
2839
2840        switch (cmd_ptr[0]) {
2841                /*
2842                 * Commands of which the format has been verified. They
2843                 * should work. Copy the command into the destination
2844                 * buffer.
2845                 */
2846        case INQUIRY:
2847                /*
2848                 * some drives wedge when asked for full inquiry
2849                 * information.
2850                 */
2851                if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2852                        memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2853
2854                        sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2855                        return (1);
2856                }
2857                break;
2858
2859        case TEST_UNIT_READY:
2860                if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2861                        DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2862                            "to START_UNIT\n");
2863                        sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2864                        sc->sc_transfer.cmd_data[4] = SSS_START;
2865                        return (1);
2866                }
2867                break;
2868
2869        case REZERO_UNIT:
2870        case REQUEST_SENSE:
2871        case START_STOP_UNIT:
2872        case SEND_DIAGNOSTIC:
2873        case PREVENT_ALLOW:
2874        case READ_CAPACITY:
2875        case READ_10:
2876        case WRITE_10:
2877        case POSITION_TO_ELEMENT:       /* SEEK_10 */
2878        case SYNCHRONIZE_CACHE:
2879        case MODE_SELECT_10:
2880        case MODE_SENSE_10:
2881        case READ_BUFFER:
2882        case 0x42:                      /* READ_SUBCHANNEL */
2883        case 0x43:                      /* READ_TOC */
2884        case 0x44:                      /* READ_HEADER */
2885        case 0x47:                      /* PLAY_MSF (Play Minute/Second/Frame) */
2886        case 0x48:                      /* PLAY_TRACK */
2887        case 0x49:                      /* PLAY_TRACK_REL */
2888        case 0x4b:                      /* PAUSE */
2889        case 0x51:                      /* READ_DISK_INFO */
2890        case 0x52:                      /* READ_TRACK_INFO */
2891        case 0x54:                      /* SEND_OPC */
2892        case 0x59:                      /* READ_MASTER_CUE */
2893        case 0x5b:                      /* CLOSE_TR_SESSION */
2894        case 0x5c:                      /* READ_BUFFER_CAP */
2895        case 0x5d:                      /* SEND_CUE_SHEET */
2896        case 0xa1:                      /* BLANK */
2897        case 0xa5:                      /* PLAY_12 */
2898        case 0xa6:                      /* EXCHANGE_MEDIUM */
2899        case 0xad:                      /* READ_DVD_STRUCTURE */
2900        case 0xbb:                      /* SET_CD_SPEED */
2901        case 0xe5:                      /* READ_TRACK_INFO_PHILIPS */
2902                break;
2903
2904        case READ_12:
2905        case WRITE_12:
2906        default:
2907                DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
2908                    "command 0x%02x - trying anyway\n",
2909                    cmd_ptr[0]);
2910                break;
2911        }
2912
2913        memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2914        return (1);                     /* success */
2915}
2916
2917static uint8_t
2918umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
2919    uint8_t cmdlen)
2920{
2921        return (0);                     /* failure */
2922}
2923
2924static uint8_t
2925umass_std_transform(struct umass_softc *sc, union ccb *ccb,
2926    uint8_t *cmd, uint8_t cmdlen)
2927{
2928        uint8_t retval;
2929
2930        retval = (sc->sc_transform) (sc, cmd, cmdlen);
2931
2932        if (retval == 2) {
2933                ccb->ccb_h.status = CAM_REQ_CMP;
2934                xpt_done(ccb);
2935                return (0);
2936        } else if (retval == 0) {
2937                xpt_freeze_devq(ccb->ccb_h.path, 1);
2938                ccb->ccb_h.status = CAM_REQ_INVALID | CAM_DEV_QFRZN;
2939                xpt_done(ccb);
2940                return (0);
2941        }
2942        /* Command should be executed */
2943        return (1);
2944}
2945
2946#ifdef USB_DEBUG
2947static void
2948umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
2949{
2950        uint8_t *c = cbw->CBWCDB;
2951
2952        uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
2953        uint32_t tag = UGETDW(cbw->dCBWTag);
2954
2955        uint8_t clen = cbw->bCDBLength;
2956        uint8_t flags = cbw->bCBWFlags;
2957        uint8_t lun = cbw->bCBWLUN;
2958
2959        DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
2960            "(0x%02x%02x%02x%02x%02x%02x%s), "
2961            "data = %db, lun = %d, dir = %s\n",
2962            tag, clen,
2963            c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
2964            dlen, lun, (flags == CBWFLAGS_IN ? "in" :
2965            (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
2966}
2967
2968static void
2969umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
2970{
2971        uint32_t sig = UGETDW(csw->dCSWSignature);
2972        uint32_t tag = UGETDW(csw->dCSWTag);
2973        uint32_t res = UGETDW(csw->dCSWDataResidue);
2974        uint8_t status = csw->bCSWStatus;
2975
2976        DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
2977            "res = %d, status = 0x%02x (%s)\n",
2978            tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
2979            tag, res,
2980            status, (status == CSWSTATUS_GOOD ? "good" :
2981            (status == CSWSTATUS_FAILED ? "failed" :
2982            (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
2983}
2984
2985static void
2986umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
2987{
2988        uint8_t *c = cmd;
2989        uint8_t dir = sc->sc_transfer.dir;
2990
2991        DPRINTF(sc, UDMASS_BBB, "cmd = %db "
2992            "(0x%02x%02x%02x%02x%02x%02x%s), "
2993            "data = %db, dir = %s\n",
2994            cmdlen,
2995            c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
2996            sc->sc_transfer.data_len,
2997            (dir == DIR_IN ? "in" :
2998            (dir == DIR_OUT ? "out" :
2999            (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3000}
3001
3002static void
3003umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3004    uint32_t printlen)
3005{
3006        uint32_t i, j;
3007        char s1[40];
3008        char s2[40];
3009        char s3[5];
3010
3011        s1[0] = '\0';
3012        s3[0] = '\0';
3013
3014        sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3015        for (i = 0; (i < buflen) && (i < printlen); i++) {
3016                j = i % 16;
3017                if (j == 0 && i != 0) {
3018                        DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3019                            s1, s2);
3020                        s2[0] = '\0';
3021                }
3022                sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3023        }
3024        if (buflen > printlen)
3025                sprintf(s3, " ...");
3026        DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",
3027            s1, s2, s3);
3028}
3029
3030#endif
Note: See TracBrowser for help on using the repository browser.