^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/miscdevice.h> /* for misc_register, and MISC_DYNAMIC_MINOR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "speakup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "spk_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) static int misc_registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static int dev_opened;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static ssize_t speakup_file_write(struct file *fp, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) size_t nbytes, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) size_t count = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) const char __user *ptr = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) size_t bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) u_char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (!synth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bytes = min(count, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (copy_from_user(buf, ptr, bytes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) count -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ptr += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) spin_lock_irqsave(&speakup_info.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) synth_write(buf, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) spin_unlock_irqrestore(&speakup_info.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return (ssize_t)nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static ssize_t speakup_file_read(struct file *fp, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) size_t nbytes, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int speakup_file_open(struct inode *ip, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!synth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (xchg(&dev_opened, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int speakup_file_release(struct inode *ip, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) dev_opened = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const struct file_operations synth_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .read = speakup_file_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .write = speakup_file_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .open = speakup_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .release = speakup_file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static struct miscdevice synth_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .name = "synth",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .fops = &synth_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) void speakup_register_devsynth(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (misc_registered != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* zero it so if register fails, deregister will not ref invalid ptrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (misc_register(&synth_device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pr_warn("Couldn't initialize miscdevice /dev/synth.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pr_info("initialized device: /dev/synth, node (MAJOR %d, MINOR %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) MISC_MAJOR, synth_device.minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) misc_registered = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) void speakup_unregister_devsynth(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!misc_registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_info("speakup: unregistering synth device /dev/synth\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) misc_deregister(&synth_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) misc_registered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }