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