amdtp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
  206. syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
  207. syt += syt_offset % TICKS_PER_CYCLE;
  208. return syt & 0xffff;
  209. }
  210. static void amdtp_write_s32(struct amdtp_out_stream *s,
  211. struct snd_pcm_substream *pcm,
  212. __be32 *buffer, unsigned int frames)
  213. {
  214. struct snd_pcm_runtime *runtime = pcm->runtime;
  215. unsigned int channels, remaining_frames, frame_step, i, c;
  216. const u32 *src;
  217. channels = s->pcm_channels;
  218. src = (void *)runtime->dma_area +
  219. s->pcm_buffer_pointer * (runtime->frame_bits / 8);
  220. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  221. frame_step = s->data_block_quadlets - channels;
  222. for (i = 0; i < frames; ++i) {
  223. for (c = 0; c < channels; ++c) {
  224. *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
  225. src++;
  226. buffer++;
  227. }
  228. buffer += frame_step;
  229. if (--remaining_frames == 0)
  230. src = (void *)runtime->dma_area;
  231. }
  232. }
  233. static void amdtp_write_s16(struct amdtp_out_stream *s,
  234. struct snd_pcm_substream *pcm,
  235. __be32 *buffer, unsigned int frames)
  236. {
  237. struct snd_pcm_runtime *runtime = pcm->runtime;
  238. unsigned int channels, remaining_frames, frame_step, i, c;
  239. const u16 *src;
  240. channels = s->pcm_channels;
  241. src = (void *)runtime->dma_area +
  242. s->pcm_buffer_pointer * (runtime->frame_bits / 8);
  243. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  244. frame_step = s->data_block_quadlets - channels;
  245. for (i = 0; i < frames; ++i) {
  246. for (c = 0; c < channels; ++c) {
  247. *buffer = cpu_to_be32((*src << 8) | 0x40000000);
  248. src++;
  249. buffer++;
  250. }
  251. buffer += frame_step;
  252. if (--remaining_frames == 0)
  253. src = (void *)runtime->dma_area;
  254. }
  255. }
  256. static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
  257. __be32 *buffer, unsigned int frames)
  258. {
  259. unsigned int i, c;
  260. for (i = 0; i < frames; ++i) {
  261. for (c = 0; c < s->pcm_channels; ++c)
  262. buffer[c] = cpu_to_be32(0x40000000);
  263. buffer += s->data_block_quadlets;
  264. }
  265. }
  266. static void amdtp_fill_midi(struct amdtp_out_stream *s,
  267. __be32 *buffer, unsigned int frames)
  268. {
  269. unsigned int i;
  270. for (i = 0; i < frames; ++i)
  271. buffer[s->pcm_channels + i * s->data_block_quadlets] =
  272. cpu_to_be32(0x80000000);
  273. }
  274. static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
  275. {
  276. __be32 *buffer;
  277. unsigned int data_blocks, syt, ptr;
  278. struct snd_pcm_substream *pcm;
  279. struct fw_iso_packet packet;
  280. int err;
  281. data_blocks = calculate_data_blocks(s);
  282. syt = calculate_syt(s, cycle);
  283. buffer = s->buffer.packets[s->packet_counter].buffer;
  284. buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
  285. (s->data_block_quadlets << 16) |
  286. s->data_block_counter);
  287. buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
  288. (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
  289. buffer += 2;
  290. pcm = ACCESS_ONCE(s->pcm);
  291. if (pcm)
  292. s->transfer_samples(s, pcm, buffer, data_blocks);
  293. else
  294. amdtp_fill_pcm_silence(s, buffer, data_blocks);
  295. if (s->midi_ports)
  296. amdtp_fill_midi(s, buffer, data_blocks);
  297. s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
  298. packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
  299. packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
  300. INTERRUPT_INTERVAL);
  301. packet.skip = 0;
  302. packet.tag = TAG_CIP;
  303. packet.sy = 0;
  304. packet.header_length = 0;
  305. err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
  306. s->buffer.packets[s->packet_counter].offset);
  307. if (err < 0)
  308. dev_err(&s->unit->device, "queueing error: %d\n", err);
  309. if (++s->packet_counter >= QUEUE_LENGTH)
  310. s->packet_counter = 0;
  311. if (pcm) {
  312. ptr = s->pcm_buffer_pointer + data_blocks;
  313. if (ptr >= pcm->runtime->buffer_size)
  314. ptr -= pcm->runtime->buffer_size;
  315. ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
  316. s->pcm_period_pointer += data_blocks;
  317. if (s->pcm_period_pointer >= pcm->runtime->period_size) {
  318. s->pcm_period_pointer -= pcm->runtime->period_size;
  319. snd_pcm_period_elapsed(pcm);
  320. }
  321. }
  322. }
  323. static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
  324. size_t header_length, void *header, void *data)
  325. {
  326. struct amdtp_out_stream *s = data;
  327. unsigned int i, packets = header_length / 4;
  328. /*
  329. * Compute the cycle of the last queued packet.
  330. * (We need only the four lowest bits for the SYT, so we can ignore
  331. * that bits 0-11 must wrap around at 3072.)
  332. */
  333. cycle += QUEUE_LENGTH - packets;
  334. for (i = 0; i < packets; ++i)
  335. queue_out_packet(s, ++cycle);
  336. }
  337. static int queue_initial_skip_packets(struct amdtp_out_stream *s)
  338. {
  339. struct fw_iso_packet skip_packet = {
  340. .skip = 1,
  341. };
  342. unsigned int i;
  343. int err;
  344. for (i = 0; i < QUEUE_LENGTH; ++i) {
  345. skip_packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
  346. INTERRUPT_INTERVAL);
  347. err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
  348. if (err < 0)
  349. return err;
  350. if (++s->packet_counter >= QUEUE_LENGTH)
  351. s->packet_counter = 0;
  352. }
  353. return 0;
  354. }
  355. /**
  356. * amdtp_out_stream_start - start sending packets
  357. * @s: the AMDTP output stream to start
  358. * @channel: the isochronous channel on the bus
  359. * @speed: firewire speed code
  360. *
  361. * The stream cannot be started until it has been configured with
  362. * amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
  363. * amdtp_out_stream_set_midi(); and it must be started before any
  364. * PCM or MIDI device can be started.
  365. */
  366. int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
  367. {
  368. static const struct {
  369. unsigned int data_block;
  370. unsigned int syt_offset;
  371. } initial_state[] = {
  372. [CIP_SFC_32000] = { 4, 3072 },
  373. [CIP_SFC_48000] = { 6, 1024 },
  374. [CIP_SFC_96000] = { 12, 1024 },
  375. [CIP_SFC_192000] = { 24, 1024 },
  376. [CIP_SFC_44100] = { 0, 67 },
  377. [CIP_SFC_88200] = { 0, 67 },
  378. [CIP_SFC_176400] = { 0, 67 },
  379. };
  380. int err;
  381. mutex_lock(&s->mutex);
  382. if (WARN_ON(!IS_ERR(s->context) ||
  383. (!s->pcm_channels && !s->midi_ports))) {
  384. err = -EBADFD;
  385. goto err_unlock;
  386. }
  387. s->data_block_state = initial_state[s->sfc].data_block;
  388. s->syt_offset_state = initial_state[s->sfc].syt_offset;
  389. s->last_syt_offset = TICKS_PER_CYCLE;
  390. err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
  391. amdtp_out_stream_get_max_payload(s),
  392. DMA_TO_DEVICE);
  393. if (err < 0)
  394. goto err_unlock;
  395. s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
  396. FW_ISO_CONTEXT_TRANSMIT,
  397. channel, speed, 0,
  398. out_packet_callback, s);
  399. if (IS_ERR(s->context)) {
  400. err = PTR_ERR(s->context);
  401. if (err == -EBUSY)
  402. dev_err(&s->unit->device,
  403. "no free output stream on this controller\n");
  404. goto err_buffer;
  405. }
  406. amdtp_out_stream_update(s);
  407. s->packet_counter = 0;
  408. s->data_block_counter = 0;
  409. err = queue_initial_skip_packets(s);
  410. if (err < 0)
  411. goto err_context;
  412. err = fw_iso_context_start(s->context, -1, 0, 0);
  413. if (err < 0)
  414. goto err_context;
  415. mutex_unlock(&s->mutex);
  416. return 0;
  417. err_context:
  418. fw_iso_context_destroy(s->context);
  419. s->context = ERR_PTR(-1);
  420. err_buffer:
  421. iso_packets_buffer_destroy(&s->buffer, s->unit);
  422. err_unlock:
  423. mutex_unlock(&s->mutex);
  424. return err;
  425. }
  426. EXPORT_SYMBOL(amdtp_out_stream_start);
  427. /**
  428. * amdtp_out_stream_update - update the stream after a bus reset
  429. * @s: the AMDTP output stream
  430. */
  431. void amdtp_out_stream_update(struct amdtp_out_stream *s)
  432. {
  433. ACCESS_ONCE(s->source_node_id_field) =
  434. (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
  435. }
  436. EXPORT_SYMBOL(amdtp_out_stream_update);
  437. /**
  438. * amdtp_out_stream_stop - stop sending packets
  439. * @s: the AMDTP output stream to stop
  440. *
  441. * All PCM and MIDI devices of the stream must be stopped before the stream
  442. * itself can be stopped.
  443. */
  444. void amdtp_out_stream_stop(struct amdtp_out_stream *s)
  445. {
  446. mutex_lock(&s->mutex);
  447. if (IS_ERR(s->context)) {
  448. mutex_unlock(&s->mutex);
  449. return;
  450. }
  451. fw_iso_context_stop(s->context);
  452. fw_iso_context_destroy(s->context);
  453. s->context = ERR_PTR(-1);
  454. iso_packets_buffer_destroy(&s->buffer, s->unit);
  455. mutex_unlock(&s->mutex);
  456. }
  457. EXPORT_SYMBOL(amdtp_out_stream_stop);
  458. /**
  459. * amdtp_out_stream_pcm_abort - abort the running PCM device
  460. * @s: the AMDTP stream about to be stopped
  461. *
  462. * If the isochronous stream needs to be stopped asynchronously, call this
  463. * function first to stop the PCM device.
  464. */
  465. void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
  466. {
  467. struct snd_pcm_substream *pcm;
  468. pcm = ACCESS_ONCE(s->pcm);
  469. if (pcm) {
  470. snd_pcm_stream_lock_irq(pcm);
  471. if (snd_pcm_running(pcm))
  472. snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
  473. snd_pcm_stream_unlock_irq(pcm);
  474. }
  475. }
  476. EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);