ff-protocol-former.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. // SPDX-License-Identifier: GPL-2.0
  2. // ff-protocol-former.c - a part of driver for RME Fireface series
  3. //
  4. // Copyright (c) 2019 Takashi Sakamoto
  5. #include <linux/delay.h>
  6. #include "ff.h"
  7. #define FORMER_REG_SYNC_STATUS 0x0000801c0000ull
  8. /* For block write request. */
  9. #define FORMER_REG_FETCH_PCM_FRAMES 0x0000801c0000ull
  10. #define FORMER_REG_CLOCK_CONFIG 0x0000801c0004ull
  11. static int parse_clock_bits(u32 data, unsigned int *rate,
  12. enum snd_ff_clock_src *src)
  13. {
  14. static const struct {
  15. unsigned int rate;
  16. u32 mask;
  17. } *rate_entry, rate_entries[] = {
  18. { 32000, 0x00000002, },
  19. { 44100, 0x00000000, },
  20. { 48000, 0x00000006, },
  21. { 64000, 0x0000000a, },
  22. { 88200, 0x00000008, },
  23. { 96000, 0x0000000e, },
  24. { 128000, 0x00000012, },
  25. { 176400, 0x00000010, },
  26. { 192000, 0x00000016, },
  27. };
  28. static const struct {
  29. enum snd_ff_clock_src src;
  30. u32 mask;
  31. } *clk_entry, clk_entries[] = {
  32. { SND_FF_CLOCK_SRC_ADAT1, 0x00000000, },
  33. { SND_FF_CLOCK_SRC_ADAT2, 0x00000400, },
  34. { SND_FF_CLOCK_SRC_SPDIF, 0x00000c00, },
  35. { SND_FF_CLOCK_SRC_WORD, 0x00001000, },
  36. { SND_FF_CLOCK_SRC_LTC, 0x00001800, },
  37. };
  38. int i;
  39. for (i = 0; i < ARRAY_SIZE(rate_entries); ++i) {
  40. rate_entry = rate_entries + i;
  41. if ((data & 0x0000001e) == rate_entry->mask) {
  42. *rate = rate_entry->rate;
  43. break;
  44. }
  45. }
  46. if (i == ARRAY_SIZE(rate_entries))
  47. return -EIO;
  48. if (data & 0x00000001) {
  49. *src = SND_FF_CLOCK_SRC_INTERNAL;
  50. } else {
  51. for (i = 0; i < ARRAY_SIZE(clk_entries); ++i) {
  52. clk_entry = clk_entries + i;
  53. if ((data & 0x00001c00) == clk_entry->mask) {
  54. *src = clk_entry->src;
  55. break;
  56. }
  57. }
  58. if (i == ARRAY_SIZE(clk_entries))
  59. return -EIO;
  60. }
  61. return 0;
  62. }
  63. static int former_get_clock(struct snd_ff *ff, unsigned int *rate,
  64. enum snd_ff_clock_src *src)
  65. {
  66. __le32 reg;
  67. u32 data;
  68. int err;
  69. err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
  70. FORMER_REG_CLOCK_CONFIG, &reg, sizeof(reg), 0);
  71. if (err < 0)
  72. return err;
  73. data = le32_to_cpu(reg);
  74. return parse_clock_bits(data, rate, src);
  75. }
  76. static int former_switch_fetching_mode(struct snd_ff *ff, bool enable)
  77. {
  78. unsigned int count;
  79. __le32 *reg;
  80. int i;
  81. int err;
  82. count = 0;
  83. for (i = 0; i < SND_FF_STREAM_MODE_COUNT; ++i)
  84. count = max(count, ff->spec->pcm_playback_channels[i]);
  85. reg = kcalloc(count, sizeof(__le32), GFP_KERNEL);
  86. if (!reg)
  87. return -ENOMEM;
  88. if (!enable) {
  89. /*
  90. * Each quadlet is corresponding to data channels in a data
  91. * blocks in reverse order. Precisely, quadlets for available
  92. * data channels should be enabled. Here, I take second best
  93. * to fetch PCM frames from all of data channels regardless of
  94. * stf.
  95. */
  96. for (i = 0; i < count; ++i)
  97. reg[i] = cpu_to_le32(0x00000001);
  98. }
  99. err = snd_fw_transaction(ff->unit, TCODE_WRITE_BLOCK_REQUEST,
  100. FORMER_REG_FETCH_PCM_FRAMES, reg,
  101. sizeof(__le32) * count, 0);
  102. kfree(reg);
  103. return err;
  104. }
  105. static void dump_clock_config(struct snd_ff *ff, struct snd_info_buffer *buffer)
  106. {
  107. __le32 reg;
  108. u32 data;
  109. unsigned int rate;
  110. enum snd_ff_clock_src src;
  111. const char *label;
  112. int err;
  113. err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST,
  114. FORMER_REG_CLOCK_CONFIG, &reg, sizeof(reg), 0);
  115. if (err < 0)
  116. return;
  117. data = le32_to_cpu(reg);
  118. snd_iprintf(buffer, "Output S/PDIF format: %s (Emphasis: %s)\n",
  119. (data & 0x00000020) ? "Professional" : "Consumer",
  120. (data & 0x00000040) ? "on" : "off");
  121. snd_iprintf(buffer, "Optical output interface format: %s\n",
  122. (data & 0x00000100) ? "S/PDIF" : "ADAT");
  123. snd_iprintf(buffer, "Word output single speed: %s\n",
  124. (data & 0x00002000) ? "on" : "off");
  125. snd_iprintf(buffer, "S/PDIF input interface: %s\n",
  126. (data & 0x00000200) ? "Optical" : "Coaxial");
  127. err = parse_clock_bits(data, &rate, &src);
  128. if (err < 0)
  129. return;
  130. label = snd_ff_proc_get_clk_label(src);
  131. if (!label)
  132. return;
  133. snd_iprintf(buffer, "Clock configuration: %d %s\n", rate, label);
  134. }
  135. static void dump_sync_status(struct snd_ff *ff, struct snd_info_buffer *buffer)
  136. {
  137. static const struct {
  138. char *const label;
  139. u32 locked_mask;
  140. u32 synced_mask;
  141. } *clk_entry, clk_entries[] = {
  142. { "WDClk", 0x40000000, 0x20000000, },
  143. { "S/PDIF", 0x00080000, 0x00040000, },
  144. { "ADAT1", 0x00000400, 0x00001000, },
  145. { "ADAT2", 0x00000800, 0x00002000, },
  146. };
  147. static const struct {
  148. char *const label;
  149. u32 mask;
  150. } *referred_entry, referred_entries[] = {
  151. { "ADAT1", 0x00000000, },
  152. { "ADAT2", 0x00400000, },
  153. { "S/PDIF", 0x00c00000, },
  154. { "WDclk", 0x01000000, },
  155. { "TCO", 0x01400000, },
  156. };
  157. static const struct {
  158. unsigned int rate;
  159. u32 mask;
  160. } *rate_entry, rate_entries[] = {
  161. { 32000, 0x02000000, },
  162. { 44100, 0x04000000, },
  163. { 48000, 0x06000000, },
  164. { 64000, 0x08000000, },
  165. { 88200, 0x0a000000, },
  166. { 96000, 0x0c000000, },
  167. { 128000, 0x0e000000, },
  168. { 176400, 0x10000000, },
  169. { 192000, 0x12000000, },
  170. };
  171. __le32 reg[2];
  172. u32 data[2];
  173. int i;
  174. int err;
  175. err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST,
  176. FORMER_REG_SYNC_STATUS, reg, sizeof(reg), 0);
  177. if (err < 0)
  178. return;
  179. data[0] = le32_to_cpu(reg[0]);
  180. data[1] = le32_to_cpu(reg[1]);
  181. snd_iprintf(buffer, "External source detection:\n");
  182. for (i = 0; i < ARRAY_SIZE(clk_entries); ++i) {
  183. const char *state;
  184. clk_entry = clk_entries + i;
  185. if (data[0] & clk_entry->locked_mask) {
  186. if (data[0] & clk_entry->synced_mask)
  187. state = "sync";
  188. else
  189. state = "lock";
  190. } else {
  191. state = "none";
  192. }
  193. snd_iprintf(buffer, "%s: %s\n", clk_entry->label, state);
  194. }
  195. snd_iprintf(buffer, "Referred clock:\n");
  196. if (data[1] & 0x00000001) {
  197. snd_iprintf(buffer, "Internal\n");
  198. } else {
  199. unsigned int rate;
  200. const char *label;
  201. for (i = 0; i < ARRAY_SIZE(referred_entries); ++i) {
  202. referred_entry = referred_entries + i;
  203. if ((data[0] & 0x1e0000) == referred_entry->mask) {
  204. label = referred_entry->label;
  205. break;
  206. }
  207. }
  208. if (i == ARRAY_SIZE(referred_entries))
  209. label = "none";
  210. for (i = 0; i < ARRAY_SIZE(rate_entries); ++i) {
  211. rate_entry = rate_entries + i;
  212. if ((data[0] & 0x1e000000) == rate_entry->mask) {
  213. rate = rate_entry->rate;
  214. break;
  215. }
  216. }
  217. if (i == ARRAY_SIZE(rate_entries))
  218. rate = 0;
  219. snd_iprintf(buffer, "%s %d\n", label, rate);
  220. }
  221. }
  222. static void former_dump_status(struct snd_ff *ff,
  223. struct snd_info_buffer *buffer)
  224. {
  225. dump_clock_config(ff, buffer);
  226. dump_sync_status(ff, buffer);
  227. }
  228. static int former_fill_midi_msg(struct snd_ff *ff,
  229. struct snd_rawmidi_substream *substream,
  230. unsigned int port)
  231. {
  232. u8 *buf = (u8 *)ff->msg_buf[port];
  233. int len;
  234. int i;
  235. len = snd_rawmidi_transmit_peek(substream, buf,
  236. SND_FF_MAXIMIM_MIDI_QUADS);
  237. if (len <= 0)
  238. return len;
  239. // One quadlet includes one byte.
  240. for (i = len - 1; i >= 0; --i)
  241. ff->msg_buf[port][i] = cpu_to_le32(buf[i]);
  242. ff->rx_bytes[port] = len;
  243. return len;
  244. }
  245. #define FF800_STF 0x0000fc88f000
  246. #define FF800_RX_PACKET_FORMAT 0x0000fc88f004
  247. #define FF800_ALLOC_TX_STREAM 0x0000fc88f008
  248. #define FF800_ISOC_COMM_START 0x0000fc88f00c
  249. #define FF800_TX_S800_FLAG 0x00000800
  250. #define FF800_ISOC_COMM_STOP 0x0000fc88f010
  251. #define FF800_TX_PACKET_ISOC_CH 0x0000801c0008
  252. static int allocate_tx_resources(struct snd_ff *ff)
  253. {
  254. __le32 reg;
  255. unsigned int count;
  256. unsigned int tx_isoc_channel;
  257. int err;
  258. reg = cpu_to_le32(ff->tx_stream.data_block_quadlets);
  259. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  260. FF800_ALLOC_TX_STREAM, &reg, sizeof(reg), 0);
  261. if (err < 0)
  262. return err;
  263. // Wait till the format of tx packet is available.
  264. count = 0;
  265. while (count++ < 10) {
  266. u32 data;
  267. err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
  268. FF800_TX_PACKET_ISOC_CH, &reg, sizeof(reg), 0);
  269. if (err < 0)
  270. return err;
  271. data = le32_to_cpu(reg);
  272. if (data != 0xffffffff) {
  273. tx_isoc_channel = data;
  274. break;
  275. }
  276. msleep(50);
  277. }
  278. if (count >= 10)
  279. return -ETIMEDOUT;
  280. // NOTE: this is a makeshift to start OHCI 1394 IR context in the
  281. // channel. On the other hand, 'struct fw_iso_resources.allocated' is
  282. // not true and it's not deallocated at stop.
  283. ff->tx_resources.channel = tx_isoc_channel;
  284. return 0;
  285. }
  286. static int ff800_allocate_resources(struct snd_ff *ff, unsigned int rate)
  287. {
  288. u32 data;
  289. __le32 reg;
  290. int err;
  291. reg = cpu_to_le32(rate);
  292. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  293. FF800_STF, &reg, sizeof(reg), 0);
  294. if (err < 0)
  295. return err;
  296. // If starting isochronous communication immediately, change of STF has
  297. // no effect. In this case, the communication runs based on former STF.
  298. // Let's sleep for a bit.
  299. msleep(100);
  300. // Controllers should allocate isochronous resources for rx stream.
  301. err = fw_iso_resources_allocate(&ff->rx_resources,
  302. amdtp_stream_get_max_payload(&ff->rx_stream),
  303. fw_parent_device(ff->unit)->max_speed);
  304. if (err < 0)
  305. return err;
  306. // Set isochronous channel and the number of quadlets of rx packets.
  307. // This should be done before the allocation of tx resources to avoid
  308. // periodical noise.
  309. data = ff->rx_stream.data_block_quadlets << 3;
  310. data = (data << 8) | ff->rx_resources.channel;
  311. reg = cpu_to_le32(data);
  312. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  313. FF800_RX_PACKET_FORMAT, &reg, sizeof(reg), 0);
  314. if (err < 0)
  315. return err;
  316. return allocate_tx_resources(ff);
  317. }
  318. static int ff800_begin_session(struct snd_ff *ff, unsigned int rate)
  319. {
  320. unsigned int generation = ff->rx_resources.generation;
  321. __le32 reg;
  322. if (generation != fw_parent_device(ff->unit)->card->generation) {
  323. int err = fw_iso_resources_update(&ff->rx_resources);
  324. if (err < 0)
  325. return err;
  326. }
  327. reg = cpu_to_le32(0x80000000);
  328. reg |= cpu_to_le32(ff->tx_stream.data_block_quadlets);
  329. if (fw_parent_device(ff->unit)->max_speed == SCODE_800)
  330. reg |= cpu_to_le32(FF800_TX_S800_FLAG);
  331. return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  332. FF800_ISOC_COMM_START, &reg, sizeof(reg), 0);
  333. }
  334. static void ff800_finish_session(struct snd_ff *ff)
  335. {
  336. __le32 reg;
  337. reg = cpu_to_le32(0x80000000);
  338. snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  339. FF800_ISOC_COMM_STOP, &reg, sizeof(reg), 0);
  340. }
  341. // Fireface 800 doesn't allow drivers to register lower 4 bytes of destination
  342. // address.
  343. // A write transaction to clear registered higher 4 bytes of destination address
  344. // has an effect to suppress asynchronous transaction from device.
  345. static void ff800_handle_midi_msg(struct snd_ff *ff, unsigned int offset, const __le32 *buf,
  346. size_t length, u32 tstamp)
  347. {
  348. int i;
  349. for (i = 0; i < length / 4; i++) {
  350. u8 byte = le32_to_cpu(buf[i]) & 0xff;
  351. struct snd_rawmidi_substream *substream;
  352. substream = READ_ONCE(ff->tx_midi_substreams[0]);
  353. if (substream)
  354. snd_rawmidi_receive(substream, &byte, 1);
  355. }
  356. }
  357. const struct snd_ff_protocol snd_ff_protocol_ff800 = {
  358. .handle_msg = ff800_handle_midi_msg,
  359. .fill_midi_msg = former_fill_midi_msg,
  360. .get_clock = former_get_clock,
  361. .switch_fetching_mode = former_switch_fetching_mode,
  362. .allocate_resources = ff800_allocate_resources,
  363. .begin_session = ff800_begin_session,
  364. .finish_session = ff800_finish_session,
  365. .dump_status = former_dump_status,
  366. };
  367. #define FF400_STF 0x000080100500ull
  368. #define FF400_RX_PACKET_FORMAT 0x000080100504ull
  369. #define FF400_ISOC_COMM_START 0x000080100508ull
  370. #define FF400_TX_PACKET_FORMAT 0x00008010050cull
  371. #define FF400_ISOC_COMM_STOP 0x000080100510ull
  372. // Fireface 400 manages isochronous channel number in 3 bit field. Therefore,
  373. // we can allocate between 0 and 7 channel.
  374. static int ff400_allocate_resources(struct snd_ff *ff, unsigned int rate)
  375. {
  376. __le32 reg;
  377. enum snd_ff_stream_mode mode;
  378. int i;
  379. int err;
  380. // Check whether the given value is supported or not.
  381. for (i = 0; i < CIP_SFC_COUNT; i++) {
  382. if (amdtp_rate_table[i] == rate)
  383. break;
  384. }
  385. if (i >= CIP_SFC_COUNT)
  386. return -EINVAL;
  387. // Set the number of data blocks transferred in a second.
  388. reg = cpu_to_le32(rate);
  389. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  390. FF400_STF, &reg, sizeof(reg), 0);
  391. if (err < 0)
  392. return err;
  393. msleep(100);
  394. err = snd_ff_stream_get_multiplier_mode(i, &mode);
  395. if (err < 0)
  396. return err;
  397. // Keep resources for in-stream.
  398. ff->tx_resources.channels_mask = 0x00000000000000ffuLL;
  399. err = fw_iso_resources_allocate(&ff->tx_resources,
  400. amdtp_stream_get_max_payload(&ff->tx_stream),
  401. fw_parent_device(ff->unit)->max_speed);
  402. if (err < 0)
  403. return err;
  404. // Keep resources for out-stream.
  405. ff->rx_resources.channels_mask = 0x00000000000000ffuLL;
  406. err = fw_iso_resources_allocate(&ff->rx_resources,
  407. amdtp_stream_get_max_payload(&ff->rx_stream),
  408. fw_parent_device(ff->unit)->max_speed);
  409. if (err < 0)
  410. fw_iso_resources_free(&ff->tx_resources);
  411. return err;
  412. }
  413. static int ff400_begin_session(struct snd_ff *ff, unsigned int rate)
  414. {
  415. unsigned int generation = ff->rx_resources.generation;
  416. __le32 reg;
  417. int err;
  418. if (generation != fw_parent_device(ff->unit)->card->generation) {
  419. err = fw_iso_resources_update(&ff->tx_resources);
  420. if (err < 0)
  421. return err;
  422. err = fw_iso_resources_update(&ff->rx_resources);
  423. if (err < 0)
  424. return err;
  425. }
  426. // Set isochronous channel and the number of quadlets of received
  427. // packets.
  428. reg = cpu_to_le32(((ff->rx_stream.data_block_quadlets << 3) << 8) |
  429. ff->rx_resources.channel);
  430. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  431. FF400_RX_PACKET_FORMAT, &reg, sizeof(reg), 0);
  432. if (err < 0)
  433. return err;
  434. // Set isochronous channel and the number of quadlets of transmitted
  435. // packet.
  436. // TODO: investigate the purpose of this 0x80.
  437. reg = cpu_to_le32((0x80 << 24) |
  438. (ff->tx_resources.channel << 5) |
  439. (ff->tx_stream.data_block_quadlets));
  440. err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  441. FF400_TX_PACKET_FORMAT, &reg, sizeof(reg), 0);
  442. if (err < 0)
  443. return err;
  444. // Allow to transmit packets.
  445. reg = cpu_to_le32(0x00000001);
  446. return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  447. FF400_ISOC_COMM_START, &reg, sizeof(reg), 0);
  448. }
  449. static void ff400_finish_session(struct snd_ff *ff)
  450. {
  451. __le32 reg;
  452. reg = cpu_to_le32(0x80000000);
  453. snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  454. FF400_ISOC_COMM_STOP, &reg, sizeof(reg), 0);
  455. }
  456. static void parse_midi_msg(struct snd_ff *ff, u32 quad, unsigned int port)
  457. {
  458. struct snd_rawmidi_substream *substream = READ_ONCE(ff->tx_midi_substreams[port]);
  459. if (substream != NULL) {
  460. u8 byte = (quad >> (16 * port)) & 0x000000ff;
  461. snd_rawmidi_receive(substream, &byte, 1);
  462. }
  463. }
  464. #define FF400_QUEUE_SIZE 32
  465. struct ff400_msg_parser {
  466. struct {
  467. u32 msg;
  468. u32 tstamp;
  469. } msgs[FF400_QUEUE_SIZE];
  470. size_t push_pos;
  471. size_t pull_pos;
  472. };
  473. static bool ff400_has_msg(struct snd_ff *ff)
  474. {
  475. struct ff400_msg_parser *parser = ff->msg_parser;
  476. return (parser->push_pos != parser->pull_pos);
  477. }
  478. // For Fireface 400, lower 4 bytes of destination address is configured by bit
  479. // flag in quadlet register (little endian) at 0x'0000'801'0051c. Drivers can
  480. // select one of 4 options:
  481. //
  482. // bit flags: offset of destination address
  483. // - 0x04000000: 0x'....'....'0000'0000
  484. // - 0x08000000: 0x'....'....'0000'0080
  485. // - 0x10000000: 0x'....'....'0000'0100
  486. // - 0x20000000: 0x'....'....'0000'0180
  487. //
  488. // Drivers can suppress the device to transfer asynchronous transactions by
  489. // using below 2 bits.
  490. // - 0x01000000: suppress transmission
  491. // - 0x02000000: suppress transmission
  492. //
  493. // Actually, the register is write-only and includes the other options such as
  494. // input attenuation. This driver allocates destination address with '0000'0000
  495. // in its lower offset and expects userspace application to configure the
  496. // register for it.
  497. // When the message is for signal level operation, the upper 4 bits in MSB expresses the pair of
  498. // stereo physical port.
  499. // - 0: Microphone input 0/1
  500. // - 1: Line input 0/1
  501. // - [2-4]: Line output 0-5
  502. // - 5: Headphone output 0/1
  503. // - 6: S/PDIF output 0/1
  504. // - [7-10]: ADAT output 0-7
  505. //
  506. // The value of signal level can be detected by mask of 0x00fffc00. For signal level of microphone
  507. // input:
  508. //
  509. // - 0: 0.0 dB
  510. // - 10: +10.0 dB
  511. // - 11: +11.0 dB
  512. // - 12: +12.0 dB
  513. // - ...
  514. // - 63: +63.0 dB:
  515. // - 64: +64.0 dB:
  516. // - 65: +65.0 dB:
  517. //
  518. // For signal level of line input:
  519. //
  520. // - 0: 0.0 dB
  521. // - 1: +0.5 dB
  522. // - 2: +1.0 dB
  523. // - 3: +1.5 dB
  524. // - ...
  525. // - 34: +17.0 dB:
  526. // - 35: +17.5 dB:
  527. // - 36: +18.0 dB:
  528. //
  529. // For signal level of any type of output:
  530. //
  531. // - 63: -infinite
  532. // - 62: -58.0 dB
  533. // - 61: -56.0 dB
  534. // - 60: -54.0 dB
  535. // - 59: -53.0 dB
  536. // - 58: -52.0 dB
  537. // - ...
  538. // - 7: -1.0 dB
  539. // - 6: 0.0 dB
  540. // - 5: +1.0 dB
  541. // - ...
  542. // - 2: +4.0 dB
  543. // - 1: +5.0 dB
  544. // - 0: +6.0 dB
  545. //
  546. // When the message is not for signal level operation, it's for MIDI bytes. When matching to
  547. // FF400_MSG_FLAG_IS_MIDI_PORT_0, one MIDI byte can be detected by mask of 0x000000ff. When
  548. // matching to FF400_MSG_FLAG_IS_MIDI_PORT_1, one MIDI byte can be detected by mask of 0x00ff0000.
  549. #define FF400_MSG_FLAG_IS_SIGNAL_LEVEL 0x04000000
  550. #define FF400_MSG_FLAG_IS_RIGHT_CHANNEL 0x08000000
  551. #define FF400_MSG_FLAG_IS_STEREO_PAIRED 0x02000000
  552. #define FF400_MSG_MASK_STEREO_PAIR 0xf0000000
  553. #define FF400_MSG_MASK_SIGNAL_LEVEL 0x00fffc00
  554. #define FF400_MSG_FLAG_IS_MIDI_PORT_0 0x00000100
  555. #define FF400_MSG_MASK_MIDI_PORT_0 0x000000ff
  556. #define FF400_MSG_FLAG_IS_MIDI_PORT_1 0x01000000
  557. #define FF400_MSG_MASK_MIDI_PORT_1 0x00ff0000
  558. static void ff400_handle_msg(struct snd_ff *ff, unsigned int offset, const __le32 *buf,
  559. size_t length, u32 tstamp)
  560. {
  561. bool need_hwdep_wake_up = false;
  562. int i;
  563. for (i = 0; i < length / 4; i++) {
  564. u32 quad = le32_to_cpu(buf[i]);
  565. if (quad & FF400_MSG_FLAG_IS_SIGNAL_LEVEL) {
  566. struct ff400_msg_parser *parser = ff->msg_parser;
  567. parser->msgs[parser->push_pos].msg = quad;
  568. parser->msgs[parser->push_pos].tstamp = tstamp;
  569. ++parser->push_pos;
  570. if (parser->push_pos >= FF400_QUEUE_SIZE)
  571. parser->push_pos = 0;
  572. need_hwdep_wake_up = true;
  573. } else if (quad & FF400_MSG_FLAG_IS_MIDI_PORT_0) {
  574. parse_midi_msg(ff, quad, 0);
  575. } else if (quad & FF400_MSG_FLAG_IS_MIDI_PORT_1) {
  576. parse_midi_msg(ff, quad, 1);
  577. }
  578. }
  579. if (need_hwdep_wake_up)
  580. wake_up(&ff->hwdep_wait);
  581. }
  582. static long ff400_copy_msg_to_user(struct snd_ff *ff, char __user *buf, long count)
  583. {
  584. struct snd_firewire_event_ff400_message ev = {
  585. .type = SNDRV_FIREWIRE_EVENT_FF400_MESSAGE,
  586. .message_count = 0,
  587. };
  588. struct ff400_msg_parser *parser = ff->msg_parser;
  589. long consumed = 0;
  590. long ret = 0;
  591. if (count < sizeof(ev) || parser->pull_pos == parser->push_pos)
  592. return 0;
  593. count -= sizeof(ev);
  594. consumed += sizeof(ev);
  595. while (count >= sizeof(*parser->msgs) && parser->pull_pos != parser->push_pos) {
  596. spin_unlock_irq(&ff->lock);
  597. if (copy_to_user(buf + consumed, parser->msgs + parser->pull_pos,
  598. sizeof(*parser->msgs)))
  599. ret = -EFAULT;
  600. spin_lock_irq(&ff->lock);
  601. if (ret)
  602. return ret;
  603. ++parser->pull_pos;
  604. if (parser->pull_pos >= FF400_QUEUE_SIZE)
  605. parser->pull_pos = 0;
  606. ++ev.message_count;
  607. count -= sizeof(*parser->msgs);
  608. consumed += sizeof(*parser->msgs);
  609. }
  610. spin_unlock_irq(&ff->lock);
  611. if (copy_to_user(buf, &ev, sizeof(ev)))
  612. ret = -EFAULT;
  613. spin_lock_irq(&ff->lock);
  614. if (ret)
  615. return ret;
  616. return consumed;
  617. }
  618. const struct snd_ff_protocol snd_ff_protocol_ff400 = {
  619. .msg_parser_size = sizeof(struct ff400_msg_parser),
  620. .has_msg = ff400_has_msg,
  621. .copy_msg_to_user = ff400_copy_msg_to_user,
  622. .handle_msg = ff400_handle_msg,
  623. .fill_midi_msg = former_fill_midi_msg,
  624. .get_clock = former_get_clock,
  625. .switch_fetching_mode = former_switch_fetching_mode,
  626. .allocate_resources = ff400_allocate_resources,
  627. .begin_session = ff400_begin_session,
  628. .finish_session = ff400_finish_session,
  629. .dump_status = former_dump_status,
  630. };