audio.c 18 KB

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