audio.c 18 KB

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