Import 2.1.118
[davej-history.git] / drivers / cdrom / sonycd535.c
blobaaad43c966ed01d02f5604ebb2c2a0bac0a70849
1 /*
2 * Sony CDU-535 interface device driver
4 * This is a modified version of the CDU-31A device driver (see below).
5 * Changes were made using documentation for the CDU-531 (which Sony
6 * assures me is very similar to the 535) and partial disassembly of the
7 * DOS driver. I used Minyard's driver and replaced the CDU-31A
8 * commands with the CDU-531 commands. This was complicated by a different
9 * interface protocol with the drive. The driver is still polled.
11 * Data transfer rate is about 110 Kb/sec, theoretical maximum is 150 Kb/sec.
12 * I tried polling without the sony_sleep during the data transfers but
13 * it did not speed things up any.
15 * 1993-05-23 (rgj) changed the major number to 21 to get rid of conflict
16 * with CDU-31A driver. This is the also the number from the Linux
17 * Device Driver Registry for the Sony Drive. Hope nobody else is using it.
19 * 1993-08-29 (rgj) remove the configuring of the interface board address
20 * from the top level configuration, you have to modify it in this file.
22 * 1995-01-26 Made module-capable (Joel Katz <Stimpson@Panix.COM>)
24 * 1995-05-20
25 * Modified to support CDU-510/515 series
26 * (Claudio Porfiri<C.Porfiri@nisms.tei.ericsson.se>)
27 * Fixed to report verify_area() failures
28 * (Heiko Eissfeldt <heiko@colossus.escape.de>)
30 * 1995-06-01
31 * More changes to support CDU-510/515 series
32 * (Claudio Porfiri<C.Porfiri@nisms.tei.ericsson.se>)
34 * Things to do:
35 * - handle errors and status better, put everything into a single word
36 * - use interrupts (code mostly there, but a big hole still missing)
37 * - handle multi-session CDs?
38 * - use DMA?
40 * Known Bugs:
41 * -
43 * Ken Pizzini (ken@halcyon.com)
45 * Original by:
46 * Ron Jeppesen (ronj.an@site007.saic.com)
49 *------------------------------------------------------------------------
50 * Sony CDROM interface device driver.
52 * Corey Minyard (minyard@wf-rch.cirr.com) (CDU-535 complaints to Ken above)
54 * Colossians 3:17
56 * The Sony interface device driver handles Sony interface CDROM
57 * drives and provides a complete block-level interface as well as an
58 * ioctl() interface compatible with the Sun (as specified in
59 * include/linux/cdrom.h). With this interface, CDROMs can be
60 * accessed and standard audio CDs can be played back normally.
62 * This interface is (unfortunately) a polled interface. This is
63 * because most Sony interfaces are set up with DMA and interrupts
64 * disables. Some (like mine) do not even have the capability to
65 * handle interrupts or DMA. For this reason you will see a lot of
66 * the following:
68 * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
69 * while ((retry_count > jiffies) && (! <some condition to wait for))
70 * {
71 * while (handle_sony_cd_attention())
72 * ;
74 * sony_sleep();
75 * }
76 * if (the condition not met)
77 * {
78 * return an error;
79 * }
81 * This ugly hack waits for something to happen, sleeping a little
82 * between every try. it also handles attentions, which are
83 * asynchronous events from the drive informing the driver that a disk
84 * has been inserted, removed, etc.
86 * One thing about these drives: They talk in MSF (Minute Second Frame) format.
87 * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
88 * disk. The funny thing is that these are sent to the drive in BCD, but the
89 * interface wants to see them in decimal. A lot of conversion goes on.
91 * Copyright (C) 1993 Corey Minyard
93 * This program is free software; you can redistribute it and/or modify
94 * it under the terms of the GNU General Public License as published by
95 * the Free Software Foundation; either version 2 of the License, or
96 * (at your option) any later version.
98 * This program is distributed in the hope that it will be useful,
99 * but WITHOUT ANY WARRANTY; without even the implied warranty of
100 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
101 * GNU General Public License for more details.
103 * You should have received a copy of the GNU General Public License
104 * along with this program; if not, write to the Free Software
105 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
110 # include <linux/module.h>
112 #include <linux/errno.h>
113 #include <linux/signal.h>
114 #include <linux/sched.h>
115 #include <linux/timer.h>
116 #include <linux/fs.h>
117 #include <linux/kernel.h>
118 #include <linux/ioport.h>
119 #include <linux/hdreg.h>
120 #include <linux/genhd.h>
121 #include <linux/mm.h>
122 #include <linux/malloc.h>
123 #include <linux/init.h>
125 #define REALLY_SLOW_IO
126 #include <asm/system.h>
127 #include <asm/io.h>
128 #include <asm/uaccess.h>
130 #include <linux/cdrom.h>
132 #define MAJOR_NR CDU535_CDROM_MAJOR
133 # include <linux/blk.h>
134 #define sony535_cd_base_io sonycd535 /* for compatible parameter passing with "insmod" */
135 #include "sonycd535.h"
138 * this is the base address of the interface card for the Sony CDU-535
139 * CDROM drive. If your jumpers are set for an address other than
140 * this one (the default), change the following line to the
141 * proper address.
143 #ifndef CDU535_ADDRESS
144 # define CDU535_ADDRESS 0x340
145 #endif
146 #ifndef CDU535_INTERRUPT
147 # define CDU535_INTERRUPT 0
148 #endif
149 #ifndef CDU535_HANDLE
150 # define CDU535_HANDLE "cdu535"
151 #endif
152 #ifndef CDU535_MESSAGE_NAME
153 # define CDU535_MESSAGE_NAME "Sony CDU-535"
154 #endif
156 #ifndef MAX_SPINUP_RETRY
157 # define MAX_SPINUP_RETRY 3 /* 1 is sufficient for most drives... */
158 #endif
159 #ifndef RETRY_FOR_BAD_STATUS
160 # define RETRY_FOR_BAD_STATUS 100 /* in 10th of second */
161 #endif
163 #ifndef DEBUG
164 # define DEBUG 1
165 #endif
168 * SONY535_BUFFER_SIZE determines the size of internal buffer used
169 * by the drive. It must be at least 2K and the larger the buffer
170 * the better the transfer rate. It does however take system memory.
171 * On my system I get the following transfer rates using dd to read
172 * 10 Mb off /dev/cdrom.
174 * 8K buffer 43 Kb/sec
175 * 16K buffer 66 Kb/sec
176 * 32K buffer 91 Kb/sec
177 * 64K buffer 111 Kb/sec
178 * 128K buffer 123 Kb/sec
179 * 512K buffer 123 Kb/sec
181 #define SONY535_BUFFER_SIZE (64*1024)
184 * if LOCK_DOORS is defined then the eject button is disabled while
185 * the device is open.
187 #ifndef NO_LOCK_DOORS
188 # define LOCK_DOORS
189 #endif
191 static int read_subcode(void);
192 static void sony_get_toc(void);
193 static int cdu_open(struct inode *inode, struct file *filp);
194 static inline unsigned int int_to_bcd(unsigned int val);
195 static unsigned int bcd_to_int(unsigned int bcd);
196 static int do_sony_cmd(Byte * cmd, int nCmd, Byte status[2],
197 Byte * response, int n_response, int ignoreStatusBit7);
199 /* The base I/O address of the Sony Interface. This is a variable (not a
200 #define) so it can be easily changed via some future ioctl() */
201 static unsigned int sony535_cd_base_io = CDU535_ADDRESS;
202 MODULE_PARM(sony535_cd_base_io, "i");
205 * The following are I/O addresses of the various registers for the drive. The
206 * comment for the base address also applies here.
208 static unsigned short select_unit_reg;
209 static unsigned short result_reg;
210 static unsigned short command_reg;
211 static unsigned short read_status_reg;
212 static unsigned short data_reg;
214 static int initialized = 0; /* Has the drive been initialized? */
215 static int sony_disc_changed = 1; /* Has the disk been changed
216 since the last check? */
217 static int sony_toc_read = 0; /* Has the table of contents been
218 read? */
219 static unsigned int sony_buffer_size; /* Size in bytes of the read-ahead
220 buffer. */
221 static unsigned int sony_buffer_sectors; /* Size (in 2048 byte records) of
222 the read-ahead buffer. */
223 static unsigned int sony_usage = 0; /* How many processes have the
224 drive open. */
226 static int sony_first_block = -1; /* First OS block (512 byte) in
227 the read-ahead buffer */
228 static int sony_last_block = -1; /* Last OS block (512 byte) in
229 the read-ahead buffer */
231 static struct s535_sony_toc *sony_toc; /* Points to the table of
232 contents. */
234 static struct s535_sony_subcode *last_sony_subcode; /* Points to the last
235 subcode address read */
236 static Byte **sony_buffer; /* Points to the pointers
237 to the sector buffers */
239 static int sony_inuse = 0; /* is the drive in use? Only one
240 open at a time allowed */
243 * The audio status uses the values from read subchannel data as specified
244 * in include/linux/cdrom.h.
246 static int sony_audio_status = CDROM_AUDIO_NO_STATUS;
249 * The following are a hack for pausing and resuming audio play. The drive
250 * does not work as I would expect it, if you stop it then start it again,
251 * the drive seeks back to the beginning and starts over. This holds the
252 * position during a pause so a resume can restart it. It uses the
253 * audio status variable above to tell if it is paused.
254 * I just kept the CDU-31A driver behavior rather than using the PAUSE
255 * command on the CDU-535.
257 static Byte cur_pos_msf[3] = {0, 0, 0};
258 static Byte final_pos_msf[3] = {0, 0, 0};
260 /* What IRQ is the drive using? 0 if none. */
261 static int sony535_irq_used = CDU535_INTERRUPT;
263 /* The interrupt handler will wake this queue up when it gets an interrupt. */
264 static struct wait_queue *cdu535_irq_wait = NULL;
268 * This routine returns 1 if the disk has been changed since the last
269 * check or 0 if it hasn't. Setting flag to 0 resets the changed flag.
271 static int
272 cdu535_check_media_change(kdev_t full_dev)
274 int retval;
276 if (MINOR(full_dev) != 0) {
277 printk(CDU535_MESSAGE_NAME " request error: invalid device.\n");
278 return 0;
281 /* if driver is not initialized, always return 0 */
282 retval = initialized ? sony_disc_changed : 0;
283 sony_disc_changed = 0;
284 return retval;
287 static inline void
288 enable_interrupts(void)
290 #ifdef USE_IRQ
292 * This code was taken from cdu31a.c; it will not
293 * directly work for the cdu535 as written...
295 curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
296 | SONY_RES_RDY_INT_EN_BIT
297 | SONY_DATA_RDY_INT_EN_BIT);
298 outb(curr_control_reg, sony_cd_control_reg);
299 #endif
302 static inline void
303 disable_interrupts(void)
305 #ifdef USE_IRQ
307 * This code was taken from cdu31a.c; it will not
308 * directly work for the cdu535 as written...
310 curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
311 | SONY_RES_RDY_INT_EN_BIT
312 | SONY_DATA_RDY_INT_EN_BIT);
313 outb(curr_control_reg, sony_cd_control_reg);
314 #endif
317 static void
318 cdu535_interrupt(int irq, void *dev_id, struct pt_regs *regs)
320 disable_interrupts();
321 if (cdu535_irq_wait != NULL)
322 wake_up(&cdu535_irq_wait);
323 else
324 printk(CDU535_MESSAGE_NAME
325 ": Got an interrupt but nothing was waiting\n");
330 * Wait a little while (used for polling the drive). If in initialization,
331 * setting a timeout doesn't work, so just loop for a while. (We trust
332 * that the sony_sleep() call is protected by a test for proper jiffies count.)
334 static inline void
335 sony_sleep(void)
337 if (sony535_irq_used <= 0) { /* poll */
338 current->state = TASK_INTERRUPTIBLE;
339 current->timeout = jiffies;
340 schedule();
341 } else { /* Interrupt driven */
342 cli();
343 enable_interrupts();
344 interruptible_sleep_on(&cdu535_irq_wait);
345 sti();
349 /*------------------start of SONY CDU535 very specific ---------------------*/
351 /****************************************************************************
352 * void select_unit( int unit_no )
354 * Select the specified unit (0-3) so that subsequent commands reference it
355 ****************************************************************************/
356 static void
357 select_unit(int unit_no)
359 unsigned int select_mask = ~(1 << unit_no);
360 outb(select_mask, select_unit_reg);
363 /***************************************************************************
364 * int read_result_reg( Byte *data_ptr )
366 * Read a result byte from the Sony CDU controller, store in location pointed
367 * to by data_ptr. Return zero on success, TIME_OUT if we did not receive
368 * data.
369 ***************************************************************************/
370 static int
371 read_result_reg(Byte *data_ptr)
373 int retry_count;
374 int read_status;
376 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
377 while (jiffies < retry_count) {
378 if (((read_status = inb(read_status_reg)) & SONY535_RESULT_NOT_READY_BIT) == 0) {
379 #if DEBUG > 1
380 printk(CDU535_MESSAGE_NAME
381 ": read_result_reg(): readStatReg = 0x%x\n", read_status);
382 #endif
383 *data_ptr = inb(result_reg);
384 return 0;
385 } else {
386 sony_sleep();
389 printk(CDU535_MESSAGE_NAME " read_result_reg: TIME OUT!\n");
390 return TIME_OUT;
393 /****************************************************************************
394 * int read_exec_status( Byte status[2] )
396 * Read the execution status of the last command and put into status.
397 * Handles reading second status word if available. Returns 0 on success,
398 * TIME_OUT on failure.
399 ****************************************************************************/
400 static int
401 read_exec_status(Byte status[2])
403 status[1] = 0;
404 if (read_result_reg(&(status[0])) != 0)
405 return TIME_OUT;
406 if ((status[0] & 0x80) != 0) { /* byte two follows */
407 if (read_result_reg(&(status[1])) != 0)
408 return TIME_OUT;
410 #if DEBUG > 1
411 printk(CDU535_MESSAGE_NAME ": read_exec_status: read 0x%x 0x%x\n",
412 status[0], status[1]);
413 #endif
414 return 0;
417 /****************************************************************************
418 * int check_drive_status( void )
420 * Check the current drive status. Using this before executing a command
421 * takes care of the problem of unsolicited drive status-2 messages.
422 * Add a check of the audio status if we think the disk is playing.
423 ****************************************************************************/
424 static int
425 check_drive_status(void)
427 Byte status, e_status[2];
428 int CDD, ATN;
429 Byte cmd;
431 select_unit(0);
432 if (sony_audio_status == CDROM_AUDIO_PLAY) { /* check status */
433 outb(SONY535_REQUEST_AUDIO_STATUS, command_reg);
434 if (read_result_reg(&status) == 0) {
435 switch (status) {
436 case 0x0:
437 break; /* play in progress */
438 case 0x1:
439 break; /* paused */
440 case 0x3: /* audio play completed */
441 case 0x5: /* play not requested */
442 sony_audio_status = CDROM_AUDIO_COMPLETED;
443 read_subcode();
444 break;
445 case 0x4: /* error during play */
446 sony_audio_status = CDROM_AUDIO_ERROR;
447 break;
451 /* now check drive status */
452 outb(SONY535_REQUEST_DRIVE_STATUS_2, command_reg);
453 if (read_result_reg(&status) != 0)
454 return TIME_OUT;
456 #if DEBUG > 1
457 printk(CDU535_MESSAGE_NAME ": check_drive_status() got 0x%x\n", status);
458 #endif
460 if (status == 0)
461 return 0;
463 ATN = status & 0xf;
464 CDD = (status >> 4) & 0xf;
466 switch (ATN) {
467 case 0x0:
468 break; /* go on to CDD stuff */
469 case SONY535_ATN_BUSY:
470 if (initialized)
471 printk(CDU535_MESSAGE_NAME " error: drive busy\n");
472 return CD_BUSY;
473 case SONY535_ATN_EJECT_IN_PROGRESS:
474 printk(CDU535_MESSAGE_NAME " error: eject in progress\n");
475 sony_audio_status = CDROM_AUDIO_INVALID;
476 return CD_BUSY;
477 case SONY535_ATN_RESET_OCCURRED:
478 case SONY535_ATN_DISC_CHANGED:
479 case SONY535_ATN_RESET_AND_DISC_CHANGED:
480 #if DEBUG > 0
481 printk(CDU535_MESSAGE_NAME " notice: reset occurred or disc changed\n");
482 #endif
483 sony_disc_changed = 1;
484 sony_toc_read = 0;
485 sony_audio_status = CDROM_AUDIO_NO_STATUS;
486 sony_first_block = -1;
487 sony_last_block = -1;
488 if (initialized) {
489 cmd = SONY535_SPIN_UP;
490 do_sony_cmd(&cmd, 1, e_status, NULL, 0, 0);
491 sony_get_toc();
493 return 0;
494 default:
495 printk(CDU535_MESSAGE_NAME " error: drive busy (ATN=0x%x)\n", ATN);
496 return CD_BUSY;
498 switch (CDD) { /* the 531 docs are not helpful in decoding this */
499 case 0x0: /* just use the values from the DOS driver */
500 case 0x2:
501 case 0xa:
502 break; /* no error */
503 case 0xc:
504 printk(CDU535_MESSAGE_NAME
505 ": check_drive_status(): CDD = 0xc! Not properly handled!\n");
506 return CD_BUSY; /* ? */
507 default:
508 return CD_BUSY;
510 return 0;
511 } /* check_drive_status() */
513 /*****************************************************************************
514 * int do_sony_cmd( Byte *cmd, int n_cmd, Byte status[2],
515 * Byte *response, int n_response, int ignore_status_bit7 )
517 * Generic routine for executing commands. The command and its parameters
518 * should be placed in the cmd[] array, number of bytes in the command is
519 * stored in nCmd. The response from the command will be stored in the
520 * response array. The number of bytes you expect back (excluding status)
521 * should be passed in n_response. Finally, some
522 * commands set bit 7 of the return status even when there is no second
523 * status byte, on these commands set ignoreStatusBit7 TRUE.
524 * If the command was sent and data received back, then we return 0,
525 * else we return TIME_OUT. You still have to check the status yourself.
526 * You should call check_drive_status() before calling this routine
527 * so that you do not lose notifications of disk changes, etc.
528 ****************************************************************************/
529 static int
530 do_sony_cmd(Byte * cmd, int n_cmd, Byte status[2],
531 Byte * response, int n_response, int ignore_status_bit7)
533 int i;
535 /* write out the command */
536 for (i = 0; i < n_cmd; i++)
537 outb(cmd[i], command_reg);
539 /* read back the status */
540 if (read_result_reg(status) != 0)
541 return TIME_OUT;
542 if (!ignore_status_bit7 && ((status[0] & 0x80) != 0)) {
543 /* get second status byte */
544 if (read_result_reg(status + 1) != 0)
545 return TIME_OUT;
546 } else {
547 status[1] = 0;
549 #if DEBUG > 2
550 printk(CDU535_MESSAGE_NAME ": do_sony_cmd %x: %x %x\n",
551 *cmd, status[0], status[1]);
552 #endif
554 /* do not know about when I should read set of data and when not to */
555 if ((status[0] & ((ignore_status_bit7 ? 0x7f : 0xff) & 0x8f)) != 0)
556 return 0;
558 /* else, read in rest of data */
559 for (i = 0; 0 < n_response; n_response--, i++)
560 if (read_result_reg(response + i) != 0)
561 return TIME_OUT;
562 return 0;
563 } /* do_sony_cmd() */
565 /**************************************************************************
566 * int set_drive_mode( int mode, Byte status[2] )
568 * Set the drive mode to the specified value (mode=0 is audio, mode=e0
569 * is mode-1 CDROM
570 **************************************************************************/
571 static int
572 set_drive_mode(int mode, Byte status[2])
574 Byte cmd_buff[2];
575 Byte ret_buff[1];
577 cmd_buff[0] = SONY535_SET_DRIVE_MODE;
578 cmd_buff[1] = mode;
579 return do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1);
582 /***************************************************************************
583 * int seek_and_read_N_blocks( Byte params[], int n_blocks, Byte status[2],
584 * Byte *data_buff, int buff_size )
586 * Read n_blocks of data from the CDROM starting at position params[0:2],
587 * number of blocks in stored in params[3:5] -- both these are already
588 * int bcd format.
589 * Transfer the data into the buffer pointed at by data_buff. buff_size
590 * gives the number of bytes available in the buffer.
591 * The routine returns number of bytes read in if successful, otherwise
592 * it returns one of the standard error returns.
593 ***************************************************************************/
594 static int
595 seek_and_read_N_blocks(Byte params[], int n_blocks, Byte status[2],
596 Byte **buff, int buf_size)
598 const int block_size = 2048;
599 Byte cmd_buff[7];
600 int i;
601 int read_status;
602 int retry_count;
603 Byte *data_buff;
604 int sector_count = 0;
606 if (buf_size < ((long)block_size) * n_blocks)
607 return NO_ROOM;
609 set_drive_mode(SONY535_CDROM_DRIVE_MODE, status);
611 /* send command to read the data */
612 cmd_buff[0] = SONY535_SEEK_AND_READ_N_BLOCKS_1;
613 for (i = 0; i < 6; i++)
614 cmd_buff[i + 1] = params[i];
615 for (i = 0; i < 7; i++)
616 outb(cmd_buff[i], command_reg);
618 /* read back the data one block at a time */
619 while (0 < n_blocks--) {
620 /* wait for data to be ready */
621 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
622 while (jiffies < retry_count) {
623 read_status = inb(read_status_reg);
624 if ((read_status & SONY535_RESULT_NOT_READY_BIT) == 0) {
625 read_exec_status(status);
626 return BAD_STATUS;
628 if ((read_status & SONY535_DATA_NOT_READY_BIT) == 0) {
629 /* data is ready, read it */
630 data_buff = buff[sector_count++];
631 for (i = 0; i < block_size; i++)
632 *data_buff++ = inb(data_reg); /* unrolling this loop does not seem to help */
633 break; /* exit the timeout loop */
635 sony_sleep(); /* data not ready, sleep a while */
637 if (retry_count <= jiffies)
638 return TIME_OUT; /* if we reach this stage */
641 /* read all the data, now read the status */
642 if ((i = read_exec_status(status)) != 0)
643 return i;
644 return block_size * sector_count;
645 } /* seek_and_read_N_blocks() */
647 /****************************************************************************
648 * int request_toc_data( Byte status[2], struct s535_sony_toc *toc )
650 * Read in the table of contents data. Converts all the bcd data
651 * into integers in the toc structure.
652 ****************************************************************************/
653 static int
654 request_toc_data(Byte status[2], struct s535_sony_toc *toc)
656 int to_status;
657 int i, j, n_tracks, track_no;
658 int first_track_num, last_track_num;
659 Byte cmd_no = 0xb2;
660 Byte track_address_buffer[5];
662 /* read the fixed portion of the table of contents */
663 if ((to_status = do_sony_cmd(&cmd_no, 1, status, (Byte *) toc, 15, 1)) != 0)
664 return to_status;
666 /* convert the data into integers so we can use them */
667 first_track_num = bcd_to_int(toc->first_track_num);
668 last_track_num = bcd_to_int(toc->last_track_num);
669 n_tracks = last_track_num - first_track_num + 1;
671 /* read each of the track address descriptors */
672 for (i = 0; i < n_tracks; i++) {
673 /* read the descriptor into a temporary buffer */
674 for (j = 0; j < 5; j++) {
675 if (read_result_reg(track_address_buffer + j) != 0)
676 return TIME_OUT;
677 if (j == 1) /* need to convert from bcd */
678 track_no = bcd_to_int(track_address_buffer[j]);
680 /* copy the descriptor to proper location - sonycd.c just fills */
681 memcpy(toc->tracks + i, track_address_buffer, 5);
683 return 0;
684 } /* request_toc_data() */
686 /***************************************************************************
687 * int spin_up_drive( Byte status[2] )
689 * Spin up the drive (unless it is already spinning).
690 ***************************************************************************/
691 static int
692 spin_up_drive(Byte status[2])
694 Byte cmd;
696 /* first see if the drive is already spinning */
697 cmd = SONY535_REQUEST_DRIVE_STATUS_1;
698 if (do_sony_cmd(&cmd, 1, status, NULL, 0, 0) != 0)
699 return TIME_OUT;
700 if ((status[0] & SONY535_STATUS1_NOT_SPINNING) == 0)
701 return 0; /* it's already spinning */
703 /* otherwise, give the spin-up command */
704 cmd = SONY535_SPIN_UP;
705 return do_sony_cmd(&cmd, 1, status, NULL, 0, 0);
708 /*--------------------end of SONY CDU535 very specific ---------------------*/
710 /* Convert from an integer 0-99 to BCD */
711 static inline unsigned int
712 int_to_bcd(unsigned int val)
714 int retval;
716 retval = (val / 10) << 4;
717 retval = retval | val % 10;
718 return retval;
722 /* Convert from BCD to an integer from 0-99 */
723 static unsigned int
724 bcd_to_int(unsigned int bcd)
726 return (((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f);
731 * Convert a logical sector value (like the OS would want to use for
732 * a block device) to an MSF format.
734 static void
735 log_to_msf(unsigned int log, Byte *msf)
737 log = log + LOG_START_OFFSET;
738 msf[0] = int_to_bcd(log / 4500);
739 log = log % 4500;
740 msf[1] = int_to_bcd(log / 75);
741 msf[2] = int_to_bcd(log % 75);
746 * Convert an MSF format to a logical sector.
748 static unsigned int
749 msf_to_log(Byte *msf)
751 unsigned int log;
754 log = bcd_to_int(msf[2]);
755 log += bcd_to_int(msf[1]) * 75;
756 log += bcd_to_int(msf[0]) * 4500;
757 log = log - LOG_START_OFFSET;
759 return log;
764 * Take in integer size value and put it into a buffer like
765 * the drive would want to see a number-of-sector value.
767 static void
768 size_to_buf(unsigned int size, Byte *buf)
770 buf[0] = size / 65536;
771 size = size % 65536;
772 buf[1] = size / 256;
773 buf[2] = size % 256;
778 * The OS calls this to perform a read or write operation to the drive.
779 * Write obviously fail. Reads to a read ahead of sony_buffer_size
780 * bytes to help speed operations. This especially helps since the OS
781 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
782 * data access on a CD is done sequentially, this saves a lot of operations.
784 static void
785 do_cdu535_request(void)
787 unsigned int dev;
788 unsigned int read_size;
789 int block;
790 int nsect;
791 int copyoff;
792 int spin_up_retry;
793 Byte params[10];
794 Byte status[2];
795 Byte cmd[2];
797 if (!sony_inuse) {
798 cdu_open(NULL, NULL);
800 while (1) {
802 * The beginning here is stolen from the hard disk driver. I hope
803 * it's right.
805 if (!(CURRENT) || CURRENT->rq_status == RQ_INACTIVE) {
806 return;
808 INIT_REQUEST;
809 dev = MINOR(CURRENT->rq_dev);
810 block = CURRENT->sector;
811 nsect = CURRENT->nr_sectors;
812 if (dev != 0) {
813 end_request(0);
814 continue;
816 switch (CURRENT->cmd) {
817 case READ:
819 * If the block address is invalid or the request goes beyond the end of
820 * the media, return an error.
823 if (sony_toc->lead_out_start_lba <= (block / 4)) {
824 end_request(0);
825 return;
827 if (sony_toc->lead_out_start_lba <= ((block + nsect) / 4)) {
828 end_request(0);
829 return;
831 while (0 < nsect) {
833 * If the requested sector is not currently in the read-ahead buffer,
834 * it must be read in.
836 if ((block < sony_first_block) || (sony_last_block < block)) {
837 sony_first_block = (block / 4) * 4;
838 log_to_msf(block / 4, params);
841 * If the full read-ahead would go beyond the end of the media, trim
842 * it back to read just till the end of the media.
844 if (sony_toc->lead_out_start_lba <= ((block / 4) + sony_buffer_sectors)) {
845 sony_last_block = (sony_toc->lead_out_start_lba * 4) - 1;
846 read_size = sony_toc->lead_out_start_lba - (block / 4);
847 } else {
848 sony_last_block = sony_first_block + (sony_buffer_sectors * 4) - 1;
849 read_size = sony_buffer_sectors;
851 size_to_buf(read_size, &params[3]);
854 * Read the data. If the drive was not spinning,
855 * spin it up and try some more.
857 for (spin_up_retry=0 ;; ++spin_up_retry) {
858 /* This loop has been modified to support the Sony
859 * CDU-510/515 series, thanks to Claudio Porfiri
860 * <C.Porfiri@nisms.tei.ericsson.se>.
863 * This part is to deal with very slow hardware. We
864 * try at most MAX_SPINUP_RETRY times to read the same
865 * block. A check for seek_and_read_N_blocks' result is
866 * performed; if the result is wrong, the CDROM's engine
867 * is restarted and the operation is tried again.
870 * 1995-06-01: The system got problems when downloading
871 * from Slackware CDROM, the problem seems to be:
872 * seek_and_read_N_blocks returns BAD_STATUS and we
873 * should wait for a while before retrying, so a new
874 * part was added to discriminate the return value from
875 * seek_and_read_N_blocks for the various cases.
877 int readStatus = seek_and_read_N_blocks(params, read_size,
878 status, sony_buffer, (read_size * 2048));
879 if (0 <= readStatus) /* Good data; common case, placed first */
880 break;
881 if (readStatus == NO_ROOM || spin_up_retry == MAX_SPINUP_RETRY) {
882 /* give up */
883 if (readStatus == NO_ROOM)
884 printk(CDU535_MESSAGE_NAME " No room to read from CD\n");
885 else
886 printk(CDU535_MESSAGE_NAME " Read error: 0x%.2x\n",
887 status[0]);
888 sony_first_block = -1;
889 sony_last_block = -1;
890 end_request(0);
891 return;
893 if (readStatus == BAD_STATUS) {
894 /* Sleep for a while, then retry */
895 current->state = TASK_INTERRUPTIBLE;
896 current->timeout = jiffies + RETRY_FOR_BAD_STATUS;
897 schedule();
899 #if DEBUG > 0
900 printk(CDU535_MESSAGE_NAME
901 " debug: calling spin up when reading data!\n");
902 #endif
903 cmd[0] = SONY535_SPIN_UP;
904 do_sony_cmd(cmd, 1, status, NULL, 0, 0);
908 * The data is in memory now, copy it to the buffer and advance to the
909 * next block to read.
911 copyoff = block - sony_first_block;
912 memcpy(CURRENT->buffer,
913 sony_buffer[copyoff / 4] + 512 * (copyoff % 4), 512);
915 block += 1;
916 nsect -= 1;
917 CURRENT->buffer += 512;
920 end_request(1);
921 break;
923 case WRITE:
924 end_request(0);
925 break;
927 default:
928 panic("Unknown SONY CD cmd");
935 * Read the table of contents from the drive and set sony_toc_read if
936 * successful.
938 static void
939 sony_get_toc(void)
941 Byte status[2];
942 if (!sony_toc_read) {
943 /* do not call check_drive_status() from here since it can call this routine */
944 if (request_toc_data(status, sony_toc) < 0)
945 return;
946 sony_toc->lead_out_start_lba = msf_to_log(sony_toc->lead_out_start_msf);
947 sony_toc_read = 1;
953 * Search for a specific track in the table of contents. track is
954 * passed in bcd format
956 static int
957 find_track(int track)
959 int i;
960 int num_tracks;
963 num_tracks = bcd_to_int(sony_toc->last_track_num) -
964 bcd_to_int(sony_toc->first_track_num) + 1;
965 for (i = 0; i < num_tracks; i++) {
966 if (sony_toc->tracks[i].track == track) {
967 return i;
971 return -1;
975 * Read the subcode and put it int last_sony_subcode for future use.
977 static int
978 read_subcode(void)
980 Byte cmd = SONY535_REQUEST_SUB_Q_DATA;
981 Byte status[2];
982 int dsc_status;
984 if (check_drive_status() != 0)
985 return -EIO;
987 if ((dsc_status = do_sony_cmd(&cmd, 1, status, (Byte *) last_sony_subcode,
988 sizeof(struct s535_sony_subcode), 1)) != 0) {
989 printk(CDU535_MESSAGE_NAME " error 0x%.2x, %d (read_subcode)\n",
990 status[0], dsc_status);
991 return -EIO;
993 return 0;
998 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
999 * the drive is playing, the subchannel needs to be read (since it would be
1000 * changing). If the drive is paused or completed, the subcode information has
1001 * already been stored, just use that. The ioctl call wants things in decimal
1002 * (not BCD), so all the conversions are done.
1004 static int
1005 sony_get_subchnl_info(long arg)
1007 struct cdrom_subchnl schi;
1008 int err;
1010 /* Get attention stuff */
1011 if (check_drive_status() != 0)
1012 return -EIO;
1014 sony_get_toc();
1015 if (!sony_toc_read) {
1016 return -EIO;
1018 err = verify_area(VERIFY_WRITE /* and read */ , (char *)arg, sizeof schi);
1019 if (err)
1020 return err;
1022 copy_from_user(&schi, (char *)arg, sizeof schi);
1024 switch (sony_audio_status) {
1025 case CDROM_AUDIO_PLAY:
1026 if (read_subcode() < 0) {
1027 return -EIO;
1029 break;
1031 case CDROM_AUDIO_PAUSED:
1032 case CDROM_AUDIO_COMPLETED:
1033 break;
1035 case CDROM_AUDIO_NO_STATUS:
1036 schi.cdsc_audiostatus = sony_audio_status;
1037 copy_to_user((char *)arg, &schi, sizeof schi);
1038 return 0;
1039 break;
1041 case CDROM_AUDIO_INVALID:
1042 case CDROM_AUDIO_ERROR:
1043 default:
1044 return -EIO;
1047 schi.cdsc_audiostatus = sony_audio_status;
1048 schi.cdsc_adr = last_sony_subcode->address;
1049 schi.cdsc_ctrl = last_sony_subcode->control;
1050 schi.cdsc_trk = bcd_to_int(last_sony_subcode->track_num);
1051 schi.cdsc_ind = bcd_to_int(last_sony_subcode->index_num);
1052 if (schi.cdsc_format == CDROM_MSF) {
1053 schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode->abs_msf[0]);
1054 schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode->abs_msf[1]);
1055 schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode->abs_msf[2]);
1057 schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode->rel_msf[0]);
1058 schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode->rel_msf[1]);
1059 schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode->rel_msf[2]);
1060 } else if (schi.cdsc_format == CDROM_LBA) {
1061 schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode->abs_msf);
1062 schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode->rel_msf);
1064 copy_to_user((char *)arg, &schi, sizeof schi);
1065 return 0;
1070 * The big ugly ioctl handler.
1072 static int
1073 cdu_ioctl(struct inode *inode,
1074 struct file *file,
1075 unsigned int cmd,
1076 unsigned long arg)
1078 unsigned int dev;
1079 Byte status[2];
1080 Byte cmd_buff[10], params[10];
1081 int i;
1082 int dsc_status;
1083 int err;
1085 if (!inode) {
1086 return -EINVAL;
1088 dev = MINOR(inode->i_rdev) >> 6;
1089 if (dev != 0) {
1090 return -EINVAL;
1092 if (check_drive_status() != 0)
1093 return -EIO;
1095 switch (cmd) {
1096 case CDROMSTART: /* Spin up the drive */
1097 if (spin_up_drive(status) < 0) {
1098 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTART)\n",
1099 status[0]);
1100 return -EIO;
1102 return 0;
1103 break;
1105 case CDROMSTOP: /* Spin down the drive */
1106 cmd_buff[0] = SONY535_HOLD;
1107 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1110 * Spin the drive down, ignoring the error if the disk was
1111 * already not spinning.
1113 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1114 cmd_buff[0] = SONY535_SPIN_DOWN;
1115 dsc_status = do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1116 if (((dsc_status < 0) && (dsc_status != BAD_STATUS)) ||
1117 ((status[0] & ~(SONY535_STATUS1_NOT_SPINNING)) != 0)) {
1118 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTOP)\n",
1119 status[0]);
1120 return -EIO;
1122 return 0;
1123 break;
1125 case CDROMPAUSE: /* Pause the drive */
1126 cmd_buff[0] = SONY535_HOLD; /* CDU-31 driver uses AUDIO_STOP, not pause */
1127 if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
1128 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPAUSE)\n",
1129 status[0]);
1130 return -EIO;
1132 /* Get the current position and save it for resuming */
1133 if (read_subcode() < 0) {
1134 return -EIO;
1136 cur_pos_msf[0] = last_sony_subcode->abs_msf[0];
1137 cur_pos_msf[1] = last_sony_subcode->abs_msf[1];
1138 cur_pos_msf[2] = last_sony_subcode->abs_msf[2];
1139 sony_audio_status = CDROM_AUDIO_PAUSED;
1140 return 0;
1141 break;
1143 case CDROMRESUME: /* Start the drive after being paused */
1144 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1146 if (sony_audio_status != CDROM_AUDIO_PAUSED) {
1147 return -EINVAL;
1149 spin_up_drive(status);
1151 /* Start the drive at the saved position. */
1152 cmd_buff[0] = SONY535_PLAY_AUDIO;
1153 cmd_buff[1] = 0; /* play back starting at this address */
1154 cmd_buff[2] = cur_pos_msf[0];
1155 cmd_buff[3] = cur_pos_msf[1];
1156 cmd_buff[4] = cur_pos_msf[2];
1157 cmd_buff[5] = SONY535_PLAY_AUDIO;
1158 cmd_buff[6] = 2; /* set ending address */
1159 cmd_buff[7] = final_pos_msf[0];
1160 cmd_buff[8] = final_pos_msf[1];
1161 cmd_buff[9] = final_pos_msf[2];
1162 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1163 (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1164 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMRESUME)\n",
1165 status[0]);
1166 return -EIO;
1168 sony_audio_status = CDROM_AUDIO_PLAY;
1169 return 0;
1170 break;
1172 case CDROMPLAYMSF: /* Play starting at the given MSF address. */
1173 err = verify_area(VERIFY_READ, (char *)arg, 6);
1174 if (err)
1175 return err;
1176 spin_up_drive(status);
1177 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1178 copy_from_user(params, (void *)arg, 6);
1180 /* The parameters are given in int, must be converted */
1181 for (i = 0; i < 3; i++) {
1182 cmd_buff[2 + i] = int_to_bcd(params[i]);
1183 cmd_buff[7 + i] = int_to_bcd(params[i + 3]);
1185 cmd_buff[0] = SONY535_PLAY_AUDIO;
1186 cmd_buff[1] = 0; /* play back starting at this address */
1187 /* cmd_buff[2-4] are filled in for loop above */
1188 cmd_buff[5] = SONY535_PLAY_AUDIO;
1189 cmd_buff[6] = 2; /* set ending address */
1190 /* cmd_buff[7-9] are filled in for loop above */
1191 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1192 (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1193 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYMSF)\n",
1194 status[0]);
1195 return -EIO;
1197 /* Save the final position for pauses and resumes */
1198 final_pos_msf[0] = cmd_buff[7];
1199 final_pos_msf[1] = cmd_buff[8];
1200 final_pos_msf[2] = cmd_buff[9];
1201 sony_audio_status = CDROM_AUDIO_PLAY;
1202 return 0;
1203 break;
1205 case CDROMREADTOCHDR: /* Read the table of contents header */
1207 struct cdrom_tochdr *hdr;
1208 struct cdrom_tochdr loc_hdr;
1210 sony_get_toc();
1211 if (!sony_toc_read)
1212 return -EIO;
1213 hdr = (struct cdrom_tochdr *)arg;
1214 err = verify_area(VERIFY_WRITE, hdr, sizeof *hdr);
1215 if (err)
1216 return err;
1217 loc_hdr.cdth_trk0 = bcd_to_int(sony_toc->first_track_num);
1218 loc_hdr.cdth_trk1 = bcd_to_int(sony_toc->last_track_num);
1219 copy_to_user(hdr, &loc_hdr, sizeof *hdr);
1221 return 0;
1222 break;
1224 case CDROMREADTOCENTRY: /* Read a given table of contents entry */
1226 struct cdrom_tocentry *entry;
1227 struct cdrom_tocentry loc_entry;
1228 int track_idx;
1229 Byte *msf_val = NULL;
1231 sony_get_toc();
1232 if (!sony_toc_read) {
1233 return -EIO;
1235 entry = (struct cdrom_tocentry *)arg;
1236 err = verify_area(VERIFY_WRITE /* and read */ , entry, sizeof *entry);
1237 if (err)
1238 return err;
1240 copy_from_user(&loc_entry, entry, sizeof loc_entry);
1242 /* Lead out is handled separately since it is special. */
1243 if (loc_entry.cdte_track == CDROM_LEADOUT) {
1244 loc_entry.cdte_adr = 0 /*sony_toc->address2 */ ;
1245 loc_entry.cdte_ctrl = sony_toc->control2;
1246 msf_val = sony_toc->lead_out_start_msf;
1247 } else {
1248 track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
1249 if (track_idx < 0)
1250 return -EINVAL;
1251 loc_entry.cdte_adr = 0 /*sony_toc->tracks[track_idx].address */ ;
1252 loc_entry.cdte_ctrl = sony_toc->tracks[track_idx].control;
1253 msf_val = sony_toc->tracks[track_idx].track_start_msf;
1256 /* Logical buffer address or MSF format requested? */
1257 if (loc_entry.cdte_format == CDROM_LBA) {
1258 loc_entry.cdte_addr.lba = msf_to_log(msf_val);
1259 } else if (loc_entry.cdte_format == CDROM_MSF) {
1260 loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
1261 loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val + 1));
1262 loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val + 2));
1264 copy_to_user(entry, &loc_entry, sizeof *entry);
1266 return 0;
1267 break;
1269 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
1271 struct cdrom_ti ti;
1272 int track_idx;
1274 sony_get_toc();
1275 if (!sony_toc_read)
1276 return -EIO;
1277 err = verify_area(VERIFY_READ, (char *)arg, sizeof ti);
1278 if (err)
1279 return err;
1281 copy_from_user(&ti, (char *)arg, sizeof ti);
1282 if ((ti.cdti_trk0 < sony_toc->first_track_num)
1283 || (sony_toc->last_track_num < ti.cdti_trk0)
1284 || (ti.cdti_trk1 < ti.cdti_trk0)) {
1285 return -EINVAL;
1287 track_idx = find_track(int_to_bcd(ti.cdti_trk0));
1288 if (track_idx < 0)
1289 return -EINVAL;
1290 params[1] = sony_toc->tracks[track_idx].track_start_msf[0];
1291 params[2] = sony_toc->tracks[track_idx].track_start_msf[1];
1292 params[3] = sony_toc->tracks[track_idx].track_start_msf[2];
1294 * If we want to stop after the last track, use the lead-out
1295 * MSF to do that.
1297 if (bcd_to_int(sony_toc->last_track_num) <= ti.cdti_trk1) {
1298 log_to_msf(msf_to_log(sony_toc->lead_out_start_msf) - 1,
1299 &(params[4]));
1300 } else {
1301 track_idx = find_track(int_to_bcd(ti.cdti_trk1 + 1));
1302 if (track_idx < 0)
1303 return -EINVAL;
1304 log_to_msf(msf_to_log(sony_toc->tracks[track_idx].track_start_msf) - 1,
1305 &(params[4]));
1307 params[0] = 0x03;
1309 spin_up_drive(status);
1311 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1313 /* Start the drive at the saved position. */
1314 cmd_buff[0] = SONY535_PLAY_AUDIO;
1315 cmd_buff[1] = 0; /* play back starting at this address */
1316 cmd_buff[2] = params[1];
1317 cmd_buff[3] = params[2];
1318 cmd_buff[4] = params[3];
1319 cmd_buff[5] = SONY535_PLAY_AUDIO;
1320 cmd_buff[6] = 2; /* set ending address */
1321 cmd_buff[7] = params[4];
1322 cmd_buff[8] = params[5];
1323 cmd_buff[9] = params[6];
1324 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1325 (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1326 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYTRKIND)\n",
1327 status[0]);
1328 printk("... Params: %x %x %x %x %x %x %x\n",
1329 params[0], params[1], params[2],
1330 params[3], params[4], params[5], params[6]);
1331 return -EIO;
1333 /* Save the final position for pauses and resumes */
1334 final_pos_msf[0] = params[4];
1335 final_pos_msf[1] = params[5];
1336 final_pos_msf[2] = params[6];
1337 sony_audio_status = CDROM_AUDIO_PLAY;
1338 return 0;
1341 case CDROMSUBCHNL: /* Get subchannel info */
1342 return sony_get_subchnl_info(arg);
1344 case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
1346 struct cdrom_volctrl volctrl;
1348 err = verify_area(VERIFY_READ, (char *)arg, sizeof volctrl);
1349 if (err)
1350 return err;
1352 copy_from_user(&volctrl, (char *)arg, sizeof volctrl);
1353 cmd_buff[0] = SONY535_SET_VOLUME;
1354 cmd_buff[1] = volctrl.channel0;
1355 cmd_buff[2] = volctrl.channel1;
1356 if (do_sony_cmd(cmd_buff, 3, status, NULL, 0, 0) != 0) {
1357 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMVOLCTRL)\n",
1358 status[0]);
1359 return -EIO;
1362 return 0;
1364 case CDROMEJECT: /* Eject the drive */
1365 cmd_buff[0] = SONY535_STOP;
1366 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1367 cmd_buff[0] = SONY535_SPIN_DOWN;
1368 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1370 sony_audio_status = CDROM_AUDIO_INVALID;
1371 cmd_buff[0] = SONY535_EJECT_CADDY;
1372 if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
1373 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMEJECT)\n",
1374 status[0]);
1375 return -EIO;
1377 return 0;
1378 break;
1380 default:
1381 return -EINVAL;
1387 * Open the drive for operations. Spin the drive up and read the table of
1388 * contents if these have not already been done.
1390 static int
1391 cdu_open(struct inode *inode,
1392 struct file *filp)
1394 Byte status[2], cmd_buff[2];
1397 if (sony_inuse)
1398 return -EBUSY;
1399 if (check_drive_status() != 0)
1400 return -EIO;
1401 sony_inuse = 1;
1402 MOD_INC_USE_COUNT;
1404 if (spin_up_drive(status) != 0) {
1405 printk(CDU535_MESSAGE_NAME " error 0x%.2x (cdu_open, spin up)\n",
1406 status[0]);
1407 sony_inuse = 0;
1408 MOD_DEC_USE_COUNT;
1409 return -EIO;
1411 sony_get_toc();
1412 if (!sony_toc_read) {
1413 cmd_buff[0] = SONY535_SPIN_DOWN;
1414 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1415 sony_inuse = 0;
1416 MOD_DEC_USE_COUNT;
1417 return -EIO;
1419 if (inode) {
1420 check_disk_change(inode->i_rdev);
1422 sony_usage++;
1424 #ifdef LOCK_DOORS
1425 /* disable the eject button while mounted */
1426 cmd_buff[0] = SONY535_DISABLE_EJECT_BUTTON;
1427 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1428 #endif
1430 return 0;
1435 * Close the drive. Spin it down if no task is using it. The spin
1436 * down will fail if playing audio, so audio play is OK.
1438 static int
1439 cdu_release(struct inode *inode,
1440 struct file *filp)
1442 Byte status[2], cmd_no;
1444 sony_inuse = 0;
1445 MOD_DEC_USE_COUNT;
1447 if (0 < sony_usage) {
1448 sony_usage--;
1450 if (sony_usage == 0) {
1451 sync_dev(inode->i_rdev);
1452 check_drive_status();
1454 if (sony_audio_status != CDROM_AUDIO_PLAY) {
1455 cmd_no = SONY535_SPIN_DOWN;
1456 do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
1458 #ifdef LOCK_DOORS
1459 /* enable the eject button after umount */
1460 cmd_no = SONY535_ENABLE_EJECT_BUTTON;
1461 do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
1462 #endif
1464 return 0;
1468 static struct file_operations cdu_fops =
1470 NULL, /* lseek - default */
1471 block_read, /* read - general block-dev read */
1472 block_write, /* write - general block-dev write */
1473 NULL, /* readdir - bad */
1474 NULL, /* poll */
1475 cdu_ioctl, /* ioctl */
1476 NULL, /* mmap */
1477 cdu_open, /* open */
1478 NULL, /* flush */
1479 cdu_release, /* release */
1480 NULL, /* fsync */
1481 NULL, /* fasync */
1482 cdu535_check_media_change, /* check media change */
1483 NULL /* revalidate */
1487 * Initialize the driver.
1489 __initfunc(int
1490 sony535_init(void))
1492 struct s535_sony_drive_config drive_config;
1493 Byte cmd_buff[3];
1494 Byte ret_buff[2];
1495 Byte status[2];
1496 int retry_count;
1497 int tmp_irq;
1498 int i;
1500 /* Setting the base I/O address to 0 will disable it. */
1501 if ((sony535_cd_base_io == 0xffff)||(sony535_cd_base_io == 0))
1502 return 0;
1504 /* Set up all the register locations */
1505 result_reg = sony535_cd_base_io;
1506 command_reg = sony535_cd_base_io;
1507 data_reg = sony535_cd_base_io + 1;
1508 read_status_reg = sony535_cd_base_io + 2;
1509 select_unit_reg = sony535_cd_base_io + 3;
1511 #ifndef USE_IRQ
1512 sony535_irq_used = 0; /* polling only until this is ready... */
1513 #endif
1514 /* we need to poll until things get initialized */
1515 tmp_irq = sony535_irq_used;
1516 sony535_irq_used = 0;
1518 #if DEBUG > 0
1519 printk(KERN_INFO CDU535_MESSAGE_NAME ": probing base address %03X\n",
1520 sony535_cd_base_io);
1521 #endif
1522 if (check_region(sony535_cd_base_io,4)) {
1523 printk(CDU535_MESSAGE_NAME ": my base address is not free!\n");
1524 return -EIO;
1526 /* look for the CD-ROM, follows the procedure in the DOS driver */
1527 inb(select_unit_reg);
1528 retry_count = jiffies + 2 * HZ;
1529 while (jiffies < retry_count)
1530 sony_sleep(); /* wait for 40 18 Hz ticks (from DOS driver) */
1531 inb(result_reg);
1533 outb(0, read_status_reg); /* does a reset? */
1534 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1535 while (jiffies < retry_count) {
1536 select_unit(0);
1537 if (inb(result_reg) != 0xff)
1538 break;
1539 sony_sleep();
1542 if ((jiffies < retry_count) && (check_drive_status() != TIME_OUT)) {
1543 /* CD-ROM drive responded -- get the drive configuration */
1544 cmd_buff[0] = SONY535_INQUIRY;
1545 if (do_sony_cmd(cmd_buff, 1, status,
1546 (Byte *)&drive_config, 28, 1) == 0) {
1547 /* was able to get the configuration,
1548 * set drive mode as rest of init
1550 #if DEBUG > 0
1551 /* 0x50 == CADDY_NOT_INSERTED | NOT_SPINNING */
1552 if ( (status[0] & 0x7f) != 0 && (status[0] & 0x7f) != 0x50 )
1553 printk(CDU535_MESSAGE_NAME
1554 "Inquiry command returned status = 0x%x\n", status[0]);
1555 #endif
1556 /* now ready to use interrupts, if available */
1557 sony535_irq_used = tmp_irq;
1558 #ifndef MODULE
1559 /* This code is not in MODULEs by default, since the autoirq stuff might
1560 * not be in the module-accessible symbol table.
1562 /* A negative sony535_irq_used will attempt an autoirq. */
1563 if (sony535_irq_used < 0) {
1564 autoirq_setup(0);
1565 enable_interrupts();
1566 outb(0, read_status_reg); /* does a reset? */
1567 sony535_irq_used = autoirq_report(10);
1568 disable_interrupts();
1570 #endif
1571 if (sony535_irq_used > 0) {
1572 if (request_irq(sony535_irq_used, cdu535_interrupt,
1573 SA_INTERRUPT, CDU535_HANDLE, NULL)) {
1574 printk("Unable to grab IRQ%d for the " CDU535_MESSAGE_NAME
1575 " driver; polling instead.\n", sony535_irq_used);
1576 sony535_irq_used = 0;
1579 cmd_buff[0] = SONY535_SET_DRIVE_MODE;
1580 cmd_buff[1] = 0x0; /* default audio */
1581 if (do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1) == 0) {
1582 /* set the drive mode successful, we are set! */
1583 sony_buffer_size = SONY535_BUFFER_SIZE;
1584 sony_buffer_sectors = sony_buffer_size / 2048;
1586 printk(KERN_INFO CDU535_MESSAGE_NAME " I/F CDROM : %8.8s %16.16s %4.4s",
1587 drive_config.vendor_id,
1588 drive_config.product_id,
1589 drive_config.product_rev_level);
1590 printk(" base address %03X, ", sony535_cd_base_io);
1591 if (tmp_irq > 0)
1592 printk("IRQ%d, ", tmp_irq);
1593 printk("using %d byte buffer\n", sony_buffer_size);
1595 if (register_blkdev(MAJOR_NR, CDU535_HANDLE, &cdu_fops)) {
1596 printk("Unable to get major %d for %s\n",
1597 MAJOR_NR, CDU535_MESSAGE_NAME);
1598 return -EIO;
1600 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1601 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read-ahead */
1603 sony_toc = (struct s535_sony_toc *)
1604 kmalloc(sizeof *sony_toc, GFP_KERNEL);
1605 if (sony_toc == NULL)
1606 return -ENOMEM;
1607 last_sony_subcode = (struct s535_sony_subcode *)
1608 kmalloc(sizeof *last_sony_subcode, GFP_KERNEL);
1609 if (last_sony_subcode == NULL) {
1610 kfree(sony_toc);
1611 return -ENOMEM;
1613 sony_buffer = (Byte **)
1614 kmalloc(4 * sony_buffer_sectors, GFP_KERNEL);
1615 if (sony_buffer == NULL) {
1616 kfree(sony_toc);
1617 kfree(last_sony_subcode);
1618 return -ENOMEM;
1620 for (i = 0; i < sony_buffer_sectors; i++) {
1621 sony_buffer[i] = (Byte *)kmalloc(2048, GFP_KERNEL);
1622 if (sony_buffer[i] == NULL) {
1623 while (--i>=0)
1624 kfree(sony_buffer[i]);
1625 kfree(sony_buffer);
1626 kfree(sony_toc);
1627 kfree(last_sony_subcode);
1628 return -ENOMEM;
1631 initialized = 1;
1636 if (!initialized) {
1637 printk("Did not find a " CDU535_MESSAGE_NAME " drive\n");
1638 return -EIO;
1640 request_region(sony535_cd_base_io, 4, CDU535_HANDLE);
1641 return 0;
1644 #ifndef MODULE
1646 * accept "kernel command line" parameters
1647 * (added by emoenke@gwdg.de)
1649 * use: tell LILO:
1650 * sonycd535=0x320
1652 * the address value has to be the existing CDROM port address.
1654 __initfunc(void
1655 sonycd535_setup(char *strings, int *ints))
1657 /* if IRQ change and default io base desired,
1658 * then call with io base of 0
1660 if (ints[0] > 0)
1661 if (ints[1] != 0)
1662 sony535_cd_base_io = ints[1];
1663 if (ints[0] > 1)
1664 sony535_irq_used = ints[2];
1665 if ((strings != NULL) && (*strings != '\0'))
1666 printk(CDU535_MESSAGE_NAME
1667 ": Warning: Unknown interface type: %s\n", strings);
1670 #else /* MODULE */
1672 int init_module(void)
1674 return sony535_init();
1677 void
1678 cleanup_module(void)
1680 int i;
1682 release_region(sony535_cd_base_io, 4);
1683 for (i = 0; i < sony_buffer_sectors; i++)
1684 kfree_s(sony_buffer[i], 2048);
1685 kfree_s(sony_buffer, 4 * sony_buffer_sectors);
1686 kfree_s(last_sony_subcode, sizeof *last_sony_subcode);
1687 kfree_s(sony_toc, sizeof *sony_toc);
1688 if (unregister_blkdev(MAJOR_NR, CDU535_HANDLE) == -EINVAL)
1689 printk("Uh oh, couldn't unregister " CDU535_HANDLE "\n");
1690 else
1691 printk(KERN_INFO CDU535_HANDLE " module released\n");
1693 #endif /* MODULE */