pcsp_lib.c 9.1 KB

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