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