umass.c revision 1.1 1 1.1 thorpej /* $NetBSD: umass.c,v 1.1 1999/08/29 00:30:08 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>,
5 1.1 thorpej * Nick Hibma <hibma (at) skylink.it>
6 1.1 thorpej * All rights reserved.
7 1.1 thorpej *
8 1.1 thorpej * Redistribution and use in source and binary forms, with or without
9 1.1 thorpej * modification, are permitted provided that the following conditions
10 1.1 thorpej * are met:
11 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer.
13 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.1 thorpej * documentation and/or other materials provided with the distribution.
16 1.1 thorpej *
17 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 thorpej * SUCH DAMAGE.
28 1.1 thorpej *
29 1.1 thorpej * FreeBSD: src/sys/dev/usb/umass.c,v 1.8 1999/06/20 15:46:13 n_hibma Exp
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej /*
33 1.1 thorpej * Universal Serial Bus Mass Storage Class Bulk-Only Transport
34 1.1 thorpej * http://www.usb.org/developers/usbmassbulk_09.pdf
35 1.1 thorpej *
36 1.1 thorpej * Relevant parts have been quoted in the source.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /* To do:
40 1.1 thorpej * x The umass_usb_transfer routine uses synchroneous transfers. This
41 1.1 thorpej * should be changed to async and state handling.
42 1.1 thorpej *
43 1.1 thorpej * x Should handle more than just Iomega USB Zip drives. There are
44 1.1 thorpej * a fair number of USB->SCSI dongles out there.
45 1.1 thorpej */
46 1.1 thorpej
47 1.1 thorpej /* Authors: (with short acronyms for comments)
48 1.1 thorpej * NWH - Nick Hibma <hibma (at) skylink.it>
49 1.1 thorpej * JRT - Jason R. Thorpe <thorpej (at) shagadelic.org>
50 1.1 thorpej */
51 1.1 thorpej
52 1.1 thorpej #include <sys/param.h>
53 1.1 thorpej #include <sys/systm.h>
54 1.1 thorpej #include <sys/kernel.h>
55 1.1 thorpej #include <sys/malloc.h>
56 1.1 thorpej #include <sys/device.h>
57 1.1 thorpej #include <sys/buf.h>
58 1.1 thorpej #include <sys/proc.h>
59 1.1 thorpej
60 1.1 thorpej #include <dev/usb/usb.h>
61 1.1 thorpej #include <dev/usb/usbdi.h>
62 1.1 thorpej #include <dev/usb/usbdi_util.h>
63 1.1 thorpej
64 1.1 thorpej #include <dev/scsipi/scsi_all.h>
65 1.1 thorpej #include <dev/scsipi/scsipi_all.h>
66 1.1 thorpej #include <dev/scsipi/scsiconf.h>
67 1.1 thorpej
68 1.1 thorpej #ifdef UMASS_DEBUG
69 1.1 thorpej #define DPRINTF(m, x) if (umassdebug & (m)) logprintf x
70 1.1 thorpej #define UDMASS_SCSI 0x00020000
71 1.1 thorpej #define UDMASS_USB 0x00040000
72 1.1 thorpej #define UDMASS_BULK 0x00080000
73 1.1 thorpej #define UDMASS_ALL 0xffff0000
74 1.1 thorpej int umassdebug = /* UDMASS_SCSI|UDMASS_BULK|UDMASS_USB */ 0;
75 1.1 thorpej #else
76 1.1 thorpej #define DPRINTF(m, x)
77 1.1 thorpej #endif
78 1.1 thorpej
79 1.1 thorpej typedef struct umass_softc {
80 1.1 thorpej bdevice sc_dev; /* base device */
81 1.1 thorpej usbd_interface_handle sc_iface; /* the interface we use */
82 1.1 thorpej
83 1.1 thorpej u_int8_t sc_bulkout; /* bulk-out Endpoint Address */
84 1.1 thorpej usbd_pipe_handle sc_bulkout_pipe;
85 1.1 thorpej u_int8_t sc_bulkin; /* bulk-in Endpoint Address */
86 1.1 thorpej usbd_pipe_handle sc_bulkin_pipe;
87 1.1 thorpej
88 1.1 thorpej struct scsipi_link sc_link; /* prototype for devs */
89 1.1 thorpej struct scsipi_adapter sc_adapter;
90 1.1 thorpej } umass_softc_t;
91 1.1 thorpej
92 1.1 thorpej #define USBD_COMMAND_FAILED USBD_INVAL /* redefine some errors for */
93 1.1 thorpej
94 1.1 thorpej #define UPROTO_MASS_ZIP 0x50 /* letter 'P' for protoype */
95 1.1 thorpej
96 1.1 thorpej #define UMASS_SCSIID_HOST 0x00
97 1.1 thorpej #define UMASS_SCSIID_DEVICE 0x01
98 1.1 thorpej
99 1.1 thorpej #define DIR_OUT 0
100 1.1 thorpej #define DIR_IN 1
101 1.1 thorpej #define DIR_NONE 2
102 1.1 thorpej
103 1.1 thorpej /* Bulk-Only specific request */
104 1.1 thorpej #define UR_RESET 0xff
105 1.1 thorpej
106 1.1 thorpej /* Bulk-Only Mass Storage features */
107 1.1 thorpej /* Command Block Wrapper */
108 1.1 thorpej typedef struct {
109 1.1 thorpej uDWord dCBWSignature;
110 1.1 thorpej #define CBWSIGNATURE 0x43425355
111 1.1 thorpej uDWord dCBWTag;
112 1.1 thorpej uDWord dCBWDataTransferLength;
113 1.1 thorpej uByte bCBWFlags;
114 1.1 thorpej #define CBWFLAGS_OUT 0x00
115 1.1 thorpej #define CBWFLAGS_IN 0x80
116 1.1 thorpej uByte bCBWLUN;
117 1.1 thorpej uByte bCDBLength;
118 1.1 thorpej uByte CBWCDB[16];
119 1.1 thorpej } usb_bulk_cbw_t;
120 1.1 thorpej #define USB_BULK_CBW_SIZE 31
121 1.1 thorpej
122 1.1 thorpej /* Command Status Wrapper */
123 1.1 thorpej typedef struct {
124 1.1 thorpej uDWord dCSWSignature;
125 1.1 thorpej #define CSWSIGNATURE 0x53425355
126 1.1 thorpej uDWord dCSWTag;
127 1.1 thorpej uDWord dCSWDataResidue;
128 1.1 thorpej uByte bCSWStatus;
129 1.1 thorpej #define CSWSTATUS_GOOD 0x0
130 1.1 thorpej #define CSWSTATUS_FAILED 0x1
131 1.1 thorpej #define CSWSTATUS_PHASE 0x2
132 1.1 thorpej } usb_bulk_csw_t;
133 1.1 thorpej #define USB_BULK_CSW_SIZE 13
134 1.1 thorpej
135 1.1 thorpej
136 1.1 thorpej USB_DECLARE_DRIVER(umass);
137 1.1 thorpej
138 1.1 thorpej /* USB related functions */
139 1.1 thorpej usbd_status umass_usb_transfer __P((usbd_interface_handle iface,
140 1.1 thorpej usbd_pipe_handle pipe,
141 1.1 thorpej void *buf, int buflen,
142 1.1 thorpej int flags, int *xfer_size));
143 1.1 thorpej
144 1.1 thorpej /* Bulk-Only related functions */
145 1.1 thorpej usbd_status umass_bulk_reset __P((umass_softc_t *sc));
146 1.1 thorpej usbd_status umass_bulk_transfer __P((umass_softc_t *sc, int lun,
147 1.1 thorpej void *cmd, int cmdlen,
148 1.1 thorpej void *data, int datalen,
149 1.1 thorpej int dir, int *residue));
150 1.1 thorpej
151 1.1 thorpej /* SCSIPI related functions */
152 1.1 thorpej struct scsipi_device umass_dev = {
153 1.1 thorpej NULL, /* Use default error handler */
154 1.1 thorpej NULL, /* have a queue, served by this */
155 1.1 thorpej NULL, /* have no async handler */
156 1.1 thorpej NULL, /* Use default `done' routine */
157 1.1 thorpej };
158 1.1 thorpej
159 1.1 thorpej void umass_minphys __P((struct buf *));
160 1.1 thorpej int umass_scsi_cmd __P((struct scsipi_xfer *));
161 1.1 thorpej
162 1.1 thorpej
163 1.1 thorpej
164 1.1 thorpej USB_MATCH(umass)
165 1.1 thorpej {
166 1.1 thorpej USB_MATCH_START(umass, uaa);
167 1.1 thorpej usb_interface_descriptor_t *id;
168 1.1 thorpej
169 1.1 thorpej if (!uaa->iface)
170 1.1 thorpej return(UMATCH_NONE);
171 1.1 thorpej
172 1.1 thorpej id = usbd_get_interface_descriptor(uaa->iface);
173 1.1 thorpej if (id
174 1.1 thorpej && id->bInterfaceClass == UCLASS_MASS
175 1.1 thorpej && id->bInterfaceSubClass == USUBCLASS_SCSI
176 1.1 thorpej && id->bInterfaceProtocol == UPROTO_MASS_ZIP)
177 1.1 thorpej /* probe the Iomega USB Zip 100 drive */
178 1.1 thorpej return(UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO);
179 1.1 thorpej
180 1.1 thorpej return(UMATCH_NONE);
181 1.1 thorpej }
182 1.1 thorpej
183 1.1 thorpej USB_ATTACH(umass)
184 1.1 thorpej {
185 1.1 thorpej USB_ATTACH_START(umass, sc, uaa);
186 1.1 thorpej usb_interface_descriptor_t *id;
187 1.1 thorpej usb_endpoint_descriptor_t *ed;
188 1.1 thorpej char devinfo[1024];
189 1.1 thorpej int i;
190 1.1 thorpej int err;
191 1.1 thorpej
192 1.1 thorpej sc->sc_iface = uaa->iface;
193 1.1 thorpej sc->sc_bulkout_pipe = NULL;
194 1.1 thorpej sc->sc_bulkin_pipe = NULL;
195 1.1 thorpej
196 1.1 thorpej usbd_devinfo(uaa->device, 0, devinfo);
197 1.1 thorpej USB_ATTACH_SETUP;
198 1.1 thorpej
199 1.1 thorpej id = usbd_get_interface_descriptor(sc->sc_iface);
200 1.1 thorpej printf("%s: %s, iclass %d/%d/%d\n", USBDEVNAME(sc->sc_dev), devinfo,
201 1.1 thorpej id->bInterfaceClass, id->bInterfaceSubClass,
202 1.1 thorpej id->bInterfaceProtocol);
203 1.1 thorpej
204 1.1 thorpej /*
205 1.1 thorpej * A Bulk-Only Mass Storage device supports the following endpoints,
206 1.1 thorpej * in addition to the Endpoint 0 for Control transfer that is required
207 1.1 thorpej * of all USB devices:
208 1.1 thorpej * (a) bulk-in endpoint.
209 1.1 thorpej * (b) bulk-out endpoint.
210 1.1 thorpej *
211 1.1 thorpej * The endpoint addresses are not fixed, so we have to read them
212 1.1 thorpej * from the device descriptors of the current interface.
213 1.1 thorpej */
214 1.1 thorpej for (i = 0 ; i < id->bNumEndpoints ; i++) {
215 1.1 thorpej ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
216 1.1 thorpej if (!ed) {
217 1.1 thorpej printf("%s: could not read endpoint descriptor\n",
218 1.1 thorpej USBDEVNAME(sc->sc_dev));
219 1.1 thorpej USB_ATTACH_ERROR_RETURN;
220 1.1 thorpej }
221 1.1 thorpej if (UE_GET_DIR(ed->bEndpointAddress) == UE_IN
222 1.1 thorpej && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
223 1.1 thorpej sc->sc_bulkin = ed->bEndpointAddress;
224 1.1 thorpej } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_OUT
225 1.1 thorpej && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
226 1.1 thorpej sc->sc_bulkout = ed->bEndpointAddress;
227 1.1 thorpej }
228 1.1 thorpej }
229 1.1 thorpej
230 1.1 thorpej /* Open the bulk-in and -out pipe */
231 1.1 thorpej err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
232 1.1 thorpej USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
233 1.1 thorpej if (err) {
234 1.1 thorpej DPRINTF(UDMASS_USB, ("cannot open bulk out pipe (address %d)\n",
235 1.1 thorpej sc->sc_bulkout));
236 1.1 thorpej USB_ATTACH_ERROR_RETURN;
237 1.1 thorpej }
238 1.1 thorpej err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
239 1.1 thorpej USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
240 1.1 thorpej if (err) {
241 1.1 thorpej DPRINTF(UDMASS_USB, ("cannot open bulk in pipe (address %d)\n",
242 1.1 thorpej sc->sc_bulkin));
243 1.1 thorpej usbd_close_pipe(sc->sc_bulkout_pipe);
244 1.1 thorpej USB_ATTACH_ERROR_RETURN;
245 1.1 thorpej }
246 1.1 thorpej
247 1.1 thorpej /* attach the device to the SCSI layer */
248 1.1 thorpej sc->sc_adapter.scsipi_cmd = umass_scsi_cmd;
249 1.1 thorpej sc->sc_adapter.scsipi_minphys = umass_minphys;
250 1.1 thorpej
251 1.1 thorpej sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
252 1.1 thorpej sc->sc_link.adapter_softc = sc;
253 1.1 thorpej sc->sc_link.scsipi_scsi.adapter_target = UMASS_SCSIID_HOST;
254 1.1 thorpej sc->sc_link.adapter = &sc->sc_adapter;
255 1.1 thorpej sc->sc_link.device = &umass_dev;
256 1.1 thorpej sc->sc_link.openings = 4; /* XXX */
257 1.1 thorpej sc->sc_link.scsipi_scsi.max_target = UMASS_SCSIID_DEVICE; /* XXX */
258 1.1 thorpej sc->sc_link.scsipi_scsi.max_lun = 0; /* XXX */
259 1.1 thorpej sc->sc_link.type = BUS_SCSI;
260 1.1 thorpej
261 1.1 thorpej if (config_found(&sc->sc_dev, &sc->sc_link, scsiprint) == NULL) {
262 1.1 thorpej usbd_close_pipe(sc->sc_bulkout_pipe);
263 1.1 thorpej usbd_close_pipe(sc->sc_bulkin_pipe);
264 1.1 thorpej /* XXX Not really an error. */
265 1.1 thorpej USB_ATTACH_ERROR_RETURN;
266 1.1 thorpej }
267 1.1 thorpej
268 1.1 thorpej USB_ATTACH_SUCCESS_RETURN;
269 1.1 thorpej }
270 1.1 thorpej
271 1.1 thorpej int
272 1.1 thorpej umass_activate(self, act)
273 1.1 thorpej struct device *self;
274 1.1 thorpej enum devact act;
275 1.1 thorpej {
276 1.1 thorpej
277 1.1 thorpej switch (act) {
278 1.1 thorpej case DVACT_ACTIVATE:
279 1.1 thorpej return (EOPNOTSUPP);
280 1.1 thorpej break;
281 1.1 thorpej
282 1.1 thorpej case DVACT_DEACTIVATE:
283 1.1 thorpej /* XXX Not supported yet. */
284 1.1 thorpej return (EOPNOTSUPP);
285 1.1 thorpej break;
286 1.1 thorpej }
287 1.1 thorpej return (0);
288 1.1 thorpej }
289 1.1 thorpej
290 1.1 thorpej int
291 1.1 thorpej umass_detach(self, flags)
292 1.1 thorpej struct device *self;
293 1.1 thorpej int flags;
294 1.1 thorpej {
295 1.1 thorpej
296 1.1 thorpej /* XXX Not supported yet. */
297 1.1 thorpej return (EOPNOTSUPP);
298 1.1 thorpej }
299 1.1 thorpej
300 1.1 thorpej /* Performs a request over a pipe.
301 1.1 thorpej *
302 1.1 thorpej * flags: Can be set to USBD_SHORT_XFER_OK
303 1.1 thorpej * xfer_size: if not null returns the nr. of bytes transferred
304 1.1 thorpej *
305 1.1 thorpej * If the returned error is USBD_STALLED the pipe stall has
306 1.1 thorpej * been cleared again.
307 1.1 thorpej */
308 1.1 thorpej
309 1.1 thorpej usbd_status
310 1.1 thorpej umass_usb_transfer(usbd_interface_handle iface, usbd_pipe_handle pipe,
311 1.1 thorpej void *buf, int buflen, int flags, int *xfer_size)
312 1.1 thorpej {
313 1.1 thorpej usbd_request_handle reqh;
314 1.1 thorpej usbd_private_handle priv;
315 1.1 thorpej void *buffer;
316 1.1 thorpej int size;
317 1.1 thorpej usbd_status err;
318 1.1 thorpej
319 1.1 thorpej /* A transfer is done synchronously. We create and schedule the
320 1.1 thorpej * transfer and then wait for it to complete
321 1.1 thorpej */
322 1.1 thorpej
323 1.1 thorpej reqh = usbd_alloc_request();
324 1.1 thorpej if (!reqh) {
325 1.1 thorpej DPRINTF(UDMASS_USB, ("Not enough memory\n"));
326 1.1 thorpej return USBD_NOMEM;
327 1.1 thorpej }
328 1.1 thorpej
329 1.1 thorpej (void) usbd_setup_request(reqh, pipe, 0, buf, buflen,
330 1.1 thorpej flags, 3000 /*ms*/, NULL);
331 1.1 thorpej err = usbd_sync_transfer(reqh);
332 1.1 thorpej if (err) {
333 1.1 thorpej DPRINTF(UDMASS_USB, ("transfer failed, %s\n",
334 1.1 thorpej usbd_errstr(err)));
335 1.1 thorpej usbd_free_request(reqh);
336 1.1 thorpej return(err);
337 1.1 thorpej }
338 1.1 thorpej
339 1.1 thorpej usbd_get_request_status(reqh, &priv, &buffer, &size, &err);
340 1.1 thorpej
341 1.1 thorpej if (xfer_size)
342 1.1 thorpej *xfer_size = size;
343 1.1 thorpej
344 1.1 thorpej usbd_free_request(reqh);
345 1.1 thorpej return(USBD_NORMAL_COMPLETION);
346 1.1 thorpej }
347 1.1 thorpej
348 1.1 thorpej
349 1.1 thorpej
350 1.1 thorpej
351 1.1 thorpej usbd_status
352 1.1 thorpej umass_bulk_reset(umass_softc_t *sc)
353 1.1 thorpej {
354 1.1 thorpej usbd_device_handle dev;
355 1.1 thorpej usb_device_request_t req;
356 1.1 thorpej usbd_status err;
357 1.1 thorpej usb_interface_descriptor_t *id;
358 1.1 thorpej
359 1.1 thorpej /*
360 1.1 thorpej * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
361 1.1 thorpej *
362 1.1 thorpej * For Reset Recovery the host shall issue in the following order:
363 1.1 thorpej * a) a Bulk-Only Mass Storage Reset
364 1.1 thorpej * b) a Clear Feature HALT to the Bulk-In endpoint
365 1.1 thorpej * c) a Clear Feature HALT to the Bulk-Out endpoint
366 1.1 thorpej */
367 1.1 thorpej
368 1.1 thorpej DPRINTF(UDMASS_BULK, ("%s: Reset\n",
369 1.1 thorpej USBDEVNAME(sc->sc_dev)));
370 1.1 thorpej
371 1.1 thorpej usbd_interface2device_handle(sc->sc_iface, &dev);
372 1.1 thorpej id = usbd_get_interface_descriptor(sc->sc_iface);
373 1.1 thorpej
374 1.1 thorpej /* the reset command is a class specific interface request */
375 1.1 thorpej req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
376 1.1 thorpej req.bRequest = UR_RESET;
377 1.1 thorpej USETW(req.wValue, 0);
378 1.1 thorpej USETW(req.wIndex, id->bInterfaceNumber);
379 1.1 thorpej USETW(req.wLength, 0);
380 1.1 thorpej
381 1.1 thorpej err = usbd_do_request(dev, &req, 0);
382 1.1 thorpej if (err) {
383 1.1 thorpej printf("%s: Reset failed, %s\n",
384 1.1 thorpej USBDEVNAME(sc->sc_dev), usbd_errstr(err));
385 1.1 thorpej /* XXX we should port_reset the device */
386 1.1 thorpej return(err);
387 1.1 thorpej }
388 1.1 thorpej
389 1.1 thorpej usbd_clear_endpoint_stall(sc->sc_bulkout_pipe);
390 1.1 thorpej usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
391 1.1 thorpej
392 1.1 thorpej /*
393 1.1 thorpej * XXX we should convert this into a more friendly delay.
394 1.1 thorpej * Perhaps a tsleep (or is this routine run from int context?)
395 1.1 thorpej */
396 1.1 thorpej
397 1.1 thorpej DELAY(2500000 /*us*/);
398 1.1 thorpej
399 1.1 thorpej return(USBD_NORMAL_COMPLETION);
400 1.1 thorpej }
401 1.1 thorpej
402 1.1 thorpej /*
403 1.1 thorpej * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
404 1.1 thorpej * a data phase of datalen bytes from/to data and finally a csw read
405 1.1 thorpej * phase.
406 1.1 thorpej *
407 1.1 thorpej * If the data direction was inbound a maximum of datalen bytes
408 1.1 thorpej * is stored in the buffer pointed to by data.
409 1.1 thorpej * The status returned is USBD_NORMAL_COMPLETION,
410 1.1 thorpej * USBD_IOERROR, USBD_COMMAND_FAILED.
411 1.1 thorpej * In the last case *residue is set to the residue from the CSW,
412 1.1 thorpej * otherwise to 0.
413 1.1 thorpej *
414 1.1 thorpej * For the functionality of this subroutine see the Mass Storage
415 1.1 thorpej * Spec., the graphs on page 14 and page 19 and beyong (v0.9 of
416 1.1 thorpej * the spec).
417 1.1 thorpej */
418 1.1 thorpej
419 1.1 thorpej usbd_status
420 1.1 thorpej umass_bulk_transfer(umass_softc_t *sc, int lun, void *cmd, int cmdlen,
421 1.1 thorpej void *data, int datalen, int dir, int *residue)
422 1.1 thorpej {
423 1.1 thorpej static int dCBWtag = 42; /* tag to be used in transfers,
424 1.1 thorpej * incremented at each transfer */
425 1.1 thorpej usb_bulk_cbw_t cbw; /* command block wrapper struct */
426 1.1 thorpej usb_bulk_csw_t csw; /* command status wrapper struct */
427 1.1 thorpej u_int32_t n = 0; /* number of bytes transported */
428 1.1 thorpej usbd_status err;
429 1.1 thorpej
430 1.1 thorpej #ifdef UMASS_DEBUG
431 1.1 thorpej u_int8_t *c = cmd;
432 1.1 thorpej
433 1.1 thorpej /* check the given arguments */
434 1.1 thorpej if (!data && datalen > 0) { /* no buffer for transfer */
435 1.1 thorpej DPRINTF(UDMASS_BULK, ("%s: no buffer, but datalen > 0 !\n",
436 1.1 thorpej USBDEVNAME(sc->sc_dev)));
437 1.1 thorpej return USBD_IOERROR;
438 1.1 thorpej }
439 1.1 thorpej
440 1.1 thorpej DPRINTF(UDMASS_BULK, ("%s: cmd: %d bytes (0x%02x%02x%02x%02x%02x%02x%s)"
441 1.1 thorpej ", data: %d bytes, dir: %s\n",
442 1.1 thorpej USBDEVNAME(sc->sc_dev),
443 1.1 thorpej cmdlen, c[0], c[1], c[2], c[3], c[4], c[5],
444 1.1 thorpej (cmdlen > 6? "...":""),
445 1.1 thorpej datalen, (dir == DIR_IN? "in":"out")));
446 1.1 thorpej #endif
447 1.1 thorpej
448 1.1 thorpej if (dir == DIR_NONE || datalen == 0) { /* make sure they correspond */
449 1.1 thorpej datalen = 0;
450 1.1 thorpej dir = DIR_NONE;
451 1.1 thorpej }
452 1.1 thorpej
453 1.1 thorpej *residue = 0; /* reset residue */
454 1.1 thorpej
455 1.1 thorpej /*
456 1.1 thorpej * Determine the direction of transferring data and data length.
457 1.1 thorpej *
458 1.1 thorpej * dCBWDataTransferLength (datalen) :
459 1.1 thorpej * This field indicates the number of bytes of data that the host
460 1.1 thorpej * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
461 1.1 thorpej * the Direction bit) during the execution of this command. If this
462 1.1 thorpej * field is set to 0, the device will expect that no data will be
463 1.1 thorpej * transferred IN or OUT during this command, regardless of the value
464 1.1 thorpej * of the Direction bit defined in dCBWFlags.
465 1.1 thorpej *
466 1.1 thorpej * dCBWFlags (dir) :
467 1.1 thorpej * The bits of the Flags field are defined as follows:
468 1.1 thorpej * Bits 0-6 reserved
469 1.1 thorpej * Bit 7 Direction - this bit shall be ignored if the
470 1.1 thorpej * dCBWDataTransferLength field is zero.
471 1.1 thorpej * 0 = data Out from host to device
472 1.1 thorpej * 1 = data In from device to host
473 1.1 thorpej */
474 1.1 thorpej
475 1.1 thorpej
476 1.1 thorpej /*
477 1.1 thorpej * Command transport phase
478 1.1 thorpej */
479 1.1 thorpej
480 1.1 thorpej /* Fill in the Command Block Wrapper */
481 1.1 thorpej USETDW(cbw.dCBWSignature, CBWSIGNATURE);
482 1.1 thorpej USETDW(cbw.dCBWTag, dCBWtag++);
483 1.1 thorpej USETDW(cbw.dCBWDataTransferLength, datalen);
484 1.1 thorpej /* we do not check for DIR_NONE below (see text on dCBWFlags above) */
485 1.1 thorpej cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
486 1.1 thorpej cbw.bCBWLUN = lun;
487 1.1 thorpej cbw.bCDBLength = cmdlen;
488 1.1 thorpej bcopy(cmd, cbw.CBWCDB, cmdlen);
489 1.1 thorpej
490 1.1 thorpej /* Send the CBW from host to device via bulk-out endpoint. */
491 1.1 thorpej err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkout_pipe,
492 1.1 thorpej &cbw, USB_BULK_CBW_SIZE, 0, NULL);
493 1.1 thorpej if (err) {
494 1.1 thorpej DPRINTF(UDMASS_BULK, ("%s: failed to send CBW\n",
495 1.1 thorpej USBDEVNAME(sc->sc_dev)));
496 1.1 thorpej /* If the device detects that the CBW is invalid, then the
497 1.1 thorpej * device may STALL both bulk endpoints and require a
498 1.1 thorpej * Bulk-Only MS Reset
499 1.1 thorpej */
500 1.1 thorpej umass_bulk_reset(sc);
501 1.1 thorpej return(USBD_IOERROR);
502 1.1 thorpej }
503 1.1 thorpej
504 1.1 thorpej
505 1.1 thorpej /*
506 1.1 thorpej * Data transport phase (only if there is data to be sent/received)
507 1.1 thorpej */
508 1.1 thorpej
509 1.1 thorpej if (dir == DIR_IN) {
510 1.1 thorpej /* we allow short transfers for bulk-in pipes */
511 1.1 thorpej err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe,
512 1.1 thorpej data, datalen,
513 1.1 thorpej USBD_SHORT_XFER_OK, &n);
514 1.1 thorpej if (err)
515 1.1 thorpej DPRINTF(UDMASS_BULK, ("%s: failed to receive data, "
516 1.1 thorpej "(%d bytes, n = %d), %s\n",
517 1.1 thorpej USBDEVNAME(sc->sc_dev),
518 1.1 thorpej datalen, n, usbd_errstr(err)));
519 1.1 thorpej } else if (dir == DIR_OUT) {
520 1.1 thorpej err = umass_usb_transfer(sc->sc_iface,
521 1.1 thorpej sc->sc_bulkout_pipe,
522 1.1 thorpej data, datalen, 0, &n);
523 1.1 thorpej if (err)
524 1.1 thorpej DPRINTF(UDMASS_BULK, ("%s: failed to send data, "
525 1.1 thorpej "(%d bytes, n = %d), %s\n",
526 1.1 thorpej USBDEVNAME(sc->sc_dev),
527 1.1 thorpej datalen, n, usbd_errstr(err)));
528 1.1 thorpej }
529 1.1 thorpej if (err && err != USBD_STALLED)
530 1.1 thorpej return(USBD_IOERROR);
531 1.1 thorpej
532 1.1 thorpej
533 1.1 thorpej /*
534 1.1 thorpej * Status transport phase
535 1.1 thorpej */
536 1.1 thorpej
537 1.1 thorpej /* Read the Command Status Wrapper via bulk-in endpoint. */
538 1.1 thorpej err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe,
539 1.1 thorpej &csw, USB_BULK_CSW_SIZE, 0, NULL);
540 1.1 thorpej /* Try again if the bulk-in pipe was stalled */
541 1.1 thorpej if (err == USBD_STALLED) {
542 1.1 thorpej err = usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
543 1.1 thorpej if (!err) {
544 1.1 thorpej err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe,
545 1.1 thorpej &csw, USB_BULK_CSW_SIZE, 0, NULL);
546 1.1 thorpej }
547 1.1 thorpej }
548 1.1 thorpej if (err && err != USBD_STALLED)
549 1.1 thorpej return(USBD_IOERROR);
550 1.1 thorpej
551 1.1 thorpej /*
552 1.1 thorpej * Check the CSW for status and validity, and check for fatal errors
553 1.1 thorpej */
554 1.1 thorpej
555 1.1 thorpej /* Invalid CSW: Wrong signature or wrong tag might indicate
556 1.1 thorpej * that the device is confused -> reset it.
557 1.1 thorpej * Other fatal errors: STALL on read of CSW and Phase error
558 1.1 thorpej * or unknown status.
559 1.1 thorpej */
560 1.1 thorpej if (err == USBD_STALLED
561 1.1 thorpej || UGETDW(csw.dCSWSignature) != CSWSIGNATURE
562 1.1 thorpej || UGETDW(csw.dCSWTag) != UGETDW(cbw.dCBWTag)
563 1.1 thorpej || csw.bCSWStatus == CSWSTATUS_PHASE
564 1.1 thorpej || csw.bCSWStatus > CSWSTATUS_PHASE) {
565 1.1 thorpej if (err) {
566 1.1 thorpej printf("%s: failed to read CSW, %s\n",
567 1.1 thorpej USBDEVNAME(sc->sc_dev), usbd_errstr(err));
568 1.1 thorpej } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
569 1.1 thorpej printf("%s: Phase Error, residue = %d, n = %d\n",
570 1.1 thorpej USBDEVNAME(sc->sc_dev),
571 1.1 thorpej UGETDW(csw.dCSWDataResidue), n);
572 1.1 thorpej } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
573 1.1 thorpej printf("%s: Unknown status %d in CSW\n",
574 1.1 thorpej USBDEVNAME(sc->sc_dev), csw.bCSWStatus);
575 1.1 thorpej } else {
576 1.1 thorpej printf("%s: invalid CSW, sig = 0x%08x, tag = %d (!= %d)\n",
577 1.1 thorpej USBDEVNAME(sc->sc_dev),
578 1.1 thorpej UGETDW(csw.dCSWSignature),
579 1.1 thorpej UGETDW(csw.dCSWTag), UGETDW(cbw.dCBWTag));
580 1.1 thorpej }
581 1.1 thorpej umass_bulk_reset(sc);
582 1.1 thorpej return(USBD_IOERROR);
583 1.1 thorpej }
584 1.1 thorpej
585 1.1 thorpej if (csw.bCSWStatus == CSWSTATUS_FAILED) {
586 1.1 thorpej DPRINTF(UDMASS_BULK, ("%s: Command Failed, "
587 1.1 thorpej "residue = %d, n = %d\n",
588 1.1 thorpej USBDEVNAME(sc->sc_dev),
589 1.1 thorpej UGETDW(csw.dCSWDataResidue), n));
590 1.1 thorpej *residue = UGETDW(csw.dCSWDataResidue);
591 1.1 thorpej return(USBD_COMMAND_FAILED);
592 1.1 thorpej }
593 1.1 thorpej
594 1.1 thorpej /*
595 1.1 thorpej * XXX a residue not equal to 0 might indicate that something
596 1.1 thorpej * is wrong. Does CAM high level drivers check this for us?
597 1.1 thorpej */
598 1.1 thorpej
599 1.1 thorpej return(USBD_NORMAL_COMPLETION);
600 1.1 thorpej }
601 1.1 thorpej
602 1.1 thorpej
603 1.1 thorpej /*
604 1.1 thorpej * SCSIPI specific functions
605 1.1 thorpej */
606 1.1 thorpej
607 1.1 thorpej int
608 1.1 thorpej umass_scsi_cmd(xs)
609 1.1 thorpej struct scsipi_xfer *xs;
610 1.1 thorpej {
611 1.1 thorpej struct scsipi_link *sc_link = xs->sc_link;
612 1.1 thorpej struct umass_softc *sc = sc_link->adapter_softc;
613 1.1 thorpej int residue, dir;
614 1.1 thorpej usbd_status err;
615 1.1 thorpej
616 1.1 thorpej DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd %d:%d\n",
617 1.1 thorpej USBDEVNAME(sc->sc_dev),
618 1.1 thorpej sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
619 1.1 thorpej
620 1.1 thorpej #ifdef UMASS_DEBUG
621 1.1 thorpej if (sc_link->scsipi_scsi.target != UMASS_SCSIID_DEVICE ||
622 1.1 thorpej sc_link->scsipi_scsi.lun != 0) {
623 1.1 thorpej DPRINTF(UDMASS_SCSI, ("%s: Wrong SCSI ID %d or LUN %d\n",
624 1.1 thorpej USBDEVNAME(sc->sc_dev),
625 1.1 thorpej sc_link->scsipi_scsi.target,
626 1.1 thorpej sc_link->scsipi_scsi.lun));
627 1.1 thorpej xs->error = XS_DRIVER_STUFFUP;
628 1.1 thorpej return (COMPLETE);
629 1.1 thorpej }
630 1.1 thorpej #endif
631 1.1 thorpej
632 1.1 thorpej dir = DIR_NONE;
633 1.1 thorpej if (xs->datalen) {
634 1.1 thorpej switch (xs->flags & (SCSI_DATA_IN|SCSI_DATA_OUT)) {
635 1.1 thorpej case SCSI_DATA_IN:
636 1.1 thorpej dir = DIR_IN;
637 1.1 thorpej break;
638 1.1 thorpej case SCSI_DATA_OUT:
639 1.1 thorpej dir = DIR_OUT;
640 1.1 thorpej break;
641 1.1 thorpej }
642 1.1 thorpej }
643 1.1 thorpej
644 1.1 thorpej err = umass_bulk_transfer(sc, sc_link->scsipi_scsi.lun,
645 1.1 thorpej xs->cmd, xs->cmdlen, xs->data, xs->datalen, dir, &residue);
646 1.1 thorpej
647 1.1 thorpej /*
648 1.1 thorpej * FAILED commands are supposed to be SCSI failed commands
649 1.1 thorpej * and are therefore considered to be successfull CDW/CSW
650 1.1 thorpej * transfers. PHASE errors are more serious and should return
651 1.1 thorpej * an error to the SCSIPI system.
652 1.1 thorpej *
653 1.1 thorpej * XXX This is however more based on empirical evidence than on
654 1.1 thorpej * hard proof from the Bulk-Only spec.
655 1.1 thorpej */
656 1.1 thorpej if (err == USBD_NORMAL_COMPLETION)
657 1.1 thorpej xs->error = XS_NOERROR;
658 1.1 thorpej else
659 1.1 thorpej xs->error = XS_DRIVER_STUFFUP; /* XXX */
660 1.1 thorpej xs->resid = residue;
661 1.1 thorpej
662 1.1 thorpej DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd: error = %d, resid = 0x%x\n",
663 1.1 thorpej USBDEVNAME(sc->sc_dev), xs->error, xs->resid));
664 1.1 thorpej
665 1.1 thorpej xs->flags |= ITSDONE;
666 1.1 thorpej scsipi_done(xs);
667 1.1 thorpej
668 1.1 thorpej /*
669 1.1 thorpej * XXXJRT We must return successfully queued if we're an
670 1.1 thorpej * XXXJRT `asynchronous' command, otherwise `xs' will be
671 1.1 thorpej * XXXJRT freed twice: once in scsipi_done(), and once in
672 1.1 thorpej * XXXJRT scsi_scsipi_cmd().
673 1.1 thorpej */
674 1.1 thorpej if (SCSIPI_XFER_ASYNC(xs))
675 1.1 thorpej return (SUCCESSFULLY_QUEUED);
676 1.1 thorpej
677 1.1 thorpej return (COMPLETE);
678 1.1 thorpej }
679 1.1 thorpej
680 1.1 thorpej void
681 1.1 thorpej umass_minphys(bp)
682 1.1 thorpej struct buf *bp;
683 1.1 thorpej {
684 1.1 thorpej
685 1.1 thorpej /* No limit here. */
686 1.1 thorpej minphys(bp);
687 1.1 thorpej }
688