spc.c 37.4 KB
Newer Older
Mario Danic's avatar
Mario Danic committed
1 2
/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */

3 4 5 6 7 8
/* Copyright (c) 2004 - 2006 Derek Foreman, Ben Jansens
   Copyright (c) 2006 - 2010 Thomas Schmitt <scdbackup@gmx.net>
   Provided under GPL version 2 or later.
*/


Mario Danic's avatar
Mario Danic committed
9 10 11 12 13 14 15 16 17
/* scsi primary commands */

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
18 19 20 21

/* ts A61008 */
/* #include <a ssert.h> */

Mario Danic's avatar
Mario Danic committed
22 23
#include <stdlib.h>

24
#include "libburn.h"
Mario Danic's avatar
Mario Danic committed
25 26 27 28 29 30 31 32
#include "transport.h"
#include "spc.h"
#include "mmc.h"
#include "sbc.h"
#include "drive.h"
#include "debug.h"
#include "options.h"

33 34 35
#include "libdax_msgs.h"
extern struct libdax_msgs *libdax_messenger;

36 37 38 39 40 41 42 43
/* ts A91111 :
   whether to log SCSI commands:
   bit0= log in /tmp/libburn_sg_command_log
   bit1= log to stderr
   bit2= flush every line
*/
extern int burn_sg_log_scsi;

44

Mario Danic's avatar
Mario Danic committed
45
/* spc command set */
46 47
/* ts A70519 : allocation length byte 3+4 was 0,255 */
static unsigned char SPC_INQUIRY[] = { 0x12, 0, 0, 0, 36, 0 };
Mario Danic's avatar
Mario Danic committed
48 49 50 51 52

/*static char SPC_TEST[]={0,0,0,0,0,0};*/
static unsigned char SPC_PREVENT[] = { 0x1e, 0, 0, 0, 1, 0 };
static unsigned char SPC_ALLOW[] = { 0x1e, 0, 0, 0, 0, 0 };
static unsigned char SPC_MODE_SENSE[] = { 0x5a, 0, 0, 0, 0, 0, 0, 16, 0, 0 };
53
static unsigned char SPC_MODE_SELECT[] = { 0x55, 16, 0, 0, 0, 0, 0, 0, 0, 0 };
Mario Danic's avatar
Mario Danic committed
54 55 56
static unsigned char SPC_REQUEST_SENSE[] = { 0x03, 0, 0, 0, 18, 0 };
static unsigned char SPC_TEST_UNIT_READY[] = { 0x00, 0, 0, 0, 0, 0 };

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

/* ts A70519 : An initializer for the abstract SCSI command structure */
int scsi_init_command(struct command *c, unsigned char *opcode, int oplen)
{
	if (oplen > 16)
		return 0;
	memcpy(c->opcode, opcode, oplen);
	c->oplen = oplen;
	c->dir = NO_TRANSFER;
	c->dxfer_len = -1;
	memset(c->sense, 0, sizeof(c->sense));
	c->error = 0;
	c->retry = 0;
	c->page = NULL;
	return 1;
}


75
int spc_test_unit_ready_r(struct burn_drive *d, int *key, int *asc, int *ascq)
Mario Danic's avatar
Mario Danic committed
76 77 78
{
	struct command c;

79 80 81
	if (mmc_function_spy(d, "test_unit_ready") <= 0)
		return 0;

82 83
	scsi_init_command(&c, SPC_TEST_UNIT_READY,sizeof(SPC_TEST_UNIT_READY));
/*
Mario Danic's avatar
Mario Danic committed
84 85 86
	c.oplen = sizeof(SPC_TEST_UNIT_READY);
	memcpy(c.opcode, SPC_TEST_UNIT_READY, sizeof(SPC_TEST_UNIT_READY));
	c.page = NULL;
87 88
*/
	c.retry = 0;
Mario Danic's avatar
Mario Danic committed
89 90
	c.dir = NO_TRANSFER;
	d->issue_command(d, &c);
91 92 93 94
	if (c.error) {
		*key= c.sense[2];
		*asc= c.sense[12];
		*ascq= c.sense[13];
Mario Danic's avatar
Mario Danic committed
95
		return (c.sense[2] & 0xF) == 0;
96
	}
Mario Danic's avatar
Mario Danic committed
97 98 99
	return 1;
}

100 101 102 103 104 105 106 107 108
int spc_test_unit_ready(struct burn_drive *d)
{
	int key,asc,ascq;

	return spc_test_unit_ready_r(d, &key, &asc, &ascq);
}


/* ts A70315 */
109 110 111
/** @param flag bit0=do not wait 0.1 seconds before first test unit ready
                bit1=do not issue success message
 */
112
/** Wait until the drive state becomes clear or until max_usec elapsed */
113 114
int spc_wait_unit_attention(struct burn_drive *d, int max_sec, char *cmd_text,
				int flag)
115
{
116 117
	int i, ret = 1, key = 0, asc = 0, ascq = 0, clueless_start = 0;
	static int clueless_timeout = 5 * 10;
118 119 120
	char msg[320];
	unsigned char sense[14];
	enum response resp;
121

122 123 124
	if (!(flag & 1))
		usleep(100000);
	for(i = !(flag & 1); i < max_sec * 10; i++) {
125
		ret = spc_test_unit_ready_r(d, &key, &asc, &ascq);
126
		if (ret > 0) /* ready */
127
	break;
128
		if (key!=0x2 || asc!=0x4) {
129 130
			if (key == 0x2 && asc == 0x3A) {
				ret = 1; /* medium not present = ok */
131 132 133 134 135 136 137 138 139 140
/* <<<
  ts A70912 :
  My LG GSA-4082B on asynchronous load:
    first it reports no media 2,3A,00,
    then it reports not ready 2,04,00,
    further media inquiry retrieves wrong data

				if(i<=100)
					goto slumber;
*/
141 142 143
	break;
			}
			if (key == 0x6 && asc == 0x28 && ascq == 0x00)
144 145 146
				/* media change notice = try again */
				goto slumber;

147
handle_error:;
148 149
			/* ts A90213 */
			sprintf(msg,
150
				"Asynchronous SCSI error on %s: ", cmd_text);
151 152 153 154 155
			sense[2] = key;
			sense[12] = asc;
			sense[13] = ascq;
			resp = scsi_error_msg(d, sense, 14, msg + strlen(msg),
						 &key, &asc, &ascq);
156 157 158 159 160 161
			libdax_msgs_submit(libdax_messenger, d->global_index,
				0x0002014d,
				LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
				msg, 0, 0);
			d->cancel = 1;
	break;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
		} else if (ascq == 0x00) { /* CAUSE NOT REPORTABLE */
			/* Might be a clueless system adapter */
			if (clueless_start == 0)
				clueless_start = i;
			if (i - clueless_start > clueless_timeout) {
				libdax_msgs_submit(libdax_messenger,
				  d->global_index,
				  0x00000002,
				  LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
				  "Ended clueless NOT READY cycle",
				  0, 0);
				ret = 1; /* medium not present = ok */
	break;
			}
		} else if (ascq == 0x02 || ascq == 0x03) 
			goto handle_error;

179
slumber:;
180
		usleep(100000);
181
	}
182 183 184 185 186 187 188 189
	if (ret <= 0 || !(flag & 2)) {
		sprintf(msg, "Async %s %s after %d.%d seconds",
			cmd_text, (ret > 0 ? "succeeded" : "failed"),
			i / 10, i % 10);
		libdax_msgs_submit(libdax_messenger, d->global_index,
			 0x00020150, LIBDAX_MSGS_SEV_DEBUG,
			 LIBDAX_MSGS_PRIO_LOW, msg, 0, 0);
	}
190 191

	if (i < max_sec * 10)
192
		return (ret > 0);
193 194 195 196 197

	sprintf(msg, "Timeout (%d s) with asynchronous SCSI command %s\n",
	 	max_sec, cmd_text);
	libdax_msgs_submit(libdax_messenger, d->global_index, 0x0002014f,
		LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
198 199 200 201
	return 0;
}


Mario Danic's avatar
Mario Danic committed
202 203 204 205
void spc_request_sense(struct burn_drive *d, struct buffer *buf)
{
	struct command c;

206 207 208
	if (mmc_function_spy(d, "request_sense") <= 0)
		return;

209
	scsi_init_command(&c, SPC_REQUEST_SENSE, sizeof(SPC_REQUEST_SENSE));
Mario Danic's avatar
Mario Danic committed
210
	c.retry = 0;
211
/*
Mario Danic's avatar
Mario Danic committed
212 213
	c.oplen = sizeof(SPC_REQUEST_SENSE);
	memcpy(c.opcode, SPC_REQUEST_SENSE, sizeof(SPC_REQUEST_SENSE));
214 215 216
*/
	c.dxfer_len= c.opcode[4];
	c.retry = 0;
Mario Danic's avatar
Mario Danic committed
217 218 219 220 221 222 223 224 225 226 227
	c.page = buf;
	c.page->sectors = 0;
	c.page->bytes = 0;
	c.dir = FROM_DRIVE;
	d->issue_command(d, &c);
}

int spc_get_erase_progress(struct burn_drive *d)
{
	struct buffer b;

228
	if (mmc_function_spy(d, "get_erase_progress") <= 0)
229
		return 0;
230

Mario Danic's avatar
Mario Danic committed
231 232 233 234 235 236 237
	spc_request_sense(d, &b);
	return (b.data[16] << 8) | b.data[17];
}

void spc_inquiry(struct burn_drive *d)
{
	struct buffer buf;
238
	struct burn_scsi_inquiry_data *id;
Mario Danic's avatar
Mario Danic committed
239 240
	struct command c;

241 242 243
	if (mmc_function_spy(d, "inquiry") <= 0)
		return;

244 245
	scsi_init_command(&c, SPC_INQUIRY, sizeof(SPC_INQUIRY));
/*
Mario Danic's avatar
Mario Danic committed
246 247
	memcpy(c.opcode, SPC_INQUIRY, sizeof(SPC_INQUIRY));
	c.oplen = sizeof(SPC_INQUIRY);
248 249 250
*/
	c.dxfer_len= (c.opcode[3] << 8) | c.opcode[4];
	c.retry = 1;
Mario Danic's avatar
Mario Danic committed
251 252 253 254 255
	c.page = &buf;
	c.page->bytes = 0;
	c.page->sectors = 0;
	c.dir = FROM_DRIVE;
	d->issue_command(d, &c);
256
	id = (struct burn_scsi_inquiry_data *)d->idata;
257 258 259 260 261 262 263
	memset(id->vendor, 0, 9);
	memset(id->product, 0, 17);
	memset(id->revision, 0, 5);
	if (c.error) {
		id->valid = -1;
		return;
	}
Mario Danic's avatar
Mario Danic committed
264 265 266 267 268 269 270 271 272 273 274
	memcpy(id->vendor, c.page->data + 8, 8);
	memcpy(id->product, c.page->data + 16, 16);
	memcpy(id->revision, c.page->data + 32, 4);
	id->valid = 1;
	return;
}

void spc_prevent(struct burn_drive *d)
{
	struct command c;

275 276 277
	if (mmc_function_spy(d, "prevent") <= 0)
		return;

278 279
	scsi_init_command(&c, SPC_PREVENT, sizeof(SPC_PREVENT));
	c.retry = 1;
Mario Danic's avatar
Mario Danic committed
280 281
	c.dir = NO_TRANSFER;
	d->issue_command(d, &c);
282 283 284 285 286

#ifdef Libburn_pioneer_dvr_216d_get_evenT
        mmc_get_event(d);
#endif

Mario Danic's avatar
Mario Danic committed
287 288 289 290 291 292
}

void spc_allow(struct burn_drive *d)
{
	struct command c;

293 294 295
	if (mmc_function_spy(d, "allow") <= 0)
		return;

296 297
	scsi_init_command(&c, SPC_ALLOW, sizeof(SPC_ALLOW));
/*
Mario Danic's avatar
Mario Danic committed
298 299 300
	memcpy(c.opcode, SPC_ALLOW, sizeof(SPC_ALLOW));
	c.oplen = sizeof(SPC_ALLOW);
	c.page = NULL;
301 302
*/
	c.retry = 1;
Mario Danic's avatar
Mario Danic committed
303 304 305 306
	c.dir = NO_TRANSFER;
	d->issue_command(d, &c);
}

307
/*
308
ts A70518 - A90603 : Do not call with *alloc_len < 10
309 310 311
*/
/** flag&1= do only inquire alloc_len */
static int spc_sense_caps_al(struct burn_drive *d, int *alloc_len, int flag)
Mario Danic's avatar
Mario Danic committed
312 313 314
{
	struct buffer buf;
	struct scsi_mode_data *m;
315
	int size, page_length, num_write_speeds = 0, i, speed, ret;
316
	int old_alloc_len, was_error = 0;
Mario Danic's avatar
Mario Danic committed
317 318
	unsigned char *page;
	struct command c;
319
	struct burn_speed_descriptor *sd;
320
	char msg[BURN_DRIVE_ADR_LEN + 160];
Mario Danic's avatar
Mario Danic committed
321

322 323 324
	/* ts A61225 : 1 = report about post-MMC-1 speed descriptors */
	static int speed_debug = 0;

325
	if (*alloc_len < 10)
326 327
		return 0;

328 329 330 331 332
	/* ts A90602 : Clearing mdata before command execution */
	m = d->mdata;
	m->valid = 0;
	burn_mdata_free_subs(m);

333
	memset(&buf, 0, sizeof(buf));
334 335
	scsi_init_command(&c, SPC_MODE_SENSE, sizeof(SPC_MODE_SENSE));
/*
Mario Danic's avatar
Mario Danic committed
336 337
	memcpy(c.opcode, SPC_MODE_SENSE, sizeof(SPC_MODE_SENSE));
	c.oplen = sizeof(SPC_MODE_SENSE);
338 339 340 341 342
*/
	c.dxfer_len = *alloc_len;
	c.opcode[7] = (c.dxfer_len >> 8) & 0xff;
	c.opcode[8] = c.dxfer_len & 0xff;
	c.retry = 1;
Mario Danic's avatar
Mario Danic committed
343 344 345 346 347 348
	c.opcode[2] = 0x2A;
	c.page = &buf;
	c.page->bytes = 0;
	c.page->sectors = 0;
	c.dir = FROM_DRIVE;
	d->issue_command(d, &c);
349
	if (c.error) {
350
		memset(&buf, 0, sizeof(buf));
351
		m->valid = -1;
352 353
		was_error = 1;
	}
Mario Danic's avatar
Mario Danic committed
354

355
	size = c.page->data[0] * 256 + c.page->data[1] + 2;
Mario Danic's avatar
Mario Danic committed
356 357
	page = c.page->data + 8;

358 359 360 361 362 363
	/* ts A61225 :
	   Although MODE SENSE indeed belongs to SPC, the returned code page
	   2Ah is part of MMC-1 to MMC-3. In MMC-1 5.2.3.4. it has 22 bytes,
	   in MMC-3 6.3.11 there are at least 28 bytes plus a variable length
	   set of speed descriptors. In MMC-5 E.11 it is declared "legacy".
	*/
364 365 366 367
	/* ts A90603 :
	   SPC-1 8.3.3 enumerates mode page format bytes from 0 to n and
	   defines Page Length as (n-1).
	*/
368
	page_length = page[1];
369
	old_alloc_len = *alloc_len;
370
	*alloc_len = page_length + 10;
371 372
	if (flag & 1)
		return !was_error;
373 374 375
	if (page_length + 10 > old_alloc_len)
		page_length = old_alloc_len - 10;

376
	/* ts A90602 : 20 asserts page[21]. (see SPC-1 8.3.3) */
377
	if (page_length < 20) {
378 379 380 381 382 383
		m->valid = -1;
		sprintf(msg, "MODE SENSE page 2A too short: %s : %d",
			d->devname, page_length);
		libdax_msgs_submit(libdax_messenger, d->global_index,
				0x0002016e, LIBDAX_MSGS_SEV_DEBUG,
				LIBDAX_MSGS_PRIO_LOW, msg, 0, 0);
384
		return 0;
385
	}
386

Mario Danic's avatar
Mario Danic committed
387 388 389 390 391 392 393 394 395 396 397
	m->buffer_size = page[12] * 256 + page[13];
	m->dvdram_read = page[2] & 32;
	m->dvdram_write = page[3] & 32;
	m->dvdr_read = page[2] & 16;
	m->dvdr_write = page[3] & 16;
	m->dvdrom_read = page[2] & 8;
	m->simulate = page[3] & 4;
	m->cdrw_read = page[2] & 2;
	m->cdrw_write = page[3] & 2;
	m->cdr_read = page[2] & 1;
	m->cdr_write = page[3] & 1;
398

399 400 401
	m->c2_pointers = page[5] & 16;
	m->underrun_proof = page[4] & 128;

402
	/* ts A61021 : these fields are marked obsolete in MMC 3 */
Mario Danic's avatar
Mario Danic committed
403 404
	m->max_read_speed = page[8] * 256 + page[9];
	m->cur_read_speed = page[14] * 256 + page[15];
405

Mario Danic's avatar
Mario Danic committed
406
	m->max_write_speed = page[18] * 256 + page[19];
407 408 409
	m->cur_write_speed = page[20] * 256 + page[21];

	/* ts A61021 : New field to be set by atip (or following MMC-3 info) */
410 411
	m->min_write_speed = m->max_write_speed;

412 413 414 415
	/* ts A61225 : for ACh GET PERFORMANCE, Type 03h */
	m->min_end_lba = 0x7fffffff;
	m->max_end_lba = 0;

416 417
	if (!was_error)
		m->valid = 1;
418

419 420
	mmc_get_configuration(d);

421
	/* ts A61225 : end of MMC-1 , begin of MMC-3 */
422
	if (page_length < 30) /* no write speed descriptors ? */
423
		goto try_mmc_get_performance;
424 425 426 427 428 429 430 431 432

	m->cur_write_speed = page[28] * 256 + page[29];

	if (speed_debug) 
	fprintf(stderr, "LIBBURN_DEBUG: cur_write_speed = %d\n",
		m->cur_write_speed);

	num_write_speeds = page[30] * 256 + page[31];
	m->max_write_speed = m->min_write_speed = m->cur_write_speed;
433 434 435 436 437 438 439 440 441

        if (32 + 4 * num_write_speeds > page_length + 2) {
		char msg[161];

		sprintf(msg, "Malformed capabilities page 2Ah received (len=%d, #speeds=%d)", page_length, num_write_speeds);
		libdax_msgs_submit(libdax_messenger, d->global_index,
				0x0002013c,
				LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
				msg, 0, 0);
442
		return 0;
443 444
	}

445
	for (i = 0; i < num_write_speeds; i++) {
446
		speed = page[32 + 4 * i + 2] * 256 + page[32 + 4 * i + 3];
447 448 449 450

		if (speed_debug) 
		fprintf(stderr,
			"LIBBURN_DEBUG: write speed #%d = %d kB/s  (rc %d)\n",
451
			i, speed, page[32 + 4 * i + 1] & 7);
452

453 454 455 456 457 458 459 460 461 462 463
		/* ts A61226 */
		ret = burn_speed_descriptor_new(&(d->mdata->speed_descriptors),
				NULL, d->mdata->speed_descriptors, 0);
		if (ret > 0) {
			sd = d->mdata->speed_descriptors;
			sd->source = 1;
			if (d->current_profile > 0) {
				sd->profile_loaded = d->current_profile;
				strcpy(sd->profile_name,
					d->current_profile_text);
			}
464
			sd->wrc = (( page[32 + 4 * i + 1] & 7 ) == 1 );
465 466 467
			sd->write_speed = speed;
		}

468 469 470 471 472 473 474 475
		if (speed > m->max_write_speed)
			m->max_write_speed = speed;
		if (speed < m->min_write_speed)
			m->min_write_speed = speed;
	}

	if (speed_debug) 
	fprintf(stderr,
476 477 478
	"LIBBURN_DEBUG: 5Ah,2Ah min_write_speed = %d , max_write_speed = %d\n",
		m->min_write_speed, m->max_write_speed);

479
try_mmc_get_performance:;
480 481 482 483 484 485 486 487 488 489 490 491 492 493
	if (m->cdrw_write || page_length >= 32) {
		/* ts A90823:
		   One has to avoid U3 enhanced memory sticks here. On my
		   SuSE 10.2 a SanDisk Cruzer 4GB stalls at the second occasion
		   of ACh GET PERFORMANCE. (The first one is obviously called
		   by the OS at plug time.)
		   This pseudo drive returns no write capabilities and a page
		   length of 28. MMC-3 describes page length 32. Regrettably
		   MMC-2 prescribes a page length of 26. Here i have to trust
		   m->cdrw_write to reliably indicate any MMC-2 burner.
		*/
		ret = mmc_get_write_performance(d);
		if (ret > 0 && speed_debug)
			fprintf(stderr,
494
	  "LIBBURN_DEBUG: ACh min_write_speed = %d , max_write_speed = %d\n",
495 496 497
			m->min_write_speed, m->max_write_speed);
	}

498 499 500 501 502 503
	return !was_error;
}


void spc_sense_caps(struct burn_drive *d)
{
504
	int alloc_len, start_len = 30, ret;
505

506
	mmc_start_if_needed(d, 1);
507 508 509
	if (mmc_function_spy(d, "sense_caps") <= 0)
		return;

510 511 512 513 514 515 516 517 518 519
	/* first command execution to learn Allocation Length */
	alloc_len = start_len;
	ret = spc_sense_caps_al(d, &alloc_len, 1);
/*
	fprintf(stderr,"LIBBURN_DEBUG: 5Ah alloc_len = %d , ret = %d\n",
			alloc_len, ret);
*/
	if (alloc_len >= start_len && ret > 0)
		/* second execution with announced length */
		spc_sense_caps_al(d, &alloc_len, 0);
Mario Danic's avatar
Mario Danic committed
520 521
}

522

Mario Danic's avatar
Mario Danic committed
523 524 525 526
void spc_sense_error_params(struct burn_drive *d)
{
	struct buffer buf;
	struct scsi_mode_data *m;
527
	int size, alloc_len = 12 ;
Mario Danic's avatar
Mario Danic committed
528 529 530
	unsigned char *page;
	struct command c;

531
	mmc_start_if_needed(d, 1);
532 533 534
	if (mmc_function_spy(d, "sense_error_params") <= 0)
		return;

535 536
	scsi_init_command(&c, SPC_MODE_SENSE, sizeof(SPC_MODE_SENSE));
/*
Mario Danic's avatar
Mario Danic committed
537 538
	memcpy(c.opcode, SPC_MODE_SENSE, sizeof(SPC_MODE_SENSE));
	c.oplen = sizeof(SPC_MODE_SENSE);
539 540 541 542 543
*/
	c.dxfer_len = alloc_len;
	c.opcode[7] = (c.dxfer_len >> 8) & 0xff;
	c.opcode[8] = c.dxfer_len & 0xff;
	c.retry = 1;
Mario Danic's avatar
Mario Danic committed
544 545 546 547 548 549 550
	c.opcode[2] = 0x01;
	c.page = &buf;
	c.page->bytes = 0;
	c.page->sectors = 0;
	c.dir = FROM_DRIVE;
	d->issue_command(d, &c);

551
	size = c.page->data[0] * 256 + c.page->data[1] + 2;
Mario Danic's avatar
Mario Danic committed
552 553 554 555 556 557 558 559 560 561 562 563 564
	m = d->mdata;
	page = c.page->data + 8;
	d->params.retries = page[3];
	m->retry_page_length = page[1];
	m->retry_page_valid = 1;
}

void spc_select_error_params(struct burn_drive *d,
			     const struct burn_read_opts *o)
{
	struct buffer buf;
	struct command c;

565
	mmc_start_if_needed(d, 1);
566 567 568
	if (mmc_function_spy(d, "select_error_params") <= 0)
		return;

569 570
	scsi_init_command(&c, SPC_MODE_SELECT, sizeof(SPC_MODE_SELECT));
/*
Mario Danic's avatar
Mario Danic committed
571 572
	memcpy(c.opcode, SPC_MODE_SELECT, sizeof(SPC_MODE_SELECT));
	c.oplen = sizeof(SPC_MODE_SELECT);
573 574
*/
	c.retry = 1;
Mario Danic's avatar
Mario Danic committed
575 576 577 578
	c.opcode[8] = 8 + 2 + d->mdata->retry_page_length;
	c.page = &buf;
	c.page->bytes = 0;
	c.page->sectors = 0;
579 580 581 582

	/* ts A61007 : moved up to only caller burn_disc_read() */
	/* a ssert(d->mdata->valid); */

Mario Danic's avatar
Mario Danic committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	memset(c.page->data, 0, 8 + 2 + d->mdata->retry_page_length);
	c.page->bytes = 8 + 2 + d->mdata->retry_page_length;
	c.page->data[8] = 1;
	c.page->data[9] = d->mdata->retry_page_length;
	if (o->transfer_damaged_blocks)
		c.page->data[10] |= 32;
	if (o->report_recovered_errors)
		c.page->data[10] |= 4;
	if (!o->hardware_error_recovery)
		c.page->data[10] |= 1;
/*burn_print(1, "error parameter 0x%x\n", c->page->data[10]);*/
	c.page->data[11] = d->params.retries;
	c.dir = TO_DRIVE;
	d->issue_command(d, &c);
}

void spc_sense_write_params(struct burn_drive *d)
{
	struct buffer buf;
	struct scsi_mode_data *m;
603
	int size, dummy, alloc_len = 10;
Mario Danic's avatar
Mario Danic committed
604 605 606
	unsigned char *page;
	struct command c;

607
	mmc_start_if_needed(d, 1);
608 609 610
	if (mmc_function_spy(d, "sense_write_params") <= 0)
		return;

611 612 613
	/* ts A61007 : Done in soft at only caller burn_drive_grab() */
	/* a ssert(d->mdata->cdr_write || d->mdata->cdrw_write ||
	       d->mdata->dvdr_write || d->mdata->dvdram_write); */
Mario Danic's avatar
Mario Danic committed
614

615 616
	scsi_init_command(&c, SPC_MODE_SENSE, sizeof(SPC_MODE_SENSE));
/*
Mario Danic's avatar
Mario Danic committed
617 618
	memcpy(c.opcode, SPC_MODE_SENSE, sizeof(SPC_MODE_SENSE));
	c.oplen = sizeof(SPC_MODE_SENSE);
619 620 621 622 623
*/
	c.dxfer_len = alloc_len;
	c.opcode[7] = (c.dxfer_len >> 8) & 0xff;
	c.opcode[8] = c.dxfer_len & 0xff;
	c.retry = 1;
Mario Danic's avatar
Mario Danic committed
624 625 626 627 628 629 630
	c.opcode[2] = 0x05;
	c.page = &buf;
	c.page->bytes = 0;
	c.page->sectors = 0;
	c.dir = FROM_DRIVE;
	d->issue_command(d, &c);

631
	/* ts A71128 : do not interpret reply if error */
Mario Danic's avatar
Mario Danic committed
632
	m = d->mdata;
633
	if (!c.error) {
634
		size = c.page->data[0] * 256 + c.page->data[1] + 2;
635 636 637 638 639 640
		page = c.page->data + 8;
		burn_print(1, "write page length 0x%x\n", page[1]);
		m->write_page_length = page[1];
		m->write_page_valid = 1;
	} else
		m->write_page_valid = 0;
Mario Danic's avatar
Mario Danic committed
641
	mmc_read_disc_info(d);
642 643 644

	/* ts A70212 : try to setup d->media_capacity_remaining */
	if (d->current_profile == 0x1a || d->current_profile == 0x13 ||
645
	    d->current_profile == 0x12 || d->current_profile == 0x43)
646 647 648 649 650 651 652 653 654 655 656 657
		d->read_format_capacities(d, -1);
	else if (d->status == BURN_DISC_BLANK ||
	     (d->current_is_cd_profile && d->status == BURN_DISC_APPENDABLE)) {
		d->get_nwa(d, -1, &dummy, &dummy);
	}
	/* others are hopefully up to date from mmc_read_disc_info() */

/*
        fprintf(stderr, "LIBBURN_DEBUG: media_capacity_remaining = %.f\n",
                (double) d->media_capacity_remaining);
*/

Mario Danic's avatar
Mario Danic committed
658 659
}

660

661 662 663
/* remark ts A61104 :
Although command MODE SELECT is SPC, the content of the
Write Parameters Mode Page (05h) is MMC (Table 108 in MMC-1). 
Thomas Schmitt's avatar
Thomas Schmitt committed
664
Thus the filling of the mode page is done by mmc_compose_mode_page_5().
665
*/
Mario Danic's avatar
Mario Danic committed
666 667 668 669 670
void spc_select_write_params(struct burn_drive *d,
			     const struct burn_write_opts *o)
{
	struct buffer buf;
	struct command c;
671
	int alloc_len;
Mario Danic's avatar
Mario Danic committed
672

673
	mmc_start_if_needed(d, 1);
674 675 676
	if (mmc_function_spy(d, "select_write_params") <= 0)
		return;

677 678 679
	/* ts A61007 : All current callers are safe. */
	/* a ssert(o->drive == d); */

680 681 682 683 684 685 686
	/* <<< A61030
	fprintf(stderr,"libburn_debug: write_type=%d  multi=%d  control=%d\n",
		o->write_type,o->multi,o->control);
	fprintf(stderr,"libburn_debug: block_type=%d  spc_block_type=%d\n",
		o->block_type,spc_block_type(o->block_type));
	*/

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
	alloc_len = 8 + 2 + d->mdata->write_page_length;
	memset(&(buf.data), 0, alloc_len);

#ifdef Libburn_pioneer_dvr_216d_load_mode5

	scsi_init_command(&c, SPC_MODE_SENSE, sizeof(SPC_MODE_SENSE));
	c.dxfer_len = alloc_len;
	c.opcode[7] = (alloc_len >> 8) & 0xff;
	c.opcode[8] = alloc_len & 0xff;
	c.retry = 1;
	c.opcode[2] = 0x05;
	c.page = &buf;
	c.page->bytes = 0;
	c.page->sectors = 0;
	c.dir = FROM_DRIVE;
	d->issue_command(d, &c);

	if (c.error) 
		memset(&(buf.data), 0,
				8 + 2 + d->mdata->write_page_length);

#endif /* Libburn_pioneer_dvr_216d_load_mode5 */

710 711
	scsi_init_command(&c, SPC_MODE_SELECT, sizeof(SPC_MODE_SELECT));
	c.retry = 1;
712 713
	c.opcode[7] = (alloc_len >> 8) & 0xff;
	c.opcode[8] = alloc_len & 0xff;
Mario Danic's avatar
Mario Danic committed
714 715 716
	c.page = &buf;
	c.page->bytes = 0;
	c.page->sectors = 0;
717 718 719 720

	/* ts A61007 : moved up to burn_disc_write() */
	/* a ssert(d->mdata->valid); */

721
	c.page->bytes = alloc_len;
Mario Danic's avatar
Mario Danic committed
722 723 724

	burn_print(12, "using write page length %d (valid %d)\n",
		   d->mdata->write_page_length, d->mdata->write_page_valid);
725 726 727 728 729

	/* ts A61229 */
	if (mmc_compose_mode_page_5(d, o, c.page->data + 8) <= 0)
		return;

Mario Danic's avatar
Mario Danic committed
730 731 732 733 734 735
	c.dir = TO_DRIVE;
	d->issue_command(d, &c);
}

void spc_getcaps(struct burn_drive *d)
{
736 737 738
	if (mmc_function_spy(d, "getcaps") <= 0)
		return;

Mario Danic's avatar
Mario Danic committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
	spc_inquiry(d);
	spc_sense_caps(d);
	spc_sense_error_params(d);
}

/*
don't check totally stupid modes (raw/raw0)
some drives say they're ok, and they're not.
*/

void spc_probe_write_modes(struct burn_drive *d)
{
	struct buffer buf;
	int try_write_type = 1;
	int try_block_type = 0;
754 755
	int key, asc, ascq, useable_write_type = -1, useable_block_type = -1;
	int last_try = 0;
Mario Danic's avatar
Mario Danic committed
756 757
	struct command c;

758
	mmc_start_if_needed(d, 1);
759 760 761
	if (mmc_function_spy(d, "spc_probe_write_modes") <= 0)
		return;

762 763
	/* ts A70213 : added pseudo try_write_type 4 to set a suitable mode */
	while (try_write_type != 5) {
Mario Danic's avatar
Mario Danic committed
764 765
		burn_print(9, "trying %d, %d\n", try_write_type,
			   try_block_type);
766 767 768 769 770 771 772 773 774 775

		/* ts A70213 */
		if (try_write_type == 4) {
			/* Pseudo write type NONE . Set a useable write mode */
			if (useable_write_type == -1)
	break;
			try_write_type = useable_write_type;
			try_block_type = useable_block_type;
			last_try= 1;
		}
776

777 778
		scsi_init_command(&c, SPC_MODE_SELECT,sizeof(SPC_MODE_SELECT));
		c.retry = 1;
Mario Danic's avatar
Mario Danic committed
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
		c.opcode[8] = 8 + 2 + 0x32;
		c.page = &buf;

		memset(c.page->data, 0, 8 + 2 + 0x32);
		c.page->bytes = 8 + 2 + 0x32;

		c.page->data[8] = 5;
		c.page->data[9] = 0x32;
		c.page->data[10] = try_write_type;
		if (try_block_type > 4)
			c.page->data[11] = 4;
		else
			c.page->data[11] = 0;
		c.page->data[12] = try_block_type;
		c.page->data[23] = 150;
		c.dir = TO_DRIVE;
795 796

		d->silent_on_scsi_error = 1;
Mario Danic's avatar
Mario Danic committed
797
		d->issue_command(d, &c);
798
		d->silent_on_scsi_error = 0;
Mario Danic's avatar
Mario Danic committed
799

800 801 802
		if (last_try)
	break;

Mario Danic's avatar
Mario Danic committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816
		key = c.sense[2];
		asc = c.sense[12];
		ascq = c.sense[13];
		if (key)
			burn_print(7, "%d not supported\n", try_block_type);
		else {
			burn_print(7, "%d:%d SUPPORTED MODE!\n",
				   try_write_type, try_block_type);
			if (try_write_type == 2)	/* sao */
				d->block_types[try_write_type] =
					BURN_BLOCK_SAO;
			else
				d->block_types[try_write_type] |=
					1 << try_block_type;
817 818 819 820 821 822 823 824 825

			/* ts A70213 */
			if ((useable_write_type < 0 && try_write_type > 0) ||
			    (try_write_type == 1 && try_block_type == 8)) {
			 	/* Packet is not supported yet.
				   Prefer TAO MODE_1. */
				useable_write_type = try_write_type;
				useable_block_type = try_block_type;
			} 
Mario Danic's avatar
Mario Danic committed
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
		}
		switch (try_block_type) {
		case 0:
		case 1:
		case 2:
			try_block_type++;
			break;
		case 3:
			try_block_type = 8;
			break;
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
			try_block_type++;
			break;
		case 13:
			try_block_type = 0;
			try_write_type++;
			break;
		default:
			return;
		}
	}
851

Mario Danic's avatar
Mario Danic committed
852 853
}

854 855
/* ( ts A61229 : shouldn't this go to mmc.c too ?) */

856
/** @return -1 = error */
Mario Danic's avatar
Mario Danic committed
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
int spc_block_type(enum burn_block_types b)
{
	switch (b) {
	case BURN_BLOCK_SAO:
		return 0;	/* ignored bitz */
	case BURN_BLOCK_RAW0:
		return 0;
	case BURN_BLOCK_RAW16:
		return 1;
	case BURN_BLOCK_RAW96P:
		return 2;
	case BURN_BLOCK_RAW96R:
		return 3;
	case BURN_BLOCK_MODE1:
		return 8;
	case BURN_BLOCK_MODE2R:
		return 9;
	case BURN_BLOCK_MODE2_PATHETIC:
		return 10;
	case BURN_BLOCK_MODE2_LAME:
		return 11;
	case BURN_BLOCK_MODE2_OBSCURE:
		return 12;
	case BURN_BLOCK_MODE2_OK:
		return 13;
882 883
	default:
		return -1;
Mario Danic's avatar
Mario Danic committed
884
	}
885 886
	/* ts A61007 : already prevented in burn_write_opts_set_write_type() */
	/* a ssert(0); */;
Mario Danic's avatar
Mario Danic committed
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

/* ts A61021 : the spc specific part of sg.c:enumerate_common()
*/
int spc_setup_drive(struct burn_drive *d)
{
	d->getcaps = spc_getcaps;
	d->lock = spc_prevent;
	d->unlock = spc_allow;
	d->read_disc_info = spc_sense_write_params;
	d->get_erase_progress = spc_get_erase_progress;
	d->test_unit_ready = spc_test_unit_ready;
	d->probe_write_modes = spc_probe_write_modes;
	d->send_parameters = spc_select_error_params;
	d->send_write_parameters = spc_select_write_params;
	return 1;	
}

/* ts A61021 : the general SCSI specific part of sg.c:enumerate_common()
   @param flag Bitfiled for control purposes
               bit0= do not setup spc/sbc/mmc
*/
int burn_scsi_setup_drive(struct burn_drive *d, int bus_no, int host_no,
			int channel_no, int target_no, int lun_no, int flag)
{
	int ret;

        /* ts A60923 */
	d->bus_no = bus_no;
	d->host = host_no;
	d->id = target_no;
	d->channel = channel_no;
	d->lun = lun_no;

921 922 923 924
	/* ts A61106 */
	d->silent_on_scsi_error = 0;


925 926
	d->idata = calloc(1, sizeof(struct burn_scsi_inquiry_data));
	d->mdata = calloc(1, sizeof(struct scsi_mode_data));
927 928

	/* ts A61007 : obsolete Assert in drive_getcaps() */
929
	if (d->idata == NULL || d->mdata == NULL) {
930 931 932 933 934
	        libdax_msgs_submit(libdax_messenger, -1, 0x00020108,
	                LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
	                "Could not allocate new drive object", 0, 0);
		return -1;
	}
935 936 937
	d->idata->valid = 0;
	d->mdata->valid = 0;
	d->mdata->speed_descriptors = NULL;
938
	if (!(flag & 1)) {
939 940 941 942 943 944 945 946 947 948 949 950
		ret = spc_setup_drive(d);
		if (ret<=0)
			return ret;
		ret = sbc_setup_drive(d);
		if (ret<=0)
			return ret;
		ret = mmc_setup_drive(d);
		if (ret<=0)
			return ret;
	}
	return 1;
}
951 952


953
/* ts A61122 - A80829 */
954
enum response scsi_error_msg(struct burn_drive *d, unsigned char *sense,
955
			     int senselen, char msg_data[161],
956
			     int *key, int *asc, int *ascq)
957
{
958
	char *msg;
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	static char key_def[16][40] = {
		"(no specific error)",
		"Recovered error",
		"Drive not ready",
		"Medium error",
		"Drive error",
		"Illegal request",
		"Drive event",
		"Data protected",
		"Blank/Nonblank",
		"Vendor specific code",
		"Copy aborted",
		"Command aborted",
		"(obsolete error code)",
		"Volume overflow",
		"Miscompare",
		"(reserved error code)",
	};
977 978

	msg= msg_data;
979
	*key= *asc= *ascq= -1;
980

981
	if (senselen<=0 || senselen>2)
982
		*key = sense[2] & 0x0f;
983 984 985 986
	if (senselen<=0 || senselen>12)
		*asc = sense[12];
	if (senselen<=0 || senselen>13)
		*ascq = sense[13];
987

988
	sprintf(msg, "[%X %2.2X %2.2X] ", *key, *asc, *ascq);
989 990
	msg= msg + strlen(msg);

991
	burn_print(12, "CONDITION: 0x%x 0x%x 0x%x on %s %s\n",
992
		   *key, *asc, *ascq, d->idata->vendor, d->idata->product);
993

994
	switch (*asc) {
995 996
	case 0x00:
		sprintf(msg, "(No error reported by SCSI transaction)");
997 998
		return RETRY;

999 1000
	case 0x02:
		sprintf(msg, "Not ready");
1001
		return RETRY;
1002
	case 0x04:
1003 1004
		if (*ascq == 1)
			sprintf(msg,
1005
			"Logical unit is in the process of becoming ready");
1006 1007
		else
			sprintf(msg, "Logical unit is not ready");
1008
		return RETRY;
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	case 0x08:
		if (*key != 4)
			break;
		if (*ascq == 0)
			sprintf(msg, "Logical unit communication failure");
		else if (*ascq == 1)
			sprintf(msg, "Logical unit communication timeout");
		else if (*ascq == 2)
			sprintf(msg, "Logical unit communication parity error");
		else if (*ascq == 3)
			sprintf(msg, "Logical unit communication crc error");
		return RETRY;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	case 0x09:
		if (*key != 4)
			break;
		if (*ascq == 0)
			sprintf(msg, "Track following error");
		else if (*ascq == 1)
			sprintf(msg, "Tracking servo failure");
		else if (*ascq == 2)
			sprintf(msg, "Focus servo failure");
		else if (*ascq == 3)
			sprintf(msg, "Spindle servo failure");
		else if (*ascq == 4)
			sprintf(msg, "Head select fault");
		else
			break;
		return FAIL;
	case 0x0C:
		if (*key == 2 && *ascq == 7)
			sprintf(msg, "Write error, recovery needed"); 
		else if (*key == 2 && *ascq == 0x0f)
			sprintf(msg, "Defects in error window"); 
		else if (*key == 3 && *ascq == 2)
			sprintf(msg, "Write error, auto reallocation failed");
		else if (*key == 3 && *ascq == 9)
			sprintf(msg, "Write error, loss of streaming");
		else if (*key == 3)
			sprintf(msg, "Write error");
		else
			break;
		return FAIL;
	case 0x11:
		if (*key != 3)
			break;
		if (*ascq == 0)
			sprintf(msg, "Unrecovered read error");
		else if (*ascq == 1)
			sprintf(msg, "Read retries exhausted");
		else if (*ascq == 2)
			sprintf(msg, "Error too long to correct");
		else if (*ascq == 5)
			sprintf(msg, "L-EC uncorrectable error");
		else if (*ascq == 6)
			sprintf(msg, "CIRC uncorrectable error");
		else
			break;
		return FAIL;
	case 0x15:
		if (*key != 3 && *key != 4)
			break;
		sprintf(msg, "Random positioning error");
		return FAIL;
	case 0x1a:
		if (*key != 5)
			break;
		sprintf(msg, "Parameter list length error");
		return FAIL;
	case 0x1b:
		if (*key != 4)
			break;
		sprintf(msg, "Synchronous data transfer error");
		return FAIL;
1082
	case 0x20:
1083 1084 1085
		if (*key != 5)
			break;
		sprintf(msg, "Invalid command operation code");
1086 1087
		return FAIL;
	case 0x21:
1088 1089 1090 1091 1092 1093 1094 1095
		if (*key != 5)
			break;
		if (*ascq == 0)
			sprintf(msg, "Lba out of range");
		else if (*ascq == 3)
			sprintf(msg, "Invalid write crossing layer jump");
		else
			sprintf(msg, "Invalid address");
1096 1097
		return FAIL;
	case 0x24:
1098
		if (*key != 5)
1099
			break;
1100
		sprintf(msg, "Invalid field in cdb");
1101 1102
		return FAIL;
	case 0x26:
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		if (*key != 5)
			break;
		if (*ascq == 1)
			sprintf(msg, "Parameter not supported");
		else if (*ascq == 2)
			sprintf(msg, "Parameter value invalid");
		else
			sprintf(msg, "Invalid field in parameter list");
		return FAIL;
	case 0x27:
		if (*key != 7)
			break;
		sprintf(msg, "Write protected");
1116 1117
		return FAIL;
	case 0x28:
1118 1119 1120
		if (*key != 6)
			break;
		if (*ascq == 0)
1121
			sprintf(msg, "Medium may have changed");
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		else if (*ascq == 2)
			sprintf(msg, "Format layer may have changed");
		else
			break;
		return RETRY;
	case 0x29:
		if (*key != 6)
			break;
		if (*ascq == 0)
			sprintf(msg,
                               "Power on, reset, or bus device reset occured");
		else if (*ascq == 1)
			sprintf(msg, "Power on occured");
		else if (*ascq == 2)
			sprintf(msg, "Bus reset occured");
		else if (*ascq == 3)
			sprintf(msg, "Bus device reset function occured");
		else if (*ascq == 4)
			sprintf(msg, "Device internal reset");
1141 1142 1143
		else
			break;
		return RETRY;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	case 0x2c:
		if (*key != 5)
			break;
		if (*ascq == 0)
			sprintf(msg, "Command sequence error");
		else 
			break;
		return FAIL;
	case 0x2e:
		if (*key != 6)
			break;
		if (*ascq == 0)
			sprintf(msg,
                               "Insufficient time for operation");
		else 
			break;
		return FAIL;
	case 0x30:
1162
		if (*key != 2 && *key != 5)
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
			break;
		if (*ascq == 1)
			sprintf(msg, "Cannot read medium, unknown format");
		else if (*ascq == 2)
			sprintf(msg,
				"Cannot read medium, incompatible format");
		else if (*ascq == 4)
			sprintf(msg, "Cannot write medium, unknown format");
		else if (*ascq == 5)
			sprintf(msg,
				"Cannot write medium, incompatible format");
		else if (*ascq == 6)
			sprintf(msg,
				"Cannot format medium, incompatible medium");
		else if (*ascq == 7)
			sprintf(msg, "Cleaning failure");
		else
			sprintf(msg, "Incompatible medium installed");
		return FAIL;
1182 1183 1184 1185 1186 1187 1188 1189
	case 0x31:
		if (*key != 3)
			break;
		if (*ascq == 0)
			sprintf(msg, "Medium unformatted or format corrupted");
		else if (*ascq == 1)
			sprintf(msg, "Format command failed");
		return FAIL;
1190
	case 0x3A:
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
		if (*key != 2)
			break;
		if (*ascq == 1)
			sprintf(msg, "Medium not present, tray closed");
		else if (*ascq == 2)
			sprintf(msg, "Medium not present, tray open");
		else if (*ascq == 3)
			sprintf(msg, "Medium not present, loadable");
		else
			sprintf(msg, "Medium not present");
1201 1202
		d->status = BURN_DISC_EMPTY;
		return FAIL;
1203 1204 1205 1206 1207
	case 0x57:
		if (*key != 3 || *ascq != 0)
			break;
		sprintf(msg, "Unable to recover Table-of-Content");
		return FAIL;
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	case 0x63:
		if (*key != 5)
			break;
		if (*ascq == 0)
			sprintf(msg,
				"End of user area encountered on this track");
		else if (*ascq == 1)
			sprintf(msg, "Packet does not fit in available space");
		else
			break;
		return FAIL;
	case 0x64:
		if (*key != 5)
			break;
		if (*ascq == 0)
			sprintf(msg, "Illegal mode for this track");
		else if (*ascq == 1)
			sprintf(msg, "Invalid packet size");
		else
			break;
		return FAIL;
	case 0x72:
		if (*key == 3)
			sprintf(msg, "Session fixation error");
		else if (*key == 5 && *ascq == 3)
			sprintf(msg,
			"Session fixation error, incomplete track in session");
		else if (*key == 5 && *ascq == 4)
			sprintf(msg,
			"Empty or partially written reserved track");
		else if (*key == 5 && *ascq == 5)
			sprintf(msg,
			"No more track reservations allowed");
		else
			break;
		return FAIL;
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	case 0x73:
		if (*key == 3 && *ascq == 0)
			sprintf(msg, "CD control error");
		else if (*key == 3 && *ascq == 2)
			sprintf(msg, "Power calibration area is full");
		else if (*key == 3 && *ascq == 3)
			sprintf(msg, "Power calibration area error");
		else if (*key == 3 && *ascq == 4)
			sprintf(msg, "Program memory area update failure");
		else if (*key == 3 && *ascq == 5)
			sprintf(msg, "Program memory area is full");
		else
			break;
		return FAIL;
1258
	}
1259
	sprintf(msg_data,
1260 1261
		"See MMC specs: Sense Key %X \"%s\", ASC %2.2X ASCQ %2.2X",
		*key & 0xf, key_def[(*key) & 0xf], *asc, *ascq);
1262 1263 1264 1265
	return FAIL;
}


1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
/* ts A61115 moved from sg-*.c */
/* ts A61122 made it frontend to scsi_error_msg() */
enum response scsi_error(struct burn_drive *d, unsigned char *sense,
			 int senselen)
{
	int key, asc, ascq;
	char msg[160];
	enum response resp;

	resp = scsi_error_msg(d, sense, senselen, msg, &key, &asc, &ascq);
	if (asc == 0 || asc == 0x3A)
		burn_print(12, "%s\n", msg);
	else
		burn_print(1, "%s\n", msg);
	return resp;
}


1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
static char *scsi_command_name(unsigned int c, int flag)
{
	switch (c) {
        case 0x00:
		return "TEST UNIT READY";
        case 0x03:
		return "REQUEST SENSE";
        case 0x04:
		return "FORMAT UNIT";
        case 0x1b:
		return "START/STOP UNIT";
1295 1296
	case 0x12:
		return "INQUIRY";
1297 1298 1299 1300
	case 0x1e:
		return "PREVENT/ALLOW MEDIA REMOVAL";
        case 0x23:
		return "READ FORMAT CAPACITIES";
1301 1302
        case 0x25:
		return "READ CAPACITY";
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
        case 0x28:
		return "READ(10)";
        case 0x2a:
		return "WRITE(10)";
        case 0x35:
		return "SYNCHRONIZE CACHE";
        case 0x43:
		return "READ TOC/PMA/ATIP";
        case 0x46:
		return "GET CONFIGURATION";
        case 0x4a:
		return "GET EVENT STATUS NOTIFICATION";
        case 0x51:
		return "READ DISC INFORMATION";
        case 0x52:
		return "READ TRACK INFORMATION";
        case 0x53:
		return "RESERVE TRACK";
        case 0x54:
		return "SEND OPC INFORMATION";
        case 0x55:
		return "MODE SELECT";
	case 0x5a:
1326
		return "MODE SENSE";
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
        case 0x5b:
		return "CLOSE TRACK/SESSION";
        case 0x5c:
		return "READ BUFFER CAPACITY";
        case 0x5d:
		return "SEND CUE SHEET";
        case 0xa1:
		return "BLANK";
        case 0xaa:
		return "WRITE(12)";
        case 0xac:
		return "GET PERFORMANCE";
1339 1340
        case 0xad:
		return "READ DISC STRUCTURE";
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
        case 0xb6:
		return "SET STREAMING";
        case 0xbb:
		return "SET CD SPEED";
        case 0xbe:
		return "READ CD";
	}
	return "(NOT IN COMMAND LIST)";
}


1352 1353 1354 1355 1356 1357
/* ts A61030 - A61115 */
/* @param flag bit0=do report conditions which are considered not an error */
int scsi_notify_error(struct burn_drive *d, struct command *c,
                      unsigned char *sense, int senselen, int flag)
{
	int key= -1, asc= -1, ascq= -1, ret;
1358
	char msg[320],scsi_msg[160];
1359 1360 1361 1362

	if (d->silent_on_scsi_error)
		return 1;

1363
	scsi_error_msg(d, sense, senselen, scsi_msg, &key, &asc, &ascq);
1364

1365
	if (!(flag & 1)) {
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
		/* SPC : TEST UNIT READY command */
		if (c->opcode[0] == 0)
			return 1;
		/* MMC : READ DISC INFORMATION command */
		if (c->opcode[0] == 0x51)
			if (key == 0x2 && asc == 0x3A &&
			    ascq>=0 && ascq <= 0x02) /* MEDIUM NOT PRESENT */
				return 1;
	}

1376 1377 1378
	sprintf(msg, "SCSI error condition on command %2.2Xh %s: ",
		c->opcode[0],
		scsi_command_name((unsigned int) c->opcode[0], 0));
1379 1380

#ifdef NIX
1381 1382 1383 1384 1385 1386 1387 1388
	if (key>=0)
		sprintf(msg+strlen(msg), " key=%Xh", key);
	if (asc>=0)
		sprintf(msg+strlen(msg), " asc=%2.2Xh", asc);
	if (ascq>=0)
		sprintf(msg+strlen(msg), " ascq=%2.2Xh", ascq);
	ret = libdax_msgs_submit(libdax_messenger, d->global_index, 0x0002010f,
			LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg,0,0);
1389 1390 1391 1392 1393
	if (ret < 0)
		return ret;
	ret = libdax_msgs_submit(libdax_messenger, d->global_index, 0x0002010f,
			LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
			scsi_msg,0,0);
1394 1395 1396 1397 1398 1399 1400
#else
	strcat(msg, scsi_msg);
	ret = libdax_msgs_submit(libdax_messenger, d->global_index, 0x0002010f,
			LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg,0,0);

#endif /* NIX */

1401 1402 1403
	return ret;
}

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466

/* ts A91106 */
/* @param flag bit0= do not show eventual data payload sent to the drive
                     (never with WRITE commands)
               bit1= show write length and target LBA in decimal
*/
int scsi_show_cmd_text(struct command *c, void *fp_in, int flag)
{
	int i;
	FILE *fp = fp_in;

	fprintf(fp, "\n%s\n",
		 scsi_command_name((unsigned int) c->opcode[0], 0));
	for(i = 0; i < 16 && i < c->oplen; i++)
		fprintf(fp, "%2.2x ", c->opcode[i]);
	if (i > 0)
		fprintf(fp, "\n");
	if (flag & 1)
		return 1;
	if (c->opcode[0] == 0x2A) { /* WRITE 10 */
		if (flag & 2)
			fprintf(fp, "%d -> %d\n",
				(c->opcode[7] << 8) | c->opcode[8], 
				mmc_four_char_to_int(c->opcode + 2));
	} else if (c->opcode[0] == 0xAA) { /* WRITE 12 */
		if (flag & 2)
			fprintf(fp, "%d -> %d\n",
				mmc_four_char_to_int(c->opcode + 6),
				mmc_four_char_to_int(c->opcode + 2));	
	} else if (c->dir == TO_DRIVE) {
		fprintf(fp, "To drive: %db\n", c->page->bytes);
		for (i = 0; i < c->page->bytes; i++) 
			fprintf(fp, "%2.2x%c", c->page->data[i],
				((i % 20) == 19 ? '\n' : ' '));
		if (i % 20)
			fprintf(fp, "\n");
	}
	return 1;
}

/* ts A91106 */
int scsi_show_cmd_reply(struct command *c, void *fp_in, int flag)
{
	int i;
	FILE *fp = fp_in;

	if (c->dir != FROM_DRIVE)
		return 2;
	if (c->opcode[0] == 0x28 || c->opcode[0] == 0x3C ||
	    c->opcode[0] == 0xA8 || c->opcode[0] == 0xBE) {
							/* READ commands */
		/* >>> report amount of data */;

		return 2;
	}
	fprintf(fp, "From drive: %db\n", c->dxfer_len);
	for (i = 0; i < c->dxfer_len; i++)
		fprintf(fp, "%2.2x%c", c->page->data[i],
			((i % 20) == 19 ? '\n' : ' '));
	if (i % 20)
		fprintf(fp, "\n");
	return 1;
}
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485


/* ts A91218 (former sg_log_cmd ts A70518) */ 
/** Logs command (before execution) */
int scsi_log_cmd(struct command *c, void *fp_in, int flag)
{
	FILE *fp = fp_in;

	if (fp != NULL && (fp == stderr || (burn_sg_log_scsi & 1))) {
		scsi_show_cmd_text(c, fp, 0);
		if (burn_sg_log_scsi & 4)
			fflush(fp);
	}
	if (fp == stderr || !(burn_sg_log_scsi & 2))
		return 1;
	scsi_log_cmd(c, stderr, flag);
	return 1;
}

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

/* ts A91221 (former sg_log_err ts A91108) */
/** Logs outcome of a sg command.
    @param flag  bit0 causes an error message
                 bit1 do not print duration
*/
int scsi_log_err(struct command *c, void *fp_in, unsigned char sense[18],
		 int duration, int flag)
{
	char durtxt[20];
	FILE *fp = fp_in;

      	if(fp != NULL && (fp == stderr || (burn_sg_log_scsi & 1))) {
		if(flag & 1) {
			durtxt[0] = 0;
			if (!(flag & 2))
  				sprintf(durtxt, "   (%6d ms)\n",duration);
  			fprintf(fp, "+++ key=%X  asc=%2.2Xh  ascq=%2.2Xh%s\n",
				sense[2], sense[12], sense[13], durtxt);

		} else {
			scsi_show_cmd_reply(c, fp, 0);
			if (!(flag & 2))
				fprintf(fp,"%6d ms\n", duration);
		}
		if (burn_sg_log_scsi & 4)
			fflush(fp);
	}
	if (fp == stderr || !(burn_sg_log_scsi & 2))
		return 1;
	scsi_log_err(c, stderr, sense, duration, flag);
	return 1;
}