umass.c revision 1.29 1 /* $NetBSD: umass.c,v 1.29 2000/04/03 03:56:49 enami Exp $ */
2 /*-
3 * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>,
4 * Nick Hibma <n_hibma (at) freebsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
29 */
30
31 /*
32 * Universal Serial Bus Mass Storage Class Bulk-Only Transport
33 * http://www.usb.org/developers/usbmassbulk_09.pdf
34 * XXX Add URL to CBI spec in www.usb.org
35 */
36
37 /*
38 * Ported to NetBSD by Lennart Augustsson <augustss (at) netbsd.org>.
39 * Parts of the code written my Jason R. Thorpe <thorpej (at) shagadelic.org>.
40 */
41
42 /*
43 * The driver handles 3 Wire Protocols
44 * - Command/Bulk/Interrupt (CBI)
45 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
46 * - Mass Storage Bulk-Only (BBB)
47 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
48 *
49 * Over these wire protocols it handles the following command protocols
50 * - SCSI
51 * - UFI (floppy command set)
52 * - 8070 (ATA/ATAPI)
53 *
54 * UFI and 8070i are transformed versions of the SCSI command set. The
55 * sc->transform method is used to convert the commands into the appropriate
56 * format (if at all necessary). For example, UFI requires all commands to be
57 * 12 bytes in length amongst other things.
58 *
59 * The source code below is marked and can be split into a number of pieces
60 * (in this order):
61 *
62 * - probe/attach/detach
63 * - generic transfer routines
64 * - BBB
65 * - CBI
66 * - CBI_I (in addition to functions from CBI)
67 * - CAM (Common Access Method)
68 * - SCSI
69 * - UFI
70 * - 8070i
71 *
72 * The protocols are implemented using a state machine, for the transfers as
73 * well as for the resets. The state machine is contained in umass_*_state.
74 * The state machine is started through either umass_*_transfer or
75 * umass_*_reset.
76 *
77 * The reason for doing this is a) CAM performs a lot better this way and b) it
78 * avoids using tsleep from interrupt context (for example after a failed
79 * transfer).
80 */
81
82 /*
83 * The SCSI related part of this driver has been derived from the
84 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch (at) freebsd.org).
85 *
86 * The CAM layer uses so called actions which are messages sent to the host
87 * adapter for completion. The actions come in through umass_cam_action. The
88 * appropriate block of routines is called depending on the transport protocol
89 * in use. When the transfer has finished, these routines call
90 * umass_cam_cb again to complete the CAM command.
91 */
92
93 /* XXX Should we split the driver into a number of files? umass.c,
94 * umass_scsi.c, umass_8070.c, umass_ufi.c, umass_bbb.c, umass_cbi.c or
95 * something similar?
96 */
97
98 #include "atapibus.h"
99
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/kernel.h>
103 #include <sys/conf.h>
104 #if defined(__NetBSD__) || defined(__OpenBSD__)
105 #include <sys/buf.h>
106 #include <sys/device.h>
107 #include <sys/ioctl.h>
108 #include <sys/malloc.h>
109 #undef KASSERT
110 #define KASSERT(cond, msg)
111 #elif defined(__FreeBSD__)
112 #include <sys/module.h>
113 #include <sys/bus.h>
114 #include <machine/clock.h>
115 #endif
116
117 #include <dev/usb/usb.h>
118 #include <dev/usb/usbdi.h>
119 #include <dev/usb/usbdi_util.h>
120 #include <dev/usb/usbdevs.h>
121
122 #if defined(__FreeBSD__)
123 #include <cam/cam.h>
124 #include <cam/cam_ccb.h>
125 #include <cam/cam_sim.h>
126 #include <cam/cam_xpt_sim.h>
127 #include <cam/scsi/scsi_all.h>
128 #include <cam/scsi/scsi_da.h>
129
130 #ifdef UMASS_DO_CAM_RESCAN
131 #include <sys/devicestat.h>
132 #include <cam/cam_periph.h>
133 #endif
134
135 #elif defined(__NetBSD__) || defined(__OpenBSD__)
136 #include <sys/scsiio.h>
137 #include <dev/scsipi/scsi_all.h>
138 #include <dev/scsipi/scsipi_all.h>
139 #include <dev/scsipi/scsiconf.h>
140
141 #include <dev/scsipi/atapiconf.h>
142
143 #include <dev/scsipi/scsipi_disk.h>
144 #include <dev/scsipi/scsi_disk.h>
145 #include <dev/scsipi/scsi_changer.h>
146
147 #include <dev/ata/atavar.h> /* XXX */
148 #endif
149
150 #ifdef UMASS_DEBUG
151 #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0)
152 #define DPRINTF(m, x) if (umassdebug & (m)) logprintf x
153 #define UDMASS_UPPER 0x00008000 /* upper layer */
154 #define UDMASS_GEN 0x00010000 /* general */
155 #define UDMASS_SCSI 0x00020000 /* scsi */
156 #define UDMASS_UFI 0x00040000 /* ufi command set */
157 #define UDMASS_8070 0x00080000 /* 8070i command set */
158 #define UDMASS_USB 0x00100000 /* USB general */
159 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
160 #define UDMASS_CBI 0x00400000 /* CBI transfers */
161 #define UDMASS_ALL 0xffff0000 /* all of the above */
162
163 #define UDMASS_XFER 0x40000000 /* all transfers */
164 #define UDMASS_CMD 0x80000000
165
166 int umassdebug = 0; //UDMASS_ALL;
167 #else
168 #define DIF(m, x) /* nop */
169 #define DPRINTF(m, x) /* nop */
170 #endif
171
172
173 /* Generic definitions */
174
175 /* Direction for umass_*_transfer */
176 #define DIR_NONE 0
177 #define DIR_IN 1
178 #define DIR_OUT 2
179
180 /* The transfer speed determines the timeout value */
181 #define UMASS_DEFAULT_TRANSFER_SPEED 150 /* in kb/s, conservative est. */
182 #define UMASS_FLOPPY_TRANSFER_SPEED 20
183 #define UMASS_ZIP100_TRANSFER_SPEED 650
184
185 #define UMASS_SPINUP_TIME 10000 /* ms */
186
187 #ifdef __FreeBSD__
188 /* device name */
189 #define DEVNAME "umass"
190 #define DEVNAME_SIM "umass-"
191
192 #define UMASS_MAX_TRANSFER_SIZE 65536
193
194 /* CAM specific definitions */
195
196 /* The bus id, whatever that is */
197 #define UMASS_SCSI_BUS 0
198
199 /* All USB drives are 'connected' to one SIM (SCSI controller). umass3
200 * ends up being target 3 on that SIM. When a request for target 3
201 * comes in we fetch the softc with devclass_get_softc(target_id).
202 *
203 * The SIM is the highest target number. This makes sure that umass0 corresponds
204 * to target 0 on the USB SCSI bus.
205 */
206 #ifndef UMASS_DEBUG
207 #define UMASS_SCSIID_MAX 32 /* maximum number of drives expected */
208 #else
209 /* while debugging avoid unnecessary clutter in the output at umass_cam_rescan
210 * (XPT_PATH_INQ)
211 */
212 #define UMASS_SCSIID_MAX 3 /* maximum number of drives expected */
213 #endif
214 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
215 #endif
216
217 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
218
219
220 /* Bulk-Only features */
221
222 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
223 #define UR_BBB_GET_MAX_LUN 0xfe
224
225 /* Command Block Wrapper */
226 typedef struct {
227 uDWord dCBWSignature;
228 # define CBWSIGNATURE 0x43425355
229 uDWord dCBWTag;
230 uDWord dCBWDataTransferLength;
231 uByte bCBWFlags;
232 # define CBWFLAGS_OUT 0x00
233 # define CBWFLAGS_IN 0x80
234 uByte bCBWLUN;
235 uByte bCDBLength;
236 # define CBWCDBLENGTH 16
237 uByte CBWCDB[CBWCDBLENGTH];
238 } umass_bbb_cbw_t;
239 #define UMASS_BBB_CBW_SIZE 31
240
241 /* Command Status Wrapper */
242 typedef struct {
243 uDWord dCSWSignature;
244 # define CSWSIGNATURE 0x53425355
245 uDWord dCSWTag;
246 uDWord dCSWDataResidue;
247 uByte bCSWStatus;
248 # define CSWSTATUS_GOOD 0x0
249 # define CSWSTATUS_FAILED 0x1
250 # define CSWSTATUS_PHASE 0x2
251 } umass_bbb_csw_t;
252 #define UMASS_BBB_CSW_SIZE 13
253
254 /* CBI features */
255
256 #define UR_CBI_ADSC 0x00
257
258 typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */
259
260 typedef union {
261 struct {
262 unsigned char type;
263 #define IDB_TYPE_CCI 0x00
264 unsigned char value;
265 #define IDB_VALUE_PASS 0x00
266 #define IDB_VALUE_FAIL 0x01
267 #define IDB_VALUE_PHASE 0x02
268 #define IDB_VALUE_PERSISTENT 0x03
269 #define IDB_VALUE_STATUS_MASK 0x03
270 } common;
271
272 struct {
273 unsigned char asc;
274 unsigned char ascq;
275 } ufi;
276 } umass_cbi_sbl_t;
277
278
279
280 struct umass_softc; /* see below */
281
282 typedef void (*transfer_cb_f) __P((struct umass_softc *sc, void *priv,
283 int residue, int status));
284 #define STATUS_CMD_OK 0 /* everything ok */
285 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
286 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
287 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
288
289 typedef void (*wire_reset_f) __P((struct umass_softc *sc, int status));
290 typedef void (*wire_transfer_f) __P((struct umass_softc *sc, int lun,
291 void *cmd, int cmdlen, void *data, int datalen,
292 int dir, transfer_cb_f cb, void *priv));
293 typedef void (*wire_state_f) __P((usbd_xfer_handle xfer,
294 usbd_private_handle priv, usbd_status err));
295
296 #if defined(__FreeBSD__)
297 typedef int (*command_transform_f) __P((struct umass_softc *sc,
298 unsigned char *cmd, int cmdlen,
299 unsigned char **rcmd, int *rcmdlen));
300 #endif
301
302
303 /* the per device structure */
304 struct umass_softc {
305 USBBASEDEVICE sc_dev; /* base device */
306 usbd_device_handle sc_udev; /* device */
307
308 unsigned char drive;
309 # define DRIVE_GENERIC 0 /* use defaults for this one */
310 # define ZIP_100 1 /* to be used for quirks */
311 # define SHUTTLE_EUSB 2
312
313 unsigned char quirks;
314 /* The drive does not support Test Unit Ready. Convert to
315 * Start Unit.
316 * Y-E Data
317 * ZIP 100
318 */
319 # define NO_TEST_UNIT_READY 0x01
320 /* The drive does not reset the Unit Attention state after
321 * REQUEST SENSE has been sent. The INQUIRY command does not reset
322 * the UA either, and so CAM runs in circles trying to retrieve the
323 * initial INQUIRY data.
324 * Y-E Data
325 */
326 # define RS_NO_CLEAR_UA 0x02 /* no REQUEST SENSE on INQUIRY*/
327 /* The drive does not support START_STOP.
328 * Shuttle E-USB
329 */
330 # define NO_START_STOP 0x04
331
332 unsigned int proto;
333 # define PROTO_UNKNOWN 0x0000 /* unknown protocol */
334 # define PROTO_BBB 0x0001 /* USB wire protocol */
335 # define PROTO_CBI 0x0002
336 # define PROTO_CBI_I 0x0004
337 # define PROTO_WIRE 0x00ff /* USB wire protocol mask */
338 # define PROTO_SCSI 0x0100 /* command protocol */
339 # define PROTO_8070 0x0200
340 # define PROTO_UFI 0x0400
341 # define PROTO_COMMAND 0xff00 /* command protocol mask */
342
343 usbd_interface_handle iface; /* Mass Storage interface */
344 int ifaceno; /* MS iface number */
345
346 u_int8_t bulkin; /* bulk-in Endpoint Address */
347 u_int8_t bulkout; /* bulk-out Endpoint Address */
348 u_int8_t intrin; /* intr-in Endp. (CBI) */
349 usbd_pipe_handle bulkin_pipe;
350 usbd_pipe_handle bulkout_pipe;
351 usbd_pipe_handle intrin_pipe;
352
353 /* Reset the device in a wire protocol specific way */
354 wire_reset_f reset;
355
356 /* The start of a wire transfer. It prepares the whole transfer (cmd,
357 * data, and status stage) and initiates it. It is up to the state
358 * machine (below) to handle the various stages and errors in these
359 */
360 wire_transfer_f transfer;
361
362 /* The state machine, handling the various states during a transfer */
363 wire_state_f state;
364
365 #if defined(__FreeBSD__)
366 /* The command transform function is used to conver the SCSI commands
367 * into their derivatives, like UFI, ATAPI, and friends.
368 */
369 command_transform_f transform; /* command transform */
370 #endif
371
372 /* Bulk specific variables for transfers in progress */
373 umass_bbb_cbw_t cbw; /* command block wrapper */
374 umass_bbb_csw_t csw; /* command status wrapper*/
375 /* CBI specific variables for transfers in progress */
376 umass_cbi_cbl_t cbl; /* command block */
377 umass_cbi_sbl_t sbl; /* status block */
378
379 /* generic variables for transfers in progress */
380 /* ctrl transfer requests */
381 usb_device_request_t request;
382
383 /* xfer handles
384 * Most of our operations are initiated from interrupt context, so
385 * we need to avoid using the one that is in use. We want to avoid
386 * allocating them in the interrupt context as well.
387 */
388 /* indices into array below */
389 # define XFER_BBB_CBW 0 /* Bulk-Only */
390 # define XFER_BBB_DATA 1
391 # define XFER_BBB_DCLEAR 2
392 # define XFER_BBB_CSW1 3
393 # define XFER_BBB_CSW2 4
394 # define XFER_BBB_SCLEAR 5
395 # define XFER_BBB_RESET1 6
396 # define XFER_BBB_RESET2 7
397 # define XFER_BBB_RESET3 8
398
399 # define XFER_CBI_CB 0 /* CBI */
400 # define XFER_CBI_DATA 1
401 # define XFER_CBI_STATUS 2
402 # define XFER_CBI_DCLEAR 3
403 # define XFER_CBI_SCLEAR 4
404 # define XFER_CBI_RESET1 5
405 # define XFER_CBI_RESET2 6
406 # define XFER_CBI_RESET3 7
407
408 # define XFER_NR 9 /* maximum number */
409
410 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */
411
412 void *data_buffer;
413
414 int transfer_dir; /* data direction */
415 void *transfer_data; /* data buffer */
416 int transfer_datalen; /* (maximum) length */
417 int transfer_actlen; /* actual length */
418 transfer_cb_f transfer_cb; /* callback */
419 void *transfer_priv; /* for callback */
420 int transfer_status;
421
422 int transfer_state;
423 # define TSTATE_IDLE 0
424 # define TSTATE_BBB_COMMAND 1 /* CBW transfer */
425 # define TSTATE_BBB_DATA 2 /* Data transfer */
426 # define TSTATE_BBB_DCLEAR 3 /* clear endpt stall */
427 # define TSTATE_BBB_STATUS1 4 /* clear endpt stall */
428 # define TSTATE_BBB_SCLEAR 5 /* clear endpt stall */
429 # define TSTATE_BBB_STATUS2 6 /* CSW transfer */
430 # define TSTATE_BBB_RESET1 7 /* reset command */
431 # define TSTATE_BBB_RESET2 8 /* in clear stall */
432 # define TSTATE_BBB_RESET3 9 /* out clear stall */
433 # define TSTATE_CBI_COMMAND 10 /* command transfer */
434 # define TSTATE_CBI_DATA 11 /* data transfer */
435 # define TSTATE_CBI_STATUS 12 /* status transfer */
436 # define TSTATE_CBI_DCLEAR 13 /* clear ep stall */
437 # define TSTATE_CBI_SCLEAR 14 /* clear ep stall */
438 # define TSTATE_CBI_RESET1 15 /* reset command */
439 # define TSTATE_CBI_RESET2 16 /* in clear stall */
440 # define TSTATE_CBI_RESET3 17 /* out clear stall */
441 # define TSTATE_STATES 18 /* # of states above */
442
443
444 int transfer_speed; /* in kb/s */
445 int timeout; /* in msecs */
446
447 u_int8_t maxlun; /* max lun supported */
448
449 #if defined(__FreeBSD__)
450 /* SCSI/CAM specific variables */
451 struct scsi_sense cam_scsi_sense;
452
453 #elif defined(__NetBSD__) || defined(__OpenBSD__)
454 union {
455 struct scsipi_link sc_link;
456 struct {
457 struct ata_atapi_attach sc_aa;
458 struct ata_drive_datas sc_aa_drive;
459 } aa;
460 } u;
461 struct atapi_adapter sc_atapi_adapter;
462 #define sc_adapter sc_atapi_adapter._generic
463 int sc_xfer_flags;
464 usbd_status sc_sync_status;
465 struct scsipi_sense sc_sense_cmd;
466
467 device_ptr_t sc_child; /* child device, for detach */
468 char sc_dying;
469
470 #endif
471 };
472
473 #ifdef UMASS_DEBUG
474 char *states[TSTATE_STATES+1] = {
475 /* should be kept in sync with the list at transfer_state */
476 "Idle",
477 "BBB CBW",
478 "BBB Data",
479 "BBB Data bulk-in/-out clear stall",
480 "BBB CSW, 1st attempt",
481 "BBB CSW bulk-in clear stall",
482 "BBB CSW, 2nd attempt",
483 "BBB Reset",
484 "BBB bulk-in clear stall",
485 "BBB bulk-out clear stall",
486 "CBI Command",
487 "CBI Data",
488 "CBI Status",
489 "CBI Data bulk-in/-out clear stall",
490 "CBI Status intr-in clear stall",
491 "CBI Reset",
492 "CBI bulk-in clear stall",
493 "CBI bulk-out clear stall",
494 NULL
495 };
496 #endif
497
498 struct cam_sim *umass_sim; /* SCSI Interface Module */
499 struct cam_path *umass_path; /* and its path */
500
501
502 /* USB device probe/attach/detach functions */
503 USB_DECLARE_DRIVER(umass);
504 Static void umass_disco __P((struct umass_softc *sc));
505 Static int umass_match_proto __P((struct umass_softc *sc,
506 usbd_interface_handle iface,
507 usbd_device_handle dev));
508 Static void umass_init_shuttle __P((struct umass_softc *sc));
509
510 /* generic transfer functions */
511 Static usbd_status umass_setup_transfer __P((struct umass_softc *sc,
512 usbd_pipe_handle pipe,
513 void *buffer, int buflen, int flags,
514 usbd_xfer_handle xfer));
515 Static usbd_status umass_setup_ctrl_transfer __P((struct umass_softc *sc,
516 usbd_device_handle dev,
517 usb_device_request_t *req,
518 void *buffer, int buflen, int flags,
519 usbd_xfer_handle xfer));
520 Static void umass_clear_endpoint_stall __P((struct umass_softc *sc,
521 u_int8_t endpt, usbd_pipe_handle pipe,
522 int state, usbd_xfer_handle xfer));
523 #if 0
524 Static void umass_reset __P((struct umass_softc *sc,
525 transfer_cb_f cb, void *priv));
526 #endif
527
528 /* Bulk-Only related functions */
529 Static void umass_bbb_reset __P((struct umass_softc *sc, int status));
530 Static void umass_bbb_transfer __P((struct umass_softc *sc, int lun,
531 void *cmd, int cmdlen,
532 void *data, int datalen, int dir,
533 transfer_cb_f cb, void *priv));
534 Static void umass_bbb_state __P((usbd_xfer_handle xfer,
535 usbd_private_handle priv,
536 usbd_status err));
537 usbd_status umass_bbb_get_max_lun __P((struct umass_softc *sc,
538 u_int8_t *maxlun));
539
540
541 /* CBI related functions */
542 Static int umass_cbi_adsc __P((struct umass_softc *sc, char *buffer,int buflen,
543 usbd_xfer_handle xfer));
544 Static void umass_cbi_reset __P((struct umass_softc *sc, int status));
545 Static void umass_cbi_transfer __P((struct umass_softc *sc, int lun,
546 void *cmd, int cmdlen,
547 void *data, int datalen, int dir,
548 transfer_cb_f cb, void *priv));
549 Static void umass_cbi_state __P((usbd_xfer_handle xfer,
550 usbd_private_handle priv, usbd_status err));
551
552 #if defined(__FreeBSD__)
553 /* CAM related functions */
554 Static void umass_cam_action __P((struct cam_sim *sim, union ccb *ccb));
555 Static void umass_cam_poll __P((struct cam_sim *sim));
556
557 Static void umass_cam_cb __P((struct umass_softc *sc, void *priv,
558 int residue, int status));
559 Static void umass_cam_sense_cb __P((struct umass_softc *sc, void *priv,
560 int residue, int status));
561
562 #ifdef UMASS_DO_CAM_RESCAN
563 Static void umass_cam_rescan __P((struct umass_softc *sc));
564 #endif
565
566 Static int umass_cam_attach_sim __P((void));
567 Static int umass_cam_attach __P((struct umass_softc *sc));
568 Static int umass_cam_detach_sim __P((void));
569 Static int umass_cam_detach __P((struct umass_softc *sc));
570
571 #elif defined(__NetBSD__) || defined(__OpenBSD__)
572
573 #define UMASS_SCSIID_HOST 0x00
574 #define UMASS_SCSIID_DEVICE 0x01
575
576 #define UMASS_MAX_TRANSFER_SIZE MAXBSIZE
577
578 struct scsipi_device umass_dev =
579 {
580 NULL, /* Use default error handler */
581 NULL, /* have a queue, served by this */
582 NULL, /* have no async handler */
583 NULL, /* Use default 'done' routine */
584 };
585
586 Static int umass_scsipi_cmd __P((struct scsipi_xfer *xs));
587 Static void umass_scsipi_minphys __P((struct buf *bp));
588 Static int umass_scsipi_ioctl __P((struct scsipi_link *, u_long,
589 caddr_t, int, struct proc *));
590 Static void umass_scsipi_cb __P((struct umass_softc *sc, void *priv,
591 int residue, int status));
592 Static void umass_scsipi_sense_cb __P((struct umass_softc *sc, void *priv,
593 int residue, int status));
594
595 Static int scsipiprint __P((void *aux, const char *pnp));
596 #if NATAPIBUS > 0
597 Static void umass_atapi_probedev __P((struct atapibus_softc *, int));
598 #endif
599 #endif
600
601 #if defined(__FreeBSD__)
602 /* SCSI specific functions */
603 Static int umass_scsi_transform __P((struct umass_softc *sc,
604 unsigned char *cmd, int cmdlen,
605 unsigned char **rcmd, int *rcmdlen));
606
607 /* UFI specific functions */
608 Static int umass_ufi_transform __P((struct umass_softc *sc,
609 unsigned char *cmd, int cmdlen,
610 unsigned char **rcmd, int *rcmdlen));
611
612 /* 8070 specific functions */
613 Static int umass_8070_transform __P((struct umass_softc *sc,
614 unsigned char *cmd, int cmdlen,
615 unsigned char **rcmd, int *rcmdlen));
616 #endif
617
618 #ifdef UMASS_DEBUG
619 /* General debugging functions */
620 Static void umass_bbb_dump_cbw __P((struct umass_softc *sc,
621 umass_bbb_cbw_t *cbw));
622 Static void umass_bbb_dump_csw __P((struct umass_softc *sc,
623 umass_bbb_csw_t *csw));
624 Static void umass_dump_buffer __P((struct umass_softc *sc, u_int8_t *buffer,
625 int buflen, int printlen));
626 #endif
627
628
629 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe); /* XXXXX */
630
631 /*
632 * USB device probe/attach/detach
633 */
634
635 /*
636 * Match the device we are seeing with the devices supported. Fill in the
637 * proto and drive fields in the softc accordingly.
638 * This function is called from both probe and attach.
639 */
640
641 Static int
642 umass_match_proto(sc, iface, dev)
643 struct umass_softc *sc;
644 usbd_interface_handle iface;
645 usbd_device_handle dev;
646 {
647 usb_device_descriptor_t *dd;
648 usb_interface_descriptor_t *id;
649
650 /*
651 * Fill in sc->drive and sc->proto and return a match
652 * value if both are determined and 0 otherwise.
653 */
654
655 sc->drive = DRIVE_GENERIC;
656 sc->proto = PROTO_UNKNOWN;
657 sc->transfer_speed = UMASS_DEFAULT_TRANSFER_SPEED;
658
659 sc->sc_udev = dev;
660 dd = usbd_get_device_descriptor(dev);
661
662 if (UGETW(dd->idVendor) == USB_VENDOR_SHUTTLE
663 && UGETW(dd->idProduct) == USB_PRODUCT_SHUTTLE_EUSB) {
664 sc->drive = SHUTTLE_EUSB;
665 #if CBI_I
666 sc->proto = PROTO_8070 | PROTO_CBI_I;
667 #else
668 sc->proto = PROTO_8070 | PROTO_CBI;
669 #endif
670 sc->quirks |= NO_TEST_UNIT_READY | NO_START_STOP;
671 return (UMATCH_VENDOR_PRODUCT);
672 }
673
674 if (UGETW(dd->idVendor) == USB_VENDOR_YEDATA
675 && UGETW(dd->idProduct) == USB_PRODUCT_YEDATA_FLASHBUSTERU) {
676
677 /* Revisions < 1.28 do not handle the interrupt endpoint
678 * very well.
679 */
680 if (UGETW(dd->bcdDevice) < 0x128)
681 sc->proto = PROTO_UFI | PROTO_CBI;
682 else
683 #if CBI_I
684 sc->proto = PROTO_UFI | PROTO_CBI_I;
685 #else
686 sc->proto = PROTO_UFI | PROTO_CBI;
687 #endif
688 /*
689 * Revisions < 1.28 do not have the TEST UNIT READY command
690 * Revisions == 1.28 have a broken TEST UNIT READY
691 */
692 if (UGETW(dd->bcdDevice) <= 0x128)
693 sc->quirks |= NO_TEST_UNIT_READY;
694
695 sc->quirks |= RS_NO_CLEAR_UA;
696 sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED;
697 return (UMATCH_VENDOR_PRODUCT_REV);
698 }
699
700
701 id = usbd_get_interface_descriptor(iface);
702 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
703 return (UMATCH_NONE);
704
705 switch (id->bInterfaceSubClass) {
706 case UISUBCLASS_SCSI:
707 sc->proto |= PROTO_SCSI;
708 break;
709 case UISUBCLASS_UFI:
710 sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED;
711 sc->proto |= PROTO_UFI;
712 break;
713 case UISUBCLASS_SFF8020I:
714 case UISUBCLASS_SFF8070I:
715 sc->proto |= PROTO_8070;
716 break;
717 default:
718 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
719 USBDEVNAME(sc->sc_dev), id->bInterfaceSubClass));
720 return (UMATCH_NONE);
721 }
722
723 switch (id->bInterfaceProtocol) {
724 case UIPROTO_MASS_CBI:
725 sc->proto |= PROTO_CBI;
726 break;
727 case UIPROTO_MASS_CBI_I:
728 #if CBI_I
729 sc->proto |= PROTO_CBI_I;
730 #else
731 sc->proto |= PROTO_CBI;
732 #endif
733 break;
734 case UIPROTO_MASS_BBB:
735 sc->proto |= PROTO_BBB;
736 break;
737 case UIPROTO_MASS_BBB_P:
738 sc->drive = ZIP_100;
739 sc->proto |= PROTO_BBB;
740 sc->transfer_speed = UMASS_ZIP100_TRANSFER_SPEED;
741 sc->quirks |= NO_TEST_UNIT_READY;
742 break;
743 default:
744 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
745 USBDEVNAME(sc->sc_dev), id->bInterfaceProtocol));
746 return (UMATCH_NONE);
747 }
748
749 return (UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
750 }
751
752 USB_MATCH(umass)
753 {
754 USB_MATCH_START(umass, uaa);
755 #if defined(__FreeBSD__)
756 struct umass_softc *sc = device_get_softc(self);
757 #else if defined(__NetBSD__) || defined(__OpenBSD__)
758 struct umass_softc scs, *sc = &scs;
759 memset(sc, 0, sizeof *sc);
760 #endif
761
762 if (uaa->iface == NULL)
763 return(UMATCH_NONE);
764
765 return (umass_match_proto(sc, uaa->iface, uaa->device));
766 }
767
768 USB_ATTACH(umass)
769 {
770 USB_ATTACH_START(umass, sc, uaa);
771 usb_interface_descriptor_t *id;
772 usb_endpoint_descriptor_t *ed;
773 char devinfo[1024];
774 int i, bno;
775 int err;
776
777 /*
778 * the softc struct is bzero-ed in device_set_driver. We can safely
779 * call umass_detach without specifically initialising the struct.
780 */
781
782 usbd_devinfo(uaa->device, 0, devinfo);
783 USB_ATTACH_SETUP;
784
785 sc->iface = uaa->iface;
786 sc->ifaceno = uaa->ifaceno;
787
788 /* initialise the proto and drive values in the umass_softc (again) */
789 (void) umass_match_proto(sc, sc->iface, uaa->device);
790
791 /*
792 * The timeout is based on the maximum expected transfer size
793 * divided by the expected transfer speed.
794 * We multiply by 4 to make sure a busy system doesn't make things
795 * fail.
796 */
797 sc->timeout = 4 * UMASS_MAX_TRANSFER_SIZE / sc->transfer_speed;
798 sc->timeout += UMASS_SPINUP_TIME; /* allow for spinning up */
799
800 id = usbd_get_interface_descriptor(sc->iface);
801 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
802
803 printf("%s: using ", USBDEVNAME(sc->sc_dev));
804 switch (sc->proto & PROTO_COMMAND) {
805 case PROTO_SCSI:
806 printf("SCSI");
807 break;
808 case PROTO_8070:
809 printf("8070i");
810 break;
811 case PROTO_UFI:
812 printf("UFI");
813 break;
814 default:
815 printf("(unknown 0x%02x)", sc->proto&PROTO_COMMAND);
816 break;
817 }
818 printf(" over ");
819 switch (sc->proto & PROTO_WIRE) {
820 case PROTO_BBB:
821 printf("Bulk-Only");
822 break;
823 case PROTO_CBI: /* uses Command/Bulk pipes */
824 printf("CBI");
825 break;
826 case PROTO_CBI_I: /* uses Command/Bulk/Interrupt pipes */
827 printf("CBI with CCI");
828 break;
829 default:
830 printf("(unknown 0x%02x)", sc->proto&PROTO_WIRE);
831 }
832 printf("\n");
833
834 /*
835 * In addition to the Control endpoint the following endpoints
836 * are required:
837 * a) bulk-in endpoint.
838 * b) bulk-out endpoint.
839 * and for Control/Bulk/Interrupt with CCI (CBI_I)
840 * c) intr-in
841 *
842 * The endpoint addresses are not fixed, so we have to read them
843 * from the device descriptors of the current interface.
844 */
845 for (i = 0 ; i < id->bNumEndpoints ; i++) {
846 ed = usbd_interface2endpoint_descriptor(sc->iface, i);
847 if (!ed) {
848 printf("%s: could not read endpoint descriptor\n",
849 USBDEVNAME(sc->sc_dev));
850 USB_ATTACH_ERROR_RETURN;
851 }
852 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
853 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
854 sc->bulkin = ed->bEndpointAddress;
855 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
856 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
857 sc->bulkout = ed->bEndpointAddress;
858 } else if (sc->proto & PROTO_CBI_I
859 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
860 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
861 sc->intrin = ed->bEndpointAddress;
862 #ifdef UMASS_DEBUG
863 if (UGETW(ed->wMaxPacketSize) > 2) {
864 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
865 USBDEVNAME(sc->sc_dev),
866 UGETW(ed->wMaxPacketSize)));
867 }
868 #endif
869 }
870 }
871
872 /* check whether we found all the endpoints we need */
873 if (!sc->bulkin || !sc->bulkout
874 || (sc->proto & PROTO_CBI_I && !sc->intrin) ) {
875 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
876 USBDEVNAME(sc->sc_dev),
877 sc->bulkin, sc->bulkout, sc->intrin));
878 umass_disco(sc);
879 USB_ATTACH_ERROR_RETURN;
880 }
881
882 /*
883 * Get the maximum LUN supported by the device.
884 */
885 if ((sc->proto & PROTO_WIRE) == PROTO_BBB) {
886 err = umass_bbb_get_max_lun(sc, &sc->maxlun);
887 if (err) {
888 printf("%s: unable to get Max Lun: %s\n",
889 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
890 USB_ATTACH_ERROR_RETURN;
891 }
892 } else {
893 sc->maxlun = 0;
894 }
895
896 /* Open the bulk-in and -out pipe */
897 err = usbd_open_pipe(sc->iface, sc->bulkout,
898 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
899 if (err) {
900 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
901 USBDEVNAME(sc->sc_dev), sc->bulkout));
902 umass_disco(sc);
903 USB_ATTACH_ERROR_RETURN;
904 }
905 err = usbd_open_pipe(sc->iface, sc->bulkin,
906 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
907 if (err) {
908 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
909 USBDEVNAME(sc->sc_dev), sc->bulkin));
910 umass_disco(sc);
911 USB_ATTACH_ERROR_RETURN;
912 }
913 /*
914 * Open the intr-in pipe if the protocol is CBI with CCI.
915 * Note: early versions of the Zip drive do have an interrupt pipe, but
916 * this pipe is unused
917 *
918 * We do not open the interrupt pipe as an interrupt pipe, but as a
919 * normal bulk endpoint. We send an IN transfer down the wire at the
920 * appropriate time, because we know exactly when to expect data on
921 * that endpoint. This saves bandwidth, but more important, makes the
922 * code for handling the data on that endpoint simpler. No data
923 * arriving concurrently.
924 */
925 if (sc->proto & PROTO_CBI_I) {
926 err = usbd_open_pipe(sc->iface, sc->intrin,
927 USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
928 if (err) {
929 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
930 USBDEVNAME(sc->sc_dev), sc->intrin));
931 umass_disco(sc);
932 USB_ATTACH_ERROR_RETURN;
933 }
934 }
935
936 /* initialisation of generic part */
937 sc->transfer_state = TSTATE_IDLE;
938
939 /* request a sufficient number of xfer handles */
940 for (i = 0; i < XFER_NR; i++) {
941 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
942 if (sc->transfer_xfer[i] == 0) {
943 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
944 USBDEVNAME(sc->sc_dev)));
945 umass_disco(sc);
946 USB_ATTACH_ERROR_RETURN;
947 }
948 }
949 /* Allocate buffer for data transfer (it's huge). */
950 switch (sc->proto & PROTO_WIRE) {
951 case PROTO_BBB:
952 bno = XFER_BBB_DATA;
953 goto dalloc;
954 case PROTO_CBI:
955 bno = XFER_CBI_DATA;
956 goto dalloc;
957 case PROTO_CBI_I:
958 bno = XFER_CBI_DATA;
959 dalloc:
960 sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno],
961 UMASS_MAX_TRANSFER_SIZE);
962 if (sc->data_buffer == NULL) {
963 umass_disco(sc);
964 USB_ATTACH_ERROR_RETURN;
965 }
966 break;
967 default:
968 break;
969 }
970
971 /* Initialise the wire protocol specific methods */
972 if (sc->proto & PROTO_BBB) {
973 sc->reset = umass_bbb_reset;
974 sc->transfer = umass_bbb_transfer;
975 sc->state = umass_bbb_state;
976 } else if ((sc->proto & PROTO_CBI) || (sc->proto & PROTO_CBI_I)) {
977 sc->reset = umass_cbi_reset;
978 sc->transfer = umass_cbi_transfer;
979 sc->state = umass_cbi_state;
980 #ifdef UMASS_DEBUG
981 } else {
982 panic("%s:%d: Unknown proto 0x%02x\n",
983 __FILE__, __LINE__, sc->proto);
984 #endif
985 }
986
987 if (sc->drive == SHUTTLE_EUSB)
988 umass_init_shuttle(sc);
989
990 #if defined(__FreeBSD__)
991 if (sc->proto & PROTO_SCSI)
992 sc->transform = umass_scsi_transform;
993 else if (sc->proto & PROTO_UFI)
994 sc->transform = umass_ufi_transform;
995 else if (sc->proto & PROTO_8070)
996 sc->transform = umass_8070_transform;
997 #ifdef UMASS_DEBUG
998 else
999 panic("No transformation defined for command proto 0x%02x\n",
1000 sc->proto & PROTO_COMMAND);
1001 #endif
1002
1003 /* From here onwards the device can be used. */
1004
1005 if ((sc->proto & PROTO_SCSI) ||
1006 (sc->proto & PROTO_8070) ||
1007 (sc->proto & PROTO_UFI)) {
1008 /* Prepare the SCSI command block */
1009 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1010
1011 /* If this is the first device register the SIM */
1012 if (umass_sim == NULL) {
1013 err = umass_cam_attach_sim();
1014 if (err) {
1015 umass_disco(self);
1016 USB_ATTACH_ERROR_RETURN;
1017 }
1018 }
1019
1020 /* Attach the new device to our SCSI host controller (SIM) */
1021 err = umass_cam_attach(sc);
1022 if (err) {
1023 umass_disco(self);
1024 USB_ATTACH_ERROR_RETURN;
1025 }
1026 } else {
1027 panic("%s:%d: Unknown proto 0x%02x\n",
1028 __FILE__, __LINE__, sc->proto);
1029 }
1030 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1031 /*
1032 * Fill in the adapter.
1033 */
1034 sc->sc_adapter.scsipi_cmd = umass_scsipi_cmd;
1035 sc->sc_adapter.scsipi_minphys = umass_scsipi_minphys;
1036 sc->sc_adapter.scsipi_ioctl = umass_scsipi_ioctl;
1037
1038 /*
1039 * fill in the prototype scsipi_link.
1040 */
1041 switch (sc->proto & PROTO_COMMAND) {
1042 case PROTO_SCSI:
1043 /*case PROTO_UFI:*/
1044 sc->u.sc_link.type = BUS_SCSI;
1045 sc->u.sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
1046 sc->u.sc_link.adapter_softc = sc;
1047 sc->u.sc_link.scsipi_scsi.adapter_target = UMASS_SCSIID_HOST;
1048 sc->u.sc_link.adapter = &sc->sc_adapter;
1049 sc->u.sc_link.device = &umass_dev;
1050 sc->u.sc_link.openings = 1;
1051 sc->u.sc_link.scsipi_scsi.max_target = UMASS_SCSIID_DEVICE;
1052 sc->u.sc_link.scsipi_scsi.max_lun = sc->maxlun;
1053
1054 if(sc->quirks & NO_TEST_UNIT_READY)
1055 sc->u.sc_link.quirks |= ADEV_NOTUR;
1056 break;
1057
1058 #if NATAPIBUS > 0
1059 case PROTO_8070:
1060 sc->u.aa.sc_aa.aa_type = T_ATAPI;
1061 sc->u.aa.sc_aa.aa_channel = 0;
1062 sc->u.aa.sc_aa.aa_openings = 1;
1063 sc->u.aa.sc_aa.aa_drv_data = &sc->u.aa.sc_aa_drive;
1064 sc->u.aa.sc_aa.aa_bus_private = &sc->sc_atapi_adapter;
1065 sc->sc_atapi_adapter.atapi_probedev = umass_atapi_probedev;
1066 sc->sc_atapi_adapter.atapi_kill_pending = scsi_kill_pending;
1067 break;
1068 #endif
1069
1070 default:
1071 printf("%s: proto=0x%x not supported yet\n",
1072 USBDEVNAME(sc->sc_dev), sc->proto);
1073 umass_disco(sc);
1074 USB_ATTACH_ERROR_RETURN;
1075 }
1076
1077 sc->sc_child = config_found(&sc->sc_dev, &sc->u, scsipiprint);
1078 if (sc->sc_child == NULL) {
1079 umass_disco(sc);
1080 /* Not an error, just not a complete success. */
1081 USB_ATTACH_SUCCESS_RETURN;
1082 }
1083 #endif
1084
1085 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
1086 USBDEV(sc->sc_dev));
1087
1088 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev)));
1089
1090 USB_ATTACH_SUCCESS_RETURN;
1091 }
1092
1093 Static int
1094 scsipiprint(aux, pnp)
1095 void *aux;
1096 const char *pnp;
1097 {
1098 extern int atapi_print __P((void *aux, const char *pnp));
1099 struct scsipi_link *l = aux;
1100
1101 if (l->type == BUS_SCSI)
1102 return (scsiprint(aux, pnp));
1103 else
1104 return (atapi_print(aux, pnp));
1105 }
1106
1107 USB_DETACH(umass)
1108 {
1109 USB_DETACH_START(umass, sc);
1110 int rv = 0;
1111
1112 DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev)));
1113
1114 /* Abort the pipes to wake up any waiting processes. */
1115 if (sc->bulkout_pipe != NULL)
1116 usbd_abort_pipe(sc->bulkout_pipe);
1117 if (sc->bulkin_pipe != NULL)
1118 usbd_abort_pipe(sc->bulkin_pipe);
1119 if (sc->intrin_pipe != NULL)
1120 usbd_abort_pipe(sc->intrin_pipe);
1121
1122 #if 0
1123 /* Do we really need referebce counting? Perhaps in ioctl() */
1124 s = splusb();
1125 if (--sc->sc_refcnt >= 0) {
1126 /* Wait for processes to go away. */
1127 usb_detach_wait(USBDEV(sc->sc_dev));
1128 }
1129 splx(s);
1130 #endif
1131
1132 #if defined(__FreeBSD__)
1133 if ((sc->proto & PROTO_SCSI) ||
1134 (sc->proto & PROTO_8070) ||
1135 (sc->proto & PROTO_UFI))
1136 /* detach the device from the SCSI host controller (SIM) */
1137 rv = umass_cam_detach(sc);
1138 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1139 if (sc->sc_child != NULL)
1140 rv = config_detach(sc->sc_child, flags);
1141 #endif
1142 if (rv != 0)
1143 return (rv);
1144
1145 umass_disco(sc);
1146
1147 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
1148 USBDEV(sc->sc_dev));
1149
1150 return (0);
1151 }
1152
1153 #if defined(__NetBSD__) || defined(__OpenBSD__)
1154 int
1155 umass_activate(self, act)
1156 struct device *self;
1157 enum devact act;
1158 {
1159 struct umass_softc *sc = (struct umass_softc *) self;
1160 int rv = 0;
1161
1162 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
1163 USBDEVNAME(sc->sc_dev), act));
1164
1165 switch (act) {
1166 case DVACT_ACTIVATE:
1167 rv = EOPNOTSUPP;
1168 break;
1169
1170 case DVACT_DEACTIVATE:
1171 if (sc->sc_child != NULL)
1172 break;
1173 rv = config_deactivate(sc->sc_child);
1174 DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
1175 "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
1176 if (rv == 0)
1177 sc->sc_dying = 1;
1178 break;
1179 }
1180 return (rv);
1181 }
1182 #endif
1183
1184 Static void
1185 umass_disco(sc)
1186 struct umass_softc *sc;
1187 {
1188 int i;
1189
1190 DPRINTF(UDMASS_GEN, ("umass_disco\n"));
1191
1192 /* Free the xfers. */
1193 for (i = 0; i < XFER_NR; i++)
1194 if (sc->transfer_xfer[i] != NULL) {
1195 usbd_free_xfer(sc->transfer_xfer[i]);
1196 sc->transfer_xfer[i] = NULL;
1197 }
1198
1199 /* Remove all the pipes. */
1200 if (sc->bulkout_pipe != NULL)
1201 usbd_close_pipe(sc->bulkout_pipe);
1202 if (sc->bulkin_pipe != NULL)
1203 usbd_close_pipe(sc->bulkin_pipe);
1204 if (sc->intrin_pipe != NULL)
1205 usbd_close_pipe(sc->intrin_pipe);
1206 }
1207
1208 Static void
1209 umass_init_shuttle(struct umass_softc *sc)
1210 {
1211 usb_device_request_t req;
1212 u_char status[2];
1213
1214 /* The Linux driver does this */
1215 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1216 req.bRequest = 1;
1217 USETW(req.wValue, 0);
1218 USETW(req.wIndex, sc->ifaceno);
1219 USETW(req.wLength, sizeof status);
1220 (void)usbd_do_request(sc->sc_udev, &req, &status);
1221 }
1222
1223 /*
1224 * Generic functions to handle transfers
1225 */
1226
1227 Static usbd_status
1228 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
1229 void *buffer, int buflen, int flags,
1230 usbd_xfer_handle xfer)
1231 {
1232 usbd_status err;
1233
1234 if (sc->sc_dying)
1235 return (USBD_IOERROR);
1236
1237 /* Initialiase a USB transfer and then schedule it */
1238
1239 usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen,
1240 flags | sc->sc_xfer_flags, sc->timeout, sc->state);
1241
1242 err = usbd_transfer(xfer);
1243 DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d flags=0x%x "
1244 "timeout=%d\n", USBDEVNAME(sc->sc_dev),
1245 buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout));
1246 if (err && err != USBD_IN_PROGRESS) {
1247 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
1248 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
1249 return (err);
1250 }
1251
1252 return (USBD_NORMAL_COMPLETION);
1253 }
1254
1255
1256 Static usbd_status
1257 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle dev,
1258 usb_device_request_t *req,
1259 void *buffer, int buflen, int flags,
1260 usbd_xfer_handle xfer)
1261 {
1262 usbd_status err;
1263
1264 if (sc->sc_dying)
1265 return (USBD_IOERROR);
1266
1267 /* Initialiase a USB control transfer and then schedule it */
1268
1269 usbd_setup_default_xfer(xfer, dev, (void *) sc,
1270 sc->timeout, req, buffer, buflen, flags, sc->state);
1271
1272 err = usbd_transfer(xfer);
1273 if (err && err != USBD_IN_PROGRESS) {
1274 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
1275 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
1276
1277 /* do not reset, as this would make us loop */
1278 return (err);
1279 }
1280
1281 return (USBD_NORMAL_COMPLETION);
1282 }
1283
1284 Static void
1285 umass_clear_endpoint_stall(struct umass_softc *sc,
1286 u_int8_t endpt, usbd_pipe_handle pipe,
1287 int state, usbd_xfer_handle xfer)
1288 {
1289 usbd_device_handle dev;
1290
1291 if (sc->sc_dying)
1292 return;
1293
1294 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
1295 USBDEVNAME(sc->sc_dev), endpt));
1296
1297 usbd_interface2device_handle(sc->iface, &dev);
1298
1299 sc->transfer_state = state;
1300
1301 usbd_clear_endpoint_toggle(pipe);
1302
1303 sc->request.bmRequestType = UT_WRITE_ENDPOINT;
1304 sc->request.bRequest = UR_CLEAR_FEATURE;
1305 USETW(sc->request.wValue, UF_ENDPOINT_HALT);
1306 USETW(sc->request.wIndex, endpt);
1307 USETW(sc->request.wLength, 0);
1308 umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0, xfer);
1309 }
1310
1311 #if 0
1312 Static void
1313 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
1314 {
1315 sc->transfer_cb = cb;
1316 sc->transfer_priv = priv;
1317
1318 /* The reset is a forced reset, so no error (yet) */
1319 sc->reset(sc, STATUS_CMD_OK);
1320 }
1321 #endif
1322
1323 /*
1324 * Bulk protocol specific functions
1325 */
1326
1327 Static void
1328 umass_bbb_reset(struct umass_softc *sc, int status)
1329 {
1330 usbd_device_handle dev;
1331
1332 KASSERT(sc->proto & PROTO_BBB,
1333 ("sc->proto == 0x%02x wrong for umass_bbb_reset\n", sc->proto));
1334
1335 if (sc->sc_dying)
1336 return;
1337
1338 /*
1339 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1340 *
1341 * For Reset Recovery the host shall issue in the following order:
1342 * a) a Bulk-Only Mass Storage Reset
1343 * b) a Clear Feature HALT to the Bulk-In endpoint
1344 * c) a Clear Feature HALT to the Bulk-Out endpoint
1345 *
1346 * This is done in 3 steps, states:
1347 * TSTATE_BBB_RESET1
1348 * TSTATE_BBB_RESET2
1349 * TSTATE_BBB_RESET3
1350 *
1351 * If the reset doesn't succeed, the device should be port reset.
1352 */
1353
1354 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
1355 USBDEVNAME(sc->sc_dev)));
1356
1357 sc->transfer_state = TSTATE_BBB_RESET1;
1358 sc->transfer_status = status;
1359
1360 usbd_interface2device_handle(sc->iface, &dev);
1361
1362 /* reset is a class specific interface write */
1363 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1364 sc->request.bRequest = UR_BBB_RESET;
1365 USETW(sc->request.wValue, 0);
1366 USETW(sc->request.wIndex, sc->ifaceno);
1367 USETW(sc->request.wLength, 0);
1368 umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0,
1369 sc->transfer_xfer[XFER_BBB_RESET1]);
1370 }
1371
1372 Static void
1373 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
1374 void *data, int datalen, int dir,
1375 transfer_cb_f cb, void *priv)
1376 {
1377 static int dCBWtag = 42; /* unique for CBW of transfer */
1378
1379 DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n",
1380 USBDEVNAME(sc->sc_dev), *(u_char*)cmd));
1381
1382 KASSERT(sc->proto & PROTO_BBB,
1383 ("sc->proto == 0x%02x wrong for umass_bbb_transfer\n",
1384 sc->proto));
1385
1386 /*
1387 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1388 * a data phase of datalen bytes from/to the device and finally a
1389 * csw read phase.
1390 * If the data direction was inbound a maximum of datalen bytes
1391 * is stored in the buffer pointed to by data.
1392 *
1393 * umass_bbb_transfer initialises the transfer and lets the state
1394 * machine in umass_bbb_state handle the completion. It uses the
1395 * following states:
1396 * TSTATE_BBB_COMMAND
1397 * -> TSTATE_BBB_DATA
1398 * -> TSTATE_BBB_STATUS
1399 * -> TSTATE_BBB_STATUS2
1400 * -> TSTATE_BBB_IDLE
1401 *
1402 * An error in any of those states will invoke
1403 * umass_bbb_reset.
1404 */
1405
1406 /* check the given arguments */
1407 KASSERT(datalen == 0 || data != NULL,
1408 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1409 KASSERT(cmdlen <= CBWCDBLENGTH,
1410 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1411 USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH));
1412 KASSERT(dir == DIR_NONE || datalen > 0,
1413 ("%s: datalen == 0 while direction is not NONE\n",
1414 USBDEVNAME(sc->sc_dev)));
1415 KASSERT(datalen == 0 || dir != DIR_NONE,
1416 ("%s: direction is NONE while datalen is not zero\n",
1417 USBDEVNAME(sc->sc_dev)));
1418 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1419 ("%s: CBW struct does not have the right size (%d vs. %d)\n",
1420 USBDEVNAME(sc->sc_dev),
1421 sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1422 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1423 ("%s: CSW struct does not have the right size (%d vs. %d)\n",
1424 USBDEVNAME(sc->sc_dev),
1425 sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1426
1427 /*
1428 * Determine the direction of the data transfer and the length.
1429 *
1430 * dCBWDataTransferLength (datalen) :
1431 * This field indicates the number of bytes of data that the host
1432 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1433 * the Direction bit) during the execution of this command. If this
1434 * field is set to 0, the device will expect that no data will be
1435 * transferred IN or OUT during this command, regardless of the value
1436 * of the Direction bit defined in dCBWFlags.
1437 *
1438 * dCBWFlags (dir) :
1439 * The bits of the Flags field are defined as follows:
1440 * Bits 0-6 reserved
1441 * Bit 7 Direction - this bit shall be ignored if the
1442 * dCBWDataTransferLength field is zero.
1443 * 0 = data Out from host to device
1444 * 1 = data In from device to host
1445 */
1446
1447 /* Fill in the Command Block Wrapper */
1448 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1449 USETDW(sc->cbw.dCBWTag, dCBWtag);
1450 dCBWtag++; /* cannot be done in macro (it will be done 4 times) */
1451 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1452 /* DIR_NONE is treated as DIR_OUT (0x00) */
1453 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1454 sc->cbw.bCBWLUN = lun;
1455 sc->cbw.bCDBLength = cmdlen;
1456 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1457
1458 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1459
1460 /* store the details for the data transfer phase */
1461 sc->transfer_dir = dir;
1462 sc->transfer_data = data;
1463 sc->transfer_datalen = datalen;
1464 sc->transfer_actlen = 0;
1465 sc->transfer_cb = cb;
1466 sc->transfer_priv = priv;
1467 sc->transfer_status = STATUS_CMD_OK;
1468
1469 /* move from idle to the command state */
1470 sc->transfer_state = TSTATE_BBB_COMMAND;
1471
1472 /* Send the CBW from host to device via bulk-out endpoint. */
1473 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1474 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1475 sc->transfer_xfer[XFER_BBB_CBW])) {
1476 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1477 }
1478 }
1479
1480
1481 Static void
1482 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1483 usbd_status err)
1484 {
1485 struct umass_softc *sc = (struct umass_softc *) priv;
1486 usbd_xfer_handle next_xfer;
1487
1488 KASSERT(sc->proto & PROTO_BBB,
1489 ("sc->proto == 0x%02x wrong for umass_bbb_state\n",sc->proto));
1490
1491 if (sc->sc_dying)
1492 return;
1493
1494 /*
1495 * State handling for BBB transfers.
1496 *
1497 * The subroutine is rather long. It steps through the states given in
1498 * Annex A of the Bulk-Only specification.
1499 * Each state first does the error handling of the previous transfer
1500 * and then prepares the next transfer.
1501 * Each transfer is done asynchroneously so after the request/transfer
1502 * has been submitted you will find a 'return;'.
1503 */
1504
1505 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1506 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1507 states[sc->transfer_state], xfer, usbd_errstr(err)));
1508
1509 switch (sc->transfer_state) {
1510
1511 /***** Bulk Transfer *****/
1512 case TSTATE_BBB_COMMAND:
1513 /* Command transport phase, error handling */
1514 if (err) {
1515 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1516 USBDEVNAME(sc->sc_dev)));
1517 /* If the device detects that the CBW is invalid, then
1518 * the device may STALL both bulk endpoints and require
1519 * a Bulk-Reset
1520 */
1521 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1522 return;
1523 }
1524
1525 /* Data transport phase, setup transfer */
1526 sc->transfer_state = TSTATE_BBB_DATA;
1527 if (sc->transfer_dir == DIR_IN) {
1528 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1529 sc->data_buffer, sc->transfer_datalen,
1530 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1531 sc->transfer_xfer[XFER_BBB_DATA]))
1532 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1533
1534 return;
1535 } else if (sc->transfer_dir == DIR_OUT) {
1536 memcpy(sc->data_buffer, sc->transfer_data,
1537 sc->transfer_datalen);
1538 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1539 sc->data_buffer, sc->transfer_datalen,
1540 USBD_NO_COPY,/* fixed length transfer */
1541 sc->transfer_xfer[XFER_BBB_DATA]))
1542 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1543
1544 return;
1545 } else {
1546 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1547 USBDEVNAME(sc->sc_dev)));
1548 }
1549
1550 /* FALLTHROUGH if no data phase, err == 0 */
1551 case TSTATE_BBB_DATA:
1552 /* Command transport phase, error handling (ignored if no data
1553 * phase (fallthrough from previous state)) */
1554 if (sc->transfer_dir != DIR_NONE) {
1555 /* retrieve the length of the transfer that was done */
1556 usbd_get_xfer_status(xfer, NULL, NULL,
1557 &sc->transfer_actlen, NULL);
1558
1559 if (err) {
1560 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
1561 "%s\n", USBDEVNAME(sc->sc_dev),
1562 (sc->transfer_dir == DIR_IN?"in":"out"),
1563 sc->transfer_datalen,usbd_errstr(err)));
1564
1565 if (err == USBD_STALLED) {
1566 umass_clear_endpoint_stall(sc,
1567 (sc->transfer_dir == DIR_IN?
1568 sc->bulkin:sc->bulkout),
1569 (sc->transfer_dir == DIR_IN?
1570 sc->bulkin_pipe:sc->bulkout_pipe),
1571 TSTATE_BBB_DCLEAR,
1572 sc->transfer_xfer[XFER_BBB_DCLEAR]);
1573 return;
1574 } else {
1575 /* Unless the error is a pipe stall the
1576 * error is fatal.
1577 */
1578 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1579 return;
1580 }
1581 }
1582 }
1583
1584 if (sc->transfer_dir == DIR_IN)
1585 memcpy(sc->transfer_data, sc->data_buffer,
1586 sc->transfer_actlen);
1587
1588 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1589 umass_dump_buffer(sc, sc->transfer_data,
1590 sc->transfer_datalen, 48));
1591
1592 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
1593 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1594 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1595 /* Reading of CSW after bulk stall condition in data phase
1596 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1597 * reading CSW (TSTATE_BBB_SCLEAR).
1598 * In the case of no data phase or successfull data phase,
1599 * err == 0 and the following if block is passed.
1600 */
1601 if (err) { /* should not occur */
1602 /* try the transfer below, even if clear stall failed */
1603 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1604 ", %s\n", USBDEVNAME(sc->sc_dev),
1605 (sc->transfer_dir == DIR_IN? "in":"out"),
1606 usbd_errstr(err)));
1607 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1608 return;
1609 }
1610
1611 /* Status transport phase, setup transfer */
1612 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1613 sc->transfer_state == TSTATE_BBB_DATA ||
1614 sc->transfer_state == TSTATE_BBB_DCLEAR) {
1615 /* After no data phase, successfull data phase and
1616 * after clearing bulk-in/-out stall condition
1617 */
1618 sc->transfer_state = TSTATE_BBB_STATUS1;
1619 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1620 } else {
1621 /* After first attempt of fetching CSW */
1622 sc->transfer_state = TSTATE_BBB_STATUS2;
1623 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1624 }
1625
1626 /* Read the Command Status Wrapper via bulk-in endpoint. */
1627 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1628 &sc->csw, UMASS_BBB_CSW_SIZE, 0,
1629 next_xfer)) {
1630 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1631 return;
1632 }
1633
1634 return;
1635 case TSTATE_BBB_STATUS1: /* first attempt */
1636 case TSTATE_BBB_STATUS2: /* second attempt */
1637 /* Status transfer, error handling */
1638 if (err) {
1639 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1640 USBDEVNAME(sc->sc_dev), usbd_errstr(err),
1641 (sc->transfer_state == TSTATE_BBB_STATUS1?
1642 ", retrying":"")));
1643
1644 /* If this was the first attempt at fetching the CSW
1645 * retry it, otherwise fail.
1646 */
1647 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1648 umass_clear_endpoint_stall(sc,
1649 sc->bulkin, sc->bulkin_pipe,
1650 TSTATE_BBB_SCLEAR,
1651 sc->transfer_xfer[XFER_BBB_SCLEAR]);
1652 return;
1653 } else {
1654 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1655 return;
1656 }
1657 }
1658
1659 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1660
1661 /* Check CSW and handle any error */
1662 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1663 /* Invalid CSW: Wrong signature or wrong tag might
1664 * indicate that the device is confused -> reset it.
1665 */
1666 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1667 USBDEVNAME(sc->sc_dev),
1668 UGETDW(sc->csw.dCSWSignature),
1669 CSWSIGNATURE);
1670
1671 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1672 return;
1673 } else if (UGETDW(sc->csw.dCSWTag)
1674 != UGETDW(sc->cbw.dCBWTag)) {
1675 printf("%s: Invalid CSW: tag %d should be %d\n",
1676 USBDEVNAME(sc->sc_dev),
1677 UGETDW(sc->csw.dCSWTag),
1678 UGETDW(sc->cbw.dCBWTag));
1679
1680 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1681 return;
1682
1683 /* CSW is valid here */
1684 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1685 printf("%s: Invalid CSW: status %d > %d\n",
1686 USBDEVNAME(sc->sc_dev),
1687 sc->csw.bCSWStatus,
1688 CSWSTATUS_PHASE);
1689
1690 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1691 return;
1692 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1693 printf("%s: Phase Error, residue = %d\n",
1694 USBDEVNAME(sc->sc_dev),
1695 UGETDW(sc->csw.dCSWDataResidue));
1696
1697 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1698 return;
1699
1700 } else if (sc->transfer_actlen > sc->transfer_datalen) {
1701 /* Buffer overrun! Don't let this go by unnoticed */
1702 panic("%s: transferred %d bytes instead of %d bytes\n",
1703 USBDEVNAME(sc->sc_dev),
1704 sc->transfer_actlen, sc->transfer_datalen);
1705 } else if (sc->transfer_datalen - sc->transfer_actlen
1706 != UGETDW(sc->csw.dCSWDataResidue)) {
1707 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1708 USBDEVNAME(sc->sc_dev),
1709 sc->transfer_datalen - sc->transfer_actlen,
1710 UGETDW(sc->csw.dCSWDataResidue)));
1711
1712 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1713 return;
1714
1715 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1716 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1717 USBDEVNAME(sc->sc_dev),
1718 UGETDW(sc->csw.dCSWDataResidue)));
1719
1720 /* SCSI command failed but transfer was succesful */
1721 sc->transfer_state = TSTATE_IDLE;
1722 sc->transfer_cb(sc, sc->transfer_priv,
1723 UGETDW(sc->csw.dCSWDataResidue),
1724 STATUS_CMD_FAILED);
1725
1726 return;
1727
1728 } else { /* success */
1729 sc->transfer_state = TSTATE_IDLE;
1730 sc->transfer_cb(sc, sc->transfer_priv,
1731 UGETDW(sc->csw.dCSWDataResidue),
1732 STATUS_CMD_OK);
1733
1734 return;
1735 }
1736
1737 /***** Bulk Reset *****/
1738 case TSTATE_BBB_RESET1:
1739 if (err)
1740 printf("%s: BBB reset failed, %s\n",
1741 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1742
1743 umass_clear_endpoint_stall(sc,
1744 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
1745 sc->transfer_xfer[XFER_BBB_RESET2]);
1746
1747 return;
1748 case TSTATE_BBB_RESET2:
1749 if (err) /* should not occur */
1750 printf("%s: BBB bulk-in clear stall failed, %s\n",
1751 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1752 /* no error recovery, otherwise we end up in a loop */
1753
1754 umass_clear_endpoint_stall(sc,
1755 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
1756 sc->transfer_xfer[XFER_BBB_RESET3]);
1757
1758 return;
1759 case TSTATE_BBB_RESET3:
1760 if (err) /* should not occur */
1761 printf("%s: BBB bulk-out clear stall failed, %s\n",
1762 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1763 /* no error recovery, otherwise we end up in a loop */
1764
1765 sc->transfer_state = TSTATE_IDLE;
1766 if (sc->transfer_priv) {
1767 sc->transfer_cb(sc, sc->transfer_priv,
1768 sc->transfer_datalen,
1769 sc->transfer_status);
1770 }
1771
1772 return;
1773
1774 /***** Default *****/
1775 default:
1776 panic("%s: Unknown state %d\n",
1777 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1778 }
1779 }
1780
1781 /*
1782 * Command/Bulk/Interrupt (CBI) specific functions
1783 */
1784
1785 Static int
1786 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1787 usbd_xfer_handle xfer)
1788 {
1789 usbd_device_handle dev;
1790
1791 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1792 ("sc->proto == 0x%02x wrong for umass_cbi_adsc\n",sc->proto));
1793
1794 usbd_interface2device_handle(sc->iface, &dev);
1795
1796 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1797 sc->request.bRequest = UR_CBI_ADSC;
1798 USETW(sc->request.wValue, 0);
1799 USETW(sc->request.wIndex, sc->ifaceno);
1800 USETW(sc->request.wLength, buflen);
1801 return umass_setup_ctrl_transfer(sc, dev, &sc->request, buffer,
1802 buflen, 0, xfer);
1803 }
1804
1805
1806 Static void
1807 umass_cbi_reset(struct umass_softc *sc, int status)
1808 {
1809 int i;
1810 # define SEND_DIAGNOSTIC_CMDLEN 12
1811
1812 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1813 ("sc->proto == 0x%02x wrong for umass_cbi_reset\n",sc->proto));
1814
1815 if (sc->sc_dying)
1816 return;
1817
1818 /*
1819 * Command Block Reset Protocol
1820 *
1821 * First send a reset request to the device. Then clear
1822 * any possibly stalled bulk endpoints.
1823
1824 * This is done in 3 steps, states:
1825 * TSTATE_CBI_RESET1
1826 * TSTATE_CBI_RESET2
1827 * TSTATE_CBI_RESET3
1828 *
1829 * If the reset doesn't succeed, the device should be port reset.
1830 */
1831
1832 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1833 USBDEVNAME(sc->sc_dev)));
1834
1835 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1836 ("%s: CBL struct is too small (%d < %d)\n",
1837 USBDEVNAME(sc->sc_dev),
1838 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1839
1840 sc->transfer_state = TSTATE_CBI_RESET1;
1841 sc->transfer_status = status;
1842
1843 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1844 * the two the last 10 bytes of the cbl is filled with 0xff (section
1845 * 2.2 of the CBI spec).
1846 */
1847 sc->cbl[0] = 0x1d; /* Command Block Reset */
1848 sc->cbl[1] = 0x04;
1849 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1850 sc->cbl[i] = 0xff;
1851
1852 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1853 sc->transfer_xfer[XFER_CBI_RESET1]);
1854 /* XXX if the command fails we should reset the port on the bub */
1855 }
1856
1857 Static void
1858 umass_cbi_transfer(struct umass_softc *sc, int lun,
1859 void *cmd, int cmdlen, void *data, int datalen, int dir,
1860 transfer_cb_f cb, void *priv)
1861 {
1862 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1863 USBDEVNAME(sc->sc_dev), *(u_char*)cmd, datalen));
1864
1865 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1866 ("sc->proto == 0x%02x wrong for umass_cbi_transfer\n",
1867 sc->proto));
1868
1869 if (sc->sc_dying)
1870 return;
1871
1872 /*
1873 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1874 * a data phase of datalen bytes from/to the device and finally a
1875 * csw read phase.
1876 * If the data direction was inbound a maximum of datalen bytes
1877 * is stored in the buffer pointed to by data.
1878 *
1879 * umass_cbi_transfer initialises the transfer and lets the state
1880 * machine in umass_cbi_state handle the completion. It uses the
1881 * following states:
1882 * TSTATE_CBI_COMMAND
1883 * -> XXX fill in
1884 *
1885 * An error in any of those states will invoke
1886 * umass_cbi_reset.
1887 */
1888
1889 /* check the given arguments */
1890 KASSERT(datalen == 0 || data != NULL,
1891 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1892 KASSERT(datalen == 0 || dir != DIR_NONE,
1893 ("%s: direction is NONE while datalen is not zero\n",
1894 USBDEVNAME(sc->sc_dev)));
1895
1896 /* store the details for the data transfer phase */
1897 sc->transfer_dir = dir;
1898 sc->transfer_data = data;
1899 sc->transfer_datalen = datalen;
1900 sc->transfer_actlen = 0;
1901 sc->transfer_cb = cb;
1902 sc->transfer_priv = priv;
1903 sc->transfer_status = STATUS_CMD_OK;
1904
1905 /* move from idle to the command state */
1906 sc->transfer_state = TSTATE_CBI_COMMAND;
1907
1908 /* Send the Command Block from host to device via control endpoint. */
1909 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
1910 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1911 }
1912
1913 Static void
1914 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1915 usbd_status err)
1916 {
1917 struct umass_softc *sc = (struct umass_softc *) priv;
1918
1919 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1920 ("sc->proto == 0x%02x wrong for umass_cbi_state\n", sc->proto));
1921
1922 if (sc->sc_dying)
1923 return;
1924
1925 /*
1926 * State handling for CBI transfers.
1927 */
1928
1929 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1930 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1931 states[sc->transfer_state], xfer, usbd_errstr(err)));
1932
1933 switch (sc->transfer_state) {
1934
1935 /***** CBI Transfer *****/
1936 case TSTATE_CBI_COMMAND:
1937 if (err == USBD_STALLED) {
1938 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1939 USBDEVNAME(sc->sc_dev)));
1940 /* Status transport by control pipe (section 2.3.2.1).
1941 * The command contained in the command block failed.
1942 *
1943 * The control pipe has already been unstalled by the
1944 * USB stack.
1945 * Section 2.4.3.1.1 states that the bulk in endpoints
1946 * should not stalled at this point.
1947 */
1948
1949 sc->transfer_state = TSTATE_IDLE;
1950 sc->transfer_cb(sc, sc->transfer_priv,
1951 sc->transfer_datalen,
1952 STATUS_CMD_FAILED);
1953
1954 return;
1955 } else if (err) {
1956 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1957 USBDEVNAME(sc->sc_dev)));
1958 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1959
1960 return;
1961 }
1962
1963 sc->transfer_state = TSTATE_CBI_DATA;
1964 if (sc->transfer_dir == DIR_IN) {
1965 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1966 sc->transfer_data, sc->transfer_datalen,
1967 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1968 sc->transfer_xfer[XFER_CBI_DATA]))
1969 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1970
1971 } else if (sc->transfer_dir == DIR_OUT) {
1972 memcpy(sc->data_buffer, sc->transfer_data,
1973 sc->transfer_datalen);
1974 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1975 sc->transfer_data, sc->transfer_datalen,
1976 USBD_NO_COPY,/* fixed length transfer */
1977 sc->transfer_xfer[XFER_CBI_DATA]))
1978 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1979
1980 } else if (sc->proto & PROTO_CBI_I) {
1981 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1982 USBDEVNAME(sc->sc_dev)));
1983 sc->transfer_state = TSTATE_CBI_STATUS;
1984 if (umass_setup_transfer(sc, sc->intrin_pipe,
1985 &sc->sbl, sizeof(sc->sbl),
1986 0, /* fixed length transfer */
1987 sc->transfer_xfer[XFER_CBI_STATUS])){
1988 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1989 }
1990 } else {
1991 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1992 USBDEVNAME(sc->sc_dev)));
1993 /* No command completion interrupt. Request
1994 * sense data.
1995 */
1996 sc->transfer_state = TSTATE_IDLE;
1997 sc->transfer_cb(sc, sc->transfer_priv,
1998 0, STATUS_CMD_UNKNOWN);
1999 }
2000
2001 return;
2002
2003 case TSTATE_CBI_DATA:
2004 /* retrieve the length of the transfer that was done */
2005 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
2006 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
2007 USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
2008
2009 if (err) {
2010 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
2011 "%s\n", USBDEVNAME(sc->sc_dev),
2012 (sc->transfer_dir == DIR_IN?"in":"out"),
2013 sc->transfer_datalen,usbd_errstr(err)));
2014
2015 if (err == USBD_STALLED) {
2016 umass_clear_endpoint_stall(sc,
2017 sc->bulkin, sc->bulkin_pipe,
2018 TSTATE_CBI_DCLEAR,
2019 sc->transfer_xfer[XFER_CBI_DCLEAR]);
2020 } else {
2021 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2022 }
2023 return;
2024 }
2025
2026 if (sc->transfer_dir == DIR_IN)
2027 memcpy(sc->transfer_data, sc->data_buffer,
2028 sc->transfer_actlen);
2029
2030 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
2031 umass_dump_buffer(sc, sc->transfer_data,
2032 sc->transfer_actlen, 48));
2033
2034 if (sc->proto & PROTO_CBI_I) {
2035 sc->transfer_state = TSTATE_CBI_STATUS;
2036 memset(&sc->sbl, 0, sizeof(sc->sbl));
2037 if (umass_setup_transfer(sc, sc->intrin_pipe,
2038 &sc->sbl, sizeof(sc->sbl),
2039 0, /* fixed length transfer */
2040 sc->transfer_xfer[XFER_CBI_STATUS])){
2041 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2042 }
2043 } else {
2044 /* No command completion interrupt. Request
2045 * sense to get status of command.
2046 */
2047 sc->transfer_state = TSTATE_IDLE;
2048 sc->transfer_cb(sc, sc->transfer_priv,
2049 sc->transfer_datalen - sc->transfer_actlen,
2050 STATUS_CMD_UNKNOWN);
2051 }
2052 return;
2053
2054 case TSTATE_CBI_STATUS:
2055 if (err) {
2056 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
2057 USBDEVNAME(sc->sc_dev)));
2058 /* Status transport by interrupt pipe (section 2.3.2.2).
2059 */
2060
2061 if (err == USBD_STALLED) {
2062 umass_clear_endpoint_stall(sc,
2063 sc->intrin, sc->intrin_pipe,
2064 TSTATE_CBI_SCLEAR,
2065 sc->transfer_xfer[XFER_CBI_SCLEAR]);
2066 } else {
2067 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2068 }
2069 return;
2070 }
2071
2072 /* Dissect the information in the buffer */
2073
2074 if (sc->proto & PROTO_UFI) {
2075 int status;
2076
2077 /* Section 3.4.3.1.3 specifies that the UFI command
2078 * protocol returns an ASC and ASCQ in the interrupt
2079 * data block.
2080 */
2081
2082 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
2083 "ASCQ = 0x%02x\n",
2084 USBDEVNAME(sc->sc_dev),
2085 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
2086
2087 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
2088 status = STATUS_CMD_OK;
2089 else
2090 status = STATUS_CMD_FAILED;
2091
2092 /* No sense, command successfull */
2093 } else {
2094 /* Command Interrupt Data Block */
2095 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
2096 USBDEVNAME(sc->sc_dev),
2097 sc->sbl.common.type, sc->sbl.common.value));
2098
2099 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2100 int err;
2101
2102 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
2103 == IDB_VALUE_PASS) {
2104 err = STATUS_CMD_OK;
2105 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2106 == IDB_VALUE_FAIL ||
2107 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
2108 == IDB_VALUE_PERSISTENT) {
2109 err = STATUS_CMD_FAILED;
2110 } else {
2111 err = STATUS_WIRE_FAILED;
2112 }
2113
2114 sc->transfer_state = TSTATE_IDLE;
2115 sc->transfer_cb(sc, sc->transfer_priv,
2116 sc->transfer_datalen,
2117 err);
2118 }
2119 }
2120 return;
2121
2122 case TSTATE_CBI_DCLEAR:
2123 if (err) { /* should not occur */
2124 printf("%s: CBI bulk-in/out stall clear failed, %s\n",
2125 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2126 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
2127 }
2128
2129 sc->transfer_state = TSTATE_IDLE;
2130 sc->transfer_cb(sc, sc->transfer_priv,
2131 sc->transfer_datalen,
2132 STATUS_CMD_FAILED);
2133 return;
2134
2135 case TSTATE_CBI_SCLEAR:
2136 if (err) /* should not occur */
2137 printf("%s: CBI intr-in stall clear failed, %s\n",
2138 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2139
2140 /* Something really bad is going on. Reset the device */
2141 umass_cbi_reset(sc, STATUS_CMD_FAILED);
2142 return;
2143
2144 /***** CBI Reset *****/
2145 case TSTATE_CBI_RESET1:
2146 if (err)
2147 printf("%s: CBI reset failed, %s\n",
2148 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2149
2150 umass_clear_endpoint_stall(sc,
2151 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
2152 sc->transfer_xfer[XFER_CBI_RESET2]);
2153
2154 return;
2155 case TSTATE_CBI_RESET2:
2156 if (err) /* should not occur */
2157 printf("%s: CBI bulk-in stall clear failed, %s\n",
2158 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2159 /* no error recovery, otherwise we end up in a loop */
2160
2161 umass_clear_endpoint_stall(sc,
2162 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
2163 sc->transfer_xfer[XFER_CBI_RESET3]);
2164
2165 return;
2166 case TSTATE_CBI_RESET3:
2167 if (err) /* should not occur */
2168 printf("%s: CBI bulk-out stall clear failed, %s\n",
2169 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2170 /* no error recovery, otherwise we end up in a loop */
2171
2172 sc->transfer_state = TSTATE_IDLE;
2173 if (sc->transfer_priv) {
2174 sc->transfer_cb(sc, sc->transfer_priv,
2175 sc->transfer_datalen,
2176 sc->transfer_status);
2177 }
2178
2179 return;
2180
2181
2182 /***** Default *****/
2183 default:
2184 panic("%s: Unknown state %d\n",
2185 USBDEVNAME(sc->sc_dev), sc->transfer_state);
2186 }
2187 }
2188
2189 usbd_status
2190 umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
2191 {
2192 usbd_device_handle dev;
2193 usb_device_request_t req;
2194 usbd_status err;
2195 usb_interface_descriptor_t *id;
2196
2197 *maxlun = 0; /* Default to 0. */
2198
2199 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
2200
2201 usbd_interface2device_handle(sc->iface, &dev);
2202 id = usbd_get_interface_descriptor(sc->iface);
2203
2204 /* The Get Max Lun command is a class-specific request. */
2205 req.bmRequestType = UT_READ_CLASS_INTERFACE;
2206 req.bRequest = UR_BBB_GET_MAX_LUN;
2207 USETW(req.wValue, 0);
2208 USETW(req.wIndex, id->bInterfaceNumber);
2209 USETW(req.wLength, 1);
2210
2211 err = usbd_do_request(dev, &req, maxlun);
2212 switch (err) {
2213 case USBD_NORMAL_COMPLETION:
2214 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
2215 USBDEVNAME(sc->sc_dev), *maxlun));
2216 break;
2217
2218 case USBD_STALLED:
2219 /*
2220 * Device doesn't support Get Max Lun request.
2221 */
2222 err = USBD_NORMAL_COMPLETION;
2223 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
2224 USBDEVNAME(sc->sc_dev)));
2225 break;
2226
2227 case USBD_SHORT_XFER:
2228 /*
2229 * XXX This must mean Get Max Lun is not supported, too!
2230 */
2231 err = USBD_NORMAL_COMPLETION;
2232 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
2233 USBDEVNAME(sc->sc_dev)));
2234 break;
2235
2236 default:
2237 printf("%s: Get Max Lun failed: %s\n",
2238 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
2239 /* XXX Should we port_reset the device? */
2240 break;
2241 }
2242
2243 return (err);
2244 }
2245
2246
2247
2248 #if defined(__FreeBSD__)
2249 /*
2250 * CAM specific functions (used by SCSI, UFI, 8070)
2251 */
2252
2253 Static int
2254 umass_cam_attach_sim()
2255 {
2256 struct cam_devq *devq; /* Per device Queue */
2257
2258 /* A HBA is attached to the CAM layer.
2259 *
2260 * The CAM layer will then after a while start probing for
2261 * devices on the bus. The number of devices is limitted to one.
2262 */
2263
2264 /* SCSI transparent command set */
2265
2266 devq = cam_simq_alloc(1 /*maximum openings*/);
2267 if (devq == NULL)
2268 return(ENOMEM);
2269
2270 umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll, DEVNAME,
2271 NULL /*priv*/, 0 /*unit number*/,
2272 1 /*maximum device openings*/,
2273 0 /*maximum tagged device openings*/,
2274 devq);
2275 if (umass_sim == NULL) {
2276 cam_simq_free(devq);
2277 return(ENOMEM);
2278 }
2279
2280 if(xpt_bus_register(umass_sim, 0) != CAM_SUCCESS)
2281 return(ENOMEM);
2282
2283 if (xpt_create_path(&umass_path, NULL, cam_sim_path(umass_sim),
2284 UMASS_SCSIID_HOST, 0)
2285 != CAM_REQ_CMP)
2286 return(ENOMEM);
2287
2288 return(0);
2289 }
2290
2291 #ifdef UMASS_DO_CAM_RESCAN
2292 /* this function is only used from umass_cam_rescan, so mention
2293 * prototype down here.
2294 */
2295 Static void umass_cam_rescan_callback(struct cam_periph *periph,union ccb *ccb);
2296
2297 Static void
2298 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2299 {
2300 #ifdef UMASS_DEBUG
2301 struct umass_softc *sc = devclass_get_softc(umass_devclass,
2302 ccb->ccb_h.target_id);
2303
2304 if (ccb->ccb_h.status != CAM_REQ_CMP) {
2305 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan failed, 0x%04x\n",
2306 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2307 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2308 ccb->ccb_h.status));
2309 } else {
2310 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan succeeded, freeing resources.\n",
2311 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2312 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2313 }
2314 #endif
2315
2316 xpt_free_path(ccb->ccb_h.path);
2317 free(ccb, M_USBDEV);
2318 }
2319
2320 Static void
2321 umass_cam_rescan(struct umass_softc *sc)
2322 {
2323 struct cam_path *path;
2324 union ccb *ccb = malloc(sizeof(union ccb), M_USBDEV, M_WAITOK);
2325
2326 memset(ccb, 0, sizeof(union ccb));
2327
2328 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: scanning bus for new device %d\n",
2329 USBDEVNAME(sc->sc_dev), cam_sim_path(umass_sim),
2330 device_get_unit(sc->sc_dev), 0,
2331 device_get_unit(sc->sc_dev)));
2332
2333 if (xpt_create_path(&path, xpt_periph, cam_sim_path(umass_sim),
2334 device_get_unit(sc->sc_dev), 0)
2335 != CAM_REQ_CMP)
2336 return;
2337
2338 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2339 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2340 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
2341 ccb->crcn.flags = CAM_FLAG_NONE;
2342 xpt_action(ccb);
2343
2344 /* The scan is in progress now. */
2345 }
2346 #endif
2347
2348 Static int
2349 umass_cam_attach(struct umass_softc *sc)
2350 {
2351 /* SIM already attached at module load. The device is a target on the
2352 * one SIM we registered: target device_get_unit(self).
2353 */
2354
2355 /* The artificial limit UMASS_SCSIID_MAX is there because CAM expects
2356 * a limit to the number of targets that are present on a SIM.
2357 */
2358 if (device_get_unit(sc->sc_dev) > UMASS_SCSIID_MAX) {
2359 printf("%s: Increase UMASS_SCSIID_MAX (currently %d) in %s "
2360 "and try again.\n", USBDEVNAME(sc->sc_dev),
2361 UMASS_SCSIID_MAX, __FILE__);
2362 return(1);
2363 }
2364
2365 #ifdef UMASS_DO_CAM_RESCAN
2366 if (!cold) {
2367 /* Notify CAM of the new device. Any failure is benign, as the
2368 * user can still do it by hand (camcontrol rescan <busno>).
2369 * Only do this if we are not booting, because CAM does a scan
2370 * after booting has completed, when interrupts have been
2371 * enabled.
2372 */
2373 umass_cam_rescan(sc);
2374 }
2375 #endif
2376
2377 return(0); /* always succesful */
2378 }
2379
2380 /* umass_cam_detach
2381 * detach from the CAM layer
2382 */
2383
2384 Static int
2385 umass_cam_detach_sim()
2386 {
2387 if (umass_sim)
2388 return(EBUSY); /* XXX CAM can't handle disappearing SIMs yet */
2389
2390 if (umass_path) {
2391 /* XXX do we need to send an asynchroneous event for the SIM?
2392 xpt_async(AC_LOST_DEVICE, umass_path, NULL);
2393 */
2394 xpt_free_path(umass_path);
2395 umass_path = NULL;
2396 }
2397
2398 if (umass_sim) {
2399 if (xpt_bus_deregister(cam_sim_path(umass_sim)))
2400 cam_sim_free(umass_sim, /*free_devq*/TRUE);
2401 else
2402 return(EBUSY);
2403
2404 umass_sim = NULL;
2405 }
2406
2407 return(0);
2408 }
2409
2410 Static int
2411 umass_cam_detach(struct umass_softc *sc)
2412 {
2413 struct cam_path *path;
2414
2415 /* detach of sim not done until module unload */
2416 DPRINTF(UDMASS_SCSI, ("%s: losing CAM device entry\n",
2417 USBDEVNAME(sc->sc_dev)));
2418
2419 if (xpt_create_path(&path, NULL, cam_sim_path(umass_sim),
2420 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD)
2421 != CAM_REQ_CMP)
2422 return(ENOMEM);
2423 xpt_async(AC_LOST_DEVICE, path, NULL);
2424 xpt_free_path(path);
2425
2426 return(0);
2427 }
2428
2429
2430
2431 /* umass_cam_action
2432 * CAM requests for action come through here
2433 */
2434
2435 Static void
2436 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2437 {
2438 struct umass_softc *sc = devclass_get_softc(umass_devclass,
2439 ccb->ccb_h.target_id);
2440
2441 /* The softc is still there, but marked as going away. umass_cam_detach
2442 * has not yet notified CAM of the lost device however.
2443 */
2444 if (sc && sc->sc_dying) {
2445 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2446 "Invalid target (gone)\n",
2447 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2448 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2449 ccb->ccb_h.func_code));
2450 ccb->ccb_h.status = CAM_TID_INVALID;
2451 xpt_done(ccb);
2452 return;
2453 }
2454
2455 /* Verify, depending on the operation to perform, that we either got a
2456 * valid sc, because an existing target was referenced, or otherwise
2457 * the SIM is addressed.
2458 *
2459 * This avoids bombing out at a printf and does give the CAM layer some
2460 * sensible feedback on errors.
2461 */
2462 switch (ccb->ccb_h.func_code) {
2463 case XPT_SCSI_IO:
2464 case XPT_RESET_DEV:
2465 case XPT_GET_TRAN_SETTINGS:
2466 case XPT_SET_TRAN_SETTINGS:
2467 case XPT_CALC_GEOMETRY:
2468 /* the opcodes requiring a target. These should never occur. */
2469 if (sc == NULL) {
2470 printf("%s:%d:%d:%d:func_code 0x%04x: "
2471 "Invalid target\n",
2472 DEVNAME_SIM, UMASS_SCSI_BUS,
2473 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2474 ccb->ccb_h.func_code);
2475
2476 ccb->ccb_h.status = CAM_TID_INVALID;
2477 xpt_done(ccb);
2478 return;
2479 }
2480 break;
2481 case XPT_PATH_INQ:
2482 case XPT_NOOP:
2483 /* The opcodes sometimes aimed at a target (sc is valid),
2484 * sometimes aimed at the SIM (sc is invalid and target is
2485 * CAM_TARGET_WILDCARD)
2486 */
2487 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2488 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2489 "Invalid target\n",
2490 DEVNAME_SIM, UMASS_SCSI_BUS,
2491 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2492 ccb->ccb_h.func_code));
2493
2494 ccb->ccb_h.status = CAM_TID_INVALID;
2495 xpt_done(ccb);
2496 return;
2497 }
2498 break;
2499 default:
2500 /* XXX Hm, we should check the input parameters */
2501 }
2502
2503 /* Perform the requested action */
2504 switch (ccb->ccb_h.func_code) {
2505 case XPT_SCSI_IO:
2506 {
2507 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2508 int dir;
2509 unsigned char *cmd;
2510 int cmdlen;
2511
2512 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2513 "cmd: 0x%02x, flags: 0x%02x, "
2514 "%db cmd/%db data/%db sense\n",
2515 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2516 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2517 csio->cdb_io.cdb_bytes[0],
2518 ccb->ccb_h.flags & CAM_DIR_MASK,
2519 csio->cdb_len, csio->dxfer_len,
2520 csio->sense_len));
2521
2522 /* clear the end of the buffer to make sure we don't send out
2523 * garbage.
2524 */
2525 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
2526 == CAM_DIR_OUT)
2527 umass_dump_buffer(sc, csio->data_ptr,
2528 csio->dxfer_len, 48));
2529
2530 if (sc->transfer_state != TSTATE_IDLE) {
2531 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
2532 "I/O requested while busy (state %d, %s)\n",
2533 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2534 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2535 sc->transfer_state,states[sc->transfer_state]));
2536 ccb->ccb_h.status = CAM_SCSI_BUSY;
2537 xpt_done(ccb);
2538 return;
2539 }
2540
2541 switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
2542 case CAM_DIR_IN:
2543 dir = DIR_IN;
2544 break;
2545 case CAM_DIR_OUT:
2546 dir = DIR_OUT;
2547 break;
2548 default:
2549 dir = DIR_NONE;
2550 }
2551
2552 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2553 if (sc->transform(sc, csio->cdb_io.cdb_bytes, csio->cdb_len,
2554 &cmd, &cmdlen)) {
2555 sc->transfer(sc, ccb->ccb_h.target_lun, cmd, cmdlen,
2556 csio->data_ptr,
2557 csio->dxfer_len, dir,
2558 umass_cam_cb, (void *) ccb);
2559 } else {
2560 ccb->ccb_h.status = CAM_REQ_INVALID;
2561 xpt_done(ccb);
2562 }
2563
2564 break;
2565 }
2566 case XPT_PATH_INQ:
2567 {
2568 struct ccb_pathinq *cpi = &ccb->cpi;
2569
2570 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
2571 (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
2572 UMASS_SCSI_BUS,
2573 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2574
2575 /* host specific information */
2576 cpi->version_num = 1;
2577 cpi->hba_inquiry = 0;
2578 cpi->target_sprt = 0;
2579 cpi->hba_misc = 0;
2580 cpi->hba_eng_cnt = 0;
2581 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2582 cpi->max_lun = 0; /* no LUN's supported */
2583 cpi->initiator_id = UMASS_SCSIID_HOST;
2584 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2585 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2586 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2587 cpi->unit_number = cam_sim_unit(sim);
2588 cpi->bus_id = UMASS_SCSI_BUS;
2589 if (sc) {
2590 cpi->base_transfer_speed = sc->transfer_speed;
2591 cpi->max_lun = sc->maxlun;
2592 }
2593
2594 cpi->ccb_h.status = CAM_REQ_CMP;
2595 xpt_done(ccb);
2596 break;
2597 }
2598 case XPT_RESET_DEV:
2599 {
2600 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
2601 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2602 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2603
2604 ccb->ccb_h.status = CAM_REQ_INPROG;
2605 umass_reset(sc, umass_cam_cb, (void *) ccb);
2606 break;
2607 }
2608 case XPT_GET_TRAN_SETTINGS:
2609 {
2610 struct ccb_trans_settings *cts = &ccb->cts;
2611
2612 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2613 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2614 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2615
2616 cts->valid = 0;
2617 cts->flags = 0; /* no disconnection, tagging */
2618
2619 ccb->ccb_h.status = CAM_REQ_CMP;
2620 xpt_done(ccb);
2621 break;
2622 }
2623 case XPT_SET_TRAN_SETTINGS:
2624 {
2625 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2626 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2627 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2628
2629 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2630 xpt_done(ccb);
2631 break;
2632 }
2633 case XPT_CALC_GEOMETRY:
2634 {
2635 struct ccb_calc_geometry *ccg = &ccb->ccg;
2636
2637 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2638 "Volume size = %d\n",
2639 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
2640 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2641 ccg->volume_size));
2642
2643 /* XXX We should probably ask the drive for the details
2644 * instead of cluching them up ourselves
2645 */
2646 if (sc->drive == ZIP_100) {
2647 ccg->heads = 64;
2648 ccg->secs_per_track = 32;
2649 ccg->cylinders = ccg->volume_size / ccg->heads
2650 / ccg->secs_per_track;
2651 ccb->ccb_h.status = CAM_REQ_CMP;
2652 break;
2653 } else if (sc->proto & PROTO_UFI) {
2654 ccg->heads = 2;
2655 if (ccg->volume_size == 2880)
2656 ccg->secs_per_track = 18;
2657 else
2658 ccg->secs_per_track = 9;
2659 ccg->cylinders = 80;
2660 break;
2661 } else {
2662 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2663 }
2664
2665 xpt_done(ccb);
2666 break;
2667 }
2668 case XPT_NOOP:
2669 {
2670 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
2671 (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
2672 UMASS_SCSI_BUS,
2673 ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
2674
2675 ccb->ccb_h.status = CAM_REQ_CMP;
2676 xpt_done(ccb);
2677 break;
2678 }
2679 default:
2680 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
2681 "Not implemented\n",
2682 (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
2683 UMASS_SCSI_BUS,
2684 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2685 ccb->ccb_h.func_code));
2686
2687 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2688 xpt_done(ccb);
2689 break;
2690 }
2691 }
2692
2693 /* umass_cam_poll
2694 * all requests are handled through umass_cam_action, requests
2695 * are never pending. So, nothing to do here.
2696 */
2697 Static void
2698 umass_cam_poll(struct cam_sim *sim)
2699 {
2700 #ifdef UMASS_DEBUG
2701 struct umass_softc *sc = (struct umass_softc *) sim->softc;
2702
2703 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
2704 USBDEVNAME(sc->sc_dev)));
2705 #endif
2706
2707 /* nop */
2708 }
2709
2710
2711 /* umass_cam_cb
2712 * finalise a completed CAM command
2713 */
2714
2715 Static void
2716 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
2717 {
2718 union ccb *ccb = (union ccb *) priv;
2719 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2720
2721 csio->resid = residue;
2722
2723 switch (status) {
2724 case STATUS_CMD_OK:
2725 ccb->ccb_h.status = CAM_REQ_CMP;
2726 xpt_done(ccb);
2727 break;
2728
2729 case STATUS_CMD_UNKNOWN:
2730 case STATUS_CMD_FAILED:
2731 switch (ccb->ccb_h.func_code) {
2732 case XPT_SCSI_IO:
2733 {
2734 unsigned char *cmd;
2735 int cmdlen;
2736
2737 /* fetch sense data */
2738 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
2739 USBDEVNAME(sc->sc_dev),
2740 sc->cam_scsi_sense.length));
2741
2742 sc->cam_scsi_sense.length = csio->sense_len;
2743
2744 if (sc->transform(sc, (char *) &sc->cam_scsi_sense,
2745 sizeof(sc->cam_scsi_sense),
2746 &cmd, &cmdlen)) {
2747 sc->transfer(sc, ccb->ccb_h.target_lun,
2748 cmd, cmdlen,
2749 &csio->sense_data,
2750 csio->sense_len, DIR_IN,
2751 umass_cam_sense_cb, (void *) ccb);
2752 } else {
2753 #ifdef UMASS_DEBUG
2754 panic("transform(REQUEST_SENSE) failed\n");
2755 #else
2756 csio->resid = sc->transfer_datalen;
2757 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2758 xpt_done(ccb);
2759 #endif
2760 }
2761 break;
2762 }
2763 case XPT_RESET_DEV: /* Reset failed */
2764 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2765 xpt_done(ccb);
2766 break;
2767 default:
2768 panic("umass_cam_cb called for func_code %d\n",
2769 ccb->ccb_h.func_code);
2770 }
2771 break;
2772
2773 case STATUS_WIRE_FAILED:
2774 /* the wire protocol failed and will have recovered
2775 * (hopefully). We return an error to CAM and let CAM retry
2776 * the command if necessary.
2777 */
2778 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2779 xpt_done(ccb);
2780 break;
2781
2782 default:
2783 panic("%s: Unknown status %d in umass_cam_cb\n",
2784 USBDEVNAME(sc->sc_dev), status);
2785 }
2786 }
2787
2788 /* Finalise a completed autosense operation
2789 */
2790 Static void
2791 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
2792 {
2793 union ccb *ccb = (union ccb *) priv;
2794 struct ccb_scsiio *csio = &ccb->csio; /* deref union */
2795
2796 switch (status) {
2797 case STATUS_CMD_OK:
2798 case STATUS_CMD_UNKNOWN:
2799 /* Getting sense data succeeded. The length of the sense data
2800 * is not returned in any way. The sense data itself contains
2801 * the length of the sense data that is valid.
2802 */
2803 if (sc->quirks & RS_NO_CLEAR_UA
2804 && csio->cdb_io.cdb_bytes[0] == INQUIRY
2805 && (csio->sense_data.flags & SSD_KEY)
2806 == SSD_KEY_UNIT_ATTENTION) {
2807 /* Ignore unit attention errors in the case where
2808 * the Unit Attention state is not cleared on
2809 * REQUEST SENSE. They will appear again at the next
2810 * command.
2811 */
2812 ccb->ccb_h.status = CAM_REQ_CMP;
2813 } else if ((csio->sense_data.flags & SSD_KEY)
2814 == SSD_KEY_NO_SENSE) {
2815 /* No problem after all (in the case of CBI without
2816 * CCI)
2817 */
2818 ccb->ccb_h.status = CAM_REQ_CMP;
2819 } else {
2820 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2821 | CAM_AUTOSNS_VALID;
2822 csio->scsi_status = SCSI_STATUS_CHECK_COND;
2823 }
2824 xpt_done(ccb);
2825 break;
2826
2827 default:
2828 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
2829 USBDEVNAME(sc->sc_dev), status));
2830 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
2831 xpt_done(ccb);
2832 }
2833 }
2834
2835
2836 Static int
2837 umass_driver_load(module_t mod, int what, void *arg)
2838 {
2839 int err;
2840
2841 switch (what) {
2842 case MOD_UNLOAD:
2843 err = umass_cam_detach_sim();
2844 if (err)
2845 return(err);
2846 return(usbd_driver_load(mod, what, arg));
2847 case MOD_LOAD:
2848 /* We don't attach to CAM at this point, because it will try
2849 * and malloc memory for it. This is not possible when the
2850 * boot loader loads umass as a module before the kernel
2851 * has been bootstrapped.
2852 */
2853 default:
2854 return(usbd_driver_load(mod, what, arg));
2855 }
2856 }
2857
2858
2859
2860 /* (even the comment is missing) */
2861
2862 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0);
2863
2864
2865 /*
2866 * SCSI specific functions
2867 */
2868
2869 Static int
2870 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2871 unsigned char **rcmd, int *rcmdlen)
2872 {
2873 *rcmd = cmd; /* trivial copy */
2874 *rcmdlen = cmdlen;
2875
2876 switch (cmd[0]) {
2877 case TEST_UNIT_READY:
2878 if (sc->quirks & NO_TEST_UNIT_READY) {
2879 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
2880 "to START_UNIT\n", USBDEVNAME(sc->sc_dev)));
2881 cmd[0] = START_STOP_UNIT;
2882 cmd[4] = SSS_START;
2883 }
2884 break;
2885 }
2886
2887 return 1; /* success */
2888 }
2889
2890 /*
2891 * UFI specific functions
2892 */
2893
2894 Static int
2895 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2896 unsigned char **rcmd, int *rcmdlen)
2897 {
2898 *rcmd = cmd;
2899 /* A UFI command is always 12 bytes in length */
2900 /* XXX cmd[(cmdlen+1)..12] contains garbage */
2901 *rcmdlen = 12;
2902
2903 switch (cmd[0]) {
2904 case TEST_UNIT_READY:
2905 if (sc->quirks & NO_TEST_UNIT_READY) {
2906 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
2907 "to START_UNIT\n", USBDEVNAME(sc->sc_dev)));
2908 cmd[0] = START_STOP_UNIT;
2909 cmd[4] = SSS_START;
2910 }
2911 return 1;
2912 case INQUIRY:
2913 case START_STOP_UNIT:
2914 case MODE_SENSE:
2915 case PREVENT_ALLOW:
2916 case READ_10:
2917 case READ_12:
2918 case READ_CAPACITY:
2919 case REQUEST_SENSE:
2920 case REZERO_UNIT:
2921 case POSITION_TO_ELEMENT: /* SEEK_10 */
2922 case SEND_DIAGNOSTIC:
2923 case WRITE_10:
2924 case WRITE_12:
2925 /* FORMAT_UNIT */
2926 /* MODE_SELECT */
2927 /* READ_FORMAT_CAPACITY */
2928 /* VERIFY */
2929 /* WRITE_AND_VERIFY */
2930 return 1; /* success */
2931 default:
2932 return 0; /* success */
2933 }
2934 }
2935
2936 /*
2937 * 8070 specific functions
2938 */
2939 Static int
2940 umass_8070_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
2941 unsigned char **rcmd, int *rcmdlen)
2942 {
2943 return 0; /* failure */
2944 }
2945
2946 #endif /* __FreeBSD__ */
2947
2948
2949 #ifdef UMASS_DEBUG
2950 Static void
2951 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
2952 {
2953 int clen = cbw->bCDBLength;
2954 int dlen = UGETDW(cbw->dCBWDataTransferLength);
2955 u_int8_t *c = cbw->CBWCDB;
2956 int tag = UGETDW(cbw->dCBWTag);
2957 int flags = cbw->bCBWFlags;
2958
2959 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
2960 "(0x%02x%02x%02x%02x%02x%02x%s), "
2961 "data = %d bytes, dir = %s\n",
2962 USBDEVNAME(sc->sc_dev), tag, clen,
2963 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
2964 dlen, (flags == CBWFLAGS_IN? "in":
2965 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
2966 }
2967
2968 Static void
2969 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
2970 {
2971 int sig = UGETDW(csw->dCSWSignature);
2972 int tag = UGETW(csw->dCSWTag);
2973 int res = UGETDW(csw->dCSWDataResidue);
2974 int status = csw->bCSWStatus;
2975
2976 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
2977 "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
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
2985 Static void
2986 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
2987 int printlen)
2988 {
2989 int i, j;
2990 char s1[40];
2991 char s2[40];
2992 char s3[5];
2993
2994 s1[0] = '\0';
2995 s3[0] = '\0';
2996
2997 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
2998 for (i = 0; i < buflen && i < printlen; i++) {
2999 j = i % 16;
3000 if (j == 0 && i != 0) {
3001 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
3002 USBDEVNAME(sc->sc_dev), s1, s2));
3003 s2[0] = '\0';
3004 }
3005 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
3006 }
3007 if (buflen > printlen)
3008 sprintf(s3, " ...");
3009 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
3010 USBDEVNAME(sc->sc_dev), s1, s2, s3));
3011 }
3012 #endif
3013
3014
3015
3016
3017
3018
3019
3020
3021 #if defined(__NetBSD__) || defined(__OpenBSD__)
3022 Static int
3023 umass_scsipi_cmd(xs)
3024 struct scsipi_xfer *xs;
3025 {
3026 struct scsipi_link *sc_link = xs->sc_link;
3027 struct umass_softc *sc = sc_link->adapter_softc;
3028 int dir;
3029
3030 DIF(UDMASS_UPPER, sc_link->flags |= DEBUGLEVEL);
3031
3032 DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: %d:%d xs=%p cmd=0x%02x "
3033 "(quirks=0x%x, poll=%d)\n", USBDEVNAME(sc->sc_dev),
3034 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun,
3035 xs, xs->cmd->opcode, sc_link->quirks,
3036 xs->xs_control & XS_CTL_POLL));
3037 #if defined(USB_DEBUG) && defined(SCSIDEBUG)
3038 if (umassdebug & UDMASS_SCSI)
3039 show_scsipi_xs(xs);
3040 else if (umassdebug & ~UDMASS_CMD)
3041 show_scsipi_cmd(xs);
3042 #endif
3043
3044 if (sc->sc_dying) {
3045 xs->error = XS_DRIVER_STUFFUP;
3046 goto done;
3047 }
3048
3049 #ifdef UMASS_DEBUG
3050 if ((sc_link->type == BUS_ATAPI ?
3051 sc_link->scsipi_atapi.drive : sc_link->scsipi_scsi.target)
3052 != UMASS_SCSIID_DEVICE) {
3053 DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
3054 USBDEVNAME(sc->sc_dev),
3055 sc_link->scsipi_scsi.target));
3056 xs->error = XS_DRIVER_STUFFUP;
3057 goto done;
3058 }
3059 #endif
3060
3061 if (xs->cmd->opcode == SCSI_MODE_SENSE &&
3062 (sc_link->quirks & SDEV_NOMODESENSE)) {
3063 /*printf("%s: SCSI_MODE_SENSE\n", USBDEVNAME(sc->sc_dev));*/
3064 xs->error = XS_TIMEOUT;
3065 goto done;
3066 }
3067
3068 if (xs->cmd->opcode == START_STOP &&
3069 (sc->quirks & NO_START_STOP)) {
3070 /*printf("%s: START_STOP\n", USBDEVNAME(sc->sc_dev));*/
3071 xs->error = XS_NOERROR;
3072 goto done;
3073 }
3074
3075 dir = DIR_NONE;
3076 if (xs->datalen) {
3077 switch (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
3078 case XS_CTL_DATA_IN:
3079 dir = DIR_IN;
3080 break;
3081 case XS_CTL_DATA_OUT:
3082 dir = DIR_OUT;
3083 break;
3084 }
3085 }
3086
3087 if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
3088 printf("umass_cmd: large datalen, %d\n", xs->datalen);
3089 xs->error = XS_DRIVER_STUFFUP;
3090 goto done;
3091 }
3092
3093 if (xs->xs_control & XS_CTL_POLL) {
3094 /* Use sync transfer. XXX Broken! */
3095 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: sync dir=%d\n", dir));
3096 sc->sc_xfer_flags = USBD_SYNCHRONOUS;
3097 sc->sc_sync_status = USBD_INVAL;
3098 sc->transfer(sc, sc_link->scsipi_scsi.lun, xs->cmd, xs->cmdlen,
3099 xs->data, xs->datalen, dir, 0, xs);
3100 sc->sc_xfer_flags = 0;
3101 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
3102 sc->sc_sync_status));
3103 switch (sc->sc_sync_status) {
3104 case USBD_NORMAL_COMPLETION:
3105 xs->error = XS_NOERROR;
3106 break;
3107 case USBD_TIMEOUT:
3108 xs->error = XS_TIMEOUT;
3109 break;
3110 default:
3111 xs->error = XS_DRIVER_STUFFUP;
3112 break;
3113 }
3114 goto done;
3115 } else {
3116 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
3117 " datalen=%d\n",
3118 dir, xs->cmdlen, xs->datalen));
3119 sc->transfer(sc, sc_link->scsipi_scsi.lun, xs->cmd, xs->cmdlen,
3120 xs->data, xs->datalen, dir, umass_scsipi_cb, xs);
3121 return (SUCCESSFULLY_QUEUED);
3122 }
3123
3124 /* Return if command finishes early. */
3125 done:
3126 xs->xs_status |= XS_STS_DONE;
3127 scsipi_done(xs);
3128 if (xs->xs_control & XS_CTL_POLL)
3129 return (COMPLETE);
3130 else
3131 return (SUCCESSFULLY_QUEUED);
3132 }
3133
3134 Static void
3135 umass_scsipi_minphys(bp)
3136 struct buf *bp;
3137 {
3138 if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
3139 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
3140 minphys(bp);
3141 }
3142
3143 int
3144 umass_scsipi_ioctl(link, cmd, arg, flag, p)
3145 struct scsipi_link *link;
3146 u_long cmd;
3147 caddr_t arg;
3148 int flag;
3149 struct proc *p;
3150 {
3151 /*struct umass_softc *sc = link->adapter_softc;*/
3152
3153 switch (cmd) {
3154 #if 0
3155 case SCBUSIORESET:
3156 ccb->ccb_h.status = CAM_REQ_INPROG;
3157 umass_reset(sc, umass_cam_cb, (void *) ccb);
3158 return (0);
3159 #endif
3160 default:
3161 return (ENOTTY);
3162 }
3163 }
3164
3165 Static void
3166 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
3167 {
3168 struct scsipi_xfer *xs = priv;
3169 struct scsipi_link *sc_link = xs->sc_link;
3170 int s;
3171
3172 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: xs=%p residue=%d status=%d\n",
3173 xs, residue, status));
3174
3175 xs->resid = residue;
3176
3177 switch (status) {
3178 case STATUS_CMD_OK:
3179 xs->error = XS_NOERROR;
3180 break;
3181
3182 case STATUS_CMD_UNKNOWN:
3183 case STATUS_CMD_FAILED:
3184 /* fetch sense data */
3185 memset(&sc->sc_sense_cmd, 0, sizeof(sc->sc_sense_cmd));
3186 sc->sc_sense_cmd.opcode = REQUEST_SENSE;
3187 sc->sc_sense_cmd.byte2 = sc_link->scsipi_scsi.lun <<
3188 SCSI_CMD_LUN_SHIFT;
3189 sc->sc_sense_cmd.length = sizeof(xs->sense);
3190
3191 sc->transfer(sc, sc_link->scsipi_scsi.lun,
3192 &sc->sc_sense_cmd, sizeof(sc->sc_sense_cmd),
3193 &xs->sense, sizeof(xs->sense), DIR_IN,
3194 umass_scsipi_sense_cb, xs);
3195 return;
3196
3197 case STATUS_WIRE_FAILED:
3198 xs->error = XS_RESET;
3199 break;
3200
3201 default:
3202 panic("%s: Unknown status %d in umass_scsipi_cb\n",
3203 USBDEVNAME(sc->sc_dev), status);
3204 }
3205
3206 xs->xs_status |= XS_STS_DONE;
3207
3208 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: return xs->error=%d, "
3209 "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
3210 xs->resid));
3211
3212 s = splbio();
3213 scsipi_done(xs);
3214 splx(s);
3215 }
3216
3217 /*
3218 * Finalise a completed autosense operation
3219 */
3220 Static void
3221 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
3222 int status)
3223 {
3224 struct scsipi_xfer *xs = priv;
3225 int s;
3226
3227 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
3228 "status=%d\n", xs, residue, status));
3229
3230 switch (status) {
3231 case STATUS_CMD_OK:
3232 case STATUS_CMD_UNKNOWN:
3233 /* getting sense data succeeded */
3234 /* XXX look at residue */
3235 if (residue == 0 || residue == 14)/* XXX */
3236 xs->error = XS_SENSE;
3237 else
3238 xs->error = XS_SHORTSENSE;
3239 break;
3240 default:
3241 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
3242 USBDEVNAME(sc->sc_dev), status));
3243 xs->error = XS_DRIVER_STUFFUP;
3244 break;
3245 }
3246
3247 xs->xs_status |= XS_STS_DONE;
3248
3249 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
3250 "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
3251 xs->resid));
3252
3253 s = splbio();
3254 scsipi_done(xs);
3255 splx(s);
3256 }
3257
3258 #if NATAPIBUS > 0
3259 Static void
3260 umass_atapi_probedev(atapi, target)
3261 struct atapibus_softc *atapi;
3262 int target;
3263 {
3264 struct scsipi_link *sc_link;
3265 struct scsipibus_attach_args sa;
3266 struct ata_drive_datas *drvp = &atapi->sc_drvs[target];
3267 char vendor[33], product[65], revision[17];
3268 struct scsipi_inquiry_data inqbuf;
3269
3270 DPRINTF(UDMASS_SCSI,("umass_atapi_probedev: atapi=%p target=%d\n",
3271 atapi, target));
3272
3273 if (atapi->sc_link[target])
3274 return;
3275
3276 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
3277 if (sc_link == NULL) {
3278 printf("%s: can't allocate link for drive %d\n",
3279 atapi->sc_dev.dv_xname, target);
3280 return;
3281 }
3282 *sc_link = *atapi->adapter_link;
3283
3284 DIF(UDMASS_UPPER, sc_link->flags |= DEBUGLEVEL);
3285
3286 /* Fill generic parts of the link. */
3287 sc_link->active = 0;
3288 sc_link->scsipi_atapi.drive = target;
3289 sc_link->device = &umass_dev;
3290 TAILQ_INIT(&sc_link->pending_xfers);
3291
3292 DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing inquiry\n"));
3293 /* Now go ask the device all about itself. */
3294 memset(&inqbuf, 0, sizeof(inqbuf));
3295 if (scsipi_inquire(sc_link, &inqbuf, XS_CTL_DISCOVERY) != 0)
3296 goto bad;
3297
3298 scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
3299 scsipi_strvis(product, 65, inqbuf.product, 16);
3300 scsipi_strvis(revision, 17, inqbuf.revision, 4);
3301
3302 sa.sa_sc_link = sc_link;
3303 sa.sa_inqbuf.type = inqbuf.device;
3304 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
3305 T_REMOV : T_FIXED;
3306 if (sa.sa_inqbuf.removable)
3307 sc_link->flags |= SDEV_REMOVABLE;
3308 /* XXX how? sc_link->scsipi_atapi.cap |= ACAP_LEN;*/
3309 sa.sa_inqbuf.vendor = vendor;
3310 sa.sa_inqbuf.product = product;
3311 sa.sa_inqbuf.revision = revision;
3312 sa.sa_inqptr = NULL;
3313
3314 drvp->drv_softc = atapi_probedev(atapi, target, sc_link, &sa);
3315 /* atapi_probedev() frees the scsipi_link when there is no device. */
3316 return;
3317
3318 bad:
3319 free(sc_link, M_DEVBUF);
3320 return;
3321 }
3322 #endif
3323 #endif
3324