pcm.c 17 KB

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