playback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 <sound/core.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include "capture.h"
  16. #include "driver.h"
  17. #include "pcm.h"
  18. #include "playback.h"
  19. /*
  20. Software stereo volume control.
  21. */
  22. static void change_volume(struct urb *urb_out, int volume[],
  23. int bytes_per_frame)
  24. {
  25. int chn = 0;
  26. if (volume[0] == 256 && volume[1] == 256)
  27. return; /* maximum volume - no change */
  28. if (bytes_per_frame == 4) {
  29. __le16 *p, *buf_end;
  30. p = (__le16 *)urb_out->transfer_buffer;
  31. buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
  32. for (; p < buf_end; ++p) {
  33. short pv = le16_to_cpu(*p);
  34. int val = (pv * volume[chn & 1]) >> 8;
  35. pv = clamp(val, -0x8000, 0x7fff);
  36. *p = cpu_to_le16(pv);
  37. ++chn;
  38. }
  39. } else if (bytes_per_frame == 6) {
  40. unsigned char *p, *buf_end;
  41. p = (unsigned char *)urb_out->transfer_buffer;
  42. buf_end = p + urb_out->transfer_buffer_length;
  43. for (; p < buf_end; p += 3) {
  44. int val;
  45. val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
  46. val = (val * volume[chn & 1]) >> 8;
  47. val = clamp(val, -0x800000, 0x7fffff);
  48. p[0] = val;
  49. p[1] = val >> 8;
  50. p[2] = val >> 16;
  51. ++chn;
  52. }
  53. }
  54. }
  55. /*
  56. Create signal for impulse response test.
  57. */
  58. static void create_impulse_test_signal(struct snd_line6_pcm *line6pcm,
  59. struct urb *urb_out, int bytes_per_frame)
  60. {
  61. int frames = urb_out->transfer_buffer_length / bytes_per_frame;
  62. if (bytes_per_frame == 4) {
  63. int i;
  64. short *pi = (short *)line6pcm->prev_fbuf;
  65. short *po = (short *)urb_out->transfer_buffer;
  66. for (i = 0; i < frames; ++i) {
  67. po[0] = pi[0];
  68. po[1] = 0;
  69. pi += 2;
  70. po += 2;
  71. }
  72. } else if (bytes_per_frame == 6) {
  73. int i, j;
  74. unsigned char *pi = line6pcm->prev_fbuf;
  75. unsigned char *po = urb_out->transfer_buffer;
  76. for (i = 0; i < frames; ++i) {
  77. for (j = 0; j < bytes_per_frame / 2; ++j)
  78. po[j] = pi[j];
  79. for (; j < bytes_per_frame; ++j)
  80. po[j] = 0;
  81. pi += bytes_per_frame;
  82. po += bytes_per_frame;
  83. }
  84. }
  85. if (--line6pcm->impulse_count <= 0) {
  86. ((unsigned char *)(urb_out->transfer_buffer))[bytes_per_frame -
  87. 1] =
  88. line6pcm->impulse_volume;
  89. line6pcm->impulse_count = line6pcm->impulse_period;
  90. }
  91. }
  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. __le16 *pi, *po, *buf_end;
  102. pi = (__le16 *)signal;
  103. po = (__le16 *)urb_out->transfer_buffer;
  104. buf_end = po + urb_out->transfer_buffer_length / sizeof(*po);
  105. for (; po < buf_end; ++pi, ++po) {
  106. short pov = le16_to_cpu(*po);
  107. short piv = le16_to_cpu(*pi);
  108. int val = pov + ((piv * volume) >> 8);
  109. pov = clamp(val, -0x8000, 0x7fff);
  110. *po = cpu_to_le16(pov);
  111. }
  112. }
  113. /*
  114. We don't need to handle devices with 6 bytes per frame here
  115. since they all support hardware monitoring.
  116. */
  117. }
  118. /*
  119. Find a free URB, prepare audio data, and submit URB.
  120. must be called in line6pcm->out.lock context
  121. */
  122. static int submit_audio_out_urb(struct snd_line6_pcm *line6pcm)
  123. {
  124. int index;
  125. int i, urb_size, urb_frames;
  126. int ret;
  127. const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
  128. const int frame_increment =
  129. line6pcm->properties->rates.rats[0].num_min;
  130. const int frame_factor =
  131. line6pcm->properties->rates.rats[0].den *
  132. (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL);
  133. struct urb *urb_out;
  134. index =
  135. find_first_zero_bit(&line6pcm->out.active_urbs, LINE6_ISO_BUFFERS);
  136. if (index < 0 || index >= LINE6_ISO_BUFFERS) {
  137. dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
  138. return -EINVAL;
  139. }
  140. urb_out = line6pcm->out.urbs[index];
  141. urb_size = 0;
  142. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  143. /* compute frame size for given sampling rate */
  144. int fsize = 0;
  145. struct usb_iso_packet_descriptor *fout =
  146. &urb_out->iso_frame_desc[i];
  147. fsize = line6pcm->prev_fsize;
  148. if (fsize == 0) {
  149. int n;
  150. line6pcm->out.count += frame_increment;
  151. n = line6pcm->out.count / frame_factor;
  152. line6pcm->out.count -= n * frame_factor;
  153. fsize = n * bytes_per_frame;
  154. }
  155. fout->offset = urb_size;
  156. fout->length = fsize;
  157. urb_size += fsize;
  158. }
  159. if (urb_size == 0) {
  160. /* can't determine URB size */
  161. dev_err(line6pcm->line6->ifcdev, "driver bug: urb_size = 0\n");
  162. return -EINVAL;
  163. }
  164. urb_frames = urb_size / bytes_per_frame;
  165. urb_out->transfer_buffer =
  166. line6pcm->out.buffer +
  167. index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
  168. urb_out->transfer_buffer_length = urb_size;
  169. urb_out->context = line6pcm;
  170. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running) &&
  171. !test_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags)) {
  172. struct snd_pcm_runtime *runtime =
  173. get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK)->runtime;
  174. if (line6pcm->out.pos + urb_frames > runtime->buffer_size) {
  175. /*
  176. The transferred area goes over buffer boundary,
  177. copy the data to the temp buffer.
  178. */
  179. int len;
  180. len = runtime->buffer_size - line6pcm->out.pos;
  181. if (len > 0) {
  182. memcpy(urb_out->transfer_buffer,
  183. runtime->dma_area +
  184. line6pcm->out.pos * bytes_per_frame,
  185. len * bytes_per_frame);
  186. memcpy(urb_out->transfer_buffer +
  187. len * bytes_per_frame, runtime->dma_area,
  188. (urb_frames - len) * bytes_per_frame);
  189. } else
  190. dev_err(line6pcm->line6->ifcdev, "driver bug: len = %d\n",
  191. len);
  192. } else {
  193. memcpy(urb_out->transfer_buffer,
  194. runtime->dma_area +
  195. line6pcm->out.pos * bytes_per_frame,
  196. urb_out->transfer_buffer_length);
  197. }
  198. line6pcm->out.pos += urb_frames;
  199. if (line6pcm->out.pos >= runtime->buffer_size)
  200. line6pcm->out.pos -= runtime->buffer_size;
  201. change_volume(urb_out, line6pcm->volume_playback,
  202. bytes_per_frame);
  203. } else {
  204. memset(urb_out->transfer_buffer, 0,
  205. urb_out->transfer_buffer_length);
  206. }
  207. spin_lock_nested(&line6pcm->in.lock, SINGLE_DEPTH_NESTING);
  208. if (line6pcm->prev_fbuf) {
  209. if (test_bit(LINE6_STREAM_IMPULSE, &line6pcm->out.running)) {
  210. create_impulse_test_signal(line6pcm, urb_out,
  211. bytes_per_frame);
  212. if (test_bit(LINE6_STREAM_PCM, &line6pcm->in.running)) {
  213. line6_capture_copy(line6pcm,
  214. urb_out->transfer_buffer,
  215. urb_out->
  216. transfer_buffer_length);
  217. line6_capture_check_period(line6pcm,
  218. urb_out->transfer_buffer_length);
  219. }
  220. } else {
  221. if (!(line6pcm->line6->properties->capabilities & LINE6_CAP_HWMON)
  222. && line6pcm->out.running && line6pcm->in.running)
  223. add_monitor_signal(urb_out, line6pcm->prev_fbuf,
  224. line6pcm->volume_monitor,
  225. bytes_per_frame);
  226. }
  227. line6pcm->prev_fbuf = NULL;
  228. line6pcm->prev_fsize = 0;
  229. }
  230. spin_unlock(&line6pcm->in.lock);
  231. ret = usb_submit_urb(urb_out, GFP_ATOMIC);
  232. if (ret == 0)
  233. set_bit(index, &line6pcm->out.active_urbs);
  234. else
  235. dev_err(line6pcm->line6->ifcdev,
  236. "URB out #%d submission failed (%d)\n", index, ret);
  237. return 0;
  238. }
  239. /*
  240. Submit all currently available playback URBs.
  241. must be called in line6pcm->out.lock context
  242. */
  243. int line6_submit_audio_out_all_urbs(struct snd_line6_pcm *line6pcm)
  244. {
  245. int ret = 0, i;
  246. for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
  247. ret = submit_audio_out_urb(line6pcm);
  248. if (ret < 0)
  249. break;
  250. }
  251. return ret;
  252. }
  253. /*
  254. Callback for completed playback URB.
  255. */
  256. static void audio_out_callback(struct urb *urb)
  257. {
  258. int i, index, length = 0, shutdown = 0;
  259. unsigned long flags;
  260. struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
  261. struct snd_pcm_substream *substream =
  262. get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK);
  263. #if USE_CLEAR_BUFFER_WORKAROUND
  264. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  265. #endif
  266. line6pcm->out.last_frame = urb->start_frame;
  267. /* find index of URB */
  268. for (index = 0; index < LINE6_ISO_BUFFERS; index++)
  269. if (urb == line6pcm->out.urbs[index])
  270. break;
  271. if (index >= LINE6_ISO_BUFFERS)
  272. return; /* URB has been unlinked asynchronously */
  273. for (i = 0; i < LINE6_ISO_PACKETS; i++)
  274. length += urb->iso_frame_desc[i].length;
  275. spin_lock_irqsave(&line6pcm->out.lock, flags);
  276. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
  277. struct snd_pcm_runtime *runtime = substream->runtime;
  278. line6pcm->out.pos_done +=
  279. length / line6pcm->properties->bytes_per_frame;
  280. if (line6pcm->out.pos_done >= runtime->buffer_size)
  281. line6pcm->out.pos_done -= runtime->buffer_size;
  282. }
  283. clear_bit(index, &line6pcm->out.active_urbs);
  284. for (i = 0; i < LINE6_ISO_PACKETS; i++)
  285. if (urb->iso_frame_desc[i].status == -EXDEV) {
  286. shutdown = 1;
  287. break;
  288. }
  289. if (test_and_clear_bit(index, &line6pcm->out.unlink_urbs))
  290. shutdown = 1;
  291. if (!shutdown) {
  292. submit_audio_out_urb(line6pcm);
  293. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
  294. line6pcm->out.bytes += length;
  295. if (line6pcm->out.bytes >= line6pcm->out.period) {
  296. line6pcm->out.bytes %= line6pcm->out.period;
  297. spin_unlock(&line6pcm->out.lock);
  298. snd_pcm_period_elapsed(substream);
  299. spin_lock(&line6pcm->out.lock);
  300. }
  301. }
  302. }
  303. spin_unlock_irqrestore(&line6pcm->out.lock, flags);
  304. }
  305. /* open playback callback */
  306. static int snd_line6_playback_open(struct snd_pcm_substream *substream)
  307. {
  308. int err;
  309. struct snd_pcm_runtime *runtime = substream->runtime;
  310. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  311. err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  312. &line6pcm->properties->rates);
  313. if (err < 0)
  314. return err;
  315. runtime->hw = line6pcm->properties->playback_hw;
  316. return 0;
  317. }
  318. /* close playback callback */
  319. static int snd_line6_playback_close(struct snd_pcm_substream *substream)
  320. {
  321. return 0;
  322. }
  323. /* playback operators */
  324. struct snd_pcm_ops snd_line6_playback_ops = {
  325. .open = snd_line6_playback_open,
  326. .close = snd_line6_playback_close,
  327. .ioctl = snd_pcm_lib_ioctl,
  328. .hw_params = snd_line6_hw_params,
  329. .hw_free = snd_line6_hw_free,
  330. .prepare = snd_line6_prepare,
  331. .trigger = snd_line6_trigger,
  332. .pointer = snd_line6_pointer,
  333. };
  334. int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
  335. {
  336. struct usb_line6 *line6 = line6pcm->line6;
  337. int i;
  338. /* create audio URBs and fill in constant values: */
  339. for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
  340. struct urb *urb;
  341. /* URB for audio out: */
  342. urb = line6pcm->out.urbs[i] =
  343. usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
  344. if (urb == NULL)
  345. return -ENOMEM;
  346. urb->dev = line6->usbdev;
  347. urb->pipe =
  348. usb_sndisocpipe(line6->usbdev,
  349. line6->properties->ep_audio_w &
  350. USB_ENDPOINT_NUMBER_MASK);
  351. urb->transfer_flags = URB_ISO_ASAP;
  352. urb->start_frame = -1;
  353. urb->number_of_packets = LINE6_ISO_PACKETS;
  354. urb->interval = LINE6_ISO_INTERVAL;
  355. urb->error_count = 0;
  356. urb->complete = audio_out_callback;
  357. }
  358. return 0;
  359. }