amdtp.c 29 KB

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