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