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