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