[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / oss / sh_dac_audio.c
blobc09cdeedc1914f33ee7e443715761a456997f24b
1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/sched.h>
5 #include <linux/version.h>
6 #include <linux/linkage.h>
7 #include <linux/slab.h>
8 #include <linux/fs.h>
9 #include <linux/sound.h>
10 #include <linux/soundcard.h>
11 #include <asm/io.h>
12 #include <asm/uaccess.h>
13 #include <asm/irq.h>
14 #include <asm/delay.h>
15 #include <linux/interrupt.h>
17 #include <asm/cpu/dac.h>
19 #ifdef MACH_HP600
20 #include <asm/hp6xx/hp6xx.h>
21 #include <asm/hd64461/hd64461.h>
22 #endif
24 #define MODNAME "sh_dac_audio"
26 #define TMU_TOCR_INIT 0x00
28 #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
29 #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
31 #define TMU_TSTR 0xfffffe92
32 #define TMU1_TCOR 0xfffffea0
33 #define TMU1_TCNT 0xfffffea4
34 #define TMU1_TCR 0xfffffea8
36 #define BUFFER_SIZE 48000
38 static int rate;
39 static int empty;
40 static char *data_buffer, *buffer_begin, *buffer_end;
41 static int in_use, device_major;
43 static void dac_audio_start_timer(void)
45 u8 tstr;
47 tstr = ctrl_inb(TMU_TSTR);
48 tstr |= TMU1_TSTR_INIT;
49 ctrl_outb(tstr, TMU_TSTR);
52 static void dac_audio_stop_timer(void)
54 u8 tstr;
56 tstr = ctrl_inb(TMU_TSTR);
57 tstr &= ~TMU1_TSTR_INIT;
58 ctrl_outb(tstr, TMU_TSTR);
61 static void dac_audio_reset(void)
63 dac_audio_stop_timer();
64 buffer_begin = buffer_end = data_buffer;
65 empty = 1;
68 static void dac_audio_sync(void)
70 while (!empty)
71 schedule();
74 static void dac_audio_start(void)
76 #ifdef MACH_HP600
77 u16 v;
78 v = inw(HD64461_GPADR);
79 v &= ~HD64461_GPADR_SPEAKER;
80 outw(v, HD64461_GPADR);
81 #endif
82 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
83 ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
85 static void dac_audio_stop(void)
87 #ifdef MACH_HP600
88 u16 v;
89 #endif
90 dac_audio_stop_timer();
91 #ifdef MACH_HP600
92 v = inw(HD64461_GPADR);
93 v |= HD64461_GPADR_SPEAKER;
94 outw(v, HD64461_GPADR);
95 #endif
96 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
99 static void dac_audio_set_rate(void)
101 unsigned long interval;
103 interval = (current_cpu_data.module_clock / 4) / rate;
104 ctrl_outl(interval, TMU1_TCOR);
105 ctrl_outl(interval, TMU1_TCNT);
108 static int dac_audio_ioctl(struct inode *inode, struct file *file,
109 unsigned int cmd, unsigned long arg)
111 int val;
113 switch (cmd) {
114 case OSS_GETVERSION:
115 return put_user(SOUND_VERSION, (int *)arg);
117 case SNDCTL_DSP_SYNC:
118 dac_audio_sync();
119 return 0;
121 case SNDCTL_DSP_RESET:
122 dac_audio_reset();
123 return 0;
125 case SNDCTL_DSP_GETFMTS:
126 return put_user(AFMT_U8, (int *)arg);
128 case SNDCTL_DSP_SETFMT:
129 return put_user(AFMT_U8, (int *)arg);
131 case SNDCTL_DSP_NONBLOCK:
132 file->f_flags |= O_NONBLOCK;
133 return 0;
135 case SNDCTL_DSP_GETCAPS:
136 return 0;
138 case SOUND_PCM_WRITE_RATE:
139 val = *(int *)arg;
140 if (val > 0) {
141 rate = val;
142 dac_audio_set_rate();
144 return put_user(rate, (int *)arg);
146 case SNDCTL_DSP_STEREO:
147 return put_user(0, (int *)arg);
149 case SOUND_PCM_WRITE_CHANNELS:
150 return put_user(1, (int *)arg);
152 case SNDCTL_DSP_SETDUPLEX:
153 return -EINVAL;
155 case SNDCTL_DSP_PROFILE:
156 return -EINVAL;
158 case SNDCTL_DSP_GETBLKSIZE:
159 return put_user(BUFFER_SIZE, (int *)arg);
161 case SNDCTL_DSP_SETFRAGMENT:
162 return 0;
164 default:
165 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
166 cmd);
167 return -EINVAL;
169 return -EINVAL;
172 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
173 loff_t * ppos)
175 int free;
176 int nbytes;
178 if (count < 0)
179 return -EINVAL;
181 if (!count) {
182 dac_audio_sync();
183 return 0;
186 free = buffer_begin - buffer_end;
188 if (free < 0)
189 free += BUFFER_SIZE;
190 if ((free == 0) && (empty))
191 free = BUFFER_SIZE;
192 if (count > free)
193 count = free;
194 if (buffer_begin > buffer_end) {
195 if (copy_from_user((void *)buffer_end, buf, count))
196 return -EFAULT;
198 buffer_end += count;
199 } else {
200 nbytes = data_buffer + BUFFER_SIZE - buffer_end;
201 if (nbytes > count) {
202 if (copy_from_user((void *)buffer_end, buf, count))
203 return -EFAULT;
204 buffer_end += count;
205 } else {
206 if (copy_from_user((void *)buffer_end, buf, nbytes))
207 return -EFAULT;
208 if (copy_from_user
209 ((void *)data_buffer, buf + nbytes, count - nbytes))
210 return -EFAULT;
211 buffer_end = data_buffer + count - nbytes;
215 if (empty) {
216 empty = 0;
217 dac_audio_start_timer();
220 return count;
223 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
224 loff_t * ppos)
226 return -EINVAL;
229 static int dac_audio_open(struct inode *inode, struct file *file)
231 if (file->f_mode & FMODE_READ)
232 return -ENODEV;
233 if (in_use)
234 return -EBUSY;
236 in_use = 1;
238 dac_audio_start();
240 return 0;
243 static int dac_audio_release(struct inode *inode, struct file *file)
245 dac_audio_sync();
246 dac_audio_stop();
247 in_use = 0;
249 return 0;
252 struct file_operations dac_audio_fops = {
253 .read = dac_audio_read,
254 .write = dac_audio_write,
255 .ioctl = dac_audio_ioctl,
256 .open = dac_audio_open,
257 .release = dac_audio_release,
260 static irqreturn_t timer1_interrupt(int irq, void *dev, struct pt_regs *regs)
262 unsigned long timer_status;
264 timer_status = ctrl_inw(TMU1_TCR);
265 timer_status &= ~0x100;
266 ctrl_outw(timer_status, TMU1_TCR);
268 if (!empty) {
269 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
270 buffer_begin++;
272 if (buffer_begin == data_buffer + BUFFER_SIZE)
273 buffer_begin = data_buffer;
274 if (buffer_begin == buffer_end) {
275 empty = 1;
276 dac_audio_stop_timer();
279 return IRQ_HANDLED;
282 static int __init dac_audio_init(void)
284 int retval;
286 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
287 printk(KERN_ERR "Cannot register dsp device");
288 return device_major;
291 in_use = 0;
293 data_buffer = (char *)kmalloc(BUFFER_SIZE, GFP_KERNEL);
294 if (data_buffer == NULL)
295 return -ENOMEM;
297 dac_audio_reset();
298 rate = 8000;
299 dac_audio_set_rate();
301 retval =
302 request_irq(TIMER1_IRQ, timer1_interrupt, SA_INTERRUPT, MODNAME, 0);
303 if (retval < 0) {
304 printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
305 TIMER1_IRQ);
306 return retval;
309 return 0;
312 static void __exit dac_audio_exit(void)
314 free_irq(TIMER1_IRQ, 0);
316 unregister_sound_dsp(device_major);
317 kfree((void *)data_buffer);
320 module_init(dac_audio_init);
321 module_exit(dac_audio_exit);
323 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
324 MODULE_DESCRIPTION("SH DAC sound driver");
325 MODULE_LICENSE("GPL");