File: os_cdsim.c

package info (click to toggle)
xmcd 2.6-17sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,464 kB
  • ctags: 5,075
  • sloc: ansic: 51,322; sh: 4,039; makefile: 82; pascal: 67
file content (972 lines) | stat: -rw-r--r-- 19,271 bytes parent folder | download | duplicates (5)
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
/*
 *   libdi - scsipt SCSI Device Interface Library
 *
 *   Copyright (C) 1993-2000  Ti Kan
 *   E-mail: ti@amb.org
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library 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
 *   Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifndef LINT
static char *_os_cdsim_c_ident_ = "@(#)os_cdsim.c	6.19 99/12/27";
#endif

#include "common_d/appenv.h"
#include "common_d/util.h"
#include "libdi_d/libdi.h"
#include "libdi_d/scsipt.h"

#ifdef DEMO_ONLY

#ifndef CDSIM_VERS
#define CDSIM_VERS		"1.01"	/* CD-ROM simulator version */
#endif

extern appdata_t		app_data;
extern int			cdsim_sfd[],
				cdsim_rfd[];
extern FILE			*errfp;

STATIC simstat_t		cdsim_stat;
STATIC time_t			cdsim_start_time,
				cdsim_pause_time,
				cdsim_pause_elapsed,
				cdsim_prev_pause,
				cdsim_elapsed,
				cdsim_now;

STATIC inquiry_data_t		cdsim_inqdata;
STATIC byte_t			cdsim_tocdata1[SZ_RDTOC],
				cdsim_tocdata2[SZ_RDTOC];



/*
 * cdsim_sendpkt
 *	Write a CD simulator packet down the pipe
 *
 * Args:
 *	name - The text string describing the caller module
 *	fd - Pipe file descriptor
 *	s - Pointer to the packet data
 *
 * Return:
 *	TRUE - pipe write successful
 *	FALSE - pipe write failed
 */
bool_t
cdsim_sendpkt(char *name, int fd, simpkt_t *s)
{
	byte_t	*p = (byte_t *) s;
	int	i,
		ret;

	if (fd < 0)
		return FALSE;

	/* Brand packet with magic number */
	s->magic = CDSIM_MAGIC;

	/* Send a packet */
	i = CDSIM_PKTSZ;
	while ((ret = write(fd, p, i)) < i) {
		if (ret < 0 && errno != EBADF) {
			(void) fprintf(errfp,
				       "%s: packet write error (errno=%d)\n",
				       name, errno);
			return FALSE;
		}

		if (ret == 0) {
			/* avoid hogging CPU */
			(void) sleep(1);
		}
		else {
			i -= ret;
			p += ret;
		}
	}

	return TRUE;
}


/*
 * cdsim_getpkt
 *	Read a CD simulator packet from the pipe
 *
 * Args:
 *	name - The text string describing the caller module
 *	fd - Pipe file descriptor
 *	s - Pointer to the packet data
 *
 * Return:
 *	TRUE - pipe read successful
 *	FALSE - pipe read failed
 */
bool_t
cdsim_getpkt(char *name, int fd, simpkt_t *r)
{
	byte_t	*p = (byte_t *) r;
	int	i,
		ret;

	if (fd < 0)
		return FALSE;

	/* Get a packet */
	i = CDSIM_PKTSZ;
	while ((ret = read(fd, p, i)) < i) {
		if (ret < 0 && errno != EBADF) {
			(void) fprintf(errfp,
				       "%s: packet read error (errno=%d)\n",
				       name, errno);
			return FALSE;
		}

		if (ret == 0) {
			/* avoid hogging CPU */
			(void) sleep(1);
		}
		else {
			i -= ret;
			p += ret;
		}
	}

	/* Check packet for magic number */
	if (r->magic != CDSIM_MAGIC) {
		(void) fprintf(errfp, "%s: bad packet magic number.\n", name);
		return FALSE;
	}

	return TRUE;
}


/*
 * cdsim_sig
 *	CD simulator process signal handler
 *
 * Args:
 *	sig - The signal number
 *
 * Return:
 *	Nothing.
 */
/*ARGSUSED*/
STATIC void
cdsim_sig(int sig)
{
	(void) fprintf(errfp, "CD-ROM simulator exiting.\n");
	(void) close(cdsim_sfd[0]);
	(void) close(cdsim_rfd[1]);
	exit(0);
}


/*
 * cdsim_s_test
 *	Test Unit Ready command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_s_test(simpkt_t *r, simpkt_t *s)
{
	if (cdsim_stat.status == CDSIM_NODISC)
		return CDSIM_COMPERR;
	else
		return CDSIM_COMPOK;
}


/*
 * cdsim_s_inquir
 *	Inquiry command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
STATIC byte_t
cdsim_s_inquir(simpkt_t *r, simpkt_t *s)
{
	s->len = (r->len > CDSIM_INQSZ) ? CDSIM_INQSZ : r->len;

	/* Copy inquiry data into packet */
	(void) memcpy(s->data, (byte_t *) &cdsim_inqdata, s->len);

	return CDSIM_COMPOK;
}


/*
 * cdsim_s_mselect
 *	Mode Select command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_s_mselect(simpkt_t *r, simpkt_t *s)
{
	return CDSIM_COMPOK;
}


/*
 * cdsim_s_msense
 *	Mode Sense command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_s_msense(simpkt_t *r, simpkt_t *s)
{
	return CDSIM_COMPOK;
}


/*
 * cdsim_s_start
 *	Start/Stop Unit command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_s_start(simpkt_t *r, simpkt_t *s)
{
	cdsim_start_time = 0;
	cdsim_elapsed = 0;
	cdsim_pause_time = 0;
	cdsim_pause_elapsed = 0;
	cdsim_prev_pause = 0;

	if (r->cdb[4] & 0x01) {
		/* Start unit */
		if (r->cdb[4] & 0x02) {
			/* Load disc */
			cdsim_stat.status = CDSIM_STOPPED;
			return CDSIM_COMPOK;
		}
		else if (cdsim_stat.status == CDSIM_NODISC)
			return CDSIM_COMPERR;
	}
	else {
		/* Stop unit */
		if (cdsim_stat.status == CDSIM_NODISC)
			return CDSIM_COMPERR;
		else if (r->cdb[4] & 0x02) {
			/* Eject disc */
			if (cdsim_stat.caddylock)
				return CDSIM_COMPERR;
			else {
				cdsim_stat.status = CDSIM_NODISC;
				return CDSIM_COMPOK;
			}
		}
		else {
			/* Stop disc */
			cdsim_stat.status = CDSIM_STOPPED;
			return CDSIM_COMPOK;
		}
	}
	return CDSIM_COMPOK;
}


/*
 * cdsim_s_prevent
 *	Prevent/Allow Medium Removal command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_s_prevent(simpkt_t *r, simpkt_t *s)
{
	if (r->cdb[4] & 0x01)
		cdsim_stat.caddylock = TRUE;
	else
		cdsim_stat.caddylock = FALSE;

	return CDSIM_COMPOK;
}


/*
 * cdsim_m_rdsubq
 *	Read Subchannel command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
STATIC byte_t
cdsim_m_rdsubq(simpkt_t *r, simpkt_t *s)
{
	subq_hdr_t	*h = (subq_hdr_t *)(void *) s->data;
	subq_01_t	*s1 = (subq_01_t *)(void *)
			      (s->data + sizeof(subq_hdr_t));

	/* Subchannel formats */
	switch (r->cdb[3]) {
	case SUB_CURPOS:
		h->subch_len = 15;

		s1->fmt_code = SUB_CURPOS;
		s1->preemph = 0;
		s1->copyallow = 0;
		s1->trktype = 0;
		s1->audioch = 0;
		s1->adr = 0;

		s1->trkno = cdsim_stat.trkno;
		s1->idxno = cdsim_stat.idxno;

		if (r->cdb[1] & 0x02) {
			util_blktomsf(
				cdsim_stat.absaddr,
				&s1->abs_addr.msf.min,
				&s1->abs_addr.msf.sec,
				&s1->abs_addr.msf.frame,
				MSF_OFFSET
			);

			util_blktomsf(
				cdsim_stat.reladdr,
				&s1->rel_addr.msf.min,
				&s1->rel_addr.msf.sec,
				&s1->rel_addr.msf.frame,
				0
			);
		}
		else {
			s1->abs_addr.logical = util_bswap32(cdsim_stat.absaddr);
			s1->rel_addr.logical = util_bswap32(cdsim_stat.reladdr);
		}

		s->len = sizeof(subq_hdr_t) + sizeof(subq_01_t);

		break;

	default:
		/* The other formats are not implemented */
		return CDSIM_COMPERR;
	}

	switch (cdsim_stat.status) {
	case CDSIM_PLAYING:
		h->audio_status = AUDIO_PLAYING;
		break;

	case CDSIM_PAUSED:
		h->audio_status = AUDIO_PAUSED;
		break;

	default:
		h->audio_status = AUDIO_COMPLETED;
		break;
	}

	return CDSIM_COMPOK;
}


/*
 * cdsim_m_rdtoc
 *	Read TOC command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
STATIC byte_t
cdsim_m_rdtoc(simpkt_t *r, simpkt_t *s)
{
	byte_t	*startoff;

	if (r->cdb[1] & 0x02)
		startoff = cdsim_tocdata2 + sizeof(toc_hdr_t);
	else
		startoff = cdsim_tocdata1 + sizeof(toc_hdr_t);

	s->len = (r->len > SZ_RDTOC) ? SZ_RDTOC : r->len;

	if (r->cdb[6] > 1) {
		int	skip;

		skip = ((int) r->cdb[6] - 1) *
			sizeof(toc_trk_descr_t);
		s->len -= skip;
		startoff += skip;
	}

	/* Copy TOC data into packet */

	/* Header info */
	(void) memcpy(s->data, cdsim_tocdata1, sizeof(toc_hdr_t));

	/* TOC data */
	(void) memcpy(s->data + sizeof(toc_hdr_t), startoff, s->len);

	return CDSIM_COMPOK;
}


/*
 * cdsim_m_play
 *	Play Audio (10) command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_m_play(simpkt_t *r, simpkt_t *s)
{
	cdsim_stat.startaddr = (r->cdb[2] << 24) | (r->cdb[3] << 16) |
		(r->cdb[4] << 8) | r->cdb[5];
	cdsim_stat.endaddr = cdsim_stat.startaddr +
		((r->cdb[7] << 8) | r->cdb[8]);

	if (cdsim_stat.endaddr <= cdsim_stat.startaddr)
		return CDSIM_PARMERR;

	cdsim_start_time = cdsim_now;
	cdsim_elapsed = 0;
	cdsim_pause_time = 0;
	cdsim_pause_elapsed = 0;
	cdsim_prev_pause = 0;

	cdsim_stat.status = CDSIM_PLAYING;

	return CDSIM_COMPOK;
}


/*
 * cdsim_m_playmsf
 *	Play Audio MSF command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_m_playmsf(simpkt_t *r, simpkt_t *s)
{
	util_msftoblk(	
		r->cdb[3],
		r->cdb[4],
		r->cdb[5],
		&cdsim_stat.startaddr,
		MSF_OFFSET
	);
	util_msftoblk(
		r->cdb[6],
		r->cdb[7],
		r->cdb[8],
		&cdsim_stat.endaddr,
		MSF_OFFSET
	);

	if (cdsim_stat.endaddr <= cdsim_stat.startaddr)
		return CDSIM_PARMERR;

	cdsim_start_time = cdsim_now;
	cdsim_elapsed = 0;
	cdsim_pause_time = 0;
	cdsim_pause_elapsed = 0;
	cdsim_prev_pause = 0;

	cdsim_stat.status = CDSIM_PLAYING;

	return CDSIM_COMPOK;
}


/*
 * cdsim_m_playti
 *	Play Audio Track/index command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_m_playti(simpkt_t *r, simpkt_t *s)
{
	int	strk = (int) r->cdb[4],
		sidx = (int) r->cdb[5],
		etrk = (int) r->cdb[7],
		eidx = (int) r->cdb[8];

	if (sidx > (int) cdsim_stat.trk[strk - 1].nidxs) {
		strk++;
		sidx = 1;
	}

	if (strk > (int) cdsim_stat.ntrks || etrk > (int) cdsim_stat.ntrks)
		return CDSIM_PARMERR;

	if (eidx > (int) cdsim_stat.trk[strk - 1].nidxs)
		eidx = (int) cdsim_stat.trk[strk - 1].nidxs;

	cdsim_stat.startaddr = cdsim_stat.trk[strk - 1].iaddr[sidx - 1];
	cdsim_stat.endaddr = cdsim_stat.trk[etrk].iaddr[eidx - 1];

	if (cdsim_stat.endaddr <= cdsim_stat.startaddr)
		return CDSIM_PARMERR;

	cdsim_start_time = cdsim_now;
	cdsim_elapsed = 0;
	cdsim_pause_time = 0;
	cdsim_pause_elapsed = 0;
	cdsim_prev_pause = 0;

	cdsim_stat.status = CDSIM_PLAYING;

	return CDSIM_COMPOK;
}


/*
 * cdsim_m_pause
 *	Pause/Resume command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_m_pause(simpkt_t *r, simpkt_t *s)
{
	if (cdsim_stat.status == CDSIM_PAUSED && r->cdb[8] & 0x01) {
		/* Resume */
		cdsim_stat.status = CDSIM_PLAYING;
		cdsim_prev_pause += cdsim_pause_elapsed;
		cdsim_pause_elapsed = 0;

		return CDSIM_COMPOK;
	}
	else {
		cdsim_stat.status = CDSIM_PAUSED;
		time(&cdsim_pause_time);
		return CDSIM_COMPOK;
	}
}


/*
 * cdsim_m_pause
 *	Play Audio (12) command simulation function
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	Command completion status code
 */
/*ARGSUSED*/
STATIC byte_t
cdsim_l_play(simpkt_t *r, simpkt_t *s)
{
	cdsim_stat.startaddr = (r->cdb[2] << 24) | (r->cdb[3] << 16) |
		(r->cdb[4] << 8) | r->cdb[5];
	cdsim_stat.endaddr = cdsim_stat.startaddr +
		((r->cdb[6] << 24) | (r->cdb[7] << 16) |
		 (r->cdb[8] << 8) | r->cdb[9]);

	if (cdsim_stat.endaddr <= cdsim_stat.startaddr)
		return CDSIM_PARMERR;

	cdsim_start_time = cdsim_now;
	cdsim_elapsed = 0;
	cdsim_pause_time = 0;
	cdsim_pause_elapsed = 0;
	cdsim_prev_pause = 0;

	cdsim_stat.status = CDSIM_PLAYING;

	return CDSIM_COMPOK;
}


/*
 * cdsim_svccmd
 *	Service a command
 *
 * Args:
 *	r - Pointer to the command packet
 *	s - Pointer to the response packet
 *
 * Return:
 *	TRUE - success
 *	FALSE - failure
 */
STATIC bool_t
cdsim_svccmd(simpkt_t *r, simpkt_t *s)
{
	(void) memset(s, 0, CDSIM_PKTSZ);

	DBGPRN(errfp, "\ncdsim: pktid=%d cdbsz=%d len=%d dir=%d",
		r->pktid, r->cdbsz, r->len, r->dir);

	DBGDUMP("cdsim: SCSI CDB bytes", (byte_t *) r->cdb, (int) r->cdbsz);

	/* Set return packet id */
	s->pktid = r->pktid;

	/* Copy CDB */
	s->cdbsz = r->cdbsz;
	(void) memcpy(s->cdb, r->cdb, r->cdbsz);

	/* Truncate if necessary */
	if (s->len > MAX_DATALEN)
		s->len = MAX_DATALEN;

	/* Direction flag */
	s->dir = r->dir;

	/* Interpret CDB and service the command */
	switch (r->cdb[0]) {
	case OP_S_TEST:
		/* Test unit ready */
		s->retcode = cdsim_s_test(r, s);
		break;

	case OP_S_INQUIR:
		/* Inquiry */
		s->retcode = cdsim_s_inquir(r, s);
		break;

	case OP_S_MSELECT:
		/* Mode select */
		s->retcode = cdsim_s_mselect(r, s);
		break;

	case OP_S_MSENSE:
		/* Mode sense */
		s->retcode = cdsim_s_msense(r, s);
		break;

	case OP_S_START:
		/* Start/stop unit */
		s->retcode = cdsim_s_start(r, s);
		break;

	case OP_S_PREVENT:
		/* Prevent/allow medium removal */
		s->retcode = cdsim_s_prevent(r, s);
		break;

	case OP_M_RDSUBQ:
		/* Read subchannel */
		s->retcode = cdsim_m_rdsubq(r, s);
		break;

	case OP_M_RDTOC:
		/* Read TOC */
		s->retcode = cdsim_m_rdtoc(r, s);
		break;

	case OP_M_PLAY:
		/* Play audio (10) */
		s->retcode = cdsim_m_play(r, s);
		break;

	case OP_M_PLAYMSF:
		/* Play audio MSF */
		s->retcode = cdsim_m_playmsf(r, s);
		break;

	case OP_M_PLAYTI:
		/* Play audio track/index */
		s->retcode = cdsim_m_playti(r, s);
		break;

	case OP_M_PAUSE:
		/* Pause/resume */
		s->retcode = cdsim_m_pause(r, s);
		break;

	case OP_L_PLAY:
		/* Play audio (12) */
		s->retcode = cdsim_l_play(r, s);
		break;

	default:
		/* Command not implemented */
		s->retcode = CDSIM_NOTSUPP;
		break;
	}

	return (cdsim_sendpkt("cdsim", cdsim_rfd[1], s));
}


/*
 * cdsim_init
 *	Initialize the CD-simulator subsystem
 *
 * Args:
 *	Nothing.
 *
 * Return:
 *	Nothing.
 */
STATIC void
cdsim_init(void)
{
	int		i,
			j;
	toc_hdr_t	*h1 = (toc_hdr_t *)(void *) cdsim_tocdata1,
			*h2 = (toc_hdr_t *)(void *) cdsim_tocdata2;
	toc_trk_descr_t	*t1,
			*t2;

	/* Initialize internal states */
	cdsim_stat.status = CDSIM_STOPPED;
	cdsim_stat.ntrks = CDSIM_NTRKS;
	cdsim_stat.absaddr = 0;
	cdsim_stat.reladdr = 0;
	cdsim_stat.startaddr = 0;
	cdsim_stat.endaddr = 0;
	cdsim_stat.caddylock = 0;

	/* Addresses for each simulated track and index */
	for (i = 0; i < CDSIM_NTRKS; i++) {
		if (i == 0)
			cdsim_stat.trk[i].addr = 0;
		else
			cdsim_stat.trk[i].addr = (i * CDSIM_TRKLEN) - 150;

		cdsim_stat.trk[i].nidxs = CDSIM_NIDXS;

		for (j = 0; j < (int) cdsim_stat.trk[i].nidxs; j++) {
			cdsim_stat.trk[i].iaddr[j] = (j * CDSIM_IDXLEN) +
						     cdsim_stat.trk[i].addr;
		}
	}

	/* Simulated lead-out track */
	cdsim_stat.trk[i].addr = (i * CDSIM_TRKLEN) - 150;
	cdsim_stat.trk[i].nidxs = 0;
	for (j = 0; j < CDSIM_NIDXS; j++)
		cdsim_stat.trk[i].iaddr[j] = cdsim_stat.trk[i].addr;

	/* Initialize inquiry data */
	cdsim_inqdata.type = DEV_ROM;
	cdsim_inqdata.pqual = 0;
	cdsim_inqdata.qualif = 0;
	cdsim_inqdata.rmb = 1;
	cdsim_inqdata.ver = 2;
	cdsim_inqdata.len = 38;
	(void) strncpy((char *) cdsim_inqdata.vendor, "XMCD    ", 8);
	(void) strncpy((char *) cdsim_inqdata.prod, "CD-ROM SIMULATOR", 16);
	(void) strncpy((char *) cdsim_inqdata.revnum, CDSIM_VERS, 4);

	/* Initialize TOC data */
	h1->data_len = h2->data_len = util_bswap16(
		((CDSIM_NTRKS + 1) * sizeof(toc_trk_descr_t)) + 2
	);

	h1->first_trk = h2->first_trk = 1;
	h1->last_trk = h2->last_trk = CDSIM_NTRKS;

	t1 = (toc_trk_descr_t *)(void *) (cdsim_tocdata1 + sizeof(toc_hdr_t));
	t2 = (toc_trk_descr_t *)(void *) (cdsim_tocdata2 + sizeof(toc_hdr_t));

	for (i = 0; i < CDSIM_NTRKS; i++) {
		t1->preemph = t2->preemph = 0;
		t1->copyallow = t2->copyallow = 0;
		t1->trktype = t2->trktype = 0;
		t1->audioch = t2->audioch = 0;
		t1->adr = t2->adr = 0;
		t1->trkno = t2->trkno = i + 1;
		t1->abs_addr.logical = util_bswap32(cdsim_stat.trk[i].addr);
		util_blktomsf(
			cdsim_stat.trk[i].addr,
			&t2->abs_addr.msf.min,
			&t2->abs_addr.msf.sec,
			&t2->abs_addr.msf.frame,
			MSF_OFFSET
		);

		t1 = (toc_trk_descr_t *)(void *)
			((byte_t *)(void *) t1 + sizeof(toc_trk_descr_t));
		t2 = (toc_trk_descr_t *)(void *)
			((byte_t *)(void *) t2 + sizeof(toc_trk_descr_t));
	}

	/* Lead-out track */
	t1->preemph = t2->preemph = 0;
	t1->copyallow = t2->copyallow = 0;
	t1->trktype = t2->trktype = 0;
	t1->audioch = t2->audioch = 0;
	t1->adr = t2->adr = 0;
	t1->trkno = t2->trkno = LEAD_OUT_TRACK;
	t1->abs_addr.logical = util_bswap32(cdsim_stat.trk[i].addr);
	util_blktomsf(
		cdsim_stat.trk[i].addr,
		&t2->abs_addr.msf.min,
		&t2->abs_addr.msf.sec,
		&t2->abs_addr.msf.frame,
		MSF_OFFSET
	);
}


/*
 * cdsim_main
 *	The CD simulator main function
 *
 * Args:
 *	Nothing.
 *
 * Return:
 *	Nothing.
 */
void
cdsim_main(void)
{
	int		i,
			j;
	simpkt_t	spkt,
			rpkt;

	(void) fprintf(errfp,
		       "CD-ROM simulator version %s (pid=%d) starting...\n",
		       CDSIM_VERS, (int) getpid());

	/* Install signal handlers */
	signal(SIGINT, cdsim_sig);
	signal(SIGTERM, cdsim_sig);
	signal(SIGHUP, cdsim_sig);

	/* Initialize CD-ROM simulator */
	cdsim_init();

	/* Main simulation service loop */
	for (;;) {
		/* Get SCSI request */
		if (!cdsim_getpkt("cdsim", cdsim_sfd[0], &rpkt))
			continue;

		time(&cdsim_now);
		/* Update status */
		switch (cdsim_stat.status) {
		case CDSIM_PLAYING:
			cdsim_elapsed = cdsim_now - cdsim_start_time -
				cdsim_pause_elapsed - cdsim_prev_pause;

			cdsim_stat.absaddr = cdsim_elapsed * FRAME_PER_SEC +
				cdsim_stat.startaddr;

			cdsim_stat.trkno = 0;
			for (i = 0; i < (int) cdsim_stat.ntrks; i++) {
				if (cdsim_stat.trk[i].addr > cdsim_stat.absaddr)
					break;
				cdsim_stat.trkno++;
			}

			cdsim_stat.idxno = 0;
			for (j = 0; j < (int) cdsim_stat.trk[i-1].nidxs; j++) {
				if (cdsim_stat.trk[i-1].iaddr[j] >
				    cdsim_stat.absaddr)
					break;
				cdsim_stat.idxno++;
			}

			cdsim_stat.reladdr = cdsim_stat.absaddr -
				cdsim_stat.trk[i-1].addr;

			if (cdsim_stat.absaddr > cdsim_stat.endaddr) {
				cdsim_stat.status = CDSIM_STOPPED;
				cdsim_elapsed = 0;
				cdsim_start_time = 0;
				cdsim_pause_time = 0;
				cdsim_prev_pause = 0;
				cdsim_pause_elapsed = 0;
			}
			break;

		case CDSIM_PAUSED:
			cdsim_pause_elapsed = cdsim_now - cdsim_pause_time;
			break;
		}

		/* Process SCSI request */
		(void) cdsim_svccmd(&rpkt, &spkt);
	}
}

#endif	/* DEMO_ONLY */