Advertisement
goroh_kun

sh01dのshboot_daemon_drv.cをIS11Sに移植

Feb 23rd, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.59 KB | None | 0 0
  1. /* drivers/sharp/shboot/shboot_daemon_drv.c
  2.  *
  3.  * Copyright (C) 2011 Sharp Corporation
  4.  *
  5.  * This software is licensed under the terms of the GNU General Public
  6.  * License version 2, as published by the Free Software Foundation, and
  7.  * may be copied, distributed, and modified under those terms.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  */
  15. #include <asm/uaccess.h>
  16. #include <linux/module.h>
  17. #include <linux/cdev.h>
  18. #include <linux/semaphore.h>
  19. #include <linux/sched.h>
  20. #include <linux/wait.h>
  21. #include <linux/fs.h>
  22. #include <linux/mm.h>
  23. #include <linux/device.h>
  24. #include "shboot_daemon_drv.h"
  25.  
  26. #define DEVICE_NAME "boot_daemon_drv"
  27.  
  28. #define GETTING_REQUEST 0
  29. #define GETTING_RESULT  1
  30. #define GET_RESULT      2
  31.  
  32. struct s_shboot_request {
  33.     int offset;
  34.     size_t len;
  35. };
  36.  
  37. static struct s_shboot_request shboot_request = {
  38.     .offset = 0,
  39.     .len = 0
  40. };
  41.  
  42. static void                 *shboot_buf = NULL;
  43. static wait_queue_head_t    shboot_call_q;
  44. static wait_queue_head_t    shboot_daemon_q;
  45. static struct semaphore     shboot_sem;
  46. static volatile int         shboot_state = GETTING_REQUEST;
  47. static int                  shboot_daemon_drv_opened = 0;
  48.  
  49. int shboot_daemon_drv_read_process(int offset, size_t len, void *buf)
  50. {
  51.     int ret = 0;
  52.    
  53.     if (!shboot_daemon_drv_opened) {
  54.         printk(KERN_ALERT "%s: sharp bootinfo device driver can't be opened.\n", __func__);
  55.         return -EPERM;
  56.     }
  57.    
  58.     ret = down_interruptible(&shboot_sem);
  59.     shboot_request.offset = offset;
  60.     shboot_request.len = len;
  61.     shboot_buf = buf;
  62.     shboot_state = GETTING_RESULT;
  63.     wake_up_interruptible(&shboot_daemon_q);
  64.     wait_event_interruptible(shboot_call_q, (shboot_state == GET_RESULT));
  65.     if (!strncmp(shboot_buf, "invalid request", sizeof("invalid request"))) {
  66.         printk(KERN_ALERT "%s :invalid request: offset = 0x%x, len = 0x%x\n", __func__, offset, len);
  67.         ret = -EPERM;
  68.     }
  69.     shboot_state = GETTING_REQUEST;
  70.     shboot_buf = NULL;
  71.     up(&shboot_sem);
  72.    
  73.     return ret;
  74. }
  75.  
  76. static ssize_t shboot_daemon_read(struct file *filp, char __user *buf,
  77.                                   size_t count, loff_t *f_pos)
  78. {
  79.     wait_event_interruptible(shboot_daemon_q, (shboot_state == GETTING_RESULT));
  80.     /* copy request */
  81.     if (copy_to_user(buf, &shboot_request, count)) {
  82.         printk(KERN_ALERT "%s :copy_to_user failed.: \n", __func__);
  83.         return 0;
  84.     }
  85.    
  86.     return count;
  87. }
  88.  
  89. static ssize_t shboot_daemon_write(struct file *filp, const char __user *buf,
  90.                                   size_t count, loff_t *f_pos)
  91. {
  92.     int ret = 0;
  93.     if(!shboot_buf) {
  94.         wake_up_interruptible(&shboot_call_q);
  95.         return ret;
  96.     }
  97.     if (copy_from_user(shboot_buf, buf, count)) {
  98.         printk(KERN_ALERT "%s :copy_from_user failed.: \n", __func__);
  99.         ret = 0;
  100.     } else {
  101.         ret = count;
  102.     }
  103.    
  104.     shboot_state = GET_RESULT;
  105.     wake_up_interruptible(&shboot_call_q);
  106.    
  107.     return ret;
  108. }
  109.  
  110. static int shboot_daemon_mmap(struct file *filp, struct vm_area_struct *vma)
  111. {
  112.     int err;
  113. //    err = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, vma->vm_end - vma->vm_start, vma->vm_page_prot);
  114.     err = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, vma->vm_end - vma->vm_start, vma->vm_page_prot);
  115.     if (err) {
  116.         printk(KERN_ALERT "%s :io_remap_pfn_range failed.: \n", __func__);
  117.         return -EAGAIN;
  118.     }
  119.     return 0;
  120. }
  121.  
  122. static int shboot_daemon_open(struct inode *inode, struct file *filp)
  123. {
  124.     shboot_daemon_drv_opened = 1;
  125.     return 0;
  126. }
  127.  
  128. static int shboot_daemon_release(struct inode *inode, struct file *filp)
  129. {
  130.     shboot_daemon_drv_opened = 0;
  131.     return 0;
  132. }
  133.  
  134. static struct file_operations shboot_daemon_fops = {
  135.     .owner = THIS_MODULE,
  136.     .read = shboot_daemon_read,
  137.     .write = shboot_daemon_write,
  138.     .mmap = shboot_daemon_mmap,
  139.     .open = shboot_daemon_open,
  140.     .release = shboot_daemon_release
  141. };
  142.  
  143. static int alloc_chrdev_region_ok = 0;  /* false */
  144. static int cdev_add_ok = 0;             /* false */
  145. static int class_create_ok = 0;         /* false */
  146. static int device_create_ok = 0;        /* false */
  147.  
  148. static struct cdev shboot_daemon_cdev;
  149. static dev_t shboot_daemon_dev;
  150. static struct class *shboot_daemon_class;
  151.  
  152. static void cleanup(void)
  153. {
  154.     if (device_create_ok) {
  155.         device_destroy(shboot_daemon_class, shboot_daemon_dev);
  156.     }
  157.     if (class_create_ok) {
  158.         class_destroy(shboot_daemon_class);
  159.     }
  160.     if (cdev_add_ok) {
  161.         cdev_del(&shboot_daemon_cdev);
  162.     }
  163.     if (alloc_chrdev_region_ok) {
  164.         unregister_chrdev_region(shboot_daemon_dev, 1);
  165.     }
  166.  
  167.     return;
  168. }
  169.  
  170. static int __init shboot_daemon_drv_init(void)
  171. {
  172.     int err = -1;
  173.  
  174.     err = alloc_chrdev_region(&shboot_daemon_dev, 0, 1, DEVICE_NAME);
  175.     if (err) {
  176.         printk(KERN_ALERT "%s :alloc_chrdev_region failed.: %d\n", __func__, err);
  177.         goto fail;
  178.     } else {
  179.         alloc_chrdev_region_ok = 1; /* true */
  180.     }
  181.  
  182.     cdev_init(&shboot_daemon_cdev, &shboot_daemon_fops);
  183.     shboot_daemon_cdev.owner = THIS_MODULE;
  184.  
  185.     err = cdev_add(&shboot_daemon_cdev, shboot_daemon_dev, 1);
  186.     if (err) {
  187.         printk(KERN_ALERT "%s :cdev_add failed.: %d\n", __func__, err);
  188.         goto fail;
  189.     } else {
  190.         cdev_add_ok = 1; /* true */
  191.     }
  192.  
  193.     /* make device file */
  194.     shboot_daemon_class = class_create(THIS_MODULE, DEVICE_NAME);
  195.  
  196.     if (IS_ERR(shboot_daemon_class)) {
  197.         printk(KERN_ALERT "%s :class_create failed.: \n", __func__);
  198.         goto fail;
  199.     } else {
  200.         class_create_ok = 1; /* true */
  201.     }
  202.  
  203.     if (device_create(shboot_daemon_class, NULL, shboot_daemon_dev,
  204.                       &shboot_daemon_cdev, DEVICE_NAME) < 0) {
  205.         printk(KERN_ALERT "%s :device_create failed.: \n", __func__);
  206.         goto fail;
  207.     } else {
  208.         device_create_ok = 1; /* true */
  209.     }
  210.  
  211.     init_MUTEX(&shboot_sem);
  212.     init_waitqueue_head(&shboot_call_q);
  213.     init_waitqueue_head(&shboot_daemon_q);
  214.    
  215.     return 0;
  216.  
  217. fail:
  218.     cleanup();
  219.     return err;
  220. }
  221.  
  222. module_init(shboot_daemon_drv_init);
  223.  
  224. static void __exit shboot_daemon_drv_exit(void)
  225. {
  226.     cleanup();
  227.     return;
  228. }
  229.  
  230. module_exit(shboot_daemon_drv_exit);
  231.  
  232. MODULE_DESCRIPTION("SHARP BOOTINFO DAEMON DRIVER MODULE");
  233. MODULE_LICENSE("GPL v2");
  234. MODULE_AUTHOR("SHARP CORPORATION");
  235. MODULE_VERSION("1.00");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement