Linux USB Device Driver Part6 - Storing Private Data

As there are chances to retrieve information related to interface later in the driver, usb provides an API to store pointer to any data type and saves it in usb_interface structure.

static inline void usb_set_intfdata(struct usb_interface *intf, void *data);

To retrieve the data back, we will use the following API.

static inline void *usb_get_intfdata(struct usb_interface *intf);

Let's update over previous driver code, storing a new variable int count in our private data, which represents the number of times the device has been opened

Code:



Output:




Notes:

1. In the probe function, we allocated memory for private data structure, and initialize the members, and call usb_set_intfdata to save the pointer into usb_interface data structure
2. In the open function, we first get the interface structure by using usb_find_interface API and then get the private data structure using usb_get_intfdata API
3. Finally in the disconnect function, we free the memory and set the interface data to NULL

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig