btaudio.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. btaudio - bt878 audio dma driver for linux 2.4.x
  3. (c) 2000-2002 Gerd Knorr <kraxel@bytesex.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/pci.h>
  19. #include <linux/sched.h>
  20. #include <linux/signal.h>
  21. #include <linux/types.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/poll.h>
  25. #include <linux/sound.h>
  26. #include <linux/soundcard.h>
  27. #include <linux/slab.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/mutex.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/io.h>
  32. /* mmio access */
  33. #define btwrite(dat,adr) writel((dat), (bta->mmio+(adr)))
  34. #define btread(adr) readl(bta->mmio+(adr))
  35. #define btand(dat,adr) btwrite((dat) & btread(adr), adr)
  36. #define btor(dat,adr) btwrite((dat) | btread(adr), adr)
  37. #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
  38. /* registers (shifted because bta->mmio is long) */
  39. #define REG_INT_STAT (0x100 >> 2)
  40. #define REG_INT_MASK (0x104 >> 2)
  41. #define REG_GPIO_DMA_CTL (0x10c >> 2)
  42. #define REG_PACKET_LEN (0x110 >> 2)
  43. #define REG_RISC_STRT_ADD (0x114 >> 2)
  44. #define REG_RISC_COUNT (0x120 >> 2)
  45. /* IRQ bits - REG_INT_(STAT|MASK) */
  46. #define IRQ_SCERR (1 << 19)
  47. #define IRQ_OCERR (1 << 18)
  48. #define IRQ_PABORT (1 << 17)
  49. #define IRQ_RIPERR (1 << 16)
  50. #define IRQ_PPERR (1 << 15)
  51. #define IRQ_FDSR (1 << 14)
  52. #define IRQ_FTRGT (1 << 13)
  53. #define IRQ_FBUS (1 << 12)
  54. #define IRQ_RISCI (1 << 11)
  55. #define IRQ_OFLOW (1 << 3)
  56. #define IRQ_BTAUDIO (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\
  57. IRQ_PPERR | IRQ_FDSR | IRQ_FTRGT | IRQ_FBUS |\
  58. IRQ_RISCI)
  59. /* REG_GPIO_DMA_CTL bits */
  60. #define DMA_CTL_A_PWRDN (1 << 26)
  61. #define DMA_CTL_DA_SBR (1 << 14)
  62. #define DMA_CTL_DA_ES2 (1 << 13)
  63. #define DMA_CTL_ACAP_EN (1 << 4)
  64. #define DMA_CTL_RISC_EN (1 << 1)
  65. #define DMA_CTL_FIFO_EN (1 << 0)
  66. /* RISC instructions */
  67. #define RISC_WRITE (0x01 << 28)
  68. #define RISC_JUMP (0x07 << 28)
  69. #define RISC_SYNC (0x08 << 28)
  70. /* RISC bits */
  71. #define RISC_WR_SOL (1 << 27)
  72. #define RISC_WR_EOL (1 << 26)
  73. #define RISC_IRQ (1 << 24)
  74. #define RISC_SYNC_RESYNC (1 << 15)
  75. #define RISC_SYNC_FM1 0x06
  76. #define RISC_SYNC_VRO 0x0c
  77. #define HWBASE_AD (448000)
  78. /* -------------------------------------------------------------- */
  79. struct btaudio {
  80. /* linked list */
  81. struct btaudio *next;
  82. /* device info */
  83. int dsp_digital;
  84. int dsp_analog;
  85. int mixer_dev;
  86. struct pci_dev *pci;
  87. unsigned int irq;
  88. unsigned long mem;
  89. unsigned long __iomem *mmio;
  90. /* locking */
  91. int users;
  92. struct mutex lock;
  93. /* risc instructions */
  94. unsigned int risc_size;
  95. unsigned long *risc_cpu;
  96. dma_addr_t risc_dma;
  97. /* audio data */
  98. unsigned int buf_size;
  99. unsigned char *buf_cpu;
  100. dma_addr_t buf_dma;
  101. /* buffer setup */
  102. int line_bytes;
  103. int line_count;
  104. int block_bytes;
  105. int block_count;
  106. /* read fifo management */
  107. int recording;
  108. int dma_block;
  109. int read_offset;
  110. int read_count;
  111. wait_queue_head_t readq;
  112. /* settings */
  113. int gain[3];
  114. int source;
  115. int bits;
  116. int decimation;
  117. int mixcount;
  118. int sampleshift;
  119. int channels;
  120. int analog;
  121. int rate;
  122. };
  123. struct cardinfo {
  124. char *name;
  125. int rate;
  126. };
  127. static struct btaudio *btaudios;
  128. static unsigned int debug;
  129. static unsigned int irq_debug;
  130. /* -------------------------------------------------------------- */
  131. #define BUF_DEFAULT 128*1024
  132. #define BUF_MIN 8192
  133. static int alloc_buffer(struct btaudio *bta)
  134. {
  135. if (NULL == bta->buf_cpu) {
  136. for (bta->buf_size = BUF_DEFAULT; bta->buf_size >= BUF_MIN;
  137. bta->buf_size = bta->buf_size >> 1) {
  138. bta->buf_cpu = pci_alloc_consistent
  139. (bta->pci, bta->buf_size, &bta->buf_dma);
  140. if (NULL != bta->buf_cpu)
  141. break;
  142. }
  143. if (NULL == bta->buf_cpu)
  144. return -ENOMEM;
  145. memset(bta->buf_cpu,0,bta->buf_size);
  146. }
  147. if (NULL == bta->risc_cpu) {
  148. bta->risc_size = PAGE_SIZE;
  149. bta->risc_cpu = pci_alloc_consistent
  150. (bta->pci, bta->risc_size, &bta->risc_dma);
  151. if (NULL == bta->risc_cpu) {
  152. pci_free_consistent(bta->pci, bta->buf_size, bta->buf_cpu, bta->buf_dma);
  153. bta->buf_cpu = NULL;
  154. return -ENOMEM;
  155. }
  156. }
  157. return 0;
  158. }
  159. static void free_buffer(struct btaudio *bta)
  160. {
  161. if (NULL != bta->buf_cpu) {
  162. pci_free_consistent(bta->pci, bta->buf_size,
  163. bta->buf_cpu, bta->buf_dma);
  164. bta->buf_cpu = NULL;
  165. }
  166. if (NULL != bta->risc_cpu) {
  167. pci_free_consistent(bta->pci, bta->risc_size,
  168. bta->risc_cpu, bta->risc_dma);
  169. bta->risc_cpu = NULL;
  170. }
  171. }
  172. static int make_risc(struct btaudio *bta)
  173. {
  174. int rp, bp, line, block;
  175. unsigned long risc;
  176. bta->block_bytes = bta->buf_size >> 4;
  177. bta->block_count = 1 << 4;
  178. bta->line_bytes = bta->block_bytes;
  179. bta->line_count = bta->block_count;
  180. while (bta->line_bytes > 4095) {
  181. bta->line_bytes >>= 1;
  182. bta->line_count <<= 1;
  183. }
  184. if (bta->line_count > 255)
  185. return -EINVAL;
  186. if (debug)
  187. printk(KERN_DEBUG
  188. "btaudio: bufsize=%d - bs=%d bc=%d - ls=%d, lc=%d\n",
  189. bta->buf_size,bta->block_bytes,bta->block_count,
  190. bta->line_bytes,bta->line_count);
  191. rp = 0; bp = 0;
  192. block = 0;
  193. bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_FM1);
  194. bta->risc_cpu[rp++] = cpu_to_le32(0);
  195. for (line = 0; line < bta->line_count; line++) {
  196. risc = RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL;
  197. risc |= bta->line_bytes;
  198. if (0 == (bp & (bta->block_bytes-1))) {
  199. risc |= RISC_IRQ;
  200. risc |= (block & 0x0f) << 16;
  201. risc |= (~block & 0x0f) << 20;
  202. block++;
  203. }
  204. bta->risc_cpu[rp++] = cpu_to_le32(risc);
  205. bta->risc_cpu[rp++] = cpu_to_le32(bta->buf_dma + bp);
  206. bp += bta->line_bytes;
  207. }
  208. bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_VRO);
  209. bta->risc_cpu[rp++] = cpu_to_le32(0);
  210. bta->risc_cpu[rp++] = cpu_to_le32(RISC_JUMP);
  211. bta->risc_cpu[rp++] = cpu_to_le32(bta->risc_dma);
  212. return 0;
  213. }
  214. static int start_recording(struct btaudio *bta)
  215. {
  216. int ret;
  217. if (0 != (ret = alloc_buffer(bta)))
  218. return ret;
  219. if (0 != (ret = make_risc(bta)))
  220. return ret;
  221. btwrite(bta->risc_dma, REG_RISC_STRT_ADD);
  222. btwrite((bta->line_count << 16) | bta->line_bytes,
  223. REG_PACKET_LEN);
  224. btwrite(IRQ_BTAUDIO, REG_INT_MASK);
  225. if (bta->analog) {
  226. btwrite(DMA_CTL_ACAP_EN |
  227. DMA_CTL_RISC_EN |
  228. DMA_CTL_FIFO_EN |
  229. DMA_CTL_DA_ES2 |
  230. ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
  231. (bta->gain[bta->source] << 28) |
  232. (bta->source << 24) |
  233. (bta->decimation << 8),
  234. REG_GPIO_DMA_CTL);
  235. } else {
  236. btwrite(DMA_CTL_ACAP_EN |
  237. DMA_CTL_RISC_EN |
  238. DMA_CTL_FIFO_EN |
  239. DMA_CTL_DA_ES2 |
  240. DMA_CTL_A_PWRDN |
  241. (1 << 6) |
  242. ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
  243. (bta->gain[bta->source] << 28) |
  244. (bta->source << 24) |
  245. (bta->decimation << 8),
  246. REG_GPIO_DMA_CTL);
  247. }
  248. bta->dma_block = 0;
  249. bta->read_offset = 0;
  250. bta->read_count = 0;
  251. bta->recording = 1;
  252. if (debug)
  253. printk(KERN_DEBUG "btaudio: recording started\n");
  254. return 0;
  255. }
  256. static void stop_recording(struct btaudio *bta)
  257. {
  258. btand(~15, REG_GPIO_DMA_CTL);
  259. bta->recording = 0;
  260. if (debug)
  261. printk(KERN_DEBUG "btaudio: recording stopped\n");
  262. }
  263. /* -------------------------------------------------------------- */
  264. static int btaudio_mixer_open(struct inode *inode, struct file *file)
  265. {
  266. int minor = iminor(inode);
  267. struct btaudio *bta;
  268. for (bta = btaudios; bta != NULL; bta = bta->next)
  269. if (bta->mixer_dev == minor)
  270. break;
  271. if (NULL == bta)
  272. return -ENODEV;
  273. if (debug)
  274. printk("btaudio: open mixer [%d]\n",minor);
  275. file->private_data = bta;
  276. return 0;
  277. }
  278. static int btaudio_mixer_release(struct inode *inode, struct file *file)
  279. {
  280. return 0;
  281. }
  282. static int btaudio_mixer_ioctl(struct inode *inode, struct file *file,
  283. unsigned int cmd, unsigned long arg)
  284. {
  285. struct btaudio *bta = file->private_data;
  286. int ret,val=0,i=0;
  287. void __user *argp = (void __user *)arg;
  288. if (cmd == SOUND_MIXER_INFO) {
  289. mixer_info info;
  290. memset(&info,0,sizeof(info));
  291. strlcpy(info.id,"bt878",sizeof(info.id));
  292. strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
  293. info.modify_counter = bta->mixcount;
  294. if (copy_to_user(argp, &info, sizeof(info)))
  295. return -EFAULT;
  296. return 0;
  297. }
  298. if (cmd == SOUND_OLD_MIXER_INFO) {
  299. _old_mixer_info info;
  300. memset(&info,0,sizeof(info));
  301. strlcpy(info.id,"bt878",sizeof(info.id)-1);
  302. strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
  303. if (copy_to_user(argp, &info, sizeof(info)))
  304. return -EFAULT;
  305. return 0;
  306. }
  307. if (cmd == OSS_GETVERSION)
  308. return put_user(SOUND_VERSION, (int __user *)argp);
  309. /* read */
  310. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  311. if (get_user(val, (int __user *)argp))
  312. return -EFAULT;
  313. switch (cmd) {
  314. case MIXER_READ(SOUND_MIXER_CAPS):
  315. ret = SOUND_CAP_EXCL_INPUT;
  316. break;
  317. case MIXER_READ(SOUND_MIXER_STEREODEVS):
  318. ret = 0;
  319. break;
  320. case MIXER_READ(SOUND_MIXER_RECMASK):
  321. case MIXER_READ(SOUND_MIXER_DEVMASK):
  322. ret = SOUND_MASK_LINE1|SOUND_MASK_LINE2|SOUND_MASK_LINE3;
  323. break;
  324. case MIXER_WRITE(SOUND_MIXER_RECSRC):
  325. if (val & SOUND_MASK_LINE1 && bta->source != 0)
  326. bta->source = 0;
  327. else if (val & SOUND_MASK_LINE2 && bta->source != 1)
  328. bta->source = 1;
  329. else if (val & SOUND_MASK_LINE3 && bta->source != 2)
  330. bta->source = 2;
  331. btaor((bta->gain[bta->source] << 28) |
  332. (bta->source << 24),
  333. 0x0cffffff, REG_GPIO_DMA_CTL);
  334. case MIXER_READ(SOUND_MIXER_RECSRC):
  335. switch (bta->source) {
  336. case 0: ret = SOUND_MASK_LINE1; break;
  337. case 1: ret = SOUND_MASK_LINE2; break;
  338. case 2: ret = SOUND_MASK_LINE3; break;
  339. default: ret = 0;
  340. }
  341. break;
  342. case MIXER_WRITE(SOUND_MIXER_LINE1):
  343. case MIXER_WRITE(SOUND_MIXER_LINE2):
  344. case MIXER_WRITE(SOUND_MIXER_LINE3):
  345. if (MIXER_WRITE(SOUND_MIXER_LINE1) == cmd)
  346. i = 0;
  347. if (MIXER_WRITE(SOUND_MIXER_LINE2) == cmd)
  348. i = 1;
  349. if (MIXER_WRITE(SOUND_MIXER_LINE3) == cmd)
  350. i = 2;
  351. bta->gain[i] = (val & 0xff) * 15 / 100;
  352. if (bta->gain[i] > 15) bta->gain[i] = 15;
  353. if (bta->gain[i] < 0) bta->gain[i] = 0;
  354. if (i == bta->source)
  355. btaor((bta->gain[bta->source]<<28),
  356. 0x0fffffff, REG_GPIO_DMA_CTL);
  357. ret = bta->gain[i] * 100 / 15;
  358. ret |= ret << 8;
  359. break;
  360. case MIXER_READ(SOUND_MIXER_LINE1):
  361. case MIXER_READ(SOUND_MIXER_LINE2):
  362. case MIXER_READ(SOUND_MIXER_LINE3):
  363. if (MIXER_READ(SOUND_MIXER_LINE1) == cmd)
  364. i = 0;
  365. if (MIXER_READ(SOUND_MIXER_LINE2) == cmd)
  366. i = 1;
  367. if (MIXER_READ(SOUND_MIXER_LINE3) == cmd)
  368. i = 2;
  369. ret = bta->gain[i] * 100 / 15;
  370. ret |= ret << 8;
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. if (put_user(ret, (int __user *)argp))
  376. return -EFAULT;
  377. return 0;
  378. }
  379. static struct file_operations btaudio_mixer_fops = {
  380. .owner = THIS_MODULE,
  381. .llseek = no_llseek,
  382. .open = btaudio_mixer_open,
  383. .release = btaudio_mixer_release,
  384. .ioctl = btaudio_mixer_ioctl,
  385. };
  386. /* -------------------------------------------------------------- */
  387. static int btaudio_dsp_open(struct inode *inode, struct file *file,
  388. struct btaudio *bta, int analog)
  389. {
  390. mutex_lock(&bta->lock);
  391. if (bta->users)
  392. goto busy;
  393. bta->users++;
  394. file->private_data = bta;
  395. bta->analog = analog;
  396. bta->dma_block = 0;
  397. bta->read_offset = 0;
  398. bta->read_count = 0;
  399. bta->sampleshift = 0;
  400. mutex_unlock(&bta->lock);
  401. return 0;
  402. busy:
  403. mutex_unlock(&bta->lock);
  404. return -EBUSY;
  405. }
  406. static int btaudio_dsp_open_digital(struct inode *inode, struct file *file)
  407. {
  408. int minor = iminor(inode);
  409. struct btaudio *bta;
  410. for (bta = btaudios; bta != NULL; bta = bta->next)
  411. if (bta->dsp_digital == minor)
  412. break;
  413. if (NULL == bta)
  414. return -ENODEV;
  415. if (debug)
  416. printk("btaudio: open digital dsp [%d]\n",minor);
  417. return btaudio_dsp_open(inode,file,bta,0);
  418. }
  419. static int btaudio_dsp_open_analog(struct inode *inode, struct file *file)
  420. {
  421. int minor = iminor(inode);
  422. struct btaudio *bta;
  423. for (bta = btaudios; bta != NULL; bta = bta->next)
  424. if (bta->dsp_analog == minor)
  425. break;
  426. if (NULL == bta)
  427. return -ENODEV;
  428. if (debug)
  429. printk("btaudio: open analog dsp [%d]\n",minor);
  430. return btaudio_dsp_open(inode,file,bta,1);
  431. }
  432. static int btaudio_dsp_release(struct inode *inode, struct file *file)
  433. {
  434. struct btaudio *bta = file->private_data;
  435. mutex_lock(&bta->lock);
  436. if (bta->recording)
  437. stop_recording(bta);
  438. bta->users--;
  439. mutex_unlock(&bta->lock);
  440. return 0;
  441. }
  442. static ssize_t btaudio_dsp_read(struct file *file, char __user *buffer,
  443. size_t swcount, loff_t *ppos)
  444. {
  445. struct btaudio *bta = file->private_data;
  446. int hwcount = swcount << bta->sampleshift;
  447. int nsrc, ndst, err, ret = 0;
  448. DECLARE_WAITQUEUE(wait, current);
  449. add_wait_queue(&bta->readq, &wait);
  450. mutex_lock(&bta->lock);
  451. while (swcount > 0) {
  452. if (0 == bta->read_count) {
  453. if (!bta->recording) {
  454. if (0 != (err = start_recording(bta))) {
  455. if (0 == ret)
  456. ret = err;
  457. break;
  458. }
  459. }
  460. if (file->f_flags & O_NONBLOCK) {
  461. if (0 == ret)
  462. ret = -EAGAIN;
  463. break;
  464. }
  465. mutex_unlock(&bta->lock);
  466. current->state = TASK_INTERRUPTIBLE;
  467. schedule();
  468. mutex_lock(&bta->lock);
  469. if(signal_pending(current)) {
  470. if (0 == ret)
  471. ret = -EINTR;
  472. break;
  473. }
  474. }
  475. nsrc = (bta->read_count < hwcount) ? bta->read_count : hwcount;
  476. if (nsrc > bta->buf_size - bta->read_offset)
  477. nsrc = bta->buf_size - bta->read_offset;
  478. ndst = nsrc >> bta->sampleshift;
  479. if ((bta->analog && 0 == bta->sampleshift) ||
  480. (!bta->analog && 2 == bta->channels)) {
  481. /* just copy */
  482. if (copy_to_user(buffer + ret, bta->buf_cpu + bta->read_offset, nsrc)) {
  483. if (0 == ret)
  484. ret = -EFAULT;
  485. break;
  486. }
  487. } else if (!bta->analog) {
  488. /* stereo => mono (digital audio) */
  489. __s16 *src = (__s16*)(bta->buf_cpu + bta->read_offset);
  490. __s16 __user *dst = (__s16 __user *)(buffer + ret);
  491. __s16 avg;
  492. int n = ndst>>1;
  493. if (!access_ok(VERIFY_WRITE, dst, ndst)) {
  494. if (0 == ret)
  495. ret = -EFAULT;
  496. break;
  497. }
  498. for (; n; n--, dst++) {
  499. avg = (__s16)le16_to_cpu(*src) / 2; src++;
  500. avg += (__s16)le16_to_cpu(*src) / 2; src++;
  501. __put_user(cpu_to_le16(avg),dst);
  502. }
  503. } else if (8 == bta->bits) {
  504. /* copy + byte downsampling (audio A/D) */
  505. __u8 *src = bta->buf_cpu + bta->read_offset;
  506. __u8 __user *dst = buffer + ret;
  507. int n = ndst;
  508. if (!access_ok(VERIFY_WRITE, dst, ndst)) {
  509. if (0 == ret)
  510. ret = -EFAULT;
  511. break;
  512. }
  513. for (; n; n--, src += (1 << bta->sampleshift), dst++)
  514. __put_user(*src, dst);
  515. } else {
  516. /* copy + word downsampling (audio A/D) */
  517. __u16 *src = (__u16*)(bta->buf_cpu + bta->read_offset);
  518. __u16 __user *dst = (__u16 __user *)(buffer + ret);
  519. int n = ndst>>1;
  520. if (!access_ok(VERIFY_WRITE,dst,ndst)) {
  521. if (0 == ret)
  522. ret = -EFAULT;
  523. break;
  524. }
  525. for (; n; n--, src += (1 << bta->sampleshift), dst++)
  526. __put_user(*src, dst);
  527. }
  528. ret += ndst;
  529. swcount -= ndst;
  530. hwcount -= nsrc;
  531. bta->read_count -= nsrc;
  532. bta->read_offset += nsrc;
  533. if (bta->read_offset == bta->buf_size)
  534. bta->read_offset = 0;
  535. }
  536. mutex_unlock(&bta->lock);
  537. remove_wait_queue(&bta->readq, &wait);
  538. current->state = TASK_RUNNING;
  539. return ret;
  540. }
  541. static ssize_t btaudio_dsp_write(struct file *file, const char __user *buffer,
  542. size_t count, loff_t *ppos)
  543. {
  544. return -EINVAL;
  545. }
  546. static int btaudio_dsp_ioctl(struct inode *inode, struct file *file,
  547. unsigned int cmd, unsigned long arg)
  548. {
  549. struct btaudio *bta = file->private_data;
  550. int s, i, ret, val = 0;
  551. void __user *argp = (void __user *)arg;
  552. int __user *p = argp;
  553. switch (cmd) {
  554. case OSS_GETVERSION:
  555. return put_user(SOUND_VERSION, p);
  556. case SNDCTL_DSP_GETCAPS:
  557. return 0;
  558. case SNDCTL_DSP_SPEED:
  559. if (get_user(val, p))
  560. return -EFAULT;
  561. if (bta->analog) {
  562. for (s = 0; s < 16; s++)
  563. if (val << s >= HWBASE_AD*4/15)
  564. break;
  565. for (i = 15; i >= 5; i--)
  566. if (val << s <= HWBASE_AD*4/i)
  567. break;
  568. bta->sampleshift = s;
  569. bta->decimation = i;
  570. if (debug)
  571. printk(KERN_DEBUG "btaudio: rate: req=%d "
  572. "dec=%d shift=%d hwrate=%d swrate=%d\n",
  573. val,i,s,(HWBASE_AD*4/i),(HWBASE_AD*4/i)>>s);
  574. } else {
  575. bta->sampleshift = (bta->channels == 2) ? 0 : 1;
  576. bta->decimation = 0;
  577. }
  578. if (bta->recording) {
  579. mutex_lock(&bta->lock);
  580. stop_recording(bta);
  581. start_recording(bta);
  582. mutex_unlock(&bta->lock);
  583. }
  584. /* fall through */
  585. case SOUND_PCM_READ_RATE:
  586. if (bta->analog) {
  587. return put_user(HWBASE_AD*4/bta->decimation>>bta->sampleshift, p);
  588. } else {
  589. return put_user(bta->rate, p);
  590. }
  591. case SNDCTL_DSP_STEREO:
  592. if (!bta->analog) {
  593. if (get_user(val, p))
  594. return -EFAULT;
  595. bta->channels = (val > 0) ? 2 : 1;
  596. bta->sampleshift = (bta->channels == 2) ? 0 : 1;
  597. if (debug)
  598. printk(KERN_INFO
  599. "btaudio: stereo=%d channels=%d\n",
  600. val,bta->channels);
  601. } else {
  602. if (val == 1)
  603. return -EFAULT;
  604. else {
  605. bta->channels = 1;
  606. if (debug)
  607. printk(KERN_INFO
  608. "btaudio: stereo=0 channels=1\n");
  609. }
  610. }
  611. return put_user((bta->channels)-1, p);
  612. case SNDCTL_DSP_CHANNELS:
  613. if (!bta->analog) {
  614. if (get_user(val, p))
  615. return -EFAULT;
  616. bta->channels = (val > 1) ? 2 : 1;
  617. bta->sampleshift = (bta->channels == 2) ? 0 : 1;
  618. if (debug)
  619. printk(KERN_DEBUG
  620. "btaudio: val=%d channels=%d\n",
  621. val,bta->channels);
  622. }
  623. /* fall through */
  624. case SOUND_PCM_READ_CHANNELS:
  625. return put_user(bta->channels, p);
  626. case SNDCTL_DSP_GETFMTS: /* Returns a mask */
  627. if (bta->analog)
  628. return put_user(AFMT_S16_LE|AFMT_S8, p);
  629. else
  630. return put_user(AFMT_S16_LE, p);
  631. case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
  632. if (get_user(val, p))
  633. return -EFAULT;
  634. if (val != AFMT_QUERY) {
  635. if (bta->analog)
  636. bta->bits = (val == AFMT_S8) ? 8 : 16;
  637. else
  638. bta->bits = 16;
  639. if (bta->recording) {
  640. mutex_lock(&bta->lock);
  641. stop_recording(bta);
  642. start_recording(bta);
  643. mutex_unlock(&bta->lock);
  644. }
  645. }
  646. if (debug)
  647. printk(KERN_DEBUG "btaudio: fmt: bits=%d\n",bta->bits);
  648. return put_user((bta->bits==16) ? AFMT_S16_LE : AFMT_S8,
  649. p);
  650. break;
  651. case SOUND_PCM_READ_BITS:
  652. return put_user(bta->bits, p);
  653. case SNDCTL_DSP_NONBLOCK:
  654. file->f_flags |= O_NONBLOCK;
  655. return 0;
  656. case SNDCTL_DSP_RESET:
  657. if (bta->recording) {
  658. mutex_lock(&bta->lock);
  659. stop_recording(bta);
  660. mutex_unlock(&bta->lock);
  661. }
  662. return 0;
  663. case SNDCTL_DSP_GETBLKSIZE:
  664. if (!bta->recording) {
  665. if (0 != (ret = alloc_buffer(bta)))
  666. return ret;
  667. if (0 != (ret = make_risc(bta)))
  668. return ret;
  669. }
  670. return put_user(bta->block_bytes>>bta->sampleshift,p);
  671. case SNDCTL_DSP_SYNC:
  672. /* NOP */
  673. return 0;
  674. case SNDCTL_DSP_GETISPACE:
  675. {
  676. audio_buf_info info;
  677. if (!bta->recording)
  678. return -EINVAL;
  679. info.fragsize = bta->block_bytes>>bta->sampleshift;
  680. info.fragstotal = bta->block_count;
  681. info.bytes = bta->read_count;
  682. info.fragments = info.bytes / info.fragsize;
  683. if (debug)
  684. printk(KERN_DEBUG "btaudio: SNDCTL_DSP_GETISPACE "
  685. "returns %d/%d/%d/%d\n",
  686. info.fragsize, info.fragstotal,
  687. info.bytes, info.fragments);
  688. if (copy_to_user(argp, &info, sizeof(info)))
  689. return -EFAULT;
  690. return 0;
  691. }
  692. #if 0 /* TODO */
  693. case SNDCTL_DSP_GETTRIGGER:
  694. case SNDCTL_DSP_SETTRIGGER:
  695. case SNDCTL_DSP_SETFRAGMENT:
  696. #endif
  697. default:
  698. return -EINVAL;
  699. }
  700. }
  701. static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait)
  702. {
  703. struct btaudio *bta = file->private_data;
  704. unsigned int mask = 0;
  705. poll_wait(file, &bta->readq, wait);
  706. if (0 != bta->read_count)
  707. mask |= (POLLIN | POLLRDNORM);
  708. return mask;
  709. }
  710. static struct file_operations btaudio_digital_dsp_fops = {
  711. .owner = THIS_MODULE,
  712. .llseek = no_llseek,
  713. .open = btaudio_dsp_open_digital,
  714. .release = btaudio_dsp_release,
  715. .read = btaudio_dsp_read,
  716. .write = btaudio_dsp_write,
  717. .ioctl = btaudio_dsp_ioctl,
  718. .poll = btaudio_dsp_poll,
  719. };
  720. static struct file_operations btaudio_analog_dsp_fops = {
  721. .owner = THIS_MODULE,
  722. .llseek = no_llseek,
  723. .open = btaudio_dsp_open_analog,
  724. .release = btaudio_dsp_release,
  725. .read = btaudio_dsp_read,
  726. .write = btaudio_dsp_write,
  727. .ioctl = btaudio_dsp_ioctl,
  728. .poll = btaudio_dsp_poll,
  729. };
  730. /* -------------------------------------------------------------- */
  731. static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "",
  732. "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
  733. "RIPERR", "PABORT", "OCERR", "SCERR" };
  734. static irqreturn_t btaudio_irq(int irq, void *dev_id, struct pt_regs * regs)
  735. {
  736. int count = 0;
  737. u32 stat,astat;
  738. struct btaudio *bta = dev_id;
  739. int handled = 0;
  740. for (;;) {
  741. count++;
  742. stat = btread(REG_INT_STAT);
  743. astat = stat & btread(REG_INT_MASK);
  744. if (!astat)
  745. return IRQ_RETVAL(handled);
  746. handled = 1;
  747. btwrite(astat,REG_INT_STAT);
  748. if (irq_debug) {
  749. int i;
  750. printk(KERN_DEBUG "btaudio: irq loop=%d risc=%x, bits:",
  751. count, stat>>28);
  752. for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) {
  753. if (stat & (1 << i))
  754. printk(" %s",irq_name[i]);
  755. if (astat & (1 << i))
  756. printk("*");
  757. }
  758. printk("\n");
  759. }
  760. if (stat & IRQ_RISCI) {
  761. int blocks;
  762. blocks = (stat >> 28) - bta->dma_block;
  763. if (blocks < 0)
  764. blocks += bta->block_count;
  765. bta->dma_block = stat >> 28;
  766. if (bta->read_count + 2*bta->block_bytes > bta->buf_size) {
  767. stop_recording(bta);
  768. printk(KERN_INFO "btaudio: buffer overrun\n");
  769. }
  770. if (blocks > 0) {
  771. bta->read_count += blocks * bta->block_bytes;
  772. wake_up_interruptible(&bta->readq);
  773. }
  774. }
  775. if (count > 10) {
  776. printk(KERN_WARNING
  777. "btaudio: Oops - irq mask cleared\n");
  778. btwrite(0, REG_INT_MASK);
  779. }
  780. }
  781. return IRQ_NONE;
  782. }
  783. /* -------------------------------------------------------------- */
  784. static unsigned int dsp1 = -1;
  785. static unsigned int dsp2 = -1;
  786. static unsigned int mixer = -1;
  787. static int latency = -1;
  788. static int digital = 1;
  789. static int analog = 1;
  790. static int rate;
  791. #define BTA_OSPREY200 1
  792. static struct cardinfo cards[] = {
  793. [0] = {
  794. .name = "default",
  795. .rate = 32000,
  796. },
  797. [BTA_OSPREY200] = {
  798. .name = "Osprey 200",
  799. .rate = 44100,
  800. },
  801. };
  802. static int __devinit btaudio_probe(struct pci_dev *pci_dev,
  803. const struct pci_device_id *pci_id)
  804. {
  805. struct btaudio *bta;
  806. struct cardinfo *card = &cards[pci_id->driver_data];
  807. unsigned char revision,lat;
  808. int rc = -EBUSY;
  809. if (pci_enable_device(pci_dev))
  810. return -EIO;
  811. if (!request_mem_region(pci_resource_start(pci_dev,0),
  812. pci_resource_len(pci_dev,0),
  813. "btaudio")) {
  814. return -EBUSY;
  815. }
  816. bta = kmalloc(sizeof(*bta),GFP_ATOMIC);
  817. if (!bta) {
  818. rc = -ENOMEM;
  819. goto fail0;
  820. }
  821. memset(bta,0,sizeof(*bta));
  822. bta->pci = pci_dev;
  823. bta->irq = pci_dev->irq;
  824. bta->mem = pci_resource_start(pci_dev,0);
  825. bta->mmio = ioremap(pci_resource_start(pci_dev,0),
  826. pci_resource_len(pci_dev,0));
  827. bta->source = 1;
  828. bta->bits = 8;
  829. bta->channels = 1;
  830. if (bta->analog) {
  831. bta->decimation = 15;
  832. } else {
  833. bta->decimation = 0;
  834. bta->sampleshift = 1;
  835. }
  836. /* sample rate */
  837. bta->rate = card->rate;
  838. if (rate)
  839. bta->rate = rate;
  840. mutex_init(&bta->lock);
  841. init_waitqueue_head(&bta->readq);
  842. if (-1 != latency) {
  843. printk(KERN_INFO "btaudio: setting pci latency timer to %d\n",
  844. latency);
  845. pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
  846. }
  847. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
  848. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat);
  849. printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ",
  850. pci_dev->device,revision,pci_dev->bus->number,
  851. PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn));
  852. printk("irq: %d, latency: %d, mmio: 0x%lx\n",
  853. bta->irq, lat, bta->mem);
  854. printk("btaudio: using card config \"%s\"\n", card->name);
  855. /* init hw */
  856. btwrite(0, REG_GPIO_DMA_CTL);
  857. btwrite(0, REG_INT_MASK);
  858. btwrite(~0U, REG_INT_STAT);
  859. pci_set_master(pci_dev);
  860. if ((rc = request_irq(bta->irq, btaudio_irq, IRQF_SHARED|IRQF_DISABLED,
  861. "btaudio",(void *)bta)) < 0) {
  862. printk(KERN_WARNING
  863. "btaudio: can't request irq (rc=%d)\n",rc);
  864. goto fail1;
  865. }
  866. /* register devices */
  867. if (digital) {
  868. rc = bta->dsp_digital =
  869. register_sound_dsp(&btaudio_digital_dsp_fops,dsp1);
  870. if (rc < 0) {
  871. printk(KERN_WARNING
  872. "btaudio: can't register digital dsp (rc=%d)\n",rc);
  873. goto fail2;
  874. }
  875. printk(KERN_INFO "btaudio: registered device dsp%d [digital]\n",
  876. bta->dsp_digital >> 4);
  877. }
  878. if (analog) {
  879. rc = bta->dsp_analog =
  880. register_sound_dsp(&btaudio_analog_dsp_fops,dsp2);
  881. if (rc < 0) {
  882. printk(KERN_WARNING
  883. "btaudio: can't register analog dsp (rc=%d)\n",rc);
  884. goto fail3;
  885. }
  886. printk(KERN_INFO "btaudio: registered device dsp%d [analog]\n",
  887. bta->dsp_analog >> 4);
  888. rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer);
  889. if (rc < 0) {
  890. printk(KERN_WARNING
  891. "btaudio: can't register mixer (rc=%d)\n",rc);
  892. goto fail4;
  893. }
  894. printk(KERN_INFO "btaudio: registered device mixer%d\n",
  895. bta->mixer_dev >> 4);
  896. }
  897. /* hook into linked list */
  898. bta->next = btaudios;
  899. btaudios = bta;
  900. pci_set_drvdata(pci_dev,bta);
  901. return 0;
  902. fail4:
  903. unregister_sound_dsp(bta->dsp_analog);
  904. fail3:
  905. if (digital)
  906. unregister_sound_dsp(bta->dsp_digital);
  907. fail2:
  908. free_irq(bta->irq,bta);
  909. fail1:
  910. kfree(bta);
  911. fail0:
  912. release_mem_region(pci_resource_start(pci_dev,0),
  913. pci_resource_len(pci_dev,0));
  914. return rc;
  915. }
  916. static void __devexit btaudio_remove(struct pci_dev *pci_dev)
  917. {
  918. struct btaudio *bta = pci_get_drvdata(pci_dev);
  919. struct btaudio *walk;
  920. /* turn off all DMA / IRQs */
  921. btand(~15, REG_GPIO_DMA_CTL);
  922. btwrite(0, REG_INT_MASK);
  923. btwrite(~0U, REG_INT_STAT);
  924. /* unregister devices */
  925. if (digital) {
  926. unregister_sound_dsp(bta->dsp_digital);
  927. }
  928. if (analog) {
  929. unregister_sound_dsp(bta->dsp_analog);
  930. unregister_sound_mixer(bta->mixer_dev);
  931. }
  932. /* free resources */
  933. free_buffer(bta);
  934. free_irq(bta->irq,bta);
  935. release_mem_region(pci_resource_start(pci_dev,0),
  936. pci_resource_len(pci_dev,0));
  937. /* remove from linked list */
  938. if (bta == btaudios) {
  939. btaudios = NULL;
  940. } else {
  941. for (walk = btaudios; walk->next != bta; walk = walk->next)
  942. ; /* if (NULL == walk->next) BUG(); */
  943. walk->next = bta->next;
  944. }
  945. pci_set_drvdata(pci_dev, NULL);
  946. kfree(bta);
  947. return;
  948. }
  949. /* -------------------------------------------------------------- */
  950. static struct pci_device_id btaudio_pci_tbl[] = {
  951. {
  952. .vendor = PCI_VENDOR_ID_BROOKTREE,
  953. .device = 0x0878,
  954. .subvendor = 0x0070,
  955. .subdevice = 0xff01,
  956. .driver_data = BTA_OSPREY200,
  957. },{
  958. .vendor = PCI_VENDOR_ID_BROOKTREE,
  959. .device = 0x0878,
  960. .subvendor = PCI_ANY_ID,
  961. .subdevice = PCI_ANY_ID,
  962. },{
  963. .vendor = PCI_VENDOR_ID_BROOKTREE,
  964. .device = 0x0878,
  965. .subvendor = PCI_ANY_ID,
  966. .subdevice = PCI_ANY_ID,
  967. },{
  968. /* --- end of list --- */
  969. }
  970. };
  971. static struct pci_driver btaudio_pci_driver = {
  972. .name = "btaudio",
  973. .id_table = btaudio_pci_tbl,
  974. .probe = btaudio_probe,
  975. .remove = __devexit_p(btaudio_remove),
  976. };
  977. static int btaudio_init_module(void)
  978. {
  979. printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n",
  980. digital ? "digital" : "",
  981. analog && digital ? "+" : "",
  982. analog ? "analog" : "");
  983. return pci_register_driver(&btaudio_pci_driver);
  984. }
  985. static void btaudio_cleanup_module(void)
  986. {
  987. pci_unregister_driver(&btaudio_pci_driver);
  988. return;
  989. }
  990. module_init(btaudio_init_module);
  991. module_exit(btaudio_cleanup_module);
  992. module_param(dsp1, int, S_IRUGO);
  993. module_param(dsp2, int, S_IRUGO);
  994. module_param(mixer, int, S_IRUGO);
  995. module_param(debug, int, S_IRUGO | S_IWUSR);
  996. module_param(irq_debug, int, S_IRUGO | S_IWUSR);
  997. module_param(digital, int, S_IRUGO);
  998. module_param(analog, int, S_IRUGO);
  999. module_param(rate, int, S_IRUGO);
  1000. module_param(latency, int, S_IRUGO);
  1001. MODULE_PARM_DESC(latency,"pci latency timer");
  1002. MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
  1003. MODULE_DESCRIPTION("bt878 audio dma driver");
  1004. MODULE_AUTHOR("Gerd Knorr");
  1005. MODULE_LICENSE("GPL");
  1006. /*
  1007. * Local variables:
  1008. * c-basic-offset: 8
  1009. * End:
  1010. */