Detect when a CD ROM is inserted

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Detect when a CD ROM is inserted

Post by BenLunt »

Hi guys,

A fellow member of this forum asked me a few questions (via PM) and it got me to check some of my code with QEMU. My code works on real hardware, Bochs, and VirtualBox, but not on QEMU. So I went looking. Come to find out, unless I am mistaken, QEMU returns 0x70 (No CDROM inserted) every time for the (cd-rom) MODE SENSE command.

For reference, see Line 866, Line 885, and Line 904.

Since my code uses the MODE SENSE command to detect if a disk is inserted and what type of disk it is, I am out of luck with QEMU.

For example, a value of 0x10 or 0x18 would be a normal return.

Code: Select all

  MEDIA_TYPE_CDR10   = 0x10,  // Door closed / caddy inserted, medium type (CD-R) size unknown
  MEDIA_TYPE_CDR18   = 0x18,  //  80 mm CD-ROM (CD-R) Hybrid disc (Photo CD), door closed or caddy inserted
  MEDIA_TYPE_NO_DISC = 0x70,  // 70h Door closed, no disc present
In my case, QEMU returns 0x70 every time.

I would think that QEMU would "patch" this byte at the end of the call, but apparently it does not.

I am wondering, what other ways do you know to detect if a disc is inserted and possibly what type? Currently, I am only concerned with the inserted status.

Thanks,
Ben
- http://www.fysnet.net/osdesign_book_series.htm
Last edited by BenLunt on Sat Feb 15, 2020 2:52 pm, edited 1 time in total.
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Detect when a CD ROM is inserted

Post by BenLunt »

I guess what I am trying to ask is, what methods do you use.

The MMC-4 (Multimedia Commands - 4) specification, revision 5a, section 6.7, has an event notification.

Then if you request the Media event (value of 4), per section 6.7.2.5, the media status will be reported, a single bit (bit 1), hence either inserted or not.

I would guess that this is the most used/recommended choice, called every few seconds or so, to see if the media has changed (using EventCode field), etc.

Just asking what method(s) you use.

Thanks,
Ben

P.S. This works fine in QEMU, but Bochs:

Code: Select all

     BX_DEBUG_ATAPI(("ATAPI command 0x%x not implemented yet", atapi_command));
Let's see if I can get a patch committed within the next day or so.
Octocontrabass
Member
Member
Posts: 5584
Joined: Mon Mar 25, 2013 7:01 pm

Re: Detect when a CD ROM is inserted

Post by Octocontrabass »

I'd guess most software uses TEST UNIT READY to detect if a disc is inserted. It seems to be intended for that purpose.
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Detect when a CD ROM is inserted

Post by BenLunt »

Octocontrabass wrote:I'd guess most software uses TEST UNIT READY to detect if a disc is inserted. It seems to be intended for that purpose.
I thought this was the case too, and it probably is.

However, the specification states:
If the logical unit is able to accept an appropriate medium-access command without returning CHECK CONDITION status, this command shall return a GOOD status. If the logical unit is unable to become operational or is in a state such that an application client action (e.g., START UNIT command) is required to make the logical unit ready, the command shall be terminated with CHECK CONDITION status, with the sense key set to NOT READY.
The first sentence would imply that this command could be used for exactly this purpose. However, the unit could be in a different state and need a reset or other action before it is ready for medium-access *and* still have a disc inserted. i.e.: It needs the START_UNIT command.

Therefore, I went looking for another technique. I would like a way to see the "inserted" status, no matter the state of the logical-unit. (Granted if the unit can't provide the state, there is no need to know that a disk is inserted.)

Doing some more tests, I think I might stick with the Event Notification command technique. This technique is designed to be periodically sent to the unit, maybe once every few seconds.

Thanks,
Ben
Octocontrabass
Member
Member
Posts: 5584
Joined: Mon Mar 25, 2013 7:01 pm

Re: Detect when a CD ROM is inserted

Post by Octocontrabass »

The standards seem to imply you should follow your TEST UNIT READY with REQUEST SENSE to see why the drive isn't ready and respond accordingly.

This method should work even on drives that predate the event notification (and presumably also Bochs, which seems to emulate hardware from around that era).
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

Re: Detect when a CD ROM is inserted

Post by zity »

I have a quick question related to this topic, so I will just post it here.

Currently I am trying to improve my ATAPI driver by adding some more features and checks. As suggested here, I implemented the TEST UNIT READY and REQUEST SENSE commands, and from these I am able to detect when a disc is missing and when the media has changed.

My question is now, do I need to acknowledge a media change? As far as I can tell, this is not necessary, but I am honestly finding the ATAPI documentation a bit unclear.
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Detect when a CD ROM is inserted

Post by BenLunt »

zity wrote:My question is now, do I need to acknowledge a media change? As far as I can tell, this is not necessary, but I am honestly finding the ATAPI documentation a bit unclear.
By acknowledge, do you mean send a command to the drive to tell it you have acknowledged a media change? No, you don't have to do this.

For CD-ROM's, maybe look at "INFORMATION TECHNOLOGY - Multimedia Commands 4 (MMC-4)" instead. The is called the MMC-4 specs (I think there is a MMC-5 now). For the Event Notification command, see section 6.7.

Ben
- http://www.fysnet.net/osdesign_book_series.htm
Post Reply