OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [cdrom/] [sonycd535.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.