sb16_main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Routines for control of 16-bit SoundBlaster cards and clones
  4. * Note: This is very ugly hardware which uses one 8-bit DMA channel and
  5. * second 16-bit DMA channel. Unfortunately 8-bit DMA channel can't
  6. * transfer 16-bit samples and 16-bit DMA channels can't transfer
  7. * 8-bit samples. This make full duplex more complicated than
  8. * can be... People, don't buy these soundcards for full 16-bit
  9. * duplex!!!
  10. * Note: 16-bit wide is assigned to first direction which made request.
  11. * With full duplex - playback is preferred with abstract layer.
  12. *
  13. * Note: Some chip revisions have hardware bug. Changing capture
  14. * channel from full-duplex 8bit DMA to 16bit DMA will block
  15. * 16bit DMA transfers from DSP chip (capture) until 8bit transfer
  16. * to DSP chip (playback) starts. This bug can be avoided with
  17. * "16bit DMA Allocation" setting set to Playback or Capture.
  18. *
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. *
  34. */
  35. #include <sound/driver.h>
  36. #include <asm/io.h>
  37. #include <asm/dma.h>
  38. #include <linux/init.h>
  39. #include <linux/time.h>
  40. #include <sound/core.h>
  41. #include <sound/sb.h>
  42. #include <sound/sb16_csp.h>
  43. #include <sound/mpu401.h>
  44. #include <sound/control.h>
  45. #include <sound/info.h>
  46. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  47. MODULE_DESCRIPTION("Routines for control of 16-bit SoundBlaster cards and clones");
  48. MODULE_LICENSE("GPL");
  49. #ifdef CONFIG_SND_SB16_CSP
  50. static void snd_sb16_csp_playback_prepare(sb_t *chip, snd_pcm_runtime_t *runtime)
  51. {
  52. if (chip->hardware == SB_HW_16CSP) {
  53. snd_sb_csp_t *csp = chip->csp;
  54. if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
  55. /* manually loaded codec */
  56. if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) &&
  57. ((1U << runtime->format) == csp->acc_format)) {
  58. /* Supported runtime PCM format for playback */
  59. if (csp->ops.csp_use(csp) == 0) {
  60. /* If CSP was successfully acquired */
  61. goto __start_CSP;
  62. }
  63. } else if ((csp->mode & SNDRV_SB_CSP_MODE_QSOUND) && (csp->q_enabled)) {
  64. /* QSound decoder is loaded and enabled */
  65. if ((1 << runtime->format) & (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  66. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE)) {
  67. /* Only for simple PCM formats */
  68. if (csp->ops.csp_use(csp) == 0) {
  69. /* If CSP was successfully acquired */
  70. goto __start_CSP;
  71. }
  72. }
  73. }
  74. } else if (csp->ops.csp_use(csp) == 0) {
  75. /* Acquire CSP and try to autoload hardware codec */
  76. if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_WRITE)) {
  77. /* Unsupported format, release CSP */
  78. csp->ops.csp_unuse(csp);
  79. } else {
  80. __start_CSP:
  81. /* Try to start CSP */
  82. if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_PLAYBACK_16) ?
  83. SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT,
  84. (runtime->channels > 1) ?
  85. SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) {
  86. /* Failed, release CSP */
  87. csp->ops.csp_unuse(csp);
  88. } else {
  89. /* Success, CSP acquired and running */
  90. chip->open = SNDRV_SB_CSP_MODE_DSP_WRITE;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. static void snd_sb16_csp_capture_prepare(sb_t *chip, snd_pcm_runtime_t *runtime)
  97. {
  98. if (chip->hardware == SB_HW_16CSP) {
  99. snd_sb_csp_t *csp = chip->csp;
  100. if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
  101. /* manually loaded codec */
  102. if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) &&
  103. ((1U << runtime->format) == csp->acc_format)) {
  104. /* Supported runtime PCM format for capture */
  105. if (csp->ops.csp_use(csp) == 0) {
  106. /* If CSP was successfully acquired */
  107. goto __start_CSP;
  108. }
  109. }
  110. } else if (csp->ops.csp_use(csp) == 0) {
  111. /* Acquire CSP and try to autoload hardware codec */
  112. if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_READ)) {
  113. /* Unsupported format, release CSP */
  114. csp->ops.csp_unuse(csp);
  115. } else {
  116. __start_CSP:
  117. /* Try to start CSP */
  118. if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_CAPTURE_16) ?
  119. SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT,
  120. (runtime->channels > 1) ?
  121. SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) {
  122. /* Failed, release CSP */
  123. csp->ops.csp_unuse(csp);
  124. } else {
  125. /* Success, CSP acquired and running */
  126. chip->open = SNDRV_SB_CSP_MODE_DSP_READ;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. static void snd_sb16_csp_update(sb_t *chip)
  133. {
  134. if (chip->hardware == SB_HW_16CSP) {
  135. snd_sb_csp_t *csp = chip->csp;
  136. if (csp->qpos_changed) {
  137. spin_lock(&chip->reg_lock);
  138. csp->ops.csp_qsound_transfer (csp);
  139. spin_unlock(&chip->reg_lock);
  140. }
  141. }
  142. }
  143. static void snd_sb16_csp_playback_open(sb_t *chip, snd_pcm_runtime_t *runtime)
  144. {
  145. /* CSP decoders (QSound excluded) support only 16bit transfers */
  146. if (chip->hardware == SB_HW_16CSP) {
  147. snd_sb_csp_t *csp = chip->csp;
  148. if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
  149. /* manually loaded codec */
  150. if (csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) {
  151. runtime->hw.formats |= csp->acc_format;
  152. }
  153. } else {
  154. /* autoloaded codecs */
  155. runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  156. SNDRV_PCM_FMTBIT_IMA_ADPCM;
  157. }
  158. }
  159. }
  160. static void snd_sb16_csp_playback_close(sb_t *chip)
  161. {
  162. if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_WRITE)) {
  163. snd_sb_csp_t *csp = chip->csp;
  164. if (csp->ops.csp_stop(csp) == 0) {
  165. csp->ops.csp_unuse(csp);
  166. chip->open = 0;
  167. }
  168. }
  169. }
  170. static void snd_sb16_csp_capture_open(sb_t *chip, snd_pcm_runtime_t *runtime)
  171. {
  172. /* CSP coders support only 16bit transfers */
  173. if (chip->hardware == SB_HW_16CSP) {
  174. snd_sb_csp_t *csp = chip->csp;
  175. if (csp->running & SNDRV_SB_CSP_ST_LOADED) {
  176. /* manually loaded codec */
  177. if (csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) {
  178. runtime->hw.formats |= csp->acc_format;
  179. }
  180. } else {
  181. /* autoloaded codecs */
  182. runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  183. SNDRV_PCM_FMTBIT_IMA_ADPCM;
  184. }
  185. }
  186. }
  187. static void snd_sb16_csp_capture_close(sb_t *chip)
  188. {
  189. if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_READ)) {
  190. snd_sb_csp_t *csp = chip->csp;
  191. if (csp->ops.csp_stop(csp) == 0) {
  192. csp->ops.csp_unuse(csp);
  193. chip->open = 0;
  194. }
  195. }
  196. }
  197. #else
  198. #define snd_sb16_csp_playback_prepare(chip, runtime) /*nop*/
  199. #define snd_sb16_csp_capture_prepare(chip, runtime) /*nop*/
  200. #define snd_sb16_csp_update(chip) /*nop*/
  201. #define snd_sb16_csp_playback_open(chip, runtime) /*nop*/
  202. #define snd_sb16_csp_playback_close(chip) /*nop*/
  203. #define snd_sb16_csp_capture_open(chip, runtime) /*nop*/
  204. #define snd_sb16_csp_capture_close(chip) /*nop*/
  205. #endif
  206. static void snd_sb16_setup_rate(sb_t *chip,
  207. unsigned short rate,
  208. int channel)
  209. {
  210. unsigned long flags;
  211. spin_lock_irqsave(&chip->reg_lock, flags);
  212. if (chip->mode & (channel == SNDRV_PCM_STREAM_PLAYBACK ? SB_MODE_PLAYBACK_16 : SB_MODE_CAPTURE_16))
  213. snd_sb_ack_16bit(chip);
  214. else
  215. snd_sb_ack_8bit(chip);
  216. if (!(chip->mode & SB_RATE_LOCK)) {
  217. chip->locked_rate = rate;
  218. snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_IN);
  219. snd_sbdsp_command(chip, rate >> 8);
  220. snd_sbdsp_command(chip, rate & 0xff);
  221. snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
  222. snd_sbdsp_command(chip, rate >> 8);
  223. snd_sbdsp_command(chip, rate & 0xff);
  224. }
  225. spin_unlock_irqrestore(&chip->reg_lock, flags);
  226. }
  227. static int snd_sb16_hw_params(snd_pcm_substream_t * substream,
  228. snd_pcm_hw_params_t * hw_params)
  229. {
  230. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  231. }
  232. static int snd_sb16_hw_free(snd_pcm_substream_t * substream)
  233. {
  234. snd_pcm_lib_free_pages(substream);
  235. return 0;
  236. }
  237. static int snd_sb16_playback_prepare(snd_pcm_substream_t * substream)
  238. {
  239. unsigned long flags;
  240. sb_t *chip = snd_pcm_substream_chip(substream);
  241. snd_pcm_runtime_t *runtime = substream->runtime;
  242. unsigned char format;
  243. unsigned int size, count, dma;
  244. snd_sb16_csp_playback_prepare(chip, runtime);
  245. if (snd_pcm_format_unsigned(runtime->format) > 0) {
  246. format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO;
  247. } else {
  248. format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
  249. }
  250. snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_PLAYBACK);
  251. size = chip->p_dma_size = snd_pcm_lib_buffer_bytes(substream);
  252. dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16;
  253. snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  254. count = snd_pcm_lib_period_bytes(substream);
  255. spin_lock_irqsave(&chip->reg_lock, flags);
  256. if (chip->mode & SB_MODE_PLAYBACK_16) {
  257. count >>= 1;
  258. count--;
  259. snd_sbdsp_command(chip, SB_DSP4_OUT16_AI);
  260. snd_sbdsp_command(chip, format);
  261. snd_sbdsp_command(chip, count & 0xff);
  262. snd_sbdsp_command(chip, count >> 8);
  263. snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
  264. } else {
  265. count--;
  266. snd_sbdsp_command(chip, SB_DSP4_OUT8_AI);
  267. snd_sbdsp_command(chip, format);
  268. snd_sbdsp_command(chip, count & 0xff);
  269. snd_sbdsp_command(chip, count >> 8);
  270. snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
  271. }
  272. spin_unlock_irqrestore(&chip->reg_lock, flags);
  273. return 0;
  274. }
  275. static int snd_sb16_playback_trigger(snd_pcm_substream_t * substream,
  276. int cmd)
  277. {
  278. sb_t *chip = snd_pcm_substream_chip(substream);
  279. int result = 0;
  280. spin_lock(&chip->reg_lock);
  281. switch (cmd) {
  282. case SNDRV_PCM_TRIGGER_START:
  283. chip->mode |= SB_RATE_LOCK_PLAYBACK;
  284. snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
  285. break;
  286. case SNDRV_PCM_TRIGGER_STOP:
  287. snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF);
  288. /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */
  289. if (chip->mode & SB_RATE_LOCK_CAPTURE)
  290. snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
  291. chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
  292. break;
  293. default:
  294. result = -EINVAL;
  295. }
  296. spin_unlock(&chip->reg_lock);
  297. return result;
  298. }
  299. static int snd_sb16_capture_prepare(snd_pcm_substream_t * substream)
  300. {
  301. unsigned long flags;
  302. sb_t *chip = snd_pcm_substream_chip(substream);
  303. snd_pcm_runtime_t *runtime = substream->runtime;
  304. unsigned char format;
  305. unsigned int size, count, dma;
  306. snd_sb16_csp_capture_prepare(chip, runtime);
  307. if (snd_pcm_format_unsigned(runtime->format) > 0) {
  308. format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO;
  309. } else {
  310. format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO;
  311. }
  312. snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_CAPTURE);
  313. size = chip->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
  314. dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16;
  315. snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  316. count = snd_pcm_lib_period_bytes(substream);
  317. spin_lock_irqsave(&chip->reg_lock, flags);
  318. if (chip->mode & SB_MODE_CAPTURE_16) {
  319. count >>= 1;
  320. count--;
  321. snd_sbdsp_command(chip, SB_DSP4_IN16_AI);
  322. snd_sbdsp_command(chip, format);
  323. snd_sbdsp_command(chip, count & 0xff);
  324. snd_sbdsp_command(chip, count >> 8);
  325. snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
  326. } else {
  327. count--;
  328. snd_sbdsp_command(chip, SB_DSP4_IN8_AI);
  329. snd_sbdsp_command(chip, format);
  330. snd_sbdsp_command(chip, count & 0xff);
  331. snd_sbdsp_command(chip, count >> 8);
  332. snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
  333. }
  334. spin_unlock_irqrestore(&chip->reg_lock, flags);
  335. return 0;
  336. }
  337. static int snd_sb16_capture_trigger(snd_pcm_substream_t * substream,
  338. int cmd)
  339. {
  340. sb_t *chip = snd_pcm_substream_chip(substream);
  341. int result = 0;
  342. spin_lock(&chip->reg_lock);
  343. switch (cmd) {
  344. case SNDRV_PCM_TRIGGER_START:
  345. chip->mode |= SB_RATE_LOCK_CAPTURE;
  346. snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
  347. break;
  348. case SNDRV_PCM_TRIGGER_STOP:
  349. snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF);
  350. /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */
  351. if (chip->mode & SB_RATE_LOCK_PLAYBACK)
  352. snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON);
  353. chip->mode &= ~SB_RATE_LOCK_CAPTURE;
  354. break;
  355. default:
  356. result = -EINVAL;
  357. }
  358. spin_unlock(&chip->reg_lock);
  359. return result;
  360. }
  361. irqreturn_t snd_sb16dsp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  362. {
  363. sb_t *chip = dev_id;
  364. unsigned char status;
  365. int ok;
  366. spin_lock(&chip->mixer_lock);
  367. status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
  368. spin_unlock(&chip->mixer_lock);
  369. if ((status & SB_IRQTYPE_MPUIN) && chip->rmidi_callback)
  370. chip->rmidi_callback(irq, chip->rmidi->private_data, regs);
  371. if (status & SB_IRQTYPE_8BIT) {
  372. ok = 0;
  373. if (chip->mode & SB_MODE_PLAYBACK_8) {
  374. snd_pcm_period_elapsed(chip->playback_substream);
  375. snd_sb16_csp_update(chip);
  376. ok++;
  377. }
  378. if (chip->mode & SB_MODE_CAPTURE_8) {
  379. snd_pcm_period_elapsed(chip->capture_substream);
  380. ok++;
  381. }
  382. spin_lock(&chip->reg_lock);
  383. if (!ok)
  384. snd_sbdsp_command(chip, SB_DSP_DMA8_OFF);
  385. snd_sb_ack_8bit(chip);
  386. spin_unlock(&chip->reg_lock);
  387. }
  388. if (status & SB_IRQTYPE_16BIT) {
  389. ok = 0;
  390. if (chip->mode & SB_MODE_PLAYBACK_16) {
  391. snd_pcm_period_elapsed(chip->playback_substream);
  392. snd_sb16_csp_update(chip);
  393. ok++;
  394. }
  395. if (chip->mode & SB_MODE_CAPTURE_16) {
  396. snd_pcm_period_elapsed(chip->capture_substream);
  397. ok++;
  398. }
  399. spin_lock(&chip->reg_lock);
  400. if (!ok)
  401. snd_sbdsp_command(chip, SB_DSP_DMA16_OFF);
  402. snd_sb_ack_16bit(chip);
  403. spin_unlock(&chip->reg_lock);
  404. }
  405. return IRQ_HANDLED;
  406. }
  407. /*
  408. */
  409. static snd_pcm_uframes_t snd_sb16_playback_pointer(snd_pcm_substream_t * substream)
  410. {
  411. sb_t *chip = snd_pcm_substream_chip(substream);
  412. unsigned int dma;
  413. size_t ptr;
  414. dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16;
  415. ptr = snd_dma_pointer(dma, chip->p_dma_size);
  416. return bytes_to_frames(substream->runtime, ptr);
  417. }
  418. static snd_pcm_uframes_t snd_sb16_capture_pointer(snd_pcm_substream_t * substream)
  419. {
  420. sb_t *chip = snd_pcm_substream_chip(substream);
  421. unsigned int dma;
  422. size_t ptr;
  423. dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16;
  424. ptr = snd_dma_pointer(dma, chip->c_dma_size);
  425. return bytes_to_frames(substream->runtime, ptr);
  426. }
  427. /*
  428. */
  429. static snd_pcm_hardware_t snd_sb16_playback =
  430. {
  431. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  432. SNDRV_PCM_INFO_MMAP_VALID),
  433. .formats = 0,
  434. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
  435. .rate_min = 4000,
  436. .rate_max = 44100,
  437. .channels_min = 1,
  438. .channels_max = 2,
  439. .buffer_bytes_max = (128*1024),
  440. .period_bytes_min = 64,
  441. .period_bytes_max = (128*1024),
  442. .periods_min = 1,
  443. .periods_max = 1024,
  444. .fifo_size = 0,
  445. };
  446. static snd_pcm_hardware_t snd_sb16_capture =
  447. {
  448. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  449. SNDRV_PCM_INFO_MMAP_VALID),
  450. .formats = 0,
  451. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
  452. .rate_min = 4000,
  453. .rate_max = 44100,
  454. .channels_min = 1,
  455. .channels_max = 2,
  456. .buffer_bytes_max = (128*1024),
  457. .period_bytes_min = 64,
  458. .period_bytes_max = (128*1024),
  459. .periods_min = 1,
  460. .periods_max = 1024,
  461. .fifo_size = 0,
  462. };
  463. /*
  464. * open/close
  465. */
  466. static int snd_sb16_playback_open(snd_pcm_substream_t * substream)
  467. {
  468. unsigned long flags;
  469. sb_t *chip = snd_pcm_substream_chip(substream);
  470. snd_pcm_runtime_t *runtime = substream->runtime;
  471. spin_lock_irqsave(&chip->open_lock, flags);
  472. if (chip->mode & SB_MODE_PLAYBACK) {
  473. spin_unlock_irqrestore(&chip->open_lock, flags);
  474. return -EAGAIN;
  475. }
  476. runtime->hw = snd_sb16_playback;
  477. /* skip if 16 bit DMA was reserved for capture */
  478. if (chip->force_mode16 & SB_MODE_CAPTURE_16)
  479. goto __skip_16bit;
  480. if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_CAPTURE_16)) {
  481. chip->mode |= SB_MODE_PLAYBACK_16;
  482. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
  483. /* Vibra16X hack */
  484. if (chip->dma16 <= 3) {
  485. runtime->hw.buffer_bytes_max =
  486. runtime->hw.period_bytes_max = 64 * 1024;
  487. } else {
  488. snd_sb16_csp_playback_open(chip, runtime);
  489. }
  490. goto __open_ok;
  491. }
  492. __skip_16bit:
  493. if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_CAPTURE_8)) {
  494. chip->mode |= SB_MODE_PLAYBACK_8;
  495. /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */
  496. if (chip->dma16 < 0) {
  497. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
  498. chip->mode |= SB_MODE_PLAYBACK_16;
  499. } else {
  500. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
  501. }
  502. runtime->hw.buffer_bytes_max =
  503. runtime->hw.period_bytes_max = 64 * 1024;
  504. goto __open_ok;
  505. }
  506. spin_unlock_irqrestore(&chip->open_lock, flags);
  507. return -EAGAIN;
  508. __open_ok:
  509. if (chip->hardware == SB_HW_ALS100)
  510. runtime->hw.rate_max = 48000;
  511. if (chip->mode & SB_RATE_LOCK)
  512. runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate;
  513. chip->playback_substream = substream;
  514. spin_unlock_irqrestore(&chip->open_lock, flags);
  515. return 0;
  516. }
  517. static int snd_sb16_playback_close(snd_pcm_substream_t * substream)
  518. {
  519. unsigned long flags;
  520. sb_t *chip = snd_pcm_substream_chip(substream);
  521. snd_sb16_csp_playback_close(chip);
  522. spin_lock_irqsave(&chip->open_lock, flags);
  523. chip->playback_substream = NULL;
  524. chip->mode &= ~SB_MODE_PLAYBACK;
  525. spin_unlock_irqrestore(&chip->open_lock, flags);
  526. return 0;
  527. }
  528. static int snd_sb16_capture_open(snd_pcm_substream_t * substream)
  529. {
  530. unsigned long flags;
  531. sb_t *chip = snd_pcm_substream_chip(substream);
  532. snd_pcm_runtime_t *runtime = substream->runtime;
  533. spin_lock_irqsave(&chip->open_lock, flags);
  534. if (chip->mode & SB_MODE_CAPTURE) {
  535. spin_unlock_irqrestore(&chip->open_lock, flags);
  536. return -EAGAIN;
  537. }
  538. runtime->hw = snd_sb16_capture;
  539. /* skip if 16 bit DMA was reserved for playback */
  540. if (chip->force_mode16 & SB_MODE_PLAYBACK_16)
  541. goto __skip_16bit;
  542. if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_16)) {
  543. chip->mode |= SB_MODE_CAPTURE_16;
  544. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
  545. /* Vibra16X hack */
  546. if (chip->dma16 <= 3) {
  547. runtime->hw.buffer_bytes_max =
  548. runtime->hw.period_bytes_max = 64 * 1024;
  549. } else {
  550. snd_sb16_csp_capture_open(chip, runtime);
  551. }
  552. goto __open_ok;
  553. }
  554. __skip_16bit:
  555. if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_8)) {
  556. chip->mode |= SB_MODE_CAPTURE_8;
  557. /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */
  558. if (chip->dma16 < 0) {
  559. runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE;
  560. chip->mode |= SB_MODE_CAPTURE_16;
  561. } else {
  562. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8;
  563. }
  564. runtime->hw.buffer_bytes_max =
  565. runtime->hw.period_bytes_max = 64 * 1024;
  566. goto __open_ok;
  567. }
  568. spin_unlock_irqrestore(&chip->open_lock, flags);
  569. return -EAGAIN;
  570. __open_ok:
  571. if (chip->hardware == SB_HW_ALS100)
  572. runtime->hw.rate_max = 48000;
  573. if (chip->mode & SB_RATE_LOCK)
  574. runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate;
  575. chip->capture_substream = substream;
  576. spin_unlock_irqrestore(&chip->open_lock, flags);
  577. return 0;
  578. }
  579. static int snd_sb16_capture_close(snd_pcm_substream_t * substream)
  580. {
  581. unsigned long flags;
  582. sb_t *chip = snd_pcm_substream_chip(substream);
  583. snd_sb16_csp_capture_close(chip);
  584. spin_lock_irqsave(&chip->open_lock, flags);
  585. chip->capture_substream = NULL;
  586. chip->mode &= ~SB_MODE_CAPTURE;
  587. spin_unlock_irqrestore(&chip->open_lock, flags);
  588. return 0;
  589. }
  590. /*
  591. * DMA control interface
  592. */
  593. static int snd_sb16_set_dma_mode(sb_t *chip, int what)
  594. {
  595. if (chip->dma8 < 0 || chip->dma16 < 0) {
  596. snd_assert(what == 0, return -EINVAL);
  597. return 0;
  598. }
  599. if (what == 0) {
  600. chip->force_mode16 = 0;
  601. } else if (what == 1) {
  602. chip->force_mode16 = SB_MODE_PLAYBACK_16;
  603. } else if (what == 2) {
  604. chip->force_mode16 = SB_MODE_CAPTURE_16;
  605. } else {
  606. return -EINVAL;
  607. }
  608. return 0;
  609. }
  610. static int snd_sb16_get_dma_mode(sb_t *chip)
  611. {
  612. if (chip->dma8 < 0 || chip->dma16 < 0)
  613. return 0;
  614. switch (chip->force_mode16) {
  615. case SB_MODE_PLAYBACK_16:
  616. return 1;
  617. case SB_MODE_CAPTURE_16:
  618. return 2;
  619. default:
  620. return 0;
  621. }
  622. }
  623. static int snd_sb16_dma_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  624. {
  625. static char *texts[3] = {
  626. "Auto", "Playback", "Capture"
  627. };
  628. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  629. uinfo->count = 1;
  630. uinfo->value.enumerated.items = 3;
  631. if (uinfo->value.enumerated.item > 2)
  632. uinfo->value.enumerated.item = 2;
  633. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  634. return 0;
  635. }
  636. static int snd_sb16_dma_control_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  637. {
  638. sb_t *chip = snd_kcontrol_chip(kcontrol);
  639. unsigned long flags;
  640. spin_lock_irqsave(&chip->reg_lock, flags);
  641. ucontrol->value.enumerated.item[0] = snd_sb16_get_dma_mode(chip);
  642. spin_unlock_irqrestore(&chip->reg_lock, flags);
  643. return 0;
  644. }
  645. static int snd_sb16_dma_control_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  646. {
  647. sb_t *chip = snd_kcontrol_chip(kcontrol);
  648. unsigned long flags;
  649. unsigned char nval, oval;
  650. int change;
  651. if ((nval = ucontrol->value.enumerated.item[0]) > 2)
  652. return -EINVAL;
  653. spin_lock_irqsave(&chip->reg_lock, flags);
  654. oval = snd_sb16_get_dma_mode(chip);
  655. change = nval != oval;
  656. snd_sb16_set_dma_mode(chip, nval);
  657. spin_unlock_irqrestore(&chip->reg_lock, flags);
  658. return change;
  659. }
  660. static snd_kcontrol_new_t snd_sb16_dma_control = {
  661. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  662. .name = "16-bit DMA Allocation",
  663. .info = snd_sb16_dma_control_info,
  664. .get = snd_sb16_dma_control_get,
  665. .put = snd_sb16_dma_control_put
  666. };
  667. /*
  668. * Initialization part
  669. */
  670. int snd_sb16dsp_configure(sb_t * chip)
  671. {
  672. unsigned long flags;
  673. unsigned char irqreg = 0, dmareg = 0, mpureg;
  674. unsigned char realirq, realdma, realmpureg;
  675. /* note: mpu register should be present only on SB16 Vibra soundcards */
  676. // printk("codec->irq=%i, codec->dma8=%i, codec->dma16=%i\n", chip->irq, chip->dma8, chip->dma16);
  677. spin_lock_irqsave(&chip->mixer_lock, flags);
  678. mpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP) & ~0x06;
  679. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  680. switch (chip->irq) {
  681. case 2:
  682. case 9:
  683. irqreg |= SB_IRQSETUP_IRQ9;
  684. break;
  685. case 5:
  686. irqreg |= SB_IRQSETUP_IRQ5;
  687. break;
  688. case 7:
  689. irqreg |= SB_IRQSETUP_IRQ7;
  690. break;
  691. case 10:
  692. irqreg |= SB_IRQSETUP_IRQ10;
  693. break;
  694. default:
  695. return -EINVAL;
  696. }
  697. if (chip->dma8 >= 0) {
  698. switch (chip->dma8) {
  699. case 0:
  700. dmareg |= SB_DMASETUP_DMA0;
  701. break;
  702. case 1:
  703. dmareg |= SB_DMASETUP_DMA1;
  704. break;
  705. case 3:
  706. dmareg |= SB_DMASETUP_DMA3;
  707. break;
  708. default:
  709. return -EINVAL;
  710. }
  711. }
  712. if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
  713. switch (chip->dma16) {
  714. case 5:
  715. dmareg |= SB_DMASETUP_DMA5;
  716. break;
  717. case 6:
  718. dmareg |= SB_DMASETUP_DMA6;
  719. break;
  720. case 7:
  721. dmareg |= SB_DMASETUP_DMA7;
  722. break;
  723. default:
  724. return -EINVAL;
  725. }
  726. }
  727. switch (chip->mpu_port) {
  728. case 0x300:
  729. mpureg |= 0x04;
  730. break;
  731. case 0x330:
  732. mpureg |= 0x00;
  733. break;
  734. default:
  735. mpureg |= 0x02; /* disable MPU */
  736. }
  737. spin_lock_irqsave(&chip->mixer_lock, flags);
  738. snd_sbmixer_write(chip, SB_DSP4_IRQSETUP, irqreg);
  739. realirq = snd_sbmixer_read(chip, SB_DSP4_IRQSETUP);
  740. snd_sbmixer_write(chip, SB_DSP4_DMASETUP, dmareg);
  741. realdma = snd_sbmixer_read(chip, SB_DSP4_DMASETUP);
  742. snd_sbmixer_write(chip, SB_DSP4_MPUSETUP, mpureg);
  743. realmpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP);
  744. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  745. if ((~realirq) & irqreg || (~realdma) & dmareg) {
  746. snd_printk("SB16 [0x%lx]: unable to set DMA & IRQ (PnP device?)\n", chip->port);
  747. snd_printk("SB16 [0x%lx]: wanted: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, realirq, realdma, realmpureg);
  748. snd_printk("SB16 [0x%lx]: got: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, irqreg, dmareg, mpureg);
  749. return -ENODEV;
  750. }
  751. return 0;
  752. }
  753. static snd_pcm_ops_t snd_sb16_playback_ops = {
  754. .open = snd_sb16_playback_open,
  755. .close = snd_sb16_playback_close,
  756. .ioctl = snd_pcm_lib_ioctl,
  757. .hw_params = snd_sb16_hw_params,
  758. .hw_free = snd_sb16_hw_free,
  759. .prepare = snd_sb16_playback_prepare,
  760. .trigger = snd_sb16_playback_trigger,
  761. .pointer = snd_sb16_playback_pointer,
  762. };
  763. static snd_pcm_ops_t snd_sb16_capture_ops = {
  764. .open = snd_sb16_capture_open,
  765. .close = snd_sb16_capture_close,
  766. .ioctl = snd_pcm_lib_ioctl,
  767. .hw_params = snd_sb16_hw_params,
  768. .hw_free = snd_sb16_hw_free,
  769. .prepare = snd_sb16_capture_prepare,
  770. .trigger = snd_sb16_capture_trigger,
  771. .pointer = snd_sb16_capture_pointer,
  772. };
  773. static void snd_sb16dsp_pcm_free(snd_pcm_t *pcm)
  774. {
  775. snd_pcm_lib_preallocate_free_for_all(pcm);
  776. }
  777. int snd_sb16dsp_pcm(sb_t * chip, int device, snd_pcm_t ** rpcm)
  778. {
  779. snd_card_t *card = chip->card;
  780. snd_pcm_t *pcm;
  781. int err;
  782. if (rpcm)
  783. *rpcm = NULL;
  784. if ((err = snd_pcm_new(card, "SB16 DSP", device, 1, 1, &pcm)) < 0)
  785. return err;
  786. sprintf(pcm->name, "DSP v%i.%i", chip->version >> 8, chip->version & 0xff);
  787. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  788. pcm->private_data = chip;
  789. pcm->private_free = snd_sb16dsp_pcm_free;
  790. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sb16_playback_ops);
  791. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sb16_capture_ops);
  792. if (chip->dma16 >= 0 && chip->dma8 != chip->dma16)
  793. snd_ctl_add(card, snd_ctl_new1(&snd_sb16_dma_control, chip));
  794. else
  795. pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  796. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  797. snd_dma_isa_data(),
  798. 64*1024, 128*1024);
  799. if (rpcm)
  800. *rpcm = pcm;
  801. return 0;
  802. }
  803. const snd_pcm_ops_t *snd_sb16dsp_get_pcm_ops(int direction)
  804. {
  805. return direction == SNDRV_PCM_STREAM_PLAYBACK ?
  806. &snd_sb16_playback_ops : &snd_sb16_capture_ops;
  807. }
  808. EXPORT_SYMBOL(snd_sb16dsp_pcm);
  809. EXPORT_SYMBOL(snd_sb16dsp_get_pcm_ops);
  810. EXPORT_SYMBOL(snd_sb16dsp_configure);
  811. EXPORT_SYMBOL(snd_sb16dsp_interrupt);
  812. /*
  813. * INIT part
  814. */
  815. static int __init alsa_sb16_init(void)
  816. {
  817. return 0;
  818. }
  819. static void __exit alsa_sb16_exit(void)
  820. {
  821. }
  822. module_init(alsa_sb16_init)
  823. module_exit(alsa_sb16_exit)