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