1/* 2* Sony CDU-31A CDROM interface device driver. 3* 4* Corey Minyard (minyard@wf-rch.cirr.com) 5* 6* Colossians 3:17 7* 8* See Documentation/cdrom/cdu31a for additional details about this driver. 9* 10* The Sony interface device driver handles Sony interface CDROM 11* drives and provides a complete block-level interface as well as an 12* ioctl() interface compatible with the Sun (as specified in 13* include/linux/cdrom.h). With this interface, CDROMs can be 14* accessed and standard audio CDs can be played back normally. 15* 16* WARNING - All autoprobes have been removed from the driver. 17* You MUST configure the CDU31A via a LILO config 18* at boot time or in lilo.conf. I have the 19* following in my lilo.conf: 20* 21* append="cdu31a=0x1f88,0,PAS" 22* 23* The first number is the I/O base address of the 24* card. The second is the interrupt (0 means none). 25 * The third should be "PAS" if on a Pro-Audio 26 * spectrum, or nothing if on something else. 27 * 28 * This interface is (unfortunately) a polled interface. This is 29 * because most Sony interfaces are set up with DMA and interrupts 30 * disables. Some (like mine) do not even have the capability to 31 * handle interrupts or DMA. For this reason you will see a lot of 32 * the following: 33 * 34 * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT; 35 * while (time_before(jiffies, retry_count) && (! <some condition to wait for)) 36 * { 37 * while (handle_sony_cd_attention()) 38 * ; 39 * 40 * sony_sleep(); 41 * } 42 * if (the condition not met) 43 * { 44 * return an error; 45 * } 46 * 47 * This ugly hack waits for something to happen, sleeping a little 48 * between every try. it also handles attentions, which are 49 * asynchronous events from the drive informing the driver that a disk 50 * has been inserted, removed, etc. 51 * 52 * NEWS FLASH - The driver now supports interrupts but they are 53 * turned off by default. Use of interrupts is highly encouraged, it 54 * cuts CPU usage down to a reasonable level. I had DMA in for a while 55 * but PC DMA is just too slow. Better to just insb() it. 56 * 57 * One thing about these drives: They talk in MSF (Minute Second Frame) format. 58 * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a 59 * disk. The funny thing is that these are sent to the drive in BCD, but the 60 * interface wants to see them in decimal. A lot of conversion goes on. 61 * 62 * DRIVER SPECIAL FEATURES 63 * ----------------------- 64 * 65 * This section describes features beyond the normal audio and CD-ROM 66 * functions of the drive. 67 * 68 * 2048 byte buffer mode 69 * 70 * If a disk is mounted with -o block=2048, data is copied straight 71 * from the drive data port to the buffer. Otherwise, the readahead 72 * buffer must be involved to hold the other 1K of data when a 1K 73 * block operation is done. Note that with 2048 byte blocks you 74 * cannot execute files from the CD. 75 * 76 * XA compatibility 77 * 78 * The driver should support XA disks for both the CDU31A and CDU33A. 79 * It does this transparently, the using program doesn't need to set it. 80 * 81 * Multi-Session 82 * 83 * A multi-session disk looks just like a normal disk to the user. 84 * Just mount one normally, and all the data should be there. 85 * A special thanks to Koen for help with this! 86 * 87 * Raw sector I/O 88 * 89 * Using the CDROMREADAUDIO it is possible to read raw audio and data 90 * tracks. Both operations return 2352 bytes per sector. On the data 91 * tracks, the first 12 bytes is not returned by the drive and the value 92 * of that data is indeterminate. 93 * 94 * 95 * Copyright (C) 1993 Corey Minyard 96 * 97 * This program is free software; you can redistribute it and/or modify 98 * it under the terms of the GNU General Public License as published by 99 * the Free Software Foundation; either version 2 of the License, or 100 * (at your option) any later version. 101 * 102 * This program is distributed in the hope that it will be useful, 103 * but WITHOUT ANY WARRANTY; without even the implied warranty of 104 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 105 * GNU General Public License for more details. 106 * 107 * You should have received a copy of the GNU General Public License 108 * along with this program; if not, write to the Free Software 109 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 110 * 111 * TODO: 112 * CDs with form1 and form2 sectors cause problems 113 * with current read-ahead strategy. 114 * 115 * Credits: 116 * Heiko Eissfeldt <heiko@colossus.escape.de> 117 * For finding abug in the return of the track numbers. 118 * TOC processing redone for proper multisession support. 119 * 120 * 121 * It probably a little late to be adding a history, but I guess I 122 * will start. 123 * 124 * 10/24/95 - Added support for disabling the eject button when the 125 * drive is open. Note that there is a small problem 126 * still here, if the eject button is pushed while the 127 * drive light is flashing, the drive will return a bad 128 * status and be reset. It recovers, though. 129 * 130 * 03/07/97 - Fixed a problem with timers. 131 * 132 * 133 * 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by 134 * Heiko Eissfeldt <heiko@colossus.escape.de> with additional 135 * changes by Erik Andersen <andersee@debian.org> 136 * 137 * 24 January 1998 -- Removed the scd_disc_status() function, which was now 138 * just dead code left over from the port. 139 * Erik Andersen <andersee@debian.org> 140 * 141 * 16 July 1998 -- Drive donated to Erik Andersen by John Kodis 142 * <kodis@jagunet.com>. Work begun on fixing driver to 143 * work under 2.1.X. Added temporary extra printks 144 * which seem to slow it down enough to work. 145 * 146 * 9 November 1999 -- Make kernel-parameter implementation work with 2.3.x 147 * Removed init_module & cleanup_module in favor of 148 * module_init & module_exit. 149 * Torben Mathiasen <tmm@image.dk> 150*/ 151 152#include <linux/major.h> 153 154#include <linux/module.h> 155 156#include <linux/errno.h> 157#include <linux/signal.h> 158#include <linux/sched.h> 159#include <linux/timer.h> 160#include <linux/fs.h> 161#include <linux/kernel.h> 162#include <linux/hdreg.h> 163#include <linux/genhd.h> 164#include <linux/ioport.h> 165#include <linux/devfs_fs_kernel.h> 166#include <linux/string.h> 167#include <linux/slab.h> 168#include <linux/init.h> 169#include <linux/interrupt.h> 170 171#include <asm/system.h> 172#include <asm/io.h> 173#include <asm/uaccess.h> 174#include <asm/dma.h> 175 176#include <linux/cdrom.h> 177#include "cdu31a.h" 178 179#define MAJOR_NR CDU31A_CDROM_MAJOR 180#include <linux/blk.h> 181 182#define CDU31A_READAHEAD 4 /* 128 sector, 64kB, 32 reads read-ahead */ 183#define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10 184 185#define DEBUG 0 186 187/* Define the following if you have data corruption problems. */ 188#undef SONY_POLL_EACH_BYTE 189 190/* 191** Edit the following data to change interrupts, DMA channels, etc. 192** Default is polled and no DMA. DMA is not recommended for double-speed 193** drives. 194*/ 195static struct { 196 unsigned short base; /* I/O Base Address */ 197 short int_num; /* Interrupt Number (-1 means scan for it, 198 0 means don't use) */ 199} cdu31a_addresses[] __initdata = { 200 {0} 201}; 202 203static int handle_sony_cd_attention(void); 204static int read_subcode(void); 205static void sony_get_toc(void); 206static int scd_spinup(void); 207/*static int scd_open(struct inode *inode, struct file *filp);*/ 208static int scd_open(struct cdrom_device_info *, int); 209static void do_sony_cd_cmd(unsigned char cmd, 210 unsigned char *params, 211 unsigned int num_params, 212 unsigned char *result_buffer, 213 unsigned int *result_size); 214static void size_to_buf(unsigned int size, unsigned char *buf); 215 216/* Parameters for the read-ahead. */ 217static unsigned int sony_next_block; /* Next 512 byte block offset */ 218static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left 219 in the current read command. */ 220 221 222/* The base I/O address of the Sony Interface. This is a variable (not a 223 #define) so it can be easily changed via some future ioctl() */ 224static unsigned int cdu31a_port = 0; 225MODULE_PARM(cdu31a_port, "i"); 226 227/* 228 * The following are I/O addresses of the various registers for the drive. The 229 * comment for the base address also applies here. 230 */ 231static volatile unsigned short sony_cd_cmd_reg; 232static volatile unsigned short sony_cd_param_reg; 233static volatile unsigned short sony_cd_write_reg; 234static volatile unsigned short sony_cd_control_reg; 235static volatile unsigned short sony_cd_status_reg; 236static volatile unsigned short sony_cd_result_reg; 237static volatile unsigned short sony_cd_read_reg; 238static volatile unsigned short sony_cd_fifost_reg; 239 240 241static int sony_spun_up = 0; /* Has the drive been spun up? */ 242 243static int sony_speed = 0; /* Last wanted speed */ 244 245static int sony_xa_mode = 0; /* Is an XA disk in the drive 246 and the drive a CDU31A? */ 247 248static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio. 249 For raw data reads. */ 250 251static unsigned int sony_usage = 0; /* How many processes have the 252 drive open. */ 253 254static int sony_pas_init = 0; /* Initialize the Pro-Audio 255 Spectrum card? */ 256 257static struct s_sony_session_toc single_toc; /* Holds the 258 table of 259 contents. */ 260 261static struct s_all_sessions_toc sony_toc; /* entries gathered from all 262 sessions */ 263 264static int sony_toc_read = 0; /* Has the TOC been read for 265 the drive? */ 266 267static struct s_sony_subcode last_sony_subcode; /* Points to the last 268 subcode address read */ 269 270static volatile int sony_inuse = 0; /* Is the drive in use? Only one operation 271 at a time allowed */ 272 273static DECLARE_WAIT_QUEUE_HEAD(sony_wait); /* Things waiting for the drive */ 274 275static struct task_struct *has_cd_task = NULL; /* The task that is currently 276 using the CDROM drive, or 277 NULL if none. */ 278 279static int is_double_speed = 0; /* does the drive support double speed ? */ 280static int is_a_cdu31a = 1; /* Is the drive a CDU31A? */ 281 282static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */ 283 284/* 285 * The audio status uses the values from read subchannel data as specified 286 * in include/linux/cdrom.h. 287 */ 288static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS; 289 290/* 291 * The following are a hack for pausing and resuming audio play. The drive 292 * does not work as I would expect it, if you stop it then start it again, 293 * the drive seeks back to the beginning and starts over. This holds the 294 * position during a pause so a resume can restart it. It uses the 295 * audio status variable above to tell if it is paused. 296 */ 297static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 }; 298static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 }; 299 300/* What IRQ is the drive using? 0 if none. */ 301static int cdu31a_irq = 0; 302MODULE_PARM(cdu31a_irq, "i"); 303 304/* The interrupt handler will wake this queue up when it gets an 305 interrupts. */ 306DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait); 307 308static int curr_control_reg = 0; /* Current value of the control register */ 309 310/* A disk changed variable. When a disk change is detected, it will 311 all be set to TRUE. As the upper layers ask for disk_changed status 312 it will be cleared. */ 313static char disk_changed; 314 315/* Variable for using the readahead buffer. The readahead buffer 316 is used for raw sector reads and for blocksizes that are smaller 317 than 2048 bytes. */ 318static char readahead_buffer[CD_FRAMESIZE_RAW]; 319static int readahead_dataleft = 0; 320static int readahead_bad = 0; 321 322/* Used to time a short period to abort an operation after the 323 drive has been idle for a while. This keeps the light on 324 the drive from flashing for very long. */ 325static struct timer_list cdu31a_abort_timer; 326 327/* Marks if the timeout has started an abort read. This is used 328 on entry to the drive to tell the code to read out the status 329 from the abort read. */ 330static int abort_read_started = 0; 331 332 333/* 334 * This routine returns 1 if the disk has been changed since the last 335 * check or 0 if it hasn't. 336 */ 337static int scd_disk_change(kdev_t full_dev) 338{ 339 int retval; 340 341 retval = disk_changed; 342 disk_changed = 0; 343 344 return retval; 345} 346 347/* 348 * Uniform cdrom interface function 349 * report back, if disc has changed from time of last request. 350 */ 351static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr) 352{ 353 return scd_disk_change(cdi->dev); 354} 355 356/* 357 * Uniform cdrom interface function 358 * report back, if drive is ready 359 */ 360static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr) 361{ 362 if (CDSL_CURRENT != slot_nr) { 363 /* we have no changer support */ 364 return -EINVAL; 365 } 366 if (scd_spinup() == 0) { 367 sony_spun_up = 1; 368 } 369 return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY; 370} 371 372static inline void enable_interrupts(void) 373{ 374 curr_control_reg |= (SONY_ATTN_INT_EN_BIT 375 | SONY_RES_RDY_INT_EN_BIT 376 | SONY_DATA_RDY_INT_EN_BIT); 377 outb(curr_control_reg, sony_cd_control_reg); 378} 379 380static inline void disable_interrupts(void) 381{ 382 curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT 383 | SONY_RES_RDY_INT_EN_BIT 384 | SONY_DATA_RDY_INT_EN_BIT); 385 outb(curr_control_reg, sony_cd_control_reg); 386} 387 388/* 389 * Wait a little while (used for polling the drive). If in initialization, 390 * setting a timeout doesn't work, so just loop for a while. 391 */ 392static inline void sony_sleep(void) 393{ 394 unsigned long flags; 395 396 if (cdu31a_irq <= 0) { 397 current->state = TASK_INTERRUPTIBLE; 398 schedule_timeout(0); 399 } else { /* Interrupt driven */ 400 401 save_flags(flags); 402 cli(); 403 enable_interrupts(); 404 interruptible_sleep_on(&cdu31a_irq_wait); 405 restore_flags(flags); 406 } 407} 408 409 410/* 411 * The following are convenience routine to read various status and set 412 * various conditions in the drive. 413 */ 414static inline int is_attention(void) 415{ 416 return ((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0); 417} 418 419static inline int is_busy(void) 420{ 421 return ((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0); 422} 423 424static inline int is_data_ready(void) 425{ 426 return ((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0); 427} 428 429static inline int is_data_requested(void) 430{ 431 return ((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0); 432} 433 434static inline int is_result_ready(void) 435{ 436 return ((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0); 437} 438 439static inline int is_param_write_rdy(void) 440{ 441 return ((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0); 442} 443 444static inline int is_result_reg_not_empty(void) 445{ 446 return ((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0); 447} 448 449static inline void reset_drive(void) 450{ 451 curr_control_reg = 0; 452 readahead_dataleft = 0; 453 sony_toc_read = 0; 454 outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg); 455} 456 457/* 458 * Uniform cdrom interface function 459 * reset drive and return when it is ready 460 */ 461static int scd_reset(struct cdrom_device_info *cdi) 462{ 463 int retry_count; 464 465 reset_drive(); 466 467 retry_count = jiffies + SONY_RESET_TIMEOUT; 468 while (time_before(jiffies, retry_count) && (!is_attention())) { 469 sony_sleep(); 470 } 471 472 return 0; 473} 474 475static inline void clear_attention(void) 476{ 477 outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg); 478} 479 480static inline void clear_result_ready(void) 481{ 482 outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg); 483} 484 485static inline void clear_data_ready(void) 486{ 487 outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT, 488 sony_cd_control_reg); 489} 490 491static inline void clear_param_reg(void) 492{ 493 outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg); 494} 495 496static inline unsigned char read_status_register(void) 497{ 498 return (inb(sony_cd_status_reg)); 499} 500 501static inline unsigned char read_result_register(void) 502{ 503 return (inb(sony_cd_result_reg)); 504} 505 506static inline unsigned char read_data_register(void) 507{ 508 return (inb(sony_cd_read_reg)); 509} 510 511static inline void write_param(unsigned char param) 512{ 513 outb(param, sony_cd_param_reg); 514} 515 516static inline void write_cmd(unsigned char cmd) 517{ 518 outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT, 519 sony_cd_control_reg); 520 outb(cmd, sony_cd_cmd_reg); 521} 522 523static void cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs) 524{ 525 unsigned char val; 526 527 if (abort_read_started) { 528 /* We might be waiting for an abort to finish. Don't 529 disable interrupts yet, though, because we handle 530 this one here. */ 531 /* Clear out the result registers. */ 532 while (is_result_reg_not_empty()) { 533 val = read_result_register(); 534 } 535 clear_data_ready(); 536 clear_result_ready(); 537 538 /* Clear out the data */ 539 while (is_data_requested()) { 540 val = read_data_register(); 541 } 542 abort_read_started = 0; 543 544 /* If something was waiting, wake it up now. */ 545 if (waitqueue_active(&cdu31a_irq_wait)) { 546 disable_interrupts(); 547 wake_up(&cdu31a_irq_wait); 548 } 549 } else if (waitqueue_active(&cdu31a_irq_wait)) { 550 disable_interrupts(); 551 wake_up(&cdu31a_irq_wait); 552 } else { 553 disable_interrupts(); 554 printk 555 ("CDU31A: Got an interrupt but nothing was waiting\n"); 556 } 557} 558 559/* 560 * give more verbose error messages 561 */ 562static unsigned char *translate_error(unsigned char err_code) 563{ 564 static unsigned char errbuf[80]; 565 566 switch (err_code) { 567 case 0x10: 568 return "illegal command "; 569 case 0x11: 570 return "illegal parameter "; 571 572 case 0x20: 573 return "not loaded "; 574 case 0x21: 575 return "no disc "; 576 case 0x22: 577 return "not spinning "; 578 case 0x23: 579 return "spinning "; 580 case 0x25: 581 return "spindle servo "; 582 case 0x26: 583 return "focus servo "; 584 case 0x29: 585 return "eject mechanism "; 586 case 0x2a: 587 return "audio playing "; 588 case 0x2c: 589 return "emergency eject "; 590 591 case 0x30: 592 return "focus "; 593 case 0x31: 594 return "frame sync "; 595 case 0x32: 596 return "subcode address "; 597 case 0x33: 598 return "block sync "; 599 case 0x34: 600 return "header address "; 601 602 case 0x40: 603 return "illegal track read "; 604 case 0x41: 605 return "mode 0 read "; 606 case 0x42: 607 return "illegal mode read "; 608 case 0x43: 609 return "illegal block size read "; 610 case 0x44: 611 return "mode read "; 612 case 0x45: 613 return "form read "; 614 case 0x46: 615 return "leadout read "; 616 case 0x47: 617 return "buffer overrun "; 618 619 case 0x53: 620 return "unrecoverable CIRC "; 621 case 0x57: 622 return "unrecoverable LECC "; 623 624 case 0x60: 625 return "no TOC "; 626 case 0x61: 627 return "invalid subcode data "; 628 case 0x63: 629 return "focus on TOC read "; 630 case 0x64: 631 return "frame sync on TOC read "; 632 case 0x65: 633 return "TOC data "; 634 635 case 0x70: 636 return "hardware failure "; 637 case 0x91: 638 return "leadin "; 639 case 0x92: 640 return "leadout "; 641 case 0x93: 642 return "data track "; 643 } 644 sprintf(errbuf, "unknown 0x%02x ", err_code); 645 return errbuf; 646} 647 648/* 649 * Set the drive parameters so the drive will auto-spin-up when a 650 * disk is inserted. 651 */ 652static void set_drive_params(int want_doublespeed) 653{ 654 unsigned char res_reg[12]; 655 unsigned int res_size; 656 unsigned char params[3]; 657 658 659 params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME; 660 params[1] = 0x00; /* Never spin down the drive. */ 661 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, 662 params, 2, res_reg, &res_size); 663 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) { 664 printk(" Unable to set spin-down time: 0x%2.2x\n", 665 res_reg[1]); 666 } 667 668 params[0] = SONY_SD_MECH_CONTROL; 669 params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */ 670 671 if (is_auto_eject) 672 params[1] |= SONY_AUTO_EJECT_BIT; 673 674 if (is_double_speed && want_doublespeed) { 675 params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if 676 possible */ 677 } 678 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, 679 params, 2, res_reg, &res_size); 680 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) { 681 printk(" Unable to set mechanical parameters: 0x%2.2x\n", 682 res_reg[1]); 683 } 684} 685 686/* 687 * Uniform cdrom interface function 688 * select reading speed for data access 689 */ 690static int scd_select_speed(struct cdrom_device_info *cdi, int speed) 691{ 692 if (speed == 0) 693 sony_speed = 1; 694 else 695 sony_speed = speed - 1; 696 697 set_drive_params(sony_speed); 698 return 0; 699} 700 701/* 702 * Uniform cdrom interface function 703 * lock or unlock eject button 704 */ 705static int scd_lock_door(struct cdrom_device_info *cdi, int lock) 706{ 707 if (lock == 0 && sony_usage == 1) { 708 /* Unlock the door, only if nobody is using the drive */ 709 is_auto_eject = 1; 710 } else { 711 is_auto_eject = 0; 712 } 713 set_drive_params(sony_speed); 714 return 0; 715} 716 717/* 718 * This code will reset the drive and attempt to restore sane parameters. 719 */ 720static void restart_on_error(void) 721{ 722 unsigned char res_reg[12]; 723 unsigned int res_size; 724 unsigned int retry_count; 725 726 727 printk("cdu31a: Resetting drive on error\n"); 728 reset_drive(); 729 retry_count = jiffies + SONY_RESET_TIMEOUT; 730 while (time_before(jiffies, retry_count) && (!is_attention())) { 731 sony_sleep(); 732 } 733 set_drive_params(sony_speed); 734 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size); 735 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) { 736 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", 737 res_reg[1]); 738 } 739 740 current->state = TASK_INTERRUPTIBLE; 741 schedule_timeout(2 * HZ); 742 743 sony_get_toc(); 744} 745 746/* 747 * This routine writes data to the parameter register. Since this should 748 * happen fairly fast, it is polled with no OS waits between. 749 */ 750static int write_params(unsigned char *params, int num_params) 751{ 752 unsigned int retry_count; 753 754 755 retry_count = SONY_READY_RETRIES; 756 while ((retry_count > 0) && (!is_param_write_rdy())) { 757 retry_count--; 758 } 759 if (!is_param_write_rdy()) { 760 return -EIO; 761 } 762 763 while (num_params > 0) { 764 write_param(*params); 765 params++; 766 num_params--; 767 } 768 769 return 0; 770} 771 772 773/* 774 * The following reads data from the command result register. It is a 775 * fairly complex routine, all status info flows back through this 776 * interface. The algorithm is stolen directly from the flowcharts in 777 * the drive manual. 778 */ 779static void 780get_result(unsigned char *result_buffer, unsigned int *result_size) 781{ 782 unsigned char a, b; 783 int i; 784 unsigned int retry_count; 785 786 787 while (handle_sony_cd_attention()); 788 /* Wait for the result data to be ready */ 789 retry_count = jiffies + SONY_JIFFIES_TIMEOUT; 790 while (time_before(jiffies, retry_count) 791 && (is_busy() || (!(is_result_ready())))) { 792 sony_sleep(); 793 794 while (handle_sony_cd_attention()); 795 } 796 if (is_busy() || (!(is_result_ready()))) { 797#if DEBUG 798 printk("CDU31A timeout out %d\n", __LINE__); 799#endif 800 result_buffer[0] = 0x20; 801 result_buffer[1] = SONY_TIMEOUT_OP_ERR; 802 *result_size = 2; 803 return; 804 } 805 806 /* 807 * Get the first two bytes. This determines what else needs 808 * to be done. 809 */ 810 clear_result_ready(); 811 a = read_result_register(); 812 *result_buffer = a; 813 result_buffer++; 814 815 /* Check for block error status result. */ 816 if ((a & 0xf0) == 0x50) { 817 *result_size = 1; 818 return; 819 } 820 821 b = read_result_register(); 822 *result_buffer = b; 823 result_buffer++; 824 *result_size = 2; 825 826 /* 827 * 0x20 means an error occurred. Byte 2 will have the error code. 828 * Otherwise, the command succeeded, byte 2 will have the count of 829 * how many more status bytes are coming. 830 * 831 * The result register can be read 10 bytes at a time, a wait for 832 * result ready to be asserted must be done between every 10 bytes. 833 */ 834 if ((a & 0xf0) != 0x20) { 835 if (b > 8) { 836 for (i = 0; i < 8; i++) { 837 *result_buffer = read_result_register(); 838 result_buffer++; 839 (*result_size)++; 840 } 841 b = b - 8; 842 843 while (b > 10) { 844 retry_count = SONY_READY_RETRIES; 845 while ((retry_count > 0) 846 && (!is_result_ready())) { 847 retry_count--; 848 } 849 if (!is_result_ready()) { 850#if DEBUG 851 printk("CDU31A timeout out %d\n", 852 __LINE__); 853#endif 854 result_buffer[0] = 0x20; 855 result_buffer[1] = 856 SONY_TIMEOUT_OP_ERR; 857 *result_size = 2; 858 return; 859 } 860 861 clear_result_ready(); 862 863 for (i = 0; i < 10; i++) { 864 *result_buffer = 865 read_result_register(); 866 result_buffer++; 867 (*result_size)++; 868 } 869 b = b - 10; 870 } 871 872 if (b > 0) { 873 retry_count = SONY_READY_RETRIES; 874 while ((retry_count > 0) 875 && (!is_result_ready())) { 876 retry_count--; 877 } 878 if (!is_result_ready()) { 879#if DEBUG 880 printk("CDU31A timeout out %d\n", 881 __LINE__); 882#endif 883 result_buffer[0] = 0x20; 884 result_buffer[1] = 885 SONY_TIMEOUT_OP_ERR; 886 *result_size = 2; 887 return; 888 } 889 } 890 } 891 892 while (b > 0) { 893 *result_buffer = read_result_register(); 894 result_buffer++; 895 (*result_size)++; 896 b--; 897 } 898 } 899} 900 901/* 902 * Do a command that does not involve data transfer. This routine must 903 * be re-entrant from the same task to support being called from the 904 * data operation code when an error occurs. 905 */ 906static void 907do_sony_cd_cmd(unsigned char cmd, 908 unsigned char *params, 909 unsigned int num_params, 910 unsigned char *result_buffer, unsigned int *result_size) 911{ 912 unsigned int retry_count; 913 int num_retries; 914 int recursive_call; 915 unsigned long flags; 916 917 918 save_flags(flags); 919 cli(); 920 if (current != has_cd_task) { /* Allow recursive calls to this routine */ 921 while (sony_inuse) { 922 interruptible_sleep_on(&sony_wait); 923 if (signal_pending(current)) { 924 result_buffer[0] = 0x20; 925 result_buffer[1] = SONY_SIGNAL_OP_ERR; 926 *result_size = 2; 927 restore_flags(flags); 928 return; 929 } 930 } 931 sony_inuse = 1; 932 has_cd_task = current; 933 recursive_call = 0; 934 } else { 935 recursive_call = 1; 936 } 937 938 num_retries = 0; 939retry_cd_operation: 940 941 while (handle_sony_cd_attention()); 942 943 sti(); 944 945 retry_count = jiffies + SONY_JIFFIES_TIMEOUT; 946 while (time_before(jiffies, retry_count) && (is_busy())) { 947 sony_sleep(); 948 949 while (handle_sony_cd_attention()); 950 } 951 if (is_busy()) { 952#if DEBUG 953 printk("CDU31A timeout out %d\n", __LINE__); 954#endif 955 result_buffer[0] = 0x20; 956 result_buffer[1] = SONY_TIMEOUT_OP_ERR; 957 *result_size = 2; 958 } else { 959 clear_result_ready(); 960 clear_param_reg(); 961 962 write_params(params, num_params); 963 write_cmd(cmd); 964 965 get_result(result_buffer, result_size); 966 } 967 968 if (((result_buffer[0] & 0xf0) == 0x20) 969 && (num_retries < MAX_CDU31A_RETRIES)) { 970 num_retries++; 971 current->state = TASK_INTERRUPTIBLE; 972 schedule_timeout(HZ / 10); /* Wait .1 seconds on retries */ 973 goto retry_cd_operation; 974 } 975 976 if (!recursive_call) { 977 has_cd_task = NULL; 978 sony_inuse = 0; 979 wake_up_interruptible(&sony_wait); 980 } 981 982 restore_flags(flags); 983} 984 985 986/* 987 * Handle an attention from the drive. This will return 1 if it found one 988 * or 0 if not (if one is found, the caller might want to call again). 989 * 990 * This routine counts the number of consecutive times it is called 991 * (since this is always called from a while loop until it returns 992 * a 0), and returns a 0 if it happens too many times. This will help 993 * prevent a lockup. 994 */ 995static int handle_sony_cd_attention(void) 996{ 997 unsigned char atten_code; 998 static int num_consecutive_attentions = 0; 999 volatile int val; 1000 1001 1002#if 0*DEBUG 1003 printk("Entering handle_sony_cd_attention\n"); 1004#endif 1005 if (is_attention()) { 1006 if (num_consecutive_attentions > 1007 CDU31A_MAX_CONSECUTIVE_ATTENTIONS) { 1008 printk 1009 ("cdu31a: Too many consecutive attentions: %d\n", 1010 num_consecutive_attentions); 1011 num_consecutive_attentions = 0; 1012#if DEBUG 1013 printk("Leaving handle_sony_cd_attention at %d\n", 1014 __LINE__); 1015#endif 1016 return (0); 1017 } 1018 1019 clear_attention(); 1020 atten_code = read_result_register(); 1021 1022 switch (atten_code) { 1023 /* Someone changed the CD. Mark it as changed */ 1024 case SONY_MECH_LOADED_ATTN: 1025 disk_changed = 1; 1026 sony_toc_read = 0; 1027 sony_audio_status = CDROM_AUDIO_NO_STATUS; 1028 sony_blocks_left = 0; 1029 break; 1030 1031 case SONY_SPIN_DOWN_COMPLETE_ATTN: 1032 /* Mark the disk as spun down. */ 1033 sony_spun_up = 0; 1034 break; 1035 1036 case SONY_AUDIO_PLAY_DONE_ATTN: 1037 sony_audio_status = CDROM_AUDIO_COMPLETED; 1038 read_subcode(); 1039 break; 1040 1041 case SONY_EJECT_PUSHED_ATTN: 1042 if (is_auto_eject) { 1043 sony_audio_status = CDROM_AUDIO_INVALID; 1044 } 1045 break; 1046 1047 case SONY_LEAD_IN_ERR_ATTN: 1048 case SONY_LEAD_OUT_ERR_ATTN: 1049 case SONY_DATA_TRACK_ERR_ATTN: 1050 case SONY_AUDIO_PLAYBACK_ERR_ATTN: 1051 sony_audio_status = CDROM_AUDIO_ERROR; 1052 break; 1053 } 1054 1055 num_consecutive_attentions++; 1056#if DEBUG 1057 printk("Leaving handle_sony_cd_attention at %d\n", 1058 __LINE__); 1059#endif 1060 return (1); 1061 } else if (abort_read_started) { 1062 while (is_result_reg_not_empty()) { 1063 val = read_result_register(); 1064 } 1065 clear_data_ready(); 1066 clear_result_ready(); 1067 /* Clear out the data */ 1068 while (is_data_requested()) { 1069 val = read_data_register(); 1070 } 1071 abort_read_started = 0; 1072#if DEBUG 1073 printk("Leaving handle_sony_cd_attention at %d\n", 1074 __LINE__); 1075#endif 1076 return (1); 1077 } 1078 1079 num_consecutive_attentions = 0; 1080#if 0*DEBUG 1081 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__); 1082#endif 1083 return (0); 1084} 1085 1086 1087/* Convert from an integer 0-99 to BCD */ 1088static inline unsigned int int_to_bcd(unsigned int val) 1089{ 1090 int retval; 1091 1092 1093 retval = (val / 10) << 4; 1094 retval = retval | val % 10; 1095 return (retval); 1096} 1097 1098 1099/* Convert from BCD to an integer from 0-99 */ 1100static unsigned int bcd_to_int(unsigned int bcd) 1101{ 1102 return ((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f)); 1103} 1104 1105 1106/* 1107 * Convert a logical sector value (like the OS would want to use for 1108 * a block device) to an MSF format. 1109 */ 1110static void log_to_msf(unsigned int log, unsigned char *msf) 1111{ 1112 log = log + LOG_START_OFFSET; 1113 msf[0] = int_to_bcd(log / 4500); 1114 log = log % 4500; 1115 msf[1] = int_to_bcd(log / 75); 1116 msf[2] = int_to_bcd(log % 75); 1117} 1118 1119 1120/* 1121 * Convert an MSF format to a logical sector. 1122 */ 1123static unsigned int msf_to_log(unsigned char *msf) 1124{ 1125 unsigned int log; 1126 1127 1128 log = msf[2]; 1129 log += msf[1] * 75; 1130 log += msf[0] * 4500; 1131 log = log - LOG_START_OFFSET; 1132 1133 return log; 1134} 1135 1136 1137/* 1138 * Take in integer size value and put it into a buffer like 1139 * the drive would want to see a number-of-sector value. 1140 */ 1141static void size_to_buf(unsigned int size, unsigned char *buf) 1142{ 1143 buf[0] = size / 65536; 1144 size = size % 65536; 1145 buf[1] = size / 256; 1146 buf[2] = size % 256; 1147} 1148 1149/* Starts a read operation. Returns 0 on success and 1 on failure. 1150 The read operation used here allows multiple sequential sectors 1151 to be read and status returned for each sector. The driver will 1152 read the output one at a time as the requests come and abort the 1153 operation if the requested sector is not the next one from the 1154 drive. */ 1155static int 1156start_request(unsigned int sector, unsigned int nsect, int read_nsect_only) 1157{ 1158 unsigned char params[6]; 1159 unsigned int read_size; 1160 unsigned int retry_count; 1161 1162 1163#if DEBUG 1164 printk("Entering start_request\n"); 1165#endif 1166 log_to_msf(sector, params); 1167 /* If requested, read exactly what was asked. */ 1168 if (read_nsect_only) { 1169 read_size = nsect; 1170 } 1171 /* 1172 * If the full read-ahead would go beyond the end of the media, trim 1173 * it back to read just till the end of the media. 1174 */ 1175 else if ((sector + nsect) >= sony_toc.lead_out_start_lba) { 1176 read_size = sony_toc.lead_out_start_lba - sector; 1177 } 1178 /* Read the full readahead amount. */ 1179 else { 1180 read_size = CDU31A_READAHEAD / 4; 1181 } 1182 size_to_buf(read_size, ¶ms[3]); 1183 1184 /* 1185 * Clear any outstanding attentions and wait for the drive to 1186 * complete any pending operations. 1187 */ 1188 while (handle_sony_cd_attention()); 1189 1190 retry_count = jiffies + SONY_JIFFIES_TIMEOUT; 1191 while (time_before(jiffies, retry_count) && (is_busy())) { 1192 sony_sleep(); 1193 1194 while (handle_sony_cd_attention()); 1195 } 1196 1197 if (is_busy()) { 1198 printk("CDU31A: Timeout while waiting to issue command\n"); 1199#if DEBUG 1200 printk("Leaving start_request at %d\n", __LINE__); 1201#endif 1202 return (1); 1203 } else { 1204 /* Issue the command */ 1205 clear_result_ready(); 1206 clear_param_reg(); 1207 1208 write_params(params, 6); 1209 write_cmd(SONY_READ_BLKERR_STAT_CMD); 1210 1211 sony_blocks_left = read_size * 4; 1212 sony_next_block = sector * 4; 1213 readahead_dataleft = 0; 1214 readahead_bad = 0; 1215#if DEBUG 1216 printk("Leaving start_request at %d\n", __LINE__); 1217#endif 1218 return (0); 1219 } 1220#if DEBUG 1221 printk("Leaving start_request at %d\n", __LINE__); 1222#endif 1223} 1224 1225/* Abort a pending read operation. Clear all the drive status and 1226 readahead variables. */ 1227static void abort_read(void) 1228{ 1229 unsigned char result_reg[2]; 1230 int result_size; 1231 volatile int val; 1232 1233 1234 do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size); 1235 if ((result_reg[0] & 0xf0) == 0x20) { 1236 printk("CDU31A: Error aborting read, %s error\n", 1237 translate_error(result_reg[1])); 1238 } 1239 1240 while (is_result_reg_not_empty()) { 1241 val = read_result_register(); 1242 } 1243 clear_data_ready(); 1244 clear_result_ready(); 1245 /* Clear out the data */ 1246 while (is_data_requested()) { 1247 val = read_data_register(); 1248 } 1249 1250 sony_blocks_left = 0; 1251 readahead_dataleft = 0; 1252 readahead_bad = 0; 1253} 1254 1255/* Called when the timer times out. This will abort the 1256 pending read operation. */ 1257static void handle_abort_timeout(unsigned long data) 1258{ 1259 unsigned long flags; 1260 1261#if DEBUG 1262 printk("Entering handle_abort_timeout\n"); 1263#endif 1264 save_flags(flags); 1265 cli(); 1266 /* If it is in use, ignore it. */ 1267 if (!sony_inuse) { 1268 /* We can't use abort_read(), because it will sleep 1269 or schedule in the timer interrupt. Just start 1270 the operation, finish it on the next access to 1271 the drive. */ 1272 clear_result_ready(); 1273 clear_param_reg(); 1274 write_cmd(SONY_ABORT_CMD); 1275 1276 sony_blocks_left = 0; 1277 readahead_dataleft = 0; 1278 readahead_bad = 0; 1279 abort_read_started = 1; 1280 } 1281 restore_flags(flags); 1282#if DEBUG 1283 printk("Leaving handle_abort_timeout\n"); 1284#endif 1285} 1286 1287/* Actually get data and status from the drive. */ 1288static void 1289input_data(char *buffer, 1290 unsigned int bytesleft, 1291 unsigned int nblocks, unsigned int offset, unsigned int skip) 1292{ 1293 int i; 1294 volatile unsigned char val; 1295 1296 1297#if DEBUG 1298 printk("Entering input_data\n"); 1299#endif 1300 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from 1301 the disk. The real data is after that. */ 1302 if (sony_xa_mode) { 1303 for (i = 0; i < CD_XA_HEAD; i++) { 1304 val = read_data_register(); 1305 } 1306 } 1307 1308 clear_data_ready(); 1309 1310 if (bytesleft == 2048) { /* 2048 byte direct buffer transfer */ 1311 insb(sony_cd_read_reg, buffer, 2048); 1312 readahead_dataleft = 0; 1313 } else { 1314 /* If the input read did not align with the beginning of the block, 1315 skip the necessary bytes. */ 1316 if (skip != 0) { 1317 insb(sony_cd_read_reg, readahead_buffer, skip); 1318 } 1319 1320 /* Get the data into the buffer. */ 1321 insb(sony_cd_read_reg, &buffer[offset], bytesleft); 1322 1323 /* Get the rest of the data into the readahead buffer at the 1324 proper location. */ 1325 readahead_dataleft = (2048 - skip) - bytesleft; 1326 insb(sony_cd_read_reg, 1327 readahead_buffer + bytesleft, readahead_dataleft); 1328 } 1329 sony_blocks_left -= nblocks; 1330 sony_next_block += nblocks; 1331 1332 /* If an XA disk, we have to clear out the rest of the unused 1333 error correction data. */ 1334 if (sony_xa_mode) { 1335 for (i = 0; i < CD_XA_TAIL; i++) { 1336 val = read_data_register(); 1337 } 1338 } 1339#if DEBUG 1340 printk("Leaving input_data at %d\n", __LINE__); 1341#endif 1342} 1343 1344/* read data from the drive. Note the nsect must be <= 4. */ 1345static void 1346read_data_block(char *buffer, 1347 unsigned int block, 1348 unsigned int nblocks, 1349 unsigned char res_reg[], int *res_size) 1350{ 1351 unsigned int retry_count; 1352 unsigned int bytesleft; 1353 unsigned int offset; 1354 unsigned int skip; 1355 1356 1357#if DEBUG 1358 printk("Entering read_data_block\n"); 1359#endif 1360 1361 res_reg[0] = 0; 1362 res_reg[1] = 0; 1363 *res_size = 0; 1364 bytesleft = nblocks * 512; 1365 offset = 0; 1366 1367 /* If the data in the read-ahead does not match the block offset, 1368 then fix things up. */ 1369 if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048)) { 1370 sony_next_block += block % 4; 1371 sony_blocks_left -= block % 4; 1372 skip = (block % 4) * 512; 1373 } else { 1374 skip = 0; 1375 } 1376 1377 /* We have readahead data in the buffer, get that first before we 1378 decide if a read is necessary. */ 1379 if (readahead_dataleft != 0) { 1380 if (bytesleft > readahead_dataleft) { 1381 /* The readahead will not fill the requested buffer, but 1382 get the data out of the readahead into the buffer. */ 1383 memcpy(buffer, 1384 readahead_buffer + (2048 - 1385 readahead_dataleft), 1386 readahead_dataleft); 1387 readahead_dataleft = 0; 1388 bytesleft -= readahead_dataleft; 1389 offset += readahead_dataleft; 1390 } else { 1391 /* The readahead will fill the whole buffer, get the data 1392 and return. */ 1393 memcpy(buffer, 1394 readahead_buffer + (2048 - 1395 readahead_dataleft), 1396 bytesleft); 1397 readahead_dataleft -= bytesleft; 1398 bytesleft = 0; 1399 sony_blocks_left -= nblocks; 1400 sony_next_block += nblocks; 1401 1402 /* If the data in the readahead is bad, return an error so the 1403 driver will abort the buffer. */ 1404 if (readahead_bad) { 1405 res_reg[0] = 0x20; 1406 res_reg[1] = SONY_BAD_DATA_ERR; 1407 *res_size = 2; 1408 } 1409 1410 if (readahead_dataleft == 0) { 1411 readahead_bad = 0; 1412 } 1413 1414 /* Final transfer is done for read command, get final result. */ 1415 if (sony_blocks_left == 0) { 1416 get_result(res_reg, res_size); 1417 } 1418#if DEBUG 1419 printk("Leaving read_data_block at %d\n", 1420 __LINE__); 1421#endif 1422 return; 1423 } 1424 } 1425 1426 /* Wait for the drive to tell us we have something */ 1427 retry_count = jiffies + SONY_JIFFIES_TIMEOUT; 1428 while (time_before(jiffies, retry_count) && !(is_data_ready())) { 1429 while (handle_sony_cd_attention()); 1430 1431 sony_sleep(); 1432 } 1433 if (!(is_data_ready())) { 1434 if (is_result_ready()) { 1435 get_result(res_reg, res_size); 1436 if ((res_reg[0] & 0xf0) != 0x20) { 1437 printk 1438 ("CDU31A: Got result that should have been error: %d\n", 1439 res_reg[0]); 1440 res_reg[0] = 0x20; 1441 res_reg[1] = SONY_BAD_DATA_ERR; 1442 *res_size = 2; 1443 } 1444 abort_read(); 1445 } else { 1446#if DEBUG 1447 printk("CDU31A timeout out %d\n", __LINE__); 1448#endif 1449 res_reg[0] = 0x20; 1450 res_reg[1] = SONY_TIMEOUT_OP_ERR; 1451 *res_size = 2; 1452 abort_read(); 1453 } 1454 } else { 1455 input_data(buffer, bytesleft, nblocks, offset, skip); 1456 1457 /* Wait for the status from the drive. */ 1458 retry_count = jiffies + SONY_JIFFIES_TIMEOUT; 1459 while (time_before(jiffies, retry_count) 1460 && !(is_result_ready())) { 1461 while (handle_sony_cd_attention()); 1462 1463 sony_sleep(); 1464 } 1465 1466 if (!is_result_ready()) { 1467#if DEBUG 1468 printk("CDU31A timeout out %d\n", __LINE__); 1469#endif 1470 res_reg[0] = 0x20; 1471 res_reg[1] = SONY_TIMEOUT_OP_ERR; 1472 *res_size = 2; 1473 abort_read(); 1474 } else { 1475 get_result(res_reg, res_size); 1476 1477 /* If we got a buffer status, handle that. */ 1478 if ((res_reg[0] & 0xf0) == 0x50) { 1479 1480 if ((res_reg[0] == 1481 SONY_NO_CIRC_ERR_BLK_STAT) 1482 || (res_reg[0] == 1483 SONY_NO_LECC_ERR_BLK_STAT) 1484 || (res_reg[0] == 1485 SONY_RECOV_LECC_ERR_BLK_STAT)) { 1486 /* The data was successful, but if data was read from 1487 the readahead and it was bad, set the whole 1488 buffer as bad. */ 1489 if (readahead_bad) { 1490 readahead_bad = 0; 1491 res_reg[0] = 0x20; 1492 res_reg[1] = 1493 SONY_BAD_DATA_ERR; 1494 *res_size = 2; 1495 } 1496 } else { 1497 printk 1498 ("CDU31A: Data block error: 0x%x\n", 1499 res_reg[0]); 1500 res_reg[0] = 0x20; 1501 res_reg[1] = SONY_BAD_DATA_ERR; 1502 *res_size = 2; 1503 1504 /* Data is in the readahead buffer but an error was returned. 1505 Make sure future requests don't use the data. */ 1506 if (bytesleft != 2048) { 1507 readahead_bad = 1; 1508 } 1509 } 1510 1511 /* Final transfer is done for read command, get final result. */ 1512 if (sony_blocks_left == 0) { 1513 get_result(res_reg, res_size); 1514 } 1515 } else if ((res_reg[0] & 0xf0) != 0x20) { 1516 /* The drive gave me bad status, I don't know what to do. 1517 Reset the driver and return an error. */ 1518 printk 1519 ("CDU31A: Invalid block status: 0x%x\n", 1520 res_reg[0]); 1521 restart_on_error(); 1522 res_reg[0] = 0x20; 1523 res_reg[1] = SONY_BAD_DATA_ERR; 1524 *res_size = 2; 1525 } 1526 } 1527 } 1528#if DEBUG 1529 printk("Leaving read_data_block at %d\n", __LINE__); 1530#endif 1531} 1532 1533 1534/* 1535 * The OS calls this to perform a read or write operation to the drive. 1536 * Write obviously fail. Reads to a read ahead of sony_buffer_size 1537 * bytes to help speed operations. This especially helps since the OS 1538 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most 1539 * data access on a CD is done sequentially, this saves a lot of operations. 1540 */ 1541static void do_cdu31a_request(request_queue_t * q) 1542{ 1543 int block; 1544 int nblock; 1545 unsigned char res_reg[12]; 1546 unsigned int res_size; 1547 int num_retries; 1548 unsigned long flags; 1549 1550 1551#if DEBUG 1552 printk("Entering do_cdu31a_request\n"); 1553#endif 1554 1555 /* 1556 * Make sure no one else is using the driver; wait for them 1557 * to finish if it is so. 1558 */ 1559 save_flags(flags); 1560 cli(); 1561 while (sony_inuse) { 1562 interruptible_sleep_on(&sony_wait); 1563 if (signal_pending(current)) { 1564 restore_flags(flags); 1565 if (!QUEUE_EMPTY 1566 && CURRENT->rq_status != RQ_INACTIVE) { 1567 end_request(0); 1568 } 1569 restore_flags(flags); 1570#if DEBUG 1571 printk("Leaving do_cdu31a_request at %d\n", 1572 __LINE__); 1573#endif 1574 return; 1575 } 1576 } 1577 sony_inuse = 1; 1578 has_cd_task = current; 1579 1580 /* Get drive status before doing anything. */ 1581 while (handle_sony_cd_attention()); 1582 1583 /* Make sure we have a valid TOC. */ 1584 sony_get_toc(); 1585 1586 spin_unlock_irq(&io_request_lock); 1587 1588 /* Make sure the timer is cancelled. */ 1589 del_timer(&cdu31a_abort_timer); 1590 1591 while (1) { 1592 cdu31a_request_startover: 1593 /* 1594 * The beginning here is stolen from the hard disk driver. I hope 1595 * it's right. 1596 */ 1597 if (QUEUE_EMPTY || CURRENT->rq_status == RQ_INACTIVE) { 1598 goto end_do_cdu31a_request; 1599 } 1600 1601 if (!sony_spun_up) { 1602 scd_spinup(); 1603 } 1604 1605 /* I don't use INIT_REQUEST because it calls return, which would 1606 return without unlocking the device. It shouldn't matter, 1607 but just to be safe... */ 1608 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR) { 1609 panic(DEVICE_NAME ": request list destroyed"); 1610 } 1611 if (CURRENT->bh) { 1612 if (!buffer_locked(CURRENT->bh)) { 1613 panic(DEVICE_NAME ": block not locked"); 1614 } 1615 } 1616 1617 block = CURRENT->sector; 1618 nblock = CURRENT->nr_sectors; 1619 1620 if (!sony_toc_read) { 1621 printk("CDU31A: TOC not read\n"); 1622 end_request(0); 1623 goto cdu31a_request_startover; 1624 } 1625 1626 switch (CURRENT->cmd) { 1627 case READ: 1628 /* 1629 * If the block address is invalid or the request goes beyond the end of 1630 * the media, return an error. 1631 */ 1632 if ((block / 4) >= sony_toc.lead_out_start_lba) { 1633 printk 1634 ("CDU31A: Request past end of media\n"); 1635 end_request(0); 1636 goto cdu31a_request_startover; 1637 } 1638 if (((block + nblock) / 4) >= 1639 sony_toc.lead_out_start_lba) { 1640 printk 1641 ("CDU31A: Request past end of media\n"); 1642 end_request(0); 1643 goto cdu31a_request_startover; 1644 } 1645 1646 num_retries = 0; 1647 1648 try_read_again: 1649 while (handle_sony_cd_attention()); 1650 1651 if (!sony_toc_read) { 1652 printk("CDU31A: TOC not read\n"); 1653 end_request(0); 1654 goto cdu31a_request_startover; 1655 } 1656 1657 /* If no data is left to be read from the drive, start the 1658 next request. */ 1659 if (sony_blocks_left == 0) { 1660 if (start_request 1661 (block / 4, CDU31A_READAHEAD / 4, 0)) { 1662 end_request(0); 1663 goto cdu31a_request_startover; 1664 } 1665 } 1666 /* If the requested block is not the next one waiting in 1667 the driver, abort the current operation and start a 1668 new one. */ 1669 else if (block != sony_next_block) { 1670#if DEBUG 1671 printk 1672 ("CDU31A Warning: Read for block %d, expected %d\n", 1673 block, sony_next_block); 1674#endif 1675 abort_read(); 1676 if (!sony_toc_read) { 1677 printk("CDU31A: TOC not read\n"); 1678 end_request(0); 1679 goto cdu31a_request_startover; 1680 } 1681 if (start_request 1682 (block / 4, CDU31A_READAHEAD / 4, 0)) { 1683 printk 1684 ("CDU31a: start request failed\n"); 1685 end_request(0); 1686 goto cdu31a_request_startover; 1687 } 1688 } 1689 1690 read_data_block(CURRENT->buffer, block, nblock, 1691 res_reg, &res_size); 1692 if (res_reg[0] == 0x20) { 1693 if (num_retries > MAX_CDU31A_RETRIES) { 1694 end_request(0); 1695 goto cdu31a_request_startover; 1696 } 1697 1698 num_retries++; 1699 if (res_reg[1] == SONY_NOT_SPIN_ERR) { 1700 do_sony_cd_cmd(SONY_SPIN_UP_CMD, 1701 NULL, 0, res_reg, 1702 &res_size); 1703 } else { 1704 printk 1705 ("CDU31A: %s error for block %d, nblock %d\n", 1706 translate_error(res_reg[1]), 1707 block, nblock); 1708 } 1709 goto try_read_again; 1710 } else { 1711 end_request(1); 1712 } 1713 break; 1714 1715 case WRITE: 1716 end_request(0); 1717 break; 1718 1719 default: 1720 panic("CDU31A: Unknown cmd"); 1721 } 1722 } 1723 1724 end_do_cdu31a_request: 1725 spin_lock_irq(&io_request_lock); 1726 /* Start a timer to time out after a while to disable 1727 the read. */ 1728 cdu31a_abort_timer.expires = jiffies + 2 * HZ; /* Wait 2 seconds */ 1729 add_timer(&cdu31a_abort_timer); 1730 1731 has_cd_task = NULL; 1732 sony_inuse = 0; 1733 wake_up_interruptible(&sony_wait); 1734 restore_flags(flags); 1735#if DEBUG 1736 printk("Leaving do_cdu31a_request at %d\n", __LINE__); 1737#endif 1738} 1739 1740 1741/* 1742 * Read the table of contents from the drive and set up TOC if 1743 * successful. 1744 */ 1745static void sony_get_toc(void) 1746{ 1747 unsigned char res_reg[2]; 1748 unsigned int res_size; 1749 unsigned char parms[1]; 1750 int session; 1751 int num_spin_ups; 1752 int totaltracks = 0; 1753 int mint = 99; 1754 int maxt = 0; 1755 1756#if DEBUG 1757 printk("Entering sony_get_toc\n"); 1758#endif 1759 1760 num_spin_ups = 0; 1761 if (!sony_toc_read) { 1762 respinup_on_gettoc: 1763 /* Ignore the result, since it might error if spinning already. */ 1764 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, 1765 &res_size); 1766 1767 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, 1768 &res_size); 1769 1770 /* The drive sometimes returns error 0. I don't know why, but ignore 1771 it. It seems to mean the drive has already done the operation. */ 1772 if ((res_size < 2) 1773 || ((res_reg[0] != 0) && (res_reg[1] != 0))) { 1774 /* If the drive is already playing, it's ok. */ 1775 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) 1776 || (res_reg[1] == 0)) { 1777 goto gettoc_drive_spinning; 1778 } 1779 1780 /* If the drive says it is not spun up (even though we just did it!) 1781 then retry the operation at least a few times. */ 1782 if ((res_reg[1] == SONY_NOT_SPIN_ERR) 1783 && (num_spin_ups < MAX_CDU31A_RETRIES)) { 1784 num_spin_ups++; 1785 goto respinup_on_gettoc; 1786 } 1787 1788 printk("cdu31a: Error reading TOC: %x %s\n", 1789 res_reg[0], translate_error(res_reg[1])); 1790 return; 1791 } 1792 1793 gettoc_drive_spinning: 1794 1795 /* The idea here is we keep asking for sessions until the command 1796 fails. Then we know what the last valid session on the disk is. 1797 No need to check session 0, since session 0 is the same as session 1798 1; the command returns different information if you give it 0. 1799 */ 1800#if DEBUG 1801 memset(&sony_toc, 0x0e, sizeof(sony_toc)); 1802 memset(&single_toc, 0x0f, sizeof(single_toc)); 1803#endif 1804 session = 1; 1805 while (1) { 1806/* This seems to slow things down enough to make it work. This 1807 * appears to be a problem in do_sony_cd_cmd. This printk seems 1808 * to address the symptoms... -Erik */ 1809 printk("cdu31a: Trying session %d\n", session); 1810 parms[0] = session; 1811 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD, 1812 parms, 1, res_reg, &res_size); 1813 1814#if DEBUG 1815 printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]); 1816#endif 1817 1818 if ((res_size < 2) 1819 || ((res_reg[0] & 0xf0) == 0x20)) { 1820 /* An error reading the TOC, this must be past the last session. */ 1821 if (session == 1) 1822 printk 1823 ("Yikes! Couldn't read any sessions!"); 1824 break; 1825 } 1826#if DEBUG 1827 printk("Reading session %d\n", session); 1828#endif 1829 1830 parms[0] = session; 1831 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD, 1832 parms, 1833 1, 1834 (unsigned char *) &single_toc, 1835 &res_size); 1836 if ((res_size < 2) 1837 || ((single_toc.exec_status[0] & 0xf0) == 1838 0x20)) { 1839 printk 1840 ("cdu31a: Error reading session %d: %x %s\n", 1841 session, single_toc.exec_status[0], 1842 translate_error(single_toc. 1843 exec_status[1])); 1844 /* An error reading the TOC. Return without sony_toc_read 1845 set. */ 1846 return; 1847 } 1848#if DEBUG 1849 printk 1850 ("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n", 1851 single_toc.address0, single_toc.control0, 1852 single_toc.point0, 1853 bcd_to_int(single_toc.first_track_num), 1854 single_toc.disk_type, single_toc.dummy0); 1855 printk 1856 ("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n", 1857 single_toc.address1, single_toc.control1, 1858 single_toc.point1, 1859 bcd_to_int(single_toc.last_track_num), 1860 single_toc.dummy1, single_toc.dummy2); 1861 printk 1862 ("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n", 1863 single_toc.address2, single_toc.control2, 1864 single_toc.point2, 1865 bcd_to_int(single_toc.lead_out_start_msf[0]), 1866 bcd_to_int(single_toc.lead_out_start_msf[1]), 1867 bcd_to_int(single_toc.lead_out_start_msf[2])); 1868 if (res_size > 18 && single_toc.pointb0 > 0xaf) 1869 printk 1870 ("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n" 1871 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n", 1872 single_toc.addressb0, 1873 single_toc.controlb0, 1874 single_toc.pointb0, 1875 bcd_to_int(single_toc. 1876 next_poss_prog_area_msf 1877 [0]), 1878 bcd_to_int(single_toc. 1879 next_poss_prog_area_msf 1880 [1]), 1881 bcd_to_int(single_toc. 1882 next_poss_prog_area_msf 1883 [2]), 1884 single_toc.num_mode_5_pointers, 1885 bcd_to_int(single_toc. 1886 max_start_outer_leadout_msf 1887 [0]), 1888 bcd_to_int(single_toc. 1889 max_start_outer_leadout_msf 1890 [1]), 1891 bcd_to_int(single_toc. 1892 max_start_outer_leadout_msf 1893 [2])); 1894 if (res_size > 27 && single_toc.pointb1 > 0xaf) 1895 printk 1896 ("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n", 1897 single_toc.addressb1, 1898 single_toc.controlb1, 1899 single_toc.pointb1, 1900 single_toc.dummyb0_1[0], 1901 single_toc.dummyb0_1[1], 1902 single_toc.dummyb0_1[2], 1903 single_toc.dummyb0_1[3], 1904 single_toc.num_skip_interval_pointers, 1905 single_toc.num_skip_track_assignments, 1906 single_toc.dummyb0_2); 1907 if (res_size > 36 && single_toc.pointb2 > 0xaf) 1908 printk 1909 ("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n", 1910 single_toc.addressb2, 1911 single_toc.controlb2, 1912 single_toc.pointb2, 1913 single_toc.tracksb2[0], 1914 single_toc.tracksb2[1], 1915 single_toc.tracksb2[2], 1916 single_toc.tracksb2[3], 1917 single_toc.tracksb2[4], 1918 single_toc.tracksb2[5], 1919 single_toc.tracksb2[6]); 1920 if (res_size > 45 && single_toc.pointb3 > 0xaf) 1921 printk 1922 ("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n", 1923 single_toc.addressb3, 1924 single_toc.controlb3, 1925 single_toc.pointb3, 1926 single_toc.tracksb3[0], 1927 single_toc.tracksb3[1], 1928 single_toc.tracksb3[2], 1929 single_toc.tracksb3[3], 1930 single_toc.tracksb3[4], 1931 single_toc.tracksb3[5], 1932 single_toc.tracksb3[6]); 1933 if (res_size > 54 && single_toc.pointb4 > 0xaf) 1934 printk 1935 ("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n", 1936 single_toc.addressb4, 1937 single_toc.controlb4, 1938 single_toc.pointb4, 1939 single_toc.tracksb4[0], 1940 single_toc.tracksb4[1], 1941 single_toc.tracksb4[2], 1942 single_toc.tracksb4[3], 1943 single_toc.tracksb4[4], 1944 single_toc.tracksb4[5], 1945 single_toc.tracksb4[6]); 1946 if (res_size > 63 && single_toc.pointc0 > 0xaf) 1947 printk 1948 ("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n", 1949 single_toc.addressc0, 1950 single_toc.controlc0, 1951 single_toc.pointc0, 1952 single_toc.dummyc0[0], 1953 single_toc.dummyc0[1], 1954 single_toc.dummyc0[2], 1955 single_toc.dummyc0[3], 1956 single_toc.dummyc0[4], 1957 single_toc.dummyc0[5], 1958 single_toc.dummyc0[6]); 1959#endif 1960#undef DEBUG 1961#define DEBUG 0 1962 1963 sony_toc.lead_out_start_msf[0] = 1964 bcd_to_int(single_toc.lead_out_start_msf[0]); 1965 sony_toc.lead_out_start_msf[1] = 1966 bcd_to_int(single_toc.lead_out_start_msf[1]); 1967 sony_toc.lead_out_start_msf[2] = 1968 bcd_to_int(single_toc.lead_out_start_msf[2]); 1969 sony_toc.lead_out_start_lba = 1970 single_toc.lead_out_start_lba = 1971 msf_to_log(sony_toc.lead_out_start_msf); 1972 1973 /* For points that do not exist, move the data over them 1974 to the right location. */ 1975 if (single_toc.pointb0 != 0xb0) { 1976 memmove(((char *) &single_toc) + 27, 1977 ((char *) &single_toc) + 18, 1978 res_size - 18); 1979 res_size += 9; 1980 } else if (res_size > 18) { 1981 sony_toc.lead_out_start_msf[0] = 1982 bcd_to_int(single_toc. 1983 max_start_outer_leadout_msf 1984 [0]); 1985 sony_toc.lead_out_start_msf[1] = 1986 bcd_to_int(single_toc. 1987 max_start_outer_leadout_msf 1988 [1]); 1989 sony_toc.lead_out_start_msf[2] = 1990 bcd_to_int(single_toc. 1991 max_start_outer_leadout_msf 1992 [2]); 1993 sony_toc.lead_out_start_lba = 1994 msf_to_log(sony_toc. 1995 lead_out_start_msf); 1996 } 1997 if (single_toc.pointb1 != 0xb1) { 1998 memmove(((char *) &single_toc) + 36, 1999 ((char *) &single_toc) + 27, 2000 res_size - 27); 2001 res_size += 9; 2002 } 2003 if (single_toc.pointb2 != 0xb2) { 2004 memmove(((char *) &single_toc) + 45, 2005 ((char *) &single_toc) + 36, 2006 res_size - 36); 2007 res_size += 9; 2008 } 2009 if (single_toc.pointb3 != 0xb3) { 2010 memmove(((char *) &single_toc) + 54, 2011 ((char *) &single_toc) + 45, 2012 res_size - 45); 2013 res_size += 9; 2014 } 2015 if (single_toc.pointb4 != 0xb4) { 2016 memmove(((char *) &single_toc) + 63, 2017 ((char *) &single_toc) + 54, 2018 res_size - 54); 2019 res_size += 9; 2020 } 2021 if (single_toc.pointc0 != 0xc0) { 2022 memmove(((char *) &single_toc) + 72, 2023 ((char *) &single_toc) + 63, 2024 res_size - 63); 2025 res_size += 9; 2026 } 2027#if DEBUG 2028 printk 2029 ("start track lba %u, leadout start lba %u\n", 2030 single_toc.start_track_lba, 2031 single_toc.lead_out_start_lba); 2032 { 2033 int i; 2034 for (i = 0; 2035 i < 2036 1 + 2037 bcd_to_int(single_toc.last_track_num) 2038 - 2039 bcd_to_int(single_toc. 2040 first_track_num); i++) { 2041 printk 2042 ("trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n", 2043 i, 2044 single_toc.tracks[i].address, 2045 single_toc.tracks[i].control, 2046 bcd_to_int(single_toc. 2047 tracks[i].track), 2048 bcd_to_int(single_toc. 2049 tracks[i]. 2050 track_start_msf 2051 [0]), 2052 bcd_to_int(single_toc. 2053 tracks[i]. 2054 track_start_msf 2055 [1]), 2056 bcd_to_int(single_toc. 2057 tracks[i]. 2058 track_start_msf 2059 [2])); 2060 if (mint > 2061 bcd_to_int(single_toc. 2062 tracks[i].track)) 2063 mint = 2064 bcd_to_int(single_toc. 2065 tracks[i]. 2066 track); 2067 if (maxt < 2068 bcd_to_int(single_toc. 2069 tracks[i].track)) 2070 maxt = 2071 bcd_to_int(single_toc. 2072 tracks[i]. 2073 track); 2074 } 2075 printk 2076 ("min track number %d, max track number %d\n", 2077 mint, maxt); 2078 } 2079#endif 2080 2081 /* prepare a special table of contents for a CD-I disc. They don't have one. */ 2082 if (single_toc.disk_type == 0x10 && 2083 single_toc.first_track_num == 2 && 2084 single_toc.last_track_num == 2 /* CD-I */ ) { 2085 sony_toc.tracks[totaltracks].address = 1; 2086 sony_toc.tracks[totaltracks].control = 4; /* force data tracks */ 2087 sony_toc.tracks[totaltracks].track = 1; 2088 sony_toc.tracks[totaltracks]. 2089 track_start_msf[0] = 0; 2090 sony_toc.tracks[totaltracks]. 2091 track_start_msf[1] = 2; 2092 sony_toc.tracks[totaltracks]. 2093 track_start_msf[2] = 0; 2094 mint = maxt = 1; 2095 totaltracks++; 2096 } else 2097 /* gather track entries from this session */ 2098 { 2099 int i; 2100 for (i = 0; 2101 i < 2102 1 + 2103 bcd_to_int(single_toc.last_track_num) 2104 - 2105 bcd_to_int(single_toc. 2106 first_track_num); 2107 i++, totaltracks++) { 2108 sony_toc.tracks[totaltracks]. 2109 address = 2110 single_toc.tracks[i].address; 2111 sony_toc.tracks[totaltracks]. 2112 control = 2113 single_toc.tracks[i].control; 2114 sony_toc.tracks[totaltracks]. 2115 track = 2116 bcd_to_int(single_toc. 2117 tracks[i].track); 2118 sony_toc.tracks[totaltracks]. 2119 track_start_msf[0] = 2120 bcd_to_int(single_toc. 2121 tracks[i]. 2122 track_start_msf[0]); 2123 sony_toc.tracks[totaltracks]. 2124 track_start_msf[1] = 2125 bcd_to_int(single_toc. 2126 tracks[i]. 2127 track_start_msf[1]); 2128 sony_toc.tracks[totaltracks]. 2129 track_start_msf[2] = 2130 bcd_to_int(single_toc. 2131 tracks[i]. 2132 track_start_msf[2]); 2133 if (i == 0) 2134 single_toc. 2135 start_track_lba = 2136 msf_to_log(sony_toc. 2137 tracks 2138 [totaltracks]. 2139 track_start_msf); 2140 if (mint > 2141 sony_toc.tracks[totaltracks]. 2142 track) 2143 mint = 2144 sony_toc. 2145 tracks[totaltracks]. 2146 track; 2147 if (maxt < 2148 sony_toc.tracks[totaltracks]. 2149 track) 2150 maxt = 2151 sony_toc. 2152 tracks[totaltracks]. 2153 track; 2154 } 2155 } 2156 sony_toc.first_track_num = mint; 2157 sony_toc.last_track_num = maxt; 2158 /* Disk type of last session wins. For example: 2159 CD-Extra has disk type 0 for the first session, so 2160 a dumb HiFi CD player thinks it is a plain audio CD. 2161 We are interested in the disk type of the last session, 2162 which is 0x20 (XA) for CD-Extra, so we can access the 2163 data track ... */ 2164 sony_toc.disk_type = single_toc.disk_type; 2165 sony_toc.sessions = session; 2166 2167 /* don't believe everything :-) */ 2168 if (session == 1) 2169 single_toc.start_track_lba = 0; 2170 sony_toc.start_track_lba = 2171 single_toc.start_track_lba; 2172 2173 if (session > 1 && single_toc.pointb0 == 0xb0 && 2174 sony_toc.lead_out_start_lba == 2175 single_toc.lead_out_start_lba) { 2176 break; 2177 } 2178 2179 /* Let's not get carried away... */ 2180 if (session > 40) { 2181 printk("cdu31a: too many sessions: %d\n", 2182 session); 2183 break; 2184 } 2185 session++; 2186 } 2187 sony_toc.track_entries = totaltracks; 2188 /* add one entry for the LAST track with track number CDROM_LEADOUT */ 2189 sony_toc.tracks[totaltracks].address = single_toc.address2; 2190 sony_toc.tracks[totaltracks].control = single_toc.control2; 2191 sony_toc.tracks[totaltracks].track = CDROM_LEADOUT; 2192 sony_toc.tracks[totaltracks].track_start_msf[0] = 2193 sony_toc.lead_out_start_msf[0]; 2194 sony_toc.tracks[totaltracks].track_start_msf[1] = 2195 sony_toc.lead_out_start_msf[1]; 2196 sony_toc.tracks[totaltracks].track_start_msf[2] = 2197 sony_toc.lead_out_start_msf[2]; 2198 2199 sony_toc_read = 1; 2200#undef DEBUG 2201#if DEBUG 2202 printk 2203 ("Disk session %d, start track: %d, stop track: %d\n", 2204 session, single_toc.start_track_lba, 2205 single_toc.lead_out_start_lba); 2206#endif 2207 } 2208#if DEBUG 2209 printk("Leaving sony_get_toc\n"); 2210#endif 2211} 2212 2213 2214/* 2215 * Uniform cdrom interface function 2216 * return multisession offset and sector information 2217 */ 2218static int scd_get_last_session(struct cdrom_device_info *cdi, 2219 struct cdrom_multisession *ms_info) 2220{ 2221 if (ms_info == NULL) 2222 return 1; 2223 2224 if (!sony_toc_read) 2225 sony_get_toc(); 2226 2227 ms_info->addr_format = CDROM_LBA; 2228 ms_info->addr.lba = sony_toc.start_track_lba; 2229 ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE || 2230 sony_toc.disk_type == 0x10 /* CDI */ ; 2231 2232 return 0; 2233} 2234 2235/* 2236 * Search for a specific track in the table of contents. 2237 */ 2238static int find_track(int track) 2239{ 2240 int i; 2241 2242 for (i = 0; i <= sony_toc.track_entries; i++) { 2243 if (sony_toc.tracks[i].track == track) { 2244 return i; 2245 } 2246 } 2247 2248 return -1; 2249} 2250 2251 2252/* 2253 * Read the subcode and put it in last_sony_subcode for future use. 2254 */ 2255static int read_subcode(void) 2256{ 2257 unsigned int res_size; 2258 2259 2260 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD, 2261 NULL, 2262 0, (unsigned char *) &last_sony_subcode, &res_size); 2263 if ((res_size < 2) 2264 || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20)) { 2265 printk("Sony CDROM error %s (read_subcode)\n", 2266 translate_error(last_sony_subcode.exec_status[1])); 2267 return -EIO; 2268 } 2269 2270 last_sony_subcode.track_num = 2271 bcd_to_int(last_sony_subcode.track_num); 2272 last_sony_subcode.index_num = 2273 bcd_to_int(last_sony_subcode.index_num); 2274 last_sony_subcode.abs_msf[0] = 2275 bcd_to_int(last_sony_subcode.abs_msf[0]); 2276 last_sony_subcode.abs_msf[1] = 2277 bcd_to_int(last_sony_subcode.abs_msf[1]); 2278 last_sony_subcode.abs_msf[2] = 2279 bcd_to_int(last_sony_subcode.abs_msf[2]); 2280 2281 last_sony_subcode.rel_msf[0] = 2282 bcd_to_int(last_sony_subcode.rel_msf[0]); 2283 last_sony_subcode.rel_msf[1] = 2284 bcd_to_int(last_sony_subcode.rel_msf[1]); 2285 last_sony_subcode.rel_msf[2] = 2286 bcd_to_int(last_sony_subcode.rel_msf[2]); 2287 return 0; 2288} 2289 2290/* 2291 * Uniform cdrom interface function 2292 * return the media catalog number found on some older audio cds 2293 */ 2294static int 2295scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn) 2296{ 2297 unsigned char resbuffer[2 + 14]; 2298 unsigned char *mcnp = mcn->medium_catalog_number; 2299 unsigned char *resp = resbuffer + 3; 2300 unsigned int res_size; 2301 2302 memset(mcn->medium_catalog_number, 0, 14); 2303 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD, 2304 NULL, 0, resbuffer, &res_size); 2305 if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20)); 2306 else { 2307 /* packed bcd to single ASCII digits */ 2308 *mcnp++ = (*resp >> 4) + '0'; 2309 *mcnp++ = (*resp++ & 0x0f) + '0'; 2310 *mcnp++ = (*resp >> 4) + '0'; 2311 *mcnp++ = (*resp++ & 0x0f) + '0'; 2312 *mcnp++ = (*resp >> 4) + '0'; 2313 *mcnp++ = (*resp++ & 0x0f) + '0'; 2314 *mcnp++ = (*resp >> 4) + '0'; 2315 *mcnp++ = (*resp++ & 0x0f) + '0'; 2316 *mcnp++ = (*resp >> 4) + '0'; 2317 *mcnp++ = (*resp++ & 0x0f) + '0'; 2318 *mcnp++ = (*resp >> 4) + '0'; 2319 *mcnp++ = (*resp++ & 0x0f) + '0'; 2320 *mcnp++ = (*resp >> 4) + '0'; 2321 } 2322 *mcnp = '\0'; 2323 return 0; 2324} 2325 2326 2327/* 2328 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If 2329 * the drive is playing, the subchannel needs to be read (since it would be 2330 * changing). If the drive is paused or completed, the subcode information has 2331 * already been stored, just use that. The ioctl call wants things in decimal 2332 * (not BCD), so all the conversions are done. 2333 */ 2334static int sony_get_subchnl_info(struct cdrom_subchnl *schi) 2335{ 2336 /* Get attention stuff */ 2337 while (handle_sony_cd_attention()); 2338 2339 sony_get_toc(); 2340 if (!sony_toc_read) { 2341 return -EIO; 2342 } 2343 2344 switch (sony_audio_status) { 2345 case CDROM_AUDIO_NO_STATUS: 2346 case CDROM_AUDIO_PLAY: 2347 if (read_subcode() < 0) { 2348 return -EIO; 2349 } 2350 break; 2351 2352 case CDROM_AUDIO_PAUSED: 2353 case CDROM_AUDIO_COMPLETED: 2354 break; 2355 2356 case CDROM_AUDIO_INVALID: 2357 case CDROM_AUDIO_ERROR: 2358 default: 2359 return -EIO; 2360 } 2361 2362 schi->cdsc_audiostatus = sony_audio_status; 2363 schi->cdsc_adr = last_sony_subcode.address; 2364 schi->cdsc_ctrl = last_sony_subcode.control; 2365 schi->cdsc_trk = last_sony_subcode.track_num; 2366 schi->cdsc_ind = last_sony_subcode.index_num; 2367 if (schi->cdsc_format == CDROM_MSF) { 2368 schi->cdsc_absaddr.msf.minute = 2369 last_sony_subcode.abs_msf[0]; 2370 schi->cdsc_absaddr.msf.second = 2371 last_sony_subcode.abs_msf[1]; 2372 schi->cdsc_absaddr.msf.frame = 2373 last_sony_subcode.abs_msf[2]; 2374 2375 schi->cdsc_reladdr.msf.minute = 2376 last_sony_subcode.rel_msf[0]; 2377 schi->cdsc_reladdr.msf.second = 2378 last_sony_subcode.rel_msf[1]; 2379 schi->cdsc_reladdr.msf.frame = 2380 last_sony_subcode.rel_msf[2]; 2381 } else if (schi->cdsc_format == CDROM_LBA) { 2382 schi->cdsc_absaddr.lba = 2383 msf_to_log(last_sony_subcode.abs_msf); 2384 schi->cdsc_reladdr.lba = 2385 msf_to_log(last_sony_subcode.rel_msf); 2386 } 2387 2388 return 0; 2389} 2390 2391/* Get audio data from the drive. This is fairly complex because I 2392 am looking for status and data at the same time, but if I get status 2393 then I just look for data. I need to get the status immediately so 2394 the switch from audio to data tracks will happen quickly. */ 2395static void 2396read_audio_data(char *buffer, unsigned char res_reg[], int *res_size) 2397{ 2398 unsigned int retry_count; 2399 int result_read; 2400 2401 2402 res_reg[0] = 0; 2403 res_reg[1] = 0; 2404 *res_size = 0; 2405 result_read = 0; 2406 2407 /* Wait for the drive to tell us we have something */ 2408 retry_count = jiffies + SONY_JIFFIES_TIMEOUT; 2409 continue_read_audio_wait: 2410 while (time_before(jiffies, retry_count) && !(is_data_ready()) 2411 && !(is_result_ready() || result_read)) { 2412 while (handle_sony_cd_attention()); 2413 2414 sony_sleep(); 2415 } 2416 if (!(is_data_ready())) { 2417 if (is_result_ready() && !result_read) { 2418 get_result(res_reg, res_size); 2419 2420 /* Read block status and continue waiting for data. */ 2421 if ((res_reg[0] & 0xf0) == 0x50) { 2422 result_read = 1; 2423 goto continue_read_audio_wait; 2424 } 2425 /* Invalid data from the drive. Shut down the operation. */ 2426 else if ((res_reg[0] & 0xf0) != 0x20) { 2427 printk 2428 ("CDU31A: Got result that should have been error: %d\n", 2429 res_reg[0]); 2430 res_reg[0] = 0x20; 2431 res_reg[1] = SONY_BAD_DATA_ERR; 2432 *res_size = 2; 2433 } 2434 abort_read(); 2435 } else { 2436#if DEBUG 2437 printk("CDU31A timeout out %d\n", __LINE__); 2438#endif 2439 res_reg[0] = 0x20; 2440 res_reg[1] = SONY_TIMEOUT_OP_ERR; 2441 *res_size = 2; 2442 abort_read(); 2443 } 2444 } else { 2445 clear_data_ready(); 2446 2447 /* If data block, then get 2340 bytes offset by 12. */ 2448 if (sony_raw_data_mode) { 2449 insb(sony_cd_read_reg, buffer + CD_XA_HEAD, 2450 CD_FRAMESIZE_RAW1); 2451 } else { 2452 /* Audio gets the whole 2352 bytes. */ 2453 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW); 2454 } 2455 2456 /* If I haven't already gotten the result, get it now. */ 2457 if (!result_read) { 2458 /* Wait for the drive to tell us we have something */ 2459 retry_count = jiffies + SONY_JIFFIES_TIMEOUT; 2460 while (time_before(jiffies, retry_count) 2461 && !(is_result_ready())) { 2462 while (handle_sony_cd_attention()); 2463 2464 sony_sleep(); 2465 } 2466 2467 if (!is_result_ready()) { 2468#if DEBUG 2469 printk("CDU31A timeout out %d\n", 2470 __LINE__); 2471#endif 2472 res_reg[0] = 0x20; 2473 res_reg[1] = SONY_TIMEOUT_OP_ERR; 2474 *res_size = 2; 2475 abort_read(); 2476 return; 2477 } else { 2478 get_result(res_reg, res_size); 2479 } 2480 } 2481 2482 if ((res_reg[0] & 0xf0) == 0x50) { 2483 if ((res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT) 2484 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT) 2485 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT) 2486 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT)) { 2487 /* Ok, nothing to do. */ 2488 } else { 2489 printk("CDU31A: Data block error: 0x%x\n", 2490 res_reg[0]); 2491 res_reg[0] = 0x20; 2492 res_reg[1] = SONY_BAD_DATA_ERR; 2493 *res_size = 2; 2494 } 2495 } else if ((res_reg[0] & 0xf0) != 0x20) { 2496 /* The drive gave me bad status, I don't know what to do. 2497 Reset the driver and return an error. */ 2498 printk("CDU31A: Invalid block status: 0x%x\n", 2499 res_reg[0]); 2500 restart_on_error(); 2501 res_reg[0] = 0x20; 2502 res_reg[1] = SONY_BAD_DATA_ERR; 2503 *res_size = 2; 2504 } 2505 } 2506} 2507 2508/* Perform a raw data read. This will automatically detect the 2509 track type and read the proper data (audio or data). */ 2510static int read_audio(struct cdrom_read_audio *ra) 2511{ 2512 int retval; 2513 unsigned char params[2]; 2514 unsigned char res_reg[12]; 2515 unsigned int res_size; 2516 unsigned int cframe; 2517 unsigned long flags; 2518 2519 /* 2520 * Make sure no one else is using the driver; wait for them 2521 * to finish if it is so. 2522 */ 2523 save_flags(flags); 2524 cli(); 2525 while (sony_inuse) { 2526 interruptible_sleep_on(&sony_wait); 2527 if (signal_pending(current)) { 2528 restore_flags(flags); 2529 return -EAGAIN; 2530 } 2531 } 2532 sony_inuse = 1; 2533 has_cd_task = current; 2534 restore_flags(flags); 2535 2536 if (!sony_spun_up) { 2537 scd_spinup(); 2538 } 2539 2540 /* Set the drive to do raw operations. */ 2541 params[0] = SONY_SD_DECODE_PARAM; 2542 params[1] = 0x06 | sony_raw_data_mode; 2543 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, 2544 params, 2, res_reg, &res_size); 2545 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) { 2546 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", 2547 res_reg[1]); 2548 return -EIO; 2549 } 2550 2551 /* From here down, we have to goto exit_read_audio instead of returning 2552 because the drive parameters have to be set back to data before 2553 return. */ 2554 2555 retval = 0; 2556 /* start_request clears out any readahead data, so it should be safe. */ 2557 if (start_request(ra->addr.lba, ra->nframes, 1)) { 2558 retval = -EIO; 2559 goto exit_read_audio; 2560 } 2561 2562 /* For every requested frame. */ 2563 cframe = 0; 2564 while (cframe < ra->nframes) { 2565 read_audio_data(readahead_buffer, res_reg, &res_size); 2566 if ((res_reg[0] & 0xf0) == 0x20) { 2567 if (res_reg[1] == SONY_BAD_DATA_ERR) { 2568 printk 2569 ("CDU31A: Data error on audio sector %d\n", 2570 ra->addr.lba + cframe); 2571 } else if (res_reg[1] == SONY_ILL_TRACK_R_ERR) { 2572 /* Illegal track type, change track types and start over. */ 2573 sony_raw_data_mode = 2574 (sony_raw_data_mode) ? 0 : 1; 2575 2576 /* Set the drive mode. */ 2577 params[0] = SONY_SD_DECODE_PARAM; 2578 params[1] = 0x06 | sony_raw_data_mode; 2579 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, 2580 params, 2581 2, res_reg, &res_size); 2582 if ((res_size < 2) 2583 || ((res_reg[0] & 0xf0) == 0x20)) { 2584 printk 2585 ("CDU31A: Unable to set decode params: 0x%2.2x\n", 2586 res_reg[1]); 2587 retval = -EIO; 2588 goto exit_read_audio; 2589 } 2590 2591 /* Restart the request on the current frame. */ 2592 if (start_request 2593 (ra->addr.lba + cframe, 2594 ra->nframes - cframe, 1)) { 2595 retval = -EIO; 2596 goto exit_read_audio; 2597 } 2598 2599 /* Don't go back to the top because don't want to get into 2600 and infinite loop. A lot of code gets duplicated, but 2601 that's no big deal, I don't guess. */ 2602 read_audio_data(readahead_buffer, res_reg, 2603 &res_size); 2604 if ((res_reg[0] & 0xf0) == 0x20) { 2605 if (res_reg[1] == 2606 SONY_BAD_DATA_ERR) { 2607 printk 2608 ("CDU31A: Data error on audio sector %d\n", 2609 ra->addr.lba + 2610 cframe); 2611 } else { 2612 printk 2613 ("CDU31A: Error reading audio data on sector %d: %s\n", 2614 ra->addr.lba + cframe, 2615 translate_error 2616 (res_reg[1])); 2617 retval = -EIO; 2618 goto exit_read_audio; 2619 } 2620 } else { 2621 copy_to_user((char *) (ra->buf + 2622 (CD_FRAMESIZE_RAW 2623 * cframe)), 2624 (char *) 2625 readahead_buffer, 2626 CD_FRAMESIZE_RAW); 2627 } 2628 } else { 2629 printk 2630 ("CDU31A: Error reading audio data on sector %d: %s\n", 2631 ra->addr.lba + cframe, 2632 translate_error(res_reg[1])); 2633 retval = -EIO; 2634 goto exit_read_audio; 2635 } 2636 } else { 2637 copy_to_user((char *) (ra->buf + 2638 (CD_FRAMESIZE_RAW * 2639 cframe)), 2640 (char *) readahead_buffer, 2641 CD_FRAMESIZE_RAW); 2642 } 2643 2644 cframe++; 2645 } 2646 2647 get_result(res_reg, &res_size); 2648 if ((res_reg[0] & 0xf0) == 0x20) { 2649 printk("CDU31A: Error return from audio read: %s\n", 2650 translate_error(res_reg[1])); 2651 retval = -EIO; 2652 goto exit_read_audio; 2653 } 2654 2655 exit_read_audio: 2656 2657 /* Set the drive mode back to the proper one for the disk. */ 2658 params[0] = SONY_SD_DECODE_PARAM; 2659 if (!sony_xa_mode) { 2660 params[1] = 0x0f; 2661 } else { 2662 params[1] = 0x07; 2663 } 2664 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, 2665 params, 2, res_reg, &res_size); 2666 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) { 2667 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", 2668 res_reg[1]); 2669 return -EIO; 2670 } 2671 2672 has_cd_task = NULL; 2673 sony_inuse = 0; 2674 wake_up_interruptible(&sony_wait); 2675 2676 return (retval); 2677} 2678 2679static int 2680do_sony_cd_cmd_chk(const char *name, 2681 unsigned char cmd, 2682 unsigned char *params, 2683 unsigned int num_params, 2684 unsigned char *result_buffer, unsigned int *result_size) 2685{ 2686 do_sony_cd_cmd(cmd, params, num_params, result_buffer, 2687 result_size); 2688 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20)) { 2689 printk("Sony CDROM error %s (CDROM%s)\n", 2690 translate_error(result_buffer[1]), name); 2691 return -EIO; 2692 } 2693 return 0; 2694} 2695 2696/* 2697 * Uniform cdrom interface function 2698 * open the tray 2699 */ 2700static int scd_tray_move(struct cdrom_device_info *cdi, int position) 2701{ 2702 if (position == 1 /* open tray */ ) { 2703 unsigned char res_reg[12]; 2704 unsigned int res_size; 2705 2706 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, 2707 &res_size); 2708 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, 2709 &res_size); 2710 2711 sony_audio_status = CDROM_AUDIO_INVALID; 2712 return do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD, NULL, 0, 2713 res_reg, &res_size); 2714 } else { 2715 if (0 == scd_spinup()) 2716 sony_spun_up = 1; 2717 return 0; 2718 } 2719} 2720 2721/* 2722 * The big ugly ioctl handler. 2723 */ 2724static int scd_audio_ioctl(struct cdrom_device_info *cdi, 2725 unsigned int cmd, void *arg) 2726{ 2727 unsigned char res_reg[12]; 2728 unsigned int res_size; 2729 unsigned char params[7]; 2730 int i; 2731 2732 2733 switch (cmd) { 2734 case CDROMSTART: /* Spin up the drive */ 2735 return do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD, NULL, 2736 0, res_reg, &res_size); 2737 break; 2738 2739 case CDROMSTOP: /* Spin down the drive */ 2740 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, 2741 &res_size); 2742 2743 /* 2744 * Spin the drive down, ignoring the error if the disk was 2745 * already not spinning. 2746 */ 2747 sony_audio_status = CDROM_AUDIO_NO_STATUS; 2748 return do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD, NULL, 2749 0, res_reg, &res_size); 2750 2751 case CDROMPAUSE: /* Pause the drive */ 2752 if (do_sony_cd_cmd_chk 2753 ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, 2754 &res_size)) 2755 return -EIO; 2756 /* Get the current position and save it for resuming */ 2757 if (read_subcode() < 0) { 2758 return -EIO; 2759 } 2760 cur_pos_msf[0] = last_sony_subcode.abs_msf[0]; 2761 cur_pos_msf[1] = last_sony_subcode.abs_msf[1]; 2762 cur_pos_msf[2] = last_sony_subcode.abs_msf[2]; 2763 sony_audio_status = CDROM_AUDIO_PAUSED; 2764 return 0; 2765 break; 2766 2767 case CDROMRESUME: /* Start the drive after being paused */ 2768 if (sony_audio_status != CDROM_AUDIO_PAUSED) { 2769 return -EINVAL; 2770 } 2771 2772 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, 2773 &res_size); 2774 2775 /* Start the drive at the saved position. */ 2776 params[1] = int_to_bcd(cur_pos_msf[0]); 2777 params[2] = int_to_bcd(cur_pos_msf[1]); 2778 params[3] = int_to_bcd(cur_pos_msf[2]); 2779 params[4] = int_to_bcd(final_pos_msf[0]); 2780 params[5] = int_to_bcd(final_pos_msf[1]); 2781 params[6] = int_to_bcd(final_pos_msf[2]); 2782 params[0] = 0x03; 2783 if (do_sony_cd_cmd_chk 2784 ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, 2785 &res_size) < 0) 2786 return -EIO; 2787 sony_audio_status = CDROM_AUDIO_PLAY; 2788 return 0; 2789 2790 case CDROMPLAYMSF: /* Play starting at the given MSF address. */ 2791 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, 2792 &res_size); 2793 2794 /* The parameters are given in int, must be converted */ 2795 for (i = 1; i < 7; i++) { 2796 params[i] = 2797 int_to_bcd(((unsigned char *) arg)[i - 1]); 2798 } 2799 params[0] = 0x03; 2800 if (do_sony_cd_cmd_chk 2801 ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7, 2802 res_reg, &res_size) < 0) 2803 return -EIO; 2804 2805 /* Save the final position for pauses and resumes */ 2806 final_pos_msf[0] = bcd_to_int(params[4]); 2807 final_pos_msf[1] = bcd_to_int(params[5]); 2808 final_pos_msf[2] = bcd_to_int(params[6]); 2809 sony_audio_status = CDROM_AUDIO_PLAY; 2810 return 0; 2811 2812 case CDROMREADTOCHDR: /* Read the table of contents header */ 2813 { 2814 struct cdrom_tochdr *hdr; 2815 2816 sony_get_toc(); 2817 if (!sony_toc_read) { 2818 return -EIO; 2819 } 2820 2821 hdr = (struct cdrom_tochdr *) arg; 2822 hdr->cdth_trk0 = sony_toc.first_track_num; 2823 hdr->cdth_trk1 = sony_toc.last_track_num; 2824 } 2825 return 0; 2826 2827 case CDROMREADTOCENTRY: /* Read a given table of contents entry */ 2828 { 2829 struct cdrom_tocentry *entry; 2830 int track_idx; 2831 unsigned char *msf_val = NULL; 2832 2833 sony_get_toc(); 2834 if (!sony_toc_read) { 2835 return -EIO; 2836 } 2837 2838 entry = (struct cdrom_tocentry *) arg; 2839 2840 track_idx = find_track(entry->cdte_track); 2841 if (track_idx < 0) { 2842 return -EINVAL; 2843 } 2844 2845 entry->cdte_adr = 2846 sony_toc.tracks[track_idx].address; 2847 entry->cdte_ctrl = 2848 sony_toc.tracks[track_idx].control; 2849 msf_val = 2850 sony_toc.tracks[track_idx].track_start_msf; 2851 2852 /* Logical buffer address or MSF format requested? */ 2853 if (entry->cdte_format == CDROM_LBA) { 2854 entry->cdte_addr.lba = msf_to_log(msf_val); 2855 } else if (entry->cdte_format == CDROM_MSF) { 2856 entry->cdte_addr.msf.minute = *msf_val; 2857 entry->cdte_addr.msf.second = 2858 *(msf_val + 1); 2859 entry->cdte_addr.msf.frame = 2860 *(msf_val + 2); 2861 } 2862 } 2863 return 0; 2864 break; 2865 2866 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */ 2867 { 2868 struct cdrom_ti *ti = (struct cdrom_ti *) arg; 2869 int track_idx; 2870 2871 sony_get_toc(); 2872 if (!sony_toc_read) { 2873 return -EIO; 2874 } 2875 2876 if ((ti->cdti_trk0 < sony_toc.first_track_num) 2877 || (ti->cdti_trk0 > sony_toc.last_track_num) 2878 || (ti->cdti_trk1 < ti->cdti_trk0)) { 2879 return -EINVAL; 2880 } 2881 2882 track_idx = find_track(ti->cdti_trk0); 2883 if (track_idx < 0) { 2884 return -EINVAL; 2885 } 2886 params[1] = 2887 int_to_bcd(sony_toc.tracks[track_idx]. 2888 track_start_msf[0]); 2889 params[2] = 2890 int_to_bcd(sony_toc.tracks[track_idx]. 2891 track_start_msf[1]); 2892 params[3] = 2893 int_to_bcd(sony_toc.tracks[track_idx]. 2894 track_start_msf[2]); 2895 2896 /* 2897 * If we want to stop after the last track, use the lead-out 2898 * MSF to do that. 2899 */ 2900 if (ti->cdti_trk1 >= sony_toc.last_track_num) { 2901 track_idx = find_track(CDROM_LEADOUT); 2902 } else { 2903 track_idx = find_track(ti->cdti_trk1 + 1); 2904 } 2905 if (track_idx < 0) { 2906 return -EINVAL; 2907 } 2908 params[4] = 2909 int_to_bcd(sony_toc.tracks[track_idx]. 2910 track_start_msf[0]); 2911 params[5] = 2912 int_to_bcd(sony_toc.tracks[track_idx]. 2913 track_start_msf[1]); 2914 params[6] = 2915 int_to_bcd(sony_toc.tracks[track_idx]. 2916 track_start_msf[2]); 2917 params[0] = 0x03; 2918 2919 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, 2920 &res_size); 2921 2922 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, 2923 res_reg, &res_size); 2924 2925 if ((res_size < 2) 2926 || ((res_reg[0] & 0xf0) == 0x20)) { 2927 printk("Params: %x %x %x %x %x %x %x\n", 2928 params[0], params[1], params[2], 2929 params[3], params[4], params[5], 2930 params[6]); 2931 printk 2932 ("Sony CDROM error %s (CDROMPLAYTRKIND)\n", 2933 translate_error(res_reg[1])); 2934 return -EIO; 2935 } 2936 2937 /* Save the final position for pauses and resumes */ 2938 final_pos_msf[0] = bcd_to_int(params[4]); 2939 final_pos_msf[1] = bcd_to_int(params[5]); 2940 final_pos_msf[2] = bcd_to_int(params[6]); 2941 sony_audio_status = CDROM_AUDIO_PLAY; 2942 return 0; 2943 } 2944 2945 case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */ 2946 { 2947 struct cdrom_volctrl *volctrl = 2948 (struct cdrom_volctrl *) arg; 2949 2950 params[0] = SONY_SD_AUDIO_VOLUME; 2951 params[1] = volctrl->channel0; 2952 params[2] = volctrl->channel1; 2953 return do_sony_cd_cmd_chk("VOLCTRL", 2954 SONY_SET_DRIVE_PARAM_CMD, 2955 params, 3, res_reg, 2956 &res_size); 2957 } 2958 case CDROMSUBCHNL: /* Get subchannel info */ 2959 return sony_get_subchnl_info((struct cdrom_subchnl *) arg); 2960 2961 default: 2962 return -EINVAL; 2963 } 2964} 2965 2966static int scd_dev_ioctl(struct cdrom_device_info *cdi, 2967 unsigned int cmd, unsigned long arg) 2968{ 2969 int i; 2970 2971 switch (cmd) { 2972 case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte 2973 raw data tracks. */ 2974 { 2975 struct cdrom_read_audio ra; 2976 2977 2978 sony_get_toc(); 2979 if (!sony_toc_read) { 2980 return -EIO; 2981 } 2982 2983 if (copy_from_user(&ra, (char *) arg, sizeof(ra))) 2984 return -EFAULT; 2985 2986 if (ra.nframes == 0) { 2987 return 0; 2988 } 2989 2990 i = verify_area(VERIFY_WRITE, ra.buf, 2991 CD_FRAMESIZE_RAW * ra.nframes); 2992 if (i < 0) 2993 return i; 2994 2995 if (ra.addr_format == CDROM_LBA) { 2996 if ((ra.addr.lba >= 2997 sony_toc.lead_out_start_lba) 2998 || (ra.addr.lba + ra.nframes >= 2999 sony_toc.lead_out_start_lba)) { 3000 return -EINVAL; 3001 } 3002 } else if (ra.addr_format == CDROM_MSF) { 3003 if ((ra.addr.msf.minute >= 75) 3004 || (ra.addr.msf.second >= 60) 3005 || (ra.addr.msf.frame >= 75)) { 3006 return -EINVAL; 3007 } 3008 3009 ra.addr.lba = ((ra.addr.msf.minute * 4500) 3010 + (ra.addr.msf.second * 75) 3011 + ra.addr.msf.frame); 3012 if ((ra.addr.lba >= 3013 sony_toc.lead_out_start_lba) 3014 || (ra.addr.lba + ra.nframes >= 3015 sony_toc.lead_out_start_lba)) { 3016 return -EINVAL; 3017 } 3018 3019 /* I know, this can go negative on an unsigned. However, 3020 the first thing done to the data is to add this value, 3021 so this should compensate and allow direct msf access. */ 3022 ra.addr.lba -= LOG_START_OFFSET; 3023 } else { 3024 return -EINVAL; 3025 } 3026 3027 return (read_audio(&ra)); 3028 } 3029 return 0; 3030 break; 3031 3032 default: 3033 return -EINVAL; 3034 } 3035} 3036 3037static int scd_spinup(void) 3038{ 3039 unsigned char res_reg[12]; 3040 unsigned int res_size; 3041 int num_spin_ups; 3042 3043 num_spin_ups = 0; 3044 3045 respinup_on_open: 3046 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size); 3047 3048 /* The drive sometimes returns error 0. I don't know why, but ignore 3049 it. It seems to mean the drive has already done the operation. */ 3050 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) { 3051 printk("Sony CDROM %s error (scd_open, spin up)\n", 3052 translate_error(res_reg[1])); 3053 return 1; 3054 } 3055 3056 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size); 3057 3058 /* The drive sometimes returns error 0. I don't know why, but ignore 3059 it. It seems to mean the drive has already done the operation. */ 3060 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) { 3061 /* If the drive is already playing, it's ok. */ 3062 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) 3063 || (res_reg[1] == 0)) { 3064 return 0; 3065 } 3066 3067 /* If the drive says it is not spun up (even though we just did it!) 3068 then retry the operation at least a few times. */ 3069 if ((res_reg[1] == SONY_NOT_SPIN_ERR) 3070 && (num_spin_ups < MAX_CDU31A_RETRIES)) { 3071 num_spin_ups++; 3072 goto respinup_on_open; 3073 } 3074 3075 printk("Sony CDROM error %s (scd_open, read toc)\n", 3076 translate_error(res_reg[1])); 3077 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, 3078 &res_size); 3079 return 1; 3080 } 3081 return 0; 3082} 3083 3084/* 3085 * Open the drive for operations. Spin the drive up and read the table of 3086 * contents if these have not already been done. 3087 */ 3088static int scd_open(struct cdrom_device_info *cdi, int openmode) 3089{ 3090 unsigned char res_reg[12]; 3091 unsigned int res_size; 3092 unsigned char params[2]; 3093 3094 if (sony_usage == 0) { 3095 if (scd_spinup() != 0) 3096 return -EIO; 3097 sony_get_toc(); 3098 if (!sony_toc_read) { 3099 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, 3100 res_reg, &res_size); 3101 return -EIO; 3102 } 3103 3104 /* For XA on the CDU31A only, we have to do special reads. 3105 The CDU33A handles XA automagically. */ 3106 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */ 3107 if ((sony_toc.disk_type != 0x00) 3108 && (!is_double_speed)) { 3109 params[0] = SONY_SD_DECODE_PARAM; 3110 params[1] = 0x07; 3111 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, 3112 params, 2, res_reg, &res_size); 3113 if ((res_size < 2) 3114 || ((res_reg[0] & 0xf0) == 0x20)) { 3115 printk 3116 ("CDU31A: Unable to set XA params: 0x%2.2x\n", 3117 res_reg[1]); 3118 } 3119 sony_xa_mode = 1; 3120 } 3121 /* A non-XA disk. Set the parms back if necessary. */ 3122 else if (sony_xa_mode) { 3123 params[0] = SONY_SD_DECODE_PARAM; 3124 params[1] = 0x0f; 3125 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, 3126 params, 2, res_reg, &res_size); 3127 if ((res_size < 2) 3128 || ((res_reg[0] & 0xf0) == 0x20)) { 3129 printk 3130 ("CDU31A: Unable to reset XA params: 0x%2.2x\n", 3131 res_reg[1]); 3132 } 3133 sony_xa_mode = 0; 3134 } 3135 3136 sony_spun_up = 1; 3137 } 3138 3139 sony_usage++; 3140 3141 return 0; 3142} 3143 3144 3145/* 3146 * Close the drive. Spin it down if no task is using it. The spin 3147 * down will fail if playing audio, so audio play is OK. 3148 */ 3149static void scd_release(struct cdrom_device_info *cdi) 3150{ 3151 if (sony_usage == 1) { 3152 unsigned char res_reg[12]; 3153 unsigned int res_size; 3154 3155 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, 3156 &res_size); 3157 3158 sony_spun_up = 0; 3159 } 3160 sony_usage--; 3161} 3162 3163struct block_device_operations scd_bdops = 3164{ 3165 owner: THIS_MODULE, 3166 open: cdrom_open, 3167 release: cdrom_release, 3168 ioctl: cdrom_ioctl, 3169 check_media_change: cdrom_media_changed, 3170}; 3171 3172static struct cdrom_device_ops scd_dops = { 3173 open:scd_open, 3174 release:scd_release, 3175 drive_status:scd_drive_status, 3176 media_changed:scd_media_changed, 3177 tray_move:scd_tray_move, 3178 lock_door:scd_lock_door, 3179 select_speed:scd_select_speed, 3180 get_last_session:scd_get_last_session, 3181 get_mcn:scd_get_mcn, 3182 reset:scd_reset, 3183 audio_ioctl:scd_audio_ioctl, 3184 dev_ioctl:scd_dev_ioctl, 3185 capability:CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK | 3186 CDC_SELECT_SPEED | CDC_MULTI_SESSION | 3187 CDC_MULTI_SESSION | CDC_MCN | 3188 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | 3189 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS, 3190 n_minors:1, 3191}; 3192 3193static struct cdrom_device_info scd_info = { 3194 ops:&scd_dops, 3195 speed:2, 3196 capacity:1, 3197 name:"cdu31a" 3198}; 3199 3200/* The different types of disc loading mechanisms supported */ 3201static char *load_mech[] __initdata = 3202 { "caddy", "tray", "pop-up", "unknown" }; 3203 3204static void __init 3205get_drive_configuration(unsigned short base_io, 3206 unsigned char res_reg[], unsigned int *res_size) 3207{ 3208 int retry_count; 3209 3210 3211 /* Set the base address */ 3212 cdu31a_port = base_io; 3213 3214 /* Set up all the register locations */ 3215 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET; 3216 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET; 3217 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET; 3218 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET; 3219 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET; 3220 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET; 3221 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET; 3222 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET; 3223 3224 /* 3225 * Check to see if anything exists at the status register location. 3226 * I don't know if this is a good way to check, but it seems to work 3227 * ok for me. 3228 */ 3229 if (read_status_register() != 0xff) { 3230 /* 3231 * Reset the drive and wait for attention from it (to say it's reset). 3232 * If you don't wait, the next operation will probably fail. 3233 */ 3234 reset_drive(); 3235 retry_count = jiffies + SONY_RESET_TIMEOUT; 3236 while (time_before(jiffies, retry_count) 3237 && (!is_attention())) { 3238 sony_sleep(); 3239 } 3240 3241 3242 /* 3243 * Get the drive configuration. 3244 */ 3245 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD, 3246 NULL, 3247 0, (unsigned char *) res_reg, res_size); 3248 return; 3249 } 3250 3251 /* Return an error */ 3252 res_reg[0] = 0x20; 3253} 3254 3255#ifndef MODULE 3256/* 3257 * Set up base I/O and interrupts, called from main.c. 3258 3259 */ 3260 3261static int __init cdu31a_setup(char *strings) 3262{ 3263 int ints[4]; 3264 3265 (void) get_options(strings, ARRAY_SIZE(ints), ints); 3266 3267 if (ints[0] > 0) { 3268 cdu31a_port = ints[1]; 3269 } 3270 if (ints[0] > 1) { 3271 cdu31a_irq = ints[2]; 3272 } 3273 if ((strings != NULL) && (*strings != '\0')) { 3274 if (strcmp(strings, "PAS") == 0) { 3275 sony_pas_init = 1; 3276 } else { 3277 printk("CDU31A: Unknown interface type: %s\n", 3278 strings); 3279 } 3280 } 3281 3282 return 1; 3283} 3284 3285__setup("cdu31a=", cdu31a_setup); 3286 3287#endif 3288 3289static int cdu31a_block_size; 3290 3291/* 3292 * Initialize the driver. 3293 */ 3294int __init cdu31a_init(void) 3295{ 3296 struct s_sony_drive_config drive_config; 3297 unsigned int res_size; 3298 char msg[255]; 3299 char buf[40]; 3300 int i; 3301 int drive_found; 3302 int tmp_irq; 3303 3304 3305 /* 3306 * According to Alex Freed (freed@europa.orion.adobe.com), this is 3307 * required for the Fusion CD-16 package. If the sound driver is 3308 * loaded, it should work fine, but just in case... 3309 * 3310 * The following turn on the CD-ROM interface for a Fusion CD-16. 3311 */ 3312 if (sony_pas_init) { 3313 outb(0xbc, 0x9a01); 3314 outb(0xe2, 0x9a01); 3315 } 3316 3317 drive_found = 0; 3318 3319 /* Setting the base I/O address to 0xffff will disable it. */ 3320 if (cdu31a_port == 0xffff) { 3321 } else if (cdu31a_port != 0) { 3322 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */ 3323 cdu31a_irq = 0; 3324 3325 get_drive_configuration(cdu31a_port, 3326 drive_config.exec_status, 3327 &res_size); 3328 if ((res_size > 2) 3329 && ((drive_config.exec_status[0] & 0xf0) == 0x00)) { 3330 drive_found = 1; 3331 } 3332 3333 cdu31a_irq = tmp_irq; 3334 } else { 3335 cdu31a_irq = 0; 3336 i = 0; 3337 while ((cdu31a_addresses[i].base != 0) 3338 && (!drive_found)) { 3339 if (check_region(cdu31a_addresses[i].base, 4)) { 3340 i++; 3341 continue; 3342 } 3343 get_drive_configuration(cdu31a_addresses[i].base, 3344 drive_config.exec_status, 3345 &res_size); 3346 if ((res_size > 2) 3347 && ((drive_config.exec_status[0] & 0xf0) == 3348 0x00)) { 3349 drive_found = 1; 3350 cdu31a_irq = cdu31a_addresses[i].int_num; 3351 } else { 3352 i++; 3353 } 3354 } 3355 } 3356 3357 if (drive_found) { 3358 int deficiency = 0; 3359 3360 if (!request_region(cdu31a_port, 4, "cdu31a")) 3361 goto errout3; 3362 3363 if (devfs_register_blkdev(MAJOR_NR, "cdu31a", &scd_bdops)) { 3364 printk("Unable to get major %d for CDU-31a\n", 3365 MAJOR_NR); 3366 goto errout2; 3367 } 3368 3369 if (SONY_HWC_DOUBLE_SPEED(drive_config)) { 3370 is_double_speed = 1; 3371 } 3372 3373 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */ 3374 cdu31a_irq = 0; 3375 3376 set_drive_params(sony_speed); 3377 3378 cdu31a_irq = tmp_irq; 3379 3380 if (cdu31a_irq > 0) { 3381 if (request_irq 3382 (cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT, 3383 "cdu31a", NULL)) { 3384 printk 3385 ("Unable to grab IRQ%d for the CDU31A driver\n", 3386 cdu31a_irq); 3387 cdu31a_irq = 0; 3388 } 3389 } 3390 3391 sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n", 3392 drive_config.vendor_id, 3393 drive_config.product_id, 3394 drive_config.product_rev_level); 3395 sprintf(buf, " Capabilities: %s", 3396 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]); 3397 strcat(msg, buf); 3398 if (SONY_HWC_AUDIO_PLAYBACK(drive_config)) { 3399 strcat(msg, ", audio"); 3400 } else 3401 deficiency |= CDC_PLAY_AUDIO; 3402 if (SONY_HWC_EJECT(drive_config)) { 3403 strcat(msg, ", eject"); 3404 } else 3405 deficiency |= CDC_OPEN_TRAY; 3406 if (SONY_HWC_LED_SUPPORT(drive_config)) { 3407 strcat(msg, ", LED"); 3408 } 3409 if (SONY_HWC_ELECTRIC_VOLUME(drive_config)) { 3410 strcat(msg, ", elec. Vol"); 3411 } 3412 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config)) { 3413 strcat(msg, ", sep. Vol"); 3414 } 3415 if (is_double_speed) { 3416 strcat(msg, ", double speed"); 3417 } else 3418 deficiency |= CDC_SELECT_SPEED; 3419 if (cdu31a_irq > 0) { 3420 sprintf(buf, ", irq %d", cdu31a_irq); 3421 strcat(msg, buf); 3422 } 3423 strcat(msg, "\n"); 3424 3425 is_a_cdu31a = 3426 strcmp("CD-ROM CDU31A", drive_config.product_id) == 0; 3427 3428 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), 3429 DEVICE_REQUEST); 3430 read_ahead[MAJOR_NR] = CDU31A_READAHEAD; 3431 cdu31a_block_size = 1024; /* 1kB default block size */ 3432 /* use 'mount -o block=2048' */ 3433 blksize_size[MAJOR_NR] = &cdu31a_block_size; 3434 3435 init_timer(&cdu31a_abort_timer); 3436 cdu31a_abort_timer.function = handle_abort_timeout; 3437 3438 scd_info.dev = MKDEV(MAJOR_NR, 0); 3439 scd_info.mask = deficiency; 3440 strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name)); 3441 3442 if (register_cdrom(&scd_info)) { 3443 goto errout0; 3444 } 3445 devfs_plain_cdrom(&scd_info, &scd_bdops); 3446 } 3447 3448 3449 disk_changed = 1; 3450 3451 if (drive_found) { 3452 return (0); 3453 } else { 3454 goto errout3; 3455 } 3456 errout0: 3457 printk("Unable to register CDU-31a with Uniform cdrom driver\n"); 3458 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR)); 3459 if (devfs_unregister_blkdev(MAJOR_NR, "cdu31a")) { 3460 printk("Can't unregister block device for cdu31a\n"); 3461 } 3462 errout2: 3463 release_region(cdu31a_port, 4); 3464 errout3: 3465 return -EIO; 3466} 3467 3468 3469void __exit cdu31a_exit(void) 3470{ 3471 if (unregister_cdrom(&scd_info)) { 3472 printk 3473 ("Can't unregister cdu31a from Uniform cdrom driver\n"); 3474 return; 3475 } 3476 if ((devfs_unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) { 3477 printk("Can't unregister cdu31a\n"); 3478 return; 3479 } 3480 3481 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR)); 3482 3483 if (cdu31a_irq > 0) 3484 free_irq(cdu31a_irq, NULL); 3485 3486 release_region(cdu31a_port, 4); 3487 printk(KERN_INFO "cdu31a module released.\n"); 3488} 3489 3490#ifdef MODULE 3491module_init(cdu31a_init); 3492#endif 3493module_exit(cdu31a_exit); 3494 3495MODULE_LICENSE("GPL"); 3496EXPORT_NO_SYMBOLS; 3497