7027
|
1 /*****************************************************************************
|
|
2 * device.h: DVD device access
|
|
3 *****************************************************************************
|
|
4 * Copyright (C) 1998-2002 VideoLAN
|
|
5 * $Id$
|
|
6 *
|
|
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
|
|
8 * Samuel Hocevar <sam@zoy.org>
|
|
9 * Håkan Hjort <d95hjort@dtek.chalmers.se>
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2 of the License, or
|
|
14 * (at your option) any later version.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
|
|
24 *****************************************************************************/
|
|
25
|
|
26 /*****************************************************************************
|
|
27 * Preamble
|
|
28 *****************************************************************************/
|
|
29 #include "config.h"
|
|
30
|
|
31 #include <stdio.h>
|
|
32 #include <stdlib.h>
|
|
33 #include <string.h>
|
|
34 #include <sys/types.h>
|
|
35 #include <sys/stat.h>
|
|
36 #include <fcntl.h>
|
|
37
|
|
38 #ifdef HAVE_UNISTD_H
|
|
39 # include <unistd.h>
|
|
40 #endif
|
|
41
|
|
42 #if defined( WIN32 )
|
|
43 # include <io.h> /* read() */
|
|
44 #else
|
|
45 # include <sys/uio.h> /* struct iovec */
|
|
46 #endif
|
|
47
|
7033
|
48 #include "dvdcss.h"
|
7027
|
49
|
|
50 #include "common.h"
|
|
51 #include "css.h"
|
|
52 #include "libdvdcss.h"
|
|
53 #include "ioctl.h"
|
|
54 #include "device.h"
|
|
55
|
|
56 /*****************************************************************************
|
|
57 * Device reading prototypes, win32 specific
|
|
58 *****************************************************************************/
|
|
59 #ifdef WIN32
|
|
60 int _win32_dvdcss_readv ( int, struct iovec *, int, char * );
|
|
61 int _win32_dvdcss_aopen ( dvdcss_t, char );
|
|
62 int _win32_dvdcss_aclose ( int );
|
|
63 int _win32_dvdcss_aseek ( int, int, int );
|
|
64 int _win32_dvdcss_aread ( int, void *, int );
|
|
65 #endif
|
|
66
|
|
67 /*****************************************************************************
|
|
68 * readv_*: readv() replacements for iovec-impaired C libraries
|
|
69 *****************************************************************************/
|
|
70 #if defined( WIN32 )
|
|
71 static inline int readv( int i_fd, struct iovec * p_iovec, int i_count )
|
|
72 {
|
|
73 int i_index, i_len, i_total = 0;
|
|
74 unsigned char *p_base;
|
|
75 int i_bytes;
|
|
76
|
|
77 for( i_index = i_count; i_index; i_index-- )
|
|
78 {
|
|
79 i_len = p_iovec->iov_len;
|
|
80 p_base = p_iovec->iov_base;
|
|
81
|
|
82 /* Loop is unrolled one time to spare the (i_bytes <= 0) test */
|
|
83
|
|
84 if( i_len > 0 )
|
|
85 {
|
|
86 i_bytes = read( i_fd, p_base, i_len );
|
|
87
|
|
88 if( i_bytes < 0 )
|
|
89 {
|
|
90 /* One of the reads failed, too bad.
|
|
91 We won't even bother returning the reads that went ok,
|
|
92 and as in the posix spec the file postition is left
|
|
93 unspecified after a failure */
|
|
94 return -1;
|
|
95 }
|
|
96
|
|
97 i_total += i_bytes;
|
|
98
|
|
99 if( i_bytes != i_len )
|
|
100 {
|
|
101 /* we reached the end of the file or a signal interrupted
|
|
102 the read */
|
|
103 return i_total;
|
|
104 }
|
|
105 }
|
|
106
|
|
107 p_iovec++;
|
|
108 }
|
|
109
|
|
110 return i_total;
|
|
111 }
|
|
112 #endif /* WIN32 */
|
|
113
|
|
114 int _dvdcss_use_ioctls( dvdcss_t dvdcss )
|
|
115 {
|
|
116 #if defined( WIN32 )
|
|
117 /* Some one need to implement this for Windows */
|
|
118 if( WIN2K )
|
|
119 {
|
|
120 return 1;
|
|
121 }
|
|
122 else
|
|
123 {
|
|
124 return 1;
|
|
125 }
|
|
126 #else
|
|
127 struct stat fileinfo;
|
|
128 int ret;
|
|
129
|
|
130 ret = fstat( dvdcss->i_fd, &fileinfo );
|
|
131 if( ret < 0 )
|
|
132 {
|
|
133 return 1; /* What to do? Be conservative and try to use the ioctls */
|
|
134 }
|
|
135
|
|
136 /* Complete this list and check that we test for the right things
|
|
137 * (I've assumed for all OSs that 'r', (raw) device, are char devices
|
|
138 * and those that don't contain/use an 'r' in the name are block devices)
|
|
139 *
|
|
140 * Linux needs a block device
|
|
141 * Solaris needs a char device
|
|
142 * Darwin needs a char device
|
|
143 * OpenBSD needs a char device
|
|
144 * NetBSD needs a char device
|
|
145 * FreeBSD can use either the block or the char device
|
|
146 * BSD/OS can use either the block or the char device
|
|
147 */
|
|
148
|
|
149 /* Check if this is a block/char device */
|
|
150 if( S_ISBLK( fileinfo.st_mode ) ||
|
|
151 S_ISCHR( fileinfo.st_mode ) )
|
|
152 {
|
|
153 return 1;
|
|
154 }
|
|
155 else
|
|
156 {
|
|
157 return 0;
|
|
158 }
|
|
159 #endif
|
|
160 }
|
|
161
|
|
162 int _dvdcss_open ( dvdcss_t dvdcss )
|
|
163 {
|
|
164 char *psz_device = dvdcss->psz_device;
|
|
165
|
|
166 #if defined( WIN32 )
|
|
167 if( WIN2K )
|
|
168 {
|
|
169 char psz_dvd[7];
|
|
170 _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_device[0] );
|
|
171
|
|
172 /* To have access to ioctls, we need read and write access to the
|
|
173 * device. This is only allowed if you have administrator priviledges
|
|
174 * so we allow for a fallback method where ioctls are not available but
|
|
175 * we at least have read access to the device.
|
|
176 * (See Microsoft Q241374: Read and Write Access Required for SCSI
|
|
177 * Pass Through Requests) */
|
|
178 (HANDLE) dvdcss->i_fd =
|
|
179 CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
|
|
180 FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
181 NULL, OPEN_EXISTING,
|
|
182 FILE_FLAG_RANDOM_ACCESS, NULL );
|
|
183
|
|
184 if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
|
|
185 (HANDLE) dvdcss->i_fd =
|
|
186 CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ,
|
|
187 NULL, OPEN_EXISTING,
|
|
188 FILE_FLAG_RANDOM_ACCESS, NULL );
|
|
189
|
|
190 if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
|
|
191 {
|
|
192 _dvdcss_error( dvdcss, "failed opening device" );
|
|
193 return -1;
|
|
194 }
|
|
195 }
|
|
196 else
|
|
197 {
|
|
198 dvdcss->i_fd = _win32_dvdcss_aopen( dvdcss, psz_device[0] );
|
|
199 if( dvdcss->i_fd == -1 )
|
|
200 {
|
|
201 _dvdcss_error( dvdcss, "failed opening device" );
|
|
202 return -1;
|
|
203 }
|
|
204 }
|
|
205
|
|
206 /* initialise readv temporary buffer */
|
|
207 dvdcss->p_readv_buffer = NULL;
|
|
208 dvdcss->i_readv_buf_size = 0;
|
|
209
|
|
210 #else
|
|
211 dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, 0 );
|
|
212
|
|
213 if( dvdcss->i_fd == -1 )
|
|
214 {
|
|
215 _dvdcss_error( dvdcss, "failed opening device" );
|
|
216 return -1;
|
|
217 }
|
|
218
|
|
219 #endif
|
|
220
|
|
221 return 0;
|
|
222 }
|
|
223
|
|
224 #ifndef WIN32
|
|
225 int _dvdcss_raw_open ( dvdcss_t dvdcss, char *psz_device )
|
|
226 {
|
|
227 dvdcss->i_raw_fd = open( psz_device, 0 );
|
|
228
|
|
229 if( dvdcss->i_raw_fd == -1 )
|
|
230 {
|
|
231 _dvdcss_error( dvdcss, "failed opening raw device, continuing" );
|
|
232 return -1;
|
|
233 }
|
|
234 else
|
|
235 {
|
|
236 dvdcss->i_read_fd = dvdcss->i_raw_fd;
|
|
237 }
|
|
238
|
|
239 return 0;
|
|
240 }
|
|
241 #endif
|
|
242
|
|
243 int _dvdcss_close ( dvdcss_t dvdcss )
|
|
244 {
|
|
245 #if defined( WIN32 )
|
|
246 if( WIN2K )
|
|
247 {
|
|
248 CloseHandle( (HANDLE) dvdcss->i_fd );
|
|
249 }
|
|
250 else
|
|
251 {
|
|
252 _win32_dvdcss_aclose( dvdcss->i_fd );
|
|
253 }
|
|
254
|
|
255 /* Free readv temporary buffer */
|
|
256 if( dvdcss->p_readv_buffer )
|
|
257 {
|
|
258 free( dvdcss->p_readv_buffer );
|
|
259 dvdcss->p_readv_buffer = NULL;
|
|
260 dvdcss->i_readv_buf_size = 0;
|
|
261 }
|
|
262
|
|
263 #else
|
|
264 close( dvdcss->i_fd );
|
|
265
|
|
266 if( dvdcss->i_raw_fd >= 0 )
|
|
267 {
|
|
268 close( dvdcss->i_raw_fd );
|
|
269 dvdcss->i_raw_fd = -1;
|
|
270 }
|
|
271
|
|
272 #endif
|
|
273
|
|
274 return 0;
|
|
275 }
|
|
276
|
|
277 int _dvdcss_seek ( dvdcss_t dvdcss, int i_blocks )
|
|
278 {
|
|
279 #if defined( WIN32 )
|
|
280 dvdcss->i_seekpos = i_blocks;
|
|
281
|
|
282 if( WIN2K )
|
|
283 {
|
|
284 LARGE_INTEGER li_read;
|
|
285
|
|
286 #ifndef INVALID_SET_FILE_POINTER
|
|
287 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
|
|
288 #endif
|
|
289
|
|
290 li_read.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
|
|
291
|
|
292 li_read.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd,
|
|
293 li_read.LowPart,
|
|
294 &li_read.HighPart, FILE_BEGIN );
|
|
295 if( (li_read.LowPart == INVALID_SET_FILE_POINTER)
|
|
296 && GetLastError() != NO_ERROR)
|
|
297 {
|
|
298 li_read.QuadPart = -DVDCSS_BLOCK_SIZE;
|
|
299 }
|
|
300
|
|
301 li_read.QuadPart /= DVDCSS_BLOCK_SIZE;
|
|
302 return (int)li_read.QuadPart;
|
|
303 }
|
|
304 else
|
|
305 {
|
|
306 return ( _win32_dvdcss_aseek( dvdcss->i_fd, i_blocks, SEEK_SET ) );
|
|
307 }
|
|
308 #else
|
|
309 off_t i_read;
|
|
310
|
|
311 dvdcss->i_seekpos = i_blocks;
|
|
312
|
|
313 i_read = lseek( dvdcss->i_read_fd,
|
|
314 (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
|
|
315
|
|
316 if( i_read < 0 )
|
|
317 {
|
|
318 _dvdcss_error( dvdcss, "seek error" );
|
|
319 }
|
|
320
|
|
321 return i_read / DVDCSS_BLOCK_SIZE;
|
|
322 #endif
|
|
323
|
|
324 }
|
|
325
|
|
326 int _dvdcss_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
|
|
327 {
|
|
328 #if defined( WIN32 )
|
|
329 if( WIN2K )
|
|
330 {
|
|
331 int i_bytes;
|
|
332
|
|
333 if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer,
|
|
334 i_blocks * DVDCSS_BLOCK_SIZE,
|
|
335 (LPDWORD)&i_bytes, NULL ) )
|
|
336 {
|
|
337 return -1;
|
|
338 }
|
|
339 return i_bytes / DVDCSS_BLOCK_SIZE;
|
|
340 }
|
|
341 else
|
|
342 {
|
|
343 return _win32_dvdcss_aread( dvdcss->i_fd, p_buffer, i_blocks );
|
|
344 }
|
|
345
|
|
346 #else
|
|
347 int i_ret;
|
|
348 /* TODO: partial reads are wrong,i.e 2200/2048 = 1
|
|
349 * but the location has advanced 2200 bytes (lseek possition that is) */
|
|
350 i_ret = read( dvdcss->i_read_fd, p_buffer,
|
|
351 (off_t)i_blocks * DVDCSS_BLOCK_SIZE );
|
|
352 if( i_ret < 0 )
|
|
353 {
|
|
354 _dvdcss_error( dvdcss, "read error" );
|
|
355 return i_ret;
|
|
356 }
|
|
357
|
|
358 return i_ret / DVDCSS_BLOCK_SIZE;
|
|
359 #endif
|
|
360
|
|
361 }
|
|
362
|
|
363 int _dvdcss_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
|
|
364 {
|
|
365 int i_read;
|
|
366
|
|
367 #if defined( WIN32 )
|
|
368 /* Check the size of the readv temp buffer, just in case we need to
|
|
369 * realloc something bigger */
|
|
370 if( dvdcss->i_readv_buf_size < i_blocks * DVDCSS_BLOCK_SIZE )
|
|
371 {
|
|
372 dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE;
|
|
373
|
|
374 if( dvdcss->p_readv_buffer ) free( dvdcss->p_readv_buffer );
|
|
375
|
|
376 /* Allocate a buffer which will be used as a temporary storage
|
|
377 * for readv */
|
|
378 dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size );
|
|
379 if( !dvdcss->p_readv_buffer )
|
|
380 {
|
|
381 _dvdcss_error( dvdcss, " failed (readv)" );
|
|
382 return -1;
|
|
383 }
|
|
384 }
|
|
385
|
|
386 i_read = _win32_dvdcss_readv( dvdcss->i_fd, p_iovec, i_blocks,
|
|
387 dvdcss->p_readv_buffer );
|
|
388 return i_read;
|
|
389
|
|
390 #else
|
|
391 i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks );
|
|
392 return i_read / DVDCSS_BLOCK_SIZE;
|
|
393
|
|
394 #endif
|
|
395 }
|
|
396
|
|
397
|
|
398 #if defined( WIN32 )
|
|
399
|
|
400 /*****************************************************************************
|
|
401 * _win32_dvdcss_readv: vectored read using ReadFile for Win2K and
|
|
402 * _win32_dvdcss_aread for win9x
|
|
403 *****************************************************************************/
|
|
404 int _win32_dvdcss_readv( int i_fd, struct iovec *p_iovec,
|
|
405 int i_num_buffers, char *p_tmp_buffer )
|
|
406 {
|
|
407 int i_index;
|
|
408 int i_blocks, i_blocks_total = 0;
|
|
409
|
|
410 for( i_index = i_num_buffers; i_index; i_index-- )
|
|
411 {
|
|
412 i_blocks_total += p_iovec[i_index-1].iov_len;
|
|
413 }
|
|
414
|
|
415 if( i_blocks_total <= 0 ) return 0;
|
|
416
|
|
417 i_blocks_total /= DVDCSS_BLOCK_SIZE;
|
|
418
|
|
419 if( WIN2K )
|
|
420 {
|
|
421 unsigned long int i_bytes;
|
|
422 if( !ReadFile( (HANDLE)i_fd, p_tmp_buffer,
|
|
423 i_blocks_total * DVDCSS_BLOCK_SIZE, &i_bytes, NULL ) )
|
|
424 {
|
|
425 return -1;
|
|
426 /* The read failed... too bad.
|
|
427 As in the posix spec the file postition is left
|
|
428 unspecified after a failure */
|
|
429 }
|
|
430 i_blocks = i_bytes / DVDCSS_BLOCK_SIZE;
|
|
431 }
|
|
432 else /* Win9x */
|
|
433 {
|
|
434 i_blocks = _win32_dvdcss_aread( i_fd, p_tmp_buffer, i_blocks_total );
|
|
435 if( i_blocks < 0 )
|
|
436 {
|
|
437 return -1; /* idem */
|
|
438 }
|
|
439 }
|
|
440
|
|
441 /* We just have to copy the content of the temp buffer into the iovecs */
|
|
442 i_index = 0;
|
|
443 i_blocks_total = i_blocks;
|
|
444 while( i_blocks_total > 0 )
|
|
445 {
|
|
446 memcpy( p_iovec[i_index].iov_base,
|
|
447 &p_tmp_buffer[(i_blocks - i_blocks_total) * DVDCSS_BLOCK_SIZE],
|
|
448 p_iovec[i_index].iov_len );
|
|
449 /* if we read less blocks than asked, we'll just end up copying
|
|
450 garbage, this isn't an issue as we return the number of
|
|
451 blocks actually read */
|
|
452 i_blocks_total -= ( p_iovec[i_index].iov_len / DVDCSS_BLOCK_SIZE );
|
|
453 i_index++;
|
|
454 }
|
|
455
|
|
456 return i_blocks;
|
|
457 }
|
|
458
|
|
459 /*****************************************************************************
|
|
460 * _win32_dvdcss_aopen: open dvd drive (load aspi and init w32_aspidev
|
|
461 * structure)
|
|
462 *****************************************************************************/
|
|
463 int _win32_dvdcss_aopen( dvdcss_t dvdcss, char c_drive )
|
|
464 {
|
|
465 HMODULE hASPI;
|
|
466 DWORD dwSupportInfo;
|
|
467 struct w32_aspidev *fd;
|
|
468 int i, j, i_hostadapters;
|
|
469 long (*lpGetSupport)( void );
|
|
470 long (*lpSendCommand)( void* );
|
|
471
|
|
472 hASPI = LoadLibrary( "wnaspi32.dll" );
|
|
473 if( hASPI == NULL )
|
|
474 {
|
|
475 _dvdcss_error( dvdcss, "unable to load wnaspi32.dll" );
|
|
476 return -1;
|
|
477 }
|
|
478
|
|
479 (FARPROC) lpGetSupport = GetProcAddress( hASPI, "GetASPI32SupportInfo" );
|
|
480 (FARPROC) lpSendCommand = GetProcAddress( hASPI, "SendASPI32Command" );
|
|
481
|
|
482 if(lpGetSupport == NULL || lpSendCommand == NULL )
|
|
483 {
|
|
484 _dvdcss_debug( dvdcss, "unable to get aspi function pointers" );
|
|
485 FreeLibrary( hASPI );
|
|
486 return -1;
|
|
487 }
|
|
488
|
|
489 dwSupportInfo = lpGetSupport();
|
|
490
|
|
491 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
|
|
492 {
|
|
493 _dvdcss_debug( dvdcss, "no host adapters found (aspi)" );
|
|
494 FreeLibrary( hASPI );
|
|
495 return -1;
|
|
496 }
|
|
497
|
|
498 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
|
|
499 {
|
|
500 _dvdcss_error( dvdcss, "unable to initalize aspi layer" );
|
|
501 FreeLibrary( hASPI );
|
|
502 return -1;
|
|
503 }
|
|
504
|
|
505 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
|
|
506 if( i_hostadapters == 0 )
|
|
507 {
|
|
508 FreeLibrary( hASPI );
|
|
509 return -1;
|
|
510 }
|
|
511
|
|
512 fd = malloc( sizeof( struct w32_aspidev ) );
|
|
513 if( fd == NULL )
|
|
514 {
|
|
515 FreeLibrary( hASPI );
|
|
516 return -1;
|
|
517 }
|
|
518
|
|
519 fd->i_blocks = 0;
|
|
520 fd->hASPI = (long) hASPI;
|
|
521 fd->lpSendCommand = lpSendCommand;
|
|
522
|
|
523 c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
|
|
524
|
|
525 for( i = 0; i < i_hostadapters; i++ )
|
|
526 {
|
|
527 for( j = 0; j < 15; j++ )
|
|
528 {
|
|
529 struct SRB_GetDiskInfo srbDiskInfo;
|
|
530
|
|
531 srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
|
|
532 srbDiskInfo.SRB_HaId = i;
|
|
533 srbDiskInfo.SRB_Flags = 0;
|
|
534 srbDiskInfo.SRB_Hdr_Rsvd = 0;
|
|
535 srbDiskInfo.SRB_Target = j;
|
|
536 srbDiskInfo.SRB_Lun = 0;
|
|
537
|
|
538 lpSendCommand( (void*) &srbDiskInfo );
|
|
539
|
|
540 if( (srbDiskInfo.SRB_Status == SS_COMP) &&
|
|
541 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
|
|
542 {
|
|
543 fd->i_sid = MAKEWORD( i, j );
|
|
544 return (int) fd;
|
|
545 }
|
|
546 }
|
|
547 }
|
|
548
|
|
549 free( (void*) fd );
|
|
550 FreeLibrary( hASPI );
|
|
551 _dvdcss_debug( dvdcss, "unable to get haid and target (aspi)" );
|
|
552 return( -1 );
|
|
553 }
|
|
554
|
|
555 /*****************************************************************************
|
|
556 * _win32_dvdcss_aclose: close dvd drive (unload aspi and free w32_aspidev
|
|
557 * structure)
|
|
558 *****************************************************************************/
|
|
559 int _win32_dvdcss_aclose( int i_fd )
|
|
560 {
|
|
561 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
|
|
562
|
|
563 FreeLibrary( (HMODULE) fd->hASPI );
|
|
564 free( (void*) i_fd );
|
|
565
|
|
566 return 0;
|
|
567 }
|
|
568
|
|
569 /*****************************************************************************
|
|
570 * _win32_dvdcss_aseek: aspi version of _dvdcss_seek
|
|
571 *
|
|
572 * returns the number of blocks read.
|
|
573 *****************************************************************************/
|
|
574 int _win32_dvdcss_aseek( int i_fd, int i_blocks, int i_method )
|
|
575 {
|
|
576 int i_old_blocks;
|
|
577 char sz_buf[ DVDCSS_BLOCK_SIZE ];
|
|
578 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
|
|
579
|
|
580 i_old_blocks = fd->i_blocks;
|
|
581 fd->i_blocks = i_blocks;
|
|
582
|
|
583 if( _win32_dvdcss_aread( i_fd, sz_buf, 1 ) == -1 )
|
|
584 {
|
|
585 fd->i_blocks = i_old_blocks;
|
|
586 return -1;
|
|
587 }
|
|
588
|
|
589 (fd->i_blocks)--;
|
|
590
|
|
591 return fd->i_blocks;
|
|
592 }
|
|
593
|
|
594 /*****************************************************************************
|
|
595 * _win32_dvdcss_aread: aspi version of _dvdcss_read
|
|
596 *
|
|
597 * returns the number of blocks read.
|
|
598 *****************************************************************************/
|
|
599 int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks )
|
|
600 {
|
|
601 HANDLE hEvent;
|
|
602 struct SRB_ExecSCSICmd ssc;
|
|
603 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
|
|
604
|
|
605 /* Create the transfer completion event */
|
|
606 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
607 if( hEvent == NULL )
|
|
608 {
|
|
609 return -1;
|
|
610 }
|
|
611
|
|
612 memset( &ssc, 0, sizeof( ssc ) );
|
|
613
|
|
614 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
|
|
615 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
|
|
616 ssc.SRB_HaId = LOBYTE( fd->i_sid );
|
|
617 ssc.SRB_Target = HIBYTE( fd->i_sid );
|
|
618 ssc.SRB_SenseLen = SENSE_LEN;
|
|
619
|
|
620 ssc.SRB_PostProc = (LPVOID) hEvent;
|
|
621 ssc.SRB_BufPointer = p_data;
|
|
622 ssc.SRB_CDBLen = 12;
|
|
623
|
|
624 ssc.CDBByte[0] = 0xA8; /* RAW */
|
|
625 ssc.CDBByte[2] = (UCHAR) (fd->i_blocks >> 24);
|
|
626 ssc.CDBByte[3] = (UCHAR) (fd->i_blocks >> 16) & 0xff;
|
|
627 ssc.CDBByte[4] = (UCHAR) (fd->i_blocks >> 8) & 0xff;
|
|
628 ssc.CDBByte[5] = (UCHAR) (fd->i_blocks) & 0xff;
|
|
629
|
|
630 /* We have to break down the reads into 64kb pieces (ASPI restriction) */
|
|
631 if( i_blocks > 32 )
|
|
632 {
|
|
633 ssc.SRB_BufLen = 32 * DVDCSS_BLOCK_SIZE;
|
|
634 ssc.CDBByte[9] = 32;
|
|
635 fd->i_blocks += 32;
|
|
636
|
|
637 /* Initiate transfer */
|
|
638 ResetEvent( hEvent );
|
|
639 fd->lpSendCommand( (void*) &ssc );
|
|
640
|
|
641 /* transfer the next 64kb (_win32_dvdcss_aread is called recursively)
|
|
642 * We need to check the status of the read on return */
|
|
643 if( _win32_dvdcss_aread( i_fd, (u8*) p_data + 32 * DVDCSS_BLOCK_SIZE,
|
|
644 i_blocks - 32) < 0 )
|
|
645 {
|
|
646 return -1;
|
|
647 }
|
|
648 }
|
|
649 else
|
|
650 {
|
|
651 /* This is the last transfer */
|
|
652 ssc.SRB_BufLen = i_blocks * DVDCSS_BLOCK_SIZE;
|
|
653 ssc.CDBByte[9] = (UCHAR) i_blocks;
|
|
654 fd->i_blocks += i_blocks;
|
|
655
|
|
656 /* Initiate transfer */
|
|
657 ResetEvent( hEvent );
|
|
658 fd->lpSendCommand( (void*) &ssc );
|
|
659
|
|
660 }
|
|
661
|
|
662 /* If the command has still not been processed, wait until it's finished */
|
|
663 if( ssc.SRB_Status == SS_PENDING )
|
|
664 {
|
|
665 WaitForSingleObject( hEvent, INFINITE );
|
|
666 }
|
|
667 CloseHandle( hEvent );
|
|
668
|
|
669 /* check that the transfer went as planned */
|
|
670 if( ssc.SRB_Status != SS_COMP )
|
|
671 {
|
|
672 return -1;
|
|
673 }
|
|
674
|
|
675 return i_blocks;
|
|
676 }
|
|
677
|
|
678 #endif
|
|
679
|