PageRenderTime 77ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/sound/oss/ad1848.c

https://bitbucket.org/abioy/linux
C | 3069 lines | 2251 code | 480 blank | 338 comment | 558 complexity | cdf106b686bfe9ed8abdf5f0479692eb MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * sound/oss/ad1848.c
  3. *
  4. * The low level driver for the AD1848/CS4248 codec chip which
  5. * is used for example in the MS Sound System.
  6. *
  7. * The CS4231 which is used in the GUS MAX and some other cards is
  8. * upwards compatible with AD1848 and this driver is able to drive it.
  9. *
  10. * CS4231A and AD1845 are upward compatible with CS4231. However
  11. * the new features of these chips are different.
  12. *
  13. * CS4232 is a PnP audio chip which contains a CS4231A (and SB, MPU).
  14. * CS4232A is an improved version of CS4232.
  15. *
  16. *
  17. *
  18. * Copyright (C) by Hannu Savolainen 1993-1997
  19. *
  20. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  21. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  22. * for more info.
  23. *
  24. *
  25. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  26. * general sleep/wakeup clean up.
  27. * Alan Cox : reformatted. Fixed SMP bugs. Moved to kernel alloc/free
  28. * of irqs. Use dev_id.
  29. * Christoph Hellwig : adapted to module_init/module_exit
  30. * Aki Laukkanen : added power management support
  31. * Arnaldo C. de Melo : added missing restore_flags in ad1848_resume
  32. * Miguel Freitas : added ISA PnP support
  33. * Alan Cox : Added CS4236->4239 identification
  34. * Daniel T. Cobra : Alernate config/mixer for later chips
  35. * Alan Cox : Merged chip idents and config code
  36. *
  37. * TODO
  38. * APM save restore assist code on IBM thinkpad
  39. *
  40. * Status:
  41. * Tested. Believed fully functional.
  42. */
  43. #include <linux/init.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/module.h>
  46. #include <linux/stddef.h>
  47. #include <linux/slab.h>
  48. #include <linux/isapnp.h>
  49. #include <linux/pnp.h>
  50. #include <linux/spinlock.h>
  51. #define DEB(x)
  52. #define DEB1(x)
  53. #include "sound_config.h"
  54. #include "ad1848.h"
  55. #include "ad1848_mixer.h"
  56. typedef struct
  57. {
  58. spinlock_t lock;
  59. int base;
  60. int irq;
  61. int dma1, dma2;
  62. int dual_dma; /* 1, when two DMA channels allocated */
  63. int subtype;
  64. unsigned char MCE_bit;
  65. unsigned char saved_regs[64]; /* Includes extended register space */
  66. int debug_flag;
  67. int audio_flags;
  68. int record_dev, playback_dev;
  69. int xfer_count;
  70. int audio_mode;
  71. int open_mode;
  72. int intr_active;
  73. char *chip_name, *name;
  74. int model;
  75. #define MD_1848 1
  76. #define MD_4231 2
  77. #define MD_4231A 3
  78. #define MD_1845 4
  79. #define MD_4232 5
  80. #define MD_C930 6
  81. #define MD_IWAVE 7
  82. #define MD_4235 8 /* Crystal Audio CS4235 */
  83. #define MD_1845_SSCAPE 9 /* Ensoniq Soundscape PNP*/
  84. #define MD_4236 10 /* 4236 and higher */
  85. #define MD_42xB 11 /* CS 42xB */
  86. #define MD_4239 12 /* CS4239 */
  87. /* Mixer parameters */
  88. int recmask;
  89. int supported_devices, orig_devices;
  90. int supported_rec_devices, orig_rec_devices;
  91. int *levels;
  92. short mixer_reroute[32];
  93. int dev_no;
  94. volatile unsigned long timer_ticks;
  95. int timer_running;
  96. int irq_ok;
  97. mixer_ents *mix_devices;
  98. int mixer_output_port;
  99. } ad1848_info;
  100. typedef struct ad1848_port_info
  101. {
  102. int open_mode;
  103. int speed;
  104. unsigned char speed_bits;
  105. int channels;
  106. int audio_format;
  107. unsigned char format_bits;
  108. }
  109. ad1848_port_info;
  110. static struct address_info cfg;
  111. static int nr_ad1848_devs;
  112. static int deskpro_xl;
  113. static int deskpro_m;
  114. static int soundpro;
  115. static volatile signed char irq2dev[17] = {
  116. -1, -1, -1, -1, -1, -1, -1, -1,
  117. -1, -1, -1, -1, -1, -1, -1, -1, -1
  118. };
  119. #ifndef EXCLUDE_TIMERS
  120. static int timer_installed = -1;
  121. #endif
  122. static int loaded;
  123. static int ad_format_mask[13 /*devc->model */ ] =
  124. {
  125. 0,
  126. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
  127. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  128. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  129. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW, /* AD1845 */
  130. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  131. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  132. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  133. AFMT_U8 | AFMT_S16_LE /* CS4235 */,
  134. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW /* Ensoniq Soundscape*/,
  135. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  136. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  137. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM
  138. };
  139. static ad1848_info adev_info[MAX_AUDIO_DEV];
  140. #define io_Index_Addr(d) ((d)->base)
  141. #define io_Indexed_Data(d) ((d)->base+1)
  142. #define io_Status(d) ((d)->base+2)
  143. #define io_Polled_IO(d) ((d)->base+3)
  144. static struct {
  145. unsigned char flags;
  146. #define CAP_F_TIMER 0x01
  147. } capabilities [10 /*devc->model */ ] = {
  148. {0}
  149. ,{0} /* MD_1848 */
  150. ,{CAP_F_TIMER} /* MD_4231 */
  151. ,{CAP_F_TIMER} /* MD_4231A */
  152. ,{CAP_F_TIMER} /* MD_1845 */
  153. ,{CAP_F_TIMER} /* MD_4232 */
  154. ,{0} /* MD_C930 */
  155. ,{CAP_F_TIMER} /* MD_IWAVE */
  156. ,{0} /* MD_4235 */
  157. ,{CAP_F_TIMER} /* MD_1845_SSCAPE */
  158. };
  159. #ifdef CONFIG_PNP
  160. static int isapnp = 1;
  161. static int isapnpjump;
  162. static int reverse;
  163. static int audio_activated;
  164. #else
  165. static int isapnp;
  166. #endif
  167. static int ad1848_open(int dev, int mode);
  168. static void ad1848_close(int dev);
  169. static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag);
  170. static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag);
  171. static int ad1848_prepare_for_output(int dev, int bsize, int bcount);
  172. static int ad1848_prepare_for_input(int dev, int bsize, int bcount);
  173. static void ad1848_halt(int dev);
  174. static void ad1848_halt_input(int dev);
  175. static void ad1848_halt_output(int dev);
  176. static void ad1848_trigger(int dev, int bits);
  177. static irqreturn_t adintr(int irq, void *dev_id);
  178. #ifndef EXCLUDE_TIMERS
  179. static int ad1848_tmr_install(int dev);
  180. static void ad1848_tmr_reprogram(int dev);
  181. #endif
  182. static int ad_read(ad1848_info * devc, int reg)
  183. {
  184. int x;
  185. int timeout = 900000;
  186. while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
  187. timeout--;
  188. if(reg < 32)
  189. {
  190. outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  191. x = inb(io_Indexed_Data(devc));
  192. }
  193. else
  194. {
  195. int xreg, xra;
  196. xreg = (reg & 0xff) - 32;
  197. xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
  198. outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  199. outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
  200. x = inb(io_Indexed_Data(devc));
  201. }
  202. return x;
  203. }
  204. static void ad_write(ad1848_info * devc, int reg, int data)
  205. {
  206. int timeout = 900000;
  207. while (timeout > 0 && inb(devc->base) == 0x80) /* Are we initializing */
  208. timeout--;
  209. if(reg < 32)
  210. {
  211. outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  212. outb(((unsigned char) (data & 0xff)), io_Indexed_Data(devc));
  213. }
  214. else
  215. {
  216. int xreg, xra;
  217. xreg = (reg & 0xff) - 32;
  218. xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
  219. outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  220. outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
  221. outb((unsigned char) (data & 0xff), io_Indexed_Data(devc));
  222. }
  223. }
  224. static void wait_for_calibration(ad1848_info * devc)
  225. {
  226. int timeout = 0;
  227. /*
  228. * Wait until the auto calibration process has finished.
  229. *
  230. * 1) Wait until the chip becomes ready (reads don't return 0x80).
  231. * 2) Wait until the ACI bit of I11 gets on and then off.
  232. */
  233. timeout = 100000;
  234. while (timeout > 0 && inb(devc->base) == 0x80)
  235. timeout--;
  236. if (inb(devc->base) & 0x80)
  237. printk(KERN_WARNING "ad1848: Auto calibration timed out(1).\n");
  238. timeout = 100;
  239. while (timeout > 0 && !(ad_read(devc, 11) & 0x20))
  240. timeout--;
  241. if (!(ad_read(devc, 11) & 0x20))
  242. return;
  243. timeout = 80000;
  244. while (timeout > 0 && (ad_read(devc, 11) & 0x20))
  245. timeout--;
  246. if (ad_read(devc, 11) & 0x20)
  247. if ((devc->model != MD_1845) && (devc->model != MD_1845_SSCAPE))
  248. printk(KERN_WARNING "ad1848: Auto calibration timed out(3).\n");
  249. }
  250. static void ad_mute(ad1848_info * devc)
  251. {
  252. int i;
  253. unsigned char prev;
  254. /*
  255. * Save old register settings and mute output channels
  256. */
  257. for (i = 6; i < 8; i++)
  258. {
  259. prev = devc->saved_regs[i] = ad_read(devc, i);
  260. }
  261. }
  262. static void ad_unmute(ad1848_info * devc)
  263. {
  264. }
  265. static void ad_enter_MCE(ad1848_info * devc)
  266. {
  267. int timeout = 1000;
  268. unsigned short prev;
  269. while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
  270. timeout--;
  271. devc->MCE_bit = 0x40;
  272. prev = inb(io_Index_Addr(devc));
  273. if (prev & 0x40)
  274. {
  275. return;
  276. }
  277. outb((devc->MCE_bit), io_Index_Addr(devc));
  278. }
  279. static void ad_leave_MCE(ad1848_info * devc)
  280. {
  281. unsigned char prev, acal;
  282. int timeout = 1000;
  283. while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
  284. timeout--;
  285. acal = ad_read(devc, 9);
  286. devc->MCE_bit = 0x00;
  287. prev = inb(io_Index_Addr(devc));
  288. outb((0x00), io_Index_Addr(devc)); /* Clear the MCE bit */
  289. if ((prev & 0x40) == 0) /* Not in MCE mode */
  290. {
  291. return;
  292. }
  293. outb((0x00), io_Index_Addr(devc)); /* Clear the MCE bit */
  294. if (acal & 0x08) /* Auto calibration is enabled */
  295. wait_for_calibration(devc);
  296. }
  297. static int ad1848_set_recmask(ad1848_info * devc, int mask)
  298. {
  299. unsigned char recdev;
  300. int i, n;
  301. unsigned long flags;
  302. mask &= devc->supported_rec_devices;
  303. /* Rename the mixer bits if necessary */
  304. for (i = 0; i < 32; i++)
  305. {
  306. if (devc->mixer_reroute[i] != i)
  307. {
  308. if (mask & (1 << i))
  309. {
  310. mask &= ~(1 << i);
  311. mask |= (1 << devc->mixer_reroute[i]);
  312. }
  313. }
  314. }
  315. n = 0;
  316. for (i = 0; i < 32; i++) /* Count selected device bits */
  317. if (mask & (1 << i))
  318. n++;
  319. spin_lock_irqsave(&devc->lock,flags);
  320. if (!soundpro) {
  321. if (n == 0)
  322. mask = SOUND_MASK_MIC;
  323. else if (n != 1) { /* Too many devices selected */
  324. mask &= ~devc->recmask; /* Filter out active settings */
  325. n = 0;
  326. for (i = 0; i < 32; i++) /* Count selected device bits */
  327. if (mask & (1 << i))
  328. n++;
  329. if (n != 1)
  330. mask = SOUND_MASK_MIC;
  331. }
  332. switch (mask) {
  333. case SOUND_MASK_MIC:
  334. recdev = 2;
  335. break;
  336. case SOUND_MASK_LINE:
  337. case SOUND_MASK_LINE3:
  338. recdev = 0;
  339. break;
  340. case SOUND_MASK_CD:
  341. case SOUND_MASK_LINE1:
  342. recdev = 1;
  343. break;
  344. case SOUND_MASK_IMIX:
  345. recdev = 3;
  346. break;
  347. default:
  348. mask = SOUND_MASK_MIC;
  349. recdev = 2;
  350. }
  351. recdev <<= 6;
  352. ad_write(devc, 0, (ad_read(devc, 0) & 0x3f) | recdev);
  353. ad_write(devc, 1, (ad_read(devc, 1) & 0x3f) | recdev);
  354. } else { /* soundpro */
  355. unsigned char val;
  356. int set_rec_bit;
  357. int j;
  358. for (i = 0; i < 32; i++) { /* For each bit */
  359. if ((devc->supported_rec_devices & (1 << i)) == 0)
  360. continue; /* Device not supported */
  361. for (j = LEFT_CHN; j <= RIGHT_CHN; j++) {
  362. if (devc->mix_devices[i][j].nbits == 0) /* Inexistent channel */
  363. continue;
  364. /*
  365. * This is tricky:
  366. * set_rec_bit becomes 1 if the corresponding bit in mask is set
  367. * then it gets flipped if the polarity is inverse
  368. */
  369. set_rec_bit = ((mask & (1 << i)) != 0) ^ devc->mix_devices[i][j].recpol;
  370. val = ad_read(devc, devc->mix_devices[i][j].recreg);
  371. val &= ~(1 << devc->mix_devices[i][j].recpos);
  372. val |= (set_rec_bit << devc->mix_devices[i][j].recpos);
  373. ad_write(devc, devc->mix_devices[i][j].recreg, val);
  374. }
  375. }
  376. }
  377. spin_unlock_irqrestore(&devc->lock,flags);
  378. /* Rename the mixer bits back if necessary */
  379. for (i = 0; i < 32; i++)
  380. {
  381. if (devc->mixer_reroute[i] != i)
  382. {
  383. if (mask & (1 << devc->mixer_reroute[i]))
  384. {
  385. mask &= ~(1 << devc->mixer_reroute[i]);
  386. mask |= (1 << i);
  387. }
  388. }
  389. }
  390. devc->recmask = mask;
  391. return mask;
  392. }
  393. static void change_bits(ad1848_info * devc, unsigned char *regval,
  394. unsigned char *muteval, int dev, int chn, int newval)
  395. {
  396. unsigned char mask;
  397. int shift;
  398. int mute;
  399. int mutemask;
  400. int set_mute_bit;
  401. set_mute_bit = (newval == 0) ^ devc->mix_devices[dev][chn].mutepol;
  402. if (devc->mix_devices[dev][chn].polarity == 1) /* Reverse */
  403. newval = 100 - newval;
  404. mask = (1 << devc->mix_devices[dev][chn].nbits) - 1;
  405. shift = devc->mix_devices[dev][chn].bitpos;
  406. if (devc->mix_devices[dev][chn].mutepos == 8)
  407. { /* if there is no mute bit */
  408. mute = 0; /* No mute bit; do nothing special */
  409. mutemask = ~0; /* No mute bit; do nothing special */
  410. }
  411. else
  412. {
  413. mute = (set_mute_bit << devc->mix_devices[dev][chn].mutepos);
  414. mutemask = ~(1 << devc->mix_devices[dev][chn].mutepos);
  415. }
  416. newval = (int) ((newval * mask) + 50) / 100; /* Scale it */
  417. *regval &= ~(mask << shift); /* Clear bits */
  418. *regval |= (newval & mask) << shift; /* Set new value */
  419. *muteval &= mutemask;
  420. *muteval |= mute;
  421. }
  422. static int ad1848_mixer_get(ad1848_info * devc, int dev)
  423. {
  424. if (!((1 << dev) & devc->supported_devices))
  425. return -EINVAL;
  426. dev = devc->mixer_reroute[dev];
  427. return devc->levels[dev];
  428. }
  429. static void ad1848_mixer_set_channel(ad1848_info *devc, int dev, int value, int channel)
  430. {
  431. int regoffs, muteregoffs;
  432. unsigned char val, muteval;
  433. unsigned long flags;
  434. regoffs = devc->mix_devices[dev][channel].regno;
  435. muteregoffs = devc->mix_devices[dev][channel].mutereg;
  436. val = ad_read(devc, regoffs);
  437. if (muteregoffs != regoffs) {
  438. muteval = ad_read(devc, muteregoffs);
  439. change_bits(devc, &val, &muteval, dev, channel, value);
  440. }
  441. else
  442. change_bits(devc, &val, &val, dev, channel, value);
  443. spin_lock_irqsave(&devc->lock,flags);
  444. ad_write(devc, regoffs, val);
  445. devc->saved_regs[regoffs] = val;
  446. if (muteregoffs != regoffs) {
  447. ad_write(devc, muteregoffs, muteval);
  448. devc->saved_regs[muteregoffs] = muteval;
  449. }
  450. spin_unlock_irqrestore(&devc->lock,flags);
  451. }
  452. static int ad1848_mixer_set(ad1848_info * devc, int dev, int value)
  453. {
  454. int left = value & 0x000000ff;
  455. int right = (value & 0x0000ff00) >> 8;
  456. int retvol;
  457. if (dev > 31)
  458. return -EINVAL;
  459. if (!(devc->supported_devices & (1 << dev)))
  460. return -EINVAL;
  461. dev = devc->mixer_reroute[dev];
  462. if (devc->mix_devices[dev][LEFT_CHN].nbits == 0)
  463. return -EINVAL;
  464. if (left > 100)
  465. left = 100;
  466. if (right > 100)
  467. right = 100;
  468. if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0) /* Mono control */
  469. right = left;
  470. retvol = left | (right << 8);
  471. /* Scale volumes */
  472. left = mix_cvt[left];
  473. right = mix_cvt[right];
  474. devc->levels[dev] = retvol;
  475. /*
  476. * Set the left channel
  477. */
  478. ad1848_mixer_set_channel(devc, dev, left, LEFT_CHN);
  479. /*
  480. * Set the right channel
  481. */
  482. if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0)
  483. goto out;
  484. ad1848_mixer_set_channel(devc, dev, right, RIGHT_CHN);
  485. out:
  486. return retvol;
  487. }
  488. static void ad1848_mixer_reset(ad1848_info * devc)
  489. {
  490. int i;
  491. char name[32];
  492. unsigned long flags;
  493. devc->mix_devices = &(ad1848_mix_devices[0]);
  494. sprintf(name, "%s_%d", devc->chip_name, nr_ad1848_devs);
  495. for (i = 0; i < 32; i++)
  496. devc->mixer_reroute[i] = i;
  497. devc->supported_rec_devices = MODE1_REC_DEVICES;
  498. switch (devc->model)
  499. {
  500. case MD_4231:
  501. case MD_4231A:
  502. case MD_1845:
  503. case MD_1845_SSCAPE:
  504. devc->supported_devices = MODE2_MIXER_DEVICES;
  505. break;
  506. case MD_C930:
  507. devc->supported_devices = C930_MIXER_DEVICES;
  508. devc->mix_devices = &(c930_mix_devices[0]);
  509. break;
  510. case MD_IWAVE:
  511. devc->supported_devices = MODE3_MIXER_DEVICES;
  512. devc->mix_devices = &(iwave_mix_devices[0]);
  513. break;
  514. case MD_42xB:
  515. case MD_4239:
  516. devc->mix_devices = &(cs42xb_mix_devices[0]);
  517. devc->supported_devices = MODE3_MIXER_DEVICES;
  518. break;
  519. case MD_4232:
  520. case MD_4235:
  521. case MD_4236:
  522. devc->supported_devices = MODE3_MIXER_DEVICES;
  523. break;
  524. case MD_1848:
  525. if (soundpro) {
  526. devc->supported_devices = SPRO_MIXER_DEVICES;
  527. devc->supported_rec_devices = SPRO_REC_DEVICES;
  528. devc->mix_devices = &(spro_mix_devices[0]);
  529. break;
  530. }
  531. default:
  532. devc->supported_devices = MODE1_MIXER_DEVICES;
  533. }
  534. devc->orig_devices = devc->supported_devices;
  535. devc->orig_rec_devices = devc->supported_rec_devices;
  536. devc->levels = load_mixer_volumes(name, default_mixer_levels, 1);
  537. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  538. {
  539. if (devc->supported_devices & (1 << i))
  540. ad1848_mixer_set(devc, i, devc->levels[i]);
  541. }
  542. ad1848_set_recmask(devc, SOUND_MASK_MIC);
  543. devc->mixer_output_port = devc->levels[31] | AUDIO_HEADPHONE | AUDIO_LINE_OUT;
  544. spin_lock_irqsave(&devc->lock,flags);
  545. if (!soundpro) {
  546. if (devc->mixer_output_port & AUDIO_SPEAKER)
  547. ad_write(devc, 26, ad_read(devc, 26) & ~0x40); /* Unmute mono out */
  548. else
  549. ad_write(devc, 26, ad_read(devc, 26) | 0x40); /* Mute mono out */
  550. } else {
  551. /*
  552. * From the "wouldn't it be nice if the mixer API had (better)
  553. * support for custom stuff" category
  554. */
  555. /* Enable surround mode and SB16 mixer */
  556. ad_write(devc, 16, 0x60);
  557. }
  558. spin_unlock_irqrestore(&devc->lock,flags);
  559. }
  560. static int ad1848_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
  561. {
  562. ad1848_info *devc = mixer_devs[dev]->devc;
  563. int val;
  564. if (cmd == SOUND_MIXER_PRIVATE1)
  565. {
  566. if (get_user(val, (int __user *)arg))
  567. return -EFAULT;
  568. if (val != 0xffff)
  569. {
  570. unsigned long flags;
  571. val &= (AUDIO_SPEAKER | AUDIO_HEADPHONE | AUDIO_LINE_OUT);
  572. devc->mixer_output_port = val;
  573. val |= AUDIO_HEADPHONE | AUDIO_LINE_OUT; /* Always on */
  574. devc->mixer_output_port = val;
  575. spin_lock_irqsave(&devc->lock,flags);
  576. if (val & AUDIO_SPEAKER)
  577. ad_write(devc, 26, ad_read(devc, 26) & ~0x40); /* Unmute mono out */
  578. else
  579. ad_write(devc, 26, ad_read(devc, 26) | 0x40); /* Mute mono out */
  580. spin_unlock_irqrestore(&devc->lock,flags);
  581. }
  582. val = devc->mixer_output_port;
  583. return put_user(val, (int __user *)arg);
  584. }
  585. if (cmd == SOUND_MIXER_PRIVATE2)
  586. {
  587. if (get_user(val, (int __user *)arg))
  588. return -EFAULT;
  589. return(ad1848_control(AD1848_MIXER_REROUTE, val));
  590. }
  591. if (((cmd >> 8) & 0xff) == 'M')
  592. {
  593. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  594. {
  595. switch (cmd & 0xff)
  596. {
  597. case SOUND_MIXER_RECSRC:
  598. if (get_user(val, (int __user *)arg))
  599. return -EFAULT;
  600. val = ad1848_set_recmask(devc, val);
  601. break;
  602. default:
  603. if (get_user(val, (int __user *)arg))
  604. return -EFAULT;
  605. val = ad1848_mixer_set(devc, cmd & 0xff, val);
  606. break;
  607. }
  608. return put_user(val, (int __user *)arg);
  609. }
  610. else
  611. {
  612. switch (cmd & 0xff)
  613. {
  614. /*
  615. * Return parameters
  616. */
  617. case SOUND_MIXER_RECSRC:
  618. val = devc->recmask;
  619. break;
  620. case SOUND_MIXER_DEVMASK:
  621. val = devc->supported_devices;
  622. break;
  623. case SOUND_MIXER_STEREODEVS:
  624. val = devc->supported_devices;
  625. if (devc->model != MD_C930)
  626. val &= ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
  627. break;
  628. case SOUND_MIXER_RECMASK:
  629. val = devc->supported_rec_devices;
  630. break;
  631. case SOUND_MIXER_CAPS:
  632. val=SOUND_CAP_EXCL_INPUT;
  633. break;
  634. default:
  635. val = ad1848_mixer_get(devc, cmd & 0xff);
  636. break;
  637. }
  638. return put_user(val, (int __user *)arg);
  639. }
  640. }
  641. else
  642. return -EINVAL;
  643. }
  644. static int ad1848_set_speed(int dev, int arg)
  645. {
  646. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  647. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  648. /*
  649. * The sampling speed is encoded in the least significant nibble of I8. The
  650. * LSB selects the clock source (0=24.576 MHz, 1=16.9344 MHz) and other
  651. * three bits select the divisor (indirectly):
  652. *
  653. * The available speeds are in the following table. Keep the speeds in
  654. * the increasing order.
  655. */
  656. typedef struct
  657. {
  658. int speed;
  659. unsigned char bits;
  660. }
  661. speed_struct;
  662. static speed_struct speed_table[] =
  663. {
  664. {5510, (0 << 1) | 1},
  665. {5510, (0 << 1) | 1},
  666. {6620, (7 << 1) | 1},
  667. {8000, (0 << 1) | 0},
  668. {9600, (7 << 1) | 0},
  669. {11025, (1 << 1) | 1},
  670. {16000, (1 << 1) | 0},
  671. {18900, (2 << 1) | 1},
  672. {22050, (3 << 1) | 1},
  673. {27420, (2 << 1) | 0},
  674. {32000, (3 << 1) | 0},
  675. {33075, (6 << 1) | 1},
  676. {37800, (4 << 1) | 1},
  677. {44100, (5 << 1) | 1},
  678. {48000, (6 << 1) | 0}
  679. };
  680. int i, n, selected = -1;
  681. n = sizeof(speed_table) / sizeof(speed_struct);
  682. if (arg <= 0)
  683. return portc->speed;
  684. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE) /* AD1845 has different timer than others */
  685. {
  686. if (arg < 4000)
  687. arg = 4000;
  688. if (arg > 50000)
  689. arg = 50000;
  690. portc->speed = arg;
  691. portc->speed_bits = speed_table[3].bits;
  692. return portc->speed;
  693. }
  694. if (arg < speed_table[0].speed)
  695. selected = 0;
  696. if (arg > speed_table[n - 1].speed)
  697. selected = n - 1;
  698. for (i = 1 /*really */ ; selected == -1 && i < n; i++)
  699. {
  700. if (speed_table[i].speed == arg)
  701. selected = i;
  702. else if (speed_table[i].speed > arg)
  703. {
  704. int diff1, diff2;
  705. diff1 = arg - speed_table[i - 1].speed;
  706. diff2 = speed_table[i].speed - arg;
  707. if (diff1 < diff2)
  708. selected = i - 1;
  709. else
  710. selected = i;
  711. }
  712. }
  713. if (selected == -1)
  714. {
  715. printk(KERN_WARNING "ad1848: Can't find speed???\n");
  716. selected = 3;
  717. }
  718. portc->speed = speed_table[selected].speed;
  719. portc->speed_bits = speed_table[selected].bits;
  720. return portc->speed;
  721. }
  722. static short ad1848_set_channels(int dev, short arg)
  723. {
  724. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  725. if (arg != 1 && arg != 2)
  726. return portc->channels;
  727. portc->channels = arg;
  728. return arg;
  729. }
  730. static unsigned int ad1848_set_bits(int dev, unsigned int arg)
  731. {
  732. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  733. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  734. static struct format_tbl
  735. {
  736. int format;
  737. unsigned char bits;
  738. }
  739. format2bits[] =
  740. {
  741. {
  742. 0, 0
  743. }
  744. ,
  745. {
  746. AFMT_MU_LAW, 1
  747. }
  748. ,
  749. {
  750. AFMT_A_LAW, 3
  751. }
  752. ,
  753. {
  754. AFMT_IMA_ADPCM, 5
  755. }
  756. ,
  757. {
  758. AFMT_U8, 0
  759. }
  760. ,
  761. {
  762. AFMT_S16_LE, 2
  763. }
  764. ,
  765. {
  766. AFMT_S16_BE, 6
  767. }
  768. ,
  769. {
  770. AFMT_S8, 0
  771. }
  772. ,
  773. {
  774. AFMT_U16_LE, 0
  775. }
  776. ,
  777. {
  778. AFMT_U16_BE, 0
  779. }
  780. };
  781. int i, n = sizeof(format2bits) / sizeof(struct format_tbl);
  782. if (arg == 0)
  783. return portc->audio_format;
  784. if (!(arg & ad_format_mask[devc->model]))
  785. arg = AFMT_U8;
  786. portc->audio_format = arg;
  787. for (i = 0; i < n; i++)
  788. if (format2bits[i].format == arg)
  789. {
  790. if ((portc->format_bits = format2bits[i].bits) == 0)
  791. return portc->audio_format = AFMT_U8; /* Was not supported */
  792. return arg;
  793. }
  794. /* Still hanging here. Something must be terribly wrong */
  795. portc->format_bits = 0;
  796. return portc->audio_format = AFMT_U8;
  797. }
  798. static struct audio_driver ad1848_audio_driver =
  799. {
  800. .owner = THIS_MODULE,
  801. .open = ad1848_open,
  802. .close = ad1848_close,
  803. .output_block = ad1848_output_block,
  804. .start_input = ad1848_start_input,
  805. .prepare_for_input = ad1848_prepare_for_input,
  806. .prepare_for_output = ad1848_prepare_for_output,
  807. .halt_io = ad1848_halt,
  808. .halt_input = ad1848_halt_input,
  809. .halt_output = ad1848_halt_output,
  810. .trigger = ad1848_trigger,
  811. .set_speed = ad1848_set_speed,
  812. .set_bits = ad1848_set_bits,
  813. .set_channels = ad1848_set_channels
  814. };
  815. static struct mixer_operations ad1848_mixer_operations =
  816. {
  817. .owner = THIS_MODULE,
  818. .id = "SOUNDPORT",
  819. .name = "AD1848/CS4248/CS4231",
  820. .ioctl = ad1848_mixer_ioctl
  821. };
  822. static int ad1848_open(int dev, int mode)
  823. {
  824. ad1848_info *devc;
  825. ad1848_port_info *portc;
  826. unsigned long flags;
  827. if (dev < 0 || dev >= num_audiodevs)
  828. return -ENXIO;
  829. devc = (ad1848_info *) audio_devs[dev]->devc;
  830. portc = (ad1848_port_info *) audio_devs[dev]->portc;
  831. /* here we don't have to protect against intr */
  832. spin_lock(&devc->lock);
  833. if (portc->open_mode || (devc->open_mode & mode))
  834. {
  835. spin_unlock(&devc->lock);
  836. return -EBUSY;
  837. }
  838. devc->dual_dma = 0;
  839. if (audio_devs[dev]->flags & DMA_DUPLEX)
  840. {
  841. devc->dual_dma = 1;
  842. }
  843. devc->intr_active = 0;
  844. devc->audio_mode = 0;
  845. devc->open_mode |= mode;
  846. portc->open_mode = mode;
  847. spin_unlock(&devc->lock);
  848. ad1848_trigger(dev, 0);
  849. if (mode & OPEN_READ)
  850. devc->record_dev = dev;
  851. if (mode & OPEN_WRITE)
  852. devc->playback_dev = dev;
  853. /*
  854. * Mute output until the playback really starts. This decreases clicking (hope so).
  855. */
  856. spin_lock_irqsave(&devc->lock,flags);
  857. ad_mute(devc);
  858. spin_unlock_irqrestore(&devc->lock,flags);
  859. return 0;
  860. }
  861. static void ad1848_close(int dev)
  862. {
  863. unsigned long flags;
  864. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  865. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  866. DEB(printk("ad1848_close(void)\n"));
  867. devc->intr_active = 0;
  868. ad1848_halt(dev);
  869. spin_lock_irqsave(&devc->lock,flags);
  870. devc->audio_mode = 0;
  871. devc->open_mode &= ~portc->open_mode;
  872. portc->open_mode = 0;
  873. ad_unmute(devc);
  874. spin_unlock_irqrestore(&devc->lock,flags);
  875. }
  876. static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag)
  877. {
  878. unsigned long flags, cnt;
  879. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  880. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  881. cnt = count;
  882. if (portc->audio_format == AFMT_IMA_ADPCM)
  883. {
  884. cnt /= 4;
  885. }
  886. else
  887. {
  888. if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
  889. cnt >>= 1;
  890. }
  891. if (portc->channels > 1)
  892. cnt >>= 1;
  893. cnt--;
  894. if ((devc->audio_mode & PCM_ENABLE_OUTPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
  895. intrflag &&
  896. cnt == devc->xfer_count)
  897. {
  898. devc->audio_mode |= PCM_ENABLE_OUTPUT;
  899. devc->intr_active = 1;
  900. return; /*
  901. * Auto DMA mode on. No need to react
  902. */
  903. }
  904. spin_lock_irqsave(&devc->lock,flags);
  905. ad_write(devc, 15, (unsigned char) (cnt & 0xff));
  906. ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
  907. devc->xfer_count = cnt;
  908. devc->audio_mode |= PCM_ENABLE_OUTPUT;
  909. devc->intr_active = 1;
  910. spin_unlock_irqrestore(&devc->lock,flags);
  911. }
  912. static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag)
  913. {
  914. unsigned long flags, cnt;
  915. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  916. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  917. cnt = count;
  918. if (portc->audio_format == AFMT_IMA_ADPCM)
  919. {
  920. cnt /= 4;
  921. }
  922. else
  923. {
  924. if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
  925. cnt >>= 1;
  926. }
  927. if (portc->channels > 1)
  928. cnt >>= 1;
  929. cnt--;
  930. if ((devc->audio_mode & PCM_ENABLE_INPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
  931. intrflag &&
  932. cnt == devc->xfer_count)
  933. {
  934. devc->audio_mode |= PCM_ENABLE_INPUT;
  935. devc->intr_active = 1;
  936. return; /*
  937. * Auto DMA mode on. No need to react
  938. */
  939. }
  940. spin_lock_irqsave(&devc->lock,flags);
  941. if (devc->model == MD_1848)
  942. {
  943. ad_write(devc, 15, (unsigned char) (cnt & 0xff));
  944. ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
  945. }
  946. else
  947. {
  948. ad_write(devc, 31, (unsigned char) (cnt & 0xff));
  949. ad_write(devc, 30, (unsigned char) ((cnt >> 8) & 0xff));
  950. }
  951. ad_unmute(devc);
  952. devc->xfer_count = cnt;
  953. devc->audio_mode |= PCM_ENABLE_INPUT;
  954. devc->intr_active = 1;
  955. spin_unlock_irqrestore(&devc->lock,flags);
  956. }
  957. static int ad1848_prepare_for_output(int dev, int bsize, int bcount)
  958. {
  959. int timeout;
  960. unsigned char fs, old_fs, tmp = 0;
  961. unsigned long flags;
  962. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  963. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  964. ad_mute(devc);
  965. spin_lock_irqsave(&devc->lock,flags);
  966. fs = portc->speed_bits | (portc->format_bits << 5);
  967. if (portc->channels > 1)
  968. fs |= 0x10;
  969. ad_enter_MCE(devc); /* Enables changes to the format select reg */
  970. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE) /* Use alternate speed select registers */
  971. {
  972. fs &= 0xf0; /* Mask off the rate select bits */
  973. ad_write(devc, 22, (portc->speed >> 8) & 0xff); /* Speed MSB */
  974. ad_write(devc, 23, portc->speed & 0xff); /* Speed LSB */
  975. }
  976. old_fs = ad_read(devc, 8);
  977. if (devc->model == MD_4232 || devc->model >= MD_4236)
  978. {
  979. tmp = ad_read(devc, 16);
  980. ad_write(devc, 16, tmp | 0x30);
  981. }
  982. if (devc->model == MD_IWAVE)
  983. ad_write(devc, 17, 0xc2); /* Disable variable frequency select */
  984. ad_write(devc, 8, fs);
  985. /*
  986. * Write to I8 starts resynchronization. Wait until it completes.
  987. */
  988. timeout = 0;
  989. while (timeout < 100 && inb(devc->base) != 0x80)
  990. timeout++;
  991. timeout = 0;
  992. while (timeout < 10000 && inb(devc->base) == 0x80)
  993. timeout++;
  994. if (devc->model >= MD_4232)
  995. ad_write(devc, 16, tmp & ~0x30);
  996. ad_leave_MCE(devc); /*
  997. * Starts the calibration process.
  998. */
  999. spin_unlock_irqrestore(&devc->lock,flags);
  1000. devc->xfer_count = 0;
  1001. #ifndef EXCLUDE_TIMERS
  1002. if (dev == timer_installed && devc->timer_running)
  1003. if ((fs & 0x01) != (old_fs & 0x01))
  1004. {
  1005. ad1848_tmr_reprogram(dev);
  1006. }
  1007. #endif
  1008. ad1848_halt_output(dev);
  1009. return 0;
  1010. }
  1011. static int ad1848_prepare_for_input(int dev, int bsize, int bcount)
  1012. {
  1013. int timeout;
  1014. unsigned char fs, old_fs, tmp = 0;
  1015. unsigned long flags;
  1016. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1017. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  1018. if (devc->audio_mode)
  1019. return 0;
  1020. spin_lock_irqsave(&devc->lock,flags);
  1021. fs = portc->speed_bits | (portc->format_bits << 5);
  1022. if (portc->channels > 1)
  1023. fs |= 0x10;
  1024. ad_enter_MCE(devc); /* Enables changes to the format select reg */
  1025. if ((devc->model == MD_1845) || (devc->model == MD_1845_SSCAPE)) /* Use alternate speed select registers */
  1026. {
  1027. fs &= 0xf0; /* Mask off the rate select bits */
  1028. ad_write(devc, 22, (portc->speed >> 8) & 0xff); /* Speed MSB */
  1029. ad_write(devc, 23, portc->speed & 0xff); /* Speed LSB */
  1030. }
  1031. if (devc->model == MD_4232)
  1032. {
  1033. tmp = ad_read(devc, 16);
  1034. ad_write(devc, 16, tmp | 0x30);
  1035. }
  1036. if (devc->model == MD_IWAVE)
  1037. ad_write(devc, 17, 0xc2); /* Disable variable frequency select */
  1038. /*
  1039. * If mode >= 2 (CS4231), set I28. It's the capture format register.
  1040. */
  1041. if (devc->model != MD_1848)
  1042. {
  1043. old_fs = ad_read(devc, 28);
  1044. ad_write(devc, 28, fs);
  1045. /*
  1046. * Write to I28 starts resynchronization. Wait until it completes.
  1047. */
  1048. timeout = 0;
  1049. while (timeout < 100 && inb(devc->base) != 0x80)
  1050. timeout++;
  1051. timeout = 0;
  1052. while (timeout < 10000 && inb(devc->base) == 0x80)
  1053. timeout++;
  1054. if (devc->model != MD_1848 && devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
  1055. {
  1056. /*
  1057. * CS4231 compatible devices don't have separate sampling rate selection
  1058. * register for recording an playback. The I8 register is shared so we have to
  1059. * set the speed encoding bits of it too.
  1060. */
  1061. unsigned char tmp = portc->speed_bits | (ad_read(devc, 8) & 0xf0);
  1062. ad_write(devc, 8, tmp);
  1063. /*
  1064. * Write to I8 starts resynchronization. Wait until it completes.
  1065. */
  1066. timeout = 0;
  1067. while (timeout < 100 && inb(devc->base) != 0x80)
  1068. timeout++;
  1069. timeout = 0;
  1070. while (timeout < 10000 && inb(devc->base) == 0x80)
  1071. timeout++;
  1072. }
  1073. }
  1074. else
  1075. { /* For AD1848 set I8. */
  1076. old_fs = ad_read(devc, 8);
  1077. ad_write(devc, 8, fs);
  1078. /*
  1079. * Write to I8 starts resynchronization. Wait until it completes.
  1080. */
  1081. timeout = 0;
  1082. while (timeout < 100 && inb(devc->base) != 0x80)
  1083. timeout++;
  1084. timeout = 0;
  1085. while (timeout < 10000 && inb(devc->base) == 0x80)
  1086. timeout++;
  1087. }
  1088. if (devc->model == MD_4232)
  1089. ad_write(devc, 16, tmp & ~0x30);
  1090. ad_leave_MCE(devc); /*
  1091. * Starts the calibration process.
  1092. */
  1093. spin_unlock_irqrestore(&devc->lock,flags);
  1094. devc->xfer_count = 0;
  1095. #ifndef EXCLUDE_TIMERS
  1096. if (dev == timer_installed && devc->timer_running)
  1097. {
  1098. if ((fs & 0x01) != (old_fs & 0x01))
  1099. {
  1100. ad1848_tmr_reprogram(dev);
  1101. }
  1102. }
  1103. #endif
  1104. ad1848_halt_input(dev);
  1105. return 0;
  1106. }
  1107. static void ad1848_halt(int dev)
  1108. {
  1109. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1110. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  1111. unsigned char bits = ad_read(devc, 9);
  1112. if (bits & 0x01 && (portc->open_mode & OPEN_WRITE))
  1113. ad1848_halt_output(dev);
  1114. if (bits & 0x02 && (portc->open_mode & OPEN_READ))
  1115. ad1848_halt_input(dev);
  1116. devc->audio_mode = 0;
  1117. }
  1118. static void ad1848_halt_input(int dev)
  1119. {
  1120. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1121. unsigned long flags;
  1122. if (!(ad_read(devc, 9) & 0x02))
  1123. return; /* Capture not enabled */
  1124. spin_lock_irqsave(&devc->lock,flags);
  1125. ad_mute(devc);
  1126. {
  1127. int tmout;
  1128. if(!isa_dma_bridge_buggy)
  1129. disable_dma(audio_devs[dev]->dmap_in->dma);
  1130. for (tmout = 0; tmout < 100000; tmout++)
  1131. if (ad_read(devc, 11) & 0x10)
  1132. break;
  1133. ad_write(devc, 9, ad_read(devc, 9) & ~0x02); /* Stop capture */
  1134. if(!isa_dma_bridge_buggy)
  1135. enable_dma(audio_devs[dev]->dmap_in->dma);
  1136. devc->audio_mode &= ~PCM_ENABLE_INPUT;
  1137. }
  1138. outb(0, io_Status(devc)); /* Clear interrupt status */
  1139. outb(0, io_Status(devc)); /* Clear interrupt status */
  1140. devc->audio_mode &= ~PCM_ENABLE_INPUT;
  1141. spin_unlock_irqrestore(&devc->lock,flags);
  1142. }
  1143. static void ad1848_halt_output(int dev)
  1144. {
  1145. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1146. unsigned long flags;
  1147. if (!(ad_read(devc, 9) & 0x01))
  1148. return; /* Playback not enabled */
  1149. spin_lock_irqsave(&devc->lock,flags);
  1150. ad_mute(devc);
  1151. {
  1152. int tmout;
  1153. if(!isa_dma_bridge_buggy)
  1154. disable_dma(audio_devs[dev]->dmap_out->dma);
  1155. for (tmout = 0; tmout < 100000; tmout++)
  1156. if (ad_read(devc, 11) & 0x10)
  1157. break;
  1158. ad_write(devc, 9, ad_read(devc, 9) & ~0x01); /* Stop playback */
  1159. if(!isa_dma_bridge_buggy)
  1160. enable_dma(audio_devs[dev]->dmap_out->dma);
  1161. devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
  1162. }
  1163. outb((0), io_Status(devc)); /* Clear interrupt status */
  1164. outb((0), io_Status(devc)); /* Clear interrupt status */
  1165. devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
  1166. spin_unlock_irqrestore(&devc->lock,flags);
  1167. }
  1168. static void ad1848_trigger(int dev, int state)
  1169. {
  1170. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1171. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  1172. unsigned long flags;
  1173. unsigned char tmp, old;
  1174. spin_lock_irqsave(&devc->lock,flags);
  1175. state &= devc->audio_mode;
  1176. tmp = old = ad_read(devc, 9);
  1177. if (portc->open_mode & OPEN_READ)
  1178. {
  1179. if (state & PCM_ENABLE_INPUT)
  1180. tmp |= 0x02;
  1181. else
  1182. tmp &= ~0x02;
  1183. }
  1184. if (portc->open_mode & OPEN_WRITE)
  1185. {
  1186. if (state & PCM_ENABLE_OUTPUT)
  1187. tmp |= 0x01;
  1188. else
  1189. tmp &= ~0x01;
  1190. }
  1191. /* ad_mute(devc); */
  1192. if (tmp != old)
  1193. {
  1194. ad_write(devc, 9, tmp);
  1195. ad_unmute(devc);
  1196. }
  1197. spin_unlock_irqrestore(&devc->lock,flags);
  1198. }
  1199. static void ad1848_init_hw(ad1848_info * devc)
  1200. {
  1201. int i;
  1202. int *init_values;
  1203. /*
  1204. * Initial values for the indirect registers of CS4248/AD1848.
  1205. */
  1206. static int init_values_a[] =
  1207. {
  1208. 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
  1209. 0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
  1210. /* Positions 16 to 31 just for CS4231/2 and ad1845 */
  1211. 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
  1212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1213. };
  1214. static int init_values_b[] =
  1215. {
  1216. /*
  1217. Values for the newer chips
  1218. Some of the register initialization values were changed. In
  1219. order to get rid of the click that preceded PCM playback,
  1220. calibration was disabled on the 10th byte. On that same byte,
  1221. dual DMA was enabled; on the 11th byte, ADC dithering was
  1222. enabled, since that is theoretically desirable; on the 13th
  1223. byte, Mode 3 was selected, to enable access to extended
  1224. registers.
  1225. */
  1226. 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
  1227. 0x00, 0x00, 0x06, 0x00, 0xe0, 0x01, 0x00, 0x00,
  1228. 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
  1229. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1230. };
  1231. /*
  1232. * Select initialisation data
  1233. */
  1234. init_values = init_values_a;
  1235. if(devc->model >= MD_4236)
  1236. init_values = init_values_b;
  1237. for (i = 0; i < 16; i++)
  1238. ad_write(devc, i, init_values[i]);
  1239. ad_mute(devc); /* Initialize some variables */
  1240. ad_unmute(devc); /* Leave it unmuted now */
  1241. if (devc->model > MD_1848)
  1242. {
  1243. if (devc->model == MD_1845_SSCAPE)
  1244. ad_write(devc, 12, ad_read(devc, 12) | 0x50);
  1245. else
  1246. ad_write(devc, 12, ad_read(devc, 12) | 0x40); /* Mode2 = enabled */
  1247. if (devc->model == MD_IWAVE)
  1248. ad_write(devc, 12, 0x6c); /* Select codec mode 3 */
  1249. if (devc->model != MD_1845_SSCAPE)
  1250. for (i = 16; i < 32; i++)
  1251. ad_write(devc, i, init_values[i]);
  1252. if (devc->model == MD_IWAVE)
  1253. ad_write(devc, 16, 0x30); /* Playback and capture counters enabled */
  1254. }
  1255. if (devc->model > MD_1848)
  1256. {
  1257. if (devc->audio_flags & DMA_DUPLEX)
  1258. ad_write(devc, 9, ad_read(devc, 9) & ~0x04); /* Dual DMA mode */
  1259. else
  1260. ad_write(devc, 9, ad_read(devc, 9) | 0x04); /* Single DMA mode */
  1261. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
  1262. ad_write(devc, 27, ad_read(devc, 27) | 0x08); /* Alternate freq select enabled */
  1263. if (devc->model == MD_IWAVE)
  1264. { /* Some magic Interwave specific initialization */
  1265. ad_write(devc, 12, 0x6c); /* Select codec mode 3 */
  1266. ad_write(devc, 16, 0x30); /* Playback and capture counters enabled */
  1267. ad_write(devc, 17, 0xc2); /* Alternate feature enable */
  1268. }
  1269. }
  1270. else
  1271. {
  1272. devc->audio_flags &= ~DMA_DUPLEX;
  1273. ad_write(devc, 9, ad_read(devc, 9) | 0x04); /* Single DMA mode */
  1274. if (soundpro)
  1275. ad_write(devc, 12, ad_read(devc, 12) | 0x40); /* Mode2 = enabled */
  1276. }
  1277. outb((0), io_Status(devc)); /* Clear pending interrupts */
  1278. /*
  1279. * Toggle the MCE bit. It completes the initialization phase.
  1280. */
  1281. ad_enter_MCE(devc); /* In case the bit was off */
  1282. ad_leave_MCE(devc);
  1283. ad1848_mixer_reset(devc);
  1284. }
  1285. int ad1848_detect(struct resource *ports, int *ad_flags, int *osp)
  1286. {
  1287. unsigned char tmp;
  1288. ad1848_info *devc = &adev_info[nr_ad1848_devs];
  1289. unsigned char tmp1 = 0xff, tmp2 = 0xff;
  1290. int optiC930 = 0; /* OPTi 82C930 flag */
  1291. int interwave = 0;
  1292. int ad1847_flag = 0;
  1293. int cs4248_flag = 0;
  1294. int sscape_flag = 0;
  1295. int io_base = ports->start;
  1296. int i;
  1297. DDB(printk("ad1848_detect(%x)\n", io_base));
  1298. if (ad_flags)
  1299. {
  1300. if (*ad_flags == 0x12345678)
  1301. {
  1302. interwave = 1;
  1303. *ad_flags = 0;
  1304. }
  1305. if (*ad_flags == 0x87654321)
  1306. {
  1307. sscape_flag = 1;
  1308. *ad_flags = 0;
  1309. }
  1310. if (*ad_flags == 0x12345677)
  1311. {
  1312. cs4248_flag = 1;
  1313. *ad_flags = 0;
  1314. }
  1315. }
  1316. if (nr_ad1848_devs >= MAX_AUDIO_DEV)
  1317. {
  1318. printk(KERN_ERR "ad1848 - Too many audio devices\n");
  1319. return 0;
  1320. }
  1321. spin_lock_init(&devc->lock);
  1322. devc->base = io_base;
  1323. devc->irq_ok = 0;
  1324. devc->timer_running = 0;
  1325. devc->MCE_bit = 0x40;
  1326. devc->irq = 0;
  1327. devc->open_mode = 0;
  1328. devc->chip_name = devc->name = "AD1848";
  1329. devc->model = MD_1848; /* AD1848 or CS4248 */
  1330. devc->levels = NULL;
  1331. devc->debug_flag = 0;
  1332. /*
  1333. * Check that the I/O address is in use.
  1334. *
  1335. * The bit 0x80 of the base I/O port is known to be 0 after the
  1336. * chip has performed its power on initialization. Just assume
  1337. * this has happened before the OS is starting.
  1338. *
  1339. * If the I/O address is unused, it typically returns 0xff.
  1340. */
  1341. if (inb(devc->base) == 0xff)
  1342. {
  1343. DDB(printk("ad1848_detect: The base I/O address appears to be dead\n"));
  1344. }
  1345. /*
  1346. * Wait for the device to stop initialization
  1347. */
  1348. DDB(printk("ad1848_detect() - step 0\n"));
  1349. for (i = 0; i < 10000000; i++)
  1350. {
  1351. unsigned char x = inb(devc->base);
  1352. if (x == 0xff || !(x & 0x80))
  1353. break;
  1354. }
  1355. DDB(printk("ad1848_detect() - step A\n"));
  1356. if (inb(devc->base) == 0x80) /* Not ready. Let's wait */
  1357. ad_leave_MCE(devc);
  1358. if ((inb(devc->base) & 0x80) != 0x00) /* Not a AD1848 */
  1359. {
  1360. DDB(printk("ad1848 detect error - step A (%02x)\n", (int) inb(devc->base)));
  1361. return 0;
  1362. }
  1363. /*
  1364. * Test if it's possible to change contents of the indirect registers.
  1365. * Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
  1366. * so try to avoid using it.
  1367. */
  1368. DDB(printk("ad1848_detect() - step B\n"));
  1369. ad_write(devc, 0, 0xaa);
  1370. ad_write(devc, 1, 0x45); /* 0x55 with bit 0x10 clear */
  1371. if ((tmp1 = ad_read(devc, 0)) != 0xaa || (tmp2 = ad_read(devc, 1)) != 0x45)
  1372. {
  1373. if (tmp2 == 0x65) /* AD1847 has couple of bits hardcoded to 1 */
  1374. ad1847_flag = 1;
  1375. else
  1376. {
  1377. DDB(printk("ad1848 detect error - step B (%x/%x)\n", tmp1, tmp2));
  1378. return 0;
  1379. }
  1380. }
  1381. DDB(printk("ad1848_detect() - step C\n"));
  1382. ad_write(devc, 0, 0x45);
  1383. ad_write(devc, 1, 0xaa);
  1384. if ((tmp1 = ad_read(devc, 0)) != 0x45 || (tmp2 = ad_read(devc, 1)) != 0xaa)
  1385. {
  1386. if (tmp2 == 0x8a) /* AD1847 has few bits hardcoded to 1 */
  1387. ad1847_flag = 1;
  1388. else
  1389. {
  1390. DDB(printk("ad1848 detect error - step C (%x/%x)\n", tmp1, tmp2));
  1391. return 0;
  1392. }
  1393. }
  1394. /*
  1395. * The indirect register I12 has some read only bits. Let's
  1396. * try to change them.
  1397. */
  1398. DDB(printk("ad1848_detect() - step D\n"));
  1399. tmp = ad_read(devc, 12);
  1400. ad_write(devc, 12, (~tmp) & 0x0f);
  1401. if ((tmp & 0x0f) != ((tmp1 = ad_read(devc, 12)) & 0x0f))
  1402. {
  1403. DDB(printk("ad1848 detect error - step D (%x)\n", tmp1));
  1404. return 0;
  1405. }
  1406. /*
  1407. * NOTE! Last 4 bits of the reg I12 tell the chip revision.
  1408. * 0x01=RevB and 0x0A=RevC.
  1409. */
  1410. /*
  1411. * The original AD1848/CS4248 has just 15 indirect registers. This means
  1412. * that I0 and I16 should return the same value (etc.).
  1413. * However this doesn't work with CS4248. Actually it seems to be impossible
  1414. * to detect if the chip is a CS4231 or CS4248.
  1415. * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
  1416. * with CS4231.
  1417. */
  1418. /*
  1419. * OPTi 82C930 has mode2 control bit in another place. This test will fail
  1420. * with it. Accept this situation as a possible indication of this chip.
  1421. */
  1422. DDB(printk("ad1848_detect() - step F\n"));
  1423. ad_write(devc, 12, 0); /* Mode2=disabled */
  1424. for (i = 0; i < 16; i++)
  1425. {
  1426. if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16)))
  1427. {
  1428. DDB(printk("ad1848 detect step F(%d/%x/%x) - OPTi chip???\n", i, tmp1, tmp2));
  1429. if (!ad1847_flag)
  1430. optiC930 = 1;
  1431. break;
  1432. }
  1433. }
  1434. /*
  1435. * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit (0x40).
  1436. * The bit 0x80 is always 1 in CS4248 and CS4231.
  1437. */
  1438. DDB(printk("ad1848_detect() - step G\n"));
  1439. if (ad_flags && *ad_flags == 400)
  1440. *ad_flags = 0;
  1441. else
  1442. ad_write(devc, 12, 0x40); /* Set mode2, clear 0x80 */
  1443. if (ad_flags)
  1444. *ad_flags = 0;
  1445. tmp1 = ad_read(devc, 12);
  1446. if (tmp1 & 0x80)
  1447. {
  1448. if (ad_flags)
  1449. *ad_flags |= AD_F_CS4248;
  1450. devc->chip_name = "CS4248"; /* Our best knowledge just now */
  1451. }
  1452. if (optiC930 || (tmp1 & 0xc0) == (0x80 | 0x40))
  1453. {
  1454. /*
  1455. * CS4231 detected - is it?
  1456. *
  1457. * Verify that setting I0 doesn't change I16.
  1458. */
  1459. DDB(printk("ad1848_detect() - step H\n"));
  1460. ad_write(devc, 16, 0); /* Set I16 to known value */
  1461. ad_write(devc, 0, 0x45);
  1462. if ((tmp1 = ad_read(devc, 16)) != 0x45) /* No change -> CS4231? */
  1463. {
  1464. ad_write(devc, 0, 0xaa);
  1465. if ((tmp1 = ad_read(devc, 16)) == 0xaa) /* Rotten bits? */
  1466. {
  1467. DDB(printk("ad1848 detect error - step H(%x)\n", tmp1));
  1468. return 0;
  1469. }
  1470. /*
  1471. * Verify that some bits of I25 are read only.
  1472. */
  1473. DDB(printk("ad1848_detect() - step I\n"));
  1474. tmp1 = ad_read(devc, 25); /* Original bits */
  1475. ad_write(devc, 25, ~tmp1); /* Invert all bits */
  1476. if ((ad_read(devc, 25) & 0xe7) == (tmp1 & 0xe7))
  1477. {
  1478. int id;
  1479. /*
  1480. * It's at least CS4231
  1481. */
  1482. devc->chip_name = "CS4231";
  1483. devc->model = MD_4231;
  1484. /*
  1485. * It could be an AD1845 or CS4231A as well.
  1486. * CS4231 and AD1845 report the same revision info in I25
  1487. * while the CS4231A reports different.
  1488. */
  1489. id = ad_read(devc, 25);
  1490. if ((id & 0xe7) == 0x80) /* Device busy??? */
  1491. id = ad_read(devc, 25);
  1492. if ((id & 0xe7) == 0x80) /* Device still busy??? */
  1493. id = ad_read(devc, 25);
  1494. DDB(printk("ad1848_detect() - step J (%02x/%02x)\n", id, ad_read(devc, 25)));
  1495. if ((id & 0xe7) == 0x80) {
  1496. /*
  1497. * It must be a CS4231 or AD1845. The register I23 of
  1498. * CS4231 is undefined and it appears to be read only.
  1499. * AD1845 uses I23 for setting sample rate. Assume
  1500. * the chip is AD1845 if I23 is changeable.
  1501. */
  1502. unsigned char tmp = ad_read(devc, 23);
  1503. ad_write(devc, 23, ~tmp);
  1504. if (interwave)
  1505. {
  1506. devc->model = MD_IWAVE;
  1507. devc->chip_name = "IWave";
  1508. }
  1509. else if (ad_read(devc, 23) != tmp) /* AD1845 ? */
  1510. {
  1511. devc->chip_name = "AD1845";
  1512. devc->model = MD_1845;
  1513. }
  1514. else if (cs4248_flag)
  1515. {
  1516. if (ad_flags)
  1517. *ad_flags |= AD_F_CS4248;
  1518. devc->chip_name = "CS4248";
  1519. devc->model = MD_1848;
  1520. ad_write(devc, 12, ad_read(devc, 12) & ~0x40); /* Mode2 off */
  1521. }
  1522. ad_write(devc, 23, tmp); /* Restore */
  1523. }
  1524. else
  1525. {
  1526. switch (id & 0x1f) {
  1527. case 3: /* CS4236/CS4235/CS42xB/CS4239 */
  1528. {
  1529. int xid;
  1530. ad_write(devc, 12, ad_read(devc, 12) | 0x60); /* switch to mode 3 */
  1531. ad_write(devc, 23, 0x9c); /* select extended register 25 */
  1532. xid = inb(io_Indexed_Data(devc));
  1533. ad_write(devc, 12, ad_read(devc, 12) & ~0x60); /* back to mode 0 */
  1534. switch (xid & 0x1f)
  1535. {
  1536. case 0x00:
  1537. devc->chip_name = "CS4237B(B)";
  1538. devc->model = MD_42xB;
  1539. break;
  1540. case 0x08:
  1541. /* Seems to be a 4238 ?? */
  1542. devc->chip_name = "CS4238";
  1543. devc->model = MD_42xB;
  1544. break;
  1545. case 0x09:
  1546. devc->chip_name = "CS4238B";
  1547. devc->model = MD_42xB;
  1548. break;
  1549. case 0x0b:
  1550. devc->chip_name = "CS4236B";
  1551. devc->model = MD_4236;
  1552. break;
  1553. case 0x10:
  1554. devc->chip_name = "CS4237B";
  1555. devc->model = MD_42xB;
  1556. break;
  1557. case 0x1d:
  1558. devc->chip_name = "CS4235";
  1559. devc->model = MD_4235;
  1560. break;
  1561. case 0x1e:
  1562. devc->chip_name = "CS4239";
  1563. devc->model = MD_4239;
  1564. break;
  1565. default:
  1566. printk("Chip ident is %X.\n", xid&0x1F);
  1567. devc->chip_name = "CS42xx";
  1568. devc->model = MD_4232;
  1569. break;
  1570. }
  1571. }
  1572. break;
  1573. case 2: /* CS4232/CS4232A */
  1574. devc->chip_name = "CS4232";
  1575. devc->model = MD_4232;
  1576. break;
  1577. case 0:
  1578. if ((id & 0xe0) == 0xa0)
  1579. {
  1580. devc->chip_name = "CS4231A";
  1581. devc->model = MD_4231A;
  1582. }
  1583. else
  1584. {
  1585. devc->chip_name = "CS4321";
  1586. devc->model = MD_4231;
  1587. }
  1588. break;
  1589. default: /* maybe */
  1590. DDB(printk("ad1848: I25 = %02x/%02x\n", ad_read(devc, 25), ad_read(devc, 25) & 0xe7));
  1591. if (optiC930)
  1592. {
  1593. devc->chip_name = "82C930";
  1594. devc->model = MD_C930;
  1595. }
  1596. else
  1597. {
  1598. devc->chip_name = "CS4231";
  1599. devc->model = MD_4231;
  1600. }
  1601. }
  1602. }
  1603. }
  1604. ad_write(devc, 25, tmp1); /* Restore bits */
  1605. DDB(printk("ad1848_detect() - step K\n"));
  1606. }
  1607. } else if (tmp1 == 0x0a) {
  1608. /*
  1609. * Is it perhaps a SoundPro CMI8330?
  1610. * If so, then we should be able to change indirect registers
  1611. * greater than I15 after activating MODE2, even though reading
  1612. * back I12 does not show it.
  1613. */
  1614. /*
  1615. * Let's try comparing register values
  1616. */
  1617. for (i = 0; i < 16; i++) {
  1618. if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16))) {
  1619. DDB(printk("ad1848 detect step H(%d/%x/%x) - SoundPro chip?\n", i, tmp1, tmp2));
  1620. soundpro = 1;
  1621. devc->chip_name = "SoundPro CMI 8330";
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. DDB(printk("ad1848_detect() - step L\n"));
  1627. if (ad_flags)
  1628. {
  1629. if (devc->model != MD_1848)
  1630. *ad_flags |= AD_F_CS4231;
  1631. }
  1632. DDB(printk("ad1848_detect() - Detected OK\n"));
  1633. if (devc->model == MD_1848 && ad1847_flag)
  1634. devc->chip_name = "AD1847";
  1635. if (sscape_flag == 1)
  1636. devc->model = MD_1845_SSCAPE;
  1637. return 1;
  1638. }
  1639. int ad1848_init (char *name, struct resource *ports, int irq, int dma_playback,
  1640. int dma_capture, int share_dma, int *osp, struct module *owner)
  1641. {
  1642. /*
  1643. * NOTE! If irq < 0, there is another driver which has allocated the IRQ
  1644. * so that this driver doesn't need to allocate/deallocate it.
  1645. * The actually used IRQ is ABS(irq).
  1646. */
  1647. int my_dev;
  1648. char dev_name[100];
  1649. int e;
  1650. ad1848_info *devc = &adev_info[nr_ad1848_devs];
  1651. ad1848_port_info *portc = NULL;
  1652. devc->irq = (irq > 0) ? irq : 0;
  1653. devc->open_mode = 0;
  1654. devc->timer_ticks = 0;
  1655. devc->dma1 = dma_playback;
  1656. devc->dma2 = dma_capture;
  1657. devc->subtype = cfg.card_subtype;
  1658. devc->audio_flags = DMA_AUTOMODE;
  1659. devc->playback_dev = devc->record_dev = 0;
  1660. if (name != NULL)
  1661. devc->name = name;
  1662. if (name != NULL && name[0] != 0)
  1663. sprintf(dev_name,
  1664. "%s (%s)", name, devc->chip_name);
  1665. else
  1666. sprintf(dev_name,
  1667. "Generic audio codec (%s)", devc->chip_name);
  1668. rename_region(ports, devc->name);
  1669. conf_printf2(dev_name, devc->base, devc->irq, dma_playback, dma_capture);
  1670. if (devc->model == MD_1848 || devc->model == MD_C930)
  1671. devc->audio_flags |= DMA_HARDSTOP;
  1672. if (devc->model > MD_1848)
  1673. {
  1674. if (devc->dma1 == devc->dma2 || devc->dma2 == -1 || devc->dma1 == -1)
  1675. devc->audio_flags &= ~DMA_DUPLEX;
  1676. else
  1677. devc->audio_flags |= DMA_DUPLEX;
  1678. }
  1679. portc = kmalloc(sizeof(ad1848_port_info), GFP_KERNEL);
  1680. if(portc==NULL) {
  1681. release_region(devc->base, 4);
  1682. return -1;
  1683. }
  1684. if ((my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
  1685. dev_name,
  1686. &ad1848_audio_driver,
  1687. sizeof(struct audio_driver),
  1688. devc->audio_flags,
  1689. ad_format_mask[devc->model],
  1690. devc,
  1691. dma_playback,
  1692. dma_capture)) < 0)
  1693. {
  1694. release_region(devc->base, 4);
  1695. kfree(portc);
  1696. return -1;
  1697. }
  1698. audio_devs[my_dev]->portc = portc;
  1699. audio_devs[my_dev]->mixer_dev = -1;
  1700. if (owner)
  1701. audio_devs[my_dev]->d->owner = owner;
  1702. memset((char *) portc, 0, sizeof(*portc));
  1703. nr_ad1848_devs++;
  1704. ad1848_init_hw(devc);
  1705. if (irq > 0)
  1706. {
  1707. devc->dev_no = my_dev;
  1708. if (request_irq(devc->irq, adintr, 0, devc->name,
  1709. (void *)(long)my_dev) < 0)
  1710. {
  1711. printk(KERN_WARNING "ad1848: Unable to allocate IRQ\n");
  1712. /* Don't free it either then.. */
  1713. devc->irq = 0;
  1714. }
  1715. if (capabilities[devc->model].flags & CAP_F_TIMER)
  1716. {
  1717. #ifndef CONFIG_SMP
  1718. int x;
  1719. unsigned char tmp = ad_read(devc, 16);
  1720. #endif
  1721. devc->timer_ticks = 0;
  1722. ad_write(devc, 21, 0x00); /* Timer MSB */
  1723. ad_write(devc, 20, 0x10); /* Timer LSB */
  1724. #ifndef CONFIG_SMP
  1725. ad_write(devc, 16, tmp | 0x40); /* Enable timer */
  1726. for (x = 0; x < 100000 && devc->timer_ticks == 0; x++);
  1727. ad_write(devc, 16, tmp & ~0x40); /* Disable timer */
  1728. if (devc->timer_ticks == 0)
  1729. printk(KERN_WARNING "ad1848: Interrupt test failed (IRQ%d)\n", irq);
  1730. else
  1731. {
  1732. DDB(printk("Interrupt test OK\n"));
  1733. devc->irq_ok = 1;
  1734. }
  1735. #else
  1736. devc->irq_ok = 1;
  1737. #endif
  1738. }
  1739. else
  1740. devc->irq_ok = 1; /* Couldn't test. assume it's OK */
  1741. } else if (irq < 0)
  1742. irq2dev[-irq] = devc->dev_no = my_dev;
  1743. #ifndef EXCLUDE_TIMERS
  1744. if ((capabilities[devc->model].flags & CAP_F_TIMER) &&
  1745. devc->irq_ok)
  1746. ad1848_tmr_install(my_dev);
  1747. #endif
  1748. if (!share_dma)
  1749. {
  1750. if (sound_alloc_dma(dma_playback, devc->name))
  1751. printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_playback);
  1752. if (dma_capture != dma_playback)
  1753. if (sound_alloc_dma(dma_capture, devc->name))
  1754. printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_capture);
  1755. }
  1756. if ((e = sound_install_mixer(MIXER_DRIVER_VERSION,
  1757. dev_name,
  1758. &ad1848_mixer_operations,
  1759. sizeof(struct mixer_operations),
  1760. devc)) >= 0)
  1761. {
  1762. audio_devs[my_dev]->mixer_dev = e;
  1763. if (owner)
  1764. mixer_devs[e]->owner = owner;
  1765. }
  1766. return my_dev;
  1767. }
  1768. int ad1848_control(int cmd, int arg)
  1769. {
  1770. ad1848_info *devc;
  1771. unsigned long flags;
  1772. if (nr_ad1848_devs < 1)
  1773. return -ENODEV;
  1774. devc = &adev_info[nr_ad1848_devs - 1];
  1775. switch (cmd)
  1776. {
  1777. case AD1848_SET_XTAL: /* Change clock frequency of AD1845 (only ) */
  1778. if (devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
  1779. return -EINVAL;
  1780. spin_lock_irqsave(&devc->lock,flags);
  1781. ad_enter_MCE(devc);
  1782. ad_write(devc, 29, (ad_read(devc, 29) & 0x1f) | (arg << 5));
  1783. ad_leave_MCE(devc);
  1784. spin_unlock_irqrestore(&devc->lock,flags);
  1785. break;
  1786. case AD1848_MIXER_REROUTE:
  1787. {
  1788. int o = (arg >> 8) & 0xff;
  1789. int n = arg & 0xff;
  1790. if (o < 0 || o >= SOUND_MIXER_NRDEVICES)
  1791. return -EINVAL;
  1792. if (!(devc->supported_devices & (1 << o)) &&
  1793. !(devc->supported_rec_devices & (1 << o)))
  1794. return -EINVAL;
  1795. if (n == SOUND_MIXER_NONE)
  1796. { /* Just hide this control */
  1797. ad1848_mixer_set(devc, o, 0); /* Shut up it */
  1798. devc->supported_devices &= ~(1 << o);
  1799. devc->supported_rec_devices &= ~(1 << o);
  1800. break;
  1801. }
  1802. /* Make the mixer control identified by o to appear as n */
  1803. if (n < 0 || n >= SOUND_MIXER_NRDEVICES)
  1804. return -EINVAL;
  1805. devc->mixer_reroute[n] = o; /* Rename the control */
  1806. if (devc->supported_devices & (1 << o))
  1807. devc->supported_devices |= (1 << n);
  1808. if (devc->supported_rec_devices & (1 << o))
  1809. devc->supported_rec_devices |= (1 << n);
  1810. devc->supported_devices &= ~(1 << o);
  1811. devc->supported_rec_devices &= ~(1 << o);
  1812. }
  1813. break;
  1814. }
  1815. return 0;
  1816. }
  1817. void ad1848_unload(int io_base, int irq, int dma_playback, int dma_capture, int share_dma)
  1818. {
  1819. int i, mixer, dev = 0;
  1820. ad1848_info *devc = NULL;
  1821. for (i = 0; devc == NULL && i < nr_ad1848_devs; i++)
  1822. {
  1823. if (adev_info[i].base == io_base)
  1824. {
  1825. devc = &adev_info[i];
  1826. dev = devc->dev_no;
  1827. }
  1828. }
  1829. if (devc != NULL)
  1830. {
  1831. kfree(audio_devs[dev]->portc);
  1832. release_region(devc->base, 4);
  1833. if (!share_dma)
  1834. {
  1835. if (devc->irq > 0) /* There is no point in freeing irq, if it wasn't allocated */
  1836. free_irq(devc->irq, (void *)(long)devc->dev_no);
  1837. sound_free_dma(dma_playback);
  1838. if (dma_playback != dma_capture)
  1839. sound_free_dma(dma_capture);
  1840. }
  1841. mixer = audio_devs[devc->dev_no]->mixer_dev;
  1842. if(mixer>=0)
  1843. sound_unload_mixerdev(mixer);
  1844. nr_ad1848_devs--;
  1845. for ( ; i < nr_ad1848_devs ; i++)
  1846. adev_info[i] = adev_info[i+1];
  1847. }
  1848. else
  1849. printk(KERN_ERR "ad1848: Can't find device to be unloaded. Base=%x\n", io_base);
  1850. }
  1851. static irqreturn_t adintr(int irq, void *dev_id)
  1852. {
  1853. unsigned char status;
  1854. ad1848_info *devc;
  1855. int dev;
  1856. int alt_stat = 0xff;
  1857. unsigned char c930_stat = 0;
  1858. int cnt = 0;
  1859. dev = (long)dev_id;
  1860. devc = (ad1848_info *) audio_devs[dev]->devc;
  1861. interrupt_again: /* Jump back here if int status doesn't reset */
  1862. status = inb(io_Status(devc));
  1863. if (status == 0x80)
  1864. printk(KERN_DEBUG "adintr: Why?\n");
  1865. if (devc->model == MD_1848)
  1866. outb((0), io_Status(devc)); /* Clear interrupt status */
  1867. if (status & 0x01)
  1868. {
  1869. if (devc->model == MD_C930)
  1870. { /* 82C930 has interrupt status register in MAD16 register MC11 */
  1871. spin_lock(&devc->lock);
  1872. /* 0xe0e is C930 address port
  1873. * 0xe0f is C930 data port
  1874. */
  1875. outb(11, 0xe0e);
  1876. c930_stat = inb(0xe0f);
  1877. outb((~c930_stat), 0xe0f);
  1878. spin_unlock(&devc->lock);
  1879. alt_stat = (c930_stat << 2) & 0x30;
  1880. }
  1881. else if (devc->model != MD_1848)
  1882. {
  1883. spin_lock(&devc->lock);
  1884. alt_stat = ad_read(devc, 24);
  1885. ad_write(devc, 24, ad_read(devc, 24) & ~alt_stat); /* Selective ack */
  1886. spin_unlock(&devc->lock);
  1887. }
  1888. if ((devc->open_mode & OPEN_READ) && (devc->audio_mode & PCM_ENABLE_INPUT) && (alt_stat & 0x20))
  1889. {
  1890. DMAbuf_inputintr(devc->record_dev);
  1891. }
  1892. if ((devc->open_mode & OPEN_WRITE) && (devc->audio_mode & PCM_ENABLE_OUTPUT) &&
  1893. (alt_stat & 0x10))
  1894. {
  1895. DMAbuf_outputintr(devc->playback_dev, 1);
  1896. }
  1897. if (devc->model != MD_1848 && (alt_stat & 0x40)) /* Timer interrupt */
  1898. {
  1899. devc->timer_ticks++;
  1900. #ifndef EXCLUDE_TIMERS
  1901. if (timer_installed == dev && devc->timer_running)
  1902. sound_timer_interrupt();
  1903. #endif
  1904. }
  1905. }
  1906. /*
  1907. * Sometimes playback or capture interrupts occur while a timer interrupt
  1908. * is being handled. The interrupt will not be retriggered if we don't
  1909. * handle it now. Check if an interrupt is still pending and restart
  1910. * the handler in this case.
  1911. */
  1912. if (inb(io_Status(devc)) & 0x01 && cnt++ < 4)
  1913. {
  1914. goto interrupt_again;
  1915. }
  1916. return IRQ_HANDLED;
  1917. }
  1918. /*
  1919. * Experimental initialization sequence for the integrated sound system
  1920. * of the Compaq Deskpro M.
  1921. */
  1922. static int init_deskpro_m(struct address_info *hw_config)
  1923. {
  1924. unsigned char tmp;
  1925. if ((tmp = inb(0xc44)) == 0xff)
  1926. {
  1927. DDB(printk("init_deskpro_m: Dead port 0xc44\n"));
  1928. return 0;
  1929. }
  1930. outb(0x10, 0xc44);
  1931. outb(0x40, 0xc45);
  1932. outb(0x00, 0xc46);
  1933. outb(0xe8, 0xc47);
  1934. outb(0x14, 0xc44);
  1935. outb(0x40, 0xc45);
  1936. outb(0x00, 0xc46);
  1937. outb(0xe8, 0xc47);
  1938. outb(0x10, 0xc44);
  1939. return 1;
  1940. }
  1941. /*
  1942. * Experimental initialization sequence for the integrated sound system
  1943. * of Compaq Deskpro XL.
  1944. */
  1945. static int init_deskpro(struct address_info *hw_config)
  1946. {
  1947. unsigned char tmp;
  1948. if ((tmp = inb(0xc44)) == 0xff)
  1949. {
  1950. DDB(printk("init_deskpro: Dead port 0xc44\n"));
  1951. return 0;
  1952. }
  1953. outb((tmp | 0x04), 0xc44); /* Select bank 1 */
  1954. if (inb(0xc44) != 0x04)
  1955. {
  1956. DDB(printk("init_deskpro: Invalid bank1 signature in port 0xc44\n"));
  1957. return 0;
  1958. }
  1959. /*
  1960. * OK. It looks like a Deskpro so let's proceed.
  1961. */
  1962. /*
  1963. * I/O port 0xc44 Audio configuration register.
  1964. *
  1965. * bits 0xc0: Audio revision bits
  1966. * 0x00 = Compaq Business Audio
  1967. * 0x40 = MS Sound System Compatible (reset default)
  1968. * 0x80 = Reserved
  1969. * 0xc0 = Reserved
  1970. * bit 0x20: No Wait State Enable
  1971. * 0x00 = Disabled (reset default, DMA mode)
  1972. * 0x20 = Enabled (programmed I/O mode)
  1973. * bit 0x10: MS Sound System Decode Enable
  1974. * 0x00 = Decoding disabled (reset default)
  1975. * 0x10 = Decoding enabled
  1976. * bit 0x08: FM Synthesis Decode Enable
  1977. * 0x00 = Decoding Disabled (reset default)
  1978. * 0x08 = Decoding enabled
  1979. * bit 0x04 Bank select
  1980. * 0x00 = Bank 0
  1981. * 0x04 = Bank 1
  1982. * bits 0x03 MSS Base address
  1983. * 0x00 = 0x530 (reset default)
  1984. * 0x01 = 0x604
  1985. * 0x02 = 0xf40
  1986. * 0x03 = 0xe80
  1987. */
  1988. #ifdef DEBUGXL
  1989. /* Debug printing */
  1990. printk("Port 0xc44 (before): ");
  1991. outb((tmp & ~0x04), 0xc44);
  1992. printk("%02x ", inb(0xc44));
  1993. outb((tmp | 0x04), 0xc44);
  1994. printk("%02x\n", inb(0xc44));
  1995. #endif
  1996. /* Set bank 1 of the register */
  1997. tmp = 0x58; /* MSS Mode, MSS&FM decode enabled */
  1998. switch (hw_config->io_base)
  1999. {
  2000. case 0x530:
  2001. tmp |= 0x00;
  2002. break;
  2003. case 0x604:
  2004. tmp |= 0x01;
  2005. break;
  2006. case 0xf40:
  2007. tmp |= 0x02;
  2008. break;
  2009. case 0xe80:
  2010. tmp |= 0x03;
  2011. break;
  2012. default:
  2013. DDB(printk("init_deskpro: Invalid MSS port %x\n", hw_config->io_base));
  2014. return 0;
  2015. }
  2016. outb((tmp & ~0x04), 0xc44); /* Write to bank=0 */
  2017. #ifdef DEBUGXL
  2018. /* Debug printing */
  2019. printk("Port 0xc44 (after): ");
  2020. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2021. printk("%02x ", inb(0xc44));
  2022. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2023. printk("%02x\n", inb(0xc44));
  2024. #endif
  2025. /*
  2026. * I/O port 0xc45 FM Address Decode/MSS ID Register.
  2027. *
  2028. * bank=0, bits 0xfe: FM synthesis Decode Compare bits 7:1 (default=0x88)
  2029. * bank=0, bit 0x01: SBIC Power Control Bit
  2030. * 0x00 = Powered up
  2031. * 0x01 = Powered down
  2032. * bank=1, bits 0xfc: MSS ID (default=0x40)
  2033. */
  2034. #ifdef DEBUGXL
  2035. /* Debug printing */
  2036. printk("Port 0xc45 (before): ");
  2037. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2038. printk("%02x ", inb(0xc45));
  2039. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2040. printk("%02x\n", inb(0xc45));
  2041. #endif
  2042. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2043. outb((0x88), 0xc45); /* FM base 7:0 = 0x88 */
  2044. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2045. outb((0x10), 0xc45); /* MSS ID = 0x10 (MSS port returns 0x04) */
  2046. #ifdef DEBUGXL
  2047. /* Debug printing */
  2048. printk("Port 0xc45 (after): ");
  2049. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2050. printk("%02x ", inb(0xc45));
  2051. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2052. printk("%02x\n", inb(0xc45));
  2053. #endif
  2054. /*
  2055. * I/O port 0xc46 FM Address Decode/Address ASIC Revision Register.
  2056. *
  2057. * bank=0, bits 0xff: FM synthesis Decode Compare bits 15:8 (default=0x03)
  2058. * bank=1, bits 0xff: Audio addressing ASIC id
  2059. */
  2060. #ifdef DEBUGXL
  2061. /* Debug printing */
  2062. printk("Port 0xc46 (before): ");
  2063. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2064. printk("%02x ", inb(0xc46));
  2065. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2066. printk("%02x\n", inb(0xc46));
  2067. #endif
  2068. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2069. outb((0x03), 0xc46); /* FM base 15:8 = 0x03 */
  2070. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2071. outb((0x11), 0xc46); /* ASIC ID = 0x11 */
  2072. #ifdef DEBUGXL
  2073. /* Debug printing */
  2074. printk("Port 0xc46 (after): ");
  2075. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2076. printk("%02x ", inb(0xc46));
  2077. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2078. printk("%02x\n", inb(0xc46));
  2079. #endif
  2080. /*
  2081. * I/O port 0xc47 FM Address Decode Register.
  2082. *
  2083. * bank=0, bits 0xff: Decode enable selection for various FM address bits
  2084. * bank=1, bits 0xff: Reserved
  2085. */
  2086. #ifdef DEBUGXL
  2087. /* Debug printing */
  2088. printk("Port 0xc47 (before): ");
  2089. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2090. printk("%02x ", inb(0xc47));
  2091. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2092. printk("%02x\n", inb(0xc47));
  2093. #endif
  2094. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2095. outb((0x7c), 0xc47); /* FM decode enable bits = 0x7c */
  2096. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2097. outb((0x00), 0xc47); /* Reserved bank1 = 0x00 */
  2098. #ifdef DEBUGXL
  2099. /* Debug printing */
  2100. printk("Port 0xc47 (after): ");
  2101. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2102. printk("%02x ", inb(0xc47));
  2103. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2104. printk("%02x\n", inb(0xc47));
  2105. #endif
  2106. /*
  2107. * I/O port 0xc6f = Audio Disable Function Register
  2108. */
  2109. #ifdef DEBUGXL
  2110. printk("Port 0xc6f (before) = %02x\n", inb(0xc6f));
  2111. #endif
  2112. outb((0x80), 0xc6f);
  2113. #ifdef DEBUGXL
  2114. printk("Port 0xc6f (after) = %02x\n", inb(0xc6f));
  2115. #endif
  2116. return 1;
  2117. }
  2118. int probe_ms_sound(struct address_info *hw_config, struct resource *ports)
  2119. {
  2120. unsigned char tmp;
  2121. DDB(printk("Entered probe_ms_sound(%x, %d)\n", hw_config->io_base, hw_config->card_subtype));
  2122. if (hw_config->card_subtype == 1) /* Has no IRQ/DMA registers */
  2123. {
  2124. /* check_opl3(0x388, hw_config); */
  2125. return ad1848_detect(ports, NULL, hw_config->osp);
  2126. }
  2127. if (deskpro_xl && hw_config->card_subtype == 2) /* Compaq Deskpro XL */
  2128. {
  2129. if (!init_deskpro(hw_config))
  2130. return 0;
  2131. }
  2132. if (deskpro_m) /* Compaq Deskpro M */
  2133. {
  2134. if (!init_deskpro_m(hw_config))
  2135. return 0;
  2136. }
  2137. /*
  2138. * Check if the IO port returns valid signature. The original MS Sound
  2139. * system returns 0x04 while some cards (AudioTrix Pro for example)
  2140. * return 0x00 or 0x0f.
  2141. */
  2142. if ((tmp = inb(hw_config->io_base + 3)) == 0xff) /* Bus float */
  2143. {
  2144. int ret;
  2145. DDB(printk("I/O address is inactive (%x)\n", tmp));
  2146. if (!(ret = ad1848_detect(ports, NULL, hw_config->osp)))
  2147. return 0;
  2148. return 1;
  2149. }
  2150. DDB(printk("MSS signature = %x\n", tmp & 0x3f));
  2151. if ((tmp & 0x3f) != 0x04 &&
  2152. (tmp & 0x3f) != 0x0f &&
  2153. (tmp & 0x3f) != 0x00)
  2154. {
  2155. int ret;
  2156. MDB(printk(KERN_ERR "No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, (int) inb(hw_config->io_base + 3)));
  2157. DDB(printk("Trying to detect codec anyway but IRQ/DMA may not work\n"));
  2158. if (!(ret = ad1848_detect(ports, NULL, hw_config->osp)))
  2159. return 0;
  2160. hw_config->card_subtype = 1;
  2161. return 1;
  2162. }
  2163. if ((hw_config->irq != 5) &&
  2164. (hw_config->irq != 7) &&
  2165. (hw_config->irq != 9) &&
  2166. (hw_config->irq != 10) &&
  2167. (hw_config->irq != 11) &&
  2168. (hw_config->irq != 12))
  2169. {
  2170. printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
  2171. return 0;
  2172. }
  2173. if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
  2174. {
  2175. printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
  2176. return 0;
  2177. }
  2178. /*
  2179. * Check that DMA0 is not in use with a 8 bit board.
  2180. */
  2181. if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
  2182. {
  2183. printk(KERN_ERR "MSS: Can't use DMA0 with a 8 bit card/slot\n");
  2184. return 0;
  2185. }
  2186. if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
  2187. {
  2188. printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
  2189. return 0;
  2190. }
  2191. return ad1848_detect(ports, NULL, hw_config->osp);
  2192. }
  2193. void attach_ms_sound(struct address_info *hw_config, struct resource *ports, struct module *owner)
  2194. {
  2195. static signed char interrupt_bits[12] =
  2196. {
  2197. -1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20
  2198. };
  2199. signed char bits;
  2200. char dma2_bit = 0;
  2201. static char dma_bits[4] =
  2202. {
  2203. 1, 2, 0, 3
  2204. };
  2205. int config_port = hw_config->io_base + 0;
  2206. int version_port = hw_config->io_base + 3;
  2207. int dma = hw_config->dma;
  2208. int dma2 = hw_config->dma2;
  2209. if (hw_config->card_subtype == 1) /* Has no IRQ/DMA registers */
  2210. {
  2211. hw_config->slots[0] = ad1848_init("MS Sound System", ports,
  2212. hw_config->irq,
  2213. hw_config->dma,
  2214. hw_config->dma2, 0,
  2215. hw_config->osp,
  2216. owner);
  2217. return;
  2218. }
  2219. /*
  2220. * Set the IRQ and DMA addresses.
  2221. */
  2222. bits = interrupt_bits[hw_config->irq];
  2223. if (bits == -1)
  2224. {
  2225. printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
  2226. release_region(ports->start, 4);
  2227. release_region(ports->start - 4, 4);
  2228. return;
  2229. }
  2230. outb((bits | 0x40), config_port);
  2231. if ((inb(version_port) & 0x40) == 0)
  2232. printk(KERN_ERR "[MSS: IRQ Conflict?]\n");
  2233. /*
  2234. * Handle the capture DMA channel
  2235. */
  2236. if (dma2 != -1 && dma2 != dma)
  2237. {
  2238. if (!((dma == 0 && dma2 == 1) ||
  2239. (dma == 1 && dma2 == 0) ||
  2240. (dma == 3 && dma2 == 0)))
  2241. { /* Unsupported combination. Try to swap channels */
  2242. int tmp = dma;
  2243. dma = dma2;
  2244. dma2 = tmp;
  2245. }
  2246. if ((dma == 0 && dma2 == 1) ||
  2247. (dma == 1 && dma2 == 0) ||
  2248. (dma == 3 && dma2 == 0))
  2249. {
  2250. dma2_bit = 0x04; /* Enable capture DMA */
  2251. }
  2252. else
  2253. {
  2254. printk(KERN_WARNING "MSS: Invalid capture DMA\n");
  2255. dma2 = dma;
  2256. }
  2257. }
  2258. else
  2259. {
  2260. dma2 = dma;
  2261. }
  2262. hw_config->dma = dma;
  2263. hw_config->dma2 = dma2;
  2264. outb((bits | dma_bits[dma] | dma2_bit), config_port); /* Write IRQ+DMA setup */
  2265. hw_config->slots[0] = ad1848_init("MS Sound System", ports,
  2266. hw_config->irq,
  2267. dma, dma2, 0,
  2268. hw_config->osp,
  2269. THIS_MODULE);
  2270. }
  2271. void unload_ms_sound(struct address_info *hw_config)
  2272. {
  2273. ad1848_unload(hw_config->io_base + 4,
  2274. hw_config->irq,
  2275. hw_config->dma,
  2276. hw_config->dma2, 0);
  2277. sound_unload_audiodev(hw_config->slots[0]);
  2278. release_region(hw_config->io_base, 4);
  2279. }
  2280. #ifndef EXCLUDE_TIMERS
  2281. /*
  2282. * Timer stuff (for /dev/music).
  2283. */
  2284. static unsigned int current_interval;
  2285. static unsigned int ad1848_tmr_start(int dev, unsigned int usecs)
  2286. {
  2287. unsigned long flags;
  2288. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  2289. unsigned long xtal_nsecs; /* nanoseconds per xtal oscillator tick */
  2290. unsigned long divider;
  2291. spin_lock_irqsave(&devc->lock,flags);
  2292. /*
  2293. * Length of the timer interval (in nanoseconds) depends on the
  2294. * selected crystal oscillator. Check this from bit 0x01 of I8.
  2295. *
  2296. * AD1845 has just one oscillator which has cycle time of 10.050 us
  2297. * (when a 24.576 MHz xtal oscillator is used).
  2298. *
  2299. * Convert requested interval to nanoseconds before computing
  2300. * the timer divider.
  2301. */
  2302. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
  2303. xtal_nsecs = 10050;
  2304. else if (ad_read(devc, 8) & 0x01)
  2305. xtal_nsecs = 9920;
  2306. else
  2307. xtal_nsecs = 9969;
  2308. divider = (usecs * 1000 + xtal_nsecs / 2) / xtal_nsecs;
  2309. if (divider < 100) /* Don't allow shorter intervals than about 1ms */
  2310. divider = 100;
  2311. if (divider > 65535) /* Overflow check */
  2312. divider = 65535;
  2313. ad_write(devc, 21, (divider >> 8) & 0xff); /* Set upper bits */
  2314. ad_write(devc, 20, divider & 0xff); /* Set lower bits */
  2315. ad_write(devc, 16, ad_read(devc, 16) | 0x40); /* Start the timer */
  2316. devc->timer_running = 1;
  2317. spin_unlock_irqrestore(&devc->lock,flags);
  2318. return current_interval = (divider * xtal_nsecs + 500) / 1000;
  2319. }
  2320. static void ad1848_tmr_reprogram(int dev)
  2321. {
  2322. /*
  2323. * Audio driver has changed sampling rate so that a different xtal
  2324. * oscillator was selected. We have to reprogram the timer rate.
  2325. */
  2326. ad1848_tmr_start(dev, current_interval);
  2327. sound_timer_syncinterval(current_interval);
  2328. }
  2329. static void ad1848_tmr_disable(int dev)
  2330. {
  2331. unsigned long flags;
  2332. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  2333. spin_lock_irqsave(&devc->lock,flags);
  2334. ad_write(devc, 16, ad_read(devc, 16) & ~0x40);
  2335. devc->timer_running = 0;
  2336. spin_unlock_irqrestore(&devc->lock,flags);
  2337. }
  2338. static void ad1848_tmr_restart(int dev)
  2339. {
  2340. unsigned long flags;
  2341. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  2342. if (current_interval == 0)
  2343. return;
  2344. spin_lock_irqsave(&devc->lock,flags);
  2345. ad_write(devc, 16, ad_read(devc, 16) | 0x40);
  2346. devc->timer_running = 1;
  2347. spin_unlock_irqrestore(&devc->lock,flags);
  2348. }
  2349. static struct sound_lowlev_timer ad1848_tmr =
  2350. {
  2351. 0,
  2352. 2,
  2353. ad1848_tmr_start,
  2354. ad1848_tmr_disable,
  2355. ad1848_tmr_restart
  2356. };
  2357. static int ad1848_tmr_install(int dev)
  2358. {
  2359. if (timer_installed != -1)
  2360. return 0; /* Don't install another timer */
  2361. timer_installed = ad1848_tmr.dev = dev;
  2362. sound_timer_init(&ad1848_tmr, audio_devs[dev]->name);
  2363. return 1;
  2364. }
  2365. #endif /* EXCLUDE_TIMERS */
  2366. EXPORT_SYMBOL(ad1848_detect);
  2367. EXPORT_SYMBOL(ad1848_init);
  2368. EXPORT_SYMBOL(ad1848_unload);
  2369. EXPORT_SYMBOL(ad1848_control);
  2370. EXPORT_SYMBOL(probe_ms_sound);
  2371. EXPORT_SYMBOL(attach_ms_sound);
  2372. EXPORT_SYMBOL(unload_ms_sound);
  2373. static int __initdata io = -1;
  2374. static int __initdata irq = -1;
  2375. static int __initdata dma = -1;
  2376. static int __initdata dma2 = -1;
  2377. static int __initdata type = 0;
  2378. module_param(io, int, 0); /* I/O for a raw AD1848 card */
  2379. module_param(irq, int, 0); /* IRQ to use */
  2380. module_param(dma, int, 0); /* First DMA channel */
  2381. module_param(dma2, int, 0); /* Second DMA channel */
  2382. module_param(type, int, 0); /* Card type */
  2383. module_param(deskpro_xl, bool, 0); /* Special magic for Deskpro XL boxen */
  2384. module_param(deskpro_m, bool, 0); /* Special magic for Deskpro M box */
  2385. module_param(soundpro, bool, 0); /* More special magic for SoundPro chips */
  2386. #ifdef CONFIG_PNP
  2387. module_param(isapnp, int, 0);
  2388. module_param(isapnpjump, int, 0);
  2389. module_param(reverse, bool, 0);
  2390. MODULE_PARM_DESC(isapnp, "When set to 0, Plug & Play support will be disabled");
  2391. MODULE_PARM_DESC(isapnpjump, "Jumps to a specific slot in the driver's PnP table. Use the source, Luke.");
  2392. MODULE_PARM_DESC(reverse, "When set to 1, will reverse ISAPnP search order");
  2393. static struct pnp_dev *ad1848_dev = NULL;
  2394. /* Please add new entries at the end of the table */
  2395. static struct {
  2396. char *name;
  2397. unsigned short card_vendor, card_device,
  2398. vendor, function;
  2399. short mss_io, irq, dma, dma2; /* index into isapnp table */
  2400. int type;
  2401. } ad1848_isapnp_list[] __initdata = {
  2402. {"CMI 8330 SoundPRO",
  2403. ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
  2404. ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001),
  2405. 0, 0, 0,-1, 0},
  2406. {"CS4232 based card",
  2407. ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2408. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000),
  2409. 0, 0, 0, 1, 0},
  2410. {"CS4232 based card",
  2411. ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2412. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100),
  2413. 0, 0, 0, 1, 0},
  2414. {"OPL3-SA2 WSS mode",
  2415. ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2416. ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021),
  2417. 1, 0, 0, 1, 1},
  2418. {"Advanced Gravis InterWave Audio",
  2419. ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
  2420. ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000),
  2421. 0, 0, 0, 1, 0},
  2422. {NULL}
  2423. };
  2424. static struct isapnp_device_id id_table[] __devinitdata = {
  2425. { ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
  2426. ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001), 0 },
  2427. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2428. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000), 0 },
  2429. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2430. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100), 0 },
  2431. /* The main driver for this card is opl3sa2
  2432. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2433. ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021), 0 },
  2434. */
  2435. { ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
  2436. ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000), 0 },
  2437. {0}
  2438. };
  2439. MODULE_DEVICE_TABLE(isapnp, id_table);
  2440. static struct pnp_dev *activate_dev(char *devname, char *resname, struct pnp_dev *dev)
  2441. {
  2442. int err;
  2443. err = pnp_device_attach(dev);
  2444. if (err < 0)
  2445. return(NULL);
  2446. if((err = pnp_activate_dev(dev)) < 0) {
  2447. printk(KERN_ERR "ad1848: %s %s config failed (out of resources?)[%d]\n", devname, resname, err);
  2448. pnp_device_detach(dev);
  2449. return(NULL);
  2450. }
  2451. audio_activated = 1;
  2452. return(dev);
  2453. }
  2454. static struct pnp_dev __init *ad1848_init_generic(struct pnp_card *bus,
  2455. struct address_info *hw_config, int slot)
  2456. {
  2457. /* Configure Audio device */
  2458. if((ad1848_dev = pnp_find_dev(bus, ad1848_isapnp_list[slot].vendor, ad1848_isapnp_list[slot].function, NULL)))
  2459. {
  2460. if((ad1848_dev = activate_dev(ad1848_isapnp_list[slot].name, "ad1848", ad1848_dev)))
  2461. {
  2462. hw_config->io_base = pnp_port_start(ad1848_dev, ad1848_isapnp_list[slot].mss_io);
  2463. hw_config->irq = pnp_irq(ad1848_dev, ad1848_isapnp_list[slot].irq);
  2464. hw_config->dma = pnp_dma(ad1848_dev, ad1848_isapnp_list[slot].dma);
  2465. if(ad1848_isapnp_list[slot].dma2 != -1)
  2466. hw_config->dma2 = pnp_dma(ad1848_dev, ad1848_isapnp_list[slot].dma2);
  2467. else
  2468. hw_config->dma2 = -1;
  2469. hw_config->card_subtype = ad1848_isapnp_list[slot].type;
  2470. } else
  2471. return(NULL);
  2472. } else
  2473. return(NULL);
  2474. return(ad1848_dev);
  2475. }
  2476. static int __init ad1848_isapnp_init(struct address_info *hw_config, struct pnp_card *bus, int slot)
  2477. {
  2478. char *busname = bus->name[0] ? bus->name : ad1848_isapnp_list[slot].name;
  2479. /* Initialize this baby. */
  2480. if(ad1848_init_generic(bus, hw_config, slot)) {
  2481. /* We got it. */
  2482. printk(KERN_NOTICE "ad1848: PnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
  2483. busname,
  2484. hw_config->io_base, hw_config->irq, hw_config->dma,
  2485. hw_config->dma2);
  2486. return 1;
  2487. }
  2488. return 0;
  2489. }
  2490. static int __init ad1848_isapnp_probe(struct address_info *hw_config)
  2491. {
  2492. static int first = 1;
  2493. int i;
  2494. /* Count entries in sb_isapnp_list */
  2495. for (i = 0; ad1848_isapnp_list[i].card_vendor != 0; i++);
  2496. i--;
  2497. /* Check and adjust isapnpjump */
  2498. if( isapnpjump < 0 || isapnpjump > i) {
  2499. isapnpjump = reverse ? i : 0;
  2500. printk(KERN_ERR "ad1848: Valid range for isapnpjump is 0-%d. Adjusted to %d.\n", i, isapnpjump);
  2501. }
  2502. if(!first || !reverse)
  2503. i = isapnpjump;
  2504. first = 0;
  2505. while(ad1848_isapnp_list[i].card_vendor != 0) {
  2506. static struct pnp_card *bus = NULL;
  2507. while ((bus = pnp_find_card(
  2508. ad1848_isapnp_list[i].card_vendor,
  2509. ad1848_isapnp_list[i].card_device,
  2510. bus))) {
  2511. if(ad1848_isapnp_init(hw_config, bus, i)) {
  2512. isapnpjump = i; /* start next search from here */
  2513. return 0;
  2514. }
  2515. }
  2516. i += reverse ? -1 : 1;
  2517. }
  2518. return -ENODEV;
  2519. }
  2520. #endif
  2521. static int __init init_ad1848(void)
  2522. {
  2523. printk(KERN_INFO "ad1848/cs4248 codec driver Copyright (C) by Hannu Savolainen 1993-1996\n");
  2524. #ifdef CONFIG_PNP
  2525. if(isapnp && (ad1848_isapnp_probe(&cfg) < 0) ) {
  2526. printk(KERN_NOTICE "ad1848: No ISAPnP cards found, trying standard ones...\n");
  2527. isapnp = 0;
  2528. }
  2529. #endif
  2530. if(io != -1) {
  2531. struct resource *ports;
  2532. if( isapnp == 0 )
  2533. {
  2534. if(irq == -1 || dma == -1) {
  2535. printk(KERN_WARNING "ad1848: must give I/O , IRQ and DMA.\n");
  2536. return -EINVAL;
  2537. }
  2538. cfg.irq = irq;
  2539. cfg.io_base = io;
  2540. cfg.dma = dma;
  2541. cfg.dma2 = dma2;
  2542. cfg.card_subtype = type;
  2543. }
  2544. ports = request_region(io + 4, 4, "ad1848");
  2545. if (!ports)
  2546. return -EBUSY;
  2547. if (!request_region(io, 4, "WSS config")) {
  2548. release_region(io + 4, 4);
  2549. return -EBUSY;
  2550. }
  2551. if (!probe_ms_sound(&cfg, ports)) {
  2552. release_region(io + 4, 4);
  2553. release_region(io, 4);
  2554. return -ENODEV;
  2555. }
  2556. attach_ms_sound(&cfg, ports, THIS_MODULE);
  2557. loaded = 1;
  2558. }
  2559. return 0;
  2560. }
  2561. static void __exit cleanup_ad1848(void)
  2562. {
  2563. if(loaded)
  2564. unload_ms_sound(&cfg);
  2565. #ifdef CONFIG_PNP
  2566. if(ad1848_dev){
  2567. if(audio_activated)
  2568. pnp_device_detach(ad1848_dev);
  2569. }
  2570. #endif
  2571. }
  2572. module_init(init_ad1848);
  2573. module_exit(cleanup_ad1848);
  2574. #ifndef MODULE
  2575. static int __init setup_ad1848(char *str)
  2576. {
  2577. /* io, irq, dma, dma2, type */
  2578. int ints[6];
  2579. str = get_options(str, ARRAY_SIZE(ints), ints);
  2580. io = ints[1];
  2581. irq = ints[2];
  2582. dma = ints[3];
  2583. dma2 = ints[4];
  2584. type = ints[5];
  2585. return 1;
  2586. }
  2587. __setup("ad1848=", setup_ad1848);
  2588. #endif
  2589. MODULE_LICENSE("GPL");