amdtp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
  3. * with Common Isochronous Packet (IEC 61883-1) headers
  4. *
  5. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/firewire.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <sound/pcm.h>
  14. #include "amdtp.h"
  15. #define TICKS_PER_CYCLE 3072
  16. #define CYCLES_PER_SECOND 8000
  17. #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
  18. #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
  19. #define TAG_CIP 1
  20. #define CIP_EOH (1u << 31)
  21. #define CIP_FMT_AM (0x10 << 24)
  22. #define AMDTP_FDF_AM824 (0 << 19)
  23. #define AMDTP_FDF_SFC_SHIFT 16
  24. /* TODO: make these configurable */
  25. #define INTERRUPT_INTERVAL 16
  26. #define QUEUE_LENGTH 48
  27. /**
  28. * amdtp_out_stream_init - initialize an AMDTP output stream structure
  29. * @s: the AMDTP output stream to initialize
  30. * @unit: the target of the stream
  31. * @flags: the packet transmission method to use
  32. */
  33. int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
  34. enum cip_out_flags flags)
  35. {
  36. if (flags != CIP_NONBLOCKING)
  37. return -EINVAL;
  38. s->unit = fw_unit_get(unit);
  39. s->flags = flags;
  40. s->context = ERR_PTR(-1);
  41. mutex_init(&s->mutex);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL(amdtp_out_stream_init);
  45. /**
  46. * amdtp_out_stream_destroy - free stream resources
  47. * @s: the AMDTP output stream to destroy
  48. */
  49. void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
  50. {
  51. WARN_ON(!IS_ERR(s->context));
  52. mutex_destroy(&s->mutex);
  53. fw_unit_put(s->unit);
  54. }
  55. EXPORT_SYMBOL(amdtp_out_stream_destroy);
  56. /**
  57. * amdtp_out_stream_set_rate - set the sample rate
  58. * @s: the AMDTP output stream to configure
  59. * @rate: the sample rate
  60. *
  61. * The sample rate must be set before the stream is started, and must not be
  62. * changed while the stream is running.
  63. */
  64. void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
  65. {
  66. static const struct {
  67. unsigned int rate;
  68. unsigned int syt_interval;
  69. } rate_info[] = {
  70. [CIP_SFC_32000] = { 32000, 8, },
  71. [CIP_SFC_44100] = { 44100, 8, },
  72. [CIP_SFC_48000] = { 48000, 8, },
  73. [CIP_SFC_88200] = { 88200, 16, },
  74. [CIP_SFC_96000] = { 96000, 16, },
  75. [CIP_SFC_176400] = { 176400, 32, },
  76. [CIP_SFC_192000] = { 192000, 32, },
  77. };
  78. unsigned int sfc;
  79. if (WARN_ON(!IS_ERR(s->context)))
  80. return;
  81. for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
  82. if (rate_info[sfc].rate == rate) {
  83. s->sfc = sfc;
  84. s->syt_interval = rate_info[sfc].syt_interval;
  85. return;
  86. }
  87. WARN_ON(1);
  88. }
  89. EXPORT_SYMBOL(amdtp_out_stream_set_rate);
  90. /**
  91. * amdtp_out_stream_get_max_payload - get the stream's packet size
  92. * @s: the AMDTP output stream
  93. *
  94. * This function must not be called before the stream has been configured
  95. * with amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
  96. * amdtp_out_stream_set_midi().
  97. */
  98. unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
  99. {
  100. static const unsigned int max_data_blocks[] = {
  101. [CIP_SFC_32000] = 4,
  102. [CIP_SFC_44100] = 6,
  103. [CIP_SFC_48000] = 6,
  104. [CIP_SFC_88200] = 12,
  105. [CIP_SFC_96000] = 12,
  106. [CIP_SFC_176400] = 23,
  107. [CIP_SFC_192000] = 24,
  108. };
  109. s->data_block_quadlets = s->pcm_channels;
  110. s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
  111. return 8 + max_data_blocks[s->sfc] * 4 * s->data_block_quadlets;
  112. }
  113. EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
  114. static void amdtp_write_s16(struct amdtp_out_stream *s,
  115. struct snd_pcm_substream *pcm,
  116. __be32 *buffer, unsigned int frames);
  117. static void amdtp_write_s32(struct amdtp_out_stream *s,
  118. struct snd_pcm_substream *pcm,
  119. __be32 *buffer, unsigned int frames);
  120. /**
  121. * amdtp_out_stream_set_pcm_format - set the PCM format
  122. * @s: the AMDTP output stream to configure
  123. * @format: the format of the ALSA PCM device
  124. *
  125. * The sample format must be set before the stream is started, and must not be
  126. * changed while the stream is running.
  127. */
  128. void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
  129. snd_pcm_format_t format)
  130. {
  131. if (WARN_ON(!IS_ERR(s->context)))
  132. return;
  133. switch (format) {
  134. default:
  135. WARN_ON(1);
  136. /* fall through */
  137. case SNDRV_PCM_FORMAT_S16:
  138. s->transfer_samples = amdtp_write_s16;
  139. break;
  140. case SNDRV_PCM_FORMAT_S32:
  141. s->transfer_samples = amdtp_write_s32;
  142. break;
  143. }
  144. }
  145. EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
  146. static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
  147. {
  148. unsigned int phase, data_blocks;
  149. if (!cip_sfc_is_base_44100(s->sfc)) {
  150. /* Sample_rate / 8000 is an integer, and precomputed. */
  151. data_blocks = s->data_block_state;
  152. } else {
  153. phase = s->data_block_state;
  154. /*
  155. * This calculates the number of data blocks per packet so that
  156. * 1) the overall rate is correct and exactly synchronized to
  157. * the bus clock, and
  158. * 2) packets with a rounded-up number of blocks occur as early
  159. * as possible in the sequence (to prevent underruns of the
  160. * device's buffer).
  161. */
  162. if (s->sfc == CIP_SFC_44100)
  163. /* 6 6 5 6 5 6 5 ... */
  164. data_blocks = 5 + ((phase & 1) ^
  165. (phase == 0 || phase >= 40));
  166. else
  167. /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
  168. data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
  169. if (++phase >= (80 >> (s->sfc >> 1)))
  170. phase = 0;
  171. s->data_block_state = phase;
  172. }
  173. return data_blocks;
  174. }
  175. static unsigned int calculate_syt(struct amdtp_out_stream *s,
  176. unsigned int cycle)
  177. {
  178. unsigned int syt_offset, phase, index, syt;
  179. if (s->last_syt_offset < TICKS_PER_CYCLE) {
  180. if (!cip_sfc_is_base_44100(s->sfc))
  181. syt_offset = s->last_syt_offset + s->syt_offset_state;
  182. else {
  183. /*
  184. * The time, in ticks, of the n'th SYT_INTERVAL sample is:
  185. * n * SYT_INTERVAL * 24576000 / sample_rate
  186. * Modulo TICKS_PER_CYCLE, the difference between successive
  187. * elements is about 1386.23. Rounding the results of this
  188. * formula to the SYT precision results in a sequence of
  189. * differences that begins with:
  190. * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
  191. * This code generates _exactly_ the same sequence.
  192. */
  193. phase = s->syt_offset_state;
  194. index = phase % 13;
  195. syt_offset = s->last_syt_offset;
  196. syt_offset += 1386 + ((index && !(index & 3)) ||
  197. phase == 146);
  198. if (++phase >= 147)
  199. phase = 0;
  200. s->syt_offset_state = phase;
  201. }
  202. } else
  203. syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
  204. s->last_syt_offset = syt_offset;
  205. if (syt_offset < TICKS_PER_CYCLE) {
  206. syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
  207. syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
  208. syt += syt_offset % TICKS_PER_CYCLE;
  209. return syt & 0xffff;
  210. } else {
  211. return 0xffff; /* no info */
  212. }
  213. }
  214. static void amdtp_write_s32(struct amdtp_out_stream *s,
  215. struct snd_pcm_substream *pcm,
  216. __be32 *buffer, unsigned int frames)
  217. {
  218. struct snd_pcm_runtime *runtime = pcm->runtime;
  219. unsigned int channels, remaining_frames, frame_step, i, c;
  220. const u32 *src;
  221. channels = s->pcm_channels;
  222. src = (void *)runtime->dma_area +
  223. s->pcm_buffer_pointer * (runtime->frame_bits / 8);
  224. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  225. frame_step = s->data_block_quadlets - channels;
  226. for (i = 0; i < frames; ++i) {
  227. for (c = 0; c < channels; ++c) {
  228. *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
  229. src++;
  230. buffer++;
  231. }
  232. buffer += frame_step;
  233. if (--remaining_frames == 0)
  234. src = (void *)runtime->dma_area;
  235. }
  236. }
  237. static void amdtp_write_s16(struct amdtp_out_stream *s,
  238. struct snd_pcm_substream *pcm,
  239. __be32 *buffer, unsigned int frames)
  240. {
  241. struct snd_pcm_runtime *runtime = pcm->runtime;
  242. unsigned int channels, remaining_frames, frame_step, i, c;
  243. const u16 *src;
  244. channels = s->pcm_channels;
  245. src = (void *)runtime->dma_area +
  246. s->pcm_buffer_pointer * (runtime->frame_bits / 8);
  247. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  248. frame_step = s->data_block_quadlets - channels;
  249. for (i = 0; i < frames; ++i) {
  250. for (c = 0; c < channels; ++c) {
  251. *buffer = cpu_to_be32((*src << 8) | 0x40000000);
  252. src++;
  253. buffer++;
  254. }
  255. buffer += frame_step;
  256. if (--remaining_frames == 0)
  257. src = (void *)runtime->dma_area;
  258. }
  259. }
  260. static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
  261. __be32 *buffer, unsigned int frames)
  262. {
  263. unsigned int i, c;
  264. for (i = 0; i < frames; ++i) {
  265. for (c = 0; c < s->pcm_channels; ++c)
  266. buffer[c] = cpu_to_be32(0x40000000);
  267. buffer += s->data_block_quadlets;
  268. }
  269. }
  270. static void amdtp_fill_midi(struct amdtp_out_stream *s,
  271. __be32 *buffer, unsigned int frames)
  272. {
  273. unsigned int i;
  274. for (i = 0; i < frames; ++i)
  275. buffer[s->pcm_channels + i * s->data_block_quadlets] =
  276. cpu_to_be32(0x80000000);
  277. }
  278. static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
  279. {
  280. __be32 *buffer;
  281. unsigned int data_blocks, syt, ptr;
  282. struct snd_pcm_substream *pcm;
  283. struct fw_iso_packet packet;
  284. int err;
  285. data_blocks = calculate_data_blocks(s);
  286. syt = calculate_syt(s, cycle);
  287. buffer = s->buffer.packets[s->packet_counter].buffer;
  288. buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
  289. (s->data_block_quadlets << 16) |
  290. s->data_block_counter);
  291. buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
  292. (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
  293. buffer += 2;
  294. pcm = ACCESS_ONCE(s->pcm);
  295. if (pcm)
  296. s->transfer_samples(s, pcm, buffer, data_blocks);
  297. else
  298. amdtp_fill_pcm_silence(s, buffer, data_blocks);
  299. if (s->midi_ports)
  300. amdtp_fill_midi(s, buffer, data_blocks);
  301. s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
  302. packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
  303. packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
  304. INTERRUPT_INTERVAL);
  305. packet.skip = 0;
  306. packet.tag = TAG_CIP;
  307. packet.sy = 0;
  308. packet.header_length = 0;
  309. err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
  310. s->buffer.packets[s->packet_counter].offset);
  311. if (err < 0)
  312. dev_err(&s->unit->device, "queueing error: %d\n", err);
  313. if (++s->packet_counter >= QUEUE_LENGTH)
  314. s->packet_counter = 0;
  315. if (pcm) {
  316. ptr = s->pcm_buffer_pointer + data_blocks;
  317. if (ptr >= pcm->runtime->buffer_size)
  318. ptr -= pcm->runtime->buffer_size;
  319. ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
  320. s->pcm_period_pointer += data_blocks;
  321. if (s->pcm_period_pointer >= pcm->runtime->period_size) {
  322. s->pcm_period_pointer -= pcm->runtime->period_size;
  323. snd_pcm_period_elapsed(pcm);
  324. }
  325. }
  326. }
  327. static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
  328. size_t header_length, void *header, void *data)
  329. {
  330. struct amdtp_out_stream *s = data;
  331. unsigned int i, packets = header_length / 4;
  332. /*
  333. * Compute the cycle of the last queued packet.
  334. * (We need only the four lowest bits for the SYT, so we can ignore
  335. * that bits 0-11 must wrap around at 3072.)
  336. */
  337. cycle += QUEUE_LENGTH - packets;
  338. for (i = 0; i < packets; ++i)
  339. queue_out_packet(s, ++cycle);
  340. }
  341. static int queue_initial_skip_packets(struct amdtp_out_stream *s)
  342. {
  343. struct fw_iso_packet skip_packet = {
  344. .skip = 1,
  345. };
  346. unsigned int i;
  347. int err;
  348. for (i = 0; i < QUEUE_LENGTH; ++i) {
  349. skip_packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
  350. INTERRUPT_INTERVAL);
  351. err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
  352. if (err < 0)
  353. return err;
  354. if (++s->packet_counter >= QUEUE_LENGTH)
  355. s->packet_counter = 0;
  356. }
  357. return 0;
  358. }
  359. /**
  360. * amdtp_out_stream_start - start sending packets
  361. * @s: the AMDTP output stream to start
  362. * @channel: the isochronous channel on the bus
  363. * @speed: firewire speed code
  364. *
  365. * The stream cannot be started until it has been configured with
  366. * amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
  367. * amdtp_out_stream_set_midi(); and it must be started before any
  368. * PCM or MIDI device can be started.
  369. */
  370. int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
  371. {
  372. static const struct {
  373. unsigned int data_block;
  374. unsigned int syt_offset;
  375. } initial_state[] = {
  376. [CIP_SFC_32000] = { 4, 3072 },
  377. [CIP_SFC_48000] = { 6, 1024 },
  378. [CIP_SFC_96000] = { 12, 1024 },
  379. [CIP_SFC_192000] = { 24, 1024 },
  380. [CIP_SFC_44100] = { 0, 67 },
  381. [CIP_SFC_88200] = { 0, 67 },
  382. [CIP_SFC_176400] = { 0, 67 },
  383. };
  384. int err;
  385. mutex_lock(&s->mutex);
  386. if (WARN_ON(!IS_ERR(s->context) ||
  387. (!s->pcm_channels && !s->midi_ports))) {
  388. err = -EBADFD;
  389. goto err_unlock;
  390. }
  391. s->data_block_state = initial_state[s->sfc].data_block;
  392. s->syt_offset_state = initial_state[s->sfc].syt_offset;
  393. s->last_syt_offset = TICKS_PER_CYCLE;
  394. err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
  395. amdtp_out_stream_get_max_payload(s),
  396. DMA_TO_DEVICE);
  397. if (err < 0)
  398. goto err_unlock;
  399. s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
  400. FW_ISO_CONTEXT_TRANSMIT,
  401. channel, speed, 0,
  402. out_packet_callback, s);
  403. if (IS_ERR(s->context)) {
  404. err = PTR_ERR(s->context);
  405. if (err == -EBUSY)
  406. dev_err(&s->unit->device,
  407. "no free output stream on this controller\n");
  408. goto err_buffer;
  409. }
  410. amdtp_out_stream_update(s);
  411. s->packet_counter = 0;
  412. s->data_block_counter = 0;
  413. err = queue_initial_skip_packets(s);
  414. if (err < 0)
  415. goto err_context;
  416. err = fw_iso_context_start(s->context, -1, 0, 0);
  417. if (err < 0)
  418. goto err_context;
  419. mutex_unlock(&s->mutex);
  420. return 0;
  421. err_context:
  422. fw_iso_context_destroy(s->context);
  423. s->context = ERR_PTR(-1);
  424. err_buffer:
  425. iso_packets_buffer_destroy(&s->buffer, s->unit);
  426. err_unlock:
  427. mutex_unlock(&s->mutex);
  428. return err;
  429. }
  430. EXPORT_SYMBOL(amdtp_out_stream_start);
  431. /**
  432. * amdtp_out_stream_update - update the stream after a bus reset
  433. * @s: the AMDTP output stream
  434. */
  435. void amdtp_out_stream_update(struct amdtp_out_stream *s)
  436. {
  437. ACCESS_ONCE(s->source_node_id_field) =
  438. (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
  439. }
  440. EXPORT_SYMBOL(amdtp_out_stream_update);
  441. /**
  442. * amdtp_out_stream_stop - stop sending packets
  443. * @s: the AMDTP output stream to stop
  444. *
  445. * All PCM and MIDI devices of the stream must be stopped before the stream
  446. * itself can be stopped.
  447. */
  448. void amdtp_out_stream_stop(struct amdtp_out_stream *s)
  449. {
  450. mutex_lock(&s->mutex);
  451. if (IS_ERR(s->context)) {
  452. mutex_unlock(&s->mutex);
  453. return;
  454. }
  455. fw_iso_context_stop(s->context);
  456. fw_iso_context_destroy(s->context);
  457. s->context = ERR_PTR(-1);
  458. iso_packets_buffer_destroy(&s->buffer, s->unit);
  459. mutex_unlock(&s->mutex);
  460. }
  461. EXPORT_SYMBOL(amdtp_out_stream_stop);
  462. /**
  463. * amdtp_out_stream_pcm_abort - abort the running PCM device
  464. * @s: the AMDTP stream about to be stopped
  465. *
  466. * If the isochronous stream needs to be stopped asynchronously, call this
  467. * function first to stop the PCM device.
  468. */
  469. void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
  470. {
  471. struct snd_pcm_substream *pcm;
  472. pcm = ACCESS_ONCE(s->pcm);
  473. if (pcm) {
  474. snd_pcm_stream_lock_irq(pcm);
  475. if (snd_pcm_running(pcm))
  476. snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
  477. snd_pcm_stream_unlock_irq(pcm);
  478. }
  479. }
  480. EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);