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