xref: /netbsd/sys/dev/usb/umass_scsipi.c (revision bf9ec67e)
1 /*	$NetBSD: umass_scsipi.c,v 1.4 2001/12/31 12:15:21 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.4 2001/12/31 12:15:21 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 	scbus->base.sc_child =
144 	    config_found(&sc->sc_dev, &scbus->sc_channel, scsipiprint);
145 
146 	return (0);
147 }
148 #endif
149 
150 #if NATAPIBUS > 0
151 int
152 umass_atapi_attach(struct umass_softc *sc)
153 {
154 	struct umass_scsipi_softc *scbus;
155 
156 	scbus = umass_scsipi_setup(sc);
157 	scbus->sc_atapi_adapter.atapi_probe_device =  umass_atapi_probe_device;
158 
159 	scbus->sc_channel.chan_bustype = &umass_atapi_bustype;
160 	scbus->sc_channel.chan_ntargets = 2;
161 	scbus->sc_channel.chan_nluns = 1;
162 
163 	scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
164 	DPRINTF(UDMASS_USB, ("%s: umass_attach_bus: ATAPI\n",
165 			     USBDEVNAME(sc->sc_dev)));
166 	scbus->base.sc_child =
167 	    config_found(&sc->sc_dev, &scbus->sc_channel, scsipiprint);
168 
169 	return (0);
170 }
171 #endif
172 
173 Static struct umass_scsipi_softc *
174 umass_scsipi_setup(struct umass_softc *sc)
175 {
176 	struct umass_scsipi_softc *scbus;
177 
178 	scbus = malloc(sizeof *scbus, M_DEVBUF, M_WAITOK | M_ZERO);
179 	sc->bus = &scbus->base;
180 
181 	/* Only use big commands for USB SCSI devices. */
182 	sc->sc_busquirks |= PQUIRK_ONLYBIG;
183 
184 	/* Fill in the adapter. */
185 	memset(&scbus->sc_adapter, 0, sizeof(scbus->sc_adapter));
186 	scbus->sc_adapter.adapt_dev = &sc->sc_dev;
187 	scbus->sc_adapter.adapt_nchannels = 1;
188 	scbus->sc_adapter.adapt_request = umass_scsipi_request;
189 	scbus->sc_adapter.adapt_minphys = umass_scsipi_minphys;
190 	scbus->sc_adapter.adapt_ioctl = umass_scsipi_ioctl;
191 	scbus->sc_adapter.adapt_getgeom = umass_scsipi_getgeom;
192 
193 	/* Fill in the channel. */
194 	memset(&scbus->sc_channel, 0, sizeof(scbus->sc_channel));
195 	scbus->sc_channel.chan_adapter = &scbus->sc_adapter;
196 	scbus->sc_channel.chan_channel = 0;
197 	scbus->sc_channel.chan_flags = SCSIPI_CHAN_OPENINGS;
198 	scbus->sc_channel.chan_openings = 1;
199 	scbus->sc_channel.chan_max_periph = 1;
200 	scbus->sc_channel.chan_defquirks |= sc->sc_busquirks;
201 
202 	return (scbus);
203 }
204 
205 Static int
206 scsipiprint(void *aux, const char *pnp)
207 {
208 	struct scsipi_channel *chan = aux;
209 
210 	if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_SCSI) {
211 #if NSCSIBUS > 0
212 		return (scsiprint(aux, pnp));
213 #else
214 		if (pnp)
215 			printf("scsibus at %s", pnp);
216 		return (UNCONF);
217 #endif
218 	} else {
219 #if NATAPIBUS > 0
220 		return (atapiprint(aux, pnp));
221 #else
222 		if (pnp)
223 			printf("atapibus at %s", pnp);
224 		return (UNCONF);
225 #endif
226 	}
227 }
228 
229 Static void
230 umass_scsipi_request(struct scsipi_channel *chan,
231 		scsipi_adapter_req_t req, void *arg)
232 {
233 	struct scsipi_adapter *adapt = chan->chan_adapter;
234 	struct scsipi_periph *periph;
235 	struct scsipi_xfer *xs;
236 	struct umass_softc *sc = (void *)adapt->adapt_dev;
237 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
238 	struct scsipi_generic *cmd, trcmd;
239 	int cmdlen;
240 	int dir;
241 #ifdef UMASS_DEBUG
242 	microtime(&sc->tv);
243 #endif
244 	switch(req) {
245 	case ADAPTER_REQ_RUN_XFER:
246 		xs = arg;
247 		periph = xs->xs_periph;
248 		DIF(UDMASS_UPPER, periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS);
249 
250 		DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: at %lu.%06lu: %d:%d "
251 		    "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
252 		    USBDEVNAME(sc->sc_dev), sc->tv.tv_sec, sc->tv.tv_usec,
253 		    periph->periph_target, periph->periph_lun,
254 		    xs, xs->cmd->opcode, xs->datalen,
255 		    periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
256 #if defined(USB_DEBUG) && defined(SCSIPI_DEBUG)
257 		if (umassdebug & UDMASS_SCSI)
258 			show_scsipi_xs(xs);
259 		else if (umassdebug & ~UDMASS_CMD)
260 			show_scsipi_cmd(xs);
261 #endif
262 
263 		if (sc->sc_dying) {
264 			xs->error = XS_DRIVER_STUFFUP;
265 			goto done;
266 		}
267 
268 #ifdef UMASS_DEBUG
269 		if (chan->chan_bustype->bustype_type == SCSIPI_BUSTYPE_ATAPI ?
270 		    periph->periph_target != UMASS_ATAPI_DRIVE :
271 		    periph->periph_target != UMASS_SCSIID_DEVICE) {
272 			DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
273 			    USBDEVNAME(sc->sc_dev),
274 			    periph->periph_target));
275 			xs->error = XS_DRIVER_STUFFUP;
276 			goto done;
277 		}
278 #endif
279 
280 		cmd = xs->cmd;
281 		cmdlen = xs->cmdlen;
282 
283 		/* XXX should use transform */
284 
285 		if (cmd->opcode == START_STOP &&
286 		    (sc->sc_quirks & UMASS_QUIRK_NO_START_STOP)) {
287 			/*printf("%s: START_STOP\n", USBDEVNAME(sc->sc_dev));*/
288 			xs->error = XS_NOERROR;
289 			goto done;
290 		}
291 
292 		if (cmd->opcode == INQUIRY &&
293 		    (sc->sc_quirks & UMASS_QUIRK_FORCE_SHORT_INQUIRY)) {
294 			/*
295 			 * some drives wedge when asked for full inquiry
296 			 * information.
297 			 */
298 			memcpy(&trcmd, cmd, sizeof trcmd);
299 			trcmd.bytes[4] = SHORT_INQUIRY_LENGTH;
300 			cmd = &trcmd;
301 		}
302 
303 		dir = DIR_NONE;
304 		if (xs->datalen) {
305 			switch (xs->xs_control &
306 			    (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
307 			case XS_CTL_DATA_IN:
308 				dir = DIR_IN;
309 				break;
310 			case XS_CTL_DATA_OUT:
311 				dir = DIR_OUT;
312 				break;
313 			}
314 		}
315 
316 		if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
317 			printf("umass_cmd: large datalen, %d\n", xs->datalen);
318 			xs->error = XS_DRIVER_STUFFUP;
319 			goto done;
320 		}
321 
322 		if (xs->xs_control & XS_CTL_POLL) {
323 			/* Use sync transfer. XXX Broken! */
324 			DPRINTF(UDMASS_SCSI,
325 			    ("umass_scsi_cmd: sync dir=%d\n", dir));
326 			sc->sc_xfer_flags = USBD_SYNCHRONOUS;
327 			scbus->sc_sync_status = USBD_INVAL;
328 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
329 						  cmdlen, xs->data,
330 						  xs->datalen, dir,
331 						  xs->timeout, 0, xs);
332 			sc->sc_xfer_flags = 0;
333 			DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
334 					      scbus->sc_sync_status));
335 			switch (scbus->sc_sync_status) {
336 			case USBD_NORMAL_COMPLETION:
337 				xs->error = XS_NOERROR;
338 				break;
339 			case USBD_TIMEOUT:
340 				xs->error = XS_TIMEOUT;
341 				break;
342 			default:
343 				xs->error = XS_DRIVER_STUFFUP;
344 				break;
345 			}
346 			goto done;
347 		} else {
348 			DPRINTF(UDMASS_SCSI,
349 			    ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
350 				      " datalen=%d\n",
351 				      dir, cmdlen, xs->datalen));
352 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
353 						  cmdlen, xs->data,
354 						  xs->datalen, dir,
355 						  xs->timeout,
356 						  umass_scsipi_cb, xs);
357 			return;
358 		}
359 
360 		/* Return if command finishes early. */
361  done:
362 		scsipi_done(xs);
363 		return;
364 	default:
365 		/* Not supported, nothing to do. */
366 		;
367 	}
368 }
369 
370 Static void
371 umass_scsipi_minphys(struct buf *bp)
372 {
373 #ifdef DIAGNOSTIC
374 	if (bp->b_bcount <= 0) {
375 		printf("umass_scsipi_minphys count(%ld) <= 0\n",
376 		       bp->b_bcount);
377 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
378 	}
379 #endif
380 	if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
381 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
382 	minphys(bp);
383 }
384 
385 int
386 umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, caddr_t arg,
387 		   int flag, usb_proc_ptr p)
388 {
389 	/*struct umass_softc *sc = link->adapter_softc;*/
390 	/*struct umass_scsipi_softc *scbus = sc->bus;*/
391 
392 	switch (cmd) {
393 #if 0
394 	case SCBUSIORESET:
395 		ccb->ccb_h.status = CAM_REQ_INPROG;
396 		umass_reset(sc, umass_cam_cb, (void *) ccb);
397 		return (0);
398 #endif
399 	default:
400 		return (ENOTTY);
401 	}
402 }
403 
404 Static int
405 umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
406 		     u_long sectors)
407 {
408 	struct umass_softc *sc =
409 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
410 
411 	/* If it's not a floppy, we don't know what to do. */
412 	if (sc->sc_cmd != UMASS_CPROTO_UFI)
413 		return (0);
414 
415 	switch (sectors) {
416 	case 1440:
417 		/* Most likely a single density 3.5" floppy. */
418 		dp->heads = 2;
419 		dp->sectors = 9;
420 		dp->cyls = 80;
421 		return (1);
422 	case 2880:
423 		/* Most likely a double density 3.5" floppy. */
424 		dp->heads = 2;
425 		dp->sectors = 18;
426 		dp->cyls = 80;
427 		return (1);
428 	default:
429 		return (0);
430 	}
431 }
432 
433 Static void
434 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
435 {
436 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
437 	struct scsipi_xfer *xs = priv;
438 	struct scsipi_periph *periph = xs->xs_periph;
439 	int cmdlen;
440 	int s;
441 #ifdef UMASS_DEBUG
442 	struct timeval tv;
443 	u_int delta;
444 	microtime(&tv);
445 	delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
446 #endif
447 
448 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu, delta=%u: xs=%p residue=%d"
449 	    " status=%d\n", tv.tv_sec, tv.tv_usec, delta, xs, residue, status));
450 
451 	xs->resid = residue;
452 
453 	switch (status) {
454 	case STATUS_CMD_OK:
455 		xs->error = XS_NOERROR;
456 		break;
457 
458 	case STATUS_CMD_UNKNOWN:
459 		/* we can't issue REQUEST SENSE */
460 		if (xs->xs_periph->periph_quirks & PQUIRK_NOSENSE) {
461 			/*
462 			 * If no residue and no other USB error,
463 			 * command succeeded.
464 			 */
465 			if (residue == 0) {
466 				xs->error = XS_NOERROR;
467 				break;
468 			}
469 
470 			/*
471 			 * Some devices return a short INQUIRY
472 			 * response, omitting response data from the
473 			 * "vendor specific data" on...
474 			 */
475 			if (xs->cmd->opcode == INQUIRY &&
476 			    residue < xs->datalen) {
477 				xs->error = XS_NOERROR;
478 				break;
479 			}
480 
481 			xs->error = XS_DRIVER_STUFFUP;
482 			break;
483 		}
484 		/* FALLTHROUGH */
485 	case STATUS_CMD_FAILED:
486 		/* fetch sense data */
487 		memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
488 		scbus->sc_sense_cmd.opcode = REQUEST_SENSE;
489 		scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
490 		    SCSI_CMD_LUN_SHIFT;
491 		scbus->sc_sense_cmd.length = sizeof(xs->sense);
492 
493 		cmdlen = sizeof(scbus->sc_sense_cmd);
494 		if (sc->sc_cmd == UMASS_CPROTO_UFI) /* XXX */
495 			cmdlen = UFI_COMMAND_LENGTH;
496 		sc->sc_methods->wire_xfer(sc, periph->periph_lun,
497 					  &scbus->sc_sense_cmd, cmdlen,
498 					  &xs->sense, sizeof(xs->sense),
499 					  DIR_IN, xs->timeout,
500 					  umass_scsipi_sense_cb, xs);
501 		return;
502 
503 	case STATUS_WIRE_FAILED:
504 		xs->error = XS_RESET;
505 		break;
506 
507 	default:
508 		panic("%s: Unknown status %d in umass_scsipi_cb\n",
509 			USBDEVNAME(sc->sc_dev), status);
510 	}
511 
512 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu: return xs->error="
513             "%d, xs->xs_status=0x%x xs->resid=%d\n",
514 	     tv.tv_sec, tv.tv_usec,
515 	     xs->error, xs->xs_status, xs->resid));
516 
517 	s = splbio();
518 	scsipi_done(xs);
519 	splx(s);
520 }
521 
522 /*
523  * Finalise a completed autosense operation
524  */
525 Static void
526 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
527 		      int status)
528 {
529 	struct scsipi_xfer *xs = priv;
530 	int s;
531 
532 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
533 		"status=%d\n", xs, residue, status));
534 
535 	switch (status) {
536 	case STATUS_CMD_OK:
537 	case STATUS_CMD_UNKNOWN:
538 		/* getting sense data succeeded */
539 		if (xs->cmd->opcode == INQUIRY && (xs->resid < xs->datalen ||
540 		    (sc->sc_quirks & UMASS_QUIRK_RS_NO_CLEAR_UA /* XXX */))) {
541 			/*
542 			 * Some drivers return SENSE errors even after INQUIRY.
543 			 * The upper layer doesn't like that.
544 			 */
545 			xs->error = XS_NOERROR;
546 			break;
547 		}
548 		/* XXX look at residue */
549 		if (residue == 0 || residue == 14)/* XXX */
550 			xs->error = XS_SENSE;
551 		else
552 			xs->error = XS_SHORTSENSE;
553 		break;
554 	default:
555 		DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
556 			USBDEVNAME(sc->sc_dev), status));
557 		xs->error = XS_DRIVER_STUFFUP;
558 		break;
559 	}
560 
561 	xs->xs_status |= XS_STS_DONE;
562 
563 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
564 		"xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
565 		xs->resid));
566 
567 	s = splbio();
568 	scsipi_done(xs);
569 	splx(s);
570 }
571 
572 #if NATAPIBUS > 0
573 Static void
574 umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
575 {
576 	struct scsipi_channel *chan = atapi->sc_channel;
577 	struct scsipi_periph *periph;
578 	struct scsipibus_attach_args sa;
579 	char vendor[33], product[65], revision[17];
580 	struct scsipi_inquiry_data inqbuf;
581 
582 	DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n",
583 			     atapi, target));
584 
585 	if (target != UMASS_ATAPI_DRIVE)	/* only probe drive 0 */
586 		return;
587 
588 	/* skip if already attached */
589 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
590 		return;
591 
592 	periph = scsipi_alloc_periph(M_NOWAIT);
593 	if (periph == NULL) {
594 		printf("%s: can't allocate link for drive %d\n",
595 		       atapi->sc_dev.dv_xname, target);
596 		return;
597 	}
598 
599 	DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
600 	periph->periph_channel = chan;
601 	periph->periph_switch = &atapi_probe_periphsw;
602 	periph->periph_target = target;
603 	periph->periph_quirks = chan->chan_defquirks;
604 
605 	DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n"));
606 	/* Now go ask the device all about itself. */
607 	memset(&inqbuf, 0, sizeof(inqbuf));
608 	if (scsipi_inquire(periph, &inqbuf,
609 	    XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK) != 0) {
610 		DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: "
611 		    "scsipi_inquire failed\n"));
612 		free(periph, M_DEVBUF);
613 		return;
614 	}
615 
616 	scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
617 	scsipi_strvis(product, 65, inqbuf.product, 16);
618 	scsipi_strvis(revision, 17, inqbuf.revision, 4);
619 
620 	sa.sa_periph = periph;
621 	sa.sa_inqbuf.type = inqbuf.device;
622 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
623 	    T_REMOV : T_FIXED;
624 	if (sa.sa_inqbuf.removable)
625 		periph->periph_flags |= PERIPH_REMOVABLE;
626 	sa.sa_inqbuf.vendor = vendor;
627 	sa.sa_inqbuf.product = product;
628 	sa.sa_inqbuf.revision = revision;
629 	sa.sa_inqptr = NULL;
630 
631 	DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing atapi_probedev on "
632 			      "'%s' '%s' '%s'\n", vendor, product, revision));
633 	atapi_probe_device(atapi, target, periph, &sa);
634 	/* atapi_probe_device() frees the periph when there is no device.*/
635 }
636 #endif
637