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