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