Home | History | Annotate | Line # | Download | only in common
linux_cdrom.c revision 1.4.12.1
      1 /*	$NetBSD: linux_cdrom.c,v 1.4.12.1 2000/11/20 18:08:21 bouyer Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by the NetBSD
     18  *        Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/file.h>
     40 #include <sys/filedesc.h>
     41 #include <sys/mount.h>
     42 #include <sys/proc.h>
     43 #include <sys/cdio.h>
     44 #include <sys/dvdio.h>
     45 
     46 #include <sys/syscallargs.h>
     47 
     48 #include <compat/linux/common/linux_types.h>
     49 #include <compat/linux/common/linux_ioctl.h>
     50 #include <compat/linux/common/linux_signal.h>
     51 #include <compat/linux/common/linux_util.h>
     52 #include <compat/linux/common/linux_cdrom.h>
     53 
     54 #include <compat/linux/linux_syscallargs.h>
     55 
     56 #if 0
     57 #define DPRINTF(x) printf x
     58 #else
     59 #define DPRINTF(x)
     60 #endif
     61 
     62 int
     63 linux_ioctl_cdrom(p, uap, retval)
     64 	struct proc *p;
     65 	struct linux_sys_ioctl_args /* {
     66 		syscallarg(int) fd;
     67 		syscallarg(u_long) com;
     68 		syscallarg(caddr_t) data;
     69 	} */ *uap;
     70 	register_t *retval;
     71 {
     72 	int error, idata;
     73 	u_long com, ncom;
     74 	caddr_t sg;
     75 	struct file *fp;
     76 	struct filedesc *fdp;
     77 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
     78 
     79 	struct linux_cdrom_blk l_blk;
     80 	struct linux_cdrom_msf l_msf;
     81 	struct linux_cdrom_ti l_ti;
     82 	struct linux_cdrom_tochdr l_tochdr;
     83 	struct linux_cdrom_tocentry l_tocentry;
     84 	struct linux_cdrom_subchnl l_subchnl;
     85 	struct linux_cdrom_volctrl l_volctrl;
     86 
     87 	struct ioc_play_blocks t_blocks;
     88 	struct ioc_play_msf t_msf;
     89 	struct ioc_play_track t_track;
     90 	struct ioc_toc_header t_header;
     91 	struct cd_toc_entry *entry, t_entry;
     92 	struct ioc_read_toc_entry t_toc_entry;
     93 	struct cd_sub_channel_info *info, t_info;
     94 	struct ioc_read_subchannel t_subchannel;
     95 	struct ioc_vol t_vol;
     96 
     97 	dvd_struct ds;
     98 	dvd_authinfo dai;
     99 
    100 	fdp = p->p_fd;
    101 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
    102 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    103 		return (EBADF);
    104 
    105 	com = SCARG(uap, com);
    106 	ioctlf = fp->f_ops->fo_ioctl;
    107 	retval[0] = 0;
    108 
    109 	switch(com) {
    110 	case LINUX_CDROMPLAYMSF:
    111 		error = copyin(SCARG(uap, data), &l_msf, sizeof l_msf);
    112 		if (error)
    113 			return error;
    114 
    115 		t_msf.start_m = l_msf.cdmsf_min0;
    116 		t_msf.start_s = l_msf.cdmsf_sec0;
    117 		t_msf.start_f = l_msf.cdmsf_frame0;
    118 		t_msf.end_m = l_msf.cdmsf_min1;
    119 		t_msf.end_s = l_msf.cdmsf_sec1;
    120 		t_msf.end_f = l_msf.cdmsf_frame1;
    121 
    122 		return ioctlf(fp, CDIOCPLAYMSF, (caddr_t)&t_msf, p);
    123 
    124 	case LINUX_CDROMPLAYTRKIND:
    125 		error = copyin(SCARG(uap, data), &l_ti, sizeof l_ti);
    126 		if (error)
    127 			return error;
    128 
    129 		t_track.start_track = l_ti.cdti_trk0;
    130 		t_track.start_index = l_ti.cdti_ind0;
    131 		t_track.end_track = l_ti.cdti_trk1;
    132 		t_track.end_index = l_ti.cdti_ind1;
    133 
    134 		return ioctlf(fp, CDIOCPLAYTRACKS, (caddr_t)&t_track, p);
    135 
    136 	case LINUX_CDROMREADTOCHDR:
    137 		error = ioctlf(fp, CDIOREADTOCHEADER, (caddr_t)&t_header, p);
    138 		if (error)
    139 			return error;
    140 
    141 		l_tochdr.cdth_trk0 = t_header.starting_track;
    142 		l_tochdr.cdth_trk1 = t_header.ending_track;
    143 
    144 		return copyout(&l_tochdr, SCARG(uap, data), sizeof l_tochdr);
    145 
    146 	case LINUX_CDROMREADTOCENTRY:
    147 		error = copyin(SCARG(uap, data), &l_tocentry,
    148 			       sizeof l_tocentry);
    149 		if (error)
    150 			return error;
    151 
    152 		sg = stackgap_init(p->p_emul);
    153 		entry = stackgap_alloc(&sg, sizeof *entry);
    154 		t_toc_entry.address_format = l_tocentry.cdte_format;
    155 		t_toc_entry.starting_track = l_tocentry.cdte_track;
    156 		t_toc_entry.data_len = sizeof *entry;
    157 		t_toc_entry.data = entry;
    158 
    159 		error = ioctlf(fp, CDIOREADTOCENTRYS, (caddr_t)&t_toc_entry,
    160 			       p);
    161 		if (error)
    162 			return error;
    163 
    164 		error = copyin(entry, &t_entry, sizeof t_entry);
    165 		if (error)
    166 			return error;
    167 
    168 		l_tocentry.cdte_adr = t_entry.addr_type;
    169 		l_tocentry.cdte_ctrl = t_entry.control;
    170 		switch (t_toc_entry.address_format) {
    171 		case CD_LBA_FORMAT:
    172 			l_tocentry.cdte_addr.lba = t_entry.addr.lba;
    173 			break;
    174 		case CD_MSF_FORMAT:
    175 			l_tocentry.cdte_addr.msf.minute =
    176 			    t_entry.addr.msf.minute;
    177 			l_tocentry.cdte_addr.msf.second =
    178 			    t_entry.addr.msf.second;
    179 			l_tocentry.cdte_addr.msf.frame =
    180 			    t_entry.addr.msf.frame;
    181 			break;
    182 		default:
    183 			printf("linux_ioctl: unknown format msf/lba\n");
    184 			return EINVAL;
    185 		}
    186 
    187 		return copyout(&l_tocentry, SCARG(uap, data),
    188 			       sizeof l_tocentry);
    189 
    190 	case LINUX_CDROMVOLCTRL:
    191 		error = copyin(SCARG(uap, data), &l_volctrl, sizeof l_volctrl);
    192 		if (error)
    193 			return error;
    194 
    195 		t_vol.vol[0] = l_volctrl.channel0;
    196 		t_vol.vol[1] = l_volctrl.channel1;
    197 		t_vol.vol[2] = l_volctrl.channel2;
    198 		t_vol.vol[3] = l_volctrl.channel3;
    199 
    200 		return ioctlf(fp, CDIOCSETVOL, (caddr_t)&t_vol, p);
    201 
    202 	case LINUX_CDROMVOLREAD:
    203 		error = ioctlf(fp, CDIOCGETVOL, (caddr_t)&t_vol, p);
    204 		if (error)
    205 			return error;
    206 
    207 		l_volctrl.channel0 = t_vol.vol[0];
    208 		l_volctrl.channel1 = t_vol.vol[1];
    209 		l_volctrl.channel2 = t_vol.vol[2];
    210 		l_volctrl.channel3 = t_vol.vol[3];
    211 
    212 		return copyout(&l_volctrl, SCARG(uap, data), sizeof l_volctrl);
    213 
    214 	case LINUX_CDROMSUBCHNL:
    215 		error = copyin(SCARG(uap, data), &l_subchnl, sizeof l_subchnl);
    216 		if (error)
    217 			return error;
    218 
    219 		sg = stackgap_init(p->p_emul);
    220 		info = stackgap_alloc(&sg, sizeof *info);
    221 		t_subchannel.address_format = l_subchnl.cdsc_format;
    222 		t_subchannel.data_format = CD_CURRENT_POSITION;
    223 		t_subchannel.track = l_subchnl.cdsc_trk;
    224 		t_subchannel.data_len = sizeof *info;
    225 		t_subchannel.data = info;
    226 		DPRINTF(("linux_ioctl: CDROMSUBCHNL %d %d\n",
    227 			 l_subchnl.cdsc_format, l_subchnl.cdsc_trk));
    228 
    229 		error = ioctlf(fp, CDIOCREADSUBCHANNEL, (caddr_t)&t_subchannel,
    230 			       p);
    231 		if (error)
    232 		    return error;
    233 
    234 		error = copyin(info, &t_info, sizeof t_info);
    235 		if (error)
    236 			return error;
    237 
    238 	    l_subchnl.cdsc_audiostatus = t_info.header.audio_status;
    239 	    l_subchnl.cdsc_adr = t_info.what.position.addr_type;
    240 	    l_subchnl.cdsc_ctrl = t_info.what.position.control;
    241 	    l_subchnl.cdsc_ind = t_info.what.position.index_number;
    242 
    243 	    DPRINTF(("linux_ioctl: CDIOCREADSUBCHANNEL %d %d %d\n",
    244 		     t_info.header.audio_status,
    245 		     t_info.header.data_len[0],
    246 		     t_info.header.data_len[1]));
    247 	    DPRINTF(("(more) %d %d %d %d %d\n",
    248 		     t_info.what.position.data_format,
    249 		     t_info.what.position.control,
    250 		     t_info.what.position.addr_type,
    251 		     t_info.what.position.track_number,
    252 		     t_info.what.position.index_number));
    253 
    254 	    switch (t_subchannel.address_format) {
    255 	    case CD_LBA_FORMAT:
    256 		    l_subchnl.cdsc_absaddr.lba =
    257 			t_info.what.position.absaddr.lba;
    258 		    l_subchnl.cdsc_reladdr.lba =
    259 			t_info.what.position.reladdr.lba;
    260 		    DPRINTF(("LBA: %d %d\n",
    261 			     t_info.what.position.absaddr.lba,
    262 			     t_info.what.position.reladdr.lba));
    263 		    break;
    264 
    265 	    case CD_MSF_FORMAT:
    266 		    l_subchnl.cdsc_absaddr.msf.minute =
    267 			t_info.what.position.absaddr.msf.minute;
    268 		    l_subchnl.cdsc_absaddr.msf.second =
    269 			t_info.what.position.absaddr.msf.second;
    270 		    l_subchnl.cdsc_absaddr.msf.frame =
    271 			t_info.what.position.absaddr.msf.frame;
    272 
    273 		    l_subchnl.cdsc_reladdr.msf.minute =
    274 			t_info.what.position.reladdr.msf.minute;
    275 		    l_subchnl.cdsc_reladdr.msf.second =
    276 			t_info.what.position.reladdr.msf.second;
    277 		    l_subchnl.cdsc_reladdr.msf.frame =
    278 			t_info.what.position.reladdr.msf.frame;
    279 		    DPRINTF(("MSF: %d %d %d %d\n",
    280 			     t_info.what.position.absaddr.msf.minute,
    281 			     t_info.what.position.absaddr.msf.second,
    282 			     t_info.what.position.reladdr.msf.minute,
    283 			     t_info.what.position.reladdr.msf.second));
    284 		    break;
    285 
    286 	    default:
    287 		    DPRINTF(("linux_ioctl: unknown format msf/lba\n"));
    288 		    return EINVAL;
    289 	    }
    290 
    291 	    return copyout(&l_subchnl, SCARG(uap, data), sizeof l_subchnl);
    292 
    293 	case LINUX_CDROMPLAYBLK:
    294 		error = copyin(SCARG(uap, data), &l_blk, sizeof l_blk);
    295 		if (error)
    296 			return error;
    297 
    298 		t_blocks.blk = l_blk.from;
    299 		t_blocks.len = l_blk.len;
    300 
    301 		return ioctlf(fp, CDIOCPLAYBLOCKS, (caddr_t)&t_blocks, p);
    302 
    303 	case LINUX_CDROMEJECT_SW:
    304 		error = copyin(SCARG(uap, data), &idata, sizeof idata);
    305 		if (error)
    306 			return error;
    307 
    308 		if (idata == 1)
    309 			ncom = CDIOCALLOW;
    310 		else
    311 			ncom = CDIOCPREVENT;
    312 		break;
    313 
    314 	case LINUX_CDROMPAUSE:
    315 		ncom = CDIOCPAUSE;
    316 		break;
    317 
    318 	case LINUX_CDROMRESUME:
    319 		ncom = CDIOCRESUME;
    320 		break;
    321 
    322 	case LINUX_CDROMSTOP:
    323 		ncom = CDIOCSTOP;
    324 		break;
    325 
    326 	case LINUX_CDROMSTART:
    327 		ncom = CDIOCSTART;
    328 		break;
    329 
    330 	case LINUX_CDROMEJECT:
    331 		ncom = CDIOCEJECT;
    332 		break;
    333 
    334 	case LINUX_CDROMRESET:
    335 		ncom = CDIOCRESET;
    336 		break;
    337 
    338 	case LINUX_DVD_READ_STRUCT:
    339 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
    340 		if (error)
    341 			return (error);
    342 		error = ioctlf(fp, DVD_READ_STRUCT, (caddr_t)&ds, p);
    343 		if (error)
    344 			return (error);
    345 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
    346 		if (error)
    347 			return (error);
    348 		return (0);
    349 
    350 	case LINUX_DVD_WRITE_STRUCT:
    351 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
    352 		if (error)
    353 			return (error);
    354 		error = ioctlf(fp, DVD_WRITE_STRUCT, (caddr_t)&ds, p);
    355 		if (error)
    356 			return (error);
    357 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
    358 		if (error)
    359 			return (error);
    360 		return (0);
    361 
    362 	case LINUX_DVD_AUTH:
    363 		error = copyin(SCARG(uap, data), &dai, sizeof dai);
    364 		if (error)
    365 			return (error);
    366 		error = ioctlf(fp, DVD_AUTH, (caddr_t)&dai, p);
    367 		if (error)
    368 			return (error);
    369 		error = copyout(&dai, SCARG(uap, data), sizeof dai);
    370 		if (error)
    371 			return (error);
    372 		return (0);
    373 
    374 
    375 	default:
    376 		DPRINTF(("linux_ioctl: unimplemented ioctl %08lx\n", com));
    377 		return EINVAL;
    378 	}
    379 
    380 	return ioctlf(fp, ncom, NULL, p);
    381 }
    382