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