Is there some implicit locking of device methods?

Bartosz Fabianowski freebsd at chillt.de
Wed Apr 27 22:14:52 UTC 2011


Indeed, I may have mixed up terminology. Sorry about that. What I am 
doing (or trying to do) is very simple:

There is a single physical USB device. I have a single device node 
representing it. This device can be opened for reading, concurrently, 
any number of times. Everyone who open()s the device can read() it at 
their own pace. I implemented this by maintaining an individual queue of 
incoming data for each open() call. This queue resides in cdevpriv.

So open() instantiates a queue and adds it to the driver's global list 
of queues. Whenever a packet arrives from the device, it is placed in 
all the queues (I have a linked list of all queues for that purpose). 
When the open() is eventually followed by a close(), the cdevpriv 
destructor removes the queue from the global list and frees its memory.

In addition to this, I need to start the USB transfer when the first 
open() occurs and stop it again when the last close() occurs. I am doing 
this by checking the length of the global list. When the list is 
zero-length on open(), I start the transfer. When the list i zero-length 
in the cdevpriv destructor, I stop the transfer.

I cannot see how else to achieve this behavior (other than device 
cloning which I was using before but which is more complicated and 
probably more error-prone). If I am doing something wrong and there is a 
more correct way to do it, I would love to hear about it.

- Bartosz


More information about the freebsd-hackers mailing list