pcm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * Linux driver for TerraTec DMX 6Fire USB
  3. *
  4. * PCM driver
  5. *
  6. * Author: Torsten Schenk <torsten.schenk@zoho.com>
  7. * Created: Jan 01, 2011
  8. * Version: 0.3.0
  9. * Copyright: (C) Torsten Schenk
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include "pcm.h"
  17. #include "chip.h"
  18. #include "comm.h"
  19. #include "control.h"
  20. enum {
  21. OUT_N_CHANNELS = 6, IN_N_CHANNELS = 4
  22. };
  23. /* keep next two synced with
  24. * FW_EP_W_MAX_PACKET_SIZE[] and RATES_MAX_PACKET_SIZE
  25. * and CONTROL_RATE_XXX in control.h */
  26. static const int rates_in_packet_size[] = { 228, 228, 420, 420, 404, 404 };
  27. static const int rates_out_packet_size[] = { 228, 228, 420, 420, 604, 604 };
  28. static const int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000 };
  29. static const int rates_alsaid[] = {
  30. SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_48000,
  31. SNDRV_PCM_RATE_88200, SNDRV_PCM_RATE_96000,
  32. SNDRV_PCM_RATE_176400, SNDRV_PCM_RATE_192000 };
  33. enum { /* settings for pcm */
  34. OUT_EP = 6, IN_EP = 2, MAX_BUFSIZE = 128 * 1024
  35. };
  36. enum { /* pcm streaming states */
  37. STREAM_DISABLED, /* no pcm streaming */
  38. STREAM_STARTING, /* pcm streaming requested, waiting to become ready */
  39. STREAM_RUNNING, /* pcm streaming running */
  40. STREAM_STOPPING
  41. };
  42. static const struct snd_pcm_hardware pcm_hw = {
  43. .info = SNDRV_PCM_INFO_MMAP |
  44. SNDRV_PCM_INFO_INTERLEAVED |
  45. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  46. SNDRV_PCM_INFO_MMAP_VALID |
  47. SNDRV_PCM_INFO_BATCH,
  48. .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  49. .rates = SNDRV_PCM_RATE_44100 |
  50. SNDRV_PCM_RATE_48000 |
  51. SNDRV_PCM_RATE_88200 |
  52. SNDRV_PCM_RATE_96000 |
  53. SNDRV_PCM_RATE_176400 |
  54. SNDRV_PCM_RATE_192000,
  55. .rate_min = 44100,
  56. .rate_max = 192000,
  57. .channels_min = 1,
  58. .channels_max = 0, /* set in pcm_open, depending on capture/playback */
  59. .buffer_bytes_max = MAX_BUFSIZE,
  60. .period_bytes_min = PCM_N_PACKETS_PER_URB * (PCM_MAX_PACKET_SIZE - 4),
  61. .period_bytes_max = MAX_BUFSIZE,
  62. .periods_min = 2,
  63. .periods_max = 1024
  64. };
  65. static int usb6fire_pcm_set_rate(struct pcm_runtime *rt)
  66. {
  67. int ret;
  68. struct control_runtime *ctrl_rt = rt->chip->control;
  69. ctrl_rt->usb_streaming = false;
  70. ret = ctrl_rt->update_streaming(ctrl_rt);
  71. if (ret < 0) {
  72. snd_printk(KERN_ERR PREFIX "error stopping streaming while "
  73. "setting samplerate %d.\n", rates[rt->rate]);
  74. return ret;
  75. }
  76. ret = ctrl_rt->set_rate(ctrl_rt, rt->rate);
  77. if (ret < 0) {
  78. snd_printk(KERN_ERR PREFIX "error setting samplerate %d.\n",
  79. rates[rt->rate]);
  80. return ret;
  81. }
  82. ret = ctrl_rt->set_channels(ctrl_rt, OUT_N_CHANNELS, IN_N_CHANNELS,
  83. false, false);
  84. if (ret < 0) {
  85. snd_printk(KERN_ERR PREFIX "error initializing channels "
  86. "while setting samplerate %d.\n",
  87. rates[rt->rate]);
  88. return ret;
  89. }
  90. ctrl_rt->usb_streaming = true;
  91. ret = ctrl_rt->update_streaming(ctrl_rt);
  92. if (ret < 0) {
  93. snd_printk(KERN_ERR PREFIX "error starting streaming while "
  94. "setting samplerate %d.\n", rates[rt->rate]);
  95. return ret;
  96. }
  97. rt->in_n_analog = IN_N_CHANNELS;
  98. rt->out_n_analog = OUT_N_CHANNELS;
  99. rt->in_packet_size = rates_in_packet_size[rt->rate];
  100. rt->out_packet_size = rates_out_packet_size[rt->rate];
  101. return 0;
  102. }
  103. static struct pcm_substream *usb6fire_pcm_get_substream(
  104. struct snd_pcm_substream *alsa_sub)
  105. {
  106. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  107. if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  108. return &rt->playback;
  109. else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE)
  110. return &rt->capture;
  111. snd_printk(KERN_ERR PREFIX "error getting pcm substream slot.\n");
  112. return NULL;
  113. }
  114. /* call with stream_mutex locked */
  115. static void usb6fire_pcm_stream_stop(struct pcm_runtime *rt)
  116. {
  117. int i;
  118. struct control_runtime *ctrl_rt = rt->chip->control;
  119. if (rt->stream_state != STREAM_DISABLED) {
  120. for (i = 0; i < PCM_N_URBS; i++) {
  121. usb_kill_urb(&rt->in_urbs[i].instance);
  122. usb_kill_urb(&rt->out_urbs[i].instance);
  123. }
  124. ctrl_rt->usb_streaming = false;
  125. ctrl_rt->update_streaming(ctrl_rt);
  126. rt->stream_state = STREAM_DISABLED;
  127. }
  128. }
  129. /* call with stream_mutex locked */
  130. static int usb6fire_pcm_stream_start(struct pcm_runtime *rt)
  131. {
  132. int ret;
  133. int i;
  134. int k;
  135. struct usb_iso_packet_descriptor *packet;
  136. if (rt->stream_state == STREAM_DISABLED) {
  137. /* submit our in urbs */
  138. rt->stream_wait_cond = false;
  139. rt->stream_state = STREAM_STARTING;
  140. for (i = 0; i < PCM_N_URBS; i++) {
  141. for (k = 0; k < PCM_N_PACKETS_PER_URB; k++) {
  142. packet = &rt->in_urbs[i].packets[k];
  143. packet->offset = k * rt->in_packet_size;
  144. packet->length = rt->in_packet_size;
  145. packet->actual_length = 0;
  146. packet->status = 0;
  147. }
  148. ret = usb_submit_urb(&rt->in_urbs[i].instance,
  149. GFP_ATOMIC);
  150. if (ret) {
  151. usb6fire_pcm_stream_stop(rt);
  152. return ret;
  153. }
  154. }
  155. /* wait for first out urb to return (sent in in urb handler) */
  156. wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
  157. HZ);
  158. if (rt->stream_wait_cond)
  159. rt->stream_state = STREAM_RUNNING;
  160. else {
  161. usb6fire_pcm_stream_stop(rt);
  162. return -EIO;
  163. }
  164. }
  165. return 0;
  166. }
  167. /* call with substream locked */
  168. static void usb6fire_pcm_capture(struct pcm_substream *sub, struct pcm_urb *urb)
  169. {
  170. int i;
  171. int frame;
  172. int frame_count;
  173. unsigned int total_length = 0;
  174. struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
  175. struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
  176. u32 *src = NULL;
  177. u32 *dest = (u32 *) (alsa_rt->dma_area + sub->dma_off
  178. * (alsa_rt->frame_bits >> 3));
  179. u32 *dest_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
  180. * (alsa_rt->frame_bits >> 3));
  181. int bytes_per_frame = alsa_rt->channels << 2;
  182. for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
  183. /* at least 4 header bytes for valid packet.
  184. * after that: 32 bits per sample for analog channels */
  185. if (urb->packets[i].actual_length > 4)
  186. frame_count = (urb->packets[i].actual_length - 4)
  187. / (rt->in_n_analog << 2);
  188. else
  189. frame_count = 0;
  190. if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
  191. src = (u32 *) (urb->buffer + total_length);
  192. else if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
  193. src = (u32 *) (urb->buffer - 1 + total_length);
  194. else
  195. return;
  196. src++; /* skip leading 4 bytes of every packet */
  197. total_length += urb->packets[i].length;
  198. for (frame = 0; frame < frame_count; frame++) {
  199. memcpy(dest, src, bytes_per_frame);
  200. dest += alsa_rt->channels;
  201. src += rt->in_n_analog;
  202. sub->dma_off++;
  203. sub->period_off++;
  204. if (dest == dest_end) {
  205. sub->dma_off = 0;
  206. dest = (u32 *) alsa_rt->dma_area;
  207. }
  208. }
  209. }
  210. }
  211. /* call with substream locked */
  212. static void usb6fire_pcm_playback(struct pcm_substream *sub,
  213. struct pcm_urb *urb)
  214. {
  215. int i;
  216. int frame;
  217. int frame_count;
  218. struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
  219. struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
  220. u32 *src = (u32 *) (alsa_rt->dma_area + sub->dma_off
  221. * (alsa_rt->frame_bits >> 3));
  222. u32 *src_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
  223. * (alsa_rt->frame_bits >> 3));
  224. u32 *dest;
  225. int bytes_per_frame = alsa_rt->channels << 2;
  226. if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
  227. dest = (u32 *) (urb->buffer - 1);
  228. else if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
  229. dest = (u32 *) (urb->buffer);
  230. else {
  231. snd_printk(KERN_ERR PREFIX "Unknown sample format.");
  232. return;
  233. }
  234. for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
  235. /* at least 4 header bytes for valid packet.
  236. * after that: 32 bits per sample for analog channels */
  237. if (urb->packets[i].length > 4)
  238. frame_count = (urb->packets[i].length - 4)
  239. / (rt->out_n_analog << 2);
  240. else
  241. frame_count = 0;
  242. dest++; /* skip leading 4 bytes of every frame */
  243. for (frame = 0; frame < frame_count; frame++) {
  244. memcpy(dest, src, bytes_per_frame);
  245. src += alsa_rt->channels;
  246. dest += rt->out_n_analog;
  247. sub->dma_off++;
  248. sub->period_off++;
  249. if (src == src_end) {
  250. src = (u32 *) alsa_rt->dma_area;
  251. sub->dma_off = 0;
  252. }
  253. }
  254. }
  255. }
  256. static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb)
  257. {
  258. struct pcm_urb *in_urb = usb_urb->context;
  259. struct pcm_urb *out_urb = in_urb->peer;
  260. struct pcm_runtime *rt = in_urb->chip->pcm;
  261. struct pcm_substream *sub;
  262. unsigned long flags;
  263. int total_length = 0;
  264. int frame_count;
  265. int frame;
  266. int channel;
  267. int i;
  268. u8 *dest;
  269. if (usb_urb->status || rt->panic || rt->stream_state == STREAM_STOPPING)
  270. return;
  271. for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
  272. if (in_urb->packets[i].status) {
  273. rt->panic = true;
  274. return;
  275. }
  276. if (rt->stream_state == STREAM_DISABLED) {
  277. snd_printk(KERN_ERR PREFIX "internal error: "
  278. "stream disabled in in-urb handler.\n");
  279. return;
  280. }
  281. /* receive our capture data */
  282. sub = &rt->capture;
  283. spin_lock_irqsave(&sub->lock, flags);
  284. if (sub->active) {
  285. usb6fire_pcm_capture(sub, in_urb);
  286. if (sub->period_off >= sub->instance->runtime->period_size) {
  287. sub->period_off %= sub->instance->runtime->period_size;
  288. spin_unlock_irqrestore(&sub->lock, flags);
  289. snd_pcm_period_elapsed(sub->instance);
  290. } else
  291. spin_unlock_irqrestore(&sub->lock, flags);
  292. } else
  293. spin_unlock_irqrestore(&sub->lock, flags);
  294. /* setup out urb structure */
  295. for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
  296. out_urb->packets[i].offset = total_length;
  297. out_urb->packets[i].length = (in_urb->packets[i].actual_length
  298. - 4) / (rt->in_n_analog << 2)
  299. * (rt->out_n_analog << 2) + 4;
  300. out_urb->packets[i].status = 0;
  301. total_length += out_urb->packets[i].length;
  302. }
  303. memset(out_urb->buffer, 0, total_length);
  304. /* now send our playback data (if a free out urb was found) */
  305. sub = &rt->playback;
  306. spin_lock_irqsave(&sub->lock, flags);
  307. if (sub->active) {
  308. usb6fire_pcm_playback(sub, out_urb);
  309. if (sub->period_off >= sub->instance->runtime->period_size) {
  310. sub->period_off %= sub->instance->runtime->period_size;
  311. spin_unlock_irqrestore(&sub->lock, flags);
  312. snd_pcm_period_elapsed(sub->instance);
  313. } else
  314. spin_unlock_irqrestore(&sub->lock, flags);
  315. } else
  316. spin_unlock_irqrestore(&sub->lock, flags);
  317. /* setup the 4th byte of each sample (0x40 for analog channels) */
  318. dest = out_urb->buffer;
  319. for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
  320. if (out_urb->packets[i].length >= 4) {
  321. frame_count = (out_urb->packets[i].length - 4)
  322. / (rt->out_n_analog << 2);
  323. *(dest++) = 0xaa;
  324. *(dest++) = 0xaa;
  325. *(dest++) = frame_count;
  326. *(dest++) = 0x00;
  327. for (frame = 0; frame < frame_count; frame++)
  328. for (channel = 0;
  329. channel < rt->out_n_analog;
  330. channel++) {
  331. dest += 3; /* skip sample data */
  332. *(dest++) = 0x40;
  333. }
  334. }
  335. usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
  336. usb_submit_urb(&in_urb->instance, GFP_ATOMIC);
  337. }
  338. static void usb6fire_pcm_out_urb_handler(struct urb *usb_urb)
  339. {
  340. struct pcm_urb *urb = usb_urb->context;
  341. struct pcm_runtime *rt = urb->chip->pcm;
  342. if (rt->stream_state == STREAM_STARTING) {
  343. rt->stream_wait_cond = true;
  344. wake_up(&rt->stream_wait_queue);
  345. }
  346. }
  347. static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub)
  348. {
  349. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  350. struct pcm_substream *sub = NULL;
  351. struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
  352. if (rt->panic)
  353. return -EPIPE;
  354. mutex_lock(&rt->stream_mutex);
  355. alsa_rt->hw = pcm_hw;
  356. if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  357. if (rt->rate < ARRAY_SIZE(rates))
  358. alsa_rt->hw.rates = rates_alsaid[rt->rate];
  359. alsa_rt->hw.channels_max = OUT_N_CHANNELS;
  360. sub = &rt->playback;
  361. } else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE) {
  362. if (rt->rate < ARRAY_SIZE(rates))
  363. alsa_rt->hw.rates = rates_alsaid[rt->rate];
  364. alsa_rt->hw.channels_max = IN_N_CHANNELS;
  365. sub = &rt->capture;
  366. }
  367. if (!sub) {
  368. mutex_unlock(&rt->stream_mutex);
  369. snd_printk(KERN_ERR PREFIX "invalid stream type.\n");
  370. return -EINVAL;
  371. }
  372. sub->instance = alsa_sub;
  373. sub->active = false;
  374. mutex_unlock(&rt->stream_mutex);
  375. return 0;
  376. }
  377. static int usb6fire_pcm_close(struct snd_pcm_substream *alsa_sub)
  378. {
  379. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  380. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  381. unsigned long flags;
  382. if (rt->panic)
  383. return 0;
  384. mutex_lock(&rt->stream_mutex);
  385. if (sub) {
  386. /* deactivate substream */
  387. spin_lock_irqsave(&sub->lock, flags);
  388. sub->instance = NULL;
  389. sub->active = false;
  390. spin_unlock_irqrestore(&sub->lock, flags);
  391. /* all substreams closed? if so, stop streaming */
  392. if (!rt->playback.instance && !rt->capture.instance) {
  393. usb6fire_pcm_stream_stop(rt);
  394. rt->rate = ARRAY_SIZE(rates);
  395. }
  396. }
  397. mutex_unlock(&rt->stream_mutex);
  398. return 0;
  399. }
  400. static int usb6fire_pcm_hw_params(struct snd_pcm_substream *alsa_sub,
  401. struct snd_pcm_hw_params *hw_params)
  402. {
  403. return snd_pcm_lib_malloc_pages(alsa_sub,
  404. params_buffer_bytes(hw_params));
  405. }
  406. static int usb6fire_pcm_hw_free(struct snd_pcm_substream *alsa_sub)
  407. {
  408. return snd_pcm_lib_free_pages(alsa_sub);
  409. }
  410. static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
  411. {
  412. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  413. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  414. struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
  415. int ret;
  416. if (rt->panic)
  417. return -EPIPE;
  418. if (!sub)
  419. return -ENODEV;
  420. mutex_lock(&rt->stream_mutex);
  421. sub->dma_off = 0;
  422. sub->period_off = 0;
  423. if (rt->stream_state == STREAM_DISABLED) {
  424. for (rt->rate = 0; rt->rate < ARRAY_SIZE(rates); rt->rate++)
  425. if (alsa_rt->rate == rates[rt->rate])
  426. break;
  427. if (rt->rate == ARRAY_SIZE(rates)) {
  428. mutex_unlock(&rt->stream_mutex);
  429. snd_printk("invalid rate %d in prepare.\n",
  430. alsa_rt->rate);
  431. return -EINVAL;
  432. }
  433. ret = usb6fire_pcm_set_rate(rt);
  434. if (ret) {
  435. mutex_unlock(&rt->stream_mutex);
  436. return ret;
  437. }
  438. ret = usb6fire_pcm_stream_start(rt);
  439. if (ret) {
  440. mutex_unlock(&rt->stream_mutex);
  441. snd_printk(KERN_ERR PREFIX
  442. "could not start pcm stream.\n");
  443. return ret;
  444. }
  445. }
  446. mutex_unlock(&rt->stream_mutex);
  447. return 0;
  448. }
  449. static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
  450. {
  451. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  452. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  453. unsigned long flags;
  454. if (rt->panic)
  455. return -EPIPE;
  456. if (!sub)
  457. return -ENODEV;
  458. switch (cmd) {
  459. case SNDRV_PCM_TRIGGER_START:
  460. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  461. spin_lock_irqsave(&sub->lock, flags);
  462. sub->active = true;
  463. spin_unlock_irqrestore(&sub->lock, flags);
  464. return 0;
  465. case SNDRV_PCM_TRIGGER_STOP:
  466. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  467. spin_lock_irqsave(&sub->lock, flags);
  468. sub->active = false;
  469. spin_unlock_irqrestore(&sub->lock, flags);
  470. return 0;
  471. default:
  472. return -EINVAL;
  473. }
  474. }
  475. static snd_pcm_uframes_t usb6fire_pcm_pointer(
  476. struct snd_pcm_substream *alsa_sub)
  477. {
  478. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  479. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  480. unsigned long flags;
  481. snd_pcm_uframes_t ret;
  482. if (rt->panic || !sub)
  483. return SNDRV_PCM_STATE_XRUN;
  484. spin_lock_irqsave(&sub->lock, flags);
  485. ret = sub->dma_off;
  486. spin_unlock_irqrestore(&sub->lock, flags);
  487. return ret;
  488. }
  489. static struct snd_pcm_ops pcm_ops = {
  490. .open = usb6fire_pcm_open,
  491. .close = usb6fire_pcm_close,
  492. .ioctl = snd_pcm_lib_ioctl,
  493. .hw_params = usb6fire_pcm_hw_params,
  494. .hw_free = usb6fire_pcm_hw_free,
  495. .prepare = usb6fire_pcm_prepare,
  496. .trigger = usb6fire_pcm_trigger,
  497. .pointer = usb6fire_pcm_pointer,
  498. };
  499. static void __devinit usb6fire_pcm_init_urb(struct pcm_urb *urb,
  500. struct sfire_chip *chip, bool in, int ep,
  501. void (*handler)(struct urb *))
  502. {
  503. urb->chip = chip;
  504. usb_init_urb(&urb->instance);
  505. urb->instance.transfer_buffer = urb->buffer;
  506. urb->instance.transfer_buffer_length =
  507. PCM_N_PACKETS_PER_URB * PCM_MAX_PACKET_SIZE;
  508. urb->instance.dev = chip->dev;
  509. urb->instance.pipe = in ? usb_rcvisocpipe(chip->dev, ep)
  510. : usb_sndisocpipe(chip->dev, ep);
  511. urb->instance.interval = 1;
  512. urb->instance.transfer_flags = URB_ISO_ASAP;
  513. urb->instance.complete = handler;
  514. urb->instance.context = urb;
  515. urb->instance.number_of_packets = PCM_N_PACKETS_PER_URB;
  516. }
  517. int __devinit usb6fire_pcm_init(struct sfire_chip *chip)
  518. {
  519. int i;
  520. int ret;
  521. struct snd_pcm *pcm;
  522. struct pcm_runtime *rt =
  523. kzalloc(sizeof(struct pcm_runtime), GFP_KERNEL);
  524. if (!rt)
  525. return -ENOMEM;
  526. rt->chip = chip;
  527. rt->stream_state = STREAM_DISABLED;
  528. rt->rate = ARRAY_SIZE(rates);
  529. init_waitqueue_head(&rt->stream_wait_queue);
  530. mutex_init(&rt->stream_mutex);
  531. spin_lock_init(&rt->playback.lock);
  532. spin_lock_init(&rt->capture.lock);
  533. for (i = 0; i < PCM_N_URBS; i++) {
  534. usb6fire_pcm_init_urb(&rt->in_urbs[i], chip, true, IN_EP,
  535. usb6fire_pcm_in_urb_handler);
  536. usb6fire_pcm_init_urb(&rt->out_urbs[i], chip, false, OUT_EP,
  537. usb6fire_pcm_out_urb_handler);
  538. rt->in_urbs[i].peer = &rt->out_urbs[i];
  539. rt->out_urbs[i].peer = &rt->in_urbs[i];
  540. }
  541. ret = snd_pcm_new(chip->card, "DMX6FireUSB", 0, 1, 1, &pcm);
  542. if (ret < 0) {
  543. kfree(rt);
  544. snd_printk(KERN_ERR PREFIX "cannot create pcm instance.\n");
  545. return ret;
  546. }
  547. pcm->private_data = rt;
  548. strcpy(pcm->name, "DMX 6Fire USB");
  549. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
  550. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);
  551. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  552. SNDRV_DMA_TYPE_CONTINUOUS,
  553. snd_dma_continuous_data(GFP_KERNEL),
  554. MAX_BUFSIZE, MAX_BUFSIZE);
  555. if (ret) {
  556. kfree(rt);
  557. snd_printk(KERN_ERR PREFIX
  558. "error preallocating pcm buffers.\n");
  559. return ret;
  560. }
  561. rt->instance = pcm;
  562. chip->pcm = rt;
  563. return 0;
  564. }
  565. void usb6fire_pcm_abort(struct sfire_chip *chip)
  566. {
  567. struct pcm_runtime *rt = chip->pcm;
  568. int i;
  569. if (rt) {
  570. rt->panic = true;
  571. if (rt->playback.instance)
  572. snd_pcm_stop(rt->playback.instance,
  573. SNDRV_PCM_STATE_XRUN);
  574. if (rt->capture.instance)
  575. snd_pcm_stop(rt->capture.instance,
  576. SNDRV_PCM_STATE_XRUN);
  577. for (i = 0; i < PCM_N_URBS; i++) {
  578. usb_poison_urb(&rt->in_urbs[i].instance);
  579. usb_poison_urb(&rt->out_urbs[i].instance);
  580. }
  581. }
  582. }
  583. void usb6fire_pcm_destroy(struct sfire_chip *chip)
  584. {
  585. kfree(chip->pcm);
  586. chip->pcm = NULL;
  587. }