btaudio.c 28 KB

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