pcsp_lib.c 9.6 KB

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