1 /* -*-linux-c-*-
2 
3  * vendor-specific code for SCSI CD-ROM's goes here.
4  *
5  * This is needed becauce most of the new features (multisession and
6  * the like) are too new to be included into the SCSI-II standard (to
7  * be exact: there is'nt anything in my draft copy).
8  *
9  * Aug 1997: Ha! Got a SCSI-3 cdrom spec across my fingers. SCSI-3 does
10  *           multisession using the READ TOC command (like SONY).
11  *
12  *           Rearranged stuff here: SCSI-3 is included allways, support
13  *           for NEC/TOSHIBA/HP commands is optional.
14  *
15  *   Gerd Knorr <kraxel@cs.tu-berlin.de>
16  *
17  * --------------------------------------------------------------------------
18  *
19  * support for XA/multisession-CD's
20  *
21  *   - NEC:     Detection and support of multisession CD's.
22  *
23  *   - TOSHIBA: Detection and support of multisession CD's.
24  *              Some XA-Sector tweaking, required for older drives.
25  *
26  *   - SONY:    Detection and support of multisession CD's.
27  *              added by Thomas Quinot <thomas@cuivre.freenix.fr>
28  *
29  *   - PIONEER, HITACHI, PLEXTOR, MATSHITA, TEAC, PHILIPS: known to
30  *              work with SONY (SCSI3 now)  code.
31  *
32  *   - HP:      Much like SONY, but a little different... (Thomas)
33  *              HP-Writers only ??? Maybe other CD-Writers work with this too ?
34  *              HP 6020 writers now supported.
35  */
36 
37 #include <linux/config.h>
38 #include <linux/errno.h>
39 #include <linux/string.h>
40 
41 #include <linux/blk.h>
42 #include "scsi.h"
43 #include "hosts.h"
44 #include <scsi/scsi_ioctl.h>
45 
46 #include <linux/cdrom.h>
47 #include "sr.h"
48 
49 #if 0
50 #define DEBUG
51 #endif
52 
53 /* here are some constants to sort the vendors into groups */
54 
55 #define VENDOR_SCSI3           1	/* default: scsi-3 mmc */
56 
57 #define VENDOR_NEC             2
58 #define VENDOR_TOSHIBA         3
59 #define VENDOR_WRITER          4	/* pre-scsi3 writers */
60 
61 #define VENDOR_ID (scsi_CDs[minor].vendor)
62 
sr_vendor_init(int minor)63 void sr_vendor_init(int minor)
64 {
65 #ifndef CONFIG_BLK_DEV_SR_VENDOR
66 	VENDOR_ID = VENDOR_SCSI3;
67 #else
68 	char *vendor = scsi_CDs[minor].device->vendor;
69 	char *model = scsi_CDs[minor].device->model;
70 
71 	/* default */
72 	VENDOR_ID = VENDOR_SCSI3;
73 	if (scsi_CDs[minor].readcd_known)
74 		/* this is true for scsi3/mmc drives - no more checks */
75 		return;
76 
77 	if (scsi_CDs[minor].device->type == TYPE_WORM) {
78 		VENDOR_ID = VENDOR_WRITER;
79 
80 	} else if (!strncmp(vendor, "NEC", 3)) {
81 		VENDOR_ID = VENDOR_NEC;
82 		if (!strncmp(model, "CD-ROM DRIVE:25", 15) ||
83 		    !strncmp(model, "CD-ROM DRIVE:36", 15) ||
84 		    !strncmp(model, "CD-ROM DRIVE:83", 15) ||
85 		    !strncmp(model, "CD-ROM DRIVE:84 ", 16)
86 #if 0
87 		/* my NEC 3x returns the read-raw data if a read-raw
88 		   is followed by a read for the same sector - aeb */
89 		    || !strncmp(model, "CD-ROM DRIVE:500", 16)
90 #endif
91 		    )
92 			/* these can't handle multisession, may hang */
93 			scsi_CDs[minor].cdi.mask |= CDC_MULTI_SESSION;
94 
95 	} else if (!strncmp(vendor, "TOSHIBA", 7)) {
96 		VENDOR_ID = VENDOR_TOSHIBA;
97 
98 	}
99 #endif
100 }
101 
102 
103 /* small handy function for switching block length using MODE SELECT,
104  * used by sr_read_sector() */
105 
sr_set_blocklength(int minor,int blocklength)106 int sr_set_blocklength(int minor, int blocklength)
107 {
108 	unsigned char *buffer;	/* the buffer for the ioctl */
109 	unsigned char cmd[MAX_COMMAND_SIZE];	/* the scsi-command */
110 	struct ccs_modesel_head *modesel;
111 	int rc, density = 0;
112 
113 #ifdef CONFIG_BLK_DEV_SR_VENDOR
114 	if (VENDOR_ID == VENDOR_TOSHIBA)
115 		density = (blocklength > 2048) ? 0x81 : 0x83;
116 #endif
117 
118 	buffer = (unsigned char *) scsi_malloc(512);
119 	if (!buffer)
120 		return -ENOMEM;
121 
122 #ifdef DEBUG
123 	printk("sr%d: MODE SELECT 0x%x/%d\n", minor, density, blocklength);
124 #endif
125 	memset(cmd, 0, MAX_COMMAND_SIZE);
126 	cmd[0] = MODE_SELECT;
127 	cmd[1] = (scsi_CDs[minor].device->scsi_level <= SCSI_2) ?
128 	         (scsi_CDs[minor].device->lun << 5) : 0;
129 	cmd[1] |= (1 << 4);
130 	cmd[4] = 12;
131 	modesel = (struct ccs_modesel_head *) buffer;
132 	memset(modesel, 0, sizeof(*modesel));
133 	modesel->block_desc_length = 0x08;
134 	modesel->density = density;
135 	modesel->block_length_med = (blocklength >> 8) & 0xff;
136 	modesel->block_length_lo = blocklength & 0xff;
137 	if (0 == (rc = sr_do_ioctl(minor, cmd, buffer, sizeof(*modesel), 0, SCSI_DATA_WRITE, NULL))) {
138 		scsi_CDs[minor].device->sector_size = blocklength;
139 	}
140 #ifdef DEBUG
141 	else
142 		printk("sr%d: switching blocklength to %d bytes failed\n",
143 		       minor, blocklength);
144 #endif
145 	scsi_free(buffer, 512);
146 	return rc;
147 }
148 
149 /* This function gets called after a media change. Checks if the CD is
150    multisession, asks for offset etc. */
151 
152 #define BCD_TO_BIN(x)    ((((int)x & 0xf0) >> 4)*10 + ((int)x & 0x0f))
153 
sr_cd_check(struct cdrom_device_info * cdi)154 int sr_cd_check(struct cdrom_device_info *cdi)
155 {
156 	unsigned long sector;
157 	unsigned char *buffer;	/* the buffer for the ioctl */
158 	unsigned char cmd[MAX_COMMAND_SIZE];	/* the scsi-command */
159 	int rc, no_multi, minor;
160 
161 	minor = MINOR(cdi->dev);
162 	if (scsi_CDs[minor].cdi.mask & CDC_MULTI_SESSION)
163 		return 0;
164 
165 	buffer = (unsigned char *) scsi_malloc(512);
166 	if (!buffer)
167 		return -ENOMEM;
168 
169 	sector = 0;		/* the multisession sector offset goes here  */
170 	no_multi = 0;		/* flag: the drive can't handle multisession */
171 	rc = 0;
172 
173 	switch (VENDOR_ID) {
174 
175 	case VENDOR_SCSI3:
176 		memset(cmd, 0, MAX_COMMAND_SIZE);
177 		cmd[0] = READ_TOC;
178 		cmd[1] = (scsi_CDs[minor].device->scsi_level <= SCSI_2) ?
179 		         (scsi_CDs[minor].device->lun << 5) : 0;
180 		cmd[8] = 12;
181 		cmd[9] = 0x40;
182 		rc = sr_do_ioctl(minor, cmd, buffer, 12, 1, SCSI_DATA_READ, NULL);
183 		if (rc != 0)
184 			break;
185 		if ((buffer[0] << 8) + buffer[1] < 0x0a) {
186 			printk(KERN_INFO "sr%d: Hmm, seems the drive "
187 			   "doesn't support multisession CD's\n", minor);
188 			no_multi = 1;
189 			break;
190 		}
191 		sector = buffer[11] + (buffer[10] << 8) +
192 		    (buffer[9] << 16) + (buffer[8] << 24);
193 		if (buffer[6] <= 1) {
194 			/* ignore sector offsets from first track */
195 			sector = 0;
196 		}
197 		break;
198 
199 #ifdef CONFIG_BLK_DEV_SR_VENDOR
200 	case VENDOR_NEC:{
201 			unsigned long min, sec, frame;
202 			memset(cmd, 0, MAX_COMMAND_SIZE);
203 			cmd[0] = 0xde;
204 			cmd[1] = (scsi_CDs[minor].device->scsi_level <= SCSI_2) ?
205 			         (scsi_CDs[minor].device->lun << 5) : 0;
206 			cmd[1] |= 0x03;
207 			cmd[2] = 0xb0;
208 			rc = sr_do_ioctl(minor, cmd, buffer, 0x16, 1, SCSI_DATA_READ, NULL);
209 			if (rc != 0)
210 				break;
211 			if (buffer[14] != 0 && buffer[14] != 0xb0) {
212 				printk(KERN_INFO "sr%d: Hmm, seems the cdrom "
213 				       "doesn't support multisession CD's\n", minor);
214 				no_multi = 1;
215 				break;
216 			}
217 			min = BCD_TO_BIN(buffer[15]);
218 			sec = BCD_TO_BIN(buffer[16]);
219 			frame = BCD_TO_BIN(buffer[17]);
220 			sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
221 			break;
222 		}
223 
224 	case VENDOR_TOSHIBA:{
225 			unsigned long min, sec, frame;
226 
227 			/* we request some disc information (is it a XA-CD ?,
228 			 * where starts the last session ?) */
229 			memset(cmd, 0, MAX_COMMAND_SIZE);
230 			cmd[0] = 0xc7;
231 			cmd[1] = (scsi_CDs[minor].device->scsi_level <= SCSI_2) ?
232 			         (scsi_CDs[minor].device->lun << 5) : 0;
233 			cmd[1] |= 0x03;
234 			rc = sr_do_ioctl(minor, cmd, buffer, 4, 1, SCSI_DATA_READ, NULL);
235 			if (rc == -EINVAL) {
236 				printk(KERN_INFO "sr%d: Hmm, seems the drive "
237 				       "doesn't support multisession CD's\n", minor);
238 				no_multi = 1;
239 				break;
240 			}
241 			if (rc != 0)
242 				break;
243 			min = BCD_TO_BIN(buffer[1]);
244 			sec = BCD_TO_BIN(buffer[2]);
245 			frame = BCD_TO_BIN(buffer[3]);
246 			sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
247 			if (sector)
248 				sector -= CD_MSF_OFFSET;
249 			sr_set_blocklength(minor, 2048);
250 			break;
251 		}
252 
253 	case VENDOR_WRITER:
254 		memset(cmd, 0, MAX_COMMAND_SIZE);
255 		cmd[0] = READ_TOC;
256 		cmd[1] = (scsi_CDs[minor].device->scsi_level <= SCSI_2) ?
257 		         (scsi_CDs[minor].device->lun << 5) : 0;
258 		cmd[8] = 0x04;
259 		cmd[9] = 0x40;
260 		rc = sr_do_ioctl(minor, cmd, buffer, 0x04, 1, SCSI_DATA_READ, NULL);
261 		if (rc != 0) {
262 			break;
263 		}
264 		if ((rc = buffer[2]) == 0) {
265 			printk(KERN_WARNING
266 			       "sr%d: No finished session\n", minor);
267 			break;
268 		}
269 		cmd[0] = READ_TOC;	/* Read TOC */
270 		cmd[1] = (scsi_CDs[minor].device->scsi_level <= SCSI_2) ?
271 		         (scsi_CDs[minor].device->lun << 5) : 0;
272 		cmd[6] = rc & 0x7f;	/* number of last session */
273 		cmd[8] = 0x0c;
274 		cmd[9] = 0x40;
275 		rc = sr_do_ioctl(minor, cmd, buffer, 12, 1, SCSI_DATA_READ, NULL);
276 		if (rc != 0) {
277 			break;
278 		}
279 		sector = buffer[11] + (buffer[10] << 8) +
280 		    (buffer[9] << 16) + (buffer[8] << 24);
281 		break;
282 #endif				/* CONFIG_BLK_DEV_SR_VENDOR */
283 
284 	default:
285 		/* should not happen */
286 		printk(KERN_WARNING
287 		   "sr%d: unknown vendor code (%i), not initialized ?\n",
288 		       minor, VENDOR_ID);
289 		sector = 0;
290 		no_multi = 1;
291 		break;
292 	}
293 	scsi_CDs[minor].ms_offset = sector;
294 	scsi_CDs[minor].xa_flag = 0;
295 	if (CDS_AUDIO != sr_disk_status(cdi) && 1 == sr_is_xa(minor))
296 		scsi_CDs[minor].xa_flag = 1;
297 
298 	if (2048 != scsi_CDs[minor].device->sector_size) {
299 		sr_set_blocklength(minor, 2048);
300 	}
301 	if (no_multi)
302 		cdi->mask |= CDC_MULTI_SESSION;
303 
304 #ifdef DEBUG
305 	if (sector)
306 		printk(KERN_DEBUG "sr%d: multisession offset=%lu\n",
307 		       minor, sector);
308 #endif
309 	scsi_free(buffer, 512);
310 	return rc;
311 }
312