Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/errno.h>
0003 #include <linux/miscdevice.h>   /* for misc_register, and MISC_DYNAMIC_MINOR */
0004 #include <linux/types.h>
0005 #include <linux/uaccess.h>
0006 
0007 #include "speakup.h"
0008 #include "spk_priv.h"
0009 
0010 static int misc_registered;
0011 static int dev_opened;
0012 
0013 static ssize_t speakup_file_write(struct file *fp, const char __user *buffer,
0014                   size_t nbytes, loff_t *ppos)
0015 {
0016     size_t count = nbytes;
0017     const char __user *ptr = buffer;
0018     size_t bytes;
0019     unsigned long flags;
0020     u_char buf[256];
0021 
0022     if (!synth)
0023         return -ENODEV;
0024     while (count > 0) {
0025         bytes = min(count, sizeof(buf));
0026         if (copy_from_user(buf, ptr, bytes))
0027             return -EFAULT;
0028         count -= bytes;
0029         ptr += bytes;
0030         spin_lock_irqsave(&speakup_info.spinlock, flags);
0031         synth_write(buf, bytes);
0032         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
0033     }
0034     return (ssize_t)nbytes;
0035 }
0036 
0037 static ssize_t speakup_file_read(struct file *fp, char __user *buf,
0038                  size_t nbytes, loff_t *ppos)
0039 {
0040     return 0;
0041 }
0042 
0043 static int speakup_file_open(struct inode *ip, struct file *fp)
0044 {
0045     if (!synth)
0046         return -ENODEV;
0047     if (xchg(&dev_opened, 1))
0048         return -EBUSY;
0049     return 0;
0050 }
0051 
0052 static int speakup_file_release(struct inode *ip, struct file *fp)
0053 {
0054     dev_opened = 0;
0055     return 0;
0056 }
0057 
0058 static const struct file_operations synth_fops = {
0059     .read = speakup_file_read,
0060     .write = speakup_file_write,
0061     .open = speakup_file_open,
0062     .release = speakup_file_release,
0063 };
0064 
0065 static struct miscdevice synth_device = {
0066     .minor = MISC_DYNAMIC_MINOR,
0067     .name = "synth",
0068     .fops = &synth_fops,
0069 };
0070 
0071 void speakup_register_devsynth(void)
0072 {
0073     if (misc_registered != 0)
0074         return;
0075 /* zero it so if register fails, deregister will not ref invalid ptrs */
0076     if (misc_register(&synth_device)) {
0077         pr_warn("Couldn't initialize miscdevice /dev/synth.\n");
0078     } else {
0079         pr_info("initialized device: /dev/synth, node (MAJOR %d, MINOR %d)\n",
0080             MISC_MAJOR, synth_device.minor);
0081         misc_registered = 1;
0082     }
0083 }
0084 
0085 void speakup_unregister_devsynth(void)
0086 {
0087     if (!misc_registered)
0088         return;
0089     pr_info("speakup: unregistering synth device /dev/synth\n");
0090     misc_deregister(&synth_device);
0091     misc_registered = 0;
0092 }