florianscholz

airsensor.c

May 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.57 KB | None | 0 0
  1. /************************************************************************/
  2. /* Kerneltreiber fuer den Luftqualitaetsstick                           */
  3. /* Im Rahmen der Veranstaltung Embedded Systems bei Juergen Quade       */
  4. /* Copyright (c) 2017 Florian Scholz                                    */
  5. /*                                                                      */
  6. /* This program is free software; you can redistribute it and/or modify */
  7. /* it under the terms of the GNU General Public License as published by */
  8. /* the Free Software Foundation; either version 2 of the License, or    */
  9. /* (at your option) any later version.                                  */
  10. /*                                                                      */
  11. /* This program is distributed in the hope that it will be useful,      */
  12. /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
  13. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         */
  14. /* GNU General Public License for more details.                         */
  15. /*                                                                      */
  16. /************************************************************************/
  17. #include <linux/module.h>
  18. #include <linux/fs.h>
  19. #include <linux/usb.h>
  20. #include <linux/slab.h>
  21. #include <asm/uaccess.h>
  22.  
  23. #define USB_VENDOR_ID 0x03eb
  24. #define USB_DEVICE_ID 0x2013
  25.  
  26. struct usb_device *dev;
  27. static DEFINE_MUTEX( ulock );
  28.  
  29. static short airsensorfs_read_sensor_value( void );
  30.  
  31. static ssize_t airsensorfs_read( struct file *instanz, char *buffer,
  32.     size_t count, loff_t *ofs )
  33. {
  34.     char pbuf[20];
  35.  
  36.     short value;
  37.  
  38.     value = airsensorfs_read_sensor_value();
  39.     int cnt = snprintf(pbuf, count, "%d\n", value);
  40.  
  41.    
  42.     count -= copy_to_user(buffer,pbuf,cnt);
  43.     *ofs += count;
  44.     return count;
  45. }
  46.  
  47.  
  48. static short airsensorfs_read_sensor_value() {
  49.     char buf[1000];
  50.     int rcvd_bytes, retval;
  51.     short value;
  52.     memcpy(buf, "\x40\x68\x2a\x54\x52\x0a\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40", 0x0000010);
  53.     mutex_lock(&ulock);
  54.     retval = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x2), buf, 0x10, &rcvd_bytes, 1000);
  55.     if(retval != 0)
  56.     {
  57.         printk("airsensorfs: couldn't send write request inital\n");
  58.     }
  59.  
  60.     retval = usb_interrupt_msg(dev, usb_rcvintpipe(dev, 0x81), buf, 0x10, &rcvd_bytes, 1000);
  61.     if(retval != 0)
  62.     {
  63.         printk("airsensorfs: failed to read data\n");
  64.     }
  65.  
  66.  
  67.     retval = usb_interrupt_msg(dev, usb_rcvintpipe(dev, 0x81), buf, 0x10, &rcvd_bytes, 1000);
  68.     if(retval != 0)
  69.     {
  70.         printk("airsensorfs: failed to read remaining data\n");
  71.     }
  72.     mutex_unlock(&ulock);
  73.     memcpy(&value, buf+2,2);
  74.  
  75.     return value;
  76. }
  77.  
  78. static int airsensorfs_open( struct inode *devicefile, struct file *instanz )
  79. {
  80.     return 0;
  81. }
  82.  
  83. static struct file_operations usb_fops = {
  84.     .owner = THIS_MODULE,
  85.     .open  = airsensorfs_open,
  86.     .read  = airsensorfs_read,
  87. };
  88.  
  89. static struct usb_device_id usbid [] = {
  90.     { USB_DEVICE(USB_VENDOR_ID, USB_DEVICE_ID), },
  91.     { }                 /* Terminating entry */
  92. };
  93.  
  94. struct usb_class_driver class_descr = {
  95.     .name = "airsensorfs",
  96.     .fops = &usb_fops,
  97.     .minor_base = 16,
  98. };
  99.  
  100. static int airsensorfs_probe(struct usb_interface *interface,
  101.     const struct usb_device_id *id)
  102. {
  103.     dev = interface_to_usbdev(interface);
  104.     printk("airsensorfs driver by florian scholz: 0x%4.4x|0x%4.4x, if=%p\n", dev->descriptor.idVendor,
  105.         dev->descriptor.idProduct, interface );
  106.     if (dev->descriptor.idVendor==USB_VENDOR_ID
  107.         && dev->descriptor.idProduct==USB_DEVICE_ID) {
  108.         if (usb_register_dev( interface, &class_descr )) {
  109.             return -EIO;
  110.         }
  111.         printk("got minor= %d\n", interface->minor );
  112.  
  113.  
  114.  
  115.         char buf[1000];
  116.         int rcvd_bytes;
  117.         int retval = usb_interrupt_msg(dev, usb_rcvintpipe(dev, 0x81), buf, 0x10, &rcvd_bytes, 1000);
  118.         if(retval != 0)
  119.         {
  120.             printk("airsensorfs: failed to read remaining data\n");
  121.  
  122.             return 0;
  123.         }
  124.  
  125.     }
  126.    
  127.  
  128.     return -ENODEV;
  129. }
  130.  
  131. static void airsensorfs_disconnect( struct usb_interface *iface )
  132. {
  133.     mutex_lock( &ulock );
  134.     usb_deregister_dev( iface, &class_descr );
  135.     mutex_unlock( &ulock );
  136. }
  137.  
  138. static struct usb_driver airsensorfs = {
  139.     .name= "airsensorfs",
  140.     .id_table= usbid,
  141.     .probe= airsensorfs_probe,
  142.     .disconnect= airsensorfs_disconnect,
  143. };
  144.  
  145. static int __init airsensorfs_init(void)
  146. {
  147.     if (usb_register(&airsensorfs) ) {
  148.         printk("airsensorfs: unable to register usb driver\n");
  149.         return -EIO;
  150.     }
  151.     else
  152.     {
  153.         printk("airsensorfs: registered usb driver\n");
  154.     }
  155.     return 0;
  156. }
  157.  
  158. static void __exit airsensorfs_exit(void)
  159. {
  160.     usb_deregister(&airsensorfs);  
  161. }
  162.  
  163. module_init(airsensorfs_init);
  164. module_exit(airsensorfs_exit);
  165. MODULE_LICENSE( "GPL" );
Advertisement
Add Comment
Please, Sign In to add comment