pcsp_lib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, hrtimer_get_expires(&chip->timer),
  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, hrtimer_get_expires(&chip->timer),
  106. ktime_set(0, ns));
  107. return HRTIMER_RESTART;
  108. exit_nr_unlock2:
  109. snd_pcm_stream_unlock(substream);
  110. exit_nr_unlock1:
  111. spin_unlock_irq(&chip->substream_lock);
  112. return HRTIMER_NORESTART;
  113. }
  114. static void pcsp_start_playing(struct snd_pcsp *chip)
  115. {
  116. #if PCSP_DEBUG
  117. printk(KERN_INFO "PCSP: start_playing called\n");
  118. #endif
  119. if (atomic_read(&chip->timer_active)) {
  120. printk(KERN_ERR "PCSP: Timer already active\n");
  121. return;
  122. }
  123. spin_lock(&i8253_lock);
  124. chip->val61 = inb(0x61) | 0x03;
  125. outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
  126. spin_unlock(&i8253_lock);
  127. atomic_set(&chip->timer_active, 1);
  128. chip->thalf = 0;
  129. hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
  130. }
  131. static void pcsp_stop_playing(struct snd_pcsp *chip)
  132. {
  133. #if PCSP_DEBUG
  134. printk(KERN_INFO "PCSP: stop_playing called\n");
  135. #endif
  136. if (!atomic_read(&chip->timer_active))
  137. return;
  138. atomic_set(&chip->timer_active, 0);
  139. spin_lock(&i8253_lock);
  140. /* restore the timer */
  141. outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
  142. outb(chip->val61 & 0xFC, 0x61);
  143. spin_unlock(&i8253_lock);
  144. }
  145. static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
  146. {
  147. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  148. #if PCSP_DEBUG
  149. printk(KERN_INFO "PCSP: close called\n");
  150. #endif
  151. if (atomic_read(&chip->timer_active)) {
  152. printk(KERN_ERR "PCSP: timer still active\n");
  153. pcsp_stop_playing(chip);
  154. }
  155. spin_lock_irq(&chip->substream_lock);
  156. chip->playback_substream = NULL;
  157. spin_unlock_irq(&chip->substream_lock);
  158. return 0;
  159. }
  160. static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
  161. struct snd_pcm_hw_params *hw_params)
  162. {
  163. int err;
  164. err = snd_pcm_lib_malloc_pages(substream,
  165. params_buffer_bytes(hw_params));
  166. if (err < 0)
  167. return err;
  168. return 0;
  169. }
  170. static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
  171. {
  172. #if PCSP_DEBUG
  173. printk(KERN_INFO "PCSP: hw_free called\n");
  174. #endif
  175. return snd_pcm_lib_free_pages(substream);
  176. }
  177. static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
  178. {
  179. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  180. #if PCSP_DEBUG
  181. printk(KERN_INFO "PCSP: prepare called, "
  182. "size=%zi psize=%zi f=%zi f1=%i\n",
  183. snd_pcm_lib_buffer_bytes(substream),
  184. snd_pcm_lib_period_bytes(substream),
  185. snd_pcm_lib_buffer_bytes(substream) /
  186. snd_pcm_lib_period_bytes(substream),
  187. substream->runtime->periods);
  188. #endif
  189. chip->playback_ptr = 0;
  190. chip->period_ptr = 0;
  191. return 0;
  192. }
  193. static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
  194. {
  195. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  196. #if PCSP_DEBUG
  197. printk(KERN_INFO "PCSP: trigger called\n");
  198. #endif
  199. switch (cmd) {
  200. case SNDRV_PCM_TRIGGER_START:
  201. case SNDRV_PCM_TRIGGER_RESUME:
  202. pcsp_start_playing(chip);
  203. break;
  204. case SNDRV_PCM_TRIGGER_STOP:
  205. case SNDRV_PCM_TRIGGER_SUSPEND:
  206. pcsp_stop_playing(chip);
  207. break;
  208. default:
  209. return -EINVAL;
  210. }
  211. return 0;
  212. }
  213. static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
  214. *substream)
  215. {
  216. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  217. return bytes_to_frames(substream->runtime, chip->playback_ptr);
  218. }
  219. static struct snd_pcm_hardware snd_pcsp_playback = {
  220. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  221. SNDRV_PCM_INFO_HALF_DUPLEX |
  222. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  223. .formats = (SNDRV_PCM_FMTBIT_U8
  224. #if DMIX_WANTS_S16
  225. | SNDRV_PCM_FMTBIT_S16_LE
  226. #endif
  227. ),
  228. .rates = SNDRV_PCM_RATE_KNOT,
  229. .rate_min = PCSP_DEFAULT_SRATE,
  230. .rate_max = PCSP_DEFAULT_SRATE,
  231. .channels_min = 1,
  232. .channels_max = 1,
  233. .buffer_bytes_max = PCSP_BUFFER_SIZE,
  234. .period_bytes_min = 64,
  235. .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
  236. .periods_min = 2,
  237. .periods_max = PCSP_MAX_PERIODS,
  238. .fifo_size = 0,
  239. };
  240. static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
  241. {
  242. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  243. struct snd_pcm_runtime *runtime = substream->runtime;
  244. #if PCSP_DEBUG
  245. printk(KERN_INFO "PCSP: open called\n");
  246. #endif
  247. if (atomic_read(&chip->timer_active)) {
  248. printk(KERN_ERR "PCSP: still active!!\n");
  249. return -EBUSY;
  250. }
  251. runtime->hw = snd_pcsp_playback;
  252. spin_lock_irq(&chip->substream_lock);
  253. chip->playback_substream = substream;
  254. spin_unlock_irq(&chip->substream_lock);
  255. return 0;
  256. }
  257. static struct snd_pcm_ops snd_pcsp_playback_ops = {
  258. .open = snd_pcsp_playback_open,
  259. .close = snd_pcsp_playback_close,
  260. .ioctl = snd_pcm_lib_ioctl,
  261. .hw_params = snd_pcsp_playback_hw_params,
  262. .hw_free = snd_pcsp_playback_hw_free,
  263. .prepare = snd_pcsp_playback_prepare,
  264. .trigger = snd_pcsp_trigger,
  265. .pointer = snd_pcsp_playback_pointer,
  266. };
  267. int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
  268. {
  269. int err;
  270. err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
  271. if (err < 0)
  272. return err;
  273. snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  274. &snd_pcsp_playback_ops);
  275. chip->pcm->private_data = chip;
  276. chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  277. strcpy(chip->pcm->name, "pcsp");
  278. snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
  279. SNDRV_DMA_TYPE_CONTINUOUS,
  280. snd_dma_continuous_data
  281. (GFP_KERNEL), PCSP_BUFFER_SIZE,
  282. PCSP_BUFFER_SIZE);
  283. return 0;
  284. }