tty 驱动学习

9710阅读 0评论2020-02-22 fangdikui
分类:LINUX

Makefile

点击(此处)折叠或打开

  1. KBUILD_EXTRA_SYMBOLS=/home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-3.2.0-psp04.06.00.11/am335x_evm/Module.symvers
  2. ifneq ($(KERNELRELEASE),)
  3.     obj-m := my_tty.o
  4. else
  5.     KERNELDIR ?= /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-3.2.0-psp04.06.00.11/am335x_evm
  6.     PWD := $(shell pwd)
  7. default:
  8.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
  9.     cp *.ko /home/fang/ti-sdk-am335x-evm-06.00.00.00/filesystem/rootfs/test_modules
  10. clean:
  11.     @rm -rf *.o *.mod.c *.mod.o *.ko *.order *.symvers .*.cmd .tmp_versions
  12. endif


my_tty.c

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/sched.h>
  4. #include <linux/tty.h>
  5. #include <linux/tty_flip.h>
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <linux/errno.h>
  9. #include <linux/kd.h>
  10. #include <linux/slab.h>
  11. #include <linux/major.h>
  12. #include <linux/mm.h>
  13. #include <linux/console.h>
  14. #include <linux/init.h>
  15. #include <linux/mutex.h>
  16. #include <linux/vt_kern.h>
  17. #include <linux/selection.h>
  18. #include <linux/tiocl.h>
  19. #include <linux/kbd_kern.h>
  20. #include <linux/consolemap.h>
  21. #include <linux/timer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/pm.h>
  25. #include <linux/font.h>
  26. #include <linux/bitops.h>
  27. #include <linux/notifier.h>
  28. #include <linux/device.h>
  29. #include <linux/io.h>
  30. #include <asm/system.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/kdb.h>
  33. #include <linux/ctype.h>

  34. static int con_open(struct tty_struct *, struct file *);
  35. static void vc_init(struct vc_data *vc, unsigned int rows,
  36.          unsigned int cols, int do_clear);
  37. static void gotoxy(struct vc_data *vc, int new_x, int new_y);
  38. static void save_cur(struct vc_data *vc);
  39. static void reset_terminal(struct vc_data *vc, int do_clear);
  40. static void con_flush_chars(struct tty_struct *tty);
  41. static int set_vesa_blanking(char __user *p);
  42. static void set_cursor(struct vc_data *vc);
  43. static void hide_cursor(struct vc_data *vc);
  44. static void console_callback(struct work_struct *ignored);
  45. static void blank_screen_t(unsigned long dummy);
  46. static void set_palette(struct vc_data *vc);


  47. struct tty_driver *console_driver;

  48. /*
  49.  * /dev/ttyN handling
  50.  */

  51. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  52. {
  53.     int    retval;
  54.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  55.     return count;
  56. }

  57. static int con_put_char(struct tty_struct *tty, unsigned char ch)
  58. {
  59.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  60.     return 0;
  61. }

  62. static int con_chars_in_buffer(struct tty_struct *tty)
  63. {
  64.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  65.     return 0;        /* we're not buffering */
  66. }

  67. /*
  68.  * con_throttle and con_unthrottle are only used for
  69.  * paste_selection(), which has to stuff in a large number of
  70.  * characters...
  71.  */
  72. static void con_throttle(struct tty_struct *tty)
  73. {
  74.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  75. }

  76. static void con_unthrottle(struct tty_struct *tty)
  77. {
  78.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  79. }

  80. /*
  81.  * Allocate the console screen memory.
  82.  */
  83. static int con_open(struct tty_struct *tty, struct file *filp)
  84. {
  85.     unsigned int currcons = tty->index;
  86.     int ret = 0;
  87.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  88.     console_lock();
  89.     console_unlock();
  90.     return ret;
  91. }

  92. static void con_close(struct tty_struct *tty, struct file *filp)
  93. {
  94.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  95.     /* Nothing to do - we defer to shutdown */
  96. }

  97. static void con_shutdown(struct tty_struct *tty)
  98. {
  99.     struct vc_data *vc = tty->driver_data;
  100.     //BUG_ON(vc == NULL);
  101.     console_lock();
  102.     //vc->port.tty = NULL;
  103.     console_unlock();
  104.     tty_shutdown(tty);
  105. }
  106. static int con_write_room(struct tty_struct *tty)
  107. {
  108.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  109.     if (tty->stopped)
  110.         return 0;
  111.     return 32768;        /* No limit, really; we're not buffering */
  112. }
  113. static void con_stop(struct tty_struct *tty)
  114. {
  115.     int console_num;
  116.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  117.     
  118. }
  119. /*
  120.  * Turn the Scroll-Lock LED off when the console is started
  121.  */
  122. static void con_start(struct tty_struct *tty)
  123. {
  124.     int console_num;
  125.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  126. }

  127. static int vt_resize(struct tty_struct *tty, struct winsize *ws)
  128. {
  129.     struct vc_data *vc = tty->driver_data;
  130.     int ret;

  131.     console_lock();
  132.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  133.     console_unlock();
  134.     return 0;
  135. }
  136. static void con_flush_chars(struct tty_struct *tty)
  137. {
  138.     printk(" %s %s line:%d\n", __func__,__FILE__,__LINE__);
  139. }




  140. /*
  141.  * We handle the console-specific ioctl's here. We allow the
  142.  * capability to modify any console, not just the fg_console.
  143.  */
  144. int vt_ioctl(struct tty_struct *tty,
  145.      unsigned int cmd, unsigned long arg)
  146. {
  147.     return 0;
  148. }

  149. static const struct tty_operations con_ops = {
  150.     .open = con_open,
  151.     .close = con_close,
  152.     .write = con_write,
  153.     .write_room = con_write_room,
  154.     .put_char = con_put_char,
  155.     .flush_chars = con_flush_chars,
  156.     .chars_in_buffer = con_chars_in_buffer,
  157.     .ioctl = vt_ioctl,
  158. #ifdef CONFIG_COMPAT
  159.     .compat_ioctl = vt_compat_ioctl,
  160. #endif
  161.     .stop = con_stop,
  162.     .start = con_start,
  163.     .throttle = con_throttle,
  164.     .unthrottle = con_unthrottle,
  165.     .resize = vt_resize,
  166.     .shutdown = con_shutdown
  167. };

  168. static struct cdev vc0_cdev;
  169. int default_utf8 = true;
  170. int __init mytty_init(const struct file_operations *console_fops)
  171. {
  172.     console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  173.     if (!console_driver)
  174.         panic("Couldn't allocate console driver\n");
  175.     console_driver->owner = THIS_MODULE;
  176.     console_driver->name = "tty_my";
  177.     console_driver->name_base = 1;
  178.     console_driver->major = TTY_MAJOR;
  179.     console_driver->minor_start = 128;
  180.     console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  181.     console_driver->init_termios = tty_std_termios;
  182.     if (default_utf8)
  183.         console_driver->init_termios.c_iflag |= IUTF8;
  184.     console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  185.     tty_set_operations(console_driver, &con_ops);
  186.     printk("console_driver ->num:%08x %s %s line:%d\n", console_driver->num,__func__,__FILE__,__LINE__);
  187.     if (tty_register_driver(console_driver))
  188.         panic("Couldn't register console driver\n");

  189.     return 0;
  190. }
  191. static void mytty_exit(void)
  192. {
  193.     int i = 0;
  194.     tty_unregister_driver(console_driver);
  195.     for (i=0; i <console_driver->num; i++)
  196.         tty_unregister_device(console_driver, i);
  197. }

  198. module_init(mytty_init);
  199. module_exit(mytty_exit);

  200. MODULE_LICENSE("GPL");
测试:

点击(此处)折叠或打开

  1. insmod my_tty.ko
  2. echo a>/dev/tty_my1
  3. [ 165.343902] con_open /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-modules/3.2.0/ttydriver/my_tty.c line:99
  4. [ 165.355804] con_write_room /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-modules/3.2.0/ttydriver/my_tty.c line:122
  5. [ 165.368194] con_write /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-modules/3.2.0/ttydriver/my_tty.c line:61
  6. [ 165.380035] con_write_room /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-modules/3.2.0/ttydriver/my_tty.c line:122
  7. [ 165.392395] con_write /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-modules/3.2.0/ttydriver/my_tty.c line:61
  8. [ 165.404235] con_flush_chars /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-modules/3.2.0/ttydriver/my_tty.c line:154
  9. [ 165.416748] con_close /home/fang/ti-sdk-am335x-evm-06.00.00.00/board-support/linux-modules/3.2.0/ttydriver/my_tty.c line:107


上一篇:linux 3.2.0 vmem_disk驱动源码
下一篇:arm linux kgdb