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