rtlx.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can distribute it and/or modify it
  5. * under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. * for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/init.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/list.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/elf.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/moduleloader.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/poll.h>
  32. #include <linux/sched.h>
  33. #include <linux/wait.h>
  34. #include <asm/mipsmtregs.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/atomic.h>
  37. #include <asm/cpu.h>
  38. #include <asm/processor.h>
  39. #include <asm/system.h>
  40. #include <asm/rtlx.h>
  41. #define RTLX_MAJOR 64
  42. #define RTLX_TARG_VPE 1
  43. struct rtlx_info *rtlx;
  44. static int major;
  45. static char module_name[] = "rtlx";
  46. static inline int spacefree(int read, int write, int size);
  47. static struct chan_waitqueues {
  48. wait_queue_head_t rt_queue;
  49. wait_queue_head_t lx_queue;
  50. } channel_wqs[RTLX_CHANNELS];
  51. static struct irqaction irq;
  52. static int irq_num;
  53. extern void *vpe_get_shared(int index);
  54. static void rtlx_dispatch(struct pt_regs *regs)
  55. {
  56. do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ, regs);
  57. }
  58. irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  59. {
  60. irqreturn_t r = IRQ_HANDLED;
  61. int i;
  62. for (i = 0; i < RTLX_CHANNELS; i++) {
  63. struct rtlx_channel *chan = &rtlx->channel[i];
  64. if (chan->lx_read != chan->lx_write)
  65. wake_up_interruptible(&channel_wqs[i].lx_queue);
  66. }
  67. return r;
  68. }
  69. void dump_rtlx(void)
  70. {
  71. int i;
  72. printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
  73. for (i = 0; i < RTLX_CHANNELS; i++) {
  74. struct rtlx_channel *chan = &rtlx->channel[i];
  75. printk(" rt_state %d lx_state %d buffer_size %d\n",
  76. chan->rt_state, chan->lx_state, chan->buffer_size);
  77. printk(" rt_read %d rt_write %d\n",
  78. chan->rt_read, chan->rt_write);
  79. printk(" lx_read %d lx_write %d\n",
  80. chan->lx_read, chan->lx_write);
  81. printk(" rt_buffer <%s>\n", chan->rt_buffer);
  82. printk(" lx_buffer <%s>\n", chan->lx_buffer);
  83. }
  84. }
  85. /* call when we have the address of the shared structure from the SP side. */
  86. static int rtlx_init(struct rtlx_info *rtlxi)
  87. {
  88. int i;
  89. if (rtlxi->id != RTLX_ID) {
  90. printk(KERN_WARNING "no valid RTLX id at 0x%p\n", rtlxi);
  91. return (-ENOEXEC);
  92. }
  93. /* initialise the wait queues */
  94. for (i = 0; i < RTLX_CHANNELS; i++) {
  95. init_waitqueue_head(&channel_wqs[i].rt_queue);
  96. init_waitqueue_head(&channel_wqs[i].lx_queue);
  97. }
  98. /* set up for interrupt handling */
  99. memset(&irq, 0, sizeof(struct irqaction));
  100. if (cpu_has_vint) {
  101. set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
  102. }
  103. irq_num = MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ;
  104. irq.handler = rtlx_interrupt;
  105. irq.flags = SA_INTERRUPT;
  106. irq.name = "RTLX";
  107. irq.dev_id = rtlx;
  108. setup_irq(irq_num, &irq);
  109. rtlx = rtlxi;
  110. return (0);
  111. }
  112. /* only allow one open process at a time to open each channel */
  113. static int rtlx_open(struct inode *inode, struct file *filp)
  114. {
  115. int minor, ret;
  116. struct rtlx_channel *chan;
  117. /* assume only 1 device at the mo. */
  118. minor = MINOR(inode->i_rdev);
  119. if (rtlx == NULL) {
  120. struct rtlx_info **p;
  121. if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) {
  122. printk(" vpe_get_shared is NULL. Has an SP program been loaded?\n");
  123. return (-EFAULT);
  124. }
  125. if (*p == NULL) {
  126. printk(" vpe_shared %p %p\n", p, *p);
  127. return (-EFAULT);
  128. }
  129. if ((ret = rtlx_init(*p)) < 0)
  130. return (ret);
  131. }
  132. chan = &rtlx->channel[minor];
  133. /* already open? */
  134. if (chan->lx_state == RTLX_STATE_OPENED)
  135. return (-EBUSY);
  136. chan->lx_state = RTLX_STATE_OPENED;
  137. return (0);
  138. }
  139. static int rtlx_release(struct inode *inode, struct file *filp)
  140. {
  141. int minor;
  142. minor = MINOR(inode->i_rdev);
  143. rtlx->channel[minor].lx_state = RTLX_STATE_UNUSED;
  144. return (0);
  145. }
  146. static unsigned int rtlx_poll(struct file *file, poll_table * wait)
  147. {
  148. int minor;
  149. unsigned int mask = 0;
  150. struct rtlx_channel *chan;
  151. minor = MINOR(file->f_dentry->d_inode->i_rdev);
  152. chan = &rtlx->channel[minor];
  153. poll_wait(file, &channel_wqs[minor].rt_queue, wait);
  154. poll_wait(file, &channel_wqs[minor].lx_queue, wait);
  155. /* data available to read? */
  156. if (chan->lx_read != chan->lx_write)
  157. mask |= POLLIN | POLLRDNORM;
  158. /* space to write */
  159. if (spacefree(chan->rt_read, chan->rt_write, chan->buffer_size))
  160. mask |= POLLOUT | POLLWRNORM;
  161. return (mask);
  162. }
  163. static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count,
  164. loff_t * ppos)
  165. {
  166. size_t fl = 0L;
  167. int minor;
  168. struct rtlx_channel *lx;
  169. DECLARE_WAITQUEUE(wait, current);
  170. minor = MINOR(file->f_dentry->d_inode->i_rdev);
  171. lx = &rtlx->channel[minor];
  172. /* data available? */
  173. if (lx->lx_write == lx->lx_read) {
  174. if (file->f_flags & O_NONBLOCK)
  175. return (0); // -EAGAIN makes cat whinge
  176. /* go to sleep */
  177. add_wait_queue(&channel_wqs[minor].lx_queue, &wait);
  178. set_current_state(TASK_INTERRUPTIBLE);
  179. while (lx->lx_write == lx->lx_read)
  180. schedule();
  181. set_current_state(TASK_RUNNING);
  182. remove_wait_queue(&channel_wqs[minor].lx_queue, &wait);
  183. /* back running */
  184. }
  185. /* find out how much in total */
  186. count = min( count,
  187. (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size);
  188. /* then how much from the read pointer onwards */
  189. fl = min( count, (size_t)lx->buffer_size - lx->lx_read);
  190. copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl);
  191. /* and if there is anything left at the beginning of the buffer */
  192. if ( count - fl )
  193. copy_to_user (buffer + fl, lx->lx_buffer, count - fl);
  194. /* update the index */
  195. lx->lx_read += count;
  196. lx->lx_read %= lx->buffer_size;
  197. return (count);
  198. }
  199. static inline int spacefree(int read, int write, int size)
  200. {
  201. if (read == write) {
  202. /* never fill the buffer completely, so indexes are always equal if empty
  203. and only empty, or !equal if data available */
  204. return (size - 1);
  205. }
  206. return ((read + size - write) % size) - 1;
  207. }
  208. static ssize_t rtlx_write(struct file *file, const char __user * buffer,
  209. size_t count, loff_t * ppos)
  210. {
  211. int minor;
  212. struct rtlx_channel *rt;
  213. size_t fl;
  214. DECLARE_WAITQUEUE(wait, current);
  215. minor = MINOR(file->f_dentry->d_inode->i_rdev);
  216. rt = &rtlx->channel[minor];
  217. /* any space left... */
  218. if (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) {
  219. if (file->f_flags & O_NONBLOCK)
  220. return (-EAGAIN);
  221. add_wait_queue(&channel_wqs[minor].rt_queue, &wait);
  222. set_current_state(TASK_INTERRUPTIBLE);
  223. while (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size))
  224. schedule();
  225. set_current_state(TASK_RUNNING);
  226. remove_wait_queue(&channel_wqs[minor].rt_queue, &wait);
  227. }
  228. /* total number of bytes to copy */
  229. count = min( count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) );
  230. /* first bit from write pointer to the end of the buffer, or count */
  231. fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
  232. copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl);
  233. /* if there's any left copy to the beginning of the buffer */
  234. if( count - fl )
  235. copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
  236. rt->rt_write += count;
  237. rt->rt_write %= rt->buffer_size;
  238. return(count);
  239. }
  240. static struct file_operations rtlx_fops = {
  241. .owner = THIS_MODULE,
  242. .open = rtlx_open,
  243. .release = rtlx_release,
  244. .write = rtlx_write,
  245. .read = rtlx_read,
  246. .poll = rtlx_poll
  247. };
  248. static int rtlx_module_init(void)
  249. {
  250. if ((major = register_chrdev(RTLX_MAJOR, module_name, &rtlx_fops)) < 0) {
  251. printk("rtlx_module_init: unable to register device\n");
  252. return (-EBUSY);
  253. }
  254. if (major == 0)
  255. major = RTLX_MAJOR;
  256. return (0);
  257. }
  258. static void rtlx_module_exit(void)
  259. {
  260. unregister_chrdev(major, module_name);
  261. }
  262. module_init(rtlx_module_init);
  263. module_exit(rtlx_module_exit);
  264. MODULE_DESCRIPTION("MIPS RTLX");
  265. MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc");
  266. MODULE_LICENSE("GPL");