umass.c revision 1.24 1 /* $NetBSD: umass.c,v 1.24 2000/02/02 13:18:47 augustss 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 #if defined(USB_DEBUG) && !defined(UMASS_DEBUG)
118 #define UMASS_DEBUG 1
119 #endif
120
121 #ifdef UMASS_DEBUG
122 #define DPRINTF(m, x) if (umassdebug & (m)) logprintf x
123 #define UDMASS_SCSI 0x00020000
124 #define UDMASS_USB 0x00040000
125 #define UDMASS_BULK 0x00080000
126 #define UDMASS_ALL 0xffff0000
127 int umassdebug = /* UDMASS_SCSI|UDMASS_BULK|UDMASS_USB */ 0;
128 #else
129 #define DPRINTF(m, x)
130 #endif
131
132 typedef struct umass_softc {
133 USBBASEDEVICE sc_dev; /* base device */
134 usbd_device_handle sc_udev;
135 usbd_interface_handle sc_iface; /* the interface we use */
136
137 u_int8_t sc_subclass; /* our USB subclass */
138 u_int8_t sc_protocol; /* our USB protocol */
139
140 u_int8_t sc_bulkout; /* bulk-out Endpoint Address */
141 usbd_pipe_handle sc_bulkout_pipe;
142 u_int8_t sc_bulkin; /* bulk-in Endpoint Address */
143 usbd_pipe_handle sc_bulkin_pipe;
144
145 struct scsipi_link sc_link; /* prototype for devs */
146 struct scsipi_adapter sc_adapter;
147
148 device_ptr_t sc_child; /* child device, for detach */
149
150 int sc_refcnt;
151 char sc_dying;
152 } umass_softc_t;
153
154 #define USBD_COMMAND_FAILED USBD_INVAL /* redefine some errors for */
155
156 #define UMASS_SCSIID_HOST 0x00
157 #define UMASS_SCSIID_DEVICE 0x01
158
159 #define DIR_OUT 0
160 #define DIR_IN 1
161 #define DIR_NONE 2
162
163 /* Bulk-Only specific request */
164 #define UR_RESET 0xff
165 #define UR_GET_MAX_LUN 0xfe
166
167 /* Bulk-Only Mass Storage features */
168 /* Command Block Wrapper */
169 typedef struct {
170 uDWord dCBWSignature;
171 #define CBWSIGNATURE 0x43425355
172 uDWord dCBWTag;
173 uDWord dCBWDataTransferLength;
174 uByte bCBWFlags;
175 #define CBWFLAGS_OUT 0x00
176 #define CBWFLAGS_IN 0x80
177 uByte bCBWLUN;
178 uByte bCDBLength;
179 uByte CBWCDB[16];
180 } usb_bulk_cbw_t;
181 #define USB_BULK_CBW_SIZE 31
182
183 /* Command Status Wrapper */
184 typedef struct {
185 uDWord dCSWSignature;
186 #define CSWSIGNATURE 0x53425355
187 uDWord dCSWTag;
188 uDWord dCSWDataResidue;
189 uByte bCSWStatus;
190 #define CSWSTATUS_GOOD 0x0
191 #define CSWSTATUS_FAILED 0x1
192 #define CSWSTATUS_PHASE 0x2
193 } usb_bulk_csw_t;
194 #define USB_BULK_CSW_SIZE 13
195
196
197 USB_DECLARE_DRIVER(umass);
198
199 /* USB related functions */
200 usbd_status umass_usb_transfer __P((umass_softc_t *,
201 usbd_pipe_handle pipe,
202 void *buf, int buflen,
203 int flags, int *xfer_size));
204
205 /* Bulk-Only related functions */
206 usbd_status umass_bulk_reset __P((umass_softc_t *sc));
207 usbd_status umass_bulk_get_max_lun __P((umass_softc_t *sc, u_int8_t *maxlun));
208 usbd_status umass_bulk_transfer __P((umass_softc_t *sc, int lun,
209 void *cmd, int cmdlen,
210 void *data, int datalen,
211 int dir, int *residue));
212
213 /* SCSIPI related functions */
214 struct scsipi_device umass_dev = {
215 NULL, /* Use default error handler */
216 NULL, /* have a queue, served by this */
217 NULL, /* have no async handler */
218 NULL, /* Use default `done' routine */
219 };
220
221 void umass_scsipi_minphys __P((struct buf *));
222 int umass_scsipi_scsi_cmd __P((struct scsipi_xfer *));
223
224
225
226 USB_MATCH(umass)
227 {
228 USB_MATCH_START(umass, uaa);
229 usb_interface_descriptor_t *id;
230
231 if (uaa->iface == NULL)
232 return(UMATCH_NONE);
233
234 id = usbd_get_interface_descriptor(uaa->iface);
235 if (id != NULL
236 && id->bInterfaceClass == UCLASS_MASS
237 && id->bInterfaceSubClass == USUBCLASS_SCSI
238 && id->bInterfaceProtocol == UPROTO_MASS_BULK_P)
239 return(UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
240
241 return(UMATCH_NONE);
242 }
243
244 USB_ATTACH(umass)
245 {
246 USB_ATTACH_START(umass, sc, uaa);
247 usb_interface_descriptor_t *id;
248 usb_endpoint_descriptor_t *ed;
249 char devinfo[1024];
250 usbd_status err;
251 int i;
252 u_int8_t maxlun;
253 const char *subclass, *protocol;
254
255 sc->sc_udev = uaa->device;
256 sc->sc_iface = uaa->iface;
257 sc->sc_bulkout_pipe = NULL;
258 sc->sc_bulkin_pipe = NULL;
259
260 usbd_devinfo(uaa->device, 0, devinfo);
261 USB_ATTACH_SETUP;
262
263 id = usbd_get_interface_descriptor(sc->sc_iface);
264
265 sc->sc_subclass = id->bInterfaceSubClass;
266 sc->sc_protocol = id->bInterfaceProtocol;
267
268 switch (sc->sc_subclass) {
269 #if 0
270 case USUBCLASS_RBC: subclass = "RBC"; break;
271 case USUBCLASS_SFF8020I: subclass = "8020i"; break;
272 case USUBCLASS_QIC157: subclass = "QIC157"; break;
273 case USUBCLASS_UFI: subclass = "UFI"; break;
274 case USUBCLASS_SFF8070I: subclass = "8070i"; break;
275 #endif
276 case USUBCLASS_SCSI: subclass = "SCSI"; break;
277 default:
278 panic("umass_attach: impossible subclass");
279 }
280
281 switch (sc->sc_protocol) {
282 #if 0
283 case UPROTO_MASS_CBI_I: protocol = "CBI with CCI"; break;
284 case UPROTO_MASS_CBI: protocol = "CBI"; break;
285 #endif
286 case UPROTO_MASS_BULK: /* XXX Is this really right? */
287 case UPROTO_MASS_BULK_P: protocol = "Bulk-Only"; break;
288 default:
289 panic("umass_attach: impossible protocol");
290 }
291
292 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
293 printf("%s: %s over %s (iclass %d/%d/%d)\n", USBDEVNAME(sc->sc_dev),
294 subclass, protocol, id->bInterfaceClass, id->bInterfaceSubClass,
295 id->bInterfaceProtocol);
296
297 /*
298 * A Bulk-Only Mass Storage device supports the following endpoints,
299 * in addition to the Endpoint 0 for Control transfer that is required
300 * of all USB devices:
301 * (a) bulk-in endpoint.
302 * (b) bulk-out endpoint.
303 *
304 * The endpoint addresses are not fixed, so we have to read them
305 * from the device descriptors of the current interface.
306 */
307 for (i = 0 ; i < id->bNumEndpoints ; i++) {
308 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
309 if (ed == NULL) {
310 printf("%s: could not read endpoint descriptor\n",
311 USBDEVNAME(sc->sc_dev));
312 USB_ATTACH_ERROR_RETURN;
313 }
314 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
315 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
316 sc->sc_bulkin = ed->bEndpointAddress;
317 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
318 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
319 sc->sc_bulkout = ed->bEndpointAddress;
320 }
321 }
322
323 /*
324 * Get the maximum LUN supported by the device.
325 */
326 err = umass_bulk_get_max_lun(sc, &maxlun);
327 if (err) {
328 printf("%s: unable to get Max Lun: %s\n",
329 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
330 USB_ATTACH_ERROR_RETURN;
331 }
332
333 /* Open the bulk-in and -out pipe */
334 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
335 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
336 if (err) {
337 DPRINTF(UDMASS_USB,("cannot open bulk out pipe (address %d)\n",
338 sc->sc_bulkout));
339 USB_ATTACH_ERROR_RETURN;
340 }
341 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
342 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
343 if (err) {
344 DPRINTF(UDMASS_USB,("cannot open bulk in pipe (address %d)\n",
345 sc->sc_bulkin));
346 usbd_close_pipe(sc->sc_bulkout_pipe);
347 USB_ATTACH_ERROR_RETURN;
348 }
349
350 /* attach the device to the SCSIPI layer */
351 sc->sc_adapter.scsipi_cmd = umass_scsipi_scsi_cmd;
352 sc->sc_adapter.scsipi_minphys = umass_scsipi_minphys;
353
354 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
355 sc->sc_link.adapter_softc = sc;
356 sc->sc_link.scsipi_scsi.adapter_target = UMASS_SCSIID_HOST;
357 sc->sc_link.adapter = &sc->sc_adapter;
358 sc->sc_link.device = &umass_dev;
359 sc->sc_link.openings = 1;
360 sc->sc_link.scsipi_scsi.max_target = UMASS_SCSIID_DEVICE; /* XXX */
361 sc->sc_link.scsipi_scsi.max_lun = maxlun;
362 sc->sc_link.type = BUS_SCSI;
363
364 sc->sc_child = config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
365 if (sc->sc_child == NULL) {
366 usbd_close_pipe(sc->sc_bulkout_pipe);
367 usbd_close_pipe(sc->sc_bulkin_pipe);
368 /* XXX Not really an error. */
369 USB_ATTACH_ERROR_RETURN;
370 }
371
372 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
373 USBDEV(sc->sc_dev));
374
375 USB_ATTACH_SUCCESS_RETURN;
376 }
377
378 int
379 umass_activate(self, act)
380 struct device *self;
381 enum devact act;
382 {
383 struct umass_softc *sc = (struct umass_softc *) self;
384 int s, rv = 0;
385
386 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
387 USBDEVNAME(sc->sc_dev), act));
388
389 s = splhigh();
390 switch (act) {
391 case DVACT_ACTIVATE:
392 rv = EOPNOTSUPP;
393 break;
394
395 case DVACT_DEACTIVATE:
396 if (sc->sc_child == NULL || sc->sc_dying)
397 break;
398 rv = config_deactivate(sc->sc_child);
399 DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
400 "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
401 if (rv == 0)
402 sc->sc_dying = 1;
403 break;
404 }
405 splx(s);
406 return (rv);
407 }
408
409 USB_DETACH(umass)
410 {
411 USB_DETACH_START(umass, sc);
412 int s, rv = 0;
413
414 DPRINTF(UDMASS_USB, ("%s: umass_detach: flags 0x%x\n",
415 USBDEVNAME(sc->sc_dev), flags));
416
417 if (sc->sc_child != NULL)
418 rv = config_detach(sc->sc_child, flags);
419
420 if (rv != 0)
421 return (rv);
422
423 /* Abort the pipes to wake up any waiting processes. */
424 if (sc->sc_bulkin_pipe != NULL)
425 usbd_abort_pipe(sc->sc_bulkin_pipe);
426 if (sc->sc_bulkout_pipe != NULL)
427 usbd_abort_pipe(sc->sc_bulkout_pipe);
428
429 s = splusb();
430 if (--sc->sc_refcnt >= 0) {
431 /* Wait for processes to go away. */
432 usb_detach_wait(USBDEV(sc->sc_dev));
433 }
434 splx(s);
435
436 if (sc->sc_bulkin_pipe != NULL) {
437 usbd_close_pipe(sc->sc_bulkin_pipe);
438 sc->sc_bulkin_pipe = NULL;
439 }
440 if (sc->sc_bulkout_pipe != NULL) {
441 usbd_close_pipe(sc->sc_bulkout_pipe);
442 sc->sc_bulkout_pipe = NULL;
443 }
444
445 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
446 USBDEV(sc->sc_dev));
447
448 return (rv);
449 }
450
451 /* Performs a request over a pipe.
452 *
453 * flags: Can be set to USBD_SHORT_XFER_OK
454 * xfer_size: if not null returns the nr. of bytes transferred
455 *
456 * If the returned error is USBD_STALLED the pipe stall has
457 * been cleared again.
458 */
459
460 usbd_status
461 umass_usb_transfer(umass_softc_t *sc, usbd_pipe_handle pipe,
462 void *buf, int buflen, int flags, int *xfer_size)
463 {
464 usbd_xfer_handle xfer;
465 usbd_private_handle priv;
466 void *buffer;
467 int size;
468 usbd_status err;
469
470 /* A transfer is done synchronously. We create and schedule the
471 * transfer and then wait for it to complete
472 */
473
474 xfer = usbd_alloc_xfer(usbd_pipe2device_handle(pipe));
475 if (xfer == NULL) {
476 DPRINTF(UDMASS_USB, ("%s: not enough memory\n",
477 USBDEVNAME(sc->sc_dev)));
478 return USBD_NOMEM;
479 }
480
481 usbd_setup_xfer(xfer, pipe, 0, buf, buflen,flags, 3000/*ms*/, NULL);
482 err = usbd_sync_transfer(xfer);
483 if (err) {
484 DPRINTF(UDMASS_USB, ("%s: transfer failed: %s\n",
485 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
486 usbd_free_xfer(xfer);
487 return(err);
488 }
489
490 usbd_get_xfer_status(xfer, &priv, &buffer, &size, &err);
491
492 if (xfer_size != NULL)
493 *xfer_size = size;
494
495 usbd_free_xfer(xfer);
496 return(USBD_NORMAL_COMPLETION);
497 }
498
499 usbd_status
500 umass_bulk_get_max_lun(umass_softc_t *sc, u_int8_t *maxlun)
501 {
502 usbd_device_handle dev;
503 usb_device_request_t req;
504 usbd_status err;
505 usb_interface_descriptor_t *id;
506
507 *maxlun = 0; /* Default to 0. */
508
509 DPRINTF(UDMASS_BULK, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
510
511 usbd_interface2device_handle(sc->sc_iface, &dev);
512 id = usbd_get_interface_descriptor(sc->sc_iface);
513
514 /* The Get Max Lun command is a class-specific request. */
515 req.bmRequestType = UT_READ_CLASS_INTERFACE;
516 req.bRequest = UR_GET_MAX_LUN;
517 USETW(req.wValue, 0);
518 USETW(req.wIndex, id->bInterfaceNumber);
519 USETW(req.wLength, 1);
520
521 err = usbd_do_request(dev, &req, maxlun);
522 switch (err) {
523 case USBD_NORMAL_COMPLETION:
524 DPRINTF(UDMASS_BULK, ("%s: Max Lun %d\n",
525 USBDEVNAME(sc->sc_dev), *maxlun));
526 break;
527
528 case USBD_STALLED:
529 /*
530 * Device doesn't support Get Max Lun request.
531 */
532 err = USBD_NORMAL_COMPLETION;
533 DPRINTF(UDMASS_BULK, ("%s: Get Max Lun not supported\n",
534 USBDEVNAME(sc->sc_dev)));
535 break;
536
537 case USBD_SHORT_XFER:
538 /*
539 * XXX This must mean Get Max Lun is not supported, too!
540 */
541 err = USBD_NORMAL_COMPLETION;
542 DPRINTF(UDMASS_BULK, ("%s: Get Max Lun SHORT_XFER\n",
543 USBDEVNAME(sc->sc_dev)));
544 break;
545
546 default:
547 printf("%s: Get Max Lun failed: %s\n",
548 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
549 /* XXX Should we port_reset the device? */
550 break;
551 }
552
553 return (err);
554 }
555
556 usbd_status
557 umass_bulk_reset(umass_softc_t *sc)
558 {
559 usbd_device_handle dev;
560 usb_device_request_t req;
561 usbd_status err;
562 usb_interface_descriptor_t *id;
563
564 /*
565 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
566 *
567 * For Reset Recovery the host shall issue in the following order:
568 * a) a Bulk-Only Mass Storage Reset
569 * b) a Clear Feature HALT to the Bulk-In endpoint
570 * c) a Clear Feature HALT to the Bulk-Out endpoint
571 */
572
573 DPRINTF(UDMASS_BULK, ("%s: Reset\n",
574 USBDEVNAME(sc->sc_dev)));
575
576 usbd_interface2device_handle(sc->sc_iface, &dev);
577 id = usbd_get_interface_descriptor(sc->sc_iface);
578
579 /* the reset command is a class specific interface request */
580 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
581 req.bRequest = UR_RESET;
582 USETW(req.wValue, 0);
583 USETW(req.wIndex, id->bInterfaceNumber);
584 USETW(req.wLength, 0);
585
586 err = usbd_do_request(dev, &req, 0);
587 if (err) {
588 printf("%s: Reset failed, %s\n",
589 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
590 /* XXX we should port_reset the device */
591 return(err);
592 }
593
594 usbd_clear_endpoint_stall(sc->sc_bulkout_pipe);
595 usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
596
597 #if 0
598 /*
599 * XXX we should convert this into a more friendly delay.
600 * Perhaps a tsleep (or is this routine run from int context?)
601 */
602
603 DELAY(2500000 /*us*/);
604 #else
605 usbd_delay_ms(dev, 2500);
606 #endif
607
608 return(USBD_NORMAL_COMPLETION);
609 }
610
611 /*
612 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
613 * a data phase of datalen bytes from/to data and finally a csw read
614 * phase.
615 *
616 * If the data direction was inbound a maximum of datalen bytes
617 * is stored in the buffer pointed to by data.
618 * The status returned is USBD_NORMAL_COMPLETION,
619 * USBD_IOERROR, USBD_COMMAND_FAILED.
620 * In the last case *residue is set to the residue from the CSW,
621 * otherwise to 0.
622 *
623 * For the functionality of this subroutine see the Mass Storage
624 * Spec., the graphs on page 14 and page 19 and beyong (v0.9 of
625 * the spec).
626 */
627
628 usbd_status
629 umass_bulk_transfer(umass_softc_t *sc, int lun, void *cmd, int cmdlen,
630 void *data, int datalen, int dir, int *residue)
631 {
632 static int dCBWtag = 42; /* tag to be used in transfers,
633 * incremented at each transfer */
634 usb_bulk_cbw_t cbw; /* command block wrapper struct */
635 usb_bulk_csw_t csw; /* command status wrapper struct */
636 u_int32_t n = 0; /* number of bytes transported */
637 usbd_status err;
638
639 #ifdef UMASS_DEBUG
640 u_int8_t *c = cmd;
641
642 /* check the given arguments */
643 if (data == NULL && datalen > 0) { /* no buffer for transfer */
644 DPRINTF(UDMASS_BULK, ("%s: no buffer, but datalen > 0 !\n",
645 USBDEVNAME(sc->sc_dev)));
646 return USBD_IOERROR;
647 }
648
649 DPRINTF(UDMASS_BULK, ("%s: cmd: %d bytes (0x%02x%02x%02x%02x%02x%02x%s)"
650 ", data: %d bytes, dir: %s\n",
651 USBDEVNAME(sc->sc_dev),
652 cmdlen, c[0], c[1], c[2], c[3], c[4], c[5],
653 (cmdlen > 6? "...":""),
654 datalen, (dir == DIR_IN? "in":"out")));
655 #endif
656
657 if (dir == DIR_NONE || datalen == 0) { /* make sure they correspond */
658 datalen = 0;
659 dir = DIR_NONE;
660 }
661
662 if (residue != NULL)
663 *residue = 0; /* reset residue */
664
665 /*
666 * Determine the direction of transferring data and data length.
667 *
668 * dCBWDataTransferLength (datalen) :
669 * This field indicates the number of bytes of data that the host
670 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
671 * the Direction bit) during the execution of this command. If this
672 * field is set to 0, the device will expect that no data will be
673 * transferred IN or OUT during this command, regardless of the value
674 * of the Direction bit defined in dCBWFlags.
675 *
676 * dCBWFlags (dir) :
677 * The bits of the Flags field are defined as follows:
678 * Bits 0-6 reserved
679 * Bit 7 Direction - this bit shall be ignored if the
680 * dCBWDataTransferLength field is zero.
681 * 0 = data Out from host to device
682 * 1 = data In from device to host
683 */
684
685
686 /*
687 * Command transport phase
688 */
689
690 /* Fill in the Command Block Wrapper */
691 USETDW(cbw.dCBWSignature, CBWSIGNATURE);
692 USETDW(cbw.dCBWTag, dCBWtag++);
693 USETDW(cbw.dCBWDataTransferLength, datalen);
694 /* we do not check for DIR_NONE below (see text on dCBWFlags above) */
695 cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
696 cbw.bCBWLUN = lun;
697 cbw.bCDBLength = cmdlen;
698 bcopy(cmd, cbw.CBWCDB, cmdlen);
699
700 /* Send the CBW from host to device via bulk-out endpoint. */
701 err = umass_usb_transfer(sc, sc->sc_bulkout_pipe,
702 &cbw, USB_BULK_CBW_SIZE, 0, NULL);
703 if (err) {
704 DPRINTF(UDMASS_BULK, ("%s: failed to send CBW\n",
705 USBDEVNAME(sc->sc_dev)));
706 /* If the device detects that the CBW is invalid, then the
707 * device may STALL both bulk endpoints and require a
708 * Bulk-Only MS Reset
709 */
710 if (!sc->sc_dying)
711 umass_bulk_reset(sc);
712 return(USBD_IOERROR);
713 }
714
715
716 /*
717 * Data transport phase (only if there is data to be sent/received)
718 */
719
720 if (dir == DIR_IN) {
721 /* we allow short transfers for bulk-in pipes */
722 err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
723 data, datalen,
724 USBD_SHORT_XFER_OK, &n);
725 if (err)
726 DPRINTF(UDMASS_BULK, ("%s: failed to receive data, "
727 "(%d bytes, n = %d), %s\n",
728 USBDEVNAME(sc->sc_dev),
729 datalen, n, usbd_errstr(err)));
730 } else if (dir == DIR_OUT) {
731 err = umass_usb_transfer(sc, sc->sc_bulkout_pipe,
732 data, datalen, 0, &n);
733 if (err)
734 DPRINTF(UDMASS_BULK, ("%s: failed to send data, "
735 "(%d bytes, n = %d), %s\n",
736 USBDEVNAME(sc->sc_dev),
737 datalen, n, usbd_errstr(err)));
738 }
739 if (err && err != USBD_STALLED)
740 return(USBD_IOERROR);
741
742
743 /*
744 * Status transport phase
745 */
746
747 /* Read the Command Status Wrapper via bulk-in endpoint. */
748 err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
749 &csw, USB_BULK_CSW_SIZE, 0, NULL);
750 /* Try again if the bulk-in pipe was stalled */
751 if (err == USBD_STALLED) {
752 err = usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
753 if (!err) {
754 err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
755 &csw, USB_BULK_CSW_SIZE, 0, NULL);
756 }
757 }
758 if (err && err != USBD_STALLED)
759 return(USBD_IOERROR);
760
761 /*
762 * Check the CSW for status and validity, and check for fatal errors
763 */
764
765 /* Invalid CSW: Wrong signature or wrong tag might indicate
766 * that the device is confused -> reset it.
767 * Other fatal errors: STALL on read of CSW and Phase error
768 * or unknown status.
769 */
770 if (err == USBD_STALLED
771 || UGETDW(csw.dCSWSignature) != CSWSIGNATURE
772 || UGETDW(csw.dCSWTag) != UGETDW(cbw.dCBWTag)
773 || csw.bCSWStatus == CSWSTATUS_PHASE
774 || csw.bCSWStatus > CSWSTATUS_PHASE) {
775 if (err) {
776 printf("%s: failed to read CSW, %s\n",
777 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
778 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
779 printf("%s: Phase Error, residue = %d, n = %d\n",
780 USBDEVNAME(sc->sc_dev),
781 UGETDW(csw.dCSWDataResidue), n);
782 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
783 printf("%s: Unknown status %d in CSW\n",
784 USBDEVNAME(sc->sc_dev), csw.bCSWStatus);
785 } else {
786 printf("%s: invalid CSW, sig = 0x%08x, tag = %d (!= %d)\n",
787 USBDEVNAME(sc->sc_dev),
788 UGETDW(csw.dCSWSignature),
789 UGETDW(csw.dCSWTag), UGETDW(cbw.dCBWTag));
790 }
791 umass_bulk_reset(sc);
792 return(USBD_IOERROR);
793 }
794
795 if (csw.bCSWStatus == CSWSTATUS_FAILED) {
796 DPRINTF(UDMASS_BULK, ("%s: Command Failed, "
797 "residue = %d, n = %d\n",
798 USBDEVNAME(sc->sc_dev),
799 UGETDW(csw.dCSWDataResidue), n));
800 if (residue != NULL)
801 *residue = UGETDW(csw.dCSWDataResidue);
802 return(USBD_COMMAND_FAILED);
803 }
804
805 /*
806 * XXX a residue not equal to 0 might indicate that something
807 * is wrong. Does CAM high level drivers check this for us?
808 */
809
810 return(USBD_NORMAL_COMPLETION);
811 }
812
813
814 /*
815 * SCSIPI specific functions
816 */
817
818 int
819 umass_scsipi_scsi_cmd(xs)
820 struct scsipi_xfer *xs;
821 {
822 struct scsipi_link *sc_link = xs->sc_link;
823 struct umass_softc *sc = sc_link->adapter_softc;
824 int residue, dir;
825 usbd_status err;
826 struct scsipi_sense sense_cmd;
827
828 DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd %d:%d\n",
829 USBDEVNAME(sc->sc_dev),
830 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
831
832 if (sc->sc_dying) {
833 xs->xs_status |= XS_STS_DONE;
834 xs->error = XS_DRIVER_STUFFUP;
835 scsipi_done(xs);
836 if (xs->xs_control & XS_CTL_POLL)
837 return (SUCCESSFULLY_QUEUED);
838 else
839 return (COMPLETE);
840 }
841
842 #ifdef UMASS_DEBUG
843 if (sc_link->scsipi_scsi.target != UMASS_SCSIID_DEVICE) {
844 DPRINTF(UDMASS_SCSI, ("%s: Wrong SCSI ID %d\n",
845 USBDEVNAME(sc->sc_dev),
846 sc_link->scsipi_scsi.target));
847 xs->error = XS_DRIVER_STUFFUP;
848 return (COMPLETE);
849 }
850 #endif
851
852 dir = DIR_NONE;
853 if (xs->datalen) {
854 switch (xs->xs_control & (XS_CTL_DATA_IN|XS_CTL_DATA_OUT)) {
855 case XS_CTL_DATA_IN:
856 dir = DIR_IN;
857 break;
858 case XS_CTL_DATA_OUT:
859 dir = DIR_OUT;
860 break;
861 }
862 }
863
864 /* Make sure we don't lose our softc. */
865 sc->sc_refcnt++;
866
867 err = umass_bulk_transfer(sc, sc_link->scsipi_scsi.lun,
868 xs->cmd, xs->cmdlen, xs->data, xs->datalen, dir, &residue);
869
870 /*
871 * FAILED commands are supposed to be SCSI failed commands
872 * and are therefore considered to be successfull CDW/CSW
873 * transfers. PHASE errors are more serious and should return
874 * an error to the SCSIPI system.
875 *
876 * XXX This is however more based on empirical evidence than on
877 * hard proof from the Bulk-Only spec.
878 */
879 if (err == USBD_NORMAL_COMPLETION) {
880 xs->error = XS_NOERROR;
881 } else if (sc->sc_dying) {
882 /* We are being detached, no use talking to the device. */
883 xs->error = XS_DRIVER_STUFFUP;
884 } else {
885 DPRINTF(UDMASS_USB|UDMASS_SCSI, ("%s: bulk transfer completed "
886 "with error %s\n", USBDEVNAME(sc->sc_dev),
887 usbd_errstr(err)));
888
889 /*
890 * Probably have a CHECK CONDITION here. Issue a
891 * REQUEST SENSE.
892 */
893 memset(&sense_cmd, 0, sizeof(sense_cmd));
894 sense_cmd.opcode = REQUEST_SENSE;
895 sense_cmd.byte2 = sc_link->scsipi_scsi.lun <<
896 SCSI_CMD_LUN_SHIFT;
897 sense_cmd.length = sizeof(xs->sense);
898
899 if ((err = umass_bulk_transfer(sc, sc_link->scsipi_scsi.lun,
900 (struct scsipi_generic *)&sense_cmd, sizeof(sense_cmd),
901 &xs->sense, sizeof(xs->sense), DIR_IN, NULL)) !=
902 USBD_NORMAL_COMPLETION) {
903 DPRINTF(UDMASS_SCSI, ("%s: REQUEST SENSE failed: %s\n",
904 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
905 xs->error = XS_DRIVER_STUFFUP; /* XXX */
906 } else
907 xs->error = XS_SENSE;
908 }
909 xs->resid = residue;
910
911 DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd: error = %d, resid = 0x%x\n",
912 USBDEVNAME(sc->sc_dev), xs->error, xs->resid));
913
914 xs->xs_status |= XS_STS_DONE;
915 scsipi_done(xs);
916
917 /* We are done with the softc for now. */
918 if (--sc->sc_refcnt < 0)
919 usb_detach_wakeup(USBDEV(sc->sc_dev));
920
921 /*
922 * XXXJRT We must return successfully queued if we're an
923 * XXXJRT `asynchronous' command, otherwise `xs' will be
924 * XXXJRT freed twice: once in scsipi_done(), and once in
925 * XXXJRT scsi_scsipi_cmd().
926 */
927 if ((xs->xs_control & XS_CTL_POLL) == 0)
928 return (SUCCESSFULLY_QUEUED);
929
930 return (COMPLETE);
931 }
932
933 void
934 umass_scsipi_minphys(bp)
935 struct buf *bp;
936 {
937
938 /* No limit here. */
939 minphys(bp);
940 }
941