umass.c revision 1.16 1 /* $NetBSD: umass.c,v 1.16 1999/09/11 21:45:28 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>,
41 * Nick Hibma <hibma (at) skylink.it>
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * FreeBSD: src/sys/dev/usb/umass.c,v 1.8 1999/06/20 15:46:13 n_hibma Exp
66 */
67
68 /*
69 * Universal Serial Bus Mass Storage Class Control/Interrupt/Bulk (CBI)
70 * Specification:
71 *
72 * http://www.usb.org/developers/data/usbmass-cbi10.pdf
73 *
74 * Universal Serial Bus Mass Storage Bulk Only 1.0rc4 Specification:
75 *
76 * http://www.usb.org/developers/data/usbmassbulk_10rc4.pdf
77 *
78 * Relevant parts of the old spec (Bulk-only 0.9) have been quoted
79 * in the source.
80 */
81
82 /* To do:
83 * x The umass_usb_transfer routine uses synchroneous transfers. This
84 * should be changed to async and state handling.
85 *
86 * x Should handle more than just Iomega USB Zip drives. There are
87 * a fair number of USB->SCSI dongles out there.
88 *
89 * x Need to implement SCSI command timeout/abort handling.
90 *
91 * x Add support for other than Bulk.
92 *
93 * x Add support for other than SCSI.
94 */
95
96 /* Authors: (with short acronyms for comments)
97 * NWH - Nick Hibma <hibma (at) skylink.it>
98 * JRT - Jason R. Thorpe <thorpej (at) shagadelic.org>
99 */
100
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/kernel.h>
104 #include <sys/malloc.h>
105 #include <sys/device.h>
106 #include <sys/buf.h>
107 #include <sys/proc.h>
108
109 #include <dev/usb/usb.h>
110 #include <dev/usb/usbdi.h>
111 #include <dev/usb/usbdi_util.h>
112
113 #include <dev/scsipi/scsi_all.h>
114 #include <dev/scsipi/scsipi_all.h>
115 #include <dev/scsipi/scsiconf.h>
116
117 #ifdef UMASS_DEBUG
118 #define DPRINTF(m, x) if (umassdebug & (m)) logprintf x
119 #define UDMASS_SCSI 0x00020000
120 #define UDMASS_USB 0x00040000
121 #define UDMASS_BULK 0x00080000
122 #define UDMASS_ALL 0xffff0000
123 int umassdebug = /* UDMASS_SCSI|UDMASS_BULK|UDMASS_USB */ 0;
124 #else
125 #define DPRINTF(m, x)
126 #endif
127
128 typedef struct umass_softc {
129 USBBASEDEVICE sc_dev; /* base device */
130 usbd_interface_handle sc_iface; /* the interface we use */
131
132 u_int8_t sc_bulkout; /* bulk-out Endpoint Address */
133 usbd_pipe_handle sc_bulkout_pipe;
134 u_int8_t sc_bulkin; /* bulk-in Endpoint Address */
135 usbd_pipe_handle sc_bulkin_pipe;
136
137 struct scsipi_link sc_link; /* prototype for devs */
138 struct scsipi_adapter sc_adapter;
139
140 device_ptr_t sc_child; /* child device, for detach */
141
142 char sc_dying;
143 } umass_softc_t;
144
145 #define USBD_COMMAND_FAILED USBD_INVAL /* redefine some errors for */
146
147 #define UMASS_SCSIID_HOST 0x00
148 #define UMASS_SCSIID_DEVICE 0x01
149
150 #define DIR_OUT 0
151 #define DIR_IN 1
152 #define DIR_NONE 2
153
154 /* Bulk-Only specific request */
155 #define UR_RESET 0xff
156 #define UR_GET_MAX_LUN 0xfe
157
158 /* Bulk-Only Mass Storage features */
159 /* Command Block Wrapper */
160 typedef struct {
161 uDWord dCBWSignature;
162 #define CBWSIGNATURE 0x43425355
163 uDWord dCBWTag;
164 uDWord dCBWDataTransferLength;
165 uByte bCBWFlags;
166 #define CBWFLAGS_OUT 0x00
167 #define CBWFLAGS_IN 0x80
168 uByte bCBWLUN;
169 uByte bCDBLength;
170 uByte CBWCDB[16];
171 } usb_bulk_cbw_t;
172 #define USB_BULK_CBW_SIZE 31
173
174 /* Command Status Wrapper */
175 typedef struct {
176 uDWord dCSWSignature;
177 #define CSWSIGNATURE 0x53425355
178 uDWord dCSWTag;
179 uDWord dCSWDataResidue;
180 uByte bCSWStatus;
181 #define CSWSTATUS_GOOD 0x0
182 #define CSWSTATUS_FAILED 0x1
183 #define CSWSTATUS_PHASE 0x2
184 } usb_bulk_csw_t;
185 #define USB_BULK_CSW_SIZE 13
186
187
188 USB_DECLARE_DRIVER(umass);
189
190 /* USB related functions */
191 usbd_status umass_usb_transfer __P((umass_softc_t *,
192 usbd_pipe_handle pipe,
193 void *buf, int buflen,
194 int flags, int *xfer_size));
195
196 /* Bulk-Only related functions */
197 usbd_status umass_bulk_reset __P((umass_softc_t *sc));
198 usbd_status umass_bulk_get_max_lun __P((umass_softc_t *sc, u_int8_t *maxlun));
199 usbd_status umass_bulk_transfer __P((umass_softc_t *sc, int lun,
200 void *cmd, int cmdlen,
201 void *data, int datalen,
202 int dir, int *residue));
203
204 /* SCSIPI related functions */
205 struct scsipi_device umass_dev = {
206 NULL, /* Use default error handler */
207 NULL, /* have a queue, served by this */
208 NULL, /* have no async handler */
209 NULL, /* Use default `done' routine */
210 };
211
212 void umass_scsipi_minphys __P((struct buf *));
213 int umass_scsipi_scsi_cmd __P((struct scsipi_xfer *));
214
215
216
217 USB_MATCH(umass)
218 {
219 USB_MATCH_START(umass, uaa);
220 usb_interface_descriptor_t *id;
221
222 if (!uaa->iface)
223 return(UMATCH_NONE);
224
225 id = usbd_get_interface_descriptor(uaa->iface);
226 if (id
227 && id->bInterfaceClass == UCLASS_MASS
228 && id->bInterfaceSubClass == USUBCLASS_SCSI
229 && id->bInterfaceProtocol == UPROTO_MASS_BULK)
230 return(UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
231
232 return(UMATCH_NONE);
233 }
234
235 USB_ATTACH(umass)
236 {
237 USB_ATTACH_START(umass, sc, uaa);
238 usb_interface_descriptor_t *id;
239 usb_endpoint_descriptor_t *ed;
240 char devinfo[1024];
241 usbd_status err;
242 int i;
243 u_int8_t maxlun;
244
245 sc->sc_iface = uaa->iface;
246 sc->sc_bulkout_pipe = NULL;
247 sc->sc_bulkin_pipe = NULL;
248
249 usbd_devinfo(uaa->device, 0, devinfo);
250 USB_ATTACH_SETUP;
251
252 id = usbd_get_interface_descriptor(sc->sc_iface);
253 printf("%s: %s, iclass %d/%d/%d\n", USBDEVNAME(sc->sc_dev), devinfo,
254 id->bInterfaceClass, id->bInterfaceSubClass,
255 id->bInterfaceProtocol);
256
257 /*
258 * A Bulk-Only Mass Storage device supports the following endpoints,
259 * in addition to the Endpoint 0 for Control transfer that is required
260 * of all USB devices:
261 * (a) bulk-in endpoint.
262 * (b) bulk-out endpoint.
263 *
264 * The endpoint addresses are not fixed, so we have to read them
265 * from the device descriptors of the current interface.
266 */
267 for (i = 0 ; i < id->bNumEndpoints ; i++) {
268 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
269 if (!ed) {
270 printf("%s: could not read endpoint descriptor\n",
271 USBDEVNAME(sc->sc_dev));
272 USB_ATTACH_ERROR_RETURN;
273 }
274 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
275 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
276 sc->sc_bulkin = ed->bEndpointAddress;
277 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
278 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
279 sc->sc_bulkout = ed->bEndpointAddress;
280 }
281 }
282
283 /*
284 * Get the maximum LUN supported by the device.
285 */
286 err = umass_bulk_get_max_lun(sc, &maxlun);
287 if (err != USBD_NORMAL_COMPLETION) {
288 printf("%s: unable to get Max Lun: %s\n",
289 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
290 USB_ATTACH_ERROR_RETURN;
291 }
292
293 /* Open the bulk-in and -out pipe */
294 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
295 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
296 if (err) {
297 DPRINTF(UDMASS_USB,("cannot open bulk out pipe (address %d)\n",
298 sc->sc_bulkout));
299 USB_ATTACH_ERROR_RETURN;
300 }
301 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
302 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
303 if (err) {
304 DPRINTF(UDMASS_USB,("cannot open bulk in pipe (address %d)\n",
305 sc->sc_bulkin));
306 usbd_close_pipe(sc->sc_bulkout_pipe);
307 USB_ATTACH_ERROR_RETURN;
308 }
309
310 /* attach the device to the SCSIPI layer */
311 sc->sc_adapter.scsipi_cmd = umass_scsipi_scsi_cmd;
312 sc->sc_adapter.scsipi_minphys = umass_scsipi_minphys;
313
314 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
315 sc->sc_link.adapter_softc = sc;
316 sc->sc_link.scsipi_scsi.adapter_target = UMASS_SCSIID_HOST;
317 sc->sc_link.adapter = &sc->sc_adapter;
318 sc->sc_link.device = &umass_dev;
319 sc->sc_link.openings = 1;
320 sc->sc_link.scsipi_scsi.max_target = UMASS_SCSIID_DEVICE; /* XXX */
321 sc->sc_link.scsipi_scsi.max_lun = maxlun;
322 sc->sc_link.type = BUS_SCSI;
323
324 sc->sc_child = config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
325 if (sc->sc_child == NULL) {
326 usbd_close_pipe(sc->sc_bulkout_pipe);
327 usbd_close_pipe(sc->sc_bulkin_pipe);
328 /* XXX Not really an error. */
329 USB_ATTACH_ERROR_RETURN;
330 }
331
332 USB_ATTACH_SUCCESS_RETURN;
333 }
334
335 int
336 umass_activate(self, act)
337 struct device *self;
338 enum devact act;
339 {
340 struct umass_softc *sc = (struct umass_softc *) self;
341 int s, rv = 0;
342
343 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
344 USBDEVNAME(sc->sc_dev), act));
345
346 s = splhigh();
347 switch (act) {
348 case DVACT_ACTIVATE:
349 rv = EOPNOTSUPP;
350 break;
351
352 case DVACT_DEACTIVATE:
353 if (sc->sc_child == NULL || sc->sc_dying)
354 break;
355 rv = config_deactivate(sc->sc_child);
356 DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
357 "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
358 if (rv == 0)
359 sc->sc_dying = 1;
360 break;
361 }
362 splx(s);
363 return (rv);
364 }
365
366 int
367 umass_detach(self, flags)
368 struct device *self;
369 int flags;
370 {
371 struct umass_softc *sc = (struct umass_softc *) self;
372 int rv = 0;
373
374 DPRINTF(UDMASS_USB, ("%s: umass_detach: flags 0x%x\n",
375 USBDEVNAME(sc->sc_dev), flags));
376
377 if (sc->sc_child != NULL)
378 rv = config_detach(sc->sc_child, flags);
379
380 if (rv == 0) {
381 if (sc->sc_bulkin_pipe != NULL) {
382 usbd_abort_pipe(sc->sc_bulkin_pipe);
383 usbd_close_pipe(sc->sc_bulkin_pipe);
384 }
385 if (sc->sc_bulkout_pipe != NULL) {
386 usbd_abort_pipe(sc->sc_bulkout_pipe);
387 usbd_close_pipe(sc->sc_bulkout_pipe);
388 }
389 }
390
391 return (rv);
392 }
393
394 /* Performs a request over a pipe.
395 *
396 * flags: Can be set to USBD_SHORT_XFER_OK
397 * xfer_size: if not null returns the nr. of bytes transferred
398 *
399 * If the returned error is USBD_STALLED the pipe stall has
400 * been cleared again.
401 */
402
403 usbd_status
404 umass_usb_transfer(umass_softc_t *sc, usbd_pipe_handle pipe,
405 void *buf, int buflen, int flags, int *xfer_size)
406 {
407 usbd_request_handle reqh;
408 usbd_private_handle priv;
409 void *buffer;
410 int size;
411 usbd_status err;
412
413 /* A transfer is done synchronously. We create and schedule the
414 * transfer and then wait for it to complete
415 */
416
417 reqh = usbd_alloc_request(usbd_pipe2device_handle(pipe));
418 if (!reqh) {
419 DPRINTF(UDMASS_USB, ("%s: not enough memory\n",
420 USBDEVNAME(sc->sc_dev)));
421 return USBD_NOMEM;
422 }
423
424 usbd_setup_request(reqh, pipe, 0, buf, buflen,flags, 3000 /*ms*/, NULL);
425 err = usbd_sync_transfer(reqh);
426 if (err) {
427 DPRINTF(UDMASS_USB, ("%s: transfer failed: %s\n",
428 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
429 usbd_free_request(reqh);
430 return(err);
431 }
432
433 usbd_get_request_status(reqh, &priv, &buffer, &size, &err);
434
435 if (xfer_size)
436 *xfer_size = size;
437
438 usbd_free_request(reqh);
439 return(USBD_NORMAL_COMPLETION);
440 }
441
442 usbd_status
443 umass_bulk_get_max_lun(umass_softc_t *sc, u_int8_t *maxlun)
444 {
445 usbd_device_handle dev;
446 usb_device_request_t req;
447 usbd_status err;
448 usb_interface_descriptor_t *id;
449
450 *maxlun = 0; /* Default to 0. */
451
452 DPRINTF(UDMASS_BULK, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
453
454 usbd_interface2device_handle(sc->sc_iface, &dev);
455 id = usbd_get_interface_descriptor(sc->sc_iface);
456
457 /* The Get Max Lun command is a class-specific request. */
458 req.bmRequestType = UT_READ_CLASS_INTERFACE;
459 req.bRequest = UR_GET_MAX_LUN;
460 USETW(req.wValue, 0);
461 USETW(req.wIndex, id->bInterfaceNumber);
462 USETW(req.wLength, 1);
463
464 err = usbd_do_request(dev, &req, maxlun);
465 switch (err) {
466 case USBD_NORMAL_COMPLETION:
467 DPRINTF(UDMASS_BULK, ("%s: Max Lun %d\n",
468 USBDEVNAME(sc->sc_dev), *maxlun));
469 break;
470
471 case USBD_STALLED:
472 /*
473 * Device doesn't support Get Max Lun request.
474 */
475 err = USBD_NORMAL_COMPLETION;
476 DPRINTF(UDMASS_BULK, ("%s: Get Max Lun not supported\n",
477 USBDEVNAME(sc->sc_dev)));
478 break;
479
480 case USBD_SHORT_XFER:
481 /*
482 * XXX This must mean Get Max Lun is not supported, too!
483 */
484 err = USBD_NORMAL_COMPLETION;
485 DPRINTF(UDMASS_BULK, ("%s: Get Max Lun SHORT_XFER\n",
486 USBDEVNAME(sc->sc_dev)));
487 break;
488
489 default:
490 printf("%s: Get Max Lun failed: %s\n",
491 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
492 /* XXX Should we port_reset the device? */
493 break;
494 }
495
496 return (err);
497 }
498
499 usbd_status
500 umass_bulk_reset(umass_softc_t *sc)
501 {
502 usbd_device_handle dev;
503 usb_device_request_t req;
504 usbd_status err;
505 usb_interface_descriptor_t *id;
506
507 /*
508 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
509 *
510 * For Reset Recovery the host shall issue in the following order:
511 * a) a Bulk-Only Mass Storage Reset
512 * b) a Clear Feature HALT to the Bulk-In endpoint
513 * c) a Clear Feature HALT to the Bulk-Out endpoint
514 */
515
516 DPRINTF(UDMASS_BULK, ("%s: Reset\n",
517 USBDEVNAME(sc->sc_dev)));
518
519 usbd_interface2device_handle(sc->sc_iface, &dev);
520 id = usbd_get_interface_descriptor(sc->sc_iface);
521
522 /* the reset command is a class specific interface request */
523 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
524 req.bRequest = UR_RESET;
525 USETW(req.wValue, 0);
526 USETW(req.wIndex, id->bInterfaceNumber);
527 USETW(req.wLength, 0);
528
529 err = usbd_do_request(dev, &req, 0);
530 if (err) {
531 printf("%s: Reset failed, %s\n",
532 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
533 /* XXX we should port_reset the device */
534 return(err);
535 }
536
537 usbd_clear_endpoint_stall(sc->sc_bulkout_pipe);
538 usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
539
540 /*
541 * XXX we should convert this into a more friendly delay.
542 * Perhaps a tsleep (or is this routine run from int context?)
543 */
544
545 DELAY(2500000 /*us*/);
546
547 return(USBD_NORMAL_COMPLETION);
548 }
549
550 /*
551 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
552 * a data phase of datalen bytes from/to data and finally a csw read
553 * phase.
554 *
555 * If the data direction was inbound a maximum of datalen bytes
556 * is stored in the buffer pointed to by data.
557 * The status returned is USBD_NORMAL_COMPLETION,
558 * USBD_IOERROR, USBD_COMMAND_FAILED.
559 * In the last case *residue is set to the residue from the CSW,
560 * otherwise to 0.
561 *
562 * For the functionality of this subroutine see the Mass Storage
563 * Spec., the graphs on page 14 and page 19 and beyong (v0.9 of
564 * the spec).
565 */
566
567 usbd_status
568 umass_bulk_transfer(umass_softc_t *sc, int lun, void *cmd, int cmdlen,
569 void *data, int datalen, int dir, int *residue)
570 {
571 static int dCBWtag = 42; /* tag to be used in transfers,
572 * incremented at each transfer */
573 usb_bulk_cbw_t cbw; /* command block wrapper struct */
574 usb_bulk_csw_t csw; /* command status wrapper struct */
575 u_int32_t n = 0; /* number of bytes transported */
576 usbd_status err;
577
578 #ifdef UMASS_DEBUG
579 u_int8_t *c = cmd;
580
581 /* check the given arguments */
582 if (!data && datalen > 0) { /* no buffer for transfer */
583 DPRINTF(UDMASS_BULK, ("%s: no buffer, but datalen > 0 !\n",
584 USBDEVNAME(sc->sc_dev)));
585 return USBD_IOERROR;
586 }
587
588 DPRINTF(UDMASS_BULK, ("%s: cmd: %d bytes (0x%02x%02x%02x%02x%02x%02x%s)"
589 ", data: %d bytes, dir: %s\n",
590 USBDEVNAME(sc->sc_dev),
591 cmdlen, c[0], c[1], c[2], c[3], c[4], c[5],
592 (cmdlen > 6? "...":""),
593 datalen, (dir == DIR_IN? "in":"out")));
594 #endif
595
596 if (dir == DIR_NONE || datalen == 0) { /* make sure they correspond */
597 datalen = 0;
598 dir = DIR_NONE;
599 }
600
601 if (residue != NULL)
602 *residue = 0; /* reset residue */
603
604 /*
605 * Determine the direction of transferring data and data length.
606 *
607 * dCBWDataTransferLength (datalen) :
608 * This field indicates the number of bytes of data that the host
609 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
610 * the Direction bit) during the execution of this command. If this
611 * field is set to 0, the device will expect that no data will be
612 * transferred IN or OUT during this command, regardless of the value
613 * of the Direction bit defined in dCBWFlags.
614 *
615 * dCBWFlags (dir) :
616 * The bits of the Flags field are defined as follows:
617 * Bits 0-6 reserved
618 * Bit 7 Direction - this bit shall be ignored if the
619 * dCBWDataTransferLength field is zero.
620 * 0 = data Out from host to device
621 * 1 = data In from device to host
622 */
623
624
625 /*
626 * Command transport phase
627 */
628
629 /* Fill in the Command Block Wrapper */
630 USETDW(cbw.dCBWSignature, CBWSIGNATURE);
631 USETDW(cbw.dCBWTag, dCBWtag++);
632 USETDW(cbw.dCBWDataTransferLength, datalen);
633 /* we do not check for DIR_NONE below (see text on dCBWFlags above) */
634 cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
635 cbw.bCBWLUN = lun;
636 cbw.bCDBLength = cmdlen;
637 bcopy(cmd, cbw.CBWCDB, cmdlen);
638
639 /* Send the CBW from host to device via bulk-out endpoint. */
640 err = umass_usb_transfer(sc, sc->sc_bulkout_pipe,
641 &cbw, USB_BULK_CBW_SIZE, 0, NULL);
642 if (err) {
643 DPRINTF(UDMASS_BULK, ("%s: failed to send CBW\n",
644 USBDEVNAME(sc->sc_dev)));
645 /* If the device detects that the CBW is invalid, then the
646 * device may STALL both bulk endpoints and require a
647 * Bulk-Only MS Reset
648 */
649 umass_bulk_reset(sc);
650 return(USBD_IOERROR);
651 }
652
653
654 /*
655 * Data transport phase (only if there is data to be sent/received)
656 */
657
658 if (dir == DIR_IN) {
659 /* we allow short transfers for bulk-in pipes */
660 err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
661 data, datalen,
662 USBD_SHORT_XFER_OK, &n);
663 if (err)
664 DPRINTF(UDMASS_BULK, ("%s: failed to receive data, "
665 "(%d bytes, n = %d), %s\n",
666 USBDEVNAME(sc->sc_dev),
667 datalen, n, usbd_errstr(err)));
668 } else if (dir == DIR_OUT) {
669 err = umass_usb_transfer(sc, sc->sc_bulkout_pipe,
670 data, datalen, 0, &n);
671 if (err)
672 DPRINTF(UDMASS_BULK, ("%s: failed to send data, "
673 "(%d bytes, n = %d), %s\n",
674 USBDEVNAME(sc->sc_dev),
675 datalen, n, usbd_errstr(err)));
676 }
677 if (err && err != USBD_STALLED)
678 return(USBD_IOERROR);
679
680
681 /*
682 * Status transport phase
683 */
684
685 /* Read the Command Status Wrapper via bulk-in endpoint. */
686 err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
687 &csw, USB_BULK_CSW_SIZE, 0, NULL);
688 /* Try again if the bulk-in pipe was stalled */
689 if (err == USBD_STALLED) {
690 err = usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
691 if (!err) {
692 err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
693 &csw, USB_BULK_CSW_SIZE, 0,
694 NULL);
695 }
696 }
697 if (err && err != USBD_STALLED)
698 return(USBD_IOERROR);
699
700 /*
701 * Check the CSW for status and validity, and check for fatal errors
702 */
703
704 /* Invalid CSW: Wrong signature or wrong tag might indicate
705 * that the device is confused -> reset it.
706 * Other fatal errors: STALL on read of CSW and Phase error
707 * or unknown status.
708 */
709 if (err == USBD_STALLED
710 || UGETDW(csw.dCSWSignature) != CSWSIGNATURE
711 || UGETDW(csw.dCSWTag) != UGETDW(cbw.dCBWTag)
712 || csw.bCSWStatus == CSWSTATUS_PHASE
713 || csw.bCSWStatus > CSWSTATUS_PHASE) {
714 if (err) {
715 printf("%s: failed to read CSW, %s\n",
716 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
717 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
718 printf("%s: Phase Error, residue = %d, n = %d\n",
719 USBDEVNAME(sc->sc_dev),
720 UGETDW(csw.dCSWDataResidue), n);
721 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
722 printf("%s: Unknown status %d in CSW\n",
723 USBDEVNAME(sc->sc_dev), csw.bCSWStatus);
724 } else {
725 printf("%s: invalid CSW, sig = 0x%08x, tag = %d (!= %d)\n",
726 USBDEVNAME(sc->sc_dev),
727 UGETDW(csw.dCSWSignature),
728 UGETDW(csw.dCSWTag), UGETDW(cbw.dCBWTag));
729 }
730 umass_bulk_reset(sc);
731 return(USBD_IOERROR);
732 }
733
734 if (csw.bCSWStatus == CSWSTATUS_FAILED) {
735 DPRINTF(UDMASS_BULK, ("%s: Command Failed, "
736 "residue = %d, n = %d\n",
737 USBDEVNAME(sc->sc_dev),
738 UGETDW(csw.dCSWDataResidue), n));
739 if (residue != NULL)
740 *residue = UGETDW(csw.dCSWDataResidue);
741 return(USBD_COMMAND_FAILED);
742 }
743
744 /*
745 * XXX a residue not equal to 0 might indicate that something
746 * is wrong. Does CAM high level drivers check this for us?
747 */
748
749 return(USBD_NORMAL_COMPLETION);
750 }
751
752
753 /*
754 * SCSIPI specific functions
755 */
756
757 int
758 umass_scsipi_scsi_cmd(xs)
759 struct scsipi_xfer *xs;
760 {
761 struct scsipi_link *sc_link = xs->sc_link;
762 struct umass_softc *sc = sc_link->adapter_softc;
763 int residue, dir;
764 usbd_status err;
765 struct scsipi_sense sense_cmd;
766
767 DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd %d:%d\n",
768 USBDEVNAME(sc->sc_dev),
769 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
770
771 if (sc->sc_dying) {
772 xs->flags |= ITSDONE;
773 xs->error = XS_DRIVER_STUFFUP;
774 scsipi_done(xs);
775 if (xs->flags & SCSI_POLL)
776 return (SUCCESSFULLY_QUEUED);
777 else
778 return (COMPLETE);
779 }
780
781 #ifdef UMASS_DEBUG
782 if (sc_link->scsipi_scsi.target != UMASS_SCSIID_DEVICE) {
783 DPRINTF(UDMASS_SCSI, ("%s: Wrong SCSI ID %d\n",
784 USBDEVNAME(sc->sc_dev),
785 sc_link->scsipi_scsi.target));
786 xs->error = XS_DRIVER_STUFFUP;
787 return (COMPLETE);
788 }
789 #endif
790
791 dir = DIR_NONE;
792 if (xs->datalen) {
793 switch (xs->flags & (SCSI_DATA_IN|SCSI_DATA_OUT)) {
794 case SCSI_DATA_IN:
795 dir = DIR_IN;
796 break;
797 case SCSI_DATA_OUT:
798 dir = DIR_OUT;
799 break;
800 }
801 }
802
803 err = umass_bulk_transfer(sc, sc_link->scsipi_scsi.lun,
804 xs->cmd, xs->cmdlen, xs->data, xs->datalen, dir, &residue);
805
806 /*
807 * FAILED commands are supposed to be SCSI failed commands
808 * and are therefore considered to be successfull CDW/CSW
809 * transfers. PHASE errors are more serious and should return
810 * an error to the SCSIPI system.
811 *
812 * XXX This is however more based on empirical evidence than on
813 * hard proof from the Bulk-Only spec.
814 */
815 if (err == USBD_NORMAL_COMPLETION)
816 xs->error = XS_NOERROR;
817 else {
818 DPRINTF(UDMASS_USB|UDMASS_SCSI, ("%s: bulk transfer completed "
819 "with error %s\n", USBDEVNAME(sc->sc_dev),
820 usbd_errstr(err)));
821
822 /*
823 * Probably have a CHECK CONDITION here. Issue a
824 * REQUEST SENSE.
825 */
826 memset(&sense_cmd, 0, sizeof(sense_cmd));
827 sense_cmd.opcode = REQUEST_SENSE;
828 sense_cmd.byte2 = sc_link->scsipi_scsi.lun <<
829 SCSI_CMD_LUN_SHIFT;
830 sense_cmd.length = sizeof(xs->sense);
831
832 if ((err = umass_bulk_transfer(sc, sc_link->scsipi_scsi.lun,
833 (struct scsipi_generic *)&sense_cmd, sizeof(sense_cmd),
834 &xs->sense, sizeof(xs->sense), DIR_IN, NULL)) !=
835 USBD_NORMAL_COMPLETION) {
836 DPRINTF(UDMASS_SCSI, ("%s: REQUEST SENSE failed: %s\n",
837 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
838 xs->error = XS_DRIVER_STUFFUP; /* XXX */
839 } else
840 xs->error = XS_SENSE;
841 }
842 xs->resid = residue;
843
844 DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd: error = %d, resid = 0x%x\n",
845 USBDEVNAME(sc->sc_dev), xs->error, xs->resid));
846
847 xs->flags |= ITSDONE;
848 scsipi_done(xs);
849
850 /*
851 * XXXJRT We must return successfully queued if we're an
852 * XXXJRT `asynchronous' command, otherwise `xs' will be
853 * XXXJRT freed twice: once in scsipi_done(), and once in
854 * XXXJRT scsi_scsipi_cmd().
855 */
856 if ((xs->flags & SCSI_POLL) == 0)
857 return (SUCCESSFULLY_QUEUED);
858
859 return (COMPLETE);
860 }
861
862 void
863 umass_scsipi_minphys(bp)
864 struct buf *bp;
865 {
866
867 /* No limit here. */
868 minphys(bp);
869 }
870