ipc: optimize semget/shmget/msgget for lots of keys
[linux/fpc-iii.git] / drivers / ide / ide-cd_ioctl.c
blob9d26c9737e2127066e80af152f61584a59b98b41
1 /*
2 * cdrom.c IOCTLs handling for ide-cd driver.
4 * Copyright (C) 1994-1996 Scott Snyder <snyder@fnald0.fnal.gov>
5 * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
6 * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
7 */
9 #include <linux/kernel.h>
10 #include <linux/cdrom.h>
11 #include <linux/gfp.h>
12 #include <linux/ide.h>
13 #include <scsi/scsi.h>
15 #include "ide-cd.h"
17 /****************************************************************************
18 * Other driver requests (open, close, check media change).
20 int ide_cdrom_open_real(struct cdrom_device_info *cdi, int purpose)
22 return 0;
26 * Close down the device. Invalidate all cached blocks.
28 void ide_cdrom_release_real(struct cdrom_device_info *cdi)
30 ide_drive_t *drive = cdi->handle;
32 if (!cdi->use_count)
33 drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
37 * add logic to try GET_EVENT command first to check for media and tray
38 * status. this should be supported by newer cd-r/w and all DVD etc
39 * drives
41 int ide_cdrom_drive_status(struct cdrom_device_info *cdi, int slot_nr)
43 ide_drive_t *drive = cdi->handle;
44 struct media_event_desc med;
45 struct request_sense sense;
46 int stat;
48 if (slot_nr != CDSL_CURRENT)
49 return -EINVAL;
51 stat = cdrom_check_status(drive, &sense);
52 if (!stat || sense.sense_key == UNIT_ATTENTION)
53 return CDS_DISC_OK;
55 if (!cdrom_get_media_event(cdi, &med)) {
56 if (med.media_present)
57 return CDS_DISC_OK;
58 else if (med.door_open)
59 return CDS_TRAY_OPEN;
60 else
61 return CDS_NO_DISC;
64 if (sense.sense_key == NOT_READY && sense.asc == 0x04
65 && sense.ascq == 0x04)
66 return CDS_DISC_OK;
69 * If not using Mt Fuji extended media tray reports,
70 * just return TRAY_OPEN since ATAPI doesn't provide
71 * any other way to detect this...
73 if (sense.sense_key == NOT_READY) {
74 if (sense.asc == 0x3a && sense.ascq == 1)
75 return CDS_NO_DISC;
76 else
77 return CDS_TRAY_OPEN;
79 return CDS_DRIVE_NOT_READY;
83 * ide-cd always generates media changed event if media is missing, which
84 * makes it impossible to use for proper event reporting, so disk->events
85 * is cleared to 0 and the following function is used only to trigger
86 * revalidation and never propagated to userland.
88 unsigned int ide_cdrom_check_events_real(struct cdrom_device_info *cdi,
89 unsigned int clearing, int slot_nr)
91 ide_drive_t *drive = cdi->handle;
92 int retval;
94 if (slot_nr == CDSL_CURRENT) {
95 (void) cdrom_check_status(drive, NULL);
96 retval = (drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED) ? 1 : 0;
97 drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
98 return retval ? DISK_EVENT_MEDIA_CHANGE : 0;
99 } else {
100 return 0;
104 /* Eject the disk if EJECTFLAG is 0.
105 If EJECTFLAG is 1, try to reload the disk. */
106 static
107 int cdrom_eject(ide_drive_t *drive, int ejectflag,
108 struct request_sense *sense)
110 struct cdrom_info *cd = drive->driver_data;
111 struct cdrom_device_info *cdi = &cd->devinfo;
112 char loej = 0x02;
113 unsigned char cmd[BLK_MAX_CDB];
115 if ((drive->atapi_flags & IDE_AFLAG_NO_EJECT) && !ejectflag)
116 return -EDRIVE_CANT_DO_THIS;
118 /* reload fails on some drives, if the tray is locked */
119 if ((drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED) && ejectflag)
120 return 0;
122 /* only tell drive to close tray if open, if it can do that */
123 if (ejectflag && (cdi->mask & CDC_CLOSE_TRAY))
124 loej = 0;
126 memset(cmd, 0, BLK_MAX_CDB);
128 cmd[0] = GPCMD_START_STOP_UNIT;
129 cmd[4] = loej | (ejectflag != 0);
131 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, 0);
134 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
135 static
136 int ide_cd_lockdoor(ide_drive_t *drive, int lockflag,
137 struct request_sense *sense)
139 struct request_sense my_sense;
140 int stat;
142 if (sense == NULL)
143 sense = &my_sense;
145 /* If the drive cannot lock the door, just pretend. */
146 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0) {
147 stat = 0;
148 } else {
149 unsigned char cmd[BLK_MAX_CDB];
151 memset(cmd, 0, BLK_MAX_CDB);
153 cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
154 cmd[4] = lockflag ? 1 : 0;
156 stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL,
157 sense, 0, 0);
160 /* If we got an illegal field error, the drive
161 probably cannot lock the door. */
162 if (stat != 0 &&
163 sense->sense_key == ILLEGAL_REQUEST &&
164 (sense->asc == 0x24 || sense->asc == 0x20)) {
165 printk(KERN_ERR "%s: door locking not supported\n",
166 drive->name);
167 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
168 stat = 0;
171 /* no medium, that's alright. */
172 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
173 stat = 0;
175 if (stat == 0) {
176 if (lockflag)
177 drive->atapi_flags |= IDE_AFLAG_DOOR_LOCKED;
178 else
179 drive->atapi_flags &= ~IDE_AFLAG_DOOR_LOCKED;
182 return stat;
185 int ide_cdrom_tray_move(struct cdrom_device_info *cdi, int position)
187 ide_drive_t *drive = cdi->handle;
188 struct request_sense sense;
190 if (position) {
191 int stat = ide_cd_lockdoor(drive, 0, &sense);
193 if (stat)
194 return stat;
197 return cdrom_eject(drive, !position, &sense);
200 int ide_cdrom_lock_door(struct cdrom_device_info *cdi, int lock)
202 ide_drive_t *drive = cdi->handle;
204 return ide_cd_lockdoor(drive, lock, NULL);
208 * ATAPI devices are free to select the speed you request or any slower
209 * rate. :-( Requesting too fast a speed will _not_ produce an error.
211 int ide_cdrom_select_speed(struct cdrom_device_info *cdi, int speed)
213 ide_drive_t *drive = cdi->handle;
214 struct cdrom_info *cd = drive->driver_data;
215 struct request_sense sense;
216 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
217 int stat;
218 unsigned char cmd[BLK_MAX_CDB];
220 if (speed == 0)
221 speed = 0xffff; /* set to max */
222 else
223 speed *= 177; /* Nx to kbytes/s */
225 memset(cmd, 0, BLK_MAX_CDB);
227 cmd[0] = GPCMD_SET_SPEED;
228 /* Read Drive speed in kbytes/second MSB/LSB */
229 cmd[2] = (speed >> 8) & 0xff;
230 cmd[3] = speed & 0xff;
231 if ((cdi->mask & (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) !=
232 (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) {
233 /* Write Drive speed in kbytes/second MSB/LSB */
234 cmd[4] = (speed >> 8) & 0xff;
235 cmd[5] = speed & 0xff;
238 stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, &sense, 0, 0);
240 if (!ide_cdrom_get_capabilities(drive, buf)) {
241 ide_cdrom_update_speed(drive, buf);
242 cdi->speed = cd->current_speed;
245 return 0;
248 int ide_cdrom_get_last_session(struct cdrom_device_info *cdi,
249 struct cdrom_multisession *ms_info)
251 struct atapi_toc *toc;
252 ide_drive_t *drive = cdi->handle;
253 struct cdrom_info *info = drive->driver_data;
254 struct request_sense sense;
255 int ret;
257 if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0 || !info->toc) {
258 ret = ide_cd_read_toc(drive, &sense);
259 if (ret)
260 return ret;
263 toc = info->toc;
264 ms_info->addr.lba = toc->last_session_lba;
265 ms_info->xa_flag = toc->xa_flag;
267 return 0;
270 int ide_cdrom_get_mcn(struct cdrom_device_info *cdi,
271 struct cdrom_mcn *mcn_info)
273 ide_drive_t *drive = cdi->handle;
274 int stat, mcnlen;
275 char buf[24];
276 unsigned char cmd[BLK_MAX_CDB];
277 unsigned len = sizeof(buf);
279 memset(cmd, 0, BLK_MAX_CDB);
281 cmd[0] = GPCMD_READ_SUBCHANNEL;
282 cmd[1] = 2; /* MSF addressing */
283 cmd[2] = 0x40; /* request subQ data */
284 cmd[3] = 2; /* format */
285 cmd[8] = len;
287 stat = ide_cd_queue_pc(drive, cmd, 0, buf, &len, NULL, 0, 0);
288 if (stat)
289 return stat;
291 mcnlen = sizeof(mcn_info->medium_catalog_number) - 1;
292 memcpy(mcn_info->medium_catalog_number, buf + 9, mcnlen);
293 mcn_info->medium_catalog_number[mcnlen] = '\0';
295 return 0;
298 int ide_cdrom_reset(struct cdrom_device_info *cdi)
300 ide_drive_t *drive = cdi->handle;
301 struct cdrom_info *cd = drive->driver_data;
302 struct request_sense sense;
303 struct request *rq;
304 int ret;
306 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
307 ide_req(rq)->type = ATA_PRIV_MISC;
308 rq->rq_flags = RQF_QUIET;
309 blk_execute_rq(drive->queue, cd->disk, rq, 0);
310 ret = scsi_req(rq)->result ? -EIO : 0;
311 blk_put_request(rq);
313 * A reset will unlock the door. If it was previously locked,
314 * lock it again.
316 if (drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED)
317 (void)ide_cd_lockdoor(drive, 1, &sense);
319 return ret;
322 static int ide_cd_get_toc_entry(ide_drive_t *drive, int track,
323 struct atapi_toc_entry **ent)
325 struct cdrom_info *info = drive->driver_data;
326 struct atapi_toc *toc = info->toc;
327 int ntracks;
330 * don't serve cached data, if the toc isn't valid
332 if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0)
333 return -EINVAL;
335 /* Check validity of requested track number. */
336 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
338 if (toc->hdr.first_track == CDROM_LEADOUT)
339 ntracks = 0;
341 if (track == CDROM_LEADOUT)
342 *ent = &toc->ent[ntracks];
343 else if (track < toc->hdr.first_track || track > toc->hdr.last_track)
344 return -EINVAL;
345 else
346 *ent = &toc->ent[track - toc->hdr.first_track];
348 return 0;
351 static int ide_cd_fake_play_trkind(ide_drive_t *drive, void *arg)
353 struct cdrom_ti *ti = arg;
354 struct atapi_toc_entry *first_toc, *last_toc;
355 unsigned long lba_start, lba_end;
356 int stat;
357 struct request_sense sense;
358 unsigned char cmd[BLK_MAX_CDB];
360 stat = ide_cd_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
361 if (stat)
362 return stat;
364 stat = ide_cd_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
365 if (stat)
366 return stat;
368 if (ti->cdti_trk1 != CDROM_LEADOUT)
369 ++last_toc;
370 lba_start = first_toc->addr.lba;
371 lba_end = last_toc->addr.lba;
373 if (lba_end <= lba_start)
374 return -EINVAL;
376 memset(cmd, 0, BLK_MAX_CDB);
378 cmd[0] = GPCMD_PLAY_AUDIO_MSF;
379 lba_to_msf(lba_start, &cmd[3], &cmd[4], &cmd[5]);
380 lba_to_msf(lba_end - 1, &cmd[6], &cmd[7], &cmd[8]);
382 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, &sense, 0, 0);
385 static int ide_cd_read_tochdr(ide_drive_t *drive, void *arg)
387 struct cdrom_info *cd = drive->driver_data;
388 struct cdrom_tochdr *tochdr = arg;
389 struct atapi_toc *toc;
390 int stat;
392 /* Make sure our saved TOC is valid. */
393 stat = ide_cd_read_toc(drive, NULL);
394 if (stat)
395 return stat;
397 toc = cd->toc;
398 tochdr->cdth_trk0 = toc->hdr.first_track;
399 tochdr->cdth_trk1 = toc->hdr.last_track;
401 return 0;
404 static int ide_cd_read_tocentry(ide_drive_t *drive, void *arg)
406 struct cdrom_tocentry *tocentry = arg;
407 struct atapi_toc_entry *toce;
408 int stat;
410 stat = ide_cd_get_toc_entry(drive, tocentry->cdte_track, &toce);
411 if (stat)
412 return stat;
414 tocentry->cdte_ctrl = toce->control;
415 tocentry->cdte_adr = toce->adr;
416 if (tocentry->cdte_format == CDROM_MSF) {
417 lba_to_msf(toce->addr.lba,
418 &tocentry->cdte_addr.msf.minute,
419 &tocentry->cdte_addr.msf.second,
420 &tocentry->cdte_addr.msf.frame);
421 } else
422 tocentry->cdte_addr.lba = toce->addr.lba;
424 return 0;
427 int ide_cdrom_audio_ioctl(struct cdrom_device_info *cdi,
428 unsigned int cmd, void *arg)
430 ide_drive_t *drive = cdi->handle;
432 switch (cmd) {
434 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
435 * atapi doesn't support it
437 case CDROMPLAYTRKIND:
438 return ide_cd_fake_play_trkind(drive, arg);
439 case CDROMREADTOCHDR:
440 return ide_cd_read_tochdr(drive, arg);
441 case CDROMREADTOCENTRY:
442 return ide_cd_read_tocentry(drive, arg);
443 default:
444 return -EINVAL;
448 /* the generic packet interface to cdrom.c */
449 int ide_cdrom_packet(struct cdrom_device_info *cdi,
450 struct packet_command *cgc)
452 ide_drive_t *drive = cdi->handle;
453 req_flags_t flags = 0;
454 unsigned len = cgc->buflen;
456 if (cgc->timeout <= 0)
457 cgc->timeout = ATAPI_WAIT_PC;
459 /* here we queue the commands from the uniform CD-ROM
460 layer. the packet must be complete, as we do not
461 touch it at all. */
463 if (cgc->sense)
464 memset(cgc->sense, 0, sizeof(struct request_sense));
466 if (cgc->quiet)
467 flags |= RQF_QUIET;
469 cgc->stat = ide_cd_queue_pc(drive, cgc->cmd,
470 cgc->data_direction == CGC_DATA_WRITE,
471 cgc->buffer, &len,
472 cgc->sense, cgc->timeout, flags);
473 if (!cgc->stat)
474 cgc->buflen -= len;
475 return cgc->stat;