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