pcsp_lib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * PC-Speaker driver for Linux
  3. *
  4. * Copyright (C) 1993-1997 Michael Beck
  5. * Copyright (C) 1997-2001 David Woodhouse
  6. * Copyright (C) 2001-2008 Stas Sergeev
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <sound/pcm.h>
  11. #include <asm/io.h>
  12. #include "pcsp.h"
  13. static int nforce_wa;
  14. module_param(nforce_wa, bool, 0444);
  15. MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
  16. "(expect bad sound)");
  17. #define DMIX_WANTS_S16 1
  18. enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
  19. {
  20. unsigned char timer_cnt, val;
  21. int fmt_size, periods_elapsed;
  22. u64 ns;
  23. size_t period_bytes, buffer_bytes;
  24. struct snd_pcm_substream *substream;
  25. struct snd_pcm_runtime *runtime;
  26. struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
  27. if (chip->thalf) {
  28. outb(chip->val61, 0x61);
  29. chip->thalf = 0;
  30. if (!atomic_read(&chip->timer_active))
  31. return HRTIMER_NORESTART;
  32. hrtimer_forward(&chip->timer, chip->timer.expires,
  33. ktime_set(0, chip->ns_rem));
  34. return HRTIMER_RESTART;
  35. }
  36. spin_lock_irq(&chip->substream_lock);
  37. /* Takashi Iwai says regarding this extra lock:
  38. If the irq handler handles some data on the DMA buffer, it should
  39. do snd_pcm_stream_lock().
  40. That protects basically against all races among PCM callbacks, yes.
  41. However, there are two remaining issues:
  42. 1. The substream pointer you try to lock isn't protected _before_
  43. this lock yet.
  44. 2. snd_pcm_period_elapsed() itself acquires the lock.
  45. The requirement of another lock is because of 1. When you get
  46. chip->playback_substream, it's not protected.
  47. Keeping this lock while snd_pcm_period_elapsed() assures the substream
  48. is still protected (at least, not released). And the other status is
  49. handled properly inside snd_pcm_stream_lock() in
  50. snd_pcm_period_elapsed().
  51. */
  52. if (!chip->playback_substream)
  53. goto exit_nr_unlock1;
  54. substream = chip->playback_substream;
  55. snd_pcm_stream_lock(substream);
  56. if (!atomic_read(&chip->timer_active))
  57. goto exit_nr_unlock2;
  58. runtime = substream->runtime;
  59. fmt_size = snd_pcm_format_physical_width(runtime->format) >> 3;
  60. /* assume it is mono! */
  61. val = runtime->dma_area[chip->playback_ptr + fmt_size - 1];
  62. if (snd_pcm_format_signed(runtime->format))
  63. val ^= 0x80;
  64. timer_cnt = val * CUR_DIV() / 256;
  65. if (timer_cnt && chip->enable) {
  66. spin_lock(&i8253_lock);
  67. if (!nforce_wa) {
  68. outb_p(chip->val61, 0x61);
  69. outb_p(timer_cnt, 0x42);
  70. outb(chip->val61 ^ 1, 0x61);
  71. } else {
  72. outb(chip->val61 ^ 2, 0x61);
  73. chip->thalf = 1;
  74. }
  75. spin_unlock(&i8253_lock);
  76. }
  77. period_bytes = snd_pcm_lib_period_bytes(substream);
  78. buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  79. chip->playback_ptr += PCSP_INDEX_INC() * fmt_size;
  80. periods_elapsed = chip->playback_ptr - chip->period_ptr;
  81. if (periods_elapsed < 0) {
  82. #if PCSP_DEBUG
  83. printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
  84. "(%zi %zi %zi)\n",
  85. chip->playback_ptr, period_bytes, buffer_bytes);
  86. #endif
  87. periods_elapsed += buffer_bytes;
  88. }
  89. periods_elapsed /= period_bytes;
  90. /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
  91. * or ALSA will BUG on us. */
  92. chip->playback_ptr %= buffer_bytes;
  93. snd_pcm_stream_unlock(substream);
  94. if (periods_elapsed) {
  95. snd_pcm_period_elapsed(substream);
  96. chip->period_ptr += periods_elapsed * period_bytes;
  97. chip->period_ptr %= buffer_bytes;
  98. }
  99. spin_unlock_irq(&chip->substream_lock);
  100. if (!atomic_read(&chip->timer_active))
  101. return HRTIMER_NORESTART;
  102. chip->ns_rem = PCSP_PERIOD_NS();
  103. ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
  104. chip->ns_rem -= ns;
  105. hrtimer_forward(&chip->timer, chip->timer.expires, ktime_set(0, ns));
  106. return HRTIMER_RESTART;
  107. exit_nr_unlock2:
  108. snd_pcm_stream_unlock(substream);
  109. exit_nr_unlock1:
  110. spin_unlock_irq(&chip->substream_lock);
  111. return HRTIMER_NORESTART;
  112. }
  113. static void pcsp_start_playing(struct snd_pcsp *chip)
  114. {
  115. #if PCSP_DEBUG
  116. printk(KERN_INFO "PCSP: start_playing called\n");
  117. #endif
  118. if (atomic_read(&chip->timer_active)) {
  119. printk(KERN_ERR "PCSP: Timer already active\n");
  120. return;
  121. }
  122. spin_lock(&i8253_lock);
  123. chip->val61 = inb(0x61) | 0x03;
  124. outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
  125. spin_unlock(&i8253_lock);
  126. atomic_set(&chip->timer_active, 1);
  127. chip->thalf = 0;
  128. hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
  129. }
  130. static void pcsp_stop_playing(struct snd_pcsp *chip)
  131. {
  132. #if PCSP_DEBUG
  133. printk(KERN_INFO "PCSP: stop_playing called\n");
  134. #endif
  135. if (!atomic_read(&chip->timer_active))
  136. return;
  137. atomic_set(&chip->timer_active, 0);
  138. spin_lock(&i8253_lock);
  139. /* restore the timer */
  140. outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
  141. outb(chip->val61 & 0xFC, 0x61);
  142. spin_unlock(&i8253_lock);
  143. }
  144. static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
  145. {
  146. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  147. #if PCSP_DEBUG
  148. printk(KERN_INFO "PCSP: close called\n");
  149. #endif
  150. if (atomic_read(&chip->timer_active)) {
  151. printk(KERN_ERR "PCSP: timer still active\n");
  152. pcsp_stop_playing(chip);
  153. }
  154. spin_lock_irq(&chip->substream_lock);
  155. chip->playback_substream = NULL;
  156. spin_unlock_irq(&chip->substream_lock);
  157. return 0;
  158. }
  159. static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
  160. struct snd_pcm_hw_params *hw_params)
  161. {
  162. int err;
  163. err = snd_pcm_lib_malloc_pages(substream,
  164. params_buffer_bytes(hw_params));
  165. if (err < 0)
  166. return err;
  167. return 0;
  168. }
  169. static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
  170. {
  171. #if PCSP_DEBUG
  172. printk(KERN_INFO "PCSP: hw_free called\n");
  173. #endif
  174. return snd_pcm_lib_free_pages(substream);
  175. }
  176. static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
  177. {
  178. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  179. #if PCSP_DEBUG
  180. printk(KERN_INFO "PCSP: prepare called, "
  181. "size=%zi psize=%zi f=%zi f1=%i\n",
  182. snd_pcm_lib_buffer_bytes(substream),
  183. snd_pcm_lib_period_bytes(substream),
  184. snd_pcm_lib_buffer_bytes(substream) /
  185. snd_pcm_lib_period_bytes(substream),
  186. substream->runtime->periods);
  187. #endif
  188. chip->playback_ptr = 0;
  189. chip->period_ptr = 0;
  190. return 0;
  191. }
  192. static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
  193. {
  194. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  195. #if PCSP_DEBUG
  196. printk(KERN_INFO "PCSP: trigger called\n");
  197. #endif
  198. switch (cmd) {
  199. case SNDRV_PCM_TRIGGER_START:
  200. case SNDRV_PCM_TRIGGER_RESUME:
  201. pcsp_start_playing(chip);
  202. break;
  203. case SNDRV_PCM_TRIGGER_STOP:
  204. case SNDRV_PCM_TRIGGER_SUSPEND:
  205. pcsp_stop_playing(chip);
  206. break;
  207. default:
  208. return -EINVAL;
  209. }
  210. return 0;
  211. }
  212. static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
  213. *substream)
  214. {
  215. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  216. return bytes_to_frames(substream->runtime, chip->playback_ptr);
  217. }
  218. static struct snd_pcm_hardware snd_pcsp_playback = {
  219. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  220. SNDRV_PCM_INFO_HALF_DUPLEX |
  221. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  222. .formats = (SNDRV_PCM_FMTBIT_U8
  223. #if DMIX_WANTS_S16
  224. | SNDRV_PCM_FMTBIT_S16_LE
  225. #endif
  226. ),
  227. .rates = SNDRV_PCM_RATE_KNOT,
  228. .rate_min = PCSP_DEFAULT_SRATE,
  229. .rate_max = PCSP_DEFAULT_SRATE,
  230. .channels_min = 1,
  231. .channels_max = 1,
  232. .buffer_bytes_max = PCSP_BUFFER_SIZE,
  233. .period_bytes_min = 64,
  234. .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
  235. .periods_min = 2,
  236. .periods_max = PCSP_MAX_PERIODS,
  237. .fifo_size = 0,
  238. };
  239. static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
  240. {
  241. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  242. struct snd_pcm_runtime *runtime = substream->runtime;
  243. #if PCSP_DEBUG
  244. printk(KERN_INFO "PCSP: open called\n");
  245. #endif
  246. if (atomic_read(&chip->timer_active)) {
  247. printk(KERN_ERR "PCSP: still active!!\n");
  248. return -EBUSY;
  249. }
  250. runtime->hw = snd_pcsp_playback;
  251. spin_lock_irq(&chip->substream_lock);
  252. chip->playback_substream = substream;
  253. spin_unlock_irq(&chip->substream_lock);
  254. return 0;
  255. }
  256. static struct snd_pcm_ops snd_pcsp_playback_ops = {
  257. .open = snd_pcsp_playback_open,
  258. .close = snd_pcsp_playback_close,
  259. .ioctl = snd_pcm_lib_ioctl,
  260. .hw_params = snd_pcsp_playback_hw_params,
  261. .hw_free = snd_pcsp_playback_hw_free,
  262. .prepare = snd_pcsp_playback_prepare,
  263. .trigger = snd_pcsp_trigger,
  264. .pointer = snd_pcsp_playback_pointer,
  265. };
  266. int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
  267. {
  268. int err;
  269. err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
  270. if (err < 0)
  271. return err;
  272. snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  273. &snd_pcsp_playback_ops);
  274. chip->pcm->private_data = chip;
  275. chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  276. strcpy(chip->pcm->name, "pcsp");
  277. snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
  278. SNDRV_DMA_TYPE_CONTINUOUS,
  279. snd_dma_continuous_data
  280. (GFP_KERNEL), PCSP_BUFFER_SIZE,
  281. PCSP_BUFFER_SIZE);
  282. return 0;
  283. }