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