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