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