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