Staging: add speakup to the staging directory
[deliverable/linux.git] / drivers / staging / speakup / devsynth.c
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
13 static int misc_registered;
14 static int dev_opened;
15
16 static ssize_t speakup_file_write(struct file *fp, const char *buffer,
17 size_t nbytes, loff_t *ppos)
18 {
19 size_t count = nbytes;
20 const char *ptr = buffer;
21 int bytes;
22 unsigned long flags;
23 u_char buf[256];
24 if (synth == NULL)
25 return -ENODEV;
26 while (count > 0) {
27 bytes = min_t(size_t, count, sizeof(buf));
28 if (copy_from_user(buf, ptr, bytes))
29 return -EFAULT;
30 count -= bytes;
31 ptr += bytes;
32 spk_lock(flags);
33 synth_write(buf, bytes);
34 spk_unlock(flags);
35 }
36 return (ssize_t) nbytes;
37 }
38
39 static ssize_t speakup_file_read(struct file *fp, char *buf, size_t nbytes, loff_t *ppos)
40 {
41 return 0;
42 }
43
44 static int speakup_file_open(struct inode *ip, struct file *fp)
45 {
46 if (synth == NULL)
47 return -ENODEV;
48 if (xchg(&dev_opened, 1))
49 return -EBUSY;
50 return 0;
51 }
52
53 static int speakup_file_release(struct inode *ip, struct file *fp)
54 {
55 dev_opened = 0;
56 return 0;
57 }
58
59 static struct file_operations synth_fops = {
60 .read = speakup_file_read,
61 .write = speakup_file_write,
62 .open = speakup_file_open,
63 .release = speakup_file_release,
64 };
65
66 static struct miscdevice synth_device = {
67 .minor = SYNTH_MINOR,
68 .name = "synth",
69 .fops = &synth_fops,
70 };
71
72 void speakup_register_devsynth(void)
73 {
74 if (misc_registered != 0)
75 return;
76 /* zero it so if register fails, deregister will not ref invalid ptrs */
77 if (misc_register(&synth_device))
78 pr_warn("Couldn't initialize miscdevice /dev/synth.\n");
79 else {
80 pr_info("initialized device: /dev/synth, node (MAJOR %d, MINOR %d)\n", MISC_MAJOR, SYNTH_MINOR);
81 misc_registered = 1;
82 }
83 }
84
85 void speakup_unregister_devsynth(void)
86 {
87 if (!misc_registered)
88 return;
89 pr_info("speakup: unregistering synth device /dev/synth\n");
90 misc_deregister(&synth_device);
91 misc_registered = 0;
92 }
This page took 0.040744 seconds and 5 git commands to generate.