pcm.c 14 KB

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