amdtp.c 27 KB

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