| 1599 | |
| 1600 | = 11/20 = |
| 1601 | == fs/char_dev.c == |
| 1602 | * struct cdev *cdev_alloc(void) |
| 1603 | {{{ |
| 1604 | * cdev_alloc() - allocate a cdev structure |
| 1605 | * |
| 1606 | * Allocates and returns a cdev structure, or NULL on failure. |
| 1607 | }}} |
| 1608 | * void cdev_init(struct cdev *cdev, const struct file_operations *fops) |
| 1609 | {{{ |
| 1610 | * cdev_init() - initialize a cdev structure |
| 1611 | * @cdev: the structure to initialize |
| 1612 | * @fops: the file_operations for this device |
| 1613 | * |
| 1614 | * Initializes @cdev, remembering @fops, making it ready to add to the |
| 1615 | * system with cdev_add(). |
| 1616 | }}} |
| 1617 | * int cdev_add(struct cdev *p, dev_t dev, unsigned count) |
| 1618 | {{{ |
| 1619 | * cdev_add() - add a char device to the system |
| 1620 | * @p: the cdev structure for the device |
| 1621 | * @dev: the first device number for which this device is responsible |
| 1622 | * @count: the number of consecutive minor numbers corresponding to this |
| 1623 | * device |
| 1624 | * |
| 1625 | * cdev_add() adds the device represented by @p to the system, making it |
| 1626 | * live immediately. A negative error code is returned on failure. |
| 1627 | }}} |
| 1628 | * void cdev_del(struct cdev *p) |
| 1629 | {{{ |
| 1630 | * cdev_del() - remove a cdev from the system |
| 1631 | * @p: the cdev structure to be removed |
| 1632 | * |
| 1633 | * cdev_del() removes @p from the system, possibly freeing the structure |
| 1634 | * itself. |
| 1635 | }}} |
| 1636 | |
| 1637 | == stackmod with open/close/read/write skeleton == |
| 1638 | * 参考 |
| 1639 | * drivers/char/raw.c -> register_chrdev_region, cdev_init |
| 1640 | * include/linux/cdev.h -> struct cdev |
| 1641 | * include/linux/fs.h -> struct file_operations |
| 1642 | |
| 1643 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko |
| 1644 | {{{ |
| 1645 | Nov 20 17:47:12 ubuntu-lucid64 kernel: [ 4074.120567] stackmod is loaded |
| 1646 | Nov 20 17:47:12 ubuntu-lucid64 kernel: [ 4074.120571] stackmod: 128 entry, major is 251, minor is 0 |
| 1647 | Nov 20 17:47:12 ubuntu-lucid64 kernel: [ 4074.120576] stackmod is added successfully |
| 1648 | }}} |
| 1649 | |
| 1650 | * s0711489@ubuntu-lucid64:~$ grep stack /proc/devices |
| 1651 | {{{ |
| 1652 | 251 stackmod |
| 1653 | }}} |
| 1654 | |
| 1655 | * s0711489@ubuntu-lucid64:~$ sudo mknod /dev/stack c 251 0 |
| 1656 | * s0711489@ubuntu-lucid64:~$ ls -l /dev/stack |
| 1657 | {{{ |
| 1658 | crw-r--r-- 1 root root 251, 0 2011-11-20 17:50 /dev/stack |
| 1659 | }}} |
| 1660 | |
| 1661 | * s0711489@ubuntu-lucid64:~$ cat /dev/stack |
| 1662 | {{{ |
| 1663 | Nov 20 17:50:56 ubuntu-lucid64 kernel: [ 4297.683404] stackmod is opened |
| 1664 | Nov 20 17:50:56 ubuntu-lucid64 kernel: [ 4297.683424] stackmod is read |
| 1665 | Nov 20 17:50:56 ubuntu-lucid64 kernel: [ 4297.683428] stackmod is released |
| 1666 | }}} |
| 1667 | |
| 1668 | * s0711489@ubuntu-lucid64:~$ sudo sh -c "echo 1 > /dev/stack" |
| 1669 | {{{ |
| 1670 | ^C |
| 1671 | }}} |
| 1672 | {{{ |
| 1673 | (snip) |
| 1674 | |
| 1675 | Nov 20 17:51:43 ubuntu-lucid64 kernel: [ 4343.961818] stackmod is written |
| 1676 | Nov 20 17:51:43 ubuntu-lucid64 kernel: [ 4343.961818] stackmod is written |
| 1677 | Nov 20 17:51:43 ubuntu-lucid64 kernel: [ 4343.965238] stackmod is released |
| 1678 | }}} |
| 1679 | |
| 1680 | * s0711489@ubuntu-lucid64:~$ sudo dd if=/dev/null of=/dev/stack |
| 1681 | {{{ |
| 1682 | 0+0 records in |
| 1683 | 0+0 records out |
| 1684 | 0 bytes (0 B) copied, 2.3001e-05 s, 0.0 kB/s |
| 1685 | }}} |
| 1686 | {{{ |
| 1687 | Nov 20 17:52:58 ubuntu-lucid64 kernel: [ 4419.365245] stackmod is opened |
| 1688 | Nov 20 17:52:58 ubuntu-lucid64 kernel: [ 4419.365281] stackmod is released |
| 1689 | }}} |