vivid-rds-gen.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * vivid-rds-gen.c - rds (radio data system) generator support functions.
  3. *
  4. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/ktime.h>
  21. #include <linux/string.h>
  22. #include <linux/videodev2.h>
  23. #include "vivid-rds-gen.h"
  24. static u8 vivid_get_di(const struct vivid_rds_gen *rds, unsigned grp)
  25. {
  26. switch (grp) {
  27. case 0:
  28. return (rds->dyn_pty << 2) | (grp & 3);
  29. case 1:
  30. return (rds->compressed << 2) | (grp & 3);
  31. case 2:
  32. return (rds->art_head << 2) | (grp & 3);
  33. case 3:
  34. return (rds->mono_stereo << 2) | (grp & 3);
  35. }
  36. return 0;
  37. }
  38. /*
  39. * This RDS generator creates 57 RDS groups (one group == four RDS blocks).
  40. * Groups 0-3, 22-25 and 44-47 (spaced 22 groups apart) are filled with a
  41. * standard 0B group containing the PI code and PS name.
  42. *
  43. * Groups 4-19 and 26-41 use group 2A for the radio text.
  44. *
  45. * Group 56 contains the time (group 4A).
  46. *
  47. * All remaining groups use a filler group 15B block that just repeats
  48. * the PI and PTY codes.
  49. */
  50. void vivid_rds_generate(struct vivid_rds_gen *rds)
  51. {
  52. struct v4l2_rds_data *data = rds->data;
  53. unsigned grp;
  54. struct tm tm;
  55. unsigned date;
  56. unsigned time;
  57. int l;
  58. for (grp = 0; grp < VIVID_RDS_GEN_GROUPS; grp++, data += VIVID_RDS_GEN_BLKS_PER_GRP) {
  59. data[0].lsb = rds->picode & 0xff;
  60. data[0].msb = rds->picode >> 8;
  61. data[0].block = V4L2_RDS_BLOCK_A | (V4L2_RDS_BLOCK_A << 3);
  62. data[1].lsb = rds->pty << 5;
  63. data[1].msb = (rds->pty >> 3) | (rds->tp << 2);
  64. data[1].block = V4L2_RDS_BLOCK_B | (V4L2_RDS_BLOCK_B << 3);
  65. data[3].block = V4L2_RDS_BLOCK_D | (V4L2_RDS_BLOCK_D << 3);
  66. switch (grp) {
  67. case 0 ... 3:
  68. case 22 ... 25:
  69. case 44 ... 47: /* Group 0B */
  70. data[1].lsb |= (rds->ta << 4) | (rds->ms << 3);
  71. data[1].lsb |= vivid_get_di(rds, grp % 22);
  72. data[1].msb |= 1 << 3;
  73. data[2].lsb = rds->picode & 0xff;
  74. data[2].msb = rds->picode >> 8;
  75. data[2].block = V4L2_RDS_BLOCK_C_ALT | (V4L2_RDS_BLOCK_C_ALT << 3);
  76. data[3].lsb = rds->psname[2 * (grp % 22) + 1];
  77. data[3].msb = rds->psname[2 * (grp % 22)];
  78. break;
  79. case 4 ... 19:
  80. case 26 ... 41: /* Group 2A */
  81. data[1].lsb |= (grp - 4) % 22;
  82. data[1].msb |= 4 << 3;
  83. data[2].msb = rds->radiotext[4 * ((grp - 4) % 22)];
  84. data[2].lsb = rds->radiotext[4 * ((grp - 4) % 22) + 1];
  85. data[2].block = V4L2_RDS_BLOCK_C | (V4L2_RDS_BLOCK_C << 3);
  86. data[3].msb = rds->radiotext[4 * ((grp - 4) % 22) + 2];
  87. data[3].lsb = rds->radiotext[4 * ((grp - 4) % 22) + 3];
  88. break;
  89. case 56:
  90. /*
  91. * Group 4A
  92. *
  93. * Uses the algorithm from Annex G of the RDS standard
  94. * EN 50067:1998 to convert a UTC date to an RDS Modified
  95. * Julian Day.
  96. */
  97. time_to_tm(get_seconds(), 0, &tm);
  98. l = tm.tm_mon <= 1;
  99. date = 14956 + tm.tm_mday + ((tm.tm_year - l) * 1461) / 4 +
  100. ((tm.tm_mon + 2 + l * 12) * 306001) / 10000;
  101. time = (tm.tm_hour << 12) |
  102. (tm.tm_min << 6) |
  103. (sys_tz.tz_minuteswest >= 0 ? 0x20 : 0) |
  104. (abs(sys_tz.tz_minuteswest) / 30);
  105. data[1].lsb &= ~3;
  106. data[1].lsb |= date >> 15;
  107. data[1].msb |= 8 << 3;
  108. data[2].lsb = (date << 1) & 0xfe;
  109. data[2].lsb |= (time >> 16) & 1;
  110. data[2].msb = (date >> 7) & 0xff;
  111. data[2].block = V4L2_RDS_BLOCK_C | (V4L2_RDS_BLOCK_C << 3);
  112. data[3].lsb = time & 0xff;
  113. data[3].msb = (time >> 8) & 0xff;
  114. break;
  115. default: /* Group 15B */
  116. data[1].lsb |= (rds->ta << 4) | (rds->ms << 3);
  117. data[1].lsb |= vivid_get_di(rds, grp % 22);
  118. data[1].msb |= 0x1f << 3;
  119. data[2].lsb = rds->picode & 0xff;
  120. data[2].msb = rds->picode >> 8;
  121. data[2].block = V4L2_RDS_BLOCK_C_ALT | (V4L2_RDS_BLOCK_C_ALT << 3);
  122. data[3].lsb = rds->pty << 5;
  123. data[3].lsb |= (rds->ta << 4) | (rds->ms << 3);
  124. data[3].lsb |= vivid_get_di(rds, grp % 22);
  125. data[3].msb |= rds->pty >> 3;
  126. data[3].msb |= 0x1f << 3;
  127. break;
  128. }
  129. }
  130. }
  131. void vivid_rds_gen_fill(struct vivid_rds_gen *rds, unsigned freq,
  132. bool alt)
  133. {
  134. /* Alternate PTY between Info and Weather */
  135. if (rds->use_rbds) {
  136. rds->picode = 0x2e75; /* 'KLNX' call sign */
  137. rds->pty = alt ? 29 : 2;
  138. } else {
  139. rds->picode = 0x8088;
  140. rds->pty = alt ? 16 : 3;
  141. }
  142. rds->mono_stereo = true;
  143. rds->art_head = false;
  144. rds->compressed = false;
  145. rds->dyn_pty = false;
  146. rds->tp = true;
  147. rds->ta = alt;
  148. rds->ms = true;
  149. snprintf(rds->psname, sizeof(rds->psname), "%6d.%1d",
  150. freq / 16, ((freq & 0xf) * 10) / 16);
  151. if (alt)
  152. strlcpy(rds->radiotext,
  153. " The Radio Data System can switch between different Radio Texts ",
  154. sizeof(rds->radiotext));
  155. else
  156. strlcpy(rds->radiotext,
  157. "An example of Radio Text as transmitted by the Radio Data System",
  158. sizeof(rds->radiotext));
  159. }