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