btaudio.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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 const 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 const 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 const 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)
  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 = kzalloc(sizeof(*bta),GFP_ATOMIC);
  817. if (!bta) {
  818. rc = -ENOMEM;
  819. goto fail0;
  820. }
  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. mutex_init(&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, IRQF_SHARED|IRQF_DISABLED,
  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. iounmap(bta->mmio);
  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. iounmap(bta->mmio);
  938. /* remove from linked list */
  939. if (bta == btaudios) {
  940. btaudios = NULL;
  941. } else {
  942. for (walk = btaudios; walk->next != bta; walk = walk->next)
  943. ; /* if (NULL == walk->next) BUG(); */
  944. walk->next = bta->next;
  945. }
  946. pci_set_drvdata(pci_dev, NULL);
  947. kfree(bta);
  948. return;
  949. }
  950. /* -------------------------------------------------------------- */
  951. static struct pci_device_id btaudio_pci_tbl[] = {
  952. {
  953. .vendor = PCI_VENDOR_ID_BROOKTREE,
  954. .device = 0x0878,
  955. .subvendor = 0x0070,
  956. .subdevice = 0xff01,
  957. .driver_data = BTA_OSPREY200,
  958. },{
  959. .vendor = PCI_VENDOR_ID_BROOKTREE,
  960. .device = 0x0878,
  961. .subvendor = PCI_ANY_ID,
  962. .subdevice = PCI_ANY_ID,
  963. },{
  964. .vendor = PCI_VENDOR_ID_BROOKTREE,
  965. .device = 0x0878,
  966. .subvendor = PCI_ANY_ID,
  967. .subdevice = PCI_ANY_ID,
  968. },{
  969. /* --- end of list --- */
  970. }
  971. };
  972. static struct pci_driver btaudio_pci_driver = {
  973. .name = "btaudio",
  974. .id_table = btaudio_pci_tbl,
  975. .probe = btaudio_probe,
  976. .remove = __devexit_p(btaudio_remove),
  977. };
  978. static int btaudio_init_module(void)
  979. {
  980. printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n",
  981. digital ? "digital" : "",
  982. analog && digital ? "+" : "",
  983. analog ? "analog" : "");
  984. return pci_register_driver(&btaudio_pci_driver);
  985. }
  986. static void btaudio_cleanup_module(void)
  987. {
  988. pci_unregister_driver(&btaudio_pci_driver);
  989. return;
  990. }
  991. module_init(btaudio_init_module);
  992. module_exit(btaudio_cleanup_module);
  993. module_param(dsp1, int, S_IRUGO);
  994. module_param(dsp2, int, S_IRUGO);
  995. module_param(mixer, int, S_IRUGO);
  996. module_param(debug, int, S_IRUGO | S_IWUSR);
  997. module_param(irq_debug, int, S_IRUGO | S_IWUSR);
  998. module_param(digital, int, S_IRUGO);
  999. module_param(analog, int, S_IRUGO);
  1000. module_param(rate, int, S_IRUGO);
  1001. module_param(latency, int, S_IRUGO);
  1002. MODULE_PARM_DESC(latency,"pci latency timer");
  1003. MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
  1004. MODULE_DESCRIPTION("bt878 audio dma driver");
  1005. MODULE_AUTHOR("Gerd Knorr");
  1006. MODULE_LICENSE("GPL");
  1007. /*
  1008. * Local variables:
  1009. * c-basic-offset: 8
  1010. * End:
  1011. */