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