amdtp.c 16 KB

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