xref: /netbsd/sys/compat/linux/common/linux_cdrom.c (revision bf9ec67e)
1 /*	$NetBSD: linux_cdrom.c,v 1.13 2002/03/22 17:14:18 christos 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/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: linux_cdrom.c,v 1.13 2002/03/22 17:14:18 christos Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/ioctl.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/cdio.h>
47 #include <sys/dvdio.h>
48 
49 #include <sys/syscallargs.h>
50 
51 #include <compat/linux/common/linux_types.h>
52 #include <compat/linux/common/linux_ioctl.h>
53 #include <compat/linux/common/linux_signal.h>
54 #include <compat/linux/common/linux_util.h>
55 #include <compat/linux/common/linux_cdrom.h>
56 
57 #include <compat/linux/linux_syscallargs.h>
58 
59 static int bsd_to_linux_msf_lba(unsigned address_format, union msf_lba *bml,
60 				union linux_cdrom_addr *llml);
61 
62 #if DEBUG_LINUX
63 #define DPRINTF(x) uprintf x
64 #else
65 #define DPRINTF(x)
66 #endif
67 
68 /*
69  * XXX from dev/scsipi/cd.c
70  */
71 #define MAXTRACK 99
72 
73 static int
74 bsd_to_linux_msf_lba(unsigned address_format, union msf_lba *bml,
75 		      union linux_cdrom_addr *llml)
76 {
77 	switch (address_format) {
78 	case CD_LBA_FORMAT:
79 		llml->lba = bml->lba;
80 		break;
81 	case CD_MSF_FORMAT:
82 		llml->msf.minute = bml->msf.minute;
83 		llml->msf.second = bml->msf.second;
84 		llml->msf.frame = bml->msf.frame;
85 		break;
86 	default:
87 		return -1;
88 	}
89 	return 0;
90 }
91 
92 int
93 linux_ioctl_cdrom(p, uap, retval)
94 	struct proc *p;
95 	struct linux_sys_ioctl_args /* {
96 		syscallarg(int) fd;
97 		syscallarg(u_long) com;
98 		syscallarg(caddr_t) data;
99 	} */ *uap;
100 	register_t *retval;
101 {
102 	int error, idata;
103 	u_long com, ncom;
104 	caddr_t sg;
105 	struct file *fp;
106 	struct filedesc *fdp;
107 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
108 
109 	struct linux_cdrom_blk l_blk;
110 	struct linux_cdrom_msf l_msf;
111 	struct linux_cdrom_ti l_ti;
112 	struct linux_cdrom_tochdr l_tochdr;
113 	struct linux_cdrom_tocentry l_tocentry;
114 	struct linux_cdrom_subchnl l_subchnl;
115 	struct linux_cdrom_volctrl l_volctrl;
116 	struct linux_cdrom_multisession l_session;
117 
118 	struct ioc_play_blocks t_blocks;
119 	struct ioc_play_msf t_msf;
120 	struct ioc_play_track t_track;
121 	struct ioc_toc_header t_header;
122 	struct cd_toc_entry *entry, t_entry;
123 	struct ioc_read_toc_entry t_toc_entry;
124 	struct cd_sub_channel_info *info, t_info;
125 	struct ioc_read_subchannel t_subchannel;
126 	struct ioc_vol t_vol;
127 
128 	dvd_struct ds;
129 	dvd_authinfo dai;
130 
131 	fdp = p->p_fd;
132 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
133 		return (EBADF);
134 
135 	FILE_USE(fp);
136 
137 	com = SCARG(uap, com);
138 	ioctlf = fp->f_ops->fo_ioctl;
139 	retval[0] = error = 0;
140 
141 	switch(com) {
142 	case LINUX_CDROMPLAYMSF:
143 		error = copyin(SCARG(uap, data), &l_msf, sizeof l_msf);
144 		if (error)
145 			break;
146 
147 		t_msf.start_m = l_msf.cdmsf_min0;
148 		t_msf.start_s = l_msf.cdmsf_sec0;
149 		t_msf.start_f = l_msf.cdmsf_frame0;
150 		t_msf.end_m = l_msf.cdmsf_min1;
151 		t_msf.end_s = l_msf.cdmsf_sec1;
152 		t_msf.end_f = l_msf.cdmsf_frame1;
153 
154 		error = ioctlf(fp, CDIOCPLAYMSF, (caddr_t)&t_msf, p);
155 		break;
156 
157 	case LINUX_CDROMPLAYTRKIND:
158 		error = copyin(SCARG(uap, data), &l_ti, sizeof l_ti);
159 		if (error)
160 			break;
161 
162 		t_track.start_track = l_ti.cdti_trk0;
163 		t_track.start_index = l_ti.cdti_ind0;
164 		t_track.end_track = l_ti.cdti_trk1;
165 		t_track.end_index = l_ti.cdti_ind1;
166 
167 		error = ioctlf(fp, CDIOCPLAYTRACKS, (caddr_t)&t_track, p);
168 		break;
169 
170 	case LINUX_CDROMREADTOCHDR:
171 		error = ioctlf(fp, CDIOREADTOCHEADER, (caddr_t)&t_header, p);
172 		if (error)
173 			break;
174 
175 		l_tochdr.cdth_trk0 = t_header.starting_track;
176 		l_tochdr.cdth_trk1 = t_header.ending_track;
177 
178 		error = copyout(&l_tochdr, SCARG(uap, data), sizeof l_tochdr);
179 		break;
180 
181 	case LINUX_CDROMREADTOCENTRY:
182 		error = copyin(SCARG(uap, data), &l_tocentry,
183 			       sizeof l_tocentry);
184 		if (error)
185 			break;
186 
187 		sg = stackgap_init(p, 0);
188 		entry = stackgap_alloc(p, &sg, sizeof *entry);
189 		t_toc_entry.address_format = l_tocentry.cdte_format;
190 		t_toc_entry.starting_track = l_tocentry.cdte_track;
191 		t_toc_entry.data_len = sizeof *entry;
192 		t_toc_entry.data = entry;
193 
194 		error = ioctlf(fp, CDIOREADTOCENTRIES, (caddr_t)&t_toc_entry,
195 			       p);
196 		if (error)
197 			break;
198 
199 		error = copyin(entry, &t_entry, sizeof t_entry);
200 		if (error)
201 			break;
202 
203 		l_tocentry.cdte_adr = t_entry.addr_type;
204 		l_tocentry.cdte_ctrl = t_entry.control;
205 		if (bsd_to_linux_msf_lba(t_entry.addr_type, &t_entry.addr,
206 		    &l_tocentry.cdte_addr) < 0) {
207 			DPRINTF(("linux_ioctl: unknown format msf/lba\n"));
208 			error = EINVAL;
209 			break;
210 		}
211 
212 		error = copyout(&l_tocentry, SCARG(uap, data),
213 			       sizeof l_tocentry);
214 		break;
215 
216 	case LINUX_CDROMVOLCTRL:
217 		error = copyin(SCARG(uap, data), &l_volctrl, sizeof l_volctrl);
218 		if (error)
219 			break;
220 
221 		t_vol.vol[0] = l_volctrl.channel0;
222 		t_vol.vol[1] = l_volctrl.channel1;
223 		t_vol.vol[2] = l_volctrl.channel2;
224 		t_vol.vol[3] = l_volctrl.channel3;
225 
226 		error = ioctlf(fp, CDIOCSETVOL, (caddr_t)&t_vol, p);
227 		break;
228 
229 	case LINUX_CDROMVOLREAD:
230 		error = ioctlf(fp, CDIOCGETVOL, (caddr_t)&t_vol, p);
231 		if (error)
232 			break;
233 
234 		l_volctrl.channel0 = t_vol.vol[0];
235 		l_volctrl.channel1 = t_vol.vol[1];
236 		l_volctrl.channel2 = t_vol.vol[2];
237 		l_volctrl.channel3 = t_vol.vol[3];
238 
239 		error = copyout(&l_volctrl, SCARG(uap, data), sizeof l_volctrl);
240 		break;
241 
242 	case LINUX_CDROMSUBCHNL:
243 		error = copyin(SCARG(uap, data), &l_subchnl, sizeof l_subchnl);
244 		if (error)
245 			break;
246 
247 		sg = stackgap_init(p, 0);
248 		info = stackgap_alloc(p, &sg, sizeof *info);
249 		t_subchannel.address_format = CD_MSF_FORMAT;
250 		t_subchannel.track = 0;
251 		t_subchannel.data_format = l_subchnl.cdsc_format;
252 		t_subchannel.data_len = sizeof *info;
253 		t_subchannel.data = info;
254 		DPRINTF(("linux_ioctl: CDROMSUBCHNL %d %d\n",
255 			 l_subchnl.cdsc_format, l_subchnl.cdsc_trk));
256 
257 		error = ioctlf(fp, CDIOCREADSUBCHANNEL, (caddr_t)&t_subchannel,
258 			       p);
259 		if (error)
260 			break;
261 
262 		error = copyin(info, &t_info, sizeof t_info);
263 		if (error)
264 			break;
265 
266 		l_subchnl.cdsc_audiostatus = t_info.header.audio_status;
267 		l_subchnl.cdsc_adr = t_info.what.position.addr_type;
268 		l_subchnl.cdsc_ctrl = t_info.what.position.control;
269 		l_subchnl.cdsc_ind = t_info.what.position.index_number;
270 
271 		DPRINTF(("linux_ioctl: CDIOCREADSUBCHANNEL %d %d %d\n",
272 			t_info.header.audio_status,
273 			t_info.header.data_len[0],
274 			t_info.header.data_len[1]));
275 		DPRINTF(("(more) %d %d %d %d %d\n",
276 			t_info.what.position.data_format,
277 			t_info.what.position.control,
278 			t_info.what.position.addr_type,
279 			t_info.what.position.track_number,
280 			t_info.what.position.index_number));
281 
282 		if (bsd_to_linux_msf_lba(t_subchannel.address_format,
283 					 &t_info.what.position.absaddr,
284 					 &l_subchnl.cdsc_absaddr) < 0 ||
285 		    bsd_to_linux_msf_lba(t_subchannel.address_format,
286 					 &t_info.what.position.reladdr,
287 					 &l_subchnl.cdsc_reladdr) < 0) {
288 			DPRINTF(("linux_ioctl: unknown format msf/lba\n"));
289 			error = EINVAL;
290 			break;
291 		}
292 
293 		error = copyout(&l_subchnl, SCARG(uap, data), sizeof l_subchnl);
294 		break;
295 
296 	case LINUX_CDROMPLAYBLK:
297 		error = copyin(SCARG(uap, data), &l_blk, sizeof l_blk);
298 		if (error)
299 			break;
300 
301 		t_blocks.blk = l_blk.from;
302 		t_blocks.len = l_blk.len;
303 
304 		error = ioctlf(fp, CDIOCPLAYBLOCKS, (caddr_t)&t_blocks, p);
305 		break;
306 
307 	case LINUX_CDROMEJECT_SW:
308 		error = copyin(SCARG(uap, data), &idata, sizeof idata);
309 		if (error)
310 			break;
311 
312 		if (idata == 1)
313 			ncom = CDIOCALLOW;
314 		else
315 			ncom = CDIOCPREVENT;
316 		error = ioctlf(fp, ncom, NULL, p);
317 		break;
318 
319 	case LINUX_CDROMPAUSE:
320 		error = ioctlf(fp, CDIOCPAUSE, NULL, p);
321 		break;
322 
323 	case LINUX_CDROMRESUME:
324 		error = ioctlf(fp, CDIOCRESUME, NULL, p);
325 		break;
326 
327 	case LINUX_CDROMSTOP:
328 		error = ioctlf(fp, CDIOCSTOP, NULL, p);
329 		break;
330 
331 	case LINUX_CDROMSTART:
332 		error = ioctlf(fp, CDIOCSTART, NULL, p);
333 		break;
334 
335 	case LINUX_CDROMEJECT:
336 		error = ioctlf(fp, CDIOCEJECT, NULL, p);
337 		break;
338 
339 	case LINUX_CDROMRESET:
340 		error = ioctlf(fp, CDIOCRESET, NULL, p);
341 		break;
342 
343 	case LINUX_CDROMMULTISESSION:
344 		error = copyin(SCARG(uap, data), &l_session, sizeof l_session);
345 		if (error)
346 			break;
347 
348 		error = ioctlf(fp, CDIOREADTOCHEADER, (caddr_t)&t_header, p);
349 		if (error)
350 			break;
351 
352 		sg = stackgap_init(p, 0);
353 		entry = stackgap_alloc(p, &sg, sizeof *entry);
354 		t_toc_entry.address_format = l_session.addr_format;
355 		t_toc_entry.starting_track = 0;
356 		t_toc_entry.data_len = sizeof *entry;
357 		t_toc_entry.data = entry;
358 
359 		error = ioctlf(fp, CDIOREADTOCENTRIES,
360 		    (caddr_t)&t_toc_entry, p);
361 		if (error)
362 			break;
363 
364 		error = copyin(entry, &t_entry, sizeof t_entry);
365 		if (error)
366 			break;
367 
368 		if (bsd_to_linux_msf_lba(l_session.addr_format,
369 		    &t_entry.addr, &l_session.addr) < 0) {
370 			error = EINVAL;
371 			break;
372 		}
373 
374 		l_session.xa_flag =
375 		    t_header.starting_track != t_header.ending_track;
376 
377 		error = copyout(&l_session, SCARG(uap, data), sizeof l_session);
378 		break;
379 
380 	case LINUX_CDROMCLOSETRAY:
381 		error = ioctlf(fp, CDIOCCLOSE, NULL, p);
382 		break;
383 
384 	case LINUX_CDROM_LOCKDOOR:
385 		ncom = SCARG(uap, data) != 0 ? CDIOCPREVENT : CDIOCALLOW;
386 		error = ioctlf(fp, ncom, NULL, p);
387 		break;
388 
389 	case LINUX_CDROM_SET_OPTIONS:
390 	case LINUX_CDROM_CLEAR_OPTIONS:
391 		/* whatever you say */
392 		break;
393 
394 	case LINUX_CDROM_DEBUG:
395 		ncom = SCARG(uap, data) != 0 ? CDIOCSETDEBUG : CDIOCCLRDEBUG;
396 		error = ioctlf(fp, ncom, NULL, p);
397 		break;
398 
399 	case LINUX_CDROM_SELECT_SPEED:
400 	case LINUX_CDROM_SELECT_DISC:
401 	case LINUX_CDROM_MEDIA_CHANGED:
402 	case LINUX_CDROM_DRIVE_STATUS:
403 	case LINUX_CDROM_DISC_STATUS:
404 	case LINUX_CDROM_CHANGER_NSLOTS:
405 	case LINUX_CDROM_GET_CAPABILITY:
406 		error = ENOSYS;
407 		break;
408 
409 	case LINUX_DVD_READ_STRUCT:
410 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
411 		if (error)
412 			break;
413 		error = ioctlf(fp, DVD_READ_STRUCT, (caddr_t)&ds, p);
414 		if (error)
415 			break;
416 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
417 		break;
418 
419 	case LINUX_DVD_WRITE_STRUCT:
420 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
421 		if (error)
422 			break;
423 		error = ioctlf(fp, DVD_WRITE_STRUCT, (caddr_t)&ds, p);
424 		if (error)
425 			break;
426 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
427 		break;
428 
429 	case LINUX_DVD_AUTH:
430 		error = copyin(SCARG(uap, data), &dai, sizeof dai);
431 		if (error)
432 			break;
433 		error = ioctlf(fp, DVD_AUTH, (caddr_t)&dai, p);
434 		if (error)
435 			break;
436 		error = copyout(&dai, SCARG(uap, data), sizeof dai);
437 		break;
438 
439 
440 	default:
441 		DPRINTF(("linux_ioctl: unimplemented ioctl %08lx\n", com));
442 		error = EINVAL;
443 	}
444 
445 	FILE_UNUSE(fp, p);
446 	return error;
447 }
448