summaryrefslogtreecommitdiff
path: root/lib/driver/MSWindows/win32.c
blob: b15afef64c1c710f9d9accad12e92bd71d3f110d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
/*
  Copyright (C) 2003, 2004, 2005, 2006, 2008, 2010, 2011, 2012
  Rocky Bernstein <rocky@gnu.org>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* This file contains Win32-specific code and implements low-level 
   control of the CD drive. Inspired by vlc's cdrom.h code 
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#endif 

#include <cdio/cdio.h>
#include <cdio/sector.h>
#include <cdio/util.h>
#include <cdio/mmc.h>
#include <cdio/logging.h>
#include "cdio_assert.h"
#include "cdio_private.h" /* protoype for cdio_is_device_win32 */

#include <string.h>

#ifdef HAVE_WIN32_CDROM

#include <ctype.h>
#include <stdio.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#include <windows.h>
#include <winioctl.h>
#include "win32.h"

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#if defined (_MSC_VER) || defined (_XBOX)
#undef IN
#else
#include "aspi32.h"
#endif

#ifdef _XBOX
#include "stdint.h"
#include <xtl.h>
#define WIN_NT 1
#else
#define WIN_NT               ( GetVersion() < 0x80000000 )
#endif

/*!
  Set the volume of an audio CD.
  
  @param p_cdio the CD object to be acted upon.
  
*/
static driver_return_code_t
audio_get_volume_win32 ( void *p_user_data, 
			 /*out*/ cdio_audio_volume_t *p_volume)
{
  if ( WIN_NT ) {
    return audio_get_volume_win32ioctl (p_user_data, p_volume);
  } else {
    return DRIVER_OP_UNSUPPORTED; /* not yet, but soon I hope */
  }
}

/*!
  Pause playing CD through analog output
  
  @param p_cdio the CD object to be acted upon.
*/
static driver_return_code_t 
audio_pause_win32 (void *p_user_data)
{
  if ( WIN_NT ) {
    return audio_pause_win32ioctl (p_user_data);
  } else {
    return DRIVER_OP_UNSUPPORTED; /* not yet, but soon I hope */
  }
}

/*!
  Playing CD through analog output at the given MSF.
  
  @param p_cdio the CD object to be acted upon.
*/
static driver_return_code_t 
audio_play_msf_win32 (void *p_user_data, msf_t *p_start_msf, msf_t *p_end_msf)
{
  if ( WIN_NT ) {
    return audio_play_msf_win32ioctl (p_user_data, p_start_msf, p_end_msf);
  } else {
    return DRIVER_OP_UNSUPPORTED; /* not yet, but soon I hope */
  }
}

/*!
  Read Audio Subchannel information
  
  @param p_cdio the CD object to be acted upon.
  
*/
static driver_return_code_t
audio_read_subchannel_win32 (void *p_user_data, 
			     cdio_subchannel_t *p_subchannel)
{
  if ( WIN_NT ) {
    return audio_read_subchannel_win32ioctl (p_user_data, p_subchannel);
  } else {
    return audio_read_subchannel_mmc(p_user_data, p_subchannel);
  }
}

  /*!
    Resume playing an audio CD.
    
    @param p_cdio the CD object to be acted upon.

  */
static driver_return_code_t 
audio_resume_win32 (void *p_user_data)
{
  if ( WIN_NT ) {
    return audio_resume_win32ioctl (p_user_data);
  } else {
    return DRIVER_OP_UNSUPPORTED; /* not yet, but soon I hope */
  }
}

/*!
  Set the volume of an audio CD.
  
  @param p_cdio the CD object to be acted upon.
  
*/
static driver_return_code_t
audio_set_volume_win32 ( void *p_user_data, cdio_audio_volume_t *p_volume)
{
  if ( WIN_NT ) {
    return audio_set_volume_win32ioctl (p_user_data, p_volume);
  } else {
    return DRIVER_OP_UNSUPPORTED; /* not yet, but soon I hope */
  }
}

static driver_return_code_t
audio_stop_win32 ( void *p_user_data)
{
  if ( WIN_NT ) {
    return audio_stop_win32ioctl (p_user_data);
  } else {
    return DRIVER_OP_UNSUPPORTED; /* not yet, but soon I hope */
  }
}

/* General ioctl() CD-ROM command function */
static bool 
_cdio_mciSendCommand(int id, UINT msg, DWORD flags, void *arg)
{
#ifdef _XBOX
  return false;
#else
  MCIERROR mci_error;
  
  mci_error = mciSendCommand(id, msg, flags, (DWORD_PTR)arg);
  if ( mci_error ) {
    char error[256];
    
    mciGetErrorString(mci_error, error, 256);
    cdio_warn("mciSendCommand() error: %s", error);
  }
  return(mci_error == 0);
#endif
}

static access_mode_t 
str_to_access_mode_win32(const char *psz_access_mode) 
{
  const access_mode_t default_access_mode = 
    WIN_NT ? _AM_IOCTL : _AM_ASPI;

  if (NULL==psz_access_mode) return default_access_mode;
  
  if (!strcmp(psz_access_mode, "ioctl"))
    return _AM_IOCTL;
  else if (!strcmp(psz_access_mode, "ASPI")) {
#ifdef _XBOX
    cdio_warn ("XBOX doesn't support access type: %s. Default used instead.", 
	       psz_access_mode);
    return default_access_mode;
#else 
    return _AM_ASPI;
#endif    
  } else if (!strcmp(psz_access_mode, "MMC_RDWR")) {
    return _AM_MMC_RDWR;
  } else if (!strcmp(psz_access_mode, "MMC_RDWR_EXCL")) {
    return _AM_MMC_RDWR_EXCL;
  } else {
    cdio_warn ("unknown access type: %s. Default used instead.", 
	       psz_access_mode);
    return default_access_mode;
  }
}

static discmode_t
get_discmode_win32(void *p_user_data) 
{
  _img_private_t *p_env = p_user_data;

  if (p_env->hASPI) {
    return get_discmode_aspi (p_env);
  } else {
    return get_discmode_win32ioctl (p_env);
  }
}

static const char *
is_cdrom_win32(const char drive_letter) {
  if ( WIN_NT ) {
    return is_cdrom_win32ioctl (drive_letter);
  } else {
    return is_cdrom_aspi(drive_letter);
  }
}

/*!
  Run a SCSI MMC command. 
 
  env	        private CD structure 
  i_timeout_ms  time in milliseconds we will wait for the command
                to complete. If this value is -1, use the default 
		time-out value.
  p_buf	        Buffer for data, both sending and receiving
  i_buf	        Size of buffer
  e_direction	direction the transfer is to go.
  cdb	        CDB bytes. All values that are needed should be set on 
                input. We'll figure out what the right CDB length should be.

  Return 0 if command completed successfully.
 */
static int
run_mmc_cmd_win32( void *p_user_data, unsigned int i_timeout_ms,
		   unsigned int i_cdb, const mmc_cdb_t *p_cdb, 
		   cdio_mmc_direction_t e_direction, 
		   unsigned int i_buf, /*in/out*/ void *p_buf )
{
  _img_private_t *p_env = p_user_data;

  if (p_env->hASPI) {
    return run_mmc_cmd_aspi( p_env, i_timeout_ms, i_cdb, p_cdb,  
			     e_direction, i_buf, p_buf );
  } else {
    return run_mmc_cmd_win32ioctl( p_env, i_timeout_ms, i_cdb, p_cdb,  
				   e_direction, i_buf, p_buf );
  }
}

/*!
  Initialize CD device.
 */
static bool
init_win32 (void *p_user_data)
{
  _img_private_t *p_env = p_user_data;
  bool b_ret;
  if (p_env->gen.init) {
    cdio_error ("init called more than once");
    return false;
  }
  
  p_env->gen.init           = true;
  p_env->gen.toc_init       = false;
  p_env->gen.b_cdtext_error = false;
  p_env->gen.fd             = open (p_env->gen.source_name, O_RDONLY, 0);

  /* Initializations */
  p_env->h_device_handle = NULL;
  p_env->i_sid           = 0;
  p_env->hASPI           = 0;
  p_env->lpSendCommand   = 0;
  p_env->b_aspi_init     = false;
  p_env->b_ioctl_init    = false;


  switch (p_env->access_mode) {
  case _AM_IOCTL:
  case _AM_MMC_RDWR:
  case _AM_MMC_RDWR_EXCL:
    b_ret = init_win32ioctl(p_env);
    break;
  case _AM_ASPI:
    b_ret = init_aspi(p_env);
    break;
  default:
    return 0;
  }

  /* It looks like get_media_changed_mmc will always
     return 1 (media changed) on the first call. So 
     we call it here to clear that flag. We may have
     to rethink this if there's a problem doing this
     extra work down the line. */
  get_media_changed_mmc(p_user_data);
  return b_ret;
}

/*!
  Release and free resources associated with cd. 
 */
static void
free_win32 (void *p_user_data)
{
  _img_private_t *p_env = p_user_data;

  if (NULL == p_env) return;
  if (p_env->gen.fd >= 0) close(p_env->gen.fd);

  free (p_env->gen.source_name);

  if( p_env->h_device_handle )
    CloseHandle( p_env->h_device_handle );
  if( p_env->hASPI )
    FreeLibrary( p_env->hASPI );

  free (p_env);
}

/*!
   Reads an audio device into data starting from lsn.
   Returns 0 if no error. 
 */
static int
read_audio_sectors (void *p_user_data, void *p_buf, lsn_t i_lsn, 
		    unsigned int i_blocks) 
{
  _img_private_t *p_env = p_user_data;
  if ( p_env->hASPI ) {
    return read_audio_sectors_aspi( p_env, p_buf, i_lsn, i_blocks );
  } else {
#if 0
    return read_audio_sectors_win32ioctl( p_env, p_buf, i_lsn, i_blocks );
#else
    return mmc_read_sectors( p_env->gen.cdio, p_buf, i_lsn,
                                  CDIO_MMC_READ_TYPE_CDDA, i_blocks);
#endif
  }
}

/*!
   Reads an audio device into data starting from lsn.
   Returns 0 if no error. 
 */
static driver_return_code_t
read_data_sectors_win32 (void *p_user_data, void *p_buf, lsn_t i_lsn, 
			 uint16_t i_blocksize, uint32_t i_blocks) 
{
  driver_return_code_t rc =
    read_data_sectors_mmc(p_user_data, p_buf, i_lsn, i_blocksize, i_blocks);
  if ( DRIVER_OP_SUCCESS != rc  ) {
    /* Try using generic "cooked" mode. */
    return read_data_sectors_generic( p_user_data, p_buf, i_lsn, 
				      i_blocksize, i_blocks );
  } 
  return DRIVER_OP_SUCCESS;
}

/*!
   Reads a single mode1 sector from cd device into data starting from
   lsn. Returns 0 if no error.
 */
static int
read_mode1_sector_win32 (void *p_user_data, void *p_buf, lsn_t lsn, 
			 bool b_form2)
{
  _img_private_t *p_env = p_user_data;

  if (p_env->gen.ioctls_debugged == 75)
    cdio_debug ("only displaying every 75th ioctl from now on");

  if (p_env->gen.ioctls_debugged == 30 * 75)
    cdio_debug ("only displaying every 30*75th ioctl from now on");
  
  if (p_env->gen.ioctls_debugged < 75 
      || (p_env->gen.ioctls_debugged < (30 * 75)  
	  && p_env->gen.ioctls_debugged % 75 == 0)
      || p_env->gen.ioctls_debugged % (30 * 75) == 0)
    cdio_debug ("reading %lu", (unsigned long int) lsn);
  
  p_env->gen.ioctls_debugged++;

  if ( p_env->hASPI ) {
    return read_mode1_sector_aspi( p_env, p_buf, lsn, b_form2 );
  } else {
    return read_mode1_sector_win32ioctl( p_env, p_buf, lsn, b_form2 );
  }
}

/*!
   Reads nblocks of mode1 sectors from cd device into data starting
   from lsn.
   Returns 0 if no error. 
 */
static int
read_mode1_sectors_win32 (void *p_user_data, void *p_buf, lsn_t lsn, 
			  bool b_form2, unsigned int nblocks)
{
  _img_private_t *p_env = p_user_data;
  int i;
  int retval;

  for (i = 0; i < nblocks; i++) {
    if (b_form2) {
      retval = read_mode1_sector_win32 (p_env, ((char *)p_buf) + 
					(M2RAW_SECTOR_SIZE * i),
					lsn + i, true);
      if ( retval ) return retval;
    } else {
      char buf[M2RAW_SECTOR_SIZE] = { 0, };
      if ( (retval = read_mode1_sector_win32 (p_env, buf, lsn + i, false)) )
	return retval;
      
      memcpy (((char *)p_buf) + (CDIO_CD_FRAMESIZE * i), 
	      buf, CDIO_CD_FRAMESIZE);
    }
  }
  return 0;
}

/*!
   Reads a single mode2 sector from cd device into data starting
   from lsn. Returns 0 if no error. 
 */
static int
read_mode2_sector_win32 (void *p_user_data, void *data, lsn_t lsn, 
			 bool b_form2)
{
  char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
  _img_private_t *p_env = p_user_data;

  if (p_env->gen.ioctls_debugged == 75)
    cdio_debug ("only displaying every 75th ioctl from now on");

  if (p_env->gen.ioctls_debugged == 30 * 75)
    cdio_debug ("only displaying every 30*75th ioctl from now on");
  
  if (p_env->gen.ioctls_debugged < 75 
      || (p_env->gen.ioctls_debugged < (30 * 75)  
	  && p_env->gen.ioctls_debugged % 75 == 0)
      || p_env->gen.ioctls_debugged % (30 * 75) == 0)
    cdio_debug ("reading %lu", (unsigned long int) lsn);
  
  p_env->gen.ioctls_debugged++;

  if ( p_env->hASPI ) {
    int ret;
    ret = read_mode2_sector_aspi(p_user_data, buf, lsn, 1);
    if( ret != 0 ) return ret;
    if (b_form2)
      memcpy (data, buf, M2RAW_SECTOR_SIZE);
    else
      memcpy (((char *)data), buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
    return 0;
  } else {
    return read_mode2_sector_win32ioctl( p_env, data, lsn, b_form2 );
  }
}

/*!
   Reads nblocks of mode2 sectors from cd device into data starting
   from lsn.
   Returns 0 if no error. 
 */
static int
read_mode2_sectors_win32 (void *p_user_data, void *data, lsn_t lsn, 
			  bool b_form2, unsigned int i_blocks)
{
  int i;
  int retval;
  unsigned int blocksize = b_form2 ? M2RAW_SECTOR_SIZE : CDIO_CD_FRAMESIZE;

  for (i = 0; i < i_blocks; i++) {
    if ( (retval = read_mode2_sector_win32 (p_user_data, 
					    ((char *)data) + (blocksize * i),
					    lsn + i, b_form2)) )
      return retval;
  }
  return 0;
}

/*!
   Return the size of the CD in logical block address (LBA) units.
 */
static lsn_t
get_disc_last_lsn_win32 (void *p_user_data)
{
  _img_private_t *p_env = p_user_data;

  return p_env->tocent[p_env->gen.i_tracks].start_lsn;
}

/*!
  Set the key "arg" to "value" in source device.
*/
static int
set_arg_win32 (void *p_user_data, const char key[], const char value[])
{
  _img_private_t *p_env = p_user_data;

  if (!strcmp (key, "source"))
    {
      if (!value)
	return -2;

      free (p_env->gen.source_name);
      
      p_env->gen.source_name = strdup (value);
    }
  else if (!strcmp (key, "access-mode"))
    {
      p_env->access_mode = str_to_access_mode_win32(value);
      if (p_env->access_mode == _AM_ASPI && !p_env->b_aspi_init) 
	return init_aspi(p_env) ? 1 : -3;
      else if (p_env->access_mode == _AM_IOCTL && !p_env->b_ioctl_init) 
	return init_win32ioctl(p_env) ? 1 : -3;
      else
	return -4;
      return 0;
    }
  else 
    return -1;

  return 0;
}

/*! 
  Read and cache the CD's Track Table of Contents and track info.
  Return true if successful or false if an error.
*/
static bool
read_toc_win32 (void *p_user_data) 
{
  _img_private_t *p_env = p_user_data;
  bool ret;
  if( p_env->hASPI ) {
    ret = read_toc_aspi( p_env );
  } else {
    ret = read_toc_win32ioctl( p_env );
  }
  if (ret) p_env->gen.toc_init = true ;
  return ret;
}

/*!
  Close media tray.
 */
static driver_return_code_t
open_close_media_win32 (const char *psz_win32_drive, DWORD command_flags) 
{
#ifdef _XBOX
  return DRIVER_OP_UNSUPPORTED;
#else
  MCI_OPEN_PARMS op;
  DWORD i_flags;
  int ret;
    
  memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
  op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
  op.lpstrElementName = psz_win32_drive;
  
  /* Set the flags for the device type */
  i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
    MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
  
  if( _cdio_mciSendCommand( 0, MCI_OPEN, i_flags, &op ) ) {
    /* Eject disc */
    ret = _cdio_mciSendCommand( op.wDeviceID, MCI_SET, command_flags, 0 ) == 0;
    /* Release access to the device */
    _cdio_mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
  } else 
    ret = DRIVER_OP_ERROR;
  
  return ret;
#endif
}

/*!
  Eject media.
 */
static driver_return_code_t
eject_media_win32 (void *p_user_data) 
{
  const _img_private_t *p_env = p_user_data;
  char psz_drive[4];
  unsigned int i_device = strlen(p_env->gen.source_name);
    
  strcpy( psz_drive, "X:" );
  if (6 == i_device) {
    psz_drive[0] = p_env->gen.source_name[4];
  } else if (2 == i_device) {
    psz_drive[0] = p_env->gen.source_name[0];
  } else {
    cdio_info ("Can't pick out drive letter from device %s", 
	       p_env->gen.source_name);
    return DRIVER_OP_ERROR;
  }
  
  return open_close_media_win32(psz_drive, MCI_SET_DOOR_OPEN);
}

static bool
is_mmc_supported(void *user_data)
{
    _img_private_t *env = user_data;
    switch (env->access_mode) {
      case _AM_NONE:
	return false;
      case _AM_IOCTL:
      case _AM_ASPI:
      case _AM_MMC_RDWR:
      case _AM_MMC_RDWR_EXCL:
	return true;
    }
    /* Not reached. */
    return false;
}

/*!
  Return the value associated with the key "arg".
*/
static const char *
get_arg_win32 (void *p_user_data, const char key[])
{
  _img_private_t *p_env = p_user_data;

  if (!strcmp (key, "source")) {
    return p_env->gen.source_name;
  } else if (!strcmp (key, "access-mode")) {
    switch (p_env->access_mode) {
    case _AM_IOCTL:
      return "ioctl";
    case _AM_ASPI:
      return "ASPI";
    case _AM_MMC_RDWR:
      return "MMC_RDWR";
    case _AM_MMC_RDWR_EXCL:
      return "MMC_RDWR_EXCL";
    case _AM_NONE:
      return "no access method";
    }
  } else if (!strcmp (key, "scsi-tuple")) {
    return p_env->gen.scsi_tuple;
  } else if (!strcmp (key, "mmc-supported?")) {
      return is_mmc_supported(p_user_data) ? "true" : "false";
  }
  return NULL;
}

/*!
  Return the media catalog number MCN.

  Note: string is malloc'd so caller should free() then returned
  string when done with it.

 */
static char *
_cdio_get_mcn (const void *p_user_data) {
  const _img_private_t *p_env = p_user_data;

  if( p_env->hASPI ) {
    return mmc_get_mcn( p_env->gen.cdio );
  } else {
    return get_mcn_win32ioctl(p_env);
  }
}

/*!  
  Get format of track. 
*/
static track_format_t
_cdio_get_track_format(void *p_obj, track_t i_track) 
{
  _img_private_t *p_env = p_obj;
  
  if ( !p_env ) return TRACK_FORMAT_ERROR;
  
  if (!p_env->gen.toc_init) 
    if (!read_toc_win32 (p_env)) return TRACK_FORMAT_ERROR;

  if ( i_track < p_env->gen.i_first_track
       || i_track >= p_env->gen.i_tracks + p_env->gen.i_first_track )
    return TRACK_FORMAT_ERROR;

  if( p_env->hASPI ) {
    return get_track_format_aspi(p_env, i_track);
  } else {
    return get_track_format_win32ioctl(p_env, i_track);
  }
}

/*!
  Return true if we have XA data (green, mode2 form1) or
  XA data (green, mode2 form2). That is track begins:
  sync - header - subheader
  12     4      -  8

  FIXME: there's gotta be a better design for this and get_track_format?
*/
static bool
_cdio_get_track_green(void *p_obj, track_t i_track) 
{
  _img_private_t *p_env = p_obj;
  
  switch (_cdio_get_track_format(p_env, i_track)) {
  case TRACK_FORMAT_XA:
    return true;
  case TRACK_FORMAT_ERROR:
  case TRACK_FORMAT_CDI:
  case TRACK_FORMAT_AUDIO:
    return false;
  case TRACK_FORMAT_DATA:
    if (_AM_ASPI == p_env->access_mode ) 
      return ((p_env->tocent[i_track-p_env->gen.i_first_track].Control & 8) != 0);
  default:
    break;
  }

  /* FIXME: Dunno if this is the right way, but it's what 
     I was using in cd-info for a while.
   */
  return ((p_env->tocent[i_track-p_env->gen.i_first_track].Control & 2) != 0);
}

/*!  
  Return the starting MSF (minutes/secs/frames) for track number
  i_tracks in obj.  Track numbers start at 1.
  The "leadout" track is specified either by
  using i_tracks LEADOUT_TRACK or the total tracks+1.
  False is returned if there is no track entry.
*/
static bool
_cdio_get_track_msf(void *p_user_data, track_t i_tracks, msf_t *p_msf)
{
  _img_private_t *p_env = p_user_data;

  if (!p_msf) return false;

  if (!p_env->gen.toc_init) 
    if (!read_toc_win32 (p_env)) return false;

  if (i_tracks == CDIO_CDROM_LEADOUT_TRACK) i_tracks = p_env->gen.i_tracks+1;

  if (i_tracks > p_env->gen.i_tracks+1 || i_tracks == 0) {
    return false;
  } else {
    cdio_lsn_to_msf(p_env->tocent[i_tracks-1].start_lsn, p_msf);
    return true;
  }
}

#endif /* HAVE_WIN32_CDROM */

/*!
  Return an array of strings giving possible CD devices.
 */
char **
cdio_get_devices_win32 (void)
{
#ifndef HAVE_WIN32_CDROM
  return NULL;
#else
  char **drives = NULL;
  unsigned int num_drives=0;
  char drive_letter;
  
  /* Scan the system for CD-ROM drives.
  */

#if FINISHED
  /* Now check the currently mounted CD drives */
  if (NULL != (ret_drive = cdio_check_mounts("/etc/mtab"))) {
    cdio_add_device_list(&drives, drive, &num_drives);
  }
  
  /* Finally check possible mountable drives in /etc/fstab */
  if (NULL != (ret_drive = cdio_check_mounts("/etc/fstab"))) {
    cdio_add_device_list(&drives, drive, &num_drives);
  }
#endif

  /* Scan the system for CD-ROM drives.
     Not always 100% reliable, so use the USE_MNTENT code above first.
  */
  for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
    const char *drive_str=is_cdrom_win32(drive_letter);
    if (drive_str != NULL) {
      cdio_add_device_list(&drives, drive_str, &num_drives);
    }
  }
  cdio_add_device_list(&drives, NULL, &num_drives);
  return drives;
#endif /*HAVE_WIN32_CDROM*/
}

/*!
  Return a string containing the default CD device if none is specified.
  if CdIo is NULL (we haven't initialized a specific device driver), 
  then find a suitable one and return the default device for that.
  
  NULL is returned if we couldn't get a default device.
*/
char *
cdio_get_default_device_win32(void)
{

#ifdef HAVE_WIN32_CDROM
  char drive_letter;

  for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
    const char *drive_str=is_cdrom_win32(drive_letter);
    if (drive_str != NULL) {
      return strdup(drive_str);
    }
  }
#endif
  return NULL;
}

/*!  
  Return true if source_name could be a device containing a CD-ROM and
  we are on a MS Windows platform.
*/
bool
cdio_is_device_win32(const char *source_name)
{
#ifdef HAVE_WIN32_CDROM
  unsigned int len;

  if (NULL == source_name) return false;
  len = strlen(source_name);

  if ((len == 2) && isalpha(source_name[0]) 
	    && (source_name[len-1] == ':'))
    return true;

  if ( ! WIN_NT ) return false;

  /* Test to see if of form: \\.\x: */
  return ( (len == 6) 
	   && source_name[0] == '\\' && source_name[1] == '\\'
	   && source_name[2] == '.'  && source_name[3] == '\\'
	   && isalpha(source_name[len-2])
	   && (source_name[len-1] == ':') );
#else 
  return false;
#endif
}

driver_return_code_t
close_tray_win32 (const char *psz_win32_drive)
{
#ifdef HAVE_WIN32_CDROM
#if 1
  return open_close_media_win32(psz_win32_drive, MCI_SET_DOOR_CLOSED|MCI_WAIT);
#else 
  if ( WIN_NT ) {
    return close_tray_win32ioctl (psz_win32_drive);
  } else {
    return DRIVER_OP_UNSUPPORTED; /* not yet, but soon I hope */
  }
#endif
#else
  return DRIVER_OP_UNSUPPORTED;
#endif /* HAVE_WIN32_CDROM*/
}

/*!
  Initialization routine. This is the only thing that doesn't
  get called via a function pointer. In fact *we* are the
  ones to set that up.
 */
CdIo_t *
cdio_open_win32 (const char *psz_source_name)
{
#ifdef HAVE_WIN32_CDROM
  if ( WIN_NT ) {
    return cdio_open_am_win32(psz_source_name, "ioctl");
  } else {
    return cdio_open_am_win32(psz_source_name, "ASPI");
  }
#else 
  return NULL;
#endif /* HAVE_WIN32_CDROM */
}

/*!
  Initialization routine. This is the only thing that doesn't
  get called via a function pointer. In fact *we* are the
  ones to set that up.
 */
CdIo_t *
cdio_open_am_win32 (const char *psz_orig_source, const char *psz_access_mode)
{

#ifdef HAVE_WIN32_CDROM
  CdIo_t *ret;
  _img_private_t *_data;
  char *psz_source;

  cdio_funcs_t _funcs;

  memset( &_funcs, 0, sizeof(_funcs) );

  _funcs.audio_get_volume       = audio_get_volume_win32;
  _funcs.audio_pause            = audio_pause_win32;
  _funcs.audio_play_msf         = audio_play_msf_win32;
#if 0
  _funcs.audio_play_track_index = audio_play_track_index_win32;
#endif
  _funcs.audio_read_subchannel  = audio_read_subchannel_win32;
  _funcs.audio_resume           = audio_resume_win32;
  _funcs.audio_set_volume       = audio_set_volume_win32;
  _funcs.audio_stop             = audio_stop_win32;
  _funcs.eject_media            = eject_media_win32;
  _funcs.free                   = free_win32;
  _funcs.get_arg                = get_arg_win32;
  _funcs.get_cdtext             = get_cdtext_generic;
  _funcs.get_cdtext_raw         = read_cdtext_generic;
  _funcs.get_default_device     = cdio_get_default_device_win32;
  _funcs.get_devices            = cdio_get_devices_win32;
  _funcs.get_disc_last_lsn      = get_disc_last_lsn_win32;
  _funcs.get_discmode           = get_discmode_win32;
  _funcs.get_drive_cap          = get_drive_cap_mmc;
  _funcs.get_first_track_num    = get_first_track_num_generic;
  _funcs.get_hwinfo             = NULL;
  _funcs.get_media_changed      = get_media_changed_mmc;
  _funcs.get_mcn                = _cdio_get_mcn;
  _funcs.get_num_tracks         = get_num_tracks_generic;
  _funcs.get_track_channels     = get_track_channels_generic,
  _funcs.get_track_copy_permit  = get_track_copy_permit_generic,
  _funcs.get_track_format       = _cdio_get_track_format;
  _funcs.get_track_green        = _cdio_get_track_green;
  _funcs.get_track_lba          = NULL; /* This could be done if need be. */
  _funcs.get_track_msf          = _cdio_get_track_msf;
  _funcs.get_track_preemphasis  = get_track_preemphasis_generic,
  _funcs.lseek                  = NULL;
  _funcs.read                   = NULL;
  _funcs.read_audio_sectors     = read_audio_sectors;
  _funcs.read_data_sectors      = read_data_sectors_win32;
  _funcs.read_mode1_sector      = read_mode1_sector_win32;
  _funcs.read_mode1_sectors     = read_mode1_sectors_win32;
  _funcs.read_mode2_sector      = read_mode2_sector_win32;
  _funcs.read_mode2_sectors     = read_mode2_sectors_win32;
  _funcs.read_toc               = read_toc_win32;
  _funcs.run_mmc_cmd            = run_mmc_cmd_win32;
  _funcs.set_arg                = set_arg_win32;
  _funcs.set_blocksize          = set_blocksize_mmc;
  _funcs.set_speed              = set_drive_speed_mmc;

  _data                 = calloc(1, sizeof (_img_private_t));
  _data->access_mode    = str_to_access_mode_win32(psz_access_mode);
  _data->gen.init       = false;
  _data->gen.fd         = -1;

  if (NULL == psz_orig_source) {
    psz_source=cdio_get_default_device_win32();
    if (NULL == psz_source) return NULL;
    set_arg_win32(_data, "source", psz_source);
    free(psz_source);
  } else {
    if (cdio_is_device_win32(psz_orig_source))
      set_arg_win32(_data, "source", psz_orig_source);
    else {
      /* The below would be okay as an info message if all device
	 drivers worked this way. */
      cdio_debug ("source %s is a not a device", psz_orig_source);
      free(_data);
      return NULL;
    }
  }

  ret = cdio_new ((void *)_data, &_funcs);
  if (ret == NULL) return NULL;

  if (init_win32(_data))
    return ret;
  else {
    free_win32 (_data);
    return NULL;
  }
  
#else 
  return NULL;
#endif /* HAVE_WIN32_CDROM */

}

bool
cdio_have_win32 (void)
{
#ifdef HAVE_WIN32_CDROM
  return true;
#else 
  return false;
#endif /* HAVE_WIN32_CDROM */
}