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