pcm.c 18 KB

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