Mercurial > mplayer.hg
annotate libmpdvdkit2/bsdi_ioctl.c @ 14880:991f5a7b1b7d
telecine now works in display order (rather than decoding), as far as there are no more than 4 consecutive b-frames; added support for FMP4
author | nicodvb |
---|---|
date | Tue, 01 Mar 2005 20:56:43 +0000 |
parents | c2ddedd0619e |
children | 954cdf2171f6 |
rev | line source |
---|---|
7027 | 1 #include "config.h" |
2 | |
3 /* | |
4 * Hacked version of the linux cdrom.c kernel module - everything except the | |
5 * DVD handling ripped out and the rest rewritten to use raw SCSI commands | |
6 * on BSD/OS 4.2 (but should work with earlier versions as well). | |
14860
c2ddedd0619e
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
14643
diff
changeset
|
7 * |
c2ddedd0619e
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
14643
diff
changeset
|
8 * Modified for use with MPlayer, changes contained in libdvdcss_changes.diff. |
c2ddedd0619e
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
14643
diff
changeset
|
9 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
c2ddedd0619e
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
14643
diff
changeset
|
10 * $Id$ |
7027 | 11 */ |
12 | |
13 #include <sys/types.h> | |
14 #include <stdlib.h> | |
15 #include <stdio.h> | |
16 #include <errno.h> | |
17 #include <unistd.h> | |
18 #include <sys/ioctl.h> | |
19 #include </sys/dev/scsi/scsi.h> | |
20 #include </sys/dev/scsi/scsi_ioctl.h> | |
21 | |
22 #include "bsdi_dvd.h" | |
23 | |
24 /* | |
25 * Now get rid of the override/intercept macro so we can call the real ioctl() | |
26 * routine! | |
27 */ | |
28 #undef ioctl | |
29 | |
30 #define CMD_READ_10 0x28 | |
31 #define CMD_READ_TOC_PMA_ATIP 0x43 | |
32 #define CMD_READ_CD 0xbe | |
33 #define CMD_START_STOP_UNIT 0x1b | |
34 | |
35 #define CMD_SEND_KEY 0xa3 | |
36 #define CMD_REPORT_KEY 0xa4 | |
37 #define CMD_READ_DVD_STRUCTURE 0xad | |
38 | |
39 #define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key)) | |
40 #define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge)) | |
41 | |
42 /* Define the Cdrom Generic Command structure */ | |
43 typedef struct cgc | |
44 { | |
45 u_char cdb[12]; | |
46 u_char *buf; | |
47 int buflen; | |
48 int rw; | |
49 int timeout; | |
50 scsi_user_sense_t *sus; | |
51 } cgc_t; | |
52 | |
53 static int scsi_cmd(int, cgc_t *); | |
54 static int cdrom_ioctl(int, u_long, void *); | |
55 static int cdrom_tray_move(int, int); | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
56 static void cdrom_count_tracks(int, tracktype *); |
7027 | 57 static int dvd_ioctl(int, u_long, void *); |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
58 static int debug = 0; |
7027 | 59 |
60 void dvd_cdrom_debug(int flag) | |
61 { | |
62 debug = flag; | |
63 } | |
64 | |
65 /* | |
66 * This is the published entry point. Actually applications should simply | |
67 * include <dvd.h> and not refer to this at all. | |
68 */ | |
69 int dvd_cdrom_ioctl(int fd, unsigned long cmd, void *arg) | |
70 { | |
71 switch (cmd) | |
72 { | |
73 case DVD_AUTH: | |
74 case DVD_READ_STRUCT: | |
75 return(dvd_ioctl(fd, cmd, arg)); | |
76 case CDROMREADTOCHDR: | |
77 case CDROMREADTOCENTRY: | |
78 case CDROMEJECT: | |
79 case CDROMREADRAW: | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
80 case CDROMREADMODE1: |
7027 | 81 case CDROMREADMODE2: |
82 case CDROMCLOSETRAY: | |
83 case CDROM_DRIVE_STATUS: | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
84 case CDROM_DISC_STATUS: |
7027 | 85 return(cdrom_ioctl(fd, cmd, arg)); |
86 default: | |
87 return(ioctl(fd, cmd, arg)); | |
88 } | |
89 } | |
90 | |
91 static void setup_report_key(cgc_t *cgc, u_int agid, u_int type) | |
92 { | |
93 | |
94 cgc->cdb[0] = CMD_REPORT_KEY; | |
95 cgc->cdb[10] = type | (agid << 6); | |
96 switch (type) | |
97 { | |
98 case 0: | |
99 case 5: | |
100 case 8: | |
101 cgc->buflen = 8; | |
102 break; | |
103 case 1: | |
104 cgc->buflen = 16; | |
105 break; | |
106 case 2: | |
107 case 4: | |
108 cgc->buflen = 12; | |
109 break; | |
110 } | |
111 cgc->cdb[9] = cgc->buflen; | |
112 cgc->rw = SUC_READ;; | |
113 } | |
114 | |
115 static void setup_send_key(cgc_t *cgc, u_int agid, u_int type) | |
116 { | |
117 | |
118 cgc->cdb[0] = CMD_SEND_KEY; | |
119 cgc->cdb[10] = type | (agid << 6); | |
120 switch (type) | |
121 { | |
122 case 1: | |
123 cgc->buflen = 16; | |
124 break; | |
125 case 3: | |
126 cgc->buflen = 12; | |
127 break; | |
128 case 6: | |
129 cgc->buflen = 8; | |
130 break; | |
131 } | |
132 cgc->cdb[9] = cgc->buflen; | |
133 cgc->rw = SUC_WRITE; | |
134 } | |
135 | |
136 static void cgc_init(cgc_t *cgc, void *buf, int len, int type) | |
137 { | |
138 | |
139 memset(cgc, 0, sizeof (*cgc)); | |
140 if (buf) | |
141 memset(buf, 0, len); | |
142 cgc->buf = (u_char *)buf; | |
143 cgc->buflen = len; | |
144 cgc->rw = type; | |
145 cgc->timeout = 5; /* 5 second timeout */ | |
146 } | |
147 | |
148 static int dvd_do_auth(int fd, dvd_authinfo *ai) | |
149 { | |
150 int ret; | |
151 u_char buf[20]; | |
152 cgc_t cgc; | |
153 rpc_state_t rpc_state; | |
154 | |
155 memset(buf, 0, sizeof(buf)); | |
156 cgc_init(&cgc, buf, 0, SUC_READ); | |
157 | |
158 switch (ai->type) | |
159 { | |
160 case DVD_LU_SEND_AGID: /* LU data send */ | |
161 setup_report_key(&cgc, ai->lsa.agid, 0); | |
162 if (ret = scsi_cmd(fd, &cgc)) | |
163 return ret; | |
164 ai->lsa.agid = buf[7] >> 6; | |
165 break; | |
166 case DVD_LU_SEND_KEY1: | |
167 setup_report_key(&cgc, ai->lsk.agid, 2); | |
168 if (ret = scsi_cmd(fd, &cgc)) | |
169 return ret; | |
170 copy_key(ai->lsk.key, &buf[4]); | |
171 break; | |
172 case DVD_LU_SEND_CHALLENGE: | |
173 setup_report_key(&cgc, ai->lsc.agid, 1); | |
174 if (ret = scsi_cmd(fd, &cgc)) | |
175 return ret; | |
176 copy_chal(ai->lsc.chal, &buf[4]); | |
177 break; | |
178 case DVD_LU_SEND_TITLE_KEY: /* Post-auth key */ | |
179 setup_report_key(&cgc, ai->lstk.agid, 4); | |
180 cgc.cdb[5] = ai->lstk.lba; | |
181 cgc.cdb[4] = ai->lstk.lba >> 8; | |
182 cgc.cdb[3] = ai->lstk.lba >> 16; | |
183 cgc.cdb[2] = ai->lstk.lba >> 24; | |
184 if (ret = scsi_cmd(fd, &cgc)) | |
185 return ret; | |
186 ai->lstk.cpm = (buf[4] >> 7) & 1; | |
187 ai->lstk.cp_sec = (buf[4] >> 6) & 1; | |
188 ai->lstk.cgms = (buf[4] >> 4) & 3; | |
189 copy_key(ai->lstk.title_key, &buf[5]); | |
190 break; | |
191 case DVD_LU_SEND_ASF: | |
192 setup_report_key(&cgc, ai->lsasf.agid, 5); | |
193 if (ret = scsi_cmd(fd, &cgc)) | |
194 return ret; | |
195 ai->lsasf.asf = buf[7] & 1; | |
196 break; | |
197 case DVD_HOST_SEND_CHALLENGE: /* LU data receive (LU changes state) */ | |
198 setup_send_key(&cgc, ai->hsc.agid, 1); | |
199 buf[1] = 0xe; | |
200 copy_chal(&buf[4], ai->hsc.chal); | |
201 if (ret = scsi_cmd(fd, &cgc)) | |
202 return ret; | |
203 ai->type = DVD_LU_SEND_KEY1; | |
204 break; | |
205 case DVD_HOST_SEND_KEY2: | |
206 setup_send_key(&cgc, ai->hsk.agid, 3); | |
207 buf[1] = 0xa; | |
208 copy_key(&buf[4], ai->hsk.key); | |
209 if (ret = scsi_cmd(fd, &cgc)) | |
210 { | |
211 ai->type = DVD_AUTH_FAILURE; | |
212 return ret; | |
213 } | |
214 ai->type = DVD_AUTH_ESTABLISHED; | |
215 break; | |
216 case DVD_INVALIDATE_AGID: | |
217 setup_report_key(&cgc, ai->lsa.agid, 0x3f); | |
218 if (ret = scsi_cmd(fd, &cgc)) | |
219 return ret; | |
220 break; | |
221 case DVD_LU_SEND_RPC_STATE: /* Get region settings */ | |
222 setup_report_key(&cgc, 0, 8); | |
223 memset(&rpc_state, 0, sizeof(rpc_state_t)); | |
224 cgc.buf = (char *) &rpc_state; | |
225 if (ret = scsi_cmd(fd, &cgc)) | |
226 { | |
227 ai->lrpcs.type = 0; | |
228 ai->lrpcs.rpc_scheme = 0; | |
229 } | |
230 else | |
231 { | |
232 ai->lrpcs.type = rpc_state.type_code; | |
233 ai->lrpcs.vra = rpc_state.vra; | |
234 ai->lrpcs.ucca = rpc_state.ucca; | |
235 ai->lrpcs.region_mask = rpc_state.region_mask; | |
236 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme; | |
237 } | |
238 break; | |
239 case DVD_HOST_SEND_RPC_STATE: /* Set region settings */ | |
240 setup_send_key(&cgc, 0, 6); | |
241 buf[1] = 6; | |
242 buf[4] = ai->hrpcs.pdrc; | |
243 if (ret = scsi_cmd(fd, &cgc)) | |
244 return ret; | |
245 break; | |
246 default: | |
247 return EINVAL; | |
248 } | |
249 return 0; | |
250 } | |
251 | |
252 static int dvd_read_physical(int fd, dvd_struct *s) | |
253 { | |
254 int ret, i; | |
255 u_char buf[4 + 4 * 20], *base; | |
256 struct dvd_layer *layer; | |
257 cgc_t cgc; | |
258 | |
259 cgc_init(&cgc, buf, sizeof(buf), SUC_READ); | |
260 cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; | |
261 cgc.cdb[6] = s->physical.layer_num; | |
262 cgc.cdb[7] = s->type; | |
263 cgc.cdb[9] = cgc.buflen & 0xff; | |
264 | |
265 if (ret = scsi_cmd(fd, &cgc)) | |
266 return ret; | |
267 | |
268 base = &buf[4]; | |
269 layer = &s->physical.layer[0]; | |
270 | |
271 /* place the data... really ugly, but at least we won't have to | |
272 worry about endianess in userspace or here. */ | |
273 for (i = 0; i < 4; ++i, base += 20, ++layer) | |
274 { | |
275 memset(layer, 0, sizeof(*layer)); | |
276 layer->book_version = base[0] & 0xf; | |
277 layer->book_type = base[0] >> 4; | |
278 layer->min_rate = base[1] & 0xf; | |
279 layer->disc_size = base[1] >> 4; | |
280 layer->layer_type = base[2] & 0xf; | |
281 layer->track_path = (base[2] >> 4) & 1; | |
282 layer->nlayers = (base[2] >> 5) & 3; | |
283 layer->track_density = base[3] & 0xf; | |
284 layer->linear_density = base[3] >> 4; | |
285 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7]; | |
286 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11]; | |
287 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15]; | |
288 layer->bca = base[16] >> 7; | |
289 } | |
290 return 0; | |
291 } | |
292 | |
293 static int dvd_read_copyright(int fd, dvd_struct *s) | |
294 { | |
295 int ret; | |
296 u_char buf[8]; | |
297 cgc_t cgc; | |
298 | |
299 cgc_init(&cgc, buf, sizeof(buf), SUC_READ); | |
300 cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; | |
301 cgc.cdb[6] = s->copyright.layer_num; | |
302 cgc.cdb[7] = s->type; | |
303 cgc.cdb[8] = cgc.buflen >> 8; | |
304 cgc.cdb[9] = cgc.buflen & 0xff; | |
305 | |
306 if (ret = scsi_cmd(fd, &cgc)) | |
307 return ret; | |
308 s->copyright.cpst = buf[4]; | |
309 s->copyright.rmi = buf[5]; | |
310 return 0; | |
311 } | |
312 | |
313 static int dvd_read_disckey(int fd, dvd_struct *s) | |
314 { | |
315 int ret, size; | |
316 u_char *buf; | |
317 cgc_t cgc; | |
318 | |
319 size = sizeof(s->disckey.value) + 4; | |
320 | |
321 if ((buf = (u_char *) malloc(size)) == NULL) | |
322 return ENOMEM; | |
323 | |
324 cgc_init(&cgc, buf, size, SUC_READ); | |
325 cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; | |
326 cgc.cdb[7] = s->type; | |
327 cgc.cdb[8] = size >> 8; | |
328 cgc.cdb[9] = size & 0xff; | |
329 cgc.cdb[10] = s->disckey.agid << 6; | |
330 | |
331 if (!(ret = scsi_cmd(fd, &cgc))) | |
332 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value)); | |
333 free(buf); | |
334 return ret; | |
335 } | |
336 | |
337 static int dvd_read_bca(int fd, dvd_struct *s) | |
338 { | |
339 int ret; | |
340 u_char buf[4 + 188]; | |
341 cgc_t cgc; | |
342 | |
343 cgc_init(&cgc, buf, sizeof(buf), SUC_READ); | |
344 cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; | |
345 cgc.cdb[7] = s->type; | |
346 cgc.cdb[9] = cgc.buflen = 0xff; | |
347 | |
348 if (ret = scsi_cmd(fd, &cgc)) | |
349 return ret; | |
350 s->bca.len = buf[0] << 8 | buf[1]; | |
351 if (s->bca.len < 12 || s->bca.len > 188) | |
352 return EIO; | |
353 memcpy(s->bca.value, &buf[4], s->bca.len); | |
354 return 0; | |
355 } | |
356 | |
357 static int dvd_read_manufact(int fd, dvd_struct *s) | |
358 { | |
359 int ret = 0, size; | |
360 u_char *buf; | |
361 cgc_t cgc; | |
362 | |
363 size = sizeof(s->manufact.value) + 4; | |
364 | |
365 if ((buf = (u_char *) malloc(size)) == NULL) | |
366 return ENOMEM; | |
367 | |
368 cgc_init(&cgc, buf, size, SUC_READ); | |
369 cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; | |
370 cgc.cdb[7] = s->type; | |
371 cgc.cdb[8] = size >> 8; | |
372 cgc.cdb[9] = size & 0xff; | |
373 | |
374 if (ret = scsi_cmd(fd, &cgc)) | |
375 { | |
376 free(buf); | |
377 return ret; | |
378 } | |
379 s->manufact.len = buf[0] << 8 | buf[1]; | |
380 if (s->manufact.len < 0 || s->manufact.len > 2048) | |
381 ret = -EIO; | |
382 else | |
383 memcpy(s->manufact.value, &buf[4], s->manufact.len); | |
384 free(buf); | |
385 return ret; | |
386 } | |
387 | |
388 static int dvd_read_struct(int fd, dvd_struct *s) | |
389 { | |
390 switch (s->type) | |
391 { | |
392 case DVD_STRUCT_PHYSICAL: | |
393 return dvd_read_physical(fd, s); | |
394 case DVD_STRUCT_COPYRIGHT: | |
395 return dvd_read_copyright(fd, s); | |
396 case DVD_STRUCT_DISCKEY: | |
397 return dvd_read_disckey(fd, s); | |
398 case DVD_STRUCT_BCA: | |
399 return dvd_read_bca(fd, s); | |
400 case DVD_STRUCT_MANUFACT: | |
401 return dvd_read_manufact(fd, s); | |
402 default: | |
403 return EINVAL; | |
404 } | |
405 } | |
406 | |
407 static u_char scsi_cdblen[8] = {6, 10, 10, 12, 12, 12, 10, 10}; | |
408 | |
409 static int scsi_cmd(int fd, cgc_t *cgc) | |
410 { | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
411 int i, scsistatus, cdblen; |
7027 | 412 unsigned char *cp; |
413 struct scsi_user_cdb suc; | |
414 | |
415 /* safety checks */ | |
416 if (cgc->rw != SUC_READ && cgc->rw != SUC_WRITE) | |
417 return(EINVAL); | |
418 | |
419 suc.suc_flags = cgc->rw; | |
420 cdblen = scsi_cdblen[(cgc->cdb[0] >> 5) & 7]; | |
421 suc.suc_cdblen = cdblen; | |
422 bcopy(cgc->cdb, suc.suc_cdb, cdblen); | |
423 suc.suc_data = cgc->buf; | |
424 suc.suc_datalen = cgc->buflen; | |
425 suc.suc_timeout = cgc->timeout; | |
426 if (ioctl(fd, SCSIRAWCDB, &suc) == -1) | |
427 return(errno); | |
428 scsistatus = suc.suc_sus.sus_status; | |
429 | |
430 /* | |
431 * If the device returns a scsi sense error and debugging is enabled print | |
432 * some hopefully useful information on stderr. | |
433 */ | |
434 if (scsistatus && debug) | |
435 { | |
436 cp = suc.suc_sus.sus_sense; | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
437 fprintf(stderr,"scsistatus = %x cdb =", |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
438 scsistatus); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
439 for (i = 0; i < cdblen; i++) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
440 fprintf(stderr, " %x", cgc->cdb[i]); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
441 fprintf(stderr, "\nsense ="); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
442 for (i = 0; i < 16; i++) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
443 fprintf(stderr, " %x", cp[i]); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
444 fprintf(stderr, "\n"); |
7027 | 445 } |
446 if (cgc->sus) | |
447 bcopy(&suc.suc_sus, cgc->sus, sizeof (struct scsi_user_sense)); | |
448 if (scsistatus) | |
449 return(EIO); /* generic i/o error for unsuccessful status */ | |
450 return(0); | |
451 } | |
452 | |
453 /* | |
454 * The entry point for the DVDioctls for BSD/OS. | |
455 */ | |
456 static int dvd_ioctl(int fd, u_long cmd, void *arg) | |
457 { | |
458 int ret; | |
459 | |
460 switch (cmd) | |
461 { | |
462 case DVD_READ_STRUCT: | |
463 ret = dvd_read_struct(fd, (dvd_struct *)arg); | |
464 if (ret) | |
465 errno = ret; | |
466 return(ret ? -1 : 0); | |
467 case DVD_AUTH: | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
468 ret = dvd_do_auth(fd, (dvd_authinfo *)arg); |
7027 | 469 if (ret) |
470 errno = ret; | |
471 return(ret ? -1 : 0); | |
472 default: | |
473 errno = EINVAL; | |
474 return(-1); | |
475 } | |
476 } | |
477 | |
478 /* | |
479 * The entry point for the CDROMioctls for BSD/OS | |
480 */ | |
481 static int cdrom_read_block(int, cgc_t *, int, int, int, int); | |
482 static int cdrom_read_cd(int, cgc_t *, int, int, int ); | |
483 int cdrom_blocksize(int, int ); | |
484 | |
485 static inline | |
486 int msf_to_lba(char m, char s, char f) | |
487 { | |
488 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET; | |
489 } | |
490 | |
491 cdrom_ioctl(int fd, u_long cmd, void *arg) | |
492 { | |
493 int ret; | |
494 cgc_t cgc; | |
495 | |
496 switch (cmd) | |
497 { | |
498 case CDROMREADRAW: | |
499 case CDROMREADMODE1: | |
500 case CDROMREADMODE2: | |
501 { | |
502 struct cdrom_msf *msf; | |
503 int blocksize = 0, format = 0, lba; | |
504 | |
505 switch (cmd) | |
506 { | |
507 case CDROMREADRAW: | |
508 blocksize = CD_FRAMESIZE_RAW; | |
509 break; | |
510 case CDROMREADMODE1: | |
511 blocksize = CD_FRAMESIZE; | |
512 format = 2; | |
513 break; | |
514 case CDROMREADMODE2: | |
515 blocksize = CD_FRAMESIZE_RAW0; | |
516 break; | |
517 } | |
518 msf = (struct cdrom_msf *)arg; | |
519 lba = msf_to_lba(msf->cdmsf_min0,msf->cdmsf_sec0, | |
520 msf->cdmsf_frame0); | |
521 ret = EINVAL; | |
522 if (lba < 0) | |
523 break; | |
524 | |
525 cgc_init(&cgc, arg, blocksize, SUC_READ); | |
526 ret = cdrom_read_block(fd, &cgc, lba, 1, format, blocksize); | |
527 if (ret) | |
528 { | |
529 /* | |
530 * SCSI-II devices are not required to support CMD_READ_CD (which specifies | |
531 * the blocksize to read) so try switching the block size with a mode select, | |
532 * doing the normal read sector command and then changing the sector size back | |
533 * to 2048. | |
534 * | |
535 * If the program dies before changing the blocksize back sdopen() | |
536 * in the kernel will fail opens with a message that looks something like: | |
537 * | |
538 * "sr1: blksize 2336 not multiple of 512: cannot use" | |
539 * | |
540 * At that point the drive has to be power cycled (or reset in some other way). | |
541 */ | |
542 if (ret = cdrom_blocksize(fd, blocksize)) | |
543 break; | |
544 ret = cdrom_read_cd(fd, &cgc, lba, blocksize, 1); | |
545 ret |= cdrom_blocksize(fd, 2048); | |
546 } | |
547 break; | |
548 } | |
549 case CDROMREADTOCHDR: | |
550 { | |
551 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg; | |
552 u_char buffer[12]; | |
553 | |
554 cgc_init(&cgc, buffer, sizeof (buffer), SUC_READ); | |
555 cgc.cdb[0] = CMD_READ_TOC_PMA_ATIP; | |
556 cgc.cdb[1] = 0x2; /* MSF */ | |
557 cgc.cdb[8] = 12; /* LSB of length */ | |
558 | |
559 ret = scsi_cmd(fd, &cgc); | |
560 if (!ret) | |
561 { | |
562 tochdr->cdth_trk0 = buffer[2]; | |
563 tochdr->cdth_trk1 = buffer[3]; | |
564 } | |
565 break; | |
566 } | |
567 case CDROMREADTOCENTRY: | |
568 { | |
569 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry *) arg; | |
570 u_char buffer[12]; | |
571 | |
572 cgc_init(&cgc, buffer, sizeof (buffer), SUC_READ); | |
573 cgc.cdb[0] = CMD_READ_TOC_PMA_ATIP; | |
574 cgc.cdb[1] = (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0; | |
575 cgc.cdb[6] = tocentry->cdte_track; | |
576 cgc.cdb[8] = 12; /* LSB of length */ | |
577 | |
578 ret = scsi_cmd(fd, &cgc); | |
579 if (ret) | |
580 break; | |
581 | |
582 tocentry->cdte_ctrl = buffer[5] & 0xf; | |
583 tocentry->cdte_adr = buffer[5] >> 4; | |
584 tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0; | |
585 if (tocentry->cdte_format == CDROM_MSF) | |
586 { | |
587 tocentry->cdte_addr.msf.minute = buffer[9]; | |
588 tocentry->cdte_addr.msf.second = buffer[10]; | |
589 tocentry->cdte_addr.msf.frame = buffer[11]; | |
590 } | |
591 else | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
592 tocentry->cdte_addr.lba = (((((buffer[8] << 8) |
7027 | 593 + buffer[9]) << 8) |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
594 + buffer[10]) << 8) |
7027 | 595 + buffer[11]; |
596 break; | |
597 } | |
598 case CDROMEJECT: /* NO-OP for now */ | |
599 ret = cdrom_tray_move(fd, 1); | |
600 break; | |
601 case CDROMCLOSETRAY: | |
602 ret = cdrom_tray_move(fd, 0); | |
603 break; | |
604 /* | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
605 * This sucks but emulates the expected behaviour. Instead of the return |
7027 | 606 * value being the actual status a success/fail indicator should have been |
607 * returned and the 3rd arg to the ioctl should have been an 'int *' to update | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
608 * with the actual status. Both the drive and disc status ioctl calls are |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
609 * similarily braindamaged. |
7027 | 610 */ |
611 case CDROM_DRIVE_STATUS: | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
612 return(CDS_NO_INFO); /* XXX */ |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
613 case CDROM_DISC_STATUS: |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
614 { |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
615 tracktype tracks; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
616 int cnt; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
617 |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
618 cdrom_count_tracks(fd, &tracks); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
619 if (tracks.error) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
620 return(tracks.error); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
621 if (tracks.audio > 0) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
622 { |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
623 cnt = tracks.data + tracks.cdi + tracks.xa; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
624 if (cnt == 0) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
625 return(CDS_AUDIO); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
626 else |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
627 return(CDS_MIXED); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
628 } |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
629 if (tracks.cdi) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
630 return(CDS_XA_2_2); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
631 if (tracks.xa) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
632 return(CDS_XA_2_1); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
633 if (tracks.data) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
634 return(CDS_DATA_1); |
7027 | 635 return(CDS_NO_INFO); |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
636 } |
7027 | 637 } |
638 errno = ret; | |
639 return(ret ? -1 : 0); | |
640 } | |
641 | |
642 static int cdrom_read_cd(int fd, cgc_t *cgc, int lba, int blocksize, int nblocks) | |
643 { | |
644 | |
645 memset(&cgc->cdb, 0, sizeof(cgc->cdb)); | |
646 cgc->cdb[0] = CMD_READ_10; | |
647 cgc->cdb[2] = (lba >> 24) & 0xff; | |
648 cgc->cdb[3] = (lba >> 16) & 0xff; | |
649 cgc->cdb[4] = (lba >> 8) & 0xff; | |
650 cgc->cdb[5] = lba & 0xff; | |
651 cgc->cdb[6] = (nblocks >> 16) & 0xff; | |
652 cgc->cdb[7] = (nblocks >> 8) & 0xff; | |
653 cgc->cdb[8] = nblocks & 0xff; | |
654 cgc->buflen = blocksize * nblocks; | |
655 return(scsi_cmd(fd, cgc)); | |
656 } | |
657 | |
658 static int cdrom_read_block(int fd, cgc_t *cgc, | |
659 int lba, int nblocks, int format, int blksize) | |
660 { | |
661 | |
662 memset(&cgc->cdb, 0, sizeof(cgc->cdb)); | |
663 cgc->cdb[0] = CMD_READ_CD; | |
664 /* expected sector size - cdda,mode1,etc. */ | |
665 cgc->cdb[1] = format << 2; | |
666 /* starting address */ | |
667 cgc->cdb[2] = (lba >> 24) & 0xff; | |
668 cgc->cdb[3] = (lba >> 16) & 0xff; | |
669 cgc->cdb[4] = (lba >> 8) & 0xff; | |
670 cgc->cdb[5] = lba & 0xff; | |
671 /* number of blocks */ | |
672 cgc->cdb[6] = (nblocks >> 16) & 0xff; | |
673 cgc->cdb[7] = (nblocks >> 8) & 0xff; | |
674 cgc->cdb[8] = nblocks & 0xff; | |
675 cgc->buflen = blksize * nblocks; | |
676 | |
677 /* set the header info returned */ | |
678 switch (blksize) | |
679 { | |
680 case CD_FRAMESIZE_RAW0: | |
681 cgc->cdb[9] = 0x58; | |
682 break; | |
683 case CD_FRAMESIZE_RAW1: | |
684 cgc->cdb[9] = 0x78; | |
685 break; | |
686 case CD_FRAMESIZE_RAW: | |
687 cgc->cdb[9] = 0xf8; | |
688 break; | |
689 default: | |
690 cgc->cdb[9] = 0x10; | |
691 } | |
692 return(scsi_cmd(fd, cgc)); | |
693 } | |
694 | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
695 static void cdrom_count_tracks(int fd, tracktype *tracks) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
696 { |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
697 struct cdrom_tochdr header; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
698 struct cdrom_tocentry entry; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
699 int ret, i; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
700 |
14643
d59cc68ba13b
bzero is deprecated patch by ?Gianluigi Tiesi <mplayer at netfarm.it>
faust3
parents:
9333
diff
changeset
|
701 memset(tracks, 0, sizeof (*tracks)); |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
702 ret = cdrom_ioctl(fd, CDROMREADTOCHDR, &header); |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
703 /* |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
704 * This whole business is a crock anyhow so we don't bother distinguishing |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
705 * between no media, drive not ready, etc and on any error just say we have |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
706 * no info. |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
707 */ |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
708 if (ret) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
709 { |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
710 tracks->error = CDS_NO_INFO; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
711 return; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
712 } |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
713 |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
714 entry.cdte_format = CDROM_MSF; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
715 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
716 { |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
717 entry.cdte_track = i; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
718 if (cdrom_ioctl(fd, CDROMREADTOCENTRY, &entry)) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
719 { |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
720 tracks->error = CDS_NO_INFO; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
721 return; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
722 } |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
723 if (entry.cdte_ctrl & CDROM_DATA_TRACK) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
724 { |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
725 if (entry.cdte_format == 0x10) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
726 tracks->cdi++; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
727 else if (entry.cdte_format == 0x20) |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
728 tracks->xa++; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
729 else |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
730 tracks->data++; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
731 } |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
732 else |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
733 tracks->audio++; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
734 } |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
735 return; |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
736 } |
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
7027
diff
changeset
|
737 |
7027 | 738 static int cdrom_tray_move(int fd, int flag) |
739 { | |
740 cgc_t cgc; | |
741 | |
742 cgc_init(&cgc, NULL, 0, SUC_READ); | |
743 cgc.cdb[0] = CMD_START_STOP_UNIT; | |
744 cgc.cdb[1] = 1; /* immediate */ | |
745 cgc.cdb[4] = flag ? 0x2 : 0x3; /* eject : close */ | |
746 return(scsi_cmd(fd, &cgc)); | |
747 } | |
748 | |
749 /* | |
750 * Required when we need to use READ_10 to issue other than 2048 block | |
751 * reads | |
752 */ | |
753 int cdrom_blocksize(int fd, int size) | |
754 { | |
755 cgc_t cgc; | |
756 struct modesel_head mh; | |
757 | |
758 memset(&mh, 0, sizeof(mh)); | |
759 mh.block_desc_length = 0x08; | |
760 mh.block_length_med = (size >> 8) & 0xff; | |
761 mh.block_length_lo = size & 0xff; | |
762 | |
763 memset(&cgc, 0, sizeof(cgc)); | |
764 cgc.cdb[0] = 0x15; | |
765 cgc.cdb[1] = 1 << 4; | |
766 cgc.cdb[4] = 12; | |
767 cgc.buflen = sizeof(mh); | |
768 cgc.buf = (u_char *) &mh; | |
769 cgc.rw = SUC_WRITE; | |
770 mh.block_desc_length = 0x08; | |
771 mh.block_length_med = (size >> 8) & 0xff; | |
772 mh.block_length_lo = size & 0xff; | |
773 return(scsi_cmd(fd, &cgc)); | |
774 } |