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