pcsp_lib.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/core.h>
  15. #include <sound/pcm.h>
  16. #include "pcsp.h"
  17. static bool nforce_wa;
  18. module_param(nforce_wa, bool, 0444);
  19. MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
  20. "(expect bad sound)");
  21. #define DMIX_WANTS_S16 1
  22. /*
  23. * Call snd_pcm_period_elapsed in a work
  24. * This avoids spinlock messes and long-running irq contexts
  25. */
  26. static void pcsp_call_pcm_elapsed(struct work_struct *work)
  27. {
  28. if (atomic_read(&pcsp_chip.timer_active)) {
  29. struct snd_pcm_substream *substream;
  30. substream = pcsp_chip.playback_substream;
  31. if (substream)
  32. snd_pcm_period_elapsed(substream);
  33. }
  34. }
  35. static DECLARE_WORK(pcsp_pcm_work, pcsp_call_pcm_elapsed);
  36. /* write the port and returns the next expire time in ns;
  37. * called at the trigger-start and in hrtimer callback
  38. */
  39. static u64 pcsp_timer_update(struct snd_pcsp *chip)
  40. {
  41. unsigned char timer_cnt, val;
  42. u64 ns;
  43. struct snd_pcm_substream *substream;
  44. struct snd_pcm_runtime *runtime;
  45. unsigned long flags;
  46. if (chip->thalf) {
  47. outb(chip->val61, 0x61);
  48. chip->thalf = 0;
  49. return chip->ns_rem;
  50. }
  51. substream = chip->playback_substream;
  52. if (!substream)
  53. return 0;
  54. runtime = substream->runtime;
  55. /* assume it is mono! */
  56. val = runtime->dma_area[chip->playback_ptr + chip->fmt_size - 1];
  57. if (chip->is_signed)
  58. val ^= 0x80;
  59. timer_cnt = val * CUR_DIV() / 256;
  60. if (timer_cnt && chip->enable) {
  61. raw_spin_lock_irqsave(&i8253_lock, flags);
  62. if (!nforce_wa) {
  63. outb_p(chip->val61, 0x61);
  64. outb_p(timer_cnt, 0x42);
  65. outb(chip->val61 ^ 1, 0x61);
  66. } else {
  67. outb(chip->val61 ^ 2, 0x61);
  68. chip->thalf = 1;
  69. }
  70. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  71. }
  72. chip->ns_rem = PCSP_PERIOD_NS();
  73. ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
  74. chip->ns_rem -= ns;
  75. return ns;
  76. }
  77. static void pcsp_pointer_update(struct snd_pcsp *chip)
  78. {
  79. struct snd_pcm_substream *substream;
  80. size_t period_bytes, buffer_bytes;
  81. int periods_elapsed;
  82. unsigned long flags;
  83. /* update the playback position */
  84. substream = chip->playback_substream;
  85. if (!substream)
  86. return;
  87. period_bytes = snd_pcm_lib_period_bytes(substream);
  88. buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  89. spin_lock_irqsave(&chip->substream_lock, flags);
  90. chip->playback_ptr += PCSP_INDEX_INC() * chip->fmt_size;
  91. periods_elapsed = chip->playback_ptr - chip->period_ptr;
  92. if (periods_elapsed < 0) {
  93. #if PCSP_DEBUG
  94. dev_dbg(chip->card->dev,
  95. "PCSP: buffer_bytes mod period_bytes != 0 ? (%zi %zi %zi)\n",
  96. chip->playback_ptr, period_bytes, buffer_bytes);
  97. #endif
  98. periods_elapsed += buffer_bytes;
  99. }
  100. periods_elapsed /= period_bytes;
  101. /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
  102. * or ALSA will BUG on us. */
  103. chip->playback_ptr %= buffer_bytes;
  104. if (periods_elapsed) {
  105. chip->period_ptr += periods_elapsed * period_bytes;
  106. chip->period_ptr %= buffer_bytes;
  107. queue_work(system_highpri_wq, &pcsp_pcm_work);
  108. }
  109. spin_unlock_irqrestore(&chip->substream_lock, flags);
  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. dev_warn(chip->card->dev, "PCSP: unexpected stop\n");
  122. return HRTIMER_NORESTART;
  123. }
  124. if (pointer_update)
  125. pcsp_pointer_update(chip);
  126. hrtimer_forward_now(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. dev_dbg(chip->card->dev, "PCSP: start_playing called\n");
  133. #endif
  134. if (atomic_read(&chip->timer_active)) {
  135. dev_err(chip->card->dev, "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. dev_dbg(chip->card->dev, "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. cancel_work_sync(&pcsp_pcm_work);
  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. dev_dbg(chip->card->dev, "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. pcsp_sync_stop(chip);
  187. return 0;
  188. }
  189. static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
  190. {
  191. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  192. #if PCSP_DEBUG
  193. dev_dbg(chip->card->dev, "PCSP: hw_free called\n");
  194. #endif
  195. pcsp_sync_stop(chip);
  196. return 0;
  197. }
  198. static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
  199. {
  200. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  201. pcsp_sync_stop(chip);
  202. chip->playback_ptr = 0;
  203. chip->period_ptr = 0;
  204. chip->fmt_size =
  205. snd_pcm_format_physical_width(substream->runtime->format) >> 3;
  206. chip->is_signed = snd_pcm_format_signed(substream->runtime->format);
  207. #if PCSP_DEBUG
  208. dev_dbg(chip->card->dev, "PCSP: prepare called, size=%zi psize=%zi f=%zi f1=%i fsize=%i\n",
  209. snd_pcm_lib_buffer_bytes(substream),
  210. snd_pcm_lib_period_bytes(substream),
  211. snd_pcm_lib_buffer_bytes(substream) /
  212. snd_pcm_lib_period_bytes(substream),
  213. substream->runtime->periods,
  214. chip->fmt_size);
  215. #endif
  216. return 0;
  217. }
  218. static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
  219. {
  220. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  221. #if PCSP_DEBUG
  222. dev_dbg(chip->card->dev, "PCSP: trigger called\n");
  223. #endif
  224. switch (cmd) {
  225. case SNDRV_PCM_TRIGGER_START:
  226. case SNDRV_PCM_TRIGGER_RESUME:
  227. return pcsp_start_playing(chip);
  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. unsigned int pos;
  242. spin_lock(&chip->substream_lock);
  243. pos = chip->playback_ptr;
  244. spin_unlock(&chip->substream_lock);
  245. return bytes_to_frames(substream->runtime, pos);
  246. }
  247. static const struct snd_pcm_hardware snd_pcsp_playback = {
  248. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  249. SNDRV_PCM_INFO_HALF_DUPLEX |
  250. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  251. .formats = (SNDRV_PCM_FMTBIT_U8
  252. #if DMIX_WANTS_S16
  253. | SNDRV_PCM_FMTBIT_S16_LE
  254. #endif
  255. ),
  256. .rates = SNDRV_PCM_RATE_KNOT,
  257. .rate_min = PCSP_DEFAULT_SRATE,
  258. .rate_max = PCSP_DEFAULT_SRATE,
  259. .channels_min = 1,
  260. .channels_max = 1,
  261. .buffer_bytes_max = PCSP_BUFFER_SIZE,
  262. .period_bytes_min = 64,
  263. .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
  264. .periods_min = 2,
  265. .periods_max = PCSP_MAX_PERIODS,
  266. .fifo_size = 0,
  267. };
  268. static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
  269. {
  270. struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  271. struct snd_pcm_runtime *runtime = substream->runtime;
  272. #if PCSP_DEBUG
  273. dev_dbg(chip->card->dev, "PCSP: open called\n");
  274. #endif
  275. if (atomic_read(&chip->timer_active)) {
  276. dev_err(chip->card->dev, "PCSP: still active!!\n");
  277. return -EBUSY;
  278. }
  279. runtime->hw = snd_pcsp_playback;
  280. chip->playback_substream = substream;
  281. return 0;
  282. }
  283. static const struct snd_pcm_ops snd_pcsp_playback_ops = {
  284. .open = snd_pcsp_playback_open,
  285. .close = snd_pcsp_playback_close,
  286. .hw_params = snd_pcsp_playback_hw_params,
  287. .hw_free = snd_pcsp_playback_hw_free,
  288. .prepare = snd_pcsp_playback_prepare,
  289. .trigger = snd_pcsp_trigger,
  290. .pointer = snd_pcsp_playback_pointer,
  291. };
  292. int snd_pcsp_new_pcm(struct snd_pcsp *chip)
  293. {
  294. int err;
  295. err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
  296. if (err < 0)
  297. return err;
  298. snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  299. &snd_pcsp_playback_ops);
  300. chip->pcm->private_data = chip;
  301. chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  302. strcpy(chip->pcm->name, "pcsp");
  303. snd_pcm_set_managed_buffer_all(chip->pcm,
  304. SNDRV_DMA_TYPE_CONTINUOUS,
  305. NULL,
  306. PCSP_BUFFER_SIZE,
  307. PCSP_BUFFER_SIZE);
  308. return 0;
  309. }