File: aix.c

package info (click to toggle)
libcdio 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 16,564 kB
  • sloc: ansic: 38,843; sh: 5,501; cpp: 2,555; makefile: 777; ruby: 24; perl: 24
file content (1040 lines) | stat: -rw-r--r-- 27,222 bytes parent folder | download | duplicates (4)
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
/*
  Copyright (C) 2004-2006, 2008, 2010, 2011-2013, 2017
  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/>.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

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

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <cdio/logging.h>
#include <cdio/sector.h>
#include <cdio/util.h>
#include <cdio/mmc.h>
#include "cdio_assert.h"
#include "cdio_private.h"

#define DEFAULT_CDIO_DEVICE "/dev/rcd0"

#ifdef HAVE_AIX_CDROM

#ifdef HAVE_GLOB_H
#include <glob.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/scsi.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/devinfo.h>
#include <sys/ide.h>
#include <sys/idecdrom.h>
#include <sys/scdisk.h>
#include "cdtext_private.h"

typedef struct _TRACK_DATA {
    uchar Format;
    uchar Control : 4;
    uchar Adr : 4;
    uchar TrackNumber;
    uchar Reserved1;
    uchar Address[4];
} TRACK_DATA, *PTRACK_DATA;

typedef struct _CDROM_TOC {
    uchar Length[2];
    uchar FirstTrack;
    uchar LastTrack;
    TRACK_DATA TrackData[CDIO_CD_MAX_TRACKS+1];
} CDROM_TOC, *PCDROM_TOC;


typedef struct _TRACK_DATA_FULL {
    uchar SessionNumber;
    uchar Control : 4;
    uchar Adr : 4;
    uchar TNO;
    uchar POINT;  /* Tracknumber (of session?) or lead-out/in (0xA0, 0xA1, 0xA2)  */
    uchar Min;  /* Only valid if disctype is CDDA ? */
    uchar Sec;  /* Only valid if disctype is CDDA ? */
    uchar Frame;  /* Only valid if disctype is CDDA ? */
    uchar Zero;  /* Always zero */
    uchar PMIN;  /* start min, if POINT is a track; if lead-out/in 0xA0: First Track */
    uchar PSEC;
    uchar PFRAME;
} TRACK_DATA_FULL, *PTRACK_DATA_FULL;

typedef struct _CDROM_TOC_FULL {
    uchar Length[2];
    uchar FirstSession;
    uchar LastSession;
    TRACK_DATA_FULL TrackData[CDIO_CD_MAX_TRACKS+3];
} CDROM_TOC_FULL, *PCDROM_TOC_FULL;


/* reader */

typedef  enum {
    _AM_NONE,
    _AM_CTRL_SCSI
} access_mode_t;


typedef struct {
  lsn_t          start_lsn;
  uchar          Control : 4;
  uchar          Format;
} track_info_t;

typedef struct {
  /* Things common to all drivers like this.
     This must be first. */
  generic_img_private_t gen;

  access_mode_t access_mode;

  /* Some of the more OS specific things. */
  /* Entry info for each track, add 1 for leadout. */
  track_info_t tocent[CDIO_CD_MAX_TRACKS+1];

} _img_private_t;

static track_format_t get_track_format_aix(void *p_user_data,
                                               track_t i_track);

static access_mode_t
str_to_access_mode_aix(const char *psz_access_mode)
{
  const access_mode_t default_access_mode = _AM_CTRL_SCSI;

  if (NULL==psz_access_mode) return default_access_mode;

  if (!strcmp(psz_access_mode, "SCSI"))
    return _AM_CTRL_SCSI;
  else {
    cdio_warn ("unknown access type: %s. Default SCSI used.",
               psz_access_mode);
    return default_access_mode;
  }
}


/*!
  Initialize CD device.
 */
static bool
init_aix (_img_private_t *p_env)
{

  if (p_env->gen.init) {
    cdio_warn ("init called more than once");
    return false;
  }

  p_env->gen.fd = openx (p_env->gen.source_name, O_RDONLY, NULL,
                         SC_DIAGNOSTIC);

  /*p_env->gen.fd = openx (p_env->gen.source_name, O_RDONLY, NULL,
    IDE_SINGLE);*/

  if (p_env->gen.fd < 0)
    {
      cdio_warn ("open (%s): %s", p_env->gen.source_name, strerror (errno));
      return false;
    }

  p_env->gen.init = true;
  p_env->gen.toc_init = false;
  p_env->gen.u_joliet_level = 0;  /* Assume no Joliet extensions initally */
  p_env->access_mode = _AM_CTRL_SCSI;

  return true;
}

/*!
  Run a SCSI MMC command.

  p_user_data   internal CD structure.
  i_timeout_ms   time in milliseconds we will wait for the command
                to complete.
  i_cdb         Size of p_cdb
  p_cdb         CDB bytes.
  e_direction   direction the transfer is to go.
  i_buf         Size of buffer
  p_buf         Buffer for data, both sending and receiving
 */
static driver_return_code_t
run_mmc_cmd_aix( 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 )
{
  const _img_private_t *p_env = p_user_data;
  struct sc_passthru cgc;
  int i_rc;

  memset (&cgc, 0, sizeof (cgc));
  memcpy(cgc.scsi_cdb,  p_cdb, sizeof(mmc_cdb_t));

#ifdef AIX_DISABLE_ASYNC
        /* This enables synchronous negotiation mode.  Some CD-ROM drives
         * don't handle this well.
         */
  cgc.flags = 0;
#else
  cgc.flags = SC_ASYNC;
#endif

  if (0 != i_buf)
    cgc.flags     |= MMC_DATA_READ == e_direction ? B_READ : B_WRITE;

  cgc.timeout_value = msecs2secs(i_timeout_ms);
  cgc.buffer        = p_buf;
  cgc.data_length   = i_buf;
  cgc.command_length= i_cdb;

  i_rc = ioctl(p_env->gen.fd, DK_PASSTHRU, &cgc);
  if (-1 == i_rc) {
    cdio_warn("DKIOCMD error: %s", strerror(errno));
  }
  return i_rc;
}

/*!
   Reads audio sectors from CD device into data starting from lsn.
   Returns 0 if no error.

   May have to check size of nblocks. There may be a limit that
   can be read in one go, e.g. 25 blocks.
*/

static driver_return_code_t
_read_audio_sectors_aix (void *p_user_data, void *data, lsn_t lsn,
                          unsigned int nblocks)
{
  char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };

#ifdef FINISHED
  struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
  msf_t _msf;
  struct cdrom_cdda cdda;

  _img_private_t *env = p_user_data;

  cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
  msf->cdmsf_min0   = from_bcd8(_msf.m);
  msf->cdmsf_sec0   = from_bcd8(_msf.s);
  msf->cdmsf_frame0 = from_bcd8(_msf.f);

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

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

  if (env->gen.ioctls_debugged < 75
      || (env->gen.ioctls_debugged < (30 * 75)
          && env->gen.ioctls_debugged % 75 == 0)
      || env->gen.ioctls_debugged % (30 * 75) == 0)
    cdio_debug ("reading %d", lsn);

  env->gen.ioctls_debugged++;

  cdda.cdda_addr   = lsn;
  cdda.cdda_length = nblocks;
  cdda.cdda_data   = (caddr_t) data;
  if (ioctl (env->gen.fd, CDROMCDDA, &cdda) == -1) {
    perror ("ioctl(..,CDROMCDDA,..)");
        return 1;
        /* exit (EXIT_FAILURE); */
  }
#endif
  memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW);

  return 0;
}

/*!
   Reads a single mode1 sector from cd device into data starting
   from lsn. Returns 0 if no error.
 */
static driver_return_code_t
_read_mode1_sector_aix (void *env, void *data, lsn_t lsn,
                            bool b_form2)
{

#if FIXED
  do something here.
#else
  return cdio_generic_read_form1_sector(env, data, lsn);
#endif
}

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

  for (i = 0; i < nblocks; i++) {
    if ( (retval = _read_mode1_sector_aix (p_env,
                                            ((char *)p_data) + (blocksize * i),
                                               lsn + i, b_form2)) )
      return retval;
  }
  return 0;
}

/*!
   Reads a single mode2 sector from cd device into data starting from lsn.
   Returns 0 if no error.
 */
static driver_return_code_t
_read_mode2_sector_aix (void *p_user_data, void *p_data, lsn_t lsn,
                            bool b_form2)
{
  char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
  int offset = 0;
#ifdef FINISHED
  struct cdrom_msf *msf = (struct cdrom_msf *) &buf;
  msf_t _msf;
  struct cdrom_cdxa cd_read;

  _img_private_t *p_env = p_user_data;

  cdio_lba_to_msf (cdio_lsn_to_lba(lsn), &_msf);
  msf->cdmsf_min0 = from_bcd8(_msf.m);
  msf->cdmsf_sec0 = from_bcd8(_msf.s);
  msf->cdmsf_frame0 = from_bcd8(_msf.f);

  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 %2.2d:%2.2d:%2.2d",
               msf->cdmsf_min0, msf->cdmsf_sec0, msf->cdmsf_frame0);

  p_env->gen.ioctls_debugged++;

  /* Using CDROMXA ioctl will actually use the same uscsi command
   * as ATAPI, except we don't need to be root
   */
  offset = CDIO_CD_XA_SYNC_HEADER;
  cd_read.cdxa_addr = lsn;
  cd_read.cdxa_data = buf;
  cd_read.cdxa_length = 1;
  cd_read.cdxa_format = CDROM_XA_SECTOR_DATA;

  if (ioctl (p_env->gen.fd, CDROMCDXA, &cd_read) == -1) {
    perror ("ioctl(..,CDROMCDXA,..)");
    return 1;
    /* exit (EXIT_FAILURE); */
  }
#endif

  if (b_form2)
    memcpy (p_data, buf + (offset-CDIO_CD_SUBHEADER_SIZE), M2RAW_SECTOR_SIZE);
  else
    memcpy (((char *)p_data), buf + offset, CDIO_CD_FRAMESIZE);

  return 0;
}

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

  for (i = 0; i < nblocks; i++) {
    if ( (retval = _read_mode2_sector_aix (env,
                                            ((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_aix (void *p_user_data)
{
  uint32_t i_size=0;
#ifdef FINISHED
  _img_private_t *env = p_user_data;

  struct cdrom_tocentry tocent;

  tocent.cdte_track  = CDIO_CDROM_LEADOUT_TRACK;
  tocent.cdte_format = CDIO_CDROM_LBA;
  if (ioctl (env->gen.fd, CDROMREADTOCENTRY, &tocent) == -1)
    {
      perror ("ioctl(CDROMREADTOCENTRY)");
      exit (EXIT_FAILURE);
    }

  i_size = tocent.cdte_addr.lba;
#endif

  return i_size;
}

/*!
  Set the arg "key" with "value" in the source device.
  Currently "source" and "access-mode" are valid keys.
  "source" sets the source device in I/O operations
  "access-mode" sets the the method of CD access

  0 is returned if no error was found, and nonzero if there as an error.
*/
static driver_return_code_t
_set_arg_aix (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 DRIVER_OP_ERROR;
      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_aix(key);
    }
  else return DRIVER_OP_ERROR;

  return DRIVER_OP_SUCCESS;
}

/*
 * aixioc_send
        Issue ioctl command.

  Args:
    p_env - environment
    cmd - ioctl command
    arg - ioctl argument
    b_print_err - whether an error message is to be displayed if the
    ioctl fails

  Return:
    true/false - ioctl successful
*/
static bool
aixioc_send(_img_private_t *p_env, int cmd, void *arg, bool b_print_err)
{
  struct cd_audio_cmd   *ac;

  if (p_env->gen.fd < 0)
    return false;

  if (cmd == DKAUDIO) {
    ac = (struct cd_audio_cmd *) arg;
    ac->status = 0;     /* Nuke status for audio cmds */
  }

  if (ioctl(p_env->gen.fd, cmd, arg) < 0) {
    if (b_print_err) {
      cdio_warn("errno=%d (%s)",  errno, strerror(errno));
    }
    return false;
  }
  return true;
}

/*!
  Read and cache the CD's Track Table of Contents and track info.
  via a SCSI MMC READ_TOC (FULTOC).  Return true if successful or
  false if an error.
*/
static bool
read_toc_ioctl_aix (void *p_user_data)
{
  _img_private_t *p_env = p_user_data;
  struct cd_audio_cmd   cmdbuf;
  int i;

  cmdbuf.msf_flag = false;
  cmdbuf.audio_cmds = CD_TRK_INFO_AUDIO;
  if (!aixioc_send(p_env, IDE_CDAUDIO, (void *) &cmdbuf, true))
    return false;

  p_env->gen.i_first_track = cmdbuf.indexing.track_index.first_track;
  p_env->gen.i_tracks      = ( cmdbuf.indexing.track_index.last_track
                               - p_env->gen.i_first_track ) + 1;

  /* Do it again to get the last MSF data */
  cmdbuf.msf_flag = true;
  if (!aixioc_send(p_env, IDE_CDAUDIO, (void *) &cmdbuf, true))
    return false;

  cmdbuf.audio_cmds = CD_GET_TRK_MSF;

  for (i = 0; i <= p_env->gen.i_tracks; i++) {
    int i_track = i + p_env->gen.i_first_track;

    /* Get the track info */
    cmdbuf.indexing.track_msf.track = i_track;
    if (!aixioc_send(p_env, IDE_CDAUDIO, (void *) &cmdbuf, TRUE))
      return false;

    p_env->tocent[ i_track ].start_lsn =
      cdio_msf3_to_lba(
                       cmdbuf.indexing.track_msf.mins,
                       cmdbuf.indexing.track_msf.secs,
                       cmdbuf.indexing.track_msf.frames );
  }

  return true;
}

/*!
  Read and cache the CD's Track Table of Contents and track info.
  via a SCSI MMC READ_TOC (FULTOC).  Return true if successful or
  false if an error.
*/
static bool
read_toc_aix (void *p_user_data)
{
  _img_private_t *p_env = p_user_data;
  mmc_cdb_t  cdb = {{0, }};
  CDROM_TOC_FULL  cdrom_toc_full;
  int             i_status, i, i_seen_flag;
  int             i_track_format = 0;

  /* Operation code */
  CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_TOC);

  cdb.field[1] = 0x00;

  /* Format */
  cdb.field[2] = CDIO_MMC_READTOC_FMT_FULTOC;

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

  /* Setup to read header, to get length of data */
  CDIO_MMC_SET_READ_LENGTH16(cdb.field, sizeof(cdrom_toc_full));

  i_status = run_scsi_cmd_aix (p_env, 1000*60*3,
                               mmc_get_cmd_len(cdb.field[0]),
                               &cdb, MMC_DATA_READ,
                               sizeof(cdrom_toc_full), &cdrom_toc_full);

  if ( 0 != i_status ) {
    cdio_debug ("SCSI MMC READ_TOC failed\n");
    return read_toc_ioctl_aix(p_user_data);
  }

  i_seen_flag=0;
  for( i = 0 ; i <= CDIO_CD_MAX_TRACKS+3; i++ ) {

    if ( 0xA0 == cdrom_toc_full.TrackData[i].POINT ) {
      /* First track number */
      p_env->gen.i_first_track = cdrom_toc_full.TrackData[i].PMIN;
      i_track_format = cdrom_toc_full.TrackData[i].PSEC;
      i_seen_flag|=0x01;
    }

    if ( 0xA1 == cdrom_toc_full.TrackData[i].POINT ) {
      /* Last track number */
      p_env->gen.i_tracks =
        cdrom_toc_full.TrackData[i].PMIN - p_env->gen.i_first_track + 1;
      i_seen_flag|=0x02;
    }

    if ( 0xA2 == cdrom_toc_full.TrackData[i].POINT ) {
      /* Start position of the lead out */
      p_env->tocent[ p_env->gen.i_tracks ].start_lsn =
        cdio_msf3_to_lba(
                         cdrom_toc_full.TrackData[i].PMIN,
                         cdrom_toc_full.TrackData[i].PSEC,
                         cdrom_toc_full.TrackData[i].PFRAME );
      p_env->tocent[ p_env->gen.i_tracks ].Control
        = cdrom_toc_full.TrackData[i].Control;
      p_env->tocent[ p_env->gen.i_tracks ].Format  = i_track_format;
      i_seen_flag|=0x04;
    }

    if (cdrom_toc_full.TrackData[i].POINT > 0
        && cdrom_toc_full.TrackData[i].POINT <= p_env->gen.i_tracks) {
      p_env->tocent[ cdrom_toc_full.TrackData[i].POINT - 1 ].start_lsn =
        cdio_msf3_to_lba(
                         cdrom_toc_full.TrackData[i].PMIN,
                         cdrom_toc_full.TrackData[i].PSEC,
                         cdrom_toc_full.TrackData[i].PFRAME );
      p_env->tocent[ cdrom_toc_full.TrackData[i].POINT - 1 ].Control =
        cdrom_toc_full.TrackData[i].Control;
      p_env->tocent[ cdrom_toc_full.TrackData[i].POINT - 1 ].Format  =
        i_track_format;

      cdio_debug("p_sectors: %i, %lu", i,
                 (unsigned long int) (p_env->tocent[i].start_lsn));

      if (cdrom_toc_full.TrackData[i].POINT == p_env->gen.i_tracks)
        i_seen_flag|=0x08;
    }

    if ( 0x0F == i_seen_flag ) break;
  }
  if ( 0x0F == i_seen_flag ) {
    p_env->gen.toc_init = true;
    return true;
  }
  return false;
}

/*!
  Eject media in CD drive. If successful, as a side effect we
  also free obj.
 */
static driver_return_code_t
eject_media_aix (void *p_user_data) {

  _img_private_t *p_env = p_user_data;
  driver_return_code_t ret=DRIVER_OP_SUCCESS;
  int i_status;

  if (p_env->gen.fd <= -1) return DRIVER_OP_UNINIT;
  i_status = ioctl(p_env->gen.fd, DKEJECT);
  if ( i_status != 0) {
    cdio_generic_free((void *) p_env);
    cdio_warn ("DKEJECT failed: %s", strerror(errno));
    ret = DRIVER_OP_ERROR;
  }
  close(p_env->gen.fd);
  p_env->gen.fd = -1;
  return ret;
}

#if 0
static void *
_cdio_malloc_and_zero(size_t size) {
  void *ptr;

  if( !size ) size++;

  if((ptr = malloc(size)) == NULL) {
    cdio_warn("malloc() failed: %s", strerror(errno));
    return NULL;
  }

  memset(ptr, 0, size);
  return ptr;
}
#endif

static bool
is_mmc_supported(void *user_data)
{
    _img_private_t *env = user_data;
    return (_AM_NONE == env->access_mode) ? false : true;
}

/*!
  Return the value associated with the key "arg".
*/
static const char *
get_arg_aix (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_CTRL_SCSI:
      return "SCSI";
    case _AM_NONE:
      return "no access method";
    }
  } else if (!strcmp (key, "mmc-supported?")) {
      return is_mmc_supported(env) ? "true" : "false";
  }
  return NULL;
}

/*!
  Return a string containing the default CD device if none is specified.
 */
char *
cdio_get_default_device_aix(void)
{
  return strdup(DEFAULT_CDIO_DEVICE);
}

/*!
  Get disc type associated with cd object.
*/

static discmode_t
get_discmode_aix (void *p_user_data)
{
  _img_private_t *p_env = p_user_data;
  struct mode_form_op media;
  int ret;

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

  /* Get the media info */
  media.action= CD_GET_MODE;

  if((ret = ioctl(p_env->gen.fd, DK_CD_MODE, &media)) != 0) {
     cdio_warn ("DK_CD_MODE failed: %s", strerror(errno));
         return CDIO_DISC_MODE_NO_INFO;
  }
  switch(media.cd_mode_form) {
  case CD_DA:
    return CDIO_DISC_MODE_CD_DA;
  case DVD_ROM:
    return CDIO_DISC_MODE_DVD_ROM;
  case DVD_RAM:
    return CDIO_DISC_MODE_DVD_RAM;
  case DVD_RW:
    return CDIO_DISC_MODE_DVD_RW;
  default: /* no valid match */
    return CDIO_DISC_MODE_NO_INFO;
  }
}

/*!
  Get format of track.
*/
static track_format_t
get_track_format_aix(void *p_user_data, track_t i_track)
{
  _img_private_t *p_env = p_user_data;

  if ( !p_env ) return TRACK_FORMAT_ERROR;
  if (!p_env->gen.init) init_aix(p_env);
  if (!p_env->gen.toc_init) read_toc_aix (p_user_data) ;

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

  i_track -= p_env->gen.i_first_track;

#ifdef FINISHED
  /* This is pretty much copied from the "badly broken" cdrom_count_tracks
     in linux/cdrom.c.
   */
  if (p_env->tocent[i_track].cdte_ctrl & CDROM_DATA_TRACK) {
    if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_CDI_TRACK)
      return TRACK_FORMAT_CDI;
    else if (p_env->tocent[i_track].cdte_format == CDIO_CDROM_XA_TRACK)
      return TRACK_FORMAT_XA;
    else
      return TRACK_FORMAT_DATA;
  } else
    return TRACK_FORMAT_AUDIO;
#else
  return TRACK_FORMAT_ERROR;
#endif

}

/*!
  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_user_data, track_t i_track)
{
  _img_private_t *p_env = p_user_data;

  if ( !p_env ) return false;
  if (!p_env->gen.init) init_aix(p_env);
  if (!p_env->gen.toc_init) read_toc_aix (p_env) ;

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

  i_track -= p_env->gen.i_first_track;

  /* FIXME: Dunno if this is the right way, but it's what
     I was using in cd-info for a while.
   */

#ifdef FINISHED
  return ((p_env->tocent[i_track].cdte_ctrl & 2) != 0);
#else
  return false;
#endif
}

/*!
  Return the starting MSF (minutes/secs/frames) for track number
  track_num in obj.  Track numbers usually start at something
  greater than 0, usually 1.

  The "leadout" track is specified either by
  using track_num LEADOUT_TRACK or the total tracks+1.
  False is returned if there is no entry.
*/
static bool
_cdio_get_track_msf(void *p_user_data, track_t i_track, msf_t *msf)
{
  _img_private_t *p_env = p_user_data;

  if (NULL == msf) return false;

  if (!p_env->gen.init) init_aix(p_env);
  if (!p_env->gen.toc_init) read_toc_aix (p_env) ;

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

#ifdef FINISHED
  if (i_track > (p_env->gen.i_tracks+p_env->gen.i_first_track)
      || i_track < p_env->gen.i_first_track) {
    return false;
  } else {
    struct cdrom_tocentry *msf0 = &p_env->tocent[i_track-1];
    msf->m = to_bcd8(msf0->cdte_addr.msf.minute);
    msf->s = to_bcd8(msf0->cdte_addr.msf.second);
    msf->f = to_bcd8(msf0->cdte_addr.msf.frame);
    return true;
  }
#else
    return FALSE;
#endif
}

#else
/*!
  Return a string containing the default VCD device if none is specified.
 */
char *
cdio_get_default_device_aix(void)
{
  return strdup(DEFAULT_CDIO_DEVICE);
}

#endif /* HAVE_AIX_CDROM */

/*!
  Return an array of strings giving possible CD devices.
 */
char **
cdio_get_devices_aix (void)
{
#ifndef HAVE_AIX_CDROM
  return NULL;
#else
  struct stat st;
  char **drives = NULL;
  unsigned int i_files=0;
#ifdef HAVE_GLOB_H
  unsigned int i;
  glob_t globbuf;

  globbuf.gl_offs = 0;
  glob("/dev/rcd?", 0, NULL, &globbuf);
  for (i=0; i<globbuf.gl_pathc; i++) {
    if(stat(globbuf.gl_pathv[i], &st) < 0)
      continue;

    /* Check if this is a block device; if so it's probably a AIX media */
    if(S_ISCHR(st.st_mode)) {
      cdio_add_device_list(&drives, globbuf.gl_pathv[i], &i_files);
    }
  }
  globfree(&globbuf);
#else
  char psz_device[30];
  for (i=0; i<9; i++) {
    sprintf(psz_device, "/dev/cd%d", i);
    if(stat(psz_device, &st) < 0)
      continue;

    /* Check if this is a block device; if so it's probably a AIX media */
    if(S_ISCHR(st.st_mode)) {
      cdio_add_device_list(&drives, psz_device, &i_files);
  }
#endif /*HAVE_GLOB_H*/
  cdio_add_device_list(&drives, NULL, &i_files);
  return drives;
#endif /*HAVE_AIX_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_aix (const char *psz_source_name)
{
  return cdio_open_am_aix(psz_source_name, NULL);
}

/*!
  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_aix (const char *psz_orig_source, const char *access_mode)
{

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

  cdio_funcs_t _funcs;

  _funcs.eject_media        = eject_media_aix;
  _funcs.free               = cdio_generic_free;
  _funcs.get_arg            = get_arg_aix;
  _funcs.get_cdtext         = get_cdtext_generic;
  _funcs.get_cdtext_raw     = read_cdtext_generic;
  _funcs.get_default_device = cdio_get_default_device_aix;
  _funcs.get_devices        = cdio_get_devices_aix;
  _funcs.get_disc_last_lsn  = get_disc_last_lsn_aix;
  _funcs.get_discmode       = get_discmode_aix;
  _funcs.get_drive_cap      = get_drive_cap_mmc;
  _funcs.get_first_track_num= get_first_track_num_generic;
  _funcs.get_hwinfo         = NULL;
  _funcs.get_mcn            = get_mcn_mmc,
  _funcs.get_num_tracks     = get_num_tracks_generic;
  _funcs.get_track_format   = get_track_format_aix;
  _funcs.get_track_green    = _cdio_get_track_green;
  _funcs.get_track_lba      = NULL; /* This could be implemented if need be. */
  _funcs.get_track_msf      = _cdio_get_track_msf;
  _funcs.get_track_isrc     = get_track_isrc_mmc;
  _funcs.lseek              = cdio_generic_lseek;
  _funcs.read               = cdio_generic_read;
  _funcs.read_audio_sectors = _read_audio_sectors_aix;
  _funcs.read_mode1_sector  = _read_mode1_sector_aix;
  _funcs.read_mode1_sectors = _read_mode1_sectors_aix;
  _funcs.read_mode2_sector  = _read_mode2_sector_aix;
  _funcs.read_mode2_sectors = _read_mode2_sectors_aix;
  _funcs.read_toc           = read_toc_aix;
  _funcs.run_mmc_cmd        = run_mmc_cmd_aix;
  _funcs.set_arg            = _set_arg_aix;

  _data                 = calloc (1, sizeof (_img_private_t));

  _data->access_mode    = _AM_CTRL_SCSI;
  _data->gen.init       = false;
  _data->gen.toc_init   = false;
  _data->gen.fd         = -1;
  _data->gen.b_cdtext_error = false;

  if (NULL == psz_orig_source) {
    psz_source = cdio_get_default_device_aix();
    if (NULL == psz_source) {
        free(_data);
        return NULL;
    }
    _set_arg_aix(_data, "source", psz_source);
    free(psz_source);
  } else {
    if (cdio_is_device_generic(psz_orig_source))
      _set_arg_aix(_data, "source", psz_orig_source);
    else {
      /* The below would be okay if all device drivers worked this way. */
#if 0
      cdio_info ("source %s is not a device", psz_orig_source);
#endif
      return NULL;
    }
  }

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

  ret->driver_id = DRIVER_AIX;

  if (init_aix(_data))
    return ret;
  else {
    cdio_generic_free (_data);
    free(ret);
    return NULL;
  }

#else
  return NULL;
#endif /* HAVE_AIX_CDROM */

}

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


/*
 * Local variables:
 *  c-file-style: "gnu"
 *  tab-width: 8
 *  indent-tabs-mode: nil
 * End:
 */