1#include <linux/kernel.h>
2#include <linux/mm.h>
3#include <linux/fs.h>
4#include <linux/errno.h>
5#include <linux/string.h>
6#include <linux/blkdev.h>
7#include <linux/module.h>
8#include <linux/blkpg.h>
9#include <linux/cdrom.h>
10#include <linux/delay.h>
11#include <linux/slab.h>
12#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>
21#include <scsi/scsi_cmnd.h>
22
23#include "sr.h"
24
25#if 0
26#define DEBUG
27#endif
28
29
30
31static int xa_test = 0;
32
33module_param(xa_test, int, S_IRUGO | S_IWUSR);
34
35
36
37#define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
38
39static 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;
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;
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}
107
108#define IOCTL_RETRIES 3
109
110
111
112
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
121 ret = sr_read_tochdr(cdi, &tochdr);
122 if (ret)
123 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
137 ret = sr_read_tocentry(cdi, &trk0_te);
138 if (ret)
139 return ret;
140 ret = sr_read_tocentry(cdi, &trk1_te);
141 if (ret)
142 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
157static 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
181
182
183
184
185int sr_do_ioctl(Scsi_CD *cd, struct packet_command *cgc)
186{
187 struct scsi_device *SDev;
188 struct scsi_sense_hdr sshdr;
189 int result, err = 0, retries = 0;
190 struct request_sense *sense = cgc->sense;
191 char logbuf[SCSI_LOG_BUFSIZE];
192
193 SDev = cd->device;
194
195 if (!sense) {
196 sense = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
197 if (!sense) {
198 err = -ENOMEM;
199 goto out;
200 }
201 }
202
203 retry:
204 if (!scsi_block_when_processing_errors(SDev)) {
205 err = -ENODEV;
206 goto out;
207 }
208
209 memset(sense, 0, sizeof(*sense));
210 result = scsi_execute(SDev, cgc->cmd, cgc->data_direction,
211 cgc->buffer, cgc->buflen, (char *)sense,
212 cgc->timeout, IOCTL_RETRIES, 0, NULL);
213
214 scsi_normalize_sense((char *)sense, sizeof(*sense), &sshdr);
215
216
217 if (driver_byte(result) != 0) {
218 switch (sshdr.sense_key) {
219 case UNIT_ATTENTION:
220 SDev->changed = 1;
221 if (!cgc->quiet)
222 sr_printk(KERN_INFO, cd,
223 "disc change detected.\n");
224 if (retries++ < 10)
225 goto retry;
226 err = -ENOMEDIUM;
227 break;
228 case NOT_READY:
229 if (sshdr.asc == 0x04 &&
230 sshdr.ascq == 0x01) {
231
232 if (!cgc->quiet)
233 sr_printk(KERN_INFO, cd,
234 "CDROM not ready yet.\n");
235 if (retries++ < 10) {
236
237 ssleep(2);
238 goto retry;
239 } else {
240
241 err = -ENOMEDIUM;
242 break;
243 }
244 }
245 if (!cgc->quiet)
246 sr_printk(KERN_INFO, cd,
247 "CDROM not ready. Make sure there "
248 "is a disc in the drive.\n");
249#ifdef DEBUG
250 scsi_print_sense_hdr(cd->device, cd->cdi.name, &sshdr);
251#endif
252 err = -ENOMEDIUM;
253 break;
254 case ILLEGAL_REQUEST:
255 err = -EIO;
256 if (sshdr.asc == 0x20 &&
257 sshdr.ascq == 0x00)
258
259 err = -EDRIVE_CANT_DO_THIS;
260#ifdef DEBUG
261 __scsi_format_command(logbuf, sizeof(logbuf),
262 cgc->cmd, CDROM_PACKET_SIZE);
263 sr_printk(KERN_INFO, cd,
264 "CDROM (ioctl) invalid command: %s\n",
265 logbuf);
266 scsi_print_sense_hdr(cd->device, cd->cdi.name, &sshdr);
267#endif
268 break;
269 default:
270 __scsi_format_command(logbuf, sizeof(logbuf),
271 cgc->cmd, CDROM_PACKET_SIZE);
272 sr_printk(KERN_ERR, cd,
273 "CDROM (ioctl) error, command: %s\n",
274 logbuf);
275 scsi_print_sense_hdr(cd->device, cd->cdi.name, &sshdr);
276 err = -EIO;
277 }
278 }
279
280
281 out:
282 if (!cgc->sense)
283 kfree(sense);
284 cgc->stat = err;
285 return err;
286}
287
288
289
290
291int sr_tray_move(struct cdrom_device_info *cdi, int pos)
292{
293 Scsi_CD *cd = cdi->handle;
294 struct packet_command cgc;
295
296 memset(&cgc, 0, sizeof(struct packet_command));
297 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
298 cgc.cmd[4] = (pos == 0) ? 0x03 : 0x02 ;
299 cgc.data_direction = DMA_NONE;
300 cgc.timeout = IOCTL_TIMEOUT;
301 return sr_do_ioctl(cd, &cgc);
302}
303
304int sr_lock_door(struct cdrom_device_info *cdi, int lock)
305{
306 Scsi_CD *cd = cdi->handle;
307
308 return scsi_set_medium_removal(cd->device, lock ?
309 SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
310}
311
312int sr_drive_status(struct cdrom_device_info *cdi, int slot)
313{
314 struct scsi_cd *cd = cdi->handle;
315 struct scsi_sense_hdr sshdr;
316 struct media_event_desc med;
317
318 if (CDSL_CURRENT != slot) {
319
320 return -EINVAL;
321 }
322 if (!scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
323 return CDS_DISC_OK;
324
325
326 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
327 && sshdr.asc == 0x04 && sshdr.ascq == 0x01)
328 return CDS_DRIVE_NOT_READY;
329
330 if (!cdrom_get_media_event(cdi, &med)) {
331 if (med.media_present)
332 return CDS_DISC_OK;
333 else if (med.door_open)
334 return CDS_TRAY_OPEN;
335 else
336 return CDS_NO_DISC;
337 }
338
339
340
341
342
343
344 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
345 && sshdr.asc == 0x04 && sshdr.ascq == 0x02)
346 return CDS_TRAY_OPEN;
347
348
349
350
351 if (sshdr.sense_key == NOT_READY && sshdr.asc == 0x04)
352 return CDS_DISC_OK;
353
354
355
356
357
358
359 if (scsi_sense_valid(&sshdr) &&
360
361 sshdr.asc == 0x3a)
362 return CDS_NO_DISC;
363 else
364 return CDS_TRAY_OPEN;
365
366 return CDS_DRIVE_NOT_READY;
367}
368
369int sr_disk_status(struct cdrom_device_info *cdi)
370{
371 Scsi_CD *cd = cdi->handle;
372 struct cdrom_tochdr toc_h;
373 struct cdrom_tocentry toc_e;
374 int i, rc, have_datatracks = 0;
375
376
377 rc = sr_read_tochdr(cdi, &toc_h);
378 if (rc)
379 return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
380
381 for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
382 toc_e.cdte_track = i;
383 toc_e.cdte_format = CDROM_LBA;
384 if (sr_read_tocentry(cdi, &toc_e))
385 return CDS_NO_INFO;
386 if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
387 have_datatracks = 1;
388 break;
389 }
390 }
391 if (!have_datatracks)
392 return CDS_AUDIO;
393
394 if (cd->xa_flag)
395 return CDS_XA_2_1;
396 else
397 return CDS_DATA_1;
398}
399
400int sr_get_last_session(struct cdrom_device_info *cdi,
401 struct cdrom_multisession *ms_info)
402{
403 Scsi_CD *cd = cdi->handle;
404
405 ms_info->addr.lba = cd->ms_offset;
406 ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
407
408 return 0;
409}
410
411int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
412{
413 Scsi_CD *cd = cdi->handle;
414 struct packet_command cgc;
415 char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
416 int result;
417
418 if (!buffer)
419 return -ENOMEM;
420
421 memset(&cgc, 0, sizeof(struct packet_command));
422 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
423 cgc.cmd[2] = 0x40;
424 cgc.cmd[3] = 0x02;
425 cgc.cmd[8] = 24;
426 cgc.buffer = buffer;
427 cgc.buflen = 24;
428 cgc.data_direction = DMA_FROM_DEVICE;
429 cgc.timeout = IOCTL_TIMEOUT;
430 result = sr_do_ioctl(cd, &cgc);
431
432 memcpy(mcn->medium_catalog_number, buffer + 9, 13);
433 mcn->medium_catalog_number[13] = 0;
434
435 kfree(buffer);
436 return result;
437}
438
439int sr_reset(struct cdrom_device_info *cdi)
440{
441 return 0;
442}
443
444int sr_select_speed(struct cdrom_device_info *cdi, int speed)
445{
446 Scsi_CD *cd = cdi->handle;
447 struct packet_command cgc;
448
449 if (speed == 0)
450 speed = 0xffff;
451 else
452 speed *= 177;
453
454 memset(&cgc, 0, sizeof(struct packet_command));
455 cgc.cmd[0] = GPCMD_SET_SPEED;
456 cgc.cmd[2] = (speed >> 8) & 0xff;
457 cgc.cmd[3] = speed & 0xff;
458 cgc.data_direction = DMA_NONE;
459 cgc.timeout = IOCTL_TIMEOUT;
460
461 if (sr_do_ioctl(cd, &cgc))
462 return -EIO;
463 return 0;
464}
465
466
467
468
469
470
471
472int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
473{
474 switch (cmd) {
475 case CDROMREADTOCHDR:
476 return sr_read_tochdr(cdi, arg);
477 case CDROMREADTOCENTRY:
478 return sr_read_tocentry(cdi, arg);
479 case CDROMPLAYTRKIND:
480 return sr_play_trkind(cdi, arg);
481 default:
482 return -EINVAL;
483 }
484}
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
501{
502 struct packet_command cgc;
503
504#ifdef DEBUG
505 sr_printk(KERN_INFO, cd, "sr_read_cd lba=%d format=%d blksize=%d\n",
506 lba, format, blksize);
507#endif
508
509 memset(&cgc, 0, sizeof(struct packet_command));
510 cgc.cmd[0] = GPCMD_READ_CD;
511 cgc.cmd[1] = ((format & 7) << 2);
512 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
513 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
514 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
515 cgc.cmd[5] = (unsigned char) lba & 0xff;
516 cgc.cmd[8] = 1;
517 switch (blksize) {
518 case 2336:
519 cgc.cmd[9] = 0x58;
520 break;
521 case 2340:
522 cgc.cmd[9] = 0x78;
523 break;
524 case 2352:
525 cgc.cmd[9] = 0xf8;
526 break;
527 default:
528 cgc.cmd[9] = 0x10;
529 break;
530 }
531 cgc.buffer = dest;
532 cgc.buflen = blksize;
533 cgc.data_direction = DMA_FROM_DEVICE;
534 cgc.timeout = IOCTL_TIMEOUT;
535 return sr_do_ioctl(cd, &cgc);
536}
537
538
539
540
541
542static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
543{
544 struct packet_command cgc;
545 int rc;
546
547
548 if (cd->readcd_known) {
549 rc = sr_read_cd(cd, dest, lba, 0, blksize);
550 if (-EDRIVE_CANT_DO_THIS != rc)
551 return rc;
552 cd->readcd_known = 0;
553 sr_printk(KERN_INFO, cd,
554 "CDROM does'nt support READ CD (0xbe) command\n");
555
556 }
557
558 if (blksize != cd->device->sector_size) {
559 if (0 != (rc = sr_set_blocklength(cd, blksize)))
560 return rc;
561 }
562#ifdef DEBUG
563 sr_printk(KERN_INFO, cd, "sr_read_sector lba=%d blksize=%d\n",
564 lba, blksize);
565#endif
566
567 memset(&cgc, 0, sizeof(struct packet_command));
568 cgc.cmd[0] = GPCMD_READ_10;
569 cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
570 cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
571 cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
572 cgc.cmd[5] = (unsigned char) lba & 0xff;
573 cgc.cmd[8] = 1;
574 cgc.buffer = dest;
575 cgc.buflen = blksize;
576 cgc.data_direction = DMA_FROM_DEVICE;
577 cgc.timeout = IOCTL_TIMEOUT;
578 rc = sr_do_ioctl(cd, &cgc);
579
580 return rc;
581}
582
583
584
585
586
587
588int sr_is_xa(Scsi_CD *cd)
589{
590 unsigned char *raw_sector;
591 int is_xa;
592
593 if (!xa_test)
594 return 0;
595
596 raw_sector = kmalloc(2048, GFP_KERNEL | SR_GFP_DMA(cd));
597 if (!raw_sector)
598 return -ENOMEM;
599 if (0 == sr_read_sector(cd, cd->ms_offset + 16,
600 CD_FRAMESIZE_RAW1, raw_sector)) {
601 is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
602 } else {
603
604 is_xa = -1;
605 }
606 kfree(raw_sector);
607#ifdef DEBUG
608 sr_printk(KERN_INFO, cd, "sr_is_xa: %d\n", is_xa);
609#endif
610 return is_xa;
611}
612