amdtp-stream.c 23 KB

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