pcm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Line 6 Linux USB driver
  4. *
  5. * Copyright (C) 2004-2010 Markus Grabner (line6@grabner-graz.at)
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/export.h>
  9. #include <sound/core.h>
  10. #include <sound/control.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include "capture.h"
  14. #include "driver.h"
  15. #include "playback.h"
  16. /* impulse response volume controls */
  17. static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
  18. struct snd_ctl_elem_info *uinfo)
  19. {
  20. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  21. uinfo->count = 1;
  22. uinfo->value.integer.min = 0;
  23. uinfo->value.integer.max = 255;
  24. return 0;
  25. }
  26. static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
  27. struct snd_ctl_elem_value *ucontrol)
  28. {
  29. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  30. ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
  31. return 0;
  32. }
  33. static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
  34. struct snd_ctl_elem_value *ucontrol)
  35. {
  36. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  37. int value = ucontrol->value.integer.value[0];
  38. int err;
  39. if (line6pcm->impulse_volume == value)
  40. return 0;
  41. line6pcm->impulse_volume = value;
  42. if (value > 0) {
  43. err = line6_pcm_acquire(line6pcm, LINE6_STREAM_IMPULSE, true);
  44. if (err < 0) {
  45. line6pcm->impulse_volume = 0;
  46. return err;
  47. }
  48. } else {
  49. line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE);
  50. }
  51. return 1;
  52. }
  53. /* impulse response period controls */
  54. static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
  55. struct snd_ctl_elem_info *uinfo)
  56. {
  57. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  58. uinfo->count = 1;
  59. uinfo->value.integer.min = 0;
  60. uinfo->value.integer.max = 2000;
  61. return 0;
  62. }
  63. static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
  64. struct snd_ctl_elem_value *ucontrol)
  65. {
  66. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  67. ucontrol->value.integer.value[0] = line6pcm->impulse_period;
  68. return 0;
  69. }
  70. static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
  71. struct snd_ctl_elem_value *ucontrol)
  72. {
  73. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  74. int value = ucontrol->value.integer.value[0];
  75. if (line6pcm->impulse_period == value)
  76. return 0;
  77. line6pcm->impulse_period = value;
  78. return 1;
  79. }
  80. /*
  81. Unlink all currently active URBs.
  82. */
  83. static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
  84. struct line6_pcm_stream *pcms)
  85. {
  86. int i;
  87. for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
  88. if (test_bit(i, &pcms->active_urbs)) {
  89. if (!test_and_set_bit(i, &pcms->unlink_urbs))
  90. usb_unlink_urb(pcms->urbs[i]);
  91. }
  92. }
  93. }
  94. /*
  95. Wait until unlinking of all currently active URBs has been finished.
  96. */
  97. static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
  98. struct line6_pcm_stream *pcms)
  99. {
  100. int timeout = HZ;
  101. int i;
  102. int alive;
  103. do {
  104. alive = 0;
  105. for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
  106. if (test_bit(i, &pcms->active_urbs))
  107. alive++;
  108. }
  109. if (!alive)
  110. break;
  111. set_current_state(TASK_UNINTERRUPTIBLE);
  112. schedule_timeout(1);
  113. } while (--timeout > 0);
  114. if (alive)
  115. dev_err(line6pcm->line6->ifcdev,
  116. "timeout: still %d active urbs..\n", alive);
  117. }
  118. static inline struct line6_pcm_stream *
  119. get_stream(struct snd_line6_pcm *line6pcm, int direction)
  120. {
  121. return (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
  122. &line6pcm->out : &line6pcm->in;
  123. }
  124. /* allocate a buffer if not opened yet;
  125. * call this in line6pcm.state_mutex
  126. */
  127. static int line6_buffer_acquire(struct snd_line6_pcm *line6pcm,
  128. struct line6_pcm_stream *pstr, int direction, int type)
  129. {
  130. const int pkt_size =
  131. (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
  132. line6pcm->max_packet_size_out :
  133. line6pcm->max_packet_size_in;
  134. /* Invoked multiple times in a row so allocate once only */
  135. if (!test_and_set_bit(type, &pstr->opened) && !pstr->buffer) {
  136. pstr->buffer =
  137. kmalloc(array3_size(line6pcm->line6->iso_buffers,
  138. LINE6_ISO_PACKETS, pkt_size),
  139. GFP_KERNEL);
  140. if (!pstr->buffer)
  141. return -ENOMEM;
  142. }
  143. return 0;
  144. }
  145. /* free a buffer if all streams are closed;
  146. * call this in line6pcm.state_mutex
  147. */
  148. static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
  149. struct line6_pcm_stream *pstr, int type)
  150. {
  151. clear_bit(type, &pstr->opened);
  152. if (!pstr->opened) {
  153. line6_wait_clear_audio_urbs(line6pcm, pstr);
  154. kfree(pstr->buffer);
  155. pstr->buffer = NULL;
  156. }
  157. }
  158. /* start a PCM stream */
  159. static int line6_stream_start(struct snd_line6_pcm *line6pcm, int direction,
  160. int type)
  161. {
  162. struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
  163. int ret = 0;
  164. guard(spinlock_irqsave)(&pstr->lock);
  165. if (!test_and_set_bit(type, &pstr->running) &&
  166. !(pstr->active_urbs || pstr->unlink_urbs)) {
  167. pstr->count = 0;
  168. /* Submit all currently available URBs */
  169. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  170. ret = line6_submit_audio_out_all_urbs(line6pcm);
  171. else
  172. ret = line6_submit_audio_in_all_urbs(line6pcm);
  173. }
  174. if (ret < 0)
  175. clear_bit(type, &pstr->running);
  176. return ret;
  177. }
  178. /* stop a PCM stream; this doesn't sync with the unlinked URBs */
  179. static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction,
  180. int type)
  181. {
  182. struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
  183. scoped_guard(spinlock_irqsave, &pstr->lock) {
  184. clear_bit(type, &pstr->running);
  185. if (pstr->running)
  186. return;
  187. }
  188. line6_unlink_audio_urbs(line6pcm, pstr);
  189. if (direction == SNDRV_PCM_STREAM_CAPTURE) {
  190. guard(spinlock_irqsave)(&pstr->lock);
  191. line6pcm->prev_fbuf = NULL;
  192. line6pcm->prev_fsize = 0;
  193. }
  194. }
  195. /* common PCM trigger callback */
  196. int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
  197. {
  198. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  199. struct snd_pcm_substream *s;
  200. int err;
  201. clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags);
  202. snd_pcm_group_for_each_entry(s, substream) {
  203. if (s->pcm->card != substream->pcm->card)
  204. continue;
  205. switch (cmd) {
  206. case SNDRV_PCM_TRIGGER_START:
  207. case SNDRV_PCM_TRIGGER_RESUME:
  208. if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
  209. (line6pcm->line6->properties->capabilities &
  210. LINE6_CAP_IN_NEEDS_OUT)) {
  211. err = line6_stream_start(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
  212. LINE6_STREAM_CAPTURE_HELPER);
  213. if (err < 0)
  214. return err;
  215. }
  216. err = line6_stream_start(line6pcm, s->stream,
  217. LINE6_STREAM_PCM);
  218. if (err < 0)
  219. return err;
  220. break;
  221. case SNDRV_PCM_TRIGGER_STOP:
  222. case SNDRV_PCM_TRIGGER_SUSPEND:
  223. if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
  224. (line6pcm->line6->properties->capabilities &
  225. LINE6_CAP_IN_NEEDS_OUT)) {
  226. line6_stream_stop(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
  227. LINE6_STREAM_CAPTURE_HELPER);
  228. }
  229. line6_stream_stop(line6pcm, s->stream,
  230. LINE6_STREAM_PCM);
  231. break;
  232. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  233. if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  234. return -EINVAL;
  235. set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
  236. break;
  237. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  238. if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  239. return -EINVAL;
  240. clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
  241. break;
  242. default:
  243. return -EINVAL;
  244. }
  245. }
  246. return 0;
  247. }
  248. /* common PCM pointer callback */
  249. snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream)
  250. {
  251. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  252. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  253. return pstr->pos_done;
  254. }
  255. /* Stop and release duplex streams */
  256. static void __line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
  257. {
  258. struct line6_pcm_stream *pstr;
  259. int dir;
  260. for (dir = 0; dir < 2; dir++)
  261. line6_stream_stop(line6pcm, dir, type);
  262. for (dir = 0; dir < 2; dir++) {
  263. pstr = get_stream(line6pcm, dir);
  264. line6_buffer_release(line6pcm, pstr, type);
  265. }
  266. }
  267. /* Stop and release duplex streams */
  268. void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
  269. {
  270. guard(mutex)(&line6pcm->state_mutex);
  271. __line6_pcm_release(line6pcm, type);
  272. }
  273. EXPORT_SYMBOL_GPL(line6_pcm_release);
  274. /* Acquire and optionally start duplex streams:
  275. * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
  276. */
  277. int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
  278. {
  279. struct line6_pcm_stream *pstr;
  280. int ret = 0, dir;
  281. /* TODO: We should assert SNDRV_PCM_STREAM_PLAYBACK/CAPTURE == 0/1 */
  282. guard(mutex)(&line6pcm->state_mutex);
  283. for (dir = 0; dir < 2; dir++) {
  284. pstr = get_stream(line6pcm, dir);
  285. ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
  286. if (ret < 0)
  287. goto error;
  288. if (!pstr->running)
  289. line6_wait_clear_audio_urbs(line6pcm, pstr);
  290. }
  291. if (start) {
  292. for (dir = 0; dir < 2; dir++) {
  293. ret = line6_stream_start(line6pcm, dir, type);
  294. if (ret < 0)
  295. goto error;
  296. }
  297. }
  298. error:
  299. if (ret < 0)
  300. __line6_pcm_release(line6pcm, type);
  301. return ret;
  302. }
  303. EXPORT_SYMBOL_GPL(line6_pcm_acquire);
  304. /* common PCM hw_params callback */
  305. int snd_line6_hw_params(struct snd_pcm_substream *substream,
  306. struct snd_pcm_hw_params *hw_params)
  307. {
  308. int ret;
  309. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  310. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  311. guard(mutex)(&line6pcm->state_mutex);
  312. ret = line6_buffer_acquire(line6pcm, pstr, substream->stream,
  313. LINE6_STREAM_PCM);
  314. if (ret < 0)
  315. return ret;
  316. pstr->period = params_period_bytes(hw_params);
  317. return 0;
  318. }
  319. /* common PCM hw_free callback */
  320. int snd_line6_hw_free(struct snd_pcm_substream *substream)
  321. {
  322. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  323. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  324. guard(mutex)(&line6pcm->state_mutex);
  325. line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
  326. return 0;
  327. }
  328. /* control info callback */
  329. static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
  330. struct snd_ctl_elem_info *uinfo)
  331. {
  332. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  333. uinfo->count = 2;
  334. uinfo->value.integer.min = 0;
  335. uinfo->value.integer.max = 256;
  336. return 0;
  337. }
  338. /* control get callback */
  339. static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
  340. struct snd_ctl_elem_value *ucontrol)
  341. {
  342. int i;
  343. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  344. for (i = 0; i < 2; i++)
  345. ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
  346. return 0;
  347. }
  348. /* control put callback */
  349. static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
  350. struct snd_ctl_elem_value *ucontrol)
  351. {
  352. int i, changed = 0;
  353. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  354. for (i = 0; i < 2; i++)
  355. if (line6pcm->volume_playback[i] !=
  356. ucontrol->value.integer.value[i]) {
  357. line6pcm->volume_playback[i] =
  358. ucontrol->value.integer.value[i];
  359. changed = 1;
  360. }
  361. return changed;
  362. }
  363. /* control definition */
  364. static const struct snd_kcontrol_new line6_controls[] = {
  365. {
  366. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  367. .name = "PCM Playback Volume",
  368. .info = snd_line6_control_playback_info,
  369. .get = snd_line6_control_playback_get,
  370. .put = snd_line6_control_playback_put
  371. },
  372. {
  373. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  374. .name = "Impulse Response Volume",
  375. .info = snd_line6_impulse_volume_info,
  376. .get = snd_line6_impulse_volume_get,
  377. .put = snd_line6_impulse_volume_put
  378. },
  379. {
  380. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  381. .name = "Impulse Response Period",
  382. .info = snd_line6_impulse_period_info,
  383. .get = snd_line6_impulse_period_get,
  384. .put = snd_line6_impulse_period_put
  385. },
  386. };
  387. /*
  388. Cleanup the PCM device.
  389. */
  390. static void cleanup_urbs(struct line6_pcm_stream *pcms, int iso_buffers)
  391. {
  392. int i;
  393. /* Most likely impossible in current code... */
  394. if (pcms->urbs == NULL)
  395. return;
  396. for (i = 0; i < iso_buffers; i++) {
  397. if (pcms->urbs[i]) {
  398. usb_kill_urb(pcms->urbs[i]);
  399. usb_free_urb(pcms->urbs[i]);
  400. }
  401. }
  402. kfree(pcms->urbs);
  403. pcms->urbs = NULL;
  404. }
  405. static void line6_cleanup_pcm(struct snd_pcm *pcm)
  406. {
  407. struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
  408. cleanup_urbs(&line6pcm->out, line6pcm->line6->iso_buffers);
  409. cleanup_urbs(&line6pcm->in, line6pcm->line6->iso_buffers);
  410. kfree(line6pcm);
  411. }
  412. /* create a PCM device */
  413. static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
  414. {
  415. struct snd_pcm *pcm;
  416. int err;
  417. err = snd_pcm_new(line6->card, (char *)line6->properties->name,
  418. 0, 1, 1, pcm_ret);
  419. if (err < 0)
  420. return err;
  421. pcm = *pcm_ret;
  422. strscpy(pcm->name, line6->properties->name);
  423. /* set operators */
  424. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  425. &snd_line6_playback_ops);
  426. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
  427. /* pre-allocation of buffers */
  428. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  429. NULL, 64 * 1024, 128 * 1024);
  430. return 0;
  431. }
  432. /*
  433. Sync with PCM stream stops.
  434. */
  435. void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
  436. {
  437. line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
  438. line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
  439. line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
  440. line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
  441. }
  442. /*
  443. Create and register the PCM device and mixer entries.
  444. Create URBs for playback and capture.
  445. */
  446. int line6_init_pcm(struct usb_line6 *line6,
  447. struct line6_pcm_properties *properties)
  448. {
  449. int i, err;
  450. unsigned ep_read = line6->properties->ep_audio_r;
  451. unsigned ep_write = line6->properties->ep_audio_w;
  452. struct snd_pcm *pcm;
  453. struct snd_line6_pcm *line6pcm;
  454. if (!(line6->properties->capabilities & LINE6_CAP_PCM))
  455. return 0; /* skip PCM initialization and report success */
  456. err = snd_line6_new_pcm(line6, &pcm);
  457. if (err < 0)
  458. return err;
  459. line6pcm = kzalloc_obj(*line6pcm);
  460. if (!line6pcm)
  461. return -ENOMEM;
  462. mutex_init(&line6pcm->state_mutex);
  463. line6pcm->pcm = pcm;
  464. line6pcm->properties = properties;
  465. line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
  466. line6pcm->volume_monitor = 255;
  467. line6pcm->line6 = line6;
  468. spin_lock_init(&line6pcm->out.lock);
  469. spin_lock_init(&line6pcm->in.lock);
  470. line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
  471. line6->line6pcm = line6pcm;
  472. pcm->private_data = line6pcm;
  473. pcm->private_free = line6_cleanup_pcm;
  474. line6pcm->max_packet_size_in =
  475. usb_maxpacket(line6->usbdev,
  476. usb_rcvisocpipe(line6->usbdev, ep_read));
  477. line6pcm->max_packet_size_out =
  478. usb_maxpacket(line6->usbdev,
  479. usb_sndisocpipe(line6->usbdev, ep_write));
  480. if (!line6pcm->max_packet_size_in || !line6pcm->max_packet_size_out) {
  481. dev_err(line6pcm->line6->ifcdev,
  482. "cannot get proper max packet size\n");
  483. return -EINVAL;
  484. }
  485. err = line6_create_audio_out_urbs(line6pcm);
  486. if (err < 0)
  487. return err;
  488. err = line6_create_audio_in_urbs(line6pcm);
  489. if (err < 0)
  490. return err;
  491. /* mixer: */
  492. for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
  493. err = snd_ctl_add(line6->card,
  494. snd_ctl_new1(&line6_controls[i], line6pcm));
  495. if (err < 0)
  496. return err;
  497. }
  498. return 0;
  499. }
  500. EXPORT_SYMBOL_GPL(line6_init_pcm);
  501. /* prepare pcm callback */
  502. int snd_line6_prepare(struct snd_pcm_substream *substream)
  503. {
  504. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  505. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  506. guard(mutex)(&line6pcm->state_mutex);
  507. if (!pstr->running)
  508. line6_wait_clear_audio_urbs(line6pcm, pstr);
  509. if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
  510. line6pcm->out.count = 0;
  511. line6pcm->out.pos = 0;
  512. line6pcm->out.pos_done = 0;
  513. line6pcm->out.bytes = 0;
  514. line6pcm->in.count = 0;
  515. line6pcm->in.pos_done = 0;
  516. line6pcm->in.bytes = 0;
  517. }
  518. return 0;
  519. }