audio.c 23 KB

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