linux_cdrom.c revision 1.27.2.1 1 /* $NetBSD: linux_cdrom.c,v 1.27.2.1 2008/05/10 23:48:55 wrstuden 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.27.2.1 2008/05/10 23:48:55 wrstuden 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/sa.h>
44 #include <sys/syscallargs.h>
45
46 #include <compat/linux/common/linux_types.h>
47 #include <compat/linux/common/linux_ioctl.h>
48 #include <compat/linux/common/linux_signal.h>
49 #include <compat/linux/common/linux_util.h>
50 #include <compat/linux/common/linux_ipc.h>
51 #include <compat/linux/common/linux_sem.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 DEBUG_LINUX
60 #define DPRINTF(x) uprintf 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(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
91 {
92 /* {
93 syscallarg(int) fd;
94 syscallarg(u_long) com;
95 syscallarg(void *) data;
96 } */
97 int error, idata;
98 u_long com, ncom;
99 file_t *fp;
100 int (*ioctlf)(file_t *, u_long, void *);
101
102 union {
103 struct linux_cdrom_blk ll_blk;
104 struct linux_cdrom_msf ll_msf;
105 struct linux_cdrom_ti ll_ti;
106 struct linux_cdrom_tochdr ll_tochdr;
107 struct linux_cdrom_tocentry ll_tocentry;
108 struct linux_cdrom_subchnl ll_subchnl;
109 struct linux_cdrom_volctrl ll_volctrl;
110 struct linux_cdrom_multisession ll_session;
111 dvd_struct ll_ds;
112 dvd_authinfo ll_dai;
113 } *u1;
114
115 #define l_blk u1->ll_blk
116 #define l_msf u1->ll_msf
117 #define l_ti u1->ll_ti
118 #define l_tochdr u1->ll_tochdr
119 #define l_tocentry u1->ll_tocentry
120 #define l_subchnl u1->ll_subchnl
121 #define l_volctrl u1->ll_volctrl
122 #define l_session u1->ll_session
123 #define ds u1->ll_ds
124 #define dai u1->ll_dai
125
126 union {
127 struct ioc_play_blocks tt_blocks;
128 struct ioc_play_msf tt_msf;
129 struct ioc_play_track tt_track;
130 struct ioc_toc_header tt_header;
131 struct ioc_read_toc_entry_buf tt_toc_entry;
132 struct ioc_read_subchannel_buf tt_subchannel;
133 struct ioc_vol tt_vol;
134 } *u2;
135
136 #define t_blocks u2->tt_blocks
137 #define t_msf u2->tt_msf
138 #define t_track u2->tt_track
139 #define t_header u2->tt_header
140 #define t_toc_entry u2->tt_toc_entry
141 #define t_subchannel u2->tt_subchannel
142 #define t_vol u2->tt_vol
143
144 struct cd_toc_entry *entry;
145
146 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
147 return (EBADF);
148
149 com = SCARG(uap, com);
150 ioctlf = fp->f_ops->fo_ioctl;
151 retval[0] = error = 0;
152
153 u1 = malloc(sizeof(*u1), M_TEMP, M_WAITOK);
154 u2 = malloc(sizeof(*u2), M_TEMP, M_WAITOK);
155
156 switch(com) {
157 case LINUX_CDROMPLAYMSF:
158 error = copyin(SCARG(uap, data), &l_msf, sizeof l_msf);
159 if (error)
160 break;
161
162 t_msf.start_m = l_msf.cdmsf_min0;
163 t_msf.start_s = l_msf.cdmsf_sec0;
164 t_msf.start_f = l_msf.cdmsf_frame0;
165 t_msf.end_m = l_msf.cdmsf_min1;
166 t_msf.end_s = l_msf.cdmsf_sec1;
167 t_msf.end_f = l_msf.cdmsf_frame1;
168
169 error = ioctlf(fp, CDIOCPLAYMSF, &t_msf);
170 break;
171
172 case LINUX_CDROMPLAYTRKIND:
173 error = copyin(SCARG(uap, data), &l_ti, sizeof l_ti);
174 if (error)
175 break;
176
177 t_track.start_track = l_ti.cdti_trk0;
178 t_track.start_index = l_ti.cdti_ind0;
179 t_track.end_track = l_ti.cdti_trk1;
180 t_track.end_index = l_ti.cdti_ind1;
181
182 error = ioctlf(fp, CDIOCPLAYTRACKS, &t_track);
183 break;
184
185 case LINUX_CDROMREADTOCHDR:
186 error = ioctlf(fp, CDIOREADTOCHEADER, &t_header);
187 if (error)
188 break;
189
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 l_volctrl.channel0 = t_vol.vol[0];
243 l_volctrl.channel1 = t_vol.vol[1];
244 l_volctrl.channel2 = t_vol.vol[2];
245 l_volctrl.channel3 = t_vol.vol[3];
246
247 error = copyout(&l_volctrl, SCARG(uap, data), sizeof l_volctrl);
248 break;
249
250 case LINUX_CDROMSUBCHNL:
251 error = copyin(SCARG(uap, data), &l_subchnl, sizeof l_subchnl);
252 if (error)
253 break;
254
255 t_subchannel.req.address_format = CD_MSF_FORMAT;
256 t_subchannel.req.track = 0;
257 t_subchannel.req.data_format = l_subchnl.cdsc_format;
258 t_subchannel.req.data_len = sizeof t_subchannel.info;
259 t_subchannel.req.data = NULL;
260 DPRINTF(("linux_ioctl: CDROMSUBCHNL %d %d\n",
261 l_subchnl.cdsc_format, l_subchnl.cdsc_trk));
262
263 error = ioctlf(fp, CDIOCREADSUBCHANNEL_BUF, &t_subchannel);
264 if (error)
265 break;
266
267 l_subchnl.cdsc_audiostatus = t_subchannel.info.header.audio_status;
268 l_subchnl.cdsc_adr = t_subchannel.info.what.position.addr_type;
269 l_subchnl.cdsc_ctrl = t_subchannel.info.what.position.control;
270 l_subchnl.cdsc_ind = t_subchannel.info.what.position.index_number;
271
272 DPRINTF(("linux_ioctl: CDIOCREADSUBCHANNEL %d %d %d\n",
273 t_subchannel.info.header.audio_status,
274 t_subchannel.info.header.data_len[0],
275 t_subchannel.info.header.data_len[1]));
276 DPRINTF(("(more) %d %d %d %d %d\n",
277 t_subchannel.info.what.position.data_format,
278 t_subchannel.info.what.position.control,
279 t_subchannel.info.what.position.addr_type,
280 t_subchannel.info.what.position.track_number,
281 t_subchannel.info.what.position.index_number));
282
283 if (bsd_to_linux_msf_lba(t_subchannel.req.address_format,
284 &t_subchannel.info.what.position.absaddr,
285 &l_subchnl.cdsc_absaddr) < 0 ||
286 bsd_to_linux_msf_lba(t_subchannel.req.address_format,
287 &t_subchannel.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, &t_blocks);
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);
318 break;
319
320 case LINUX_CDROMPAUSE:
321 error = ioctlf(fp, CDIOCPAUSE, NULL);
322 break;
323
324 case LINUX_CDROMRESUME:
325 error = ioctlf(fp, CDIOCRESUME, NULL);
326 break;
327
328 case LINUX_CDROMSTOP:
329 error = ioctlf(fp, CDIOCSTOP, NULL);
330 break;
331
332 case LINUX_CDROMSTART:
333 error = ioctlf(fp, CDIOCSTART, NULL);
334 break;
335
336 case LINUX_CDROMEJECT:
337 error = ioctlf(fp, CDIOCEJECT, NULL);
338 break;
339
340 case LINUX_CDROMRESET:
341 error = ioctlf(fp, CDIOCRESET, NULL);
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, &t_header);
350 if (error)
351 break;
352
353 t_toc_entry.req.address_format = l_session.addr_format;
354 t_toc_entry.req.starting_track = 0;
355 t_toc_entry.req.data_len = sizeof t_toc_entry.entry;
356 t_toc_entry.req.data = NULL;
357
358 error = ioctlf(fp, CDIOREADTOCENTRIES_BUF, &t_toc_entry);
359 if (error)
360 break;
361
362 if (bsd_to_linux_msf_lba(l_session.addr_format,
363 &t_toc_entry.entry[0].addr, &l_session.addr) < 0) {
364 error = EINVAL;
365 break;
366 }
367
368 l_session.xa_flag =
369 t_header.starting_track != t_header.ending_track;
370
371 error = copyout(&l_session, SCARG(uap, data), sizeof l_session);
372 break;
373
374 case LINUX_CDROMCLOSETRAY:
375 error = ioctlf(fp, CDIOCCLOSE, NULL);
376 break;
377
378 case LINUX_CDROM_LOCKDOOR:
379 ncom = SCARG(uap, data) != 0 ? CDIOCPREVENT : CDIOCALLOW;
380 error = ioctlf(fp, ncom, NULL);
381 break;
382
383 case LINUX_CDROM_SET_OPTIONS:
384 case LINUX_CDROM_CLEAR_OPTIONS:
385 /* whatever you say */
386 break;
387
388 case LINUX_CDROM_DEBUG:
389 ncom = SCARG(uap, data) != 0 ? CDIOCSETDEBUG : CDIOCCLRDEBUG;
390 error = ioctlf(fp, ncom, NULL);
391 break;
392
393 case LINUX_CDROM_SELECT_SPEED:
394 case LINUX_CDROM_SELECT_DISC:
395 case LINUX_CDROM_MEDIA_CHANGED:
396 case LINUX_CDROM_DRIVE_STATUS:
397 case LINUX_CDROM_DISC_STATUS:
398 case LINUX_CDROM_CHANGER_NSLOTS:
399 case LINUX_CDROM_GET_CAPABILITY:
400 error = ENOSYS;
401 break;
402
403 case LINUX_DVD_READ_STRUCT:
404 error = copyin(SCARG(uap, data), &ds, sizeof ds);
405 if (error)
406 break;
407 error = ioctlf(fp, DVD_READ_STRUCT, &ds);
408 if (error)
409 break;
410 error = copyout(&ds, SCARG(uap, data), sizeof ds);
411 break;
412
413 case LINUX_DVD_WRITE_STRUCT:
414 error = copyin(SCARG(uap, data), &ds, sizeof ds);
415 if (error)
416 break;
417 error = ioctlf(fp, DVD_WRITE_STRUCT, &ds);
418 if (error)
419 break;
420 error = copyout(&ds, SCARG(uap, data), sizeof ds);
421 break;
422
423 case LINUX_DVD_AUTH:
424 error = copyin(SCARG(uap, data), &dai, sizeof dai);
425 if (error)
426 break;
427 error = ioctlf(fp, DVD_AUTH, &dai);
428 if (error)
429 break;
430 error = copyout(&dai, SCARG(uap, data), sizeof dai);
431 break;
432
433
434 default:
435 DPRINTF(("linux_ioctl: unimplemented ioctl %08lx\n", com));
436 error = EINVAL;
437 }
438
439 fd_putfile(SCARG(uap, fd));
440 free(u1, M_TEMP);
441 free(u2, M_TEMP);
442 return error;
443 }
444