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