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