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