playback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 =
  128. line6pcm->properties->bytes_per_channel *
  129. line6pcm->properties->playback_hw.channels_max;
  130. const int frame_increment =
  131. line6pcm->properties->rates.rats[0].num_min;
  132. const int frame_factor =
  133. line6pcm->properties->rates.rats[0].den *
  134. (line6pcm->line6->intervals_per_second / LINE6_ISO_INTERVAL);
  135. struct urb *urb_out;
  136. index = find_first_zero_bit(&line6pcm->out.active_urbs,
  137. line6pcm->line6->iso_buffers);
  138. if (index < 0 || index >= line6pcm->line6->iso_buffers) {
  139. dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
  140. return -EINVAL;
  141. }
  142. urb_out = line6pcm->out.urbs[index];
  143. urb_size = 0;
  144. /* TODO: this may not work for LINE6_ISO_PACKETS != 1 */
  145. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  146. /* compute frame size for given sampling rate */
  147. int fsize = 0;
  148. struct usb_iso_packet_descriptor *fout =
  149. &urb_out->iso_frame_desc[i];
  150. fsize = line6pcm->prev_fsize;
  151. if (fsize == 0) {
  152. int n;
  153. line6pcm->out.count += frame_increment;
  154. n = line6pcm->out.count / frame_factor;
  155. line6pcm->out.count -= n * frame_factor;
  156. fsize = n;
  157. }
  158. fsize *= bytes_per_frame;
  159. fout->offset = urb_size;
  160. fout->length = fsize;
  161. urb_size += fsize;
  162. }
  163. if (urb_size == 0) {
  164. /* can't determine URB size */
  165. dev_err(line6pcm->line6->ifcdev, "driver bug: urb_size = 0\n");
  166. return -EINVAL;
  167. }
  168. urb_frames = urb_size / bytes_per_frame;
  169. urb_out->transfer_buffer =
  170. line6pcm->out.buffer +
  171. index * LINE6_ISO_PACKETS * line6pcm->max_packet_size_out;
  172. urb_out->transfer_buffer_length = urb_size;
  173. urb_out->context = line6pcm;
  174. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running) &&
  175. !test_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags)) {
  176. struct snd_pcm_runtime *runtime =
  177. get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK)->runtime;
  178. if (line6pcm->out.pos + urb_frames > runtime->buffer_size) {
  179. /*
  180. The transferred area goes over buffer boundary,
  181. copy the data to the temp buffer.
  182. */
  183. int len;
  184. len = runtime->buffer_size - line6pcm->out.pos;
  185. if (len > 0) {
  186. memcpy(urb_out->transfer_buffer,
  187. runtime->dma_area +
  188. line6pcm->out.pos * bytes_per_frame,
  189. len * bytes_per_frame);
  190. memcpy(urb_out->transfer_buffer +
  191. len * bytes_per_frame, runtime->dma_area,
  192. (urb_frames - len) * bytes_per_frame);
  193. } else
  194. dev_err(line6pcm->line6->ifcdev, "driver bug: len = %d\n",
  195. len);
  196. } else {
  197. memcpy(urb_out->transfer_buffer,
  198. runtime->dma_area +
  199. line6pcm->out.pos * bytes_per_frame,
  200. urb_out->transfer_buffer_length);
  201. }
  202. line6pcm->out.pos += urb_frames;
  203. if (line6pcm->out.pos >= runtime->buffer_size)
  204. line6pcm->out.pos -= runtime->buffer_size;
  205. change_volume(urb_out, line6pcm->volume_playback,
  206. bytes_per_frame);
  207. } else {
  208. memset(urb_out->transfer_buffer, 0,
  209. urb_out->transfer_buffer_length);
  210. }
  211. spin_lock_nested(&line6pcm->in.lock, SINGLE_DEPTH_NESTING);
  212. if (line6pcm->prev_fbuf) {
  213. if (test_bit(LINE6_STREAM_IMPULSE, &line6pcm->out.running)) {
  214. create_impulse_test_signal(line6pcm, urb_out,
  215. bytes_per_frame);
  216. if (test_bit(LINE6_STREAM_PCM, &line6pcm->in.running)) {
  217. line6_capture_copy(line6pcm,
  218. urb_out->transfer_buffer,
  219. urb_out->
  220. transfer_buffer_length);
  221. line6_capture_check_period(line6pcm,
  222. urb_out->transfer_buffer_length);
  223. }
  224. } else {
  225. if (!(line6pcm->line6->properties->capabilities & LINE6_CAP_HWMON)
  226. && line6pcm->out.running && line6pcm->in.running)
  227. add_monitor_signal(urb_out, line6pcm->prev_fbuf,
  228. line6pcm->volume_monitor,
  229. bytes_per_frame);
  230. }
  231. line6pcm->prev_fbuf = NULL;
  232. line6pcm->prev_fsize = 0;
  233. }
  234. spin_unlock(&line6pcm->in.lock);
  235. ret = usb_submit_urb(urb_out, GFP_ATOMIC);
  236. if (ret == 0)
  237. set_bit(index, &line6pcm->out.active_urbs);
  238. else
  239. dev_err(line6pcm->line6->ifcdev,
  240. "URB out #%d submission failed (%d)\n", index, ret);
  241. return 0;
  242. }
  243. /*
  244. Submit all currently available playback URBs.
  245. must be called in line6pcm->out.lock context
  246. */
  247. int line6_submit_audio_out_all_urbs(struct snd_line6_pcm *line6pcm)
  248. {
  249. int ret = 0, i;
  250. for (i = 0; i < line6pcm->line6->iso_buffers; ++i) {
  251. ret = submit_audio_out_urb(line6pcm);
  252. if (ret < 0)
  253. break;
  254. }
  255. return ret;
  256. }
  257. /*
  258. Callback for completed playback URB.
  259. */
  260. static void audio_out_callback(struct urb *urb)
  261. {
  262. int i, index, length = 0, shutdown = 0;
  263. unsigned long flags;
  264. struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
  265. struct snd_pcm_substream *substream =
  266. get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK);
  267. const int bytes_per_frame =
  268. line6pcm->properties->bytes_per_channel *
  269. line6pcm->properties->playback_hw.channels_max;
  270. #if USE_CLEAR_BUFFER_WORKAROUND
  271. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  272. #endif
  273. line6pcm->out.last_frame = urb->start_frame;
  274. /* find index of URB */
  275. for (index = 0; index < line6pcm->line6->iso_buffers; index++)
  276. if (urb == line6pcm->out.urbs[index])
  277. break;
  278. if (index >= line6pcm->line6->iso_buffers)
  279. return; /* URB has been unlinked asynchronously */
  280. for (i = 0; i < LINE6_ISO_PACKETS; i++)
  281. length += urb->iso_frame_desc[i].length;
  282. spin_lock_irqsave(&line6pcm->out.lock, flags);
  283. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
  284. struct snd_pcm_runtime *runtime = substream->runtime;
  285. line6pcm->out.pos_done +=
  286. length / bytes_per_frame;
  287. if (line6pcm->out.pos_done >= runtime->buffer_size)
  288. line6pcm->out.pos_done -= runtime->buffer_size;
  289. }
  290. clear_bit(index, &line6pcm->out.active_urbs);
  291. for (i = 0; i < LINE6_ISO_PACKETS; i++)
  292. if (urb->iso_frame_desc[i].status == -EXDEV) {
  293. shutdown = 1;
  294. break;
  295. }
  296. if (test_and_clear_bit(index, &line6pcm->out.unlink_urbs))
  297. shutdown = 1;
  298. if (!shutdown) {
  299. submit_audio_out_urb(line6pcm);
  300. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
  301. line6pcm->out.bytes += length;
  302. if (line6pcm->out.bytes >= line6pcm->out.period) {
  303. line6pcm->out.bytes %= line6pcm->out.period;
  304. spin_unlock(&line6pcm->out.lock);
  305. snd_pcm_period_elapsed(substream);
  306. spin_lock(&line6pcm->out.lock);
  307. }
  308. }
  309. }
  310. spin_unlock_irqrestore(&line6pcm->out.lock, flags);
  311. }
  312. /* open playback callback */
  313. static int snd_line6_playback_open(struct snd_pcm_substream *substream)
  314. {
  315. int err;
  316. struct snd_pcm_runtime *runtime = substream->runtime;
  317. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  318. err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  319. &line6pcm->properties->rates);
  320. if (err < 0)
  321. return err;
  322. runtime->hw = line6pcm->properties->playback_hw;
  323. return 0;
  324. }
  325. /* close playback callback */
  326. static int snd_line6_playback_close(struct snd_pcm_substream *substream)
  327. {
  328. return 0;
  329. }
  330. /* playback operators */
  331. struct snd_pcm_ops snd_line6_playback_ops = {
  332. .open = snd_line6_playback_open,
  333. .close = snd_line6_playback_close,
  334. .ioctl = snd_pcm_lib_ioctl,
  335. .hw_params = snd_line6_hw_params,
  336. .hw_free = snd_line6_hw_free,
  337. .prepare = snd_line6_prepare,
  338. .trigger = snd_line6_trigger,
  339. .pointer = snd_line6_pointer,
  340. };
  341. int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
  342. {
  343. struct usb_line6 *line6 = line6pcm->line6;
  344. int i;
  345. line6pcm->out.urbs = kzalloc(
  346. sizeof(struct urb *) * line6->iso_buffers, GFP_KERNEL);
  347. if (line6pcm->out.urbs == NULL)
  348. return -ENOMEM;
  349. /* create audio URBs and fill in constant values: */
  350. for (i = 0; i < line6->iso_buffers; ++i) {
  351. struct urb *urb;
  352. /* URB for audio out: */
  353. urb = line6pcm->out.urbs[i] =
  354. usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
  355. if (urb == NULL)
  356. return -ENOMEM;
  357. urb->dev = line6->usbdev;
  358. urb->pipe =
  359. usb_sndisocpipe(line6->usbdev,
  360. line6->properties->ep_audio_w &
  361. USB_ENDPOINT_NUMBER_MASK);
  362. urb->transfer_flags = URB_ISO_ASAP;
  363. urb->start_frame = -1;
  364. urb->number_of_packets = LINE6_ISO_PACKETS;
  365. urb->interval = LINE6_ISO_INTERVAL;
  366. urb->error_count = 0;
  367. urb->complete = audio_out_callback;
  368. }
  369. return 0;
  370. }