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