amdtp-stream.c 28 KB

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