pcm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. snd_printk(KERN_ERR PREFIX "error stopping streaming while "
  72. "setting samplerate %d.\n", rates[rt->rate]);
  73. return ret;
  74. }
  75. ret = ctrl_rt->set_rate(ctrl_rt, rt->rate);
  76. if (ret < 0) {
  77. snd_printk(KERN_ERR PREFIX "error setting samplerate %d.\n",
  78. rates[rt->rate]);
  79. return ret;
  80. }
  81. ret = ctrl_rt->set_channels(ctrl_rt, OUT_N_CHANNELS, IN_N_CHANNELS,
  82. false, false);
  83. if (ret < 0) {
  84. snd_printk(KERN_ERR PREFIX "error initializing channels "
  85. "while setting samplerate %d.\n",
  86. rates[rt->rate]);
  87. return ret;
  88. }
  89. ctrl_rt->usb_streaming = true;
  90. ret = ctrl_rt->update_streaming(ctrl_rt);
  91. if (ret < 0) {
  92. snd_printk(KERN_ERR PREFIX "error starting streaming while "
  93. "setting samplerate %d.\n", rates[rt->rate]);
  94. return ret;
  95. }
  96. rt->in_n_analog = IN_N_CHANNELS;
  97. rt->out_n_analog = OUT_N_CHANNELS;
  98. rt->in_packet_size = rates_in_packet_size[rt->rate];
  99. rt->out_packet_size = rates_out_packet_size[rt->rate];
  100. return 0;
  101. }
  102. static struct pcm_substream *usb6fire_pcm_get_substream(
  103. struct snd_pcm_substream *alsa_sub)
  104. {
  105. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  106. if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  107. return &rt->playback;
  108. else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE)
  109. return &rt->capture;
  110. snd_printk(KERN_ERR PREFIX "error getting pcm substream slot.\n");
  111. return NULL;
  112. }
  113. /* call with stream_mutex locked */
  114. static void usb6fire_pcm_stream_stop(struct pcm_runtime *rt)
  115. {
  116. int i;
  117. struct control_runtime *ctrl_rt = rt->chip->control;
  118. if (rt->stream_state != STREAM_DISABLED) {
  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 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. snd_printk(KERN_ERR PREFIX "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. snd_printk(KERN_ERR PREFIX "internal error: "
  277. "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. snd_printk(KERN_ERR PREFIX "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_hw_params(struct snd_pcm_substream *alsa_sub,
  400. struct snd_pcm_hw_params *hw_params)
  401. {
  402. return snd_pcm_lib_malloc_pages(alsa_sub,
  403. params_buffer_bytes(hw_params));
  404. }
  405. static int usb6fire_pcm_hw_free(struct snd_pcm_substream *alsa_sub)
  406. {
  407. return snd_pcm_lib_free_pages(alsa_sub);
  408. }
  409. static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
  410. {
  411. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  412. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  413. struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
  414. int ret;
  415. if (rt->panic)
  416. return -EPIPE;
  417. if (!sub)
  418. return -ENODEV;
  419. mutex_lock(&rt->stream_mutex);
  420. sub->dma_off = 0;
  421. sub->period_off = 0;
  422. if (rt->stream_state == STREAM_DISABLED) {
  423. for (rt->rate = 0; rt->rate < ARRAY_SIZE(rates); rt->rate++)
  424. if (alsa_rt->rate == rates[rt->rate])
  425. break;
  426. if (rt->rate == ARRAY_SIZE(rates)) {
  427. mutex_unlock(&rt->stream_mutex);
  428. snd_printk("invalid rate %d in prepare.\n",
  429. alsa_rt->rate);
  430. return -EINVAL;
  431. }
  432. ret = usb6fire_pcm_set_rate(rt);
  433. if (ret) {
  434. mutex_unlock(&rt->stream_mutex);
  435. return ret;
  436. }
  437. ret = usb6fire_pcm_stream_start(rt);
  438. if (ret) {
  439. mutex_unlock(&rt->stream_mutex);
  440. snd_printk(KERN_ERR PREFIX
  441. "could not start pcm stream.\n");
  442. return ret;
  443. }
  444. }
  445. mutex_unlock(&rt->stream_mutex);
  446. return 0;
  447. }
  448. static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
  449. {
  450. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  451. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  452. unsigned long flags;
  453. if (rt->panic)
  454. return -EPIPE;
  455. if (!sub)
  456. return -ENODEV;
  457. switch (cmd) {
  458. case SNDRV_PCM_TRIGGER_START:
  459. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  460. spin_lock_irqsave(&sub->lock, flags);
  461. sub->active = true;
  462. spin_unlock_irqrestore(&sub->lock, flags);
  463. return 0;
  464. case SNDRV_PCM_TRIGGER_STOP:
  465. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  466. spin_lock_irqsave(&sub->lock, flags);
  467. sub->active = false;
  468. spin_unlock_irqrestore(&sub->lock, flags);
  469. return 0;
  470. default:
  471. return -EINVAL;
  472. }
  473. }
  474. static snd_pcm_uframes_t usb6fire_pcm_pointer(
  475. struct snd_pcm_substream *alsa_sub)
  476. {
  477. struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
  478. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  479. unsigned long flags;
  480. snd_pcm_uframes_t ret;
  481. if (rt->panic || !sub)
  482. return SNDRV_PCM_POS_XRUN;
  483. spin_lock_irqsave(&sub->lock, flags);
  484. ret = sub->dma_off;
  485. spin_unlock_irqrestore(&sub->lock, flags);
  486. return ret;
  487. }
  488. static struct snd_pcm_ops pcm_ops = {
  489. .open = usb6fire_pcm_open,
  490. .close = usb6fire_pcm_close,
  491. .ioctl = snd_pcm_lib_ioctl,
  492. .hw_params = usb6fire_pcm_hw_params,
  493. .hw_free = usb6fire_pcm_hw_free,
  494. .prepare = usb6fire_pcm_prepare,
  495. .trigger = usb6fire_pcm_trigger,
  496. .pointer = usb6fire_pcm_pointer,
  497. };
  498. static void __devinit usb6fire_pcm_init_urb(struct pcm_urb *urb,
  499. struct sfire_chip *chip, bool in, int ep,
  500. void (*handler)(struct urb *))
  501. {
  502. urb->chip = chip;
  503. usb_init_urb(&urb->instance);
  504. urb->instance.transfer_buffer = urb->buffer;
  505. urb->instance.transfer_buffer_length =
  506. PCM_N_PACKETS_PER_URB * PCM_MAX_PACKET_SIZE;
  507. urb->instance.dev = chip->dev;
  508. urb->instance.pipe = in ? usb_rcvisocpipe(chip->dev, ep)
  509. : usb_sndisocpipe(chip->dev, ep);
  510. urb->instance.interval = 1;
  511. urb->instance.transfer_flags = URB_ISO_ASAP;
  512. urb->instance.complete = handler;
  513. urb->instance.context = urb;
  514. urb->instance.number_of_packets = PCM_N_PACKETS_PER_URB;
  515. }
  516. static int usb6fire_pcm_buffers_init(struct pcm_runtime *rt)
  517. {
  518. int i;
  519. for (i = 0; i < PCM_N_URBS; i++) {
  520. rt->out_urbs[i].buffer = kzalloc(PCM_N_PACKETS_PER_URB
  521. * PCM_MAX_PACKET_SIZE, GFP_KERNEL);
  522. if (!rt->out_urbs[i].buffer)
  523. return -ENOMEM;
  524. rt->in_urbs[i].buffer = kzalloc(PCM_N_PACKETS_PER_URB
  525. * PCM_MAX_PACKET_SIZE, GFP_KERNEL);
  526. if (!rt->in_urbs[i].buffer)
  527. return -ENOMEM;
  528. }
  529. return 0;
  530. }
  531. static void usb6fire_pcm_buffers_destroy(struct pcm_runtime *rt)
  532. {
  533. int i;
  534. for (i = 0; i < PCM_N_URBS; i++) {
  535. kfree(rt->out_urbs[i].buffer);
  536. kfree(rt->in_urbs[i].buffer);
  537. }
  538. }
  539. int __devinit usb6fire_pcm_init(struct sfire_chip *chip)
  540. {
  541. int i;
  542. int ret;
  543. struct snd_pcm *pcm;
  544. struct pcm_runtime *rt =
  545. kzalloc(sizeof(struct pcm_runtime), GFP_KERNEL);
  546. if (!rt)
  547. return -ENOMEM;
  548. ret = usb6fire_pcm_buffers_init(rt);
  549. if (ret) {
  550. usb6fire_pcm_buffers_destroy(rt);
  551. kfree(rt);
  552. return ret;
  553. }
  554. rt->chip = chip;
  555. rt->stream_state = STREAM_DISABLED;
  556. rt->rate = ARRAY_SIZE(rates);
  557. init_waitqueue_head(&rt->stream_wait_queue);
  558. mutex_init(&rt->stream_mutex);
  559. spin_lock_init(&rt->playback.lock);
  560. spin_lock_init(&rt->capture.lock);
  561. for (i = 0; i < PCM_N_URBS; i++) {
  562. usb6fire_pcm_init_urb(&rt->in_urbs[i], chip, true, IN_EP,
  563. usb6fire_pcm_in_urb_handler);
  564. usb6fire_pcm_init_urb(&rt->out_urbs[i], chip, false, OUT_EP,
  565. usb6fire_pcm_out_urb_handler);
  566. rt->in_urbs[i].peer = &rt->out_urbs[i];
  567. rt->out_urbs[i].peer = &rt->in_urbs[i];
  568. }
  569. ret = snd_pcm_new(chip->card, "DMX6FireUSB", 0, 1, 1, &pcm);
  570. if (ret < 0) {
  571. usb6fire_pcm_buffers_destroy(rt);
  572. kfree(rt);
  573. snd_printk(KERN_ERR PREFIX "cannot create pcm instance.\n");
  574. return ret;
  575. }
  576. pcm->private_data = rt;
  577. strcpy(pcm->name, "DMX 6Fire USB");
  578. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
  579. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);
  580. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  581. SNDRV_DMA_TYPE_CONTINUOUS,
  582. snd_dma_continuous_data(GFP_KERNEL),
  583. MAX_BUFSIZE, MAX_BUFSIZE);
  584. if (ret) {
  585. usb6fire_pcm_buffers_destroy(rt);
  586. kfree(rt);
  587. snd_printk(KERN_ERR PREFIX
  588. "error preallocating pcm buffers.\n");
  589. return ret;
  590. }
  591. rt->instance = pcm;
  592. chip->pcm = rt;
  593. return 0;
  594. }
  595. void usb6fire_pcm_abort(struct sfire_chip *chip)
  596. {
  597. struct pcm_runtime *rt = chip->pcm;
  598. unsigned long flags;
  599. int i;
  600. if (rt) {
  601. rt->panic = true;
  602. if (rt->playback.instance) {
  603. snd_pcm_stream_lock_irqsave(rt->playback.instance, flags);
  604. snd_pcm_stop(rt->playback.instance,
  605. SNDRV_PCM_STATE_XRUN);
  606. snd_pcm_stream_unlock_irqrestore(rt->playback.instance, flags);
  607. }
  608. if (rt->capture.instance) {
  609. snd_pcm_stream_lock_irqsave(rt->capture.instance, flags);
  610. snd_pcm_stop(rt->capture.instance,
  611. SNDRV_PCM_STATE_XRUN);
  612. snd_pcm_stream_unlock_irqrestore(rt->capture.instance, flags);
  613. }
  614. for (i = 0; i < PCM_N_URBS; i++) {
  615. usb_poison_urb(&rt->in_urbs[i].instance);
  616. usb_poison_urb(&rt->out_urbs[i].instance);
  617. }
  618. }
  619. }
  620. void usb6fire_pcm_destroy(struct sfire_chip *chip)
  621. {
  622. struct pcm_runtime *rt = chip->pcm;
  623. usb6fire_pcm_buffers_destroy(rt);
  624. kfree(rt);
  625. chip->pcm = NULL;
  626. }