amdtp.c 29 KB

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