pcm.c 15 KB

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