PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/usb/caiaq/audio.c

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