caiaq-audio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * Copyright (c) 2006,2007 Daniel Mack, Karsten Wiese
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/usb.h>
  23. #include <linux/spinlock.h>
  24. #include <sound/core.h>
  25. #include <sound/initval.h>
  26. #include <sound/pcm.h>
  27. #include <sound/rawmidi.h>
  28. #include <linux/input.h>
  29. #include "caiaq-device.h"
  30. #include "caiaq-audio.h"
  31. #define N_URBS 32
  32. #define CLOCK_DRIFT_TOLERANCE 5
  33. #define FRAMES_PER_URB 8
  34. #define BYTES_PER_FRAME 512
  35. #define CHANNELS_PER_STREAM 2
  36. #define BYTES_PER_SAMPLE 3
  37. #define BYTES_PER_SAMPLE_USB 4
  38. #define MAX_BUFFER_SIZE (128*1024)
  39. #define ENDPOINT_CAPTURE 2
  40. #define ENDPOINT_PLAYBACK 6
  41. #define MAKE_CHECKBYTE(dev,stream,i) \
  42. (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
  43. static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
  44. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  45. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  46. .formats = SNDRV_PCM_FMTBIT_S24_3BE,
  47. .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  48. SNDRV_PCM_RATE_96000),
  49. .rate_min = 44100,
  50. .rate_max = 0, /* will overwrite later */
  51. .channels_min = CHANNELS_PER_STREAM,
  52. .channels_max = CHANNELS_PER_STREAM,
  53. .buffer_bytes_max = MAX_BUFFER_SIZE,
  54. .period_bytes_min = 128,
  55. .period_bytes_max = MAX_BUFFER_SIZE,
  56. .periods_min = 1,
  57. .periods_max = 1024,
  58. };
  59. static void
  60. activate_substream(struct snd_usb_caiaqdev *dev,
  61. struct snd_pcm_substream *sub)
  62. {
  63. if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  64. dev->sub_playback[sub->number] = sub;
  65. else
  66. dev->sub_capture[sub->number] = sub;
  67. }
  68. static void
  69. deactivate_substream(struct snd_usb_caiaqdev *dev,
  70. struct snd_pcm_substream *sub)
  71. {
  72. if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  73. dev->sub_playback[sub->number] = NULL;
  74. else
  75. dev->sub_capture[sub->number] = NULL;
  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("stream_start(%p)\n", dev);
  90. spin_lock_irq(&dev->spinlock);
  91. if (dev->streaming) {
  92. spin_unlock_irq(&dev->spinlock);
  93. return -EINVAL;
  94. }
  95. dev->input_panic = 0;
  96. dev->output_panic = 0;
  97. dev->first_packet = 1;
  98. dev->streaming = 1;
  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 initial read #%d! (ret = %d)\n",
  103. i, ret);
  104. dev->streaming = 0;
  105. spin_unlock_irq(&dev->spinlock);
  106. return -EPIPE;
  107. }
  108. }
  109. spin_unlock_irq(&dev->spinlock);
  110. return 0;
  111. }
  112. static void stream_stop(struct snd_usb_caiaqdev *dev)
  113. {
  114. int i;
  115. debug("stream_stop(%p)\n", dev);
  116. if (!dev->streaming)
  117. return;
  118. dev->streaming = 0;
  119. for (i = 0; i < N_URBS; i++) {
  120. usb_unlink_urb(dev->data_urbs_in[i]);
  121. usb_unlink_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("snd_usb_caiaq_substream_open(%p)\n", 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("snd_usb_caiaq_substream_close(%p)\n", 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("snd_usb_caiaq_pcm_hw_params(%p)\n", 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("snd_usb_caiaq_pcm_hw_free(%p)\n", sub);
  155. spin_lock_irq(&dev->spinlock);
  156. deactivate_substream(dev, sub);
  157. spin_unlock_irq(&dev->spinlock);
  158. return snd_pcm_lib_free_pages(sub);
  159. }
  160. /* this should probably go upstream */
  161. #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
  162. #error "Change this table"
  163. #endif
  164. static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  165. 48000, 64000, 88200, 96000, 176400, 192000 };
  166. static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
  167. {
  168. int bytes_per_sample, bpp, ret, i;
  169. int index = substream->number;
  170. struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
  171. struct snd_pcm_runtime *runtime = substream->runtime;
  172. debug("snd_usb_caiaq_pcm_prepare(%p)\n", substream);
  173. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  174. dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1;
  175. else
  176. dev->audio_in_buf_pos[index] = 0;
  177. if (dev->streaming)
  178. return 0;
  179. /* the first client that opens a stream defines the sample rate
  180. * setting for all subsequent calls, until the last client closed. */
  181. for (i=0; i < ARRAY_SIZE(rates); i++)
  182. if (runtime->rate == rates[i])
  183. dev->pcm_info.rates = 1 << i;
  184. snd_pcm_limit_hw_rates(runtime);
  185. bytes_per_sample = BYTES_PER_SAMPLE;
  186. if (dev->spec.data_alignment == 2)
  187. bytes_per_sample++;
  188. bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
  189. * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams;
  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. spin_lock(&dev->spinlock);
  212. activate_substream(dev, sub);
  213. spin_unlock(&dev->spinlock);
  214. break;
  215. case SNDRV_PCM_TRIGGER_STOP:
  216. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  217. spin_lock(&dev->spinlock);
  218. deactivate_substream(dev, sub);
  219. spin_unlock(&dev->spinlock);
  220. break;
  221. default:
  222. return -EINVAL;
  223. }
  224. return 0;
  225. }
  226. static snd_pcm_uframes_t
  227. snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
  228. {
  229. int index = sub->number;
  230. struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
  231. if (dev->input_panic || dev->output_panic)
  232. return SNDRV_PCM_POS_XRUN;
  233. if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  234. return bytes_to_frames(sub->runtime,
  235. dev->audio_out_buf_pos[index]);
  236. else
  237. return bytes_to_frames(sub->runtime,
  238. dev->audio_in_buf_pos[index]);
  239. }
  240. /* operators for both playback and capture */
  241. static struct snd_pcm_ops snd_usb_caiaq_ops = {
  242. .open = snd_usb_caiaq_substream_open,
  243. .close = snd_usb_caiaq_substream_close,
  244. .ioctl = snd_pcm_lib_ioctl,
  245. .hw_params = snd_usb_caiaq_pcm_hw_params,
  246. .hw_free = snd_usb_caiaq_pcm_hw_free,
  247. .prepare = snd_usb_caiaq_pcm_prepare,
  248. .trigger = snd_usb_caiaq_pcm_trigger,
  249. .pointer = snd_usb_caiaq_pcm_pointer
  250. };
  251. static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev,
  252. struct snd_pcm_substream **subs)
  253. {
  254. int stream, pb, *cnt;
  255. struct snd_pcm_substream *sub;
  256. for (stream = 0; stream < dev->n_streams; stream++) {
  257. sub = subs[stream];
  258. if (!sub)
  259. continue;
  260. pb = frames_to_bytes(sub->runtime,
  261. sub->runtime->period_size);
  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. spin_lock(&dev->spinlock);
  281. for (i = 0; i < iso->actual_length;) {
  282. for (stream = 0; stream < dev->n_streams; stream++, i++) {
  283. sub = dev->sub_capture[stream];
  284. if (sub) {
  285. struct snd_pcm_runtime *rt = sub->runtime;
  286. char *audio_buf = rt->dma_area;
  287. int sz = frames_to_bytes(rt, rt->buffer_size);
  288. audio_buf[dev->audio_in_buf_pos[stream]++]
  289. = usb_buf[i];
  290. dev->period_in_count[stream]++;
  291. if (dev->audio_in_buf_pos[stream] == sz)
  292. dev->audio_in_buf_pos[stream] = 0;
  293. }
  294. }
  295. }
  296. spin_unlock(&dev->spinlock);
  297. }
  298. static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev,
  299. const struct urb *urb,
  300. const struct usb_iso_packet_descriptor *iso)
  301. {
  302. unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
  303. unsigned char check_byte;
  304. struct snd_pcm_substream *sub;
  305. int stream, i;
  306. spin_lock(&dev->spinlock);
  307. for (i = 0; i < iso->actual_length;) {
  308. if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
  309. for (stream = 0;
  310. stream < dev->n_streams;
  311. stream++, i++) {
  312. if (dev->first_packet)
  313. continue;
  314. check_byte = MAKE_CHECKBYTE(dev, stream, i);
  315. if ((usb_buf[i] & 0x3f) != check_byte)
  316. dev->input_panic = 1;
  317. if (usb_buf[i] & 0x80)
  318. dev->output_panic = 1;
  319. }
  320. }
  321. dev->first_packet = 0;
  322. for (stream = 0; stream < dev->n_streams; stream++, i++) {
  323. sub = dev->sub_capture[stream];
  324. if (sub) {
  325. struct snd_pcm_runtime *rt = sub->runtime;
  326. char *audio_buf = rt->dma_area;
  327. int sz = frames_to_bytes(rt, rt->buffer_size);
  328. audio_buf[dev->audio_in_buf_pos[stream]++] =
  329. usb_buf[i];
  330. dev->period_in_count[stream]++;
  331. if (dev->audio_in_buf_pos[stream] == sz)
  332. dev->audio_in_buf_pos[stream] = 0;
  333. }
  334. }
  335. }
  336. spin_unlock(&dev->spinlock);
  337. }
  338. static void read_in_urb(struct snd_usb_caiaqdev *dev,
  339. const struct urb *urb,
  340. const struct usb_iso_packet_descriptor *iso)
  341. {
  342. if (!dev->streaming)
  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) {
  353. debug("streaming error detected %s %s\n",
  354. dev->input_panic ? "(input)" : "",
  355. dev->output_panic ? "(output)" : "");
  356. }
  357. check_for_elapsed_periods(dev, dev->sub_capture);
  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. spin_lock(&dev->spinlock);
  367. for (i = 0; i < iso->length;) {
  368. for (stream = 0; stream < dev->n_streams; stream++, i++) {
  369. sub = dev->sub_playback[stream];
  370. if (sub) {
  371. struct snd_pcm_runtime *rt = sub->runtime;
  372. char *audio_buf = rt->dma_area;
  373. int sz = frames_to_bytes(rt, rt->buffer_size);
  374. usb_buf[i] =
  375. audio_buf[dev->audio_out_buf_pos[stream]];
  376. dev->period_out_count[stream]++;
  377. dev->audio_out_buf_pos[stream]++;
  378. if (dev->audio_out_buf_pos[stream] == sz)
  379. dev->audio_out_buf_pos[stream] = 0;
  380. } else
  381. usb_buf[i] = 0;
  382. }
  383. /* fill in the check bytes */
  384. if (dev->spec.data_alignment == 2 &&
  385. i % (dev->n_streams * BYTES_PER_SAMPLE_USB) ==
  386. (dev->n_streams * CHANNELS_PER_STREAM))
  387. for (stream = 0; stream < dev->n_streams; stream++, i++)
  388. usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i);
  389. }
  390. spin_unlock(&dev->spinlock);
  391. check_for_elapsed_periods(dev, dev->sub_playback);
  392. }
  393. static void read_completed(struct urb *urb)
  394. {
  395. struct snd_usb_caiaq_cb_info *info = urb->context;
  396. struct snd_usb_caiaqdev *dev;
  397. struct urb *out;
  398. int frame, len, send_it = 0, outframe = 0;
  399. if (urb->status || !info)
  400. return;
  401. dev = info->dev;
  402. if (!dev->streaming)
  403. return;
  404. out = dev->data_urbs_out[info->index];
  405. /* read the recently received packet and send back one which has
  406. * the same layout */
  407. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  408. if (urb->iso_frame_desc[frame].status)
  409. continue;
  410. len = urb->iso_frame_desc[outframe].actual_length;
  411. out->iso_frame_desc[outframe].length = len;
  412. out->iso_frame_desc[outframe].actual_length = 0;
  413. out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame;
  414. if (len > 0) {
  415. fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
  416. read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
  417. send_it = 1;
  418. }
  419. outframe++;
  420. }
  421. if (send_it) {
  422. out->number_of_packets = FRAMES_PER_URB;
  423. out->transfer_flags = URB_ISO_ASAP;
  424. usb_submit_urb(out, GFP_ATOMIC);
  425. }
  426. /* re-submit inbound urb */
  427. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  428. urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
  429. urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
  430. urb->iso_frame_desc[frame].actual_length = 0;
  431. }
  432. urb->number_of_packets = FRAMES_PER_URB;
  433. urb->transfer_flags = URB_ISO_ASAP;
  434. usb_submit_urb(urb, GFP_ATOMIC);
  435. }
  436. static void write_completed(struct urb *urb)
  437. {
  438. struct snd_usb_caiaq_cb_info *info = urb->context;
  439. struct snd_usb_caiaqdev *dev = info->dev;
  440. if (!dev->output_running) {
  441. dev->output_running = 1;
  442. wake_up(&dev->prepare_wait_queue);
  443. }
  444. }
  445. static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
  446. {
  447. int i, frame;
  448. struct urb **urbs;
  449. struct usb_device *usb_dev = dev->chip.dev;
  450. unsigned int pipe;
  451. pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
  452. usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
  453. usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
  454. urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
  455. if (!urbs) {
  456. log("unable to kmalloc() urbs, OOM!?\n");
  457. *ret = -ENOMEM;
  458. return NULL;
  459. }
  460. for (i = 0; i < N_URBS; i++) {
  461. urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
  462. if (!urbs[i]) {
  463. log("unable to usb_alloc_urb(), OOM!?\n");
  464. *ret = -ENOMEM;
  465. return urbs;
  466. }
  467. urbs[i]->transfer_buffer =
  468. kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
  469. if (!urbs[i]->transfer_buffer) {
  470. log("unable to kmalloc() transfer buffer, OOM!?\n");
  471. *ret = -ENOMEM;
  472. return urbs;
  473. }
  474. for (frame = 0; frame < FRAMES_PER_URB; frame++) {
  475. struct usb_iso_packet_descriptor *iso =
  476. &urbs[i]->iso_frame_desc[frame];
  477. iso->offset = BYTES_PER_FRAME * frame;
  478. iso->length = BYTES_PER_FRAME;
  479. }
  480. urbs[i]->dev = usb_dev;
  481. urbs[i]->pipe = pipe;
  482. urbs[i]->transfer_buffer_length = FRAMES_PER_URB
  483. * BYTES_PER_FRAME;
  484. urbs[i]->context = &dev->data_cb_info[i];
  485. urbs[i]->interval = 1;
  486. urbs[i]->transfer_flags = URB_ISO_ASAP;
  487. urbs[i]->number_of_packets = FRAMES_PER_URB;
  488. urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
  489. read_completed : write_completed;
  490. }
  491. *ret = 0;
  492. return urbs;
  493. }
  494. static void free_urbs(struct urb **urbs)
  495. {
  496. int i;
  497. if (!urbs)
  498. return;
  499. for (i = 0; i < N_URBS; i++) {
  500. if (!urbs[i])
  501. continue;
  502. usb_kill_urb(urbs[i]);
  503. kfree(urbs[i]->transfer_buffer);
  504. usb_free_urb(urbs[i]);
  505. }
  506. kfree(urbs);
  507. }
  508. int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
  509. {
  510. int i, ret;
  511. dev->n_audio_in = max(dev->spec.num_analog_audio_in,
  512. dev->spec.num_digital_audio_in) /
  513. CHANNELS_PER_STREAM;
  514. dev->n_audio_out = max(dev->spec.num_analog_audio_out,
  515. dev->spec.num_digital_audio_out) /
  516. CHANNELS_PER_STREAM;
  517. dev->n_streams = max(dev->n_audio_in, dev->n_audio_out);
  518. debug("dev->n_audio_in = %d\n", dev->n_audio_in);
  519. debug("dev->n_audio_out = %d\n", dev->n_audio_out);
  520. debug("dev->n_streams = %d\n", dev->n_streams);
  521. if (dev->n_streams > MAX_STREAMS) {
  522. log("unable to initialize device, too many streams.\n");
  523. return -EINVAL;
  524. }
  525. ret = snd_pcm_new(dev->chip.card, dev->product_name, 0,
  526. dev->n_audio_out, dev->n_audio_in, &dev->pcm);
  527. if (ret < 0) {
  528. log("snd_pcm_new() returned %d\n", ret);
  529. return ret;
  530. }
  531. dev->pcm->private_data = dev;
  532. strcpy(dev->pcm->name, dev->product_name);
  533. memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
  534. memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
  535. memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware,
  536. sizeof(snd_usb_caiaq_pcm_hardware));
  537. /* setup samplerates */
  538. dev->samplerates = dev->pcm_info.rates;
  539. switch (dev->chip.usb_id) {
  540. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
  541. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
  542. dev->samplerates |= SNDRV_PCM_RATE_88200;
  543. dev->samplerates |= SNDRV_PCM_RATE_192000;
  544. break;
  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("snd_usb_caiaq_audio_free (%p)\n", 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. }