msnd.c 8.3 KB

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