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