Home | History | Annotate | Line # | Download | only in usb
umass_scsipi.c revision 1.12
      1 /*	$NetBSD: umass_scsipi.c,v 1.12 2003/09/10 05:25:44 mycroft 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 (at) 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.12 2003/09/10 05:25:44 mycroft 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 		dir = DIR_NONE;
    292 		if (xs->datalen) {
    293 			switch (xs->xs_control &
    294 			    (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    295 			case XS_CTL_DATA_IN:
    296 				dir = DIR_IN;
    297 				break;
    298 			case XS_CTL_DATA_OUT:
    299 				dir = DIR_OUT;
    300 				break;
    301 			}
    302 		}
    303 
    304 		if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
    305 			printf("umass_cmd: large datalen, %d\n", xs->datalen);
    306 			xs->error = XS_DRIVER_STUFFUP;
    307 			goto done;
    308 		}
    309 
    310 		if (xs->xs_control & XS_CTL_POLL) {
    311 			/* Use sync transfer. XXX Broken! */
    312 			DPRINTF(UDMASS_SCSI,
    313 			    ("umass_scsi_cmd: sync dir=%d\n", dir));
    314 			sc->sc_xfer_flags = USBD_SYNCHRONOUS;
    315 			scbus->sc_sync_status = USBD_INVAL;
    316 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
    317 						  cmdlen, xs->data,
    318 						  xs->datalen, dir,
    319 						  xs->timeout, 0, xs);
    320 			sc->sc_xfer_flags = 0;
    321 			DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
    322 					      scbus->sc_sync_status));
    323 			switch (scbus->sc_sync_status) {
    324 			case USBD_NORMAL_COMPLETION:
    325 				xs->error = XS_NOERROR;
    326 				break;
    327 			case USBD_TIMEOUT:
    328 				xs->error = XS_TIMEOUT;
    329 				break;
    330 			default:
    331 				xs->error = XS_DRIVER_STUFFUP;
    332 				break;
    333 			}
    334 			goto done;
    335 		} else {
    336 			DPRINTF(UDMASS_SCSI,
    337 			    ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
    338 				      " datalen=%d\n",
    339 				      dir, cmdlen, xs->datalen));
    340 			sc->sc_methods->wire_xfer(sc, periph->periph_lun, cmd,
    341 						  cmdlen, xs->data,
    342 						  xs->datalen, dir,
    343 						  xs->timeout,
    344 						  umass_scsipi_cb, xs);
    345 			return;
    346 		}
    347 
    348 		/* Return if command finishes early. */
    349  done:
    350 		scsipi_done(xs);
    351 		return;
    352 	default:
    353 		/* Not supported, nothing to do. */
    354 		;
    355 	}
    356 }
    357 
    358 Static void
    359 umass_scsipi_minphys(struct buf *bp)
    360 {
    361 #ifdef DIAGNOSTIC
    362 	if (bp->b_bcount <= 0) {
    363 		printf("umass_scsipi_minphys count(%ld) <= 0\n",
    364 		       bp->b_bcount);
    365 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
    366 	}
    367 #endif
    368 	if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
    369 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
    370 	minphys(bp);
    371 }
    372 
    373 int
    374 umass_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, caddr_t arg,
    375 		   int flag, usb_proc_ptr p)
    376 {
    377 	/*struct umass_softc *sc = link->adapter_softc;*/
    378 	/*struct umass_scsipi_softc *scbus = sc->bus;*/
    379 
    380 	switch (cmd) {
    381 #if 0
    382 	case SCBUSIORESET:
    383 		ccb->ccb_h.status = CAM_REQ_INPROG;
    384 		umass_reset(sc, umass_cam_cb, (void *) ccb);
    385 		return (0);
    386 #endif
    387 	default:
    388 		return (ENOTTY);
    389 	}
    390 }
    391 
    392 Static int
    393 umass_scsipi_getgeom(struct scsipi_periph *periph, struct disk_parms *dp,
    394 		     u_long sectors)
    395 {
    396 	struct umass_softc *sc =
    397 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
    398 
    399 	/* If it's not a floppy, we don't know what to do. */
    400 	if (sc->sc_cmd != UMASS_CPROTO_UFI)
    401 		return (0);
    402 
    403 	switch (sectors) {
    404 	case 1440:
    405 		/* Most likely a single density 3.5" floppy. */
    406 		dp->heads = 2;
    407 		dp->sectors = 9;
    408 		dp->cyls = 80;
    409 		return (1);
    410 	case 2880:
    411 		/* Most likely a double density 3.5" floppy. */
    412 		dp->heads = 2;
    413 		dp->sectors = 18;
    414 		dp->cyls = 80;
    415 		return (1);
    416 	default:
    417 		return (0);
    418 	}
    419 }
    420 
    421 Static void
    422 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
    423 {
    424 	struct umass_scsipi_softc *scbus = (struct umass_scsipi_softc *)sc->bus;
    425 	struct scsipi_xfer *xs = priv;
    426 	struct scsipi_periph *periph = xs->xs_periph;
    427 	int cmdlen;
    428 	int s;
    429 #ifdef UMASS_DEBUG
    430 	struct timeval tv;
    431 	u_int delta;
    432 	microtime(&tv);
    433 	delta = (tv.tv_sec - sc->tv.tv_sec) * 1000000 + tv.tv_usec - sc->tv.tv_usec;
    434 #endif
    435 
    436 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu, delta=%u: xs=%p residue=%d"
    437 	    " status=%d\n", tv.tv_sec, tv.tv_usec, delta, xs, residue, status));
    438 
    439 	xs->resid = residue;
    440 
    441 	switch (status) {
    442 	case STATUS_CMD_OK:
    443 		xs->error = XS_NOERROR;
    444 		break;
    445 
    446 	case STATUS_CMD_UNKNOWN:
    447 		/* we can't issue REQUEST SENSE */
    448 		if (xs->xs_periph->periph_quirks & PQUIRK_NOSENSE) {
    449 			/*
    450 			 * If no residue and no other USB error,
    451 			 * command succeeded.
    452 			 */
    453 			if (residue == 0) {
    454 				xs->error = XS_NOERROR;
    455 				break;
    456 			}
    457 
    458 			/*
    459 			 * Some devices return a short INQUIRY
    460 			 * response, omitting response data from the
    461 			 * "vendor specific data" on...
    462 			 */
    463 			if (xs->cmd->opcode == INQUIRY &&
    464 			    residue < xs->datalen) {
    465 				xs->error = XS_NOERROR;
    466 				break;
    467 			}
    468 
    469 			xs->error = XS_DRIVER_STUFFUP;
    470 			break;
    471 		}
    472 		/* FALLTHROUGH */
    473 	case STATUS_CMD_FAILED:
    474 		/* fetch sense data */
    475 		sc->sc_sense = 1;
    476 		memset(&scbus->sc_sense_cmd, 0, sizeof(scbus->sc_sense_cmd));
    477 		scbus->sc_sense_cmd.opcode = REQUEST_SENSE;
    478 		scbus->sc_sense_cmd.byte2 = periph->periph_lun <<
    479 		    SCSI_CMD_LUN_SHIFT;
    480 		scbus->sc_sense_cmd.length = sizeof(xs->sense);
    481 
    482 		cmdlen = sizeof(scbus->sc_sense_cmd);
    483 		if (sc->sc_cmd == UMASS_CPROTO_UFI) /* XXX */
    484 			cmdlen = UFI_COMMAND_LENGTH;
    485 		sc->sc_methods->wire_xfer(sc, periph->periph_lun,
    486 					  &scbus->sc_sense_cmd, cmdlen,
    487 					  &xs->sense, sizeof(xs->sense),
    488 					  DIR_IN, xs->timeout,
    489 					  umass_scsipi_sense_cb, xs);
    490 		return;
    491 
    492 	case STATUS_WIRE_FAILED:
    493 		xs->error = XS_RESET;
    494 		break;
    495 
    496 	default:
    497 		panic("%s: Unknown status %d in umass_scsipi_cb",
    498 			USBDEVNAME(sc->sc_dev), status);
    499 	}
    500 
    501 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: at %lu.%06lu: return xs->error="
    502             "%d, xs->xs_status=0x%x xs->resid=%d\n",
    503 	     tv.tv_sec, tv.tv_usec,
    504 	     xs->error, xs->xs_status, xs->resid));
    505 
    506 	s = splbio();
    507 	scsipi_done(xs);
    508 	splx(s);
    509 }
    510 
    511 /*
    512  * Finalise a completed autosense operation
    513  */
    514 Static void
    515 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
    516 		      int status)
    517 {
    518 	struct scsipi_xfer *xs = priv;
    519 	int s;
    520 
    521 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
    522 		"status=%d\n", xs, residue, status));
    523 
    524 	sc->sc_sense = 0;
    525 	switch (status) {
    526 	case STATUS_CMD_OK:
    527 	case STATUS_CMD_UNKNOWN:
    528 		/* getting sense data succeeded */
    529 		if (residue == 0 || residue == 14)/* XXX */
    530 			xs->error = XS_SENSE;
    531 		else
    532 			xs->error = XS_SHORTSENSE;
    533 		break;
    534 	default:
    535 		DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
    536 			USBDEVNAME(sc->sc_dev), status));
    537 		xs->error = XS_DRIVER_STUFFUP;
    538 		break;
    539 	}
    540 
    541 	xs->xs_status |= XS_STS_DONE;
    542 
    543 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
    544 		"xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
    545 		xs->resid));
    546 
    547 	s = splbio();
    548 	scsipi_done(xs);
    549 	splx(s);
    550 }
    551 
    552 #if NATAPIBUS > 0
    553 Static void
    554 umass_atapi_probe_device(struct atapibus_softc *atapi, int target)
    555 {
    556 	struct scsipi_channel *chan = atapi->sc_channel;
    557 	struct scsipi_periph *periph;
    558 	struct scsipibus_attach_args sa;
    559 	char vendor[33], product[65], revision[17];
    560 	struct scsipi_inquiry_data inqbuf;
    561 
    562 	DPRINTF(UDMASS_SCSI,("umass_atapi_probe_device: atapi=%p target=%d\n",
    563 			     atapi, target));
    564 
    565 	if (target != UMASS_ATAPI_DRIVE)	/* only probe drive 0 */
    566 		return;
    567 
    568 	/* skip if already attached */
    569 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
    570 		return;
    571 
    572 	periph = scsipi_alloc_periph(M_NOWAIT);
    573 	if (periph == NULL) {
    574 		printf("%s: can't allocate link for drive %d\n",
    575 		       atapi->sc_dev.dv_xname, target);
    576 		return;
    577 	}
    578 
    579 	DIF(UDMASS_UPPER, periph->periph_dbflags |= 1); /* XXX 1 */
    580 	periph->periph_channel = chan;
    581 	periph->periph_switch = &atapi_probe_periphsw;
    582 	periph->periph_target = target;
    583 	periph->periph_quirks = chan->chan_defquirks;
    584 
    585 	DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: doing inquiry\n"));
    586 	/* Now go ask the device all about itself. */
    587 	memset(&inqbuf, 0, sizeof(inqbuf));
    588 	if (scsipi_inquire(periph, &inqbuf,
    589 	    XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK) != 0) {
    590 		DPRINTF(UDMASS_SCSI, ("umass_atapi_probe_device: "
    591 		    "scsipi_inquire failed\n"));
    592 		free(periph, M_DEVBUF);
    593 		return;
    594 	}
    595 
    596 	scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
    597 	scsipi_strvis(product, 65, inqbuf.product, 16);
    598 	scsipi_strvis(revision, 17, inqbuf.revision, 4);
    599 
    600 	sa.sa_periph = periph;
    601 	sa.sa_inqbuf.type = inqbuf.device;
    602 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
    603 	    T_REMOV : T_FIXED;
    604 	if (sa.sa_inqbuf.removable)
    605 		periph->periph_flags |= PERIPH_REMOVABLE;
    606 	sa.sa_inqbuf.vendor = vendor;
    607 	sa.sa_inqbuf.product = product;
    608 	sa.sa_inqbuf.revision = revision;
    609 	sa.sa_inqptr = NULL;
    610 
    611 	DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing atapi_probedev on "
    612 			      "'%s' '%s' '%s'\n", vendor, product, revision));
    613 	atapi_probe_device(atapi, target, periph, &sa);
    614 	/* atapi_probe_device() frees the periph when there is no device.*/
    615 }
    616 #endif
    617