OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:08 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Detect when a CD ROM is inserted
PostPosted: Wed Feb 20, 2019 4:09 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
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:
  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.

Top
 Profile  
 
 Post subject: Re: Detect when a CD ROM is inserted
PostPosted: Wed Feb 20, 2019 7:17 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
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:
     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.


Top
 Profile  
 
 Post subject: Re: Detect when a CD ROM is inserted
PostPosted: Thu Feb 21, 2019 7:03 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
I'd guess most software uses TEST UNIT READY to detect if a disc is inserted. It seems to be intended for that purpose.


Top
 Profile  
 
 Post subject: Re: Detect when a CD ROM is inserted
PostPosted: Thu Feb 21, 2019 10:11 am 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
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:
Quote:
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


Top
 Profile  
 
 Post subject: Re: Detect when a CD ROM is inserted
PostPosted: Fri Feb 22, 2019 4:36 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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).


Top
 Profile  
 
 Post subject: Re: Detect when a CD ROM is inserted
PostPosted: Fri Apr 19, 2019 2:25 pm 
Offline
Member
Member
User avatar

Joined: Mon Jul 13, 2009 5:52 am
Posts: 99
Location: Denmark
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.


Top
 Profile  
 
 Post subject: Re: Detect when a CD ROM is inserted
PostPosted: Sat Apr 20, 2019 9:21 am 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot] and 70 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group