pcsp_lib.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <linux/interrupt.h>
  11. #include <sound/pcm.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. /*
  20. * Call snd_pcm_period_elapsed in a tasklet
  21. * This avoids spinlock messes and long-running irq contexts
  22. */
  23. static void pcsp_call_pcm_elapsed(unsigned long priv)
  24. {
  25. if (atomic_read(&pcsp_chip.timer_active)) {
  26. struct snd_pcm_substream *substream;
  27. substream = pcsp_chip.playback_substream;
  28. if (substream)
  29. snd_pcm_period_elapsed(substream);
  30. }
  31. }
  32. static DECLARE_TASKLET(pcsp_pcm_tasklet, pcsp_call_pcm_elapsed, 0);
  33. /* write the port and returns the next expire time in ns;
  34. * called at the trigger-start and in hrtimer callback
  35. */
  36. static u64 pcsp_timer_update(struct snd_pcsp *chip)
  37. {
  38. unsigned char timer_cnt, val;
  39. u64 ns;
  40. struct snd_pcm_substream *substream;
  41. struct snd_pcm_runtime *runtime;
  42. unsigned long flags;
  43. if (chip->thalf) {
  44. outb(chip->val61, 0x61);
  45. chip->thalf = 0;
  46. return chip->ns_rem;
  47. }
  48. substream = chip->playback_substream;
  49. if (!substream)
  50. return 0;
  51. runtime = substream->runtime;
  52. /* assume it is mono! */
  53. val = runtime->dma_area[chip->playback_ptr + chip->fmt_size - 1];
  54. if (chip->is_signed)
  55. val ^= 0x80;
  56. timer_cnt = val * CUR_DIV() / 256;
  57. if (timer_cnt && chip->enable) {
  58. spin_lock_irqsave(&i8253_lock, flags);
  59. if (!nforce_wa) {
  60. outb_p(chip->val61, 0x61);
  61. outb_p(timer_cnt, 0x42);
  62. outb(chip->val61 ^ 1, 0x61);
  63. } else {
  64. outb(chip->val61 ^ 2, 0x61);
  65. chip->thalf = 1;
  66. }
  67. spin_unlock_irqrestore(&i8253_lock, flags);
  68. }
  69. chip->ns_rem = PCSP_PERIOD_NS();
  70. ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
  71. chip->ns_rem -= ns;
  72. return ns;
  73. }
  74. static void pcsp_pointer_update(struct snd_pcsp *chip)
  75. {
  76. struct snd_pcm_substream *substream;
  77. size_t period_bytes, buffer_bytes;
  78. int periods_elapsed;
  79. unsigned long flags;
  80. /* update the playback position */
  81. substream = chip->playback_substream;
  82. if (!substream)
  83. return;
  84. period_bytes = snd_pcm_lib_period_bytes(substream);
  85. buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  86. spin_lock_irqsave(&chip->substream_lock, flags);
  87. chip->playback_ptr += PCSP_INDEX_INC() * chip->fmt_size;
  88. periods_elapsed = chip->playback_ptr - chip->period_ptr;
  89. if (periods_elapsed < 0) {
  90. #if PCSP_DEBUG
  91. printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
  92. "(%zi %zi %zi)\n",
  93. chip->playback_ptr, period_bytes, buffer_bytes);
  94. #endif
  95. periods_elapsed += buffer_bytes;
  96. }
  97. periods_elapsed /= period_bytes;
  98. /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
  99. * or ALSA will BUG on us. */
  100. chip->playback_ptr %= buffer_bytes;
  101. if (periods_elapsed) {
  102. chip->period_ptr += periods_elapsed * period_bytes;
  103. chip->period_ptr %= buffer_bytes;
  104. }
  105. spin_unlock_irqrestore(&chip->substream_lock, flags);
  106. if (periods_elapsed)
  107. tasklet_schedule(&pcsp_pcm_tasklet);
  108. }
  109. enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
  110. {
  111. struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
  112. int pointer_update;
  113. u64 ns;
  114. if (!atomic_read(&chip->timer_active) || !chip->playback_substream)
  115. return HRTIMER_NORESTART;
  116. pointer_update = !chip->thalf;
  117. ns = pcsp_timer_update(chip);
  118. if (!ns) {
  119. printk(KERN_WARNING "PCSP: unexpected stop\n");
  120. return HRTIMER_NORESTART;
  121. }
  122. if (pointer_update)
  123. pcsp_pointer_update(chip);
  124. hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));
  125. return HRTIMER_RESTART;
  126. }
  127. static int pcsp_start_playing(struct snd_pcsp *chip)
  128. {
  129. #if PCSP_DEBUG
  130. printk(KERN_INFO "PCSP: start_playing called\n");
  131. #endif
  132. if (atomic_read(&chip->timer_active)) {
  133. printk(KERN_ERR "PCSP: Timer already active\n");
  134. return -EIO;
  135. }
  136. spin_lock(&i8253_lock);
  137. chip->val61 = inb(0x61) | 0x03;
  138. outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
  139. spin_unlock(&i8253_lock);
  140. atomic_set(&chip->timer_active, 1);
  141. chip->thalf = 0;
  142. hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
  143. return 0;
  144. }
  145. static void pcsp_stop_playing(struct snd_pcsp *chip)
  146. {
  147. #if PCSP_DEBUG
  148. printk(KERN_INFO "PCSP: stop_playing called\n");
  149. #endif
  150. if (!atomic_read(&chip->timer_active))
  151. return;
  152. atomic_set(&chip->timer_active, 0);
  153. spin_lock(&i8253_lock);
  154. /* restore the timer */
  155. outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
  156. outb(chip->val61 & 0xFC, 0x61);
  157. spin_unlock(&i8253_lock);
  158. }
  159. /*
  160. * Force to stop and sync the stream
  161. */
  162. void pcsp_sync_stop(struct snd_pcsp *chip)
  163. {
  164. local_irq_disable();
  165. pcsp_stop_playing(chip);
  166. local_irq_enable();
  167. hrtimer_cancel(&chip->timer);
  168. tasklet_kill(&pcsp_pcm_tasklet);
  169. }
  170. static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
  171. {
  172. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  173. #if PCSP_DEBUG
  174. printk(KERN_INFO "PCSP: close called\n");
  175. #endif
  176. pcsp_sync_stop(chip);
  177. chip->playback_substream = NULL;
  178. return 0;
  179. }
  180. static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
  181. struct snd_pcm_hw_params *hw_params)
  182. {
  183. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  184. int err;
  185. pcsp_sync_stop(chip);
  186. err = snd_pcm_lib_malloc_pages(substream,
  187. params_buffer_bytes(hw_params));
  188. if (err < 0)
  189. return err;
  190. return 0;
  191. }
  192. static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
  193. {
  194. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  195. #if PCSP_DEBUG
  196. printk(KERN_INFO "PCSP: hw_free called\n");
  197. #endif
  198. pcsp_sync_stop(chip);
  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. pcsp_sync_stop(chip);
  205. chip->playback_ptr = 0;
  206. chip->period_ptr = 0;
  207. chip->fmt_size =
  208. snd_pcm_format_physical_width(substream->runtime->format) >> 3;
  209. chip->is_signed = snd_pcm_format_signed(substream->runtime->format);
  210. #if PCSP_DEBUG
  211. printk(KERN_INFO "PCSP: prepare called, "
  212. "size=%zi psize=%zi f=%zi f1=%i fsize=%i\n",
  213. snd_pcm_lib_buffer_bytes(substream),
  214. snd_pcm_lib_period_bytes(substream),
  215. snd_pcm_lib_buffer_bytes(substream) /
  216. snd_pcm_lib_period_bytes(substream),
  217. substream->runtime->periods,
  218. chip->fmt_size);
  219. #endif
  220. return 0;
  221. }
  222. static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
  223. {
  224. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  225. #if PCSP_DEBUG
  226. printk(KERN_INFO "PCSP: trigger called\n");
  227. #endif
  228. switch (cmd) {
  229. case SNDRV_PCM_TRIGGER_START:
  230. case SNDRV_PCM_TRIGGER_RESUME:
  231. return pcsp_start_playing(chip);
  232. case SNDRV_PCM_TRIGGER_STOP:
  233. case SNDRV_PCM_TRIGGER_SUSPEND:
  234. pcsp_stop_playing(chip);
  235. break;
  236. default:
  237. return -EINVAL;
  238. }
  239. return 0;
  240. }
  241. static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
  242. *substream)
  243. {
  244. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  245. unsigned int pos;
  246. spin_lock(&chip->substream_lock);
  247. pos = chip->playback_ptr;
  248. spin_unlock(&chip->substream_lock);
  249. return bytes_to_frames(substream->runtime, pos);
  250. }
  251. static struct snd_pcm_hardware snd_pcsp_playback = {
  252. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  253. SNDRV_PCM_INFO_HALF_DUPLEX |
  254. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  255. .formats = (SNDRV_PCM_FMTBIT_U8
  256. #if DMIX_WANTS_S16
  257. | SNDRV_PCM_FMTBIT_S16_LE
  258. #endif
  259. ),
  260. .rates = SNDRV_PCM_RATE_KNOT,
  261. .rate_min = PCSP_DEFAULT_SRATE,
  262. .rate_max = PCSP_DEFAULT_SRATE,
  263. .channels_min = 1,
  264. .channels_max = 1,
  265. .buffer_bytes_max = PCSP_BUFFER_SIZE,
  266. .period_bytes_min = 64,
  267. .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
  268. .periods_min = 2,
  269. .periods_max = PCSP_MAX_PERIODS,
  270. .fifo_size = 0,
  271. };
  272. static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
  273. {
  274. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  275. struct snd_pcm_runtime *runtime = substream->runtime;
  276. #if PCSP_DEBUG
  277. printk(KERN_INFO "PCSP: open called\n");
  278. #endif
  279. if (atomic_read(&chip->timer_active)) {
  280. printk(KERN_ERR "PCSP: still active!!\n");
  281. return -EBUSY;
  282. }
  283. runtime->hw = snd_pcsp_playback;
  284. chip->playback_substream = substream;
  285. return 0;
  286. }
  287. static struct snd_pcm_ops snd_pcsp_playback_ops = {
  288. .open = snd_pcsp_playback_open,
  289. .close = snd_pcsp_playback_close,
  290. .ioctl = snd_pcm_lib_ioctl,
  291. .hw_params = snd_pcsp_playback_hw_params,
  292. .hw_free = snd_pcsp_playback_hw_free,
  293. .prepare = snd_pcsp_playback_prepare,
  294. .trigger = snd_pcsp_trigger,
  295. .pointer = snd_pcsp_playback_pointer,
  296. };
  297. int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
  298. {
  299. int err;
  300. err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
  301. if (err < 0)
  302. return err;
  303. snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  304. &snd_pcsp_playback_ops);
  305. chip->pcm->private_data = chip;
  306. chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  307. strcpy(chip->pcm->name, "pcsp");
  308. snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
  309. SNDRV_DMA_TYPE_CONTINUOUS,
  310. snd_dma_continuous_data
  311. (GFP_KERNEL), PCSP_BUFFER_SIZE,
  312. PCSP_BUFFER_SIZE);
  313. return 0;
  314. }