caiaq-audio.c 18 KB

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