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