audio.c 22 KB

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