File: ide-pc.c

package info (click to toggle)
udftools 2.3-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,628 kB
  • sloc: ansic: 15,554; sh: 4,170; makefile: 57
file content (543 lines) | stat: -rw-r--r-- 14,364 bytes parent folder | download | duplicates (3)
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
/*	ide-pc.c
 *
 * PURPOSE
 *	Execute most MMC commands on a ATAPI-Packet command CD(re)writer through IOCTL CDROM_SEND_PACKET
 *	Where similar routines and structures are defined in linux/ cdrom.h this program
 *	maintains structures strictly as defined MMC to facilitate the interface with the CDwriter
 *	at the expense of a simple user interface.
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *	Each contributing author retains all rights to their own work.
 *
 *	(C) 2001 Enno Fennema <e.fennema@dataweb.nl>
 *
 * HISTORY
 *
 *	16 Aug 01  ef  Created.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>		/* for u_char etc. */
#include <linux/cdrom.h>
#include <unistd.h>		/* sleep() */
#include <stdlib.h>

#include "bswap.h"
#include "ide-pc.h"

typedef struct cdrom_generic_command CGC;

static int rv;			// -1 if ioctl failed, else equal stat
static struct request_sense sense_data;


void fail(char *fmt, ...) {
    va_list	args;

    va_start(args, fmt); 
    vfprintf(stderr, fmt, args);
    exit(1);
}

struct request_sense *
get_sense_data() 
{
    return &sense_data;
}

char*
get_sense_string() 
{
    static char str[128];

    sprintf(str, "Stat %d Err %02X  Key %02Xx; ASC %02Xx; ASCQ %02Xx; Info %02X%02X%02X%02Xx",
	rv, sense_data.error_code, sense_data.sense_key, sense_data.asc, sense_data.ascq, 
	sense_data.information[0], sense_data.information[1], sense_data.information[2], sense_data.information[3]);
    return str;
}

void initpc(CGC *pc) {
    memset(pc,0,sizeof(*pc));
    memset(&sense_data, 0 , sizeof(sense_data));
    pc->sense=&sense_data;
    pc->data_direction = CGC_DATA_NONE;
    pc->timeout = 2500;
}

int
blank(int fd, int type, int start)
{
    CGC pc;
    uint32_t start_be32 = cpu_to_be32(start);

    initpc(&pc);
    pc.cmd[0]=GPCMD_BLANK;
    pc.cmd[1]= type & 7;
    memcpy(&pc.cmd[2], &start_be32, sizeof(start_be32));/* blank track tail : lba to start blanking
							 * blank track : track number to blank */
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
close_track_session(int fd, int close_track, int track)
{
    CGC pc;

    initpc(&pc);
    pc.cmd[0] = GPCMD_CLOSE_TRACK;
    pc.cmd[2] = close_track ? 0x01 : 0x02;
    pc.cmd[5] = track;
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
format(int fd, int session_grow, int size)
{
    struct {
	u_char	flh_rsrv;
	u_char	flh_imm;
	u_short	flh_fdlen;
	u_int	id;
	u_char	fd_sg;
	u_char	fd_rsrv[3];
	u_int	fd_size;
    }	f;			
    CGC pc;

    initpc(&pc);
    pc.data_direction = CGC_DATA_WRITE;
    memset(&f, 0, sizeof(f));
    pc.buffer= (void*)&f;
    pc.buflen= sizeof(f);
    pc.cmd[0] = GPCMD_FORMAT_UNIT;
    pc.cmd[1] = 0x17;				/* Format Parameter List present; format 0x7 */
    f.flh_fdlen = cpu_to_be16(8);
    f.fd_sg = session_grow;
    f.fd_size = cpu_to_be32(size);
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}


int
set_cdspeed(int fd, int readspeed, int writespeed)
{
    CGC pc;
    uint16_t readspeed_be16 = cpu_to_be16(readspeed);
    uint16_t writespeed_be16 = cpu_to_be16(writespeed);

    initpc(&pc);
    pc.cmd[0]=GPCMD_SET_SPEED;
    memcpy(&pc.cmd[2], &readspeed_be16, sizeof(readspeed_be16));
    memcpy(&pc.cmd[4], &writespeed_be16, sizeof(writespeed_be16));

    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
synchronize_cache(int fd)
{
    CGC pc;

    initpc(&pc);
    pc.cmd[0] = GPCMD_FLUSH_CACHE;
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
test_unit_ready(int fd)
{
    CGC pc;

    initpc(&pc);		 		/* Test Unit Ready command = 0x00 */
    /*	kernel returns -1 when CHECK_CONDITION status returned
     *	sense data is valid and gives further info
     */
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int getDriveState(int fd ) {
    int	rv;
 
    rv = test_unit_ready(fd);

    if( rv ) sleep(2);				/* wait a bit longer */

    rv = test_unit_ready(fd);

    if( rv < 0 )
	return DS_NOT_READY;

    if( rv == 0 ) 
	return DS_OPERATIONAL;

    if( sense_data.sense_key == 2 && sense_data.asc == 0x04 && sense_data.ascq == 0x04 )
	return DS_OPERATIONAL;

    if( sense_data.sense_key == 2 && sense_data.asc == 0x3A &&  sense_data.ascq == 2 )
	return DS_TRAY_OPEN;
    
    return DS_NO_DISC;
}

int
inquiry(int fd, struct cdrom_inquiry *inq)
{
    CGC pc;

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0]=GPCMD_INQUIRY;
    pc.cmd[4]=sizeof(struct cdrom_inquiry);
    pc.buffer= (void*)inq;
    pc.buflen= sizeof(struct cdrom_inquiry);
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}    

int
mediumRemoval(int fd, int allow)
{
    CGC pc;

    initpc(&pc);
    pc.data_direction = CGC_DATA_NONE;
    pc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
    pc.cmd[4] = allow;
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}    

int
read_reccapacity(int fd, struct cdrom_reccapacity *reccap)
{
    CGC pc;

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0]=GPCMD_READ_CDVD_CAPACITY;
    pc.buffer=(void*)reccap;
    pc.buflen=sizeof(struct cdrom_reccapacity);
    rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
    reccap->lastblock  = be32_to_cpu(reccap->lastblock);
    reccap->blocksize = be32_to_cpu(reccap->blocksize);
    return rv;
}

int
read_buffercapacity(int fd, struct cdrom_buffercapacity *bufcap)
{
    CGC pc;
    uint16_t size_be16 = cpu_to_be16(sizeof(struct cdrom_buffercapacity));

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0] = 0x5C;				// READ BUFFER CAPACITY
    memcpy(&pc.cmd[7], &size_be16, sizeof(size_be16));
    pc.buffer=(void*)bufcap;
    pc.buflen=sizeof(struct cdrom_buffercapacity);
    rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
    bufcap->totalBufferLength  = be32_to_cpu(bufcap->totalBufferLength);
    bufcap->freeBufferLength = be32_to_cpu(bufcap->freeBufferLength);
    return rv;
}

int
reserve_track(int fd, int size)
{
    CGC pc;
    uint32_t size_be32 = cpu_to_be32(size);

    initpc(&pc);
    pc.cmd[0]=GPCMD_RESERVE_RZONE_TRACK;		/* 0x53 */
    memcpy(&pc.cmd[5], &size_be32, sizeof(size_be32));
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
startStopUnit(int fd, int action)
{
    CGC pc;

    initpc(&pc);
    pc.cmd[0] = GPCMD_START_STOP_UNIT;
    pc.cmd[4] = action;
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
read_discinfo(int fd, struct cdrom_discinfo *di)
{
    CGC pc;
    uint16_t size_be16 = cpu_to_be16( sizeof(struct cdrom_discinfo) );

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;

    pc.cmd[0]=GPCMD_READ_DISC_INFO;
    memcpy(&pc.cmd[7], &size_be16, sizeof(size_be16));
    pc.buffer=(void*)di;
    pc.buflen=sizeof(struct cdrom_discinfo);

    rv = ioctl(fd, CDROM_SEND_PACKET, &pc);

    di->data_length = be16_to_cpu(di->data_length);
    di->disc_id = be32_to_cpu(di->disc_id);
    return rv;
}

int
read_header(int fd, struct cdrom_header *hd, u_int block)
{
    CGC pc;
    uint32_t block_be32 = cpu_to_be32( block );
    uint16_t size_be16 = cpu_to_be16( sizeof(struct cdrom_header) );

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0]=GPCMD_READ_HEADER;
    memcpy(&pc.cmd[2], &block_be32, sizeof(block_be32));
    memcpy(&pc.cmd[7], &size_be16, sizeof(size_be16));
    pc.buffer=(void*)hd;
    pc.buflen=sizeof(struct cdrom_header);
    rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
    hd->lba = be32_to_cpu(hd->lba);
    return rv;
}


int
read_trackinfo(int fd, struct cdrom_trackinfo *ti, int trackno)
{
    CGC pc;
    uint32_t trackno_be32 = cpu_to_be32( trackno );
    uint16_t size_be16 = cpu_to_be16( sizeof(struct cdrom_trackinfo) );

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0]=GPCMD_READ_TRACK_RZONE_INFO;
    pc.cmd[1]=1;
    memcpy(&pc.cmd[2], &trackno_be32, sizeof(trackno_be32));
    memcpy(&pc.cmd[7], &size_be16, sizeof(size_be16));	/* 28; if higher data underrun error */
    pc.buffer=(void*)ti;
    pc.buflen=sizeof(struct cdrom_trackinfo);
    rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
    ti->data_length = be16_to_cpu(ti->data_length);
    ti->trk_start = be32_to_cpu(ti->trk_start);
    ti->nwa = be32_to_cpu(ti->nwa);
    ti->free_blks = be32_to_cpu(ti->free_blks);
    ti->fixpkt_size = be32_to_cpu(ti->fixpkt_size);
    ti->trk_size = be32_to_cpu(ti->trk_size);
    return rv;
}


int
readCD(int fd, int sectortype, int lba, int n, unsigned char* buf)
{
    CGC pc;
    uint32_t lba_be32 = cpu_to_be32(lba);

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0]=GPCMD_READ_CD;
    pc.cmd[1]=sectortype << 2;			/* Sector type per MCC table 31 */
    memcpy(&pc.cmd[2], &lba_be32, sizeof(lba_be32));
    pc.cmd[8]=n;				/* read n blocks */
    pc.cmd[9]=0x10;				/* user data only */
    pc.buffer=buf;
    pc.buflen=n * 2048;
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
writeCD(int fd, int lba, int nblks, unsigned char* buf)
{
    CGC pc;
    uint32_t lba_be32 = cpu_to_be32(lba);
    uint16_t nblks_be16 = cpu_to_be16(nblks);

    initpc(&pc);
    pc.data_direction = CGC_DATA_WRITE;
    pc.cmd[0] = GPCMD_WRITE_10;
    pc.cmd[1] = 0;				/* DPO 0x10; FUA 0x08 */
    memcpy(&pc.cmd[2], &lba_be32, sizeof(lba_be32));
    memcpy(&pc.cmd[7], &nblks_be16, sizeof(nblks_be16));
    pc.buffer = buf;
    pc.buflen = nblks * 2048;
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}


#ifdef MMC2	// VERIFY and WRITE_AND_VERIFY are new in MMC2 and not supported by my HP8100
int
verify(int fd, int lba, int nblocks)
{
    CGC pc;
    uint32_t lba_be32 = cpu_to_be32(lba);

    initpc(&pc);
    pc.cmd[0] = GPCMD_VERIFY_10;
    pc.cmd[1] = 0;
    memcpy(&pc.cmd[2], &lba_be32, sizeof(lba_be32));
    pc.cmd[8] = (u_char) nblocks;
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}
#endif

/*
 *	mode_sense
 *	Returns 8 byte header followed by actual mode page. The HP 8100 recognizes:
 *	 1 - Read Error Recovery page
 *		In MtFuji this is called Read/Write Error recovery mode page
 *		The separate Verify Error Recovery Mode page of SCSI-2 draft has gone.
 *		The HP8100 recovers a change mask of 01 06 36 FF 00 00 00 00 
 *		This would mean retry count 0 to 255 acceptable
 *		and TB, RC, PER and DTE bits settable. Not the DCR bit.
 *		Could these also affect a verify operation. MtFuji VERIFY says no.
 *	 5 - Write parameters page
 *	 8 - Media supported mode page
 *	 D - CD-ROM page: Inactivity time, seconds/minute, frames/second
 *	 E - CD-ROM Audio Control page
 *	1D - Timeout and protect (MtFuji 12.11.3.5)
 *	2A - CD Capabilities mode page
 *	2F - ASCII message ? : '(c) 1997-1998 HP, license available'
 */

int
mode_select(int fd, u_char *buffer)
{
    CGC pc;
    int len = 2 + be16_to_cpu(((mode_hdr*)buffer)->data_length);
    uint16_t len_be16 = cpu_to_be16(len);

    initpc(&pc);
    pc.data_direction = CGC_DATA_WRITE;
    pc.cmd[0] = GPCMD_MODE_SELECT_10;	
    pc.cmd[1] = 0x10;					// PF bit
    pc.buffer = buffer;
    pc.buflen = len;
    memcpy(&pc.cmd[7], &len_be16, sizeof(len_be16));
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}

int
mode_sense(int fd, u_char **buffer, u_char **mode, u_char pageno, int pgctl)
{
    CGC pc;
    mode_hdr h;
    int len, offset;
    uint16_t size_be16 = cpu_to_be16(sizeof(mode_hdr));
    uint16_t len_be16;

    memset(&h, 0x00, sizeof(mode_hdr));

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0] = GPCMD_MODE_SENSE_10;
    pc.cmd[2] = (pgctl<<6) + (pageno & 0x3F);
    memcpy(&pc.cmd[7], &size_be16, sizeof(size_be16));
    pc.buffer = (void *)&h;
    pc.buflen = sizeof(mode_hdr);
    rv = ioctl(fd, CDROM_SEND_PACKET, &pc);

    if( rv ) {
	printf("mode_sense(1): stat %d\n", rv);
	return rv;
    }

    len = 2 + be16_to_cpu(h.data_length);
    offset = 8 + be16_to_cpu(h.block_desc_length);

    *buffer = (u_char*) calloc(len, sizeof(u_char));
    *mode = &(*buffer)[offset];
    pc.buffer = *buffer;

    pc.buflen = len;
    len_be16 = cpu_to_be16(len);
    memcpy(&pc.cmd[7], &len_be16, sizeof(len_be16));
    return ioctl(fd, CDROM_SEND_PACKET, &pc);
}


// MMC-2 feature not supported by my HP 8100 CD-Writer
#ifdef MMC2
int
get_configuration(int fd, u_char* buffer, int size, u_char feature)
{
    int i, realsize;
    uint16_t tmp_be16;
    CGC pc;

    initpc(&pc);
    pc.data_direction = CGC_DATA_READ;
    pc.cmd[0] = 0x46;				// GET CONFIGURATION
    tmp_be16 = cpu_to_be16(feature);
    memcpy(&pc.cmd[2], &tmp_be16, sizeof(tmp_be16));
    tmp_be16 = cpu_to_be16(8);
    memcpy(&pc.cmd[7], &tmp_be16, sizeof(tmp_be16));	// only to see how much will be returned
    pc.buffer=(void*)buffer;
    if( size < 8 ) return -EINVAL;
    pc.buflen = 8;
    rv = ioctl(fd, CDROM_SEND_PACKET, &pc);

    if( rv )
	return rv;

    realsize = be32_to_cpu((int)buffer[0]) + 4;
    if( realsize < size ) {
	pc.buflen = realsize;
	tmp_be16 = cpu_to_be16(realsize);
    } else {
	pc.buflen = size;
	tmp_be16 = cpu_to_be16(size);
    }
    memcpy(&pc.cmd[7], &tmp_be16, sizeof(tmp_be16));
    return rv = ioctl(fd, CDROM_SEND_PACKET, &pc);
}
#endif		// MMC2


int
get_writeparams(int fd, u_char **buffer, struct cdrom_writeparams **wp, int pgctl)
{
    rv = mode_sense(fd, buffer, (u_char**) wp, 0x05, pgctl);
    (*wp)->pkt_size = be32_to_cpu((*wp)->pkt_size);
    (*wp)->audio_pause = be16_to_cpu((*wp)->audio_pause);
    return rv;
}

int
set_writeparams(int fd, u_char *buffer, struct cdrom_writeparams *wp)
{
    wp->pkt_size = cpu_to_be32(wp->pkt_size);
    wp->audio_pause = cpu_to_be16(wp->audio_pause);

    rv = mode_select(fd, buffer);

    wp->pkt_size = be32_to_cpu(wp->pkt_size);
    wp->audio_pause = be16_to_cpu(wp->audio_pause);
    return rv;
}


int
get_capabilities(int fd, u_char **buffer, struct cdrom_capabilities **cap, int pgctl)
{
    rv = mode_sense(fd, buffer, (u_char**) cap, GPMODE_CAPABILITIES_PAGE, pgctl);

    (*cap)->max_read_speed  = be16_to_cpu((*cap)->max_read_speed);
    (*cap)->num_vol_levels  = be16_to_cpu((*cap)->num_vol_levels);
    (*cap)->buffer_size     = be16_to_cpu((*cap)->buffer_size);
    (*cap)->cur_read_speed  = be16_to_cpu((*cap)->cur_read_speed);
    (*cap)->max_write_speed = be16_to_cpu((*cap)->max_write_speed);
    (*cap)->cur_write_speed = be16_to_cpu((*cap)->cur_write_speed);
    return rv;
}