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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [scsi/] [sr.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  sr.c Copyright (C) 1992 David Giller
3
 *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4
 *
5
 *  adapted from:
6
 *      sd.c Copyright (C) 1992 Drew Eckhardt
7
 *      Linux scsi disk driver by
8
 *              Drew Eckhardt <drew@colorado.edu>
9
 *
10
 *      Modified by Eric Youngdale ericy@andante.org to
11
 *      add scatter-gather, multiple outstanding request, and other
12
 *      enhancements.
13
 *
14
 *          Modified by Eric Youngdale eric@andante.org to support loadable
15
 *          low-level scsi drivers.
16
 *
17
 *       Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
18
 *       provide auto-eject.
19
 *
20
 *          Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
21
 *          generic cdrom interface
22
 *
23
 *       Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
24
 *       interface, capabilities probe additions, ioctl cleanups, etc.
25
 *
26
 *       Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
27
 *
28
 *       Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
29
 *       transparently and loose the GHOST hack
30
 *
31
 *       Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
32
 *       check resource allocation in sr_init and some cleanups
33
 *
34
 */
35
 
36
#include <linux/module.h>
37
 
38
#include <linux/fs.h>
39
#include <linux/kernel.h>
40
#include <linux/sched.h>
41
#include <linux/mm.h>
42
#include <linux/string.h>
43
#include <linux/errno.h>
44
#include <linux/cdrom.h>
45
#include <linux/interrupt.h>
46
#include <linux/init.h>
47
#include <asm/system.h>
48
#include <asm/io.h>
49
#include <asm/uaccess.h>
50
 
51
#define MAJOR_NR SCSI_CDROM_MAJOR
52
#include <linux/blk.h>
53
#include "scsi.h"
54
#include "hosts.h"
55
#include "sr.h"
56
#include <scsi/scsi_ioctl.h>    /* For the door lock/unlock commands */
57
#include "constants.h"
58
 
59
MODULE_PARM(xa_test, "i");      /* see sr_ioctl.c */
60
 
61
#define MAX_RETRIES     3
62
#define SR_TIMEOUT      (30 * HZ)
63
 
64
static int sr_init(void);
65
static void sr_finish(void);
66
static int sr_attach(Scsi_Device *);
67
static int sr_detect(Scsi_Device *);
68
static void sr_detach(Scsi_Device *);
69
 
70
static int sr_init_command(Scsi_Cmnd *);
71
 
72
static struct Scsi_Device_Template sr_template =
73
{
74
        name:"cdrom",
75
        tag:"sr",
76
        scsi_type:TYPE_ROM,
77
        major:SCSI_CDROM_MAJOR,
78
        blk:1,
79
        detect:sr_detect,
80
        init:sr_init,
81
        finish:sr_finish,
82
        attach:sr_attach,
83
        detach:sr_detach,
84
        init_command:sr_init_command
85
};
86
 
87
Scsi_CD *scsi_CDs;
88
static int *sr_sizes;
89
 
90
static int *sr_blocksizes;
91
static int *sr_hardsizes;
92
 
93
static int sr_open(struct cdrom_device_info *, int);
94
void get_sectorsize(int);
95
void get_capabilities(int);
96
 
97
static int sr_media_change(struct cdrom_device_info *, int);
98
static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
99
 
100
static void sr_release(struct cdrom_device_info *cdi)
101
{
102
        if (scsi_CDs[MINOR(cdi->dev)].device->sector_size > 2048)
103
                sr_set_blocklength(MINOR(cdi->dev), 2048);
104
        scsi_CDs[MINOR(cdi->dev)].device->access_count--;
105
        if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
106
                __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
107
        if (sr_template.module)
108
                __MOD_DEC_USE_COUNT(sr_template.module);
109
}
110
 
111
static struct cdrom_device_ops sr_dops =
112
{
113
        open:                   sr_open,
114
        release:                sr_release,
115
        drive_status:           sr_drive_status,
116
        media_changed:          sr_media_change,
117
        tray_move:              sr_tray_move,
118
        lock_door:              sr_lock_door,
119
        select_speed:           sr_select_speed,
120
        get_last_session:       sr_get_last_session,
121
        get_mcn:                sr_get_mcn,
122
        reset:                  sr_reset,
123
        audio_ioctl:            sr_audio_ioctl,
124
        dev_ioctl:              sr_dev_ioctl,
125
        capability:             CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
126
                                CDC_SELECT_SPEED | CDC_SELECT_DISC |
127
                                CDC_MULTI_SESSION | CDC_MCN |
128
                                CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
129
                                CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS |
130
                                CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
131
                                CDC_DVD_RAM | CDC_GENERIC_PACKET,
132
        generic_packet:         sr_packet,
133
};
134
 
135
/*
136
 * This function checks to see if the media has been changed in the
137
 * CDROM drive.  It is possible that we have already sensed a change,
138
 * or the drive may have sensed one and not yet reported it.  We must
139
 * be ready for either case. This function always reports the current
140
 * value of the changed bit.  If flag is 0, then the changed bit is reset.
141
 * This function could be done as an ioctl, but we would need to have
142
 * an inode for that to work, and we do not always have one.
143
 */
144
 
145
int sr_media_change(struct cdrom_device_info *cdi, int slot)
146
{
147
        int retval;
148
 
149
        if (CDSL_CURRENT != slot) {
150
                /* no changer support */
151
                return -EINVAL;
152
        }
153
        retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
154
                            SCSI_IOCTL_TEST_UNIT_READY, 0);
155
 
156
        if (retval) {
157
                /* Unable to test, unit probably not ready.  This usually
158
                 * means there is no disc in the drive.  Mark as changed,
159
                 * and we will figure it out later once the drive is
160
                 * available again.  */
161
 
162
                scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
163
                return 1;       /* This will force a flush, if called from
164
                                 * check_disk_change */
165
        };
166
 
167
        retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
168
        scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
169
        /* If the disk changed, the capacity will now be different,
170
         * so we force a re-read of this information */
171
        if (retval) {
172
                /* check multisession offset etc */
173
                sr_cd_check(cdi);
174
 
175
                /*
176
                 * If the disk changed, the capacity will now be different,
177
                 * so we force a re-read of this information
178
                 * Force 2048 for the sector size so that filesystems won't
179
                 * be trying to use something that is too small if the disc
180
                 * has changed.
181
                 */
182
                scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
183
 
184
                scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
185
        }
186
        return retval;
187
}
188
 
189
/*
190
 * rw_intr is the interrupt routine for the device driver.  It will be notified on the
191
 * end of a SCSI read / write, and will take on of several actions based on success or failure.
192
 */
193
 
194
static void rw_intr(Scsi_Cmnd * SCpnt)
195
{
196
        int result = SCpnt->result;
197
        int this_count = SCpnt->bufflen >> 9;
198
        int good_sectors = (result == 0 ? this_count : 0);
199
        int block_sectors = 0;
200
        int device_nr = DEVICE_NR(SCpnt->request.rq_dev);
201
        long error_sector;
202
 
203
#ifdef DEBUG
204
        printk("sr.c done: %x %p\n", result, SCpnt->request.bh->b_data);
205
#endif
206
        /*
207
           Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
208
           Since this is a relatively rare error condition, no care is taken to
209
           avoid unnecessary additional work such as memcpy's that could be avoided.
210
         */
211
 
212
 
213
        if (driver_byte(result) != 0 &&          /* An error occurred */
214
            (SCpnt->sense_buffer[0] & 0x7f) == 0x70) {   /* Sense data is valid */
215
                switch (SCpnt->sense_buffer[2]) {
216
                case MEDIUM_ERROR:
217
                case VOLUME_OVERFLOW:
218
                case ILLEGAL_REQUEST:
219
                        if (!(SCpnt->sense_buffer[0] & 0x80))
220
                                break;
221
                        error_sector = (SCpnt->sense_buffer[3] << 24) |
222
                        (SCpnt->sense_buffer[4] << 16) |
223
                        (SCpnt->sense_buffer[5] << 8) |
224
                        SCpnt->sense_buffer[6];
225
                        if (SCpnt->request.bh != NULL)
226
                                block_sectors = SCpnt->request.bh->b_size >> 9;
227
                        if (block_sectors < 4)
228
                                block_sectors = 4;
229
                        if (scsi_CDs[device_nr].device->sector_size == 2048)
230
                                error_sector <<= 2;
231
                        error_sector &= ~(block_sectors - 1);
232
                        good_sectors = error_sector - SCpnt->request.sector;
233
                        if (good_sectors < 0 || good_sectors >= this_count)
234
                                good_sectors = 0;
235
                        /*
236
                         * The SCSI specification allows for the value returned
237
                         * by READ CAPACITY to be up to 75 2K sectors past the
238
                         * last readable block.  Therefore, if we hit a medium
239
                         * error within the last 75 2K sectors, we decrease the
240
                         * saved size value.
241
                         */
242
                        if ((error_sector >> 1) < sr_sizes[device_nr] &&
243
                            scsi_CDs[device_nr].capacity - error_sector < 4 *75)
244
                                sr_sizes[device_nr] = error_sector >> 1;
245
                        break;
246
 
247
                case RECOVERED_ERROR:
248
                        /*
249
                         * An error occured, but it recovered.  Inform the
250
                         * user, but make sure that it's not treated as a
251
                         * hard error.
252
                         */
253
                        print_sense("sr", SCpnt);
254
                        result = 0;
255
                        SCpnt->sense_buffer[0] = 0x0;
256
                        good_sectors = this_count;
257
                        break;
258
 
259
                default:
260
                        break;
261
                }
262
        }
263
 
264
        /*
265
         * This calls the generic completion function, now that we know
266
         * how many actual sectors finished, and how many sectors we need
267
         * to say have failed.
268
         */
269
        scsi_io_completion(SCpnt, good_sectors, block_sectors);
270
}
271
 
272
 
273
static request_queue_t *sr_find_queue(kdev_t dev)
274
{
275
        /*
276
         * No such device
277
         */
278
        if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
279
                return NULL;
280
 
281
        return &scsi_CDs[MINOR(dev)].device->request_queue;
282
}
283
 
284
static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
285
{
286
        struct scatterlist *sg, *old_sg = NULL;
287
        int i, fsize, bsize, sg_ent, sg_count;
288
        char *front, *back;
289
        void **bbpnt, **old_bbpnt = NULL;
290
 
291
        back = front = NULL;
292
        sg_ent = SCpnt->use_sg;
293
        bsize = 0; /* gcc... */
294
 
295
        /*
296
         * need front pad
297
         */
298
        if ((fsize = SCpnt->request.sector % (s_size >> 9))) {
299
                fsize <<= 9;
300
                sg_ent++;
301
                if ((front = scsi_malloc(fsize)) == NULL)
302
                        goto no_mem;
303
        }
304
        /*
305
         * need a back pad too
306
         */
307
        if ((bsize = s_size - ((SCpnt->request_bufflen + fsize) % s_size))) {
308
                sg_ent++;
309
                if ((back = scsi_malloc(bsize)) == NULL)
310
                        goto no_mem;
311
        }
312
 
313
        /*
314
         * extend or allocate new scatter-gather table
315
         */
316
        sg_count = SCpnt->use_sg;
317
        if (sg_count) {
318
                old_sg = (struct scatterlist *) SCpnt->request_buffer;
319
                old_bbpnt = SCpnt->bounce_buffers;
320
        } else {
321
                sg_count = 1;
322
                sg_ent++;
323
        }
324
 
325
        /* Get space for scatterlist and bounce buffer array. */
326
        i  = sg_ent * sizeof(struct scatterlist);
327
        i += sg_ent * sizeof(void *);
328
        i  = (i + 511) & ~511;
329
 
330
        if ((sg = scsi_malloc(i)) == NULL)
331
                goto no_mem;
332
 
333
        bbpnt = (void **)
334
                ((char *)sg + (sg_ent * sizeof(struct scatterlist)));
335
 
336
        /*
337
         * no more failing memory allocs possible, we can safely assign
338
         * SCpnt values now
339
         */
340
        SCpnt->sglist_len = i;
341
        SCpnt->use_sg = sg_count;
342
        memset(sg, 0, SCpnt->sglist_len);
343
 
344
        i = 0;
345
        if (fsize) {
346
                sg[0].address = bbpnt[0] = front;
347
                sg[0].length = fsize;
348
                i++;
349
        }
350
        if (old_sg) {
351
                memcpy(sg + i, old_sg, SCpnt->use_sg * sizeof(struct scatterlist));
352
                if (old_bbpnt)
353
                        memcpy(bbpnt + i, old_bbpnt, SCpnt->use_sg * sizeof(void *));
354
                scsi_free(old_sg, (((SCpnt->use_sg * sizeof(struct scatterlist)) +
355
                                    (SCpnt->use_sg * sizeof(void *))) + 511) & ~511);
356
        } else {
357
                sg[i].address = SCpnt->request_buffer;
358
                sg[i].length = SCpnt->request_bufflen;
359
        }
360
 
361
        SCpnt->request_bufflen += (fsize + bsize);
362
        SCpnt->request_buffer = sg;
363
        SCpnt->bounce_buffers = bbpnt;
364
        SCpnt->use_sg += i;
365
 
366
        if (bsize) {
367
                sg[SCpnt->use_sg].address = back;
368
                bbpnt[SCpnt->use_sg] = back;
369
                sg[SCpnt->use_sg].length = bsize;
370
                SCpnt->use_sg++;
371
        }
372
 
373
        return 0;
374
 
375
no_mem:
376
        printk("sr: ran out of mem for scatter pad\n");
377
        if (front)
378
                scsi_free(front, fsize);
379
        if (back)
380
                scsi_free(back, bsize);
381
 
382
        return 1;
383
}
384
 
385
 
386
static int sr_init_command(Scsi_Cmnd * SCpnt)
387
{
388
        int dev, devm, block=0, this_count, s_size;
389
 
390
        devm = MINOR(SCpnt->request.rq_dev);
391
        dev = DEVICE_NR(SCpnt->request.rq_dev);
392
 
393
        SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
394
 
395
        if (dev >= sr_template.nr_dev ||
396
            !scsi_CDs[dev].device ||
397
            !scsi_CDs[dev].device->online) {
398
                SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
399
                SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
400
                return 0;
401
        }
402
        if (scsi_CDs[dev].device->changed) {
403
                /*
404
                 * quietly refuse to do anything to a changed disc until the
405
                 * changed bit has been reset
406
                 */
407
                return 0;
408
        }
409
 
410
        if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
411
                return 0;
412
 
413
        /*
414
         * we do lazy blocksize switching (when reading XA sectors,
415
         * see CDROMREADMODE2 ioctl)
416
         */
417
        s_size = scsi_CDs[dev].device->sector_size;
418
        if (s_size > 2048) {
419
                if (!in_interrupt())
420
                        sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
421
                else
422
                        printk("sr: can't switch blocksize: in interrupt\n");
423
        }
424
 
425
        if (s_size != 512 && s_size != 1024 && s_size != 2048) {
426
                printk("sr: bad sector size %d\n", s_size);
427
                return 0;
428
        }
429
 
430
        block = SCpnt->request.sector / (s_size >> 9);
431
 
432
        /*
433
         * request doesn't start on hw block boundary, add scatter pads
434
         */
435
        if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
436
                if (sr_scatter_pad(SCpnt, s_size))
437
                        return 0;
438
 
439
        this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
440
 
441
        switch (SCpnt->request.cmd) {
442
        case WRITE:
443
                SCpnt->cmnd[0] = WRITE_10;
444
                SCpnt->sc_data_direction = SCSI_DATA_WRITE;
445
                break;
446
        case READ:
447
                SCpnt->cmnd[0] = READ_10;
448
                SCpnt->sc_data_direction = SCSI_DATA_READ;
449
                break;
450
        default:
451
                printk("Unknown sr command %d\n", SCpnt->request.cmd);
452
                return 0;
453
        }
454
 
455
        SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
456
                                   devm,
457
                   (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
458
                                 this_count, SCpnt->request.nr_sectors));
459
 
460
        SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
461
                         ((SCpnt->lun << 5) & 0xe0) : 0;
462
 
463
        if (this_count > 0xffff)
464
                this_count = 0xffff;
465
 
466
        SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
467
        SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
468
        SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
469
        SCpnt->cmnd[5] = (unsigned char) block & 0xff;
470
        SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
471
        SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
472
        SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
473
 
474
        /*
475
         * We shouldn't disconnect in the middle of a sector, so with a dumb
476
         * host adapter, it's safe to assume that we can at least transfer
477
         * this many bytes between each connect / disconnect.
478
         */
479
        SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
480
        SCpnt->underflow = this_count << 9;
481
 
482
        SCpnt->allowed = MAX_RETRIES;
483
        SCpnt->timeout_per_command = SR_TIMEOUT;
484
 
485
        /*
486
         * This is the completion routine we use.  This is matched in terms
487
         * of capability to this function.
488
         */
489
        SCpnt->done = rw_intr;
490
 
491
        {
492
                struct scatterlist *sg = SCpnt->request_buffer;
493
                int i, size = 0;
494
                for (i = 0; i < SCpnt->use_sg; i++)
495
                        size += sg[i].length;
496
 
497
                if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
498
                        printk("sr: mismatch count %d, bytes %d\n", size, SCpnt->request_bufflen);
499
                        SCpnt->request_bufflen = size;
500
                }
501
        }
502
 
503
        /*
504
         * This indicates that the command is ready from our end to be
505
         * queued.
506
         */
507
        return 1;
508
}
509
 
510
struct block_device_operations sr_bdops =
511
{
512
        owner:                  THIS_MODULE,
513
        open:                   cdrom_open,
514
        release:                cdrom_release,
515
        ioctl:                  cdrom_ioctl,
516
        check_media_change:     cdrom_media_changed,
517
};
518
 
519
static int sr_open(struct cdrom_device_info *cdi, int purpose)
520
{
521
        check_disk_change(cdi->dev);
522
 
523
        if (MINOR(cdi->dev) >= sr_template.dev_max
524
            || !scsi_CDs[MINOR(cdi->dev)].device) {
525
                return -ENXIO;  /* No such device */
526
        }
527
        /*
528
         * If the device is in error recovery, wait until it is done.
529
         * If the device is offline, then disallow any access to it.
530
         */
531
        if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
532
                return -ENXIO;
533
        }
534
        scsi_CDs[MINOR(cdi->dev)].device->access_count++;
535
        if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
536
                __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
537
        if (sr_template.module)
538
                __MOD_INC_USE_COUNT(sr_template.module);
539
 
540
        /* If this device did not have media in the drive at boot time, then
541
         * we would have been unable to get the sector size.  Check to see if
542
         * this is the case, and try again.
543
         */
544
 
545
        if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
546
                get_sectorsize(MINOR(cdi->dev));
547
 
548
        return 0;
549
}
550
 
551
static int sr_detect(Scsi_Device * SDp)
552
{
553
 
554
        if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
555
                return 0;
556
        sr_template.dev_noticed++;
557
        return 1;
558
}
559
 
560
static int sr_attach(Scsi_Device * SDp)
561
{
562
        Scsi_CD *cpnt;
563
        int i;
564
 
565
        if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
566
                return 1;
567
 
568
        if (sr_template.nr_dev >= sr_template.dev_max) {
569
                SDp->attached--;
570
                return 1;
571
        }
572
        for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
573
                if (!cpnt->device)
574
                        break;
575
 
576
        if (i >= sr_template.dev_max)
577
                panic("scsi_devices corrupt (sr)");
578
 
579
 
580
        scsi_CDs[i].device = SDp;
581
 
582
        sr_template.nr_dev++;
583
        if (sr_template.nr_dev > sr_template.dev_max)
584
                panic("scsi_devices corrupt (sr)");
585
 
586
        printk("Attached scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
587
               i, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
588
        return 0;
589
}
590
 
591
 
592
void get_sectorsize(int i)
593
{
594
        unsigned char cmd[10];
595
        unsigned char *buffer;
596
        int the_result, retries;
597
        int sector_size;
598
        Scsi_Request *SRpnt;
599
 
600
        buffer = (unsigned char *) scsi_malloc(512);
601
        SRpnt = scsi_allocate_request(scsi_CDs[i].device);
602
 
603
        if(buffer == NULL || SRpnt == NULL)
604
        {
605
                scsi_CDs[i].capacity = 0x1fffff;
606
                sector_size = 2048;     /* A guess, just in case */
607
                scsi_CDs[i].needs_sector_size = 1;
608
                if(buffer)
609
                        scsi_free(buffer, 512);
610
                if(SRpnt)
611
                        scsi_release_request(SRpnt);
612
                return;
613
        }
614
 
615
        retries = 3;
616
        do {
617
                cmd[0] = READ_CAPACITY;
618
                cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
619
                         ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
620
                memset((void *) &cmd[2], 0, 8);
621
                SRpnt->sr_request.rq_status = RQ_SCSI_BUSY;     /* Mark as really busy */
622
                SRpnt->sr_cmd_len = 0;
623
 
624
                memset(buffer, 0, 8);
625
 
626
                /* Do the command and wait.. */
627
 
628
                SRpnt->sr_data_direction = SCSI_DATA_READ;
629
                scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
630
                              8, SR_TIMEOUT, MAX_RETRIES);
631
 
632
                the_result = SRpnt->sr_result;
633
                retries--;
634
 
635
        } while (the_result && retries);
636
 
637
 
638
        scsi_release_request(SRpnt);
639
        SRpnt = NULL;
640
 
641
        if (the_result) {
642
                scsi_CDs[i].capacity = 0x1fffff;
643
                sector_size = 2048;     /* A guess, just in case */
644
                scsi_CDs[i].needs_sector_size = 1;
645
        } else {
646
#if 0
647
                if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
648
                                           &scsi_CDs[i].capacity))
649
#endif
650
                        scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
651
                                                    (buffer[1] << 16) |
652
                                                    (buffer[2] << 8) |
653
                                                    buffer[3]);
654
                sector_size = (buffer[4] << 24) |
655
                    (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
656
                switch (sector_size) {
657
                        /*
658
                         * HP 4020i CD-Recorder reports 2340 byte sectors
659
                         * Philips CD-Writers report 2352 byte sectors
660
                         *
661
                         * Use 2k sectors for them..
662
                         */
663
                case 0:
664
                case 2340:
665
                case 2352:
666
                        sector_size = 2048;
667
                        /* fall through */
668
                case 2048:
669
                        scsi_CDs[i].capacity *= 4;
670
                        /* fall through */
671
                case 512:
672
                        break;
673
                default:
674
                        printk("sr%d: unsupported sector size %d.\n",
675
                               i, sector_size);
676
                        scsi_CDs[i].capacity = 0;
677
                        scsi_CDs[i].needs_sector_size = 1;
678
                }
679
 
680
                scsi_CDs[i].device->sector_size = sector_size;
681
 
682
                /*
683
                 * Add this so that we have the ability to correctly gauge
684
                 * what the device is capable of.
685
                 */
686
                scsi_CDs[i].needs_sector_size = 0;
687
                sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
688
        };
689
        scsi_free(buffer, 512);
690
}
691
 
692
void get_capabilities(int i)
693
{
694
        unsigned char cmd[6];
695
        unsigned char *buffer;
696
        int rc, n;
697
 
698
        static char *loadmech[] =
699
        {
700
                "caddy",
701
                "tray",
702
                "pop-up",
703
                "",
704
                "changer",
705
                "cartridge changer",
706
                "",
707
                ""
708
        };
709
 
710
        buffer = (unsigned char *) scsi_malloc(512);
711
        if (!buffer)
712
        {
713
                printk(KERN_ERR "sr: out of memory.\n");
714
                return;
715
        }
716
        cmd[0] = MODE_SENSE;
717
        cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
718
                 ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
719
        cmd[2] = 0x2a;
720
        cmd[4] = 128;
721
        cmd[3] = cmd[5] = 0;
722
        rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ, NULL);
723
 
724
        if (rc) {
725
                /* failed, drive doesn't have capabilities mode page */
726
                scsi_CDs[i].cdi.speed = 1;
727
                scsi_CDs[i].cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
728
                                         CDC_DVD | CDC_DVD_RAM |
729
                                         CDC_SELECT_DISC | CDC_SELECT_SPEED);
730
                scsi_free(buffer, 512);
731
                printk("sr%i: scsi-1 drive\n", i);
732
                return;
733
        }
734
        n = buffer[3] + 4;
735
        scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
736
        scsi_CDs[i].readcd_known = 1;
737
        scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
738
        /* print some capability bits */
739
        printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
740
               ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
741
               scsi_CDs[i].cdi.speed,
742
               buffer[n + 3] & 0x01 ? "writer " : "",   /* CD Writer */
743
               buffer[n + 3] & 0x20 ? "dvd-ram " : "",
744
               buffer[n + 2] & 0x02 ? "cd/rw " : "",    /* can read rewriteable */
745
               buffer[n + 4] & 0x20 ? "xa/form2 " : "",         /* can read xa/from2 */
746
               buffer[n + 5] & 0x01 ? "cdda " : "",     /* can read audio data */
747
               loadmech[buffer[n + 6] >> 5]);
748
        if ((buffer[n + 6] >> 5) == 0)
749
                /* caddy drives can't close tray... */
750
                scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
751
        if ((buffer[n + 2] & 0x8) == 0)
752
                /* not a DVD drive */
753
                scsi_CDs[i].cdi.mask |= CDC_DVD;
754
        if ((buffer[n + 3] & 0x20) == 0) {
755
                /* can't write DVD-RAM media */
756
                scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
757
        } else {
758
                scsi_CDs[i].device->writeable = 1;
759
        }
760
        if ((buffer[n + 3] & 0x10) == 0)
761
                /* can't write DVD-R media */
762
                scsi_CDs[i].cdi.mask |= CDC_DVD_R;
763
        if ((buffer[n + 3] & 0x2) == 0)
764
                /* can't write CD-RW media */
765
                scsi_CDs[i].cdi.mask |= CDC_CD_RW;
766
        if ((buffer[n + 3] & 0x1) == 0)
767
                /* can't write CD-R media */
768
                scsi_CDs[i].cdi.mask |= CDC_CD_R;
769
        if ((buffer[n + 6] & 0x8) == 0)
770
                /* can't eject */
771
                scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
772
 
773
        if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
774
            (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
775
                scsi_CDs[i].cdi.capacity =
776
                    cdrom_number_of_slots(&(scsi_CDs[i].cdi));
777
        if (scsi_CDs[i].cdi.capacity <= 1)
778
                /* not a changer */
779
                scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
780
        /*else    I don't think it can close its tray
781
           scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
782
 
783
        scsi_free(buffer, 512);
784
}
785
 
786
/*
787
 * sr_packet() is the entry point for the generic commands generated
788
 * by the Uniform CD-ROM layer.
789
 */
790
static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
791
{
792
        Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
793
 
794
        /* set the LUN */
795
        if (device->scsi_level <= SCSI_2)
796
                cgc->cmd[1] |= device->lun << 5;
797
 
798
        cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
799
 
800
        return cgc->stat;
801
}
802
 
803
static int sr_registered;
804
 
805
static int sr_init()
806
{
807
        int i;
808
 
809
        if (sr_template.dev_noticed == 0)
810
                return 0;
811
 
812
        if (!sr_registered) {
813
                if (devfs_register_blkdev(MAJOR_NR, "sr", &sr_bdops)) {
814
                        printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
815
                        sr_template.dev_noticed = 0;
816
                        return 1;
817
                }
818
                sr_registered++;
819
        }
820
        if (scsi_CDs)
821
                return 0;
822
 
823
        sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
824
        scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
825
        if (!scsi_CDs)
826
                goto cleanup_devfs;
827
        memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
828
 
829
        sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
830
        if (!sr_sizes)
831
                goto cleanup_cds;
832
        memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
833
 
834
        sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
835
        if (!sr_blocksizes)
836
                goto cleanup_sizes;
837
 
838
        sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
839
        if (!sr_hardsizes)
840
                goto cleanup_blocksizes;
841
        /*
842
         * These are good guesses for the time being.
843
         */
844
        for (i = 0; i < sr_template.dev_max; i++) {
845
                sr_blocksizes[i] = 2048;
846
                sr_hardsizes[i] = 2048;
847
        }
848
        blksize_size[MAJOR_NR] = sr_blocksizes;
849
        hardsect_size[MAJOR_NR] = sr_hardsizes;
850
        return 0;
851
cleanup_blocksizes:
852
        kfree(sr_blocksizes);
853
cleanup_sizes:
854
        kfree(sr_sizes);
855
cleanup_cds:
856
        kfree(scsi_CDs);
857
        scsi_CDs = NULL;
858
cleanup_devfs:
859
        devfs_unregister_blkdev(MAJOR_NR, "sr");
860
        sr_template.dev_noticed = 0;
861
        sr_registered--;
862
        return 1;
863
}
864
 
865
void sr_finish()
866
{
867
        int i;
868
        char name[6];
869
 
870
        blk_dev[MAJOR_NR].queue = sr_find_queue;
871
        blk_size[MAJOR_NR] = sr_sizes;
872
 
873
        for (i = 0; i < sr_template.nr_dev; ++i) {
874
                /* If we have already seen this, then skip it.  Comes up
875
                 * with loadable modules. */
876
                if (scsi_CDs[i].capacity)
877
                        continue;
878
                scsi_CDs[i].capacity = 0x1fffff;
879
                scsi_CDs[i].device->sector_size = 2048;         /* A guess, just in case */
880
                scsi_CDs[i].needs_sector_size = 1;
881
                scsi_CDs[i].device->changed = 1;        /* force recheck CD type */
882
#if 0
883
                /* seems better to leave this for later */
884
                get_sectorsize(i);
885
                printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
886
#endif
887
                scsi_CDs[i].use = 1;
888
 
889
                scsi_CDs[i].device->ten = 1;
890
                scsi_CDs[i].device->remap = 1;
891
                scsi_CDs[i].readcd_known = 0;
892
                scsi_CDs[i].readcd_cdda = 0;
893
                sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
894
 
895
                scsi_CDs[i].cdi.ops = &sr_dops;
896
                scsi_CDs[i].cdi.handle = &scsi_CDs[i];
897
                scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
898
                scsi_CDs[i].cdi.mask = 0;
899
                scsi_CDs[i].cdi.capacity = 1;
900
                /*
901
                 *      FIXME: someone needs to handle a get_capabilities
902
                 *      failure properly ??
903
                 */
904
                get_capabilities(i);
905
                sr_vendor_init(i);
906
 
907
                sprintf(name, "sr%d", i);
908
                strcpy(scsi_CDs[i].cdi.name, name);
909
                scsi_CDs[i].cdi.de =
910
                    devfs_register (scsi_CDs[i].device->de, "cd",
911
                                    DEVFS_FL_DEFAULT, MAJOR_NR, i,
912
                                    S_IFBLK | S_IRUGO | S_IWUGO,
913
                                    &sr_bdops, NULL);
914
                register_cdrom(&scsi_CDs[i].cdi);
915
        }
916
 
917
 
918
        /* If our host adapter is capable of scatter-gather, then we increase
919
         * the read-ahead to 16 blocks (32 sectors).  If not, we use
920
         * a two block (4 sector) read ahead. */
921
        if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
922
                read_ahead[MAJOR_NR] = 32;      /* 32 sector read-ahead.  Always removable. */
923
        else
924
                read_ahead[MAJOR_NR] = 4;       /* 4 sector read-ahead */
925
 
926
        return;
927
}
928
 
929
static void sr_detach(Scsi_Device * SDp)
930
{
931
        Scsi_CD *cpnt;
932
        int i;
933
 
934
        if (scsi_CDs == NULL)
935
                return;
936
        for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
937
                if (cpnt->device == SDp) {
938
                        /*
939
                         * Since the cdrom is read-only, no need to sync the device.
940
                         * We should be kind to our buffer cache, however.
941
                         */
942
                        invalidate_device(MKDEV(MAJOR_NR, i), 0);
943
 
944
                        /*
945
                         * Reset things back to a sane state so that one can re-load a new
946
                         * driver (perhaps the same one).
947
                         */
948
                        unregister_cdrom(&(cpnt->cdi));
949
                        cpnt->device = NULL;
950
                        cpnt->capacity = 0;
951
                        SDp->attached--;
952
                        sr_template.nr_dev--;
953
                        sr_template.dev_noticed--;
954
                        sr_sizes[i] = 0;
955
                        return;
956
                }
957
        return;
958
}
959
 
960
static int __init init_sr(void)
961
{
962
        sr_template.module = THIS_MODULE;
963
        return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
964
}
965
 
966
static void __exit exit_sr(void)
967
{
968
        scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
969
        devfs_unregister_blkdev(MAJOR_NR, "sr");
970
        sr_registered--;
971
        if (scsi_CDs != NULL) {
972
                kfree(scsi_CDs);
973
 
974
                kfree(sr_sizes);
975
                sr_sizes = NULL;
976
 
977
                kfree(sr_blocksizes);
978
                sr_blocksizes = NULL;
979
                kfree(sr_hardsizes);
980
                sr_hardsizes = NULL;
981
        }
982
        blksize_size[MAJOR_NR] = NULL;
983
        hardsect_size[MAJOR_NR] = NULL;
984
        blk_size[MAJOR_NR] = NULL;
985
        read_ahead[MAJOR_NR] = 0;
986
 
987
        sr_template.dev_max = 0;
988
}
989
 
990
module_init(init_sr);
991
module_exit(exit_sr);
992
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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