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/] [cm206.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* cm206.c. A linux-driver for the cm206 cdrom player with cm260 adapter card.
2
   Copyright (c) 1995, 1996 David van Leeuwen.
3
 
4
     This program is free software; you can redistribute it and/or modify
5
     it under the terms of the GNU General Public License as published by
6
     the Free Software Foundation; either version 2 of the License, or
7
     (at your option) any later version.
8
 
9
     This program is distributed in the hope that it will be useful,
10
     but WITHOUT ANY WARRANTY; without even the implied warranty of
11
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
     GNU General Public License for more details.
13
 
14
     You should have received a copy of the GNU General Public License
15
     along with this program; if not, write to the Free Software
16
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 
18
History:
19
 Started 25 jan 1994. Waiting for documentation...
20
 22 feb 1995: 0.1a first reasonably safe polling driver.
21
              Two major bugs, one in read_sector and one in
22
              do_cm206_request, happened to cancel!
23
 25 feb 1995: 0.2a first reasonable interrupt driven version of above.
24
              uart writes are still done in polling mode.
25
 25 feb 1995: 0.21a writes also in interrupt mode, still some
26
              small bugs to be found... Larger buffer.
27
  2 mrt 1995: 0.22 Bug found (cd-> nowhere, interrupt was called in
28
              initialization), read_ahead of 16. Timeouts implemented.
29
              unclear if they do something...
30
  7 mrt 1995: 0.23 Start of background read-ahead.
31
 18 mrt 1995: 0.24 Working background read-ahead. (still problems)
32
 26 mrt 1995: 0.25 Multi-session ioctl added (kernel v1.2).
33
              Statistics implemented, though separate stats206.h.
34
              Accessible trough ioctl 0x1000 (just a number).
35
              Hard to choose between v1.2 development and 1.1.75.
36
              Bottom-half doesn't work with 1.2...
37
              0.25a: fixed... typo. Still problems...
38
  1 apr 1995: 0.26 Module support added. Most bugs found. Use kernel 1.2.n.
39
  5 apr 1995: 0.27 Auto-probe for the adapter card base address.
40
              Auto-probe for the adaptor card irq line.
41
  7 apr 1995: 0.28 Added lilo setup support for base address and irq.
42
              Use major number 32 (not in this source), officially
43
              assigned to this driver.
44
  9 apr 1995: 0.29 Added very limited audio support. Toc_header, stop, pause,
45
              resume, eject. Play_track ignores track info, because we can't
46
              read a table-of-contents entry. Toc_entry is implemented
47
              as a `placebo' function: always returns start of disc.
48
  3 may 1995: 0.30 Audio support completed. The get_toc_entry function
49
              is implemented as a binary search.
50
 15 may 1995: 0.31 More work on audio stuff. Workman is not easy to
51
              satisfy; changed binary search into linear search.
52
              Auto-probe for base address somewhat relaxed.
53
  1 jun 1995: 0.32 Removed probe_irq_on/off for module version.
54
 10 jun 1995: 0.33 Workman still behaves funny, but you should be
55
              able to eject and substitute another disc.
56
 
57
 An adaptation of 0.33 is included in linux-1.3.7 by Eberhard Moenkeberg
58
 
59
 18 jul 1995: 0.34 Patch by Heiko Eissfeldt included, mainly considering
60
              verify_area's in the ioctls. Some bugs introduced by
61
              EM considering the base port and irq fixed.
62
 
63
 18 dec 1995: 0.35 Add some code for error checking... no luck...
64
 
65
 We jump to reach our goal: version 1.0 in the next stable linux kernel.
66
 
67
 19 mar 1996: 0.95 Different implementation of CDROM_GET_UPC, on
68
              request of Thomas Quinot.
69
 25 mar 1996: 0.96 Interpretation of opening with O_WRONLY or O_RDWR:
70
              open only for ioctl operation, e.g., for operation of
71
              tray etc.
72
 4 apr 1996:  0.97 First implementation of layer between VFS and cdrom
73
              driver, a generic interface. Much of the functionality
74
              of cm206_open() and cm206_ioctl() is transferred to a
75
              new file cdrom.c and its header ucdrom.h.
76
 
77
              Upgrade to Linux kernel 1.3.78.
78
 
79
 11 apr 1996  0.98 Upgrade to Linux kernel 1.3.85
80
              Made it more uniform.
81
 *
82
 * Parts of the code are based upon lmscd.c written by Kai Petzke,
83
 * sbpcd.c written by Eberhard Moenkeberg, and mcd.c by Martin
84
 * Harriss, but any off-the-shelf dynamic programming algorithm won't
85
 * be able to find them.
86
 *
87
 * The cm206 drive interface and the cm260 adapter card seem to be
88
 * sufficiently different from their cm205/cm250 counterparts
89
 * in order to write a complete new driver.
90
 *
91
 * I call all routines connected to the Linux kernel something
92
 * with `cm206' in it, as this stuff is too series-dependent.
93
 *
94
 * Currently, my limited knowledge is based on:
95
 * - The Linux Kernel Hacker's guide, v. 0.5, by Michael K. Johnson
96
 * - Linux Kernel Programmierung, by Michael Beck and others
97
 * - Philips/LMS cm206 and cm226 product specification
98
 * - Philips/LMS cm260 product specification
99
 *
100
 *                       David van Leeuwen, david@tm.tno.nl.  */
101
#define VERSION "$Id: cm206.c,v 1.1.1.1 2001-09-10 07:44:13 simons Exp $"
102
 
103
#include <linux/module.h>       
104
 
105
#include <linux/errno.h>        /* These include what we really need */
106
#include <linux/delay.h>
107
#include <linux/string.h>
108
#include <linux/sched.h>
109
#include <linux/interrupt.h>
110
#include <linux/timer.h>
111
#include <linux/cdrom.h>
112
#include <linux/ioport.h>
113
#include <linux/mm.h>
114
#include <linux/malloc.h>
115
 
116
#include <linux/ucdrom.h>
117
 
118
#include <asm/io.h>
119
 
120
#define MAJOR_NR CM206_CDROM_MAJOR
121
#include <linux/blk.h>
122
 
123
#undef DEBUG
124
#define STATISTICS              /* record times and frequencies of events */
125
#undef AUTO_PROBE_MODULE
126
#define USE_INSW
127
 
128
#include <linux/cm206.h>
129
 
130
/* This variable defines whether or not to probe for adapter base port
131
   address and interrupt request. It can be overridden by the boot
132
   parameter `auto'.
133
*/
134
static int auto_probe=1;        /* Yes, why not? */
135
 
136
static int cm206_base = CM206_BASE;
137
static int cm206_irq = CM206_IRQ;
138
 
139
#define POLLOOP 10000
140
#define READ_AHEAD 1            /* defines private buffer, waste! */
141
#define BACK_AHEAD 1            /* defines adapter-read ahead */
142
#define DATA_TIMEOUT (3*HZ)     /* measured in jiffies (10 ms) */
143
#define UART_TIMEOUT (5*HZ/100)
144
#define DSB_TIMEOUT (7*HZ)      /* time for the slowest command to finish */
145
 
146
#define LINUX_BLOCK_SIZE 512    /* WHERE is this defined? */
147
#define RAW_SECTOR_SIZE 2352    /* ok, is also defined in cdrom.h */
148
#define ISO_SECTOR_SIZE 2048
149
#define BLOCKS_ISO (ISO_SECTOR_SIZE/LINUX_BLOCK_SIZE) /* 4 */
150
#define CD_SYNC_HEAD 16         /* CD_SYNC + CD_HEAD */
151
 
152
#ifdef STATISTICS               /* keep track of errors in counters */
153
#define stats(i) { ++cd->stats[st_ ## i]; \
154
                     cd->last_stat[st_ ## i] = cd->stat_counter++; \
155
                 }
156
#else
157
#define stats(i) (void) 0
158
#endif
159
 
160
#ifdef DEBUG                    /* from lmscd.c */
161
#define debug(a) printk a
162
#else
163
#define debug(a) (void) 0
164
#endif
165
 
166
typedef unsigned char uch;      /* 8-bits */
167
typedef unsigned short ush;     /* 16-bits */
168
 
169
struct toc_struct{              /* private copy of Table of Contents */
170
  uch track, fsm[3], q0;
171
};
172
 
173
struct cm206_struct {
174
  ush intr_ds;   /* data status read on last interrupt */
175
  ush intr_ls;   /* uart line status read on last interrupt*/
176
  uch intr_ur;                  /* uart receive buffer */
177
  uch dsb, cc;   /* drive status byte and condition (error) code */
178
  uch fool;
179
  int command;                  /* command to be written to the uart */
180
  int openfiles;
181
  ush sector[READ_AHEAD*RAW_SECTOR_SIZE/2]; /* buffered cd-sector */
182
  int sector_first, sector_last;        /* range of these sector */
183
  struct wait_queue * uart;     /* wait for interrupt */
184
  struct wait_queue * data;
185
  struct timer_list timer;      /* time-out */
186
  char timed_out;
187
  signed char max_sectors;
188
  char wait_back;               /* we're waiting for a background-read */
189
  char background;              /* is a read going on in the background? */
190
  int adapter_first;            /* if so, that's the starting sector */
191
  int adapter_last;
192
  char fifo_overflowed;
193
  uch disc_status[7];           /* result of get_disc_status command */
194
#ifdef STATISTICS
195
  int stats[NR_STATS];
196
  int last_stat[NR_STATS];      /* `time' at which stat was stat */
197
  int stat_counter;
198
#endif  
199
  struct toc_struct toc[101];   /* The whole table of contents + lead-out */
200
  uch q[10];                    /* Last read q-channel info */
201
  uch audio_status[5];          /* last read position on pause */
202
  uch media_changed;            /* record if media changed */
203
};
204
 
205
#define DISC_STATUS cd->disc_status[0]
206
#define FIRST_TRACK cd->disc_status[1]
207
#define LAST_TRACK cd->disc_status[2]
208
#define PAUSED cd->audio_status[0] /* misuse this memory byte! */
209
#define PLAY_TO cd->toc[0]      /* toc[0] records end-time in play */
210
 
211
static struct cm206_struct * cd; /* the main memory structure */
212
 
213
/* First, we define some polling functions. These are actually
214
   only being used in the initialization. */
215
 
216
void send_command_polled(int command)
217
{
218
  int loop=POLLOOP;
219
  while (!(inw(r_line_status) & ls_transmitter_buffer_empty) && loop>0)
220
    --loop;
221
  outw(command, r_uart_transmit);
222
}
223
 
224
uch receive_echo_polled(void)
225
{
226
  int loop=POLLOOP;
227
  while (!(inw(r_line_status) & ls_receive_buffer_full) && loop>0) --loop;
228
  return ((uch) inw(r_uart_receive));
229
}
230
 
231
uch send_receive_polled(int command)
232
{
233
  send_command_polled(command);
234
  return receive_echo_polled();
235
}
236
 
237
/* The interrupt handler. When the cm260 generates an interrupt, very
238
   much care has to be taken in reading out the registers in the right
239
   order; in case of a receive_buffer_full interrupt, first the
240
   uart_receive must be read, and then the line status again to
241
   de-assert the interrupt line. It took me a couple of hours to find
242
   this out:-(
243
 
244
   The function reset_cm206 appears to cause an interrupt, because
245
   pulling up the INIT line clears both the uart-write-buffer /and/
246
   the uart-write-buffer-empty mask. We call this a `lost interrupt,'
247
   as there seems so reason for this to happen.
248
*/
249
 
250
static void cm206_interrupt(int sig, void *dev_id, struct pt_regs * regs)
251
/* you rang? */
252
{
253
  volatile ush fool;
254
  cd->intr_ds = inw(r_data_status); /* resets data_ready, data_error,
255
                                       crc_error, sync_error, toc_ready
256
                                       interrupts */
257
  cd->intr_ls = inw(r_line_status); /* resets overrun bit */
258
  if (cd->intr_ls & ls_attention) stats(attention);
259
  /* receive buffer full? */
260
  if (cd->intr_ls & ls_receive_buffer_full) {
261
    cd->intr_ur = inb(r_uart_receive); /* get order right! */
262
    cd->intr_ls = inw(r_line_status); /* resets rbf interrupt */
263
    if (!cd->background && cd->uart) wake_up_interruptible(&cd->uart);
264
  }
265
  /* data ready in fifo? */
266
  else if (cd->intr_ds & ds_data_ready) {
267
    if (cd->background) ++cd->adapter_last;
268
    if ((cd->wait_back || !cd->background) && cd->data)
269
      wake_up_interruptible(&cd->data);
270
    stats(data_ready);
271
  }
272
  /* ready to issue a write command? */
273
  else if (cd->command && cd->intr_ls & ls_transmitter_buffer_empty) {
274
    outw(dc_normal | (inw(r_data_status) & 0x7f), r_data_control);
275
    outw(cd->command, r_uart_transmit);
276
    cd->command=0;
277
    if (!cd->background) wake_up_interruptible(&cd->uart);
278
  }
279
  /* now treat errors (at least, identify them for debugging) */
280
  else if (cd->intr_ds & ds_fifo_overflow) {
281
    debug(("Fifo overflow at sectors 0x%x\n", cd->sector_first));
282
    fool = inw(r_fifo_output_buffer);   /* de-assert the interrupt */
283
    cd->fifo_overflowed=1;      /* signal one word less should be read */
284
    stats(fifo_overflow);
285
  }
286
  else if (cd->intr_ds & ds_data_error) {
287
    debug(("Data error at sector 0x%x\n", cd->sector_first));
288
    stats(data_error);
289
  }
290
  else if (cd->intr_ds & ds_crc_error) {
291
    debug(("CRC error at sector 0x%x\n", cd->sector_first));
292
    stats(crc_error);
293
  }
294
  else if (cd->intr_ds & ds_sync_error) {
295
    debug(("Sync at sector 0x%x\n", cd->sector_first));
296
    stats(sync_error);
297
  }
298
  else if (cd->intr_ds & ds_toc_ready) {
299
    /* do something appropriate */
300
  }
301
  /* couldn't see why this interrupt, maybe due to init */
302
  else {
303
    outw(dc_normal | READ_AHEAD, r_data_control);
304
    stats(lost_intr);
305
  }
306
  if (cd->background && (cd->adapter_last-cd->adapter_first == cd->max_sectors
307
                         || cd->fifo_overflowed))
308
    mark_bh(CM206_BH);  /* issue a stop read command */
309
  stats(interrupt);
310
}
311
 
312
/* we have put the address of the wait queue in who */
313
void cm206_timeout(unsigned long who)
314
{
315
  cd->timed_out = 1;
316
  wake_up_interruptible((struct wait_queue **) who);
317
}
318
 
319
/* This function returns 1 if a timeout occurred, 0 if an interrupt
320
   happened */
321
int sleep_or_timeout(struct wait_queue ** wait, int timeout)
322
{
323
  cd->timer.data=(unsigned long) wait;
324
  cd->timer.expires = jiffies + timeout;
325
  add_timer(&cd->timer);
326
  interruptible_sleep_on(wait);
327
  del_timer(&cd->timer);
328
  if (cd->timed_out) {
329
    cd->timed_out = 0;
330
    return 1;
331
  }
332
  else return 0;
333
}
334
 
335
void cm206_delay(int jiffies)
336
{
337
  struct wait_queue * wait = NULL;
338
  sleep_or_timeout(&wait, jiffies);
339
}
340
 
341
void send_command(int command)
342
{
343
  if (!(inw(r_line_status) & ls_transmitter_buffer_empty)) {
344
    cd->command = command;
345
    cli();                      /* don't interrupt before sleep */
346
    outw(dc_mask_sync_error | dc_no_stop_on_error |
347
         (inw(r_data_status) & 0x7f), r_data_control);
348
    /* interrupt routine sends command */
349
    if (sleep_or_timeout(&cd->uart, UART_TIMEOUT)) {
350
      debug(("Time out on write-buffer\n"));
351
      stats(write_timeout);
352
      outw(command, r_uart_transmit);
353
    }
354
  }
355
  else outw(command, r_uart_transmit);
356
}
357
 
358
uch receive_echo(void)
359
{
360
  if (!(inw(r_line_status) & ls_receive_buffer_full) &&
361
      sleep_or_timeout(&cd->uart, UART_TIMEOUT)) {
362
    debug(("Time out on receive-buffer\n"));
363
    stats(receive_timeout);
364
    return ((uch) inw(r_uart_receive));
365
  }
366
  return cd->intr_ur;
367
}
368
 
369
inline uch send_receive(int command)
370
{
371
  send_command(command);
372
  return receive_echo();
373
}
374
 
375
uch wait_dsb(void)
376
{
377
  if (!(inw(r_line_status) & ls_receive_buffer_full) &&
378
      sleep_or_timeout(&cd->uart, DSB_TIMEOUT)) {
379
    debug(("Time out on Drive Status Byte\n"));
380
    stats(dsb_timeout);
381
    return ((uch) inw(r_uart_receive));
382
  }
383
  return cd->intr_ur;
384
}
385
 
386
int type_0_command(int command, int expect_dsb)
387
{
388
  int e;
389
  if (command != (e=send_receive(command))) {
390
    debug(("command 0x%x echoed as 0x%x\n", command, e));
391
    stats(echo);
392
    return -1;
393
  }
394
  if (expect_dsb) {
395
    cd->dsb = wait_dsb();       /* wait for command to finish */
396
  }
397
  return 0;
398
}
399
 
400
int type_1_command(int command, int bytes, uch * status) /* returns info */
401
{
402
  int i;
403
  if (type_0_command(command,0)) return -1;
404
  for(i=0; i<bytes; i++)
405
    status[i] = send_receive(c_gimme);
406
  return 0;
407
}
408
 
409
/* This function resets the adapter card. We'd better not do this too */
410
/* often, because it tends to generate `lost interrupts.' */
411
void reset_cm260(void)
412
{
413
  outw(dc_normal | dc_initialize | READ_AHEAD, r_data_control);
414
  udelay(10);                   /* 3.3 mu sec minimum */
415
  outw(dc_normal | READ_AHEAD, r_data_control);
416
}
417
 
418
/* fsm: frame-sec-min from linear address */
419
void fsm(int lba, uch * fsm)
420
{
421
  fsm[0] = lba % 75;
422
  lba /= 75; lba += 2;
423
  fsm[1] = lba % 60; fsm[2] = lba / 60;
424
}
425
 
426
inline int fsm2lba(uch * fsm)
427
{
428
  return fsm[0] + 75*(fsm[1]-2 + 60*fsm[2]);
429
}
430
 
431
inline int f_s_m2lba(uch f, uch s, uch m)
432
{
433
  return f + 75*(s-2 + 60*m);
434
}
435
 
436
int start_read(int start)
437
{
438
  uch read_sector[4] = {c_read_data, };
439
  int i, e;
440
 
441
  fsm(start, &read_sector[1]);
442
  for (i=0; i<4; i++)
443
    if (read_sector[i] != (e=send_receive(read_sector[i]))) {
444
      debug(("read_sector: %x echoes %x\n", read_sector[i], e));
445
      stats(echo);
446
      return -1;
447
    }
448
  return 0;
449
}
450
 
451
int stop_read(void)
452
{
453
  type_0_command(c_stop,0);
454
  if(receive_echo() != 0xff) {
455
    debug(("c_stop didn't send 0xff\n"));
456
    stats(stop_0xff);
457
    return -1;
458
  }
459
  return 0;
460
}
461
 
462
/* This function starts to read sectors in adapter memory, the
463
   interrupt routine should stop the read. In fact, the bottom_half
464
   routine takes care of this. Set a flag `background' in the cd
465
   struct to indicate the process. */
466
 
467
int read_background(int start, int reading)
468
{
469
  if (cd->background) return -1; /* can't do twice */
470
  outw(dc_normal | BACK_AHEAD, r_data_control);
471
  if (!reading && start_read(start)) return -2;
472
  cd->adapter_first = cd->adapter_last = start;
473
  cd->background = 1;           /* flag a read is going on */
474
  return 0;
475
}
476
 
477
#ifdef USE_INSW
478
#define transport_data insw
479
#else
480
/* this routine implements insw(,,). There was a time i had the
481
   impression that there would be any difference in error-behaviour. */
482
void transport_data(int port, ush * dest, int count)
483
{
484
  int i;
485
  ush * d;
486
  for (i=0, d=dest; i<count; i++, d++)
487
    *d = inw(port);
488
}
489
#endif
490
 
491
int read_sector(int start)
492
{
493
  if (cd->background) {
494
    cd->background=0;
495
    cd->adapter_last = -1;      /* invalidate adapter memory */
496
    stop_read();
497
  }
498
  cd->fifo_overflowed=0;
499
  reset_cm260();                /* empty fifo etc. */
500
  if (start_read(start)) return -1;
501
  if (sleep_or_timeout(&cd->data, DATA_TIMEOUT)) {
502
    debug(("Read timed out sector 0x%x\n", start));
503
    stats(read_timeout);
504
    stop_read();
505
    return -3;
506
  }
507
  transport_data(r_fifo_output_buffer, cd->sector,
508
                 READ_AHEAD*RAW_SECTOR_SIZE/2);
509
  if (read_background(start+READ_AHEAD,1)) stats(read_background);
510
  cd->sector_first = start; cd->sector_last = start+READ_AHEAD;
511
  stats(read_restarted);
512
  return 0;
513
}
514
 
515
/* The function of bottom-half is to send a stop command to the drive
516
   This isn't easy because the routine is not `owned' by any process;
517
   we can't go to sleep! The variable cd->background gives the status:
518
 
519
   1 a read is pending
520
   2 c_stop waits for write_buffer_empty
521
   3 c_stop waits for receive_buffer_full: echo
522
   4 c_stop waits for receive_buffer_full: 0xff
523
*/
524
 
525
void cm206_bh(void)
526
{
527
  debug(("bh: %d\n", cd->background));
528
  switch (cd->background) {
529
  case 1:
530
    stats(bh);
531
    if (!(cd->intr_ls & ls_transmitter_buffer_empty)) {
532
      cd->command = c_stop;
533
      outw(dc_mask_sync_error | dc_no_stop_on_error |
534
           (inw(r_data_status) & 0x7f), r_data_control);
535
      cd->background=2;
536
      break;                    /* we'd better not time-out here! */
537
    }
538
    else outw(c_stop, r_uart_transmit);
539
    /* fall into case 2: */
540
  case 2:
541
    /* the write has been satisfied by interrupt routine */
542
    cd->background=3;
543
    break;
544
  case 3:
545
    if (cd->intr_ur != c_stop) {
546
      debug(("cm206_bh: c_stop echoed 0x%x\n", cd->intr_ur));
547
      stats(echo);
548
    }
549
    cd->background++;
550
    break;
551
  case 4:
552
    if (cd->intr_ur != 0xff) {
553
      debug(("cm206_bh: c_stop reacted with 0x%x\n", cd->intr_ur));
554
      stats(stop_0xff);
555
    }
556
    cd->background=0;
557
  }
558
}
559
 
560
/* This command clears the dsb_possible_media_change flag, so we must
561
 * retain it.
562
 */
563
void get_drive_status(void)
564
{
565
  uch status[2];
566
  type_1_command(c_drive_status, 2, status); /* this might be done faster */
567
  cd->dsb=status[0];
568
  cd->cc=status[1];
569
  cd->media_changed |=
570
    !!(cd->dsb & (dsb_possible_media_change |
571
                  dsb_drive_not_ready | dsb_tray_not_closed));
572
}
573
 
574
void get_disc_status(void)
575
{
576
  if (type_1_command(c_disc_status, 7, cd->disc_status)) {
577
    debug(("get_disc_status: error\n"));
578
  }
579
}
580
 
581
/* The new open. The real opening strategy is defined in cdrom.c. */
582
 
583
static int cm206_open(kdev_t dev, int purpose)
584
{
585
  if (!cd->openfiles) {         /* reset only first time */
586
    cd->background=0;
587
    reset_cm260();
588
    cd->adapter_last = -1;      /* invalidate adapter memory */
589
    cd->sector_last = -1;
590
  }
591
  ++cd->openfiles; MOD_INC_USE_COUNT;
592
  stats(open);
593
  return 0;
594
}
595
 
596
static void cm206_release(kdev_t dev)
597
{
598
  if (cd->openfiles==1) {
599
    if (cd->background) {
600
      cd->background=0;
601
      stop_read();
602
    }
603
    cd->sector_last = -1;       /* Make our internal buffer invalid */
604
    FIRST_TRACK = 0;             /* No valid disc status */
605
  }
606
  --cd->openfiles; MOD_DEC_USE_COUNT;
607
}
608
 
609
/* Empty buffer empties $sectors$ sectors of the adapter card buffer,
610
 * and then reads a sector in kernel memory.  */
611
void empty_buffer(int sectors)
612
{
613
  while (sectors>=0) {
614
    transport_data(r_fifo_output_buffer, cd->sector + cd->fifo_overflowed,
615
         RAW_SECTOR_SIZE/2 - cd->fifo_overflowed);
616
    --sectors;
617
    ++cd->adapter_first;        /* update the current adapter sector */
618
    cd->fifo_overflowed=0;       /* reset overflow bit */
619
    stats(sector_transferred);
620
  }
621
  cd->sector_first=cd->adapter_first-1;
622
  cd->sector_last=cd->adapter_first; /* update the buffer sector */
623
}
624
 
625
/* try_adapter. This function determines if the requested sector is
626
   in adapter memory, or will appear there soon. Returns 0 upon
627
   success */
628
int try_adapter(int sector)
629
{
630
  if (cd->adapter_first <= sector && sector < cd->adapter_last) {
631
    /* sector is in adapter memory */
632
    empty_buffer(sector - cd->adapter_first);
633
    return 0;
634
  }
635
  else if (cd->background==1 && cd->adapter_first <= sector
636
           && sector < cd->adapter_first+cd->max_sectors) {
637
    /* a read is going on, we can wait for it */
638
    cd->wait_back=1;
639
    while (sector >= cd->adapter_last) {
640
      if (sleep_or_timeout(&cd->data, DATA_TIMEOUT)) {
641
        debug(("Timed out during background wait: %d %d %d %d\n", sector,
642
               cd->adapter_last, cd->adapter_first, cd->background));
643
        stats(back_read_timeout);
644
        cd->wait_back=0;
645
        return -1;
646
      }
647
    }
648
    cd->wait_back=0;
649
    empty_buffer(sector - cd->adapter_first);
650
    return 0;
651
  }
652
  else return -2;
653
}
654
 
655
/* This is not a very smart implementation. We could optimize for
656
   consecutive block numbers. I'm not convinced this would really
657
   bring down the processor load. */
658
static void do_cm206_request(void)
659
{
660
  long int i, cd_sec_no;
661
  int quarter, error;
662
  uch * source, * dest;
663
 
664
  while(1) {     /* repeat until all requests have been satisfied */
665
    INIT_REQUEST;
666
    if (CURRENT == NULL || CURRENT->rq_status == RQ_INACTIVE)
667
      return;
668
    if (CURRENT->cmd != READ) {
669
      debug(("Non-read command %d on cdrom\n", CURRENT->cmd));
670
      end_request(0);
671
      continue;
672
    }
673
    error=0;
674
    for (i=0; i<CURRENT->nr_sectors; i++) {
675
      cd_sec_no = (CURRENT->sector+i)/BLOCKS_ISO; /* 4 times 512 bytes */
676
      quarter = (CURRENT->sector+i) % BLOCKS_ISO;
677
      dest = CURRENT->buffer + i*LINUX_BLOCK_SIZE;
678
      /* is already in buffer memory? */
679
      if (cd->sector_first <= cd_sec_no && cd_sec_no < cd->sector_last) {
680
        source = ((uch *) cd->sector) + 16 + quarter*LINUX_BLOCK_SIZE
681
          + (cd_sec_no-cd->sector_first)*RAW_SECTOR_SIZE;
682
        memcpy(dest, source, LINUX_BLOCK_SIZE);
683
      }
684
      else if (!try_adapter(cd_sec_no) || !read_sector(cd_sec_no)) {
685
        source =  ((uch *) cd->sector)+16+quarter*LINUX_BLOCK_SIZE;
686
        memcpy(dest, source, LINUX_BLOCK_SIZE);
687
      }
688
      else {
689
        error=1;
690
      }
691
    }
692
    end_request(!error);
693
  }
694
}
695
 
696
/* Audio support. I've tried very hard, but the cm206 drive doesn't
697
   seem to have a get_toc (table-of-contents) function, while i'm
698
   pretty sure it must read the toc upon disc insertion. Therefore
699
   this function has been implemented through a binary search
700
   strategy. All track starts that happen to be found are stored in
701
   cd->toc[], for future use.
702
 
703
   I've spent a whole day on a bug that only shows under Workman---
704
   I don't get it. Tried everything, nothing works. If workman asks
705
   for track# 0xaa, it'll get the wrong time back. Any other program
706
   receives the correct value. I'm stymied.
707
*/
708
 
709
/* seek seeks to address lba. It does wait to arrive there. */
710
void seek(int lba)
711
{
712
  int i;
713
  uch seek_command[4]={c_seek, };
714
 
715
  fsm(lba, &seek_command[1]);
716
  for (i=0; i<4; i++) type_0_command(seek_command[i], 0);
717
  cd->dsb = wait_dsb();
718
}
719
 
720
uch bcdbin(unsigned char bcd)   /* stolen from mcd.c! */
721
{
722
  return (bcd >> 4)*10 + (bcd & 0xf);
723
}
724
 
725
inline uch normalize_track(uch track)
726
{
727
  if (track<1) return 1;
728
  if (track>LAST_TRACK) return LAST_TRACK+1;
729
  return track;
730
}
731
 
732
/* This function does a binary search for track start. It records all
733
 * tracks seen in the process. Input $track$ must be between 1 and
734
 * #-of-tracks+1 */
735
int get_toc_lba(uch track)
736
{
737
  int max=74*60*75-150, min=0;
738
  int i, lba, l, old_lba=0;
739
  uch * q = cd->q;
740
  uch ct;                       /* current track */
741
  int binary=0;
742
  const int skip = 3*60*75;
743
 
744
  for (i=track; i>0; i--) if (cd->toc[i].track) {
745
    min = fsm2lba(cd->toc[i].fsm);
746
    break;
747
  }
748
  lba = min + skip;             /* 3 minutes */
749
  do {
750
    seek(lba);
751
    type_1_command(c_read_current_q, 10, q);
752
    ct = normalize_track(q[1]);
753
    if (!cd->toc[ct].track) {
754
      l = q[9]-bcdbin(q[5]) + 75*(q[8]-bcdbin(q[4])-2 +
755
                                  60*(q[7]-bcdbin(q[3])));
756
      cd->toc[ct].track=q[1];   /* lead out still 0xaa */
757
      fsm(l, cd->toc[ct].fsm);
758
      cd->toc[ct].q0 = q[0];     /* contains adr and ctrl info */
759
      if (ct==track) return l;
760
    }
761
    old_lba=lba;
762
    if (binary) {
763
      if (ct < track) min = lba; else max = lba;
764
      lba = (min+max)/2;
765
    } else {
766
      if(ct < track) lba += skip;
767
      else {
768
        binary=1;
769
        max = lba; min = lba - skip;
770
        lba = (min+max)/2;
771
      }
772
    }
773
  } while (lba!=old_lba);
774
  return lba;
775
}
776
 
777
void update_toc_entry(uch track)
778
{
779
  track = normalize_track(track);
780
  if (!cd->toc[track].track) get_toc_lba(track);
781
}
782
 
783
/* return 0 upon success */
784
int read_toc_header(struct cdrom_tochdr * hp)
785
{
786
  if (!FIRST_TRACK) get_disc_status();
787
  if (hp && DISC_STATUS & cds_all_audio) { /* all audio */
788
    int i;
789
    hp->cdth_trk0 = FIRST_TRACK;
790
    hp->cdth_trk1 = LAST_TRACK;
791
    cd->toc[1].track=1;         /* fill in first track position */
792
    for (i=0; i<3; i++) cd->toc[1].fsm[i] = cd->disc_status[3+i];
793
    update_toc_entry(LAST_TRACK+1);             /* find most entries */
794
    return 0;
795
  }
796
  return -1;
797
}
798
 
799
void play_from_to_msf(struct cdrom_msf* msfp)
800
{
801
  uch play_command[] = {c_play,
802
           msfp->cdmsf_frame0, msfp->cdmsf_sec0, msfp->cdmsf_min0,
803
           msfp->cdmsf_frame1, msfp->cdmsf_sec1, msfp->cdmsf_min1, 2, 2};
804
  int i;
805
  for (i=0; i<9; i++) type_0_command(play_command[i], 0);
806
  for (i=0; i<3; i++)
807
    PLAY_TO.fsm[i] = play_command[i+4];
808
  PLAY_TO.track = 0;             /* say no track end */
809
  cd->dsb = wait_dsb();
810
}
811
 
812
void play_from_to_track(int from, int to)
813
{
814
  uch play_command[8] = {c_play, };
815
  int i;
816
 
817
  if (from==0) {         /* continue paused play */
818
    for (i=0; i<3; i++) {
819
      play_command[i+1] = cd->audio_status[i+2];
820
      play_command[i+4] = PLAY_TO.fsm[i];
821
    }
822
  } else {
823
    update_toc_entry(from); update_toc_entry(to+1);
824
    for (i=0; i<3; i++) {
825
      play_command[i+1] = cd->toc[from].fsm[i];
826
      PLAY_TO.fsm[i] = play_command[i+4] = cd->toc[to+1].fsm[i];
827
    }
828
    PLAY_TO.track = to;
829
  }
830
  for (i=0; i<7; i++) type_0_command(play_command[i],0);
831
  for (i=0; i<2; i++) type_0_command(0x2, 0); /* volume */
832
  cd->dsb = wait_dsb();
833
}
834
 
835
int get_current_q(struct cdrom_subchnl * qp)
836
{
837
  int i;
838
  uch * q = cd->q;
839
  if (type_1_command(c_read_current_q, 10, q)) return 0;
840
/*  q[0] = bcdbin(q[0]); Don't think so! */
841
  for (i=2; i<6; i++) q[i]=bcdbin(q[i]);
842
  qp->cdsc_adr = q[0] & 0xf; qp->cdsc_ctrl = q[0] >> 4;   /* from mcd.c */
843
  qp->cdsc_trk = q[1];  qp->cdsc_ind = q[2];
844
  if (qp->cdsc_format == CDROM_MSF) {
845
    qp->cdsc_reladdr.msf.minute = q[3];
846
    qp->cdsc_reladdr.msf.second = q[4];
847
    qp->cdsc_reladdr.msf.frame = q[5];
848
    qp->cdsc_absaddr.msf.minute = q[7];
849
    qp->cdsc_absaddr.msf.second = q[8];
850
    qp->cdsc_absaddr.msf.frame = q[9];
851
  } else {
852
    qp->cdsc_reladdr.lba = f_s_m2lba(q[5], q[4], q[3]);
853
    qp->cdsc_absaddr.lba = f_s_m2lba(q[9], q[8], q[7]);
854
  }
855
  get_drive_status();
856
  if (cd->dsb & dsb_play_in_progress)
857
    qp->cdsc_audiostatus = CDROM_AUDIO_PLAY ;
858
  else if (PAUSED)
859
    qp->cdsc_audiostatus = CDROM_AUDIO_PAUSED;
860
  else qp->cdsc_audiostatus = CDROM_AUDIO_NO_STATUS;
861
  return 0;
862
}
863
 
864
void invalidate_toc(void)
865
{
866
  memset(cd->toc, 0, sizeof(cd->toc));
867
  memset(cd->disc_status, 0, sizeof(cd->disc_status));
868
}
869
 
870
/* cdrom.c guarantees that cdte_format == CDROM_MSF */
871
void get_toc_entry(struct cdrom_tocentry * ep)
872
{
873
  uch track = normalize_track(ep->cdte_track);
874
  update_toc_entry(track);
875
  ep->cdte_addr.msf.frame = cd->toc[track].fsm[0];
876
  ep->cdte_addr.msf.second = cd->toc[track].fsm[1];
877
  ep->cdte_addr.msf.minute = cd->toc[track].fsm[2];
878
  ep->cdte_adr = cd->toc[track].q0 & 0xf;
879
  ep->cdte_ctrl = cd->toc[track].q0 >> 4;
880
  ep->cdte_datamode=0;
881
}
882
 
883
/* Audio ioctl.  Ioctl commands connected to audio are in such an
884
 * idiosyncratic i/o format, that we leave these untouched. Return 0
885
 * upon success. Memory checking has been done by cdrom_ioctl(), the
886
 * calling function, as well as LBA/MSF sanitization.
887
*/
888
int cm206_audio_ioctl(kdev_t dev, unsigned int cmd, void * arg)
889
{
890
  switch (cmd) {
891
  case CDROMREADTOCHDR:
892
    return read_toc_header((struct cdrom_tochdr *) arg);
893
  case CDROMREADTOCENTRY:
894
    get_toc_entry((struct cdrom_tocentry *) arg);
895
    return 0;
896
  case CDROMPLAYMSF:
897
    play_from_to_msf((struct cdrom_msf *) arg);
898
    return 0;
899
  case CDROMPLAYTRKIND:         /* admittedly, not particularly beautiful */
900
    play_from_to_track(((struct cdrom_ti *)arg)->cdti_trk0,
901
                       ((struct cdrom_ti *)arg)->cdti_trk1);
902
    return 0;
903
  case CDROMSTOP:
904
    PAUSED=0;
905
    if (cd->dsb & dsb_play_in_progress) return type_0_command(c_stop, 1);
906
    else return 0;
907
  case CDROMPAUSE:
908
    get_drive_status();
909
    if (cd->dsb & dsb_play_in_progress) {
910
      type_0_command(c_stop, 1);
911
      type_1_command(c_audio_status, 5, cd->audio_status);
912
      PAUSED=1; /* say we're paused */
913
    }
914
    return 0;
915
  case CDROMRESUME:
916
    if (PAUSED) play_from_to_track(0,0);
917
    PAUSED=0;
918
    return 0;
919
  case CDROMSTART:
920
  case CDROMVOLCTRL:
921
    return 0;
922
  case CDROMSUBCHNL:
923
    return get_current_q((struct cdrom_subchnl *)arg);
924
  default:
925
    return -EINVAL;
926
  }
927
}
928
 
929
/* Ioctl. These ioctls are specific to the cm206 driver. I have made
930
   some driver statistics accessible through ioctl calls.
931
 */
932
 
933
static int cm206_ioctl(kdev_t dev, unsigned int cmd, unsigned long arg)
934
{
935
  switch (cmd) {
936
#ifdef STATISTICS
937
  case CM206CTL_GET_STAT:
938
    if (arg >= NR_STATS) return -EINVAL;
939
    else return cd->stats[arg];
940
  case CM206CTL_GET_LAST_STAT:
941
    if (arg >= NR_STATS) return -EINVAL;
942
    else return cd->last_stat[arg];
943
#endif    
944
  default:
945
    debug(("Unknown ioctl call 0x%x\n", cmd));
946
    return -EINVAL;
947
  }
948
}
949
 
950
int cm206_media_changed(kdev_t dev)
951
{
952
  if (cd != NULL) {
953
    int r;
954
    get_drive_status();         /* ensure cd->media_changed OK */
955
    r = cd->media_changed;
956
    cd->media_changed = 0;       /* clear bit */
957
    return r;
958
  }
959
  else return -EIO;
960
}
961
 
962
/* The new generic cdrom support. Routines should be concise, most of
963
   the logic should be in cdrom.c */
964
 
965
/* returns number of times device is in use */
966
int cm206_open_files(kdev_t dev)
967
{
968
  if (cd) return cd->openfiles;
969
  return -1;
970
}
971
 
972
/* controls tray movement */
973
int cm206_tray_move(kdev_t dev, int position)
974
{
975
  if (position) {               /* 1: eject */
976
    type_0_command(c_open_tray,1);
977
    invalidate_toc();
978
  }
979
  else type_0_command(c_close_tray, 1); /* 0: close */
980
  return 0;
981
}
982
 
983
/* gives current state of the drive */
984
int cm206_drive_status(kdev_t dev)
985
{
986
  get_drive_status();
987
  if (cd->dsb & dsb_tray_not_closed) return CDS_TRAY_OPEN;
988
  if (!(cd->dsb & dsb_disc_present)) return CDS_NO_DISC;
989
  if (cd->dsb & dsb_drive_not_ready) return CDS_DRIVE_NOT_READY;
990
  return CDS_DISC_OK;
991
}
992
 
993
/* gives current state of disc in drive */
994
int cm206_disc_status(kdev_t dev)
995
{
996
  uch xa;
997
  get_drive_status();
998
  if ((cd->dsb & dsb_not_useful) | !(cd->dsb & dsb_disc_present))
999
    return CDS_NO_DISC;
1000
  get_disc_status();
1001
  if (DISC_STATUS & cds_all_audio) return CDS_AUDIO;
1002
  xa = DISC_STATUS >> 4;
1003
  switch (xa) {
1004
  case 0: return CDS_DATA_1;     /* can we detect CDS_DATA_2? */
1005
  case 1: return CDS_XA_2_1;    /* untested */
1006
  case 2: return CDS_XA_2_2;
1007
  }
1008
  return 0;
1009
}
1010
 
1011
/* locks or unlocks door lock==1: lock; return 0 upon success */
1012
int cm206_lock_door(kdev_t dev, int lock)
1013
{
1014
  uch command = (lock) ? c_lock_tray : c_unlock_tray;
1015
  type_0_command(command, 1);   /* wait and get dsb */
1016
  /* the logic calculates the success, 0 means successful */
1017
  return lock ^ ((cd->dsb & dsb_tray_locked) != 0);
1018
}
1019
 
1020
/* Although a session start should be in LBA format, we return it in
1021
   MSF format because it is slightly easier, and the new generic ioctl
1022
   will take care of the necessary conversion. */
1023
int cm206_get_last_session(kdev_t dev, struct cdrom_multisession * mssp)
1024
{
1025
  if (!FIRST_TRACK) get_disc_status();
1026
  if (mssp != NULL) {
1027
    if (DISC_STATUS & cds_multi_session) { /* multi-session */
1028
      mssp->addr.msf.frame = cd->disc_status[3];
1029
      mssp->addr.msf.second = cd->disc_status[4];
1030
      mssp->addr.msf.minute = cd->disc_status[5];
1031
      mssp->addr_format = CDROM_MSF;
1032
      mssp->xa_flag = 1;
1033
    } else {
1034
      mssp->xa_flag = 0;
1035
    }
1036
    return 1;
1037
  }
1038
  return 0;
1039
}
1040
 
1041
int cm206_get_upc(kdev_t dev, struct cdrom_mcn * mcn)
1042
{
1043
  uch upc[10];
1044
  char * ret = mcn->medium_catalog_number;
1045
  int i;
1046
 
1047
  if (type_1_command(c_read_upc, 10, upc)) return -EIO;
1048
  for (i=0; i<13; i++) {
1049
    int w=i/2+1, r=i%2;
1050
    if (r) ret[i] = 0x30 | (upc[w] & 0x0f);
1051
    else ret[i] = 0x30 | ((upc[w] >> 4) & 0x0f);
1052
  }
1053
  ret[13] = '\0';
1054
  return 0;
1055
}
1056
 
1057
int cm206_reset(kdev_t dev)
1058
{
1059
  stop_read();
1060
  reset_cm260();
1061
  outw(dc_normal | dc_break | READ_AHEAD, r_data_control);
1062
  udelay(1000);                 /* 750 musec minimum */
1063
  outw(dc_normal | READ_AHEAD, r_data_control);
1064
  cd->sector_last = -1;         /* flag no data buffered */
1065
  cd->adapter_last = -1;
1066
  invalidate_toc();
1067
  return 0;
1068
}
1069
 
1070
static struct cdrom_device_ops cm206_dops = {
1071
  cm206_open,                   /* open */
1072
  cm206_release,                /* release */
1073
  cm206_open_files,             /* number of open_files */
1074
  cm206_drive_status,           /* drive status */
1075
  cm206_disc_status,            /* disc status */
1076
  cm206_media_changed,          /* media changed */
1077
  cm206_tray_move,              /* tray move */
1078
  cm206_lock_door,              /* lock door */
1079
  NULL,                         /* select speed */
1080
  NULL,                         /* select disc */
1081
  cm206_get_last_session,       /* get last session */
1082
  cm206_get_upc,                /* get universal product code */
1083
  cm206_reset,                  /* hard reset */
1084
  cm206_audio_ioctl,            /* audio ioctl */
1085
  cm206_ioctl,                  /* device-specific ioctl */
1086
  CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_MULTI_SESSION |
1087
    CDC_MEDIA_CHANGED | CDC_MCN | CDC_PLAY_AUDIO, /* capability */
1088
  0,                             /* mask flags */
1089
  2,                            /* maximum speed */
1090
  1,                            /* number of minor devices */
1091
  1,                            /* number of discs */
1092
  0,                             /* options, ignored */
1093
 
1094
};
1095
 
1096
/* This routine gets called during init if thing go wrong, can be used
1097
 * in cleanup_module as well. */
1098
void cleanup(int level)
1099
{
1100
  switch (level) {
1101
  case 4:
1102
    if (unregister_cdrom(MAJOR_NR, "cm206")) {
1103
      printk("Can't unregister cdrom cm206\n");
1104
      return;
1105
    }
1106
    if (unregister_blkdev(MAJOR_NR, "cm206")) {
1107
      printk("Can't unregister major cm206\n");
1108
      return;
1109
    }
1110
  case 3:
1111
    free_irq(cm206_irq, NULL);
1112
  case 2:
1113
  case 1:
1114
    kfree(cd);
1115
    release_region(cm206_base, 16);
1116
  default:
1117
  }
1118
}
1119
 
1120
/* This function probes for the adapter card. It returns the base
1121
   address if it has found the adapter card. One can specify a base
1122
   port to probe specifically, or 0 which means span all possible
1123
   bases.
1124
 
1125
   Linus says it is too dangerous to use writes for probing, so we
1126
   stick with pure reads for a while. Hope that 8 possible ranges,
1127
   check_region, 15 bits of one port and 6 of another make things
1128
   likely enough to accept the region on the first hit...
1129
 */
1130
int probe_base_port(int base)
1131
{
1132
  int b=0x300, e=0x370;         /* this is the range of start addresses */
1133
  volatile int fool, i;
1134
 
1135
  if (base) b=e=base;
1136
  for (base=b; base<=e; base += 0x10) {
1137
    if (check_region(base, 0x10)) continue;
1138
    for (i=0; i<3; i++)
1139
      fool = inw(base+2); /* empty possibly uart_receive_buffer */
1140
    if((inw(base+6) & 0xffef) != 0x0001 || /* line_status */
1141
       (inw(base) & 0xad00) != 0) /* data status */
1142
      continue;
1143
    return(base);
1144
  }
1145
  return 0;
1146
}
1147
 
1148
#if !defined(MODULE) || defined(AUTO_PROBE_MODULE)
1149
/* Probe for irq# nr. If nr==0, probe for all possible irq's. */
1150
int probe_irq(int nr) {
1151
  int irqs, irq;
1152
  outw(dc_normal | READ_AHEAD, r_data_control); /* disable irq-generation */
1153
  sti();
1154
  irqs = probe_irq_on();
1155
  reset_cm260();                /* causes interrupt */
1156
  udelay(100);                  /* wait for it */
1157
  irq = probe_irq_off(irqs);
1158
  outw(dc_normal | READ_AHEAD, r_data_control); /* services interrupt */
1159
  if (nr && irq!=nr && irq>0) return 0;   /* wrong interrupt happened */
1160
  else return irq;
1161
}
1162
#endif
1163
 
1164
int cm206_init(void)
1165
{
1166
  uch e=0;
1167
  long int size=sizeof(struct cm206_struct);
1168
 
1169
  printk(KERN_INFO VERSION);
1170
  cm206_base = probe_base_port(auto_probe ? 0 : cm206_base);
1171
  if (!cm206_base) {
1172
    printk(" can't find adapter!\n");
1173
    return -EIO;
1174
  }
1175
  printk(" adapter at 0x%x", cm206_base);
1176
  request_region(cm206_base, 16, "cm206");
1177
  cd = (struct cm206_struct *) kmalloc(size, GFP_KERNEL);
1178
  if (!cd) return -EIO;
1179
  /* Now we have found the adaptor card, try to reset it. As we have
1180
   * found out earlier, this process generates an interrupt as well,
1181
   * so we might just exploit that fact for irq probing! */
1182
#if !defined(MODULE) || defined(AUTO_PROBE_MODULE)
1183
  cm206_irq = probe_irq(auto_probe ? 0 : cm206_irq);
1184
  if (cm206_irq<=0) {
1185
    printk("can't find IRQ!\n");
1186
    cleanup(1);
1187
    return -EIO;
1188
  }
1189
  else printk(" IRQ %d found\n", cm206_irq);
1190
#else
1191
  cli();
1192
  reset_cm260();
1193
  /* Now, the problem here is that reset_cm260 can generate an
1194
     interrupt. It seems that this can cause a kernel oops some time
1195
     later. So we wait a while and `service' this interrupt. */
1196
  udelay(10);
1197
  outw(dc_normal | READ_AHEAD, r_data_control);
1198
  sti();
1199
  printk(" using IRQ %d\n", cm206_irq);
1200
#endif
1201
  if (send_receive_polled(c_drive_configuration) != c_drive_configuration)
1202
    {
1203
      printk(" drive not there\n");
1204
      cleanup(1);
1205
      return -EIO;
1206
    }
1207
  e = send_receive_polled(c_gimme);
1208
  printk(KERN_INFO "Firmware revision %d", e & dcf_revision_code);
1209
  if (e & dcf_transfer_rate) printk(" double");
1210
  else printk(" single");
1211
  printk(" speed drive");
1212
  if (e & dcf_motorized_tray) printk(", motorized tray");
1213
  if (request_irq(cm206_irq, cm206_interrupt, 0, "cm206", NULL)) {
1214
    printk("\nUnable to reserve IRQ---aborted\n");
1215
    cleanup(2);
1216
    return -EIO;
1217
  }
1218
  printk(".\n");
1219
  if (register_blkdev(MAJOR_NR, "cm206", &cdrom_fops) != 0) {
1220
    printk("Cannot register for major %d!\n", MAJOR_NR);
1221
    cleanup(3);
1222
    return -EIO;
1223
  }
1224
  if (register_cdrom(MAJOR_NR, "cm206", &cm206_dops) != 0) {
1225
    printk("Cannot register for cdrom %d!\n", MAJOR_NR);
1226
    cleanup(3);
1227
    return -EIO;
1228
  }
1229
  blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1230
  read_ahead[MAJOR_NR] = 16;    /* reads ahead what? */
1231
  init_bh(CM206_BH, cm206_bh);
1232
 
1233
  memset(cd, 0, sizeof(*cd));    /* give'm some reasonable value */
1234
  cd->sector_last = -1;         /* flag no data buffered */
1235
  cd->adapter_last = -1;
1236
  cd->timer.function = cm206_timeout;
1237
  cd->max_sectors = (inw(r_data_status) & ds_ram_size) ? 24 : 97;
1238
  printk(KERN_INFO "%d kB adapter memory available, "
1239
         " %ld bytes kernel memory used.\n", cd->max_sectors*2, size);
1240
  return 0;
1241
}
1242
 
1243
#ifdef MODULE
1244
 
1245
static int cm206[2] = {0,0};      /* for compatible `insmod' parameter passing */
1246
 
1247
void parse_options(void)
1248
{
1249
  int i;
1250
  for (i=0; i<2; i++) {
1251
    if (0x300 <= cm206[i] && i<= 0x370 && cm206[i] % 0x10 == 0) {
1252
      cm206_base = cm206[i];
1253
      auto_probe=0;
1254
    }
1255
    else if (3 <= cm206[i] && cm206[i] <= 15) {
1256
      cm206_irq = cm206[i];
1257
      auto_probe=0;
1258
    }
1259
  }
1260
}
1261
 
1262
int init_module(void)
1263
{
1264
        parse_options();
1265
#if !defined(AUTO_PROBE_MODULE)
1266
        auto_probe=0;
1267
#endif
1268
        return cm206_init();
1269
}
1270
 
1271
void cleanup_module(void)
1272
{
1273
  cleanup(4);
1274
  printk(KERN_INFO "cm206 removed\n");
1275
}
1276
 
1277
#else /* !MODULE */
1278
 
1279
/* This setup function accepts either `auto' or numbers in the range
1280
 * 3--11 (for irq) or 0x300--0x370 (for base port) or both. */
1281
void cm206_setup(char *s, int *p)
1282
{
1283
  int i;
1284
  if (!strcmp(s, "auto")) auto_probe=1;
1285
  for(i=1; i<=p[0]; i++) {
1286
    if (0x300 <= p[i] && i<= 0x370 && p[i] % 0x10 == 0) {
1287
      cm206_base = p[i];
1288
      auto_probe = 0;
1289
    }
1290
    else if (3 <= p[i] && p[i] <= 15) {
1291
      cm206_irq = p[i];
1292
      auto_probe = 0;
1293
    }
1294
  }
1295
}
1296
#endif /* MODULE */
1297
/*
1298
 * Local variables:
1299
 * compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/include/linux -Wall -Wstrict-prototypes -O2 -m486 -c cm206.c -o cm206.o"
1300
 * End:
1301
 */

powered by: WebSVN 2.1.0

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