amdtp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. syt = calculate_syt(s, cycle);
  389. if (!(s->flags & CIP_BLOCKING)) {
  390. data_blocks = calculate_data_blocks(s);
  391. } else {
  392. if (syt != 0xffff) {
  393. data_blocks = s->syt_interval;
  394. } else {
  395. data_blocks = 0;
  396. syt = 0xffffff;
  397. }
  398. }
  399. buffer = s->buffer.packets[index].buffer;
  400. buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
  401. (s->data_block_quadlets << 16) |
  402. s->data_block_counter);
  403. buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
  404. (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
  405. buffer += 2;
  406. pcm = ACCESS_ONCE(s->pcm);
  407. if (pcm)
  408. s->transfer_samples(s, pcm, buffer, data_blocks);
  409. else
  410. amdtp_fill_pcm_silence(s, buffer, data_blocks);
  411. if (s->midi_ports)
  412. amdtp_fill_midi(s, buffer, data_blocks);
  413. s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
  414. packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
  415. packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
  416. packet.skip = 0;
  417. packet.tag = TAG_CIP;
  418. packet.sy = 0;
  419. packet.header_length = 0;
  420. err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
  421. s->buffer.packets[index].offset);
  422. if (err < 0) {
  423. dev_err(&s->unit->device, "queueing error: %d\n", err);
  424. s->packet_index = -1;
  425. amdtp_out_stream_pcm_abort(s);
  426. return;
  427. }
  428. if (++index >= QUEUE_LENGTH)
  429. index = 0;
  430. s->packet_index = index;
  431. if (pcm) {
  432. if (s->dual_wire)
  433. data_blocks *= 2;
  434. ptr = s->pcm_buffer_pointer + data_blocks;
  435. if (ptr >= pcm->runtime->buffer_size)
  436. ptr -= pcm->runtime->buffer_size;
  437. ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
  438. s->pcm_period_pointer += data_blocks;
  439. if (s->pcm_period_pointer >= pcm->runtime->period_size) {
  440. s->pcm_period_pointer -= pcm->runtime->period_size;
  441. s->pointer_flush = false;
  442. tasklet_hi_schedule(&s->period_tasklet);
  443. }
  444. }
  445. }
  446. static void pcm_period_tasklet(unsigned long data)
  447. {
  448. struct amdtp_out_stream *s = (void *)data;
  449. struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
  450. if (pcm)
  451. snd_pcm_period_elapsed(pcm);
  452. }
  453. static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
  454. size_t header_length, void *header, void *data)
  455. {
  456. struct amdtp_out_stream *s = data;
  457. unsigned int i, packets = header_length / 4;
  458. /*
  459. * Compute the cycle of the last queued packet.
  460. * (We need only the four lowest bits for the SYT, so we can ignore
  461. * that bits 0-11 must wrap around at 3072.)
  462. */
  463. cycle += QUEUE_LENGTH - packets;
  464. for (i = 0; i < packets; ++i)
  465. queue_out_packet(s, ++cycle);
  466. fw_iso_context_queue_flush(s->context);
  467. }
  468. static int queue_initial_skip_packets(struct amdtp_out_stream *s)
  469. {
  470. struct fw_iso_packet skip_packet = {
  471. .skip = 1,
  472. };
  473. unsigned int i;
  474. int err;
  475. for (i = 0; i < QUEUE_LENGTH; ++i) {
  476. skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
  477. INTERRUPT_INTERVAL);
  478. err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
  479. if (err < 0)
  480. return err;
  481. if (++s->packet_index >= QUEUE_LENGTH)
  482. s->packet_index = 0;
  483. }
  484. return 0;
  485. }
  486. /**
  487. * amdtp_out_stream_start - start sending packets
  488. * @s: the AMDTP output stream to start
  489. * @channel: the isochronous channel on the bus
  490. * @speed: firewire speed code
  491. *
  492. * The stream cannot be started until it has been configured with
  493. * amdtp_out_stream_set_parameters() and amdtp_out_stream_set_pcm_format(),
  494. * and it must be started before any PCM or MIDI device can be started.
  495. */
  496. int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
  497. {
  498. static const struct {
  499. unsigned int data_block;
  500. unsigned int syt_offset;
  501. } initial_state[] = {
  502. [CIP_SFC_32000] = { 4, 3072 },
  503. [CIP_SFC_48000] = { 6, 1024 },
  504. [CIP_SFC_96000] = { 12, 1024 },
  505. [CIP_SFC_192000] = { 24, 1024 },
  506. [CIP_SFC_44100] = { 0, 67 },
  507. [CIP_SFC_88200] = { 0, 67 },
  508. [CIP_SFC_176400] = { 0, 67 },
  509. };
  510. int err;
  511. mutex_lock(&s->mutex);
  512. if (WARN_ON(amdtp_out_stream_running(s) ||
  513. (!s->pcm_channels && !s->midi_ports))) {
  514. err = -EBADFD;
  515. goto err_unlock;
  516. }
  517. s->data_block_state = initial_state[s->sfc].data_block;
  518. s->syt_offset_state = initial_state[s->sfc].syt_offset;
  519. s->last_syt_offset = TICKS_PER_CYCLE;
  520. err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
  521. amdtp_out_stream_get_max_payload(s),
  522. DMA_TO_DEVICE);
  523. if (err < 0)
  524. goto err_unlock;
  525. s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
  526. FW_ISO_CONTEXT_TRANSMIT,
  527. channel, speed, 0,
  528. out_packet_callback, s);
  529. if (IS_ERR(s->context)) {
  530. err = PTR_ERR(s->context);
  531. if (err == -EBUSY)
  532. dev_err(&s->unit->device,
  533. "no free output stream on this controller\n");
  534. goto err_buffer;
  535. }
  536. amdtp_out_stream_update(s);
  537. s->packet_index = 0;
  538. s->data_block_counter = 0;
  539. err = queue_initial_skip_packets(s);
  540. if (err < 0)
  541. goto err_context;
  542. err = fw_iso_context_start(s->context, -1, 0, 0);
  543. if (err < 0)
  544. goto err_context;
  545. mutex_unlock(&s->mutex);
  546. return 0;
  547. err_context:
  548. fw_iso_context_destroy(s->context);
  549. s->context = ERR_PTR(-1);
  550. err_buffer:
  551. iso_packets_buffer_destroy(&s->buffer, s->unit);
  552. err_unlock:
  553. mutex_unlock(&s->mutex);
  554. return err;
  555. }
  556. EXPORT_SYMBOL(amdtp_out_stream_start);
  557. /**
  558. * amdtp_out_stream_pcm_pointer - get the PCM buffer position
  559. * @s: the AMDTP output stream that transports the PCM data
  560. *
  561. * Returns the current buffer position, in frames.
  562. */
  563. unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)
  564. {
  565. /* this optimization is allowed to be racy */
  566. if (s->pointer_flush)
  567. fw_iso_context_flush_completions(s->context);
  568. else
  569. s->pointer_flush = true;
  570. return ACCESS_ONCE(s->pcm_buffer_pointer);
  571. }
  572. EXPORT_SYMBOL(amdtp_out_stream_pcm_pointer);
  573. /**
  574. * amdtp_out_stream_update - update the stream after a bus reset
  575. * @s: the AMDTP output stream
  576. */
  577. void amdtp_out_stream_update(struct amdtp_out_stream *s)
  578. {
  579. ACCESS_ONCE(s->source_node_id_field) =
  580. (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
  581. }
  582. EXPORT_SYMBOL(amdtp_out_stream_update);
  583. /**
  584. * amdtp_out_stream_stop - stop sending packets
  585. * @s: the AMDTP output stream to stop
  586. *
  587. * All PCM and MIDI devices of the stream must be stopped before the stream
  588. * itself can be stopped.
  589. */
  590. void amdtp_out_stream_stop(struct amdtp_out_stream *s)
  591. {
  592. mutex_lock(&s->mutex);
  593. if (!amdtp_out_stream_running(s)) {
  594. mutex_unlock(&s->mutex);
  595. return;
  596. }
  597. tasklet_kill(&s->period_tasklet);
  598. fw_iso_context_stop(s->context);
  599. fw_iso_context_destroy(s->context);
  600. s->context = ERR_PTR(-1);
  601. iso_packets_buffer_destroy(&s->buffer, s->unit);
  602. mutex_unlock(&s->mutex);
  603. }
  604. EXPORT_SYMBOL(amdtp_out_stream_stop);
  605. /**
  606. * amdtp_out_stream_pcm_abort - abort the running PCM device
  607. * @s: the AMDTP stream about to be stopped
  608. *
  609. * If the isochronous stream needs to be stopped asynchronously, call this
  610. * function first to stop the PCM device.
  611. */
  612. void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
  613. {
  614. struct snd_pcm_substream *pcm;
  615. pcm = ACCESS_ONCE(s->pcm);
  616. if (pcm) {
  617. snd_pcm_stream_lock_irq(pcm);
  618. if (snd_pcm_running(pcm))
  619. snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
  620. snd_pcm_stream_unlock_irq(pcm);
  621. }
  622. }
  623. EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);