pcm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. if (pstr->active_urbs || pstr->unlink_urbs) {
  167. ret = -EBUSY;
  168. goto error;
  169. }
  170. pstr->count = 0;
  171. /* Submit all currently available URBs */
  172. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  173. ret = line6_submit_audio_out_all_urbs(line6pcm);
  174. else
  175. ret = line6_submit_audio_in_all_urbs(line6pcm);
  176. }
  177. error:
  178. if (ret < 0)
  179. clear_bit(type, &pstr->running);
  180. spin_unlock_irqrestore(&pstr->lock, flags);
  181. return ret;
  182. }
  183. /* stop a PCM stream; this doesn't sync with the unlinked URBs */
  184. static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction,
  185. int type)
  186. {
  187. unsigned long flags;
  188. struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
  189. spin_lock_irqsave(&pstr->lock, flags);
  190. clear_bit(type, &pstr->running);
  191. if (!pstr->running) {
  192. line6_unlink_audio_urbs(line6pcm, pstr);
  193. if (direction == SNDRV_PCM_STREAM_CAPTURE) {
  194. line6pcm->prev_fbuf = NULL;
  195. line6pcm->prev_fsize = 0;
  196. }
  197. }
  198. spin_unlock_irqrestore(&pstr->lock, flags);
  199. }
  200. /* common PCM trigger callback */
  201. int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
  202. {
  203. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  204. struct snd_pcm_substream *s;
  205. int err;
  206. clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags);
  207. snd_pcm_group_for_each_entry(s, substream) {
  208. if (s->pcm->card != substream->pcm->card)
  209. continue;
  210. switch (cmd) {
  211. case SNDRV_PCM_TRIGGER_START:
  212. case SNDRV_PCM_TRIGGER_RESUME:
  213. err = line6_stream_start(line6pcm, s->stream,
  214. LINE6_STREAM_PCM);
  215. if (err < 0)
  216. return err;
  217. break;
  218. case SNDRV_PCM_TRIGGER_STOP:
  219. case SNDRV_PCM_TRIGGER_SUSPEND:
  220. line6_stream_stop(line6pcm, s->stream,
  221. LINE6_STREAM_PCM);
  222. break;
  223. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  224. if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  225. return -EINVAL;
  226. set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
  227. break;
  228. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  229. if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  230. return -EINVAL;
  231. clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
  232. break;
  233. default:
  234. return -EINVAL;
  235. }
  236. }
  237. return 0;
  238. }
  239. /* common PCM pointer callback */
  240. snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream)
  241. {
  242. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  243. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  244. return pstr->pos_done;
  245. }
  246. /* Acquire and start duplex streams:
  247. * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
  248. */
  249. int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type)
  250. {
  251. struct line6_pcm_stream *pstr;
  252. int ret = 0, dir;
  253. mutex_lock(&line6pcm->state_mutex);
  254. for (dir = 0; dir < 2; dir++) {
  255. pstr = get_stream(line6pcm, dir);
  256. ret = line6_buffer_acquire(line6pcm, pstr, type);
  257. if (ret < 0)
  258. goto error;
  259. if (!pstr->running)
  260. line6_wait_clear_audio_urbs(line6pcm, pstr);
  261. }
  262. for (dir = 0; dir < 2; dir++) {
  263. ret = line6_stream_start(line6pcm, dir, type);
  264. if (ret < 0)
  265. goto error;
  266. }
  267. error:
  268. mutex_unlock(&line6pcm->state_mutex);
  269. if (ret < 0)
  270. line6_pcm_release(line6pcm, type);
  271. return ret;
  272. }
  273. EXPORT_SYMBOL_GPL(line6_pcm_acquire);
  274. /* Stop and release duplex streams */
  275. void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
  276. {
  277. struct line6_pcm_stream *pstr;
  278. int dir;
  279. mutex_lock(&line6pcm->state_mutex);
  280. for (dir = 0; dir < 2; dir++)
  281. line6_stream_stop(line6pcm, dir, type);
  282. for (dir = 0; dir < 2; dir++) {
  283. pstr = get_stream(line6pcm, dir);
  284. line6_buffer_release(line6pcm, pstr, type);
  285. }
  286. mutex_unlock(&line6pcm->state_mutex);
  287. }
  288. EXPORT_SYMBOL_GPL(line6_pcm_release);
  289. /* common PCM hw_params callback */
  290. int snd_line6_hw_params(struct snd_pcm_substream *substream,
  291. struct snd_pcm_hw_params *hw_params)
  292. {
  293. int ret;
  294. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  295. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  296. mutex_lock(&line6pcm->state_mutex);
  297. ret = line6_buffer_acquire(line6pcm, pstr, LINE6_STREAM_PCM);
  298. if (ret < 0)
  299. goto error;
  300. ret = snd_pcm_lib_malloc_pages(substream,
  301. params_buffer_bytes(hw_params));
  302. if (ret < 0) {
  303. line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
  304. goto error;
  305. }
  306. pstr->period = params_period_bytes(hw_params);
  307. error:
  308. mutex_unlock(&line6pcm->state_mutex);
  309. return ret;
  310. }
  311. /* common PCM hw_free callback */
  312. int snd_line6_hw_free(struct snd_pcm_substream *substream)
  313. {
  314. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  315. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  316. mutex_lock(&line6pcm->state_mutex);
  317. line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
  318. mutex_unlock(&line6pcm->state_mutex);
  319. return snd_pcm_lib_free_pages(substream);
  320. }
  321. /* control info callback */
  322. static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
  323. struct snd_ctl_elem_info *uinfo)
  324. {
  325. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  326. uinfo->count = 2;
  327. uinfo->value.integer.min = 0;
  328. uinfo->value.integer.max = 256;
  329. return 0;
  330. }
  331. /* control get callback */
  332. static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
  333. struct snd_ctl_elem_value *ucontrol)
  334. {
  335. int i;
  336. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  337. for (i = 0; i < 2; i++)
  338. ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
  339. return 0;
  340. }
  341. /* control put callback */
  342. static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
  343. struct snd_ctl_elem_value *ucontrol)
  344. {
  345. int i, changed = 0;
  346. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  347. for (i = 0; i < 2; i++)
  348. if (line6pcm->volume_playback[i] !=
  349. ucontrol->value.integer.value[i]) {
  350. line6pcm->volume_playback[i] =
  351. ucontrol->value.integer.value[i];
  352. changed = 1;
  353. }
  354. return changed;
  355. }
  356. /* control definition */
  357. static struct snd_kcontrol_new line6_controls[] = {
  358. {
  359. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  360. .name = "PCM Playback Volume",
  361. .info = snd_line6_control_playback_info,
  362. .get = snd_line6_control_playback_get,
  363. .put = snd_line6_control_playback_put
  364. },
  365. {
  366. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  367. .name = "Impulse Response Volume",
  368. .info = snd_line6_impulse_volume_info,
  369. .get = snd_line6_impulse_volume_get,
  370. .put = snd_line6_impulse_volume_put
  371. },
  372. {
  373. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  374. .name = "Impulse Response Period",
  375. .info = snd_line6_impulse_period_info,
  376. .get = snd_line6_impulse_period_get,
  377. .put = snd_line6_impulse_period_put
  378. },
  379. };
  380. /*
  381. Cleanup the PCM device.
  382. */
  383. static void cleanup_urbs(struct line6_pcm_stream *pcms)
  384. {
  385. int i;
  386. for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
  387. if (pcms->urbs[i]) {
  388. usb_kill_urb(pcms->urbs[i]);
  389. usb_free_urb(pcms->urbs[i]);
  390. }
  391. }
  392. }
  393. static void line6_cleanup_pcm(struct snd_pcm *pcm)
  394. {
  395. struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
  396. cleanup_urbs(&line6pcm->out);
  397. cleanup_urbs(&line6pcm->in);
  398. kfree(line6pcm);
  399. }
  400. /* create a PCM device */
  401. static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
  402. {
  403. struct snd_pcm *pcm;
  404. int err;
  405. err = snd_pcm_new(line6->card, (char *)line6->properties->name,
  406. 0, 1, 1, pcm_ret);
  407. if (err < 0)
  408. return err;
  409. pcm = *pcm_ret;
  410. strcpy(pcm->name, line6->properties->name);
  411. /* set operators */
  412. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  413. &snd_line6_playback_ops);
  414. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
  415. /* pre-allocation of buffers */
  416. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  417. snd_dma_continuous_data
  418. (GFP_KERNEL), 64 * 1024,
  419. 128 * 1024);
  420. return 0;
  421. }
  422. /*
  423. Sync with PCM stream stops.
  424. */
  425. void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
  426. {
  427. line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
  428. line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
  429. line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
  430. line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
  431. }
  432. /*
  433. Create and register the PCM device and mixer entries.
  434. Create URBs for playback and capture.
  435. */
  436. int line6_init_pcm(struct usb_line6 *line6,
  437. struct line6_pcm_properties *properties)
  438. {
  439. int i, err;
  440. unsigned ep_read = line6->properties->ep_audio_r;
  441. unsigned ep_write = line6->properties->ep_audio_w;
  442. struct snd_pcm *pcm;
  443. struct snd_line6_pcm *line6pcm;
  444. if (!(line6->properties->capabilities & LINE6_CAP_PCM))
  445. return 0; /* skip PCM initialization and report success */
  446. err = snd_line6_new_pcm(line6, &pcm);
  447. if (err < 0)
  448. return err;
  449. line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
  450. if (!line6pcm)
  451. return -ENOMEM;
  452. mutex_init(&line6pcm->state_mutex);
  453. line6pcm->pcm = pcm;
  454. line6pcm->properties = properties;
  455. line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
  456. line6pcm->volume_monitor = 255;
  457. line6pcm->line6 = line6;
  458. /* Read and write buffers are sized identically, so choose minimum */
  459. line6pcm->max_packet_size = min(
  460. usb_maxpacket(line6->usbdev,
  461. usb_rcvisocpipe(line6->usbdev, ep_read), 0),
  462. usb_maxpacket(line6->usbdev,
  463. usb_sndisocpipe(line6->usbdev, ep_write), 1));
  464. spin_lock_init(&line6pcm->out.lock);
  465. spin_lock_init(&line6pcm->in.lock);
  466. line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
  467. line6->line6pcm = line6pcm;
  468. pcm->private_data = line6pcm;
  469. pcm->private_free = line6_cleanup_pcm;
  470. err = line6_create_audio_out_urbs(line6pcm);
  471. if (err < 0)
  472. return err;
  473. err = line6_create_audio_in_urbs(line6pcm);
  474. if (err < 0)
  475. return err;
  476. /* mixer: */
  477. for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
  478. err = snd_ctl_add(line6->card,
  479. snd_ctl_new1(&line6_controls[i], line6pcm));
  480. if (err < 0)
  481. return err;
  482. }
  483. return 0;
  484. }
  485. EXPORT_SYMBOL_GPL(line6_init_pcm);
  486. /* prepare pcm callback */
  487. int snd_line6_prepare(struct snd_pcm_substream *substream)
  488. {
  489. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  490. struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
  491. mutex_lock(&line6pcm->state_mutex);
  492. if (!pstr->running)
  493. line6_wait_clear_audio_urbs(line6pcm, pstr);
  494. if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
  495. line6pcm->out.count = 0;
  496. line6pcm->out.pos = 0;
  497. line6pcm->out.pos_done = 0;
  498. line6pcm->out.bytes = 0;
  499. line6pcm->in.count = 0;
  500. line6pcm->in.pos_done = 0;
  501. line6pcm->in.bytes = 0;
  502. }
  503. mutex_unlock(&line6pcm->state_mutex);
  504. return 0;
  505. }