audio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese
  4. */
  5. #include <linux/device.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/usb.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include "device.h"
  13. #include "audio.h"
  14. #define N_URBS 32
  15. #define CLOCK_DRIFT_TOLERANCE 5
  16. #define FRAMES_PER_URB 8
  17. #define BYTES_PER_FRAME 512
  18. #define CHANNELS_PER_STREAM 2
  19. #define BYTES_PER_SAMPLE 3
  20. #define BYTES_PER_SAMPLE_USB 4
  21. #define MAX_BUFFER_SIZE (128*1024)
  22. #define MAX_ENDPOINT_SIZE 512
  23. #define ENDPOINT_CAPTURE 2
  24. #define ENDPOINT_PLAYBACK 6
  25. #define MAKE_CHECKBYTE(cdev,stream,i) \
  26. (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
  27. static const struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
  28. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  29. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  30. .formats = SNDRV_PCM_FMTBIT_S24_3BE,
  31. .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  32. SNDRV_PCM_RATE_96000),
  33. .rate_min = 44100,
  34. .rate_max = 0, /* will overwrite later */
  35. .channels_min = CHANNELS_PER_STREAM,
  36. .channels_max = CHANNELS_PER_STREAM,
  37. .buffer_bytes_max = MAX_BUFFER_SIZE,
  38. .period_bytes_min = 128,
  39. .period_bytes_max = MAX_BUFFER_SIZE,
  40. .periods_min = 1,
  41. .periods_max = 1024,
  42. };
  43. static void
  44. activate_substream(struct snd_usb_caiaqdev *cdev,
  45. struct snd_pcm_substream *sub)
  46. {
  47. spin_lock(&cdev->spinlock);
  48. if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  49. cdev->sub_playback[sub->number] = sub;
  50. else
  51. cdev->sub_capture[sub->number] = sub;
  52. spin_unlock(&cdev->spinlock);
  53. }
  54. static void
  55. deactivate_substream(struct snd_usb_caiaqdev *cdev,
  56. struct snd_pcm_substream *sub)
  57. {
  58. unsigned long flags;
  59. spin_lock_irqsave(&cdev->spinlock, flags);
  60. if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  61. cdev->sub_playback[sub->number] = NULL;
  62. else
  63. cdev->sub_capture[sub->number] = NULL;
  64. spin_unlock_irqrestore(&cdev->spinlock, flags);
  65. }
  66. static int
  67. all_substreams_zero(struct snd_pcm_substream **subs)
  68. {
  69. int i;
  70. for (i = 0; i < MAX_STREAMS; i++)
  71. if (subs[i] != NULL)
  72. return 0;
  73. return 1;
  74. }
  75. static int stream_start(struct snd_usb_caiaqdev *cdev)
  76. {
  77. int i, ret;
  78. struct device *dev = caiaqdev_to_dev(cdev);
  79. dev_dbg(dev, "%s(%p)\n", __func__, cdev);
  80. if (cdev->streaming)
  81. return -EINVAL;
  82. memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
  83. memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
  84. cdev->input_panic = 0;
  85. cdev->output_panic = 0;
  86. cdev->first_packet = 4;
  87. cdev->streaming = 1;
  88. cdev->warned = 0;
  89. for (i = 0; i < N_URBS; i++) {
  90. ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC);
  91. if (ret) {
  92. dev_err(dev, "unable to trigger read #%d! (ret %d)\n",
  93. i, ret);
  94. cdev->streaming = 0;
  95. return -EPIPE;
  96. }
  97. }
  98. return 0;
  99. }
  100. static void stream_stop(struct snd_usb_caiaqdev *cdev)
  101. {
  102. int i;
  103. struct device *dev = caiaqdev_to_dev(cdev);
  104. dev_dbg(dev, "%s(%p)\n", __func__, cdev);
  105. if (!cdev->streaming)
  106. return;
  107. cdev->streaming = 0;
  108. for (i = 0; i < N_URBS; i++) {
  109. usb_kill_urb(cdev->data_urbs_in[i]);
  110. if (test_bit(i, &cdev->outurb_active_mask))
  111. usb_kill_urb(cdev->data_urbs_out[i]);
  112. }
  113. cdev->outurb_active_mask = 0;
  114. }
  115. static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
  116. {
  117. struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
  118. struct device *dev = caiaqdev_to_dev(cdev);
  119. dev_dbg(dev, "%s(%p)\n", __func__, substream);
  120. substream->runtime->hw = cdev->pcm_info;
  121. snd_pcm_limit_hw_rates(substream->runtime);
  122. return 0;
  123. }
  124. static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
  125. {
  126. struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
  127. struct device *dev = caiaqdev_to_dev(cdev);
  128. dev_dbg(dev, "%s(%p)\n", __func__, substream);
  129. if (all_substreams_zero(cdev->sub_playback) &&
  130. all_substreams_zero(cdev->sub_capture)) {
  131. /* when the last client has stopped streaming,
  132. * all sample rates are allowed again */
  133. stream_stop(cdev);
  134. cdev->pcm_info.rates = cdev->samplerates;
  135. }
  136. return 0;
  137. }
  138. static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
  139. {
  140. struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
  141. deactivate_substream(cdev, sub);
  142. return 0;
  143. }
  144. /* this should probably go upstream */
  145. #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
  146. #error "Change this table"
  147. #endif
  148. static const unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  149. 48000, 64000, 88200, 96000, 176400, 192000 };
  150. static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
  151. {
  152. int bytes_per_sample, bpp, ret, i;
  153. int index = substream->number;
  154. struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
  155. struct snd_pcm_runtime *runtime = substream->runtime;
  156. struct device *dev = caiaqdev_to_dev(cdev);
  157. dev_dbg(dev, "%s(%p)\n", __func__, substream);
  158. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  159. int out_pos;
  160. switch (cdev->spec.data_alignment) {
  161. case 0:
  162. case 2:
  163. out_pos = BYTES_PER_SAMPLE + 1;
  164. break;
  165. case 3:
  166. default:
  167. out_pos = 0;
  168. break;
  169. }
  170. cdev->period_out_count[index] = out_pos;
  171. cdev->audio_out_buf_pos[index] = out_pos;
  172. } else {
  173. int in_pos;
  174. switch (cdev->spec.data_alignment) {
  175. case 0:
  176. in_pos = BYTES_PER_SAMPLE + 2;
  177. break;
  178. case 2:
  179. in_pos = BYTES_PER_SAMPLE;
  180. break;
  181. case 3:
  182. default:
  183. in_pos = 0;
  184. break;
  185. }
  186. cdev->period_in_count[index] = in_pos;
  187. cdev->audio_in_buf_pos[index] = in_pos;
  188. }
  189. if (cdev->streaming)
  190. return 0;
  191. /* the first client that opens a stream defines the sample rate
  192. * setting for all subsequent calls, until the last client closed. */
  193. for (i=0; i < ARRAY_SIZE(rates); i++)
  194. if (runtime->rate == rates[i])
  195. cdev->pcm_info.rates = 1 << i;
  196. snd_pcm_limit_hw_rates(runtime);
  197. bytes_per_sample = BYTES_PER_SAMPLE;
  198. if (cdev->spec.data_alignment >= 2)
  199. bytes_per_sample++;
  200. bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
  201. * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
  202. if (bpp > MAX_ENDPOINT_SIZE)
  203. bpp = MAX_ENDPOINT_SIZE;
  204. ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
  205. runtime->sample_bits, bpp);
  206. if (ret)
  207. return ret;
  208. ret = stream_start(cdev);
  209. if (ret)
  210. return ret;
  211. cdev->output_running = 0;
  212. wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
  213. if (!cdev->output_running) {
  214. stream_stop(cdev);
  215. return -EPIPE;
  216. }
  217. return 0;
  218. }
  219. static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
  220. {
  221. struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
  222. struct device *dev = caiaqdev_to_dev(cdev);
  223. dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
  224. switch (cmd) {
  225. case SNDRV_PCM_TRIGGER_START:
  226. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  227. activate_substream(cdev, sub);
  228. break;
  229. case SNDRV_PCM_TRIGGER_STOP:
  230. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  231. deactivate_substream(cdev, sub);
  232. break;
  233. default:
  234. return -EINVAL;
  235. }
  236. return 0;
  237. }
  238. static snd_pcm_uframes_t
  239. snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
  240. {
  241. int index = sub->number;
  242. struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
  243. snd_pcm_uframes_t ptr;
  244. spin_lock(&cdev->spinlock);
  245. if (cdev->input_panic || cdev->output_panic) {
  246. ptr = SNDRV_PCM_POS_XRUN;
  247. goto unlock;
  248. }
  249. if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  250. ptr = bytes_to_frames(sub->runtime,
  251. cdev->audio_out_buf_pos[index]);
  252. else
  253. ptr = bytes_to_frames(sub->runtime,
  254. cdev->audio_in_buf_pos[index]);
  255. unlock:
  256. spin_unlock(&cdev->spinlock);
  257. return ptr;
  258. }
  259. /* operators for both playback and capture */
  260. static const struct snd_pcm_ops snd_usb_caiaq_ops = {
  261. .open = snd_usb_caiaq_substream_open,
  262. .close = snd_usb_caiaq_substream_close,
  263. .hw_free = snd_usb_caiaq_pcm_hw_free,
  264. .prepare = snd_usb_caiaq_pcm_prepare,
  265. .trigger = snd_usb_caiaq_pcm_trigger,
  266. .pointer = snd_usb_caiaq_pcm_pointer,
  267. };
  268. static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
  269. struct snd_pcm_substream **subs)
  270. {
  271. int stream, pb, *cnt;
  272. struct snd_pcm_substream *sub;
  273. for (stream = 0; stream < cdev->n_streams; stream++) {
  274. sub = subs[stream];
  275. if (!sub)
  276. continue;
  277. pb = snd_pcm_lib_period_bytes(sub);
  278. cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  279. &cdev->period_out_count[stream] :
  280. &cdev->period_in_count[stream];
  281. if (*cnt >= pb) {
  282. snd_pcm_period_elapsed(sub);
  283. *cnt %= pb;
  284. }
  285. }
  286. }
  287. static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
  288. const struct urb *urb,
  289. const struct usb_iso_packet_descriptor *iso)
  290. {
  291. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  292. struct snd_pcm_substream *sub;
  293. int stream, i;
  294. if (all_substreams_zero(cdev->sub_capture))
  295. return;
  296. for (i = 0; i < iso->actual_length;) {
  297. for (stream = 0; stream < cdev->n_streams; stream++, i++) {
  298. sub = cdev->sub_capture[stream];
  299. if (sub) {
  300. struct snd_pcm_runtime *rt = sub->runtime;
  301. char *audio_buf = rt->dma_area;
  302. int sz = frames_to_bytes(rt, rt->buffer_size);
  303. audio_buf[cdev->audio_in_buf_pos[stream]++]
  304. = usb_buf[i];
  305. cdev->period_in_count[stream]++;
  306. if (cdev->audio_in_buf_pos[stream] == sz)
  307. cdev->audio_in_buf_pos[stream] = 0;
  308. }
  309. }
  310. }
  311. }
  312. static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
  313. const struct urb *urb,
  314. const struct usb_iso_packet_descriptor *iso)
  315. {
  316. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  317. unsigned char check_byte;
  318. struct snd_pcm_substream *sub;
  319. int stream, i;
  320. for (i = 0; i < iso->actual_length;) {
  321. if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
  322. for (stream = 0;
  323. stream < cdev->n_streams;
  324. stream++, i++) {
  325. if (cdev->first_packet)
  326. continue;
  327. check_byte = MAKE_CHECKBYTE(cdev, stream, i);
  328. if ((usb_buf[i] & 0x3f) != check_byte)
  329. cdev->input_panic = 1;
  330. if (usb_buf[i] & 0x80)
  331. cdev->output_panic = 1;
  332. }
  333. }
  334. cdev->first_packet = 0;
  335. for (stream = 0; stream < cdev->n_streams; stream++, i++) {
  336. sub = cdev->sub_capture[stream];
  337. if (cdev->input_panic)
  338. usb_buf[i] = 0;
  339. if (sub) {
  340. struct snd_pcm_runtime *rt = sub->runtime;
  341. char *audio_buf = rt->dma_area;
  342. int sz = frames_to_bytes(rt, rt->buffer_size);
  343. audio_buf[cdev->audio_in_buf_pos[stream]++] =
  344. usb_buf[i];
  345. cdev->period_in_count[stream]++;
  346. if (cdev->audio_in_buf_pos[stream] == sz)
  347. cdev->audio_in_buf_pos[stream] = 0;
  348. }
  349. }
  350. }
  351. }
  352. static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
  353. const struct urb *urb,
  354. const struct usb_iso_packet_descriptor *iso)
  355. {
  356. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  357. struct device *dev = caiaqdev_to_dev(cdev);
  358. int stream, i;
  359. /* paranoia check */
  360. if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
  361. return;
  362. for (i = 0; i < iso->actual_length;) {
  363. for (stream = 0; stream < cdev->n_streams; stream++) {
  364. struct snd_pcm_substream *sub = cdev->sub_capture[stream];
  365. char *audio_buf = NULL;
  366. int c, n, sz = 0;
  367. if (sub && !cdev->input_panic) {
  368. struct snd_pcm_runtime *rt = sub->runtime;
  369. audio_buf = rt->dma_area;
  370. sz = frames_to_bytes(rt, rt->buffer_size);
  371. }
  372. for (c = 0; c < CHANNELS_PER_STREAM; c++) {
  373. /* 3 audio data bytes, followed by 1 check byte */
  374. if (audio_buf) {
  375. for (n = 0; n < BYTES_PER_SAMPLE; n++) {
  376. audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
  377. if (cdev->audio_in_buf_pos[stream] == sz)
  378. cdev->audio_in_buf_pos[stream] = 0;
  379. }
  380. cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
  381. }
  382. i += BYTES_PER_SAMPLE;
  383. if (usb_buf[i] != ((stream << 1) | c) &&
  384. !cdev->first_packet) {
  385. if (!cdev->input_panic)
  386. dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
  387. ((stream << 1) | c), usb_buf[i], c, stream, i);
  388. cdev->input_panic = 1;
  389. }
  390. i++;
  391. }
  392. }
  393. }
  394. if (cdev->first_packet > 0)
  395. cdev->first_packet--;
  396. }
  397. static void read_in_urb(struct snd_usb_caiaqdev *cdev,
  398. const struct urb *urb,
  399. const struct usb_iso_packet_descriptor *iso)
  400. {
  401. struct device *dev = caiaqdev_to_dev(cdev);
  402. if (!cdev->streaming)
  403. return;
  404. if (iso->actual_length < cdev->bpp)
  405. return;
  406. switch (cdev->spec.data_alignment) {
  407. case 0:
  408. read_in_urb_mode0(cdev, urb, iso);
  409. break;
  410. case 2:
  411. read_in_urb_mode2(cdev, urb, iso);
  412. break;
  413. case 3:
  414. read_in_urb_mode3(cdev, urb, iso);
  415. break;
  416. }
  417. if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
  418. dev_warn(dev, "streaming error detected %s %s\n",
  419. cdev->input_panic ? "(input)" : "",
  420. cdev->output_panic ? "(output)" : "");
  421. cdev->warned = 1;
  422. }
  423. }
  424. static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
  425. struct urb *urb,
  426. const struct usb_iso_packet_descriptor *iso)
  427. {
  428. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  429. struct snd_pcm_substream *sub;
  430. int stream, i;
  431. for (i = 0; i < iso->length;) {
  432. for (stream = 0; stream < cdev->n_streams; stream++, i++) {
  433. sub = cdev->sub_playback[stream];
  434. if (sub) {
  435. struct snd_pcm_runtime *rt = sub->runtime;
  436. char *audio_buf = rt->dma_area;
  437. int sz = frames_to_bytes(rt, rt->buffer_size);
  438. usb_buf[i] =
  439. audio_buf[cdev->audio_out_buf_pos[stream]];
  440. cdev->period_out_count[stream]++;
  441. cdev->audio_out_buf_pos[stream]++;
  442. if (cdev->audio_out_buf_pos[stream] == sz)
  443. cdev->audio_out_buf_pos[stream] = 0;
  444. } else
  445. usb_buf[i] = 0;
  446. }
  447. /* fill in the check bytes */
  448. if (cdev->spec.data_alignment == 2 &&
  449. i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
  450. (cdev->n_streams * CHANNELS_PER_STREAM))
  451. for (stream = 0; stream < cdev->n_streams; stream++, i++)
  452. usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
  453. }
  454. }
  455. static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
  456. struct urb *urb,
  457. const struct usb_iso_packet_descriptor *iso)
  458. {
  459. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  460. int stream, i;
  461. for (i = 0; i < iso->length;) {
  462. for (stream = 0; stream < cdev->n_streams; stream++) {
  463. struct snd_pcm_substream *sub = cdev->sub_playback[stream];
  464. char *audio_buf = NULL;
  465. int c, n, sz = 0;
  466. if (sub) {
  467. struct snd_pcm_runtime *rt = sub->runtime;
  468. audio_buf = rt->dma_area;
  469. sz = frames_to_bytes(rt, rt->buffer_size);
  470. }
  471. for (c = 0; c < CHANNELS_PER_STREAM; c++) {
  472. for (n = 0; n < BYTES_PER_SAMPLE; n++) {
  473. if (audio_buf) {
  474. usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
  475. if (cdev->audio_out_buf_pos[stream] == sz)
  476. cdev->audio_out_buf_pos[stream] = 0;
  477. } else {
  478. usb_buf[i+n] = 0;
  479. }
  480. }
  481. if (audio_buf)
  482. cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
  483. i += BYTES_PER_SAMPLE;
  484. /* fill in the check byte pattern */
  485. usb_buf[i++] = (stream << 1) | c;
  486. }
  487. }
  488. }
  489. }
  490. static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
  491. struct urb *urb,
  492. const struct usb_iso_packet_descriptor *iso)
  493. {
  494. switch (cdev->spec.data_alignment) {
  495. case 0:
  496. case 2:
  497. fill_out_urb_mode_0(cdev, urb, iso);
  498. break;
  499. case 3:
  500. fill_out_urb_mode_3(cdev, urb, iso);
  501. break;
  502. }
  503. }
  504. static void read_completed(struct urb *urb)
  505. {
  506. struct snd_usb_caiaq_cb_info *info = urb->context;
  507. struct snd_usb_caiaqdev *cdev;
  508. struct device *dev;
  509. struct urb *out = NULL;
  510. int i, frame, len, send_it = 0, outframe = 0;
  511. unsigned long flags;
  512. size_t offset = 0;
  513. if (urb->status || !info)
  514. return;
  515. cdev = info->cdev;
  516. dev = caiaqdev_to_dev(cdev);
  517. if (!cdev->streaming)
  518. return;
  519. /* find an unused output urb that is unused */
  520. for (i = 0; i < N_URBS; i++)
  521. if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
  522. out = cdev->data_urbs_out[i];
  523. break;
  524. }
  525. if (!out) {
  526. dev_err(dev, "Unable to find an output urb to use\n");
  527. goto requeue;
  528. }
  529. /* read the recently received packet and send back one which has
  530. * the same layout */
  531. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  532. if (urb->iso_frame_desc[frame].status)
  533. continue;
  534. len = urb->iso_frame_desc[outframe].actual_length;
  535. out->iso_frame_desc[outframe].length = len;
  536. out->iso_frame_desc[outframe].actual_length = 0;
  537. out->iso_frame_desc[outframe].offset = offset;
  538. offset += len;
  539. if (len > 0) {
  540. spin_lock_irqsave(&cdev->spinlock, flags);
  541. fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
  542. read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
  543. spin_unlock_irqrestore(&cdev->spinlock, flags);
  544. check_for_elapsed_periods(cdev, cdev->sub_playback);
  545. check_for_elapsed_periods(cdev, cdev->sub_capture);
  546. send_it = 1;
  547. }
  548. outframe++;
  549. }
  550. if (send_it) {
  551. out->number_of_packets = outframe;
  552. usb_submit_urb(out, GFP_ATOMIC);
  553. } else {
  554. struct snd_usb_caiaq_cb_info *oinfo = out->context;
  555. clear_bit(oinfo->index, &cdev->outurb_active_mask);
  556. }
  557. requeue:
  558. /* re-submit inbound urb */
  559. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  560. urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
  561. urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
  562. urb->iso_frame_desc[frame].actual_length = 0;
  563. }
  564. urb->number_of_packets = FRAMES_PER_URB;
  565. usb_submit_urb(urb, GFP_ATOMIC);
  566. }
  567. static void write_completed(struct urb *urb)
  568. {
  569. struct snd_usb_caiaq_cb_info *info = urb->context;
  570. struct snd_usb_caiaqdev *cdev = info->cdev;
  571. if (!cdev->output_running) {
  572. cdev->output_running = 1;
  573. wake_up(&cdev->prepare_wait_queue);
  574. }
  575. clear_bit(info->index, &cdev->outurb_active_mask);
  576. }
  577. static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
  578. {
  579. int i, frame;
  580. struct urb **urbs;
  581. struct usb_device *usb_dev = cdev->chip.dev;
  582. unsigned int pipe;
  583. pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
  584. usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
  585. usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
  586. urbs = kmalloc_array(N_URBS, sizeof(*urbs), GFP_KERNEL);
  587. if (!urbs) {
  588. *ret = -ENOMEM;
  589. return NULL;
  590. }
  591. for (i = 0; i < N_URBS; i++) {
  592. urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
  593. if (!urbs[i]) {
  594. *ret = -ENOMEM;
  595. return urbs;
  596. }
  597. urbs[i]->transfer_buffer =
  598. kmalloc_array(BYTES_PER_FRAME, FRAMES_PER_URB,
  599. GFP_KERNEL);
  600. if (!urbs[i]->transfer_buffer) {
  601. *ret = -ENOMEM;
  602. return urbs;
  603. }
  604. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  605. struct usb_iso_packet_descriptor *iso =
  606. &urbs[i]->iso_frame_desc[frame];
  607. iso->offset = BYTES_PER_FRAME * frame;
  608. iso->length = BYTES_PER_FRAME;
  609. }
  610. urbs[i]->dev = usb_dev;
  611. urbs[i]->pipe = pipe;
  612. urbs[i]->transfer_buffer_length = FRAMES_PER_URB
  613. * BYTES_PER_FRAME;
  614. urbs[i]->context = &cdev->data_cb_info[i];
  615. urbs[i]->interval = 1;
  616. urbs[i]->number_of_packets = FRAMES_PER_URB;
  617. urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
  618. read_completed : write_completed;
  619. }
  620. *ret = 0;
  621. return urbs;
  622. }
  623. static void free_urbs(struct urb **urbs)
  624. {
  625. int i;
  626. if (!urbs)
  627. return;
  628. for (i = 0; i < N_URBS; i++) {
  629. if (!urbs[i])
  630. continue;
  631. usb_kill_urb(urbs[i]);
  632. kfree(urbs[i]->transfer_buffer);
  633. usb_free_urb(urbs[i]);
  634. }
  635. kfree(urbs);
  636. }
  637. int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
  638. {
  639. int i, ret;
  640. struct device *dev = caiaqdev_to_dev(cdev);
  641. cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
  642. cdev->spec.num_digital_audio_in) /
  643. CHANNELS_PER_STREAM;
  644. cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
  645. cdev->spec.num_digital_audio_out) /
  646. CHANNELS_PER_STREAM;
  647. cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
  648. dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
  649. dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
  650. dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
  651. if (cdev->n_streams > MAX_STREAMS) {
  652. dev_err(dev, "unable to initialize device, too many streams.\n");
  653. return -EINVAL;
  654. }
  655. if (cdev->n_streams < 1) {
  656. dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams);
  657. return -EINVAL;
  658. }
  659. ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
  660. cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
  661. if (ret < 0) {
  662. dev_err(dev, "snd_pcm_new() returned %d\n", ret);
  663. return ret;
  664. }
  665. cdev->pcm->private_data = cdev;
  666. strscpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
  667. memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
  668. memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
  669. memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
  670. sizeof(snd_usb_caiaq_pcm_hardware));
  671. /* setup samplerates */
  672. cdev->samplerates = cdev->pcm_info.rates;
  673. switch (cdev->chip.usb_id) {
  674. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
  675. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
  676. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
  677. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
  678. cdev->samplerates |= SNDRV_PCM_RATE_192000;
  679. fallthrough;
  680. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
  681. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
  682. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
  683. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
  684. cdev->samplerates |= SNDRV_PCM_RATE_88200;
  685. break;
  686. }
  687. snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  688. &snd_usb_caiaq_ops);
  689. snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
  690. &snd_usb_caiaq_ops);
  691. snd_pcm_set_managed_buffer_all(cdev->pcm, SNDRV_DMA_TYPE_VMALLOC,
  692. NULL, 0, 0);
  693. cdev->data_cb_info =
  694. kmalloc_array(N_URBS, sizeof(struct snd_usb_caiaq_cb_info),
  695. GFP_KERNEL);
  696. if (!cdev->data_cb_info)
  697. return -ENOMEM;
  698. cdev->outurb_active_mask = 0;
  699. BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
  700. for (i = 0; i < N_URBS; i++) {
  701. cdev->data_cb_info[i].cdev = cdev;
  702. cdev->data_cb_info[i].index = i;
  703. }
  704. cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
  705. if (ret < 0) {
  706. kfree(cdev->data_cb_info);
  707. free_urbs(cdev->data_urbs_in);
  708. return ret;
  709. }
  710. cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
  711. if (ret < 0) {
  712. kfree(cdev->data_cb_info);
  713. free_urbs(cdev->data_urbs_in);
  714. free_urbs(cdev->data_urbs_out);
  715. return ret;
  716. }
  717. return 0;
  718. }
  719. void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
  720. {
  721. struct device *dev = caiaqdev_to_dev(cdev);
  722. dev_dbg(dev, "%s(%p)\n", __func__, cdev);
  723. stream_stop(cdev);
  724. free_urbs(cdev->data_urbs_in);
  725. free_urbs(cdev->data_urbs_out);
  726. kfree(cdev->data_cb_info);
  727. }