xref: /netbsd-src/sys/compat/linux/common/linux_cdrom.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: linux_cdrom.c,v 1.15 2003/03/21 21:13:53 dsl 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.15 2003/03/21 21:13:53 dsl 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(p, uap, retval)
95 	struct proc *p;
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 proc *);
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 
129 	dvd_struct ds;
130 	dvd_authinfo dai;
131 
132 	fdp = p->p_fd;
133 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
134 		return (EBADF);
135 
136 	FILE_USE(fp);
137 
138 	com = SCARG(uap, com);
139 	ioctlf = fp->f_ops->fo_ioctl;
140 	retval[0] = error = 0;
141 
142 	switch(com) {
143 	case LINUX_CDROMPLAYMSF:
144 		error = copyin(SCARG(uap, data), &l_msf, sizeof l_msf);
145 		if (error)
146 			break;
147 
148 		t_msf.start_m = l_msf.cdmsf_min0;
149 		t_msf.start_s = l_msf.cdmsf_sec0;
150 		t_msf.start_f = l_msf.cdmsf_frame0;
151 		t_msf.end_m = l_msf.cdmsf_min1;
152 		t_msf.end_s = l_msf.cdmsf_sec1;
153 		t_msf.end_f = l_msf.cdmsf_frame1;
154 
155 		error = ioctlf(fp, CDIOCPLAYMSF, (caddr_t)&t_msf, p);
156 		break;
157 
158 	case LINUX_CDROMPLAYTRKIND:
159 		error = copyin(SCARG(uap, data), &l_ti, sizeof l_ti);
160 		if (error)
161 			break;
162 
163 		t_track.start_track = l_ti.cdti_trk0;
164 		t_track.start_index = l_ti.cdti_ind0;
165 		t_track.end_track = l_ti.cdti_trk1;
166 		t_track.end_index = l_ti.cdti_ind1;
167 
168 		error = ioctlf(fp, CDIOCPLAYTRACKS, (caddr_t)&t_track, p);
169 		break;
170 
171 	case LINUX_CDROMREADTOCHDR:
172 		error = ioctlf(fp, CDIOREADTOCHEADER, (caddr_t)&t_header, p);
173 		if (error)
174 			break;
175 
176 		l_tochdr.cdth_trk0 = t_header.starting_track;
177 		l_tochdr.cdth_trk1 = t_header.ending_track;
178 
179 		error = copyout(&l_tochdr, SCARG(uap, data), sizeof l_tochdr);
180 		break;
181 
182 	case LINUX_CDROMREADTOCENTRY:
183 		error = copyin(SCARG(uap, data), &l_tocentry,
184 			       sizeof l_tocentry);
185 		if (error)
186 			break;
187 
188 		sg = stackgap_init(p, 0);
189 		entry = stackgap_alloc(p, &sg, sizeof *entry);
190 		t_toc_entry.address_format = l_tocentry.cdte_format;
191 		t_toc_entry.starting_track = l_tocentry.cdte_track;
192 		t_toc_entry.data_len = sizeof *entry;
193 		t_toc_entry.data = entry;
194 
195 		error = ioctlf(fp, CDIOREADTOCENTRIES, (caddr_t)&t_toc_entry,
196 			       p);
197 		if (error)
198 			break;
199 
200 		error = copyin(entry, &t_entry, sizeof t_entry);
201 		if (error)
202 			break;
203 
204 		l_tocentry.cdte_adr = t_entry.addr_type;
205 		l_tocentry.cdte_ctrl = t_entry.control;
206 		if (bsd_to_linux_msf_lba(t_entry.addr_type, &t_entry.addr,
207 		    &l_tocentry.cdte_addr) < 0) {
208 			DPRINTF(("linux_ioctl: unknown format msf/lba\n"));
209 			error = EINVAL;
210 			break;
211 		}
212 
213 		error = copyout(&l_tocentry, SCARG(uap, data),
214 			       sizeof l_tocentry);
215 		break;
216 
217 	case LINUX_CDROMVOLCTRL:
218 		error = copyin(SCARG(uap, data), &l_volctrl, sizeof l_volctrl);
219 		if (error)
220 			break;
221 
222 		t_vol.vol[0] = l_volctrl.channel0;
223 		t_vol.vol[1] = l_volctrl.channel1;
224 		t_vol.vol[2] = l_volctrl.channel2;
225 		t_vol.vol[3] = l_volctrl.channel3;
226 
227 		error = ioctlf(fp, CDIOCSETVOL, (caddr_t)&t_vol, p);
228 		break;
229 
230 	case LINUX_CDROMVOLREAD:
231 		error = ioctlf(fp, CDIOCGETVOL, (caddr_t)&t_vol, p);
232 		if (error)
233 			break;
234 
235 		l_volctrl.channel0 = t_vol.vol[0];
236 		l_volctrl.channel1 = t_vol.vol[1];
237 		l_volctrl.channel2 = t_vol.vol[2];
238 		l_volctrl.channel3 = t_vol.vol[3];
239 
240 		error = copyout(&l_volctrl, SCARG(uap, data), sizeof l_volctrl);
241 		break;
242 
243 	case LINUX_CDROMSUBCHNL:
244 		error = copyin(SCARG(uap, data), &l_subchnl, sizeof l_subchnl);
245 		if (error)
246 			break;
247 
248 		sg = stackgap_init(p, 0);
249 		info = stackgap_alloc(p, &sg, sizeof *info);
250 		t_subchannel.address_format = CD_MSF_FORMAT;
251 		t_subchannel.track = 0;
252 		t_subchannel.data_format = l_subchnl.cdsc_format;
253 		t_subchannel.data_len = sizeof *info;
254 		t_subchannel.data = info;
255 		DPRINTF(("linux_ioctl: CDROMSUBCHNL %d %d\n",
256 			 l_subchnl.cdsc_format, l_subchnl.cdsc_trk));
257 
258 		error = ioctlf(fp, CDIOCREADSUBCHANNEL, (caddr_t)&t_subchannel,
259 			       p);
260 		if (error)
261 			break;
262 
263 		error = copyin(info, &t_info, sizeof t_info);
264 		if (error)
265 			break;
266 
267 		l_subchnl.cdsc_audiostatus = t_info.header.audio_status;
268 		l_subchnl.cdsc_adr = t_info.what.position.addr_type;
269 		l_subchnl.cdsc_ctrl = t_info.what.position.control;
270 		l_subchnl.cdsc_ind = t_info.what.position.index_number;
271 
272 		DPRINTF(("linux_ioctl: CDIOCREADSUBCHANNEL %d %d %d\n",
273 			t_info.header.audio_status,
274 			t_info.header.data_len[0],
275 			t_info.header.data_len[1]));
276 		DPRINTF(("(more) %d %d %d %d %d\n",
277 			t_info.what.position.data_format,
278 			t_info.what.position.control,
279 			t_info.what.position.addr_type,
280 			t_info.what.position.track_number,
281 			t_info.what.position.index_number));
282 
283 		if (bsd_to_linux_msf_lba(t_subchannel.address_format,
284 					 &t_info.what.position.absaddr,
285 					 &l_subchnl.cdsc_absaddr) < 0 ||
286 		    bsd_to_linux_msf_lba(t_subchannel.address_format,
287 					 &t_info.what.position.reladdr,
288 					 &l_subchnl.cdsc_reladdr) < 0) {
289 			DPRINTF(("linux_ioctl: unknown format msf/lba\n"));
290 			error = EINVAL;
291 			break;
292 		}
293 
294 		error = copyout(&l_subchnl, SCARG(uap, data), sizeof l_subchnl);
295 		break;
296 
297 	case LINUX_CDROMPLAYBLK:
298 		error = copyin(SCARG(uap, data), &l_blk, sizeof l_blk);
299 		if (error)
300 			break;
301 
302 		t_blocks.blk = l_blk.from;
303 		t_blocks.len = l_blk.len;
304 
305 		error = ioctlf(fp, CDIOCPLAYBLOCKS, (caddr_t)&t_blocks, p);
306 		break;
307 
308 	case LINUX_CDROMEJECT_SW:
309 		error = copyin(SCARG(uap, data), &idata, sizeof idata);
310 		if (error)
311 			break;
312 
313 		if (idata == 1)
314 			ncom = CDIOCALLOW;
315 		else
316 			ncom = CDIOCPREVENT;
317 		error = ioctlf(fp, ncom, NULL, p);
318 		break;
319 
320 	case LINUX_CDROMPAUSE:
321 		error = ioctlf(fp, CDIOCPAUSE, NULL, p);
322 		break;
323 
324 	case LINUX_CDROMRESUME:
325 		error = ioctlf(fp, CDIOCRESUME, NULL, p);
326 		break;
327 
328 	case LINUX_CDROMSTOP:
329 		error = ioctlf(fp, CDIOCSTOP, NULL, p);
330 		break;
331 
332 	case LINUX_CDROMSTART:
333 		error = ioctlf(fp, CDIOCSTART, NULL, p);
334 		break;
335 
336 	case LINUX_CDROMEJECT:
337 		error = ioctlf(fp, CDIOCEJECT, NULL, p);
338 		break;
339 
340 	case LINUX_CDROMRESET:
341 		error = ioctlf(fp, CDIOCRESET, NULL, p);
342 		break;
343 
344 	case LINUX_CDROMMULTISESSION:
345 		error = copyin(SCARG(uap, data), &l_session, sizeof l_session);
346 		if (error)
347 			break;
348 
349 		error = ioctlf(fp, CDIOREADTOCHEADER, (caddr_t)&t_header, p);
350 		if (error)
351 			break;
352 
353 		sg = stackgap_init(p, 0);
354 		entry = stackgap_alloc(p, &sg, sizeof *entry);
355 		t_toc_entry.address_format = l_session.addr_format;
356 		t_toc_entry.starting_track = 0;
357 		t_toc_entry.data_len = sizeof *entry;
358 		t_toc_entry.data = entry;
359 
360 		error = ioctlf(fp, CDIOREADTOCENTRIES,
361 		    (caddr_t)&t_toc_entry, p);
362 		if (error)
363 			break;
364 
365 		error = copyin(entry, &t_entry, sizeof t_entry);
366 		if (error)
367 			break;
368 
369 		if (bsd_to_linux_msf_lba(l_session.addr_format,
370 		    &t_entry.addr, &l_session.addr) < 0) {
371 			error = EINVAL;
372 			break;
373 		}
374 
375 		l_session.xa_flag =
376 		    t_header.starting_track != t_header.ending_track;
377 
378 		error = copyout(&l_session, SCARG(uap, data), sizeof l_session);
379 		break;
380 
381 	case LINUX_CDROMCLOSETRAY:
382 		error = ioctlf(fp, CDIOCCLOSE, NULL, p);
383 		break;
384 
385 	case LINUX_CDROM_LOCKDOOR:
386 		ncom = SCARG(uap, data) != 0 ? CDIOCPREVENT : CDIOCALLOW;
387 		error = ioctlf(fp, ncom, NULL, p);
388 		break;
389 
390 	case LINUX_CDROM_SET_OPTIONS:
391 	case LINUX_CDROM_CLEAR_OPTIONS:
392 		/* whatever you say */
393 		break;
394 
395 	case LINUX_CDROM_DEBUG:
396 		ncom = SCARG(uap, data) != 0 ? CDIOCSETDEBUG : CDIOCCLRDEBUG;
397 		error = ioctlf(fp, ncom, NULL, p);
398 		break;
399 
400 	case LINUX_CDROM_SELECT_SPEED:
401 	case LINUX_CDROM_SELECT_DISC:
402 	case LINUX_CDROM_MEDIA_CHANGED:
403 	case LINUX_CDROM_DRIVE_STATUS:
404 	case LINUX_CDROM_DISC_STATUS:
405 	case LINUX_CDROM_CHANGER_NSLOTS:
406 	case LINUX_CDROM_GET_CAPABILITY:
407 		error = ENOSYS;
408 		break;
409 
410 	case LINUX_DVD_READ_STRUCT:
411 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
412 		if (error)
413 			break;
414 		error = ioctlf(fp, DVD_READ_STRUCT, (caddr_t)&ds, p);
415 		if (error)
416 			break;
417 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
418 		break;
419 
420 	case LINUX_DVD_WRITE_STRUCT:
421 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
422 		if (error)
423 			break;
424 		error = ioctlf(fp, DVD_WRITE_STRUCT, (caddr_t)&ds, p);
425 		if (error)
426 			break;
427 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
428 		break;
429 
430 	case LINUX_DVD_AUTH:
431 		error = copyin(SCARG(uap, data), &dai, sizeof dai);
432 		if (error)
433 			break;
434 		error = ioctlf(fp, DVD_AUTH, (caddr_t)&dai, p);
435 		if (error)
436 			break;
437 		error = copyout(&dai, SCARG(uap, data), sizeof dai);
438 		break;
439 
440 
441 	default:
442 		DPRINTF(("linux_ioctl: unimplemented ioctl %08lx\n", com));
443 		error = EINVAL;
444 	}
445 
446 	FILE_UNUSE(fp, p);
447 	return error;
448 }
449