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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [cdrom/] [cdu31a.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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