pcm.c 15 KB

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