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