caiaq-audio.c 18 KB

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