pcsp_lib.c 9.0 KB

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