You are not logged in.

#1 2008-11-26 15:13:15

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Detecting when a USB device is plugged in/removed

So I was poking around recently and decided to configure a PC I had laying around as a 3rd display. So I grab the display and swivel it around so I can measure its cable as part of a placement experiment. And notice a USB port I'd either never seen before or saw, disregarded and forgot about.

"This screen has had a hub in it this whole time?!...", I thought, and poked around the front of it for ports. None turn up. The only info I've been able to find out about it is that it's a port for a mouse, of all things, so one can control the OSD via point/click. Crazy and/or unlikely, but whatever. It has ID 0471:0003 for anyone curious; Google doesn't turn much up for it, before you check. If you were to actually investigate what it does, you'd likely be better off starting with the display itself - it's a CRT, a Philips 107B (the back says it's specifically model 107B10).

Why I'm interested in the port now is because a suspicion of mine is correct: the... whatever-it-is is powered via the screen, not via USB, so when I physically turn the screen on, the device switches on too, and Linux picks the new device up. Aaand when I switch the display off, the USB thingy switches off too, so Linux reports it as removed.

My question is this: how can I detect these events without eating any more than a few % of CPU time and/or running a multibillion-layer framework like HAL? The PC the display is connected to (both USB- and VGA-wise) is a 450MHz P3, so it'd be very easy to send CPU usage skyrocketing if I don't use efficient code, and I don't see the point in unnecessarily straining it.

I'm prepared to go as far as writing a small kernel module, if that helps, isn't more than 150 SLOC, and is heavily documented. tongue

-dav7

Last edited by dav7 (2008-11-26 15:16:16)


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#2 2008-11-26 15:48:51

Zariel
Member
Registered: 2008-10-07
Posts: 446

Re: Detecting when a USB device is plugged in/removed

cant you use udev for that?

Offline

#3 2008-11-27 00:01:17

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Detecting when a USB device is plugged in/removed

Okay, it does run udev. I'll look into configuring it for this. Thanks.

(I'll mark this solved when I post the solution I find.)

-dav7

Last edited by dav7 (2008-11-27 00:02:17)


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#4 2008-11-27 01:32:15

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Detecting when a USB device is plugged in/removed

Well, udev is extremely slow (10-15 second wait before it responds).

Are there any faster ways I could manage this? hmm

-dav7


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#5 2008-11-27 02:02:43

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Detecting when a USB device is plugged in/removed

udev is pretty much "the" way to do that sort of thing. Maybe ask on LKML?

Offline

#6 2008-11-27 04:53:03

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Detecting when a USB device is plugged in/removed

Are there any web interfaces for LKML? I dislike mailinglists; the last one I subscribed to randomly stopped sending messages to me after about an hour. I might try usenet; there are some good-looking groups on there I think.

Also, I'm sure there has to be a way to do this outside of udev. I mean, if udev can be made to work without using like 30% CPU (which I'd imagine would be what would happen if it polled every 1 second, which is what I'd want it to do), I'll use it, but what about thinking outside the box a little?

I mean, my first thought was to write a kernel module that responded to dmesg at the kernel level; dmesg doesn't update all that often, and I could run through all the lines I recieve and look for, for example, my PCI ID and a signature telling me if it was added or removed. I just have no idea how to do that. tongue

-dav7


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#7 2008-11-27 17:55:31

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

Re: Detecting when a USB device is plugged in/removed

whats the difference from using the screens usb port and a usb port on your machine?

450MHz shouldnt have problems with udev, the only way you could (maybe) improve speed i to compile linux with all drivers you need directly to the kernel instead of modules.

if you compare time/effort you're way better off with udev

Last edited by robmaloy (2008-11-27 17:58:09)


☃ Snowman ☃

Offline

#8 2008-11-28 13:43:09

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Detecting when a USB device is plugged in/removed

Uh, hi there. Quoting my original post:

"This screen has had a hub in it this whole time?!...", I thought, and poked around the front of it for ports. None turn up. The only info I've been able to find out about it is that it's a port for a mouse, of all things, so one can control the OSD via point/click.

But thanks for the advice.

And I don't think udev was specifically designed for this... I would think udev is more suited toward drives, printers, etc. Although I could well be wrong.

-dav7

Last edited by dav7 (2008-11-28 13:44:50)


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#9 2008-11-28 20:19:09

test1000
Member
Registered: 2005-04-03
Posts: 834

Re: Detecting when a USB device is plugged in/removed

try running HAL and see if it's really like "running a multibillion-layer framework like HAL" tongue (maybe you don't even have to run the full hal(!))


KISS = "It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience." - Albert Einstein

Offline

#10 2008-11-29 00:12:36

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Detecting when a USB device is plugged in/removed

Well that started dbus as well and now the system seems more eager to sit on 2.0% CPU usage instead of 1.3% sad

tongue

-dav7


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#11 2008-11-29 10:40:07

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Detecting when a USB device is plugged in/removed

Use HAL. You far overstate it's size.

Otherwise... as said earlier, use udev. It's already detecting the events, so you just need to configure a script to perform whatever tasks you need.

Writing a kernel module for this task is silly, and something that really belongs on dailywtf.

Offline

#12 2008-11-30 13:25:55

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Detecting when a USB device is plugged in/removed

Okay, I'm willing to use HAL and/or udev, I just need to make it "instant".

I set up a rule when I started out on this that echoed some text to a tty to say that it was working, and I just reran it now. There's a 3-4 second delay between my screen switching on and the text appearing in the console.

That might not sound like a lot, but if dmesg can has instant updates, why can't I too? tongue

-dav7


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

Board footer

Powered by FluxBB