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