Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * The DSP56001 Device Driver, saviour of the Free World(tm)
0003  *
0004  * Authors: Fredrik Noring   <noring@nocrew.org>
0005  *          lars brinkhoff   <lars@nocrew.org>
0006  *          Tomas Berndtsson <tomas@nocrew.org>
0007  *
0008  * First version May 1996
0009  *
0010  * History:
0011  *  97-01-29   Tomas Berndtsson,
0012  *               Integrated with Linux 2.1.21 kernel sources.
0013  *  97-02-15   Tomas Berndtsson,
0014  *               Fixed for kernel 2.1.26
0015  *
0016  * BUGS:
0017  *  Hmm... there must be something here :)
0018  *
0019  * Copyright (C) 1996,1997 Fredrik Noring, lars brinkhoff & Tomas Berndtsson
0020  *
0021  * This file is subject to the terms and conditions of the GNU General Public
0022  * License.  See the file COPYING in the main directory of this archive
0023  * for more details.
0024  */
0025 
0026 #include <linux/module.h>
0027 #include <linux/major.h>
0028 #include <linux/types.h>
0029 #include <linux/errno.h>
0030 #include <linux/delay.h>    /* guess what */
0031 #include <linux/fs.h>
0032 #include <linux/mm.h>
0033 #include <linux/init.h>
0034 #include <linux/device.h>
0035 #include <linux/mutex.h>
0036 #include <linux/firmware.h>
0037 #include <linux/platform_device.h>
0038 #include <linux/uaccess.h>  /* For put_user and get_user */
0039 
0040 #include <asm/atarihw.h>
0041 #include <asm/traps.h>
0042 
0043 #include <asm/dsp56k.h>
0044 
0045 /* minor devices */
0046 #define DSP56K_DEV_56001        0    /* The only device so far */
0047 
0048 #define TIMEOUT    10   /* Host port timeout in number of tries */
0049 #define MAXIO    2048   /* Maximum number of words before sleep */
0050 #define DSP56K_MAX_BINARY_LENGTH (3*64*1024)
0051 
0052 #define DSP56K_TX_INT_ON    dsp56k_host_interface.icr |=  DSP56K_ICR_TREQ
0053 #define DSP56K_RX_INT_ON    dsp56k_host_interface.icr |=  DSP56K_ICR_RREQ
0054 #define DSP56K_TX_INT_OFF   dsp56k_host_interface.icr &= ~DSP56K_ICR_TREQ
0055 #define DSP56K_RX_INT_OFF   dsp56k_host_interface.icr &= ~DSP56K_ICR_RREQ
0056 
0057 #define DSP56K_TRANSMIT     (dsp56k_host_interface.isr & DSP56K_ISR_TXDE)
0058 #define DSP56K_RECEIVE      (dsp56k_host_interface.isr & DSP56K_ISR_RXDF)
0059 
0060 #define handshake(count, maxio, timeout, ENABLE, f) \
0061 { \
0062     long i, t, m; \
0063     while (count > 0) { \
0064         m = min_t(unsigned long, count, maxio); \
0065         for (i = 0; i < m; i++) { \
0066             for (t = 0; t < timeout && !ENABLE; t++) \
0067                 msleep(20); \
0068             if(!ENABLE) \
0069                 return -EIO; \
0070             f; \
0071         } \
0072         count -= m; \
0073         if (m == maxio) msleep(20); \
0074     } \
0075 }
0076 
0077 #define tx_wait(n) \
0078 { \
0079     int t; \
0080     for(t = 0; t < n && !DSP56K_TRANSMIT; t++) \
0081         msleep(10); \
0082     if(!DSP56K_TRANSMIT) { \
0083         return -EIO; \
0084     } \
0085 }
0086 
0087 #define rx_wait(n) \
0088 { \
0089     int t; \
0090     for(t = 0; t < n && !DSP56K_RECEIVE; t++) \
0091         msleep(10); \
0092     if(!DSP56K_RECEIVE) { \
0093         return -EIO; \
0094     } \
0095 }
0096 
0097 static DEFINE_MUTEX(dsp56k_mutex);
0098 static struct dsp56k_device {
0099     unsigned long in_use;
0100     long maxio, timeout;
0101     int tx_wsize, rx_wsize;
0102 } dsp56k;
0103 
0104 static struct class *dsp56k_class;
0105 
0106 static int dsp56k_reset(void)
0107 {
0108     u_char status;
0109     
0110     /* Power down the DSP */
0111     sound_ym.rd_data_reg_sel = 14;
0112     status = sound_ym.rd_data_reg_sel & 0xef;
0113     sound_ym.wd_data = status;
0114     sound_ym.wd_data = status | 0x10;
0115   
0116     udelay(10);
0117   
0118     /* Power up the DSP */
0119     sound_ym.rd_data_reg_sel = 14;
0120     sound_ym.wd_data = sound_ym.rd_data_reg_sel & 0xef;
0121 
0122     return 0;
0123 }
0124 
0125 static int dsp56k_upload(u_char __user *bin, int len)
0126 {
0127     struct platform_device *pdev;
0128     const struct firmware *fw;
0129     const char fw_name[] = "dsp56k/bootstrap.bin";
0130     int err;
0131     int i;
0132 
0133     dsp56k_reset();
0134 
0135     pdev = platform_device_register_simple("dsp56k", 0, NULL, 0);
0136     if (IS_ERR(pdev)) {
0137         printk(KERN_ERR "Failed to register device for \"%s\"\n",
0138                fw_name);
0139         return -EINVAL;
0140     }
0141     err = request_firmware(&fw, fw_name, &pdev->dev);
0142     platform_device_unregister(pdev);
0143     if (err) {
0144         printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
0145                fw_name, err);
0146         return err;
0147     }
0148     if (fw->size % 3) {
0149         printk(KERN_ERR "Bogus length %d in image \"%s\"\n",
0150                fw->size, fw_name);
0151         release_firmware(fw);
0152         return -EINVAL;
0153     }
0154     for (i = 0; i < fw->size; i = i + 3) {
0155         /* tx_wait(10); */
0156         dsp56k_host_interface.data.b[1] = fw->data[i];
0157         dsp56k_host_interface.data.b[2] = fw->data[i + 1];
0158         dsp56k_host_interface.data.b[3] = fw->data[i + 2];
0159     }
0160     release_firmware(fw);
0161     for (; i < 512; i++) {
0162         /* tx_wait(10); */
0163         dsp56k_host_interface.data.b[1] = 0;
0164         dsp56k_host_interface.data.b[2] = 0;
0165         dsp56k_host_interface.data.b[3] = 0;
0166     }
0167   
0168     for (i = 0; i < len; i++) {
0169         tx_wait(10);
0170         get_user(dsp56k_host_interface.data.b[1], bin++);
0171         get_user(dsp56k_host_interface.data.b[2], bin++);
0172         get_user(dsp56k_host_interface.data.b[3], bin++);
0173     }
0174 
0175     tx_wait(10);
0176     dsp56k_host_interface.data.l = 3;    /* Magic execute */
0177 
0178     return 0;
0179 }
0180 
0181 static ssize_t dsp56k_read(struct file *file, char __user *buf, size_t count,
0182                loff_t *ppos)
0183 {
0184     struct inode *inode = file_inode(file);
0185     int dev = iminor(inode) & 0x0f;
0186 
0187     switch(dev)
0188     {
0189     case DSP56K_DEV_56001:
0190     {
0191 
0192         long n;
0193 
0194         /* Don't do anything if nothing is to be done */
0195         if (!count) return 0;
0196 
0197         n = 0;
0198         switch (dsp56k.rx_wsize) {
0199         case 1:  /* 8 bit */
0200         {
0201             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
0202                   put_user(dsp56k_host_interface.data.b[3], buf+n++));
0203             return n;
0204         }
0205         case 2:  /* 16 bit */
0206         {
0207             short __user *data;
0208 
0209             count /= 2;
0210             data = (short __user *) buf;
0211             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
0212                   put_user(dsp56k_host_interface.data.w[1], data+n++));
0213             return 2*n;
0214         }
0215         case 3:  /* 24 bit */
0216         {
0217             count /= 3;
0218             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
0219                   put_user(dsp56k_host_interface.data.b[1], buf+n++);
0220                   put_user(dsp56k_host_interface.data.b[2], buf+n++);
0221                   put_user(dsp56k_host_interface.data.b[3], buf+n++));
0222             return 3*n;
0223         }
0224         case 4:  /* 32 bit */
0225         {
0226             long __user *data;
0227 
0228             count /= 4;
0229             data = (long __user *) buf;
0230             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
0231                   put_user(dsp56k_host_interface.data.l, data+n++));
0232             return 4*n;
0233         }
0234         }
0235         return -EFAULT;
0236     }
0237 
0238     default:
0239         printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
0240         return -ENXIO;
0241     }
0242 }
0243 
0244 static ssize_t dsp56k_write(struct file *file, const char __user *buf, size_t count,
0245                 loff_t *ppos)
0246 {
0247     struct inode *inode = file_inode(file);
0248     int dev = iminor(inode) & 0x0f;
0249 
0250     switch(dev)
0251     {
0252     case DSP56K_DEV_56001:
0253     {
0254         long n;
0255 
0256         /* Don't do anything if nothing is to be done */
0257         if (!count) return 0;
0258 
0259         n = 0;
0260         switch (dsp56k.tx_wsize) {
0261         case 1:  /* 8 bit */
0262         {
0263             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
0264                   get_user(dsp56k_host_interface.data.b[3], buf+n++));
0265             return n;
0266         }
0267         case 2:  /* 16 bit */
0268         {
0269             const short __user *data;
0270 
0271             count /= 2;
0272             data = (const short __user *)buf;
0273             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
0274                   get_user(dsp56k_host_interface.data.w[1], data+n++));
0275             return 2*n;
0276         }
0277         case 3:  /* 24 bit */
0278         {
0279             count /= 3;
0280             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
0281                   get_user(dsp56k_host_interface.data.b[1], buf+n++);
0282                   get_user(dsp56k_host_interface.data.b[2], buf+n++);
0283                   get_user(dsp56k_host_interface.data.b[3], buf+n++));
0284             return 3*n;
0285         }
0286         case 4:  /* 32 bit */
0287         {
0288             const long __user *data;
0289 
0290             count /= 4;
0291             data = (const long __user *)buf;
0292             handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
0293                   get_user(dsp56k_host_interface.data.l, data+n++));
0294             return 4*n;
0295         }
0296         }
0297 
0298         return -EFAULT;
0299     }
0300     default:
0301         printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
0302         return -ENXIO;
0303     }
0304 }
0305 
0306 static long dsp56k_ioctl(struct file *file, unsigned int cmd,
0307              unsigned long arg)
0308 {
0309     int dev = iminor(file_inode(file)) & 0x0f;
0310     void __user *argp = (void __user *)arg;
0311 
0312     switch(dev)
0313     {
0314     case DSP56K_DEV_56001:
0315 
0316         switch(cmd) {
0317         case DSP56K_UPLOAD:
0318         {
0319             char __user *bin;
0320             int r, len;
0321             struct dsp56k_upload __user *binary = argp;
0322     
0323             if(get_user(len, &binary->len) < 0)
0324                 return -EFAULT;
0325             if(get_user(bin, &binary->bin) < 0)
0326                 return -EFAULT;
0327         
0328             if (len <= 0) {
0329                 return -EINVAL;      /* nothing to upload?!? */
0330             }
0331             if (len > DSP56K_MAX_BINARY_LENGTH) {
0332                 return -EINVAL;
0333             }
0334             mutex_lock(&dsp56k_mutex);
0335             r = dsp56k_upload(bin, len);
0336             mutex_unlock(&dsp56k_mutex);
0337             if (r < 0) {
0338                 return r;
0339             }
0340     
0341             break;
0342         }
0343         case DSP56K_SET_TX_WSIZE:
0344             if (arg > 4 || arg < 1)
0345                 return -EINVAL;
0346             mutex_lock(&dsp56k_mutex);
0347             dsp56k.tx_wsize = (int) arg;
0348             mutex_unlock(&dsp56k_mutex);
0349             break;
0350         case DSP56K_SET_RX_WSIZE:
0351             if (arg > 4 || arg < 1)
0352                 return -EINVAL;
0353             mutex_lock(&dsp56k_mutex);
0354             dsp56k.rx_wsize = (int) arg;
0355             mutex_unlock(&dsp56k_mutex);
0356             break;
0357         case DSP56K_HOST_FLAGS:
0358         {
0359             int dir, out, status;
0360             struct dsp56k_host_flags __user *hf = argp;
0361     
0362             if(get_user(dir, &hf->dir) < 0)
0363                 return -EFAULT;
0364             if(get_user(out, &hf->out) < 0)
0365                 return -EFAULT;
0366 
0367             mutex_lock(&dsp56k_mutex);
0368             if ((dir & 0x1) && (out & 0x1))
0369                 dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
0370             else if (dir & 0x1)
0371                 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
0372             if ((dir & 0x2) && (out & 0x2))
0373                 dsp56k_host_interface.icr |= DSP56K_ICR_HF1;
0374             else if (dir & 0x2)
0375                 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
0376 
0377             status = 0;
0378             if (dsp56k_host_interface.icr & DSP56K_ICR_HF0) status |= 0x1;
0379             if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
0380             if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
0381             if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
0382             mutex_unlock(&dsp56k_mutex);
0383             return put_user(status, &hf->status);
0384         }
0385         case DSP56K_HOST_CMD:
0386             if (arg > 31)
0387                 return -EINVAL;
0388             mutex_lock(&dsp56k_mutex);
0389             dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
0390                                  DSP56K_CVR_HC);
0391             mutex_unlock(&dsp56k_mutex);
0392             break;
0393         default:
0394             return -EINVAL;
0395         }
0396         return 0;
0397 
0398     default:
0399         printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
0400         return -ENXIO;
0401     }
0402 }
0403 
0404 /* As of 2.1.26 this should be dsp56k_poll,
0405  * but how do I then check device minor number?
0406  * Do I need this function at all???
0407  */
0408 #if 0
0409 static __poll_t dsp56k_poll(struct file *file, poll_table *wait)
0410 {
0411     int dev = iminor(file_inode(file)) & 0x0f;
0412 
0413     switch(dev)
0414     {
0415     case DSP56K_DEV_56001:
0416         /* poll_wait(file, ???, wait); */
0417         return EPOLLIN | EPOLLRDNORM | EPOLLOUT;
0418 
0419     default:
0420         printk("DSP56k driver: Unknown minor device: %d\n", dev);
0421         return 0;
0422     }
0423 }
0424 #endif
0425 
0426 static int dsp56k_open(struct inode *inode, struct file *file)
0427 {
0428     int dev = iminor(inode) & 0x0f;
0429     int ret = 0;
0430 
0431     mutex_lock(&dsp56k_mutex);
0432     switch(dev)
0433     {
0434     case DSP56K_DEV_56001:
0435 
0436         if (test_and_set_bit(0, &dsp56k.in_use)) {
0437             ret = -EBUSY;
0438             goto out;
0439         }
0440 
0441         dsp56k.timeout = TIMEOUT;
0442         dsp56k.maxio = MAXIO;
0443         dsp56k.rx_wsize = dsp56k.tx_wsize = 4; 
0444 
0445         DSP56K_TX_INT_OFF;
0446         DSP56K_RX_INT_OFF;
0447 
0448         /* Zero host flags */
0449         dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
0450         dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
0451 
0452         break;
0453 
0454     default:
0455         ret = -ENODEV;
0456     }
0457 out:
0458     mutex_unlock(&dsp56k_mutex);
0459     return ret;
0460 }
0461 
0462 static int dsp56k_release(struct inode *inode, struct file *file)
0463 {
0464     int dev = iminor(inode) & 0x0f;
0465 
0466     switch(dev)
0467     {
0468     case DSP56K_DEV_56001:
0469         clear_bit(0, &dsp56k.in_use);
0470         break;
0471     default:
0472         printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
0473         return -ENXIO;
0474     }
0475 
0476     return 0;
0477 }
0478 
0479 static const struct file_operations dsp56k_fops = {
0480     .owner      = THIS_MODULE,
0481     .read       = dsp56k_read,
0482     .write      = dsp56k_write,
0483     .unlocked_ioctl = dsp56k_ioctl,
0484     .open       = dsp56k_open,
0485     .release    = dsp56k_release,
0486     .llseek     = noop_llseek,
0487 };
0488 
0489 
0490 /****** Init and module functions ******/
0491 
0492 static const char banner[] __initconst = KERN_INFO "DSP56k driver installed\n";
0493 
0494 static int __init dsp56k_init_driver(void)
0495 {
0496     int err = 0;
0497 
0498     if(!MACH_IS_ATARI || !ATARIHW_PRESENT(DSP56K)) {
0499         printk("DSP56k driver: Hardware not present\n");
0500         return -ENODEV;
0501     }
0502 
0503     if(register_chrdev(DSP56K_MAJOR, "dsp56k", &dsp56k_fops)) {
0504         printk("DSP56k driver: Unable to register driver\n");
0505         return -ENODEV;
0506     }
0507     dsp56k_class = class_create(THIS_MODULE, "dsp56k");
0508     if (IS_ERR(dsp56k_class)) {
0509         err = PTR_ERR(dsp56k_class);
0510         goto out_chrdev;
0511     }
0512     device_create(dsp56k_class, NULL, MKDEV(DSP56K_MAJOR, 0), NULL,
0513               "dsp56k");
0514 
0515     printk(banner);
0516     goto out;
0517 
0518 out_chrdev:
0519     unregister_chrdev(DSP56K_MAJOR, "dsp56k");
0520 out:
0521     return err;
0522 }
0523 module_init(dsp56k_init_driver);
0524 
0525 static void __exit dsp56k_cleanup_driver(void)
0526 {
0527     device_destroy(dsp56k_class, MKDEV(DSP56K_MAJOR, 0));
0528     class_destroy(dsp56k_class);
0529     unregister_chrdev(DSP56K_MAJOR, "dsp56k");
0530 }
0531 module_exit(dsp56k_cleanup_driver);
0532 
0533 MODULE_LICENSE("GPL");
0534 MODULE_FIRMWARE("dsp56k/bootstrap.bin");