audio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 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. .mmap = snd_pcm_lib_mmap_vmalloc,
  290. };
  291. static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
  292. struct snd_pcm_substream **subs)
  293. {
  294. int stream, pb, *cnt;
  295. struct snd_pcm_substream *sub;
  296. for (stream = 0; stream < cdev->n_streams; stream++) {
  297. sub = subs[stream];
  298. if (!sub)
  299. continue;
  300. pb = snd_pcm_lib_period_bytes(sub);
  301. cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  302. &cdev->period_out_count[stream] :
  303. &cdev->period_in_count[stream];
  304. if (*cnt >= pb) {
  305. snd_pcm_period_elapsed(sub);
  306. *cnt %= pb;
  307. }
  308. }
  309. }
  310. static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
  311. const struct urb *urb,
  312. const struct usb_iso_packet_descriptor *iso)
  313. {
  314. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  315. struct snd_pcm_substream *sub;
  316. int stream, i;
  317. if (all_substreams_zero(cdev->sub_capture))
  318. return;
  319. for (i = 0; i < iso->actual_length;) {
  320. for (stream = 0; stream < cdev->n_streams; stream++, i++) {
  321. sub = cdev->sub_capture[stream];
  322. if (sub) {
  323. struct snd_pcm_runtime *rt = sub->runtime;
  324. char *audio_buf = rt->dma_area;
  325. int sz = frames_to_bytes(rt, rt->buffer_size);
  326. audio_buf[cdev->audio_in_buf_pos[stream]++]
  327. = usb_buf[i];
  328. cdev->period_in_count[stream]++;
  329. if (cdev->audio_in_buf_pos[stream] == sz)
  330. cdev->audio_in_buf_pos[stream] = 0;
  331. }
  332. }
  333. }
  334. }
  335. static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
  336. const struct urb *urb,
  337. const struct usb_iso_packet_descriptor *iso)
  338. {
  339. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  340. unsigned char check_byte;
  341. struct snd_pcm_substream *sub;
  342. int stream, i;
  343. for (i = 0; i < iso->actual_length;) {
  344. if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
  345. for (stream = 0;
  346. stream < cdev->n_streams;
  347. stream++, i++) {
  348. if (cdev->first_packet)
  349. continue;
  350. check_byte = MAKE_CHECKBYTE(cdev, stream, i);
  351. if ((usb_buf[i] & 0x3f) != check_byte)
  352. cdev->input_panic = 1;
  353. if (usb_buf[i] & 0x80)
  354. cdev->output_panic = 1;
  355. }
  356. }
  357. cdev->first_packet = 0;
  358. for (stream = 0; stream < cdev->n_streams; stream++, i++) {
  359. sub = cdev->sub_capture[stream];
  360. if (cdev->input_panic)
  361. usb_buf[i] = 0;
  362. if (sub) {
  363. struct snd_pcm_runtime *rt = sub->runtime;
  364. char *audio_buf = rt->dma_area;
  365. int sz = frames_to_bytes(rt, rt->buffer_size);
  366. audio_buf[cdev->audio_in_buf_pos[stream]++] =
  367. usb_buf[i];
  368. cdev->period_in_count[stream]++;
  369. if (cdev->audio_in_buf_pos[stream] == sz)
  370. cdev->audio_in_buf_pos[stream] = 0;
  371. }
  372. }
  373. }
  374. }
  375. static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
  376. const struct urb *urb,
  377. const struct usb_iso_packet_descriptor *iso)
  378. {
  379. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  380. struct device *dev = caiaqdev_to_dev(cdev);
  381. int stream, i;
  382. /* paranoia check */
  383. if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
  384. return;
  385. for (i = 0; i < iso->actual_length;) {
  386. for (stream = 0; stream < cdev->n_streams; stream++) {
  387. struct snd_pcm_substream *sub = cdev->sub_capture[stream];
  388. char *audio_buf = NULL;
  389. int c, n, sz = 0;
  390. if (sub && !cdev->input_panic) {
  391. struct snd_pcm_runtime *rt = sub->runtime;
  392. audio_buf = rt->dma_area;
  393. sz = frames_to_bytes(rt, rt->buffer_size);
  394. }
  395. for (c = 0; c < CHANNELS_PER_STREAM; c++) {
  396. /* 3 audio data bytes, followed by 1 check byte */
  397. if (audio_buf) {
  398. for (n = 0; n < BYTES_PER_SAMPLE; n++) {
  399. audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
  400. if (cdev->audio_in_buf_pos[stream] == sz)
  401. cdev->audio_in_buf_pos[stream] = 0;
  402. }
  403. cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
  404. }
  405. i += BYTES_PER_SAMPLE;
  406. if (usb_buf[i] != ((stream << 1) | c) &&
  407. !cdev->first_packet) {
  408. if (!cdev->input_panic)
  409. dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
  410. ((stream << 1) | c), usb_buf[i], c, stream, i);
  411. cdev->input_panic = 1;
  412. }
  413. i++;
  414. }
  415. }
  416. }
  417. if (cdev->first_packet > 0)
  418. cdev->first_packet--;
  419. }
  420. static void read_in_urb(struct snd_usb_caiaqdev *cdev,
  421. const struct urb *urb,
  422. const struct usb_iso_packet_descriptor *iso)
  423. {
  424. struct device *dev = caiaqdev_to_dev(cdev);
  425. if (!cdev->streaming)
  426. return;
  427. if (iso->actual_length < cdev->bpp)
  428. return;
  429. switch (cdev->spec.data_alignment) {
  430. case 0:
  431. read_in_urb_mode0(cdev, urb, iso);
  432. break;
  433. case 2:
  434. read_in_urb_mode2(cdev, urb, iso);
  435. break;
  436. case 3:
  437. read_in_urb_mode3(cdev, urb, iso);
  438. break;
  439. }
  440. if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
  441. dev_warn(dev, "streaming error detected %s %s\n",
  442. cdev->input_panic ? "(input)" : "",
  443. cdev->output_panic ? "(output)" : "");
  444. cdev->warned = 1;
  445. }
  446. }
  447. static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
  448. struct urb *urb,
  449. const struct usb_iso_packet_descriptor *iso)
  450. {
  451. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  452. struct snd_pcm_substream *sub;
  453. int stream, i;
  454. for (i = 0; i < iso->length;) {
  455. for (stream = 0; stream < cdev->n_streams; stream++, i++) {
  456. sub = cdev->sub_playback[stream];
  457. if (sub) {
  458. struct snd_pcm_runtime *rt = sub->runtime;
  459. char *audio_buf = rt->dma_area;
  460. int sz = frames_to_bytes(rt, rt->buffer_size);
  461. usb_buf[i] =
  462. audio_buf[cdev->audio_out_buf_pos[stream]];
  463. cdev->period_out_count[stream]++;
  464. cdev->audio_out_buf_pos[stream]++;
  465. if (cdev->audio_out_buf_pos[stream] == sz)
  466. cdev->audio_out_buf_pos[stream] = 0;
  467. } else
  468. usb_buf[i] = 0;
  469. }
  470. /* fill in the check bytes */
  471. if (cdev->spec.data_alignment == 2 &&
  472. i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
  473. (cdev->n_streams * CHANNELS_PER_STREAM))
  474. for (stream = 0; stream < cdev->n_streams; stream++, i++)
  475. usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
  476. }
  477. }
  478. static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
  479. struct urb *urb,
  480. const struct usb_iso_packet_descriptor *iso)
  481. {
  482. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  483. int stream, i;
  484. for (i = 0; i < iso->length;) {
  485. for (stream = 0; stream < cdev->n_streams; stream++) {
  486. struct snd_pcm_substream *sub = cdev->sub_playback[stream];
  487. char *audio_buf = NULL;
  488. int c, n, sz = 0;
  489. if (sub) {
  490. struct snd_pcm_runtime *rt = sub->runtime;
  491. audio_buf = rt->dma_area;
  492. sz = frames_to_bytes(rt, rt->buffer_size);
  493. }
  494. for (c = 0; c < CHANNELS_PER_STREAM; c++) {
  495. for (n = 0; n < BYTES_PER_SAMPLE; n++) {
  496. if (audio_buf) {
  497. usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
  498. if (cdev->audio_out_buf_pos[stream] == sz)
  499. cdev->audio_out_buf_pos[stream] = 0;
  500. } else {
  501. usb_buf[i+n] = 0;
  502. }
  503. }
  504. if (audio_buf)
  505. cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
  506. i += BYTES_PER_SAMPLE;
  507. /* fill in the check byte pattern */
  508. usb_buf[i++] = (stream << 1) | c;
  509. }
  510. }
  511. }
  512. }
  513. static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
  514. struct urb *urb,
  515. const struct usb_iso_packet_descriptor *iso)
  516. {
  517. switch (cdev->spec.data_alignment) {
  518. case 0:
  519. case 2:
  520. fill_out_urb_mode_0(cdev, urb, iso);
  521. break;
  522. case 3:
  523. fill_out_urb_mode_3(cdev, urb, iso);
  524. break;
  525. }
  526. }
  527. static void read_completed(struct urb *urb)
  528. {
  529. struct snd_usb_caiaq_cb_info *info = urb->context;
  530. struct snd_usb_caiaqdev *cdev;
  531. struct device *dev;
  532. struct urb *out = NULL;
  533. int i, frame, len, send_it = 0, outframe = 0;
  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(&cdev->spinlock);
  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(&cdev->spinlock);
  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. struct device *dev = caiaqdev_to_dev(cdev);
  605. unsigned int pipe;
  606. pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
  607. usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
  608. usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
  609. urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
  610. if (!urbs) {
  611. dev_err(dev, "unable to kmalloc() urbs, OOM!?\n");
  612. *ret = -ENOMEM;
  613. return NULL;
  614. }
  615. for (i = 0; i < N_URBS; i++) {
  616. urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
  617. if (!urbs[i]) {
  618. dev_err(dev, "unable to usb_alloc_urb(), OOM!?\n");
  619. *ret = -ENOMEM;
  620. return urbs;
  621. }
  622. urbs[i]->transfer_buffer =
  623. kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
  624. if (!urbs[i]->transfer_buffer) {
  625. dev_err(dev, "unable to kmalloc() transfer buffer, OOM!?\n");
  626. *ret = -ENOMEM;
  627. return urbs;
  628. }
  629. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  630. struct usb_iso_packet_descriptor *iso =
  631. &urbs[i]->iso_frame_desc[frame];
  632. iso->offset = BYTES_PER_FRAME * frame;
  633. iso->length = BYTES_PER_FRAME;
  634. }
  635. urbs[i]->dev = usb_dev;
  636. urbs[i]->pipe = pipe;
  637. urbs[i]->transfer_buffer_length = FRAMES_PER_URB
  638. * BYTES_PER_FRAME;
  639. urbs[i]->context = &cdev->data_cb_info[i];
  640. urbs[i]->interval = 1;
  641. urbs[i]->number_of_packets = FRAMES_PER_URB;
  642. urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
  643. read_completed : write_completed;
  644. }
  645. *ret = 0;
  646. return urbs;
  647. }
  648. static void free_urbs(struct urb **urbs)
  649. {
  650. int i;
  651. if (!urbs)
  652. return;
  653. for (i = 0; i < N_URBS; i++) {
  654. if (!urbs[i])
  655. continue;
  656. usb_kill_urb(urbs[i]);
  657. kfree(urbs[i]->transfer_buffer);
  658. usb_free_urb(urbs[i]);
  659. }
  660. kfree(urbs);
  661. }
  662. int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
  663. {
  664. int i, ret;
  665. struct device *dev = caiaqdev_to_dev(cdev);
  666. cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
  667. cdev->spec.num_digital_audio_in) /
  668. CHANNELS_PER_STREAM;
  669. cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
  670. cdev->spec.num_digital_audio_out) /
  671. CHANNELS_PER_STREAM;
  672. cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
  673. dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
  674. dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
  675. dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
  676. if (cdev->n_streams > MAX_STREAMS) {
  677. dev_err(dev, "unable to initialize device, too many streams.\n");
  678. return -EINVAL;
  679. }
  680. if (cdev->n_streams < 1) {
  681. dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams);
  682. return -EINVAL;
  683. }
  684. ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
  685. cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
  686. if (ret < 0) {
  687. dev_err(dev, "snd_pcm_new() returned %d\n", ret);
  688. return ret;
  689. }
  690. cdev->pcm->private_data = cdev;
  691. strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
  692. memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
  693. memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
  694. memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
  695. sizeof(snd_usb_caiaq_pcm_hardware));
  696. /* setup samplerates */
  697. cdev->samplerates = cdev->pcm_info.rates;
  698. switch (cdev->chip.usb_id) {
  699. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
  700. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
  701. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
  702. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
  703. cdev->samplerates |= SNDRV_PCM_RATE_192000;
  704. /* fall thru */
  705. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
  706. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
  707. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
  708. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
  709. cdev->samplerates |= SNDRV_PCM_RATE_88200;
  710. break;
  711. }
  712. snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  713. &snd_usb_caiaq_ops);
  714. snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
  715. &snd_usb_caiaq_ops);
  716. cdev->data_cb_info =
  717. kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
  718. GFP_KERNEL);
  719. if (!cdev->data_cb_info)
  720. return -ENOMEM;
  721. cdev->outurb_active_mask = 0;
  722. BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
  723. for (i = 0; i < N_URBS; i++) {
  724. cdev->data_cb_info[i].cdev = cdev;
  725. cdev->data_cb_info[i].index = i;
  726. }
  727. cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
  728. if (ret < 0) {
  729. kfree(cdev->data_cb_info);
  730. free_urbs(cdev->data_urbs_in);
  731. return ret;
  732. }
  733. cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
  734. if (ret < 0) {
  735. kfree(cdev->data_cb_info);
  736. free_urbs(cdev->data_urbs_in);
  737. free_urbs(cdev->data_urbs_out);
  738. return ret;
  739. }
  740. return 0;
  741. }
  742. void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
  743. {
  744. struct device *dev = caiaqdev_to_dev(cdev);
  745. dev_dbg(dev, "%s(%p)\n", __func__, cdev);
  746. stream_stop(cdev);
  747. free_urbs(cdev->data_urbs_in);
  748. free_urbs(cdev->data_urbs_out);
  749. kfree(cdev->data_cb_info);
  750. }