xref: /netbsd-src/sys/dev/usb/umass_scsipi.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: umass_scsipi.c,v 1.9 2003/02/16 23:14:08 augustss Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: umass_scsipi.c,v 1.9 2003/02/16 23:14:08 augustss Exp $");
42 
43 #include "atapibus.h"
44 #include "scsibus.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/conf.h>
50 #include <sys/buf.h>
51 #include <sys/device.h>
52 #include <sys/ioctl.h>
53 #include <sys/malloc.h>
54 
55 /* SCSI & ATAPI */
56 #include <sys/scsiio.h>
57 #include <dev/scsipi/scsi_all.h>
58 #include <dev/scsipi/scsipi_all.h>
59 #include <dev/scsipi/scsiconf.h>
60 
61 #include <dev/scsipi/atapiconf.h>
62 
63 #include <dev/scsipi/scsipi_disk.h>
64 #include <dev/scsipi/scsi_disk.h>
65 #include <dev/scsipi/scsi_changer.h>
66 
67 #include <dev/scsipi/atapi_disk.h>
68 
69 #include <sys/disk.h>		/* XXX */
70 #include <dev/scsipi/sdvar.h>	/* XXX */
71 
72 /* USB */
73 #include <dev/usb/usb.h>
74 #include <dev/usb/usbdi.h>
75 #include <dev/usb/usbdi_util.h>
76 #include <dev/usb/usbdevs.h>
77 
78 #include <dev/usb/umassvar.h>
79 #include <dev/usb/umass_scsipi.h>
80 
81 struct umass_scsipi_softc {
82 	struct umassbus_softc	base;
83 
84 	struct atapi_adapter	sc_atapi_adapter;
85 #define sc_adapter sc_atapi_adapter._generic
86 	struct scsipi_channel sc_channel;
87 	usbd_status		sc_sync_status;
88 	struct scsipi_sense	sc_sense_cmd;
89 };
90 
91 
92 #define SHORT_INQUIRY_LENGTH    36 /* XXX */
93 
94 #define UMASS_SCSIID_HOST	0x00
95 #define UMASS_SCSIID_DEVICE	0x01
96 
97 #define UMASS_ATAPI_DRIVE	0
98 
99 Static void umass_scsipi_request(struct scsipi_channel *,
100 				 scsipi_adapter_req_t, void *);
101 Static void umass_scsipi_minphys(struct buf *bp);
102 Static int umass_scsipi_ioctl(struct scsipi_channel *, u_long,
103 			      caddr_t, int, usb_proc_ptr );
104 Static int umass_scsipi_getgeom(struct scsipi_periph *periph,
105 				struct disk_parms *, u_long sectors);
106 
107 Static void umass_scsipi_cb(struct umass_softc *sc, void *priv,
108 			    int residue, int status);
109 Static void umass_scsipi_sense_cb(struct umass_softc *sc, void *priv,
110 				  int residue, int status);
111 
112 Static struct umass_scsipi_softc *umass_scsipi_setup(struct umass_softc *sc);
113 
114 Static int scsipiprint(void *aux, const char *pnp);
115 
116 #if NATAPIBUS > 0
117 Static void umass_atapi_probe_device(struct atapibus_softc *, int);
118 
119 const struct scsipi_bustype umass_atapi_bustype = {
120 	SCSIPI_BUSTYPE_ATAPI,
121 	atapi_scsipi_cmd,
122 	atapi_interpret_sense,
123 	atapi_print_addr,
124 	scsi_kill_pending,
125 };
126 #endif
127 
128 
129 #if NSCSIBUS > 0
130 int
131 umass_scsi_attach(struct umass_softc *sc)
132 {
133 	struct umass_scsipi_softc *scbus;
134 
135 	scbus = umass_scsipi_setup(sc);
136 
137 	scbus->sc_channel.chan_bustype = &scsi_bustype;
138 	scbus->sc_channel.chan_ntargets = UMASS_SCSIID_DEVICE + 1;
139 	scbus->sc_channel.chan_nluns = sc->maxlun + 1;
140 	scbus->sc_channel.chan_id = UMASS_SCSIID_HOST;
141 	DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: SCSI\n",
142 			     USBDEVNAME(sc->sc_dev)));
143 
144 	sc->sc_refcnt++;
145 	scbus->base.sc_child =
146 	    config_found(&sc->sc_dev, &scbus->sc_channel, scsipiprint);
147 	if (--sc->sc_refcnt < 0)
148 		usb_detach_wakeup(USBDEV(sc->sc_dev));
149 
150 	return (0);
151 }
152 #endif
153 
154 #if NATAPIBUS > 0
155 int
156 umass_atapi_attach(struct umass_softc *sc)
157 {
158 	struct umass_scsipi_softc *scbus;
159 
160 	scbus = umass_scsipi_setup(sc);
161 	scbus->sc_atapi_adapter.atapi_probe_device =  umass_atapi_probe_device;
162 
163 	scbus->sc_channel.chan_bustype = &umass_atapi_bustype;
164 	scbus->sc_channel.chan_ntargets = 2;
165 	scbus->sc_channel.chan_nluns = 1;
166 
167 	scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
168 	DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: ATAPI\n",
169 			     USBDEVNAME(sc->sc_dev)));
170 
171 	sc->sc_refcnt++;
172 	scbus->base.sc_child =
173 	    config_found(&sc->sc_dev, &scbus->sc_channel, scsipiprint);
174 	if (--sc->sc_refcnt < 0)
175 		usb_detach_wakeup(USBDEV(sc->sc_dev));
176 
177 	return (0);
178 }
179 #endif
180 
181 Static struct umass_scsipi_softc *
182 umass_scsipi_setup(struct umass_softc *sc)
183 {
184 	struct umass_scsipi_softc *scbus;
185 
186 	scbus = malloc(sizeof *scbus, M_DEVBUF, M_WAITOK | M_ZERO);
187 	sc->bus = &scbus->base;
188 
189 	/* Only use big commands for USB SCSI devices. */
190 	sc->sc_busquirks |= PQUIRK_ONLYBIG;
191 
192 	/* Fill in the adapter. */
193 	memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter));
194 	scbus->sc_adapter.adapt_dev = &sc->sc_dev;
195 	scbus->sc_adapter.adapt_nchannels = 1;
196 	scbus->sc_adapter.adapt_request = umass_scsipi_request;
197 	scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys;
198 	scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl;
199 	scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom;
200 
201 	/* Fill in the channel. */
202 	memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel));
203 	scbus->sc_channel.chan_adapter = &scbus->sc_adapter;
204 	scbus->sc_channel.chan_channel = 0;
205 	scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS;
206 	scbus->sc_channel.chan_openings = 1;
207 	scbus->sc_channel.chan_max_periph = 1;
208 	scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
209 
210 	return (scbus);
211 }
212 
213 Static int
214 scsipiprint(void *aux, const char *pnp)
215 {
216 	struct scsipi_channel *chan = aux;
217 
218 	if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_SCSI) {
219 #if NSCSIBUS > 0
220 		return (scsiprint(aux, pnp));
221 #else
222 		if (pnp)
223 			aprint_normal("scsibus at %s", pnp);
224 		return (UNCONF);
225 #endif
226 	} else {
227 #if NATAPIBUS > 0
228 		return (atapiprint(aux, pnp));
229 #else
230 		if (pnp)
231 			aprint_normal("atapibus at %s", pnp);
232 		return (UNCONF);
233 #endif
234 	}
235 }
236 
237 Static void
238 umass_scsipi_request(struct scsipi_channel *chan,
239 		scsipi_adapter_req_t req, void *arg)
240 {
241 	struct scsipi_adapter *adapt = chan->chan_adapter;
242 	struct scsipi_periph *periph;
243 	struct scsipi_xfer *xs;
244 	struct umass_softc *sc = (void *)adapt->adapt_dev;
245 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
246 	struct scsipi_generic *cmd, trcmd;
247 	int cmdlen;
248 	int dir;
249 #ifdef UMASS_DEBUG
250 	microtime(&sc->tv);
251 #endif
252 	switch(req) {
253 	case ADAPTER_REQ_RUN_XFER:
254 		xs = arg;
255 		periph = xs->xs_periph;
256 		DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
257 
258 		DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %lu.%06lu: %d:%d "
259 		    "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
260 		    USBDEVNAME(sc->sc_dev), sc->tv.tv_sec, sc->tv.tv_usec,
261 		    periph->periph_target, periph->periph_lun,
262 		    xs, xs->cmd->opcode, xs->datalen,
263 		    periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
264 #if defined(USB_DEBUG) && defined(SCSIPI_DEBUG)
265 		if (umassdebug & UDMASS_SCSI)
266 			show_scsipi_xs(xs);
267 		else if (umassdebug & ~UDMASS_CMD)
268 			show_scsipi_cmd(xs);
269 #endif
270 
271 		if (sc->sc_dying) {
272 			xs->error = XS_DRIVER_STUFFUP;
273 			goto done;
274 		}
275 
276 #ifdef UMASS_DEBUG
277 		if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_ATAPI ?
278 		    periph->periph_target != UMASS_ATAPI_DRIVE :
279 		    periph->periph_target != UMASS_SCSIID_DEVICE) {
280 			DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
281 			    USBDEVNAME(sc->sc_dev),
282 			    periph->periph_target));
283 			xs->error = XS_DRIVER_STUFFUP;
284 			goto done;
285 		}
286 #endif
287 
288 		cmd = xs->cmd;
289 		cmdlen = xs->cmdlen;
290 
291 		/* XXX should use transform */
292 
293 		if (cmd->opcode == START_STOP &&
294 		    (sc->sc_quirks & UMASS_QUIRK_NO_START_STOP)) {
295 			/*printf("%s: START_STOP\n", USBDEVNAME(sc->sc_dev));*/
296 			xs->error = XS_NOERROR;
297 			goto done;
298 		}
299 
300 		if (cmd->opcode == INQUIRY &&
301 		    (sc->sc_quirks & UMASS_QUIRK_FORCE_SHORT_INQUIRY)) {
302 			/*
303 			 * Some drives wedge when asked for full inquiry
304 			 * information.
305 			 */
306 			memcpy(&trcmd, cmd, sizeof trcmd);
307 			trcmd.bytes[4] = SHORT_INQUIRY_LENGTH;
308 			cmd = &trcmd;
309 			xs->datalen = SHORT_INQUIRY_LENGTH;
310 		}
311 
312 		dir = DIR_NONE;
313 		if (xs->datalen) {
314 			switch (xs->xs_control &
315 			    (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
316 			case XS_CTL_DATA_IN:
317 				dir = DIR_IN;
318 				break;
319 			case XS_CTL_DATA_OUT:
320 				dir = DIR_OUT;
321 				break;
322 			}
323 		}
324 
325 		if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
326 			printf("umass_cmd: large datalen, %d\n", xs->datalen);
327 			xs->error = XS_DRIVER_STUFFUP;
328 			goto done;
329 		}
330 
331 		if (xs->xs_control & XS_CTL_POLL) {
332 			/* Use sync transfer. XXX Broken! */
333 			DPRINTF(UDMASS_SCSI,
334 			    ("umass_scsi_cmd: sync dir=%d\n", dir));
335 			sc->sc_xfer_flags = USBD_SYNCHRONOUS;
336 			scbus->sc_sync_status = USBD_INVAL;
337 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
338 						  cmdlen, xs->data,
339 						  xs->datalen, dir,
340 						  xs->timeout, 0, xs);
341 			sc->sc_xfer_flags = 0;
342 			DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
343 					      scbus->sc_sync_status));
344 			switch (scbus->sc_sync_status) {
345 			case USBD_NORMAL_COMPLETION:
346 				xs->error = XS_NOERROR;
347 				break;
348 			case USBD_TIMEOUT:
349 				xs->error = XS_TIMEOUT;
350 				break;
351 			default:
352 				xs->error = XS_DRIVER_STUFFUP;
353 				break;
354 			}
355 			goto done;
356 		} else {
357 			DPRINTF(UDMASS_SCSI,
358 			    ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
359 				      " datalen=%d\n",
360 				      dir, cmdlen, xs->datalen));
361 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
362 						  cmdlen, xs->data,
363 						  xs->datalen, dir,
364 						  xs->timeout,
365 						  umass_scsipi_cb, xs);
366 			return;
367 		}
368 
369 		/* Return if command finishes early. */
370  done:
371 		scsipi_done(xs);
372 		return;
373 	default:
374 		/* Not supported, nothing to do. */
375 		;
376 	}
377 }
378 
379 Static void
380 umass_scsipi_minphys(struct buf *bp)
381 {
382 #ifdef DIAGNOSTIC
383 	if (bp->b_bcount <= 0) {
384 		printf("umass_scsipi_minphys count(%ld) <= 0\n",
385 		       bp->b_bcount);
386 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
387 	}
388 #endif
389 	if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
390 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
391 	minphys(bp);
392 }
393 
394 int
395 umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, caddr_t arg,
396 		   int flag, usb_proc_ptr p)
397 {
398 	/*struct umass_softc *sc = link->adapter_softc;*/
399 	/*struct umass_scsipi_softc *scbus = sc->bus;*/
400 
401 	switch (cmd) {
402 #if 0
403 	case SCBUSIORESET:
404 		ccb->ccb_h.status = CAM_REQ_INPROG;
405 		umass_reset(sc, umass_cam_cb, (void *) ccb);
406 		return (0);
407 #endif
408 	default:
409 		return (ENOTTY);
410 	}
411 }
412 
413 Static int
414 umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
415 		     u_long sectors)
416 {
417 	struct umass_softc *sc =
418 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
419 
420 	/* If it's not a floppy, we don't know what to do. */
421 	if (sc->sc_cmd != UMASS_CPROTO_UFI)
422 		return (0);
423 
424 	switch (sectors) {
425 	case 1440:
426 		/* Most likely a single density 3.5" floppy. */
427 		dp->heads = 2;
428 		dp->sectors = 9;
429 		dp->cyls = 80;
430 		return (1);
431 	case 2880:
432 		/* Most likely a double density 3.5" floppy. */
433 		dp->heads = 2;
434 		dp->sectors = 18;
435 		dp->cyls = 80;
436 		return (1);
437 	default:
438 		return (0);
439 	}
440 }
441 
442 Static void
443 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
444 {
445 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
446 	struct scsipi_xfer *xs = priv;
447 	struct scsipi_periph *periph = xs->xs_periph;
448 	int cmdlen;
449 	int s;
450 #ifdef UMASS_DEBUG
451 	struct timeval tv;
452 	u_int delta;
453 	microtime(&tv);
454 	delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
455 #endif
456 
457 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu, delta=%u: xs=%p residue=%d"
458 	    " status=%d\n", tv.tv_sec, tv.tv_usec, delta, xs, residue, status));
459 
460 	xs->resid = residue;
461 
462 	switch (status) {
463 	case STATUS_CMD_OK:
464 		xs->error = XS_NOERROR;
465 		break;
466 
467 	case STATUS_CMD_UNKNOWN:
468 		/* we can't issue REQUEST SENSE */
469 		if (xs->xs_periph->periph_quirks & PQUIRK_NOSENSE) {
470 			/*
471 			 * If no residue and no other USB error,
472 			 * command succeeded.
473 			 */
474 			if (residue == 0) {
475 				xs->error = XS_NOERROR;
476 				break;
477 			}
478 
479 			/*
480 			 * Some devices return a short INQUIRY
481 			 * response, omitting response data from the
482 			 * "vendor specific data" on...
483 			 */
484 			if (xs->cmd->opcode == INQUIRY &&
485 			    residue < xs->datalen) {
486 				xs->error = XS_NOERROR;
487 				break;
488 			}
489 
490 			xs->error = XS_DRIVER_STUFFUP;
491 			break;
492 		}
493 		/* FALLTHROUGH */
494 	case STATUS_CMD_FAILED:
495 		/* fetch sense data */
496 		memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
497 		scbus->sc_sense_cmd.opcode = REQUEST_SENSE;
498 		scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
499 		    SCSI_CMD_LUN_SHIFT;
500 		scbus->sc_sense_cmd.length = sizeof(xs->sense);
501 
502 		cmdlen = sizeof(scbus->sc_sense_cmd);
503 		if (sc->sc_cmd == UMASS_CPROTO_UFI) /* XXX */
504 			cmdlen = UFI_COMMAND_LENGTH;
505 		sc->sc_methods->wire_xfer(sc, periph->periph_lun,
506 					  &scbus->sc_sense_cmd, cmdlen,
507 					  &xs->sense, sizeof(xs->sense),
508 					  DIR_IN, xs->timeout,
509 					  umass_scsipi_sense_cb, xs);
510 		return;
511 
512 	case STATUS_WIRE_FAILED:
513 		xs->error = XS_RESET;
514 		break;
515 
516 	default:
517 		panic("%s: Unknown status %d in umass_scsipi_cb",
518 			USBDEVNAME(sc->sc_dev), status);
519 	}
520 
521 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu: return xs->error="
522             "%d, xs->xs_status=0x%x xs->resid=%d\n",
523 	     tv.tv_sec, tv.tv_usec,
524 	     xs->error, xs->xs_status, xs->resid));
525 
526 	s = splbio();
527 	scsipi_done(xs);
528 	splx(s);
529 }
530 
531 /*
532  * Finalise a completed autosense operation
533  */
534 Static void
535 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
536 		      int status)
537 {
538 	struct scsipi_xfer *xs = priv;
539 	int s;
540 
541 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
542 		"status=%d\n", xs, residue, status));
543 
544 	switch (status) {
545 	case STATUS_CMD_OK:
546 	case STATUS_CMD_UNKNOWN:
547 		/* getting sense data succeeded */
548 		if (xs->cmd->opcode == INQUIRY && (xs->resid < xs->datalen ||
549 		    (sc->sc_quirks & UMASS_QUIRK_RS_NO_CLEAR_UA /* XXX */))) {
550 			/*
551 			 * Some drivers return SENSE errors even after INQUIRY.
552 			 * The upper layer doesn't like that.
553 			 */
554 			xs->error = XS_NOERROR;
555 			break;
556 		}
557 		/* XXX look at residue */
558 		if (residue == 0 || residue == 14)/* XXX */
559 			xs->error = XS_SENSE;
560 		else
561 			xs->error = XS_SHORTSENSE;
562 		break;
563 	default:
564 		DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
565 			USBDEVNAME(sc->sc_dev), status));
566 		xs->error = XS_DRIVER_STUFFUP;
567 		break;
568 	}
569 
570 	xs->xs_status |= XS_STS_DONE;
571 
572 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
573 		"xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
574 		xs->resid));
575 
576 	s = splbio();
577 	scsipi_done(xs);
578 	splx(s);
579 }
580 
581 #if NATAPIBUS > 0
582 Static void
583 umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
584 {
585 	struct scsipi_channel *chan = atapi->sc_channel;
586 	struct scsipi_periph *periph;
587 	struct scsipibus_attach_args sa;
588 	char vendor[33], product[65], revision[17];
589 	struct scsipi_inquiry_data inqbuf;
590 
591 	DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n",
592 			     atapi, target));
593 
594 	if (target != UMASS_ATAPI_DRIVE)	/* only probe drive 0 */
595 		return;
596 
597 	/* skip if already attached */
598 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
599 		return;
600 
601 	periph = scsipi_alloc_periph(M_NOWAIT);
602 	if (periph == NULL) {
603 		printf("%s: can't allocate link for drive %d\n",
604 		       atapi->sc_dev.dv_xname, target);
605 		return;
606 	}
607 
608 	DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
609 	periph->periph_channel = chan;
610 	periph->periph_switch = &atapi_probe_periphsw;
611 	periph->periph_target = target;
612 	periph->periph_quirks = chan->chan_defquirks;
613 
614 	DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n"));
615 	/* Now go ask the device all about itself. */
616 	memset(&inqbuf, 0, sizeof(inqbuf));
617 	if (scsipi_inquire(periph, &inqbuf,
618 	    XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK) != 0) {
619 		DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: "
620 		    "scsipi_inquire failed\n"));
621 		free(periph, M_DEVBUF);
622 		return;
623 	}
624 
625 	scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
626 	scsipi_strvis(product, 65, inqbuf.product, 16);
627 	scsipi_strvis(revision, 17, inqbuf.revision, 4);
628 
629 	sa.sa_periph = periph;
630 	sa.sa_inqbuf.type = inqbuf.device;
631 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
632 	    T_REMOV : T_FIXED;
633 	if (sa.sa_inqbuf.removable)
634 		periph->periph_flags |= PERIPH_REMOVABLE;
635 	sa.sa_inqbuf.vendor = vendor;
636 	sa.sa_inqbuf.product = product;
637 	sa.sa_inqbuf.revision = revision;
638 	sa.sa_inqptr = NULL;
639 
640 	DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing atapi_probedev on "
641 			      "'%s' '%s' '%s'\n", vendor, product, revision));
642 	atapi_probe_device(atapi, target, periph, &sa);
643 	/* atapi_probe_device() frees the periph when there is no device.*/
644 }
645 #endif
646