Home | Projects | Notes > Linux Device Drivers > Exercise 1-3: Pseudo Character Driver (Single Device) - Testing
Build the kernel module pcd.c against the host Linux kernel version
Insert the mode: sudo insmod pcd.ko
Test the pseudo character driver: echo "Hello, welcome!" > /dev/pcd dmesg
Output:
xxxxxxxxxx81[21343.359074] pcd_driver_init :Device number <major>:<minor> = 509:02[21343.359135] pcd_driver_init :Module init was successful3[21401.491786] pcd_open :open was successful4[21401.491856] pcd_write :Write requested for 16 bytes5[21401.491863] pcd_write :Current file position = 06[21401.491868] pcd_write :16 bytes successfully written7[21401.491871] pcd_write :Updated file position = 168[21401.491901] pcd_release :close was successful
echocommand opens the pseudo character device by invoking theopen()system call. (L5:pcd_open()gets called from the driver)L7: When a file is opened, the current file position is always 0.
L10:
echoinvokesclose()system call, and subsequentlypcd_release()of the driver gets called.
Check the contents of the device (buffer): cat /dev/pcd
Output:
xxxxxxxxxx11Hello, welcome!Check the read activity: dmesg | tail
xxxxxxxxxx101[22741.144956] pcd_open :open was successful2[22741.144967] pcd_read :Read requested for 131072 bytes3[22741.144969] pcd_read :Current file position = 04[22741.144971] pcd_read :512 bytes successfully read5[22741.144972] pcd_read :Updated file position = 5126[22741.144980] pcd_read :Read requested for 131072 bytes7[22741.144981] pcd_read :Current file position = 5128[22741.144982] pcd_read :0 bytes successfully read9[22741.144983] pcd_read :Updated file position = 51210[22741.144991] pcd_release :close was successfulL2: Read request for 131072 bytes because that's how the
catprogram is implemented!L5: Updated file position is 512 bytes (i.e.,
DEV_MEM_SIZE) because that's the size of our buffer.L6:
catrequests to read againL8: 0 bytes since no more to read
Attempt copying a large text file (> 512 Bytes) into /dev/pcd: cp file.txt /dev/pcd
Output:
xxxxxxxxxx11cp: error writing '/dev/pcd': Cannot allocate memoryAnd run dmesg to see what happened:
xxxxxxxxxx101[24039.217897] pcd_driver_init :Module init was successful2[24045.901773] pcd_open :open was successful3[24045.901788] pcd_write :Write requested for 1717 bytes4[24045.901790] pcd_write :Current file position = 05[24045.901792] pcd_write :512 bytes successfully written6[24045.901793] pcd_write :Updated file position = 5127[24045.901795] pcd_write :Write requested for 1205 bytes8[24045.901796] pcd_write :Current file position = 5129[24045.901797] pcd_write :No more space left on the device10[24045.901973] pcd_release :release was successfulL6: 512 bytes successfully written
L7: The file position reached the end
L8:
cpcommand attempts to copy the rest of the contents (1717 - 512 = 1205 bytes)L9: Since the file position is already at the end, no more write can be done. (
pcd_write()returns the error code-ENOMEMwhich triggers the error messagecp: error writing '/dev/pcd': Cannot allocate memory.)