caiaq-audio.c 18 KB

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