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