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