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