summaryrefslogtreecommitdiff
path: root/wodim/scsi_mmc.c
blob: f8baa1febb6d8b262f959b9404110ebceaadb095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * This file has been modified for the cdrkit suite.
 *
 * The behaviour and appearence of the program code below can differ to a major
 * extent from the version distributed by the original author(s).
 *
 * For details, see Changelog file distributed with the cdrkit package. If you
 * received this file from another source then ask the distributing person for
 * a log of modifications.
 *
 */

/* @(#)scsi_mmc.c	1.13 05/05/16 Copyright 2002-2005 J. Schilling */
/*
 *	SCSI command functions for cdrecord
 *	covering MMC-3 level and above
 *
 *	Copyright (c) 2002-2005 J. Schilling
 */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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; see the file COPYING.  If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
Includes code from http://libburnia.pykix.org/browser/libburn/trunk/libburn/mmc.c?format=txt 
*/

/*#define	DEBUG*/

#include <mconfig.h>

#include <stdio.h>
#include <standard.h>
#include <stdxlib.h>
#include <unixstd.h>
#include <fctldefs.h>
#include <errno.h>
#include <strdefs.h>
#include <timedefs.h>

#include <utypes.h>
#include <btorder.h>
#include <intcvt.h>
#include <schily.h>

#include <usal/usalcmd.h>
#include <usal/scsidefs.h>
#include <usal/scsireg.h>
#include <usal/scsitransp.h>

#include "scsimmc.h"
#include "wodim.h"

extern	int	xdebug;



int	get_configuration(SCSI *usalp, caddr_t bp, int cnt, int st_feature, 
								int rt);
static	int	get_conflen(SCSI *usalp, int st_feature, int rt);
int	get_curprofile(SCSI *usalp);
static	int	get_profiles(SCSI *usalp, caddr_t bp, int cnt);
int	print_profiles(SCSI *usalp);
int	get_proflist(SCSI *usalp, BOOL *wp, BOOL *cdp, BOOL *dvdp, BOOL *dvdplusp,
						 BOOL *ddcdp);
int	get_wproflist(SCSI *usalp, BOOL *cdp, BOOL *dvdp, BOOL *dvdplusp, 
						  BOOL *ddcdp);

/*
 * Get feature codes
 */
int
get_configuration(SCSI *usalp, caddr_t bp, int cnt, int st_feature, int rt)
{
	register struct	usal_cmd	*scmd = usalp->scmd;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	scmd->addr = bp;
	scmd->size = cnt;
	scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
	scmd->cdb_len = SC_G1_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g1_cdb.cmd = 0x46;
	scmd->cdb.g1_cdb.lun = usal_lun(usalp);
	if (rt & 1)
		scmd->cdb.g1_cdb.reladr  = 1;
	if (rt & 2)
		scmd->cdb.g1_cdb.res  = 1;

	i_to_2_byte(scmd->cdb.g1_cdb.addr, st_feature);
	g1_cdblen(&scmd->cdb.g1_cdb, cnt);

	usalp->cmdname = "get_configuration";

	return (usal_cmd(usalp));
}

/*
 * Retrieve feature code list length
 */
static int
get_conflen(SCSI *usalp, int st_feature, int rt)
{
	Uchar	cbuf[8];
	int	flen;
	int	i;

	fillbytes(cbuf, sizeof (cbuf), '\0');
	usalp->silent++;
	i = get_configuration(usalp, (char *)cbuf, sizeof (cbuf), st_feature, rt);
	usalp->silent--;
	if (i < 0)
		return (-1);
	i = sizeof (cbuf) - usal_getresid(usalp);
	if (i < 4)
		return (-1);

	flen = a_to_u_4_byte(cbuf);
	if (flen < 4)
		return (-1);
	return (flen);
}

int
get_curprofile(SCSI *usalp)
{
	Uchar	cbuf[8];
	int	amt;
	int	flen;
	int	profile;
	int	i;

	fillbytes(cbuf, sizeof (cbuf), '\0');
	usalp->silent++;
	i = get_configuration(usalp, (char *)cbuf, sizeof (cbuf), 0, 0);
	usalp->silent--;
	if (i < 0)
		return (-1);

	amt = sizeof (cbuf) - usal_getresid(usalp);
	if (amt < 8)
		return (-1);
	flen = a_to_u_4_byte(cbuf);
	if (flen < 4)
		return (-1);

	profile = a_to_u_2_byte(&cbuf[6]);

	if (xdebug > 1)
		usal_prbytes("Features: ", cbuf, amt);

	if (xdebug > 0)
		printf("feature len: %d current profile 0x%04X (%s) len %d\n",
				flen, profile,
				mmc_obtain_profile_name(profile), amt);

	return (profile);
}

static int
get_profiles(SCSI *usalp, caddr_t bp, int cnt)
{
	int	amt;
	int	flen;
	int	i;

	flen = get_conflen(usalp, 0, 0);
	if (flen < 0)
		return (-1);
	if (cnt < flen)
		flen = cnt;

	fillbytes(bp, cnt, '\0');
	usalp->silent++;
	i = get_configuration(usalp, (char *)bp, flen, 0, 0);
	usalp->silent--;
	if (i < 0)
		return (-1);
	amt = flen - usal_getresid(usalp);

	flen = a_to_u_4_byte(bp);
	if ((flen+4) < amt)
		amt = flen+4;

	return (amt);
}

int
print_profiles(SCSI *usalp)
{
	Uchar	cbuf[1024];
	Uchar	*p;
	int	flen;
	int	curprofile;
	int	profile;
	int	i;
	int	n;

	flen = get_profiles(usalp, (caddr_t)cbuf, sizeof (cbuf));
	if (flen < 0)
		return (-1);

	p = cbuf;
	if (xdebug > 1)
		usal_prbytes("Features: ", cbuf, flen);

	curprofile = a_to_u_2_byte(&p[6]);
	if (xdebug > 0)
		printf("feature len: %d current profile 0x%04X (%s)\n",
				flen, curprofile,
				mmc_obtain_profile_name(curprofile));

	printf("Current: 0x%04X (%s)\n", curprofile,
			mmc_obtain_profile_name(curprofile));

	p += 8;		/* Skip feature header	*/
	n = p[3];	/* Additional length	*/
	n /= 4;
	p += 4;

	for (i = 0; i < n; i++) {
		profile = a_to_u_2_byte(p);
		if (xdebug > 0)
			printf("Profile: 0x%04X (%s)", profile,
					mmc_obtain_profile_name(profile));
		else
			printf("Profile: ");
		printf("0x%04X (%s) %s\n", profile,
				mmc_obtain_profile_name(profile), 
				p[2] & 1 ? "(current)":"");
		p += 4;
	}
	return (curprofile);
}

int
get_proflist(SCSI *usalp, BOOL *wp, BOOL *cdp, BOOL *dvdp, BOOL *dvdplusp, 
             BOOL *ddcdp)
{
	Uchar	cbuf[1024];
	Uchar	*p;
	int	flen;
	int	curprofile;
	int	profile;
	int	i;
	int	n;
	BOOL	wr	= FALSE;
	BOOL	cd	= FALSE;
	BOOL	dvd	= FALSE;
	BOOL	dvdplus	= FALSE;
	BOOL	ddcd	= FALSE;

	flen = get_profiles(usalp, (caddr_t)cbuf, sizeof (cbuf));
	if (flen < 0)
		return (-1);

	p = cbuf;
	if (xdebug > 1)
		usal_prbytes("Features: ", cbuf, flen);

	curprofile = a_to_u_2_byte(&p[6]);
	if (xdebug > 0)
		printf("feature len: %d current profile 0x%04X (%s)\n",
				flen, curprofile, 
				mmc_obtain_profile_name(curprofile));

	p += 8;		/* Skip feature header	*/
	n = p[3];	/* Additional length	*/
	n /= 4;
	p += 4;

	for (i = 0; i < n; i++) {
		profile = a_to_u_2_byte(p);
		p += 4;
		if (profile >= 0x0008 && profile < 0x0010)
			cd = TRUE;
		if (profile > 0x0008 && profile < 0x0010)
			wr = TRUE;

		if (profile >= 0x0010 && profile < 0x0018)
			dvd = TRUE;
		if (profile > 0x0010 && profile < 0x0018)
			wr = TRUE;

		if (profile >= 0x0018 && profile < 0x0020)
			dvdplus = TRUE;
		if (profile > 0x0018 && profile < 0x0020)
			wr = TRUE;

		if (profile >= 0x0020 && profile < 0x0028)
			ddcd = TRUE;
		if (profile > 0x0020 && profile < 0x0028)
			wr = TRUE;
	}
	if (wp)
		*wp	= wr;
	if (cdp)
		*cdp	= cd;
	if (dvdp)
		*dvdp	= dvd;
	if (dvdplusp)
		*dvdplusp = dvdplus;
	if (ddcdp)
		*ddcdp	= ddcd;

	return (curprofile);
}

int
get_wproflist(SCSI *usalp, BOOL *cdp, BOOL *dvdp, BOOL *dvdplusp, BOOL *ddcdp)
{
	Uchar	cbuf[1024];
	Uchar	*p;
	int	flen;
	int	curprofile;
	int	profile;
	int	i;
	int	n;
	BOOL	cd	= FALSE;
	BOOL	dvd	= FALSE;
	BOOL	dvdplus	= FALSE;
	BOOL	ddcd	= FALSE;

	flen = get_profiles(usalp, (caddr_t)cbuf, sizeof (cbuf));
	if (flen < 0)
		return (-1);
	p = cbuf;
	curprofile = a_to_u_2_byte(&p[6]);

	p += 8;		/* Skip feature header	*/
	n = p[3];	/* Additional length	*/
	n /= 4;
	p += 4;

	for (i = 0; i < n; i++) {
		profile = a_to_u_2_byte(p);
		p += 4;
		if (profile > 0x0008 && profile < 0x0010)
			cd = TRUE;
		if (profile > 0x0010 && profile < 0x0018)
			dvd = TRUE;
		if (profile > 0x0018 && profile < 0x0020)
			dvdplus = TRUE;
		if (profile > 0x0020 && profile < 0x0028)
			ddcd = TRUE;
	}
	if (cdp)
		*cdp	= cd;
	if (dvdp)
		*dvdp	= dvd;
	if (dvdplusp)
		*dvdplusp = dvdplus;
	if (ddcdp)
		*ddcdp	= ddcd;

	return (curprofile);
}


char *mmc_obtain_profile_name(int profile_number) {
  static char *texts[0x53] = {NULL};
  static char *reserved="Reserved/Unknown";
  int i, max_pno = 0x53;
  
  if (texts[0] == NULL) {
    for (i = 0; i<max_pno; i++)
      texts[i] = reserved;
    /* mmc5r04c.pdf , Table 88, human readable profile names */
    texts[0x01] = "Non-removable disk";
    texts[0x02] = "Removable disk";
    texts[0x03] = "MO erasable";
    texts[0x04] = "Optical write once";
    texts[0x05] = "AS-MO";
    texts[0x08] = "CD-ROM";
    texts[0x09] = "CD-R";
    texts[0x0a] = "CD-RW";
    texts[0x10] = "DVD-ROM";
    texts[0x11] = "DVD-R sequential recording";
    texts[0x12] = "DVD-RAM";
    texts[0x13] = "DVD-RW restricted overwrite";
    texts[0x14] = "DVD-RW sequential recording";
    texts[0x15] = "DVD-R/DL sequential recording";
    texts[0x16] = "DVD-R/DL layer jump recording";
    texts[0x1a] = "DVD+RW";
    texts[0x1b] = "DVD+R";
    texts[0x2a] = "DVD+RW/DL";
    texts[0x2b] = "DVD+R/DL";
    texts[0x40] = "BD-ROM";
    texts[0x41] = "BD-R sequential recording";
    texts[0x42] = "BD-R random recording";
    texts[0x43] = "BD-RE";
    texts[0x50] = "HD-DVD-ROM";
    texts[0x51] = "HD-DVD-R";
    texts[0x52] = "HD-DVD-RAM";
  }
  if (profile_number<0 || profile_number>=max_pno)
    return "";
  return texts[profile_number];
}