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