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