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