umass.c revision 1.100 1 /* $NetBSD: umass.c,v 1.100 2003/09/10 02:49:18 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.100 2003/09/10 02:49:18 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 if (sc->transfer_dir == DIR_IN)
1056 memcpy(sc->transfer_data, sc->data_buffer,
1057 sc->transfer_actlen);
1058
1059 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1060 umass_dump_buffer(sc, sc->transfer_data,
1061 sc->transfer_datalen, 48));
1062
1063 /* FALLTHROUGH, err == 0 (no data phase or successful) */
1064 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1065 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1066 /* Reading of CSW after bulk stall condition in data phase
1067 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1068 * reading CSW (TSTATE_BBB_SCLEAR).
1069 * In the case of no data phase or successful data phase,
1070 * err == 0 and the following if block is passed.
1071 */
1072 if (err) { /* should not occur */
1073 /* try the transfer below, even if clear stall failed */
1074 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1075 ", %s\n", USBDEVNAME(sc->sc_dev),
1076 (sc->transfer_dir == DIR_IN? "in":"out"),
1077 usbd_errstr(err)));
1078 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1079 return;
1080 }
1081
1082 /* Status transport phase, setup transfer */
1083 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1084 sc->transfer_state == TSTATE_BBB_DATA ||
1085 sc->transfer_state == TSTATE_BBB_DCLEAR) {
1086 /* After no data phase, successful data phase and
1087 * after clearing bulk-in/-out stall condition
1088 */
1089 sc->transfer_state = TSTATE_BBB_STATUS1;
1090 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1091 } else {
1092 /* After first attempt of fetching CSW */
1093 sc->transfer_state = TSTATE_BBB_STATUS2;
1094 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1095 }
1096
1097 /* Read the Command Status Wrapper via bulk-in endpoint. */
1098 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1099 &sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
1100 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1101 return;
1102 }
1103
1104 return;
1105 case TSTATE_BBB_STATUS1: /* first attempt */
1106 case TSTATE_BBB_STATUS2: /* second attempt */
1107 /* Status transfer, error handling */
1108 if (err) {
1109 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1110 USBDEVNAME(sc->sc_dev), usbd_errstr(err),
1111 (sc->transfer_state == TSTATE_BBB_STATUS1?
1112 ", retrying":"")));
1113
1114 /* If this was the first attempt at fetching the CSW
1115 * retry it, otherwise fail.
1116 */
1117 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1118 sc->transfer_state = TSTATE_BBB_SCLEAR;
1119 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1120 sc->transfer_xfer[XFER_BBB_SCLEAR]);
1121 return;
1122 } else {
1123 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1124 return;
1125 }
1126 }
1127
1128 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1129
1130 /* Translate weird command-status signatures. */
1131 if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) &&
1132 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
1133 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1134
1135 /* Translate invalid command-status tags */
1136 if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG)
1137 USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag));
1138
1139 /* Check CSW and handle any error */
1140 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1141 /* Invalid CSW: Wrong signature or wrong tag might
1142 * indicate that the device is confused -> reset it.
1143 */
1144 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1145 USBDEVNAME(sc->sc_dev),
1146 UGETDW(sc->csw.dCSWSignature),
1147 CSWSIGNATURE);
1148
1149 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1150 return;
1151 } else if (UGETDW(sc->csw.dCSWTag)
1152 != UGETDW(sc->cbw.dCBWTag)) {
1153 printf("%s: Invalid CSW: tag %d should be %d\n",
1154 USBDEVNAME(sc->sc_dev),
1155 UGETDW(sc->csw.dCSWTag),
1156 UGETDW(sc->cbw.dCBWTag));
1157
1158 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1159 return;
1160
1161 /* CSW is valid here */
1162 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1163 printf("%s: Invalid CSW: status %d > %d\n",
1164 USBDEVNAME(sc->sc_dev),
1165 sc->csw.bCSWStatus,
1166 CSWSTATUS_PHASE);
1167
1168 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1169 return;
1170 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1171 printf("%s: Phase Error, residue = %d\n",
1172 USBDEVNAME(sc->sc_dev),
1173 UGETDW(sc->csw.dCSWDataResidue));
1174
1175 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1176 return;
1177
1178 } else if (sc->transfer_actlen > sc->transfer_datalen) {
1179 /* Buffer overrun! Don't let this go by unnoticed */
1180 panic("%s: transferred %d bytes instead of %d bytes",
1181 USBDEVNAME(sc->sc_dev),
1182 sc->transfer_actlen, sc->transfer_datalen);
1183 #if 0
1184 } else if (sc->transfer_datalen - sc->transfer_actlen
1185 != UGETDW(sc->csw.dCSWDataResidue)) {
1186 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1187 USBDEVNAME(sc->sc_dev),
1188 sc->transfer_datalen - sc->transfer_actlen,
1189 UGETDW(sc->csw.dCSWDataResidue)));
1190
1191 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1192 return;
1193 #endif
1194 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1195 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1196 USBDEVNAME(sc->sc_dev),
1197 UGETDW(sc->csw.dCSWDataResidue)));
1198
1199 /* SCSI command failed but transfer was succesful */
1200 sc->transfer_state = TSTATE_IDLE;
1201 sc->transfer_cb(sc, sc->transfer_priv,
1202 UGETDW(sc->csw.dCSWDataResidue),
1203 STATUS_CMD_FAILED);
1204
1205 return;
1206
1207 } else { /* success */
1208 sc->transfer_state = TSTATE_IDLE;
1209 sc->transfer_cb(sc, sc->transfer_priv,
1210 UGETDW(sc->csw.dCSWDataResidue),
1211 STATUS_CMD_OK);
1212
1213 return;
1214 }
1215
1216 /***** Bulk Reset *****/
1217 case TSTATE_BBB_RESET1:
1218 if (err)
1219 printf("%s: BBB reset failed, %s\n",
1220 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1221
1222 sc->transfer_state = TSTATE_BBB_RESET2;
1223 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1224 sc->transfer_xfer[XFER_BBB_RESET2]);
1225
1226 return;
1227 case TSTATE_BBB_RESET2:
1228 if (err) /* should not occur */
1229 printf("%s: BBB bulk-in clear stall failed, %s\n",
1230 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1231 /* no error recovery, otherwise we end up in a loop */
1232
1233 sc->transfer_state = TSTATE_BBB_RESET3;
1234 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1235 sc->transfer_xfer[XFER_BBB_RESET3]);
1236
1237 return;
1238 case TSTATE_BBB_RESET3:
1239 if (err) /* should not occur */
1240 printf("%s: BBB bulk-out clear stall failed, %s\n",
1241 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1242 /* no error recovery, otherwise we end up in a loop */
1243
1244 sc->transfer_state = TSTATE_IDLE;
1245 if (sc->transfer_priv) {
1246 sc->transfer_cb(sc, sc->transfer_priv,
1247 sc->transfer_datalen,
1248 sc->transfer_status);
1249 }
1250
1251 return;
1252
1253 /***** Default *****/
1254 default:
1255 panic("%s: Unknown state %d",
1256 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1257 }
1258 }
1259
1260 /*
1261 * Command/Bulk/Interrupt (CBI) specific functions
1262 */
1263
1264 Static int
1265 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1266 usbd_xfer_handle xfer)
1267 {
1268 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1269 ("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
1270 sc->sc_wire));
1271
1272 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1273 sc->sc_req.bRequest = UR_CBI_ADSC;
1274 USETW(sc->sc_req.wValue, 0);
1275 USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
1276 USETW(sc->sc_req.wLength, buflen);
1277 return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
1278 buflen, 0, xfer);
1279 }
1280
1281
1282 Static void
1283 umass_cbi_reset(struct umass_softc *sc, int status)
1284 {
1285 int i;
1286 # define SEND_DIAGNOSTIC_CMDLEN 12
1287
1288 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1289 ("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
1290 sc->sc_wire));
1291
1292 if (sc->sc_dying)
1293 return;
1294
1295 /*
1296 * Command Block Reset Protocol
1297 *
1298 * First send a reset request to the device. Then clear
1299 * any possibly stalled bulk endpoints.
1300
1301 * This is done in 3 steps, states:
1302 * TSTATE_CBI_RESET1
1303 * TSTATE_CBI_RESET2
1304 * TSTATE_CBI_RESET3
1305 *
1306 * If the reset doesn't succeed, the device should be port reset.
1307 */
1308
1309 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1310 USBDEVNAME(sc->sc_dev)));
1311
1312 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1313 ("%s: CBL struct is too small (%d < %d)\n",
1314 USBDEVNAME(sc->sc_dev),
1315 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1316
1317 sc->transfer_state = TSTATE_CBI_RESET1;
1318 sc->transfer_status = status;
1319
1320 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1321 * the two the last 10 bytes of the cbl is filled with 0xff (section
1322 * 2.2 of the CBI spec).
1323 */
1324 sc->cbl[0] = 0x1d; /* Command Block Reset */
1325 sc->cbl[1] = 0x04;
1326 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1327 sc->cbl[i] = 0xff;
1328
1329 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1330 sc->transfer_xfer[XFER_CBI_RESET1]);
1331 /* XXX if the command fails we should reset the port on the bub */
1332 }
1333
1334 Static void
1335 umass_cbi_transfer(struct umass_softc *sc, int lun,
1336 void *cmd, int cmdlen, void *data, int datalen, int dir,
1337 u_int timeout, umass_callback cb, void *priv)
1338 {
1339 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1340 USBDEVNAME(sc->sc_dev), *(u_char *)cmd, datalen));
1341
1342 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1343 ("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
1344 sc->sc_wire));
1345
1346 if (sc->sc_dying)
1347 return;
1348
1349 /* Be a little generous. */
1350 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
1351
1352 /*
1353 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1354 * a data phase of datalen bytes from/to the device and finally a
1355 * csw read phase.
1356 * If the data direction was inbound a maximum of datalen bytes
1357 * is stored in the buffer pointed to by data.
1358 *
1359 * umass_cbi_transfer initialises the transfer and lets the state
1360 * machine in umass_cbi_state handle the completion. It uses the
1361 * following states:
1362 * TSTATE_CBI_COMMAND
1363 * -> XXX fill in
1364 *
1365 * An error in any of those states will invoke
1366 * umass_cbi_reset.
1367 */
1368
1369 /* check the given arguments */
1370 KASSERT(datalen == 0 || data != NULL,
1371 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1372 KASSERT(datalen == 0 || dir != DIR_NONE,
1373 ("%s: direction is NONE while datalen is not zero\n",
1374 USBDEVNAME(sc->sc_dev)));
1375
1376 /* store the details for the data transfer phase */
1377 sc->transfer_dir = dir;
1378 sc->transfer_data = data;
1379 sc->transfer_datalen = datalen;
1380 sc->transfer_actlen = 0;
1381 sc->transfer_cb = cb;
1382 sc->transfer_priv = priv;
1383 sc->transfer_status = STATUS_CMD_OK;
1384
1385 /* move from idle to the command state */
1386 sc->transfer_state = TSTATE_CBI_COMMAND;
1387
1388 /* Send the Command Block from host to device via control endpoint. */
1389 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
1390 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1391 }
1392
1393 Static void
1394 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1395 usbd_status err)
1396 {
1397 struct umass_softc *sc = (struct umass_softc *) priv;
1398
1399 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1400 ("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
1401 sc->sc_wire));
1402
1403 if (sc->sc_dying)
1404 return;
1405
1406 /*
1407 * State handling for CBI transfers.
1408 */
1409
1410 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1411 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1412 states[sc->transfer_state], xfer, usbd_errstr(err)));
1413
1414 switch (sc->transfer_state) {
1415
1416 /***** CBI Transfer *****/
1417 case TSTATE_CBI_COMMAND:
1418 if (err == USBD_STALLED) {
1419 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1420 USBDEVNAME(sc->sc_dev)));
1421 /* Status transport by control pipe (section 2.3.2.1).
1422 * The command contained in the command block failed.
1423 *
1424 * The control pipe has already been unstalled by the
1425 * USB stack.
1426 * Section 2.4.3.1.1 states that the bulk in endpoints
1427 * should not stalled at this point.
1428 */
1429
1430 sc->transfer_state = TSTATE_IDLE;
1431 sc->transfer_cb(sc, sc->transfer_priv,
1432 sc->transfer_datalen,
1433 STATUS_CMD_FAILED);
1434
1435 return;
1436 } else if (err) {
1437 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1438 USBDEVNAME(sc->sc_dev)));
1439 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1440
1441 return;
1442 }
1443
1444 sc->transfer_state = TSTATE_CBI_DATA;
1445 if (sc->transfer_dir == DIR_IN) {
1446 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1447 sc->transfer_data, sc->transfer_datalen,
1448 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1449 sc->transfer_xfer[XFER_CBI_DATA]))
1450 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1451
1452 } else if (sc->transfer_dir == DIR_OUT) {
1453 memcpy(sc->data_buffer, sc->transfer_data,
1454 sc->transfer_datalen);
1455 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1456 sc->transfer_data, sc->transfer_datalen,
1457 USBD_NO_COPY,/* fixed length transfer */
1458 sc->transfer_xfer[XFER_CBI_DATA]))
1459 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1460
1461 } else {
1462 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1463 USBDEVNAME(sc->sc_dev)));
1464 goto dostatus;
1465 }
1466 return;
1467
1468 case TSTATE_CBI_DATA:
1469 /* retrieve the length of the transfer that was done */
1470 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
1471 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
1472 USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
1473
1474 if (err) {
1475 DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, "
1476 "%s\n", USBDEVNAME(sc->sc_dev),
1477 (sc->transfer_dir == DIR_IN?"in":"out"),
1478 sc->transfer_datalen,usbd_errstr(err)));
1479
1480 if (err == USBD_STALLED) {
1481 sc->transfer_state = TSTATE_CBI_DCLEAR;
1482 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1483 sc->transfer_xfer[XFER_CBI_DCLEAR]);
1484 } else {
1485 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1486 }
1487 return;
1488 }
1489
1490 if (sc->transfer_dir == DIR_IN)
1491 memcpy(sc->transfer_data, sc->data_buffer,
1492 sc->transfer_actlen);
1493
1494 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
1495 umass_dump_buffer(sc, sc->transfer_data,
1496 sc->transfer_actlen, 48));
1497
1498 dostatus:
1499 if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
1500 sc->transfer_state = TSTATE_CBI_STATUS;
1501 memset(&sc->sbl, 0, sizeof(sc->sbl));
1502 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
1503 &sc->sbl, sizeof(sc->sbl),
1504 0, /* fixed length transfer */
1505 sc->transfer_xfer[XFER_CBI_STATUS]))
1506 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1507 } else {
1508 /* No command completion interrupt. Request
1509 * sense to get status of command.
1510 */
1511 sc->transfer_state = TSTATE_IDLE;
1512 sc->transfer_cb(sc, sc->transfer_priv,
1513 sc->transfer_datalen - sc->transfer_actlen,
1514 STATUS_CMD_UNKNOWN);
1515 }
1516 return;
1517
1518 case TSTATE_CBI_STATUS:
1519 if (err) {
1520 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
1521 USBDEVNAME(sc->sc_dev)));
1522 /* Status transport by interrupt pipe (section 2.3.2.2).
1523 */
1524
1525 if (err == USBD_STALLED) {
1526 sc->transfer_state = TSTATE_CBI_SCLEAR;
1527 umass_clear_endpoint_stall(sc, UMASS_INTRIN,
1528 sc->transfer_xfer[XFER_CBI_SCLEAR]);
1529 } else {
1530 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1531 }
1532 return;
1533 }
1534
1535 /* Dissect the information in the buffer */
1536
1537 {
1538 u_int32_t actlen;
1539 usbd_get_xfer_status(xfer,NULL,NULL,&actlen,NULL);
1540 DPRINTF(UDMASS_CBI, ("%s: CBI_STATUS actlen=%d\n",
1541 USBDEVNAME(sc->sc_dev), actlen));
1542 if (actlen != 2)
1543 break;
1544 }
1545
1546 if (sc->sc_cmd == UMASS_CPROTO_UFI) {
1547 int status;
1548
1549 /* Section 3.4.3.1.3 specifies that the UFI command
1550 * protocol returns an ASC and ASCQ in the interrupt
1551 * data block.
1552 */
1553
1554 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
1555 "ASCQ = 0x%02x\n",
1556 USBDEVNAME(sc->sc_dev),
1557 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
1558
1559 if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) ||
1560 sc->sc_sense)
1561 status = STATUS_CMD_OK;
1562 else
1563 status = STATUS_CMD_FAILED;
1564
1565 /* No autosense, command successful */
1566 sc->transfer_state = TSTATE_IDLE;
1567 sc->transfer_cb(sc, sc->transfer_priv,
1568 sc->transfer_datalen - sc->transfer_actlen, status);
1569 } else {
1570 int status;
1571
1572 /* Command Interrupt Data Block */
1573
1574 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
1575 USBDEVNAME(sc->sc_dev),
1576 sc->sbl.common.type, sc->sbl.common.value));
1577
1578 if (sc->sbl.common.type == IDB_TYPE_CCI) {
1579 switch (sc->sbl.common.value & IDB_VALUE_STATUS_MASK) {
1580 case IDB_VALUE_PASS:
1581 status = STATUS_CMD_OK;
1582 break;
1583 case IDB_VALUE_FAIL:
1584 case IDB_VALUE_PERSISTENT:
1585 status = STATUS_CMD_FAILED;
1586 break;
1587 case IDB_VALUE_PHASE:
1588 status = STATUS_WIRE_FAILED;
1589 break;
1590 }
1591
1592 sc->transfer_state = TSTATE_IDLE;
1593 sc->transfer_cb(sc, sc->transfer_priv,
1594 sc->transfer_datalen - sc->transfer_actlen, status);
1595 }
1596 }
1597 return;
1598
1599 case TSTATE_CBI_DCLEAR:
1600 if (err) { /* should not occur */
1601 printf("%s: CBI bulk-in/out stall clear failed, %s\n",
1602 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1603 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1604 }
1605
1606 sc->transfer_state = TSTATE_IDLE;
1607 sc->transfer_cb(sc, sc->transfer_priv,
1608 sc->transfer_datalen,
1609 STATUS_CMD_FAILED);
1610 return;
1611
1612 case TSTATE_CBI_SCLEAR:
1613 if (err) /* should not occur */
1614 printf("%s: CBI intr-in stall clear failed, %s\n",
1615 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1616
1617 /* Something really bad is going on. Reset the device */
1618 umass_cbi_reset(sc, STATUS_CMD_FAILED);
1619 return;
1620
1621 /***** CBI Reset *****/
1622 case TSTATE_CBI_RESET1:
1623 if (err)
1624 printf("%s: CBI reset failed, %s\n",
1625 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1626
1627 sc->transfer_state = TSTATE_CBI_RESET2;
1628 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1629 sc->transfer_xfer[XFER_CBI_RESET2]);
1630
1631 return;
1632 case TSTATE_CBI_RESET2:
1633 if (err) /* should not occur */
1634 printf("%s: CBI bulk-in stall clear failed, %s\n",
1635 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1636 /* no error recovery, otherwise we end up in a loop */
1637
1638 sc->transfer_state = TSTATE_CBI_RESET3;
1639 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1640 sc->transfer_xfer[XFER_CBI_RESET3]);
1641
1642 return;
1643 case TSTATE_CBI_RESET3:
1644 if (err) /* should not occur */
1645 printf("%s: CBI bulk-out stall clear failed, %s\n",
1646 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1647 /* no error recovery, otherwise we end up in a loop */
1648
1649 sc->transfer_state = TSTATE_IDLE;
1650 if (sc->transfer_priv) {
1651 sc->transfer_cb(sc, sc->transfer_priv,
1652 sc->transfer_datalen,
1653 sc->transfer_status);
1654 }
1655
1656 return;
1657
1658
1659 /***** Default *****/
1660 default:
1661 panic("%s: Unknown state %d",
1662 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1663 }
1664 }
1665
1666 usbd_status
1667 umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
1668 {
1669 usb_device_request_t req;
1670 usbd_status err;
1671
1672 *maxlun = 0; /* Default to 0. */
1673
1674 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
1675
1676 /* The Get Max Lun command is a class-specific request. */
1677 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1678 req.bRequest = UR_BBB_GET_MAX_LUN;
1679 USETW(req.wValue, 0);
1680 USETW(req.wIndex, sc->sc_ifaceno);
1681 USETW(req.wLength, 1);
1682
1683 err = usbd_do_request_flags(sc->sc_udev, &req, maxlun,
1684 USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT);
1685 switch (err) {
1686 case USBD_NORMAL_COMPLETION:
1687 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
1688 USBDEVNAME(sc->sc_dev), *maxlun));
1689 break;
1690
1691 case USBD_STALLED:
1692 /*
1693 * Device doesn't support Get Max Lun request.
1694 */
1695 err = USBD_NORMAL_COMPLETION;
1696 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
1697 USBDEVNAME(sc->sc_dev)));
1698 break;
1699
1700 case USBD_SHORT_XFER:
1701 /*
1702 * XXX This must mean Get Max Lun is not supported, too!
1703 */
1704 err = USBD_NORMAL_COMPLETION;
1705 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
1706 USBDEVNAME(sc->sc_dev)));
1707 break;
1708
1709 default:
1710 printf("%s: Get Max Lun failed: %s\n",
1711 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1712 /* XXX Should we port_reset the device? */
1713 break;
1714 }
1715
1716 return (err);
1717 }
1718
1719
1720
1721
1722 #ifdef UMASS_DEBUG
1723 Static void
1724 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
1725 {
1726 int clen = cbw->bCDBLength;
1727 int dlen = UGETDW(cbw->dCBWDataTransferLength);
1728 u_int8_t *c = cbw->CBWCDB;
1729 int tag = UGETDW(cbw->dCBWTag);
1730 int flags = cbw->bCBWFlags;
1731
1732 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen=%d "
1733 "(0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%s), "
1734 "data = %d bytes, dir = %s\n",
1735 USBDEVNAME(sc->sc_dev), tag, clen,
1736 c[0], c[1], c[2], c[3], c[4], c[5],
1737 c[6], c[7], c[8], c[9],
1738 (clen > 10? "...":""),
1739 dlen, (flags == CBWFLAGS_IN? "in":
1740 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
1741 }
1742
1743 Static void
1744 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
1745 {
1746 int sig = UGETDW(csw->dCSWSignature);
1747 int tag = UGETW(csw->dCSWTag);
1748 int res = UGETDW(csw->dCSWDataResidue);
1749 int status = csw->bCSWStatus;
1750
1751 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
1752 "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
1753 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
1754 tag, res,
1755 status, (status == CSWSTATUS_GOOD? "good":
1756 (status == CSWSTATUS_FAILED? "failed":
1757 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
1758 }
1759
1760 Static void
1761 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
1762 int printlen)
1763 {
1764 int i, j;
1765 char s1[40];
1766 char s2[40];
1767 char s3[5];
1768
1769 s1[0] = '\0';
1770 s3[0] = '\0';
1771
1772 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
1773 for (i = 0; i < buflen && i < printlen; i++) {
1774 j = i % 16;
1775 if (j == 0 && i != 0) {
1776 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
1777 USBDEVNAME(sc->sc_dev), s1, s2));
1778 s2[0] = '\0';
1779 }
1780 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
1781 }
1782 if (buflen > printlen)
1783 sprintf(s3, " ...");
1784 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
1785 USBDEVNAME(sc->sc_dev), s1, s2, s3));
1786 }
1787 #endif
1788