umass.c revision 1.66 1 /* $NetBSD: umass.c,v 1.66 2001/11/23 01:15:28 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.66 2001/11/23 01:15:28 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 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 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,
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 /*
932 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
933 * a data phase of datalen bytes from/to the device and finally a
934 * csw read phase.
935 * If the data direction was inbound a maximum of datalen bytes
936 * is stored in the buffer pointed to by data.
937 *
938 * umass_bbb_transfer initialises the transfer and lets the state
939 * machine in umass_bbb_state handle the completion. It uses the
940 * following states:
941 * TSTATE_BBB_COMMAND
942 * -> TSTATE_BBB_DATA
943 * -> TSTATE_BBB_STATUS
944 * -> TSTATE_BBB_STATUS2
945 * -> TSTATE_BBB_IDLE
946 *
947 * An error in any of those states will invoke
948 * umass_bbb_reset.
949 */
950
951 /* check the given arguments */
952 KASSERT(datalen == 0 || data != NULL,
953 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
954 KASSERT(cmdlen <= CBWCDBLENGTH,
955 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
956 USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH));
957 KASSERT(dir == DIR_NONE || datalen > 0,
958 ("%s: datalen == 0 while direction is not NONE\n",
959 USBDEVNAME(sc->sc_dev)));
960 KASSERT(datalen == 0 || dir != DIR_NONE,
961 ("%s: direction is NONE while datalen is not zero\n",
962 USBDEVNAME(sc->sc_dev)));
963 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
964 ("%s: CBW struct does not have the right size (%d vs. %d)\n",
965 USBDEVNAME(sc->sc_dev),
966 sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
967 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
968 ("%s: CSW struct does not have the right size (%d vs. %d)\n",
969 USBDEVNAME(sc->sc_dev),
970 sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
971
972 /*
973 * Determine the direction of the data transfer and the length.
974 *
975 * dCBWDataTransferLength (datalen) :
976 * This field indicates the number of bytes of data that the host
977 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
978 * the Direction bit) during the execution of this command. If this
979 * field is set to 0, the device will expect that no data will be
980 * transferred IN or OUT during this command, regardless of the value
981 * of the Direction bit defined in dCBWFlags.
982 *
983 * dCBWFlags (dir) :
984 * The bits of the Flags field are defined as follows:
985 * Bits 0-6 reserved
986 * Bit 7 Direction - this bit shall be ignored if the
987 * dCBWDataTransferLength field is zero.
988 * 0 = data Out from host to device
989 * 1 = data In from device to host
990 */
991
992 /* Fill in the Command Block Wrapper */
993 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
994 USETDW(sc->cbw.dCBWTag, dCBWtag);
995 dCBWtag++; /* cannot be done in macro (it will be done 4 times) */
996 USETDW(sc->cbw.dCBWDataTransferLength, datalen);
997 /* DIR_NONE is treated as DIR_OUT (0x00) */
998 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
999 sc->cbw.bCBWLUN = lun;
1000 sc->cbw.bCDBLength = cmdlen;
1001 bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
1002
1003 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1004
1005 /* store the details for the data transfer phase */
1006 sc->transfer_dir = dir;
1007 sc->transfer_data = data;
1008 sc->transfer_datalen = datalen;
1009 sc->transfer_actlen = 0;
1010 sc->transfer_cb = cb;
1011 sc->transfer_priv = priv;
1012 sc->transfer_status = STATUS_CMD_OK;
1013
1014 /* move from idle to the command state */
1015 sc->transfer_state = TSTATE_BBB_COMMAND;
1016
1017 /* Send the CBW from host to device via bulk-out endpoint. */
1018 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1019 &sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1020 sc->transfer_xfer[XFER_BBB_CBW])) {
1021 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1022 }
1023 }
1024
1025
1026 Static void
1027 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1028 usbd_status err)
1029 {
1030 struct umass_softc *sc = (struct umass_softc *) priv;
1031 usbd_xfer_handle next_xfer;
1032
1033 KASSERT(sc->proto & PROTO_BBB,
1034 ("sc->proto == 0x%02x wrong for umass_bbb_state\n",sc->proto));
1035
1036 if (sc->sc_dying)
1037 return;
1038
1039 /*
1040 * State handling for BBB transfers.
1041 *
1042 * The subroutine is rather long. It steps through the states given in
1043 * Annex A of the Bulk-Only specification.
1044 * Each state first does the error handling of the previous transfer
1045 * and then prepares the next transfer.
1046 * Each transfer is done asynchroneously so after the request/transfer
1047 * has been submitted you will find a 'return;'.
1048 */
1049
1050 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1051 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1052 states[sc->transfer_state], xfer, usbd_errstr(err)));
1053
1054 switch (sc->transfer_state) {
1055
1056 /***** Bulk Transfer *****/
1057 case TSTATE_BBB_COMMAND:
1058 /* Command transport phase, error handling */
1059 if (err) {
1060 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1061 USBDEVNAME(sc->sc_dev)));
1062 /* If the device detects that the CBW is invalid, then
1063 * the device may STALL both bulk endpoints and require
1064 * a Bulk-Reset
1065 */
1066 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1067 return;
1068 }
1069
1070 /* Data transport phase, setup transfer */
1071 sc->transfer_state = TSTATE_BBB_DATA;
1072 if (sc->transfer_dir == DIR_IN) {
1073 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1074 sc->data_buffer, sc->transfer_datalen,
1075 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1076 sc->transfer_xfer[XFER_BBB_DATA]))
1077 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1078
1079 return;
1080 } else if (sc->transfer_dir == DIR_OUT) {
1081 memcpy(sc->data_buffer, sc->transfer_data,
1082 sc->transfer_datalen);
1083 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1084 sc->data_buffer, sc->transfer_datalen,
1085 USBD_NO_COPY,/* fixed length transfer */
1086 sc->transfer_xfer[XFER_BBB_DATA]))
1087 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1088
1089 return;
1090 } else {
1091 DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1092 USBDEVNAME(sc->sc_dev)));
1093 }
1094
1095 /* FALLTHROUGH if no data phase, err == 0 */
1096 case TSTATE_BBB_DATA:
1097 /* Command transport phase, error handling (ignored if no data
1098 * phase (fallthrough from previous state)) */
1099 if (sc->transfer_dir != DIR_NONE) {
1100 /* retrieve the length of the transfer that was done */
1101 usbd_get_xfer_status(xfer, NULL, NULL,
1102 &sc->transfer_actlen, NULL);
1103
1104 if (err) {
1105 DPRINTF(UDMASS_BBB, ("%s: Data-%s %d failed, "
1106 "%s\n", USBDEVNAME(sc->sc_dev),
1107 (sc->transfer_dir == DIR_IN?"in":"out"),
1108 sc->transfer_datalen,usbd_errstr(err)));
1109
1110 if (err == USBD_STALLED) {
1111 umass_clear_endpoint_stall(sc,
1112 (sc->transfer_dir == DIR_IN?
1113 sc->bulkin:sc->bulkout),
1114 (sc->transfer_dir == DIR_IN?
1115 sc->bulkin_pipe:sc->bulkout_pipe),
1116 TSTATE_BBB_DCLEAR,
1117 sc->transfer_xfer[XFER_BBB_DCLEAR]);
1118 return;
1119 } else {
1120 /* Unless the error is a pipe stall the
1121 * error is fatal.
1122 */
1123 umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1124 return;
1125 }
1126 }
1127 }
1128
1129 if (sc->transfer_dir == DIR_IN)
1130 memcpy(sc->transfer_data, sc->data_buffer,
1131 sc->transfer_actlen);
1132
1133 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1134 umass_dump_buffer(sc, sc->transfer_data,
1135 sc->transfer_datalen, 48));
1136
1137 /* FALLTHROUGH, err == 0 (no data phase or successfull) */
1138 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1139 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1140 /* Reading of CSW after bulk stall condition in data phase
1141 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1142 * reading CSW (TSTATE_BBB_SCLEAR).
1143 * In the case of no data phase or successfull data phase,
1144 * err == 0 and the following if block is passed.
1145 */
1146 if (err) { /* should not occur */
1147 /* try the transfer below, even if clear stall failed */
1148 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
1149 ", %s\n", USBDEVNAME(sc->sc_dev),
1150 (sc->transfer_dir == DIR_IN? "in":"out"),
1151 usbd_errstr(err)));
1152 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1153 return;
1154 }
1155
1156 /* Status transport phase, setup transfer */
1157 if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1158 sc->transfer_state == TSTATE_BBB_DATA ||
1159 sc->transfer_state == TSTATE_BBB_DCLEAR) {
1160 /* After no data phase, successfull data phase and
1161 * after clearing bulk-in/-out stall condition
1162 */
1163 sc->transfer_state = TSTATE_BBB_STATUS1;
1164 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1165 } else {
1166 /* After first attempt of fetching CSW */
1167 sc->transfer_state = TSTATE_BBB_STATUS2;
1168 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1169 }
1170
1171 /* Read the Command Status Wrapper via bulk-in endpoint. */
1172 if (umass_setup_transfer(sc, sc->bulkin_pipe, &sc->csw,
1173 UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
1174 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1175 return;
1176 }
1177
1178 return;
1179 case TSTATE_BBB_STATUS1: /* first attempt */
1180 case TSTATE_BBB_STATUS2: /* second attempt */
1181 /* Status transfer, error handling */
1182 if (err) {
1183 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1184 USBDEVNAME(sc->sc_dev), usbd_errstr(err),
1185 (sc->transfer_state == TSTATE_BBB_STATUS1?
1186 ", retrying":"")));
1187
1188 /* If this was the first attempt at fetching the CSW
1189 * retry it, otherwise fail.
1190 */
1191 if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1192 umass_clear_endpoint_stall(sc,
1193 sc->bulkin, sc->bulkin_pipe,
1194 TSTATE_BBB_SCLEAR,
1195 sc->transfer_xfer[XFER_BBB_SCLEAR]);
1196 return;
1197 } else {
1198 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1199 return;
1200 }
1201 }
1202
1203 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1204
1205 /* Check CSW and handle any error */
1206 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1207 /* Invalid CSW: Wrong signature or wrong tag might
1208 * indicate that the device is confused -> reset it.
1209 */
1210 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1211 USBDEVNAME(sc->sc_dev),
1212 UGETDW(sc->csw.dCSWSignature),
1213 CSWSIGNATURE);
1214
1215 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1216 return;
1217 } else if (UGETDW(sc->csw.dCSWTag)
1218 != UGETDW(sc->cbw.dCBWTag)) {
1219 printf("%s: Invalid CSW: tag %d should be %d\n",
1220 USBDEVNAME(sc->sc_dev),
1221 UGETDW(sc->csw.dCSWTag),
1222 UGETDW(sc->cbw.dCBWTag));
1223
1224 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1225 return;
1226
1227 /* CSW is valid here */
1228 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1229 printf("%s: Invalid CSW: status %d > %d\n",
1230 USBDEVNAME(sc->sc_dev),
1231 sc->csw.bCSWStatus,
1232 CSWSTATUS_PHASE);
1233
1234 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1235 return;
1236 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1237 printf("%s: Phase Error, residue = %d\n",
1238 USBDEVNAME(sc->sc_dev),
1239 UGETDW(sc->csw.dCSWDataResidue));
1240
1241 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1242 return;
1243
1244 } else if (sc->transfer_actlen > sc->transfer_datalen) {
1245 /* Buffer overrun! Don't let this go by unnoticed */
1246 panic("%s: transferred %d bytes instead of %d bytes\n",
1247 USBDEVNAME(sc->sc_dev),
1248 sc->transfer_actlen, sc->transfer_datalen);
1249 #if 0
1250 } else if (sc->transfer_datalen - sc->transfer_actlen
1251 != UGETDW(sc->csw.dCSWDataResidue)) {
1252 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1253 USBDEVNAME(sc->sc_dev),
1254 sc->transfer_datalen - sc->transfer_actlen,
1255 UGETDW(sc->csw.dCSWDataResidue)));
1256
1257 umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1258 return;
1259 #endif
1260 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1261 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1262 USBDEVNAME(sc->sc_dev),
1263 UGETDW(sc->csw.dCSWDataResidue)));
1264
1265 /* SCSI command failed but transfer was succesful */
1266 sc->transfer_state = TSTATE_IDLE;
1267 sc->transfer_cb(sc, sc->transfer_priv,
1268 UGETDW(sc->csw.dCSWDataResidue),
1269 STATUS_CMD_FAILED);
1270
1271 return;
1272
1273 } else { /* success */
1274 sc->transfer_state = TSTATE_IDLE;
1275 sc->transfer_cb(sc, sc->transfer_priv,
1276 UGETDW(sc->csw.dCSWDataResidue),
1277 STATUS_CMD_OK);
1278
1279 return;
1280 }
1281
1282 /***** Bulk Reset *****/
1283 case TSTATE_BBB_RESET1:
1284 if (err)
1285 printf("%s: BBB reset failed, %s\n",
1286 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1287
1288 umass_clear_endpoint_stall(sc,
1289 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
1290 sc->transfer_xfer[XFER_BBB_RESET2]);
1291
1292 return;
1293 case TSTATE_BBB_RESET2:
1294 if (err) /* should not occur */
1295 printf("%s: BBB bulk-in clear stall failed, %s\n",
1296 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1297 /* no error recovery, otherwise we end up in a loop */
1298
1299 umass_clear_endpoint_stall(sc,
1300 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
1301 sc->transfer_xfer[XFER_BBB_RESET3]);
1302
1303 return;
1304 case TSTATE_BBB_RESET3:
1305 if (err) /* should not occur */
1306 printf("%s: BBB bulk-out clear stall failed, %s\n",
1307 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1308 /* no error recovery, otherwise we end up in a loop */
1309
1310 sc->transfer_state = TSTATE_IDLE;
1311 if (sc->transfer_priv) {
1312 sc->transfer_cb(sc, sc->transfer_priv,
1313 sc->transfer_datalen,
1314 sc->transfer_status);
1315 }
1316
1317 return;
1318
1319 /***** Default *****/
1320 default:
1321 panic("%s: Unknown state %d\n",
1322 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1323 }
1324 }
1325
1326 /*
1327 * Command/Bulk/Interrupt (CBI) specific functions
1328 */
1329
1330 Static int
1331 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1332 usbd_xfer_handle xfer)
1333 {
1334 usbd_device_handle dev;
1335
1336 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1337 ("sc->proto == 0x%02x wrong for umass_cbi_adsc\n",sc->proto));
1338
1339 usbd_interface2device_handle(sc->iface, &dev);
1340
1341 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1342 sc->request.bRequest = UR_CBI_ADSC;
1343 USETW(sc->request.wValue, 0);
1344 USETW(sc->request.wIndex, sc->ifaceno);
1345 USETW(sc->request.wLength, buflen);
1346 return umass_setup_ctrl_transfer(sc, dev, &sc->request, buffer,
1347 buflen, 0, xfer);
1348 }
1349
1350
1351 Static void
1352 umass_cbi_reset(struct umass_softc *sc, int status)
1353 {
1354 int i;
1355 # define SEND_DIAGNOSTIC_CMDLEN 12
1356
1357 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1358 ("sc->proto == 0x%02x wrong for umass_cbi_reset\n",sc->proto));
1359
1360 if (sc->sc_dying)
1361 return;
1362
1363 /*
1364 * Command Block Reset Protocol
1365 *
1366 * First send a reset request to the device. Then clear
1367 * any possibly stalled bulk endpoints.
1368
1369 * This is done in 3 steps, states:
1370 * TSTATE_CBI_RESET1
1371 * TSTATE_CBI_RESET2
1372 * TSTATE_CBI_RESET3
1373 *
1374 * If the reset doesn't succeed, the device should be port reset.
1375 */
1376
1377 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1378 USBDEVNAME(sc->sc_dev)));
1379
1380 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1381 ("%s: CBL struct is too small (%d < %d)\n",
1382 USBDEVNAME(sc->sc_dev),
1383 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1384
1385 sc->transfer_state = TSTATE_CBI_RESET1;
1386 sc->transfer_status = status;
1387
1388 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1389 * the two the last 10 bytes of the cbl is filled with 0xff (section
1390 * 2.2 of the CBI spec).
1391 */
1392 sc->cbl[0] = 0x1d; /* Command Block Reset */
1393 sc->cbl[1] = 0x04;
1394 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1395 sc->cbl[i] = 0xff;
1396
1397 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1398 sc->transfer_xfer[XFER_CBI_RESET1]);
1399 /* XXX if the command fails we should reset the port on the bub */
1400 }
1401
1402 Static void
1403 umass_cbi_transfer(struct umass_softc *sc, int lun,
1404 void *cmd, int cmdlen, void *data, int datalen, int dir,
1405 transfer_cb_f cb, void *priv)
1406 {
1407 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1408 USBDEVNAME(sc->sc_dev), *(u_char*)cmd, datalen));
1409
1410 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1411 ("sc->proto == 0x%02x wrong for umass_cbi_transfer\n",
1412 sc->proto));
1413
1414 if (sc->sc_dying)
1415 return;
1416
1417 /*
1418 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1419 * a data phase of datalen bytes from/to the device and finally a
1420 * csw read phase.
1421 * If the data direction was inbound a maximum of datalen bytes
1422 * is stored in the buffer pointed to by data.
1423 *
1424 * umass_cbi_transfer initialises the transfer and lets the state
1425 * machine in umass_cbi_state handle the completion. It uses the
1426 * following states:
1427 * TSTATE_CBI_COMMAND
1428 * -> XXX fill in
1429 *
1430 * An error in any of those states will invoke
1431 * umass_cbi_reset.
1432 */
1433
1434 /* check the given arguments */
1435 KASSERT(datalen == 0 || data != NULL,
1436 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
1437 KASSERT(datalen == 0 || dir != DIR_NONE,
1438 ("%s: direction is NONE while datalen is not zero\n",
1439 USBDEVNAME(sc->sc_dev)));
1440
1441 /* store the details for the data transfer phase */
1442 sc->transfer_dir = dir;
1443 sc->transfer_data = data;
1444 sc->transfer_datalen = datalen;
1445 sc->transfer_actlen = 0;
1446 sc->transfer_cb = cb;
1447 sc->transfer_priv = priv;
1448 sc->transfer_status = STATUS_CMD_OK;
1449
1450 /* move from idle to the command state */
1451 sc->transfer_state = TSTATE_CBI_COMMAND;
1452
1453 /* Send the Command Block from host to device via control endpoint. */
1454 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
1455 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1456 }
1457
1458 Static void
1459 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1460 usbd_status err)
1461 {
1462 struct umass_softc *sc = (struct umass_softc *) priv;
1463
1464 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
1465 ("sc->proto == 0x%02x wrong for umass_cbi_state\n", sc->proto));
1466
1467 if (sc->sc_dying)
1468 return;
1469
1470 /*
1471 * State handling for CBI transfers.
1472 */
1473
1474 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1475 USBDEVNAME(sc->sc_dev), sc->transfer_state,
1476 states[sc->transfer_state], xfer, usbd_errstr(err)));
1477
1478 switch (sc->transfer_state) {
1479
1480 /***** CBI Transfer *****/
1481 case TSTATE_CBI_COMMAND:
1482 if (err == USBD_STALLED) {
1483 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1484 USBDEVNAME(sc->sc_dev)));
1485 /* Status transport by control pipe (section 2.3.2.1).
1486 * The command contained in the command block failed.
1487 *
1488 * The control pipe has already been unstalled by the
1489 * USB stack.
1490 * Section 2.4.3.1.1 states that the bulk in endpoints
1491 * should not stalled at this point.
1492 */
1493
1494 sc->transfer_state = TSTATE_IDLE;
1495 sc->transfer_cb(sc, sc->transfer_priv,
1496 sc->transfer_datalen,
1497 STATUS_CMD_FAILED);
1498
1499 return;
1500 } else if (err) {
1501 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1502 USBDEVNAME(sc->sc_dev)));
1503 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1504
1505 return;
1506 }
1507
1508 sc->transfer_state = TSTATE_CBI_DATA;
1509 if (sc->transfer_dir == DIR_IN) {
1510 if (umass_setup_transfer(sc, sc->bulkin_pipe,
1511 sc->transfer_data, sc->transfer_datalen,
1512 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1513 sc->transfer_xfer[XFER_CBI_DATA]))
1514 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1515
1516 } else if (sc->transfer_dir == DIR_OUT) {
1517 memcpy(sc->data_buffer, sc->transfer_data,
1518 sc->transfer_datalen);
1519 if (umass_setup_transfer(sc, sc->bulkout_pipe,
1520 sc->transfer_data, sc->transfer_datalen,
1521 USBD_NO_COPY,/* fixed length transfer */
1522 sc->transfer_xfer[XFER_CBI_DATA]))
1523 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1524
1525 } else if (sc->wire_proto == WPROTO_CBI_I) {
1526 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1527 USBDEVNAME(sc->sc_dev)));
1528 sc->transfer_state = TSTATE_CBI_STATUS;
1529 if (umass_setup_transfer(sc, sc->intrin_pipe,
1530 &sc->sbl, sizeof(sc->sbl),
1531 0, /* fixed length transfer */
1532 sc->transfer_xfer[XFER_CBI_STATUS])){
1533 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1534 }
1535 } else {
1536 DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1537 USBDEVNAME(sc->sc_dev)));
1538 /* No command completion interrupt. Request
1539 * sense data.
1540 */
1541 sc->transfer_state = TSTATE_IDLE;
1542 sc->transfer_cb(sc, sc->transfer_priv,
1543 0, STATUS_CMD_UNKNOWN);
1544 }
1545
1546 return;
1547
1548 case TSTATE_CBI_DATA:
1549 /* retrieve the length of the transfer that was done */
1550 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
1551 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
1552 USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
1553
1554 if (err) {
1555 DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, "
1556 "%s\n", USBDEVNAME(sc->sc_dev),
1557 (sc->transfer_dir == DIR_IN?"in":"out"),
1558 sc->transfer_datalen,usbd_errstr(err)));
1559
1560 if (err == USBD_STALLED) {
1561 umass_clear_endpoint_stall(sc,
1562 sc->bulkin, sc->bulkin_pipe,
1563 TSTATE_CBI_DCLEAR,
1564 sc->transfer_xfer[XFER_CBI_DCLEAR]);
1565 } else {
1566 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1567 }
1568 return;
1569 }
1570
1571 if (sc->transfer_dir == DIR_IN)
1572 memcpy(sc->transfer_data, sc->data_buffer,
1573 sc->transfer_actlen);
1574
1575 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
1576 umass_dump_buffer(sc, sc->transfer_data,
1577 sc->transfer_actlen, 48));
1578
1579 if (sc->wire_proto == WPROTO_CBI_I) {
1580 sc->transfer_state = TSTATE_CBI_STATUS;
1581 memset(&sc->sbl, 0, sizeof(sc->sbl));
1582 if (umass_setup_transfer(sc, sc->intrin_pipe,
1583 &sc->sbl, sizeof(sc->sbl),
1584 0, /* fixed length transfer */
1585 sc->transfer_xfer[XFER_CBI_STATUS])){
1586 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1587 }
1588 } else {
1589 /* No command completion interrupt. Request
1590 * sense to get status of command.
1591 */
1592 sc->transfer_state = TSTATE_IDLE;
1593 sc->transfer_cb(sc, sc->transfer_priv,
1594 sc->transfer_datalen - sc->transfer_actlen,
1595 STATUS_CMD_UNKNOWN);
1596 }
1597 return;
1598
1599 case TSTATE_CBI_STATUS:
1600 if (err) {
1601 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
1602 USBDEVNAME(sc->sc_dev)));
1603 /* Status transport by interrupt pipe (section 2.3.2.2).
1604 */
1605
1606 if (err == USBD_STALLED) {
1607 umass_clear_endpoint_stall(sc,
1608 sc->intrin, sc->intrin_pipe,
1609 TSTATE_CBI_SCLEAR,
1610 sc->transfer_xfer[XFER_CBI_SCLEAR]);
1611 } else {
1612 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1613 }
1614 return;
1615 }
1616
1617 /* Dissect the information in the buffer */
1618
1619 if (sc->cmd_proto == CPROTO_UFI) {
1620 int status;
1621
1622 /* Section 3.4.3.1.3 specifies that the UFI command
1623 * protocol returns an ASC and ASCQ in the interrupt
1624 * data block.
1625 */
1626
1627 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
1628 "ASCQ = 0x%02x\n",
1629 USBDEVNAME(sc->sc_dev),
1630 sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
1631
1632 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
1633 status = STATUS_CMD_OK;
1634 else
1635 status = STATUS_CMD_FAILED;
1636
1637 /* No sense, command successfull */
1638 } else {
1639 /* Command Interrupt Data Block */
1640 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
1641 USBDEVNAME(sc->sc_dev),
1642 sc->sbl.common.type, sc->sbl.common.value));
1643
1644 if (sc->sbl.common.type == IDB_TYPE_CCI) {
1645 int err;
1646
1647 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
1648 == IDB_VALUE_PASS) {
1649 err = STATUS_CMD_OK;
1650 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
1651 == IDB_VALUE_FAIL ||
1652 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
1653 == IDB_VALUE_PERSISTENT) {
1654 err = STATUS_CMD_FAILED;
1655 } else {
1656 err = STATUS_WIRE_FAILED;
1657 }
1658
1659 sc->transfer_state = TSTATE_IDLE;
1660 sc->transfer_cb(sc, sc->transfer_priv,
1661 sc->transfer_datalen,
1662 err);
1663 }
1664 }
1665 return;
1666
1667 case TSTATE_CBI_DCLEAR:
1668 if (err) { /* should not occur */
1669 printf("%s: CBI bulk-in/out stall clear failed, %s\n",
1670 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1671 umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1672 }
1673
1674 sc->transfer_state = TSTATE_IDLE;
1675 sc->transfer_cb(sc, sc->transfer_priv,
1676 sc->transfer_datalen,
1677 STATUS_CMD_FAILED);
1678 return;
1679
1680 case TSTATE_CBI_SCLEAR:
1681 if (err) /* should not occur */
1682 printf("%s: CBI intr-in stall clear failed, %s\n",
1683 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1684
1685 /* Something really bad is going on. Reset the device */
1686 umass_cbi_reset(sc, STATUS_CMD_FAILED);
1687 return;
1688
1689 /***** CBI Reset *****/
1690 case TSTATE_CBI_RESET1:
1691 if (err)
1692 printf("%s: CBI reset failed, %s\n",
1693 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1694
1695 umass_clear_endpoint_stall(sc,
1696 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
1697 sc->transfer_xfer[XFER_CBI_RESET2]);
1698
1699 return;
1700 case TSTATE_CBI_RESET2:
1701 if (err) /* should not occur */
1702 printf("%s: CBI bulk-in stall clear failed, %s\n",
1703 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1704 /* no error recovery, otherwise we end up in a loop */
1705
1706 umass_clear_endpoint_stall(sc,
1707 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
1708 sc->transfer_xfer[XFER_CBI_RESET3]);
1709
1710 return;
1711 case TSTATE_CBI_RESET3:
1712 if (err) /* should not occur */
1713 printf("%s: CBI bulk-out stall clear failed, %s\n",
1714 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1715 /* no error recovery, otherwise we end up in a loop */
1716
1717 sc->transfer_state = TSTATE_IDLE;
1718 if (sc->transfer_priv) {
1719 sc->transfer_cb(sc, sc->transfer_priv,
1720 sc->transfer_datalen,
1721 sc->transfer_status);
1722 }
1723
1724 return;
1725
1726
1727 /***** Default *****/
1728 default:
1729 panic("%s: Unknown state %d\n",
1730 USBDEVNAME(sc->sc_dev), sc->transfer_state);
1731 }
1732 }
1733
1734 usbd_status
1735 umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
1736 {
1737 usbd_device_handle dev;
1738 usb_device_request_t req;
1739 usbd_status err;
1740 usb_interface_descriptor_t *id;
1741
1742 *maxlun = 0; /* Default to 0. */
1743
1744 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
1745
1746 usbd_interface2device_handle(sc->iface, &dev);
1747 id = usbd_get_interface_descriptor(sc->iface);
1748
1749 /* The Get Max Lun command is a class-specific request. */
1750 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1751 req.bRequest = UR_BBB_GET_MAX_LUN;
1752 USETW(req.wValue, 0);
1753 USETW(req.wIndex, id->bInterfaceNumber);
1754 USETW(req.wLength, 1);
1755
1756 err = usbd_do_request(dev, &req, maxlun);
1757 switch (err) {
1758 case USBD_NORMAL_COMPLETION:
1759 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
1760 USBDEVNAME(sc->sc_dev), *maxlun));
1761 break;
1762
1763 case USBD_STALLED:
1764 /*
1765 * Device doesn't support Get Max Lun request.
1766 */
1767 err = USBD_NORMAL_COMPLETION;
1768 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
1769 USBDEVNAME(sc->sc_dev)));
1770 break;
1771
1772 case USBD_SHORT_XFER:
1773 /*
1774 * XXX This must mean Get Max Lun is not supported, too!
1775 */
1776 err = USBD_NORMAL_COMPLETION;
1777 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
1778 USBDEVNAME(sc->sc_dev)));
1779 break;
1780
1781 default:
1782 printf("%s: Get Max Lun failed: %s\n",
1783 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1784 /* XXX Should we port_reset the device? */
1785 break;
1786 }
1787
1788 return (err);
1789 }
1790
1791
1792
1793
1794 #ifdef UMASS_DEBUG
1795 Static void
1796 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
1797 {
1798 int clen = cbw->bCDBLength;
1799 int dlen = UGETDW(cbw->dCBWDataTransferLength);
1800 u_int8_t *c = cbw->CBWCDB;
1801 int tag = UGETDW(cbw->dCBWTag);
1802 int flags = cbw->bCBWFlags;
1803
1804 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen = %d "
1805 "(0x%02x%02x%02x%02x%02x%02x%s), "
1806 "data = %d bytes, dir = %s\n",
1807 USBDEVNAME(sc->sc_dev), tag, clen,
1808 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
1809 dlen, (flags == CBWFLAGS_IN? "in":
1810 (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
1811 }
1812
1813 Static void
1814 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
1815 {
1816 int sig = UGETDW(csw->dCSWSignature);
1817 int tag = UGETW(csw->dCSWTag);
1818 int res = UGETDW(csw->dCSWDataResidue);
1819 int status = csw->bCSWStatus;
1820
1821 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
1822 "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
1823 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"),
1824 tag, res,
1825 status, (status == CSWSTATUS_GOOD? "good":
1826 (status == CSWSTATUS_FAILED? "failed":
1827 (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
1828 }
1829
1830 Static void
1831 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
1832 int printlen)
1833 {
1834 int i, j;
1835 char s1[40];
1836 char s2[40];
1837 char s3[5];
1838
1839 s1[0] = '\0';
1840 s3[0] = '\0';
1841
1842 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
1843 for (i = 0; i < buflen && i < printlen; i++) {
1844 j = i % 16;
1845 if (j == 0 && i != 0) {
1846 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
1847 USBDEVNAME(sc->sc_dev), s1, s2));
1848 s2[0] = '\0';
1849 }
1850 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
1851 }
1852 if (buflen > printlen)
1853 sprintf(s3, " ...");
1854 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
1855 USBDEVNAME(sc->sc_dev), s1, s2, s3));
1856 }
1857 #endif
1858