Guest User

Untitled

a guest
May 24th, 2016
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.94 KB | None | 0 0
  1. /*
  2.  *  mousek.c, sample code for USB drivers, heavily based on usbmouse.c
  3.  * 
  4.  *  Copyright (c) 2016 Kush Kumar Sharma
  5.  *  Copyright (c) 2000 Alessandro Rubini <rubini@gnu.org>
  6.  *  Copyright (c) 1999 Vojtech Pavlik <vojtech@suse.cz>
  7.  *
  8.  */
  9.  
  10. /*
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24.  */
  25.  
  26. #ifndef __KERNEL__
  27. #  define __KERNEL__
  28. #endif
  29. #ifndef MODULE
  30. #  define MODULE
  31. #endif
  32.  
  33. #include <linux/kernel.h>
  34. #include <linux/slab.h>
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/usb/input.h>
  38. #include <linux/hid.h>
  39.  
  40. /* usb stuff */
  41. #include <linux/input.h>
  42. #include <linux/usb.h>
  43.  
  44. /* miscdevice stuff */
  45. #include <linux/poll.h>
  46. #include <linux/miscdevice.h>
  47. #include <asm/uaccess.h>
  48.  
  49. /*
  50.  * Version Information
  51.  */
  52. #define DRIVER_VERSION "v0.1"
  53. #define DRIVER_AUTHOR "Kush Kumar Sharma"
  54. #define DRIVER_DESC "Mouse driver"
  55. #define DRIVER_LICENSE "GPL"
  56.  
  57. MODULE_AUTHOR(DRIVER_AUTHOR);
  58. MODULE_DESCRIPTION(DRIVER_DESC);
  59. MODULE_LICENSE(DRIVER_LICENSE);
  60.  
  61. #define DEVICE_NAME "mousek"
  62. int Major;
  63.  
  64. struct mousek_device {
  65.     signed char data[3];     /* use a three-byte protocol */
  66.     struct urb urb;          /* USB Request block, to get USB data*/
  67.     struct input_dev *idev;   /* input device, to push out input  data */
  68.     int x, y;                /* keep track of the position of this device */
  69. };
  70.  
  71. static struct mousek_device *mouse;
  72.  
  73. int mousek_open(struct inode *inode, struct file *filp)
  74. {
  75.     /* announce yourself */
  76.     printk(KERN_INFO "mousek: faking an USB mouse via the misc device\n");
  77.    
  78.     return 0; /* Ok */
  79. }    
  80.  
  81. int mousek_release(struct inode *inode, struct file *filp)
  82. {
  83.  
  84.     printk(KERN_INFO "mousek: closing misc device\n");
  85.        
  86.     return 0;
  87. }
  88.  
  89. ssize_t mousek_read(struct file *filp,   /* see include/linux/fs.h   */
  90.                            char *buffer,        /* buffer to fill with data */
  91.                            size_t length,       /* length of the buffer     */
  92.                            loff_t * offset)
  93. {
  94.     printk(KERN_INFO "mousek: Restricted. That is okay but do not repeat this mistake.\n");
  95.     return 0;
  96. }
  97.  
  98.  
  99. ssize_t mousek_write(struct file *filp, const char *buf, size_t count,
  100.             loff_t *offp)
  101. {
  102.     static char localbuf[16];
  103.     int i;
  104.  
  105.     /* accept 16 bytes at a time, at most */
  106.     if (count >16) count=16;
  107.     copy_from_user(localbuf, buf, count);
  108.  
  109.  
  110.     struct input_dev *dev = mouse->idev;
  111.  
  112.     /* scan written data */
  113.     for (i=0; i<count; i++) {
  114.     mouse->data[1] = mouse->data[2] = 0;
  115.     switch (localbuf[i]) {
  116.         case 'u': case 'U': /* up */
  117.         mouse->data[2] = -10;
  118.         break;
  119.         case 'd': case 'D': /* down */
  120.         mouse->data[2] = 10;
  121.         break;
  122.         case 'l': case 'L': /* left */
  123.         mouse->data[1] = -10;
  124.         break;
  125.         case 'r': case 'R': /* right */
  126.         mouse->data[1] = 10;
  127.         break;
  128.         default:
  129.         continue;
  130.     }
  131.     input_report_key(dev, BTN_RIGHT, 1);
  132.     input_report_key(dev, BTN_RIGHT, 0);
  133.  
  134.     input_report_key(dev, KEY_UP, 1); /* keypress */
  135.     input_report_key(dev, KEY_UP, 0); /* release */
  136.    
  137.     input_report_rel(dev, REL_X, 30);
  138.     input_report_rel(dev, REL_Y, 30);
  139.  
  140.     printk(KERN_ALERT "mousek: Control actions of %s received %d %d",localbuf, mouse->data[1], mouse->data[2]);
  141.    
  142.  
  143.     }
  144.    
  145.  
  146.     //input_report_rel(mouse->idev, REL_WHEEL, data[3]);
  147.    
  148.     input_sync(dev);
  149.    
  150.     return count;
  151. }
  152.  
  153.  
  154. struct file_operations mousek_fops = {
  155.     write:    mousek_write,
  156.     read:     mousek_read,
  157.     open:     mousek_open,
  158.     release:  mousek_release,
  159. };
  160.  
  161.  
  162. int init_module(void)
  163. {
  164.     int retval;
  165.    
  166.     Major = register_chrdev(0, DEVICE_NAME, &mousek_fops);
  167.     if (Major < 0) {
  168.       printk(KERN_ALERT "Registering char device failed with %d\n", Major);
  169.       return Major;
  170.     }
  171.    
  172.     struct input_dev *input_dev;
  173.  
  174.     /* allocate and zero a new data structure for the new device */
  175.     mouse = kmalloc(sizeof(struct mousek_device), GFP_KERNEL);
  176.     if (!mouse) return -ENOMEM; /* failure */
  177.     memset(mouse, 0, sizeof(*mouse));
  178.  
  179.     input_dev = input_allocate_device();
  180.     if (!input_dev) {
  181.         printk(KERN_ERR "mousek.c: Not enough memory\n");
  182.         retval = -ENOMEM;
  183.         //goto err_free_irq;
  184.     }
  185.     //updating struct
  186.     mouse->idev = input_dev;
  187.        
  188.     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  189.     set_bit(103, input_dev->keybit); /* Up    */
  190.  
  191.     input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
  192.     input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  193.  
  194.     retval = input_register_device(input_dev);
  195.     if (retval) {
  196.         printk(KERN_ERR "mousek: Failed to register device\n");
  197.         goto err_free_dev;
  198.     }
  199.  
  200.    
  201.     printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major);
  202.     printk(KERN_INFO "the driver, create a dev file with\n");  
  203.     printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
  204.     printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n");
  205.     printk(KERN_INFO "the device file.\n");
  206.     printk(KERN_INFO "Remove the device file and module when done.\n");
  207.    
  208.    
  209. return 0;
  210.  
  211. err_free_dev:
  212.     input_free_device(mouse->idev);
  213.     kfree(mouse);
  214.  
  215. return retval;
  216. }
  217.  
  218. void cleanup_module(void)
  219. {
  220.  
  221.     if(!mouse) return;
  222.    
  223.     input_unregister_device(mouse->idev);
  224.     kfree(mouse);  
  225.     unregister_chrdev(Major, DEVICE_NAME);
  226.    
  227.     printk(KERN_ALERT "Uninstalled. Delete device from dev.");
  228.    
  229.    
  230.  
  231. }
Add Comment
Please, Sign In to add comment