amdtp.c 15 KB

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