public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@suse.de>
To: Erik Andersen <andersen@codepoet.org>,
	"Calin A. Culianu" <calin@ajvar.org>,
	linux-kernel@vger.kernel.org
Subject: Re: Asynchronous CDROM Events in Userland
Date: Mon, 4 Feb 2002 08:57:12 +0100	[thread overview]
Message-ID: <20020204085712.O29553@suse.de> (raw)
In-Reply-To: <Pine.LNX.4.30.0202032333200.1158-100000@rtlab.med.cornell.edu> <20020204070414.GA19268@codepoet.org>
In-Reply-To: <20020204070414.GA19268@codepoet.org>

[-- Attachment #1: Type: text/plain, Size: 492 bytes --]

On Mon, Feb 04 2002, Erik Andersen wrote:
> Jens Axboe and I wrote a little test app a year or two ago to check
> for whether drives supported asynchronous mode.  We found it to be
> unsupported on 100% of the drives we tested (and we tested quite a
> few)...

Yep, _no_ drives to date support queued event notification. However, a
polled approach is really not too bad -- it simply means that we'll push
it to user space instead. I've written a small utility for reference.

-- 
Jens Axboe


[-- Attachment #2: cd_poll.c --]
[-- Type: text/plain, Size: 2437 bytes --]

/*
 * simple media event poller for cdroms implementing the GET_EVENT
 * interface
 *
 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <linux/cdrom.h>

#define INIT_CGC(cgc)	memset((cgc), 0, sizeof(struct cdrom_generic_command))

void dump_cgc(struct cdrom_generic_command *cgc)
{
	struct request_sense *sense = cgc->sense;
	int i;

	printf("cdb: ");
	for (i = 0; i < 12; i++)
		printf("%02x ", cgc->cmd[i]);
	printf("\n");

	printf("buffer (%d): ", cgc->buflen);
	for (i = 0; i < cgc->buflen; i++)
		printf("%02x ", cgc->buffer[i]);
	printf("\n");

	if (!sense)
		return;

	printf("sense: %02x.%02x.%02x\n", sense->sense_key, sense->asc, sense->ascq);
}

int wait_cmd(int fd, struct cdrom_generic_command *cgc, void *buffer,
	     int len, int ddir, int timeout)
{
	struct request_sense sense;
	int ret;

	memset(&sense, 0, sizeof(sense));

	cgc->timeout = timeout;
	cgc->buffer = buffer;
	cgc->buflen = len;
	cgc->data_direction = ddir;
	cgc->sense = &sense;
	cgc->quiet = 1;

	ret = ioctl(fd, CDROM_SEND_PACKET, cgc);
	if (ret == -1) {
		perror("ioctl");
		dump_cgc(cgc);
	}

	return ret;
}

int get_media_event(int fd)
{
	struct cdrom_generic_command cgc;
	unsigned char buffer[8];
	int ret;

	INIT_CGC(&cgc);
	memset(buffer, 0, sizeof(buffer));

	cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
	cgc.cmd[1] = 1;
	cgc.cmd[4] = 16;
	cgc.cmd[8] = sizeof(buffer);

	ret = wait_cmd(fd, &cgc, buffer, sizeof(buffer), CGC_DATA_READ, 600);
	if (ret < 0) {
		perror("GET_EVENT");
		return ret;
	}

	return buffer[4] & 0xf;
}

int poll_events(int fd)
{
	int event, quit, first;

	quit = 0;
	first = 1;
	do {
		event = get_media_event(fd);
		if (event < 0)
			break;

		switch (event) {
			case 0:
				if (first)
					printf("no media change\n");
				break;
			case 1:
				printf("eject request\n");
				break;
			case 2:
				printf("new media\n");
				break;
			case 3:
				printf("media removal\n");
				break;
			case 4:
				printf("media change\n");
				break;
			default:
				printf("unknown media event (%d)\n", event);
				break;
		}

		first = 0;

		if (event)
			continue;

		sleep(2);

	} while (!quit);

	return event;
}

int main(int argc, char *argv[])
{
	int fd;

	fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
	if (fd == -1) {
		perror("open");
		return 1;
	}

	return poll_events(fd);
}


  reply	other threads:[~2002-02-04  7:58 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-04  4:41 Asynchronous CDROM Events in Userland Calin A. Culianu
2002-02-04  5:07 ` H. Peter Anvin
2002-02-04  5:19   ` Calin A. Culianu
2002-02-04 12:43   ` john slee
2002-02-04 15:23     ` Calin A. Culianu
2002-02-04 16:20     ` H. Peter Anvin
2002-02-04  7:04 ` Erik Andersen
2002-02-04  7:57   ` Jens Axboe [this message]
2002-02-04  9:33     ` Gregor Jasny
2002-02-04  9:39       ` Jens Axboe
2002-02-04 14:05     ` hugang
2002-02-05  7:43       ` Jens Axboe
2002-02-04 12:16   ` Alan Cox
2002-02-04 20:51     ` oops booting 2.4.18-pre7-ac3 Todd M. Roy
2002-02-05  4:04 ` Asynchronous CDROM Events in Userland Stevie O
2002-02-05  4:28   ` H. Peter Anvin
2002-02-05  4:44     ` Calin A. Culianu
2002-02-05  4:47       ` H. Peter Anvin
     [not found] ` <a3l4uc@cesium.transmeta.com>
2002-02-06 14:22   ` Pavel Machek
2002-02-06 22:48     ` H. Peter Anvin
  -- strict thread matches above, loose matches on Subject: below --
2002-02-05 13:17 David Balazic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020204085712.O29553@suse.de \
    --to=axboe@suse.de \
    --cc=andersen@codepoet.org \
    --cc=calin@ajvar.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox