playback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Line6 Linux USB driver - 0.9.1beta
  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 <sound/core.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include "audio.h"
  16. #include "capture.h"
  17. #include "driver.h"
  18. #include "pcm.h"
  19. #include "pod.h"
  20. #include "playback.h"
  21. /*
  22. Software stereo volume control.
  23. */
  24. static void change_volume(struct urb *urb_out, int volume[],
  25. int bytes_per_frame)
  26. {
  27. int chn = 0;
  28. if (volume[0] == 256 && volume[1] == 256)
  29. return; /* maximum volume - no change */
  30. if (bytes_per_frame == 4) {
  31. short *p, *buf_end;
  32. p = (short *)urb_out->transfer_buffer;
  33. buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
  34. for (; p < buf_end; ++p) {
  35. *p = (*p * volume[chn & 1]) >> 8;
  36. ++chn;
  37. }
  38. } else if (bytes_per_frame == 6) {
  39. unsigned char *p, *buf_end;
  40. p = (unsigned char *)urb_out->transfer_buffer;
  41. buf_end = p + urb_out->transfer_buffer_length;
  42. for (; p < buf_end; p += 3) {
  43. int val;
  44. val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
  45. val = (val * volume[chn & 1]) >> 8;
  46. p[0] = val;
  47. p[1] = val >> 8;
  48. p[2] = val >> 16;
  49. ++chn;
  50. }
  51. }
  52. }
  53. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  54. /*
  55. Create signal for impulse response test.
  56. */
  57. static void create_impulse_test_signal(struct snd_line6_pcm *line6pcm,
  58. struct urb *urb_out, int bytes_per_frame)
  59. {
  60. int frames = urb_out->transfer_buffer_length / bytes_per_frame;
  61. if (bytes_per_frame == 4) {
  62. int i;
  63. short *pi = (short *)line6pcm->prev_fbuf;
  64. short *po = (short *)urb_out->transfer_buffer;
  65. for (i = 0; i < frames; ++i) {
  66. po[0] = pi[0];
  67. po[1] = 0;
  68. pi += 2;
  69. po += 2;
  70. }
  71. } else if (bytes_per_frame == 6) {
  72. int i, j;
  73. unsigned char *pi = line6pcm->prev_fbuf;
  74. unsigned char *po = urb_out->transfer_buffer;
  75. for (i = 0; i < frames; ++i) {
  76. for (j = 0; j < bytes_per_frame / 2; ++j)
  77. po[j] = pi[j];
  78. for (; j < bytes_per_frame; ++j)
  79. po[j] = 0;
  80. pi += bytes_per_frame;
  81. po += bytes_per_frame;
  82. }
  83. }
  84. if (--line6pcm->impulse_count <= 0) {
  85. ((unsigned char *)(urb_out->transfer_buffer))[bytes_per_frame -
  86. 1] =
  87. line6pcm->impulse_volume;
  88. line6pcm->impulse_count = line6pcm->impulse_period;
  89. }
  90. }
  91. #endif
  92. /*
  93. Add signal to buffer for software monitoring.
  94. */
  95. static void add_monitor_signal(struct urb *urb_out, unsigned char *signal,
  96. int volume, int bytes_per_frame)
  97. {
  98. if (volume == 0)
  99. return; /* zero volume - no change */
  100. if (bytes_per_frame == 4) {
  101. short *pi, *po, *buf_end;
  102. pi = (short *)signal;
  103. po = (short *)urb_out->transfer_buffer;
  104. buf_end = po + urb_out->transfer_buffer_length / sizeof(*po);
  105. for (; po < buf_end; ++pi, ++po)
  106. *po += (*pi * volume) >> 8;
  107. }
  108. /*
  109. We don't need to handle devices with 6 bytes per frame here
  110. since they all support hardware monitoring.
  111. */
  112. }
  113. /*
  114. Find a free URB, prepare audio data, and submit URB.
  115. */
  116. static int submit_audio_out_urb(struct snd_line6_pcm *line6pcm)
  117. {
  118. int index;
  119. unsigned long flags;
  120. int i, urb_size, urb_frames;
  121. int ret;
  122. const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
  123. const int frame_increment =
  124. line6pcm->properties->snd_line6_rates.rats[0].num_min;
  125. const int frame_factor =
  126. line6pcm->properties->snd_line6_rates.rats[0].den *
  127. (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL);
  128. struct urb *urb_out;
  129. spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
  130. index =
  131. find_first_zero_bit(&line6pcm->active_urb_out, LINE6_ISO_BUFFERS);
  132. if (index < 0 || index >= LINE6_ISO_BUFFERS) {
  133. spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
  134. dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
  135. return -EINVAL;
  136. }
  137. urb_out = line6pcm->urb_audio_out[index];
  138. urb_size = 0;
  139. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  140. /* compute frame size for given sampling rate */
  141. int fsize = 0;
  142. struct usb_iso_packet_descriptor *fout =
  143. &urb_out->iso_frame_desc[i];
  144. if (line6pcm->flags & LINE6_BITS_CAPTURE_STREAM)
  145. fsize = line6pcm->prev_fsize;
  146. if (fsize == 0) {
  147. int n;
  148. line6pcm->count_out += frame_increment;
  149. n = line6pcm->count_out / frame_factor;
  150. line6pcm->count_out -= n * frame_factor;
  151. fsize = n * bytes_per_frame;
  152. }
  153. fout->offset = urb_size;
  154. fout->length = fsize;
  155. urb_size += fsize;
  156. }
  157. if (urb_size == 0) {
  158. /* can't determine URB size */
  159. spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
  160. dev_err(line6pcm->line6->ifcdev, "driver bug: urb_size = 0\n");
  161. return -EINVAL;
  162. }
  163. urb_frames = urb_size / bytes_per_frame;
  164. urb_out->transfer_buffer =
  165. line6pcm->buffer_out +
  166. index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
  167. urb_out->transfer_buffer_length = urb_size;
  168. urb_out->context = line6pcm;
  169. if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM, &line6pcm->flags) &&
  170. !test_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags)) {
  171. struct snd_pcm_runtime *runtime =
  172. get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK)->runtime;
  173. if (line6pcm->pos_out + urb_frames > runtime->buffer_size) {
  174. /*
  175. The transferred area goes over buffer boundary,
  176. copy the data to the temp buffer.
  177. */
  178. int len;
  179. len = runtime->buffer_size - line6pcm->pos_out;
  180. if (len > 0) {
  181. memcpy(urb_out->transfer_buffer,
  182. runtime->dma_area +
  183. line6pcm->pos_out * bytes_per_frame,
  184. len * bytes_per_frame);
  185. memcpy(urb_out->transfer_buffer +
  186. len * bytes_per_frame, runtime->dma_area,
  187. (urb_frames - len) * bytes_per_frame);
  188. } else
  189. dev_err(line6pcm->line6->ifcdev, "driver bug: len = %d\n",
  190. len);
  191. } else {
  192. memcpy(urb_out->transfer_buffer,
  193. runtime->dma_area +
  194. line6pcm->pos_out * bytes_per_frame,
  195. urb_out->transfer_buffer_length);
  196. }
  197. line6pcm->pos_out += urb_frames;
  198. if (line6pcm->pos_out >= runtime->buffer_size)
  199. line6pcm->pos_out -= runtime->buffer_size;
  200. } else {
  201. memset(urb_out->transfer_buffer, 0,
  202. urb_out->transfer_buffer_length);
  203. }
  204. change_volume(urb_out, line6pcm->volume_playback, bytes_per_frame);
  205. if (line6pcm->prev_fbuf != NULL) {
  206. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  207. if (line6pcm->flags & LINE6_BITS_PCM_IMPULSE) {
  208. create_impulse_test_signal(line6pcm, urb_out,
  209. bytes_per_frame);
  210. if (line6pcm->flags &
  211. LINE6_BIT_PCM_ALSA_CAPTURE_STREAM) {
  212. line6_capture_copy(line6pcm,
  213. urb_out->transfer_buffer,
  214. urb_out->
  215. transfer_buffer_length);
  216. line6_capture_check_period(line6pcm,
  217. urb_out->transfer_buffer_length);
  218. }
  219. } else {
  220. #endif
  221. if (!
  222. (line6pcm->line6->
  223. properties->capabilities & LINE6_BIT_HWMON)
  224. && (line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM)
  225. && (line6pcm->flags & LINE6_BITS_CAPTURE_STREAM))
  226. add_monitor_signal(urb_out, line6pcm->prev_fbuf,
  227. line6pcm->volume_monitor,
  228. bytes_per_frame);
  229. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  230. }
  231. #endif
  232. }
  233. ret = usb_submit_urb(urb_out, GFP_ATOMIC);
  234. if (ret == 0)
  235. set_bit(index, &line6pcm->active_urb_out);
  236. else
  237. dev_err(line6pcm->line6->ifcdev,
  238. "URB out #%d submission failed (%d)\n", index, ret);
  239. spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
  240. return 0;
  241. }
  242. /*
  243. Submit all currently available playback URBs.
  244. */
  245. int line6_submit_audio_out_all_urbs(struct snd_line6_pcm *line6pcm)
  246. {
  247. int ret, i;
  248. for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
  249. ret = submit_audio_out_urb(line6pcm);
  250. if (ret < 0)
  251. return ret;
  252. }
  253. return 0;
  254. }
  255. /*
  256. Unlink all currently active playback URBs.
  257. */
  258. void line6_unlink_audio_out_urbs(struct snd_line6_pcm *line6pcm)
  259. {
  260. unsigned int i;
  261. for (i = LINE6_ISO_BUFFERS; i--;) {
  262. if (test_bit(i, &line6pcm->active_urb_out)) {
  263. if (!test_and_set_bit(i, &line6pcm->unlink_urb_out)) {
  264. struct urb *u = line6pcm->urb_audio_out[i];
  265. usb_unlink_urb(u);
  266. }
  267. }
  268. }
  269. }
  270. /*
  271. Wait until unlinking of all currently active playback URBs has been
  272. finished.
  273. */
  274. void line6_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
  275. {
  276. int timeout = HZ;
  277. unsigned int i;
  278. int alive;
  279. do {
  280. alive = 0;
  281. for (i = LINE6_ISO_BUFFERS; i--;) {
  282. if (test_bit(i, &line6pcm->active_urb_out))
  283. alive++;
  284. }
  285. if (!alive)
  286. break;
  287. set_current_state(TASK_UNINTERRUPTIBLE);
  288. schedule_timeout(1);
  289. } while (--timeout > 0);
  290. if (alive)
  291. snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
  292. }
  293. /*
  294. Unlink all currently active playback URBs, and wait for finishing.
  295. */
  296. void line6_unlink_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
  297. {
  298. line6_unlink_audio_out_urbs(line6pcm);
  299. line6_wait_clear_audio_out_urbs(line6pcm);
  300. }
  301. void line6_free_playback_buffer(struct snd_line6_pcm *line6pcm)
  302. {
  303. kfree(line6pcm->buffer_out);
  304. line6pcm->buffer_out = NULL;
  305. }
  306. /*
  307. Callback for completed playback URB.
  308. */
  309. static void audio_out_callback(struct urb *urb)
  310. {
  311. int i, index, length = 0, shutdown = 0;
  312. unsigned long flags;
  313. struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
  314. struct snd_pcm_substream *substream =
  315. get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK);
  316. #if USE_CLEAR_BUFFER_WORKAROUND
  317. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  318. #endif
  319. line6pcm->last_frame_out = urb->start_frame;
  320. /* find index of URB */
  321. for (index = LINE6_ISO_BUFFERS; index--;)
  322. if (urb == line6pcm->urb_audio_out[index])
  323. break;
  324. if (index < 0)
  325. return; /* URB has been unlinked asynchronously */
  326. for (i = LINE6_ISO_PACKETS; i--;)
  327. length += urb->iso_frame_desc[i].length;
  328. spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
  329. if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM, &line6pcm->flags)) {
  330. struct snd_pcm_runtime *runtime = substream->runtime;
  331. line6pcm->pos_out_done +=
  332. length / line6pcm->properties->bytes_per_frame;
  333. if (line6pcm->pos_out_done >= runtime->buffer_size)
  334. line6pcm->pos_out_done -= runtime->buffer_size;
  335. }
  336. clear_bit(index, &line6pcm->active_urb_out);
  337. for (i = LINE6_ISO_PACKETS; i--;)
  338. if (urb->iso_frame_desc[i].status == -EXDEV) {
  339. shutdown = 1;
  340. break;
  341. }
  342. if (test_and_clear_bit(index, &line6pcm->unlink_urb_out))
  343. shutdown = 1;
  344. spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
  345. if (!shutdown) {
  346. submit_audio_out_urb(line6pcm);
  347. if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM,
  348. &line6pcm->flags)) {
  349. line6pcm->bytes_out += length;
  350. if (line6pcm->bytes_out >= line6pcm->period_out) {
  351. line6pcm->bytes_out %= line6pcm->period_out;
  352. snd_pcm_period_elapsed(substream);
  353. }
  354. }
  355. }
  356. }
  357. /* open playback callback */
  358. static int snd_line6_playback_open(struct snd_pcm_substream *substream)
  359. {
  360. int err;
  361. struct snd_pcm_runtime *runtime = substream->runtime;
  362. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  363. err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  364. (&line6pcm->
  365. properties->snd_line6_rates));
  366. if (err < 0)
  367. return err;
  368. runtime->hw = line6pcm->properties->snd_line6_playback_hw;
  369. return 0;
  370. }
  371. /* close playback callback */
  372. static int snd_line6_playback_close(struct snd_pcm_substream *substream)
  373. {
  374. return 0;
  375. }
  376. /* hw_params playback callback */
  377. static int snd_line6_playback_hw_params(struct snd_pcm_substream *substream,
  378. struct snd_pcm_hw_params *hw_params)
  379. {
  380. int ret;
  381. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  382. /* -- Florian Demski [FD] */
  383. /* don't ask me why, but this fixes the bug on my machine */
  384. if (line6pcm == NULL) {
  385. if (substream->pcm == NULL)
  386. return -ENOMEM;
  387. if (substream->pcm->private_data == NULL)
  388. return -ENOMEM;
  389. substream->private_data = substream->pcm->private_data;
  390. line6pcm = snd_pcm_substream_chip(substream);
  391. }
  392. /* -- [FD] end */
  393. ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
  394. if (ret < 0)
  395. return ret;
  396. ret = snd_pcm_lib_malloc_pages(substream,
  397. params_buffer_bytes(hw_params));
  398. if (ret < 0) {
  399. line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
  400. return ret;
  401. }
  402. line6pcm->period_out = params_period_bytes(hw_params);
  403. return 0;
  404. }
  405. /* hw_free playback callback */
  406. static int snd_line6_playback_hw_free(struct snd_pcm_substream *substream)
  407. {
  408. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  409. line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
  410. return snd_pcm_lib_free_pages(substream);
  411. }
  412. /* trigger playback callback */
  413. int snd_line6_playback_trigger(struct snd_line6_pcm *line6pcm, int cmd)
  414. {
  415. int err;
  416. switch (cmd) {
  417. case SNDRV_PCM_TRIGGER_START:
  418. #ifdef CONFIG_PM
  419. case SNDRV_PCM_TRIGGER_RESUME:
  420. #endif
  421. err = line6_pcm_acquire(line6pcm,
  422. LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM);
  423. if (err < 0)
  424. return err;
  425. break;
  426. case SNDRV_PCM_TRIGGER_STOP:
  427. #ifdef CONFIG_PM
  428. case SNDRV_PCM_TRIGGER_SUSPEND:
  429. #endif
  430. err = line6_pcm_release(line6pcm,
  431. LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM);
  432. if (err < 0)
  433. return err;
  434. break;
  435. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  436. set_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags);
  437. break;
  438. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  439. clear_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags);
  440. break;
  441. default:
  442. return -EINVAL;
  443. }
  444. return 0;
  445. }
  446. /* playback pointer callback */
  447. static snd_pcm_uframes_t
  448. snd_line6_playback_pointer(struct snd_pcm_substream *substream)
  449. {
  450. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  451. return line6pcm->pos_out_done;
  452. }
  453. /* playback operators */
  454. struct snd_pcm_ops snd_line6_playback_ops = {
  455. .open = snd_line6_playback_open,
  456. .close = snd_line6_playback_close,
  457. .ioctl = snd_pcm_lib_ioctl,
  458. .hw_params = snd_line6_playback_hw_params,
  459. .hw_free = snd_line6_playback_hw_free,
  460. .prepare = snd_line6_prepare,
  461. .trigger = snd_line6_trigger,
  462. .pointer = snd_line6_playback_pointer,
  463. };
  464. int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
  465. {
  466. int i;
  467. /* create audio URBs and fill in constant values: */
  468. for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
  469. struct urb *urb;
  470. /* URB for audio out: */
  471. urb = line6pcm->urb_audio_out[i] =
  472. usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
  473. if (urb == NULL) {
  474. dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
  475. return -ENOMEM;
  476. }
  477. urb->dev = line6pcm->line6->usbdev;
  478. urb->pipe =
  479. usb_sndisocpipe(line6pcm->line6->usbdev,
  480. line6pcm->ep_audio_write &
  481. USB_ENDPOINT_NUMBER_MASK);
  482. urb->transfer_flags = URB_ISO_ASAP;
  483. urb->start_frame = -1;
  484. urb->number_of_packets = LINE6_ISO_PACKETS;
  485. urb->interval = LINE6_ISO_INTERVAL;
  486. urb->error_count = 0;
  487. urb->complete = audio_out_callback;
  488. }
  489. return 0;
  490. }