Merge tag 'sched_ext-for-6.12-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-stable.git] / drivers / char / dsp56k.c
blob1c2c8439797c242d6833a470236f4c6406c2cde2
1 /*
2 * The DSP56001 Device Driver, saviour of the Free World(tm)
4 * Authors: Fredrik Noring <noring@nocrew.org>
5 * lars brinkhoff <lars@nocrew.org>
6 * Tomas Berndtsson <tomas@nocrew.org>
8 * First version May 1996
10 * History:
11 * 97-01-29 Tomas Berndtsson,
12 * Integrated with Linux 2.1.21 kernel sources.
13 * 97-02-15 Tomas Berndtsson,
14 * Fixed for kernel 2.1.26
16 * BUGS:
17 * Hmm... there must be something here :)
19 * Copyright (C) 1996,1997 Fredrik Noring, lars brinkhoff & Tomas Berndtsson
21 * This file is subject to the terms and conditions of the GNU General Public
22 * License. See the file COPYING in the main directory of this archive
23 * for more details.
26 #include <linux/module.h>
27 #include <linux/major.h>
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/delay.h> /* guess what */
31 #include <linux/fs.h>
32 #include <linux/mm.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/mutex.h>
36 #include <linux/firmware.h>
37 #include <linux/platform_device.h>
38 #include <linux/uaccess.h> /* For put_user and get_user */
40 #include <asm/atarihw.h>
41 #include <asm/traps.h>
43 #include <asm/dsp56k.h>
45 /* minor devices */
46 #define DSP56K_DEV_56001 0 /* The only device so far */
48 #define TIMEOUT 10 /* Host port timeout in number of tries */
49 #define MAXIO 2048 /* Maximum number of words before sleep */
50 #define DSP56K_MAX_BINARY_LENGTH (3*64*1024)
52 #define DSP56K_TX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_TREQ
53 #define DSP56K_RX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_RREQ
54 #define DSP56K_TX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_TREQ
55 #define DSP56K_RX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_RREQ
57 #define DSP56K_TRANSMIT (dsp56k_host_interface.isr & DSP56K_ISR_TXDE)
58 #define DSP56K_RECEIVE (dsp56k_host_interface.isr & DSP56K_ISR_RXDF)
60 #define handshake(count, maxio, timeout, ENABLE, f) \
61 { \
62 long i, t, m; \
63 while (count > 0) { \
64 m = min_t(unsigned long, count, maxio); \
65 for (i = 0; i < m; i++) { \
66 for (t = 0; t < timeout && !ENABLE; t++) \
67 msleep(20); \
68 if(!ENABLE) \
69 return -EIO; \
70 f; \
71 } \
72 count -= m; \
73 if (m == maxio) msleep(20); \
74 } \
77 #define tx_wait(n) \
78 { \
79 int t; \
80 for(t = 0; t < n && !DSP56K_TRANSMIT; t++) \
81 msleep(10); \
82 if(!DSP56K_TRANSMIT) { \
83 return -EIO; \
84 } \
87 #define rx_wait(n) \
88 { \
89 int t; \
90 for(t = 0; t < n && !DSP56K_RECEIVE; t++) \
91 msleep(10); \
92 if(!DSP56K_RECEIVE) { \
93 return -EIO; \
94 } \
97 static DEFINE_MUTEX(dsp56k_mutex);
98 static struct dsp56k_device {
99 unsigned long in_use;
100 long maxio, timeout;
101 int tx_wsize, rx_wsize;
102 } dsp56k;
104 static const struct class dsp56k_class = {
105 .name = "dsp56k",
108 static int dsp56k_reset(void)
110 u_char status;
112 /* Power down the DSP */
113 sound_ym.rd_data_reg_sel = 14;
114 status = sound_ym.rd_data_reg_sel & 0xef;
115 sound_ym.wd_data = status;
116 sound_ym.wd_data = status | 0x10;
118 udelay(10);
120 /* Power up the DSP */
121 sound_ym.rd_data_reg_sel = 14;
122 sound_ym.wd_data = sound_ym.rd_data_reg_sel & 0xef;
124 return 0;
127 static int dsp56k_upload(u_char __user *bin, int len)
129 struct platform_device *pdev;
130 const struct firmware *fw;
131 const char fw_name[] = "dsp56k/bootstrap.bin";
132 int err;
133 int i;
135 dsp56k_reset();
137 pdev = platform_device_register_simple("dsp56k", 0, NULL, 0);
138 if (IS_ERR(pdev)) {
139 printk(KERN_ERR "Failed to register device for \"%s\"\n",
140 fw_name);
141 return -EINVAL;
143 err = request_firmware(&fw, fw_name, &pdev->dev);
144 platform_device_unregister(pdev);
145 if (err) {
146 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
147 fw_name, err);
148 return err;
150 if (fw->size % 3) {
151 printk(KERN_ERR "Bogus length %d in image \"%s\"\n",
152 fw->size, fw_name);
153 release_firmware(fw);
154 return -EINVAL;
156 for (i = 0; i < fw->size; i = i + 3) {
157 /* tx_wait(10); */
158 dsp56k_host_interface.data.b[1] = fw->data[i];
159 dsp56k_host_interface.data.b[2] = fw->data[i + 1];
160 dsp56k_host_interface.data.b[3] = fw->data[i + 2];
162 release_firmware(fw);
163 for (; i < 512; i++) {
164 /* tx_wait(10); */
165 dsp56k_host_interface.data.b[1] = 0;
166 dsp56k_host_interface.data.b[2] = 0;
167 dsp56k_host_interface.data.b[3] = 0;
170 for (i = 0; i < len; i++) {
171 tx_wait(10);
172 get_user(dsp56k_host_interface.data.b[1], bin++);
173 get_user(dsp56k_host_interface.data.b[2], bin++);
174 get_user(dsp56k_host_interface.data.b[3], bin++);
177 tx_wait(10);
178 dsp56k_host_interface.data.l = 3; /* Magic execute */
180 return 0;
183 static ssize_t dsp56k_read(struct file *file, char __user *buf, size_t count,
184 loff_t *ppos)
186 struct inode *inode = file_inode(file);
187 int dev = iminor(inode) & 0x0f;
189 switch(dev)
191 case DSP56K_DEV_56001:
194 long n;
196 /* Don't do anything if nothing is to be done */
197 if (!count) return 0;
199 n = 0;
200 switch (dsp56k.rx_wsize) {
201 case 1: /* 8 bit */
203 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
204 put_user(dsp56k_host_interface.data.b[3], buf+n++));
205 return n;
207 case 2: /* 16 bit */
209 short __user *data;
211 count /= 2;
212 data = (short __user *) buf;
213 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
214 put_user(dsp56k_host_interface.data.w[1], data+n++));
215 return 2*n;
217 case 3: /* 24 bit */
219 count /= 3;
220 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
221 put_user(dsp56k_host_interface.data.b[1], buf+n++);
222 put_user(dsp56k_host_interface.data.b[2], buf+n++);
223 put_user(dsp56k_host_interface.data.b[3], buf+n++));
224 return 3*n;
226 case 4: /* 32 bit */
228 long __user *data;
230 count /= 4;
231 data = (long __user *) buf;
232 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
233 put_user(dsp56k_host_interface.data.l, data+n++));
234 return 4*n;
237 return -EFAULT;
240 default:
241 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
242 return -ENXIO;
246 static ssize_t dsp56k_write(struct file *file, const char __user *buf, size_t count,
247 loff_t *ppos)
249 struct inode *inode = file_inode(file);
250 int dev = iminor(inode) & 0x0f;
252 switch(dev)
254 case DSP56K_DEV_56001:
256 long n;
258 /* Don't do anything if nothing is to be done */
259 if (!count) return 0;
261 n = 0;
262 switch (dsp56k.tx_wsize) {
263 case 1: /* 8 bit */
265 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
266 get_user(dsp56k_host_interface.data.b[3], buf+n++));
267 return n;
269 case 2: /* 16 bit */
271 const short __user *data;
273 count /= 2;
274 data = (const short __user *)buf;
275 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
276 get_user(dsp56k_host_interface.data.w[1], data+n++));
277 return 2*n;
279 case 3: /* 24 bit */
281 count /= 3;
282 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
283 get_user(dsp56k_host_interface.data.b[1], buf+n++);
284 get_user(dsp56k_host_interface.data.b[2], buf+n++);
285 get_user(dsp56k_host_interface.data.b[3], buf+n++));
286 return 3*n;
288 case 4: /* 32 bit */
290 const long __user *data;
292 count /= 4;
293 data = (const long __user *)buf;
294 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
295 get_user(dsp56k_host_interface.data.l, data+n++));
296 return 4*n;
300 return -EFAULT;
302 default:
303 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
304 return -ENXIO;
308 static long dsp56k_ioctl(struct file *file, unsigned int cmd,
309 unsigned long arg)
311 int dev = iminor(file_inode(file)) & 0x0f;
312 void __user *argp = (void __user *)arg;
314 switch(dev)
316 case DSP56K_DEV_56001:
318 switch(cmd) {
319 case DSP56K_UPLOAD:
321 char __user *bin;
322 int r, len;
323 struct dsp56k_upload __user *binary = argp;
325 if(get_user(len, &binary->len) < 0)
326 return -EFAULT;
327 if(get_user(bin, &binary->bin) < 0)
328 return -EFAULT;
330 if (len <= 0) {
331 return -EINVAL; /* nothing to upload?!? */
333 if (len > DSP56K_MAX_BINARY_LENGTH) {
334 return -EINVAL;
336 mutex_lock(&dsp56k_mutex);
337 r = dsp56k_upload(bin, len);
338 mutex_unlock(&dsp56k_mutex);
339 if (r < 0) {
340 return r;
343 break;
345 case DSP56K_SET_TX_WSIZE:
346 if (arg > 4 || arg < 1)
347 return -EINVAL;
348 mutex_lock(&dsp56k_mutex);
349 dsp56k.tx_wsize = (int) arg;
350 mutex_unlock(&dsp56k_mutex);
351 break;
352 case DSP56K_SET_RX_WSIZE:
353 if (arg > 4 || arg < 1)
354 return -EINVAL;
355 mutex_lock(&dsp56k_mutex);
356 dsp56k.rx_wsize = (int) arg;
357 mutex_unlock(&dsp56k_mutex);
358 break;
359 case DSP56K_HOST_FLAGS:
361 int dir, out, status;
362 struct dsp56k_host_flags __user *hf = argp;
364 if(get_user(dir, &hf->dir) < 0)
365 return -EFAULT;
366 if(get_user(out, &hf->out) < 0)
367 return -EFAULT;
369 mutex_lock(&dsp56k_mutex);
370 if ((dir & 0x1) && (out & 0x1))
371 dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
372 else if (dir & 0x1)
373 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
374 if ((dir & 0x2) && (out & 0x2))
375 dsp56k_host_interface.icr |= DSP56K_ICR_HF1;
376 else if (dir & 0x2)
377 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
379 status = 0;
380 if (dsp56k_host_interface.icr & DSP56K_ICR_HF0) status |= 0x1;
381 if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
382 if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
383 if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
384 mutex_unlock(&dsp56k_mutex);
385 return put_user(status, &hf->status);
387 case DSP56K_HOST_CMD:
388 if (arg > 31)
389 return -EINVAL;
390 mutex_lock(&dsp56k_mutex);
391 dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
392 DSP56K_CVR_HC);
393 mutex_unlock(&dsp56k_mutex);
394 break;
395 default:
396 return -EINVAL;
398 return 0;
400 default:
401 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
402 return -ENXIO;
406 /* As of 2.1.26 this should be dsp56k_poll,
407 * but how do I then check device minor number?
408 * Do I need this function at all???
410 #if 0
411 static __poll_t dsp56k_poll(struct file *file, poll_table *wait)
413 int dev = iminor(file_inode(file)) & 0x0f;
415 switch(dev)
417 case DSP56K_DEV_56001:
418 /* poll_wait(file, ???, wait); */
419 return EPOLLIN | EPOLLRDNORM | EPOLLOUT;
421 default:
422 printk("DSP56k driver: Unknown minor device: %d\n", dev);
423 return 0;
426 #endif
428 static int dsp56k_open(struct inode *inode, struct file *file)
430 int dev = iminor(inode) & 0x0f;
431 int ret = 0;
433 mutex_lock(&dsp56k_mutex);
434 switch(dev)
436 case DSP56K_DEV_56001:
438 if (test_and_set_bit(0, &dsp56k.in_use)) {
439 ret = -EBUSY;
440 goto out;
443 dsp56k.timeout = TIMEOUT;
444 dsp56k.maxio = MAXIO;
445 dsp56k.rx_wsize = dsp56k.tx_wsize = 4;
447 DSP56K_TX_INT_OFF;
448 DSP56K_RX_INT_OFF;
450 /* Zero host flags */
451 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
452 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
454 break;
456 default:
457 ret = -ENODEV;
459 out:
460 mutex_unlock(&dsp56k_mutex);
461 return ret;
464 static int dsp56k_release(struct inode *inode, struct file *file)
466 int dev = iminor(inode) & 0x0f;
468 switch(dev)
470 case DSP56K_DEV_56001:
471 clear_bit(0, &dsp56k.in_use);
472 break;
473 default:
474 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
475 return -ENXIO;
478 return 0;
481 static const struct file_operations dsp56k_fops = {
482 .owner = THIS_MODULE,
483 .read = dsp56k_read,
484 .write = dsp56k_write,
485 .unlocked_ioctl = dsp56k_ioctl,
486 .open = dsp56k_open,
487 .release = dsp56k_release,
488 .llseek = noop_llseek,
492 /****** Init and module functions ******/
494 static const char banner[] __initconst = KERN_INFO "DSP56k driver installed\n";
496 static int __init dsp56k_init_driver(void)
498 int err;
500 if(!MACH_IS_ATARI || !ATARIHW_PRESENT(DSP56K)) {
501 printk("DSP56k driver: Hardware not present\n");
502 return -ENODEV;
505 if(register_chrdev(DSP56K_MAJOR, "dsp56k", &dsp56k_fops)) {
506 printk("DSP56k driver: Unable to register driver\n");
507 return -ENODEV;
509 err = class_register(&dsp56k_class);
510 if (err)
511 goto out_chrdev;
512 device_create(&dsp56k_class, NULL, MKDEV(DSP56K_MAJOR, 0), NULL,
513 "dsp56k");
515 printk(banner);
516 goto out;
518 out_chrdev:
519 unregister_chrdev(DSP56K_MAJOR, "dsp56k");
520 out:
521 return err;
523 module_init(dsp56k_init_driver);
525 static void __exit dsp56k_cleanup_driver(void)
527 device_destroy(&dsp56k_class, MKDEV(DSP56K_MAJOR, 0));
528 class_unregister(&dsp56k_class);
529 unregister_chrdev(DSP56K_MAJOR, "dsp56k");
531 module_exit(dsp56k_cleanup_driver);
533 MODULE_DESCRIPTION("Atari DSP56001 Device Driver");
534 MODULE_LICENSE("GPL");
535 MODULE_FIRMWARE("dsp56k/bootstrap.bin");