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