pcm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Linux driver for M2Tech hiFace compatible devices
  3. *
  4. * Copyright 2012-2013 (C) M2TECH S.r.l and Amarula Solutions B.V.
  5. *
  6. * Authors: Michael Trimarchi <michael@amarulasolutions.com>
  7. * Antonio Ospite <ao2@amarulasolutions.com>
  8. *
  9. * The driver is based on the work done in TerraTec DMX 6Fire USB
  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 <linux/slab.h>
  17. #include <sound/pcm.h>
  18. #include "pcm.h"
  19. #include "chip.h"
  20. #define OUT_EP 0x2
  21. #define PCM_N_URBS 8
  22. #define PCM_PACKET_SIZE 4096
  23. #define PCM_BUFFER_SIZE (2 * PCM_N_URBS * PCM_PACKET_SIZE)
  24. struct pcm_urb {
  25. struct hiface_chip *chip;
  26. struct urb instance;
  27. struct usb_anchor submitted;
  28. u8 *buffer;
  29. };
  30. struct pcm_substream {
  31. spinlock_t lock;
  32. struct snd_pcm_substream *instance;
  33. bool active;
  34. snd_pcm_uframes_t dma_off; /* current position in alsa dma_area */
  35. snd_pcm_uframes_t period_off; /* current position in current period */
  36. };
  37. enum { /* pcm streaming states */
  38. STREAM_DISABLED, /* no pcm streaming */
  39. STREAM_STARTING, /* pcm streaming requested, waiting to become ready */
  40. STREAM_RUNNING, /* pcm streaming running */
  41. STREAM_STOPPING
  42. };
  43. struct pcm_runtime {
  44. struct hiface_chip *chip;
  45. struct snd_pcm *instance;
  46. struct pcm_substream playback;
  47. bool panic; /* if set driver won't do anymore pcm on device */
  48. struct pcm_urb out_urbs[PCM_N_URBS];
  49. struct mutex stream_mutex;
  50. u8 stream_state; /* one of STREAM_XXX */
  51. u8 extra_freq;
  52. wait_queue_head_t stream_wait_queue;
  53. bool stream_wait_cond;
  54. };
  55. static const unsigned int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000,
  56. 352800, 384000 };
  57. static const struct snd_pcm_hw_constraint_list constraints_extra_rates = {
  58. .count = ARRAY_SIZE(rates),
  59. .list = rates,
  60. .mask = 0,
  61. };
  62. static const struct snd_pcm_hardware pcm_hw = {
  63. .info = SNDRV_PCM_INFO_MMAP |
  64. SNDRV_PCM_INFO_INTERLEAVED |
  65. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  66. SNDRV_PCM_INFO_PAUSE |
  67. SNDRV_PCM_INFO_MMAP_VALID |
  68. SNDRV_PCM_INFO_BATCH,
  69. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  70. .rates = SNDRV_PCM_RATE_44100 |
  71. SNDRV_PCM_RATE_48000 |
  72. SNDRV_PCM_RATE_88200 |
  73. SNDRV_PCM_RATE_96000 |
  74. SNDRV_PCM_RATE_176400 |
  75. SNDRV_PCM_RATE_192000,
  76. .rate_min = 44100,
  77. .rate_max = 192000, /* changes in hiface_pcm_open to support extra rates */
  78. .channels_min = 2,
  79. .channels_max = 2,
  80. .buffer_bytes_max = PCM_BUFFER_SIZE,
  81. .period_bytes_min = PCM_PACKET_SIZE,
  82. .period_bytes_max = PCM_BUFFER_SIZE,
  83. .periods_min = 2,
  84. .periods_max = 1024
  85. };
  86. /* message values used to change the sample rate */
  87. #define HIFACE_SET_RATE_REQUEST 0xb0
  88. #define HIFACE_RATE_44100 0x43
  89. #define HIFACE_RATE_48000 0x4b
  90. #define HIFACE_RATE_88200 0x42
  91. #define HIFACE_RATE_96000 0x4a
  92. #define HIFACE_RATE_176400 0x40
  93. #define HIFACE_RATE_192000 0x48
  94. #define HIFACE_RATE_352800 0x58
  95. #define HIFACE_RATE_384000 0x68
  96. static int hiface_pcm_set_rate(struct pcm_runtime *rt, unsigned int rate)
  97. {
  98. struct usb_device *device = rt->chip->dev;
  99. u16 rate_value;
  100. int ret;
  101. /* We are already sure that the rate is supported here thanks to
  102. * ALSA constraints
  103. */
  104. switch (rate) {
  105. case 44100:
  106. rate_value = HIFACE_RATE_44100;
  107. break;
  108. case 48000:
  109. rate_value = HIFACE_RATE_48000;
  110. break;
  111. case 88200:
  112. rate_value = HIFACE_RATE_88200;
  113. break;
  114. case 96000:
  115. rate_value = HIFACE_RATE_96000;
  116. break;
  117. case 176400:
  118. rate_value = HIFACE_RATE_176400;
  119. break;
  120. case 192000:
  121. rate_value = HIFACE_RATE_192000;
  122. break;
  123. case 352800:
  124. rate_value = HIFACE_RATE_352800;
  125. break;
  126. case 384000:
  127. rate_value = HIFACE_RATE_384000;
  128. break;
  129. default:
  130. dev_err(&device->dev, "Unsupported rate %d\n", rate);
  131. return -EINVAL;
  132. }
  133. /*
  134. * USBIO: Vendor 0xb0(wValue=0x0043, wIndex=0x0000)
  135. * 43 b0 43 00 00 00 00 00
  136. * USBIO: Vendor 0xb0(wValue=0x004b, wIndex=0x0000)
  137. * 43 b0 4b 00 00 00 00 00
  138. * This control message doesn't have any ack from the
  139. * other side
  140. */
  141. ret = usb_control_msg(device, usb_sndctrlpipe(device, 0),
  142. HIFACE_SET_RATE_REQUEST,
  143. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  144. rate_value, 0, NULL, 0, 100);
  145. if (ret < 0) {
  146. dev_err(&device->dev, "Error setting samplerate %d.\n", rate);
  147. return ret;
  148. }
  149. return 0;
  150. }
  151. static struct pcm_substream *hiface_pcm_get_substream(struct snd_pcm_substream
  152. *alsa_sub)
  153. {
  154. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  155. struct device *device = &rt->chip->dev->dev;
  156. if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  157. return &rt->playback;
  158. dev_err(device, "Error getting pcm substream slot.\n");
  159. return NULL;
  160. }
  161. /* call with stream_mutex locked */
  162. static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
  163. {
  164. int i, time;
  165. if (rt->stream_state != STREAM_DISABLED) {
  166. rt->stream_state = STREAM_STOPPING;
  167. for (i = 0; i < PCM_N_URBS; i++) {
  168. time = usb_wait_anchor_empty_timeout(
  169. &rt->out_urbs[i].submitted, 100);
  170. if (!time)
  171. usb_kill_anchored_urbs(
  172. &rt->out_urbs[i].submitted);
  173. usb_kill_urb(&rt->out_urbs[i].instance);
  174. }
  175. rt->stream_state = STREAM_DISABLED;
  176. }
  177. }
  178. /* call with stream_mutex locked */
  179. static int hiface_pcm_stream_start(struct pcm_runtime *rt)
  180. {
  181. int ret = 0;
  182. int i;
  183. if (rt->stream_state == STREAM_DISABLED) {
  184. /* reset panic state when starting a new stream */
  185. rt->panic = false;
  186. /* submit our out urbs zero init */
  187. rt->stream_state = STREAM_STARTING;
  188. for (i = 0; i < PCM_N_URBS; i++) {
  189. memset(rt->out_urbs[i].buffer, 0, PCM_PACKET_SIZE);
  190. usb_anchor_urb(&rt->out_urbs[i].instance,
  191. &rt->out_urbs[i].submitted);
  192. ret = usb_submit_urb(&rt->out_urbs[i].instance,
  193. GFP_ATOMIC);
  194. if (ret) {
  195. hiface_pcm_stream_stop(rt);
  196. return ret;
  197. }
  198. }
  199. /* wait for first out urb to return (sent in in urb handler) */
  200. wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
  201. HZ);
  202. if (rt->stream_wait_cond) {
  203. struct device *device = &rt->chip->dev->dev;
  204. dev_dbg(device, "%s: Stream is running wakeup event\n",
  205. __func__);
  206. rt->stream_state = STREAM_RUNNING;
  207. } else {
  208. hiface_pcm_stream_stop(rt);
  209. return -EIO;
  210. }
  211. }
  212. return ret;
  213. }
  214. /* The hardware wants word-swapped 32-bit values */
  215. static void memcpy_swahw32(u8 *dest, u8 *src, unsigned int n)
  216. {
  217. unsigned int i;
  218. for (i = 0; i < n / 4; i++)
  219. ((u32 *)dest)[i] = swahw32(((u32 *)src)[i]);
  220. }
  221. /* call with substream locked */
  222. /* returns true if a period elapsed */
  223. static bool hiface_pcm_playback(struct pcm_substream *sub, struct pcm_urb *urb)
  224. {
  225. struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
  226. struct device *device = &urb->chip->dev->dev;
  227. u8 *source;
  228. unsigned int pcm_buffer_size;
  229. WARN_ON(alsa_rt->format != SNDRV_PCM_FORMAT_S32_LE);
  230. pcm_buffer_size = snd_pcm_lib_buffer_bytes(sub->instance);
  231. if (sub->dma_off + PCM_PACKET_SIZE <= pcm_buffer_size) {
  232. dev_dbg(device, "%s: (1) buffer_size %#x dma_offset %#x\n", __func__,
  233. (unsigned int) pcm_buffer_size,
  234. (unsigned int) sub->dma_off);
  235. source = alsa_rt->dma_area + sub->dma_off;
  236. memcpy_swahw32(urb->buffer, source, PCM_PACKET_SIZE);
  237. } else {
  238. /* wrap around at end of ring buffer */
  239. unsigned int len;
  240. dev_dbg(device, "%s: (2) buffer_size %#x dma_offset %#x\n", __func__,
  241. (unsigned int) pcm_buffer_size,
  242. (unsigned int) sub->dma_off);
  243. len = pcm_buffer_size - sub->dma_off;
  244. source = alsa_rt->dma_area + sub->dma_off;
  245. memcpy_swahw32(urb->buffer, source, len);
  246. source = alsa_rt->dma_area;
  247. memcpy_swahw32(urb->buffer + len, source,
  248. PCM_PACKET_SIZE - len);
  249. }
  250. sub->dma_off += PCM_PACKET_SIZE;
  251. if (sub->dma_off >= pcm_buffer_size)
  252. sub->dma_off -= pcm_buffer_size;
  253. sub->period_off += PCM_PACKET_SIZE;
  254. if (sub->period_off >= alsa_rt->period_size) {
  255. sub->period_off %= alsa_rt->period_size;
  256. return true;
  257. }
  258. return false;
  259. }
  260. static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
  261. {
  262. struct pcm_urb *out_urb = usb_urb->context;
  263. struct pcm_runtime *rt = out_urb->chip->pcm;
  264. struct pcm_substream *sub;
  265. bool do_period_elapsed = false;
  266. unsigned long flags;
  267. int ret;
  268. if (rt->panic || rt->stream_state == STREAM_STOPPING)
  269. return;
  270. if (unlikely(usb_urb->status == -ENOENT || /* unlinked */
  271. usb_urb->status == -ENODEV || /* device removed */
  272. usb_urb->status == -ECONNRESET || /* unlinked */
  273. usb_urb->status == -ESHUTDOWN)) { /* device disabled */
  274. goto out_fail;
  275. }
  276. if (rt->stream_state == STREAM_STARTING) {
  277. rt->stream_wait_cond = true;
  278. wake_up(&rt->stream_wait_queue);
  279. }
  280. /* now send our playback data (if a free out urb was found) */
  281. sub = &rt->playback;
  282. spin_lock_irqsave(&sub->lock, flags);
  283. if (sub->active)
  284. do_period_elapsed = hiface_pcm_playback(sub, out_urb);
  285. else
  286. memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
  287. spin_unlock_irqrestore(&sub->lock, flags);
  288. if (do_period_elapsed)
  289. snd_pcm_period_elapsed(sub->instance);
  290. ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
  291. if (ret < 0)
  292. goto out_fail;
  293. return;
  294. out_fail:
  295. rt->panic = true;
  296. }
  297. static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
  298. {
  299. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  300. struct pcm_substream *sub = NULL;
  301. struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
  302. int ret;
  303. if (rt->panic)
  304. return -EPIPE;
  305. mutex_lock(&rt->stream_mutex);
  306. alsa_rt->hw = pcm_hw;
  307. if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
  308. sub = &rt->playback;
  309. if (!sub) {
  310. struct device *device = &rt->chip->dev->dev;
  311. mutex_unlock(&rt->stream_mutex);
  312. dev_err(device, "Invalid stream type\n");
  313. return -EINVAL;
  314. }
  315. if (rt->extra_freq) {
  316. alsa_rt->hw.rates |= SNDRV_PCM_RATE_KNOT;
  317. alsa_rt->hw.rate_max = 384000;
  318. /* explicit constraints needed as we added SNDRV_PCM_RATE_KNOT */
  319. ret = snd_pcm_hw_constraint_list(alsa_sub->runtime, 0,
  320. SNDRV_PCM_HW_PARAM_RATE,
  321. &constraints_extra_rates);
  322. if (ret < 0) {
  323. mutex_unlock(&rt->stream_mutex);
  324. return ret;
  325. }
  326. }
  327. sub->instance = alsa_sub;
  328. sub->active = false;
  329. mutex_unlock(&rt->stream_mutex);
  330. return 0;
  331. }
  332. static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
  333. {
  334. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  335. struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
  336. unsigned long flags;
  337. if (rt->panic)
  338. return 0;
  339. mutex_lock(&rt->stream_mutex);
  340. if (sub) {
  341. hiface_pcm_stream_stop(rt);
  342. /* deactivate substream */
  343. spin_lock_irqsave(&sub->lock, flags);
  344. sub->instance = NULL;
  345. sub->active = false;
  346. spin_unlock_irqrestore(&sub->lock, flags);
  347. }
  348. mutex_unlock(&rt->stream_mutex);
  349. return 0;
  350. }
  351. static int hiface_pcm_hw_params(struct snd_pcm_substream *alsa_sub,
  352. struct snd_pcm_hw_params *hw_params)
  353. {
  354. return snd_pcm_lib_alloc_vmalloc_buffer(alsa_sub,
  355. params_buffer_bytes(hw_params));
  356. }
  357. static int hiface_pcm_hw_free(struct snd_pcm_substream *alsa_sub)
  358. {
  359. return snd_pcm_lib_free_vmalloc_buffer(alsa_sub);
  360. }
  361. static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
  362. {
  363. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  364. struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
  365. struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
  366. int ret;
  367. if (rt->panic)
  368. return -EPIPE;
  369. if (!sub)
  370. return -ENODEV;
  371. mutex_lock(&rt->stream_mutex);
  372. hiface_pcm_stream_stop(rt);
  373. sub->dma_off = 0;
  374. sub->period_off = 0;
  375. if (rt->stream_state == STREAM_DISABLED) {
  376. ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
  377. if (ret) {
  378. mutex_unlock(&rt->stream_mutex);
  379. return ret;
  380. }
  381. ret = hiface_pcm_stream_start(rt);
  382. if (ret) {
  383. mutex_unlock(&rt->stream_mutex);
  384. return ret;
  385. }
  386. }
  387. mutex_unlock(&rt->stream_mutex);
  388. return 0;
  389. }
  390. static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
  391. {
  392. struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
  393. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  394. if (rt->panic)
  395. return -EPIPE;
  396. if (!sub)
  397. return -ENODEV;
  398. switch (cmd) {
  399. case SNDRV_PCM_TRIGGER_START:
  400. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  401. spin_lock_irq(&sub->lock);
  402. sub->active = true;
  403. spin_unlock_irq(&sub->lock);
  404. return 0;
  405. case SNDRV_PCM_TRIGGER_STOP:
  406. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  407. spin_lock_irq(&sub->lock);
  408. sub->active = false;
  409. spin_unlock_irq(&sub->lock);
  410. return 0;
  411. default:
  412. return -EINVAL;
  413. }
  414. }
  415. static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
  416. {
  417. struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
  418. struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
  419. unsigned long flags;
  420. snd_pcm_uframes_t dma_offset;
  421. if (rt->panic || !sub)
  422. return SNDRV_PCM_POS_XRUN;
  423. spin_lock_irqsave(&sub->lock, flags);
  424. dma_offset = sub->dma_off;
  425. spin_unlock_irqrestore(&sub->lock, flags);
  426. return bytes_to_frames(alsa_sub->runtime, dma_offset);
  427. }
  428. static struct snd_pcm_ops pcm_ops = {
  429. .open = hiface_pcm_open,
  430. .close = hiface_pcm_close,
  431. .ioctl = snd_pcm_lib_ioctl,
  432. .hw_params = hiface_pcm_hw_params,
  433. .hw_free = hiface_pcm_hw_free,
  434. .prepare = hiface_pcm_prepare,
  435. .trigger = hiface_pcm_trigger,
  436. .pointer = hiface_pcm_pointer,
  437. .page = snd_pcm_lib_get_vmalloc_page,
  438. .mmap = snd_pcm_lib_mmap_vmalloc,
  439. };
  440. static int hiface_pcm_init_urb(struct pcm_urb *urb,
  441. struct hiface_chip *chip,
  442. unsigned int ep,
  443. void (*handler)(struct urb *))
  444. {
  445. urb->chip = chip;
  446. usb_init_urb(&urb->instance);
  447. urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
  448. if (!urb->buffer)
  449. return -ENOMEM;
  450. usb_fill_bulk_urb(&urb->instance, chip->dev,
  451. usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
  452. PCM_PACKET_SIZE, handler, urb);
  453. init_usb_anchor(&urb->submitted);
  454. return 0;
  455. }
  456. void hiface_pcm_abort(struct hiface_chip *chip)
  457. {
  458. struct pcm_runtime *rt = chip->pcm;
  459. if (rt) {
  460. rt->panic = true;
  461. mutex_lock(&rt->stream_mutex);
  462. hiface_pcm_stream_stop(rt);
  463. mutex_unlock(&rt->stream_mutex);
  464. }
  465. }
  466. static void hiface_pcm_destroy(struct hiface_chip *chip)
  467. {
  468. struct pcm_runtime *rt = chip->pcm;
  469. int i;
  470. for (i = 0; i < PCM_N_URBS; i++)
  471. kfree(rt->out_urbs[i].buffer);
  472. kfree(chip->pcm);
  473. chip->pcm = NULL;
  474. }
  475. static void hiface_pcm_free(struct snd_pcm *pcm)
  476. {
  477. struct pcm_runtime *rt = pcm->private_data;
  478. if (rt)
  479. hiface_pcm_destroy(rt->chip);
  480. }
  481. int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
  482. {
  483. int i;
  484. int ret;
  485. struct snd_pcm *pcm;
  486. struct pcm_runtime *rt;
  487. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  488. if (!rt)
  489. return -ENOMEM;
  490. rt->chip = chip;
  491. rt->stream_state = STREAM_DISABLED;
  492. if (extra_freq)
  493. rt->extra_freq = 1;
  494. init_waitqueue_head(&rt->stream_wait_queue);
  495. mutex_init(&rt->stream_mutex);
  496. spin_lock_init(&rt->playback.lock);
  497. for (i = 0; i < PCM_N_URBS; i++)
  498. hiface_pcm_init_urb(&rt->out_urbs[i], chip, OUT_EP,
  499. hiface_pcm_out_urb_handler);
  500. ret = snd_pcm_new(chip->card, "USB-SPDIF Audio", 0, 1, 0, &pcm);
  501. if (ret < 0) {
  502. kfree(rt);
  503. dev_err(&chip->dev->dev, "Cannot create pcm instance\n");
  504. return ret;
  505. }
  506. pcm->private_data = rt;
  507. pcm->private_free = hiface_pcm_free;
  508. strlcpy(pcm->name, "USB-SPDIF Audio", sizeof(pcm->name));
  509. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
  510. rt->instance = pcm;
  511. chip->pcm = rt;
  512. return 0;
  513. }