# USB devices and device node creation



## peterlaursen (Jul 29, 2010)

Dear everyone,

I have a few questions with regard to USB device drivers and device node creation. I should mention that I am a newbe in programming device drivers.

I am trying to write a USB device driver for a Braille display. I would ideally like to give the Braille display its own device node in /dev so that programs could write to the device, thereby displaying text on the display without knowing the technical details. I have studied a lot of different USB device drivers, but have been unable to find a satisfactory answer.

The driver that comes closest to what I want to achieve is the driver located in /usr/share/examples/kld/cdev/module. However, this is not a device driver and I am unable to understand how I would create the device node for a real USB device. How would one create that device node?

I would also assume that the device_attach method would be the correct place to create a device node?

From looking at the driver I mentioned above, I see that the open(), close(), read() and write() functions are declared. Can those functions be declared in the USB device driver too and serve a similar purpose as they do in the cdev driver?

Are there any good references on creating USB device drivers for FreeBSD? The documentation seems to be very scarce.

One final question: Say that a program opens the newly created device node. How does the system know that this device node belongs to a particular attached device?

I think that was all for now.

Any help is greatly appreciated and I look forward to any replies,

Peter.
FreeBSD 8.1-i386


----------



## SirDice (Jul 29, 2010)

peterlaursen said:
			
		

> Are there any good references on creating USB device drivers for FreeBSD? The documentation seems to be very scarce.


Did you read the architecture handbook?

Chapter 13 USB Devices


----------



## peterlaursen (Jul 29, 2010)

SirDice said:
			
		

> Did you read the architecture handbook?
> 
> Chapter 13 USB Devices



Yes, I have read that chapter of the handbook. I was not able to find the answers to my questions.

I also got a copy of the USB specs and have also studied it.

Thanks for the reply,

Peter.


----------



## peterlaursen (Aug 24, 2010)

*Answers*

I have discovered most of the answers to my questions. In case other people have similar questions, let me share the answers I found with the community.

The reason that I could find no calls to make_dev in the USB drivers I looked through (keyboard, mass storage etc) was that they were just "front ends to a different device driver, such as the keyboard driver. All device node creations were handled there, not in the USB device river, but in the keyboard device driver.

I was correct in assuming that the device_attach method is the correct place to call make_dev(9). Of course, one should also call destroy_dev(9) in the detach routine once the device is detached.

The open, close, read and write methods work the same in USB devices as they do in the example device driver. In the write method, you'll have to start a USB transfer and then write the data to your device. This was confusing me for quite a while. The usbdi(9) man page gives a little more information.

I think that was all and I hope the information has proved useful. If I have gotten something wrong, feel free to correct me.

Peter.


----------

