amdtp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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 <linux/sched.h>
  14. #include <sound/pcm.h>
  15. #include <sound/pcm_params.h>
  16. #include <sound/rawmidi.h>
  17. #include "amdtp.h"
  18. #define TICKS_PER_CYCLE 3072
  19. #define CYCLES_PER_SECOND 8000
  20. #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
  21. /*
  22. * Nominally 3125 bytes/second, but the MIDI port's clock might be
  23. * 1% too slow, and the bus clock 100 ppm too fast.
  24. */
  25. #define MIDI_BYTES_PER_SECOND 3093
  26. /*
  27. * Several devices look only at the first eight data blocks.
  28. * In any case, this is more than enough for the MIDI data rate.
  29. */
  30. #define MAX_MIDI_RX_BLOCKS 8
  31. #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
  32. /* isochronous header parameters */
  33. #define ISO_DATA_LENGTH_SHIFT 16
  34. #define TAG_CIP 1
  35. /* common isochronous packet header parameters */
  36. #define CIP_EOH_SHIFT 31
  37. #define CIP_EOH (1u << CIP_EOH_SHIFT)
  38. #define CIP_EOH_MASK 0x80000000
  39. #define CIP_SID_SHIFT 24
  40. #define CIP_SID_MASK 0x3f000000
  41. #define CIP_DBS_MASK 0x00ff0000
  42. #define CIP_DBS_SHIFT 16
  43. #define CIP_DBC_MASK 0x000000ff
  44. #define CIP_FMT_SHIFT 24
  45. #define CIP_FMT_MASK 0x3f000000
  46. #define CIP_FDF_MASK 0x00ff0000
  47. #define CIP_FDF_SHIFT 16
  48. #define CIP_SYT_MASK 0x0000ffff
  49. #define CIP_SYT_NO_INFO 0xffff
  50. /*
  51. * Audio and Music transfer protocol specific parameters
  52. * only "Clock-based rate control mode" is supported
  53. */
  54. #define CIP_FMT_AM (0x10 << CIP_FMT_SHIFT)
  55. #define AMDTP_FDF_AM824 (0 << (CIP_FDF_SHIFT + 3))
  56. #define AMDTP_FDF_NO_DATA 0xff
  57. /* TODO: make these configurable */
  58. #define INTERRUPT_INTERVAL 16
  59. #define QUEUE_LENGTH 48
  60. #define IN_PACKET_HEADER_SIZE 4
  61. #define OUT_PACKET_HEADER_SIZE 0
  62. static void pcm_period_tasklet(unsigned long data);
  63. /**
  64. * amdtp_stream_init - initialize an AMDTP stream structure
  65. * @s: the AMDTP stream to initialize
  66. * @unit: the target of the stream
  67. * @dir: the direction of stream
  68. * @flags: the packet transmission method to use
  69. */
  70. int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
  71. enum amdtp_stream_direction dir, enum cip_flags flags)
  72. {
  73. s->unit = unit;
  74. s->direction = dir;
  75. s->flags = flags;
  76. s->context = ERR_PTR(-1);
  77. mutex_init(&s->mutex);
  78. tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
  79. s->packet_index = 0;
  80. init_waitqueue_head(&s->callback_wait);
  81. s->callbacked = false;
  82. s->sync_slave = NULL;
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(amdtp_stream_init);
  86. /**
  87. * amdtp_stream_destroy - free stream resources
  88. * @s: the AMDTP stream to destroy
  89. */
  90. void amdtp_stream_destroy(struct amdtp_stream *s)
  91. {
  92. WARN_ON(amdtp_stream_running(s));
  93. mutex_destroy(&s->mutex);
  94. }
  95. EXPORT_SYMBOL(amdtp_stream_destroy);
  96. const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
  97. [CIP_SFC_32000] = 8,
  98. [CIP_SFC_44100] = 8,
  99. [CIP_SFC_48000] = 8,
  100. [CIP_SFC_88200] = 16,
  101. [CIP_SFC_96000] = 16,
  102. [CIP_SFC_176400] = 32,
  103. [CIP_SFC_192000] = 32,
  104. };
  105. EXPORT_SYMBOL(amdtp_syt_intervals);
  106. const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
  107. [CIP_SFC_32000] = 32000,
  108. [CIP_SFC_44100] = 44100,
  109. [CIP_SFC_48000] = 48000,
  110. [CIP_SFC_88200] = 88200,
  111. [CIP_SFC_96000] = 96000,
  112. [CIP_SFC_176400] = 176400,
  113. [CIP_SFC_192000] = 192000,
  114. };
  115. EXPORT_SYMBOL(amdtp_rate_table);
  116. /**
  117. * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
  118. * @s: the AMDTP stream, which must be initialized.
  119. * @runtime: the PCM substream runtime
  120. */
  121. int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
  122. struct snd_pcm_runtime *runtime)
  123. {
  124. int err;
  125. /* AM824 in IEC 61883-6 can deliver 24bit data */
  126. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  127. if (err < 0)
  128. goto end;
  129. /*
  130. * Currently firewire-lib processes 16 packets in one software
  131. * interrupt callback. This equals to 2msec but actually the
  132. * interval of the interrupts has a jitter.
  133. * Additionally, even if adding a constraint to fit period size to
  134. * 2msec, actual calculated frames per period doesn't equal to 2msec,
  135. * depending on sampling rate.
  136. * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
  137. * Here let us use 5msec for safe period interrupt.
  138. */
  139. err = snd_pcm_hw_constraint_minmax(runtime,
  140. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  141. 5000, UINT_MAX);
  142. if (err < 0)
  143. goto end;
  144. /* Non-Blocking stream has no more constraints */
  145. if (!(s->flags & CIP_BLOCKING))
  146. goto end;
  147. /*
  148. * One AMDTP packet can include some frames. In blocking mode, the
  149. * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
  150. * depending on its sampling rate. For accurate period interrupt, it's
  151. * preferrable to align period/buffer sizes to current SYT_INTERVAL.
  152. *
  153. * TODO: These constraints can be improved with proper rules.
  154. * Currently apply LCM of SYT_INTERVALs.
  155. */
  156. err = snd_pcm_hw_constraint_step(runtime, 0,
  157. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
  158. if (err < 0)
  159. goto end;
  160. err = snd_pcm_hw_constraint_step(runtime, 0,
  161. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
  162. end:
  163. return err;
  164. }
  165. EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
  166. /**
  167. * amdtp_stream_set_parameters - set stream parameters
  168. * @s: the AMDTP stream to configure
  169. * @rate: the sample rate
  170. * @pcm_channels: the number of PCM samples in each data block, to be encoded
  171. * as AM824 multi-bit linear audio
  172. * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
  173. *
  174. * The parameters must be set before the stream is started, and must not be
  175. * changed while the stream is running.
  176. */
  177. void amdtp_stream_set_parameters(struct amdtp_stream *s,
  178. unsigned int rate,
  179. unsigned int pcm_channels,
  180. unsigned int midi_ports)
  181. {
  182. unsigned int i, sfc, midi_channels;
  183. midi_channels = DIV_ROUND_UP(midi_ports, 8);
  184. if (WARN_ON(amdtp_stream_running(s)) |
  185. WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
  186. WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
  187. return;
  188. for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc)
  189. if (amdtp_rate_table[sfc] == rate)
  190. goto sfc_found;
  191. WARN_ON(1);
  192. return;
  193. sfc_found:
  194. s->pcm_channels = pcm_channels;
  195. s->sfc = sfc;
  196. s->data_block_quadlets = s->pcm_channels + midi_channels;
  197. s->midi_ports = midi_ports;
  198. s->syt_interval = amdtp_syt_intervals[sfc];
  199. /* default buffering in the device */
  200. s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
  201. if (s->flags & CIP_BLOCKING)
  202. /* additional buffering needed to adjust for no-data packets */
  203. s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
  204. /* init the position map for PCM and MIDI channels */
  205. for (i = 0; i < pcm_channels; i++)
  206. s->pcm_positions[i] = i;
  207. s->midi_position = s->pcm_channels;
  208. /*
  209. * We do not know the actual MIDI FIFO size of most devices. Just
  210. * assume two bytes, i.e., one byte can be received over the bus while
  211. * the previous one is transmitted over MIDI.
  212. * (The value here is adjusted for midi_ratelimit_per_packet().)
  213. */
  214. s->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
  215. }
  216. EXPORT_SYMBOL(amdtp_stream_set_parameters);
  217. /**
  218. * amdtp_stream_get_max_payload - get the stream's packet size
  219. * @s: the AMDTP stream
  220. *
  221. * This function must not be called before the stream has been configured
  222. * with amdtp_stream_set_parameters().
  223. */
  224. unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
  225. {
  226. unsigned int multiplier = 1;
  227. if (s->flags & CIP_JUMBO_PAYLOAD)
  228. multiplier = 5;
  229. return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
  230. }
  231. EXPORT_SYMBOL(amdtp_stream_get_max_payload);
  232. static void write_pcm_s16(struct amdtp_stream *s,
  233. struct snd_pcm_substream *pcm,
  234. __be32 *buffer, unsigned int frames);
  235. static void write_pcm_s32(struct amdtp_stream *s,
  236. struct snd_pcm_substream *pcm,
  237. __be32 *buffer, unsigned int frames);
  238. static void read_pcm_s32(struct amdtp_stream *s,
  239. struct snd_pcm_substream *pcm,
  240. __be32 *buffer, unsigned int frames);
  241. /**
  242. * amdtp_stream_set_pcm_format - set the PCM format
  243. * @s: the AMDTP stream to configure
  244. * @format: the format of the ALSA PCM device
  245. *
  246. * The sample format must be set after the other parameters (rate/PCM channels/
  247. * MIDI) and before the stream is started, and must not be changed while the
  248. * stream is running.
  249. */
  250. void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
  251. snd_pcm_format_t format)
  252. {
  253. if (WARN_ON(amdtp_stream_pcm_running(s)))
  254. return;
  255. switch (format) {
  256. default:
  257. WARN_ON(1);
  258. /* fall through */
  259. case SNDRV_PCM_FORMAT_S16:
  260. if (s->direction == AMDTP_OUT_STREAM) {
  261. s->transfer_samples = write_pcm_s16;
  262. break;
  263. }
  264. WARN_ON(1);
  265. /* fall through */
  266. case SNDRV_PCM_FORMAT_S32:
  267. if (s->direction == AMDTP_OUT_STREAM)
  268. s->transfer_samples = write_pcm_s32;
  269. else
  270. s->transfer_samples = read_pcm_s32;
  271. break;
  272. }
  273. }
  274. EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
  275. /**
  276. * amdtp_stream_pcm_prepare - prepare PCM device for running
  277. * @s: the AMDTP stream
  278. *
  279. * This function should be called from the PCM device's .prepare callback.
  280. */
  281. void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
  282. {
  283. tasklet_kill(&s->period_tasklet);
  284. s->pcm_buffer_pointer = 0;
  285. s->pcm_period_pointer = 0;
  286. s->pointer_flush = true;
  287. }
  288. EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
  289. static unsigned int calculate_data_blocks(struct amdtp_stream *s,
  290. unsigned int syt)
  291. {
  292. unsigned int phase, data_blocks;
  293. /* Blocking mode. */
  294. if (s->flags & CIP_BLOCKING) {
  295. /* This module generate empty packet for 'no data'. */
  296. if (syt == CIP_SYT_NO_INFO)
  297. data_blocks = 0;
  298. else
  299. data_blocks = s->syt_interval;
  300. /* Non-blocking mode. */
  301. } else {
  302. if (!cip_sfc_is_base_44100(s->sfc)) {
  303. /* Sample_rate / 8000 is an integer, and precomputed. */
  304. data_blocks = s->data_block_state;
  305. } else {
  306. phase = s->data_block_state;
  307. /*
  308. * This calculates the number of data blocks per packet so that
  309. * 1) the overall rate is correct and exactly synchronized to
  310. * the bus clock, and
  311. * 2) packets with a rounded-up number of blocks occur as early
  312. * as possible in the sequence (to prevent underruns of the
  313. * device's buffer).
  314. */
  315. if (s->sfc == CIP_SFC_44100)
  316. /* 6 6 5 6 5 6 5 ... */
  317. data_blocks = 5 + ((phase & 1) ^
  318. (phase == 0 || phase >= 40));
  319. else
  320. /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
  321. data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
  322. if (++phase >= (80 >> (s->sfc >> 1)))
  323. phase = 0;
  324. s->data_block_state = phase;
  325. }
  326. }
  327. return data_blocks;
  328. }
  329. static unsigned int calculate_syt(struct amdtp_stream *s,
  330. unsigned int cycle)
  331. {
  332. unsigned int syt_offset, phase, index, syt;
  333. if (s->last_syt_offset < TICKS_PER_CYCLE) {
  334. if (!cip_sfc_is_base_44100(s->sfc))
  335. syt_offset = s->last_syt_offset + s->syt_offset_state;
  336. else {
  337. /*
  338. * The time, in ticks, of the n'th SYT_INTERVAL sample is:
  339. * n * SYT_INTERVAL * 24576000 / sample_rate
  340. * Modulo TICKS_PER_CYCLE, the difference between successive
  341. * elements is about 1386.23. Rounding the results of this
  342. * formula to the SYT precision results in a sequence of
  343. * differences that begins with:
  344. * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
  345. * This code generates _exactly_ the same sequence.
  346. */
  347. phase = s->syt_offset_state;
  348. index = phase % 13;
  349. syt_offset = s->last_syt_offset;
  350. syt_offset += 1386 + ((index && !(index & 3)) ||
  351. phase == 146);
  352. if (++phase >= 147)
  353. phase = 0;
  354. s->syt_offset_state = phase;
  355. }
  356. } else
  357. syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
  358. s->last_syt_offset = syt_offset;
  359. if (syt_offset < TICKS_PER_CYCLE) {
  360. syt_offset += s->transfer_delay;
  361. syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
  362. syt += syt_offset % TICKS_PER_CYCLE;
  363. return syt & CIP_SYT_MASK;
  364. } else {
  365. return CIP_SYT_NO_INFO;
  366. }
  367. }
  368. static void write_pcm_s32(struct amdtp_stream *s,
  369. struct snd_pcm_substream *pcm,
  370. __be32 *buffer, unsigned int frames)
  371. {
  372. struct snd_pcm_runtime *runtime = pcm->runtime;
  373. unsigned int channels, remaining_frames, i, c;
  374. const u32 *src;
  375. channels = s->pcm_channels;
  376. src = (void *)runtime->dma_area +
  377. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  378. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  379. for (i = 0; i < frames; ++i) {
  380. for (c = 0; c < channels; ++c) {
  381. buffer[s->pcm_positions[c]] =
  382. cpu_to_be32((*src >> 8) | 0x40000000);
  383. src++;
  384. }
  385. buffer += s->data_block_quadlets;
  386. if (--remaining_frames == 0)
  387. src = (void *)runtime->dma_area;
  388. }
  389. }
  390. static void write_pcm_s16(struct amdtp_stream *s,
  391. struct snd_pcm_substream *pcm,
  392. __be32 *buffer, unsigned int frames)
  393. {
  394. struct snd_pcm_runtime *runtime = pcm->runtime;
  395. unsigned int channels, remaining_frames, i, c;
  396. const u16 *src;
  397. channels = s->pcm_channels;
  398. src = (void *)runtime->dma_area +
  399. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  400. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  401. for (i = 0; i < frames; ++i) {
  402. for (c = 0; c < channels; ++c) {
  403. buffer[s->pcm_positions[c]] =
  404. cpu_to_be32((*src << 8) | 0x42000000);
  405. src++;
  406. }
  407. buffer += s->data_block_quadlets;
  408. if (--remaining_frames == 0)
  409. src = (void *)runtime->dma_area;
  410. }
  411. }
  412. static void read_pcm_s32(struct amdtp_stream *s,
  413. struct snd_pcm_substream *pcm,
  414. __be32 *buffer, unsigned int frames)
  415. {
  416. struct snd_pcm_runtime *runtime = pcm->runtime;
  417. unsigned int channels, remaining_frames, i, c;
  418. u32 *dst;
  419. channels = s->pcm_channels;
  420. dst = (void *)runtime->dma_area +
  421. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  422. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  423. for (i = 0; i < frames; ++i) {
  424. for (c = 0; c < channels; ++c) {
  425. *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
  426. dst++;
  427. }
  428. buffer += s->data_block_quadlets;
  429. if (--remaining_frames == 0)
  430. dst = (void *)runtime->dma_area;
  431. }
  432. }
  433. static void write_pcm_silence(struct amdtp_stream *s,
  434. __be32 *buffer, unsigned int frames)
  435. {
  436. unsigned int i, c;
  437. for (i = 0; i < frames; ++i) {
  438. for (c = 0; c < s->pcm_channels; ++c)
  439. buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
  440. buffer += s->data_block_quadlets;
  441. }
  442. }
  443. /*
  444. * To avoid sending MIDI bytes at too high a rate, assume that the receiving
  445. * device has a FIFO, and track how much it is filled. This values increases
  446. * by one whenever we send one byte in a packet, but the FIFO empties at
  447. * a constant rate independent of our packet rate. One packet has syt_interval
  448. * samples, so the number of bytes that empty out of the FIFO, per packet(!),
  449. * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
  450. * fractional values, the values in midi_fifo_used[] are measured in bytes
  451. * multiplied by the sample rate.
  452. */
  453. static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
  454. {
  455. int used;
  456. used = s->midi_fifo_used[port];
  457. if (used == 0) /* common shortcut */
  458. return true;
  459. used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
  460. used = max(used, 0);
  461. s->midi_fifo_used[port] = used;
  462. return used < s->midi_fifo_limit;
  463. }
  464. static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
  465. {
  466. s->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
  467. }
  468. static void write_midi_messages(struct amdtp_stream *s,
  469. __be32 *buffer, unsigned int frames)
  470. {
  471. unsigned int f, port;
  472. u8 *b;
  473. for (f = 0; f < frames; f++) {
  474. b = (u8 *)&buffer[s->midi_position];
  475. port = (s->data_block_counter + f) % 8;
  476. if (f < MAX_MIDI_RX_BLOCKS &&
  477. midi_ratelimit_per_packet(s, port) &&
  478. s->midi[port] != NULL &&
  479. snd_rawmidi_transmit(s->midi[port], &b[1], 1) == 1) {
  480. midi_rate_use_one_byte(s, port);
  481. b[0] = 0x81;
  482. } else {
  483. b[0] = 0x80;
  484. b[1] = 0;
  485. }
  486. b[2] = 0;
  487. b[3] = 0;
  488. buffer += s->data_block_quadlets;
  489. }
  490. }
  491. static void read_midi_messages(struct amdtp_stream *s,
  492. __be32 *buffer, unsigned int frames)
  493. {
  494. unsigned int f, port;
  495. int len;
  496. u8 *b;
  497. for (f = 0; f < frames; f++) {
  498. port = (s->data_block_counter + f) % 8;
  499. b = (u8 *)&buffer[s->midi_position];
  500. len = b[0] - 0x80;
  501. if ((1 <= len) && (len <= 3) && (s->midi[port]))
  502. snd_rawmidi_receive(s->midi[port], b + 1, len);
  503. buffer += s->data_block_quadlets;
  504. }
  505. }
  506. static void update_pcm_pointers(struct amdtp_stream *s,
  507. struct snd_pcm_substream *pcm,
  508. unsigned int frames)
  509. {
  510. unsigned int ptr;
  511. /*
  512. * In IEC 61883-6, one data block represents one event. In ALSA, one
  513. * event equals to one PCM frame. But Dice has a quirk to transfer
  514. * two PCM frames in one data block.
  515. */
  516. if (s->double_pcm_frames)
  517. frames *= 2;
  518. ptr = s->pcm_buffer_pointer + frames;
  519. if (ptr >= pcm->runtime->buffer_size)
  520. ptr -= pcm->runtime->buffer_size;
  521. ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
  522. s->pcm_period_pointer += frames;
  523. if (s->pcm_period_pointer >= pcm->runtime->period_size) {
  524. s->pcm_period_pointer -= pcm->runtime->period_size;
  525. s->pointer_flush = false;
  526. tasklet_hi_schedule(&s->period_tasklet);
  527. }
  528. }
  529. static void pcm_period_tasklet(unsigned long data)
  530. {
  531. struct amdtp_stream *s = (void *)data;
  532. struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
  533. if (pcm)
  534. snd_pcm_period_elapsed(pcm);
  535. }
  536. static int queue_packet(struct amdtp_stream *s,
  537. unsigned int header_length,
  538. unsigned int payload_length, bool skip)
  539. {
  540. struct fw_iso_packet p = {0};
  541. int err = 0;
  542. if (IS_ERR(s->context))
  543. goto end;
  544. p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
  545. p.tag = TAG_CIP;
  546. p.header_length = header_length;
  547. p.payload_length = (!skip) ? payload_length : 0;
  548. p.skip = skip;
  549. err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
  550. s->buffer.packets[s->packet_index].offset);
  551. if (err < 0) {
  552. dev_err(&s->unit->device, "queueing error: %d\n", err);
  553. goto end;
  554. }
  555. if (++s->packet_index >= QUEUE_LENGTH)
  556. s->packet_index = 0;
  557. end:
  558. return err;
  559. }
  560. static inline int queue_out_packet(struct amdtp_stream *s,
  561. unsigned int payload_length, bool skip)
  562. {
  563. return queue_packet(s, OUT_PACKET_HEADER_SIZE,
  564. payload_length, skip);
  565. }
  566. static inline int queue_in_packet(struct amdtp_stream *s)
  567. {
  568. return queue_packet(s, IN_PACKET_HEADER_SIZE,
  569. amdtp_stream_get_max_payload(s), false);
  570. }
  571. static int handle_out_packet(struct amdtp_stream *s, unsigned int data_blocks,
  572. unsigned int syt)
  573. {
  574. __be32 *buffer;
  575. unsigned int payload_length;
  576. struct snd_pcm_substream *pcm;
  577. buffer = s->buffer.packets[s->packet_index].buffer;
  578. buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
  579. (s->data_block_quadlets << CIP_DBS_SHIFT) |
  580. s->data_block_counter);
  581. buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
  582. (s->sfc << CIP_FDF_SHIFT) | syt);
  583. buffer += 2;
  584. pcm = ACCESS_ONCE(s->pcm);
  585. if (pcm)
  586. s->transfer_samples(s, pcm, buffer, data_blocks);
  587. else
  588. write_pcm_silence(s, buffer, data_blocks);
  589. if (s->midi_ports)
  590. write_midi_messages(s, buffer, data_blocks);
  591. s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
  592. payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
  593. if (queue_out_packet(s, payload_length, false) < 0)
  594. return -EIO;
  595. if (pcm)
  596. update_pcm_pointers(s, pcm, data_blocks);
  597. /* No need to return the number of handled data blocks. */
  598. return 0;
  599. }
  600. static int handle_in_packet(struct amdtp_stream *s,
  601. unsigned int payload_quadlets, __be32 *buffer,
  602. unsigned int *data_blocks)
  603. {
  604. u32 cip_header[2];
  605. unsigned int data_block_quadlets, data_block_counter, dbc_interval;
  606. struct snd_pcm_substream *pcm = NULL;
  607. bool lost;
  608. cip_header[0] = be32_to_cpu(buffer[0]);
  609. cip_header[1] = be32_to_cpu(buffer[1]);
  610. /*
  611. * This module supports 'Two-quadlet CIP header with SYT field'.
  612. * For convenience, also check FMT field is AM824 or not.
  613. */
  614. if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
  615. ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
  616. ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
  617. dev_info_ratelimited(&s->unit->device,
  618. "Invalid CIP header for AMDTP: %08X:%08X\n",
  619. cip_header[0], cip_header[1]);
  620. *data_blocks = 0;
  621. goto end;
  622. }
  623. /* Calculate data blocks */
  624. if (payload_quadlets < 3 ||
  625. ((cip_header[1] & CIP_FDF_MASK) ==
  626. (AMDTP_FDF_NO_DATA << CIP_FDF_SHIFT))) {
  627. *data_blocks = 0;
  628. } else {
  629. data_block_quadlets =
  630. (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
  631. /* avoid division by zero */
  632. if (data_block_quadlets == 0) {
  633. dev_err(&s->unit->device,
  634. "Detect invalid value in dbs field: %08X\n",
  635. cip_header[0]);
  636. return -EPROTO;
  637. }
  638. if (s->flags & CIP_WRONG_DBS)
  639. data_block_quadlets = s->data_block_quadlets;
  640. *data_blocks = (payload_quadlets - 2) / data_block_quadlets;
  641. }
  642. /* Check data block counter continuity */
  643. data_block_counter = cip_header[0] & CIP_DBC_MASK;
  644. if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
  645. s->data_block_counter != UINT_MAX)
  646. data_block_counter = s->data_block_counter;
  647. if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) ||
  648. (s->data_block_counter == UINT_MAX)) {
  649. lost = false;
  650. } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
  651. lost = data_block_counter != s->data_block_counter;
  652. } else {
  653. if ((*data_blocks > 0) && (s->tx_dbc_interval > 0))
  654. dbc_interval = s->tx_dbc_interval;
  655. else
  656. dbc_interval = *data_blocks;
  657. lost = data_block_counter !=
  658. ((s->data_block_counter + dbc_interval) & 0xff);
  659. }
  660. if (lost) {
  661. dev_err(&s->unit->device,
  662. "Detect discontinuity of CIP: %02X %02X\n",
  663. s->data_block_counter, data_block_counter);
  664. return -EIO;
  665. }
  666. if (*data_blocks > 0) {
  667. buffer += 2;
  668. pcm = ACCESS_ONCE(s->pcm);
  669. if (pcm)
  670. s->transfer_samples(s, pcm, buffer, *data_blocks);
  671. if (s->midi_ports)
  672. read_midi_messages(s, buffer, *data_blocks);
  673. }
  674. if (s->flags & CIP_DBC_IS_END_EVENT)
  675. s->data_block_counter = data_block_counter;
  676. else
  677. s->data_block_counter =
  678. (data_block_counter + *data_blocks) & 0xff;
  679. end:
  680. if (queue_in_packet(s) < 0)
  681. return -EIO;
  682. if (pcm)
  683. update_pcm_pointers(s, pcm, *data_blocks);
  684. return 0;
  685. }
  686. static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
  687. size_t header_length, void *header,
  688. void *private_data)
  689. {
  690. struct amdtp_stream *s = private_data;
  691. unsigned int i, syt, packets = header_length / 4;
  692. unsigned int data_blocks;
  693. if (s->packet_index < 0)
  694. return;
  695. /*
  696. * Compute the cycle of the last queued packet.
  697. * (We need only the four lowest bits for the SYT, so we can ignore
  698. * that bits 0-11 must wrap around at 3072.)
  699. */
  700. cycle += QUEUE_LENGTH - packets;
  701. for (i = 0; i < packets; ++i) {
  702. syt = calculate_syt(s, ++cycle);
  703. data_blocks = calculate_data_blocks(s, syt);
  704. if (handle_out_packet(s, data_blocks, syt) < 0) {
  705. s->packet_index = -1;
  706. amdtp_stream_pcm_abort(s);
  707. return;
  708. }
  709. }
  710. fw_iso_context_queue_flush(s->context);
  711. }
  712. static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
  713. size_t header_length, void *header,
  714. void *private_data)
  715. {
  716. struct amdtp_stream *s = private_data;
  717. unsigned int p, syt, packets;
  718. unsigned int payload_quadlets, max_payload_quadlets;
  719. unsigned int data_blocks;
  720. __be32 *buffer, *headers = header;
  721. if (s->packet_index < 0)
  722. return;
  723. /* The number of packets in buffer */
  724. packets = header_length / IN_PACKET_HEADER_SIZE;
  725. /* For buffer-over-run prevention. */
  726. max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4;
  727. for (p = 0; p < packets; p++) {
  728. buffer = s->buffer.packets[s->packet_index].buffer;
  729. /* The number of quadlets in this packet */
  730. payload_quadlets =
  731. (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
  732. if (payload_quadlets > max_payload_quadlets) {
  733. dev_err(&s->unit->device,
  734. "Detect jumbo payload: %02x %02x\n",
  735. payload_quadlets, max_payload_quadlets);
  736. s->packet_index = -1;
  737. break;
  738. }
  739. if (handle_in_packet(s, payload_quadlets, buffer,
  740. &data_blocks) < 0) {
  741. s->packet_index = -1;
  742. break;
  743. }
  744. /* Process sync slave stream */
  745. if (s->sync_slave && s->sync_slave->callbacked) {
  746. syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
  747. if (handle_out_packet(s->sync_slave,
  748. data_blocks, syt) < 0) {
  749. s->packet_index = -1;
  750. break;
  751. }
  752. }
  753. }
  754. /* Queueing error or detecting discontinuity */
  755. if (s->packet_index < 0) {
  756. amdtp_stream_pcm_abort(s);
  757. /* Abort sync slave. */
  758. if (s->sync_slave) {
  759. s->sync_slave->packet_index = -1;
  760. amdtp_stream_pcm_abort(s->sync_slave);
  761. }
  762. return;
  763. }
  764. /* when sync to device, flush the packets for slave stream */
  765. if (s->sync_slave && s->sync_slave->callbacked)
  766. fw_iso_context_queue_flush(s->sync_slave->context);
  767. fw_iso_context_queue_flush(s->context);
  768. }
  769. /* processing is done by master callback */
  770. static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
  771. size_t header_length, void *header,
  772. void *private_data)
  773. {
  774. return;
  775. }
  776. /* this is executed one time */
  777. static void amdtp_stream_first_callback(struct fw_iso_context *context,
  778. u32 cycle, size_t header_length,
  779. void *header, void *private_data)
  780. {
  781. struct amdtp_stream *s = private_data;
  782. /*
  783. * For in-stream, first packet has come.
  784. * For out-stream, prepared to transmit first packet
  785. */
  786. s->callbacked = true;
  787. wake_up(&s->callback_wait);
  788. if (s->direction == AMDTP_IN_STREAM)
  789. context->callback.sc = in_stream_callback;
  790. else if (s->flags & CIP_SYNC_TO_DEVICE)
  791. context->callback.sc = slave_stream_callback;
  792. else
  793. context->callback.sc = out_stream_callback;
  794. context->callback.sc(context, cycle, header_length, header, s);
  795. }
  796. /**
  797. * amdtp_stream_start - start transferring packets
  798. * @s: the AMDTP stream to start
  799. * @channel: the isochronous channel on the bus
  800. * @speed: firewire speed code
  801. *
  802. * The stream cannot be started until it has been configured with
  803. * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
  804. * device can be started.
  805. */
  806. int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
  807. {
  808. static const struct {
  809. unsigned int data_block;
  810. unsigned int syt_offset;
  811. } initial_state[] = {
  812. [CIP_SFC_32000] = { 4, 3072 },
  813. [CIP_SFC_48000] = { 6, 1024 },
  814. [CIP_SFC_96000] = { 12, 1024 },
  815. [CIP_SFC_192000] = { 24, 1024 },
  816. [CIP_SFC_44100] = { 0, 67 },
  817. [CIP_SFC_88200] = { 0, 67 },
  818. [CIP_SFC_176400] = { 0, 67 },
  819. };
  820. unsigned int header_size;
  821. enum dma_data_direction dir;
  822. int type, tag, err;
  823. mutex_lock(&s->mutex);
  824. if (WARN_ON(amdtp_stream_running(s) ||
  825. (s->data_block_quadlets < 1))) {
  826. err = -EBADFD;
  827. goto err_unlock;
  828. }
  829. if (s->direction == AMDTP_IN_STREAM &&
  830. s->flags & CIP_SKIP_INIT_DBC_CHECK)
  831. s->data_block_counter = UINT_MAX;
  832. else
  833. s->data_block_counter = 0;
  834. s->data_block_state = initial_state[s->sfc].data_block;
  835. s->syt_offset_state = initial_state[s->sfc].syt_offset;
  836. s->last_syt_offset = TICKS_PER_CYCLE;
  837. /* initialize packet buffer */
  838. if (s->direction == AMDTP_IN_STREAM) {
  839. dir = DMA_FROM_DEVICE;
  840. type = FW_ISO_CONTEXT_RECEIVE;
  841. header_size = IN_PACKET_HEADER_SIZE;
  842. } else {
  843. dir = DMA_TO_DEVICE;
  844. type = FW_ISO_CONTEXT_TRANSMIT;
  845. header_size = OUT_PACKET_HEADER_SIZE;
  846. }
  847. err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
  848. amdtp_stream_get_max_payload(s), dir);
  849. if (err < 0)
  850. goto err_unlock;
  851. s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
  852. type, channel, speed, header_size,
  853. amdtp_stream_first_callback, s);
  854. if (IS_ERR(s->context)) {
  855. err = PTR_ERR(s->context);
  856. if (err == -EBUSY)
  857. dev_err(&s->unit->device,
  858. "no free stream on this controller\n");
  859. goto err_buffer;
  860. }
  861. amdtp_stream_update(s);
  862. s->packet_index = 0;
  863. do {
  864. if (s->direction == AMDTP_IN_STREAM)
  865. err = queue_in_packet(s);
  866. else
  867. err = queue_out_packet(s, 0, true);
  868. if (err < 0)
  869. goto err_context;
  870. } while (s->packet_index > 0);
  871. /* NOTE: TAG1 matches CIP. This just affects in stream. */
  872. tag = FW_ISO_CONTEXT_MATCH_TAG1;
  873. if (s->flags & CIP_EMPTY_WITH_TAG0)
  874. tag |= FW_ISO_CONTEXT_MATCH_TAG0;
  875. s->callbacked = false;
  876. err = fw_iso_context_start(s->context, -1, 0, tag);
  877. if (err < 0)
  878. goto err_context;
  879. mutex_unlock(&s->mutex);
  880. return 0;
  881. err_context:
  882. fw_iso_context_destroy(s->context);
  883. s->context = ERR_PTR(-1);
  884. err_buffer:
  885. iso_packets_buffer_destroy(&s->buffer, s->unit);
  886. err_unlock:
  887. mutex_unlock(&s->mutex);
  888. return err;
  889. }
  890. EXPORT_SYMBOL(amdtp_stream_start);
  891. /**
  892. * amdtp_stream_pcm_pointer - get the PCM buffer position
  893. * @s: the AMDTP stream that transports the PCM data
  894. *
  895. * Returns the current buffer position, in frames.
  896. */
  897. unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
  898. {
  899. /* this optimization is allowed to be racy */
  900. if (s->pointer_flush && amdtp_stream_running(s))
  901. fw_iso_context_flush_completions(s->context);
  902. else
  903. s->pointer_flush = true;
  904. return ACCESS_ONCE(s->pcm_buffer_pointer);
  905. }
  906. EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
  907. /**
  908. * amdtp_stream_update - update the stream after a bus reset
  909. * @s: the AMDTP stream
  910. */
  911. void amdtp_stream_update(struct amdtp_stream *s)
  912. {
  913. /* Precomputing. */
  914. ACCESS_ONCE(s->source_node_id_field) =
  915. (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
  916. CIP_SID_MASK;
  917. }
  918. EXPORT_SYMBOL(amdtp_stream_update);
  919. /**
  920. * amdtp_stream_stop - stop sending packets
  921. * @s: the AMDTP stream to stop
  922. *
  923. * All PCM and MIDI devices of the stream must be stopped before the stream
  924. * itself can be stopped.
  925. */
  926. void amdtp_stream_stop(struct amdtp_stream *s)
  927. {
  928. mutex_lock(&s->mutex);
  929. if (!amdtp_stream_running(s)) {
  930. mutex_unlock(&s->mutex);
  931. return;
  932. }
  933. tasklet_kill(&s->period_tasklet);
  934. fw_iso_context_stop(s->context);
  935. fw_iso_context_destroy(s->context);
  936. s->context = ERR_PTR(-1);
  937. iso_packets_buffer_destroy(&s->buffer, s->unit);
  938. s->callbacked = false;
  939. mutex_unlock(&s->mutex);
  940. }
  941. EXPORT_SYMBOL(amdtp_stream_stop);
  942. /**
  943. * amdtp_stream_pcm_abort - abort the running PCM device
  944. * @s: the AMDTP stream about to be stopped
  945. *
  946. * If the isochronous stream needs to be stopped asynchronously, call this
  947. * function first to stop the PCM device.
  948. */
  949. void amdtp_stream_pcm_abort(struct amdtp_stream *s)
  950. {
  951. struct snd_pcm_substream *pcm;
  952. pcm = ACCESS_ONCE(s->pcm);
  953. if (pcm)
  954. snd_pcm_stop_xrun(pcm);
  955. }
  956. EXPORT_SYMBOL(amdtp_stream_pcm_abort);