Hi, I have a device driver that has many virtual devices that have been added
through PnP via the AddDevice DriverObject Function call via the PnP system.
I can have up to 255 of these devices for this driver. When I have created a
new version of this driver, I update it with the system call,
"UpdateDriverForPlugAndPlayDevices()". I was hoping that this call would
force a call to my driver to "REMOVE_DEVICE" one at a time until there are
no devices. At this time the PnP system would call my unload routine. Then
when the PnP system calls the AddDevice routine the new driver would be
loaded.

It does work this way if I only have one or two devices on the driver.
However, If I have more devices than this, then the PnP system sends the
"REMOVE_DEVICE" IOCTL for a few of them, then calls the AddDevice for the
devices that were just removed. Then it repeats this until all of the
devices have been removed and re-added. Consequently, the number of devices
never reaches zero and the unload routine is never called. At the end of the
driver update, a reboot is required, because the previous version of the
driver is still the running driver.

My question is this: Is there anyway to tell
UpdateDriversForPlugAndPlayDevices to remove all the devices before adding
them back?

Thanks in advance,
-David G

Re: UpdateDriverForPlugAndPlayDevices does not remove all devices. by Maxim

Maxim
Fri Apr 07 19:46:28 CDT 2006

> It does work this way if I only have one or two devices on the driver.
> However, If I have more devices than this, then the PnP system sends the
> "REMOVE_DEVICE" IOCTL for a few of them, then calls the AddDevice for the
> devices that were just removed. Then it repeats this until all of the
> devices have been removed and re-added. Consequently, the number of
>devices
> never reaches zero

Why? When the first portion is processed, your driver is derefed several times,
and is no more addrefed since these stacks are rebuilt with the new, updated
driver. So, the sequence will cause your driver to unload.

Or are you speaking about the bus driver?

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com


Re: UpdateDriverForPlugAndPlayDevices does not remove all devices. by Koen

Koen
Mon Apr 10 08:08:28 CDT 2006


"David G" <David G@discussions.microsoft.com> wrote in message
news:58FB4753-AECD-450A-9BB1-D66C5ABE599A@microsoft.com...
> Hi, I have a device driver that has many virtual devices that have been
added
> through PnP via the AddDevice DriverObject Function call via the PnP
system.
> I can have up to 255 of these devices for this driver. When I have
created a
> new version of this driver, I update it with the system call,
> "UpdateDriverForPlugAndPlayDevices()". I was hoping that this call would
> force a call to my driver to "REMOVE_DEVICE" one at a time until there
are
> no devices. At this time the PnP system would call my unload routine.
Then
> when the PnP system calls the AddDevice routine the new driver would be
> loaded.
>
> It does work this way if I only have one or two devices on the driver.
> However, If I have more devices than this, then the PnP system sends the
> "REMOVE_DEVICE" IOCTL for a few of them, then calls the AddDevice for the
> devices that were just removed. Then it repeats this until all of the
> devices have been removed and re-added. Consequently, the number of
devices
> never reaches zero and the unload routine is never called. At the end of
the
> driver update, a reboot is required, because the previous version of the
> driver is still the running driver.
>
> My question is this: Is there anyway to tell
> UpdateDriversForPlugAndPlayDevices to remove all the devices before adding
> them back?
>
> Thanks in advance,
> -David G

I've had a similar problem once. To avoid this, let each driver instance
indicate its relation to the others when being queried by PnP manager(see
IRP_MN_QUERY_DEVICE_RELATIONS).

Koen



Re: UpdateDriverForPlugAndPlayDevices does not remove all devices. by DavidG

DavidG
Mon Apr 10 10:56:01 CDT 2006

Let's say there were 2 device nodes for this driver, Com7, and Com9. When
UpdateDriverForPlugAndPlayDevices is called it might send a remove request
for Com7. Then it might send an AddDevice request for Com7. Then it sends a
request to remove Com9 and then sends an AddDevice for Com9. As far as the
driver is concerned the devices never reached 0 and the Plug and Play Manager
never calls the unload routine and the new driver is never loaded.

Koen had a great idea to have them all reference each other with
IRP_MN_QUERY_DEVICE_RELATIONS. I will try this and see what happens.

-David G

"Maxim S. Shatskih" wrote:

> > It does work this way if I only have one or two devices on the driver.
> > However, If I have more devices than this, then the PnP system sends the
> > "REMOVE_DEVICE" IOCTL for a few of them, then calls the AddDevice for the
> > devices that were just removed. Then it repeats this until all of the
> > devices have been removed and re-added. Consequently, the number of
> >devices
> > never reaches zero
>
> Why? When the first portion is processed, your driver is derefed several times,
> and is no more addrefed since these stacks are rebuilt with the new, updated
> driver. So, the sequence will cause your driver to unload.
>
> Or are you speaking about the bus driver?
>
> --
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> maxim@storagecraft.com
> http://www.storagecraft.com
>
>

Re: UpdateDriverForPlugAndPlayDevices does not remove all devices. by DavidG

DavidG
Mon Apr 10 10:57:02 CDT 2006

Thanks Koen, that is a great idea, I will try this.
-David G

"Koen" wrote:

>
> "David G" <David G@discussions.microsoft.com> wrote in message
> news:58FB4753-AECD-450A-9BB1-D66C5ABE599A@microsoft.com...
> > Hi, I have a device driver that has many virtual devices that have been
> added
> > through PnP via the AddDevice DriverObject Function call via the PnP
> system.
> > I can have up to 255 of these devices for this driver. When I have
> created a
> > new version of this driver, I update it with the system call,
> > "UpdateDriverForPlugAndPlayDevices()". I was hoping that this call would
> > force a call to my driver to "REMOVE_DEVICE" one at a time until there
> are
> > no devices. At this time the PnP system would call my unload routine.
> Then
> > when the PnP system calls the AddDevice routine the new driver would be
> > loaded.
> >
> > It does work this way if I only have one or two devices on the driver.
> > However, If I have more devices than this, then the PnP system sends the
> > "REMOVE_DEVICE" IOCTL for a few of them, then calls the AddDevice for the
> > devices that were just removed. Then it repeats this until all of the
> > devices have been removed and re-added. Consequently, the number of
> devices
> > never reaches zero and the unload routine is never called. At the end of
> the
> > driver update, a reboot is required, because the previous version of the
> > driver is still the running driver.
> >
> > My question is this: Is there anyway to tell
> > UpdateDriversForPlugAndPlayDevices to remove all the devices before adding
> > them back?
> >
> > Thanks in advance,
> > -David G
>
> I've had a similar problem once. To avoid this, let each driver instance
> indicate its relation to the others when being queried by PnP manager(see
> IRP_MN_QUERY_DEVICE_RELATIONS).
>
> Koen
>
>
>

Re: UpdateDriverForPlugAndPlayDevices does not remove all devices. by Maxim

Maxim
Mon Apr 10 14:24:28 CDT 2006

> UpdateDriverForPlugAndPlayDevices is called it might send a remove request
> for Com7. Then it might send an AddDevice request for Com7. Then it sends

It sends remove to the old driver and AddDevice to the new one.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com


Re: UpdateDriverForPlugAndPlayDevices does not remove all devices. by DavidG

DavidG
Tue Apr 11 16:04:02 CDT 2006

Koen's idea worked. The PnP IOCTL, IRP_MN_QUERY_DEVICE_RELATIONS, is called
with a type of RemovalRelations before a device is removed. I needed to
return a list of the other ports in the form of their DeviceObjects on the
removal of the first virtual device.

When not upgrading and virtual ports are being added and removed, I don't
want to return anything on the IRP_MN_QUERY_DEVICE_RELATIONS /
RemovalRelations IOCTL call. In this way the PnP Manager will remove the
virtual device under question and only that device.

In the upgrade case, I send a backdoor IOCTL to my current driver, before
the call to UpdateDriverForPlugAndPlayDevices, stating that an upgrade is
coming. Now when the IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations IOCTL
comes down I return the DeviceObjects of the other virtual devices, but not
the DeviceObject of the current virtual device being removed. The PnP
Manager will go and remove all of the other virtual devices, (which do not
return anything on their IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations
IOCTL calls), before it removes the first virtual device the PnP Manager
called. When this last virtual device is removed, the unload routine of the
driver is called. Then when the AddDevice function is called by the PnP
Manager, it will be the AddDevice of the new driver.

If the unload routine is not called, then the old driver is not removed and
the AddDevice is done on the old driver. The new driver is in the
system32\drivers directory, but will not be loaded until a reboot of the PC.

So, thanks. The upgrade is working great.

-David G

"Maxim S. Shatskih" wrote:

> > UpdateDriverForPlugAndPlayDevices is called it might send a remove request
> > for Com7. Then it might send an AddDevice request for Com7. Then it sends
>
> It sends remove to the old driver and AddDevice to the new one.
>
> --
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> maxim@storagecraft.com
> http://www.storagecraft.com
>
>