sb_audio.c 27 KB

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