sb_audio.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * sound/oss/sb_audio.c
  3. *
  4. * Audio routines for Sound Blaster compatible cards.
  5. *
  6. *
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. *
  13. * Changes
  14. * Alan Cox : Formatting and clean ups
  15. *
  16. * Status
  17. * Mostly working. Weird uart bug causing irq storms
  18. *
  19. * Daniel J. Rodriksson: Changes to make sb16 work full duplex.
  20. * Maybe other 16 bit cards in this code could behave
  21. * the same.
  22. * Chris Rankin: Use spinlocks instead of CLI/STI
  23. */
  24. #include <linux/spinlock.h>
  25. #include "sound_config.h"
  26. #include "sb_mixer.h"
  27. #include "sb.h"
  28. #include "sb_ess.h"
  29. int sb_audio_open(int dev, int mode)
  30. {
  31. sb_devc *devc = audio_devs[dev]->devc;
  32. unsigned long flags;
  33. if (devc == NULL)
  34. {
  35. printk(KERN_ERR "Sound Blaster: incomplete initialization.\n");
  36. return -ENXIO;
  37. }
  38. if (devc->caps & SB_NO_RECORDING && mode & OPEN_READ)
  39. {
  40. if (mode == OPEN_READ)
  41. return -EPERM;
  42. }
  43. spin_lock_irqsave(&devc->lock, flags);
  44. if (devc->opened)
  45. {
  46. spin_unlock_irqrestore(&devc->lock, flags);
  47. return -EBUSY;
  48. }
  49. if (devc->dma16 != -1 && devc->dma16 != devc->dma8 && !devc->duplex)
  50. {
  51. if (sound_open_dma(devc->dma16, "Sound Blaster 16 bit"))
  52. {
  53. spin_unlock_irqrestore(&devc->lock, flags);
  54. return -EBUSY;
  55. }
  56. }
  57. devc->opened = mode;
  58. spin_unlock_irqrestore(&devc->lock, flags);
  59. devc->irq_mode = IMODE_NONE;
  60. devc->irq_mode_16 = IMODE_NONE;
  61. devc->fullduplex = devc->duplex &&
  62. ((mode & OPEN_READ) && (mode & OPEN_WRITE));
  63. sb_dsp_reset(devc);
  64. /* At first glance this check isn't enough, some ESS chips might not
  65. * have a RECLEV. However if they don't common_mixer_set will refuse
  66. * cause devc->iomap has no register mapping for RECLEV
  67. */
  68. if (devc->model == MDL_ESS) ess_mixer_reload (devc, SOUND_MIXER_RECLEV);
  69. /* The ALS007 seems to require that the DSP be removed from the output */
  70. /* in order for recording to be activated properly. This is done by */
  71. /* setting the appropriate bits of the output control register 4ch to */
  72. /* zero. This code assumes that the output control registers are not */
  73. /* used anywhere else and therefore the DSP bits are *always* ON for */
  74. /* output and OFF for sampling. */
  75. if (devc->submodel == SUBMDL_ALS007)
  76. {
  77. if (mode & OPEN_READ)
  78. sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
  79. sb_getmixer(devc,ALS007_OUTPUT_CTRL2) & 0xf9);
  80. else
  81. sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
  82. sb_getmixer(devc,ALS007_OUTPUT_CTRL2) | 0x06);
  83. }
  84. return 0;
  85. }
  86. void sb_audio_close(int dev)
  87. {
  88. sb_devc *devc = audio_devs[dev]->devc;
  89. /* fix things if mmap turned off fullduplex */
  90. if(devc->duplex
  91. && !devc->fullduplex
  92. && (devc->opened & OPEN_READ) && (devc->opened & OPEN_WRITE))
  93. {
  94. struct dma_buffparms *dmap_temp;
  95. dmap_temp = audio_devs[dev]->dmap_out;
  96. audio_devs[dev]->dmap_out = audio_devs[dev]->dmap_in;
  97. audio_devs[dev]->dmap_in = dmap_temp;
  98. }
  99. audio_devs[dev]->dmap_out->dma = devc->dma8;
  100. audio_devs[dev]->dmap_in->dma = ( devc->duplex ) ?
  101. devc->dma16 : devc->dma8;
  102. if (devc->dma16 != -1 && devc->dma16 != devc->dma8 && !devc->duplex)
  103. sound_close_dma(devc->dma16);
  104. /* For ALS007, turn DSP output back on if closing the device for read */
  105. if ((devc->submodel == SUBMDL_ALS007) && (devc->opened & OPEN_READ))
  106. {
  107. sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
  108. sb_getmixer(devc,ALS007_OUTPUT_CTRL2) | 0x06);
  109. }
  110. devc->opened = 0;
  111. }
  112. static void sb_set_output_parms(int dev, unsigned long buf, int nr_bytes,
  113. int intrflag)
  114. {
  115. sb_devc *devc = audio_devs[dev]->devc;
  116. if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
  117. {
  118. devc->trg_buf = buf;
  119. devc->trg_bytes = nr_bytes;
  120. devc->trg_intrflag = intrflag;
  121. devc->irq_mode = IMODE_OUTPUT;
  122. }
  123. else
  124. {
  125. devc->trg_buf_16 = buf;
  126. devc->trg_bytes_16 = nr_bytes;
  127. devc->trg_intrflag_16 = intrflag;
  128. devc->irq_mode_16 = IMODE_OUTPUT;
  129. }
  130. }
  131. static void sb_set_input_parms(int dev, unsigned long buf, int count, int intrflag)
  132. {
  133. sb_devc *devc = audio_devs[dev]->devc;
  134. if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
  135. {
  136. devc->trg_buf = buf;
  137. devc->trg_bytes = count;
  138. devc->trg_intrflag = intrflag;
  139. devc->irq_mode = IMODE_INPUT;
  140. }
  141. else
  142. {
  143. devc->trg_buf_16 = buf;
  144. devc->trg_bytes_16 = count;
  145. devc->trg_intrflag_16 = intrflag;
  146. devc->irq_mode_16 = IMODE_INPUT;
  147. }
  148. }
  149. /*
  150. * SB1.x compatible routines
  151. */
  152. static void sb1_audio_output_block(int dev, unsigned long buf, int nr_bytes, int intrflag)
  153. {
  154. unsigned long flags;
  155. int count = nr_bytes;
  156. sb_devc *devc = audio_devs[dev]->devc;
  157. /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
  158. if (audio_devs[dev]->dmap_out->dma > 3)
  159. count >>= 1;
  160. count--;
  161. devc->irq_mode = IMODE_OUTPUT;
  162. spin_lock_irqsave(&devc->lock, flags);
  163. if (sb_dsp_command(devc, 0x14)) /* 8 bit DAC using DMA */
  164. {
  165. sb_dsp_command(devc, (unsigned char) (count & 0xff));
  166. sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
  167. }
  168. else
  169. printk(KERN_WARNING "Sound Blaster: unable to start DAC.\n");
  170. spin_unlock_irqrestore(&devc->lock, flags);
  171. devc->intr_active = 1;
  172. }
  173. static void sb1_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
  174. {
  175. unsigned long flags;
  176. int count = nr_bytes;
  177. sb_devc *devc = audio_devs[dev]->devc;
  178. /*
  179. * Start a DMA input to the buffer pointed by dmaqtail
  180. */
  181. /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
  182. if (audio_devs[dev]->dmap_out->dma > 3)
  183. count >>= 1;
  184. count--;
  185. devc->irq_mode = IMODE_INPUT;
  186. spin_lock_irqsave(&devc->lock, flags);
  187. if (sb_dsp_command(devc, 0x24)) /* 8 bit ADC using DMA */
  188. {
  189. sb_dsp_command(devc, (unsigned char) (count & 0xff));
  190. sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
  191. }
  192. else
  193. printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
  194. spin_unlock_irqrestore(&devc->lock, flags);
  195. devc->intr_active = 1;
  196. }
  197. static void sb1_audio_trigger(int dev, int bits)
  198. {
  199. sb_devc *devc = audio_devs[dev]->devc;
  200. bits &= devc->irq_mode;
  201. if (!bits)
  202. sb_dsp_command(devc, 0xd0); /* Halt DMA */
  203. else
  204. {
  205. switch (devc->irq_mode)
  206. {
  207. case IMODE_INPUT:
  208. sb1_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
  209. devc->trg_intrflag);
  210. break;
  211. case IMODE_OUTPUT:
  212. sb1_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
  213. devc->trg_intrflag);
  214. break;
  215. }
  216. }
  217. devc->trigger_bits = bits;
  218. }
  219. static int sb1_audio_prepare_for_input(int dev, int bsize, int bcount)
  220. {
  221. sb_devc *devc = audio_devs[dev]->devc;
  222. unsigned long flags;
  223. spin_lock_irqsave(&devc->lock, flags);
  224. if (sb_dsp_command(devc, 0x40))
  225. sb_dsp_command(devc, devc->tconst);
  226. sb_dsp_command(devc, DSP_CMD_SPKOFF);
  227. spin_unlock_irqrestore(&devc->lock, flags);
  228. devc->trigger_bits = 0;
  229. return 0;
  230. }
  231. static int sb1_audio_prepare_for_output(int dev, int bsize, int bcount)
  232. {
  233. sb_devc *devc = audio_devs[dev]->devc;
  234. unsigned long flags;
  235. spin_lock_irqsave(&devc->lock, flags);
  236. if (sb_dsp_command(devc, 0x40))
  237. sb_dsp_command(devc, devc->tconst);
  238. sb_dsp_command(devc, DSP_CMD_SPKON);
  239. spin_unlock_irqrestore(&devc->lock, flags);
  240. devc->trigger_bits = 0;
  241. return 0;
  242. }
  243. static int sb1_audio_set_speed(int dev, int speed)
  244. {
  245. int max_speed = 23000;
  246. sb_devc *devc = audio_devs[dev]->devc;
  247. int tmp;
  248. if (devc->opened & OPEN_READ)
  249. max_speed = 13000;
  250. if (speed > 0)
  251. {
  252. if (speed < 4000)
  253. speed = 4000;
  254. if (speed > max_speed)
  255. speed = max_speed;
  256. devc->tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
  257. tmp = 256 - devc->tconst;
  258. speed = (1000000 + tmp / 2) / tmp;
  259. devc->speed = speed;
  260. }
  261. return devc->speed;
  262. }
  263. static short sb1_audio_set_channels(int dev, short channels)
  264. {
  265. sb_devc *devc = audio_devs[dev]->devc;
  266. return devc->channels = 1;
  267. }
  268. static unsigned int sb1_audio_set_bits(int dev, unsigned int bits)
  269. {
  270. sb_devc *devc = audio_devs[dev]->devc;
  271. return devc->bits = 8;
  272. }
  273. static void sb1_audio_halt_xfer(int dev)
  274. {
  275. unsigned long flags;
  276. sb_devc *devc = audio_devs[dev]->devc;
  277. spin_lock_irqsave(&devc->lock, flags);
  278. sb_dsp_reset(devc);
  279. spin_unlock_irqrestore(&devc->lock, flags);
  280. }
  281. /*
  282. * SB 2.0 and SB 2.01 compatible routines
  283. */
  284. static void sb20_audio_output_block(int dev, unsigned long buf, int nr_bytes,
  285. int intrflag)
  286. {
  287. unsigned long flags;
  288. int count = nr_bytes;
  289. sb_devc *devc = audio_devs[dev]->devc;
  290. unsigned char cmd;
  291. /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
  292. if (audio_devs[dev]->dmap_out->dma > 3)
  293. count >>= 1;
  294. count--;
  295. devc->irq_mode = IMODE_OUTPUT;
  296. spin_lock_irqsave(&devc->lock, flags);
  297. if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
  298. {
  299. sb_dsp_command(devc, (unsigned char) (count & 0xff));
  300. sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
  301. if (devc->speed * devc->channels <= 23000)
  302. cmd = 0x1c; /* 8 bit PCM output */
  303. else
  304. cmd = 0x90; /* 8 bit high speed PCM output (SB2.01/Pro) */
  305. if (!sb_dsp_command(devc, cmd))
  306. printk(KERN_ERR "Sound Blaster: unable to start DAC.\n");
  307. }
  308. else
  309. printk(KERN_ERR "Sound Blaster: unable to start DAC.\n");
  310. spin_unlock_irqrestore(&devc->lock, flags);
  311. devc->intr_active = 1;
  312. }
  313. static void sb20_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
  314. {
  315. unsigned long flags;
  316. int count = nr_bytes;
  317. sb_devc *devc = audio_devs[dev]->devc;
  318. unsigned char cmd;
  319. /*
  320. * Start a DMA input to the buffer pointed by dmaqtail
  321. */
  322. /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
  323. if (audio_devs[dev]->dmap_out->dma > 3)
  324. count >>= 1;
  325. count--;
  326. devc->irq_mode = IMODE_INPUT;
  327. spin_lock_irqsave(&devc->lock, flags);
  328. if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
  329. {
  330. sb_dsp_command(devc, (unsigned char) (count & 0xff));
  331. sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
  332. if (devc->speed * devc->channels <= (devc->major == 3 ? 23000 : 13000))
  333. cmd = 0x2c; /* 8 bit PCM input */
  334. else
  335. cmd = 0x98; /* 8 bit high speed PCM input (SB2.01/Pro) */
  336. if (!sb_dsp_command(devc, cmd))
  337. printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
  338. }
  339. else
  340. printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
  341. spin_unlock_irqrestore(&devc->lock, flags);
  342. devc->intr_active = 1;
  343. }
  344. static void sb20_audio_trigger(int dev, int bits)
  345. {
  346. sb_devc *devc = audio_devs[dev]->devc;
  347. bits &= devc->irq_mode;
  348. if (!bits)
  349. sb_dsp_command(devc, 0xd0); /* Halt DMA */
  350. else
  351. {
  352. switch (devc->irq_mode)
  353. {
  354. case IMODE_INPUT:
  355. sb20_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
  356. devc->trg_intrflag);
  357. break;
  358. case IMODE_OUTPUT:
  359. sb20_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
  360. devc->trg_intrflag);
  361. break;
  362. }
  363. }
  364. devc->trigger_bits = bits;
  365. }
  366. /*
  367. * SB2.01 specific speed setup
  368. */
  369. static int sb201_audio_set_speed(int dev, int speed)
  370. {
  371. sb_devc *devc = audio_devs[dev]->devc;
  372. int tmp;
  373. int s = speed * devc->channels;
  374. if (speed > 0)
  375. {
  376. if (speed < 4000)
  377. speed = 4000;
  378. if (speed > 44100)
  379. speed = 44100;
  380. if (devc->opened & OPEN_READ && speed > 15000)
  381. speed = 15000;
  382. devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
  383. tmp = 256 - devc->tconst;
  384. speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
  385. devc->speed = speed;
  386. }
  387. return devc->speed;
  388. }
  389. /*
  390. * SB Pro specific routines
  391. */
  392. static int sbpro_audio_prepare_for_input(int dev, int bsize, int bcount)
  393. { /* For SB Pro and Jazz16 */
  394. sb_devc *devc = audio_devs[dev]->devc;
  395. unsigned long flags;
  396. unsigned char bits = 0;
  397. if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
  398. audio_devs[dev]->dmap_out->dma = audio_devs[dev]->dmap_in->dma =
  399. devc->bits == 16 ? devc->dma16 : devc->dma8;
  400. if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
  401. if (devc->bits == AFMT_S16_LE)
  402. bits = 0x04; /* 16 bit mode */
  403. spin_lock_irqsave(&devc->lock, flags);
  404. if (sb_dsp_command(devc, 0x40))
  405. sb_dsp_command(devc, devc->tconst);
  406. sb_dsp_command(devc, DSP_CMD_SPKOFF);
  407. if (devc->channels == 1)
  408. sb_dsp_command(devc, 0xa0 | bits); /* Mono input */
  409. else
  410. sb_dsp_command(devc, 0xa8 | bits); /* Stereo input */
  411. spin_unlock_irqrestore(&devc->lock, flags);
  412. devc->trigger_bits = 0;
  413. return 0;
  414. }
  415. static int sbpro_audio_prepare_for_output(int dev, int bsize, int bcount)
  416. { /* For SB Pro and Jazz16 */
  417. sb_devc *devc = audio_devs[dev]->devc;
  418. unsigned long flags;
  419. unsigned char tmp;
  420. unsigned char bits = 0;
  421. if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
  422. audio_devs[dev]->dmap_out->dma = audio_devs[dev]->dmap_in->dma = devc->bits == 16 ? devc->dma16 : devc->dma8;
  423. if (devc->model == MDL_SBPRO)
  424. sb_mixer_set_stereo(devc, devc->channels == 2);
  425. spin_lock_irqsave(&devc->lock, flags);
  426. if (sb_dsp_command(devc, 0x40))
  427. sb_dsp_command(devc, devc->tconst);
  428. sb_dsp_command(devc, DSP_CMD_SPKON);
  429. if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
  430. {
  431. if (devc->bits == AFMT_S16_LE)
  432. bits = 0x04; /* 16 bit mode */
  433. if (devc->channels == 1)
  434. sb_dsp_command(devc, 0xa0 | bits); /* Mono output */
  435. else
  436. sb_dsp_command(devc, 0xa8 | bits); /* Stereo output */
  437. spin_unlock_irqrestore(&devc->lock, flags);
  438. }
  439. else
  440. {
  441. spin_unlock_irqrestore(&devc->lock, flags);
  442. tmp = sb_getmixer(devc, 0x0e);
  443. if (devc->channels == 1)
  444. tmp &= ~0x02;
  445. else
  446. tmp |= 0x02;
  447. sb_setmixer(devc, 0x0e, tmp);
  448. }
  449. devc->trigger_bits = 0;
  450. return 0;
  451. }
  452. static int sbpro_audio_set_speed(int dev, int speed)
  453. {
  454. sb_devc *devc = audio_devs[dev]->devc;
  455. if (speed > 0)
  456. {
  457. if (speed < 4000)
  458. speed = 4000;
  459. if (speed > 44100)
  460. speed = 44100;
  461. if (devc->channels > 1 && speed > 22050)
  462. speed = 22050;
  463. sb201_audio_set_speed(dev, speed);
  464. }
  465. return devc->speed;
  466. }
  467. static short sbpro_audio_set_channels(int dev, short channels)
  468. {
  469. sb_devc *devc = audio_devs[dev]->devc;
  470. if (channels == 1 || channels == 2)
  471. {
  472. if (channels != devc->channels)
  473. {
  474. devc->channels = channels;
  475. if (devc->model == MDL_SBPRO && devc->channels == 2)
  476. sbpro_audio_set_speed(dev, devc->speed);
  477. }
  478. }
  479. return devc->channels;
  480. }
  481. static int jazz16_audio_set_speed(int dev, int speed)
  482. {
  483. sb_devc *devc = audio_devs[dev]->devc;
  484. if (speed > 0)
  485. {
  486. int tmp;
  487. int s = speed * devc->channels;
  488. if (speed < 5000)
  489. speed = 5000;
  490. if (speed > 44100)
  491. speed = 44100;
  492. devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
  493. tmp = 256 - devc->tconst;
  494. speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
  495. devc->speed = speed;
  496. }
  497. return devc->speed;
  498. }
  499. /*
  500. * SB16 specific routines
  501. */
  502. static int sb16_audio_set_speed(int dev, int speed)
  503. {
  504. sb_devc *devc = audio_devs[dev]->devc;
  505. int max_speed = devc->submodel == SUBMDL_ALS100 ? 48000 : 44100;
  506. if (speed > 0)
  507. {
  508. if (speed < 5000)
  509. speed = 5000;
  510. if (speed > max_speed)
  511. speed = max_speed;
  512. devc->speed = speed;
  513. }
  514. return devc->speed;
  515. }
  516. static unsigned int sb16_audio_set_bits(int dev, unsigned int bits)
  517. {
  518. sb_devc *devc = audio_devs[dev]->devc;
  519. if (bits != 0)
  520. {
  521. if (bits == AFMT_U8 || bits == AFMT_S16_LE)
  522. devc->bits = bits;
  523. else
  524. devc->bits = AFMT_U8;
  525. }
  526. return devc->bits;
  527. }
  528. static int sb16_audio_prepare_for_input(int dev, int bsize, int bcount)
  529. {
  530. sb_devc *devc = audio_devs[dev]->devc;
  531. if (!devc->fullduplex)
  532. {
  533. audio_devs[dev]->dmap_out->dma =
  534. audio_devs[dev]->dmap_in->dma =
  535. devc->bits == AFMT_S16_LE ?
  536. devc->dma16 : devc->dma8;
  537. }
  538. else if (devc->bits == AFMT_S16_LE)
  539. {
  540. audio_devs[dev]->dmap_out->dma = devc->dma8;
  541. audio_devs[dev]->dmap_in->dma = devc->dma16;
  542. }
  543. else
  544. {
  545. audio_devs[dev]->dmap_out->dma = devc->dma16;
  546. audio_devs[dev]->dmap_in->dma = devc->dma8;
  547. }
  548. devc->trigger_bits = 0;
  549. return 0;
  550. }
  551. static int sb16_audio_prepare_for_output(int dev, int bsize, int bcount)
  552. {
  553. sb_devc *devc = audio_devs[dev]->devc;
  554. if (!devc->fullduplex)
  555. {
  556. audio_devs[dev]->dmap_out->dma =
  557. audio_devs[dev]->dmap_in->dma =
  558. devc->bits == AFMT_S16_LE ?
  559. devc->dma16 : devc->dma8;
  560. }
  561. else if (devc->bits == AFMT_S16_LE)
  562. {
  563. audio_devs[dev]->dmap_out->dma = devc->dma8;
  564. audio_devs[dev]->dmap_in->dma = devc->dma16;
  565. }
  566. else
  567. {
  568. audio_devs[dev]->dmap_out->dma = devc->dma16;
  569. audio_devs[dev]->dmap_in->dma = devc->dma8;
  570. }
  571. devc->trigger_bits = 0;
  572. return 0;
  573. }
  574. static void sb16_audio_output_block(int dev, unsigned long buf, int count,
  575. int intrflag)
  576. {
  577. unsigned long flags, cnt;
  578. sb_devc *devc = audio_devs[dev]->devc;
  579. unsigned long bits;
  580. if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
  581. {
  582. devc->irq_mode = IMODE_OUTPUT;
  583. devc->intr_active = 1;
  584. }
  585. else
  586. {
  587. devc->irq_mode_16 = IMODE_OUTPUT;
  588. devc->intr_active_16 = 1;
  589. }
  590. /* save value */
  591. spin_lock_irqsave(&devc->lock, flags);
  592. bits = devc->bits;
  593. if (devc->fullduplex)
  594. devc->bits = (devc->bits == AFMT_S16_LE) ?
  595. AFMT_U8 : AFMT_S16_LE;
  596. spin_unlock_irqrestore(&devc->lock, flags);
  597. cnt = count;
  598. if (devc->bits == AFMT_S16_LE)
  599. cnt >>= 1;
  600. cnt--;
  601. spin_lock_irqsave(&devc->lock, flags);
  602. /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
  603. sb_dsp_command(devc, 0x41);
  604. sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
  605. sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
  606. sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xb6 : 0xc6));
  607. sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
  608. (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
  609. sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
  610. sb_dsp_command(devc, (unsigned char) (cnt >> 8));
  611. /* restore real value after all programming */
  612. devc->bits = bits;
  613. spin_unlock_irqrestore(&devc->lock, flags);
  614. }
  615. /*
  616. * This fails on the Cyrix MediaGX. If you don't have the DMA enabled
  617. * before the first sample arrives it locks up. However even if you
  618. * do enable the DMA in time you just get DMA timeouts and missing
  619. * interrupts and stuff, so for now I've not bothered fixing this either.
  620. */
  621. static void sb16_audio_start_input(int dev, unsigned long buf, int count, int intrflag)
  622. {
  623. unsigned long flags, cnt;
  624. sb_devc *devc = audio_devs[dev]->devc;
  625. if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
  626. {
  627. devc->irq_mode = IMODE_INPUT;
  628. devc->intr_active = 1;
  629. }
  630. else
  631. {
  632. devc->irq_mode_16 = IMODE_INPUT;
  633. devc->intr_active_16 = 1;
  634. }
  635. cnt = count;
  636. if (devc->bits == AFMT_S16_LE)
  637. cnt >>= 1;
  638. cnt--;
  639. spin_lock_irqsave(&devc->lock, flags);
  640. /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
  641. sb_dsp_command(devc, 0x42);
  642. sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
  643. sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
  644. sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xbe : 0xce));
  645. sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
  646. (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
  647. sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
  648. sb_dsp_command(devc, (unsigned char) (cnt >> 8));
  649. spin_unlock_irqrestore(&devc->lock, flags);
  650. }
  651. static void sb16_audio_trigger(int dev, int bits)
  652. {
  653. sb_devc *devc = audio_devs[dev]->devc;
  654. int bits_16 = bits & devc->irq_mode_16;
  655. bits &= devc->irq_mode;
  656. if (!bits && !bits_16)
  657. sb_dsp_command(devc, 0xd0); /* Halt DMA */
  658. else
  659. {
  660. if (bits)
  661. {
  662. switch (devc->irq_mode)
  663. {
  664. case IMODE_INPUT:
  665. sb16_audio_start_input(dev,
  666. devc->trg_buf,
  667. devc->trg_bytes,
  668. devc->trg_intrflag);
  669. break;
  670. case IMODE_OUTPUT:
  671. sb16_audio_output_block(dev,
  672. devc->trg_buf,
  673. devc->trg_bytes,
  674. devc->trg_intrflag);
  675. break;
  676. }
  677. }
  678. if (bits_16)
  679. {
  680. switch (devc->irq_mode_16)
  681. {
  682. case IMODE_INPUT:
  683. sb16_audio_start_input(dev,
  684. devc->trg_buf_16,
  685. devc->trg_bytes_16,
  686. devc->trg_intrflag_16);
  687. break;
  688. case IMODE_OUTPUT:
  689. sb16_audio_output_block(dev,
  690. devc->trg_buf_16,
  691. devc->trg_bytes_16,
  692. devc->trg_intrflag_16);
  693. break;
  694. }
  695. }
  696. }
  697. devc->trigger_bits = bits | bits_16;
  698. }
  699. static unsigned char lbuf8[2048];
  700. static signed short *lbuf16 = (signed short *)lbuf8;
  701. #define LBUFCOPYSIZE 1024
  702. static void
  703. sb16_copy_from_user(int dev,
  704. char *localbuf, int localoffs,
  705. const char __user *userbuf, int useroffs,
  706. int max_in, int max_out,
  707. int *used, int *returned,
  708. int len)
  709. {
  710. sb_devc *devc = audio_devs[dev]->devc;
  711. int i, c, p, locallen;
  712. unsigned char *buf8;
  713. signed short *buf16;
  714. /* if not duplex no conversion */
  715. if (!devc->fullduplex)
  716. {
  717. if (copy_from_user(localbuf + localoffs,
  718. userbuf + useroffs, len))
  719. return;
  720. *used = len;
  721. *returned = len;
  722. }
  723. else if (devc->bits == AFMT_S16_LE)
  724. {
  725. /* 16 -> 8 */
  726. /* max_in >> 1, max number of samples in ( 16 bits ) */
  727. /* max_out, max number of samples out ( 8 bits ) */
  728. /* len, number of samples that will be taken ( 16 bits )*/
  729. /* c, count of samples remaining in buffer ( 16 bits )*/
  730. /* p, count of samples already processed ( 16 bits )*/
  731. len = ( (max_in >> 1) > max_out) ? max_out : (max_in >> 1);
  732. c = len;
  733. p = 0;
  734. buf8 = (unsigned char *)(localbuf + localoffs);
  735. while (c)
  736. {
  737. locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
  738. /* << 1 in order to get 16 bit samples */
  739. if (copy_from_user(lbuf16,
  740. userbuf + useroffs + (p << 1),
  741. locallen << 1))
  742. return;
  743. for (i = 0; i < locallen; i++)
  744. {
  745. buf8[p+i] = ~((lbuf16[i] >> 8) & 0xff) ^ 0x80;
  746. }
  747. c -= locallen; p += locallen;
  748. }
  749. /* used = ( samples * 16 bits size ) */
  750. *used = max_in > ( max_out << 1) ? (max_out << 1) : max_in;
  751. /* returned = ( samples * 8 bits size ) */
  752. *returned = len;
  753. }
  754. else
  755. {
  756. /* 8 -> 16 */
  757. /* max_in, max number of samples in ( 8 bits ) */
  758. /* max_out >> 1, max number of samples out ( 16 bits ) */
  759. /* len, number of samples that will be taken ( 8 bits )*/
  760. /* c, count of samples remaining in buffer ( 8 bits )*/
  761. /* p, count of samples already processed ( 8 bits )*/
  762. len = max_in > (max_out >> 1) ? (max_out >> 1) : max_in;
  763. c = len;
  764. p = 0;
  765. buf16 = (signed short *)(localbuf + localoffs);
  766. while (c)
  767. {
  768. locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
  769. if (copy_from_user(lbuf8,
  770. userbuf+useroffs + p,
  771. locallen))
  772. return;
  773. for (i = 0; i < locallen; i++)
  774. {
  775. buf16[p+i] = (~lbuf8[i] ^ 0x80) << 8;
  776. }
  777. c -= locallen; p += locallen;
  778. }
  779. /* used = ( samples * 8 bits size ) */
  780. *used = len;
  781. /* returned = ( samples * 16 bits size ) */
  782. *returned = len << 1;
  783. }
  784. }
  785. static void
  786. sb16_audio_mmap(int dev)
  787. {
  788. sb_devc *devc = audio_devs[dev]->devc;
  789. devc->fullduplex = 0;
  790. }
  791. static struct audio_driver sb1_audio_driver = /* SB1.x */
  792. {
  793. .owner = THIS_MODULE,
  794. .open = sb_audio_open,
  795. .close = sb_audio_close,
  796. .output_block = sb_set_output_parms,
  797. .start_input = sb_set_input_parms,
  798. .prepare_for_input = sb1_audio_prepare_for_input,
  799. .prepare_for_output = sb1_audio_prepare_for_output,
  800. .halt_io = sb1_audio_halt_xfer,
  801. .trigger = sb1_audio_trigger,
  802. .set_speed = sb1_audio_set_speed,
  803. .set_bits = sb1_audio_set_bits,
  804. .set_channels = sb1_audio_set_channels
  805. };
  806. static struct audio_driver sb20_audio_driver = /* SB2.0 */
  807. {
  808. .owner = THIS_MODULE,
  809. .open = sb_audio_open,
  810. .close = sb_audio_close,
  811. .output_block = sb_set_output_parms,
  812. .start_input = sb_set_input_parms,
  813. .prepare_for_input = sb1_audio_prepare_for_input,
  814. .prepare_for_output = sb1_audio_prepare_for_output,
  815. .halt_io = sb1_audio_halt_xfer,
  816. .trigger = sb20_audio_trigger,
  817. .set_speed = sb1_audio_set_speed,
  818. .set_bits = sb1_audio_set_bits,
  819. .set_channels = sb1_audio_set_channels
  820. };
  821. static struct audio_driver sb201_audio_driver = /* SB2.01 */
  822. {
  823. .owner = THIS_MODULE,
  824. .open = sb_audio_open,
  825. .close = sb_audio_close,
  826. .output_block = sb_set_output_parms,
  827. .start_input = sb_set_input_parms,
  828. .prepare_for_input = sb1_audio_prepare_for_input,
  829. .prepare_for_output = sb1_audio_prepare_for_output,
  830. .halt_io = sb1_audio_halt_xfer,
  831. .trigger = sb20_audio_trigger,
  832. .set_speed = sb201_audio_set_speed,
  833. .set_bits = sb1_audio_set_bits,
  834. .set_channels = sb1_audio_set_channels
  835. };
  836. static struct audio_driver sbpro_audio_driver = /* SB Pro */
  837. {
  838. .owner = THIS_MODULE,
  839. .open = sb_audio_open,
  840. .close = sb_audio_close,
  841. .output_block = sb_set_output_parms,
  842. .start_input = sb_set_input_parms,
  843. .prepare_for_input = sbpro_audio_prepare_for_input,
  844. .prepare_for_output = sbpro_audio_prepare_for_output,
  845. .halt_io = sb1_audio_halt_xfer,
  846. .trigger = sb20_audio_trigger,
  847. .set_speed = sbpro_audio_set_speed,
  848. .set_bits = sb1_audio_set_bits,
  849. .set_channels = sbpro_audio_set_channels
  850. };
  851. static struct audio_driver jazz16_audio_driver = /* Jazz16 and SM Wave */
  852. {
  853. .owner = THIS_MODULE,
  854. .open = sb_audio_open,
  855. .close = sb_audio_close,
  856. .output_block = sb_set_output_parms,
  857. .start_input = sb_set_input_parms,
  858. .prepare_for_input = sbpro_audio_prepare_for_input,
  859. .prepare_for_output = sbpro_audio_prepare_for_output,
  860. .halt_io = sb1_audio_halt_xfer,
  861. .trigger = sb20_audio_trigger,
  862. .set_speed = jazz16_audio_set_speed,
  863. .set_bits = sb16_audio_set_bits,
  864. .set_channels = sbpro_audio_set_channels
  865. };
  866. static struct audio_driver sb16_audio_driver = /* SB16 */
  867. {
  868. .owner = THIS_MODULE,
  869. .open = sb_audio_open,
  870. .close = sb_audio_close,
  871. .output_block = sb_set_output_parms,
  872. .start_input = sb_set_input_parms,
  873. .prepare_for_input = sb16_audio_prepare_for_input,
  874. .prepare_for_output = sb16_audio_prepare_for_output,
  875. .halt_io = sb1_audio_halt_xfer,
  876. .copy_user = sb16_copy_from_user,
  877. .trigger = sb16_audio_trigger,
  878. .set_speed = sb16_audio_set_speed,
  879. .set_bits = sb16_audio_set_bits,
  880. .set_channels = sbpro_audio_set_channels,
  881. .mmap = sb16_audio_mmap
  882. };
  883. void sb_audio_init(sb_devc * devc, char *name, struct module *owner)
  884. {
  885. int audio_flags = 0;
  886. int format_mask = AFMT_U8;
  887. struct audio_driver *driver = &sb1_audio_driver;
  888. switch (devc->model)
  889. {
  890. case MDL_SB1: /* SB1.0 or SB 1.5 */
  891. DDB(printk("Will use standard SB1.x driver\n"));
  892. audio_flags = DMA_HARDSTOP;
  893. break;
  894. case MDL_SB2:
  895. DDB(printk("Will use SB2.0 driver\n"));
  896. audio_flags = DMA_AUTOMODE;
  897. driver = &sb20_audio_driver;
  898. break;
  899. case MDL_SB201:
  900. DDB(printk("Will use SB2.01 (high speed) driver\n"));
  901. audio_flags = DMA_AUTOMODE;
  902. driver = &sb201_audio_driver;
  903. break;
  904. case MDL_JAZZ:
  905. case MDL_SMW:
  906. DDB(printk("Will use Jazz16 driver\n"));
  907. audio_flags = DMA_AUTOMODE;
  908. format_mask |= AFMT_S16_LE;
  909. driver = &jazz16_audio_driver;
  910. break;
  911. case MDL_ESS:
  912. DDB(printk("Will use ESS ES688/1688 driver\n"));
  913. driver = ess_audio_init (devc, &audio_flags, &format_mask);
  914. break;
  915. case MDL_SB16:
  916. DDB(printk("Will use SB16 driver\n"));
  917. audio_flags = DMA_AUTOMODE;
  918. format_mask |= AFMT_S16_LE;
  919. if (devc->dma8 != devc->dma16 && devc->dma16 != -1)
  920. {
  921. audio_flags |= DMA_DUPLEX;
  922. devc->duplex = 1;
  923. }
  924. driver = &sb16_audio_driver;
  925. break;
  926. default:
  927. DDB(printk("Will use SB Pro driver\n"));
  928. audio_flags = DMA_AUTOMODE;
  929. driver = &sbpro_audio_driver;
  930. }
  931. if (owner)
  932. driver->owner = owner;
  933. if ((devc->dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
  934. name,driver, sizeof(struct audio_driver),
  935. audio_flags, format_mask, devc,
  936. devc->dma8,
  937. devc->duplex ? devc->dma16 : devc->dma8)) < 0)
  938. {
  939. printk(KERN_ERR "Sound Blaster: unable to install audio.\n");
  940. return;
  941. }
  942. audio_devs[devc->dev]->mixer_dev = devc->my_mixerdev;
  943. audio_devs[devc->dev]->min_fragment = 5;
  944. }