caiaq-audio.c 18 KB

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