sb_audio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. }