Thursday, April 8, 2010

LDD3 notes: Device registration and operations

My notes while reading Linux Device Drivers 3rd edition.


register_chrdrv replaced with cdev_add(), cdev_del() and struct cdev


register_chrdev_region, alloc_chrdev_region, unregister_chrdev_region to

  • statically, dynamically pick a contiguous block of dev_t's i.e. major and minor numbers.
  • assign them a device name ( /dev/devices and sysfs )
  • no need to know major/minor numbers at open
  • look at inode->cdev,
  • deduce filp->private_data using using container_of

Notes on proper behaviour with open/close

  • if the device cannot seek: nonseekable_open() , no_llseek
  • struct file represents an open file descriptor in kernel, can be shared by multiple processes
  • one open, fork&dup, single struct file, multiple close, one release

Notes on proper behaviour with read/write, Select(BSD)/Poll(SystemV)/Epoll(Linux)

  • O_NONBLOCK ( == O_NDELAY) and no progress possible -> immediate -EAGAIN
  • _interruptible fails -> -ERESTARTSYS, VFS will retry or return -EINTR
  • poll reports device writable -> next write must not block
  • encountering error in the middle of successfull transfer: return partial result, next attempt will return failure
  • security implications of blindly referencing an user pointer

IOCTL

  • asm/ioctl.h + Documentation/ioctl-number.txt
  • "clueless" legacy: 8bit magic + 8bit device specific
  • capable() - permissions
  • access_ok() - plausible user memory address

Asyncronous notifications from user space perspective, on sockets and tty's

  • whom to notify: F_SETOWN
  • please notify: F_SETFL(FASYNC)
  • receive SIGIO -> select()



No comments:

Post a Comment