waveartist.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:46k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/sound/waveartist.c
  3.  *
  4.  * The low level driver for the RWA010 Rockwell Wave Artist
  5.  * codec chip used in the Rebel.com NetWinder.
  6.  *
  7.  * Cleaned up and integrated into 2.1 by Russell King (rmk@arm.linux.org.uk)
  8.  * and Pat Beirne (patb@corel.ca)
  9.  *
  10.  *
  11.  * Copyright (C) by Rebel.com 1998-1999
  12.  *
  13.  * RWA010 specs received under NDA from Rockwell
  14.  *
  15.  * Copyright (C) by Hannu Savolainen 1993-1997
  16.  *
  17.  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  18.  * Version 2 (June 1991). See the "COPYING" file distributed with this software
  19.  * for more info.
  20.  *
  21.  * Changes:
  22.  * 11-10-2000 Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
  23.  * Added __init to waveartist_init()
  24.  */
  25. /* Debugging */
  26. #define DEBUG_CMD 1
  27. #define DEBUG_OUT 2
  28. #define DEBUG_IN 4
  29. #define DEBUG_INTR 8
  30. #define DEBUG_MIXER 16
  31. #define DEBUG_TRIGGER 32
  32. #define debug_flg (0)
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/config.h>
  36. #include <linux/sched.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/delay.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/bitops.h>
  41. #include <asm/system.h>
  42. #include "sound_config.h"
  43. #include "waveartist.h"
  44. #ifdef CONFIG_ARM
  45. #include <asm/hardware.h>
  46. #include <asm/mach-types.h>
  47. #endif
  48. #ifndef NO_DMA
  49. #define NO_DMA 255
  50. #endif
  51. #define SUPPORTED_MIXER_DEVICES (SOUND_MASK_SYNTH      |
  52.  SOUND_MASK_PCM        |
  53.  SOUND_MASK_LINE       |
  54.  SOUND_MASK_MIC        |
  55.  SOUND_MASK_LINE1      |
  56.  SOUND_MASK_RECLEV     |
  57.  SOUND_MASK_VOLUME     |
  58.  SOUND_MASK_IMIX)
  59. static unsigned short levels[SOUND_MIXER_NRDEVICES] = {
  60. 0x5555, /* Master Volume  */
  61. 0x0000, /* Bass  */
  62. 0x0000, /* Treble  */
  63. 0x2323, /* Synth (FM)  */
  64. 0x4b4b, /* PCM  */
  65. 0x6464, /* PC Speaker  */
  66. 0x0000, /* Ext Line  */
  67. 0x0000, /* Mic  */
  68. 0x0000, /* CD  */
  69. 0x6464, /* Recording monitor  */
  70. 0x0000, /* SB PCM (ALT PCM)  */
  71. 0x0000, /* Recording level  */
  72. 0x6464, /* Input gain  */
  73. 0x6464, /* Output gain  */
  74. 0x0000, /* Line1 (Aux1)  */
  75. 0x0000, /* Line2 (Aux2)  */
  76. 0x0000, /* Line3 (Aux3)  */
  77. 0x0000, /* Digital1  */
  78. 0x0000, /* Digital2  */
  79. 0x0000, /* Digital3  */
  80. 0x0000, /* Phone In  */
  81. 0x6464, /* Phone Out  */
  82. 0x0000, /* Video  */
  83. 0x0000, /* Radio  */
  84. 0x0000 /* Monitor  */
  85. };
  86. typedef struct {
  87. struct address_info  hw; /* hardware */
  88. char *chip_name;
  89. int xfer_count;
  90. int audio_mode;
  91. int open_mode;
  92. int audio_flags;
  93. int record_dev;
  94. int playback_dev;
  95. int dev_no;
  96. /* Mixer parameters */
  97. const struct waveartist_mixer_info *mix;
  98. unsigned short *levels;    /* cache of volume settings   */
  99. int recmask;    /* currently enabled recording device! */
  100. #ifdef CONFIG_ARCH_NETWINDER
  101. signed int slider_vol;    /* hardware slider volume     */
  102. unsigned int handset_detect :1;
  103. unsigned int telephone_detect:1;
  104. unsigned int no_autoselect :1;/* handset/telephone autoselects a path */
  105. unsigned int spkr_mute_state :1;/* set by ioctl or autoselect */
  106. unsigned int line_mute_state :1;/* set by ioctl or autoselect */
  107. unsigned int use_slider :1;/* use slider setting for o/p vol */
  108. #endif
  109. } wavnc_info;
  110. /*
  111.  * This is the implementation specific mixer information.
  112.  */
  113. struct waveartist_mixer_info {
  114. unsigned int supported_devs;    /* Supported devices */
  115. unsigned int recording_devs;    /* Recordable devies */
  116. unsigned int stereo_devs;    /* Stereo devices */
  117. unsigned int (*select_input)(wavnc_info *, unsigned int,
  118. unsigned char *, unsigned char *);
  119. int (*decode_mixer)(wavnc_info *, int,
  120. unsigned char, unsigned char);
  121. int (*get_mixer)(wavnc_info *, int);
  122. };
  123. typedef struct wavnc_port_info {
  124. int open_mode;
  125. int speed;
  126. int channels;
  127. int audio_format;
  128. } wavnc_port_info;
  129. static int nr_waveartist_devs;
  130. static wavnc_info adev_info[MAX_AUDIO_DEV];
  131. static spinlock_t waveartist_lock = SPIN_LOCK_UNLOCKED;
  132. #ifndef CONFIG_ARCH_NETWINDER
  133. #define machine_is_netwinder() 0
  134. #else
  135. static struct timer_list vnc_timer;
  136. static void vnc_configure_mixer(wavnc_info *devc, unsigned int input_mask);
  137. static int vnc_private_ioctl(int dev, unsigned int cmd, caddr_t arg);
  138. static void vnc_slider_tick(unsigned long data);
  139. #endif
  140. static inline void
  141. waveartist_set_ctlr(struct address_info *hw, unsigned char clear, unsigned char set)
  142. {
  143. unsigned int ctlr_port = hw->io_base + CTLR;
  144. clear = ~clear & inb(ctlr_port);
  145. outb(clear | set, ctlr_port);
  146. }
  147. /* Toggle IRQ acknowledge line
  148.  */
  149. static inline void
  150. waveartist_iack(wavnc_info *devc)
  151. {
  152. unsigned int ctlr_port = devc->hw.io_base + CTLR;
  153. int old_ctlr;
  154. old_ctlr = inb(ctlr_port) & ~IRQ_ACK;
  155. outb(old_ctlr | IRQ_ACK, ctlr_port);
  156. outb(old_ctlr, ctlr_port);
  157. }
  158. static inline int
  159. waveartist_sleep(int timeout_ms)
  160. {
  161. unsigned int timeout = timeout_ms * 10 * HZ / 100;
  162. do {
  163. set_current_state(TASK_INTERRUPTIBLE);
  164. timeout = schedule_timeout(timeout);
  165. } while (timeout);
  166. return 0;
  167. }
  168. static int
  169. waveartist_reset(wavnc_info *devc)
  170. {
  171. struct address_info *hw = &devc->hw;
  172. unsigned int timeout, res = -1;
  173. waveartist_set_ctlr(hw, -1, RESET);
  174. waveartist_sleep(2);
  175. waveartist_set_ctlr(hw, RESET, 0);
  176. timeout = 500;
  177. do {
  178. mdelay(2);
  179. if (inb(hw->io_base + STATR) & CMD_RF) {
  180. res = inw(hw->io_base + CMDR);
  181. if (res == 0x55aa)
  182. break;
  183. }
  184. } while (--timeout);
  185. if (timeout == 0) {
  186. printk(KERN_WARNING "WaveArtist: reset timeout ");
  187. if (res != (unsigned int)-1)
  188. printk("(res=%04X)", res);
  189. printk("n");
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. /* Helper function to send and receive words
  195.  * from WaveArtist.  It handles all the handshaking
  196.  * and can send or receive multiple words.
  197.  */
  198. static int
  199. waveartist_cmd(wavnc_info *devc,
  200. int nr_cmd, unsigned int *cmd,
  201. int nr_resp, unsigned int *resp)
  202. {
  203. unsigned int io_base = devc->hw.io_base;
  204. unsigned int timed_out = 0;
  205. unsigned int i;
  206. if (debug_flg & DEBUG_CMD) {
  207. printk("waveartist_cmd: cmd=");
  208. for (i = 0; i < nr_cmd; i++)
  209. printk("%04X ", cmd[i]);
  210. printk("n");
  211. }
  212. if (inb(io_base + STATR) & CMD_RF) {
  213. int old_data;
  214. /* flush the port
  215.  */
  216. old_data = inw(io_base + CMDR);
  217. if (debug_flg & DEBUG_CMD)
  218. printk("flushed %04X...", old_data);
  219. udelay(10);
  220. }
  221. for (i = 0; !timed_out && i < nr_cmd; i++) {
  222. int count;
  223. for (count = 5000; count; count--)
  224. if (inb(io_base + STATR) & CMD_WE)
  225. break;
  226. if (!count)
  227. timed_out = 1;
  228. else
  229. outw(cmd[i], io_base + CMDR);
  230. }
  231. for (i = 0; !timed_out && i < nr_resp; i++) {
  232. int count;
  233. for (count = 5000; count; count--)
  234. if (inb(io_base + STATR) & CMD_RF)
  235. break;
  236. if (!count)
  237. timed_out = 1;
  238. else
  239. resp[i] = inw(io_base + CMDR);
  240. }
  241. if (debug_flg & DEBUG_CMD) {
  242. if (!timed_out) {
  243. printk("waveartist_cmd: resp=");
  244. for (i = 0; i < nr_resp; i++)
  245. printk("%04X ", resp[i]);
  246. printk("n");
  247. } else
  248. printk("waveartist_cmd: timed outn");
  249. }
  250. return timed_out ? 1 : 0;
  251. }
  252. /*
  253.  * Send one command word
  254.  */
  255. static inline int
  256. waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
  257. {
  258. return waveartist_cmd(devc, 1, &cmd, 0, NULL);
  259. }
  260. /*
  261.  * Send one command, receive one word
  262.  */
  263. static inline unsigned int
  264. waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
  265. {
  266. unsigned int ret;
  267. waveartist_cmd(devc, 1, &cmd, 1, &ret);
  268. return ret;
  269. }
  270. /*
  271.  * Send a double command, receive one
  272.  * word (and throw it away)
  273.  */
  274. static inline int
  275. waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
  276. {
  277. unsigned int vals[2];
  278. vals[0] = cmd;
  279. vals[1] = arg;
  280. return waveartist_cmd(devc, 2, vals, 1, vals);
  281. }
  282. /*
  283.  * Send a triple command
  284.  */
  285. static inline int
  286. waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
  287. unsigned int arg1, unsigned int arg2)
  288. {
  289. unsigned int vals[3];
  290. vals[0] = cmd;
  291. vals[1] = arg1;
  292. vals[2] = arg2;
  293. return waveartist_cmd(devc, 3, vals, 0, NULL);
  294. }
  295. static int
  296. waveartist_getrev(wavnc_info *devc, char *rev)
  297. {
  298. unsigned int temp[2];
  299. unsigned int cmd = WACMD_GETREV;
  300. waveartist_cmd(devc, 1, &cmd, 2, temp);
  301. rev[0] = temp[0] >> 8;
  302. rev[1] = temp[0] & 255;
  303. rev[2] = '';
  304. return temp[0];
  305. }
  306. static void waveartist_halt_output(int dev);
  307. static void waveartist_halt_input(int dev);
  308. static void waveartist_halt(int dev);
  309. static void waveartist_trigger(int dev, int state);
  310. static int
  311. waveartist_open(int dev, int mode)
  312. {
  313. wavnc_info *devc;
  314. wavnc_port_info *portc;
  315. unsigned long flags;
  316. if (dev < 0 || dev >= num_audiodevs)
  317. return -ENXIO;
  318. devc  = (wavnc_info *) audio_devs[dev]->devc;
  319. portc = (wavnc_port_info *) audio_devs[dev]->portc;
  320. spin_lock_irqsave(&waveartist_lock, flags);
  321. if (portc->open_mode || (devc->open_mode & mode)) {
  322. spin_unlock_irqrestore(&waveartist_lock, flags);
  323. return -EBUSY;
  324. }
  325. devc->audio_mode  = 0;
  326. devc->open_mode  |= mode;
  327. portc->open_mode  = mode;
  328. waveartist_trigger(dev, 0);
  329. if (mode & OPEN_READ)
  330. devc->record_dev = dev;
  331. if (mode & OPEN_WRITE)
  332. devc->playback_dev = dev;
  333. spin_unlock_irqrestore(&waveartist_lock, flags);
  334. return 0;
  335. }
  336. static void
  337. waveartist_close(int dev)
  338. {
  339. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  340. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  341. unsigned long flags;
  342. spin_lock_irqsave(&waveartist_lock, flags);
  343. waveartist_halt(dev);
  344. devc->audio_mode = 0;
  345. devc->open_mode &= ~portc->open_mode;
  346. portc->open_mode = 0;
  347. spin_unlock_irqrestore(&waveartist_lock, flags);
  348. }
  349. static void
  350. waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
  351. {
  352. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  353. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  354. unsigned long flags;
  355. unsigned int count = __count; 
  356. if (debug_flg & DEBUG_OUT)
  357. printk("waveartist: output block, buf=0x%lx, count=0x%x...n",
  358. buf, count);
  359. /*
  360.  * 16 bit data
  361.  */
  362. if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
  363. count >>= 1;
  364. if (portc->channels > 1)
  365. count >>= 1;
  366. count -= 1;
  367. if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
  368.     audio_devs[dev]->flags & DMA_AUTOMODE &&
  369.     intrflag &&
  370.     count == devc->xfer_count) {
  371. devc->audio_mode |= PCM_ENABLE_OUTPUT;
  372. return; /*
  373.  * Auto DMA mode on. No need to react
  374.  */
  375. }
  376. spin_lock_irqsave(&waveartist_lock, flags);
  377. /*
  378.  * set sample count
  379.  */
  380. waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
  381. devc->xfer_count = count;
  382. devc->audio_mode |= PCM_ENABLE_OUTPUT;
  383. spin_unlock_irqrestore(&waveartist_lock, flags);
  384. }
  385. static void
  386. waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
  387. {
  388. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  389. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  390. unsigned long flags;
  391. unsigned int count = __count;
  392. if (debug_flg & DEBUG_IN)
  393. printk("waveartist: start input, buf=0x%lx, count=0x%x...n",
  394. buf, count);
  395. if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
  396. count >>= 1;
  397. if (portc->channels > 1)
  398. count >>= 1;
  399. count -= 1;
  400. if (devc->audio_mode & PCM_ENABLE_INPUT &&
  401.     audio_devs[dev]->flags & DMA_AUTOMODE &&
  402.     intrflag &&
  403.     count == devc->xfer_count) {
  404. devc->audio_mode |= PCM_ENABLE_INPUT;
  405. return; /*
  406.  * Auto DMA mode on. No need to react
  407.  */
  408. }
  409. spin_lock_irqsave(&waveartist_lock, flags);
  410. /*
  411.  * set sample count
  412.  */
  413. waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
  414. devc->xfer_count = count;
  415. devc->audio_mode |= PCM_ENABLE_INPUT;
  416. spin_unlock_irqrestore(&waveartist_lock, flags);
  417. }
  418. static int
  419. waveartist_ioctl(int dev, unsigned int cmd, caddr_t arg)
  420. {
  421. return -EINVAL;
  422. }
  423. static unsigned int
  424. waveartist_get_speed(wavnc_port_info *portc)
  425. {
  426. unsigned int speed;
  427. /*
  428.  * program the speed, channels, bits
  429.  */
  430. if (portc->speed == 8000)
  431. speed = 0x2E71;
  432. else if (portc->speed == 11025)
  433. speed = 0x4000;
  434. else if (portc->speed == 22050)
  435. speed = 0x8000;
  436. else if (portc->speed == 44100)
  437. speed = 0x0;
  438. else {
  439. /*
  440.  * non-standard - just calculate
  441.  */
  442. speed = portc->speed << 16;
  443. speed = (speed / 44100) & 65535;
  444. }
  445. return speed;
  446. }
  447. static unsigned int
  448. waveartist_get_bits(wavnc_port_info *portc)
  449. {
  450. unsigned int bits;
  451. if (portc->audio_format == AFMT_S16_LE)
  452. bits = 1;
  453. else if (portc->audio_format == AFMT_S8)
  454. bits = 0;
  455. else
  456. bits = 2; //default AFMT_U8
  457. return bits;
  458. }
  459. static int
  460. waveartist_prepare_for_input(int dev, int bsize, int bcount)
  461. {
  462. unsigned long flags;
  463. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  464. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  465. unsigned int speed, bits;
  466. if (devc->audio_mode)
  467. return 0;
  468. speed = waveartist_get_speed(portc);
  469. bits  = waveartist_get_bits(portc);
  470. spin_lock_irqsave(&waveartist_lock, flags);
  471. if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
  472. printk(KERN_WARNING "waveartist: error setting the "
  473.        "record format to %dn", portc->audio_format);
  474. if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
  475. printk(KERN_WARNING "waveartist: error setting record "
  476.        "to %d channelsn", portc->channels);
  477. /*
  478.  * write cmd SetSampleSpeedTimeConstant
  479.  */
  480. if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
  481. printk(KERN_WARNING "waveartist: error setting the record "
  482.        "speed to %dHz.n", portc->speed);
  483. if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
  484. printk(KERN_WARNING "waveartist: error setting the record "
  485.        "data path to 0x%Xn", 1);
  486. if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
  487. printk(KERN_WARNING "waveartist: error setting the record "
  488.        "format to %dn", portc->audio_format);
  489. devc->xfer_count = 0;
  490. spin_unlock_irqrestore(&waveartist_lock, flags);
  491. waveartist_halt_input(dev);
  492. if (debug_flg & DEBUG_INTR) {
  493. printk("WA CTLR reg: 0x%02X.n",
  494.        inb(devc->hw.io_base + CTLR));
  495. printk("WA STAT reg: 0x%02X.n",
  496.        inb(devc->hw.io_base + STATR));
  497. printk("WA IRQS reg: 0x%02X.n",
  498.        inb(devc->hw.io_base + IRQSTAT));
  499. }
  500. return 0;
  501. }
  502. static int
  503. waveartist_prepare_for_output(int dev, int bsize, int bcount)
  504. {
  505. unsigned long flags;
  506. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  507. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  508. unsigned int speed, bits;
  509. /*
  510.  * program the speed, channels, bits
  511.  */
  512. speed = waveartist_get_speed(portc);
  513. bits  = waveartist_get_bits(portc);
  514. spin_lock_irqsave(&waveartist_lock, flags);
  515. if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
  516.     waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
  517. printk(KERN_WARNING "waveartist: error setting the playback "
  518.        "speed to %dHz.n", portc->speed);
  519. if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
  520. printk(KERN_WARNING "waveartist: error setting the playback "
  521.        "to %d channelsn", portc->channels);
  522. if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
  523. printk(KERN_WARNING "waveartist: error setting the playback "
  524.        "data path to 0x%Xn", 0);
  525. if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
  526. printk(KERN_WARNING "waveartist: error setting the playback "
  527.        "format to %dn", portc->audio_format);
  528. devc->xfer_count = 0;
  529. spin_unlock_irqrestore(&waveartist_lock, flags);
  530. waveartist_halt_output(dev);
  531. if (debug_flg & DEBUG_INTR) {
  532. printk("WA CTLR reg: 0x%02X.n",inb(devc->hw.io_base + CTLR));
  533. printk("WA STAT reg: 0x%02X.n",inb(devc->hw.io_base + STATR));
  534. printk("WA IRQS reg: 0x%02X.n",inb(devc->hw.io_base + IRQSTAT));
  535. }
  536. return 0;
  537. }
  538. static void
  539. waveartist_halt(int dev)
  540. {
  541. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  542. wavnc_info *devc;
  543. if (portc->open_mode & OPEN_WRITE)
  544. waveartist_halt_output(dev);
  545. if (portc->open_mode & OPEN_READ)
  546. waveartist_halt_input(dev);
  547. devc = (wavnc_info *) audio_devs[dev]->devc;
  548. devc->audio_mode = 0;
  549. }
  550. static void
  551. waveartist_halt_input(int dev)
  552. {
  553. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  554. unsigned long flags;
  555. spin_lock_irqsave(&waveartist_lock, flags);
  556. /*
  557.  * Stop capture
  558.  */
  559. waveartist_cmd1(devc, WACMD_INPUTSTOP);
  560. devc->audio_mode &= ~PCM_ENABLE_INPUT;
  561. /*
  562.  * Clear interrupt by toggling
  563.  * the IRQ_ACK bit in CTRL
  564.  */
  565. if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
  566. waveartist_iack(devc);
  567. // devc->audio_mode &= ~PCM_ENABLE_INPUT;
  568. spin_unlock_irqrestore(&waveartist_lock, flags);
  569. }
  570. static void
  571. waveartist_halt_output(int dev)
  572. {
  573. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  574. unsigned long flags;
  575. spin_lock_irqsave(&waveartist_lock, flags);
  576. waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
  577. devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
  578. /*
  579.  * Clear interrupt by toggling
  580.  * the IRQ_ACK bit in CTRL
  581.  */
  582. if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
  583. waveartist_iack(devc);
  584. // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
  585. spin_unlock_irqrestore(&waveartist_lock, flags);
  586. }
  587. static void
  588. waveartist_trigger(int dev, int state)
  589. {
  590. wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
  591. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  592. unsigned long flags;
  593. if (debug_flg & DEBUG_TRIGGER) {
  594. printk("wavnc: audio trigger ");
  595. if (state & PCM_ENABLE_INPUT)
  596. printk("in ");
  597. if (state & PCM_ENABLE_OUTPUT)
  598. printk("out");
  599. printk("n");
  600. }
  601. spin_lock_irqsave(&waveartist_lock, flags);
  602. state &= devc->audio_mode;
  603. if (portc->open_mode & OPEN_READ &&
  604.     state & PCM_ENABLE_INPUT)
  605. /*
  606.  * enable ADC Data Transfer to PC
  607.  */
  608. waveartist_cmd1(devc, WACMD_INPUTSTART);
  609. if (portc->open_mode & OPEN_WRITE &&
  610.     state & PCM_ENABLE_OUTPUT)
  611. /*
  612.  * enable DAC data transfer from PC
  613.  */
  614. waveartist_cmd1(devc, WACMD_OUTPUTSTART);
  615. spin_unlock_irqrestore(&waveartist_lock, flags);
  616. }
  617. static int
  618. waveartist_set_speed(int dev, int arg)
  619. {
  620. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  621. if (arg <= 0)
  622. return portc->speed;
  623. if (arg < 5000)
  624. arg = 5000;
  625. if (arg > 44100)
  626. arg = 44100;
  627. portc->speed = arg;
  628. return portc->speed;
  629. }
  630. static short
  631. waveartist_set_channels(int dev, short arg)
  632. {
  633. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  634. if (arg != 1 && arg != 2)
  635. return portc->channels;
  636. portc->channels = arg;
  637. return arg;
  638. }
  639. static unsigned int
  640. waveartist_set_bits(int dev, unsigned int arg)
  641. {
  642. wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
  643. if (arg == 0)
  644. return portc->audio_format;
  645. if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
  646. arg = AFMT_U8;
  647. portc->audio_format = arg;
  648. return arg;
  649. }
  650. static struct audio_driver waveartist_audio_driver = {
  651. owner: THIS_MODULE,
  652. open: waveartist_open,
  653. close: waveartist_close,
  654. output_block: waveartist_output_block,
  655. start_input: waveartist_start_input,
  656. ioctl: waveartist_ioctl,
  657. prepare_for_input: waveartist_prepare_for_input,
  658. prepare_for_output: waveartist_prepare_for_output,
  659. halt_io: waveartist_halt,
  660. halt_input: waveartist_halt_input,
  661. halt_output: waveartist_halt_output,
  662. trigger: waveartist_trigger,
  663. set_speed: waveartist_set_speed,
  664. set_bits: waveartist_set_bits,
  665. set_channels: waveartist_set_channels
  666. };
  667. static void
  668. waveartist_intr(int irq, void *dev_id, struct pt_regs *regs)
  669. {
  670. wavnc_info *devc = (wavnc_info *)dev_id;
  671. int    irqstatus, status;
  672. irqstatus = inb(devc->hw.io_base + IRQSTAT);
  673. status    = inb(devc->hw.io_base + STATR);
  674. if (debug_flg & DEBUG_INTR)
  675. printk("waveartist_intr: stat=%02x, irqstat=%02xn",
  676.        status, irqstatus);
  677. if (status & IRQ_REQ) /* Clear interrupt */
  678. waveartist_iack(devc);
  679. else
  680. printk(KERN_WARNING "waveartist: unexpected interruptn");
  681. if (irqstatus & 0x01) {
  682. int temp = 1;
  683. /* PCM buffer done
  684.  */
  685. if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
  686. DMAbuf_outputintr(devc->playback_dev, 1);
  687. temp = 0;
  688. }
  689. if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
  690. DMAbuf_inputintr(devc->record_dev);
  691. temp = 0;
  692. }
  693. if (temp) //default:
  694. printk(KERN_WARNING "waveartist: Unknown interruptn");
  695. }
  696. if (irqstatus & 0x2)
  697. // We do not use SB mode natively...
  698. printk(KERN_WARNING "waveartist: Unexpected SB interrupt...n");
  699. }
  700. /* -------------------------------------------------------------------------
  701.  * Mixer stuff
  702.  */
  703. struct mix_ent {
  704. unsigned char reg_l;
  705. unsigned char reg_r;
  706. unsigned char shift;
  707. unsigned char max;
  708. };
  709. static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
  710. { 2, 6, 1,  7 }, /* SOUND_MIXER_VOLUME   */
  711. { 0, 0, 0,  0 }, /* SOUND_MIXER_BASS     */
  712. { 0, 0, 0,  0 }, /* SOUND_MIXER_TREBLE   */
  713. { 0, 0, 0,  0 }, /* SOUND_MIXER_SYNTH    */
  714. { 0, 0, 0,  0 }, /* SOUND_MIXER_PCM      */
  715. { 0, 0, 0,  0 }, /* SOUND_MIXER_SPEAKER  */
  716. { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE     */
  717. { 2, 6, 4,  3 }, /* SOUND_MIXER_MIC      */
  718. { 0, 0, 0,  0 }, /* SOUND_MIXER_CD       */
  719. { 0, 0, 0,  0 }, /* SOUND_MIXER_IMIX     */
  720. { 0, 0, 0,  0 }, /* SOUND_MIXER_ALTPCM   */
  721. #if 0
  722. { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV   */
  723. { 0, 0, 0,  0 }, /* SOUND_MIXER_IGAIN    */
  724. #else
  725. { 0, 0, 0,  0 }, /* SOUND_MIXER_RECLEV   */
  726. { 3, 7, 0,  7 }, /* SOUND_MIXER_IGAIN    */
  727. #endif
  728. { 0, 0, 0,  0 }, /* SOUND_MIXER_OGAIN    */
  729. { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1    */
  730. { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2    */
  731. { 0, 0, 0,  0 }, /* SOUND_MIXER_LINE3    */
  732. { 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL1 */
  733. { 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL2 */
  734. { 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL3 */
  735. { 0, 0, 0,  0 }, /* SOUND_MIXER_PHONEIN  */
  736. { 0, 0, 0,  0 }, /* SOUND_MIXER_PHONEOUT */
  737. { 0, 0, 0,  0 }, /* SOUND_MIXER_VIDEO    */
  738. { 0, 0, 0,  0 }, /* SOUND_MIXER_RADIO    */
  739. { 0, 0, 0,  0 }  /* SOUND_MIXER_MONITOR  */
  740. };
  741. static void
  742. waveartist_mixer_update(wavnc_info *devc, int whichDev)
  743. {
  744. unsigned int lev_left, lev_right;
  745. lev_left  = devc->levels[whichDev] & 0xff;
  746. lev_right = devc->levels[whichDev] >> 8;
  747. if (lev_left > 100)
  748. lev_left = 100;
  749. if (lev_right > 100)
  750. lev_right = 100;
  751. #define SCALE(lev,max) ((lev) * (max) / 100)
  752. if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
  753. whichDev = SOUND_MIXER_VOLUME;
  754. if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
  755. const struct mix_ent *mix = mix_devs + whichDev;
  756. unsigned int mask, left, right;
  757. mask = mix->max << mix->shift;
  758. lev_left  = SCALE(lev_left,  mix->max) << mix->shift;
  759. lev_right = SCALE(lev_right, mix->max) << mix->shift;
  760. /* read left setting */
  761. left  = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
  762.        mix->reg_l << 8);
  763. /* read right setting */
  764. right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
  765. mix->reg_r << 8);
  766. left  = (left  & ~mask) | (lev_left  & mask);
  767. right = (right & ~mask) | (lev_right & mask);
  768. /* write left,right back */
  769. waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
  770. } else {
  771. switch(whichDev) {
  772. case SOUND_MIXER_PCM:
  773. waveartist_cmd3(devc, WACMD_SET_LEVEL,
  774. SCALE(lev_left,  32767),
  775. SCALE(lev_right, 32767));
  776. break;
  777. case SOUND_MIXER_SYNTH:
  778. waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
  779. SCALE(lev_left,  32767),
  780. SCALE(lev_right, 32767));
  781. break;
  782. }
  783. }
  784. }
  785. /*
  786.  * Set the ADC MUX to the specified values.  We do NOT do any
  787.  * checking of the values passed, since we assume that the
  788.  * relevant *_select_input function has done that for us.
  789.  */
  790. static void
  791. waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
  792. {
  793. unsigned int reg_08, reg_09;
  794. reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
  795. reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
  796. reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
  797. waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
  798. }
  799. /*
  800.  * Decode a recording mask into a mixer selection as follows:
  801.  *
  802.  *     OSS Source WA Source Actual source
  803.  *  SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
  804.  *  SOUND_MASK_LINE Line Line in
  805.  *  SOUND_MASK_LINE1 Aux 1 Aux 1 in
  806.  *  SOUND_MASK_LINE2 Aux 2 Aux 2 in
  807.  *  SOUND_MASK_MIC Mic Microphone
  808.  */
  809. static unsigned int
  810. waveartist_select_input(wavnc_info *devc, unsigned int recmask,
  811. unsigned char *dev_l, unsigned char *dev_r)
  812. {
  813. unsigned int recdev = ADC_MUX_NONE;
  814. if (recmask & SOUND_MASK_IMIX) {
  815. recmask = SOUND_MASK_IMIX;
  816. recdev = ADC_MUX_MIXER;
  817. } else if (recmask & SOUND_MASK_LINE2) {
  818. recmask = SOUND_MASK_LINE2;
  819. recdev = ADC_MUX_AUX2;
  820. } else if (recmask & SOUND_MASK_LINE1) {
  821. recmask = SOUND_MASK_LINE1;
  822. recdev = ADC_MUX_AUX1;
  823. } else if (recmask & SOUND_MASK_LINE) {
  824. recmask = SOUND_MASK_LINE;
  825. recdev = ADC_MUX_LINE;
  826. } else if (recmask & SOUND_MASK_MIC) {
  827. recmask = SOUND_MASK_MIC;
  828. recdev = ADC_MUX_MIC;
  829. }
  830. *dev_l = *dev_r = recdev;
  831. return recmask;
  832. }
  833. static int
  834. waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
  835. unsigned char lev_r)
  836. {
  837. switch (dev) {
  838. case SOUND_MIXER_VOLUME:
  839. case SOUND_MIXER_SYNTH:
  840. case SOUND_MIXER_PCM:
  841. case SOUND_MIXER_LINE:
  842. case SOUND_MIXER_MIC:
  843. case SOUND_MIXER_IGAIN:
  844. case SOUND_MIXER_LINE1:
  845. case SOUND_MIXER_LINE2:
  846. devc->levels[dev] = lev_l | lev_r << 8;
  847. break;
  848. case SOUND_MIXER_IMIX:
  849. break;
  850. default:
  851. dev = -EINVAL;
  852. break;
  853. }
  854. return dev;
  855. }
  856. static int waveartist_get_mixer(wavnc_info *devc, int dev)
  857. {
  858. return devc->levels[dev];
  859. }
  860. static const struct waveartist_mixer_info waveartist_mixer = {
  861. supported_devs: SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
  862. recording_devs: SOUND_MASK_LINE  | SOUND_MASK_MIC   |
  863. SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
  864. SOUND_MASK_IMIX,
  865. stereo_devs: (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
  866. (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
  867. select_input: waveartist_select_input,
  868. decode_mixer: waveartist_decode_mixer,
  869. get_mixer: waveartist_get_mixer,
  870. };
  871. static void
  872. waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
  873. {
  874. unsigned char dev_l, dev_r;
  875. recmask &= devc->mix->recording_devs;
  876. /*
  877.  * If more than one recording device selected,
  878.  * disable the device that is currently in use.
  879.  */
  880. if (hweight32(recmask) > 1)
  881. recmask &= ~devc->recmask;
  882. /*
  883.  * Translate the recording device mask into
  884.  * the ADC multiplexer settings.
  885.  */
  886. devc->recmask = devc->mix->select_input(devc, recmask,
  887. &dev_l, &dev_r);
  888. waveartist_set_adc_mux(devc, dev_l, dev_r);
  889. }
  890. static int
  891. waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
  892. {
  893. unsigned int lev_left  = level & 0x00ff;
  894. unsigned int lev_right = (level & 0xff00) >> 8;
  895. if (lev_left > 100)
  896. lev_left = 100;
  897. if (lev_right > 100)
  898. lev_right = 100;
  899. /*
  900.  * Mono devices have their right volume forced to their
  901.  * left volume.  (from ALSA driver OSS emulation).
  902.  */
  903. if (!(devc->mix->stereo_devs & (1 << dev)))
  904. lev_right = lev_left;
  905. dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
  906. if (dev >= 0)
  907. waveartist_mixer_update(devc, dev);
  908. return dev < 0 ? dev : 0;
  909. }
  910. static int
  911. waveartist_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
  912. {
  913. wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
  914. int ret = 0, val, nr;
  915. /*
  916.  * All SOUND_MIXER_* ioctls use type 'M'
  917.  */
  918. if (((cmd >> 8) & 255) != 'M')
  919. return -ENOIOCTLCMD;
  920. #ifdef CONFIG_ARCH_NETWINDER
  921. if (machine_is_netwinder()) {
  922. ret = vnc_private_ioctl(dev, cmd, arg);
  923. if (ret != -ENOIOCTLCMD)
  924. return ret;
  925. else
  926. ret = 0;
  927. }
  928. #endif
  929. nr = cmd & 0xff;
  930. if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
  931. if (get_user(val, (int *)arg))
  932. return -EFAULT;
  933. switch (nr) {
  934. case SOUND_MIXER_RECSRC:
  935. waveartist_set_recmask(devc, val);
  936. break;
  937. default:
  938. ret = -EINVAL;
  939. if (nr < SOUND_MIXER_NRDEVICES &&
  940.     devc->mix->supported_devs & (1 << nr))
  941. ret = waveartist_set_mixer(devc, nr, val);
  942. }
  943. }
  944. if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
  945. ret = -EINVAL;
  946. switch (nr) {
  947. case SOUND_MIXER_RECSRC:
  948. ret = devc->recmask;
  949. break;
  950. case SOUND_MIXER_DEVMASK:
  951. ret = devc->mix->supported_devs;
  952. break;
  953. case SOUND_MIXER_STEREODEVS:
  954. ret = devc->mix->stereo_devs;
  955. break;
  956. case SOUND_MIXER_RECMASK:
  957. ret = devc->mix->recording_devs;
  958. break;
  959. case SOUND_MIXER_CAPS:
  960. ret = SOUND_CAP_EXCL_INPUT;
  961. break;
  962. default:
  963. if (nr < SOUND_MIXER_NRDEVICES)
  964. ret = devc->mix->get_mixer(devc, nr);
  965. break;
  966. }
  967. if (ret >= 0)
  968. ret = put_user(ret, (int *)arg) ? -EFAULT : 0;
  969. }
  970. return ret;
  971. }
  972. static struct mixer_operations waveartist_mixer_operations =
  973. {
  974. owner: THIS_MODULE,
  975. id: "WaveArtist",
  976. name: "WaveArtist",
  977. ioctl: waveartist_mixer_ioctl
  978. };
  979. static void
  980. waveartist_mixer_reset(wavnc_info *devc)
  981. {
  982. int i;
  983. if (debug_flg & DEBUG_MIXER)
  984. printk("%s: mixer_resetn", devc->hw.name);
  985. /*
  986.  * reset mixer cmd
  987.  */
  988. waveartist_cmd1(devc, WACMD_RST_MIXER);
  989. /*
  990.  * set input for ADC to come from 'quiet'
  991.  * turn on default modes
  992.  */
  993. waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
  994. /*
  995.  * set mixer input select to none, RX filter gains 0 dB
  996.  */
  997. waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
  998. /*
  999.  * set bit 0 reg 2 to 1 - unmute MonoOut
  1000.  */
  1001. waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
  1002. /* set default input device = internal mic
  1003.  * current recording device = none
  1004.  */
  1005. waveartist_set_recmask(devc, 0);
  1006. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  1007. waveartist_mixer_update(devc, i);
  1008. }
  1009. static int __init waveartist_init(wavnc_info *devc)
  1010. {
  1011. wavnc_port_info *portc;
  1012. char rev[3], dev_name[64];
  1013. int my_dev;
  1014. if (waveartist_reset(devc))
  1015. return -ENODEV;
  1016. sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
  1017. if (waveartist_getrev(devc, rev)) {
  1018. strcat(dev_name, " rev. ");
  1019. strcat(dev_name, rev);
  1020. }
  1021. strcat(dev_name, ")");
  1022. conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
  1023.      devc->hw.dma, devc->hw.dma2);
  1024. portc = (wavnc_port_info *)kmalloc(sizeof(wavnc_port_info), GFP_KERNEL);
  1025. if (portc == NULL)
  1026. goto nomem;
  1027. memset(portc, 0, sizeof(wavnc_port_info));
  1028. my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
  1029. &waveartist_audio_driver, sizeof(struct audio_driver),
  1030. devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
  1031. devc, devc->hw.dma, devc->hw.dma2);
  1032. if (my_dev < 0)
  1033. goto free;
  1034. audio_devs[my_dev]->portc = portc;
  1035. waveartist_mixer_reset(devc);
  1036. /*
  1037.  * clear any pending interrupt
  1038.  */
  1039. waveartist_iack(devc);
  1040. if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
  1041. printk(KERN_ERR "%s: IRQ %d in usen",
  1042. devc->hw.name, devc->hw.irq);
  1043. goto uninstall;
  1044. }
  1045. if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
  1046. printk(KERN_ERR "%s: Can't allocate DMA%dn",
  1047. devc->hw.name, devc->hw.dma);
  1048. goto uninstall_irq;
  1049. }
  1050. if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
  1051. if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
  1052. printk(KERN_ERR "%s: can't allocate DMA%dn",
  1053. devc->hw.name, devc->hw.dma2);
  1054. goto uninstall_dma;
  1055. }
  1056. waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
  1057. audio_devs[my_dev]->mixer_dev =
  1058. sound_install_mixer(MIXER_DRIVER_VERSION,
  1059. dev_name,
  1060. &waveartist_mixer_operations,
  1061. sizeof(struct mixer_operations),
  1062. devc);
  1063. return my_dev;
  1064. uninstall_dma:
  1065. sound_free_dma(devc->hw.dma);
  1066. uninstall_irq:
  1067. free_irq(devc->hw.irq, devc);
  1068. uninstall:
  1069. sound_unload_audiodev(my_dev);
  1070. free:
  1071. kfree(portc);
  1072. nomem:
  1073. return -1;
  1074. }
  1075. static int __init probe_waveartist(struct address_info *hw_config)
  1076. {
  1077. wavnc_info *devc = &adev_info[nr_waveartist_devs];
  1078. if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
  1079. printk(KERN_WARNING "waveartist: too many audio devicesn");
  1080. return 0;
  1081. }
  1082. if (check_region(hw_config->io_base, 15))  {
  1083. printk(KERN_WARNING "WaveArtist: I/O port conflictn");
  1084. return 0;
  1085. }
  1086. if (hw_config->irq > 15 || hw_config->irq < 0) {
  1087. printk(KERN_WARNING "WaveArtist: Bad IRQ %dn",
  1088.        hw_config->irq);
  1089. return 0;
  1090. }
  1091. if (hw_config->dma != 3) {
  1092. printk(KERN_WARNING "WaveArtist: Bad DMA %dn",
  1093.        hw_config->dma);
  1094. return 0;
  1095. }
  1096. hw_config->name = "WaveArtist";
  1097. devc->hw = *hw_config;
  1098. devc->open_mode = 0;
  1099. devc->chip_name = "RWA-010";
  1100. return 1;
  1101. }
  1102. static void __init
  1103. attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
  1104. {
  1105. wavnc_info *devc = &adev_info[nr_waveartist_devs];
  1106. /*
  1107.  * NOTE! If irq < 0, there is another driver which has allocated the
  1108.  *   IRQ so that this driver doesn't need to allocate/deallocate it.
  1109.  *   The actually used IRQ is ABS(irq).
  1110.  */
  1111. devc->hw = *hw;
  1112. devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
  1113. devc->open_mode = 0;
  1114. devc->playback_dev = 0;
  1115. devc->record_dev = 0;
  1116. devc->audio_flags = DMA_AUTOMODE;
  1117. devc->levels = levels;
  1118. if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
  1119. devc->audio_flags |= DMA_DUPLEX;
  1120. request_region(hw->io_base, 15, devc->hw.name);
  1121. devc->mix = mix;
  1122. devc->dev_no = waveartist_init(devc);
  1123. if (devc->dev_no < 0)
  1124. release_region(hw->io_base, 15);
  1125. else {
  1126. #ifdef CONFIG_ARCH_NETWINDER
  1127. if (machine_is_netwinder()) {
  1128. init_timer(&vnc_timer);
  1129. vnc_timer.function = vnc_slider_tick;
  1130. vnc_timer.expires  = jiffies;
  1131. vnc_timer.data     = nr_waveartist_devs;
  1132. add_timer(&vnc_timer);
  1133. vnc_configure_mixer(devc, 0);
  1134. devc->no_autoselect = 1;
  1135. }
  1136. #endif
  1137. nr_waveartist_devs += 1;
  1138. }
  1139. }
  1140. static void __exit unload_waveartist(struct address_info *hw)
  1141. {
  1142. wavnc_info *devc = NULL;
  1143. int i;
  1144. for (i = 0; i < nr_waveartist_devs; i++)
  1145. if (hw->io_base == adev_info[i].hw.io_base) {
  1146. devc = adev_info + i;
  1147. break;
  1148. }
  1149. if (devc != NULL) {
  1150. int mixer;
  1151. #ifdef CONFIG_ARCH_NETWINDER
  1152. if (machine_is_netwinder())
  1153. del_timer(&vnc_timer);
  1154. #endif
  1155. release_region(devc->hw.io_base, 15);
  1156. waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
  1157. if (devc->hw.irq >= 0)
  1158. free_irq(devc->hw.irq, devc);
  1159. sound_free_dma(devc->hw.dma);
  1160. if (devc->hw.dma != devc->hw.dma2 &&
  1161.     devc->hw.dma2 != NO_DMA)
  1162. sound_free_dma(devc->hw.dma2);
  1163. mixer = audio_devs[devc->dev_no]->mixer_dev;
  1164. if (mixer >= 0)
  1165. sound_unload_mixerdev(mixer);
  1166. if (devc->dev_no >= 0)
  1167. sound_unload_audiodev(devc->dev_no);
  1168. nr_waveartist_devs -= 1;
  1169. for (; i < nr_waveartist_devs; i++)
  1170. adev_info[i] = adev_info[i + 1];
  1171. } else
  1172. printk(KERN_WARNING "waveartist: can't find device "
  1173.        "to unloadn");
  1174. }
  1175. #ifdef CONFIG_ARCH_NETWINDER
  1176. /*
  1177.  * Rebel.com Netwinder specifics...
  1178.  */
  1179. #include <asm/hardware/dec21285.h>
  1180.  
  1181. #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
  1182. #define MIXER_PRIVATE3_RESET 0x53570000
  1183. #define MIXER_PRIVATE3_READ 0x53570001
  1184. #define MIXER_PRIVATE3_WRITE 0x53570002
  1185. #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
  1186. #define VNC_MUTE_LINE_OUT 0x10
  1187. #define VNC_PHONE_DETECT 0x20
  1188. #define VNC_HANDSET_DETECT 0x40
  1189. #define VNC_DISABLE_AUTOSWITCH 0x80
  1190. extern spinlock_t gpio_lock;
  1191. static inline void
  1192. vnc_mute_spkr(wavnc_info *devc)
  1193. {
  1194. unsigned long flags;
  1195. spin_lock_irqsave(&gpio_lock, flags);
  1196. cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
  1197. spin_unlock_irqrestore(&gpio_lock, flags);
  1198. }
  1199. static void
  1200. vnc_mute_lout(wavnc_info *devc)
  1201. {
  1202. unsigned int left, right;
  1203. left  = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
  1204. right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
  1205. if (devc->line_mute_state) {
  1206. left &= ~1;
  1207. right &= ~1;
  1208. } else {
  1209. left |= 1;
  1210. right |= 1;
  1211. }
  1212. waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
  1213. }
  1214. static int
  1215. vnc_volume_slider(wavnc_info *devc)
  1216. {
  1217. static signed int old_slider_volume;
  1218. unsigned long flags;
  1219. signed int volume = 255;
  1220. *CSR_TIMER1_LOAD = 0x00ffffff;
  1221. save_flags(flags);
  1222. cli();
  1223. outb(0xFF, 0x201);
  1224. *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
  1225. while (volume && (inb(0x201) & 0x01))
  1226. volume--;
  1227. *CSR_TIMER1_CNTL = 0;
  1228. restore_flags(flags);
  1229. volume = 0x00ffffff - *CSR_TIMER1_VALUE;
  1230. #ifndef REVERSE
  1231. volume = 150 - (volume >> 5);
  1232. #else
  1233. volume = (volume >> 6) - 25;
  1234. #endif
  1235. if (volume < 0)
  1236. volume = 0;
  1237. if (volume > 100)
  1238. volume = 100;
  1239. /*
  1240.  * slider quite often reads +-8, so debounce this random noise
  1241.  */
  1242. if (abs(volume - old_slider_volume) > 7) {
  1243. old_slider_volume = volume;
  1244. if (debug_flg & DEBUG_MIXER)
  1245. printk(KERN_DEBUG "Slider volume: %d.n", volume);
  1246. }
  1247. return old_slider_volume;
  1248. }
  1249. /*
  1250.  * Decode a recording mask into a mixer selection on the NetWinder
  1251.  * as follows:
  1252.  *
  1253.  *     OSS Source WA Source Actual source
  1254.  *  SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
  1255.  *  SOUND_MASK_LINE Line Line in
  1256.  *  SOUND_MASK_LINE1 Left Mic Handset
  1257.  *  SOUND_MASK_PHONEIN Left Aux Telephone microphone
  1258.  *  SOUND_MASK_MIC Right Mic Builtin microphone
  1259.  */
  1260. static unsigned int
  1261. netwinder_select_input(wavnc_info *devc, unsigned int recmask,
  1262.        unsigned char *dev_l, unsigned char *dev_r)
  1263. {
  1264. unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
  1265. if (recmask & SOUND_MASK_IMIX) {
  1266. recmask = SOUND_MASK_IMIX;
  1267. recdev_l = ADC_MUX_MIXER;
  1268. recdev_r = ADC_MUX_MIXER;
  1269. } else if (recmask & SOUND_MASK_LINE) {
  1270. recmask = SOUND_MASK_LINE;
  1271. recdev_l = ADC_MUX_LINE;
  1272. recdev_r = ADC_MUX_LINE;
  1273. } else if (recmask & SOUND_MASK_LINE1) {
  1274. recmask = SOUND_MASK_LINE1;
  1275. waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
  1276. recdev_l = ADC_MUX_MIC;
  1277. recdev_r = ADC_MUX_NONE;
  1278. } else if (recmask & SOUND_MASK_PHONEIN) {
  1279. recmask = SOUND_MASK_PHONEIN;
  1280. waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
  1281. recdev_l = ADC_MUX_AUX1;
  1282. recdev_r = ADC_MUX_NONE;
  1283. } else if (recmask & SOUND_MASK_MIC) {
  1284. recmask = SOUND_MASK_MIC;
  1285. waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
  1286. recdev_l = ADC_MUX_NONE;
  1287. recdev_r = ADC_MUX_MIC;
  1288. }
  1289. *dev_l = recdev_l;
  1290. *dev_r = recdev_r;
  1291. return recmask;
  1292. }
  1293. static int
  1294. netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
  1295.        unsigned char lev_r)
  1296. {
  1297. switch (dev) {
  1298. case SOUND_MIXER_VOLUME:
  1299. case SOUND_MIXER_SYNTH:
  1300. case SOUND_MIXER_PCM:
  1301. case SOUND_MIXER_LINE:
  1302. case SOUND_MIXER_IGAIN:
  1303. devc->levels[dev] = lev_l | lev_r << 8;
  1304. break;
  1305. case SOUND_MIXER_MIC: /* right mic only */
  1306. devc->levels[SOUND_MIXER_MIC] &= 0xff;
  1307. devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
  1308. break;
  1309. case SOUND_MIXER_LINE1: /* left mic only  */
  1310. devc->levels[SOUND_MIXER_MIC] &= 0xff00;
  1311. devc->levels[SOUND_MIXER_MIC] |= lev_l;
  1312. dev = SOUND_MIXER_MIC;
  1313. break;
  1314. case SOUND_MIXER_PHONEIN: /* left aux only  */
  1315. devc->levels[SOUND_MIXER_LINE1] = lev_l;
  1316. dev = SOUND_MIXER_LINE1;
  1317. break;
  1318. case SOUND_MIXER_IMIX:
  1319. case SOUND_MIXER_PHONEOUT:
  1320. break;
  1321. default:
  1322. dev = -EINVAL;
  1323. break;
  1324. }
  1325. return dev;
  1326. }
  1327. static int netwinder_get_mixer(wavnc_info *devc, int dev)
  1328. {
  1329. int levels;
  1330. switch (dev) {
  1331. case SOUND_MIXER_VOLUME:
  1332. case SOUND_MIXER_SYNTH:
  1333. case SOUND_MIXER_PCM:
  1334. case SOUND_MIXER_LINE:
  1335. case SOUND_MIXER_IGAIN:
  1336. levels = devc->levels[dev];
  1337. break;
  1338. case SOUND_MIXER_MIC: /* builtin mic: right mic only */
  1339. levels = devc->levels[SOUND_MIXER_MIC] >> 8;
  1340. levels |= levels << 8;
  1341. break;
  1342. case SOUND_MIXER_LINE1: /* handset mic: left mic only */
  1343. levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
  1344. levels |= levels << 8;
  1345. break;
  1346. case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
  1347. levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
  1348. levels |= levels << 8;
  1349. break;
  1350. default:
  1351. levels = 0;
  1352. }
  1353. return levels;
  1354. }
  1355. /*
  1356.  * Waveartist specific mixer information.
  1357.  */
  1358. static const struct waveartist_mixer_info netwinder_mixer = {
  1359. supported_devs: SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
  1360. SOUND_MASK_PCM     | SOUND_MASK_SPEAKER |
  1361. SOUND_MASK_LINE    | SOUND_MASK_MIC     |
  1362. SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
  1363. SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
  1364. SOUND_MASK_IGAIN,
  1365. recording_devs: SOUND_MASK_LINE    | SOUND_MASK_MIC     |
  1366. SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
  1367. SOUND_MASK_PHONEIN,
  1368. stereo_devs: SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
  1369. SOUND_MASK_PCM     | SOUND_MASK_LINE    |
  1370. SOUND_MASK_IMIX    | SOUND_MASK_IGAIN,
  1371. select_input: netwinder_select_input,
  1372. decode_mixer: netwinder_decode_mixer,
  1373. get_mixer: netwinder_get_mixer,
  1374. };
  1375. static void
  1376. vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
  1377. {
  1378. if (!devc->no_autoselect) {
  1379. if (devc->handset_detect) {
  1380. recmask = SOUND_MASK_LINE1;
  1381. devc->spkr_mute_state = devc->line_mute_state = 1;
  1382. } else if (devc->telephone_detect) {
  1383. recmask = SOUND_MASK_PHONEIN;
  1384. devc->spkr_mute_state = devc->line_mute_state = 1;
  1385. } else {
  1386. /* unless someone has asked for LINE-IN,
  1387.  * we default to MIC
  1388.  */
  1389. if ((devc->recmask & SOUND_MASK_LINE) == 0)
  1390. devc->recmask = SOUND_MASK_MIC;
  1391. devc->spkr_mute_state = devc->line_mute_state = 0;
  1392. }
  1393. vnc_mute_spkr(devc);
  1394. vnc_mute_lout(devc);
  1395. if (recmask != devc->recmask)
  1396. waveartist_set_recmask(devc, recmask);
  1397. }
  1398. }
  1399. static int
  1400. vnc_slider(wavnc_info *devc)
  1401. {
  1402. signed int slider_volume;
  1403. unsigned int temp, old_hs, old_td;
  1404. /*
  1405.  * read the "buttons" state.
  1406.  *  Bit 4 = 0 means handset present
  1407.  *  Bit 5 = 1 means phone offhook
  1408.  */
  1409. temp = inb(0x201);
  1410. old_hs = devc->handset_detect;
  1411. old_td = devc->telephone_detect;
  1412. devc->handset_detect = !(temp & 0x10);
  1413. devc->telephone_detect = !!(temp & 0x20);
  1414. if (!devc->no_autoselect &&
  1415.     (old_hs != devc->handset_detect ||
  1416.      old_td != devc->telephone_detect))
  1417. vnc_configure_mixer(devc, devc->recmask);
  1418. slider_volume = vnc_volume_slider(devc);
  1419. /*
  1420.  * If we're using software controlled volume, and
  1421.  * the slider moves by more than 20%, then we
  1422.  * switch back to slider controlled volume.
  1423.  */
  1424. if (abs(devc->slider_vol - slider_volume) > 20)
  1425. devc->use_slider = 1;
  1426. /*
  1427.  * use only left channel
  1428.  */
  1429. temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
  1430. if (slider_volume != temp && devc->use_slider) {
  1431. devc->slider_vol = slider_volume;
  1432. waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
  1433. slider_volume | slider_volume << 8);
  1434. return 1;
  1435. }
  1436. return 0;
  1437. }
  1438. static void
  1439. vnc_slider_tick(unsigned long data)
  1440. {
  1441. int next_timeout;
  1442. if (vnc_slider(adev_info + data))
  1443. next_timeout = 5; // mixer reported change
  1444. else
  1445. next_timeout = VNC_TIMER_PERIOD;
  1446. mod_timer(&vnc_timer, jiffies + next_timeout);
  1447. }
  1448. static int
  1449. vnc_private_ioctl(int dev, unsigned int cmd, caddr_t arg)
  1450. {
  1451. wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
  1452. int val;
  1453. switch (cmd) {
  1454. case SOUND_MIXER_PRIVATE1:
  1455. {
  1456. u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
  1457. int val;
  1458. if (get_user(val, (int *)arg))
  1459. return -EFAULT;
  1460. /* check if parameter is logical */
  1461. if (val & ~(VNC_MUTE_INTERNAL_SPKR |
  1462.     VNC_MUTE_LINE_OUT |
  1463.     VNC_DISABLE_AUTOSWITCH))
  1464. return -EINVAL;
  1465. prev_auto_state = devc->no_autoselect;
  1466. prev_spkr_mute  = devc->spkr_mute_state;
  1467. prev_line_mute  = devc->line_mute_state;
  1468. devc->no_autoselect   = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
  1469. devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
  1470. devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
  1471. if (prev_spkr_mute != devc->spkr_mute_state)
  1472. vnc_mute_spkr(devc);
  1473. if (prev_line_mute != devc->line_mute_state)
  1474. vnc_mute_lout(devc);
  1475. if (prev_auto_state != devc->no_autoselect)
  1476. vnc_configure_mixer(devc, devc->recmask);
  1477. return 0;
  1478. }
  1479. case SOUND_MIXER_PRIVATE2:
  1480. if (get_user(val, (int *)arg))
  1481. return -EFAULT;
  1482. switch (val) {
  1483. #define VNC_SOUND_PAUSE         0x53    //to pause the DSP
  1484. #define VNC_SOUND_RESUME        0x57    //to unpause the DSP
  1485. case VNC_SOUND_PAUSE:
  1486. waveartist_cmd1(devc, 0x16);
  1487. break;
  1488. case VNC_SOUND_RESUME:
  1489. waveartist_cmd1(devc, 0x18);
  1490. break;
  1491. default:
  1492. return -EINVAL;
  1493. }
  1494. return 0;
  1495. /* private ioctl to allow bulk access to waveartist */
  1496. case SOUND_MIXER_PRIVATE3:
  1497. {
  1498. unsigned long flags;
  1499. int mixer_reg[15], i, val;
  1500. if (get_user(val, (int *)arg))
  1501. return -EFAULT;
  1502. if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
  1503. return -EFAULT;
  1504. switch (mixer_reg[14]) {
  1505. case MIXER_PRIVATE3_RESET:
  1506. waveartist_mixer_reset(devc);
  1507. break;
  1508. case MIXER_PRIVATE3_WRITE:
  1509. waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
  1510. waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
  1511. waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
  1512. waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
  1513. waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
  1514. waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
  1515. waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
  1516. break;
  1517. case MIXER_PRIVATE3_READ:
  1518. spin_lock_irqsave(&waveartist_lock, flags);
  1519. for (i = 0x30; i < 14 << 8; i += 1 << 8)
  1520. waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
  1521. spin_unlock_irqrestore(&waveartist_lock, flags);
  1522. if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
  1523. return -EFAULT;
  1524. break;
  1525. default:
  1526. return -EINVAL;
  1527. }
  1528. return 0;
  1529. }
  1530. /* read back the state from PRIVATE1 */
  1531. case SOUND_MIXER_PRIVATE4:
  1532. val = (devc->spkr_mute_state  ? VNC_MUTE_INTERNAL_SPKR : 0) |
  1533.       (devc->line_mute_state  ? VNC_MUTE_LINE_OUT      : 0) |
  1534.       (devc->handset_detect   ? VNC_HANDSET_DETECT     : 0) |
  1535.       (devc->telephone_detect ? VNC_PHONE_DETECT       : 0) |
  1536.       (devc->no_autoselect    ? VNC_DISABLE_AUTOSWITCH : 0);
  1537. return put_user(val, (int *)arg) ? -EFAULT : 0;
  1538. }
  1539. if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
  1540. /*
  1541.  * special case for master volume: if we
  1542.  * received this call - switch from hw
  1543.  * volume control to a software volume
  1544.  * control, till the hw volume is modified
  1545.  * to signal that user wants to be back in
  1546.  * hardware...
  1547.  */
  1548. if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
  1549. devc->use_slider = 0;
  1550. /* speaker output            */
  1551. if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
  1552. unsigned int val, l, r;
  1553. if (get_user(val, (int *)arg))
  1554. return -EFAULT;
  1555. l = val & 0x7f;
  1556. r = (val & 0x7f00) >> 8;
  1557. val = (l + r) / 2;
  1558. devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
  1559. devc->spkr_mute_state = (val <= 50);
  1560. vnc_mute_spkr(devc);
  1561. return 0;
  1562. }
  1563. }
  1564. return -ENOIOCTLCMD;
  1565. }
  1566. #endif
  1567. static struct address_info cfg;
  1568. static int attached;
  1569. static int __initdata io = 0;
  1570. static int __initdata irq = 0;
  1571. static int __initdata dma = 0;
  1572. static int __initdata dma2 = 0;
  1573. static int __init init_waveartist(void)
  1574. {
  1575. const struct waveartist_mixer_info *mix;
  1576. if (!io && machine_is_netwinder()) {
  1577. /*
  1578.  * The NetWinder WaveArtist is at a fixed address.
  1579.  * If the user does not supply an address, use the
  1580.  * well-known parameters.
  1581.  */
  1582. io   = 0x250;
  1583. irq  = 12;
  1584. dma  = 3;
  1585. dma2 = 7;
  1586. }
  1587. mix = &waveartist_mixer;
  1588. #ifdef CONFIG_ARCH_NETWINDER
  1589. if (machine_is_netwinder())
  1590. mix = &netwinder_mixer;
  1591. #endif
  1592. cfg.io_base = io;
  1593. cfg.irq = irq;
  1594. cfg.dma = dma;
  1595. cfg.dma2 = dma2;
  1596. if (!probe_waveartist(&cfg))
  1597. return -ENODEV;
  1598. attach_waveartist(&cfg, mix);
  1599. attached = 1;
  1600. return 0;
  1601. }
  1602. static void __exit cleanup_waveartist(void)
  1603. {
  1604. if (attached)
  1605. unload_waveartist(&cfg);
  1606. }
  1607. module_init(init_waveartist);
  1608. module_exit(cleanup_waveartist);
  1609. #ifndef MODULE
  1610. static int __init setup_waveartist(char *str)
  1611. {
  1612. /* io, irq, dma, dma2 */
  1613. int ints[5];
  1614. str = get_options(str, ARRAY_SIZE(ints), ints);
  1615. io = ints[1];
  1616. irq = ints[2];
  1617. dma = ints[3];
  1618. dma2 = ints[4];
  1619. return 1;
  1620. }
  1621. __setup("waveartist=", setup_waveartist);
  1622. #endif
  1623. MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
  1624. MODULE_PARM(io, "i"); /* IO base */
  1625. MODULE_PARM(irq, "i"); /* IRQ */
  1626. MODULE_PARM(dma, "i"); /* DMA */
  1627. MODULE_PARM(dma2, "i"); /* DMA2 */
  1628. MODULE_LICENSE("GPL");