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