Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
© 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...
3© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Character Drivers
What does “Character” sta...
4© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Major & Minor Number
ls -l /dev
Major is to Catego...
5© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
3 Entities in 3 Spaces
Device
Driver
/dev/io
Devic...
6© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Registering & Unregistering
Registering the Device...
7© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The file operations
struct file_operations
struct ...
8© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Initialization for Registration
1st
way initializa...
9© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Registering the file operations
The Registration
i...
10© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The file & inode structures
Important fields of s...
11© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Register/Unregister: Old Way
Registering the Devi...
12© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The /dev/null read & write
ssize_t my_read(struct...
13© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The read flow
struct file
-----------------------...
14© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The write flow
struct file
----------------------...
15© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The mem device read
#include <asm/uaccess.h>
ssiz...
16© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The mem device write
#include <asm/uaccess.h>
ssi...
17© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The I/O Control API
API
int (*unlocked_ioctl)(str...
18© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Device Model (LDM)
struct kobject - <linux/...
19© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
udev & LDM
Daemon: udevd
Configuration: /etc/udev...
20© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Device Model & Classes
Latest way to create dynam...
21© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of Character Drivers...
22© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?
Upcoming SlideShare
Loading in …5
×

of

Character Drivers Slide 1 Character Drivers Slide 2 Character Drivers Slide 3 Character Drivers Slide 4 Character Drivers Slide 5 Character Drivers Slide 6 Character Drivers Slide 7 Character Drivers Slide 8 Character Drivers Slide 9 Character Drivers Slide 10 Character Drivers Slide 11 Character Drivers Slide 12 Character Drivers Slide 13 Character Drivers Slide 14 Character Drivers Slide 15 Character Drivers Slide 16 Character Drivers Slide 17 Character Drivers Slide 18 Character Drivers Slide 19 Character Drivers Slide 20 Character Drivers Slide 21 Character Drivers Slide 22
Upcoming SlideShare
Network Drivers
Next

18 Likes

Share

Character Drivers

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Character Drivers

  1. 1. © 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Character Drivers
  2. 2. 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. 3. 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. 4. 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. 5. 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. 6. 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. 7. 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. 8. 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. 9. 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. 10. 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. 11. 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. 12. 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. 13. 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. 14. 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. 15. 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. 16. 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. 17. 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. 18. 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. 19. 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. 20. 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. 21. 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. 22. 22© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?
  • kothapallyajaykumar

    May. 16, 2020
  • angaur

    Apr. 8, 2017
  • angaur

    Apr. 8, 2017
  • DangHuy5

    Jul. 4, 2016
  • PratikPrajapati20

    Apr. 16, 2016
  • ssuser020114

    Oct. 12, 2015
  • 9490066970

    Jul. 23, 2015
  • PraveenMahendra1

    Jul. 22, 2015
  • tthtlc

    Dec. 23, 2014
  • rampalliraj

    Dec. 22, 2014
  • PalvaiSrikanth

    Nov. 11, 2014
  • NELLAIVIJAY1

    Nov. 4, 2014
  • pvkarthik

    Mar. 29, 2014
  • beswarreddy

    Nov. 11, 2013
  • t3q

    Oct. 29, 2013
  • nhm919

    Aug. 1, 2013
  • Dddhhierazz

    Jul. 4, 2013
  • cheese74jo

    Jan. 4, 2012

Views

Total views

52,623

On Slideshare

0

From embeds

0

Number of embeds

42,444

Actions

Downloads

0

Shares

0

Comments

0

Likes

18

×