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