1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <asm/segment.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/blk.h>
#include "scsi.h"
#include "hosts.h"
#include "sr.h"
#include <scsi/scsi_ioctl.h>
#include <linux/cdrom.h>
extern void get_sectorsize(int);
extern void sr_photocd(struct inode *);
#define IOCTL_RETRIES 3
/* The CDROM is fairly slow, so we need a little extra time */
/* In fact, it is very slow if it has to spin up first */
#define IOCTL_TIMEOUT 3000
static void sr_ioctl_done(Scsi_Cmnd * SCpnt)
{
struct request * req;
req = &SCpnt->request;
req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
if (req->sem != NULL) {
up(req->sem);
}
}
/* We do our own retries because we want to know what the specific
error code is. Normally the UNIT_ATTENTION code will automatically
clear after one error */
static int do_ioctl(int target, unsigned char * sr_cmd, void * buffer, unsigned buflength)
{
Scsi_Cmnd * SCpnt;
int result;
SCpnt = allocate_device(NULL, scsi_CDs[target].device, 1);
{
struct semaphore sem = MUTEX_LOCKED;
SCpnt->request.sem = &sem;
scsi_do_cmd(SCpnt,
(void *) sr_cmd, buffer, buflength, sr_ioctl_done,
IOCTL_TIMEOUT, IOCTL_RETRIES);
down(&sem);
}
result = SCpnt->result;
/* Minimal error checking. Ignore cases we know about, and report the rest. */
if(driver_byte(result) != 0)
switch(SCpnt->sense_buffer[2] & 0xf) {
case UNIT_ATTENTION:
scsi_CDs[target].device->changed = 1;
printk("Disc change detected.\n");
break;
case NOT_READY: /* This happens if there is no disc in drive */
printk("CDROM not ready. Make sure there is a disc in the drive.\n");
break;
case ILLEGAL_REQUEST:
/* CDROMCLOSETRAY should not print an error for caddy drives. */
if (!(sr_cmd[0] == START_STOP && sr_cmd[4] == 0x03))
printk("CDROM (ioctl) reports ILLEGAL REQUEST.\n");
break;
default:
printk("SCSI CD error: host %d id %d lun %d return code = %03x\n",
scsi_CDs[target].device->host->host_no,
scsi_CDs[target].device->id,
scsi_CDs[target].device->lun,
result);
printk("\tSense class %x, sense error %x, extended sense %x\n",
sense_class(SCpnt->sense_buffer[0]),
sense_error(SCpnt->sense_buffer[0]),
SCpnt->sense_buffer[2] & 0xf);
};
result = SCpnt->result;
SCpnt->request.rq_status = RQ_INACTIVE; /* Deallocate */
wake_up(&SCpnt->device->device_wait);
/* Wake up a process waiting for device*/
return result;
}
int sr_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
{
u_char sr_cmd[10];
kdev_t dev = inode->i_rdev;
int result, target, err;
target = MINOR(dev);
if (target >= sr_template.nr_dev ||
!scsi_CDs[target].device) return -ENXIO;
switch (cmd)
{
/* Sun-compatible */
case CDROMPAUSE:
sr_cmd[0] = SCMD_PAUSE_RESUME;
sr_cmd[1] = scsi_CDs[target].device->lun << 5;
sr_cmd[2] = sr_cmd[3] = sr_cmd[4] = 0;
sr_cmd[5] = sr_cmd[6] = sr_cmd[7] = 0;
sr_cmd[8] = 0;
sr_cmd[9] = 0;
result = do_ioctl(target, sr_cmd, NULL, 255);
return result;
case CDROMRESUME:
sr_cmd[0] = SCMD_PAUSE_RESUME;
sr_cmd[1] = scsi_CDs[target].device->lun << 5;
sr_cmd[2] = sr_cmd[3] = sr_cmd[4] = 0;
sr_cmd[5] = sr_cmd[6] = sr_cmd[7] = 0;
sr_cmd[8] = 1;
sr_cmd[9] = 0;
result = do_ioctl(target, sr_cmd, NULL, 255);
return result;
case CDROMPLAYMSF:
{
struct cdrom_msf msf;
err = verify_area (VERIFY_READ, (void *) arg, sizeof (msf));
if (err) return err;
memcpy_fromfs(&msf, (void *) arg, sizeof(msf));
sr_cmd[0] = SCMD_PLAYAUDIO_MSF;
sr_cmd[1] = scsi_CDs[target].device->lun << 5;
sr_cmd[2] = 0;
sr_cmd[3] = msf.cdmsf_min0;
sr_cmd[4] = msf.cdmsf_sec0;
sr_cmd[5] = msf.cdmsf_frame0;
sr_cmd[6] = msf.cdmsf_min1;
sr_cmd[7] = msf.cdmsf_sec1;
sr_cmd[8] = msf.cdmsf_frame1;
sr_cmd[9] = 0;
result = do_ioctl(target, sr_cmd, NULL, 255);
return result;
}
case CDROMPLAYBLK:
{
struct cdrom_blk blk;
err = verify_area (VERIFY_READ, (void *) arg, sizeof (blk));
if (err) return err;
memcpy_fromfs(&blk, (void *) arg, sizeof(blk));
sr_cmd[0] = SCMD_PLAYAUDIO10;
sr_cmd[1] = scsi_CDs[target].device->lun << 5;
sr_cmd[2] = blk.from >> 24;
sr_cmd[3] = blk.from >> 16;
sr_cmd[4] = blk.from >> 8;
sr_cmd[5] = blk.from;
sr_cmd[6] = 0;
sr_cmd[7] = blk.len >> 8;
sr_cmd[8] = blk.len;
sr_cmd[9] = 0;
result = do_ioctl(target, sr_cmd, NULL, 255);
return result;
}
case CDROMPLAYTRKIND:
{
struct cdrom_ti ti;
err = verify_area (VERIFY_READ, (void *) arg, sizeof (ti));
if (err) return err;
memcpy_fromfs(&ti, (void *) arg, sizeof(ti));
sr_cmd[0] = SCMD_PLAYAUDIO_TI;
sr_cmd[1] = scsi_CDs[target].device->lun << 5;
sr_cmd[2] = 0;
sr_cmd[3] = 0;
sr_cmd[4] = ti.cdti_trk0;
sr_cmd[5] = ti.cdti_ind0;
sr_cmd[6] = 0;
sr_cmd[7] = ti.cdti_trk1;
sr_cmd[8] = ti.cdti_ind1;
sr_cmd[9] = 0;
result = do_ioctl(target, sr_cmd, NULL, 255);
return result;
}
case CDROMREADTOCHDR:
{
struct cdrom_tochdr tochdr;
char * buffer;
sr_cmd[0] = SCMD_READ_TOC;
sr_cmd[1] = ((scsi_CDs[target].device->lun) << 5);
sr_cmd[2] = sr_cmd[3] = sr_cmd[4] = sr_cmd[5] = 0;
sr_cmd[6] = 0;
sr_cmd[7] = 0; /* MSB of length (12) */
sr_cmd[8] = 12; /* LSB of length */
sr_cmd[9] = 0;
buffer = (unsigned char *) scsi_malloc(512);
if(!buffer) return -ENOMEM;
result = do_ioctl(target, sr_cmd, buffer, 12);
tochdr.cdth_trk0 = buffer[2];
tochdr.cdth_trk1 = buffer[3];
scsi_free(buffer, 512);
err = verify_area (VERIFY_WRITE, (void *) arg, sizeof (struct cdrom_tochdr));
if (err)
return err;
memcpy_tofs ((void *) arg, &tochdr, sizeof (struct cdrom_tochdr));
return result;
}
case CDROMREADTOCENTRY:
{
struct cdrom_tocentry tocentry;
unsigned char * buffer;
err = verify_area (VERIFY_READ, (void *) arg, sizeof (struct cdrom_tocentry));
if (err) return err;
memcpy_fromfs (&tocentry, (void *) arg, sizeof (struct cdrom_tocentry));
sr_cmd[0] = SCMD_READ_TOC;
sr_cmd[1] = ((scsi_CDs[target].device->lun) << 5) |
(tocentry.cdte_format == CDROM_MSF ? 0x02 : 0);
sr_cmd[2] = sr_cmd[3] = sr_cmd[4] = sr_cmd[5] = 0;
sr_cmd[6] = tocentry.cdte_track;
sr_cmd[7] = 0; /* MSB of length (12) */
sr_cmd[8] = 12; /* LSB of length */
sr_cmd[9] = 0;
buffer = (unsigned char *) scsi_malloc(512);
if(!buffer) return -ENOMEM;
result = do_ioctl (target, sr_cmd, buffer, 12);
tocentry.cdte_ctrl = buffer[5] & 0xf;
tocentry.cdte_adr = buffer[5] >> 4;
tocentry.cdte_datamode = (tocentry.cdte_ctrl & 0x04) ? 1 : 0;
if (tocentry.cdte_format == CDROM_MSF) {
tocentry.cdte_addr.msf.minute = buffer[9];
tocentry.cdte_addr.msf.second = buffer[10];
tocentry.cdte_addr.msf.frame = buffer[11];
}
else
tocentry.cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
+ buffer[10]) << 8) + buffer[11];
scsi_free(buffer, 512);
err = verify_area (VERIFY_WRITE, (void *) arg, sizeof (struct cdrom_tocentry));
if (err)
return err;
memcpy_tofs ((void *) arg, &tocentry, sizeof (struct cdrom_tocentry));
return result;
}
case CDROMSTOP:
sr_cmd[0] = START_STOP;
sr_cmd[1] = ((scsi_CDs[target].device->lun) << 5) | 1;
sr_cmd[2] = sr_cmd[3] = sr_cmd[5] = 0;
sr_cmd[4] = 0;
result = do_ioctl(target, sr_cmd, NULL, 255);
return result;
case CDROMSTART:
sr_cmd[0] = START_STOP;
sr_cmd[1] = ((scsi_CDs[target].device->lun) << 5) | 1;
sr_cmd[2] = sr_cmd[3] = sr_cmd[5] = 0;
sr_cmd[4] = 1;
result = do_ioctl(target, sr_cmd, NULL, 255);
return result;
case CDROMCLOSETRAY:
sr_cmd[0] = START_STOP;
sr_cmd[1] = ((scsi_CDs[target].device -> lun) << 5);
sr_cmd[2] = sr_cmd[3] = sr_cmd[5] = 0;
sr_cmd[4] = 0x03;
if ((result = do_ioctl(target, sr_cmd, NULL, 255)))
return result;
/* Gather information about newly inserted disc */
check_disk_change (inode->i_rdev);
sr_ioctl (inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
sr_photocd (inode);
if (scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
get_sectorsize (MINOR(inode->i_rdev));
return 0;
case CDROMEJECT:
/*
* Allow 0 for access count for auto-eject feature.
*/
if (scsi_CDs[target].device -> access_count > 1)
return -EBUSY;
sr_ioctl (inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
sr_cmd[0] = START_STOP;
sr_cmd[1] = ((scsi_CDs[target].device -> lun) << 5) | 1;
sr_cmd[2] = sr_cmd[3] = sr_cmd[5] = 0;
sr_cmd[4] = 0x02;
if (!(result = do_ioctl(target, sr_cmd, NULL, 255)))
scsi_CDs[target].device -> changed = 1;
return result;
case CDROMEJECT_SW:
scsi_CDs[target].auto_eject = !!arg;
return 0;
case CDROMVOLCTRL:
{
char * buffer, * mask;
struct cdrom_volctrl volctrl;
err = verify_area (VERIFY_READ, (void *) arg, sizeof (struct cdrom_volctrl));
if (err) return err;
memcpy_fromfs (&volctrl, (void *) arg, sizeof (struct cdrom_volctrl));
/* First we get the current params so we can just twiddle the volume */
sr_cmd[0] = MODE_SENSE;
sr_cmd[1] = (scsi_CDs[target].device -> lun) << 5;
sr_cmd[2] = 0xe; /* Want mode page 0xe, CDROM audio params */
sr_cmd[3] = 0;
sr_cmd[4] = 28;
sr_cmd[5] = 0;
buffer = (unsigned char *) scsi_malloc(512);
if(!buffer) return -ENOMEM;
if ((result = do_ioctl (target, sr_cmd, buffer, 28))) {
printk ("Hosed while obtaining audio mode page\n");
scsi_free(buffer, 512);
return result;
}
sr_cmd[0] = MODE_SENSE;
sr_cmd[1] = (scsi_CDs[target].device -> lun) << 5;
sr_cmd[2] = 0x4e; /* Want the mask for mode page 0xe */
sr_cmd[3] = 0;
sr_cmd[4] = 28;
sr_cmd[5] = 0;
mask = (unsigned char *) scsi_malloc(512);
if(!mask) {
scsi_free(buffer, 512);
return -ENOMEM;
};
if ((result = do_ioctl (target, sr_cmd, mask, 28))) {
printk ("Hosed while obtaining mask for audio mode page\n");
scsi_free(buffer, 512);
scsi_free(mask, 512);
return result;
}
/* Now mask and substitute our own volume and reuse the rest */
buffer[0] = 0; /* Clear reserved field */
buffer[21] = volctrl.channel0 & mask[21];
buffer[23] = volctrl.channel1 & mask[23];
buffer[25] = volctrl.channel2 & mask[25];
buffer[27] = volctrl.channel3 & mask[27];
sr_cmd[0] = MODE_SELECT;
sr_cmd[1] = ((scsi_CDs[target].device -> lun) << 5) | 0x10; /* Params are SCSI-2 */
sr_cmd[2] = sr_cmd[3] = 0;
sr_cmd[4] = 28;
sr_cmd[5] = 0;
result = do_ioctl (target, sr_cmd, buffer, 28);
scsi_free(buffer, 512);
scsi_free(mask, 512);
return result;
}
case CDROMVOLREAD:
{
char * buffer;
struct cdrom_volctrl volctrl;
err = verify_area (VERIFY_WRITE, (void *) arg, sizeof (struct cdrom_volctrl));
if (err) return err;
/* Get the current params */
sr_cmd[0] = MODE_SENSE;
sr_cmd[1] = (scsi_CDs[target].device -> lun) << 5;
sr_cmd[2] = 0xe; /* Want mode page 0xe, CDROM audio params */
sr_cmd[3] = 0;
sr_cmd[4] = 28;
sr_cmd[5] = 0;
buffer = (unsigned char *) scsi_malloc(512);
if(!buffer) return -ENOMEM;
if ((result = do_ioctl (target, sr_cmd, buffer, 28))) {
printk ("(CDROMVOLREAD) Hosed while obtaining audio mode page\n");
scsi_free(buffer, 512);
return result;
}
volctrl.channel0 = buffer[21];
volctrl.channel1 = buffer[23];
volctrl.channel2 = buffer[25];
volctrl.channel3 = buffer[27];
memcpy_tofs ((void *) arg, &volctrl, sizeof (struct cdrom_volctrl));
scsi_free(buffer, 512);
return 0;
}
case CDROMSUBCHNL:
{
struct cdrom_subchnl subchnl;
char * buffer;
sr_cmd[0] = SCMD_READ_SUBCHANNEL;
sr_cmd[1] = ((scsi_CDs[target].device->lun) << 5) | 0x02; /* MSF format */
sr_cmd[2] = 0x40; /* I do want the subchannel info */
sr_cmd[3] = 0x01; /* Give me current position info */
sr_cmd[4] = sr_cmd[5] = 0;
sr_cmd[6] = 0;
sr_cmd[7] = 0;
sr_cmd[8] = 16;
sr_cmd[9] = 0;
buffer = (unsigned char*) scsi_malloc(512);
if(!buffer) return -ENOMEM;
result = do_ioctl(target, sr_cmd, buffer, 16);
subchnl.cdsc_audiostatus = buffer[1];
subchnl.cdsc_format = CDROM_MSF;
subchnl.cdsc_ctrl = buffer[5] & 0xf;
subchnl.cdsc_trk = buffer[6];
subchnl.cdsc_ind = buffer[7];
subchnl.cdsc_reladdr.msf.minute = buffer[13];
subchnl.cdsc_reladdr.msf.second = buffer[14];
subchnl.cdsc_reladdr.msf.frame = buffer[15];
subchnl.cdsc_absaddr.msf.minute = buffer[9];
subchnl.cdsc_absaddr.msf.second = buffer[10];
subchnl.cdsc_absaddr.msf.frame = buffer[11];
scsi_free(buffer, 512);
err = verify_area (VERIFY_WRITE, (void *) arg, sizeof (struct cdrom_subchnl));
if (err)
return err;
memcpy_tofs ((void *) arg, &subchnl, sizeof (struct cdrom_subchnl));
return result;
}
case CDROM_GET_UPC:
{
struct cdrom_mcn mcn;
char * buffer;
sr_cmd[0] = SCMD_READ_SUBCHANNEL;
sr_cmd[1] = ((scsi_CDs[target].device->lun) << 5);
sr_cmd[2] = 0x40; /* I do want the subchannel info */
sr_cmd[3] = 0x02; /* Give me medium catalog number info */
sr_cmd[4] = sr_cmd[5] = 0;
sr_cmd[6] = 0;
sr_cmd[7] = 0;
sr_cmd[8] = 24;
sr_cmd[9] = 0;
buffer = (unsigned char*) scsi_malloc(512);
if(!buffer) return -ENOMEM;
result = do_ioctl(target, sr_cmd, buffer, 24);
memcpy (mcn.medium_catalog_number, buffer + 9, 13);
mcn.medium_catalog_number[13] = 0;
scsi_free(buffer, 512);
err = verify_area (VERIFY_WRITE, (void *) arg, sizeof (struct cdrom_mcn));
if (err)
return err;
memcpy_tofs ((void *) arg, &mcn, sizeof (struct cdrom_mcn));
return result;
}
/* these are compatible with the ide-cd driver */
case CDROMREADRAW:
case CDROMREADMODE1:
case CDROMREADMODE2:
return -EINVAL;
/* block-copy from ../block/sbpcd.c with some adjustments... */
case CDROMMULTISESSION: /* tell start-of-last-session to user */
{
struct cdrom_multisession ms_info;
long lba;
err = verify_area(VERIFY_READ, (void *) arg,
sizeof(struct cdrom_multisession));
if (err) return (err);
memcpy_fromfs(&ms_info, (void *) arg, sizeof(struct cdrom_multisession));
if (ms_info.addr_format==CDROM_MSF) { /* MSF-bin requested */
lba = scsi_CDs[target].mpcd_sector+CD_BLOCK_OFFSET;
ms_info.addr.msf.minute = lba / (CD_SECS*CD_FRAMES);
lba %= CD_SECS*CD_FRAMES;
ms_info.addr.msf.second = lba / CD_FRAMES;
ms_info.addr.msf.frame = lba % CD_FRAMES;
} else if (ms_info.addr_format==CDROM_LBA) /* lba requested */
ms_info.addr.lba=scsi_CDs[target].mpcd_sector;
else return (-EINVAL);
ms_info.xa_flag=scsi_CDs[target].xa_flags & 0x01;
err=verify_area(VERIFY_WRITE,(void *) arg,
sizeof(struct cdrom_multisession));
if (err) return (err);
memcpy_tofs((void *) arg, &ms_info, sizeof(struct cdrom_multisession));
return (0);
}
case BLKRAGET:
if (!arg)
return -EINVAL;
err = verify_area(VERIFY_WRITE, (int *) arg, sizeof(int));
if (err)
return err;
put_user(read_ahead[MAJOR(inode->i_rdev)], (int *) arg);
return 0;
case BLKRASET:
if(!suser())
return -EACCES;
if(!(inode->i_rdev))
return -EINVAL;
if(arg > 0xff)
return -EINVAL;
read_ahead[MAJOR(inode->i_rdev)] = arg;
return 0;
RO_IOCTLS(dev,arg);
case CDROMRESET:
invalidate_buffers(inode->i_rdev);
return 0;
default:
return scsi_ioctl(scsi_CDs[target].device,cmd,(void *) arg);
}
}
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-indent-level: 4
* c-brace-imaginary-offset: 0
* c-brace-offset: -4
* c-argdecl-indent: 4
* c-label-offset: -4
* c-continued-statement-offset: 4
* c-continued-brace-offset: 0
* indent-tabs-mode: nil
* tab-width: 8
* End:
*/
|