blob: fb929fac22ba4453b2abc6fdb0f6167598612630 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/mm.h>
3#include <linux/fs.h>
4#include <linux/errno.h>
5#include <linux/string.h>
6#include <linux/blkdev.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -04007#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/blkpg.h>
9#include <linux/cdrom.h>
10#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/io.h>
13#include <asm/uaccess.h>
14
15#include <scsi/scsi.h>
16#include <scsi/scsi_dbg.h>
17#include <scsi/scsi_device.h>
18#include <scsi/scsi_eh.h>
19#include <scsi/scsi_host.h>
20#include <scsi/scsi_ioctl.h>
James Bottomley820732b2005-06-12 22:21:29 -050021#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "sr.h"
24
25#if 0
26#define DEBUG
27#endif
28
29/* The sr_is_xa() seems to trigger firmware bugs with some drives :-(
30 * It is off by default and can be turned on with this module parameter */
31static int xa_test = 0;
32
33module_param(xa_test, int, S_IRUGO | S_IWUSR);
34
Christoph Hellwiga09c6312006-01-11 14:28:06 +010035/* primitive to determine whether we need to have GFP_DMA set based on
36 * the status of the unchecked_isa_dma flag in the host structure */
37#define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
38
Christoph Hellwiga09c6312006-01-11 14:28:06 +010039static int sr_read_tochdr(struct cdrom_device_info *cdi,
40 struct cdrom_tochdr *tochdr)
41{
42 struct scsi_cd *cd = cdi->handle;
43 struct packet_command cgc;
44 int result;
45 unsigned char *buffer;
46
47 buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
48 if (!buffer)
49 return -ENOMEM;
50
51 memset(&cgc, 0, sizeof(struct packet_command));
52 cgc.timeout = IOCTL_TIMEOUT;
53 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
54 cgc.cmd[8] = 12; /* LSB of length */
55 cgc.buffer = buffer;
56 cgc.buflen = 12;
57 cgc.quiet = 1;
58 cgc.data_direction = DMA_FROM_DEVICE;
59
60 result = sr_do_ioctl(cd, &cgc);
61
62 tochdr->cdth_trk0 = buffer[2];
63 tochdr->cdth_trk1 = buffer[3];
64
65 kfree(buffer);
66 return result;
67}
68
69static int sr_read_tocentry(struct cdrom_device_info *cdi,
70 struct cdrom_tocentry *tocentry)
71{
72 struct scsi_cd *cd = cdi->handle;
73 struct packet_command cgc;
74 int result;
75 unsigned char *buffer;
76
77 buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
78 if (!buffer)
79 return -ENOMEM;
80
81 memset(&cgc, 0, sizeof(struct packet_command));
82 cgc.timeout = IOCTL_TIMEOUT;
83 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
84 cgc.cmd[1] |= (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0;
85 cgc.cmd[6] = tocentry->cdte_track;
86 cgc.cmd[8] = 12; /* LSB of length */
87 cgc.buffer = buffer;
88 cgc.buflen = 12;
89 cgc.data_direction = DMA_FROM_DEVICE;
90
91 result = sr_do_ioctl(cd, &cgc);
92
93 tocentry->cdte_ctrl = buffer[5] & 0xf;
94 tocentry->cdte_adr = buffer[5] >> 4;
95 tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
96 if (tocentry->cdte_format == CDROM_MSF) {
97 tocentry->cdte_addr.msf.minute = buffer[9];
98 tocentry->cdte_addr.msf.second = buffer[10];
99 tocentry->cdte_addr.msf.frame = buffer[11];
100 } else
101 tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
102 + buffer[10]) << 8) + buffer[11];
103
104 kfree(buffer);
105 return result;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#define IOCTL_RETRIES 3
109
110/* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command. When these drives
111 are emulating a SCSI device via the idescsi module, they need to have
112 CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
113
114static int sr_fake_playtrkind(struct cdrom_device_info *cdi, struct cdrom_ti *ti)
115{
116 struct cdrom_tocentry trk0_te, trk1_te;
117 struct cdrom_tochdr tochdr;
118 struct packet_command cgc;
119 int ntracks, ret;
120
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100121 ret = sr_read_tochdr(cdi, &tochdr);
122 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return ret;
124
125 ntracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
126
127 if (ti->cdti_trk1 == ntracks)
128 ti->cdti_trk1 = CDROM_LEADOUT;
129 else if (ti->cdti_trk1 != CDROM_LEADOUT)
130 ti->cdti_trk1 ++;
131
132 trk0_te.cdte_track = ti->cdti_trk0;
133 trk0_te.cdte_format = CDROM_MSF;
134 trk1_te.cdte_track = ti->cdti_trk1;
135 trk1_te.cdte_format = CDROM_MSF;
136
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100137 ret = sr_read_tocentry(cdi, &trk0_te);
138 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return ret;
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100140 ret = sr_read_tocentry(cdi, &trk1_te);
141 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret;
143
144 memset(&cgc, 0, sizeof(struct packet_command));
145 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
146 cgc.cmd[3] = trk0_te.cdte_addr.msf.minute;
147 cgc.cmd[4] = trk0_te.cdte_addr.msf.second;
148 cgc.cmd[5] = trk0_te.cdte_addr.msf.frame;
149 cgc.cmd[6] = trk1_te.cdte_addr.msf.minute;
150 cgc.cmd[7] = trk1_te.cdte_addr.msf.second;
151 cgc.cmd[8] = trk1_te.cdte_addr.msf.frame;
152 cgc.data_direction = DMA_NONE;
153 cgc.timeout = IOCTL_TIMEOUT;
154 return sr_do_ioctl(cdi->handle, &cgc);
155}
156
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100157static int sr_play_trkind(struct cdrom_device_info *cdi,
158 struct cdrom_ti *ti)
159
160{
161 struct scsi_cd *cd = cdi->handle;
162 struct packet_command cgc;
163 int result;
164
165 memset(&cgc, 0, sizeof(struct packet_command));
166 cgc.timeout = IOCTL_TIMEOUT;
167 cgc.cmd[0] = GPCMD_PLAYAUDIO_TI;
168 cgc.cmd[4] = ti->cdti_trk0;
169 cgc.cmd[5] = ti->cdti_ind0;
170 cgc.cmd[7] = ti->cdti_trk1;
171 cgc.cmd[8] = ti->cdti_ind1;
172 cgc.data_direction = DMA_NONE;
173
174 result = sr_do_ioctl(cd, &cgc);
175 if (result == -EDRIVE_CANT_DO_THIS)
176 result = sr_fake_playtrkind(cdi, ti);
177
178 return result;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* We do our own retries because we want to know what the specific
182 error code is. Normally the UNIT_ATTENTION code will automatically
183 clear after one error */
184
185int sr_do_ioctl(Scsi_CD *cd, struct packet_command *cgc)
186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct scsi_device *SDev;
James Bottomley820732b2005-06-12 22:21:29 -0500188 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int result, err = 0, retries = 0;
James Bottomley820732b2005-06-12 22:21:29 -0500190 struct request_sense *sense = cgc->sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 SDev = cd->device;
James Bottomley820732b2005-06-12 22:21:29 -0500193
194 if (!sense) {
Linus Torvaldsf716d832006-03-06 17:41:44 -0800195 sense = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
James Bottomley820732b2005-06-12 22:21:29 -0500196 if (!sense) {
197 err = -ENOMEM;
198 goto out;
199 }
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 retry:
203 if (!scsi_block_when_processing_errors(SDev)) {
204 err = -ENODEV;
James Bottomley820732b2005-06-12 22:21:29 -0500205 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
James Bottomley820732b2005-06-12 22:21:29 -0500208 memset(sense, 0, sizeof(*sense));
209 result = scsi_execute(SDev, cgc->cmd, cgc->data_direction,
210 cgc->buffer, cgc->buflen, (char *)sense,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900211 cgc->timeout, IOCTL_RETRIES, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
James Bottomley820732b2005-06-12 22:21:29 -0500213 scsi_normalize_sense((char *)sense, sizeof(*sense), &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Minimal error checking. Ignore cases we know about, and report the rest. */
216 if (driver_byte(result) != 0) {
James Bottomley820732b2005-06-12 22:21:29 -0500217 switch (sshdr.sense_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 case UNIT_ATTENTION:
219 SDev->changed = 1;
220 if (!cgc->quiet)
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200221 sr_printk(KERN_INFO, cd,
222 "disc change detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (retries++ < 10)
224 goto retry;
225 err = -ENOMEDIUM;
226 break;
227 case NOT_READY: /* This happens if there is no disc in drive */
James Bottomley820732b2005-06-12 22:21:29 -0500228 if (sshdr.asc == 0x04 &&
229 sshdr.ascq == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* sense: Logical unit is in process of becoming ready */
231 if (!cgc->quiet)
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200232 sr_printk(KERN_INFO, cd,
233 "CDROM not ready yet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (retries++ < 10) {
235 /* sleep 2 sec and try again */
236 ssleep(2);
237 goto retry;
238 } else {
239 /* 20 secs are enough? */
240 err = -ENOMEDIUM;
241 break;
242 }
243 }
244 if (!cgc->quiet)
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200245 sr_printk(KERN_INFO, cd,
246 "CDROM not ready. Make sure there "
247 "is a disc in the drive.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#ifdef DEBUG
Hannes Reinecked811b842014-10-24 14:26:45 +0200249 scsi_print_sense_hdr(cd->device, cd->cdi.name, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251 err = -ENOMEDIUM;
252 break;
253 case ILLEGAL_REQUEST:
254 err = -EIO;
James Bottomley820732b2005-06-12 22:21:29 -0500255 if (sshdr.asc == 0x20 &&
256 sshdr.ascq == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* sense: Invalid command operation code */
258 err = -EDRIVE_CANT_DO_THIS;
259#ifdef DEBUG
Hannes Reineckea9a47bf2014-10-24 14:26:57 +0200260 __scsi_print_command(cgc->cmd, CDROM_PACKET_SIZE);
Hannes Reinecked811b842014-10-24 14:26:45 +0200261 scsi_print_sense_hdr(cd->device, cd->cdi.name, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#endif
263 break;
264 default:
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200265 sr_printk(KERN_ERR, cd,
266 "CDROM (ioctl) error, command: ");
Hannes Reineckea9a47bf2014-10-24 14:26:57 +0200267 __scsi_print_command(cgc->cmd, CDROM_PACKET_SIZE);
Hannes Reinecked811b842014-10-24 14:26:45 +0200268 scsi_print_sense_hdr(cd->device, cd->cdi.name, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 err = -EIO;
270 }
271 }
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Wake up a process waiting for device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 out:
James Bottomley820732b2005-06-12 22:21:29 -0500275 if (!cgc->sense)
276 kfree(sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 cgc->stat = err;
278 return err;
279}
280
281/* ---------------------------------------------------------------------- */
282/* interface to cdrom.c */
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284int sr_tray_move(struct cdrom_device_info *cdi, int pos)
285{
286 Scsi_CD *cd = cdi->handle;
287 struct packet_command cgc;
288
289 memset(&cgc, 0, sizeof(struct packet_command));
290 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
291 cgc.cmd[4] = (pos == 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
292 cgc.data_direction = DMA_NONE;
293 cgc.timeout = IOCTL_TIMEOUT;
294 return sr_do_ioctl(cd, &cgc);
295}
296
297int sr_lock_door(struct cdrom_device_info *cdi, int lock)
298{
299 Scsi_CD *cd = cdi->handle;
300
301 return scsi_set_medium_removal(cd->device, lock ?
302 SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
303}
304
305int sr_drive_status(struct cdrom_device_info *cdi, int slot)
306{
James Bottomley210ba1d2008-01-05 10:39:51 -0600307 struct scsi_cd *cd = cdi->handle;
308 struct scsi_sense_hdr sshdr;
309 struct media_event_desc med;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (CDSL_CURRENT != slot) {
312 /* we have no changer support */
313 return -EINVAL;
314 }
Tejun Heo9f8a2c22010-12-08 20:57:40 +0100315 if (!scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return CDS_DISC_OK;
317
Peter Jones96bcc722008-07-11 16:17:05 -0400318 /* SK/ASC/ASCQ of 2/4/1 means "unit is becoming ready" */
319 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
320 && sshdr.asc == 0x04 && sshdr.ascq == 0x01)
321 return CDS_DRIVE_NOT_READY;
322
James Bottomley210ba1d2008-01-05 10:39:51 -0600323 if (!cdrom_get_media_event(cdi, &med)) {
324 if (med.media_present)
325 return CDS_DISC_OK;
326 else if (med.door_open)
327 return CDS_TRAY_OPEN;
328 else
329 return CDS_NO_DISC;
330 }
331
332 /*
Robert Jenningsb8479172010-10-11 11:02:00 -0500333 * SK/ASC/ASCQ of 2/4/2 means "initialization required"
334 * Using CD_TRAY_OPEN results in an START_STOP_UNIT to close
335 * the tray, which resolves the initialization requirement.
336 */
337 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
338 && sshdr.asc == 0x04 && sshdr.ascq == 0x02)
339 return CDS_TRAY_OPEN;
340
341 /*
James Bottomley210ba1d2008-01-05 10:39:51 -0600342 * 0x04 is format in progress .. but there must be a disc present!
343 */
344 if (sshdr.sense_key == NOT_READY && sshdr.asc == 0x04)
345 return CDS_DISC_OK;
346
347 /*
348 * If not using Mt Fuji extended media tray reports,
349 * just return TRAY_OPEN since ATAPI doesn't provide
350 * any other way to detect this...
351 */
352 if (scsi_sense_valid(&sshdr) &&
353 /* 0x3a is medium not present */
354 sshdr.asc == 0x3a)
355 return CDS_NO_DISC;
356 else
357 return CDS_TRAY_OPEN;
358
359 return CDS_DRIVE_NOT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362int sr_disk_status(struct cdrom_device_info *cdi)
363{
364 Scsi_CD *cd = cdi->handle;
365 struct cdrom_tochdr toc_h;
366 struct cdrom_tocentry toc_e;
367 int i, rc, have_datatracks = 0;
368
369 /* look for data tracks */
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100370 rc = sr_read_tochdr(cdi, &toc_h);
371 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
373
374 for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
375 toc_e.cdte_track = i;
376 toc_e.cdte_format = CDROM_LBA;
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100377 if (sr_read_tocentry(cdi, &toc_e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return CDS_NO_INFO;
379 if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
380 have_datatracks = 1;
381 break;
382 }
383 }
384 if (!have_datatracks)
385 return CDS_AUDIO;
386
387 if (cd->xa_flag)
388 return CDS_XA_2_1;
389 else
390 return CDS_DATA_1;
391}
392
393int sr_get_last_session(struct cdrom_device_info *cdi,
394 struct cdrom_multisession *ms_info)
395{
396 Scsi_CD *cd = cdi->handle;
397
398 ms_info->addr.lba = cd->ms_offset;
399 ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
400
401 return 0;
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
405{
406 Scsi_CD *cd = cdi->handle;
407 struct packet_command cgc;
408 char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
409 int result;
410
Nate Dailey3a73e8c2005-04-21 16:14:05 -0400411 if (!buffer)
412 return -ENOMEM;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 memset(&cgc, 0, sizeof(struct packet_command));
415 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
416 cgc.cmd[2] = 0x40; /* I do want the subchannel info */
417 cgc.cmd[3] = 0x02; /* Give me medium catalog number info */
418 cgc.cmd[8] = 24;
419 cgc.buffer = buffer;
420 cgc.buflen = 24;
421 cgc.data_direction = DMA_FROM_DEVICE;
422 cgc.timeout = IOCTL_TIMEOUT;
423 result = sr_do_ioctl(cd, &cgc);
424
425 memcpy(mcn->medium_catalog_number, buffer + 9, 13);
426 mcn->medium_catalog_number[13] = 0;
427
428 kfree(buffer);
429 return result;
430}
431
432int sr_reset(struct cdrom_device_info *cdi)
433{
434 return 0;
435}
436
437int sr_select_speed(struct cdrom_device_info *cdi, int speed)
438{
439 Scsi_CD *cd = cdi->handle;
440 struct packet_command cgc;
441
442 if (speed == 0)
443 speed = 0xffff; /* set to max */
444 else
445 speed *= 177; /* Nx to kbyte/s */
446
447 memset(&cgc, 0, sizeof(struct packet_command));
448 cgc.cmd[0] = GPCMD_SET_SPEED; /* SET CD SPEED */
449 cgc.cmd[2] = (speed >> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
450 cgc.cmd[3] = speed & 0xff; /* LSB */
451 cgc.data_direction = DMA_NONE;
452 cgc.timeout = IOCTL_TIMEOUT;
453
454 if (sr_do_ioctl(cd, &cgc))
455 return -EIO;
456 return 0;
457}
458
459/* ----------------------------------------------------------------------- */
460/* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
461/* because the generic cdrom driver does the user access stuff for us. */
462/* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
463/* sr_disk_status interface for the generic cdrom driver. */
464
465int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
466{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 switch (cmd) {
468 case CDROMREADTOCHDR:
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100469 return sr_read_tochdr(cdi, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 case CDROMREADTOCENTRY:
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100471 return sr_read_tocentry(cdi, arg);
472 case CDROMPLAYTRKIND:
473 return sr_play_trkind(cdi, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 default:
Christoph Hellwiga09c6312006-01-11 14:28:06 +0100475 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479/* -----------------------------------------------------------------------
480 * a function to read all sorts of funny cdrom sectors using the READ_CD
481 * scsi-3 mmc command
482 *
483 * lba: linear block address
484 * format: 0 = data (anything)
485 * 1 = audio
486 * 2 = data (mode 1)
487 * 3 = data (mode 2)
488 * 4 = data (mode 2 form1)
489 * 5 = data (mode 2 form2)
490 * blksize: 2048 | 2336 | 2340 | 2352
491 */
492
493static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
494{
495 struct packet_command cgc;
496
497#ifdef DEBUG
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200498 sr_printk(KERN_INFO, cd, "sr_read_cd lba=%d format=%d blksize=%d\n",
499 lba, format, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500#endif
501
502 memset(&cgc, 0, sizeof(struct packet_command));
503 cgc.cmd[0] = GPCMD_READ_CD; /* READ_CD */
504 cgc.cmd[1] = ((format & 7) << 2);
505 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
506 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
507 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
508 cgc.cmd[5] = (unsigned char) lba & 0xff;
509 cgc.cmd[8] = 1;
510 switch (blksize) {
511 case 2336:
512 cgc.cmd[9] = 0x58;
513 break;
514 case 2340:
515 cgc.cmd[9] = 0x78;
516 break;
517 case 2352:
518 cgc.cmd[9] = 0xf8;
519 break;
520 default:
521 cgc.cmd[9] = 0x10;
522 break;
523 }
524 cgc.buffer = dest;
525 cgc.buflen = blksize;
526 cgc.data_direction = DMA_FROM_DEVICE;
527 cgc.timeout = IOCTL_TIMEOUT;
528 return sr_do_ioctl(cd, &cgc);
529}
530
531/*
532 * read sectors with blocksizes other than 2048
533 */
534
535static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
536{
537 struct packet_command cgc;
538 int rc;
539
540 /* we try the READ CD command first... */
541 if (cd->readcd_known) {
542 rc = sr_read_cd(cd, dest, lba, 0, blksize);
543 if (-EDRIVE_CANT_DO_THIS != rc)
544 return rc;
545 cd->readcd_known = 0;
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200546 sr_printk(KERN_INFO, cd,
547 "CDROM does'nt support READ CD (0xbe) command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* fall & retry the other way */
549 }
550 /* ... if this fails, we switch the blocksize using MODE SELECT */
551 if (blksize != cd->device->sector_size) {
552 if (0 != (rc = sr_set_blocklength(cd, blksize)))
553 return rc;
554 }
555#ifdef DEBUG
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200556 sr_printk(KERN_INFO, cd, "sr_read_sector lba=%d blksize=%d\n",
557 lba, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#endif
559
560 memset(&cgc, 0, sizeof(struct packet_command));
561 cgc.cmd[0] = GPCMD_READ_10;
562 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
563 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
564 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
565 cgc.cmd[5] = (unsigned char) lba & 0xff;
566 cgc.cmd[8] = 1;
567 cgc.buffer = dest;
568 cgc.buflen = blksize;
569 cgc.data_direction = DMA_FROM_DEVICE;
570 cgc.timeout = IOCTL_TIMEOUT;
571 rc = sr_do_ioctl(cd, &cgc);
572
573 return rc;
574}
575
576/*
577 * read a sector in raw mode to check the sector format
578 * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
579 */
580
581int sr_is_xa(Scsi_CD *cd)
582{
583 unsigned char *raw_sector;
584 int is_xa;
585
586 if (!xa_test)
587 return 0;
588
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800589 raw_sector = kmalloc(2048, GFP_KERNEL | SR_GFP_DMA(cd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!raw_sector)
591 return -ENOMEM;
592 if (0 == sr_read_sector(cd, cd->ms_offset + 16,
593 CD_FRAMESIZE_RAW1, raw_sector)) {
594 is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
595 } else {
596 /* read a raw sector failed for some reason. */
597 is_xa = -1;
598 }
599 kfree(raw_sector);
600#ifdef DEBUG
Hannes Reinecke96eefad2014-06-25 16:39:54 +0200601 sr_printk(KERN_INFO, cd, "sr_is_xa: %d\n", is_xa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#endif
603 return is_xa;
604}