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