umass.c revision 1.80 1 /* $NetBSD: umass.c,v 1.80 2001/12/24 13:43:24 augustss 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 my 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.80 2001/12/24 13:43:24 augustss 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/ioctl.h>
111 #include <sys/malloc.h>
112 #undef KASSERT
113 #define KASSERT(cond, msg)
114 #elif defined(__FreeBSD__)
115 #include <sys/module.h>
116 #include <sys/bus.h>
117 #include <machine/clock.h>
118 #endif
119
120 #include <dev/usb/usb.h>
121 #include <dev/usb/usbdi.h>
122 #include <dev/usb/usbdi_util.h>
123 #include <dev/usb/usbdevs.h>
124
125 #include <dev/usb/umassvar.h>
126 #include <dev/usb/umass_quirks.h>
127 #include <dev/usb/umass_scsipi.h>
128 #include <dev/usb/umass_isdata.h>
129
130
131 #ifdef UMASS_DEBUG
132 int umassdebug = 0;
133
134 char *states[TSTATE_STATES+1] = {
135 /* should be kept in sync with the list at transfer_state */
136 "Idle",
137 "BBB CBW",
138 "BBB Data",
139 "BBB Data bulk-in/-out clear stall",
140 "BBB CSW, 1st attempt",
141 "BBB CSW bulk-in clear stall",
142 "BBB CSW, 2nd attempt",
143 "BBB Reset",
144 "BBB bulk-in clear stall",
145 "BBB bulk-out clear stall",
146 "CBI Command",
147 "CBI Data",
148 "CBI Status",
149 "CBI Data bulk-in/-out clear stall",
150 "CBI Status intr-in clear stall",
151 "CBI Reset",
152 "CBI bulk-in clear stall",
153 "CBI bulk-out clear stall",
154 NULL
155 };
156 #endif
157
158 /* USB device probe/attach/detach functions */
159 USB_DECLARE_DRIVER(umass);
160 Static void umass_disco(struct umass_softc *sc);
161
162 /* generic transfer functions */
163 Static usbd_status umass_setup_transfer(struct umass_softc *sc,
164 usbd_pipe_handle pipe,
165 void *buffer, int buflen, int flags,
166 usbd_xfer_handle xfer);
167 Static usbd_status umass_setup_ctrl_transfer(struct umass_softc *sc,
168 usb_device_request_t *req,
169 void *buffer, int buflen, int flags,
170 usbd_xfer_handle xfer);
171 Static void umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
172 usbd_xfer_handle xfer);
173 #if 0
174 Static void umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv);
175 #endif
176
177 /* Bulk-Only related functions */
178 Static void umass_bbb_transfer(struct umass_softc *, int, void *, int, void *,
179 int, int, u_int, umass_callback, void *);
180 Static void umass_bbb_reset(struct umass_softc *, int);
181 Static void umass_bbb_state(usbd_xfer_handle, usbd_private_handle, usbd_status);
182
183 usbd_status umass_bbb_get_max_lun(struct umass_softc *, u_int8_t *);
184
185 /* CBI related functions */
186 Static void umass_cbi_transfer(struct umass_softc *, int, void *, int, void *,
187 int, int, u_int, umass_callback, void *);
188 Static void umass_cbi_reset(struct umass_softc *, int);
189 Static void umass_cbi_state(usbd_xfer_handle, usbd_private_handle, usbd_status);
190
191 Static int umass_cbi_adsc(struct umass_softc *, char *, int, usbd_xfer_handle);
192
193 const struct umass_wire_methods umass_bbb_methods = {
194 umass_bbb_transfer,
195 umass_bbb_reset,
196 umass_bbb_state
197 };
198
199 const struct umass_wire_methods umass_cbi_methods = {
200 umass_cbi_transfer,
201 umass_cbi_reset,
202 umass_cbi_state
203 };
204
205 #ifdef UMASS_DEBUG
206 /* General debugging functions */
207 Static void umass_bbb_dump_cbw(struct umass_softc *sc,
208 umass_bbb_cbw_t *cbw);
209 Static void umass_bbb_dump_csw(struct umass_softc *sc,
210 umass_bbb_csw_t *csw);
211 Static void umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer,
212 int buflen, int printlen);
213 #endif
214
215
216 /*
217 * USB device probe/attach/detach
218 */
219
220 USB_MATCH(umass)
221 {
222 USB_MATCH_START(umass, uaa);
223 const struct umass_quirk *quirk;
224 usb_interface_descriptor_t *id;
225
226 if (uaa->iface == NULL)
227 return (UMATCH_NONE);
228
229 quirk = umass_lookup(uaa->vendor, uaa->product);
230 if (quirk != NULL)
231 return (quirk->uq_match);
232
233 id = usbd_get_interface_descriptor(uaa->iface);
234 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
235 return (UMATCH_NONE);
236
237 switch (id->bInterfaceSubClass) {
238 case UISUBCLASS_RBC:
239 case UISUBCLASS_SFF8020I:
240 case UISUBCLASS_QIC157:
241 case UISUBCLASS_UFI:
242 case UISUBCLASS_SFF8070I:
243 case UISUBCLASS_SCSI:
244 break;
245 default:
246 return (UMATCH_IFACECLASS);
247 }
248
249 switch (id->bInterfaceProtocol) {
250 case UIPROTO_MASS_CBI_I:
251 case UIPROTO_MASS_CBI:
252 case UIPROTO_MASS_BBB_OLD:
253 case UIPROTO_MASS_BBB:
254 break;
255 default:
256 return (UMATCH_IFACECLASS_IFACESUBCLASS);
257 }
258
259 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
260 }
261
262 USB_ATTACH(umass)
263 {
264 USB_ATTACH_START(umass, sc, uaa);
265 const struct umass_quirk *quirk;
266 usb_interface_descriptor_t *id;
267 usb_endpoint_descriptor_t *ed;
268 const char *sWire, *sCommand;
269 char devinfo[1024];
270 usbd_status err;
271 int i, bno, error;
272
273 usbd_devinfo(uaa->device, 0, devinfo);
274 USB_ATTACH_SETUP;
275
276 sc->sc_udev = uaa->device;
277 sc->sc_iface = uaa->iface;
278 sc->sc_ifaceno = uaa->ifaceno;
279
280 quirk = umass_lookup(uaa->vendor, uaa->product);
281 if (quirk != NULL) {
282 sc->sc_wire = quirk->uq_wire;
283 sc->sc_cmd = quirk->uq_cmd;
284 sc->sc_quirks = quirk->uq_flags;
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 }
293
294 if (sc->transfer_speed == 0)
295 sc->transfer_speed = UMASS_DEFAULT_TRANSFER_SPEED;
296
297 id = usbd_get_interface_descriptor(sc->sc_iface);
298 if (id == NULL)
299 USB_ATTACH_ERROR_RETURN;
300
301 if (sc->sc_wire == UMASS_WPROTO_UNSPEC) {
302 switch (id->bInterfaceProtocol) {
303 case UIPROTO_MASS_CBI:
304 sc->sc_wire = UMASS_WPROTO_CBI;
305 break;
306 case UIPROTO_MASS_CBI_I:
307 sc->sc_wire = UMASS_WPROTO_CBI_I;
308 break;
309 case UIPROTO_MASS_BBB:
310 case UIPROTO_MASS_BBB_OLD:
311 sc->sc_wire = UMASS_WPROTO_BBB;
312 break;
313 default:
314 DPRINTF(UDMASS_GEN,
315 ("%s: Unsupported wire protocol %u\n",
316 USBDEVNAME(sc->sc_dev),
317 id->bInterfaceProtocol));
318 USB_ATTACH_ERROR_RETURN;
319 }
320 }
321
322 /* XXX - Now unsupported CBI with CCI */
323 if (sc->sc_wire == UMASS_WPROTO_CBI_I)
324 sc->sc_wire = UMASS_WPROTO_CBI;
325
326 if (sc->sc_cmd == UMASS_CPROTO_UNSPEC) {
327 switch (id->bInterfaceSubClass) {
328 case UISUBCLASS_SCSI:
329 sc->sc_cmd = UMASS_CPROTO_SCSI;
330 break;
331 case UISUBCLASS_UFI:
332 sc->sc_cmd = UMASS_CPROTO_UFI;
333 break;
334 case UISUBCLASS_SFF8020I:
335 case UISUBCLASS_SFF8070I:
336 case UISUBCLASS_QIC157:
337 sc->sc_cmd = UMASS_CPROTO_ATAPI;
338 break;
339 case UISUBCLASS_RBC:
340 sc->sc_cmd = UMASS_CPROTO_RBC;
341 break;
342 default:
343 DPRINTF(UDMASS_GEN,
344 ("%s: Unsupported command protocol %u\n",
345 USBDEVNAME(sc->sc_dev),
346 id->bInterfaceSubClass));
347 USB_ATTACH_ERROR_RETURN;
348 }
349 }
350
351 /*
352 * The timeout is based on the maximum expected transfer size
353 * divided by the expected transfer speed.
354 * We multiply by 4 to make sure a busy system doesn't make things
355 * fail.
356 */
357 sc->timeout = 4 * UMASS_MAX_TRANSFER_SIZE / sc->transfer_speed;
358 sc->timeout += UMASS_SPINUP_TIME; /* allow for spinning up */
359 #ifdef UMASS_DEBUG
360 printf("%s: timeout=%d ms\n", USBDEVNAME(sc->sc_dev), sc->timeout);
361 #endif
362
363 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
364
365 switch (sc->sc_wire) {
366 case UMASS_WPROTO_CBI:
367 sWire = "CBI";
368 break;
369 case UMASS_WPROTO_CBI_I:
370 sWire = "CBI with CCI";
371 break;
372 case UMASS_WPROTO_BBB:
373 sWire = "Bulk-Only";
374 break;
375 default:
376 sWire = "unknown";
377 break;
378 }
379
380 switch (sc->sc_cmd) {
381 case UMASS_CPROTO_RBC:
382 sCommand = "RBC";
383 break;
384 case UMASS_CPROTO_SCSI:
385 sCommand = "SCSI";
386 break;
387 case UMASS_CPROTO_UFI:
388 sCommand = "UFI";
389 break;
390 case UMASS_CPROTO_ATAPI:
391 sCommand = "ATAPI";
392 break;
393 case UMASS_CPROTO_ISD_ATA:
394 sCommand = "ISD-ATA";
395 break;
396 default:
397 sCommand = "unknown";
398 break;
399 }
400
401 printf("%s: using %s over %s\n", USBDEVNAME(sc->sc_dev), sCommand,
402 sWire);
403
404 /*
405 * In addition to the Control endpoint the following endpoints
406 * are required:
407 * a) bulk-in endpoint.
408 * b) bulk-out endpoint.
409 * and for Control/Bulk/Interrupt with CCI (CBI_I)
410 * c) intr-in
411 *
412 * The endpoint addresses are not fixed, so we have to read them
413 * from the device descriptors of the current interface.
414 */
415 for (i = 0 ; i < id->bNumEndpoints ; i++) {
416 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
417 if (ed == NULL) {
418 printf("%s: could not read endpoint descriptor\n",
419 USBDEVNAME(sc->sc_dev));
420 USB_ATTACH_ERROR_RETURN;
421 }
422 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
423 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
424 sc->sc_epaddr[UMASS_BULKIN] = ed->bEndpointAddress;
425 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
426 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
427 sc->sc_epaddr[UMASS_BULKOUT] = ed->bEndpointAddress;
428 } else if (sc->sc_wire == UMASS_WPROTO_CBI_I
429 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
430 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
431 sc->sc_epaddr[UMASS_INTRIN] = ed->bEndpointAddress;
432 #ifdef UMASS_DEBUG
433 if (UGETW(ed->wMaxPacketSize) > 2) {
434 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
435 USBDEVNAME(sc->sc_dev),
436 UGETW(ed->wMaxPacketSize)));
437 }
438 #endif
439 }
440 }
441
442 /* check whether we found all the endpoints we need */
443 if (!sc->sc_epaddr[UMASS_BULKIN] || !sc->sc_epaddr[UMASS_BULKOUT] ||
444 (sc->sc_wire == UMASS_WPROTO_CBI_I &&
445 !sc->sc_epaddr[UMASS_INTRIN])) {
446 DPRINTF(UDMASS_USB, ("%s: endpoint not found %u/%u/%u\n",
447 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKIN],
448 sc->sc_epaddr[UMASS_BULKOUT],
449 sc->sc_epaddr[UMASS_INTRIN]));
450 USB_ATTACH_ERROR_RETURN;
451 }
452
453 /*
454 * Get the maximum LUN supported by the device.
455 */
456 if (sc->sc_wire == UMASS_WPROTO_BBB) {
457 err = umass_bbb_get_max_lun(sc, &sc->maxlun);
458 if (err) {
459 printf("%s: unable to get Max Lun: %s\n",
460 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
461 USB_ATTACH_ERROR_RETURN;
462 }
463 } else {
464 sc->maxlun = 0;
465 }
466
467 /* Open the bulk-in and -out pipe */
468 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKOUT],
469 USBD_EXCLUSIVE_USE,
470 &sc->sc_pipe[UMASS_BULKOUT]);
471 if (err) {
472 DPRINTF(UDMASS_USB, ("%s: cannot open %u-out pipe (bulk)\n",
473 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKOUT]));
474 umass_disco(sc);
475 USB_ATTACH_ERROR_RETURN;
476 }
477 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKIN],
478 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_BULKIN]);
479 if (err) {
480 DPRINTF(UDMASS_USB, ("%s: could not open %u-in pipe (bulk)\n",
481 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKIN]));
482 umass_disco(sc);
483 USB_ATTACH_ERROR_RETURN;
484 }
485 /*
486 * Open the intr-in pipe if the protocol is CBI with CCI.
487 * Note: early versions of the Zip drive do have an interrupt pipe, but
488 * this pipe is unused
489 *
490 * We do not open the interrupt pipe as an interrupt pipe, but as a
491 * normal bulk endpoint. We send an IN transfer down the wire at the
492 * appropriate time, because we know exactly when to expect data on
493 * that endpoint. This saves bandwidth, but more important, makes the
494 * code for handling the data on that endpoint simpler. No data
495 * arriving concurrently.
496 */
497 if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
498 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_INTRIN],
499 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_INTRIN]);
500 if (err) {
501 DPRINTF(UDMASS_USB, ("%s: couldn't open %u-in (intr)\n",
502 USBDEVNAME(sc->sc_dev),
503 sc->sc_epaddr[UMASS_INTRIN]));
504 umass_disco(sc);
505 USB_ATTACH_ERROR_RETURN;
506 }
507 }
508
509 /* initialisation of generic part */
510 sc->transfer_state = TSTATE_IDLE;
511
512 /* request a sufficient number of xfer handles */
513 for (i = 0; i < XFER_NR; i++) {
514 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
515 if (sc->transfer_xfer[i] == 0) {
516 DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
517 USBDEVNAME(sc->sc_dev)));
518 umass_disco(sc);
519 USB_ATTACH_ERROR_RETURN;
520 }
521 }
522 /* Allocate buffer for data transfer (it's huge). */
523 switch (sc->sc_wire) {
524 case UMASS_WPROTO_BBB:
525 bno = XFER_BBB_DATA;
526 goto dalloc;
527 case UMASS_WPROTO_CBI:
528 bno = XFER_CBI_DATA;
529 goto dalloc;
530 case UMASS_WPROTO_CBI_I:
531 bno = XFER_CBI_DATA;
532 dalloc:
533 sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno],
534 UMASS_MAX_TRANSFER_SIZE);
535 if (sc->data_buffer == NULL) {
536 umass_disco(sc);
537 USB_ATTACH_ERROR_RETURN;
538 }
539 break;
540 default:
541 break;
542 }
543
544 /* Initialise the wire protocol specific methods */
545 switch (sc->sc_wire) {
546 case UMASS_WPROTO_BBB:
547 sc->sc_methods = &umass_bbb_methods;
548 break;
549 case UMASS_WPROTO_CBI:
550 case UMASS_WPROTO_CBI_I:
551 sc->sc_methods = &umass_cbi_methods;
552 break;
553 default:
554 umass_disco(sc);
555 USB_ATTACH_ERROR_RETURN;
556 }
557
558 if (quirk != NULL && quirk->uq_init != NULL) {
559 err = (*quirk->uq_init)(sc);
560 if (err) {
561 umass_disco(sc);
562 USB_ATTACH_ERROR_RETURN;
563 }
564 }
565
566 error = 0;
567 switch (sc->sc_cmd) {
568 case UMASS_CPROTO_RBC:
569 case UMASS_CPROTO_SCSI:
570 #if NSCSIBUS > 0
571 error = umass_scsi_attach(sc);
572 #else
573 printf("%s: atapibus not configured\n", USBDEVNAME(sc->sc_dev));
574 #endif
575 break;
576
577 case UMASS_CPROTO_UFI:
578 case UMASS_CPROTO_ATAPI:
579 #if NATAPIBUS > 0
580 error = umass_atapi_attach(sc);
581 #else
582 printf("%s: scsibus not configured\n", USBDEVNAME(sc->sc_dev));
583 #endif
584 break;
585
586 case UMASS_CPROTO_ISD_ATA:
587 #if NWD > 0
588 error = umass_isdata_attach(sc);
589 #else
590 printf("%s: isdata not configured\n", USBDEVNAME(sc->sc_dev));
591 #endif
592 break;
593
594 default:
595 printf("%s: command protocol=0x%x not supported\n",
596 USBDEVNAME(sc->sc_dev), sc->sc_cmd);
597 umass_disco(sc);
598 USB_ATTACH_ERROR_RETURN;
599 }
600 if (error) {
601 printf("%s: bus attach failed\n", USBDEVNAME(sc->sc_dev));
602 umass_disco(sc);
603 USB_ATTACH_ERROR_RETURN;
604 }
605
606 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
607 USBDEV(sc->sc_dev));
608
609 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev)));
610
611 USB_ATTACH_SUCCESS_RETURN;
612 }
613
614 USB_DETACH(umass)
615 {
616 USB_DETACH_START(umass, sc);
617 struct umassbus_softc *scbus = sc->bus;
618 int rv = 0, i;
619
620 DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev)));
621
622 /* Abort the pipes to wake up any waiting processes. */
623 for (i = 0 ; i < UMASS_NEP ; i++) {
624 if (sc->sc_pipe[i] != NULL)
625 usbd_abort_pipe(sc->sc_pipe[i]);
626 }
627
628 #if 0
629 /* Do we really need reference counting? Perhaps in ioctl() */
630 s = splusb();
631 if (--sc->sc_refcnt >= 0) {
632 /* Wait for processes to go away. */
633 usb_detach_wait(USBDEV(sc->sc_dev));
634 }
635 splx(s);
636 #endif
637
638 if (scbus != NULL) {
639 if (scbus->sc_child != NULL)
640 rv = config_detach(scbus->sc_child, flags);
641 free(scbus, M_DEVBUF);
642 sc->bus = NULL;
643 }
644
645 if (rv != 0)
646 return (rv);
647
648 umass_disco(sc);
649
650 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
651 USBDEV(sc->sc_dev));
652
653 return (rv);
654 }
655
656 int
657 umass_activate(struct device *dev, enum devact act)
658 {
659 struct umass_softc *sc = (struct umass_softc *)dev;
660 struct umassbus_softc *scbus = sc->bus;
661 int rv = 0;
662
663 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
664 USBDEVNAME(sc->sc_dev), act));
665
666 switch (act) {
667 case DVACT_ACTIVATE:
668 rv = EOPNOTSUPP;
669 break;
670
671 case DVACT_DEACTIVATE:
672 sc->sc_dying = 1;
673 if (scbus->sc_child == NULL)
674 break;
675 rv = config_deactivate(scbus->sc_child);
676 DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
677 "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
678 break;
679 }
680 return (rv);
681 }
682
683 Static void
684 umass_disco(struct umass_softc *sc)
685 {
686 int i;
687
688 DPRINTF(UDMASS_GEN, ("umass_disco\n"));
689
690 /* Free the xfers. */
691 for (i = 0; i < XFER_NR; i++)
692 if (sc->transfer_xfer[i] != NULL) {
693 usbd_free_xfer(sc->transfer_xfer[i]);
694 sc->transfer_xfer[i] = NULL;
695 }
696
697 /* Remove all the pipes. */
698 for (i = 0 ; i < UMASS_NEP ; i++) {
699 if (sc->sc_pipe[i] != NULL)
700 usbd_close_pipe(sc->sc_pipe[i]);
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), 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 successfull) */
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 successfull 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, successfull 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 /* Check CSW and handle any error */
1136 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1137 /* Invalid CSW: Wrong signature or wrong tag might
1138 * indicate that the device is confused -> reset it.
1139 */
1140 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1141 USBDEVNAME(sc->sc_dev),
1142 UGETDW(sc->csw.dCSWSignature),
1143 CSWSIGNATURE);
1144
1145 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1146 return;
1147 } else if (UGETDW(sc->csw.dCSWTag)
1148 != UGETDW(sc->cbw.dCBWTag)) {
1149 printf("%s: Invalid CSW: tag %d should be %d\n",
1150 USBDEVNAME(sc->sc_dev),
1151 UGETDW(sc->csw.dCSWTag),
1152 UGETDW(sc->cbw.dCBWTag));
1153
1154 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1155 return;
1156
1157 /* CSW is valid here */
1158 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1159 printf("%s: Invalid CSW: status %d > %d\n",
1160 USBDEVNAME(sc->sc_dev),
1161 sc->csw.bCSWStatus,
1162 CSWSTATUS_PHASE);
1163
1164 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1165 return;
1166 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1167 printf("%s: Phase Error, residue = %d\n",
1168 USBDEVNAME(sc->sc_dev),
1169 UGETDW(sc->csw.dCSWDataResidue));
1170
1171 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1172 return;
1173
1174 } else if (sc->transfer_actlen > sc->transfer_datalen) {
1175 /* Buffer overrun! Don't let this go by unnoticed */
1176 panic("%s: transferred %d bytes instead of %d bytes\n",
1177 USBDEVNAME(sc->sc_dev),
1178 sc->transfer_actlen, sc->transfer_datalen);
1179 #if 0
1180 } else if (sc->transfer_datalen - sc->transfer_actlen
1181 != UGETDW(sc->csw.dCSWDataResidue)) {
1182 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1183 USBDEVNAME(sc->sc_dev),
1184 sc->transfer_datalen - sc->transfer_actlen,
1185 UGETDW(sc->csw.dCSWDataResidue)));
1186
1187 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1188 return;
1189 #endif
1190 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1191 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1192 USBDEVNAME(sc->sc_dev),
1193 UGETDW(sc->csw.dCSWDataResidue)));
1194
1195 /* SCSI command failed but transfer was succesful */
1196 sc->transfer_state = TSTATE_IDLE;
1197 sc->transfer_cb(sc, sc->transfer_priv,
1198 UGETDW(sc->csw.dCSWDataResidue),
1199 STATUS_CMD_FAILED);
1200
1201 return;
1202
1203 } else { /* success */
1204 sc->transfer_state = TSTATE_IDLE;
1205 sc->transfer_cb(sc, sc->transfer_priv,
1206 UGETDW(sc->csw.dCSWDataResidue),
1207 STATUS_CMD_OK);
1208
1209 return;
1210 }
1211
1212 /***** Bulk Reset *****/
1213 case TSTATE_BBB_RESET1:
1214 if (err)
1215 printf("%s: BBB reset failed, %s\n",
1216 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1217
1218 sc->transfer_state = TSTATE_BBB_RESET2;
1219 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1220 sc->transfer_xfer[XFER_BBB_RESET2]);
1221
1222 return;
1223 case TSTATE_BBB_RESET2:
1224 if (err) /* should not occur */
1225 printf("%s: BBB bulk-in clear stall failed, %s\n",
1226 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1227 /* no error recovery, otherwise we end up in a loop */
1228
1229 sc->transfer_state = TSTATE_BBB_RESET3;
1230 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1231 sc->transfer_xfer[XFER_BBB_RESET3]);
1232
1233 return;
1234 case TSTATE_BBB_RESET3:
1235 if (err) /* should not occur */
1236 printf("%s: BBB bulk-out clear stall failed, %s\n",
1237 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1238 /* no error recovery, otherwise we end up in a loop */
1239
1240 sc->transfer_state = TSTATE_IDLE;
1241 if (sc->transfer_priv) {
1242 sc->transfer_cb(sc, sc->transfer_priv,
1243 sc->transfer_datalen,
1244 sc->transfer_status);
1245 }
1246
1247 return;
1248
1249 /***** Default *****/
1250 default:
1251 panic("%s: Unknown state %d\n",
1252 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1253 }
1254 }
1255
1256 /*
1257 * Command/Bulk/Interrupt (CBI) specific functions
1258 */
1259
1260 Static int
1261 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1262 usbd_xfer_handle xfer)
1263 {
1264 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1265 ("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
1266 sc->sc_wire));
1267
1268 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1269 sc->sc_req.bRequest = UR_CBI_ADSC;
1270 USETW(sc->sc_req.wValue, 0);
1271 USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
1272 USETW(sc->sc_req.wLength, buflen);
1273 return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
1274 buflen, 0, xfer);
1275 }
1276
1277
1278 Static void
1279 umass_cbi_reset(struct umass_softc *sc, int status)
1280 {
1281 int i;
1282 # define SEND_DIAGNOSTIC_CMDLEN 12
1283
1284 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1285 ("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
1286 sc->sc_wire));
1287
1288 if (sc->sc_dying)
1289 return;
1290
1291 /*
1292 * Command Block Reset Protocol
1293 *
1294 * First send a reset request to the device. Then clear
1295 * any possibly stalled bulk endpoints.
1296
1297 * This is done in 3 steps, states:
1298 * TSTATE_CBI_RESET1
1299 * TSTATE_CBI_RESET2
1300 * TSTATE_CBI_RESET3
1301 *
1302 * If the reset doesn't succeed, the device should be port reset.
1303 */
1304
1305 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1306 USBDEVNAME(sc->sc_dev)));
1307
1308 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1309 ("%s: CBL struct is too small (%d < %d)\n",
1310 USBDEVNAME(sc->sc_dev),
1311 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1312
1313 sc->transfer_state = TSTATE_CBI_RESET1;
1314 sc->transfer_status = status;
1315
1316 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1317 * the two the last 10 bytes of the cbl is filled with 0xff (section
1318 * 2.2 of the CBI spec).
1319 */
1320 sc->cbl[0] = 0x1d; /* Command Block Reset */
1321 sc->cbl[1] = 0x04;
1322 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1323 sc->cbl[i] = 0xff;
1324
1325 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1326 sc->transfer_xfer[XFER_CBI_RESET1]);
1327 /* XXX if the command fails we should reset the port on the bub */
1328 }
1329
1330 Static void
1331 umass_cbi_transfer(struct umass_softc *sc, int lun,
1332 void *cmd, int cmdlen, void *data, int datalen, int dir,
1333 u_int timeout, umass_callback cb, void *priv)
1334 {
1335 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1336 USBDEVNAME(sc->sc_dev), *(u_char*)cmd, datalen));
1337
1338 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1339 ("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
1340 sc->sc_wire));
1341
1342 if (sc->sc_dying)
1343 return;
1344
1345 /* Be a little generous. */
1346 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
1347
1348 /*
1349 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1350 * a data phase of datalen bytes from/to the device and finally a
1351 * csw read phase.
1352 * If the data direction was inbound a maximum of datalen bytes
1353 * is stored in the buffer pointed to by data.
1354 *
1355 * umass_cbi_transfer initialises the transfer and lets the state
1356 * machine in umass_cbi_state handle the completion. It uses the
1357 * following states:
1358 * TSTATE_CBI_COMMAND
1359 * -> XXX fill in
1360 *
1361 * An error in any of those states will invoke
1362 * umass_cbi_reset.
1363 */
1364
1365 /* check the given arguments */
1366 KASSERT(datalen == 0 || data != NULL,
1367 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1368 KASSERT(datalen == 0 || dir != DIR_NONE,
1369 ("%s: direction is NONE while datalen is not zero\n",
1370 USBDEVNAME(sc->sc_dev)));
1371
1372 /* store the details for the data transfer phase */
1373 sc->transfer_dir = dir;
1374 sc->transfer_data = data;
1375 sc->transfer_datalen = datalen;
1376 sc->transfer_actlen = 0;
1377 sc->transfer_cb = cb;
1378 sc->transfer_priv = priv;
1379 sc->transfer_status = STATUS_CMD_OK;
1380
1381 /* move from idle to the command state */
1382 sc->transfer_state = TSTATE_CBI_COMMAND;
1383
1384 /* Send the Command Block from host to device via control endpoint. */
1385 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
1386 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1387 }
1388
1389 Static void
1390 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1391 usbd_status err)
1392 {
1393 struct umass_softc *sc = (struct umass_softc *) priv;
1394
1395 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1396 ("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
1397 sc->sc_wire));
1398
1399 if (sc->sc_dying)
1400 return;
1401
1402 /*
1403 * State handling for CBI transfers.
1404 */
1405
1406 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1407 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1408 states[sc->transfer_state], xfer, usbd_errstr(err)));
1409
1410 switch (sc->transfer_state) {
1411
1412 /***** CBI Transfer *****/
1413 case TSTATE_CBI_COMMAND:
1414 if (err == USBD_STALLED) {
1415 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1416 USBDEVNAME(sc->sc_dev)));
1417 /* Status transport by control pipe (section 2.3.2.1).
1418 * The command contained in the command block failed.
1419 *
1420 * The control pipe has already been unstalled by the
1421 * USB stack.
1422 * Section 2.4.3.1.1 states that the bulk in endpoints
1423 * should not stalled at this point.
1424 */
1425
1426 sc->transfer_state = TSTATE_IDLE;
1427 sc->transfer_cb(sc, sc->transfer_priv,
1428 sc->transfer_datalen,
1429 STATUS_CMD_FAILED);
1430
1431 return;
1432 } else if (err) {
1433 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1434 USBDEVNAME(sc->sc_dev)));
1435 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1436
1437 return;
1438 }
1439
1440 sc->transfer_state = TSTATE_CBI_DATA;
1441 if (sc->transfer_dir == DIR_IN) {
1442 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1443 sc->transfer_data, sc->transfer_datalen,
1444 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1445 sc->transfer_xfer[XFER_CBI_DATA]))
1446 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1447
1448 } else if (sc->transfer_dir == DIR_OUT) {
1449 memcpy(sc->data_buffer, sc->transfer_data,
1450 sc->transfer_datalen);
1451 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1452 sc->transfer_data, sc->transfer_datalen,
1453 USBD_NO_COPY,/* fixed length transfer */
1454 sc->transfer_xfer[XFER_CBI_DATA]))
1455 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1456
1457 } else if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
1458 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1459 USBDEVNAME(sc->sc_dev)));
1460 sc->transfer_state = TSTATE_CBI_STATUS;
1461 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
1462 &sc->sbl, sizeof(sc->sbl),
1463 0, /* fixed length transfer */
1464 sc->transfer_xfer[XFER_CBI_STATUS])){
1465 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1466 }
1467 } else {
1468 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1469 USBDEVNAME(sc->sc_dev)));
1470 /* No command completion interrupt. Request
1471 * sense data.
1472 */
1473 sc->transfer_state = TSTATE_IDLE;
1474 sc->transfer_cb(sc, sc->transfer_priv,
1475 0, STATUS_CMD_UNKNOWN);
1476 }
1477
1478 return;
1479
1480 case TSTATE_CBI_DATA:
1481 /* retrieve the length of the transfer that was done */
1482 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
1483 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
1484 USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
1485
1486 if (err) {
1487 DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, "
1488 "%s\n", USBDEVNAME(sc->sc_dev),
1489 (sc->transfer_dir == DIR_IN?"in":"out"),
1490 sc->transfer_datalen,usbd_errstr(err)));
1491
1492 if (err == USBD_STALLED) {
1493 sc->transfer_state = TSTATE_CBI_DCLEAR;
1494 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1495 sc->transfer_xfer[XFER_CBI_DCLEAR]);
1496 } else {
1497 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1498 }
1499 return;
1500 }
1501
1502 if (sc->transfer_dir == DIR_IN)
1503 memcpy(sc->transfer_data, sc->data_buffer,
1504 sc->transfer_actlen);
1505
1506 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
1507 umass_dump_buffer(sc, sc->transfer_data,
1508 sc->transfer_actlen, 48));
1509
1510 if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
1511 sc->transfer_state = TSTATE_CBI_STATUS;
1512 memset(&sc->sbl, 0, sizeof(sc->sbl));
1513 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
1514 &sc->sbl, sizeof(sc->sbl),
1515 0, /* fixed length transfer */
1516 sc->transfer_xfer[XFER_CBI_STATUS])){
1517 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1518 }
1519 } else {
1520 /* No command completion interrupt. Request
1521 * sense to get status of command.
1522 */
1523 sc->transfer_state = TSTATE_IDLE;
1524 sc->transfer_cb(sc, sc->transfer_priv,
1525 sc->transfer_datalen - sc->transfer_actlen,
1526 STATUS_CMD_UNKNOWN);
1527 }
1528 return;
1529
1530 case TSTATE_CBI_STATUS:
1531 if (err) {
1532 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
1533 USBDEVNAME(sc->sc_dev)));
1534 /* Status transport by interrupt pipe (section 2.3.2.2).
1535 */
1536
1537 if (err == USBD_STALLED) {
1538 sc->transfer_state = TSTATE_CBI_SCLEAR;
1539 umass_clear_endpoint_stall(sc, UMASS_INTRIN,
1540 sc->transfer_xfer[XFER_CBI_SCLEAR]);
1541 } else {
1542 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1543 }
1544 return;
1545 }
1546
1547 /* Dissect the information in the buffer */
1548
1549 if (sc->sc_cmd == UMASS_CPROTO_UFI) {
1550 int status;
1551
1552 /* Section 3.4.3.1.3 specifies that the UFI command
1553 * protocol returns an ASC and ASCQ in the interrupt
1554 * data block.
1555 */
1556
1557 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
1558 "ASCQ = 0x%02x\n",
1559 USBDEVNAME(sc->sc_dev),
1560 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
1561
1562 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
1563 status = STATUS_CMD_OK;
1564 else
1565 status = STATUS_CMD_FAILED;
1566
1567 /* No sense, command successfull */
1568 } else {
1569 /* Command Interrupt Data Block */
1570 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
1571 USBDEVNAME(sc->sc_dev),
1572 sc->sbl.common.type, sc->sbl.common.value));
1573
1574 if (sc->sbl.common.type == IDB_TYPE_CCI) {
1575 int err;
1576
1577 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
1578 == IDB_VALUE_PASS) {
1579 err = STATUS_CMD_OK;
1580 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
1581 == IDB_VALUE_FAIL ||
1582 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
1583 == IDB_VALUE_PERSISTENT) {
1584 err = STATUS_CMD_FAILED;
1585 } else {
1586 err = STATUS_WIRE_FAILED;
1587 }
1588
1589 sc->transfer_state = TSTATE_IDLE;
1590 sc->transfer_cb(sc, sc->transfer_priv,
1591 sc->transfer_datalen,
1592 err);
1593 }
1594 }
1595 return;
1596
1597 case TSTATE_CBI_DCLEAR:
1598 if (err) { /* should not occur */
1599 printf("%s: CBI bulk-in/out stall clear failed, %s\n",
1600 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1601 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1602 }
1603
1604 sc->transfer_state = TSTATE_IDLE;
1605 sc->transfer_cb(sc, sc->transfer_priv,
1606 sc->transfer_datalen,
1607 STATUS_CMD_FAILED);
1608 return;
1609
1610 case TSTATE_CBI_SCLEAR:
1611 if (err) /* should not occur */
1612 printf("%s: CBI intr-in stall clear failed, %s\n",
1613 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1614
1615 /* Something really bad is going on. Reset the device */
1616 umass_cbi_reset(sc, STATUS_CMD_FAILED);
1617 return;
1618
1619 /***** CBI Reset *****/
1620 case TSTATE_CBI_RESET1:
1621 if (err)
1622 printf("%s: CBI reset failed, %s\n",
1623 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1624
1625 sc->transfer_state = TSTATE_CBI_RESET2;
1626 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1627 sc->transfer_xfer[XFER_CBI_RESET2]);
1628
1629 return;
1630 case TSTATE_CBI_RESET2:
1631 if (err) /* should not occur */
1632 printf("%s: CBI bulk-in stall clear failed, %s\n",
1633 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1634 /* no error recovery, otherwise we end up in a loop */
1635
1636 sc->transfer_state = TSTATE_CBI_RESET3;
1637 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1638 sc->transfer_xfer[XFER_CBI_RESET3]);
1639
1640 return;
1641 case TSTATE_CBI_RESET3:
1642 if (err) /* should not occur */
1643 printf("%s: CBI bulk-out stall clear failed, %s\n",
1644 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1645 /* no error recovery, otherwise we end up in a loop */
1646
1647 sc->transfer_state = TSTATE_IDLE;
1648 if (sc->transfer_priv) {
1649 sc->transfer_cb(sc, sc->transfer_priv,
1650 sc->transfer_datalen,
1651 sc->transfer_status);
1652 }
1653
1654 return;
1655
1656
1657 /***** Default *****/
1658 default:
1659 panic("%s: Unknown state %d\n",
1660 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1661 }
1662 }
1663
1664 usbd_status
1665 umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
1666 {
1667 usb_device_request_t req;
1668 usbd_status err;
1669
1670 *maxlun = 0; /* Default to 0. */
1671
1672 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
1673
1674 /* The Get Max Lun command is a class-specific request. */
1675 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1676 req.bRequest = UR_BBB_GET_MAX_LUN;
1677 USETW(req.wValue, 0);
1678 USETW(req.wIndex, sc->sc_ifaceno);
1679 USETW(req.wLength, 1);
1680
1681 err = usbd_do_request(sc->sc_udev, &req, maxlun);
1682 switch (err) {
1683 case USBD_NORMAL_COMPLETION:
1684 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
1685 USBDEVNAME(sc->sc_dev), *maxlun));
1686 break;
1687
1688 case USBD_STALLED:
1689 /*
1690 * Device doesn't support Get Max Lun request.
1691 */
1692 err = USBD_NORMAL_COMPLETION;
1693 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
1694 USBDEVNAME(sc->sc_dev)));
1695 break;
1696
1697 case USBD_SHORT_XFER:
1698 /*
1699 * XXX This must mean Get Max Lun is not supported, too!
1700 */
1701 err = USBD_NORMAL_COMPLETION;
1702 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
1703 USBDEVNAME(sc->sc_dev)));
1704 break;
1705
1706 default:
1707 printf("%s: Get Max Lun failed: %s\n",
1708 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1709 /* XXX Should we port_reset the device? */
1710 break;
1711 }
1712
1713 return (err);
1714 }
1715
1716
1717
1718
1719 #ifdef UMASS_DEBUG
1720 Static void
1721 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
1722 {
1723 int clen = cbw->bCDBLength;
1724 int dlen = UGETDW(cbw->dCBWDataTransferLength);
1725 u_int8_t *c = cbw->CBWCDB;
1726 int tag = UGETDW(cbw->dCBWTag);
1727 int flags = cbw->bCBWFlags;
1728
1729 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen = %d "
1730 "(0x%02x%02x%02x%02x%02x%02x%s), "
1731 "data = %d bytes, dir = %s\n",
1732 USBDEVNAME(sc->sc_dev), tag, clen,
1733 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
1734 dlen, (flags == CBWFLAGS_IN? "in":
1735 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
1736 }
1737
1738 Static void
1739 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
1740 {
1741 int sig = UGETDW(csw->dCSWSignature);
1742 int tag = UGETW(csw->dCSWTag);
1743 int res = UGETDW(csw->dCSWDataResidue);
1744 int status = csw->bCSWStatus;
1745
1746 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
1747 "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
1748 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
1749 tag, res,
1750 status, (status == CSWSTATUS_GOOD? "good":
1751 (status == CSWSTATUS_FAILED? "failed":
1752 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
1753 }
1754
1755 Static void
1756 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
1757 int printlen)
1758 {
1759 int i, j;
1760 char s1[40];
1761 char s2[40];
1762 char s3[5];
1763
1764 s1[0] = '\0';
1765 s3[0] = '\0';
1766
1767 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
1768 for (i = 0; i < buflen && i < printlen; i++) {
1769 j = i % 16;
1770 if (j == 0 && i != 0) {
1771 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
1772 USBDEVNAME(sc->sc_dev), s1, s2));
1773 s2[0] = '\0';
1774 }
1775 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
1776 }
1777 if (buflen > printlen)
1778 sprintf(s3, " ...");
1779 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
1780 USBDEVNAME(sc->sc_dev), s1, s2, s3));
1781 }
1782 #endif
1783