playback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Line 6 Linux USB driver
  4. *
  5. * Copyright (C) 2004-2010 Markus Grabner ([email protected])
  6. */
  7. #include <linux/slab.h>
  8. #include <sound/core.h>
  9. #include <sound/pcm.h>
  10. #include <sound/pcm_params.h>
  11. #include "capture.h"
  12. #include "driver.h"
  13. #include "pcm.h"
  14. #include "playback.h"
  15. /*
  16. Software stereo volume control.
  17. */
  18. static void change_volume(struct urb *urb_out, int volume[],
  19. int bytes_per_frame)
  20. {
  21. int chn = 0;
  22. if (volume[0] == 256 && volume[1] == 256)
  23. return; /* maximum volume - no change */
  24. if (bytes_per_frame == 4) {
  25. __le16 *p, *buf_end;
  26. p = (__le16 *)urb_out->transfer_buffer;
  27. buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
  28. for (; p < buf_end; ++p) {
  29. short pv = le16_to_cpu(*p);
  30. int val = (pv * volume[chn & 1]) >> 8;
  31. pv = clamp(val, -0x8000, 0x7fff);
  32. *p = cpu_to_le16(pv);
  33. ++chn;
  34. }
  35. } else if (bytes_per_frame == 6) {
  36. unsigned char *p, *buf_end;
  37. p = (unsigned char *)urb_out->transfer_buffer;
  38. buf_end = p + urb_out->transfer_buffer_length;
  39. for (; p < buf_end; p += 3) {
  40. int val;
  41. val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
  42. val = (val * volume[chn & 1]) >> 8;
  43. val = clamp(val, -0x800000, 0x7fffff);
  44. p[0] = val;
  45. p[1] = val >> 8;
  46. p[2] = val >> 16;
  47. ++chn;
  48. }
  49. }
  50. }
  51. /*
  52. Create signal for impulse response test.
  53. */
  54. static void create_impulse_test_signal(struct snd_line6_pcm *line6pcm,
  55. struct urb *urb_out, int bytes_per_frame)
  56. {
  57. int frames = urb_out->transfer_buffer_length / bytes_per_frame;
  58. if (bytes_per_frame == 4) {
  59. int i;
  60. short *pi = (short *)line6pcm->prev_fbuf;
  61. short *po = (short *)urb_out->transfer_buffer;
  62. for (i = 0; i < frames; ++i) {
  63. po[0] = pi[0];
  64. po[1] = 0;
  65. pi += 2;
  66. po += 2;
  67. }
  68. } else if (bytes_per_frame == 6) {
  69. int i, j;
  70. unsigned char *pi = line6pcm->prev_fbuf;
  71. unsigned char *po = urb_out->transfer_buffer;
  72. for (i = 0; i < frames; ++i) {
  73. for (j = 0; j < bytes_per_frame / 2; ++j)
  74. po[j] = pi[j];
  75. for (; j < bytes_per_frame; ++j)
  76. po[j] = 0;
  77. pi += bytes_per_frame;
  78. po += bytes_per_frame;
  79. }
  80. }
  81. if (--line6pcm->impulse_count <= 0) {
  82. ((unsigned char *)(urb_out->transfer_buffer))[bytes_per_frame -
  83. 1] =
  84. line6pcm->impulse_volume;
  85. line6pcm->impulse_count = line6pcm->impulse_period;
  86. }
  87. }
  88. /*
  89. Add signal to buffer for software monitoring.
  90. */
  91. static void add_monitor_signal(struct urb *urb_out, unsigned char *signal,
  92. int volume, int bytes_per_frame)
  93. {
  94. if (volume == 0)
  95. return; /* zero volume - no change */
  96. if (bytes_per_frame == 4) {
  97. __le16 *pi, *po, *buf_end;
  98. pi = (__le16 *)signal;
  99. po = (__le16 *)urb_out->transfer_buffer;
  100. buf_end = po + urb_out->transfer_buffer_length / sizeof(*po);
  101. for (; po < buf_end; ++pi, ++po) {
  102. short pov = le16_to_cpu(*po);
  103. short piv = le16_to_cpu(*pi);
  104. int val = pov + ((piv * volume) >> 8);
  105. pov = clamp(val, -0x8000, 0x7fff);
  106. *po = cpu_to_le16(pov);
  107. }
  108. }
  109. /*
  110. We don't need to handle devices with 6 bytes per frame here
  111. since they all support hardware monitoring.
  112. */
  113. }
  114. /*
  115. Find a free URB, prepare audio data, and submit URB.
  116. must be called in line6pcm->out.lock context
  117. */
  118. static int submit_audio_out_urb(struct snd_line6_pcm *line6pcm)
  119. {
  120. int index;
  121. int i, urb_size, urb_frames;
  122. int ret;
  123. const int bytes_per_frame =
  124. line6pcm->properties->bytes_per_channel *
  125. line6pcm->properties->playback_hw.channels_max;
  126. const int frame_increment =
  127. line6pcm->properties->rates.rats[0].num_min;
  128. const int frame_factor =
  129. line6pcm->properties->rates.rats[0].den *
  130. (line6pcm->line6->intervals_per_second / LINE6_ISO_INTERVAL);
  131. struct urb *urb_out;
  132. index = find_first_zero_bit(&line6pcm->out.active_urbs,
  133. line6pcm->line6->iso_buffers);
  134. if (index < 0 || index >= line6pcm->line6->iso_buffers) {
  135. dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
  136. return -EINVAL;
  137. }
  138. urb_out = line6pcm->out.urbs[index];
  139. urb_size = 0;
  140. /* TODO: this may not work for LINE6_ISO_PACKETS != 1 */
  141. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  142. /* compute frame size for given sampling rate */
  143. int fsize = 0;
  144. struct usb_iso_packet_descriptor *fout =
  145. &urb_out->iso_frame_desc[i];
  146. fsize = line6pcm->prev_fsize;
  147. if (fsize == 0) {
  148. int n;
  149. line6pcm->out.count += frame_increment;
  150. n = line6pcm->out.count / frame_factor;
  151. line6pcm->out.count -= n * frame_factor;
  152. fsize = n;
  153. }
  154. fsize *= bytes_per_frame;
  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_out;
  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 < line6pcm->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. const int bytes_per_frame =
  264. line6pcm->properties->bytes_per_channel *
  265. line6pcm->properties->playback_hw.channels_max;
  266. #if USE_CLEAR_BUFFER_WORKAROUND
  267. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  268. #endif
  269. line6pcm->out.last_frame = urb->start_frame;
  270. /* find index of URB */
  271. for (index = 0; index < line6pcm->line6->iso_buffers; index++)
  272. if (urb == line6pcm->out.urbs[index])
  273. break;
  274. if (index >= line6pcm->line6->iso_buffers)
  275. return; /* URB has been unlinked asynchronously */
  276. for (i = 0; i < LINE6_ISO_PACKETS; i++)
  277. length += urb->iso_frame_desc[i].length;
  278. spin_lock_irqsave(&line6pcm->out.lock, flags);
  279. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
  280. struct snd_pcm_runtime *runtime = substream->runtime;
  281. line6pcm->out.pos_done +=
  282. length / bytes_per_frame;
  283. if (line6pcm->out.pos_done >= runtime->buffer_size)
  284. line6pcm->out.pos_done -= runtime->buffer_size;
  285. }
  286. clear_bit(index, &line6pcm->out.active_urbs);
  287. for (i = 0; i < LINE6_ISO_PACKETS; i++)
  288. if (urb->iso_frame_desc[i].status == -EXDEV) {
  289. shutdown = 1;
  290. break;
  291. }
  292. if (test_and_clear_bit(index, &line6pcm->out.unlink_urbs))
  293. shutdown = 1;
  294. if (!shutdown) {
  295. submit_audio_out_urb(line6pcm);
  296. if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
  297. line6pcm->out.bytes += length;
  298. if (line6pcm->out.bytes >= line6pcm->out.period) {
  299. line6pcm->out.bytes %= line6pcm->out.period;
  300. spin_unlock(&line6pcm->out.lock);
  301. snd_pcm_period_elapsed(substream);
  302. spin_lock(&line6pcm->out.lock);
  303. }
  304. }
  305. }
  306. spin_unlock_irqrestore(&line6pcm->out.lock, flags);
  307. }
  308. /* open playback callback */
  309. static int snd_line6_playback_open(struct snd_pcm_substream *substream)
  310. {
  311. int err;
  312. struct snd_pcm_runtime *runtime = substream->runtime;
  313. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  314. err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  315. &line6pcm->properties->rates);
  316. if (err < 0)
  317. return err;
  318. runtime->hw = line6pcm->properties->playback_hw;
  319. return 0;
  320. }
  321. /* close playback callback */
  322. static int snd_line6_playback_close(struct snd_pcm_substream *substream)
  323. {
  324. return 0;
  325. }
  326. /* playback operators */
  327. const struct snd_pcm_ops snd_line6_playback_ops = {
  328. .open = snd_line6_playback_open,
  329. .close = snd_line6_playback_close,
  330. .hw_params = snd_line6_hw_params,
  331. .hw_free = snd_line6_hw_free,
  332. .prepare = snd_line6_prepare,
  333. .trigger = snd_line6_trigger,
  334. .pointer = snd_line6_pointer,
  335. };
  336. int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
  337. {
  338. struct usb_line6 *line6 = line6pcm->line6;
  339. int i;
  340. line6pcm->out.urbs = kcalloc(line6->iso_buffers, sizeof(struct urb *),
  341. GFP_KERNEL);
  342. if (line6pcm->out.urbs == NULL)
  343. return -ENOMEM;
  344. /* create audio URBs and fill in constant values: */
  345. for (i = 0; i < line6->iso_buffers; ++i) {
  346. struct urb *urb;
  347. /* URB for audio out: */
  348. urb = line6pcm->out.urbs[i] =
  349. usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
  350. if (urb == NULL)
  351. return -ENOMEM;
  352. urb->dev = line6->usbdev;
  353. urb->pipe =
  354. usb_sndisocpipe(line6->usbdev,
  355. line6->properties->ep_audio_w &
  356. USB_ENDPOINT_NUMBER_MASK);
  357. urb->transfer_flags = URB_ISO_ASAP;
  358. urb->start_frame = -1;
  359. urb->number_of_packets = LINE6_ISO_PACKETS;
  360. urb->interval = LINE6_ISO_INTERVAL;
  361. urb->error_count = 0;
  362. urb->complete = audio_out_callback;
  363. if (usb_urb_ep_type_check(urb))
  364. return -EINVAL;
  365. }
  366. return 0;
  367. }