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