pcm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 <[email protected]>
  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. unsigned long flags;
  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. spin_lock_irqsave(&sub->lock, flags);
  283. if (sub->active) {
  284. usb6fire_pcm_capture(sub, in_urb);
  285. if (sub->period_off >= sub->instance->runtime->period_size) {
  286. sub->period_off %= sub->instance->runtime->period_size;
  287. spin_unlock_irqrestore(&sub->lock, flags);
  288. snd_pcm_period_elapsed(sub->instance);
  289. } else
  290. spin_unlock_irqrestore(&sub->lock, flags);
  291. } else
  292. spin_unlock_irqrestore(&sub->lock, flags);
  293. /* setup out urb structure */
  294. for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
  295. out_urb->packets[i].offset = total_length;
  296. out_urb->packets[i].length = (in_urb->packets[i].actual_length
  297. - 4) / (rt->in_n_analog << 2)
  298. * (rt->out_n_analog << 2) + 4;
  299. out_urb->packets[i].status = 0;
  300. total_length += out_urb->packets[i].length;
  301. }
  302. memset(out_urb->buffer, 0, total_length);
  303. /* now send our playback data (if a free out urb was found) */
  304. sub = &rt->playback;
  305. spin_lock_irqsave(&sub->lock, flags);
  306. if (sub->active) {
  307. usb6fire_pcm_playback(sub, out_urb);
  308. if (sub->period_off >= sub->instance->runtime->period_size) {
  309. sub->period_off %= sub->instance->runtime->period_size;
  310. spin_unlock_irqrestore(&sub->lock, flags);
  311. snd_pcm_period_elapsed(sub->instance);
  312. } else
  313. spin_unlock_irqrestore(&sub->lock, flags);
  314. } else
  315. spin_unlock_irqrestore(&sub->lock, flags);
  316. /* setup the 4th byte of each sample (0x40 for analog channels) */
  317. dest = out_urb->buffer;
  318. for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
  319. if (out_urb->packets[i].length >= 4) {
  320. frame_count = (out_urb->packets[i].length - 4)
  321. / (rt->out_n_analog << 2);
  322. *(dest++) = 0xaa;
  323. *(dest++) = 0xaa;
  324. *(dest++) = frame_count;
  325. *(dest++) = 0x00;
  326. for (frame = 0; frame < frame_count; frame++)
  327. for (channel = 0;
  328. channel < rt->out_n_analog;
  329. channel++) {
  330. dest += 3; /* skip sample data */
  331. *(dest++) = 0x40;
  332. }
  333. }
  334. usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
  335. usb_submit_urb(&in_urb->instance, GFP_ATOMIC);
  336. }
  337. static void usb6fire_pcm_out_urb_handler(struct urb *usb_urb)
  338. {
  339. struct pcm_urb *urb = usb_urb->context;
  340. struct pcm_runtime *rt = urb->chip->pcm;
  341. if (rt->stream_state == STREAM_STARTING) {
  342. rt->stream_wait_cond = true;
  343. wake_up(&rt->stream_wait_queue);
  344. }
  345. }
  346. static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub)
  347. {
  348. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  349. struct pcm_substream *sub = NULL;
  350. struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
  351. if (rt->panic)
  352. return -EPIPE;
  353. mutex_lock(&rt->stream_mutex);
  354. alsa_rt->hw = pcm_hw;
  355. if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  356. if (rt->rate < ARRAY_SIZE(rates))
  357. alsa_rt->hw.rates = rates_alsaid[rt->rate];
  358. alsa_rt->hw.channels_max = OUT_N_CHANNELS;
  359. sub = &rt->playback;
  360. } else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE) {
  361. if (rt->rate < ARRAY_SIZE(rates))
  362. alsa_rt->hw.rates = rates_alsaid[rt->rate];
  363. alsa_rt->hw.channels_max = IN_N_CHANNELS;
  364. sub = &rt->capture;
  365. }
  366. if (!sub) {
  367. mutex_unlock(&rt->stream_mutex);
  368. dev_err(&rt->chip->dev->dev, "invalid stream type.\n");
  369. return -EINVAL;
  370. }
  371. sub->instance = alsa_sub;
  372. sub->active = false;
  373. mutex_unlock(&rt->stream_mutex);
  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. unsigned long flags;
  381. if (rt->panic)
  382. return 0;
  383. mutex_lock(&rt->stream_mutex);
  384. if (sub) {
  385. /* deactivate substream */
  386. spin_lock_irqsave(&sub->lock, flags);
  387. sub->instance = NULL;
  388. sub->active = false;
  389. spin_unlock_irqrestore(&sub->lock, flags);
  390. /* all substreams closed? if so, stop streaming */
  391. if (!rt->playback.instance && !rt->capture.instance) {
  392. usb6fire_pcm_stream_stop(rt);
  393. rt->rate = ARRAY_SIZE(rates);
  394. }
  395. }
  396. mutex_unlock(&rt->stream_mutex);
  397. return 0;
  398. }
  399. static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
  400. {
  401. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  402. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  403. struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
  404. int ret;
  405. if (rt->panic)
  406. return -EPIPE;
  407. if (!sub)
  408. return -ENODEV;
  409. mutex_lock(&rt->stream_mutex);
  410. sub->dma_off = 0;
  411. sub->period_off = 0;
  412. if (rt->stream_state == STREAM_DISABLED) {
  413. for (rt->rate = 0; rt->rate < ARRAY_SIZE(rates); rt->rate++)
  414. if (alsa_rt->rate == rates[rt->rate])
  415. break;
  416. if (rt->rate == ARRAY_SIZE(rates)) {
  417. mutex_unlock(&rt->stream_mutex);
  418. dev_err(&rt->chip->dev->dev,
  419. "invalid rate %d in prepare.\n",
  420. alsa_rt->rate);
  421. return -EINVAL;
  422. }
  423. ret = usb6fire_pcm_set_rate(rt);
  424. if (ret) {
  425. mutex_unlock(&rt->stream_mutex);
  426. return ret;
  427. }
  428. ret = usb6fire_pcm_stream_start(rt);
  429. if (ret) {
  430. mutex_unlock(&rt->stream_mutex);
  431. dev_err(&rt->chip->dev->dev,
  432. "could not start pcm stream.\n");
  433. return ret;
  434. }
  435. }
  436. mutex_unlock(&rt->stream_mutex);
  437. return 0;
  438. }
  439. static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
  440. {
  441. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  442. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  443. unsigned long flags;
  444. if (rt->panic)
  445. return -EPIPE;
  446. if (!sub)
  447. return -ENODEV;
  448. switch (cmd) {
  449. case SNDRV_PCM_TRIGGER_START:
  450. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  451. spin_lock_irqsave(&sub->lock, flags);
  452. sub->active = true;
  453. spin_unlock_irqrestore(&sub->lock, flags);
  454. return 0;
  455. case SNDRV_PCM_TRIGGER_STOP:
  456. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  457. spin_lock_irqsave(&sub->lock, flags);
  458. sub->active = false;
  459. spin_unlock_irqrestore(&sub->lock, flags);
  460. return 0;
  461. default:
  462. return -EINVAL;
  463. }
  464. }
  465. static snd_pcm_uframes_t usb6fire_pcm_pointer(
  466. struct snd_pcm_substream *alsa_sub)
  467. {
  468. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  469. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  470. unsigned long flags;
  471. snd_pcm_uframes_t ret;
  472. if (rt->panic || !sub)
  473. return SNDRV_PCM_POS_XRUN;
  474. spin_lock_irqsave(&sub->lock, flags);
  475. ret = sub->dma_off;
  476. spin_unlock_irqrestore(&sub->lock, flags);
  477. return ret;
  478. }
  479. static const struct snd_pcm_ops pcm_ops = {
  480. .open = usb6fire_pcm_open,
  481. .close = usb6fire_pcm_close,
  482. .prepare = usb6fire_pcm_prepare,
  483. .trigger = usb6fire_pcm_trigger,
  484. .pointer = usb6fire_pcm_pointer,
  485. };
  486. static void usb6fire_pcm_init_urb(struct pcm_urb *urb,
  487. struct sfire_chip *chip, bool in, int ep,
  488. void (*handler)(struct urb *))
  489. {
  490. urb->chip = chip;
  491. usb_init_urb(&urb->instance);
  492. urb->instance.transfer_buffer = urb->buffer;
  493. urb->instance.transfer_buffer_length =
  494. PCM_N_PACKETS_PER_URB * PCM_MAX_PACKET_SIZE;
  495. urb->instance.dev = chip->dev;
  496. urb->instance.pipe = in ? usb_rcvisocpipe(chip->dev, ep)
  497. : usb_sndisocpipe(chip->dev, ep);
  498. urb->instance.interval = 1;
  499. urb->instance.complete = handler;
  500. urb->instance.context = urb;
  501. urb->instance.number_of_packets = PCM_N_PACKETS_PER_URB;
  502. }
  503. static int usb6fire_pcm_buffers_init(struct pcm_runtime *rt)
  504. {
  505. int i;
  506. for (i = 0; i < PCM_N_URBS; i++) {
  507. rt->out_urbs[i].buffer = kcalloc(PCM_MAX_PACKET_SIZE,
  508. PCM_N_PACKETS_PER_URB,
  509. GFP_KERNEL);
  510. if (!rt->out_urbs[i].buffer)
  511. return -ENOMEM;
  512. rt->in_urbs[i].buffer = kcalloc(PCM_MAX_PACKET_SIZE,
  513. PCM_N_PACKETS_PER_URB,
  514. GFP_KERNEL);
  515. if (!rt->in_urbs[i].buffer)
  516. return -ENOMEM;
  517. }
  518. return 0;
  519. }
  520. static void usb6fire_pcm_buffers_destroy(struct pcm_runtime *rt)
  521. {
  522. int i;
  523. for (i = 0; i < PCM_N_URBS; i++) {
  524. kfree(rt->out_urbs[i].buffer);
  525. kfree(rt->in_urbs[i].buffer);
  526. }
  527. }
  528. int usb6fire_pcm_init(struct sfire_chip *chip)
  529. {
  530. int i;
  531. int ret;
  532. struct snd_pcm *pcm;
  533. struct pcm_runtime *rt =
  534. kzalloc(sizeof(struct pcm_runtime), GFP_KERNEL);
  535. if (!rt)
  536. return -ENOMEM;
  537. ret = usb6fire_pcm_buffers_init(rt);
  538. if (ret) {
  539. usb6fire_pcm_buffers_destroy(rt);
  540. kfree(rt);
  541. return ret;
  542. }
  543. rt->chip = chip;
  544. rt->stream_state = STREAM_DISABLED;
  545. rt->rate = ARRAY_SIZE(rates);
  546. init_waitqueue_head(&rt->stream_wait_queue);
  547. mutex_init(&rt->stream_mutex);
  548. spin_lock_init(&rt->playback.lock);
  549. spin_lock_init(&rt->capture.lock);
  550. for (i = 0; i < PCM_N_URBS; i++) {
  551. usb6fire_pcm_init_urb(&rt->in_urbs[i], chip, true, IN_EP,
  552. usb6fire_pcm_in_urb_handler);
  553. usb6fire_pcm_init_urb(&rt->out_urbs[i], chip, false, OUT_EP,
  554. usb6fire_pcm_out_urb_handler);
  555. rt->in_urbs[i].peer = &rt->out_urbs[i];
  556. rt->out_urbs[i].peer = &rt->in_urbs[i];
  557. }
  558. ret = snd_pcm_new(chip->card, "DMX6FireUSB", 0, 1, 1, &pcm);
  559. if (ret < 0) {
  560. usb6fire_pcm_buffers_destroy(rt);
  561. kfree(rt);
  562. dev_err(&chip->dev->dev, "cannot create pcm instance.\n");
  563. return ret;
  564. }
  565. pcm->private_data = rt;
  566. strcpy(pcm->name, "DMX 6Fire USB");
  567. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
  568. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);
  569. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  570. rt->instance = pcm;
  571. chip->pcm = rt;
  572. return 0;
  573. }
  574. void usb6fire_pcm_abort(struct sfire_chip *chip)
  575. {
  576. struct pcm_runtime *rt = chip->pcm;
  577. int i;
  578. if (rt) {
  579. rt->panic = true;
  580. if (rt->playback.instance)
  581. snd_pcm_stop_xrun(rt->playback.instance);
  582. if (rt->capture.instance)
  583. snd_pcm_stop_xrun(rt->capture.instance);
  584. for (i = 0; i < PCM_N_URBS; i++) {
  585. usb_poison_urb(&rt->in_urbs[i].instance);
  586. usb_poison_urb(&rt->out_urbs[i].instance);
  587. }
  588. }
  589. }
  590. void usb6fire_pcm_destroy(struct sfire_chip *chip)
  591. {
  592. struct pcm_runtime *rt = chip->pcm;
  593. usb6fire_pcm_buffers_destroy(rt);
  594. kfree(rt);
  595. chip->pcm = NULL;
  596. }