SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Character Drivers
2© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
After this session, you would know
W's of Character Drivers
Major & Minor Numbers
Registering & Unregistering Character Driver
File Operations of a Character Driver
Writing a Character Driver
Linux Device Model
udev & automatic device creation
3© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Character Drivers
What does “Character” stand for?
Look at entries starting with 'c' after
ls -l /dev
Device File Name
User Space specific
Used by Applications
Device File Number
Kernel Space specific
Used by Kernel Internals as easy for Computation
4© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Major & Minor Number
ls -l /dev
Major is to Category; Minor is to Device
Data Structures described in Kernel C in object
oriented fashion
Type Header: <linux/types.h>
Type: dev_t – 12 bits for major & 20 bits for minor
Macro Header: <linux/kdev_t.h>
MAJOR(dev_t dev)
MINOR(dev_t dev)
MKDEV(int major, int minor)
5© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
3 Entities in 3 Spaces
Device
Driver
/dev/io
Device
Kernel Space
User Space
Hardware
Space
VFS
Device File
Application
open()
6© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Registering & Unregistering
Registering the Device Driver
int register_chrdev_region(dev_t first, unsigned int count, char *name);
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned
int cnt, char *name);
Unregistering the Device Driver
void unregister_chrdev_region(dev_t first, unsigned int count);
Header: <linux/fs.h>
Kernel Window: /proc/devices
7© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The file operations
struct file_operations
struct module owner = THIS_MODULE; /* <linux/module.h> */
int (*open)(struct inode *, struct file *);
int (*release)(struct inode *, struct file *);
ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
loff_t (*llseek)(struct file *, loff_t, int);
int (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
Header: <linux/fs.h>
8© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Initialization for Registration
1st
way initialization
struct cdev *my_cdev = cdev_alloc();
my_cdev->owner = THIS_MODULE;
my_cdev->ops = &my_fops;
2nd
way initialization
struct cdev my_cdev;
cdev_init(&my_cdev, &my_fops);
Header: <linux/cdev.h>
9© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Registering the file operations
The Registration
int cdev_add(struct cdev *cdev, dev_t num, unsigned int count);
The Unregistration
void cdev_del(struct cdev *cdev);
Header: <linux/cdev.h>
10© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The file & inode structures
Important fields of struct file
mode_t f_mode
loff_t f_pos
unsigned int f_flags
struct file_operations *f_op
void *private_data
Important fields of struct inode
unsigned int iminor(struct inode *);
unsigned int imajor(struct inode *);
11© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Register/Unregister: Old Way
Registering the Device Driver
int register_chrdev(unsigned int major, const char *name, struct
file_operations *fops);
Unregistering the Device Driver
int unregister_chrdev(unsigned int major, const char *name);
12© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The /dev/null read & write
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
{
...
return read_cnt;
}
ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off)
{
...
return wrote_cnt;
}
13© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The read flow
struct file
-------------------------
f_count
f_flags
f_mode
-------------------------
f_pos
-------------------------
...
...
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
Buffer
(in the driver)
Buffer
(in the
application
or libc)
Kernel Space (Non-swappable) User Space (Swappable)
copy_to_user
14© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The write flow
struct file
-------------------------
f_count
f_flags
f_mode
-------------------------
f_pos
-------------------------
...
...
ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off)
Buffer
(in the driver)
Buffer
(in the
application
or libc)
Kernel Space (Non-swappable) User Space (Swappable)
copy_from_user
15© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The mem device read
#include <asm/uaccess.h>
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
{
...
if (copy_to_user(buf, from, cnt) != 0)
{
return -EFAULT;
}
...
return read_cnt;
}
16© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The mem device write
#include <asm/uaccess.h>
ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off)
{
...
if (copy_from_user(to, buf, cnt) != 0)
{
return -EFAULT;
}
...
return wrote_cnt;
}
17© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The I/O Control API
API
int (*unlocked_ioctl)(struct file *, unsigned int cmd,
unsigned long arg)
Command
Macros
_IO, _IOW, _IOR, _IOWR
Parameters
type (character) [15:8]
number (index) [7:0]
size (param type) [29:16]
Header: <linux/ioctl.h> →...→ <asm-generic/ioctl.h>
size [29:16] num[7:0]type[15:8]
dir[31:30]
18© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Device Model (LDM)
struct kobject - <linux/kobject.h>
kref object
Pointer to kset, the parent object
kobj_type, type describing the kobject
kobject instantiation → sysfs representation
Parent object guides the entries under /sys/
bus – the physical buses
class – the device categories
device – the actual devices
19© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
udev & LDM
Daemon: udevd
Configuration: /etc/udev/udev.conf
Rules: /etc/udev/rules.d/
Utility: udevinfo [-a] [-p <device_path>]
Receives uevent on a change in /sys
Accordingly, updates /dev &/or
Performs the appropriate action for
Hotplug
Microcode / Firmware Download
Module Autoload
20© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Device Model & Classes
Latest way to create dynamic devices
Create or Get the appropriate device category
Create the desired device under that category
Class Operations
struct class *class_create(struct module *owner, char
*name);
void class_destroy(struct class *cl);
Device into & out of Class
struct class_device *device_create(struct class *cl, NULL,
dev_t devnum, NULL, const char *fmt, ...);
void device_destroy(struct class *cl, dev_t devnum);
21© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of Character Drivers
Major & Minor Numbers
Registering & Unregistering Character
Driver
File Operations of a Character Driver
Writing a Character Driver
Linux Device Model
udev & automatic device creation
22© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

PDF
Spi drivers
pradeep_tewani
 
PDF
Linux Porting
Anil Kumar Pugalia
 
PDF
Arm device tree and linux device drivers
Houcheng Lin
 
PDF
Block Drivers
Anil Kumar Pugalia
 
PDF
Introduction to Linux Drivers
Anil Kumar Pugalia
 
PDF
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
PDF
Character drivers
pradeep_tewani
 
Spi drivers
pradeep_tewani
 
Linux Porting
Anil Kumar Pugalia
 
Arm device tree and linux device drivers
Houcheng Lin
 
Block Drivers
Anil Kumar Pugalia
 
Introduction to Linux Drivers
Anil Kumar Pugalia
 
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
Character drivers
pradeep_tewani
 

What's hot (20)

PDF
Linux kernel debugging
libfetion
 
PDF
Interrupts
Anil Kumar Pugalia
 
PDF
USB Drivers
Anil Kumar Pugalia
 
PDF
I2c drivers
pradeep_tewani
 
PPTX
The TCP/IP Stack in the Linux Kernel
Divye Kapoor
 
PPTX
Linux Kernel Booting Process (1) - For NLKB
shimosawa
 
PPTX
Linux Initialization Process (2)
shimosawa
 
PDF
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
 
PPTX
U-Boot Porting on New Hardware
RuggedBoardGroup
 
PDF
Linux Internals - Part I
Emertxe Information Technologies Pvt Ltd
 
PDF
DWARF Data Representation
Wang Hsiangkai
 
PPTX
Linux I2C
KaidenYu
 
PDF
Network Drivers
Anil Kumar Pugalia
 
PDF
Making Linux do Hard Real-time
National Cheng Kung University
 
PDF
Uboot startup sequence
Houcheng Lin
 
PPTX
U-Boot presentation 2013
Wave Digitech
 
PDF
Video Drivers
Anil Kumar Pugalia
 
PDF
Linux Porting to a Custom Board
Patrick Bellasi
 
PDF
ACPI Debugging from Linux Kernel
SUSE Labs Taipei
 
Linux kernel debugging
libfetion
 
Interrupts
Anil Kumar Pugalia
 
USB Drivers
Anil Kumar Pugalia
 
I2c drivers
pradeep_tewani
 
The TCP/IP Stack in the Linux Kernel
Divye Kapoor
 
Linux Kernel Booting Process (1) - For NLKB
shimosawa
 
Linux Initialization Process (2)
shimosawa
 
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
 
U-Boot Porting on New Hardware
RuggedBoardGroup
 
Linux Internals - Part I
Emertxe Information Technologies Pvt Ltd
 
DWARF Data Representation
Wang Hsiangkai
 
Linux I2C
KaidenYu
 
Network Drivers
Anil Kumar Pugalia
 
Making Linux do Hard Real-time
National Cheng Kung University
 
Uboot startup sequence
Houcheng Lin
 
U-Boot presentation 2013
Wave Digitech
 
Video Drivers
Anil Kumar Pugalia
 
Linux Porting to a Custom Board
Patrick Bellasi
 
ACPI Debugging from Linux Kernel
SUSE Labs Taipei
 
Ad

Viewers also liked (13)

PDF
Platform Drivers
SysPlay eLearning Academy for You
 
PDF
File System Modules
Anil Kumar Pugalia
 
PDF
PCI Drivers
Anil Kumar Pugalia
 
PDF
Low-level Accesses
Anil Kumar Pugalia
 
PDF
Kernel Programming
Anil Kumar Pugalia
 
PDF
Audio Drivers
Anil Kumar Pugalia
 
PDF
BeagleBoard-xM Bootloaders
SysPlay eLearning Academy for You
 
PDF
Embedded C
Anil Kumar Pugalia
 
PDF
References
Anil Kumar Pugalia
 
PDF
Kernel Debugging & Profiling
Anil Kumar Pugalia
 
PDF
gcc and friends
Anil Kumar Pugalia
 
PDF
File Systems
Anil Kumar Pugalia
 
File System Modules
Anil Kumar Pugalia
 
PCI Drivers
Anil Kumar Pugalia
 
Low-level Accesses
Anil Kumar Pugalia
 
Kernel Programming
Anil Kumar Pugalia
 
Audio Drivers
Anil Kumar Pugalia
 
BeagleBoard-xM Bootloaders
SysPlay eLearning Academy for You
 
Embedded C
Anil Kumar Pugalia
 
References
Anil Kumar Pugalia
 
Kernel Debugging & Profiling
Anil Kumar Pugalia
 
gcc and friends
Anil Kumar Pugalia
 
File Systems
Anil Kumar Pugalia
 
Ad

Similar to Character Drivers (20)

PDF
Linux Char Device Driver
Gary Yeh
 
PDF
Introduction to char device driver
Vandana Salve
 
PDF
Linux device drivers
Emertxe Information Technologies Pvt Ltd
 
PPTX
Char Drivers And Debugging Techniques
YourHelper1
 
PDF
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
PPTX
Introduction Linux Device Drivers
NEEVEE Technologies
 
PPT
Linuxdd[1]
mcganesh
 
PPTX
Writing Character driver (loadable module) in linux
RajKumar Rampelli
 
PPTX
Linux device drivers
Abhishek Sagar
 
PPTX
Device_drivers_copy_to_user_copy_from_user.pptx
siddu85
 
PPT
Linux Device Driver,LDD,
Rahul Batra
 
PPT
linux device driver
Rahul Batra
 
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
PPT
“black boxes” that make a particular p..
JcRaajab1
 
PPT
Driver_linux
Sayanton Vhaduri
 
PDF
Unit 5.2 Device Driver.pdf (Device Driver)
AnilkumarBrahmane2
 
PPTX
Introduction to Kernel and Device Drivers
RajKumar Rampelli
 
PDF
File System Modules
Anil Kumar Pugalia
 
PPTX
Device Drivers
Kushal Modi
 
Linux Char Device Driver
Gary Yeh
 
Introduction to char device driver
Vandana Salve
 
Char Drivers And Debugging Techniques
YourHelper1
 
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
Introduction Linux Device Drivers
NEEVEE Technologies
 
Linuxdd[1]
mcganesh
 
Writing Character driver (loadable module) in linux
RajKumar Rampelli
 
Linux device drivers
Abhishek Sagar
 
Device_drivers_copy_to_user_copy_from_user.pptx
siddu85
 
Linux Device Driver,LDD,
Rahul Batra
 
linux device driver
Rahul Batra
 
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
“black boxes” that make a particular p..
JcRaajab1
 
Driver_linux
Sayanton Vhaduri
 
Unit 5.2 Device Driver.pdf (Device Driver)
AnilkumarBrahmane2
 
Introduction to Kernel and Device Drivers
RajKumar Rampelli
 
File System Modules
Anil Kumar Pugalia
 
Device Drivers
Kushal Modi
 

More from Anil Kumar Pugalia (20)

PDF
Kernel Debugging & Profiling
Anil Kumar Pugalia
 
PDF
Processes
Anil Kumar Pugalia
 
PDF
System Calls
Anil Kumar Pugalia
 
PDF
Introduction to Linux
Anil Kumar Pugalia
 
PDF
Embedded Software Design
Anil Kumar Pugalia
 
PDF
Playing with R L C Circuits
Anil Kumar Pugalia
 
PDF
Mobile Hacking using Linux Drivers
Anil Kumar Pugalia
 
PDF
Shell Scripting
Anil Kumar Pugalia
 
PDF
Functional Programming with LISP
Anil Kumar Pugalia
 
PDF
Power of vi
Anil Kumar Pugalia
 
PDF
"make" system
Anil Kumar Pugalia
 
PDF
Hardware Design for Software Hackers
Anil Kumar Pugalia
 
PDF
RPM Building
Anil Kumar Pugalia
 
PDF
Linux User Space Debugging & Profiling
Anil Kumar Pugalia
 
PDF
Linux Network Management
Anil Kumar Pugalia
 
PDF
System Calls
Anil Kumar Pugalia
 
PDF
Timers
Anil Kumar Pugalia
 
PDF
Threads
Anil Kumar Pugalia
 
PDF
Synchronization
Anil Kumar Pugalia
 
PDF
Processes
Anil Kumar Pugalia
 
Kernel Debugging & Profiling
Anil Kumar Pugalia
 
Processes
Anil Kumar Pugalia
 
System Calls
Anil Kumar Pugalia
 
Introduction to Linux
Anil Kumar Pugalia
 
Embedded Software Design
Anil Kumar Pugalia
 
Playing with R L C Circuits
Anil Kumar Pugalia
 
Mobile Hacking using Linux Drivers
Anil Kumar Pugalia
 
Shell Scripting
Anil Kumar Pugalia
 
Functional Programming with LISP
Anil Kumar Pugalia
 
Power of vi
Anil Kumar Pugalia
 
"make" system
Anil Kumar Pugalia
 
Hardware Design for Software Hackers
Anil Kumar Pugalia
 
RPM Building
Anil Kumar Pugalia
 
Linux User Space Debugging & Profiling
Anil Kumar Pugalia
 
Linux Network Management
Anil Kumar Pugalia
 
System Calls
Anil Kumar Pugalia
 
Synchronization
Anil Kumar Pugalia
 
Processes
Anil Kumar Pugalia
 

Recently uploaded (20)

PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
The Future of Artificial Intelligence (AI)
Mukul
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Doc9.....................................
SofiaCollazos
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 

Character Drivers

  • 1. © 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Character Drivers
  • 2. 2© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. What to Expect? After this session, you would know W's of Character Drivers Major & Minor Numbers Registering & Unregistering Character Driver File Operations of a Character Driver Writing a Character Driver Linux Device Model udev & automatic device creation
  • 3. 3© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. W's of Character Drivers What does “Character” stand for? Look at entries starting with 'c' after ls -l /dev Device File Name User Space specific Used by Applications Device File Number Kernel Space specific Used by Kernel Internals as easy for Computation
  • 4. 4© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Major & Minor Number ls -l /dev Major is to Category; Minor is to Device Data Structures described in Kernel C in object oriented fashion Type Header: <linux/types.h> Type: dev_t – 12 bits for major & 20 bits for minor Macro Header: <linux/kdev_t.h> MAJOR(dev_t dev) MINOR(dev_t dev) MKDEV(int major, int minor)
  • 5. 5© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. 3 Entities in 3 Spaces Device Driver /dev/io Device Kernel Space User Space Hardware Space VFS Device File Application open()
  • 6. 6© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Registering & Unregistering Registering the Device Driver int register_chrdev_region(dev_t first, unsigned int count, char *name); int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int cnt, char *name); Unregistering the Device Driver void unregister_chrdev_region(dev_t first, unsigned int count); Header: <linux/fs.h> Kernel Window: /proc/devices
  • 7. 7© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The file operations struct file_operations struct module owner = THIS_MODULE; /* <linux/module.h> */ int (*open)(struct inode *, struct file *); int (*release)(struct inode *, struct file *); ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); loff_t (*llseek)(struct file *, loff_t, int); int (*unlocked_ioctl)(struct file *, unsigned int, unsigned long); Header: <linux/fs.h>
  • 8. 8© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Initialization for Registration 1st way initialization struct cdev *my_cdev = cdev_alloc(); my_cdev->owner = THIS_MODULE; my_cdev->ops = &my_fops; 2nd way initialization struct cdev my_cdev; cdev_init(&my_cdev, &my_fops); Header: <linux/cdev.h>
  • 9. 9© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Registering the file operations The Registration int cdev_add(struct cdev *cdev, dev_t num, unsigned int count); The Unregistration void cdev_del(struct cdev *cdev); Header: <linux/cdev.h>
  • 10. 10© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The file & inode structures Important fields of struct file mode_t f_mode loff_t f_pos unsigned int f_flags struct file_operations *f_op void *private_data Important fields of struct inode unsigned int iminor(struct inode *); unsigned int imajor(struct inode *);
  • 11. 11© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Register/Unregister: Old Way Registering the Device Driver int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); Unregistering the Device Driver int unregister_chrdev(unsigned int major, const char *name);
  • 12. 12© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The /dev/null read & write ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... return read_cnt; } ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... return wrote_cnt; }
  • 13. 13© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The read flow struct file ------------------------- f_count f_flags f_mode ------------------------- f_pos ------------------------- ... ... ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) Buffer (in the driver) Buffer (in the application or libc) Kernel Space (Non-swappable) User Space (Swappable) copy_to_user
  • 14. 14© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The write flow struct file ------------------------- f_count f_flags f_mode ------------------------- f_pos ------------------------- ... ... ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off) Buffer (in the driver) Buffer (in the application or libc) Kernel Space (Non-swappable) User Space (Swappable) copy_from_user
  • 15. 15© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The mem device read #include <asm/uaccess.h> ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) { ... if (copy_to_user(buf, from, cnt) != 0) { return -EFAULT; } ... return read_cnt; }
  • 16. 16© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The mem device write #include <asm/uaccess.h> ssize_t my_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off) { ... if (copy_from_user(to, buf, cnt) != 0) { return -EFAULT; } ... return wrote_cnt; }
  • 17. 17© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. The I/O Control API API int (*unlocked_ioctl)(struct file *, unsigned int cmd, unsigned long arg) Command Macros _IO, _IOW, _IOR, _IOWR Parameters type (character) [15:8] number (index) [7:0] size (param type) [29:16] Header: <linux/ioctl.h> →...→ <asm-generic/ioctl.h> size [29:16] num[7:0]type[15:8] dir[31:30]
  • 18. 18© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Linux Device Model (LDM) struct kobject - <linux/kobject.h> kref object Pointer to kset, the parent object kobj_type, type describing the kobject kobject instantiation → sysfs representation Parent object guides the entries under /sys/ bus – the physical buses class – the device categories device – the actual devices
  • 19. 19© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. udev & LDM Daemon: udevd Configuration: /etc/udev/udev.conf Rules: /etc/udev/rules.d/ Utility: udevinfo [-a] [-p <device_path>] Receives uevent on a change in /sys Accordingly, updates /dev &/or Performs the appropriate action for Hotplug Microcode / Firmware Download Module Autoload
  • 20. 20© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Device Model & Classes Latest way to create dynamic devices Create or Get the appropriate device category Create the desired device under that category Class Operations struct class *class_create(struct module *owner, char *name); void class_destroy(struct class *cl); Device into & out of Class struct class_device *device_create(struct class *cl, NULL, dev_t devnum, NULL, const char *fmt, ...); void device_destroy(struct class *cl, dev_t devnum);
  • 21. 21© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. What all have we learnt? W's of Character Drivers Major & Minor Numbers Registering & Unregistering Character Driver File Operations of a Character Driver Writing a Character Driver Linux Device Model udev & automatic device creation
  • 22. 22© 2010-14 SysPlay Workshops <[email protected]> All Rights Reserved. Any Queries?