msnd.c 8.3 KB

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