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