msnd.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*********************************************************************
  2. *
  3. * msnd.c - Driver Base
  4. *
  5. * Turtle Beach MultiSound Sound Card Driver for Linux
  6. *
  7. * Copyright (C) 1998 Andrew Veliath
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * $Id: msnd.c,v 1.17 1999/03/21 16:50:09 andrewtv Exp $
  24. *
  25. ********************************************************************/
  26. #include <linux/version.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/slab.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/types.h>
  32. #include <linux/delay.h>
  33. #include <linux/mm.h>
  34. #include <linux/init.h>
  35. #include <linux/interrupt.h>
  36. #include <asm/io.h>
  37. #include <asm/uaccess.h>
  38. #include <linux/spinlock.h>
  39. #include <asm/irq.h>
  40. #include "msnd.h"
  41. #define LOGNAME "msnd"
  42. #define MSND_MAX_DEVS 4
  43. static multisound_dev_t *devs[MSND_MAX_DEVS];
  44. static int num_devs;
  45. int __init msnd_register(multisound_dev_t *dev)
  46. {
  47. int i;
  48. for (i = 0; i < MSND_MAX_DEVS; ++i)
  49. if (devs[i] == NULL)
  50. break;
  51. if (i == MSND_MAX_DEVS)
  52. return -ENOMEM;
  53. devs[i] = dev;
  54. ++num_devs;
  55. return 0;
  56. }
  57. void msnd_unregister(multisound_dev_t *dev)
  58. {
  59. int i;
  60. for (i = 0; i < MSND_MAX_DEVS; ++i)
  61. if (devs[i] == dev)
  62. break;
  63. if (i == MSND_MAX_DEVS) {
  64. printk(KERN_WARNING LOGNAME ": Unregistering unknown device\n");
  65. return;
  66. }
  67. devs[i] = NULL;
  68. --num_devs;
  69. }
  70. void msnd_init_queue(void __iomem *base, int start, int size)
  71. {
  72. writew(PCTODSP_BASED(start), base + JQS_wStart);
  73. writew(PCTODSP_OFFSET(size) - 1, base + JQS_wSize);
  74. writew(0, base + JQS_wHead);
  75. writew(0, base + JQS_wTail);
  76. }
  77. void msnd_fifo_init(msnd_fifo *f)
  78. {
  79. f->data = NULL;
  80. }
  81. void msnd_fifo_free(msnd_fifo *f)
  82. {
  83. if (f->data) {
  84. vfree(f->data);
  85. f->data = NULL;
  86. }
  87. }
  88. int msnd_fifo_alloc(msnd_fifo *f, size_t n)
  89. {
  90. msnd_fifo_free(f);
  91. f->data = (char *)vmalloc(n);
  92. f->n = n;
  93. f->tail = 0;
  94. f->head = 0;
  95. f->len = 0;
  96. if (!f->data)
  97. return -ENOMEM;
  98. return 0;
  99. }
  100. void msnd_fifo_make_empty(msnd_fifo *f)
  101. {
  102. f->len = f->tail = f->head = 0;
  103. }
  104. int msnd_fifo_write_io(msnd_fifo *f, char __iomem *buf, size_t len)
  105. {
  106. int count = 0;
  107. while ((count < len) && (f->len != f->n)) {
  108. int nwritten;
  109. if (f->head <= f->tail) {
  110. nwritten = len - count;
  111. if (nwritten > f->n - f->tail)
  112. nwritten = f->n - f->tail;
  113. }
  114. else {
  115. nwritten = f->head - f->tail;
  116. if (nwritten > len - count)
  117. nwritten = len - count;
  118. }
  119. memcpy_fromio(f->data + f->tail, buf, nwritten);
  120. count += nwritten;
  121. buf += nwritten;
  122. f->len += nwritten;
  123. f->tail += nwritten;
  124. f->tail %= f->n;
  125. }
  126. return count;
  127. }
  128. int msnd_fifo_write(msnd_fifo *f, const char *buf, size_t len)
  129. {
  130. int count = 0;
  131. while ((count < len) && (f->len != f->n)) {
  132. int nwritten;
  133. if (f->head <= f->tail) {
  134. nwritten = len - count;
  135. if (nwritten > f->n - f->tail)
  136. nwritten = f->n - f->tail;
  137. }
  138. else {
  139. nwritten = f->head - f->tail;
  140. if (nwritten > len - count)
  141. nwritten = len - count;
  142. }
  143. memcpy(f->data + f->tail, buf, nwritten);
  144. count += nwritten;
  145. buf += nwritten;
  146. f->len += nwritten;
  147. f->tail += nwritten;
  148. f->tail %= f->n;
  149. }
  150. return count;
  151. }
  152. int msnd_fifo_read_io(msnd_fifo *f, char __iomem *buf, size_t len)
  153. {
  154. int count = 0;
  155. while ((count < len) && (f->len > 0)) {
  156. int nread;
  157. if (f->tail <= f->head) {
  158. nread = len - count;
  159. if (nread > f->n - f->head)
  160. nread = f->n - f->head;
  161. }
  162. else {
  163. nread = f->tail - f->head;
  164. if (nread > len - count)
  165. nread = len - count;
  166. }
  167. memcpy_toio(buf, f->data + f->head, nread);
  168. count += nread;
  169. buf += nread;
  170. f->len -= nread;
  171. f->head += nread;
  172. f->head %= f->n;
  173. }
  174. return count;
  175. }
  176. int msnd_fifo_read(msnd_fifo *f, char *buf, size_t len)
  177. {
  178. int count = 0;
  179. while ((count < len) && (f->len > 0)) {
  180. int nread;
  181. if (f->tail <= f->head) {
  182. nread = len - count;
  183. if (nread > f->n - f->head)
  184. nread = f->n - f->head;
  185. }
  186. else {
  187. nread = f->tail - f->head;
  188. if (nread > len - count)
  189. nread = len - count;
  190. }
  191. memcpy(buf, f->data + f->head, nread);
  192. count += nread;
  193. buf += nread;
  194. f->len -= nread;
  195. f->head += nread;
  196. f->head %= f->n;
  197. }
  198. return count;
  199. }
  200. static int msnd_wait_TXDE(multisound_dev_t *dev)
  201. {
  202. register unsigned int io = dev->io;
  203. register int timeout = 1000;
  204. while(timeout-- > 0)
  205. if (msnd_inb(io + HP_ISR) & HPISR_TXDE)
  206. return 0;
  207. return -EIO;
  208. }
  209. static int msnd_wait_HC0(multisound_dev_t *dev)
  210. {
  211. register unsigned int io = dev->io;
  212. register int timeout = 1000;
  213. while(timeout-- > 0)
  214. if (!(msnd_inb(io + HP_CVR) & HPCVR_HC))
  215. return 0;
  216. return -EIO;
  217. }
  218. int msnd_send_dsp_cmd(multisound_dev_t *dev, BYTE cmd)
  219. {
  220. unsigned long flags;
  221. spin_lock_irqsave(&dev->lock, flags);
  222. if (msnd_wait_HC0(dev) == 0) {
  223. msnd_outb(cmd, dev->io + HP_CVR);
  224. spin_unlock_irqrestore(&dev->lock, flags);
  225. return 0;
  226. }
  227. spin_unlock_irqrestore(&dev->lock, flags);
  228. printk(KERN_DEBUG LOGNAME ": Send DSP command timeout\n");
  229. return -EIO;
  230. }
  231. int msnd_send_word(multisound_dev_t *dev, unsigned char high,
  232. unsigned char mid, unsigned char low)
  233. {
  234. register unsigned int io = dev->io;
  235. if (msnd_wait_TXDE(dev) == 0) {
  236. msnd_outb(high, io + HP_TXH);
  237. msnd_outb(mid, io + HP_TXM);
  238. msnd_outb(low, io + HP_TXL);
  239. return 0;
  240. }
  241. printk(KERN_DEBUG LOGNAME ": Send host word timeout\n");
  242. return -EIO;
  243. }
  244. int msnd_upload_host(multisound_dev_t *dev, char *bin, int len)
  245. {
  246. int i;
  247. if (len % 3 != 0) {
  248. printk(KERN_WARNING LOGNAME ": Upload host data not multiple of 3!\n");
  249. return -EINVAL;
  250. }
  251. for (i = 0; i < len; i += 3)
  252. if (msnd_send_word(dev, bin[i], bin[i + 1], bin[i + 2]) != 0)
  253. return -EIO;
  254. msnd_inb(dev->io + HP_RXL);
  255. msnd_inb(dev->io + HP_CVR);
  256. return 0;
  257. }
  258. int msnd_enable_irq(multisound_dev_t *dev)
  259. {
  260. unsigned long flags;
  261. if (dev->irq_ref++)
  262. return 0;
  263. printk(KERN_DEBUG LOGNAME ": Enabling IRQ\n");
  264. spin_lock_irqsave(&dev->lock, flags);
  265. if (msnd_wait_TXDE(dev) == 0) {
  266. msnd_outb(msnd_inb(dev->io + HP_ICR) | HPICR_TREQ, dev->io + HP_ICR);
  267. if (dev->type == msndClassic)
  268. msnd_outb(dev->irqid, dev->io + HP_IRQM);
  269. msnd_outb(msnd_inb(dev->io + HP_ICR) & ~HPICR_TREQ, dev->io + HP_ICR);
  270. msnd_outb(msnd_inb(dev->io + HP_ICR) | HPICR_RREQ, dev->io + HP_ICR);
  271. enable_irq(dev->irq);
  272. msnd_init_queue(dev->DSPQ, dev->dspq_data_buff, dev->dspq_buff_size);
  273. spin_unlock_irqrestore(&dev->lock, flags);
  274. return 0;
  275. }
  276. spin_unlock_irqrestore(&dev->lock, flags);
  277. printk(KERN_DEBUG LOGNAME ": Enable IRQ failed\n");
  278. return -EIO;
  279. }
  280. int msnd_disable_irq(multisound_dev_t *dev)
  281. {
  282. unsigned long flags;
  283. if (--dev->irq_ref > 0)
  284. return 0;
  285. if (dev->irq_ref < 0)
  286. printk(KERN_DEBUG LOGNAME ": IRQ ref count is %d\n", dev->irq_ref);
  287. printk(KERN_DEBUG LOGNAME ": Disabling IRQ\n");
  288. spin_lock_irqsave(&dev->lock, flags);
  289. if (msnd_wait_TXDE(dev) == 0) {
  290. msnd_outb(msnd_inb(dev->io + HP_ICR) & ~HPICR_RREQ, dev->io + HP_ICR);
  291. if (dev->type == msndClassic)
  292. msnd_outb(HPIRQ_NONE, dev->io + HP_IRQM);
  293. disable_irq(dev->irq);
  294. spin_unlock_irqrestore(&dev->lock, flags);
  295. return 0;
  296. }
  297. spin_unlock_irqrestore(&dev->lock, flags);
  298. printk(KERN_DEBUG LOGNAME ": Disable IRQ failed\n");
  299. return -EIO;
  300. }
  301. #ifndef LINUX20
  302. EXPORT_SYMBOL(msnd_register);
  303. EXPORT_SYMBOL(msnd_unregister);
  304. EXPORT_SYMBOL(msnd_init_queue);
  305. EXPORT_SYMBOL(msnd_fifo_init);
  306. EXPORT_SYMBOL(msnd_fifo_free);
  307. EXPORT_SYMBOL(msnd_fifo_alloc);
  308. EXPORT_SYMBOL(msnd_fifo_make_empty);
  309. EXPORT_SYMBOL(msnd_fifo_write_io);
  310. EXPORT_SYMBOL(msnd_fifo_read_io);
  311. EXPORT_SYMBOL(msnd_fifo_write);
  312. EXPORT_SYMBOL(msnd_fifo_read);
  313. EXPORT_SYMBOL(msnd_send_dsp_cmd);
  314. EXPORT_SYMBOL(msnd_send_word);
  315. EXPORT_SYMBOL(msnd_upload_host);
  316. EXPORT_SYMBOL(msnd_enable_irq);
  317. EXPORT_SYMBOL(msnd_disable_irq);
  318. #endif
  319. #ifdef MODULE
  320. MODULE_AUTHOR ("Andrew Veliath <andrewtv@usa.net>");
  321. MODULE_DESCRIPTION ("Turtle Beach MultiSound Driver Base");
  322. MODULE_LICENSE("GPL");
  323. int init_module(void)
  324. {
  325. return 0;
  326. }
  327. void cleanup_module(void)
  328. {
  329. }
  330. #endif