1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/errno.h>
3 #include <linux/miscdevice.h> /* for misc_register, and SYNTH_MINOR */
4 #include <linux/types.h>
5 #include <linux/uaccess.h>
11 #define SYNTH_MINOR 25
14 static int misc_registered
;
15 static int dev_opened
;
17 static ssize_t
speakup_file_write(struct file
*fp
, const char __user
*buffer
,
18 size_t nbytes
, loff_t
*ppos
)
20 size_t count
= nbytes
;
21 const char __user
*ptr
= buffer
;
29 bytes
= min(count
, sizeof(buf
));
30 if (copy_from_user(buf
, ptr
, bytes
))
34 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
35 synth_write(buf
, bytes
);
36 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
38 return (ssize_t
)nbytes
;
41 static ssize_t
speakup_file_read(struct file
*fp
, char __user
*buf
,
42 size_t nbytes
, loff_t
*ppos
)
47 static int speakup_file_open(struct inode
*ip
, struct file
*fp
)
51 if (xchg(&dev_opened
, 1))
56 static int speakup_file_release(struct inode
*ip
, struct file
*fp
)
62 static const struct file_operations synth_fops
= {
63 .read
= speakup_file_read
,
64 .write
= speakup_file_write
,
65 .open
= speakup_file_open
,
66 .release
= speakup_file_release
,
69 static struct miscdevice synth_device
= {
75 void speakup_register_devsynth(void)
77 if (misc_registered
!= 0)
79 /* zero it so if register fails, deregister will not ref invalid ptrs */
80 if (misc_register(&synth_device
)) {
81 pr_warn("Couldn't initialize miscdevice /dev/synth.\n");
83 pr_info("initialized device: /dev/synth, node (MAJOR %d, MINOR %d)\n",
84 MISC_MAJOR
, SYNTH_MINOR
);
89 void speakup_unregister_devsynth(void)
93 pr_info("speakup: unregistering synth device /dev/synth\n");
94 misc_deregister(&synth_device
);