umass.c revision 1.99 1 /* $NetBSD: umass.c,v 1.99 2003/09/08 19:57:32 mycroft 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 specs:
33 * http://www.usb.org/developers/data/devclass/usbmassover_11.pdf
34 * http://www.usb.org/developers/data/devclass/usbmassbulk_10.pdf
35 * http://www.usb.org/developers/data/devclass/usbmass-cbi10.pdf
36 * http://www.usb.org/developers/data/devclass/usbmass-ufi10.pdf
37 */
38
39 /*
40 * Ported to NetBSD by Lennart Augustsson <augustss (at) netbsd.org>.
41 * Parts of the code written by Jason R. Thorpe <thorpej (at) shagadelic.org>.
42 */
43
44 /*
45 * The driver handles 3 Wire Protocols
46 * - Command/Bulk/Interrupt (CBI)
47 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
48 * - Mass Storage Bulk-Only (BBB)
49 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
50 *
51 * Over these wire protocols it handles the following command protocols
52 * - SCSI
53 * - 8070 (ATA/ATAPI for rewritable removable media)
54 * - UFI (USB Floppy Interface)
55 *
56 * 8070i is a transformed version of the SCSI command set. UFI is a transformed
57 * version of the 8070i command set. The sc->transform method is used to
58 * convert the commands into the appropriate format (if at all necessary).
59 * For example, ATAPI requires all commands to be 12 bytes in length amongst
60 * other things.
61 *
62 * The source code below is marked and can be split into a number of pieces
63 * (in this order):
64 *
65 * - probe/attach/detach
66 * - generic transfer routines
67 * - BBB
68 * - CBI
69 * - CBI_I (in addition to functions from CBI)
70 * - CAM (Common Access Method)
71 * - SCSI
72 * - UFI
73 * - 8070i
74 *
75 * The protocols are implemented using a state machine, for the transfers as
76 * well as for the resets. The state machine is contained in umass_*_state.
77 * The state machine is started through either umass_*_transfer or
78 * umass_*_reset.
79 *
80 * The reason for doing this is a) CAM performs a lot better this way and b) it
81 * avoids using tsleep from interrupt context (for example after a failed
82 * transfer).
83 */
84
85 /*
86 * The SCSI related part of this driver has been derived from the
87 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch (at) freebsd.org).
88 *
89 * The CAM layer uses so called actions which are messages sent to the host
90 * adapter for completion. The actions come in through umass_cam_action. The
91 * appropriate block of routines is called depending on the transport protocol
92 * in use. When the transfer has finished, these routines call
93 * umass_cam_cb again to complete the CAM command.
94 */
95
96 #include <sys/cdefs.h>
97 __KERNEL_RCSID(0, "$NetBSD: umass.c,v 1.99 2003/09/08 19:57:32 mycroft Exp $");
98
99 #include "atapibus.h"
100 #include "scsibus.h"
101 #include "wd.h"
102
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/conf.h>
107 #if defined(__NetBSD__) || defined(__OpenBSD__)
108 #include <sys/buf.h>
109 #include <sys/device.h>
110 #include <sys/malloc.h>
111 #undef KASSERT
112 #define KASSERT(cond, msg)
113 #elif defined(__FreeBSD__)
114 #include <sys/module.h>
115 #include <sys/bus.h>
116 #include <machine/clock.h>
117 #endif
118
119 #include <dev/usb/usb.h>
120 #include <dev/usb/usbdi.h>
121 #include <dev/usb/usbdi_util.h>
122 #include <dev/usb/usbdevs.h>
123
124 #include <dev/usb/umassvar.h>
125 #include <dev/usb/umass_quirks.h>
126 #include <dev/usb/umass_scsipi.h>
127 #include <dev/usb/umass_isdata.h>
128
129 #include <dev/scsipi/scsipi_all.h>
130 #include <dev/scsipi/scsipiconf.h>
131
132
133 #ifdef UMASS_DEBUG
134 int umassdebug = 0;
135
136 char *states[TSTATE_STATES+1] = {
137 /* should be kept in sync with the list at transfer_state */
138 "Idle",
139 "BBB CBW",
140 "BBB Data",
141 "BBB Data bulk-in/-out clear stall",
142 "BBB CSW, 1st attempt",
143 "BBB CSW bulk-in clear stall",
144 "BBB CSW, 2nd attempt",
145 "BBB Reset",
146 "BBB bulk-in clear stall",
147 "BBB bulk-out clear stall",
148 "CBI Command",
149 "CBI Data",
150 "CBI Status",
151 "CBI Data bulk-in/-out clear stall",
152 "CBI Status intr-in clear stall",
153 "CBI Reset",
154 "CBI bulk-in clear stall",
155 "CBI bulk-out clear stall",
156 NULL
157 };
158 #endif
159
160 /* USB device probe/attach/detach functions */
161 USB_DECLARE_DRIVER(umass);
162 Static void umass_disco(struct umass_softc *sc);
163
164 /* generic transfer functions */
165 Static usbd_status umass_setup_transfer(struct umass_softc *sc,
166 usbd_pipe_handle pipe,
167 void *buffer, int buflen, int flags,
168 usbd_xfer_handle xfer);
169 Static usbd_status umass_setup_ctrl_transfer(struct umass_softc *sc,
170 usb_device_request_t *req,
171 void *buffer, int buflen, int flags,
172 usbd_xfer_handle xfer);
173 Static void umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
174 usbd_xfer_handle xfer);
175 #if 0
176 Static void umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv);
177 #endif
178
179 /* Bulk-Only related functions */
180 Static void umass_bbb_transfer(struct umass_softc *, int, void *, int, void *,
181 int, int, u_int, umass_callback, void *);
182 Static void umass_bbb_reset(struct umass_softc *, int);
183 Static void umass_bbb_state(usbd_xfer_handle, usbd_private_handle, usbd_status);
184
185 usbd_status umass_bbb_get_max_lun(struct umass_softc *, u_int8_t *);
186
187 /* CBI related functions */
188 Static void umass_cbi_transfer(struct umass_softc *, int, void *, int, void *,
189 int, int, u_int, umass_callback, void *);
190 Static void umass_cbi_reset(struct umass_softc *, int);
191 Static void umass_cbi_state(usbd_xfer_handle, usbd_private_handle, usbd_status);
192
193 Static int umass_cbi_adsc(struct umass_softc *, char *, int, usbd_xfer_handle);
194
195 const struct umass_wire_methods umass_bbb_methods = {
196 umass_bbb_transfer,
197 umass_bbb_reset,
198 umass_bbb_state
199 };
200
201 const struct umass_wire_methods umass_cbi_methods = {
202 umass_cbi_transfer,
203 umass_cbi_reset,
204 umass_cbi_state
205 };
206
207 #ifdef UMASS_DEBUG
208 /* General debugging functions */
209 Static void umass_bbb_dump_cbw(struct umass_softc *sc,
210 umass_bbb_cbw_t *cbw);
211 Static void umass_bbb_dump_csw(struct umass_softc *sc,
212 umass_bbb_csw_t *csw);
213 Static void umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer,
214 int buflen, int printlen);
215 #endif
216
217
218 /*
219 * USB device probe/attach/detach
220 */
221
222 USB_MATCH(umass)
223 {
224 USB_MATCH_START(umass, uaa);
225 const struct umass_quirk *quirk;
226 usb_interface_descriptor_t *id;
227
228 if (uaa->iface == NULL)
229 return (UMATCH_NONE);
230
231 quirk = umass_lookup(uaa->vendor, uaa->product);
232 if (quirk != NULL)
233 return (quirk->uq_match);
234
235 id = usbd_get_interface_descriptor(uaa->iface);
236 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
237 return (UMATCH_NONE);
238
239 switch (id->bInterfaceSubClass) {
240 case UISUBCLASS_RBC:
241 case UISUBCLASS_SFF8020I:
242 case UISUBCLASS_QIC157:
243 case UISUBCLASS_UFI:
244 case UISUBCLASS_SFF8070I:
245 case UISUBCLASS_SCSI:
246 break;
247 default:
248 return (UMATCH_IFACECLASS);
249 }
250
251 switch (id->bInterfaceProtocol) {
252 case UIPROTO_MASS_CBI_I:
253 case UIPROTO_MASS_CBI:
254 case UIPROTO_MASS_BBB_OLD:
255 case UIPROTO_MASS_BBB:
256 break;
257 default:
258 return (UMATCH_IFACECLASS_IFACESUBCLASS);
259 }
260
261 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
262 }
263
264 USB_ATTACH(umass)
265 {
266 USB_ATTACH_START(umass, sc, uaa);
267 const struct umass_quirk *quirk;
268 usb_interface_descriptor_t *id;
269 usb_endpoint_descriptor_t *ed;
270 const char *sWire, *sCommand;
271 char devinfo[1024];
272 usbd_status err;
273 int i, bno, error;
274
275 usbd_devinfo(uaa->device, 0, devinfo);
276 USB_ATTACH_SETUP;
277
278 sc->sc_udev = uaa->device;
279 sc->sc_iface = uaa->iface;
280 sc->sc_ifaceno = uaa->ifaceno;
281
282 quirk = umass_lookup(uaa->vendor, uaa->product);
283 if (quirk != NULL) {
284 sc->sc_wire = quirk->uq_wire;
285 sc->sc_cmd = quirk->uq_cmd;
286 sc->sc_quirks = quirk->uq_flags;
287 sc->sc_busquirks = quirk->uq_busquirks;
288
289 if (quirk->uq_fixup != NULL)
290 (*quirk->uq_fixup)(sc);
291 } else {
292 sc->sc_wire = UMASS_WPROTO_UNSPEC;
293 sc->sc_cmd = UMASS_CPROTO_UNSPEC;
294 sc->sc_quirks = 0;
295 sc->sc_busquirks = 0;
296 }
297
298 id = usbd_get_interface_descriptor(sc->sc_iface);
299 if (id == NULL)
300 USB_ATTACH_ERROR_RETURN;
301
302 if (sc->sc_wire == UMASS_WPROTO_UNSPEC) {
303 switch (id->bInterfaceProtocol) {
304 case UIPROTO_MASS_CBI:
305 sc->sc_wire = UMASS_WPROTO_CBI;
306 break;
307 case UIPROTO_MASS_CBI_I:
308 sc->sc_wire = UMASS_WPROTO_CBI_I;
309 break;
310 case UIPROTO_MASS_BBB:
311 case UIPROTO_MASS_BBB_OLD:
312 sc->sc_wire = UMASS_WPROTO_BBB;
313 break;
314 default:
315 DPRINTF(UDMASS_GEN,
316 ("%s: Unsupported wire protocol %u\n",
317 USBDEVNAME(sc->sc_dev),
318 id->bInterfaceProtocol));
319 USB_ATTACH_ERROR_RETURN;
320 }
321 }
322
323 /* XXX - Now unsupported CBI with CCI */
324 if (sc->sc_wire == UMASS_WPROTO_CBI_I)
325 sc->sc_wire = UMASS_WPROTO_CBI;
326
327 if (sc->sc_cmd == UMASS_CPROTO_UNSPEC) {
328 switch (id->bInterfaceSubClass) {
329 case UISUBCLASS_SCSI:
330 sc->sc_cmd = UMASS_CPROTO_SCSI;
331 break;
332 case UISUBCLASS_UFI:
333 sc->sc_cmd = UMASS_CPROTO_UFI;
334 break;
335 case UISUBCLASS_SFF8020I:
336 case UISUBCLASS_SFF8070I:
337 case UISUBCLASS_QIC157:
338 sc->sc_cmd = UMASS_CPROTO_ATAPI;
339 break;
340 case UISUBCLASS_RBC:
341 sc->sc_cmd = UMASS_CPROTO_RBC;
342 break;
343 default:
344 DPRINTF(UDMASS_GEN,
345 ("%s: Unsupported command protocol %u\n",
346 USBDEVNAME(sc->sc_dev),
347 id->bInterfaceSubClass));
348 USB_ATTACH_ERROR_RETURN;
349 }
350 }
351
352 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
353
354 switch (sc->sc_wire) {
355 case UMASS_WPROTO_CBI:
356 sWire = "CBI";
357 break;
358 case UMASS_WPROTO_CBI_I:
359 sWire = "CBI with CCI";
360 break;
361 case UMASS_WPROTO_BBB:
362 sWire = "Bulk-Only";
363 break;
364 default:
365 sWire = "unknown";
366 break;
367 }
368
369 switch (sc->sc_cmd) {
370 case UMASS_CPROTO_RBC:
371 sCommand = "RBC";
372 break;
373 case UMASS_CPROTO_SCSI:
374 sCommand = "SCSI";
375 break;
376 case UMASS_CPROTO_UFI:
377 sCommand = "UFI";
378 break;
379 case UMASS_CPROTO_ATAPI:
380 sCommand = "ATAPI";
381 break;
382 case UMASS_CPROTO_ISD_ATA:
383 sCommand = "ISD-ATA";
384 break;
385 default:
386 sCommand = "unknown";
387 break;
388 }
389
390 printf("%s: using %s over %s\n", USBDEVNAME(sc->sc_dev), sCommand,
391 sWire);
392
393 /*
394 * In addition to the Control endpoint the following endpoints
395 * are required:
396 * a) bulk-in endpoint.
397 * b) bulk-out endpoint.
398 * and for Control/Bulk/Interrupt with CCI (CBI_I)
399 * c) intr-in
400 *
401 * The endpoint addresses are not fixed, so we have to read them
402 * from the device descriptors of the current interface.
403 */
404 for (i = 0 ; i < id->bNumEndpoints ; i++) {
405 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
406 if (ed == NULL) {
407 printf("%s: could not read endpoint descriptor\n",
408 USBDEVNAME(sc->sc_dev));
409 USB_ATTACH_ERROR_RETURN;
410 }
411 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
412 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
413 sc->sc_epaddr[UMASS_BULKIN] = ed->bEndpointAddress;
414 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
415 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
416 sc->sc_epaddr[UMASS_BULKOUT] = ed->bEndpointAddress;
417 } else if (sc->sc_wire == UMASS_WPROTO_CBI_I
418 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
419 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
420 sc->sc_epaddr[UMASS_INTRIN] = ed->bEndpointAddress;
421 #ifdef UMASS_DEBUG
422 if (UGETW(ed->wMaxPacketSize) > 2) {
423 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
424 USBDEVNAME(sc->sc_dev),
425 UGETW(ed->wMaxPacketSize)));
426 }
427 #endif
428 }
429 }
430
431 /* check whether we found all the endpoints we need */
432 if (!sc->sc_epaddr[UMASS_BULKIN] || !sc->sc_epaddr[UMASS_BULKOUT] ||
433 (sc->sc_wire == UMASS_WPROTO_CBI_I &&
434 !sc->sc_epaddr[UMASS_INTRIN])) {
435 DPRINTF(UDMASS_USB, ("%s: endpoint not found %u/%u/%u\n",
436 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKIN],
437 sc->sc_epaddr[UMASS_BULKOUT],
438 sc->sc_epaddr[UMASS_INTRIN]));
439 USB_ATTACH_ERROR_RETURN;
440 }
441
442 /*
443 * Get the maximum LUN supported by the device.
444 */
445 if (sc->sc_wire == UMASS_WPROTO_BBB) {
446 err = umass_bbb_get_max_lun(sc, &sc->maxlun);
447 if (err) {
448 printf("%s: unable to get Max Lun: %s\n",
449 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
450 USB_ATTACH_ERROR_RETURN;
451 }
452 if (sc->maxlun > 0)
453 sc->sc_busquirks |= PQUIRK_FORCELUNS;
454 } else {
455 sc->maxlun = 0;
456 }
457
458 /* Open the bulk-in and -out pipe */
459 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKOUT],
460 USBD_EXCLUSIVE_USE,
461 &sc->sc_pipe[UMASS_BULKOUT]);
462 if (err) {
463 DPRINTF(UDMASS_USB, ("%s: cannot open %u-out pipe (bulk)\n",
464 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKOUT]));
465 umass_disco(sc);
466 USB_ATTACH_ERROR_RETURN;
467 }
468 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKIN],
469 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_BULKIN]);
470 if (err) {
471 DPRINTF(UDMASS_USB, ("%s: could not open %u-in pipe (bulk)\n",
472 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKIN]));
473 umass_disco(sc);
474 USB_ATTACH_ERROR_RETURN;
475 }
476 /*
477 * Open the intr-in pipe if the protocol is CBI with CCI.
478 * Note: early versions of the Zip drive do have an interrupt pipe, but
479 * this pipe is unused
480 *
481 * We do not open the interrupt pipe as an interrupt pipe, but as a
482 * normal bulk endpoint. We send an IN transfer down the wire at the
483 * appropriate time, because we know exactly when to expect data on
484 * that endpoint. This saves bandwidth, but more important, makes the
485 * code for handling the data on that endpoint simpler. No data
486 * arriving concurrently.
487 */
488 if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
489 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_INTRIN],
490 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_INTRIN]);
491 if (err) {
492 DPRINTF(UDMASS_USB, ("%s: couldn't open %u-in (intr)\n",
493 USBDEVNAME(sc->sc_dev),
494 sc->sc_epaddr[UMASS_INTRIN]));
495 umass_disco(sc);
496 USB_ATTACH_ERROR_RETURN;
497 }
498 }
499
500 /* initialisation of generic part */
501 sc->transfer_state = TSTATE_IDLE;
502
503 /* request a sufficient number of xfer handles */
504 for (i = 0; i < XFER_NR; i++) {
505 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
506 if (sc->transfer_xfer[i] == NULL) {
507 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
508 USBDEVNAME(sc->sc_dev)));
509 umass_disco(sc);
510 USB_ATTACH_ERROR_RETURN;
511 }
512 }
513 /* Allocate buffer for data transfer (it's huge). */
514 switch (sc->sc_wire) {
515 case UMASS_WPROTO_BBB:
516 bno = XFER_BBB_DATA;
517 goto dalloc;
518 case UMASS_WPROTO_CBI:
519 bno = XFER_CBI_DATA;
520 goto dalloc;
521 case UMASS_WPROTO_CBI_I:
522 bno = XFER_CBI_DATA;
523 dalloc:
524 sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno],
525 UMASS_MAX_TRANSFER_SIZE);
526 if (sc->data_buffer == NULL) {
527 umass_disco(sc);
528 USB_ATTACH_ERROR_RETURN;
529 }
530 break;
531 default:
532 break;
533 }
534
535 /* Initialise the wire protocol specific methods */
536 switch (sc->sc_wire) {
537 case UMASS_WPROTO_BBB:
538 sc->sc_methods = &umass_bbb_methods;
539 break;
540 case UMASS_WPROTO_CBI:
541 case UMASS_WPROTO_CBI_I:
542 sc->sc_methods = &umass_cbi_methods;
543 break;
544 default:
545 umass_disco(sc);
546 USB_ATTACH_ERROR_RETURN;
547 }
548
549 if (quirk != NULL && quirk->uq_init != NULL) {
550 err = (*quirk->uq_init)(sc);
551 if (err) {
552 umass_disco(sc);
553 USB_ATTACH_ERROR_RETURN;
554 }
555 }
556
557 error = 0;
558 switch (sc->sc_cmd) {
559 case UMASS_CPROTO_RBC:
560 case UMASS_CPROTO_SCSI:
561 #if NSCSIBUS > 0
562 error = umass_scsi_attach(sc);
563 #else
564 printf("%s: scsibus not configured\n", USBDEVNAME(sc->sc_dev));
565 #endif
566 break;
567
568 case UMASS_CPROTO_UFI:
569 case UMASS_CPROTO_ATAPI:
570 #if NATAPIBUS > 0
571 error = umass_atapi_attach(sc);
572 #else
573 printf("%s: atapibus not configured\n",
574 USBDEVNAME(sc->sc_dev));
575 #endif
576 break;
577
578 case UMASS_CPROTO_ISD_ATA:
579 #if NWD > 0
580 error = umass_isdata_attach(sc);
581 #else
582 printf("%s: isdata not configured\n", USBDEVNAME(sc->sc_dev));
583 #endif
584 break;
585
586 default:
587 printf("%s: command protocol=0x%x not supported\n",
588 USBDEVNAME(sc->sc_dev), sc->sc_cmd);
589 umass_disco(sc);
590 USB_ATTACH_ERROR_RETURN;
591 }
592 if (error) {
593 printf("%s: bus attach failed\n", USBDEVNAME(sc->sc_dev));
594 umass_disco(sc);
595 USB_ATTACH_ERROR_RETURN;
596 }
597
598 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
599 USBDEV(sc->sc_dev));
600
601 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev)));
602
603 USB_ATTACH_SUCCESS_RETURN;
604 }
605
606 USB_DETACH(umass)
607 {
608 USB_DETACH_START(umass, sc);
609 struct umassbus_softc *scbus = sc->bus;
610 int rv = 0, i, s;
611
612 DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev)));
613
614 /* Abort the pipes to wake up any waiting processes. */
615 for (i = 0 ; i < UMASS_NEP ; i++) {
616 if (sc->sc_pipe[i] != NULL)
617 usbd_abort_pipe(sc->sc_pipe[i]);
618 }
619
620 /* Do we really need reference counting? Perhaps in ioctl() */
621 s = splusb();
622 if (--sc->sc_refcnt >= 0) {
623 #ifdef DIAGNOSTIC
624 printf("%s: waiting for refcnt\n", USBDEVNAME(sc->sc_dev));
625 #endif
626 /* Wait for processes to go away. */
627 usb_detach_wait(USBDEV(sc->sc_dev));
628 }
629 splx(s);
630
631 if (scbus != NULL) {
632 if (scbus->sc_child != NULL)
633 rv = config_detach(scbus->sc_child, flags);
634 free(scbus, M_DEVBUF);
635 sc->bus = NULL;
636 }
637
638 if (rv != 0)
639 return (rv);
640
641 umass_disco(sc);
642
643 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
644 USBDEV(sc->sc_dev));
645
646 return (rv);
647 }
648
649 int
650 umass_activate(struct device *dev, enum devact act)
651 {
652 struct umass_softc *sc = (struct umass_softc *)dev;
653 struct umassbus_softc *scbus = sc->bus;
654 int rv = 0;
655
656 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
657 USBDEVNAME(sc->sc_dev), act));
658
659 switch (act) {
660 case DVACT_ACTIVATE:
661 rv = EOPNOTSUPP;
662 break;
663
664 case DVACT_DEACTIVATE:
665 sc->sc_dying = 1;
666 if (scbus == NULL || scbus->sc_child == NULL)
667 break;
668 rv = config_deactivate(scbus->sc_child);
669 DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
670 "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
671 break;
672 }
673 return (rv);
674 }
675
676 Static void
677 umass_disco(struct umass_softc *sc)
678 {
679 int i;
680
681 DPRINTF(UDMASS_GEN, ("umass_disco\n"));
682
683 /* Free the xfers. */
684 for (i = 0; i < XFER_NR; i++)
685 if (sc->transfer_xfer[i] != NULL) {
686 usbd_free_xfer(sc->transfer_xfer[i]);
687 sc->transfer_xfer[i] = NULL;
688 }
689
690 /* Remove all the pipes. */
691 for (i = 0 ; i < UMASS_NEP ; i++) {
692 if (sc->sc_pipe[i] != NULL) {
693 usbd_close_pipe(sc->sc_pipe[i]);
694 sc->sc_pipe[i] = NULL;
695 }
696 }
697 }
698
699 /*
700 * Generic functions to handle transfers
701 */
702
703 Static usbd_status
704 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
705 void *buffer, int buflen, int flags,
706 usbd_xfer_handle xfer)
707 {
708 usbd_status err;
709
710 if (sc->sc_dying)
711 return (USBD_IOERROR);
712
713 /* Initialiase a USB transfer and then schedule it */
714
715 usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen,
716 flags | sc->sc_xfer_flags, sc->timeout, sc->sc_methods->wire_state);
717
718 err = usbd_transfer(xfer);
719 DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d flags=0x%x "
720 "timeout=%d\n", USBDEVNAME(sc->sc_dev),
721 buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout));
722 if (err && err != USBD_IN_PROGRESS) {
723 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
724 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
725 return (err);
726 }
727
728 return (USBD_NORMAL_COMPLETION);
729 }
730
731
732 Static usbd_status
733 umass_setup_ctrl_transfer(struct umass_softc *sc, usb_device_request_t *req,
734 void *buffer, int buflen, int flags, usbd_xfer_handle xfer)
735 {
736 usbd_status err;
737
738 if (sc->sc_dying)
739 return (USBD_IOERROR);
740
741 /* Initialiase a USB control transfer and then schedule it */
742
743 usbd_setup_default_xfer(xfer, sc->sc_udev, (void *) sc, sc->timeout,
744 req, buffer, buflen, flags, sc->sc_methods->wire_state);
745
746 err = usbd_transfer(xfer);
747 if (err && err != USBD_IN_PROGRESS) {
748 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
749 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
750
751 /* do not reset, as this would make us loop */
752 return (err);
753 }
754
755 return (USBD_NORMAL_COMPLETION);
756 }
757
758 Static void
759 umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
760 usbd_xfer_handle xfer)
761 {
762 if (sc->sc_dying)
763 return;
764
765 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
766 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[endpt]));
767
768 usbd_clear_endpoint_toggle(sc->sc_pipe[endpt]);
769
770 sc->sc_req.bmRequestType = UT_WRITE_ENDPOINT;
771 sc->sc_req.bRequest = UR_CLEAR_FEATURE;
772 USETW(sc->sc_req.wValue, UF_ENDPOINT_HALT);
773 USETW(sc->sc_req.wIndex, sc->sc_epaddr[endpt]);
774 USETW(sc->sc_req.wLength, 0);
775 umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0, xfer);
776 }
777
778 #if 0
779 Static void
780 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
781 {
782 sc->transfer_cb = cb;
783 sc->transfer_priv = priv;
784
785 /* The reset is a forced reset, so no error (yet) */
786 sc->reset(sc, STATUS_CMD_OK);
787 }
788 #endif
789
790 /*
791 * Bulk protocol specific functions
792 */
793
794 Static void
795 umass_bbb_reset(struct umass_softc *sc, int status)
796 {
797 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
798 ("sc->sc_wire == 0x%02x wrong for umass_bbb_reset\n",
799 sc->sc_wire));
800
801 if (sc->sc_dying)
802 return;
803
804 /*
805 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
806 *
807 * For Reset Recovery the host shall issue in the following order:
808 * a) a Bulk-Only Mass Storage Reset
809 * b) a Clear Feature HALT to the Bulk-In endpoint
810 * c) a Clear Feature HALT to the Bulk-Out endpoint
811 *
812 * This is done in 3 steps, states:
813 * TSTATE_BBB_RESET1
814 * TSTATE_BBB_RESET2
815 * TSTATE_BBB_RESET3
816 *
817 * If the reset doesn't succeed, the device should be port reset.
818 */
819
820 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
821 USBDEVNAME(sc->sc_dev)));
822
823 sc->transfer_state = TSTATE_BBB_RESET1;
824 sc->transfer_status = status;
825
826 /* reset is a class specific interface write */
827 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
828 sc->sc_req.bRequest = UR_BBB_RESET;
829 USETW(sc->sc_req.wValue, 0);
830 USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
831 USETW(sc->sc_req.wLength, 0);
832 umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0,
833 sc->transfer_xfer[XFER_BBB_RESET1]);
834 }
835
836 Static void
837 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
838 void *data, int datalen, int dir, u_int timeout,
839 umass_callback cb, void *priv)
840 {
841 static int dCBWtag = 42; /* unique for CBW of transfer */
842
843 DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n",
844 USBDEVNAME(sc->sc_dev), *(u_char *)cmd));
845
846 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
847 ("sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n",
848 sc->sc_wire));
849
850 /* Be a little generous. */
851 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
852
853 /*
854 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
855 * a data phase of datalen bytes from/to the device and finally a
856 * csw read phase.
857 * If the data direction was inbound a maximum of datalen bytes
858 * is stored in the buffer pointed to by data.
859 *
860 * umass_bbb_transfer initialises the transfer and lets the state
861 * machine in umass_bbb_state handle the completion. It uses the
862 * following states:
863 * TSTATE_BBB_COMMAND
864 * -> TSTATE_BBB_DATA
865 * -> TSTATE_BBB_STATUS
866 * -> TSTATE_BBB_STATUS2
867 * -> TSTATE_BBB_IDLE
868 *
869 * An error in any of those states will invoke
870 * umass_bbb_reset.
871 */
872
873 /* check the given arguments */
874 KASSERT(datalen == 0 || data != NULL,
875 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
876 KASSERT(cmdlen <= CBWCDBLENGTH,
877 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
878 USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH));
879 KASSERT(dir == DIR_NONE || datalen > 0,
880 ("%s: datalen == 0 while direction is not NONE\n",
881 USBDEVNAME(sc->sc_dev)));
882 KASSERT(datalen == 0 || dir != DIR_NONE,
883 ("%s: direction is NONE while datalen is not zero\n",
884 USBDEVNAME(sc->sc_dev)));
885 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
886 ("%s: CBW struct does not have the right size (%d vs. %d)\n",
887 USBDEVNAME(sc->sc_dev),
888 sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
889 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
890 ("%s: CSW struct does not have the right size (%d vs. %d)\n",
891 USBDEVNAME(sc->sc_dev),
892 sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
893
894 /*
895 * Determine the direction of the data transfer and the length.
896 *
897 * dCBWDataTransferLength (datalen) :
898 * This field indicates the number of bytes of data that the host
899 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
900 * the Direction bit) during the execution of this command. If this
901 * field is set to 0, the device will expect that no data will be
902 * transferred IN or OUT during this command, regardless of the value
903 * of the Direction bit defined in dCBWFlags.
904 *
905 * dCBWFlags (dir) :
906 * The bits of the Flags field are defined as follows:
907 * Bits 0-6 reserved
908 * Bit 7 Direction - this bit shall be ignored if the
909 * dCBWDataTransferLength field is zero.
910 * 0 = data Out from host to device
911 * 1 = data In from device to host
912 */
913
914 /* Fill in the Command Block Wrapper */
915 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
916 USETDW(sc->cbw.dCBWTag, dCBWtag);
917 dCBWtag++; /* cannot be done in macro (it will be done 4 times) */
918 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
919 /* DIR_NONE is treated as DIR_OUT (0x00) */
920 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
921 sc->cbw.bCBWLUN = lun;
922 sc->cbw.bCDBLength = cmdlen;
923 memcpy(sc->cbw.CBWCDB, cmd, cmdlen);
924
925 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
926
927 /* store the details for the data transfer phase */
928 sc->transfer_dir = dir;
929 sc->transfer_data = data;
930 sc->transfer_datalen = datalen;
931 sc->transfer_actlen = 0;
932 sc->transfer_cb = cb;
933 sc->transfer_priv = priv;
934 sc->transfer_status = STATUS_CMD_OK;
935
936 /* move from idle to the command state */
937 sc->transfer_state = TSTATE_BBB_COMMAND;
938
939 /* Send the CBW from host to device via bulk-out endpoint. */
940 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
941 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
942 sc->transfer_xfer[XFER_BBB_CBW])) {
943 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
944 }
945 }
946
947
948 Static void
949 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
950 usbd_status err)
951 {
952 struct umass_softc *sc = (struct umass_softc *) priv;
953 usbd_xfer_handle next_xfer;
954
955 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
956 ("sc->sc_wire == 0x%02x wrong for umass_bbb_state\n",
957 sc->sc_wire));
958
959 if (sc->sc_dying)
960 return;
961
962 /*
963 * State handling for BBB transfers.
964 *
965 * The subroutine is rather long. It steps through the states given in
966 * Annex A of the Bulk-Only specification.
967 * Each state first does the error handling of the previous transfer
968 * and then prepares the next transfer.
969 * Each transfer is done asynchroneously so after the request/transfer
970 * has been submitted you will find a 'return;'.
971 */
972
973 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
974 USBDEVNAME(sc->sc_dev), sc->transfer_state,
975 states[sc->transfer_state], xfer, usbd_errstr(err)));
976
977 switch (sc->transfer_state) {
978
979 /***** Bulk Transfer *****/
980 case TSTATE_BBB_COMMAND:
981 /* Command transport phase, error handling */
982 if (err) {
983 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
984 USBDEVNAME(sc->sc_dev)));
985 /* If the device detects that the CBW is invalid, then
986 * the device may STALL both bulk endpoints and require
987 * a Bulk-Reset
988 */
989 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
990 return;
991 }
992
993 /* Data transport phase, setup transfer */
994 sc->transfer_state = TSTATE_BBB_DATA;
995 if (sc->transfer_dir == DIR_IN) {
996 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
997 sc->data_buffer, sc->transfer_datalen,
998 USBD_SHORT_XFER_OK | USBD_NO_COPY,
999 sc->transfer_xfer[XFER_BBB_DATA]))
1000 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1001
1002 return;
1003 } else if (sc->transfer_dir == DIR_OUT) {
1004 memcpy(sc->data_buffer, sc->transfer_data,
1005 sc->transfer_datalen);
1006 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1007 sc->data_buffer, sc->transfer_datalen,
1008 USBD_NO_COPY,/* fixed length transfer */
1009 sc->transfer_xfer[XFER_BBB_DATA]))
1010 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1011
1012 return;
1013 } else {
1014 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1015 USBDEVNAME(sc->sc_dev)));
1016 }
1017
1018 /* FALLTHROUGH if no data phase, err == 0 */
1019 case TSTATE_BBB_DATA:
1020 /* Command transport phase, error handling (ignored if no data
1021 * phase (fallthrough from previous state)) */
1022 if (sc->transfer_dir != DIR_NONE) {
1023 /* retrieve the length of the transfer that was done */
1024 usbd_get_xfer_status(xfer, NULL, NULL,
1025 &sc->transfer_actlen, NULL);
1026
1027 if (err) {
1028 DPRINTF(UDMASS_BBB, ("%s: Data-%s %d failed, "
1029 "%s\n", USBDEVNAME(sc->sc_dev),
1030 (sc->transfer_dir == DIR_IN?"in":"out"),
1031 sc->transfer_datalen,usbd_errstr(err)));
1032
1033 if (err == USBD_STALLED) {
1034 sc->transfer_state = TSTATE_BBB_DCLEAR;
1035 umass_clear_endpoint_stall(sc,
1036 (sc->transfer_dir == DIR_IN?
1037 UMASS_BULKIN:UMASS_BULKOUT),
1038 sc->transfer_xfer[XFER_BBB_DCLEAR]);
1039 return;
1040 } else {
1041 /* Unless the error is a pipe stall the
1042 * error is fatal.
1043 */
1044 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1045 return;
1046 }
1047 }
1048 }
1049
1050 if (sc->transfer_dir == DIR_IN)
1051 memcpy(sc->transfer_data, sc->data_buffer,
1052 sc->transfer_actlen);
1053
1054 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1055 umass_dump_buffer(sc, sc->transfer_data,
1056 sc->transfer_datalen, 48));
1057
1058 /* FALLTHROUGH, err == 0 (no data phase or successful) */
1059 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1060 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1061 /* Reading of CSW after bulk stall condition in data phase
1062 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1063 * reading CSW (TSTATE_BBB_SCLEAR).
1064 * In the case of no data phase or successful data phase,
1065 * err == 0 and the following if block is passed.
1066 */
1067 if (err) { /* should not occur */
1068 /* try the transfer below, even if clear stall failed */
1069 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1070 ", %s\n", USBDEVNAME(sc->sc_dev),
1071 (sc->transfer_dir == DIR_IN? "in":"out"),
1072 usbd_errstr(err)));
1073 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1074 return;
1075 }
1076
1077 /* Status transport phase, setup transfer */
1078 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1079 sc->transfer_state == TSTATE_BBB_DATA ||
1080 sc->transfer_state == TSTATE_BBB_DCLEAR) {
1081 /* After no data phase, successful data phase and
1082 * after clearing bulk-in/-out stall condition
1083 */
1084 sc->transfer_state = TSTATE_BBB_STATUS1;
1085 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1086 } else {
1087 /* After first attempt of fetching CSW */
1088 sc->transfer_state = TSTATE_BBB_STATUS2;
1089 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1090 }
1091
1092 /* Read the Command Status Wrapper via bulk-in endpoint. */
1093 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1094 &sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
1095 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1096 return;
1097 }
1098
1099 return;
1100 case TSTATE_BBB_STATUS1: /* first attempt */
1101 case TSTATE_BBB_STATUS2: /* second attempt */
1102 /* Status transfer, error handling */
1103 if (err) {
1104 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1105 USBDEVNAME(sc->sc_dev), usbd_errstr(err),
1106 (sc->transfer_state == TSTATE_BBB_STATUS1?
1107 ", retrying":"")));
1108
1109 /* If this was the first attempt at fetching the CSW
1110 * retry it, otherwise fail.
1111 */
1112 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1113 sc->transfer_state = TSTATE_BBB_SCLEAR;
1114 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1115 sc->transfer_xfer[XFER_BBB_SCLEAR]);
1116 return;
1117 } else {
1118 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1119 return;
1120 }
1121 }
1122
1123 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1124
1125 /* Translate weird command-status signatures. */
1126 if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) &&
1127 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
1128 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1129
1130 /* Translate invalid command-status tags */
1131 if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG)
1132 USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag));
1133
1134 /* Check CSW and handle any error */
1135 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1136 /* Invalid CSW: Wrong signature or wrong tag might
1137 * indicate that the device is confused -> reset it.
1138 */
1139 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1140 USBDEVNAME(sc->sc_dev),
1141 UGETDW(sc->csw.dCSWSignature),
1142 CSWSIGNATURE);
1143
1144 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1145 return;
1146 } else if (UGETDW(sc->csw.dCSWTag)
1147 != UGETDW(sc->cbw.dCBWTag)) {
1148 printf("%s: Invalid CSW: tag %d should be %d\n",
1149 USBDEVNAME(sc->sc_dev),
1150 UGETDW(sc->csw.dCSWTag),
1151 UGETDW(sc->cbw.dCBWTag));
1152
1153 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1154 return;
1155
1156 /* CSW is valid here */
1157 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1158 printf("%s: Invalid CSW: status %d > %d\n",
1159 USBDEVNAME(sc->sc_dev),
1160 sc->csw.bCSWStatus,
1161 CSWSTATUS_PHASE);
1162
1163 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1164 return;
1165 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1166 printf("%s: Phase Error, residue = %d\n",
1167 USBDEVNAME(sc->sc_dev),
1168 UGETDW(sc->csw.dCSWDataResidue));
1169
1170 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1171 return;
1172
1173 } else if (sc->transfer_actlen > sc->transfer_datalen) {
1174 /* Buffer overrun! Don't let this go by unnoticed */
1175 panic("%s: transferred %d bytes instead of %d bytes",
1176 USBDEVNAME(sc->sc_dev),
1177 sc->transfer_actlen, sc->transfer_datalen);
1178 #if 0
1179 } else if (sc->transfer_datalen - sc->transfer_actlen
1180 != UGETDW(sc->csw.dCSWDataResidue)) {
1181 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1182 USBDEVNAME(sc->sc_dev),
1183 sc->transfer_datalen - sc->transfer_actlen,
1184 UGETDW(sc->csw.dCSWDataResidue)));
1185
1186 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1187 return;
1188 #endif
1189 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1190 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1191 USBDEVNAME(sc->sc_dev),
1192 UGETDW(sc->csw.dCSWDataResidue)));
1193
1194 /* SCSI command failed but transfer was succesful */
1195 sc->transfer_state = TSTATE_IDLE;
1196 sc->transfer_cb(sc, sc->transfer_priv,
1197 UGETDW(sc->csw.dCSWDataResidue),
1198 STATUS_CMD_FAILED);
1199
1200 return;
1201
1202 } else { /* success */
1203 sc->transfer_state = TSTATE_IDLE;
1204 sc->transfer_cb(sc, sc->transfer_priv,
1205 UGETDW(sc->csw.dCSWDataResidue),
1206 STATUS_CMD_OK);
1207
1208 return;
1209 }
1210
1211 /***** Bulk Reset *****/
1212 case TSTATE_BBB_RESET1:
1213 if (err)
1214 printf("%s: BBB reset failed, %s\n",
1215 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1216
1217 sc->transfer_state = TSTATE_BBB_RESET2;
1218 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1219 sc->transfer_xfer[XFER_BBB_RESET2]);
1220
1221 return;
1222 case TSTATE_BBB_RESET2:
1223 if (err) /* should not occur */
1224 printf("%s: BBB bulk-in clear stall failed, %s\n",
1225 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1226 /* no error recovery, otherwise we end up in a loop */
1227
1228 sc->transfer_state = TSTATE_BBB_RESET3;
1229 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1230 sc->transfer_xfer[XFER_BBB_RESET3]);
1231
1232 return;
1233 case TSTATE_BBB_RESET3:
1234 if (err) /* should not occur */
1235 printf("%s: BBB bulk-out clear stall failed, %s\n",
1236 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1237 /* no error recovery, otherwise we end up in a loop */
1238
1239 sc->transfer_state = TSTATE_IDLE;
1240 if (sc->transfer_priv) {
1241 sc->transfer_cb(sc, sc->transfer_priv,
1242 sc->transfer_datalen,
1243 sc->transfer_status);
1244 }
1245
1246 return;
1247
1248 /***** Default *****/
1249 default:
1250 panic("%s: Unknown state %d",
1251 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1252 }
1253 }
1254
1255 /*
1256 * Command/Bulk/Interrupt (CBI) specific functions
1257 */
1258
1259 Static int
1260 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1261 usbd_xfer_handle xfer)
1262 {
1263 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1264 ("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
1265 sc->sc_wire));
1266
1267 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1268 sc->sc_req.bRequest = UR_CBI_ADSC;
1269 USETW(sc->sc_req.wValue, 0);
1270 USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
1271 USETW(sc->sc_req.wLength, buflen);
1272 return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
1273 buflen, 0, xfer);
1274 }
1275
1276
1277 Static void
1278 umass_cbi_reset(struct umass_softc *sc, int status)
1279 {
1280 int i;
1281 # define SEND_DIAGNOSTIC_CMDLEN 12
1282
1283 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1284 ("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
1285 sc->sc_wire));
1286
1287 if (sc->sc_dying)
1288 return;
1289
1290 /*
1291 * Command Block Reset Protocol
1292 *
1293 * First send a reset request to the device. Then clear
1294 * any possibly stalled bulk endpoints.
1295
1296 * This is done in 3 steps, states:
1297 * TSTATE_CBI_RESET1
1298 * TSTATE_CBI_RESET2
1299 * TSTATE_CBI_RESET3
1300 *
1301 * If the reset doesn't succeed, the device should be port reset.
1302 */
1303
1304 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1305 USBDEVNAME(sc->sc_dev)));
1306
1307 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1308 ("%s: CBL struct is too small (%d < %d)\n",
1309 USBDEVNAME(sc->sc_dev),
1310 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1311
1312 sc->transfer_state = TSTATE_CBI_RESET1;
1313 sc->transfer_status = status;
1314
1315 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1316 * the two the last 10 bytes of the cbl is filled with 0xff (section
1317 * 2.2 of the CBI spec).
1318 */
1319 sc->cbl[0] = 0x1d; /* Command Block Reset */
1320 sc->cbl[1] = 0x04;
1321 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1322 sc->cbl[i] = 0xff;
1323
1324 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1325 sc->transfer_xfer[XFER_CBI_RESET1]);
1326 /* XXX if the command fails we should reset the port on the bub */
1327 }
1328
1329 Static void
1330 umass_cbi_transfer(struct umass_softc *sc, int lun,
1331 void *cmd, int cmdlen, void *data, int datalen, int dir,
1332 u_int timeout, umass_callback cb, void *priv)
1333 {
1334 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1335 USBDEVNAME(sc->sc_dev), *(u_char *)cmd, datalen));
1336
1337 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1338 ("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
1339 sc->sc_wire));
1340
1341 if (sc->sc_dying)
1342 return;
1343
1344 /* Be a little generous. */
1345 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
1346
1347 /*
1348 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1349 * a data phase of datalen bytes from/to the device and finally a
1350 * csw read phase.
1351 * If the data direction was inbound a maximum of datalen bytes
1352 * is stored in the buffer pointed to by data.
1353 *
1354 * umass_cbi_transfer initialises the transfer and lets the state
1355 * machine in umass_cbi_state handle the completion. It uses the
1356 * following states:
1357 * TSTATE_CBI_COMMAND
1358 * -> XXX fill in
1359 *
1360 * An error in any of those states will invoke
1361 * umass_cbi_reset.
1362 */
1363
1364 /* check the given arguments */
1365 KASSERT(datalen == 0 || data != NULL,
1366 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1367 KASSERT(datalen == 0 || dir != DIR_NONE,
1368 ("%s: direction is NONE while datalen is not zero\n",
1369 USBDEVNAME(sc->sc_dev)));
1370
1371 /* store the details for the data transfer phase */
1372 sc->transfer_dir = dir;
1373 sc->transfer_data = data;
1374 sc->transfer_datalen = datalen;
1375 sc->transfer_actlen = 0;
1376 sc->transfer_cb = cb;
1377 sc->transfer_priv = priv;
1378 sc->transfer_status = STATUS_CMD_OK;
1379
1380 /* move from idle to the command state */
1381 sc->transfer_state = TSTATE_CBI_COMMAND;
1382
1383 /* Send the Command Block from host to device via control endpoint. */
1384 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
1385 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1386 }
1387
1388 Static void
1389 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1390 usbd_status err)
1391 {
1392 struct umass_softc *sc = (struct umass_softc *) priv;
1393
1394 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1395 ("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
1396 sc->sc_wire));
1397
1398 if (sc->sc_dying)
1399 return;
1400
1401 /*
1402 * State handling for CBI transfers.
1403 */
1404
1405 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1406 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1407 states[sc->transfer_state], xfer, usbd_errstr(err)));
1408
1409 switch (sc->transfer_state) {
1410
1411 /***** CBI Transfer *****/
1412 case TSTATE_CBI_COMMAND:
1413 if (err == USBD_STALLED) {
1414 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1415 USBDEVNAME(sc->sc_dev)));
1416 /* Status transport by control pipe (section 2.3.2.1).
1417 * The command contained in the command block failed.
1418 *
1419 * The control pipe has already been unstalled by the
1420 * USB stack.
1421 * Section 2.4.3.1.1 states that the bulk in endpoints
1422 * should not stalled at this point.
1423 */
1424
1425 sc->transfer_state = TSTATE_IDLE;
1426 sc->transfer_cb(sc, sc->transfer_priv,
1427 sc->transfer_datalen,
1428 STATUS_CMD_FAILED);
1429
1430 return;
1431 } else if (err) {
1432 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1433 USBDEVNAME(sc->sc_dev)));
1434 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1435
1436 return;
1437 }
1438
1439 sc->transfer_state = TSTATE_CBI_DATA;
1440 if (sc->transfer_dir == DIR_IN) {
1441 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1442 sc->transfer_data, sc->transfer_datalen,
1443 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1444 sc->transfer_xfer[XFER_CBI_DATA]))
1445 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1446
1447 } else if (sc->transfer_dir == DIR_OUT) {
1448 memcpy(sc->data_buffer, sc->transfer_data,
1449 sc->transfer_datalen);
1450 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1451 sc->transfer_data, sc->transfer_datalen,
1452 USBD_NO_COPY,/* fixed length transfer */
1453 sc->transfer_xfer[XFER_CBI_DATA]))
1454 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1455
1456 } else if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
1457 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1458 USBDEVNAME(sc->sc_dev)));
1459 sc->transfer_state = TSTATE_CBI_STATUS;
1460 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
1461 &sc->sbl, sizeof(sc->sbl),
1462 0, /* fixed length transfer */
1463 sc->transfer_xfer[XFER_CBI_STATUS])){
1464 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1465 }
1466 } else {
1467 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1468 USBDEVNAME(sc->sc_dev)));
1469 /* No command completion interrupt. Request
1470 * sense data.
1471 */
1472 sc->transfer_state = TSTATE_IDLE;
1473 sc->transfer_cb(sc, sc->transfer_priv,
1474 0, STATUS_CMD_UNKNOWN);
1475 }
1476
1477 return;
1478
1479 case TSTATE_CBI_DATA:
1480 /* retrieve the length of the transfer that was done */
1481 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
1482 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
1483 USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
1484
1485 if (err) {
1486 DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, "
1487 "%s\n", USBDEVNAME(sc->sc_dev),
1488 (sc->transfer_dir == DIR_IN?"in":"out"),
1489 sc->transfer_datalen,usbd_errstr(err)));
1490
1491 if (err == USBD_STALLED) {
1492 sc->transfer_state = TSTATE_CBI_DCLEAR;
1493 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1494 sc->transfer_xfer[XFER_CBI_DCLEAR]);
1495 } else {
1496 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1497 }
1498 return;
1499 }
1500
1501 if (sc->transfer_dir == DIR_IN)
1502 memcpy(sc->transfer_data, sc->data_buffer,
1503 sc->transfer_actlen);
1504
1505 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
1506 umass_dump_buffer(sc, sc->transfer_data,
1507 sc->transfer_actlen, 48));
1508
1509 if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
1510 sc->transfer_state = TSTATE_CBI_STATUS;
1511 memset(&sc->sbl, 0, sizeof(sc->sbl));
1512 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
1513 &sc->sbl, sizeof(sc->sbl),
1514 0, /* fixed length transfer */
1515 sc->transfer_xfer[XFER_CBI_STATUS])){
1516 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1517 }
1518 } else {
1519 /* No command completion interrupt. Request
1520 * sense to get status of command.
1521 */
1522 sc->transfer_state = TSTATE_IDLE;
1523 sc->transfer_cb(sc, sc->transfer_priv,
1524 sc->transfer_datalen - sc->transfer_actlen,
1525 STATUS_CMD_UNKNOWN);
1526 }
1527 return;
1528
1529 case TSTATE_CBI_STATUS:
1530 if (err) {
1531 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
1532 USBDEVNAME(sc->sc_dev)));
1533 /* Status transport by interrupt pipe (section 2.3.2.2).
1534 */
1535
1536 if (err == USBD_STALLED) {
1537 sc->transfer_state = TSTATE_CBI_SCLEAR;
1538 umass_clear_endpoint_stall(sc, UMASS_INTRIN,
1539 sc->transfer_xfer[XFER_CBI_SCLEAR]);
1540 } else {
1541 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1542 }
1543 return;
1544 }
1545
1546 /* Dissect the information in the buffer */
1547
1548 if (sc->sc_cmd == UMASS_CPROTO_UFI) {
1549 int status;
1550
1551 /* Section 3.4.3.1.3 specifies that the UFI command
1552 * protocol returns an ASC and ASCQ in the interrupt
1553 * data block.
1554 */
1555
1556 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
1557 "ASCQ = 0x%02x\n",
1558 USBDEVNAME(sc->sc_dev),
1559 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
1560
1561 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
1562 status = STATUS_CMD_OK;
1563 else
1564 status = STATUS_CMD_FAILED;
1565
1566 /* No sense, command successful */
1567 } else {
1568 /* Command Interrupt Data Block */
1569 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
1570 USBDEVNAME(sc->sc_dev),
1571 sc->sbl.common.type, sc->sbl.common.value));
1572
1573 if (sc->sbl.common.type == IDB_TYPE_CCI) {
1574 int err;
1575
1576 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
1577 == IDB_VALUE_PASS) {
1578 err = STATUS_CMD_OK;
1579 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
1580 == IDB_VALUE_FAIL ||
1581 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
1582 == IDB_VALUE_PERSISTENT) {
1583 err = STATUS_CMD_FAILED;
1584 } else {
1585 err = STATUS_WIRE_FAILED;
1586 }
1587
1588 sc->transfer_state = TSTATE_IDLE;
1589 sc->transfer_cb(sc, sc->transfer_priv,
1590 sc->transfer_datalen,
1591 err);
1592 }
1593 }
1594 return;
1595
1596 case TSTATE_CBI_DCLEAR:
1597 if (err) { /* should not occur */
1598 printf("%s: CBI bulk-in/out stall clear failed, %s\n",
1599 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1600 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1601 }
1602
1603 sc->transfer_state = TSTATE_IDLE;
1604 sc->transfer_cb(sc, sc->transfer_priv,
1605 sc->transfer_datalen,
1606 STATUS_CMD_FAILED);
1607 return;
1608
1609 case TSTATE_CBI_SCLEAR:
1610 if (err) /* should not occur */
1611 printf("%s: CBI intr-in stall clear failed, %s\n",
1612 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1613
1614 /* Something really bad is going on. Reset the device */
1615 umass_cbi_reset(sc, STATUS_CMD_FAILED);
1616 return;
1617
1618 /***** CBI Reset *****/
1619 case TSTATE_CBI_RESET1:
1620 if (err)
1621 printf("%s: CBI reset failed, %s\n",
1622 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1623
1624 sc->transfer_state = TSTATE_CBI_RESET2;
1625 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1626 sc->transfer_xfer[XFER_CBI_RESET2]);
1627
1628 return;
1629 case TSTATE_CBI_RESET2:
1630 if (err) /* should not occur */
1631 printf("%s: CBI bulk-in stall clear failed, %s\n",
1632 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1633 /* no error recovery, otherwise we end up in a loop */
1634
1635 sc->transfer_state = TSTATE_CBI_RESET3;
1636 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1637 sc->transfer_xfer[XFER_CBI_RESET3]);
1638
1639 return;
1640 case TSTATE_CBI_RESET3:
1641 if (err) /* should not occur */
1642 printf("%s: CBI bulk-out stall clear failed, %s\n",
1643 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1644 /* no error recovery, otherwise we end up in a loop */
1645
1646 sc->transfer_state = TSTATE_IDLE;
1647 if (sc->transfer_priv) {
1648 sc->transfer_cb(sc, sc->transfer_priv,
1649 sc->transfer_datalen,
1650 sc->transfer_status);
1651 }
1652
1653 return;
1654
1655
1656 /***** Default *****/
1657 default:
1658 panic("%s: Unknown state %d",
1659 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1660 }
1661 }
1662
1663 usbd_status
1664 umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
1665 {
1666 usb_device_request_t req;
1667 usbd_status err;
1668
1669 *maxlun = 0; /* Default to 0. */
1670
1671 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
1672
1673 /* The Get Max Lun command is a class-specific request. */
1674 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1675 req.bRequest = UR_BBB_GET_MAX_LUN;
1676 USETW(req.wValue, 0);
1677 USETW(req.wIndex, sc->sc_ifaceno);
1678 USETW(req.wLength, 1);
1679
1680 err = usbd_do_request_flags(sc->sc_udev, &req, maxlun,
1681 USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT);
1682 switch (err) {
1683 case USBD_NORMAL_COMPLETION:
1684 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
1685 USBDEVNAME(sc->sc_dev), *maxlun));
1686 break;
1687
1688 case USBD_STALLED:
1689 /*
1690 * Device doesn't support Get Max Lun request.
1691 */
1692 err = USBD_NORMAL_COMPLETION;
1693 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
1694 USBDEVNAME(sc->sc_dev)));
1695 break;
1696
1697 case USBD_SHORT_XFER:
1698 /*
1699 * XXX This must mean Get Max Lun is not supported, too!
1700 */
1701 err = USBD_NORMAL_COMPLETION;
1702 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
1703 USBDEVNAME(sc->sc_dev)));
1704 break;
1705
1706 default:
1707 printf("%s: Get Max Lun failed: %s\n",
1708 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1709 /* XXX Should we port_reset the device? */
1710 break;
1711 }
1712
1713 return (err);
1714 }
1715
1716
1717
1718
1719 #ifdef UMASS_DEBUG
1720 Static void
1721 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
1722 {
1723 int clen = cbw->bCDBLength;
1724 int dlen = UGETDW(cbw->dCBWDataTransferLength);
1725 u_int8_t *c = cbw->CBWCDB;
1726 int tag = UGETDW(cbw->dCBWTag);
1727 int flags = cbw->bCBWFlags;
1728
1729 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen=%d "
1730 "(0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%s), "
1731 "data = %d bytes, dir = %s\n",
1732 USBDEVNAME(sc->sc_dev), tag, clen,
1733 c[0], c[1], c[2], c[3], c[4], c[5],
1734 c[6], c[7], c[8], c[9],
1735 (clen > 10? "...":""),
1736 dlen, (flags == CBWFLAGS_IN? "in":
1737 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
1738 }
1739
1740 Static void
1741 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
1742 {
1743 int sig = UGETDW(csw->dCSWSignature);
1744 int tag = UGETW(csw->dCSWTag);
1745 int res = UGETDW(csw->dCSWDataResidue);
1746 int status = csw->bCSWStatus;
1747
1748 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
1749 "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
1750 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
1751 tag, res,
1752 status, (status == CSWSTATUS_GOOD? "good":
1753 (status == CSWSTATUS_FAILED? "failed":
1754 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
1755 }
1756
1757 Static void
1758 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
1759 int printlen)
1760 {
1761 int i, j;
1762 char s1[40];
1763 char s2[40];
1764 char s3[5];
1765
1766 s1[0] = '\0';
1767 s3[0] = '\0';
1768
1769 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
1770 for (i = 0; i < buflen && i < printlen; i++) {
1771 j = i % 16;
1772 if (j == 0 && i != 0) {
1773 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
1774 USBDEVNAME(sc->sc_dev), s1, s2));
1775 s2[0] = '\0';
1776 }
1777 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
1778 }
1779 if (buflen > printlen)
1780 sprintf(s3, " ...");
1781 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
1782 USBDEVNAME(sc->sc_dev), s1, s2, s3));
1783 }
1784 #endif
1785