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