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