amdtp-stream.c 24 KB

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