[NETFILTER]: replace open coded checksum updates
[linux-2.6/linux-loongson.git] / sound / oss / sh_dac_audio.c
blob7b168d85f4ab83ea42e682fc6ff84afb48a97832
1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/sched.h>
4 #include <linux/linkage.h>
5 #include <linux/slab.h>
6 #include <linux/fs.h>
7 #include <linux/sound.h>
8 #include <linux/soundcard.h>
9 #include <asm/io.h>
10 #include <asm/uaccess.h>
11 #include <asm/irq.h>
12 #include <asm/delay.h>
13 #include <linux/interrupt.h>
15 #include <asm/cpu/dac.h>
17 #ifdef MACH_HP600
18 #include <asm/hp6xx/hp6xx.h>
19 #include <asm/hd64461/hd64461.h>
20 #endif
22 #define MODNAME "sh_dac_audio"
24 #define TMU_TOCR_INIT 0x00
26 #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
27 #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
29 #define TMU_TSTR 0xfffffe92
30 #define TMU1_TCOR 0xfffffea0
31 #define TMU1_TCNT 0xfffffea4
32 #define TMU1_TCR 0xfffffea8
34 #define BUFFER_SIZE 48000
36 static int rate;
37 static int empty;
38 static char *data_buffer, *buffer_begin, *buffer_end;
39 static int in_use, device_major;
41 static void dac_audio_start_timer(void)
43 u8 tstr;
45 tstr = ctrl_inb(TMU_TSTR);
46 tstr |= TMU1_TSTR_INIT;
47 ctrl_outb(tstr, TMU_TSTR);
50 static void dac_audio_stop_timer(void)
52 u8 tstr;
54 tstr = ctrl_inb(TMU_TSTR);
55 tstr &= ~TMU1_TSTR_INIT;
56 ctrl_outb(tstr, TMU_TSTR);
59 static void dac_audio_reset(void)
61 dac_audio_stop_timer();
62 buffer_begin = buffer_end = data_buffer;
63 empty = 1;
66 static void dac_audio_sync(void)
68 while (!empty)
69 schedule();
72 static void dac_audio_start(void)
74 #ifdef MACH_HP600
75 u16 v;
76 v = inw(HD64461_GPADR);
77 v &= ~HD64461_GPADR_SPEAKER;
78 outw(v, HD64461_GPADR);
79 #endif
80 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
81 ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
83 static void dac_audio_stop(void)
85 #ifdef MACH_HP600
86 u16 v;
87 #endif
88 dac_audio_stop_timer();
89 #ifdef MACH_HP600
90 v = inw(HD64461_GPADR);
91 v |= HD64461_GPADR_SPEAKER;
92 outw(v, HD64461_GPADR);
93 #endif
94 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
97 static void dac_audio_set_rate(void)
99 unsigned long interval;
101 interval = (current_cpu_data.module_clock / 4) / rate;
102 ctrl_outl(interval, TMU1_TCOR);
103 ctrl_outl(interval, TMU1_TCNT);
106 static int dac_audio_ioctl(struct inode *inode, struct file *file,
107 unsigned int cmd, unsigned long arg)
109 int val;
111 switch (cmd) {
112 case OSS_GETVERSION:
113 return put_user(SOUND_VERSION, (int *)arg);
115 case SNDCTL_DSP_SYNC:
116 dac_audio_sync();
117 return 0;
119 case SNDCTL_DSP_RESET:
120 dac_audio_reset();
121 return 0;
123 case SNDCTL_DSP_GETFMTS:
124 return put_user(AFMT_U8, (int *)arg);
126 case SNDCTL_DSP_SETFMT:
127 return put_user(AFMT_U8, (int *)arg);
129 case SNDCTL_DSP_NONBLOCK:
130 file->f_flags |= O_NONBLOCK;
131 return 0;
133 case SNDCTL_DSP_GETCAPS:
134 return 0;
136 case SOUND_PCM_WRITE_RATE:
137 val = *(int *)arg;
138 if (val > 0) {
139 rate = val;
140 dac_audio_set_rate();
142 return put_user(rate, (int *)arg);
144 case SNDCTL_DSP_STEREO:
145 return put_user(0, (int *)arg);
147 case SOUND_PCM_WRITE_CHANNELS:
148 return put_user(1, (int *)arg);
150 case SNDCTL_DSP_SETDUPLEX:
151 return -EINVAL;
153 case SNDCTL_DSP_PROFILE:
154 return -EINVAL;
156 case SNDCTL_DSP_GETBLKSIZE:
157 return put_user(BUFFER_SIZE, (int *)arg);
159 case SNDCTL_DSP_SETFRAGMENT:
160 return 0;
162 default:
163 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
164 cmd);
165 return -EINVAL;
167 return -EINVAL;
170 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
171 loff_t * ppos)
173 int free;
174 int nbytes;
176 if (count < 0)
177 return -EINVAL;
179 if (!count) {
180 dac_audio_sync();
181 return 0;
184 free = buffer_begin - buffer_end;
186 if (free < 0)
187 free += BUFFER_SIZE;
188 if ((free == 0) && (empty))
189 free = BUFFER_SIZE;
190 if (count > free)
191 count = free;
192 if (buffer_begin > buffer_end) {
193 if (copy_from_user((void *)buffer_end, buf, count))
194 return -EFAULT;
196 buffer_end += count;
197 } else {
198 nbytes = data_buffer + BUFFER_SIZE - buffer_end;
199 if (nbytes > count) {
200 if (copy_from_user((void *)buffer_end, buf, count))
201 return -EFAULT;
202 buffer_end += count;
203 } else {
204 if (copy_from_user((void *)buffer_end, buf, nbytes))
205 return -EFAULT;
206 if (copy_from_user
207 ((void *)data_buffer, buf + nbytes, count - nbytes))
208 return -EFAULT;
209 buffer_end = data_buffer + count - nbytes;
213 if (empty) {
214 empty = 0;
215 dac_audio_start_timer();
218 return count;
221 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
222 loff_t * ppos)
224 return -EINVAL;
227 static int dac_audio_open(struct inode *inode, struct file *file)
229 if (file->f_mode & FMODE_READ)
230 return -ENODEV;
231 if (in_use)
232 return -EBUSY;
234 in_use = 1;
236 dac_audio_start();
238 return 0;
241 static int dac_audio_release(struct inode *inode, struct file *file)
243 dac_audio_sync();
244 dac_audio_stop();
245 in_use = 0;
247 return 0;
250 struct file_operations dac_audio_fops = {
251 .read = dac_audio_read,
252 .write = dac_audio_write,
253 .ioctl = dac_audio_ioctl,
254 .open = dac_audio_open,
255 .release = dac_audio_release,
258 static irqreturn_t timer1_interrupt(int irq, void *dev, struct pt_regs *regs)
260 unsigned long timer_status;
262 timer_status = ctrl_inw(TMU1_TCR);
263 timer_status &= ~0x100;
264 ctrl_outw(timer_status, TMU1_TCR);
266 if (!empty) {
267 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
268 buffer_begin++;
270 if (buffer_begin == data_buffer + BUFFER_SIZE)
271 buffer_begin = data_buffer;
272 if (buffer_begin == buffer_end) {
273 empty = 1;
274 dac_audio_stop_timer();
277 return IRQ_HANDLED;
280 static int __init dac_audio_init(void)
282 int retval;
284 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
285 printk(KERN_ERR "Cannot register dsp device");
286 return device_major;
289 in_use = 0;
291 data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
292 if (data_buffer == NULL)
293 return -ENOMEM;
295 dac_audio_reset();
296 rate = 8000;
297 dac_audio_set_rate();
299 retval =
300 request_irq(TIMER1_IRQ, timer1_interrupt, IRQF_DISABLED, MODNAME, 0);
301 if (retval < 0) {
302 printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
303 TIMER1_IRQ);
304 return retval;
307 return 0;
310 static void __exit dac_audio_exit(void)
312 free_irq(TIMER1_IRQ, 0);
314 unregister_sound_dsp(device_major);
315 kfree((void *)data_buffer);
318 module_init(dac_audio_init);
319 module_exit(dac_audio_exit);
321 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
322 MODULE_DESCRIPTION("SH DAC sound driver");
323 MODULE_LICENSE("GPL");