1 // SPDX-License-Identifier: GPL-2.0
4 * vendor-specific code for SCSI CD-ROM's goes here.
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).
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).
13 * Rearranged stuff here: SCSI-3 is included allways, support
14 * for NEC/TOSHIBA/HP commands is optional.
16 * Gerd Knorr <kraxel@cs.tu-berlin.de>
18 * --------------------------------------------------------------------------
20 * support for XA/multisession-CD's
22 * - NEC: Detection and support of multisession CD's.
24 * - TOSHIBA: Detection and support of multisession CD's.
25 * Some XA-Sector tweaking, required for older drives.
27 * - SONY: Detection and support of multisession CD's.
28 * added by Thomas Quinot <thomas@cuivre.freenix.fr>
30 * - PIONEER, HITACHI, PLEXTOR, MATSHITA, TEAC, PHILIPS: known to
31 * work with SONY (SCSI3 now) code.
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.
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>
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>
57 /* here are some constants to sort the vendors into groups */
59 #define VENDOR_SCSI3 1 /* default: scsi-3 mmc */
62 #define VENDOR_TOSHIBA 3
63 #define VENDOR_WRITER 4 /* pre-scsi3 writers */
65 #define VENDOR_TIMEOUT 30*HZ
67 void sr_vendor_init(Scsi_CD *cd)
69 const char *vendor = cd->device->vendor;
70 const char *model = cd->device->model;
73 cd->vendor = VENDOR_SCSI3;
75 /* this is true for scsi3/mmc drives - no more checks */
78 if (cd->device->type == TYPE_WORM) {
79 cd->vendor = VENDOR_WRITER;
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)
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)
93 /* these can't handle multisession, may hang */
94 cd->cdi.mask |= CDC_MULTI_SESSION;
96 } else if (!strncmp(vendor, "TOSHIBA", 7)) {
97 cd->vendor = VENDOR_TOSHIBA;
103 /* small handy function for switching block length using MODE SELECT,
104 * used by sr_read_sector() */
106 int sr_set_blocklength(Scsi_CD *cd, int blocklength)
108 unsigned char *buffer; /* the buffer for the ioctl */
109 struct packet_command cgc;
110 struct ccs_modesel_head *modesel;
113 if (cd->vendor == VENDOR_TOSHIBA)
114 density = (blocklength > 2048) ? 0x81 : 0x83;
116 buffer = kmalloc(512, GFP_KERNEL);
121 sr_printk(KERN_INFO, cd, "MODE SELECT 0x%x/%d\n", density, blocklength);
123 memset(&cgc, 0, sizeof(struct packet_command));
124 cgc.cmd[0] = MODE_SELECT;
125 cgc.cmd[1] = (1 << 4);
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;
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;
142 sr_printk(KERN_INFO, cd,
143 "switching blocklength to %d bytes failed\n",
150 /* This function gets called after a media change. Checks if the CD is
151 multisession, asks for offset etc. */
153 int sr_cd_check(struct cdrom_device_info *cdi)
155 Scsi_CD *cd = cdi->handle;
156 unsigned long sector;
157 unsigned char *buffer; /* the buffer for the ioctl */
158 struct packet_command cgc;
161 if (cd->cdi.mask & CDC_MULTI_SESSION)
164 buffer = kmalloc(512, GFP_KERNEL);
168 sector = 0; /* the multisession sector offset goes here */
169 no_multi = 0; /* flag: the drive can't handle multisession */
172 memset(&cgc, 0, sizeof(struct packet_command));
174 switch (cd->vendor) {
177 cgc.cmd[0] = READ_TOC;
183 cgc.data_direction = DMA_FROM_DEVICE;
184 cgc.timeout = VENDOR_TIMEOUT;
185 rc = sr_do_ioctl(cd, &cgc);
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");
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 */
203 unsigned long min, sec, frame;
210 cgc.data_direction = DMA_FROM_DEVICE;
211 cgc.timeout = VENDOR_TIMEOUT;
212 rc = sr_do_ioctl(cd, &cgc);
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");
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;
229 case VENDOR_TOSHIBA:{
230 unsigned long min, sec, frame;
232 /* we request some disc information (is it a XA-CD ?,
233 * where starts the last session ?) */
239 cgc.data_direction = DMA_FROM_DEVICE;
240 cgc.timeout = VENDOR_TIMEOUT;
241 rc = sr_do_ioctl(cd, &cgc);
243 sr_printk(KERN_INFO, cd, "Hmm, seems the drive "
244 "doesn't support multisession CD's\n");
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;
255 sector -= CD_MSF_OFFSET;
256 sr_set_blocklength(cd, 2048);
261 cgc.cmd[0] = READ_TOC;
267 cgc.data_direction = DMA_FROM_DEVICE;
268 cgc.timeout = VENDOR_TIMEOUT;
269 rc = sr_do_ioctl(cd, &cgc);
273 if ((rc = buffer[2]) == 0) {
274 sr_printk(KERN_WARNING, cd,
275 "No finished session\n");
278 cgc.cmd[0] = READ_TOC; /* Read TOC */
279 cgc.cmd[6] = rc & 0x7f; /* number of last session */
285 cgc.data_direction = DMA_FROM_DEVICE;
286 cgc.timeout = VENDOR_TIMEOUT;
287 rc = sr_do_ioctl(cd, &cgc);
291 sector = buffer[11] + (buffer[10] << 8) +
292 (buffer[9] << 16) + (buffer[8] << 24);
296 /* should not happen */
297 sr_printk(KERN_WARNING, cd,
298 "unknown vendor code (%i), not initialized ?\n",
304 cd->ms_offset = sector;
306 if (CDS_AUDIO != sr_disk_status(cdi) && 1 == sr_is_xa(cd))
309 if (2048 != cd->device->sector_size) {
310 sr_set_blocklength(cd, 2048);
313 cdi->mask |= CDC_MULTI_SESSION;
317 sr_printk(KERN_DEBUG, cd, "multisession offset=%lu\n",