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 <linux/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(kdev_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(kdev_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.rq_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.rq_dev)].ten) {
261 scsi_CDs[DEVICE_NR(SCpnt->request.rq_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.rq_dev)].device->host->host_no,
285 scsi_CDs[DEVICE_NR(SCpnt->request.rq_dev)].device->id,
286 scsi_CDs[DEVICE_NR(SCpnt->request.rq_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
320
321 static void sr_photocd(struct inode *inode)
322 {
323 unsigned long sector,min,sec,frame;
324 unsigned char buf[40];
325 unsigned char *cmd;
326 unsigned char *send;
327 unsigned char *rec;
328 int rc,is_xa,no_multi;
329
330 if (scsi_CDs[MINOR(inode->i_rdev)].xa_flags & 0x02) {
331 #ifdef DEBUG
332 printk("sr_photocd: CDROM and/or the driver does not support multisession CD's");
333 #endif
334 return;
335 }
336
337 if (!suser()) {
338
339
340
341
342
343
344 if (1 == scsi_CDs[MINOR(inode->i_rdev)].device->access_count) {
345 scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = 0;
346 scsi_CDs[MINOR(inode->i_rdev)].xa_flags &= ~0x01;
347 }
348 return;
349 }
350
351 sector = 0;
352 is_xa = 0;
353 no_multi = 0;
354 cmd = rec = &buf[8];
355
356 switch(scsi_CDs[MINOR(inode->i_rdev)].device->manufacturer) {
357
358 case SCSI_MAN_NEC:
359 #ifdef DEBUG
360 printk("sr_photocd: use NEC code\n");
361 #endif
362 memset(buf,0,40);
363 *((unsigned long*)buf) = 0x0;
364 *((unsigned long*)buf+1) = 0x16;
365 cmd[0] = 0xde;
366 cmd[1] = 0x03;
367 cmd[2] = 0xb0;
368 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
369 SCSI_IOCTL_SEND_COMMAND, buf);
370 if (rc != 0) {
371 printk("sr_photocd: ioctl error (NEC): 0x%x\n",rc);
372 break;
373 }
374 if (rec[14] != 0 && rec[14] != 0xb0) {
375 printk("sr_photocd: Hmm, seems the CDROM doesn't support multisession CD's\n");
376 no_multi = 1;
377 break;
378 }
379 min = (unsigned long) rec[15]/16*10 + (unsigned long) rec[15]%16;
380 sec = (unsigned long) rec[16]/16*10 + (unsigned long) rec[16]%16;
381 frame = (unsigned long) rec[17]/16*10 + (unsigned long) rec[17]%16;
382 sector = min*CD_SECS*CD_FRAMES + sec*CD_FRAMES + frame;
383 is_xa = (rec[14] == 0xb0);
384 #ifdef DEBUG
385 if (sector) {
386 printk("sr_photocd: multisession CD detected. start: %lu\n",sector);
387 }
388 #endif
389 break;
390
391 case SCSI_MAN_TOSHIBA:
392 #ifdef DEBUG
393 printk("sr_photocd: use TOSHIBA code\n");
394 #endif
395
396
397
398 memset(buf,0,40);
399 *((unsigned long*)buf) = 0;
400 *((unsigned long*)buf+1) = 4;
401 cmd[0] = 0xc7;
402 cmd[1] = 3;
403 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
404 SCSI_IOCTL_SEND_COMMAND, buf);
405 if (rc != 0) {
406 if (rc == 0x28000002) {
407
408
409
410 if (kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
411 SCSI_IOCTL_TEST_UNIT_READY, NULL)) {
412 printk("sr_photocd: drive not ready\n");
413 } else {
414 printk("sr_photocd: Hmm, seems the CDROM doesn't support multisession CD's\n");
415 no_multi = 1;
416 }
417 } else
418 printk("sr_photocd: ioctl error (TOSHIBA #1): 0x%x\n",rc);
419 break;
420 }
421 is_xa = (rec[0] == 0x20);
422 min = (unsigned long) rec[1]/16*10 + (unsigned long) rec[1]%16;
423 sec = (unsigned long) rec[2]/16*10 + (unsigned long) rec[2]%16;
424 frame = (unsigned long) rec[3]/16*10 + (unsigned long) rec[3]%16;
425 sector = min*CD_SECS*CD_FRAMES + sec*CD_FRAMES + frame;
426 if (sector) {
427 sector -= CD_BLOCK_OFFSET;
428 #ifdef DEBUG
429 printk("sr_photocd: multisession CD detected: start: %lu\n",sector);
430 #endif
431 }
432
433
434 memset(buf,0,40);
435 *((unsigned long*)buf) = 0;
436 *((unsigned long*)buf+1) = 12;
437 cmd[0] = 0x1a;
438 cmd[2] = 1;
439 cmd[4] = 12;
440 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
441 SCSI_IOCTL_SEND_COMMAND, buf);
442 if (rc != 0) {
443 printk("sr_photocd: ioctl error (TOSHIBA #2): 0x%x\n",rc);
444 break;
445 }
446 #ifdef DEBUG
447 printk("sr_photocd: get_density: 0x%x\n",rec[4]);
448 #endif
449
450
451 if ((rec[4] != 0x81 && is_xa) || (rec[4] != 0 && !is_xa)) {
452 #ifdef DEBUG
453 printk("sr_photocd: doing set_density\n");
454 #endif
455 memset(buf,0,40);
456 *((unsigned long*)buf) = 12;
457 *((unsigned long*)buf+1) = 0;
458 cmd[0] = 0x15;
459 cmd[1] = (1 << 4);
460 cmd[4] = 12;
461 send = &cmd[6];
462 send[ 3] = 0x08;
463 send[ 4] = (is_xa) ? 0x81 : 0;
464 send[10] = 0x08;
465 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
466 SCSI_IOCTL_SEND_COMMAND, buf);
467 if (rc != 0) {
468 printk("sr_photocd: ioctl error (TOSHIBA #3): 0x%x\n",rc);
469 }
470
471 scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size = 1;
472 }
473 break;
474
475 case SCSI_MAN_SONY:
476 case SCSI_MAN_PIONEER:
477 #ifdef DEBUG
478 printk("sr_photocd: use SONY/PIONEER code\n");
479 #endif
480 memset(buf,0,40);
481 *((unsigned long*)buf) = 0x0;
482 *((unsigned long*)buf+1) = 0x0c;
483 cmd[0] = 0x43;
484 cmd[8] = 0x0c;
485 cmd[9] = 0x40;
486 rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
487 SCSI_IOCTL_SEND_COMMAND, buf);
488
489 if ((rc != 0) || ((rec[0] << 8) + rec[1] != 0x0a)) {
490 printk("sr_photocd: ioctl error (SONY): 0x%x\n",rc);
491 break;
492 }
493 sector = rec[11] + (rec[10] << 8) + (rec[9] << 16) + (rec[8] << 24);
494 is_xa = !!sector;
495 #ifdef DEBUG
496 if (sector)
497 printk ("sr_photocd: multisession CD detected. start: %lu\n",sector);
498 #endif
499 break;
500
501 case SCSI_MAN_NEC_OLDCDR:
502 case SCSI_MAN_UNKNOWN:
503 default:
504 sector = 0;
505 no_multi = 1;
506 break; }
507
508 scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = sector;
509 if (is_xa)
510 scsi_CDs[MINOR(inode->i_rdev)].xa_flags |= 0x01;
511 else
512 scsi_CDs[MINOR(inode->i_rdev)].xa_flags &= ~0x01;
513 if (no_multi)
514 scsi_CDs[MINOR(inode->i_rdev)].xa_flags |= 0x02;
515 return;
516 }
517
518 static int sr_open(struct inode * inode, struct file * filp)
519 {
520 if(MINOR(inode->i_rdev) >= sr_template.nr_dev ||
521 !scsi_CDs[MINOR(inode->i_rdev)].device) return -ENXIO;
522
523 if (filp->f_mode & 2)
524 return -EROFS;
525
526 check_disk_change(inode->i_rdev);
527
528 if(!scsi_CDs[MINOR(inode->i_rdev)].device->access_count++)
529 sr_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
530 if (scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)
531 (*scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)++;
532 if(sr_template.usage_count) (*sr_template.usage_count)++;
533
534 sr_photocd(inode);
535
536
537
538
539
540
541 if(scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
542 get_sectorsize(MINOR(inode->i_rdev));
543
544 return 0;
545 }
546
547
548
549
550
551
552
553
554 static void do_sr_request (void)
555 {
556 Scsi_Cmnd * SCpnt = NULL;
557 struct request * req = NULL;
558 Scsi_Device * SDev;
559 unsigned long flags;
560 int flag = 0;
561
562 while (1==1){
563 save_flags(flags);
564 cli();
565 if (CURRENT != NULL && CURRENT->rq_status == RQ_INACTIVE) {
566 restore_flags(flags);
567 return;
568 };
569
570 INIT_SCSI_REQUEST;
571
572 SDev = scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device;
573
574
575
576
577
578
579 if( SDev->was_reset )
580 {
581
582
583
584
585
586
587 if( SDev->removable && !intr_count )
588 {
589 scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, 0);
590 }
591 SDev->was_reset = 0;
592 }
593
594 if (flag++ == 0)
595 SCpnt = allocate_device(&CURRENT,
596 scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device, 0);
597 else SCpnt = NULL;
598 restore_flags(flags);
599
600
601
602
603
604
605
606
607 if (!SCpnt && sr_template.nr_dev > 1){
608 struct request *req1;
609 req1 = NULL;
610 save_flags(flags);
611 cli();
612 req = CURRENT;
613 while(req){
614 SCpnt = request_queueable(req,
615 scsi_CDs[DEVICE_NR(req->rq_dev)].device);
616 if(SCpnt) break;
617 req1 = req;
618 req = req->next;
619 };
620 if (SCpnt && req->rq_status == RQ_INACTIVE) {
621 if (req == CURRENT)
622 CURRENT = CURRENT->next;
623 else
624 req1->next = req->next;
625 };
626 restore_flags(flags);
627 };
628
629 if (!SCpnt)
630 return;
631
632 wake_up(&wait_for_request);
633
634
635 requeue_sr_request(SCpnt);
636 };
637 }
638
639 void requeue_sr_request (Scsi_Cmnd * SCpnt)
640 {
641 unsigned int dev, block, realcount;
642 unsigned char cmd[10], *buffer, tries;
643 int this_count, start, end_rec;
644
645 tries = 2;
646
647 repeat:
648 if(!SCpnt || SCpnt->request.rq_status == RQ_INACTIVE) {
649 do_sr_request();
650 return;
651 }
652
653 dev = MINOR(SCpnt->request.rq_dev);
654 block = SCpnt->request.sector;
655 buffer = NULL;
656 this_count = 0;
657
658 if (dev >= sr_template.nr_dev) {
659
660 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
661 tries = 2;
662 goto repeat;
663 }
664
665 if (!scsi_CDs[dev].use) {
666
667 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
668 tries = 2;
669 goto repeat;
670 }
671
672 if (scsi_CDs[dev].device->changed) {
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
715 SCpnt->use_sg = 0;
716
717 if (SCpnt->host->sg_tablesize > 0 &&
718 (!need_isa_buffer ||
719 dma_free_sectors >= 10)) {
720 struct buffer_head * bh;
721 struct scatterlist * sgpnt;
722 int count, this_count_max;
723 bh = SCpnt->request.bh;
724 this_count = 0;
725 count = 0;
726 this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
727
728
729 this_count = SCpnt->request.sector % 4;
730 if(this_count) count++;
731 while(bh && count < SCpnt->host->sg_tablesize) {
732 if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
733 this_count += (bh->b_size >> 9);
734 count++;
735 bh = bh->b_reqnext;
736 };
737
738 end_rec = 0;
739 if(this_count % 4) {
740 if (count < SCpnt->host->sg_tablesize) {
741 count++;
742 end_rec = (4 - (this_count % 4)) << 9;
743 this_count += 4 - (this_count % 4);
744 } else {
745 count--;
746 this_count -= (this_count % 4);
747 };
748 };
749 SCpnt->use_sg = count;
750 count = 512;
751 while( count < (SCpnt->use_sg * sizeof(struct scatterlist)))
752 count = count << 1;
753 SCpnt->sglist_len = count;
754 sgpnt = (struct scatterlist * ) scsi_malloc(count);
755 if (!sgpnt) {
756 printk("Warning - running *really* short on DMA buffers\n");
757 SCpnt->use_sg = 0;
758 } else {
759 buffer = (unsigned char *) sgpnt;
760 count = 0;
761 bh = SCpnt->request.bh;
762 if(SCpnt->request.sector % 4) {
763 sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
764 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
765 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
766 sgpnt[count].alt_address = sgpnt[count].address;
767
768 count++;
769 };
770 for(bh = SCpnt->request.bh; count < SCpnt->use_sg;
771 count++, bh = bh->b_reqnext) {
772 if (bh) {
773 sgpnt[count].address = bh->b_data;
774 sgpnt[count].length = bh->b_size;
775 sgpnt[count].alt_address = NULL;
776 } else {
777 sgpnt[count].address = (char *) scsi_malloc(end_rec);
778 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
779 sgpnt[count].length = end_rec;
780 sgpnt[count].alt_address = sgpnt[count].address;
781 if (count+1 != SCpnt->use_sg) panic("Bad sr request list");
782 break;
783 };
784 if (((long) sgpnt[count].address) + sgpnt[count].length > ISA_DMA_THRESHOLD &&
785 SCpnt->host->unchecked_isa_dma) {
786 sgpnt[count].alt_address = sgpnt[count].address;
787
788
789
790 if(dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
791 sgpnt[count].address = NULL;
792 } else {
793 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
794 };
795
796
797
798
799 if(sgpnt[count].address == NULL){
800 printk("Warning: Running low on SCSI DMA buffers");
801
802 while(--count >= 0){
803 if(sgpnt[count].alt_address)
804 scsi_free(sgpnt[count].address, sgpnt[count].length);
805 };
806 SCpnt->use_sg = 0;
807 scsi_free(buffer, SCpnt->sglist_len);
808 break;
809 };
810 };
811 };
812 #ifdef DEBUG
813 printk("SR: %d %d %d %d %d *** ",SCpnt->use_sg, SCpnt->request.sector,
814 this_count,
815 SCpnt->request.current_nr_sectors,
816 SCpnt->request.nr_sectors);
817 for(count=0; count<SCpnt->use_sg; count++)
818 printk("SGlist: %d %x %x %x\n", count,
819 sgpnt[count].address,
820 sgpnt[count].alt_address,
821 sgpnt[count].length);
822 #endif
823 };
824 };
825
826 if (SCpnt->use_sg == 0){
827
828 if (!SCpnt->request.bh)
829 this_count = SCpnt->request.nr_sectors;
830 else
831 this_count = (SCpnt->request.bh->b_size >> 9);
832
833 start = block % 4;
834 if (start)
835 {
836 this_count = ((this_count > 4 - start) ?
837 (4 - start) : (this_count));
838 buffer = (unsigned char *) scsi_malloc(2048);
839 }
840 else if (this_count < 4)
841 {
842 buffer = (unsigned char *) scsi_malloc(2048);
843 }
844 else
845 {
846 this_count -= this_count % 4;
847 buffer = (unsigned char *) SCpnt->request.buffer;
848 if (((long) buffer) + (this_count << 9) > ISA_DMA_THRESHOLD &&
849 SCpnt->host->unchecked_isa_dma)
850 buffer = (unsigned char *) scsi_malloc(this_count << 9);
851 }
852 };
853
854 if (scsi_CDs[dev].sector_size == 2048)
855 block = block >> 2;
856 else
857 block = block & 0xfffffffc;
858
859 realcount = (this_count + 3) / 4;
860
861 if (scsi_CDs[dev].sector_size == 512) realcount = realcount << 2;
862
863 if (((realcount > 0xff) || (block > 0x1fffff)) && scsi_CDs[dev].ten)
864 {
865 if (realcount > 0xffff)
866 {
867 realcount = 0xffff;
868 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
869 }
870
871 cmd[0] += READ_10 - READ_6 ;
872 cmd[2] = (unsigned char) (block >> 24) & 0xff;
873 cmd[3] = (unsigned char) (block >> 16) & 0xff;
874 cmd[4] = (unsigned char) (block >> 8) & 0xff;
875 cmd[5] = (unsigned char) block & 0xff;
876 cmd[6] = cmd[9] = 0;
877 cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
878 cmd[8] = (unsigned char) realcount & 0xff;
879 }
880 else
881 {
882 if (realcount > 0xff)
883 {
884 realcount = 0xff;
885 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
886 }
887
888 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
889 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
890 cmd[3] = (unsigned char) block & 0xff;
891 cmd[4] = (unsigned char) realcount;
892 cmd[5] = 0;
893 }
894
895 #ifdef DEBUG
896 {
897 int i;
898 printk("ReadCD: %d %d %d %d\n",block, realcount, buffer, this_count);
899 printk("Use sg: %d\n", SCpnt->use_sg);
900 printk("Dumping command: ");
901 for(i=0; i<12; i++) printk("%2.2x ", cmd[i]);
902 printk("\n");
903 };
904 #endif
905
906
907
908
909
910
911
912
913
914
915
916 SCpnt->transfersize = (scsi_CDs[dev].sector_size > 1024) ?
917 1024 : scsi_CDs[dev].sector_size;
918
919 SCpnt->this_count = this_count;
920 scsi_do_cmd (SCpnt, (void *) cmd, buffer,
921 realcount * scsi_CDs[dev].sector_size,
922 rw_intr, SR_TIMEOUT, MAX_RETRIES);
923 }
924
925 static int sr_detect(Scsi_Device * SDp){
926
927 if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 0;
928
929 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
930 sr_template.dev_noticed++,
931 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
932
933 return 1;
934 }
935
936 static int sr_attach(Scsi_Device * SDp){
937 Scsi_CD * cpnt;
938 int i;
939
940 if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 1;
941
942 if (sr_template.nr_dev >= sr_template.dev_max)
943 {
944 SDp->attached--;
945 return 1;
946 }
947
948 for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++)
949 if(!cpnt->device) break;
950
951 if(i >= sr_template.dev_max) panic ("scsi_devices corrupt (sr)");
952
953 SDp->scsi_request_fn = do_sr_request;
954 scsi_CDs[i].device = SDp;
955 sr_template.nr_dev++;
956 if(sr_template.nr_dev > sr_template.dev_max)
957 panic ("scsi_devices corrupt (sr)");
958 return 0;
959 }
960
961
962 static void sr_init_done (Scsi_Cmnd * SCpnt)
963 {
964 struct request * req;
965
966 req = &SCpnt->request;
967 req->rq_status = RQ_SCSI_DONE;
968
969 if (req->sem != NULL) {
970 up(req->sem);
971 }
972 }
973
974 static void get_sectorsize(int i){
975 unsigned char cmd[10];
976 unsigned char *buffer;
977 int the_result, retries;
978 Scsi_Cmnd * SCpnt;
979
980 buffer = (unsigned char *) scsi_malloc(512);
981 SCpnt = allocate_device(NULL, scsi_CDs[i].device, 1);
982
983 retries = 3;
984 do {
985 cmd[0] = READ_CAPACITY;
986 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
987 memset ((void *) &cmd[2], 0, 8);
988 SCpnt->request.rq_status = RQ_SCSI_BUSY;
989 SCpnt->cmd_len = 0;
990
991 memset(buffer, 0, 8);
992
993
994 {
995 struct semaphore sem = MUTEX_LOCKED;
996 SCpnt->request.sem = &sem;
997 scsi_do_cmd (SCpnt,
998 (void *) cmd, (void *) buffer,
999 512, sr_init_done, SR_TIMEOUT,
1000 MAX_RETRIES);
1001 down(&sem);
1002 }
1003
1004 the_result = SCpnt->result;
1005 retries--;
1006
1007 } while(the_result && retries);
1008
1009 SCpnt->request.rq_status = RQ_INACTIVE;
1010
1011 wake_up(&SCpnt->device->device_wait);
1012
1013 if (the_result) {
1014 scsi_CDs[i].capacity = 0x1fffff;
1015 scsi_CDs[i].sector_size = 2048;
1016 scsi_CDs[i].needs_sector_size = 1;
1017 } else {
1018 scsi_CDs[i].capacity = (buffer[0] << 24) |
1019 (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
1020 scsi_CDs[i].sector_size = (buffer[4] << 24) |
1021 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1022 if(scsi_CDs[i].sector_size == 0) scsi_CDs[i].sector_size = 2048;
1023 if(scsi_CDs[i].sector_size != 2048 &&
1024 scsi_CDs[i].sector_size != 512) {
1025 printk ("scd%d : unsupported sector size %d.\n",
1026 i, scsi_CDs[i].sector_size);
1027 scsi_CDs[i].capacity = 0;
1028 scsi_CDs[i].needs_sector_size = 1;
1029 };
1030 if(scsi_CDs[i].sector_size == 2048)
1031 scsi_CDs[i].capacity *= 4;
1032 scsi_CDs[i].needs_sector_size = 0;
1033 sr_sizes[i] = scsi_CDs[i].capacity;
1034 };
1035 scsi_free(buffer, 512);
1036 }
1037
1038 static int sr_registered = 0;
1039
1040 static int sr_init()
1041 {
1042 int i;
1043
1044 if(sr_template.dev_noticed == 0) return 0;
1045
1046 if(!sr_registered) {
1047 if (register_blkdev(MAJOR_NR,"sr",&sr_fops)) {
1048 printk("Unable to get major %d for SCSI-CD\n",MAJOR_NR);
1049 return 1;
1050 }
1051 sr_registered++;
1052 }
1053
1054
1055 if (scsi_CDs) return 0;
1056 sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
1057 scsi_CDs = (Scsi_CD *) scsi_init_malloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
1058 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
1059
1060 sr_sizes = (int *) scsi_init_malloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
1061 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
1062
1063 sr_blocksizes = (int *) scsi_init_malloc(sr_template.dev_max *
1064 sizeof(int), GFP_ATOMIC);
1065 for(i=0;i<sr_template.dev_max;i++) sr_blocksizes[i] = 2048;
1066 blksize_size[MAJOR_NR] = sr_blocksizes;
1067 return 0;
1068 }
1069
1070 void sr_finish()
1071 {
1072 int i;
1073
1074 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1075 blk_size[MAJOR_NR] = sr_sizes;
1076
1077 for (i = 0; i < sr_template.nr_dev; ++i)
1078 {
1079
1080
1081 if (scsi_CDs[i].capacity) continue;
1082 scsi_CDs[i].capacity = 0x1fffff;
1083 scsi_CDs[i].sector_size = 2048;
1084 scsi_CDs[i].needs_sector_size = 1;
1085 #if 0
1086
1087 get_sectorsize(i);
1088 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
1089 #endif
1090 scsi_CDs[i].use = 1;
1091 scsi_CDs[i].ten = 1;
1092 scsi_CDs[i].remap = 1;
1093 scsi_CDs[i].auto_eject = 0;
1094 sr_sizes[i] = scsi_CDs[i].capacity;
1095 }
1096
1097
1098
1099
1100
1101 if(scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
1102 read_ahead[MAJOR_NR] = 32;
1103 else
1104 read_ahead[MAJOR_NR] = 4;
1105
1106 return;
1107 }
1108
1109 static void sr_detach(Scsi_Device * SDp)
1110 {
1111 Scsi_CD * cpnt;
1112 int i;
1113
1114 for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++)
1115 if(cpnt->device == SDp) {
1116 kdev_t devi = MKDEV(MAJOR_NR, i);
1117
1118
1119
1120
1121
1122 invalidate_inodes(devi);
1123 invalidate_buffers(devi);
1124
1125
1126
1127
1128
1129 cpnt->device = NULL;
1130 cpnt->capacity = 0;
1131 SDp->attached--;
1132 sr_template.nr_dev--;
1133 sr_template.dev_noticed--;
1134 sr_sizes[i] = 0;
1135 return;
1136 }
1137 return;
1138 }
1139
1140
1141 #ifdef MODULE
1142 #include <linux/module.h>
1143 #include <linux/version.h>
1144
1145 char kernel_version[] = UTS_RELEASE;
1146
1147 int init_module(void) {
1148 sr_template.usage_count = &mod_use_count_;
1149 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
1150 }
1151
1152 void cleanup_module( void)
1153 {
1154 if (MOD_IN_USE) {
1155 printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
1156 return;
1157 }
1158 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
1159 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1160 sr_registered--;
1161 if(scsi_CDs != NULL) {
1162 scsi_init_free((char *) scsi_CDs,
1163 (sr_template.dev_noticed + SR_EXTRA_DEVS)
1164 * sizeof(Scsi_CD));
1165
1166 scsi_init_free((char *) sr_sizes, sr_template.dev_max * sizeof(int));
1167 scsi_init_free((char *) sr_blocksizes, sr_template.dev_max * sizeof(int));
1168 }
1169
1170 blksize_size[MAJOR_NR] = NULL;
1171 blk_dev[MAJOR_NR].request_fn = NULL;
1172 blk_size[MAJOR_NR] = NULL;
1173 read_ahead[MAJOR_NR] = 0;
1174
1175 sr_template.dev_max = 0;
1176 }
1177 #endif
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196