Advertisement
Guest User

speakup-test.c

a guest
Dec 18th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <linux/module.h>    // included for all kernel modules
  2. #include <linux/kernel.h>    // included for KERN_INFO
  3. #include <linux/init.h>      // included for __init and __exit macros
  4. #include <linux/tty.h>
  5.  
  6.  
  7. MODULE_LICENSE("GPL");
  8. MODULE_AUTHOR("Okash");
  9. MODULE_DESCRIPTION("Speakup Test");
  10.  
  11. static int speakup_test_open(struct tty_struct *tty)
  12. {
  13.     int rv;
  14.  
  15.     pr_warn("speakup_test_open(): tty->name: %s; tty->ldisc->ops->name: %s\n",
  16.         tty->name, tty->ldisc->ops->name);
  17.     rv = tty->ops->write(tty, "hi ldisc\n", 9);
  18.     pr_warn("speakup_test_open(): done writing. rv=%d\n", rv);
  19.  
  20.     return 0;
  21. }
  22.  
  23. static void speakup_test_close(struct tty_struct *tty)
  24. {
  25.     pr_warn("speakup_test_close()\n");
  26. }
  27.  
  28. static struct tty_ldisc_ops hello_ldisc_ops = {
  29.     .owner          = THIS_MODULE,
  30.     .magic          = TTY_LDISC_MAGIC,
  31.     .name           = "speakup_test_ldisc",
  32.     .open           = speakup_test_open,
  33.     .close          = speakup_test_close,
  34. };
  35.  
  36. static struct tty_struct *tty;
  37.  
  38. static int __init speakup_test_init(void)
  39. {
  40.     int ret = 0;
  41.  
  42.     ret = tty_register_ldisc(N_SPEAKUP, &hello_ldisc_ops);
  43.     if (ret) {
  44.     pr_err("speakup_test_init(): Error registering line discipline.\n");
  45.     return ret;
  46.     }
  47.  
  48.    
  49.     tty = tty_open_by_driver(MKDEV(4, 64), NULL, NULL);
  50.     if (IS_ERR(tty))
  51.         return PTR_ERR(tty);
  52.    
  53.     printk("got tty %p\n", tty);
  54.  
  55.     if (tty->ops->open)
  56.         ret = tty->ops->open(tty, NULL);
  57.     else
  58.         ret = -ENODEV;
  59.  
  60. /* to be put in ldisc open */
  61.     if (tty->ops->write == NULL)
  62.         ret = -EOPNOTSUPP;
  63. /* */
  64.  
  65.     if (ret) {
  66.         tty_unlock(tty);
  67.         return ret;
  68.     }
  69.        
  70.     clear_bit(TTY_HUPPED, &tty->flags);
  71.     tty_unlock(tty);
  72.  
  73.     tty_set_ldisc(tty, N_SPEAKUP);
  74.  
  75.     tty->ops->write(tty, "test\n", 5);
  76.  
  77.     return  ret;
  78. }
  79.  
  80. static void __exit speakup_test_cleanup(void)
  81. {
  82.     /* Very much like tty_release, perhaps the tty_release code should be moved to a function which only takes (tty,filp) and we'd call it with (tty,NULL) */
  83.  
  84.     int idx;
  85.     tty_lock(tty);
  86.     idx = tty->index;
  87.  
  88. #if 0
  89.     if (tty_release_checks(tty, idx)) {
  90.             tty_unlock(tty);
  91.             return 0;
  92.     }
  93. #endif
  94.  
  95.     if (tty->ops->close)
  96.             tty->ops->close(tty, NULL);
  97.     tty_unlock(tty);
  98. #if 0
  99.     tty_flush_works(tty);
  100. #endif
  101. #if 0
  102.     mutex_lock(&tty_mutex);
  103.     release_tty(tty, idx);
  104.     mutex_unlock(&tty_mutex);
  105. #endif
  106.  
  107.     printk("exit\n");
  108. }
  109.  
  110. module_init(speakup_test_init);
  111. module_exit(speakup_test_cleanup);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement