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