In my KMDF driver I want to restrict my USB device to only allow one
to be used on a PC at one time. What is the best place for doing
this? I am thinking I should add a flag to my driver object context
that i will check/set in EvtDeviceAdd and return an error if a device
already exists. Any issues with this implementation?
Thanks!
Jeremy
That’s exactly how I’d do it…
Peter
OSR
This is a good pattern but you have to be careful about when you clear the flag. It must be cleared in the surprise remove case as well as the graceful remove because you will want another instance to be brought up while an old one is in the surprise removed state. That means clearing the flag in the WDFDEVICE’s cleanup() routine is not sufficient. You should clear the flag on EvtDeviceSelfManagedIoFlush which is called in both graceful and surprise remove at the right time to perform this type of state management
d
Sent from my phone with no t9, all spilling mistakes are not intentional.
-----Original Message-----
From: Jeremy Ramer
Sent: Monday, March 09, 2009 1:54 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Restrict device add
In my KMDF driver I want to restrict my USB device to only allow one
to be used on a PC at one time. What is the best place for doing
this? I am thinking I should add a flag to my driver object context
that i will check/set in EvtDeviceAdd and return an error if a device
already exists. Any issues with this implementation?
Thanks!
Jeremy
—
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
Excellent point, Doron. I didn’t think of that.
How about the Destroy event processing callback for the WDFDevice? Isn’t that called during destruction? If so, that would be a bit more clear, I think.
Peter
OSR
Destroy() is called after cleanup(). Cleanup() gives you orderly removal, an irql contract on some objects, and a chance to break ref cycles. destroy() is called when the ref goes to zero and has no ordering or irql contracts associated with it. The issue here is that for a surprise removed device it can hang out in limbo without being removed (which is what triggers cleanup()) while another device is plugged in, making both cleanup() and destroy() not good for maintaining this type of state
d
Sent from my phone with no t9, all spilling mistakes are not intentional.
-----Original Message-----
From: xxxxx@osr.com
Sent: Tuesday, March 10, 2009 6:48 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Restrict device add
Excellent point, Doron. I didn’t think of that.
How about the Destroy event processing callback for the WDFDevice? Isn’t that called during destruction? If so, that would be a bit more clear, I think.
Peter
OSR
—
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
(Not arguing here, merely trying to understand)
I see… There’s a handle present, so cleanup will get called when the handle count goes to zero. Won’t this happen quickly in the surprise remove case?
I thought the issue to which you were alluding is that there might be a hanging REFERENCE to the WDF Device… and that would be handled by destroy being called (when the reference count on the WDF Device goes to zero), which is AFTER cleanup.
But once any child objects are deleted, won’t the WDF Device be immediately destructed? I guess I’m trying to understand the case when there’d be a “zombie” WDF Device hanging around, and destroy not being called… what could still be holding a reference on the object preventing destroy from being called?
Peter
OSR
I was just looking at this and was about to ask the question. I was
planning on clearing it in the device cleanup routine, but as you
suggest I can put it in EvtDeviceSelfManagedIoFlush. Thanks for the
pre-emptive answer!
Jeremy
On Tue, Mar 10, 2009 at 7:38 AM, Doron Holan wrote:
> This is a good pattern but you have to be careful about when you clear the flag. It must be cleared in the surprise remove case as well as the graceful remove because you will want another instance to be brought up while an old one is in the surprise removed state. That means clearing the flag in the WDFDEVICE’s cleanup() routine is not sufficient. You should clear the flag on EvtDeviceSelfManagedIoFlush which is called in both graceful and surprise remove at the right time to perform this type of state management
>
> d
>
> Sent from my phone with no t9, all spilling mistakes are not intentional.
>
> -----Original Message-----
> From: Jeremy Ramer
> Sent: Monday, March 09, 2009 1:54 PM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] Restrict device add
>
>
> In my KMDF driver I want to restrict my USB device to only allow one
> to be used on a PC at one time. ?What is the best place for doing
> this? ?I am thinking I should add a flag to my driver object context
> that i will check/set in EvtDeviceAdd and return an error if a device
> already exists. ?Any issues with this implementation?
>
> Thanks!
> Jeremy
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
I am referring to a file handle ref,not a wdf object ref. Cleanup() is called regardless of the object ref count, if you leak a ref on the wdfdevice, its destroy() will not be called. As we have seen from many prev posters, the last handle may not go away very quickly or at all because the poster may not have control over the app which opened the handle. To be complete, i am suggested the self managed io flush pattern which covers all cases cleanly. If you control the app and know for certaih handles will be closed immediatelt, cleanup() can clear the flag (but would to me be a spot of fragility later when you cannot make the app guarantee).
d
Sent from my phone with no t9, all spilling mistakes are not intentional.
-----Original Message-----
From: xxxxx@osr.com
Sent: Tuesday, March 10, 2009 7:14 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Restrict device add
(Not arguing here, merely trying to understand)
I see… There’s a handle present, so cleanup will get called when the handle count goes to zero. Won’t this happen quickly in the surprise remove case?
I thought the issue to which you were alluding is that there might be a hanging REFERENCE to the WDF Device… and that would be handled by destroy being called (when the reference count on the WDF Device goes to zero), which is AFTER cleanup.
But once any child objects are deleted, won’t the WDF Device be immediately destructed? I guess I’m trying to understand the case when there’d be a “zombie” WDF Device hanging around, and destroy not being called… what could still be holding a reference on the object preventing destroy from being called?
Peter
OSR
—
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
AH, got it. I was “over thinking” the issue… but you’re alluding to the basic, ordinary, case. Duh!
Thanks, Doron.
Peter
OSR
Thanks Doron, that’s good info to keep in mind.
I have one further question on synchronization. Flags in my device
context should be kept safe by the Automatic Synchronization of the
framework as I only access them in the device callbacks. However, that
would not be the case for the driver context, correct? i.e.
EvtDriverDeviceAdd could be called for two devices at the same time.
At least, the documentation on Synchronization does not seem to
preclude this. So I should be using a lock to protect my driver flag.
Jeremy
On Tue, Mar 10, 2009 at 8:25 AM, wrote:
> AH, got it. ?I was “over thinking” the issue… but you’re alluding to the basic, ordinary, case. ?Duh!
>
> Thanks, Doron.
>
> Peter
> OSR
>
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
Device sync scope does not guard adddevice() so you need to provide your own
d
Sent from my phone with no t9, all spilling mistakes are not intentional.
-----Original Message-----
From: Jeremy Ramer
Sent: Tuesday, March 10, 2009 7:40 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] Restrict device add
Thanks Doron, that’s good info to keep in mind.
I have one further question on synchronization. Flags in my device
context should be kept safe by the Automatic Synchronization of the
framework as I only access them in the device callbacks. However, that
would not be the case for the driver context, correct? i.e.
EvtDriverDeviceAdd could be called for two devices at the same time.
At least, the documentation on Synchronization does not seem to
preclude this. So I should be using a lock to protect my driver flag.
Jeremy
On Tue, Mar 10, 2009 at 8:25 AM, wrote:
> AH, got it. I was “over thinking” the issue… but you’re alluding to the basic, ordinary, case. Duh!
>
> Thanks, Doron.
>
> Peter
> OSR
>
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
—
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
Btw, I wrote about this in 2007 in more depth, http://blogs.msdn.com/doronh/archive/2007/02/26/which-callback-is-called-on-surprise-or-graceful-removal.aspx
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Tuesday, March 10, 2009 7:26 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Restrict device add
AH, got it. I was “over thinking” the issue… but you’re alluding to the basic, ordinary, case. Duh!
Thanks, Doron.
Peter
OSR
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
as much as I have been lapsing in writing posts for the last year or so, it is there 
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Tuesday, March 10, 2009 2:25 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Restrict device add
You have a blog?
(sorry…)
Peter
OSR
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
For the archives, and for those who don’t realize it: I’m teasing Doron.
His long standing blog:
http://blogs.msdn.com/doronh/
is one of the very few things on line to do with driver development that I admire and (try to) read regularly. I’m not saying there aren’t other good blogs. Just saying that Doron’s is one I try not to miss.
(Will you unlock your door and let me back in your office now, Doron?)
Peter
OSR
You have to get past the bubble wrap first (thanks to Patrick, http://blogs.msdn.com/888_umdf_4_you/)! When I was on parental leave Patrick wrapped my entire office (floor, door, bookcases, the Brain statue, keyboards, chair, etc) in bubble wrap and left a note on the door letting me know that “facilities” has child proofed my office for my future safety. All that remains are a wrapped Brain and door…
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Tuesday, March 10, 2009 3:15 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Restrict device add
For the archives, and for those who don’t realize it: I’m teasing Doron.
His long standing blog:
http://blogs.msdn.com/doronh/
is one of the very few things on line to do with driver development that I admire and (try to) read regularly. I’m not saying there aren’t other good blogs. Just saying that Doron’s is one I try not to miss.
(Will you unlock your door and let me back in your office now, Doron?)
Peter
OSR
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
You’ll leave the brain wrapped, of course.
There’s probably some rule that prohibits you leaving the door wrapped.
Peter
OSR
(Personally responsible for the subversion of THIS thread, which started with an actual useful and interesting question, into pointless trivia and thus oblivion. Can you tell I’m trying to avoid my real work? Which, in this case, is writing the text for the “USB Analyzer Shoot Out” article for the upcoming USB MegaIssue of The NT Insider).
Door:
http://blogs.msdn.com/photos/doronh/picture9469771.aspx
Brain:
http://blogs.msdn.com/photos/doronh/picture9469770.aspx
door is still wrapped 3 months later. It is a great ice breaker. Someone comes to ask me a question and while we talk they find an intact bubble on the door to pop. Not to mention it bothers the crap out of Eliyas when some pops a lot of them in succession 
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Tuesday, March 10, 2009 3:30 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Restrict device add
You’ll leave the brain wrapped, of course.
There’s probably some rule that prohibits you leaving the door wrapped.
Peter
OSR
(Personally responsible for the subversion of THIS thread, which started with an actual useful and interesting question, into pointless trivia and thus oblivion. Can you tell I’m trying to avoid my real work? Which, in this case, is writing the text for the “USB Analyzer Shoot Out” article for the upcoming USB MegaIssue of The NT Insider).
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
Maybe we could take up a collection somehow via PayPal to buy an infinite
supply of bubblewrap to refresh the door…
I mean, doesn’t Elyias want that kind of ‘theraputic’ stimulus constantly?
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Doron Holan
Sent: Tuesday, March 10, 2009 6:44 PM
To: Windows System Software Devs Interest List
Subject: RE: RE:[ntdev] Restrict device add
Door:
http://blogs.msdn.com/photos/doronh/picture9469771.aspx
Brain:
http://blogs.msdn.com/photos/doronh/picture9469770.aspx
door is still wrapped 3 months later. It is a great ice breaker. Someone
comes to ask me a question and while we talk they find an intact bubble on
the door to pop. Not to mention it bothers the crap out of Eliyas when some
pops a lot of them in succession 
d
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Tuesday, March 10, 2009 3:30 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Restrict device add
You’ll leave the brain wrapped, of course.
There’s probably some rule that prohibits you leaving the door wrapped.
Peter
OSR
(Personally responsible for the subversion of THIS thread, which started
with an actual useful and interesting question, into pointless trivia and
thus oblivion. Can you tell I’m trying to avoid my real work? Which, in
this case, is writing the text for the “USB Analyzer Shoot Out” article for
the upcoming USB MegaIssue of The NT Insider).
—
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer
—
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer
David R. Cattley wrote:
Maybe we could take up a collection somehow via PayPal to buy an infinite
supply of bubblewrap to refresh the door…
I mean, doesn’t Elyias want that kind of ‘theraputic’ stimulus constantly?
There’s a cheaper alternative:
http://www.thinkgeek.com/geektoys/japanfan/982f/
Think Geek is one of the greatest vendors on Earth. I’ve spent way too
much money there.
–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.