audio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. 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(%p)\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(%p)\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(%p)\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(%p)\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(%p)\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(%p) 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. if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  260. ptr = bytes_to_frames(sub->runtime,
  261. dev->audio_out_buf_pos[index]);
  262. else
  263. ptr = bytes_to_frames(sub->runtime,
  264. dev->audio_in_buf_pos[index]);
  265. spin_unlock(&dev->spinlock);
  266. return ptr;
  267. }
  268. /* operators for both playback and capture */
  269. static struct snd_pcm_ops snd_usb_caiaq_ops = {
  270. .open = snd_usb_caiaq_substream_open,
  271. .close = snd_usb_caiaq_substream_close,
  272. .ioctl = snd_pcm_lib_ioctl,
  273. .hw_params = snd_usb_caiaq_pcm_hw_params,
  274. .hw_free = snd_usb_caiaq_pcm_hw_free,
  275. .prepare = snd_usb_caiaq_pcm_prepare,
  276. .trigger = snd_usb_caiaq_pcm_trigger,
  277. .pointer = snd_usb_caiaq_pcm_pointer
  278. };
  279. static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev,
  280. struct snd_pcm_substream **subs)
  281. {
  282. int stream, pb, *cnt;
  283. struct snd_pcm_substream *sub;
  284. for (stream = 0; stream < dev->n_streams; stream++) {
  285. sub = subs[stream];
  286. if (!sub)
  287. continue;
  288. pb = snd_pcm_lib_period_bytes(sub);
  289. cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  290. &dev->period_out_count[stream] :
  291. &dev->period_in_count[stream];
  292. if (*cnt >= pb) {
  293. snd_pcm_period_elapsed(sub);
  294. *cnt %= pb;
  295. }
  296. }
  297. }
  298. static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev,
  299. const struct urb *urb,
  300. const struct usb_iso_packet_descriptor *iso)
  301. {
  302. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  303. struct snd_pcm_substream *sub;
  304. int stream, i;
  305. if (all_substreams_zero(dev->sub_capture))
  306. return;
  307. for (i = 0; i < iso->actual_length;) {
  308. for (stream = 0; stream < dev->n_streams; stream++, i++) {
  309. sub = dev->sub_capture[stream];
  310. if (sub) {
  311. struct snd_pcm_runtime *rt = sub->runtime;
  312. char *audio_buf = rt->dma_area;
  313. int sz = frames_to_bytes(rt, rt->buffer_size);
  314. audio_buf[dev->audio_in_buf_pos[stream]++]
  315. = usb_buf[i];
  316. dev->period_in_count[stream]++;
  317. if (dev->audio_in_buf_pos[stream] == sz)
  318. dev->audio_in_buf_pos[stream] = 0;
  319. }
  320. }
  321. }
  322. }
  323. static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev,
  324. const struct urb *urb,
  325. const struct usb_iso_packet_descriptor *iso)
  326. {
  327. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  328. unsigned char check_byte;
  329. struct snd_pcm_substream *sub;
  330. int stream, i;
  331. for (i = 0; i < iso->actual_length;) {
  332. if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
  333. for (stream = 0;
  334. stream < dev->n_streams;
  335. stream++, i++) {
  336. if (dev->first_packet)
  337. continue;
  338. check_byte = MAKE_CHECKBYTE(dev, stream, i);
  339. if ((usb_buf[i] & 0x3f) != check_byte)
  340. dev->input_panic = 1;
  341. if (usb_buf[i] & 0x80)
  342. dev->output_panic = 1;
  343. }
  344. }
  345. dev->first_packet = 0;
  346. for (stream = 0; stream < dev->n_streams; stream++, i++) {
  347. sub = dev->sub_capture[stream];
  348. if (dev->input_panic)
  349. usb_buf[i] = 0;
  350. if (sub) {
  351. struct snd_pcm_runtime *rt = sub->runtime;
  352. char *audio_buf = rt->dma_area;
  353. int sz = frames_to_bytes(rt, rt->buffer_size);
  354. audio_buf[dev->audio_in_buf_pos[stream]++] =
  355. usb_buf[i];
  356. dev->period_in_count[stream]++;
  357. if (dev->audio_in_buf_pos[stream] == sz)
  358. dev->audio_in_buf_pos[stream] = 0;
  359. }
  360. }
  361. }
  362. }
  363. static void read_in_urb_mode3(struct snd_usb_caiaqdev *dev,
  364. const struct urb *urb,
  365. const struct usb_iso_packet_descriptor *iso)
  366. {
  367. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  368. int stream, i;
  369. /* paranoia check */
  370. if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
  371. return;
  372. for (i = 0; i < iso->actual_length;) {
  373. for (stream = 0; stream < dev->n_streams; stream++) {
  374. struct snd_pcm_substream *sub = dev->sub_capture[stream];
  375. char *audio_buf = NULL;
  376. int c, n, sz = 0;
  377. if (sub && !dev->input_panic) {
  378. struct snd_pcm_runtime *rt = sub->runtime;
  379. audio_buf = rt->dma_area;
  380. sz = frames_to_bytes(rt, rt->buffer_size);
  381. }
  382. for (c = 0; c < CHANNELS_PER_STREAM; c++) {
  383. /* 3 audio data bytes, followed by 1 check byte */
  384. if (audio_buf) {
  385. for (n = 0; n < BYTES_PER_SAMPLE; n++) {
  386. audio_buf[dev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
  387. if (dev->audio_in_buf_pos[stream] == sz)
  388. dev->audio_in_buf_pos[stream] = 0;
  389. }
  390. dev->period_in_count[stream] += BYTES_PER_SAMPLE;
  391. }
  392. i += BYTES_PER_SAMPLE;
  393. if (usb_buf[i] != ((stream << 1) | c) &&
  394. !dev->first_packet) {
  395. if (!dev->input_panic)
  396. printk(" EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
  397. ((stream << 1) | c), usb_buf[i], c, stream, i);
  398. dev->input_panic = 1;
  399. }
  400. i++;
  401. }
  402. }
  403. }
  404. if (dev->first_packet > 0)
  405. dev->first_packet--;
  406. }
  407. static void read_in_urb(struct snd_usb_caiaqdev *dev,
  408. const struct urb *urb,
  409. const struct usb_iso_packet_descriptor *iso)
  410. {
  411. if (!dev->streaming)
  412. return;
  413. if (iso->actual_length < dev->bpp)
  414. return;
  415. switch (dev->spec.data_alignment) {
  416. case 0:
  417. read_in_urb_mode0(dev, urb, iso);
  418. break;
  419. case 2:
  420. read_in_urb_mode2(dev, urb, iso);
  421. break;
  422. case 3:
  423. read_in_urb_mode3(dev, urb, iso);
  424. break;
  425. }
  426. if ((dev->input_panic || dev->output_panic) && !dev->warned) {
  427. debug("streaming error detected %s %s\n",
  428. dev->input_panic ? "(input)" : "",
  429. dev->output_panic ? "(output)" : "");
  430. dev->warned = 1;
  431. }
  432. }
  433. static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *dev,
  434. struct urb *urb,
  435. const struct usb_iso_packet_descriptor *iso)
  436. {
  437. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  438. struct snd_pcm_substream *sub;
  439. int stream, i;
  440. for (i = 0; i < iso->length;) {
  441. for (stream = 0; stream < dev->n_streams; stream++, i++) {
  442. sub = dev->sub_playback[stream];
  443. if (sub) {
  444. struct snd_pcm_runtime *rt = sub->runtime;
  445. char *audio_buf = rt->dma_area;
  446. int sz = frames_to_bytes(rt, rt->buffer_size);
  447. usb_buf[i] =
  448. audio_buf[dev->audio_out_buf_pos[stream]];
  449. dev->period_out_count[stream]++;
  450. dev->audio_out_buf_pos[stream]++;
  451. if (dev->audio_out_buf_pos[stream] == sz)
  452. dev->audio_out_buf_pos[stream] = 0;
  453. } else
  454. usb_buf[i] = 0;
  455. }
  456. /* fill in the check bytes */
  457. if (dev->spec.data_alignment == 2 &&
  458. i % (dev->n_streams * BYTES_PER_SAMPLE_USB) ==
  459. (dev->n_streams * CHANNELS_PER_STREAM))
  460. for (stream = 0; stream < dev->n_streams; stream++, i++)
  461. usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i);
  462. }
  463. }
  464. static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *dev,
  465. struct urb *urb,
  466. const struct usb_iso_packet_descriptor *iso)
  467. {
  468. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  469. int stream, i;
  470. for (i = 0; i < iso->length;) {
  471. for (stream = 0; stream < dev->n_streams; stream++) {
  472. struct snd_pcm_substream *sub = dev->sub_playback[stream];
  473. char *audio_buf = NULL;
  474. int c, n, sz = 0;
  475. if (sub) {
  476. struct snd_pcm_runtime *rt = sub->runtime;
  477. audio_buf = rt->dma_area;
  478. sz = frames_to_bytes(rt, rt->buffer_size);
  479. }
  480. for (c = 0; c < CHANNELS_PER_STREAM; c++) {
  481. for (n = 0; n < BYTES_PER_SAMPLE; n++) {
  482. if (audio_buf) {
  483. usb_buf[i+n] = audio_buf[dev->audio_out_buf_pos[stream]++];
  484. if (dev->audio_out_buf_pos[stream] == sz)
  485. dev->audio_out_buf_pos[stream] = 0;
  486. } else {
  487. usb_buf[i+n] = 0;
  488. }
  489. }
  490. if (audio_buf)
  491. dev->period_out_count[stream] += BYTES_PER_SAMPLE;
  492. i += BYTES_PER_SAMPLE;
  493. /* fill in the check byte pattern */
  494. usb_buf[i++] = (stream << 1) | c;
  495. }
  496. }
  497. }
  498. }
  499. static inline void fill_out_urb(struct snd_usb_caiaqdev *dev,
  500. struct urb *urb,
  501. const struct usb_iso_packet_descriptor *iso)
  502. {
  503. switch (dev->spec.data_alignment) {
  504. case 0:
  505. case 2:
  506. fill_out_urb_mode_0(dev, urb, iso);
  507. break;
  508. case 3:
  509. fill_out_urb_mode_3(dev, urb, iso);
  510. break;
  511. }
  512. }
  513. static void read_completed(struct urb *urb)
  514. {
  515. struct snd_usb_caiaq_cb_info *info = urb->context;
  516. struct snd_usb_caiaqdev *dev;
  517. struct urb *out = NULL;
  518. int i, frame, len, send_it = 0, outframe = 0;
  519. size_t offset = 0;
  520. if (urb->status || !info)
  521. return;
  522. dev = info->dev;
  523. if (!dev->streaming)
  524. return;
  525. /* find an unused output urb that is unused */
  526. for (i = 0; i < N_URBS; i++)
  527. if (test_and_set_bit(i, &dev->outurb_active_mask) == 0) {
  528. out = dev->data_urbs_out[i];
  529. break;
  530. }
  531. if (!out) {
  532. log("Unable to find an output urb to use\n");
  533. goto requeue;
  534. }
  535. /* read the recently received packet and send back one which has
  536. * the same layout */
  537. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  538. if (urb->iso_frame_desc[frame].status)
  539. continue;
  540. len = urb->iso_frame_desc[outframe].actual_length;
  541. out->iso_frame_desc[outframe].length = len;
  542. out->iso_frame_desc[outframe].actual_length = 0;
  543. out->iso_frame_desc[outframe].offset = offset;
  544. offset += len;
  545. if (len > 0) {
  546. spin_lock(&dev->spinlock);
  547. fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
  548. read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
  549. spin_unlock(&dev->spinlock);
  550. check_for_elapsed_periods(dev, dev->sub_playback);
  551. check_for_elapsed_periods(dev, dev->sub_capture);
  552. send_it = 1;
  553. }
  554. outframe++;
  555. }
  556. if (send_it) {
  557. out->number_of_packets = outframe;
  558. out->transfer_flags = URB_ISO_ASAP;
  559. usb_submit_urb(out, GFP_ATOMIC);
  560. } else {
  561. struct snd_usb_caiaq_cb_info *oinfo = out->context;
  562. clear_bit(oinfo->index, &dev->outurb_active_mask);
  563. }
  564. requeue:
  565. /* re-submit inbound urb */
  566. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  567. urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
  568. urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
  569. urb->iso_frame_desc[frame].actual_length = 0;
  570. }
  571. urb->number_of_packets = FRAMES_PER_URB;
  572. urb->transfer_flags = URB_ISO_ASAP;
  573. usb_submit_urb(urb, GFP_ATOMIC);
  574. }
  575. static void write_completed(struct urb *urb)
  576. {
  577. struct snd_usb_caiaq_cb_info *info = urb->context;
  578. struct snd_usb_caiaqdev *dev = info->dev;
  579. if (!dev->output_running) {
  580. dev->output_running = 1;
  581. wake_up(&dev->prepare_wait_queue);
  582. }
  583. clear_bit(info->index, &dev->outurb_active_mask);
  584. }
  585. static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
  586. {
  587. int i, frame;
  588. struct urb **urbs;
  589. struct usb_device *usb_dev = dev->chip.dev;
  590. unsigned int pipe;
  591. pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
  592. usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
  593. usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
  594. urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
  595. if (!urbs) {
  596. log("unable to kmalloc() urbs, OOM!?\n");
  597. *ret = -ENOMEM;
  598. return NULL;
  599. }
  600. for (i = 0; i < N_URBS; i++) {
  601. urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
  602. if (!urbs[i]) {
  603. log("unable to usb_alloc_urb(), OOM!?\n");
  604. *ret = -ENOMEM;
  605. return urbs;
  606. }
  607. urbs[i]->transfer_buffer =
  608. kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
  609. if (!urbs[i]->transfer_buffer) {
  610. log("unable to kmalloc() transfer buffer, OOM!?\n");
  611. *ret = -ENOMEM;
  612. return urbs;
  613. }
  614. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  615. struct usb_iso_packet_descriptor *iso =
  616. &urbs[i]->iso_frame_desc[frame];
  617. iso->offset = BYTES_PER_FRAME * frame;
  618. iso->length = BYTES_PER_FRAME;
  619. }
  620. urbs[i]->dev = usb_dev;
  621. urbs[i]->pipe = pipe;
  622. urbs[i]->transfer_buffer_length = FRAMES_PER_URB
  623. * BYTES_PER_FRAME;
  624. urbs[i]->context = &dev->data_cb_info[i];
  625. urbs[i]->interval = 1;
  626. urbs[i]->transfer_flags = URB_ISO_ASAP;
  627. urbs[i]->number_of_packets = FRAMES_PER_URB;
  628. urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
  629. read_completed : write_completed;
  630. }
  631. *ret = 0;
  632. return urbs;
  633. }
  634. static void free_urbs(struct urb **urbs)
  635. {
  636. int i;
  637. if (!urbs)
  638. return;
  639. for (i = 0; i < N_URBS; i++) {
  640. if (!urbs[i])
  641. continue;
  642. usb_kill_urb(urbs[i]);
  643. kfree(urbs[i]->transfer_buffer);
  644. usb_free_urb(urbs[i]);
  645. }
  646. kfree(urbs);
  647. }
  648. int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
  649. {
  650. int i, ret;
  651. dev->n_audio_in = max(dev->spec.num_analog_audio_in,
  652. dev->spec.num_digital_audio_in) /
  653. CHANNELS_PER_STREAM;
  654. dev->n_audio_out = max(dev->spec.num_analog_audio_out,
  655. dev->spec.num_digital_audio_out) /
  656. CHANNELS_PER_STREAM;
  657. dev->n_streams = max(dev->n_audio_in, dev->n_audio_out);
  658. debug("dev->n_audio_in = %d\n", dev->n_audio_in);
  659. debug("dev->n_audio_out = %d\n", dev->n_audio_out);
  660. debug("dev->n_streams = %d\n", dev->n_streams);
  661. if (dev->n_streams > MAX_STREAMS) {
  662. log("unable to initialize device, too many streams.\n");
  663. return -EINVAL;
  664. }
  665. ret = snd_pcm_new(dev->chip.card, dev->product_name, 0,
  666. dev->n_audio_out, dev->n_audio_in, &dev->pcm);
  667. if (ret < 0) {
  668. log("snd_pcm_new() returned %d\n", ret);
  669. return ret;
  670. }
  671. dev->pcm->private_data = dev;
  672. strlcpy(dev->pcm->name, dev->product_name, sizeof(dev->pcm->name));
  673. memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
  674. memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
  675. memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware,
  676. sizeof(snd_usb_caiaq_pcm_hardware));
  677. /* setup samplerates */
  678. dev->samplerates = dev->pcm_info.rates;
  679. switch (dev->chip.usb_id) {
  680. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
  681. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
  682. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
  683. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
  684. dev->samplerates |= SNDRV_PCM_RATE_192000;
  685. /* fall thru */
  686. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
  687. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
  688. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
  689. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
  690. dev->samplerates |= SNDRV_PCM_RATE_88200;
  691. break;
  692. }
  693. snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
  694. &snd_usb_caiaq_ops);
  695. snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
  696. &snd_usb_caiaq_ops);
  697. snd_pcm_lib_preallocate_pages_for_all(dev->pcm,
  698. SNDRV_DMA_TYPE_CONTINUOUS,
  699. snd_dma_continuous_data(GFP_KERNEL),
  700. MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
  701. dev->data_cb_info =
  702. kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
  703. GFP_KERNEL);
  704. if (!dev->data_cb_info)
  705. return -ENOMEM;
  706. dev->outurb_active_mask = 0;
  707. BUILD_BUG_ON(N_URBS > (sizeof(dev->outurb_active_mask) * 8));
  708. for (i = 0; i < N_URBS; i++) {
  709. dev->data_cb_info[i].dev = dev;
  710. dev->data_cb_info[i].index = i;
  711. }
  712. dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret);
  713. if (ret < 0) {
  714. kfree(dev->data_cb_info);
  715. free_urbs(dev->data_urbs_in);
  716. return ret;
  717. }
  718. dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
  719. if (ret < 0) {
  720. kfree(dev->data_cb_info);
  721. free_urbs(dev->data_urbs_in);
  722. free_urbs(dev->data_urbs_out);
  723. return ret;
  724. }
  725. return 0;
  726. }
  727. void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev)
  728. {
  729. debug("%s(%p)\n", __func__, dev);
  730. stream_stop(dev);
  731. free_urbs(dev->data_urbs_in);
  732. free_urbs(dev->data_urbs_out);
  733. kfree(dev->data_cb_info);
  734. }