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