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