amdtp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. { unsigned int ptr;
  449. ptr = s->pcm_buffer_pointer + frames;
  450. if (ptr >= pcm->runtime->buffer_size)
  451. ptr -= pcm->runtime->buffer_size;
  452. ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
  453. s->pcm_period_pointer += frames;
  454. if (s->pcm_period_pointer >= pcm->runtime->period_size) {
  455. s->pcm_period_pointer -= pcm->runtime->period_size;
  456. s->pointer_flush = false;
  457. tasklet_hi_schedule(&s->period_tasklet);
  458. }
  459. }
  460. static void pcm_period_tasklet(unsigned long data)
  461. {
  462. struct amdtp_stream *s = (void *)data;
  463. struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
  464. if (pcm)
  465. snd_pcm_period_elapsed(pcm);
  466. }
  467. static int queue_packet(struct amdtp_stream *s,
  468. unsigned int header_length,
  469. unsigned int payload_length, bool skip)
  470. {
  471. struct fw_iso_packet p = {0};
  472. int err = 0;
  473. if (IS_ERR(s->context))
  474. goto end;
  475. p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
  476. p.tag = TAG_CIP;
  477. p.header_length = header_length;
  478. p.payload_length = (!skip) ? payload_length : 0;
  479. p.skip = skip;
  480. err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
  481. s->buffer.packets[s->packet_index].offset);
  482. if (err < 0) {
  483. dev_err(&s->unit->device, "queueing error: %d\n", err);
  484. goto end;
  485. }
  486. if (++s->packet_index >= QUEUE_LENGTH)
  487. s->packet_index = 0;
  488. end:
  489. return err;
  490. }
  491. static inline int queue_out_packet(struct amdtp_stream *s,
  492. unsigned int payload_length, bool skip)
  493. {
  494. return queue_packet(s, OUT_PACKET_HEADER_SIZE,
  495. payload_length, skip);
  496. }
  497. static inline int queue_in_packet(struct amdtp_stream *s)
  498. {
  499. return queue_packet(s, IN_PACKET_HEADER_SIZE,
  500. amdtp_stream_get_max_payload(s), false);
  501. }
  502. static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
  503. {
  504. __be32 *buffer;
  505. unsigned int data_blocks, payload_length;
  506. struct snd_pcm_substream *pcm;
  507. if (s->packet_index < 0)
  508. return;
  509. /* this module generate empty packet for 'no data' */
  510. if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
  511. data_blocks = calculate_data_blocks(s);
  512. else
  513. data_blocks = 0;
  514. buffer = s->buffer.packets[s->packet_index].buffer;
  515. buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
  516. (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
  517. s->data_block_counter);
  518. buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
  519. (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
  520. buffer += 2;
  521. pcm = ACCESS_ONCE(s->pcm);
  522. if (pcm)
  523. s->transfer_samples(s, pcm, buffer, data_blocks);
  524. else
  525. amdtp_fill_pcm_silence(s, buffer, data_blocks);
  526. if (s->midi_ports)
  527. amdtp_fill_midi(s, buffer, data_blocks);
  528. s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
  529. payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
  530. if (queue_out_packet(s, payload_length, false) < 0) {
  531. s->packet_index = -1;
  532. amdtp_stream_pcm_abort(s);
  533. return;
  534. }
  535. if (pcm)
  536. update_pcm_pointers(s, pcm, data_blocks);
  537. }
  538. static void handle_in_packet(struct amdtp_stream *s,
  539. unsigned int payload_quadlets,
  540. __be32 *buffer)
  541. {
  542. u32 cip_header[2];
  543. unsigned int data_blocks, data_block_quadlets, data_block_counter,
  544. dbc_interval;
  545. struct snd_pcm_substream *pcm = NULL;
  546. bool lost;
  547. cip_header[0] = be32_to_cpu(buffer[0]);
  548. cip_header[1] = be32_to_cpu(buffer[1]);
  549. /*
  550. * This module supports 'Two-quadlet CIP header with SYT field'.
  551. * For convenience, also check FMT field is AM824 or not.
  552. */
  553. if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
  554. ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
  555. ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
  556. dev_info_ratelimited(&s->unit->device,
  557. "Invalid CIP header for AMDTP: %08X:%08X\n",
  558. cip_header[0], cip_header[1]);
  559. goto end;
  560. }
  561. /* Calculate data blocks */
  562. if (payload_quadlets < 3 ||
  563. ((cip_header[1] & CIP_FDF_MASK) ==
  564. (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
  565. data_blocks = 0;
  566. } else {
  567. data_block_quadlets =
  568. (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
  569. /* avoid division by zero */
  570. if (data_block_quadlets == 0) {
  571. dev_info_ratelimited(&s->unit->device,
  572. "Detect invalid value in dbs field: %08X\n",
  573. cip_header[0]);
  574. goto err;
  575. }
  576. if (s->flags & CIP_WRONG_DBS)
  577. data_block_quadlets = s->data_block_quadlets;
  578. data_blocks = (payload_quadlets - 2) / data_block_quadlets;
  579. }
  580. /* Check data block counter continuity */
  581. data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
  582. if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
  583. s->data_block_counter != UINT_MAX)
  584. data_block_counter = s->data_block_counter;
  585. if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) ||
  586. (s->data_block_counter == UINT_MAX)) {
  587. lost = false;
  588. } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
  589. lost = data_block_counter != s->data_block_counter;
  590. } else {
  591. if ((data_blocks > 0) && (s->tx_dbc_interval > 0))
  592. dbc_interval = s->tx_dbc_interval;
  593. else
  594. dbc_interval = data_blocks;
  595. lost = data_block_counter !=
  596. ((s->data_block_counter + dbc_interval) & 0xff);
  597. }
  598. if (lost) {
  599. dev_info(&s->unit->device,
  600. "Detect discontinuity of CIP: %02X %02X\n",
  601. s->data_block_counter, data_block_counter);
  602. goto err;
  603. }
  604. if (data_blocks > 0) {
  605. buffer += 2;
  606. pcm = ACCESS_ONCE(s->pcm);
  607. if (pcm)
  608. s->transfer_samples(s, pcm, buffer, data_blocks);
  609. if (s->midi_ports)
  610. amdtp_pull_midi(s, buffer, data_blocks);
  611. }
  612. if (s->flags & CIP_DBC_IS_END_EVENT)
  613. s->data_block_counter = data_block_counter;
  614. else
  615. s->data_block_counter =
  616. (data_block_counter + data_blocks) & 0xff;
  617. end:
  618. if (queue_in_packet(s) < 0)
  619. goto err;
  620. if (pcm)
  621. update_pcm_pointers(s, pcm, data_blocks);
  622. return;
  623. err:
  624. s->packet_index = -1;
  625. amdtp_stream_pcm_abort(s);
  626. }
  627. static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
  628. size_t header_length, void *header,
  629. void *private_data)
  630. {
  631. struct amdtp_stream *s = private_data;
  632. unsigned int i, syt, packets = header_length / 4;
  633. /*
  634. * Compute the cycle of the last queued packet.
  635. * (We need only the four lowest bits for the SYT, so we can ignore
  636. * that bits 0-11 must wrap around at 3072.)
  637. */
  638. cycle += QUEUE_LENGTH - packets;
  639. for (i = 0; i < packets; ++i) {
  640. syt = calculate_syt(s, ++cycle);
  641. handle_out_packet(s, syt);
  642. }
  643. fw_iso_context_queue_flush(s->context);
  644. }
  645. static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
  646. size_t header_length, void *header,
  647. void *private_data)
  648. {
  649. struct amdtp_stream *s = private_data;
  650. unsigned int p, syt, packets, payload_quadlets;
  651. __be32 *buffer, *headers = header;
  652. /* The number of packets in buffer */
  653. packets = header_length / IN_PACKET_HEADER_SIZE;
  654. for (p = 0; p < packets; p++) {
  655. if (s->packet_index < 0)
  656. break;
  657. buffer = s->buffer.packets[s->packet_index].buffer;
  658. /* Process sync slave stream */
  659. if (s->sync_slave && s->sync_slave->callbacked) {
  660. syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
  661. handle_out_packet(s->sync_slave, syt);
  662. }
  663. /* The number of quadlets in this packet */
  664. payload_quadlets =
  665. (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
  666. handle_in_packet(s, payload_quadlets, buffer);
  667. }
  668. /* Queueing error or detecting discontinuity */
  669. if (s->packet_index < 0) {
  670. /* Abort sync slave. */
  671. if (s->sync_slave) {
  672. s->sync_slave->packet_index = -1;
  673. amdtp_stream_pcm_abort(s->sync_slave);
  674. }
  675. return;
  676. }
  677. /* when sync to device, flush the packets for slave stream */
  678. if (s->sync_slave && s->sync_slave->callbacked)
  679. fw_iso_context_queue_flush(s->sync_slave->context);
  680. fw_iso_context_queue_flush(s->context);
  681. }
  682. /* processing is done by master callback */
  683. static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
  684. size_t header_length, void *header,
  685. void *private_data)
  686. {
  687. return;
  688. }
  689. /* this is executed one time */
  690. static void amdtp_stream_first_callback(struct fw_iso_context *context,
  691. u32 cycle, size_t header_length,
  692. void *header, void *private_data)
  693. {
  694. struct amdtp_stream *s = private_data;
  695. /*
  696. * For in-stream, first packet has come.
  697. * For out-stream, prepared to transmit first packet
  698. */
  699. s->callbacked = true;
  700. wake_up(&s->callback_wait);
  701. if (s->direction == AMDTP_IN_STREAM)
  702. context->callback.sc = in_stream_callback;
  703. else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
  704. context->callback.sc = slave_stream_callback;
  705. else
  706. context->callback.sc = out_stream_callback;
  707. context->callback.sc(context, cycle, header_length, header, s);
  708. }
  709. /**
  710. * amdtp_stream_start - start transferring packets
  711. * @s: the AMDTP stream to start
  712. * @channel: the isochronous channel on the bus
  713. * @speed: firewire speed code
  714. *
  715. * The stream cannot be started until it has been configured with
  716. * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
  717. * device can be started.
  718. */
  719. int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
  720. {
  721. static const struct {
  722. unsigned int data_block;
  723. unsigned int syt_offset;
  724. } initial_state[] = {
  725. [CIP_SFC_32000] = { 4, 3072 },
  726. [CIP_SFC_48000] = { 6, 1024 },
  727. [CIP_SFC_96000] = { 12, 1024 },
  728. [CIP_SFC_192000] = { 24, 1024 },
  729. [CIP_SFC_44100] = { 0, 67 },
  730. [CIP_SFC_88200] = { 0, 67 },
  731. [CIP_SFC_176400] = { 0, 67 },
  732. };
  733. unsigned int header_size;
  734. enum dma_data_direction dir;
  735. int type, tag, err;
  736. mutex_lock(&s->mutex);
  737. if (WARN_ON(amdtp_stream_running(s) ||
  738. (s->data_block_quadlets < 1))) {
  739. err = -EBADFD;
  740. goto err_unlock;
  741. }
  742. if (s->direction == AMDTP_IN_STREAM &&
  743. s->flags & CIP_SKIP_INIT_DBC_CHECK)
  744. s->data_block_counter = UINT_MAX;
  745. else
  746. s->data_block_counter = 0;
  747. s->data_block_state = initial_state[s->sfc].data_block;
  748. s->syt_offset_state = initial_state[s->sfc].syt_offset;
  749. s->last_syt_offset = TICKS_PER_CYCLE;
  750. /* initialize packet buffer */
  751. if (s->direction == AMDTP_IN_STREAM) {
  752. dir = DMA_FROM_DEVICE;
  753. type = FW_ISO_CONTEXT_RECEIVE;
  754. header_size = IN_PACKET_HEADER_SIZE;
  755. } else {
  756. dir = DMA_TO_DEVICE;
  757. type = FW_ISO_CONTEXT_TRANSMIT;
  758. header_size = OUT_PACKET_HEADER_SIZE;
  759. }
  760. err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
  761. amdtp_stream_get_max_payload(s), dir);
  762. if (err < 0)
  763. goto err_unlock;
  764. s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
  765. type, channel, speed, header_size,
  766. amdtp_stream_first_callback, s);
  767. if (IS_ERR(s->context)) {
  768. err = PTR_ERR(s->context);
  769. if (err == -EBUSY)
  770. dev_err(&s->unit->device,
  771. "no free stream on this controller\n");
  772. goto err_buffer;
  773. }
  774. amdtp_stream_update(s);
  775. s->packet_index = 0;
  776. do {
  777. if (s->direction == AMDTP_IN_STREAM)
  778. err = queue_in_packet(s);
  779. else
  780. err = queue_out_packet(s, 0, true);
  781. if (err < 0)
  782. goto err_context;
  783. } while (s->packet_index > 0);
  784. /* NOTE: TAG1 matches CIP. This just affects in stream. */
  785. tag = FW_ISO_CONTEXT_MATCH_TAG1;
  786. if (s->flags & CIP_EMPTY_WITH_TAG0)
  787. tag |= FW_ISO_CONTEXT_MATCH_TAG0;
  788. s->callbacked = false;
  789. err = fw_iso_context_start(s->context, -1, 0, tag);
  790. if (err < 0)
  791. goto err_context;
  792. mutex_unlock(&s->mutex);
  793. return 0;
  794. err_context:
  795. fw_iso_context_destroy(s->context);
  796. s->context = ERR_PTR(-1);
  797. err_buffer:
  798. iso_packets_buffer_destroy(&s->buffer, s->unit);
  799. err_unlock:
  800. mutex_unlock(&s->mutex);
  801. return err;
  802. }
  803. EXPORT_SYMBOL(amdtp_stream_start);
  804. /**
  805. * amdtp_stream_pcm_pointer - get the PCM buffer position
  806. * @s: the AMDTP stream that transports the PCM data
  807. *
  808. * Returns the current buffer position, in frames.
  809. */
  810. unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
  811. {
  812. /* this optimization is allowed to be racy */
  813. if (s->pointer_flush && amdtp_stream_running(s))
  814. fw_iso_context_flush_completions(s->context);
  815. else
  816. s->pointer_flush = true;
  817. return ACCESS_ONCE(s->pcm_buffer_pointer);
  818. }
  819. EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
  820. /**
  821. * amdtp_stream_update - update the stream after a bus reset
  822. * @s: the AMDTP stream
  823. */
  824. void amdtp_stream_update(struct amdtp_stream *s)
  825. {
  826. ACCESS_ONCE(s->source_node_id_field) =
  827. (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
  828. }
  829. EXPORT_SYMBOL(amdtp_stream_update);
  830. /**
  831. * amdtp_stream_stop - stop sending packets
  832. * @s: the AMDTP stream to stop
  833. *
  834. * All PCM and MIDI devices of the stream must be stopped before the stream
  835. * itself can be stopped.
  836. */
  837. void amdtp_stream_stop(struct amdtp_stream *s)
  838. {
  839. mutex_lock(&s->mutex);
  840. if (!amdtp_stream_running(s)) {
  841. mutex_unlock(&s->mutex);
  842. return;
  843. }
  844. tasklet_kill(&s->period_tasklet);
  845. fw_iso_context_stop(s->context);
  846. fw_iso_context_destroy(s->context);
  847. s->context = ERR_PTR(-1);
  848. iso_packets_buffer_destroy(&s->buffer, s->unit);
  849. s->callbacked = false;
  850. mutex_unlock(&s->mutex);
  851. }
  852. EXPORT_SYMBOL(amdtp_stream_stop);
  853. /**
  854. * amdtp_stream_pcm_abort - abort the running PCM device
  855. * @s: the AMDTP stream about to be stopped
  856. *
  857. * If the isochronous stream needs to be stopped asynchronously, call this
  858. * function first to stop the PCM device.
  859. */
  860. void amdtp_stream_pcm_abort(struct amdtp_stream *s)
  861. {
  862. struct snd_pcm_substream *pcm;
  863. pcm = ACCESS_ONCE(s->pcm);
  864. if (pcm) {
  865. snd_pcm_stream_lock_irq(pcm);
  866. if (snd_pcm_running(pcm))
  867. snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
  868. snd_pcm_stream_unlock_irq(pcm);
  869. }
  870. }
  871. EXPORT_SYMBOL(amdtp_stream_pcm_abort);