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