amdtp.c 19 KB

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