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