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