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