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