FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/umass.c
1 /* $NetBSD: umass.c,v 1.109.2.3 2004/07/10 13:45:19 tron Exp $ */
2
3 /*
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 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@rr.iij4u.or.jp>,
41 * Nick Hibma <n_hibma@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/devclass_docs/usb_msc_overview_1.2.pdf
71 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
72 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
73 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
74 */
75
76 /*
77 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
78 * Parts of the code written by Jason R. Thorpe <thorpej@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@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.109.2.3 2004/07/10 13:45:19 tron 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 if (quirk != NULL && quirk->uq_init != NULL) {
427 err = (*quirk->uq_init)(sc);
428 if (err) {
429 printf("%s: quirk init failed\n",
430 USBDEVNAME(sc->sc_dev));
431 umass_disco(sc);
432 USB_ATTACH_ERROR_RETURN;
433 }
434 }
435
436 /*
437 * In addition to the Control endpoint the following endpoints
438 * are required:
439 * a) bulk-in endpoint.
440 * b) bulk-out endpoint.
441 * and for Control/Bulk/Interrupt with CCI (CBI_I)
442 * c) intr-in
443 *
444 * The endpoint addresses are not fixed, so we have to read them
445 * from the device descriptors of the current interface.
446 */
447 for (i = 0 ; i < id->bNumEndpoints ; i++) {
448 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
449 if (ed == NULL) {
450 printf("%s: could not read endpoint descriptor\n",
451 USBDEVNAME(sc->sc_dev));
452 USB_ATTACH_ERROR_RETURN;
453 }
454 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
455 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
456 sc->sc_epaddr[UMASS_BULKIN] = ed->bEndpointAddress;
457 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
458 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
459 sc->sc_epaddr[UMASS_BULKOUT] = ed->bEndpointAddress;
460 } else if (sc->sc_wire == UMASS_WPROTO_CBI_I
461 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
462 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
463 sc->sc_epaddr[UMASS_INTRIN] = ed->bEndpointAddress;
464 #ifdef UMASS_DEBUG
465 if (UGETW(ed->wMaxPacketSize) > 2) {
466 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
467 USBDEVNAME(sc->sc_dev),
468 UGETW(ed->wMaxPacketSize)));
469 }
470 #endif
471 }
472 }
473
474 /* check whether we found all the endpoints we need */
475 if (!sc->sc_epaddr[UMASS_BULKIN] || !sc->sc_epaddr[UMASS_BULKOUT] ||
476 (sc->sc_wire == UMASS_WPROTO_CBI_I &&
477 !sc->sc_epaddr[UMASS_INTRIN])) {
478 printf("%s: endpoint not found %u/%u/%u\n",
479 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKIN],
480 sc->sc_epaddr[UMASS_BULKOUT],
481 sc->sc_epaddr[UMASS_INTRIN]);
482 USB_ATTACH_ERROR_RETURN;
483 }
484
485 /*
486 * Get the maximum LUN supported by the device.
487 */
488 if (sc->sc_wire == UMASS_WPROTO_BBB) {
489 err = umass_bbb_get_max_lun(sc, &sc->maxlun);
490 if (err) {
491 printf("%s: unable to get Max Lun: %s\n",
492 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
493 USB_ATTACH_ERROR_RETURN;
494 }
495 if (sc->maxlun > 0)
496 sc->sc_busquirks |= PQUIRK_FORCELUNS;
497 } else {
498 sc->maxlun = 0;
499 }
500
501 /* Open the bulk-in and -out pipe */
502 DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for BULKOUT\n",
503 USBDEVNAME(sc->sc_dev), sc->sc_iface,
504 sc->sc_epaddr[UMASS_BULKOUT]));
505 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKOUT],
506 USBD_EXCLUSIVE_USE,
507 &sc->sc_pipe[UMASS_BULKOUT]);
508 if (err) {
509 printf("%s: cannot open %u-out pipe (bulk)\n",
510 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKOUT]);
511 umass_disco(sc);
512 USB_ATTACH_ERROR_RETURN;
513 }
514 DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for BULKIN\n",
515 USBDEVNAME(sc->sc_dev), sc->sc_iface,
516 sc->sc_epaddr[UMASS_BULKIN]));
517 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKIN],
518 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_BULKIN]);
519 if (err) {
520 printf("%s: could not open %u-in pipe (bulk)\n",
521 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[UMASS_BULKIN]);
522 umass_disco(sc);
523 USB_ATTACH_ERROR_RETURN;
524 }
525 /*
526 * Open the intr-in pipe if the protocol is CBI with CCI.
527 * Note: early versions of the Zip drive do have an interrupt pipe, but
528 * this pipe is unused
529 *
530 * We do not open the interrupt pipe as an interrupt pipe, but as a
531 * normal bulk endpoint. We send an IN transfer down the wire at the
532 * appropriate time, because we know exactly when to expect data on
533 * that endpoint. This saves bandwidth, but more important, makes the
534 * code for handling the data on that endpoint simpler. No data
535 * arriving concurrently.
536 */
537 if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
538 DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for INTRIN\n",
539 USBDEVNAME(sc->sc_dev), sc->sc_iface,
540 sc->sc_epaddr[UMASS_INTRIN]));
541 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_INTRIN],
542 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_INTRIN]);
543 if (err) {
544 printf("%s: couldn't open %u-in (intr)\n",
545 USBDEVNAME(sc->sc_dev),
546 sc->sc_epaddr[UMASS_INTRIN]);
547 umass_disco(sc);
548 USB_ATTACH_ERROR_RETURN;
549 }
550 }
551
552 /* initialisation of generic part */
553 sc->transfer_state = TSTATE_IDLE;
554
555 /* request a sufficient number of xfer handles */
556 for (i = 0; i < XFER_NR; i++) {
557 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
558 if (sc->transfer_xfer[i] == NULL) {
559 printf("%s: Out of memory\n",
560 USBDEVNAME(sc->sc_dev));
561 umass_disco(sc);
562 USB_ATTACH_ERROR_RETURN;
563 }
564 }
565 /* Allocate buffer for data transfer (it's huge). */
566 switch (sc->sc_wire) {
567 case UMASS_WPROTO_BBB:
568 bno = XFER_BBB_DATA;
569 goto dalloc;
570 case UMASS_WPROTO_CBI:
571 bno = XFER_CBI_DATA;
572 goto dalloc;
573 case UMASS_WPROTO_CBI_I:
574 bno = XFER_CBI_DATA;
575 dalloc:
576 sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno],
577 UMASS_MAX_TRANSFER_SIZE);
578 if (sc->data_buffer == NULL) {
579 printf("%s: no buffer memory\n",
580 USBDEVNAME(sc->sc_dev));
581 umass_disco(sc);
582 USB_ATTACH_ERROR_RETURN;
583 }
584 break;
585 default:
586 break;
587 }
588
589 /* Initialise the wire protocol specific methods */
590 switch (sc->sc_wire) {
591 case UMASS_WPROTO_BBB:
592 sc->sc_methods = &umass_bbb_methods;
593 break;
594 case UMASS_WPROTO_CBI:
595 case UMASS_WPROTO_CBI_I:
596 sc->sc_methods = &umass_cbi_methods;
597 break;
598 default:
599 umass_disco(sc);
600 USB_ATTACH_ERROR_RETURN;
601 }
602
603 error = 0;
604 switch (sc->sc_cmd) {
605 case UMASS_CPROTO_RBC:
606 case UMASS_CPROTO_SCSI:
607 #if NSCSIBUS > 0
608 error = umass_scsi_attach(sc);
609 #else
610 printf("%s: scsibus not configured\n", USBDEVNAME(sc->sc_dev));
611 #endif
612 break;
613
614 case UMASS_CPROTO_UFI:
615 case UMASS_CPROTO_ATAPI:
616 #if NATAPIBUS > 0
617 error = umass_atapi_attach(sc);
618 #else
619 printf("%s: atapibus not configured\n",
620 USBDEVNAME(sc->sc_dev));
621 #endif
622 break;
623
624 case UMASS_CPROTO_ISD_ATA:
625 #if NWD > 0
626 error = umass_isdata_attach(sc);
627 #else
628 printf("%s: isdata not configured\n", USBDEVNAME(sc->sc_dev));
629 #endif
630 break;
631
632 default:
633 printf("%s: command protocol=0x%x not supported\n",
634 USBDEVNAME(sc->sc_dev), sc->sc_cmd);
635 umass_disco(sc);
636 USB_ATTACH_ERROR_RETURN;
637 }
638 if (error) {
639 printf("%s: bus attach failed\n", USBDEVNAME(sc->sc_dev));
640 umass_disco(sc);
641 USB_ATTACH_ERROR_RETURN;
642 }
643
644 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
645 USBDEV(sc->sc_dev));
646
647 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev)));
648
649 USB_ATTACH_SUCCESS_RETURN;
650 }
651
652 USB_DETACH(umass)
653 {
654 USB_DETACH_START(umass, sc);
655 struct umassbus_softc *scbus;
656 int rv = 0, i, s;
657
658 DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev)));
659
660 /* Abort the pipes to wake up any waiting processes. */
661 for (i = 0 ; i < UMASS_NEP ; i++) {
662 if (sc->sc_pipe[i] != NULL) {
663 usbd_abort_pipe(sc->sc_pipe[i]);
664 sc->sc_pipe[i] = NULL;
665 }
666 }
667
668 /* Do we really need reference counting? Perhaps in ioctl() */
669 s = splusb();
670 if (--sc->sc_refcnt >= 0) {
671 #ifdef DIAGNOSTIC
672 printf("%s: waiting for refcnt\n", USBDEVNAME(sc->sc_dev));
673 #endif
674 /* Wait for processes to go away. */
675 usb_detach_wait(USBDEV(sc->sc_dev));
676 }
677 splx(s);
678
679 scbus = sc->bus;
680 if (scbus != NULL) {
681 if (scbus->sc_child != NULL)
682 rv = config_detach(scbus->sc_child, flags);
683 free(scbus, M_DEVBUF);
684 sc->bus = NULL;
685 }
686
687 if (rv != 0)
688 return (rv);
689
690 umass_disco(sc);
691
692 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
693 USBDEV(sc->sc_dev));
694
695 return (rv);
696 }
697
698 int
699 umass_activate(struct device *dev, enum devact act)
700 {
701 struct umass_softc *sc = (struct umass_softc *)dev;
702 struct umassbus_softc *scbus = sc->bus;
703 int rv = 0;
704
705 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
706 USBDEVNAME(sc->sc_dev), act));
707
708 switch (act) {
709 case DVACT_ACTIVATE:
710 rv = EOPNOTSUPP;
711 break;
712
713 case DVACT_DEACTIVATE:
714 sc->sc_dying = 1;
715 if (scbus == NULL || scbus->sc_child == NULL)
716 break;
717 rv = config_deactivate(scbus->sc_child);
718 DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
719 "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
720 break;
721 }
722 return (rv);
723 }
724
725 Static void
726 umass_disco(struct umass_softc *sc)
727 {
728 int i;
729
730 DPRINTF(UDMASS_GEN, ("umass_disco\n"));
731
732 /* Free the xfers. */
733 for (i = 0; i < XFER_NR; i++)
734 if (sc->transfer_xfer[i] != NULL) {
735 usbd_free_xfer(sc->transfer_xfer[i]);
736 sc->transfer_xfer[i] = NULL;
737 }
738
739 /* Remove all the pipes. */
740 for (i = 0 ; i < UMASS_NEP ; i++) {
741 if (sc->sc_pipe[i] != NULL) {
742 usbd_close_pipe(sc->sc_pipe[i]);
743 sc->sc_pipe[i] = NULL;
744 }
745 }
746 }
747
748 /*
749 * Generic functions to handle transfers
750 */
751
752 Static usbd_status
753 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
754 void *buffer, int buflen, int flags,
755 usbd_xfer_handle xfer)
756 {
757 usbd_status err;
758
759 if (sc->sc_dying)
760 return (USBD_IOERROR);
761
762 /* Initialiase a USB transfer and then schedule it */
763
764 usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen,
765 flags | sc->sc_xfer_flags, sc->timeout, sc->sc_methods->wire_state);
766
767 err = usbd_transfer(xfer);
768 DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d flags=0x%x "
769 "timeout=%d\n", USBDEVNAME(sc->sc_dev),
770 buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout));
771 if (err && err != USBD_IN_PROGRESS) {
772 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
773 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
774 return (err);
775 }
776
777 return (USBD_NORMAL_COMPLETION);
778 }
779
780
781 Static usbd_status
782 umass_setup_ctrl_transfer(struct umass_softc *sc, usb_device_request_t *req,
783 void *buffer, int buflen, int flags, usbd_xfer_handle xfer)
784 {
785 usbd_status err;
786
787 if (sc->sc_dying)
788 return (USBD_IOERROR);
789
790 /* Initialiase a USB control transfer and then schedule it */
791
792 usbd_setup_default_xfer(xfer, sc->sc_udev, (void *) sc, sc->timeout,
793 req, buffer, buflen, flags, sc->sc_methods->wire_state);
794
795 err = usbd_transfer(xfer);
796 if (err && err != USBD_IN_PROGRESS) {
797 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
798 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
799
800 /* do not reset, as this would make us loop */
801 return (err);
802 }
803
804 return (USBD_NORMAL_COMPLETION);
805 }
806
807 Static void
808 umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
809 usbd_xfer_handle xfer)
810 {
811 if (sc->sc_dying)
812 return;
813
814 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
815 USBDEVNAME(sc->sc_dev), sc->sc_epaddr[endpt]));
816
817 usbd_clear_endpoint_toggle(sc->sc_pipe[endpt]);
818
819 sc->sc_req.bmRequestType = UT_WRITE_ENDPOINT;
820 sc->sc_req.bRequest = UR_CLEAR_FEATURE;
821 USETW(sc->sc_req.wValue, UF_ENDPOINT_HALT);
822 USETW(sc->sc_req.wIndex, sc->sc_epaddr[endpt]);
823 USETW(sc->sc_req.wLength, 0);
824 umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0, xfer);
825 }
826
827 #if 0
828 Static void
829 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
830 {
831 sc->transfer_cb = cb;
832 sc->transfer_priv = priv;
833
834 /* The reset is a forced reset, so no error (yet) */
835 sc->reset(sc, STATUS_CMD_OK);
836 }
837 #endif
838
839 /*
840 * Bulk protocol specific functions
841 */
842
843 Static void
844 umass_bbb_reset(struct umass_softc *sc, int status)
845 {
846 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
847 ("sc->sc_wire == 0x%02x wrong for umass_bbb_reset\n",
848 sc->sc_wire));
849
850 if (sc->sc_dying)
851 return;
852
853 /*
854 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
855 *
856 * For Reset Recovery the host shall issue in the following order:
857 * a) a Bulk-Only Mass Storage Reset
858 * b) a Clear Feature HALT to the Bulk-In endpoint
859 * c) a Clear Feature HALT to the Bulk-Out endpoint
860 *
861 * This is done in 3 steps, states:
862 * TSTATE_BBB_RESET1
863 * TSTATE_BBB_RESET2
864 * TSTATE_BBB_RESET3
865 *
866 * If the reset doesn't succeed, the device should be port reset.
867 */
868
869 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
870 USBDEVNAME(sc->sc_dev)));
871
872 sc->transfer_state = TSTATE_BBB_RESET1;
873 sc->transfer_status = status;
874
875 /* reset is a class specific interface write */
876 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
877 sc->sc_req.bRequest = UR_BBB_RESET;
878 USETW(sc->sc_req.wValue, 0);
879 USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
880 USETW(sc->sc_req.wLength, 0);
881 umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0,
882 sc->transfer_xfer[XFER_BBB_RESET1]);
883 }
884
885 Static void
886 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
887 void *data, int datalen, int dir, u_int timeout,
888 umass_callback cb, void *priv)
889 {
890 static int dCBWtag = 42; /* unique for CBW of transfer */
891
892 DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n",
893 USBDEVNAME(sc->sc_dev), *(u_char *)cmd));
894
895 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
896 ("sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n",
897 sc->sc_wire));
898
899 if (sc->sc_dying)
900 return;
901
902 /* Be a little generous. */
903 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
904
905 /*
906 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
907 * a data phase of datalen bytes from/to the device and finally a
908 * csw read phase.
909 * If the data direction was inbound a maximum of datalen bytes
910 * is stored in the buffer pointed to by data.
911 *
912 * umass_bbb_transfer initialises the transfer and lets the state
913 * machine in umass_bbb_state handle the completion. It uses the
914 * following states:
915 * TSTATE_BBB_COMMAND
916 * -> TSTATE_BBB_DATA
917 * -> TSTATE_BBB_STATUS
918 * -> TSTATE_BBB_STATUS2
919 * -> TSTATE_BBB_IDLE
920 *
921 * An error in any of those states will invoke
922 * umass_bbb_reset.
923 */
924
925 /* check the given arguments */
926 KASSERT(datalen == 0 || data != NULL,
927 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
928 KASSERT(cmdlen <= CBWCDBLENGTH,
929 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
930 USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH));
931 KASSERT(dir == DIR_NONE || datalen > 0,
932 ("%s: datalen == 0 while direction is not NONE\n",
933 USBDEVNAME(sc->sc_dev)));
934 KASSERT(datalen == 0 || dir != DIR_NONE,
935 ("%s: direction is NONE while datalen is not zero\n",
936 USBDEVNAME(sc->sc_dev)));
937 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
938 ("%s: CBW struct does not have the right size (%d vs. %d)\n",
939 USBDEVNAME(sc->sc_dev),
940 sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
941 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
942 ("%s: CSW struct does not have the right size (%d vs. %d)\n",
943 USBDEVNAME(sc->sc_dev),
944 sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
945
946 /*
947 * Determine the direction of the data transfer and the length.
948 *
949 * dCBWDataTransferLength (datalen) :
950 * This field indicates the number of bytes of data that the host
951 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
952 * the Direction bit) during the execution of this command. If this
953 * field is set to 0, the device will expect that no data will be
954 * transferred IN or OUT during this command, regardless of the value
955 * of the Direction bit defined in dCBWFlags.
956 *
957 * dCBWFlags (dir) :
958 * The bits of the Flags field are defined as follows:
959 * Bits 0-6 reserved
960 * Bit 7 Direction - this bit shall be ignored if the
961 * dCBWDataTransferLength field is zero.
962 * 0 = data Out from host to device
963 * 1 = data In from device to host
964 */
965
966 /* Fill in the Command Block Wrapper */
967 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
968 USETDW(sc->cbw.dCBWTag, dCBWtag);
969 dCBWtag++; /* cannot be done in macro (it will be done 4 times) */
970 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
971 /* DIR_NONE is treated as DIR_OUT (0x00) */
972 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
973 sc->cbw.bCBWLUN = lun;
974 sc->cbw.bCDBLength = cmdlen;
975 memcpy(sc->cbw.CBWCDB, cmd, cmdlen);
976
977 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
978
979 /* store the details for the data transfer phase */
980 sc->transfer_dir = dir;
981 sc->transfer_data = data;
982 sc->transfer_datalen = datalen;
983 sc->transfer_actlen = 0;
984 sc->transfer_cb = cb;
985 sc->transfer_priv = priv;
986 sc->transfer_status = STATUS_CMD_OK;
987
988 /* move from idle to the command state */
989 sc->transfer_state = TSTATE_BBB_COMMAND;
990
991 /* Send the CBW from host to device via bulk-out endpoint. */
992 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
993 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
994 sc->transfer_xfer[XFER_BBB_CBW])) {
995 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
996 }
997 }
998
999
1000 Static void
1001 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1002 usbd_status err)
1003 {
1004 struct umass_softc *sc = (struct umass_softc *) priv;
1005 usbd_xfer_handle next_xfer;
1006
1007 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
1008 ("sc->sc_wire == 0x%02x wrong for umass_bbb_state\n",
1009 sc->sc_wire));
1010
1011 if (sc->sc_dying)
1012 return;
1013
1014 /*
1015 * State handling for BBB transfers.
1016 *
1017 * The subroutine is rather long. It steps through the states given in
1018 * Annex A of the Bulk-Only specification.
1019 * Each state first does the error handling of the previous transfer
1020 * and then prepares the next transfer.
1021 * Each transfer is done asynchroneously so after the request/transfer
1022 * has been submitted you will find a 'return;'.
1023 */
1024
1025 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1026 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1027 states[sc->transfer_state], xfer, usbd_errstr(err)));
1028
1029 switch (sc->transfer_state) {
1030
1031 /***** Bulk Transfer *****/
1032 case TSTATE_BBB_COMMAND:
1033 /* Command transport phase, error handling */
1034 if (err) {
1035 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1036 USBDEVNAME(sc->sc_dev)));
1037 /* If the device detects that the CBW is invalid, then
1038 * the device may STALL both bulk endpoints and require
1039 * a Bulk-Reset
1040 */
1041 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1042 return;
1043 }
1044
1045 /* Data transport phase, setup transfer */
1046 sc->transfer_state = TSTATE_BBB_DATA;
1047 if (sc->transfer_dir == DIR_IN) {
1048 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1049 sc->data_buffer, sc->transfer_datalen,
1050 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1051 sc->transfer_xfer[XFER_BBB_DATA]))
1052 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1053
1054 return;
1055 } else if (sc->transfer_dir == DIR_OUT) {
1056 memcpy(sc->data_buffer, sc->transfer_data,
1057 sc->transfer_datalen);
1058 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1059 sc->data_buffer, sc->transfer_datalen,
1060 USBD_NO_COPY,/* fixed length transfer */
1061 sc->transfer_xfer[XFER_BBB_DATA]))
1062 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1063
1064 return;
1065 } else {
1066 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1067 USBDEVNAME(sc->sc_dev)));
1068 }
1069
1070 /* FALLTHROUGH if no data phase, err == 0 */
1071 case TSTATE_BBB_DATA:
1072 /* Command transport phase error handling (ignored if no data
1073 * phase (fallthrough from previous state)) */
1074 if (sc->transfer_dir != DIR_NONE) {
1075 /* retrieve the length of the transfer that was done */
1076 usbd_get_xfer_status(xfer, NULL, NULL,
1077 &sc->transfer_actlen, NULL);
1078 DPRINTF(UDMASS_BBB, ("%s: BBB_DATA actlen=%d\n",
1079 USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
1080
1081 if (err) {
1082 DPRINTF(UDMASS_BBB, ("%s: Data-%s %d failed, "
1083 "%s\n", USBDEVNAME(sc->sc_dev),
1084 (sc->transfer_dir == DIR_IN?"in":"out"),
1085 sc->transfer_datalen,usbd_errstr(err)));
1086
1087 if (err == USBD_STALLED) {
1088 sc->transfer_state = TSTATE_BBB_DCLEAR;
1089 umass_clear_endpoint_stall(sc,
1090 (sc->transfer_dir == DIR_IN?
1091 UMASS_BULKIN:UMASS_BULKOUT),
1092 sc->transfer_xfer[XFER_BBB_DCLEAR]);
1093 } else {
1094 /* Unless the error is a pipe stall the
1095 * error is fatal.
1096 */
1097 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1098 }
1099 return;
1100 }
1101 }
1102
1103 /* FALLTHROUGH, err == 0 (no data phase or successful) */
1104 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1105 if (sc->transfer_dir == DIR_IN)
1106 memcpy(sc->transfer_data, sc->data_buffer,
1107 sc->transfer_actlen);
1108
1109 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1110 umass_dump_buffer(sc, sc->transfer_data,
1111 sc->transfer_datalen, 48));
1112
1113 /* FALLTHROUGH, err == 0 (no data phase or successful) */
1114 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1115 /* Reading of CSW after bulk stall condition in data phase
1116 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1117 * reading CSW (TSTATE_BBB_SCLEAR).
1118 * In the case of no data phase or successful data phase,
1119 * err == 0 and the following if block is passed.
1120 */
1121 if (err) { /* should not occur */
1122 printf("%s: BBB bulk-%s stall clear failed, %s\n",
1123 USBDEVNAME(sc->sc_dev),
1124 (sc->transfer_dir == DIR_IN? "in":"out"),
1125 usbd_errstr(err));
1126 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1127 return;
1128 }
1129
1130 /* Status transport phase, setup transfer */
1131 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1132 sc->transfer_state == TSTATE_BBB_DATA ||
1133 sc->transfer_state == TSTATE_BBB_DCLEAR) {
1134 /* After no data phase, successful data phase and
1135 * after clearing bulk-in/-out stall condition
1136 */
1137 sc->transfer_state = TSTATE_BBB_STATUS1;
1138 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1139 } else {
1140 /* After first attempt of fetching CSW */
1141 sc->transfer_state = TSTATE_BBB_STATUS2;
1142 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1143 }
1144
1145 /* Read the Command Status Wrapper via bulk-in endpoint. */
1146 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1147 &sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
1148 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1149 return;
1150 }
1151
1152 return;
1153 case TSTATE_BBB_STATUS1: /* first attempt */
1154 case TSTATE_BBB_STATUS2: /* second attempt */
1155 /* Status transfer, error handling */
1156 if (err) {
1157 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1158 USBDEVNAME(sc->sc_dev), usbd_errstr(err),
1159 (sc->transfer_state == TSTATE_BBB_STATUS1?
1160 ", retrying":"")));
1161
1162 /* If this was the first attempt at fetching the CSW
1163 * retry it, otherwise fail.
1164 */
1165 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1166 sc->transfer_state = TSTATE_BBB_SCLEAR;
1167 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1168 sc->transfer_xfer[XFER_BBB_SCLEAR]);
1169 return;
1170 } else {
1171 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1172 return;
1173 }
1174 }
1175
1176 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1177
1178 /* Translate weird command-status signatures. */
1179 if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) &&
1180 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
1181 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1182
1183 /* Translate invalid command-status tags */
1184 if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG)
1185 USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag));
1186
1187 /* Check CSW and handle any error */
1188 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1189 /* Invalid CSW: Wrong signature or wrong tag might
1190 * indicate that the device is confused -> reset it.
1191 */
1192 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1193 USBDEVNAME(sc->sc_dev),
1194 UGETDW(sc->csw.dCSWSignature),
1195 CSWSIGNATURE);
1196
1197 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1198 return;
1199 } else if (UGETDW(sc->csw.dCSWTag)
1200 != UGETDW(sc->cbw.dCBWTag)) {
1201 printf("%s: Invalid CSW: tag %d should be %d\n",
1202 USBDEVNAME(sc->sc_dev),
1203 UGETDW(sc->csw.dCSWTag),
1204 UGETDW(sc->cbw.dCBWTag));
1205
1206 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1207 return;
1208
1209 /* CSW is valid here */
1210 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1211 printf("%s: Invalid CSW: status %d > %d\n",
1212 USBDEVNAME(sc->sc_dev),
1213 sc->csw.bCSWStatus,
1214 CSWSTATUS_PHASE);
1215
1216 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1217 return;
1218 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1219 printf("%s: Phase Error, residue = %d\n",
1220 USBDEVNAME(sc->sc_dev),
1221 UGETDW(sc->csw.dCSWDataResidue));
1222
1223 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1224 return;
1225
1226 } else if (sc->transfer_actlen > sc->transfer_datalen) {
1227 /* Buffer overrun! Don't let this go by unnoticed */
1228 panic("%s: transferred %d bytes instead of %d bytes",
1229 USBDEVNAME(sc->sc_dev),
1230 sc->transfer_actlen, sc->transfer_datalen);
1231 #if 0
1232 } else if (sc->transfer_datalen - sc->transfer_actlen
1233 != UGETDW(sc->csw.dCSWDataResidue)) {
1234 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1235 USBDEVNAME(sc->sc_dev),
1236 sc->transfer_datalen - sc->transfer_actlen,
1237 UGETDW(sc->csw.dCSWDataResidue)));
1238
1239 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1240 return;
1241 #endif
1242 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1243 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1244 USBDEVNAME(sc->sc_dev),
1245 UGETDW(sc->csw.dCSWDataResidue)));
1246
1247 /* SCSI command failed but transfer was succesful */
1248 sc->transfer_state = TSTATE_IDLE;
1249 sc->transfer_cb(sc, sc->transfer_priv,
1250 UGETDW(sc->csw.dCSWDataResidue),
1251 STATUS_CMD_FAILED);
1252
1253 return;
1254
1255 } else { /* success */
1256 sc->transfer_state = TSTATE_IDLE;
1257 sc->transfer_cb(sc, sc->transfer_priv,
1258 UGETDW(sc->csw.dCSWDataResidue),
1259 STATUS_CMD_OK);
1260
1261 return;
1262 }
1263
1264 /***** Bulk Reset *****/
1265 case TSTATE_BBB_RESET1:
1266 if (err)
1267 printf("%s: BBB reset failed, %s\n",
1268 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1269
1270 sc->transfer_state = TSTATE_BBB_RESET2;
1271 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1272 sc->transfer_xfer[XFER_BBB_RESET2]);
1273
1274 return;
1275 case TSTATE_BBB_RESET2:
1276 if (err) /* should not occur */
1277 printf("%s: BBB bulk-in clear stall failed, %s\n",
1278 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1279 /* no error recovery, otherwise we end up in a loop */
1280
1281 sc->transfer_state = TSTATE_BBB_RESET3;
1282 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1283 sc->transfer_xfer[XFER_BBB_RESET3]);
1284
1285 return;
1286 case TSTATE_BBB_RESET3:
1287 if (err) /* should not occur */
1288 printf("%s: BBB bulk-out clear stall failed, %s\n",
1289 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1290 /* no error recovery, otherwise we end up in a loop */
1291
1292 sc->transfer_state = TSTATE_IDLE;
1293 if (sc->transfer_priv) {
1294 sc->transfer_cb(sc, sc->transfer_priv,
1295 sc->transfer_datalen,
1296 sc->transfer_status);
1297 }
1298
1299 return;
1300
1301 /***** Default *****/
1302 default:
1303 panic("%s: Unknown state %d",
1304 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1305 }
1306 }
1307
1308 /*
1309 * Command/Bulk/Interrupt (CBI) specific functions
1310 */
1311
1312 Static int
1313 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1314 usbd_xfer_handle xfer)
1315 {
1316 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1317 ("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
1318 sc->sc_wire));
1319
1320 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1321 sc->sc_req.bRequest = UR_CBI_ADSC;
1322 USETW(sc->sc_req.wValue, 0);
1323 USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
1324 USETW(sc->sc_req.wLength, buflen);
1325 return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
1326 buflen, 0, xfer);
1327 }
1328
1329
1330 Static void
1331 umass_cbi_reset(struct umass_softc *sc, int status)
1332 {
1333 int i;
1334 # define SEND_DIAGNOSTIC_CMDLEN 12
1335
1336 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1337 ("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
1338 sc->sc_wire));
1339
1340 if (sc->sc_dying)
1341 return;
1342
1343 /*
1344 * Command Block Reset Protocol
1345 *
1346 * First send a reset request to the device. Then clear
1347 * any possibly stalled bulk endpoints.
1348
1349 * This is done in 3 steps, states:
1350 * TSTATE_CBI_RESET1
1351 * TSTATE_CBI_RESET2
1352 * TSTATE_CBI_RESET3
1353 *
1354 * If the reset doesn't succeed, the device should be port reset.
1355 */
1356
1357 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1358 USBDEVNAME(sc->sc_dev)));
1359
1360 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1361 ("%s: CBL struct is too small (%d < %d)\n",
1362 USBDEVNAME(sc->sc_dev),
1363 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1364
1365 sc->transfer_state = TSTATE_CBI_RESET1;
1366 sc->transfer_status = status;
1367
1368 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1369 * the two the last 10 bytes of the cbl is filled with 0xff (section
1370 * 2.2 of the CBI spec).
1371 */
1372 sc->cbl[0] = 0x1d; /* Command Block Reset */
1373 sc->cbl[1] = 0x04;
1374 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1375 sc->cbl[i] = 0xff;
1376
1377 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1378 sc->transfer_xfer[XFER_CBI_RESET1]);
1379 /* XXX if the command fails we should reset the port on the bub */
1380 }
1381
1382 Static void
1383 umass_cbi_transfer(struct umass_softc *sc, int lun,
1384 void *cmd, int cmdlen, void *data, int datalen, int dir,
1385 u_int timeout, umass_callback cb, void *priv)
1386 {
1387 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1388 USBDEVNAME(sc->sc_dev), *(u_char *)cmd, datalen));
1389
1390 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1391 ("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
1392 sc->sc_wire));
1393
1394 if (sc->sc_dying)
1395 return;
1396
1397 /* Be a little generous. */
1398 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
1399
1400 /*
1401 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1402 * a data phase of datalen bytes from/to the device and finally a
1403 * csw read phase.
1404 * If the data direction was inbound a maximum of datalen bytes
1405 * is stored in the buffer pointed to by data.
1406 *
1407 * umass_cbi_transfer initialises the transfer and lets the state
1408 * machine in umass_cbi_state handle the completion. It uses the
1409 * following states:
1410 * TSTATE_CBI_COMMAND
1411 * -> XXX fill in
1412 *
1413 * An error in any of those states will invoke
1414 * umass_cbi_reset.
1415 */
1416
1417 /* check the given arguments */
1418 KASSERT(datalen == 0 || data != NULL,
1419 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1420 KASSERT(datalen == 0 || dir != DIR_NONE,
1421 ("%s: direction is NONE while datalen is not zero\n",
1422 USBDEVNAME(sc->sc_dev)));
1423
1424 /* store the details for the data transfer phase */
1425 sc->transfer_dir = dir;
1426 sc->transfer_data = data;
1427 sc->transfer_datalen = datalen;
1428 sc->transfer_actlen = 0;
1429 sc->transfer_cb = cb;
1430 sc->transfer_priv = priv;
1431 sc->transfer_status = STATUS_CMD_OK;
1432
1433 /* move from idle to the command state */
1434 sc->transfer_state = TSTATE_CBI_COMMAND;
1435
1436 /* Send the Command Block from host to device via control endpoint. */
1437 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
1438 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1439 }
1440
1441 Static void
1442 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1443 usbd_status err)
1444 {
1445 struct umass_softc *sc = (struct umass_softc *) priv;
1446
1447 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1448 ("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
1449 sc->sc_wire));
1450
1451 if (sc->sc_dying)
1452 return;
1453
1454 /*
1455 * State handling for CBI transfers.
1456 */
1457
1458 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1459 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1460 states[sc->transfer_state], xfer, usbd_errstr(err)));
1461
1462 switch (sc->transfer_state) {
1463
1464 /***** CBI Transfer *****/
1465 case TSTATE_CBI_COMMAND:
1466 if (err == USBD_STALLED) {
1467 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1468 USBDEVNAME(sc->sc_dev)));
1469 /* Status transport by control pipe (section 2.3.2.1).
1470 * The command contained in the command block failed.
1471 *
1472 * The control pipe has already been unstalled by the
1473 * USB stack.
1474 * Section 2.4.3.1.1 states that the bulk in endpoints
1475 * should not stalled at this point.
1476 */
1477
1478 sc->transfer_state = TSTATE_IDLE;
1479 sc->transfer_cb(sc, sc->transfer_priv,
1480 sc->transfer_datalen,
1481 STATUS_CMD_FAILED);
1482
1483 return;
1484 } else if (err) {
1485 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1486 USBDEVNAME(sc->sc_dev)));
1487 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1488 return;
1489 }
1490
1491 /* Data transport phase, setup transfer */
1492 sc->transfer_state = TSTATE_CBI_DATA;
1493 if (sc->transfer_dir == DIR_IN) {
1494 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1495 sc->data_buffer, sc->transfer_datalen,
1496 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1497 sc->transfer_xfer[XFER_CBI_DATA]))
1498 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1499
1500 return;
1501 } else if (sc->transfer_dir == DIR_OUT) {
1502 memcpy(sc->data_buffer, sc->transfer_data,
1503 sc->transfer_datalen);
1504 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1505 sc->data_buffer, sc->transfer_datalen,
1506 USBD_NO_COPY,/* fixed length transfer */
1507 sc->transfer_xfer[XFER_CBI_DATA]))
1508 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1509
1510 return;
1511 } else {
1512 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1513 USBDEVNAME(sc->sc_dev)));
1514 }
1515
1516 /* FALLTHROUGH if no data phase, err == 0 */
1517 case TSTATE_CBI_DATA:
1518 /* Command transport phase error handling (ignored if no data
1519 * phase (fallthrough from previous state)) */
1520 if (sc->transfer_dir != DIR_NONE) {
1521 /* retrieve the length of the transfer that was done */
1522 usbd_get_xfer_status(xfer, NULL, NULL,
1523 &sc->transfer_actlen, NULL);
1524 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
1525 USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
1526
1527 if (err) {
1528 DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, "
1529 "%s\n", USBDEVNAME(sc->sc_dev),
1530 (sc->transfer_dir == DIR_IN?"in":"out"),
1531 sc->transfer_datalen,usbd_errstr(err)));
1532
1533 if (err == USBD_STALLED) {
1534 sc->transfer_state = TSTATE_CBI_DCLEAR;
1535 umass_clear_endpoint_stall(sc,
1536 (sc->transfer_dir == DIR_IN?
1537 UMASS_BULKIN:UMASS_BULKOUT),
1538 sc->transfer_xfer[XFER_CBI_DCLEAR]);
1539 } else {
1540 /* Unless the error is a pipe stall the
1541 * error is fatal.
1542 */
1543 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1544 }
1545 return;
1546 }
1547 }
1548
1549 if (sc->transfer_dir == DIR_IN)
1550 memcpy(sc->transfer_data, sc->data_buffer,
1551 sc->transfer_actlen);
1552
1553 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
1554 umass_dump_buffer(sc, sc->transfer_data,
1555 sc->transfer_actlen, 48));
1556
1557 /* Status phase */
1558 if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
1559 sc->transfer_state = TSTATE_CBI_STATUS;
1560 memset(&sc->sbl, 0, sizeof(sc->sbl));
1561 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
1562 &sc->sbl, sizeof(sc->sbl),
1563 0, /* fixed length transfer */
1564 sc->transfer_xfer[XFER_CBI_STATUS]))
1565 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1566 } else {
1567 /* No command completion interrupt. Request
1568 * sense to get status of command.
1569 */
1570 sc->transfer_state = TSTATE_IDLE;
1571 sc->transfer_cb(sc, sc->transfer_priv,
1572 sc->transfer_datalen - sc->transfer_actlen,
1573 STATUS_CMD_UNKNOWN);
1574 }
1575 return;
1576
1577 case TSTATE_CBI_STATUS:
1578 if (err) {
1579 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
1580 USBDEVNAME(sc->sc_dev)));
1581 /* Status transport by interrupt pipe (section 2.3.2.2).
1582 */
1583
1584 if (err == USBD_STALLED) {
1585 sc->transfer_state = TSTATE_CBI_SCLEAR;
1586 umass_clear_endpoint_stall(sc, UMASS_INTRIN,
1587 sc->transfer_xfer[XFER_CBI_SCLEAR]);
1588 } else {
1589 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1590 }
1591 return;
1592 }
1593
1594 /* Dissect the information in the buffer */
1595
1596 {
1597 u_int32_t actlen;
1598 usbd_get_xfer_status(xfer,NULL,NULL,&actlen,NULL);
1599 DPRINTF(UDMASS_CBI, ("%s: CBI_STATUS actlen=%d\n",
1600 USBDEVNAME(sc->sc_dev), actlen));
1601 if (actlen != 2)
1602 break;
1603 }
1604
1605 if (sc->sc_cmd == UMASS_CPROTO_UFI) {
1606 int status;
1607
1608 /* Section 3.4.3.1.3 specifies that the UFI command
1609 * protocol returns an ASC and ASCQ in the interrupt
1610 * data block.
1611 */
1612
1613 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
1614 "ASCQ = 0x%02x\n",
1615 USBDEVNAME(sc->sc_dev),
1616 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
1617
1618 if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) ||
1619 sc->sc_sense)
1620 status = STATUS_CMD_OK;
1621 else
1622 status = STATUS_CMD_FAILED;
1623
1624 /* No autosense, command successful */
1625 sc->transfer_state = TSTATE_IDLE;
1626 sc->transfer_cb(sc, sc->transfer_priv,
1627 sc->transfer_datalen - sc->transfer_actlen, status);
1628 } else {
1629 int status;
1630
1631 /* Command Interrupt Data Block */
1632
1633 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
1634 USBDEVNAME(sc->sc_dev),
1635 sc->sbl.common.type, sc->sbl.common.value));
1636
1637 if (sc->sbl.common.type == IDB_TYPE_CCI) {
1638 switch (sc->sbl.common.value & IDB_VALUE_STATUS_MASK) {
1639 case IDB_VALUE_PASS:
1640 status = STATUS_CMD_OK;
1641 break;
1642 case IDB_VALUE_FAIL:
1643 case IDB_VALUE_PERSISTENT:
1644 status = STATUS_CMD_FAILED;
1645 break;
1646 case IDB_VALUE_PHASE:
1647 default: /* XXX: gcc */
1648 status = STATUS_WIRE_FAILED;
1649 break;
1650 }
1651
1652 sc->transfer_state = TSTATE_IDLE;
1653 sc->transfer_cb(sc, sc->transfer_priv,
1654 sc->transfer_datalen - sc->transfer_actlen, status);
1655 }
1656 }
1657 return;
1658
1659 case TSTATE_CBI_DCLEAR:
1660 if (err) { /* should not occur */
1661 printf("%s: CBI bulk-%s stall clear failed, %s\n",
1662 USBDEVNAME(sc->sc_dev),
1663 (sc->transfer_dir == DIR_IN? "in":"out"),
1664 usbd_errstr(err));
1665 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1666 } else {
1667 sc->transfer_state = TSTATE_IDLE;
1668 sc->transfer_cb(sc, sc->transfer_priv,
1669 sc->transfer_datalen, STATUS_CMD_FAILED);
1670 }
1671 return;
1672
1673 case TSTATE_CBI_SCLEAR:
1674 if (err) { /* should not occur */
1675 printf("%s: CBI intr-in stall clear failed, %s\n",
1676 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1677 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1678 } else {
1679 sc->transfer_state = TSTATE_IDLE;
1680 sc->transfer_cb(sc, sc->transfer_priv,
1681 sc->transfer_datalen, STATUS_CMD_FAILED);
1682 }
1683 return;
1684
1685 /***** CBI Reset *****/
1686 case TSTATE_CBI_RESET1:
1687 if (err)
1688 printf("%s: CBI reset failed, %s\n",
1689 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1690
1691 sc->transfer_state = TSTATE_CBI_RESET2;
1692 umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1693 sc->transfer_xfer[XFER_CBI_RESET2]);
1694
1695 return;
1696 case TSTATE_CBI_RESET2:
1697 if (err) /* should not occur */
1698 printf("%s: CBI bulk-in stall clear failed, %s\n",
1699 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1700 /* no error recovery, otherwise we end up in a loop */
1701
1702 sc->transfer_state = TSTATE_CBI_RESET3;
1703 umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1704 sc->transfer_xfer[XFER_CBI_RESET3]);
1705
1706 return;
1707 case TSTATE_CBI_RESET3:
1708 if (err) /* should not occur */
1709 printf("%s: CBI bulk-out stall clear failed, %s\n",
1710 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1711 /* no error recovery, otherwise we end up in a loop */
1712
1713 sc->transfer_state = TSTATE_IDLE;
1714 if (sc->transfer_priv) {
1715 sc->transfer_cb(sc, sc->transfer_priv,
1716 sc->transfer_datalen,
1717 sc->transfer_status);
1718 }
1719
1720 return;
1721
1722
1723 /***** Default *****/
1724 default:
1725 panic("%s: Unknown state %d",
1726 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1727 }
1728 }
1729
1730 usbd_status
1731 umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
1732 {
1733 usb_device_request_t req;
1734 usbd_status err;
1735
1736 *maxlun = 0; /* Default to 0. */
1737
1738 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
1739
1740 /* The Get Max Lun command is a class-specific request. */
1741 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1742 req.bRequest = UR_BBB_GET_MAX_LUN;
1743 USETW(req.wValue, 0);
1744 USETW(req.wIndex, sc->sc_ifaceno);
1745 USETW(req.wLength, 1);
1746
1747 err = usbd_do_request_flags(sc->sc_udev, &req, maxlun,
1748 USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT);
1749 switch (err) {
1750 case USBD_NORMAL_COMPLETION:
1751 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
1752 USBDEVNAME(sc->sc_dev), *maxlun));
1753 break;
1754
1755 case USBD_STALLED:
1756 /*
1757 * Device doesn't support Get Max Lun request.
1758 */
1759 err = USBD_NORMAL_COMPLETION;
1760 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
1761 USBDEVNAME(sc->sc_dev)));
1762 break;
1763
1764 case USBD_SHORT_XFER:
1765 /*
1766 * XXX This must mean Get Max Lun is not supported, too!
1767 */
1768 err = USBD_NORMAL_COMPLETION;
1769 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
1770 USBDEVNAME(sc->sc_dev)));
1771 break;
1772
1773 default:
1774 printf("%s: Get Max Lun failed: %s\n",
1775 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1776 /* XXX Should we port_reset the device? */
1777 break;
1778 }
1779
1780 return (err);
1781 }
1782
1783
1784
1785
1786 #ifdef UMASS_DEBUG
1787 Static void
1788 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
1789 {
1790 int clen = cbw->bCDBLength;
1791 int dlen = UGETDW(cbw->dCBWDataTransferLength);
1792 u_int8_t *c = cbw->CBWCDB;
1793 int tag = UGETDW(cbw->dCBWTag);
1794 int flags = cbw->bCBWFlags;
1795
1796 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen=%d "
1797 "(0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%s), "
1798 "data = %d bytes, dir = %s\n",
1799 USBDEVNAME(sc->sc_dev), tag, clen,
1800 c[0], c[1], c[2], c[3], c[4], c[5],
1801 c[6], c[7], c[8], c[9],
1802 (clen > 10? "...":""),
1803 dlen, (flags == CBWFLAGS_IN? "in":
1804 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
1805 }
1806
1807 Static void
1808 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
1809 {
1810 int sig = UGETDW(csw->dCSWSignature);
1811 int tag = UGETDW(csw->dCSWTag);
1812 int res = UGETDW(csw->dCSWDataResidue);
1813 int status = csw->bCSWStatus;
1814
1815 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
1816 "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
1817 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
1818 tag, res,
1819 status, (status == CSWSTATUS_GOOD? "good":
1820 (status == CSWSTATUS_FAILED? "failed":
1821 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
1822 }
1823
1824 Static void
1825 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
1826 int printlen)
1827 {
1828 int i, j;
1829 char s1[40];
1830 char s2[40];
1831 char s3[5];
1832
1833 s1[0] = '\0';
1834 s3[0] = '\0';
1835
1836 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
1837 for (i = 0; i < buflen && i < printlen; i++) {
1838 j = i % 16;
1839 if (j == 0 && i != 0) {
1840 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
1841 USBDEVNAME(sc->sc_dev), s1, s2));
1842 s2[0] = '\0';
1843 }
1844 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
1845 }
1846 if (buflen > printlen)
1847 sprintf(s3, " ...");
1848 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
1849 USBDEVNAME(sc->sc_dev), s1, s2, s3));
1850 }
1851 #endif
Cache object: 729b59ae7581751e381ee46be88c9dfe
|