amdtp-stream.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. ACCESS_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 = ACCESS_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(ACCESS_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 = ACCESS_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 = ACCESS_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 = ACCESS_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_quadlets, unsigned int cycle,
  545. unsigned int index)
  546. {
  547. __be32 *buffer;
  548. unsigned int data_blocks;
  549. struct snd_pcm_substream *pcm;
  550. unsigned int pcm_frames;
  551. buffer = s->buffer.packets[s->packet_index].buffer;
  552. data_blocks = payload_quadlets / s->data_block_quadlets;
  553. trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks,
  554. index);
  555. pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL);
  556. s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
  557. if (queue_in_packet(s) < 0)
  558. return -EIO;
  559. pcm = ACCESS_ONCE(s->pcm);
  560. if (pcm && pcm_frames > 0)
  561. update_pcm_pointers(s, pcm, pcm_frames);
  562. return 0;
  563. }
  564. /*
  565. * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
  566. * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
  567. * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
  568. */
  569. static inline u32 compute_cycle_count(u32 tstamp)
  570. {
  571. return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
  572. }
  573. static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
  574. {
  575. cycle += addend;
  576. if (cycle >= 8 * CYCLES_PER_SECOND)
  577. cycle -= 8 * CYCLES_PER_SECOND;
  578. return cycle;
  579. }
  580. static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
  581. {
  582. if (cycle < subtrahend)
  583. cycle += 8 * CYCLES_PER_SECOND;
  584. return cycle - subtrahend;
  585. }
  586. static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
  587. size_t header_length, void *header,
  588. void *private_data)
  589. {
  590. struct amdtp_stream *s = private_data;
  591. unsigned int i, packets = header_length / 4;
  592. u32 cycle;
  593. if (s->packet_index < 0)
  594. return;
  595. cycle = compute_cycle_count(tstamp);
  596. /* Align to actual cycle count for the last packet. */
  597. cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
  598. for (i = 0; i < packets; ++i) {
  599. cycle = increment_cycle_count(cycle, 1);
  600. if (s->handle_packet(s, 0, cycle, i) < 0) {
  601. s->packet_index = -1;
  602. if (in_interrupt())
  603. amdtp_stream_pcm_abort(s);
  604. WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
  605. return;
  606. }
  607. }
  608. fw_iso_context_queue_flush(s->context);
  609. }
  610. static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
  611. size_t header_length, void *header,
  612. void *private_data)
  613. {
  614. struct amdtp_stream *s = private_data;
  615. unsigned int i, packets;
  616. unsigned int payload_length, max_payload_length;
  617. __be32 *headers = header;
  618. u32 cycle;
  619. if (s->packet_index < 0)
  620. return;
  621. /* The number of packets in buffer */
  622. packets = header_length / IN_PACKET_HEADER_SIZE;
  623. cycle = compute_cycle_count(tstamp);
  624. /* Align to actual cycle count for the last packet. */
  625. cycle = decrement_cycle_count(cycle, packets);
  626. /* For buffer-over-run prevention. */
  627. max_payload_length = s->max_payload_length;
  628. for (i = 0; i < packets; i++) {
  629. cycle = increment_cycle_count(cycle, 1);
  630. /* The number of bytes in this packet */
  631. payload_length =
  632. (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT);
  633. if (payload_length > max_payload_length) {
  634. dev_err(&s->unit->device,
  635. "Detect jumbo payload: %04x %04x\n",
  636. payload_length, max_payload_length);
  637. break;
  638. }
  639. if (s->handle_packet(s, payload_length, cycle, i) < 0)
  640. break;
  641. }
  642. /* Queueing error or detecting invalid payload. */
  643. if (i < packets) {
  644. s->packet_index = -1;
  645. if (in_interrupt())
  646. amdtp_stream_pcm_abort(s);
  647. WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
  648. return;
  649. }
  650. fw_iso_context_queue_flush(s->context);
  651. }
  652. /* this is executed one time */
  653. static void amdtp_stream_first_callback(struct fw_iso_context *context,
  654. u32 tstamp, size_t header_length,
  655. void *header, void *private_data)
  656. {
  657. struct amdtp_stream *s = private_data;
  658. u32 cycle;
  659. unsigned int packets;
  660. s->max_payload_length = amdtp_stream_get_max_payload(s);
  661. /*
  662. * For in-stream, first packet has come.
  663. * For out-stream, prepared to transmit first packet
  664. */
  665. s->callbacked = true;
  666. wake_up(&s->callback_wait);
  667. cycle = compute_cycle_count(tstamp);
  668. if (s->direction == AMDTP_IN_STREAM) {
  669. packets = header_length / IN_PACKET_HEADER_SIZE;
  670. cycle = decrement_cycle_count(cycle, packets);
  671. context->callback.sc = in_stream_callback;
  672. if (s->flags & CIP_NO_HEADER)
  673. s->handle_packet = handle_in_packet_without_header;
  674. else
  675. s->handle_packet = handle_in_packet;
  676. } else {
  677. packets = header_length / 4;
  678. cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
  679. context->callback.sc = out_stream_callback;
  680. if (s->flags & CIP_NO_HEADER)
  681. s->handle_packet = handle_out_packet_without_header;
  682. else
  683. s->handle_packet = handle_out_packet;
  684. }
  685. s->start_cycle = cycle;
  686. context->callback.sc(context, tstamp, header_length, header, s);
  687. }
  688. /**
  689. * amdtp_stream_start - start transferring packets
  690. * @s: the AMDTP stream to start
  691. * @channel: the isochronous channel on the bus
  692. * @speed: firewire speed code
  693. *
  694. * The stream cannot be started until it has been configured with
  695. * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
  696. * device can be started.
  697. */
  698. int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
  699. {
  700. static const struct {
  701. unsigned int data_block;
  702. unsigned int syt_offset;
  703. } initial_state[] = {
  704. [CIP_SFC_32000] = { 4, 3072 },
  705. [CIP_SFC_48000] = { 6, 1024 },
  706. [CIP_SFC_96000] = { 12, 1024 },
  707. [CIP_SFC_192000] = { 24, 1024 },
  708. [CIP_SFC_44100] = { 0, 67 },
  709. [CIP_SFC_88200] = { 0, 67 },
  710. [CIP_SFC_176400] = { 0, 67 },
  711. };
  712. unsigned int header_size;
  713. enum dma_data_direction dir;
  714. int type, tag, err;
  715. mutex_lock(&s->mutex);
  716. if (WARN_ON(amdtp_stream_running(s) ||
  717. (s->data_block_quadlets < 1))) {
  718. err = -EBADFD;
  719. goto err_unlock;
  720. }
  721. if (s->direction == AMDTP_IN_STREAM)
  722. s->data_block_counter = UINT_MAX;
  723. else
  724. s->data_block_counter = 0;
  725. s->data_block_state = initial_state[s->sfc].data_block;
  726. s->syt_offset_state = initial_state[s->sfc].syt_offset;
  727. s->last_syt_offset = TICKS_PER_CYCLE;
  728. /* initialize packet buffer */
  729. if (s->direction == AMDTP_IN_STREAM) {
  730. dir = DMA_FROM_DEVICE;
  731. type = FW_ISO_CONTEXT_RECEIVE;
  732. header_size = IN_PACKET_HEADER_SIZE;
  733. } else {
  734. dir = DMA_TO_DEVICE;
  735. type = FW_ISO_CONTEXT_TRANSMIT;
  736. header_size = OUT_PACKET_HEADER_SIZE;
  737. }
  738. err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
  739. amdtp_stream_get_max_payload(s), dir);
  740. if (err < 0)
  741. goto err_unlock;
  742. s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
  743. type, channel, speed, header_size,
  744. amdtp_stream_first_callback, s);
  745. if (IS_ERR(s->context)) {
  746. err = PTR_ERR(s->context);
  747. if (err == -EBUSY)
  748. dev_err(&s->unit->device,
  749. "no free stream on this controller\n");
  750. goto err_buffer;
  751. }
  752. amdtp_stream_update(s);
  753. if (s->flags & CIP_NO_HEADER)
  754. s->tag = TAG_NO_CIP_HEADER;
  755. else
  756. s->tag = TAG_CIP;
  757. s->packet_index = 0;
  758. do {
  759. if (s->direction == AMDTP_IN_STREAM)
  760. err = queue_in_packet(s);
  761. else
  762. err = queue_out_packet(s, 0);
  763. if (err < 0)
  764. goto err_context;
  765. } while (s->packet_index > 0);
  766. /* NOTE: TAG1 matches CIP. This just affects in stream. */
  767. tag = FW_ISO_CONTEXT_MATCH_TAG1;
  768. if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
  769. tag |= FW_ISO_CONTEXT_MATCH_TAG0;
  770. s->callbacked = false;
  771. err = fw_iso_context_start(s->context, -1, 0, tag);
  772. if (err < 0)
  773. goto err_context;
  774. mutex_unlock(&s->mutex);
  775. return 0;
  776. err_context:
  777. fw_iso_context_destroy(s->context);
  778. s->context = ERR_PTR(-1);
  779. err_buffer:
  780. iso_packets_buffer_destroy(&s->buffer, s->unit);
  781. err_unlock:
  782. mutex_unlock(&s->mutex);
  783. return err;
  784. }
  785. EXPORT_SYMBOL(amdtp_stream_start);
  786. /**
  787. * amdtp_stream_pcm_pointer - get the PCM buffer position
  788. * @s: the AMDTP stream that transports the PCM data
  789. *
  790. * Returns the current buffer position, in frames.
  791. */
  792. unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
  793. {
  794. /*
  795. * This function is called in software IRQ context of period_tasklet or
  796. * process context.
  797. *
  798. * When the software IRQ context was scheduled by software IRQ context
  799. * of IR/IT contexts, queued packets were already handled. Therefore,
  800. * no need to flush the queue in buffer anymore.
  801. *
  802. * When the process context reach here, some packets will be already
  803. * queued in the buffer. These packets should be handled immediately
  804. * to keep better granularity of PCM pointer.
  805. *
  806. * Later, the process context will sometimes schedules software IRQ
  807. * context of the period_tasklet. Then, no need to flush the queue by
  808. * the same reason as described for IR/IT contexts.
  809. */
  810. if (!in_interrupt() && amdtp_stream_running(s))
  811. fw_iso_context_flush_completions(s->context);
  812. return ACCESS_ONCE(s->pcm_buffer_pointer);
  813. }
  814. EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
  815. /**
  816. * amdtp_stream_pcm_ack - acknowledge queued PCM frames
  817. * @s: the AMDTP stream that transfers the PCM frames
  818. *
  819. * Returns zero always.
  820. */
  821. int amdtp_stream_pcm_ack(struct amdtp_stream *s)
  822. {
  823. /*
  824. * Process isochronous packets for recent isochronous cycle to handle
  825. * queued PCM frames.
  826. */
  827. if (amdtp_stream_running(s))
  828. fw_iso_context_flush_completions(s->context);
  829. return 0;
  830. }
  831. EXPORT_SYMBOL(amdtp_stream_pcm_ack);
  832. /**
  833. * amdtp_stream_update - update the stream after a bus reset
  834. * @s: the AMDTP stream
  835. */
  836. void amdtp_stream_update(struct amdtp_stream *s)
  837. {
  838. /* Precomputing. */
  839. ACCESS_ONCE(s->source_node_id_field) =
  840. (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
  841. CIP_SID_MASK;
  842. }
  843. EXPORT_SYMBOL(amdtp_stream_update);
  844. /**
  845. * amdtp_stream_stop - stop sending packets
  846. * @s: the AMDTP stream to stop
  847. *
  848. * All PCM and MIDI devices of the stream must be stopped before the stream
  849. * itself can be stopped.
  850. */
  851. void amdtp_stream_stop(struct amdtp_stream *s)
  852. {
  853. mutex_lock(&s->mutex);
  854. if (!amdtp_stream_running(s)) {
  855. mutex_unlock(&s->mutex);
  856. return;
  857. }
  858. tasklet_kill(&s->period_tasklet);
  859. fw_iso_context_stop(s->context);
  860. fw_iso_context_destroy(s->context);
  861. s->context = ERR_PTR(-1);
  862. iso_packets_buffer_destroy(&s->buffer, s->unit);
  863. s->callbacked = false;
  864. mutex_unlock(&s->mutex);
  865. }
  866. EXPORT_SYMBOL(amdtp_stream_stop);
  867. /**
  868. * amdtp_stream_pcm_abort - abort the running PCM device
  869. * @s: the AMDTP stream about to be stopped
  870. *
  871. * If the isochronous stream needs to be stopped asynchronously, call this
  872. * function first to stop the PCM device.
  873. */
  874. void amdtp_stream_pcm_abort(struct amdtp_stream *s)
  875. {
  876. struct snd_pcm_substream *pcm;
  877. pcm = ACCESS_ONCE(s->pcm);
  878. if (pcm)
  879. snd_pcm_stop_xrun(pcm);
  880. }
  881. EXPORT_SYMBOL(amdtp_stream_pcm_abort);