1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/* $OpenBSD: umass_scsi.c,v 1.43 2015/12/16 14:50:26 mpi Exp $ */
/* $NetBSD: umass_scsipi.c,v 1.9 2003/02/16 23:14:08 augustss Exp $ */
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net) at
* Carlstedt Research & Technology.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/umassvar.h>
#include <dev/usb/umass_scsi.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <scsi/scsi_disk.h>
#include <machine/bus.h>
struct umass_scsi_softc {
struct device *sc_child;
struct scsi_link sc_link;
struct scsi_iopool sc_iopool;
int sc_open;
struct scsi_sense sc_sense_cmd;
};
#define UMASS_SCSIID_HOST 0x00
#define UMASS_SCSIID_DEVICE 0x01
int umass_scsi_probe(struct scsi_link *);
void umass_scsi_cmd(struct scsi_xfer *);
void umass_scsi_minphys(struct buf *, struct scsi_link *);
struct scsi_adapter umass_scsi_switch = {
umass_scsi_cmd,
umass_scsi_minphys,
umass_scsi_probe
};
void umass_scsi_cb(struct umass_softc *sc, void *priv, int residue,
int status);
void umass_scsi_sense_cb(struct umass_softc *sc, void *priv, int residue,
int status);
struct umass_scsi_softc *umass_scsi_setup(struct umass_softc *);
void *umass_io_get(void *);
void umass_io_put(void *, void *);
int
umass_scsi_attach(struct umass_softc *sc)
{
struct scsibus_attach_args saa;
struct umass_scsi_softc *scbus;
scbus = umass_scsi_setup(sc);
scbus->sc_link.adapter_target = UMASS_SCSIID_HOST;
scbus->sc_link.luns = sc->maxlun + 1;
scbus->sc_link.flags &= ~SDEV_ATAPI;
scbus->sc_link.flags |= SDEV_UMASS;
bzero(&saa, sizeof(saa));
saa.saa_sc_link = &scbus->sc_link;
DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: SCSI\n"
"sc = 0x%p, scbus = 0x%p\n",
sc->sc_dev.dv_xname, sc, scbus));
sc->sc_refcnt++;
scbus->sc_child = config_found((struct device *)sc, &saa, scsiprint);
if (--sc->sc_refcnt < 0)
usb_detach_wakeup(&sc->sc_dev);
return (0);
}
int
umass_atapi_attach(struct umass_softc *sc)
{
struct scsibus_attach_args saa;
struct umass_scsi_softc *scbus;
scbus = umass_scsi_setup(sc);
scbus->sc_link.adapter_target = UMASS_SCSIID_HOST;
scbus->sc_link.luns = 1;
scbus->sc_link.openings = 1;
scbus->sc_link.flags |= SDEV_ATAPI;
bzero(&saa, sizeof(saa));
saa.saa_sc_link = &scbus->sc_link;
DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: ATAPI\n"
"sc = 0x%p, scbus = 0x%p\n",
sc->sc_dev.dv_xname, sc, scbus));
sc->sc_refcnt++;
scbus->sc_child = config_found((struct device *)sc, &saa, scsiprint);
if (--sc->sc_refcnt < 0)
usb_detach_wakeup(&sc->sc_dev);
return (0);
}
struct umass_scsi_softc *
umass_scsi_setup(struct umass_softc *sc)
{
struct umass_scsi_softc *scbus;
scbus = malloc(sizeof(*scbus), M_DEVBUF, M_WAITOK | M_ZERO);
sc->bus = scbus;
scsi_iopool_init(&scbus->sc_iopool, scbus, umass_io_get, umass_io_put);
/* Fill in the link. */
scbus->sc_link.adapter_buswidth = 2;
scbus->sc_link.adapter = &umass_scsi_switch;
scbus->sc_link.adapter_softc = sc;
scbus->sc_link.openings = 1;
scbus->sc_link.quirks |= SDEV_ONLYBIG | sc->sc_busquirks;
scbus->sc_link.pool = &scbus->sc_iopool;
return (scbus);
}
int
umass_scsi_detach(struct umass_softc *sc, int flags)
{
struct umass_scsi_softc *scbus = sc->bus;
int rv = 0;
if (scbus != NULL) {
if (scbus->sc_child != NULL)
rv = config_detach(scbus->sc_child, flags);
free(scbus, M_DEVBUF, sizeof(*scbus));
sc->bus = NULL;
}
return (rv);
}
int
umass_scsi_probe(struct scsi_link *link)
{
struct umass_softc *sc = link->adapter_softc;
struct usb_device_info udi;
size_t len;
/* dont fake devids when more than one scsi device can attach. */
if (sc->maxlun > 0)
return (0);
usbd_fill_deviceinfo(sc->sc_udev, &udi, 1);
/*
* Create a fake devid using the vendor and product ids and the last
* 12 characters of serial number, as recommended by Section 4.1.1 of
* the USB Mass Storage Class - Bulk Only Transport spec.
*/
len = strlen(udi.udi_serial);
if (len >= 12) {
char buf[21];
snprintf(buf, sizeof(buf), "%04x%04x%s", udi.udi_vendorNo,
udi.udi_productNo, udi.udi_serial + len - 12);
link->id = devid_alloc(DEVID_SERIAL, DEVID_F_PRINT,
sizeof(buf) - 1, buf);
}
return (0);
}
void
umass_scsi_cmd(struct scsi_xfer *xs)
{
struct scsi_link *sc_link = xs->sc_link;
struct umass_softc *sc = sc_link->adapter_softc;
struct scsi_generic *cmd;
int cmdlen, dir;
#ifdef UMASS_DEBUG
microtime(&sc->tv);
#endif
DIF(UDMASS_UPPER, sc_link->flags |= SCSIDEBUG_LEVEL);
DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %lld.%06ld: %d:%d "
"xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
sc->sc_dev.dv_xname, (long long)sc->tv.tv_sec, sc->tv.tv_usec,
sc_link->target, sc_link->lun, xs, xs->cmd->opcode,
xs->datalen, sc_link->quirks, xs->flags & SCSI_POLL));
if (usbd_is_dying(sc->sc_udev)) {
xs->error = XS_DRIVER_STUFFUP;
goto done;
}
#if defined(UMASS_DEBUG)
if (sc_link->target != UMASS_SCSIID_DEVICE) {
DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
sc->sc_dev.dv_xname, sc_link->target));
xs->error = XS_DRIVER_STUFFUP;
goto done;
}
#endif
cmd = xs->cmd;
cmdlen = xs->cmdlen;
dir = DIR_NONE;
if (xs->datalen) {
switch (xs->flags & (SCSI_DATA_IN | SCSI_DATA_OUT)) {
case SCSI_DATA_IN:
dir = DIR_IN;
break;
case SCSI_DATA_OUT:
dir = DIR_OUT;
break;
}
}
if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
printf("umass_cmd: large datalen, %d\n", xs->datalen);
xs->error = XS_DRIVER_STUFFUP;
goto done;
}
if (xs->flags & SCSI_POLL) {
DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: sync dir=%d\n", dir));
usbd_set_polling(sc->sc_udev, 1);
sc->sc_xfer_flags = USBD_SYNCHRONOUS;
sc->polled_xfer_status = USBD_INVAL;
sc->sc_methods->wire_xfer(sc, sc_link->lun, cmd, cmdlen,
xs->data, xs->datalen, dir,
xs->timeout, umass_scsi_cb, xs);
sc->sc_xfer_flags = 0;
DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
sc->polled_xfer_status));
usbd_set_polling(sc->sc_udev, 0);
/* scsi_done() has already been called. */
return;
} else {
DPRINTF(UDMASS_SCSI,
("umass_scsi_cmd: async dir=%d, cmdlen=%d"
" datalen=%d\n",
dir, cmdlen, xs->datalen));
sc->sc_methods->wire_xfer(sc, sc_link->lun, cmd, cmdlen,
xs->data, xs->datalen, dir,
xs->timeout, umass_scsi_cb, xs);
/* scsi_done() has already been called. */
return;
}
/* Return if command finishes early. */
done:
scsi_done(xs);
}
void
umass_scsi_minphys(struct buf *bp, struct scsi_link *sl)
{
if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
minphys(bp);
}
void
umass_scsi_cb(struct umass_softc *sc, void *priv, int residue, int status)
{
struct umass_scsi_softc *scbus = sc->bus;
struct scsi_xfer *xs = priv;
struct scsi_link *link = xs->sc_link;
int cmdlen;
#ifdef UMASS_DEBUG
struct timeval tv;
u_int delta;
microtime(&tv);
delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 +
tv.tv_usec - sc->tv.tv_usec;
#endif
DPRINTF(UDMASS_CMD,
("umass_scsi_cb: at %lld.%06ld, delta=%u: xs=%p residue=%d"
" status=%d\n", (long long)tv.tv_sec, tv.tv_usec, delta, xs, residue,
status));
xs->resid = residue;
switch (status) {
case STATUS_CMD_OK:
xs->error = XS_NOERROR;
break;
case STATUS_CMD_UNKNOWN:
DPRINTF(UDMASS_CMD, ("umass_scsi_cb: status cmd unknown\n"));
/* we can't issue REQUEST SENSE */
if (xs->sc_link->quirks & ADEV_NOSENSE) {
/*
* If no residue and no other USB error,
* command succeeded.
*/
if (residue == 0) {
xs->error = XS_NOERROR;
break;
}
/*
* Some devices return a short INQUIRY
* response, omitting response data from the
* "vendor specific data" on...
*/
if (xs->cmd->opcode == INQUIRY &&
residue < xs->datalen) {
xs->error = XS_NOERROR;
break;
}
xs->error = XS_DRIVER_STUFFUP;
break;
}
/* FALLTHROUGH */
case STATUS_CMD_FAILED:
DPRINTF(UDMASS_CMD, ("umass_scsi_cb: status cmd failed for "
"scsi op 0x%02x\n", xs->cmd->opcode));
/* fetch sense data */
sc->sc_sense = 1;
memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
scbus->sc_sense_cmd.opcode = REQUEST_SENSE;
scbus->sc_sense_cmd.byte2 = link->lun << SCSI_CMD_LUN_SHIFT;
scbus->sc_sense_cmd.length = sizeof(xs->sense);
cmdlen = sizeof(scbus->sc_sense_cmd);
if (xs->flags & SCSI_POLL) {
usbd_set_polling(sc->sc_udev, 1);
sc->sc_xfer_flags = USBD_SYNCHRONOUS;
sc->polled_xfer_status = USBD_INVAL;
}
/* scsi_done() has already been called. */
sc->sc_methods->wire_xfer(sc, link->lun,
&scbus->sc_sense_cmd, cmdlen,
&xs->sense, sizeof(xs->sense),
DIR_IN, xs->timeout,
umass_scsi_sense_cb, xs);
if (xs->flags & SCSI_POLL) {
sc->sc_xfer_flags = 0;
usbd_set_polling(sc->sc_udev, 0);
}
return;
case STATUS_WIRE_FAILED:
xs->error = XS_RESET;
break;
default:
panic("%s: Unknown status %d in umass_scsi_cb",
sc->sc_dev.dv_xname, status);
}
DPRINTF(UDMASS_CMD,("umass_scsi_cb: at %lld.%06ld: return error=%d, "
"status=0x%x resid=%zu\n",
(long long)tv.tv_sec, tv.tv_usec,
xs->error, xs->status, xs->resid));
if ((xs->flags & SCSI_POLL) && (xs->error == XS_NOERROR)) {
switch (sc->polled_xfer_status) {
case USBD_NORMAL_COMPLETION:
xs->error = XS_NOERROR;
break;
case USBD_TIMEOUT:
xs->error = XS_TIMEOUT;
break;
default:
xs->error = XS_DRIVER_STUFFUP;
break;
}
}
scsi_done(xs);
}
/*
* Finalise a completed autosense operation
*/
void
umass_scsi_sense_cb(struct umass_softc *sc, void *priv, int residue,
int status)
{
struct scsi_xfer *xs = priv;
DPRINTF(UDMASS_CMD,("umass_scsi_sense_cb: xs=%p residue=%d "
"status=%d\n", xs, residue, status));
sc->sc_sense = 0;
switch (status) {
case STATUS_CMD_OK:
case STATUS_CMD_UNKNOWN:
/* getting sense data succeeded */
if (residue == 0 || residue == 14)/* XXX */
xs->error = XS_SENSE;
else
xs->error = XS_SHORTSENSE;
break;
default:
DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
sc->sc_dev.dv_xname, status));
xs->error = XS_DRIVER_STUFFUP;
break;
}
DPRINTF(UDMASS_CMD,("umass_scsi_sense_cb: return xs->error=%d, "
"xs->flags=0x%x xs->resid=%zu\n", xs->error, xs->status,
xs->resid));
if ((xs->flags & SCSI_POLL) && (xs->error == XS_NOERROR)) {
switch (sc->polled_xfer_status) {
case USBD_NORMAL_COMPLETION:
xs->error = XS_NOERROR;
break;
case USBD_TIMEOUT:
xs->error = XS_TIMEOUT;
break;
default:
xs->error = XS_DRIVER_STUFFUP;
break;
}
}
scsi_done(xs);
}
void *
umass_io_get(void *cookie)
{
struct umass_scsi_softc *scbus = cookie;
void *io = NULL;
int s;
s = splusb();
if (!scbus->sc_open) {
scbus->sc_open = 1;
io = scbus; /* just has to be non-NULL */
}
splx(s);
return (io);
}
void
umass_io_put(void *cookie, void *io)
{
struct umass_scsi_softc *scbus = cookie;
int s;
s = splusb();
scbus->sc_open = 0;
splx(s);
}
|