xref: /netbsd-src/sys/compat/linux/common/linux_cdrom.c (revision eab380320695b8901777f834601151f48dd29933)
1 /*	$NetBSD: linux_cdrom.c,v 1.29 2024/10/01 16:41:29 riastradh Exp $ */
2 
3 /*
4  * Copyright (c) 1997, 2008 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: linux_cdrom.c,v 1.29 2024/10/01 16:41:29 riastradh Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ioctl.h>
35 #include <sys/file.h>
36 #include <sys/filedesc.h>
37 #include <sys/mount.h>
38 #include <sys/proc.h>
39 #include <sys/cdio.h>
40 #include <sys/dvdio.h>
41 #include <sys/malloc.h>
42 
43 #include <sys/syscallargs.h>
44 
45 #include <compat/linux/common/linux_types.h>
46 #include <compat/linux/common/linux_ioctl.h>
47 #include <compat/linux/common/linux_signal.h>
48 #include <compat/linux/common/linux_util.h>
49 #include <compat/linux/common/linux_ipc.h>
50 #include <compat/linux/common/linux_sem.h>
51 #include <compat/linux/common/linux_cdrom.h>
52 
53 #include <compat/linux/linux_syscallargs.h>
54 
55 static int bsd_to_linux_msf_lba(unsigned address_format, union msf_lba *bml,
56 				union linux_cdrom_addr *llml);
57 
58 #if DEBUG_LINUX
59 #define DPRINTF(x) uprintf x
60 #else
61 #define DPRINTF(x)
62 #endif
63 
64 /*
65  * XXX from dev/scsipi/cd.c
66  */
67 #define MAXTRACK 99
68 
69 static int
70 bsd_to_linux_msf_lba(unsigned address_format, union msf_lba *bml,
71 		      union linux_cdrom_addr *llml)
72 {
73 	switch (address_format) {
74 	case CD_LBA_FORMAT:
75 		llml->lba = bml->lba;
76 		break;
77 	case CD_MSF_FORMAT:
78 		llml->msf.minute = bml->msf.minute;
79 		llml->msf.second = bml->msf.second;
80 		llml->msf.frame = bml->msf.frame;
81 		break;
82 	default:
83 		return -1;
84 	}
85 	return 0;
86 }
87 
88 int
89 linux_ioctl_cdrom(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
90 {
91 	/* {
92 		syscallarg(int) fd;
93 		syscallarg(u_long) com;
94 		syscallarg(void *) data;
95 	} */
96 	int error, idata;
97 	u_long com, ncom;
98 	file_t *fp;
99 	int (*ioctlf)(file_t *, u_long, void *);
100 
101 	union {
102 		struct linux_cdrom_blk ll_blk;
103 		struct linux_cdrom_msf ll_msf;
104 		struct linux_cdrom_ti ll_ti;
105 		struct linux_cdrom_tochdr ll_tochdr;
106 		struct linux_cdrom_tocentry ll_tocentry;
107 		struct linux_cdrom_subchnl ll_subchnl;
108 		struct linux_cdrom_volctrl ll_volctrl;
109 		struct linux_cdrom_multisession ll_session;
110 		dvd_struct ll_ds;
111 		dvd_authinfo ll_dai;
112 	} *u1;
113 
114 #define l_blk u1->ll_blk
115 #define l_msf u1->ll_msf
116 #define l_ti u1->ll_ti
117 #define l_tochdr u1->ll_tochdr
118 #define l_tocentry u1->ll_tocentry
119 #define l_subchnl u1->ll_subchnl
120 #define l_volctrl u1->ll_volctrl
121 #define l_session u1->ll_session
122 #define ds u1->ll_ds
123 #define dai u1->ll_dai
124 
125 	union {
126 		struct ioc_play_blocks tt_blocks;
127 		struct ioc_play_msf tt_msf;
128 		struct ioc_play_track tt_track;
129 		struct ioc_toc_header tt_header;
130 		struct ioc_read_toc_entry_buf tt_toc_entry;
131 		struct ioc_read_subchannel_buf tt_subchannel;
132 		struct ioc_vol tt_vol;
133 	} *u2;
134 
135 #define	t_blocks u2->tt_blocks
136 #define	t_msf u2->tt_msf
137 #define	t_track u2->tt_track
138 #define	t_header u2->tt_header
139 #define	t_toc_entry u2->tt_toc_entry
140 #define	t_subchannel u2->tt_subchannel
141 #define	t_vol u2->tt_vol
142 
143 	struct cd_toc_entry *entry;
144 
145 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
146 		return (EBADF);
147 
148 	com = SCARG(uap, com);
149 	ioctlf = fp->f_ops->fo_ioctl;
150 	retval[0] = error = 0;
151 
152 	u1 = malloc(sizeof(*u1), M_TEMP, M_WAITOK);
153 	u2 = malloc(sizeof(*u2), M_TEMP, M_WAITOK);
154 
155 	switch(com) {
156 	case LINUX_CDROMPLAYMSF:
157 		error = copyin(SCARG(uap, data), &l_msf, sizeof l_msf);
158 		if (error)
159 			break;
160 
161 		t_msf.start_m = l_msf.cdmsf_min0;
162 		t_msf.start_s = l_msf.cdmsf_sec0;
163 		t_msf.start_f = l_msf.cdmsf_frame0;
164 		t_msf.end_m = l_msf.cdmsf_min1;
165 		t_msf.end_s = l_msf.cdmsf_sec1;
166 		t_msf.end_f = l_msf.cdmsf_frame1;
167 
168 		error = ioctlf(fp, CDIOCPLAYMSF, &t_msf);
169 		break;
170 
171 	case LINUX_CDROMPLAYTRKIND:
172 		error = copyin(SCARG(uap, data), &l_ti, sizeof l_ti);
173 		if (error)
174 			break;
175 
176 		t_track.start_track = l_ti.cdti_trk0;
177 		t_track.start_index = l_ti.cdti_ind0;
178 		t_track.end_track = l_ti.cdti_trk1;
179 		t_track.end_index = l_ti.cdti_ind1;
180 
181 		error = ioctlf(fp, CDIOCPLAYTRACKS, &t_track);
182 		break;
183 
184 	case LINUX_CDROMREADTOCHDR:
185 		error = ioctlf(fp, CDIOREADTOCHEADER, &t_header);
186 		if (error)
187 			break;
188 
189 		memset(&l_tochdr, 0, sizeof(l_tochdr));
190 		l_tochdr.cdth_trk0 = t_header.starting_track;
191 		l_tochdr.cdth_trk1 = t_header.ending_track;
192 
193 		error = copyout(&l_tochdr, SCARG(uap, data), sizeof l_tochdr);
194 		break;
195 
196 	case LINUX_CDROMREADTOCENTRY:
197 		error = copyin(SCARG(uap, data), &l_tocentry,
198 			       sizeof l_tocentry);
199 		if (error)
200 			break;
201 
202 		t_toc_entry.req.address_format = l_tocentry.cdte_format;
203 		t_toc_entry.req.starting_track = l_tocentry.cdte_track;
204 		t_toc_entry.req.data_len = sizeof *entry;
205 		t_toc_entry.req.data = NULL;
206 
207 		error = ioctlf(fp, CDIOREADTOCENTRIES_BUF, &t_toc_entry);
208 		if (error)
209 			break;
210 
211 		l_tocentry.cdte_adr = t_toc_entry.entry[0].addr_type;
212 		l_tocentry.cdte_ctrl = t_toc_entry.entry[0].control;
213 		if (bsd_to_linux_msf_lba(t_toc_entry.entry[0].addr_type,
214 		    &t_toc_entry.entry[0].addr, &l_tocentry.cdte_addr) < 0) {
215 			DPRINTF(("linux_ioctl: unknown format msf/lba\n"));
216 			error = EINVAL;
217 			break;
218 		}
219 
220 		error = copyout(&l_tocentry, SCARG(uap, data),
221 			       sizeof l_tocentry);
222 		break;
223 
224 	case LINUX_CDROMVOLCTRL:
225 		error = copyin(SCARG(uap, data), &l_volctrl, sizeof l_volctrl);
226 		if (error)
227 			break;
228 
229 		t_vol.vol[0] = l_volctrl.channel0;
230 		t_vol.vol[1] = l_volctrl.channel1;
231 		t_vol.vol[2] = l_volctrl.channel2;
232 		t_vol.vol[3] = l_volctrl.channel3;
233 
234 		error = ioctlf(fp, CDIOCSETVOL, &t_vol);
235 		break;
236 
237 	case LINUX_CDROMVOLREAD:
238 		error = ioctlf(fp, CDIOCGETVOL, &t_vol);
239 		if (error)
240 			break;
241 
242 		memset(&l_volctrl, 0, sizeof(l_volctrl));
243 		l_volctrl.channel0 = t_vol.vol[0];
244 		l_volctrl.channel1 = t_vol.vol[1];
245 		l_volctrl.channel2 = t_vol.vol[2];
246 		l_volctrl.channel3 = t_vol.vol[3];
247 
248 		error = copyout(&l_volctrl, SCARG(uap, data), sizeof l_volctrl);
249 		break;
250 
251 	case LINUX_CDROMSUBCHNL:
252 		error = copyin(SCARG(uap, data), &l_subchnl, sizeof l_subchnl);
253 		if (error)
254 			break;
255 
256 		t_subchannel.req.address_format = CD_MSF_FORMAT;
257 		t_subchannel.req.track = 0;
258 		t_subchannel.req.data_format = l_subchnl.cdsc_format;
259 		t_subchannel.req.data_len = sizeof t_subchannel.info;
260 		t_subchannel.req.data = NULL;
261 		DPRINTF(("linux_ioctl: CDROMSUBCHNL %d %d\n",
262 			 l_subchnl.cdsc_format, l_subchnl.cdsc_trk));
263 
264 		error = ioctlf(fp, CDIOCREADSUBCHANNEL_BUF, &t_subchannel);
265 		if (error)
266 			break;
267 
268 		l_subchnl.cdsc_audiostatus = t_subchannel.info.header.audio_status;
269 		l_subchnl.cdsc_adr = t_subchannel.info.what.position.addr_type;
270 		l_subchnl.cdsc_ctrl = t_subchannel.info.what.position.control;
271 		l_subchnl.cdsc_ind = t_subchannel.info.what.position.index_number;
272 
273 		DPRINTF(("linux_ioctl: CDIOCREADSUBCHANNEL %d %d %d\n",
274 			t_subchannel.info.header.audio_status,
275 			t_subchannel.info.header.data_len[0],
276 			t_subchannel.info.header.data_len[1]));
277 		DPRINTF(("(more) %d %d %d %d %d\n",
278 			t_subchannel.info.what.position.data_format,
279 			t_subchannel.info.what.position.control,
280 			t_subchannel.info.what.position.addr_type,
281 			t_subchannel.info.what.position.track_number,
282 			t_subchannel.info.what.position.index_number));
283 
284 		if (bsd_to_linux_msf_lba(t_subchannel.req.address_format,
285 				     &t_subchannel.info.what.position.absaddr,
286 				     &l_subchnl.cdsc_absaddr) < 0 ||
287 		    bsd_to_linux_msf_lba(t_subchannel.req.address_format,
288 				     &t_subchannel.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, &t_blocks);
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);
319 		break;
320 
321 	case LINUX_CDROMPAUSE:
322 		error = ioctlf(fp, CDIOCPAUSE, NULL);
323 		break;
324 
325 	case LINUX_CDROMRESUME:
326 		error = ioctlf(fp, CDIOCRESUME, NULL);
327 		break;
328 
329 	case LINUX_CDROMSTOP:
330 		error = ioctlf(fp, CDIOCSTOP, NULL);
331 		break;
332 
333 	case LINUX_CDROMSTART:
334 		error = ioctlf(fp, CDIOCSTART, NULL);
335 		break;
336 
337 	case LINUX_CDROMEJECT:
338 		error = ioctlf(fp, CDIOCEJECT, NULL);
339 		break;
340 
341 	case LINUX_CDROMRESET:
342 		error = ioctlf(fp, CDIOCRESET, NULL);
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, &t_header);
351 		if (error)
352 			break;
353 
354 		t_toc_entry.req.address_format = l_session.addr_format;
355 		t_toc_entry.req.starting_track = 0;
356 		t_toc_entry.req.data_len = sizeof t_toc_entry.entry;
357 		t_toc_entry.req.data = NULL;
358 
359 		error = ioctlf(fp, CDIOREADTOCENTRIES_BUF, &t_toc_entry);
360 		if (error)
361 			break;
362 
363 		if (bsd_to_linux_msf_lba(l_session.addr_format,
364 		    &t_toc_entry.entry[0].addr, &l_session.addr) < 0) {
365 			error = EINVAL;
366 			break;
367 		}
368 
369 		l_session.xa_flag =
370 		    t_header.starting_track != t_header.ending_track;
371 
372 		error = copyout(&l_session, SCARG(uap, data), sizeof l_session);
373 		break;
374 
375 	case LINUX_CDROMCLOSETRAY:
376 		error = ioctlf(fp, CDIOCCLOSE, NULL);
377 		break;
378 
379 	case LINUX_CDROM_LOCKDOOR:
380 		ncom = SCARG(uap, data) != 0 ? CDIOCPREVENT : CDIOCALLOW;
381 		error = ioctlf(fp, ncom, NULL);
382 		break;
383 
384 	case LINUX_CDROM_SET_OPTIONS:
385 	case LINUX_CDROM_CLEAR_OPTIONS:
386 		/* whatever you say */
387 		break;
388 
389 	case LINUX_CDROM_DEBUG:
390 		ncom = SCARG(uap, data) != 0 ? CDIOCSETDEBUG : CDIOCCLRDEBUG;
391 		error = ioctlf(fp, ncom, NULL);
392 		break;
393 
394 	case LINUX_CDROM_SELECT_SPEED:
395 	case LINUX_CDROM_SELECT_DISC:
396 	case LINUX_CDROM_MEDIA_CHANGED:
397 	case LINUX_CDROM_DRIVE_STATUS:
398 	case LINUX_CDROM_DISC_STATUS:
399 	case LINUX_CDROM_CHANGER_NSLOTS:
400 	case LINUX_CDROM_GET_CAPABILITY:
401 		error = ENOSYS;
402 		break;
403 
404 	case LINUX_DVD_READ_STRUCT:
405 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
406 		if (error)
407 			break;
408 		error = ioctlf(fp, DVD_READ_STRUCT, &ds);
409 		if (error)
410 			break;
411 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
412 		break;
413 
414 	case LINUX_DVD_WRITE_STRUCT:
415 		error = copyin(SCARG(uap, data), &ds, sizeof ds);
416 		if (error)
417 			break;
418 		error = ioctlf(fp, DVD_WRITE_STRUCT, &ds);
419 		if (error)
420 			break;
421 		error = copyout(&ds, SCARG(uap, data), sizeof ds);
422 		break;
423 
424 	case LINUX_DVD_AUTH:
425 		error = copyin(SCARG(uap, data), &dai, sizeof dai);
426 		if (error)
427 			break;
428 		error = ioctlf(fp, DVD_AUTH, &dai);
429 		if (error)
430 			break;
431 		error = copyout(&dai, SCARG(uap, data), sizeof dai);
432 		break;
433 
434 
435 	default:
436 		DPRINTF(("linux_ioctl: unimplemented ioctl %08lx\n", com));
437 		error = EINVAL;
438 	}
439 
440 	fd_putfile(SCARG(uap, fd));
441 	free(u1, M_TEMP);
442 	free(u2, M_TEMP);
443 	return error;
444 }
445