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