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