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: 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: 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) || ((rec[0] << 8) + rec[1] != 0x0a)) {
486 printk("sr_photocd: ioctl error (SONY): 0x%x\n",rc);
487 break;
488 }
489 sector = rec[11] + (rec[10] << 8) + (rec[9] << 16) + (rec[8] << 24);
490 is_xa = !!sector;
491 #ifdef DEBUG
492 if (sector)
493 printk ("sr_photocd: multisession CD detected. start: %lu\n",sector);
494 #endif
495 break;
496
497 case SCSI_MAN_NEC_OLDCDR:
498 case SCSI_MAN_UNKNOWN:
499 default:
500 sector = 0;
501 no_multi = 1;
502 break; }
503
504 scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = sector;
505 if (is_xa)
506 scsi_CDs[MINOR(inode->i_rdev)].xa_flags |= 0x01;
507 else
508 scsi_CDs[MINOR(inode->i_rdev)].xa_flags &= ~0x01;
509 if (no_multi)
510 scsi_CDs[MINOR(inode->i_rdev)].xa_flags |= 0x02;
511 return;
512 }
513
514 static int sr_open(struct inode * inode, struct file * filp)
515 {
516 if(MINOR(inode->i_rdev) >= sr_template.nr_dev ||
517 !scsi_CDs[MINOR(inode->i_rdev)].device) return -ENXIO;
518
519 if (filp->f_mode & 2)
520 return -EROFS;
521
522 check_disk_change(inode->i_rdev);
523
524 if(!scsi_CDs[MINOR(inode->i_rdev)].device->access_count++)
525 sr_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
526 if (scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)
527 (*scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)++;
528 if(sr_template.usage_count) (*sr_template.usage_count)++;
529
530 sr_photocd(inode);
531
532
533
534
535
536
537 if(scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
538 get_sectorsize(MINOR(inode->i_rdev));
539
540 return 0;
541 }
542
543
544
545
546
547
548
549
550 static void do_sr_request (void)
551 {
552 Scsi_Cmnd * SCpnt = NULL;
553 struct request * req = NULL;
554 Scsi_Device * SDev;
555 unsigned long flags;
556 int flag = 0;
557
558 while (1==1){
559 save_flags(flags);
560 cli();
561 if (CURRENT != NULL && CURRENT->rq_status == RQ_INACTIVE) {
562 restore_flags(flags);
563 return;
564 };
565
566 INIT_SCSI_REQUEST;
567
568 SDev = scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device;
569
570
571
572
573
574
575 if( SDev->was_reset )
576 {
577
578
579
580
581
582
583 if( SDev->removable && !intr_count )
584 {
585 scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, 0);
586 }
587 SDev->was_reset = 0;
588 }
589
590 if (flag++ == 0)
591 SCpnt = allocate_device(&CURRENT,
592 scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device, 0);
593 else SCpnt = NULL;
594 restore_flags(flags);
595
596
597
598
599
600
601
602
603 if (!SCpnt && sr_template.nr_dev > 1){
604 struct request *req1;
605 req1 = NULL;
606 save_flags(flags);
607 cli();
608 req = CURRENT;
609 while(req){
610 SCpnt = request_queueable(req,
611 scsi_CDs[DEVICE_NR(req->rq_dev)].device);
612 if(SCpnt) break;
613 req1 = req;
614 req = req->next;
615 };
616 if (SCpnt && req->rq_status == RQ_INACTIVE) {
617 if (req == CURRENT)
618 CURRENT = CURRENT->next;
619 else
620 req1->next = req->next;
621 };
622 restore_flags(flags);
623 };
624
625 if (!SCpnt)
626 return;
627
628 wake_up(&wait_for_request);
629
630
631 requeue_sr_request(SCpnt);
632 };
633 }
634
635 void requeue_sr_request (Scsi_Cmnd * SCpnt)
636 {
637 unsigned int dev, block, realcount;
638 unsigned char cmd[10], *buffer, tries;
639 int this_count, start, end_rec;
640
641 tries = 2;
642
643 repeat:
644 if(!SCpnt || SCpnt->request.rq_status == RQ_INACTIVE) {
645 do_sr_request();
646 return;
647 }
648
649 dev = MINOR(SCpnt->request.rq_dev);
650 block = SCpnt->request.sector;
651 buffer = NULL;
652 this_count = 0;
653
654 if (dev >= sr_template.nr_dev) {
655
656 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
657 tries = 2;
658 goto repeat;
659 }
660
661 if (!scsi_CDs[dev].use) {
662
663 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
664 tries = 2;
665 goto repeat;
666 }
667
668 if (scsi_CDs[dev].device->changed) {
669
670
671
672
673
674 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
675 tries = 2;
676 goto repeat;
677 }
678
679 switch (SCpnt->request.cmd)
680 {
681 case WRITE:
682 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
683 goto repeat;
684 break;
685 case READ :
686 cmd[0] = READ_6;
687 break;
688 default :
689 panic ("Unknown sr command %d\n", SCpnt->request.cmd);
690 }
691
692 cmd[1] = (SCpnt->lun << 5) & 0xe0;
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711 SCpnt->use_sg = 0;
712
713 if (SCpnt->host->sg_tablesize > 0 &&
714 (!need_isa_buffer ||
715 dma_free_sectors >= 10)) {
716 struct buffer_head * bh;
717 struct scatterlist * sgpnt;
718 int count, this_count_max;
719 bh = SCpnt->request.bh;
720 this_count = 0;
721 count = 0;
722 this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
723
724
725 this_count = SCpnt->request.sector % 4;
726 if(this_count) count++;
727 while(bh && count < SCpnt->host->sg_tablesize) {
728 if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
729 this_count += (bh->b_size >> 9);
730 count++;
731 bh = bh->b_reqnext;
732 };
733
734 end_rec = 0;
735 if(this_count % 4) {
736 if (count < SCpnt->host->sg_tablesize) {
737 count++;
738 end_rec = (4 - (this_count % 4)) << 9;
739 this_count += 4 - (this_count % 4);
740 } else {
741 count--;
742 this_count -= (this_count % 4);
743 };
744 };
745 SCpnt->use_sg = count;
746 count = 512;
747 while( count < (SCpnt->use_sg * sizeof(struct scatterlist)))
748 count = count << 1;
749 SCpnt->sglist_len = count;
750 sgpnt = (struct scatterlist * ) scsi_malloc(count);
751 if (!sgpnt) {
752 printk("Warning - running *really* short on DMA buffers\n");
753 SCpnt->use_sg = 0;
754 } else {
755 buffer = (unsigned char *) sgpnt;
756 count = 0;
757 bh = SCpnt->request.bh;
758 if(SCpnt->request.sector % 4) {
759 sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
760 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
761 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
762 sgpnt[count].alt_address = sgpnt[count].address;
763
764 count++;
765 };
766 for(bh = SCpnt->request.bh; count < SCpnt->use_sg;
767 count++, bh = bh->b_reqnext) {
768 if (bh) {
769 sgpnt[count].address = bh->b_data;
770 sgpnt[count].length = bh->b_size;
771 sgpnt[count].alt_address = NULL;
772 } else {
773 sgpnt[count].address = (char *) scsi_malloc(end_rec);
774 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
775 sgpnt[count].length = end_rec;
776 sgpnt[count].alt_address = sgpnt[count].address;
777 if (count+1 != SCpnt->use_sg) panic("Bad sr request list");
778 break;
779 };
780 if (((long) sgpnt[count].address) + sgpnt[count].length > ISA_DMA_THRESHOLD &&
781 SCpnt->host->unchecked_isa_dma) {
782 sgpnt[count].alt_address = sgpnt[count].address;
783
784
785
786 if(dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
787 sgpnt[count].address = NULL;
788 } else {
789 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
790 };
791
792
793
794
795 if(sgpnt[count].address == NULL){
796 printk("Warning: Running low on SCSI DMA buffers");
797
798 while(--count >= 0){
799 if(sgpnt[count].alt_address)
800 scsi_free(sgpnt[count].address, sgpnt[count].length);
801 };
802 SCpnt->use_sg = 0;
803 scsi_free(buffer, SCpnt->sglist_len);
804 break;
805 };
806 };
807 };
808 #ifdef DEBUG
809 printk("SR: %d %d %d %d %d *** ",SCpnt->use_sg, SCpnt->request.sector,
810 this_count,
811 SCpnt->request.current_nr_sectors,
812 SCpnt->request.nr_sectors);
813 for(count=0; count<SCpnt->use_sg; count++)
814 printk("SGlist: %d %x %x %x\n", count,
815 sgpnt[count].address,
816 sgpnt[count].alt_address,
817 sgpnt[count].length);
818 #endif
819 };
820 };
821
822 if (SCpnt->use_sg == 0){
823
824 if (!SCpnt->request.bh)
825 this_count = SCpnt->request.nr_sectors;
826 else
827 this_count = (SCpnt->request.bh->b_size >> 9);
828
829 start = block % 4;
830 if (start)
831 {
832 this_count = ((this_count > 4 - start) ?
833 (4 - start) : (this_count));
834 buffer = (unsigned char *) scsi_malloc(2048);
835 }
836 else if (this_count < 4)
837 {
838 buffer = (unsigned char *) scsi_malloc(2048);
839 }
840 else
841 {
842 this_count -= this_count % 4;
843 buffer = (unsigned char *) SCpnt->request.buffer;
844 if (((long) buffer) + (this_count << 9) > ISA_DMA_THRESHOLD &&
845 SCpnt->host->unchecked_isa_dma)
846 buffer = (unsigned char *) scsi_malloc(this_count << 9);
847 }
848 };
849
850 if (scsi_CDs[dev].sector_size == 2048)
851 block = block >> 2;
852 else
853 block = block & 0xfffffffc;
854
855 realcount = (this_count + 3) / 4;
856
857 if (scsi_CDs[dev].sector_size == 512) realcount = realcount << 2;
858
859 if (((realcount > 0xff) || (block > 0x1fffff)) && scsi_CDs[dev].ten)
860 {
861 if (realcount > 0xffff)
862 {
863 realcount = 0xffff;
864 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
865 }
866
867 cmd[0] += READ_10 - READ_6 ;
868 cmd[2] = (unsigned char) (block >> 24) & 0xff;
869 cmd[3] = (unsigned char) (block >> 16) & 0xff;
870 cmd[4] = (unsigned char) (block >> 8) & 0xff;
871 cmd[5] = (unsigned char) block & 0xff;
872 cmd[6] = cmd[9] = 0;
873 cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
874 cmd[8] = (unsigned char) realcount & 0xff;
875 }
876 else
877 {
878 if (realcount > 0xff)
879 {
880 realcount = 0xff;
881 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
882 }
883
884 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
885 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
886 cmd[3] = (unsigned char) block & 0xff;
887 cmd[4] = (unsigned char) realcount;
888 cmd[5] = 0;
889 }
890
891 #ifdef DEBUG
892 {
893 int i;
894 printk("ReadCD: %d %d %d %d\n",block, realcount, buffer, this_count);
895 printk("Use sg: %d\n", SCpnt->use_sg);
896 printk("Dumping command: ");
897 for(i=0; i<12; i++) printk("%2.2x ", cmd[i]);
898 printk("\n");
899 };
900 #endif
901
902
903
904
905
906
907
908
909
910
911
912 SCpnt->transfersize = (scsi_CDs[dev].sector_size > 1024) ?
913 1024 : scsi_CDs[dev].sector_size;
914
915 SCpnt->this_count = this_count;
916 scsi_do_cmd (SCpnt, (void *) cmd, buffer,
917 realcount * scsi_CDs[dev].sector_size,
918 rw_intr, SR_TIMEOUT, MAX_RETRIES);
919 }
920
921 static int sr_detect(Scsi_Device * SDp){
922
923 if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 0;
924
925 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
926 sr_template.dev_noticed++,
927 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
928
929 return 1;
930 }
931
932 static int sr_attach(Scsi_Device * SDp){
933 Scsi_CD * cpnt;
934 int i;
935
936 if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 1;
937
938 if (sr_template.nr_dev >= sr_template.dev_max)
939 {
940 SDp->attached--;
941 return 1;
942 }
943
944 for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++)
945 if(!cpnt->device) break;
946
947 if(i >= sr_template.dev_max) panic ("scsi_devices corrupt (sr)");
948
949 SDp->scsi_request_fn = do_sr_request;
950 scsi_CDs[i].device = SDp;
951 sr_template.nr_dev++;
952 if(sr_template.nr_dev > sr_template.dev_max)
953 panic ("scsi_devices corrupt (sr)");
954 return 0;
955 }
956
957
958 static void sr_init_done (Scsi_Cmnd * SCpnt)
959 {
960 struct request * req;
961
962 req = &SCpnt->request;
963 req->rq_status = RQ_SCSI_DONE;
964
965 if (req->sem != NULL) {
966 up(req->sem);
967 }
968 }
969
970 static void get_sectorsize(int i){
971 unsigned char cmd[10];
972 unsigned char *buffer;
973 int the_result, retries;
974 Scsi_Cmnd * SCpnt;
975
976 buffer = (unsigned char *) scsi_malloc(512);
977 SCpnt = allocate_device(NULL, scsi_CDs[i].device, 1);
978
979 retries = 3;
980 do {
981 cmd[0] = READ_CAPACITY;
982 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
983 memset ((void *) &cmd[2], 0, 8);
984 SCpnt->request.rq_status = RQ_SCSI_BUSY;
985 SCpnt->cmd_len = 0;
986
987 memset(buffer, 0, 8);
988
989
990 {
991 struct semaphore sem = MUTEX_LOCKED;
992 SCpnt->request.sem = &sem;
993 scsi_do_cmd (SCpnt,
994 (void *) cmd, (void *) buffer,
995 512, sr_init_done, SR_TIMEOUT,
996 MAX_RETRIES);
997 down(&sem);
998 }
999
1000 the_result = SCpnt->result;
1001 retries--;
1002
1003 } while(the_result && retries);
1004
1005 SCpnt->request.rq_status = RQ_INACTIVE;
1006
1007 wake_up(&SCpnt->device->device_wait);
1008
1009 if (the_result) {
1010 scsi_CDs[i].capacity = 0x1fffff;
1011 scsi_CDs[i].sector_size = 2048;
1012 scsi_CDs[i].needs_sector_size = 1;
1013 } else {
1014 scsi_CDs[i].capacity = (buffer[0] << 24) |
1015 (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
1016 scsi_CDs[i].sector_size = (buffer[4] << 24) |
1017 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1018 if(scsi_CDs[i].sector_size == 0) scsi_CDs[i].sector_size = 2048;
1019 if(scsi_CDs[i].sector_size != 2048 &&
1020 scsi_CDs[i].sector_size != 512) {
1021 printk ("scd%d : unsupported sector size %d.\n",
1022 i, scsi_CDs[i].sector_size);
1023 scsi_CDs[i].capacity = 0;
1024 scsi_CDs[i].needs_sector_size = 1;
1025 };
1026 if(scsi_CDs[i].sector_size == 2048)
1027 scsi_CDs[i].capacity *= 4;
1028 scsi_CDs[i].needs_sector_size = 0;
1029 sr_sizes[i] = scsi_CDs[i].capacity;
1030 };
1031 scsi_free(buffer, 512);
1032 }
1033
1034 static int sr_registered = 0;
1035
1036 static int sr_init()
1037 {
1038 int i;
1039
1040 if(sr_template.dev_noticed == 0) return 0;
1041
1042 if(!sr_registered) {
1043 if (register_blkdev(MAJOR_NR,"sr",&sr_fops)) {
1044 printk("Unable to get major %d for SCSI-CD\n",MAJOR_NR);
1045 return 1;
1046 }
1047 sr_registered++;
1048 }
1049
1050
1051 if (scsi_CDs) return 0;
1052 sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
1053 scsi_CDs = (Scsi_CD *) scsi_init_malloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
1054 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
1055
1056 sr_sizes = (int *) scsi_init_malloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
1057 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
1058
1059 sr_blocksizes = (int *) scsi_init_malloc(sr_template.dev_max *
1060 sizeof(int), GFP_ATOMIC);
1061 for(i=0;i<sr_template.dev_max;i++) sr_blocksizes[i] = 2048;
1062 blksize_size[MAJOR_NR] = sr_blocksizes;
1063 return 0;
1064 }
1065
1066 void sr_finish()
1067 {
1068 int i;
1069
1070 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1071 blk_size[MAJOR_NR] = sr_sizes;
1072
1073 for (i = 0; i < sr_template.nr_dev; ++i)
1074 {
1075
1076
1077 if (scsi_CDs[i].capacity) continue;
1078 scsi_CDs[i].capacity = 0x1fffff;
1079 scsi_CDs[i].sector_size = 2048;
1080 scsi_CDs[i].needs_sector_size = 1;
1081 #if 0
1082
1083 get_sectorsize(i);
1084 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
1085 #endif
1086 scsi_CDs[i].use = 1;
1087 scsi_CDs[i].ten = 1;
1088 scsi_CDs[i].remap = 1;
1089 scsi_CDs[i].auto_eject = 0;
1090 sr_sizes[i] = scsi_CDs[i].capacity;
1091 }
1092
1093
1094
1095
1096
1097 if(scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
1098 read_ahead[MAJOR_NR] = 32;
1099 else
1100 read_ahead[MAJOR_NR] = 4;
1101
1102 return;
1103 }
1104
1105 static void sr_detach(Scsi_Device * SDp)
1106 {
1107 Scsi_CD * cpnt;
1108 int i;
1109
1110 for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++)
1111 if(cpnt->device == SDp) {
1112 kdev_t devi = MKDEV(MAJOR_NR, i);
1113
1114
1115
1116
1117
1118 invalidate_inodes(devi);
1119 invalidate_buffers(devi);
1120
1121
1122
1123
1124
1125 cpnt->device = NULL;
1126 cpnt->capacity = 0;
1127 SDp->attached--;
1128 sr_template.nr_dev--;
1129 sr_template.dev_noticed--;
1130 sr_sizes[i] = 0;
1131 return;
1132 }
1133 return;
1134 }
1135
1136
1137 #ifdef MODULE
1138
1139 int init_module(void) {
1140 sr_template.usage_count = &mod_use_count_;
1141 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
1142 }
1143
1144 void cleanup_module( void)
1145 {
1146 if (MOD_IN_USE) {
1147 printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
1148 return;
1149 }
1150 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
1151 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1152 sr_registered--;
1153 if(scsi_CDs != NULL) {
1154 scsi_init_free((char *) scsi_CDs,
1155 (sr_template.dev_noticed + SR_EXTRA_DEVS)
1156 * sizeof(Scsi_CD));
1157
1158 scsi_init_free((char *) sr_sizes, sr_template.dev_max * sizeof(int));
1159 scsi_init_free((char *) sr_blocksizes, sr_template.dev_max * sizeof(int));
1160 }
1161
1162 blksize_size[MAJOR_NR] = NULL;
1163 blk_dev[MAJOR_NR].request_fn = NULL;
1164 blk_size[MAJOR_NR] = NULL;
1165 read_ahead[MAJOR_NR] = 0;
1166
1167 sr_template.dev_max = 0;
1168 }
1169 #endif
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188