msnd.c 8.2 KB

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