4

Suppose, I have connected a device to a PC.

Question: How does the Linux OS (Ubuntu) realizes that a new hardware device is connected?

Answer: The newly connected device raises an Interrupt (and does many more things).

Is my answer correct, at least the part that says that the newly connected device raises an Interrupt?

  • 2
    When ur hardware connected , if the related driver exist in the kernel module (driver) , it will be loaded. More explained here UbuntuAsk – Cuneyit Kiris Nov 17 '17 at 12:55
  • I am looking for a handle that lets me control the first-time newly attached device. The interrupt here essentially lets me gather control over this new device. So, is an interrupt raised when a new device is connected? – Arshad Nazeer Nov 17 '17 at 13:15
  • Start by describing your device + how it will be connected. For example, any USB device will generate a setup of USB events which can be tracked. Plugging in a device to a serial or parallel port or many other ports won't generate an interrupt, because at boot time no device was sensed + no driver was loaded. Many drivers throw events when new devices are connected + the driver must be loaded for this to work... Since drivers only load when devices are sensed, at boot time. Many drivers will only load first time at reboot or by manual command line commands. – David Favor Nov 17 '17 at 13:54

2 Answers2

2

Yes, in both senses of the term 'interrupt':

If you specifically mean a hardware-level interrupt, then yes (assuming that the system can actually interact with the hardware that was just connected, hooking something up with a power-only USB cable won't trigger such an interrupt for example), but it's not necessarily device specific and may instead trigger re-enumeration of devices on the bus that raised the interrupt (this is the case for USB and IEEE 1394 for example).

If you instead mean some kind of event userspace can react to, then yes, there will always be such an event if the device in question is actually usable (that is, a kernel driver of some sort bound to it). These events are handled by a program called udev, and if you want to do something in response to a device being plugged in, the documentation for udev is where you should start. Unlike the hardware interrupt, uevents (the general term for these 'interrupts') will be device specific (so you can match specific hardware using them, provided the hardware gives some form of identification).

0

It's true, but it's kind of like saying “what happens when you meet someone for the first time? Light reflects from their body onto your retina.” It's too low level to see the interesting parts.

So yes, assuming that the hardware is connected to a controller that detects external connections (e.g. an USB/Firewire/eSATA/… controller, or a graphics processor with VGA-with-EDID/DVI/HDMI/DisplayPort), the controller will send a signal on a bus and that signal will trigger an interrupt on the CPU. The interrupt handler in the kernel, which is part of the driver for that controller, will go and read a message from the controller. That message will contain information about the new device, formatted according to the protocol used by that particular controller.

If the device is of a kind that requires a specific driver in addition to the driver for the controller, then there's a mechanism for automatically loading a module containing the proper driver. See Are driver modules loaded and unloaded automatically?

Note that it isn't a given that the hardware is detected. That depends on the electronic design. On some older types of connections (serial ports, VGA with old monitors, many sound jacks), there's no mechanism to report whether a device is plugged in or not.