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