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