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