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