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