Linux Device driver Programming Lecture 49- Pcd driver with multiple devices code implementation part-1

 

Pcd driver with multiple devices code implementation part-1

 

 

Now, let’s implement the below exercise(shown in Figure 1).

Figure 1. Exercise
Figure 1. Exercise

 

First of all, let’s get into the command prompt. Here, just create another directory 003_pseudo_char_driver_multiple. And after that, let’s get into that directory, as shown in Figure 2.

Figure 2. Go inside the directory
Figure 2. Go inside the directory

 

This is basically empty. Let’s copy our previous exercise here. Because the exercise will be almost the same, but the only thing is we have to give multiple device support, and we have to maintain some data structure for the driver and the devices. That’s why, instead of writing everything from scratch, now let’s reuse our previous code.

For that, let’s copy from 002, I’m going to copy a pcd.c. Paste here. And you also need the make file. Let’s copy the makefile as well Figure 3.

Figure 3. Copying previous source file and makefile
Figure 3. Copying previous source file and makefile

 

And now let me just rename this file pcd to pcd underscore, let’s say n.  Pcd_n.c as shown in Figure 4.

Figure 4. Rename the file
Figure 4. Rename the file

 

Let me open pcd_n.c as shown in Figure 5.

Figure 5. vim pcd_n.c
Figure 5. vim pcd_n.c

 

Pcd driver with multiple devices code implementation part-1
Figure 6. Creating pseudo device memory and driver private data structure

 

So, now, first of all, the headers. Headers will be a as it is. And below that is for our printing purpose. Let’s keep this as it is.  

Here, the  MEM_SIZE could be different because we are now maintaining a 4 devices. That’s why let’s change this  #define to something else. I would say MEM_SIZE_MAX for PCDEV1. For the PCDEV1, I would reserve 1024 bytes.

Now, let me repeat this macro 4 times. for PCDEV2, PCDEV3, PCDEV4. I would keep 512 here or any number you can choose.

 

And after that, the device buffer. Every device will have its own buffer. Buffer is nothing but our device. So, that’s why let’s a create four buffers. Let me also copy this and let me paste 4 times. So, now this device buffer for PCDEV1. This is for PCDEV2, 3, and 4. Let’s change is code. I will write it _pcdev1. This is pcdev2.

Here, in the array, so and the size is you have to give the respective size,  this will be  MEM_SIZE_MAX_PCDEV_1. We have created 4 arrays now. 

After that, let’s give the definition of our driver’s private data structure. Let’s give the definition first, this will be the driver’s private data. So, pcdrv_private_data. pcdrv_private_data. This will contain two member elements. One is total_devices, and the second one is a instances for the device’s private data structure.

Basically, since we are maintaining 4 devices, so I’ll be using array. Struct pcdev private_data. I would create a variable pcdev_data of [NO_OF_DEVICES]. So, the number of devices and this structure is not define.

Let’s create another structure here for device private data. This is nothing but struct pcdev private_data.

/* pseudo device's memory */
char device_buffer_pcdev1[MEM_SIZE_MAX_PCDEV1];
char device_buffer_pcdev2[MEM_SIZE_MAX_PCDEV2];
char device_buffer_pcdev3[MEM_SIZE_MAX_PCDEV3];
char device_buffer_pcdev4[MEM_SIZE_MAX_PCDEV4];

/*Device private data structure */

struct pcdev_private_data
{
    char *buffer;
    unsigned size;
    const char *serial_number;
    int perm;
    struct cdev cdev;
};


/*Driver private data structure */
struct pcdrv_private_data
{
    int total_devices;
    struct pcdev_private_data pcdev_data[NO_OF_DEVICES];
};

Device private data structure

The first member element is the buffer. Second could be size, you can use unsigned for that. Size. And after that, the third one is serial_number.  

And after that is a permission. I’m just using int, so I’m just using int everywhere because to keep this simple. You can also use typedef fixed integer datatype like u8, u32, u16. All that will start using little later, but let’s keep it simple. Here let’s use the struct cdev. This is our device private data structure, and this is our driver’s private data structure.

 

Pcd driver with multiple devices code implementation part-1
Figure 7. Defining a macro NO_OF_DEVICES

 

And we have to create one more macro maybe I would create here. define NO_OF_DEVICES let’s take 4.  I hope you understood about these two data structures, and the driver is going to manipulate all these data structure. I hope you can complete up to here and I’ll see you in the next article.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.