pcm.c 18 KB

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