This source file includes following definitions.
- sr_release
- check_cdrom_media_change
- rw_intr
- sr_photocd
- sr_open
- do_sr_request
- requeue_sr_request
- sr_detect
- sr_attach
- sr_init_done
- get_sectorsize
- sr_init
- sr_finish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <linux/fs.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/cdrom.h>
23 #include <asm/system.h>
24
25 #define MAJOR_NR SCSI_CDROM_MAJOR
26 #include "../block/blk.h"
27 #include "scsi.h"
28 #include "hosts.h"
29 #include "sr.h"
30 #include "scsi_ioctl.h"
31 #include "constants.h"
32
33 #define MAX_RETRIES 3
34 #define SR_TIMEOUT 5000
35
36 static void sr_init(void);
37 static void sr_finish(void);
38 static void sr_attach(Scsi_Device *);
39 static int sr_detect(Scsi_Device *);
40
41 struct Scsi_Device_Template sr_template = {NULL, "cdrom", "sr", TYPE_ROM,
42 SCSI_CDROM_MAJOR, 0, 0, 0, 1,
43 sr_detect, sr_init,
44 sr_finish, sr_attach, NULL};
45
46 Scsi_CD * scsi_CDs;
47 static int * sr_sizes;
48
49 static int * sr_blocksizes;
50
51 static int sr_open(struct inode *, struct file *);
52 static void get_sectorsize(int);
53
54 extern int sr_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
55
56 void requeue_sr_request (Scsi_Cmnd * SCpnt);
57 static int check_cdrom_media_change(dev_t);
58
59 static void sr_release(struct inode * inode, struct file * file)
60 {
61 sync_dev(inode->i_rdev);
62 if(! --scsi_CDs[MINOR(inode->i_rdev)].device->access_count)
63 sr_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
64 }
65
66 static struct file_operations sr_fops =
67 {
68 NULL,
69 block_read,
70 block_write,
71 NULL,
72 NULL,
73 sr_ioctl,
74 NULL,
75 sr_open,
76 sr_release,
77 NULL,
78 NULL,
79 check_cdrom_media_change,
80 NULL
81 };
82
83
84
85
86
87
88
89
90
91
92
93 int check_cdrom_media_change(dev_t full_dev){
94 int retval, target;
95 struct inode inode;
96 int flag = 0;
97
98 target = MINOR(full_dev);
99
100 if (target >= sr_template.nr_dev) {
101 printk("CD-ROM request error: invalid device.\n");
102 return 0;
103 };
104
105 inode.i_rdev = full_dev;
106 retval = sr_ioctl(&inode, NULL, SCSI_IOCTL_TEST_UNIT_READY, 0);
107
108 if(retval){
109
110
111
112
113 scsi_CDs[target].device->changed = 1;
114 return 1;
115
116 };
117
118 retval = scsi_CDs[target].device->changed;
119 if(!flag) {
120 scsi_CDs[target].device->changed = 0;
121
122
123 if (retval) scsi_CDs[target].needs_sector_size = 1;
124 };
125 return retval;
126 }
127
128
129
130
131
132
133 static void rw_intr (Scsi_Cmnd * SCpnt)
134 {
135 int result = SCpnt->result;
136 int this_count = SCpnt->this_count;
137
138 #ifdef DEBUG
139 printk("sr.c done: %x %x\n",result, SCpnt->request.bh->b_data);
140 #endif
141 if (!result)
142 {
143 if (SCpnt->use_sg == 0) {
144 if (SCpnt->buffer != SCpnt->request.buffer)
145 {
146 int offset;
147 offset = (SCpnt->request.sector % 4) << 9;
148 memcpy((char *)SCpnt->request.buffer,
149 (char *)SCpnt->buffer + offset,
150 this_count << 9);
151
152
153
154
155 if((offset == 0) && this_count == 2 &&
156 SCpnt->request.nr_sectors > this_count &&
157 SCpnt->request.bh &&
158 SCpnt->request.bh->b_reqnext &&
159 SCpnt->request.bh->b_reqnext->b_size == 1024) {
160 memcpy((char *)SCpnt->request.bh->b_reqnext->b_data,
161 (char *)SCpnt->buffer + 1024,
162 1024);
163 this_count += 2;
164 };
165
166 scsi_free(SCpnt->buffer, 2048);
167 }
168 } else {
169 struct scatterlist * sgpnt;
170 int i;
171 sgpnt = (struct scatterlist *) SCpnt->buffer;
172 for(i=0; i<SCpnt->use_sg; i++) {
173 if (sgpnt[i].alt_address) {
174 if (sgpnt[i].alt_address != sgpnt[i].address) {
175 memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
176 };
177 scsi_free(sgpnt[i].address, sgpnt[i].length);
178 };
179 };
180 scsi_free(SCpnt->buffer, SCpnt->sglist_len);
181 if(SCpnt->request.sector % 4) this_count -= 2;
182
183 if(this_count > SCpnt->request.nr_sectors)
184 this_count -= 2;
185 };
186
187 #ifdef DEBUG
188 printk("(%x %x %x) ",SCpnt->request.bh, SCpnt->request.nr_sectors,
189 this_count);
190 #endif
191 if (SCpnt->request.nr_sectors > this_count)
192 {
193 SCpnt->request.errors = 0;
194 if (!SCpnt->request.bh)
195 panic("sr.c: linked page request (%lx %x)",
196 SCpnt->request.sector, this_count);
197 }
198
199 SCpnt = end_scsi_request(SCpnt, 1, this_count);
200 requeue_sr_request(SCpnt);
201 return;
202 }
203
204
205
206
207 if (SCpnt->use_sg) {
208 struct scatterlist * sgpnt;
209 int i;
210 sgpnt = (struct scatterlist *) SCpnt->buffer;
211 for(i=0; i<SCpnt->use_sg; i++) {
212 if (sgpnt[i].alt_address) {
213 scsi_free(sgpnt[i].address, sgpnt[i].length);
214 };
215 };
216 scsi_free(SCpnt->buffer, SCpnt->sglist_len);
217 } else {
218 if (SCpnt->buffer != SCpnt->request.buffer)
219 scsi_free(SCpnt->buffer, SCpnt->bufflen);
220 };
221
222 if (driver_byte(result) != 0) {
223 if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
224 if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
225
226
227
228 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->changed = 1;
229 SCpnt = end_scsi_request(SCpnt, 0, this_count);
230 requeue_sr_request(SCpnt);
231 return;
232 }
233 }
234
235 if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
236 printk("CD-ROM error: Drive reports ILLEGAL REQUEST.\n");
237 if (scsi_CDs[DEVICE_NR(SCpnt->request.dev)].ten) {
238 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].ten = 0;
239 requeue_sr_request(SCpnt);
240 result = 0;
241 return;
242 } else {
243 printk("CD-ROM error: Drive reports %d.\n", SCpnt->sense_buffer[2]);
244 SCpnt = end_scsi_request(SCpnt, 0, this_count);
245 requeue_sr_request(SCpnt);
246 return;
247 }
248
249 }
250
251 if (SCpnt->sense_buffer[2] == NOT_READY) {
252 printk("CDROM not ready. Make sure you have a disc in the drive.\n");
253 SCpnt = end_scsi_request(SCpnt, 0, this_count);
254 requeue_sr_request(SCpnt);
255 return;
256 };
257 }
258
259
260 if(result) {
261 printk("SCSI CD error : host %d id %d lun %d return code = %03x\n",
262 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->host->host_no,
263 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->id,
264 scsi_CDs[DEVICE_NR(SCpnt->request.dev)].device->lun,
265 result);
266
267 if (status_byte(result) == CHECK_CONDITION)
268 print_sense("sr", SCpnt);
269
270 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
271 requeue_sr_request(SCpnt);
272 }
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 static void sr_photocd(struct inode *inode)
299 {
300 unsigned long sector,min,sec,frame;
301 unsigned char buf[40];
302 int rc;
303
304 if (!suser()) {
305
306
307
308
309
310
311 if (1 == scsi_CDs[MINOR(inode->i_rdev)].device->access_count)
312 scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = 0;
313 return;
314 }
315
316 switch(scsi_CDs[MINOR(inode->i_rdev)].device->manufacturer) {
317
318 case SCSI_MAN_NEC:
319 #ifdef DEBUG
320 printk("sr_photocd: use NEC code\n");
321 #endif
322 memset(buf,0,40);
323 *((unsigned long*)buf) = 0;
324 *((unsigned long*)buf+1) = 0x16;
325 buf[8+0] = 0xde;
326 buf[8+1] = 0x03;
327 buf[8+2] = 0xb0;
328 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
329 SCSI_IOCTL_SEND_COMMAND, buf);
330 if (rc != 0) {
331 printk("sr_photocd: ioctl error (NEC): 0x%x\n",rc);
332 sector = 0;
333 } else {
334 min = (unsigned long)buf[8+15]/16*10 + (unsigned long)buf[8+15]%16;
335 sec = (unsigned long)buf[8+16]/16*10 + (unsigned long)buf[8+16]%16;
336 frame = (unsigned long)buf[8+17]/16*10 + (unsigned long)buf[8+17]%16;
337 sector = min*60*75 + sec*75 + frame;
338 #ifdef DEBUG
339 if (sector) {
340 printk("sr_photocd: multisession CD detected. start: %lu\n",sector);
341 }
342 #endif
343 }
344 break;
345
346 case SCSI_MAN_TOSHIBA:
347 #ifdef DEBUG
348 printk("sr_photocd: use TOSHIBA code\n");
349 #endif
350
351
352 memset(buf,0,40);
353 *((unsigned long*)buf) = 12;
354 *((unsigned long*)buf+1) = 12;
355 buf[8+0] = 0x15;
356 buf[8+1] = (1 << 4);
357 buf[8+2] = 12;
358 buf[14+ 3] = 0x08;
359 buf[14+ 4] = 0x83;
360 buf[14+10] = 0x08;
361 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
362 SCSI_IOCTL_SEND_COMMAND, buf);
363 if (rc != 0) {
364 printk("sr_photocd: ioctl error (TOSHIBA #1): 0x%x\n",rc);
365 }
366
367
368 memset(buf,0,40);
369 *((unsigned long*)buf) = 0;
370 *((unsigned long*)buf+1) = 4;
371 buf[8+0] = 0xc7;
372 buf[8+1] = 3;
373 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
374 SCSI_IOCTL_SEND_COMMAND, buf);
375 if (rc != 0) {
376 printk("sr_photocd: ioctl error (TOSHIBA #2): 0x%x\n",rc);
377 sector = 0;
378 } else {
379 min = (unsigned long)buf[8+1]/16*10 + (unsigned long)buf[8+1]%16;
380 sec = (unsigned long)buf[8+2]/16*10 + (unsigned long)buf[8+2]%16;
381 frame = (unsigned long)buf[8+3]/16*10 + (unsigned long)buf[8+3]%16;
382 sector = min*60*75 + sec*75 + frame;
383 if (sector) {
384 sector -= CD_BLOCK_OFFSET;
385 #ifdef DEBUG
386 printk("sr_photocd: multisession CD detected: start: %lu\n",sector);
387 #endif
388 }
389 }
390 break;
391
392 case SCSI_MAN_UNKNOWN:
393 default:
394 #ifdef DEBUG
395 printk("sr_photocd: unknown drive, no special multisession code\n");
396 #endif
397 sector = 0;
398 break; }
399
400 scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = sector;
401 return;
402 }
403
404 static int sr_open(struct inode * inode, struct file * filp)
405 {
406 if(MINOR(inode->i_rdev) >= sr_template.nr_dev ||
407 !scsi_CDs[MINOR(inode->i_rdev)].device) return -ENXIO;
408
409 if (filp->f_mode & 2)
410 return -EROFS;
411
412 check_disk_change(inode->i_rdev);
413
414 if(!scsi_CDs[MINOR(inode->i_rdev)].device->access_count++)
415 sr_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
416
417
418
419
420
421
422 if(scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
423 get_sectorsize(MINOR(inode->i_rdev));
424
425 #if 1
426 sr_photocd(inode);
427 #endif
428
429 return 0;
430 }
431
432
433
434
435
436
437
438 static void do_sr_request (void)
439 {
440 Scsi_Cmnd * SCpnt = NULL;
441 struct request * req = NULL;
442 unsigned long flags;
443 int flag = 0;
444
445 while (1==1){
446 save_flags(flags);
447 cli();
448 if (CURRENT != NULL && CURRENT->dev == -1) {
449 restore_flags(flags);
450 return;
451 };
452
453 INIT_SCSI_REQUEST;
454
455 if (flag++ == 0)
456 SCpnt = allocate_device(&CURRENT,
457 scsi_CDs[DEVICE_NR(MINOR(CURRENT->dev))].device, 0);
458 else SCpnt = NULL;
459 restore_flags(flags);
460
461
462
463
464
465
466
467
468 if (!SCpnt && sr_template.nr_dev > 1){
469 struct request *req1;
470 req1 = NULL;
471 save_flags(flags);
472 cli();
473 req = CURRENT;
474 while(req){
475 SCpnt = request_queueable(req,
476 scsi_CDs[DEVICE_NR(MINOR(req->dev))].device);
477 if(SCpnt) break;
478 req1 = req;
479 req = req->next;
480 };
481 if (SCpnt && req->dev == -1) {
482 if (req == CURRENT)
483 CURRENT = CURRENT->next;
484 else
485 req1->next = req->next;
486 };
487 restore_flags(flags);
488 };
489
490 if (!SCpnt)
491 return;
492
493 wake_up(&wait_for_request);
494
495
496 requeue_sr_request(SCpnt);
497 };
498 }
499
500 void requeue_sr_request (Scsi_Cmnd * SCpnt)
501 {
502 unsigned int dev, block, realcount;
503 unsigned char cmd[10], *buffer, tries;
504 int this_count, start, end_rec;
505
506 tries = 2;
507
508 repeat:
509 if(!SCpnt || SCpnt->request.dev <= 0) {
510 do_sr_request();
511 return;
512 }
513
514 dev = MINOR(SCpnt->request.dev);
515 block = SCpnt->request.sector;
516 buffer = NULL;
517 this_count = 0;
518
519 if (dev >= sr_template.nr_dev)
520 {
521
522 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
523 tries = 2;
524 goto repeat;
525 }
526
527 if (!scsi_CDs[dev].use)
528 {
529
530 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
531 tries = 2;
532 goto repeat;
533 }
534
535 if (scsi_CDs[dev].device->changed)
536 {
537
538
539
540
541 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
542 tries = 2;
543 goto repeat;
544 }
545
546 switch (SCpnt->request.cmd)
547 {
548 case WRITE:
549 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
550 goto repeat;
551 break;
552 case READ :
553 cmd[0] = READ_6;
554 break;
555 default :
556 panic ("Unknown sr command %d\n", SCpnt->request.cmd);
557 }
558
559 cmd[1] = (SCpnt->lun << 5) & 0xe0;
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577 #if 1
578
579
580
581
582 if (block >= 64 && block < 68) {
583 block += scsi_CDs[dev].mpcd_sector*4; }
584 #endif
585
586 SCpnt->use_sg = 0;
587
588 if (SCpnt->host->sg_tablesize > 0 &&
589 (!need_isa_buffer ||
590 dma_free_sectors >= 10)) {
591 struct buffer_head * bh;
592 struct scatterlist * sgpnt;
593 int count, this_count_max;
594 bh = SCpnt->request.bh;
595 this_count = 0;
596 count = 0;
597 this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
598
599
600 this_count = SCpnt->request.sector % 4;
601 if(this_count) count++;
602 while(bh && count < SCpnt->host->sg_tablesize) {
603 if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
604 this_count += (bh->b_size >> 9);
605 count++;
606 bh = bh->b_reqnext;
607 };
608
609 end_rec = 0;
610 if(this_count % 4) {
611 if (count < SCpnt->host->sg_tablesize) {
612 count++;
613 end_rec = (4 - (this_count % 4)) << 9;
614 this_count += 4 - (this_count % 4);
615 } else {
616 count--;
617 this_count -= (this_count % 4);
618 };
619 };
620 SCpnt->use_sg = count;
621 count = 512;
622 while( count < (SCpnt->use_sg * sizeof(struct scatterlist)))
623 count = count << 1;
624 SCpnt->sglist_len = count;
625 sgpnt = (struct scatterlist * ) scsi_malloc(count);
626 if (!sgpnt) {
627 printk("Warning - running *really* short on DMA buffers\n");
628 SCpnt->use_sg = 0;
629 } else {
630 buffer = (unsigned char *) sgpnt;
631 count = 0;
632 bh = SCpnt->request.bh;
633 if(SCpnt->request.sector % 4) {
634 sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
635 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
636 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
637 sgpnt[count].alt_address = sgpnt[count].address;
638
639 count++;
640 };
641 for(bh = SCpnt->request.bh; count < SCpnt->use_sg;
642 count++, bh = bh->b_reqnext) {
643 if (bh) {
644 sgpnt[count].address = bh->b_data;
645 sgpnt[count].length = bh->b_size;
646 sgpnt[count].alt_address = NULL;
647 } else {
648 sgpnt[count].address = (char *) scsi_malloc(end_rec);
649 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
650 sgpnt[count].length = end_rec;
651 sgpnt[count].alt_address = sgpnt[count].address;
652 if (count+1 != SCpnt->use_sg) panic("Bad sr request list");
653 break;
654 };
655 if (((int) sgpnt[count].address) + sgpnt[count].length >
656 ISA_DMA_THRESHOLD & (SCpnt->host->unchecked_isa_dma)) {
657 sgpnt[count].alt_address = sgpnt[count].address;
658
659
660
661 if(dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
662 sgpnt[count].address = NULL;
663 } else {
664 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
665 };
666
667
668
669
670 if(sgpnt[count].address == NULL){
671 printk("Warning: Running low on SCSI DMA buffers");
672
673 while(--count >= 0){
674 if(sgpnt[count].alt_address)
675 scsi_free(sgpnt[count].address, sgpnt[count].length);
676 };
677 SCpnt->use_sg = 0;
678 scsi_free(buffer, SCpnt->sglist_len);
679 break;
680 };
681 };
682 };
683 #ifdef DEBUG
684 printk("SG: %d %d %d %d %d *** ",SCpnt->use_sg, SCpnt->request.sector,
685 this_count,
686 SCpnt->request.current_nr_sectors,
687 SCpnt->request.nr_sectors);
688 for(count=0; count<SCpnt->use_sg; count++)
689 printk("SGlist: %d %x %x %x\n", count,
690 sgpnt[count].address,
691 sgpnt[count].alt_address,
692 sgpnt[count].length);
693 #endif
694 };
695 };
696
697 if (SCpnt->use_sg == 0){
698
699 if (!SCpnt->request.bh)
700 this_count = SCpnt->request.nr_sectors;
701 else
702 this_count = (SCpnt->request.bh->b_size >> 9);
703
704 start = block % 4;
705 if (start)
706 {
707 this_count = ((this_count > 4 - start) ?
708 (4 - start) : (this_count));
709 buffer = (unsigned char *) scsi_malloc(2048);
710 }
711 else if (this_count < 4)
712 {
713 buffer = (unsigned char *) scsi_malloc(2048);
714 }
715 else
716 {
717 this_count -= this_count % 4;
718 buffer = (unsigned char *) SCpnt->request.buffer;
719 if (((int) buffer) + (this_count << 9) > ISA_DMA_THRESHOLD &
720 (SCpnt->host->unchecked_isa_dma))
721 buffer = (unsigned char *) scsi_malloc(this_count << 9);
722 }
723 };
724
725 if (scsi_CDs[dev].sector_size == 2048)
726 block = block >> 2;
727 else
728 block = block & 0xfffffffc;
729
730 realcount = (this_count + 3) / 4;
731
732 if (scsi_CDs[dev].sector_size == 512) realcount = realcount << 2;
733
734 if (((realcount > 0xff) || (block > 0x1fffff)) && scsi_CDs[dev].ten)
735 {
736 if (realcount > 0xffff)
737 {
738 realcount = 0xffff;
739 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
740 }
741
742 cmd[0] += READ_10 - READ_6 ;
743 cmd[2] = (unsigned char) (block >> 24) & 0xff;
744 cmd[3] = (unsigned char) (block >> 16) & 0xff;
745 cmd[4] = (unsigned char) (block >> 8) & 0xff;
746 cmd[5] = (unsigned char) block & 0xff;
747 cmd[6] = cmd[9] = 0;
748 cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
749 cmd[8] = (unsigned char) realcount & 0xff;
750 }
751 else
752 {
753 if (realcount > 0xff)
754 {
755 realcount = 0xff;
756 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
757 }
758
759 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
760 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
761 cmd[3] = (unsigned char) block & 0xff;
762 cmd[4] = (unsigned char) realcount;
763 cmd[5] = 0;
764 }
765
766 #ifdef DEBUG
767 {
768 int i;
769 printk("ReadCD: %d %d %d %d\n",block, realcount, buffer, this_count);
770 printk("Use sg: %d\n", SCpnt->use_sg);
771 printk("Dumping command: ");
772 for(i=0; i<12; i++) printk("%2.2x ", cmd[i]);
773 printk("\n");
774 };
775 #endif
776
777
778
779
780
781
782
783
784
785
786
787 SCpnt->transfersize = (scsi_CDs[dev].sector_size > 1024) ?
788 1024 : scsi_CDs[dev].sector_size;
789
790 SCpnt->this_count = this_count;
791 scsi_do_cmd (SCpnt, (void *) cmd, buffer,
792 realcount * scsi_CDs[dev].sector_size,
793 rw_intr, SR_TIMEOUT, MAX_RETRIES);
794 }
795
796 static int sr_detect(Scsi_Device * SDp){
797
798
799 if(scsi_loadable_module_flag) return 0;
800 if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 0;
801
802 printk("Detected scsi CD-ROM sr%d at scsi%d, id %d, lun %d\n",
803 sr_template.dev_noticed++,
804 SDp->host->host_no , SDp->id, SDp->lun);
805
806 return 1;
807 }
808
809 static void sr_attach(Scsi_Device * SDp){
810 Scsi_CD * cpnt;
811 int i;
812
813
814
815 if(scsi_loadable_module_flag) return;
816 if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return;
817
818 if (sr_template.nr_dev >= sr_template.dev_max)
819 panic ("scsi_devices corrupt (sr)");
820
821 for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++)
822 if(!cpnt->device) break;
823
824 if(i >= sr_template.dev_max) panic ("scsi_devices corrupt (sr)");
825
826 SDp->scsi_request_fn = do_sr_request;
827 scsi_CDs[i].device = SDp;
828 sr_template.nr_dev++;
829 if(sr_template.nr_dev > sr_template.dev_max)
830 panic ("scsi_devices corrupt (sr)");
831 }
832
833
834 static void sr_init_done (Scsi_Cmnd * SCpnt)
835 {
836 struct request * req;
837
838 req = &SCpnt->request;
839 req->dev = 0xfffe;
840
841 if (req->sem != NULL) {
842 up(req->sem);
843 }
844 }
845
846 static void get_sectorsize(int i){
847 unsigned char cmd[10];
848 unsigned char *buffer;
849 int the_result, retries;
850 Scsi_Cmnd * SCpnt;
851
852 buffer = (unsigned char *) scsi_malloc(512);
853 SCpnt = allocate_device(NULL, scsi_CDs[i].device, 1);
854
855 retries = 3;
856 do {
857 cmd[0] = READ_CAPACITY;
858 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
859 memset ((void *) &cmd[2], 0, 8);
860 SCpnt->request.dev = 0xffff;
861 SCpnt->cmd_len = 0;
862
863 memset(buffer, 0, 8);
864
865 scsi_do_cmd (SCpnt,
866 (void *) cmd, (void *) buffer,
867 512, sr_init_done, SR_TIMEOUT,
868 MAX_RETRIES);
869
870 if (current == task[0])
871 while(SCpnt->request.dev != 0xfffe);
872 else
873 if (SCpnt->request.dev != 0xfffe){
874 struct semaphore sem = MUTEX_LOCKED;
875 SCpnt->request.sem = &sem;
876 down(&sem);
877
878 while (SCpnt->request.dev != 0xfffe) schedule();
879 };
880
881 the_result = SCpnt->result;
882 retries--;
883
884 } while(the_result && retries);
885
886 SCpnt->request.dev = -1;
887
888 wake_up(&SCpnt->device->device_wait);
889
890 if (the_result) {
891 scsi_CDs[i].capacity = 0x1fffff;
892 scsi_CDs[i].sector_size = 2048;
893 scsi_CDs[i].needs_sector_size = 1;
894 } else {
895 scsi_CDs[i].capacity = (buffer[0] << 24) |
896 (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
897 scsi_CDs[i].sector_size = (buffer[4] << 24) |
898 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
899 if(scsi_CDs[i].sector_size == 0) scsi_CDs[i].sector_size = 2048;
900 if(scsi_CDs[i].sector_size != 2048 &&
901 scsi_CDs[i].sector_size != 512) {
902 printk ("scd%d : unsupported sector size %d.\n",
903 i, scsi_CDs[i].sector_size);
904 scsi_CDs[i].capacity = 0;
905 scsi_CDs[i].needs_sector_size = 1;
906 };
907 if(scsi_CDs[i].sector_size == 2048)
908 scsi_CDs[i].capacity *= 4;
909 scsi_CDs[i].needs_sector_size = 0;
910 };
911 scsi_free(buffer, 512);
912 }
913
914 static void sr_init()
915 {
916 int i;
917 static int sr_registered = 0;
918
919 if(sr_template.dev_noticed == 0) return;
920
921 if(!sr_registered) {
922 if (register_blkdev(MAJOR_NR,"sr",&sr_fops)) {
923 printk("Unable to get major %d for SCSI-CD\n",MAJOR_NR);
924 return;
925 }
926 }
927
928
929 if(scsi_loadable_module_flag) return;
930
931 sr_template.dev_max = sr_template.dev_noticed;
932 scsi_CDs = (Scsi_CD *) scsi_init_malloc(sr_template.dev_max * sizeof(Scsi_CD));
933 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
934
935 sr_sizes = (int *) scsi_init_malloc(sr_template.dev_max * sizeof(int));
936 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
937
938 sr_blocksizes = (int *) scsi_init_malloc(sr_template.dev_max *
939 sizeof(int));
940 for(i=0;i<sr_template.dev_max;i++) sr_blocksizes[i] = 2048;
941 blksize_size[MAJOR_NR] = sr_blocksizes;
942
943 }
944
945 void sr_finish()
946 {
947 int i;
948
949 for (i = 0; i < sr_template.nr_dev; ++i)
950 {
951 get_sectorsize(i);
952 printk("Scd sectorsize = %d bytes\n", scsi_CDs[i].sector_size);
953 scsi_CDs[i].use = 1;
954 scsi_CDs[i].ten = 1;
955 scsi_CDs[i].remap = 1;
956 sr_sizes[i] = scsi_CDs[i].capacity;
957 }
958
959 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
960 blk_size[MAJOR_NR] = sr_sizes;
961
962
963
964
965 if(scsi_CDs[0].device->host->sg_tablesize)
966 read_ahead[MAJOR_NR] = 32;
967 else
968 read_ahead[MAJOR_NR] = 4;
969
970 return;
971 }