pvrusb2-std.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include "pvrusb2-std.h"
  22. #include "pvrusb2-debug.h"
  23. #include <asm/string.h>
  24. #include <linux/slab.h>
  25. struct std_name {
  26. const char *name;
  27. v4l2_std_id id;
  28. };
  29. #define CSTD_PAL \
  30. (V4L2_STD_PAL_B| \
  31. V4L2_STD_PAL_B1| \
  32. V4L2_STD_PAL_G| \
  33. V4L2_STD_PAL_H| \
  34. V4L2_STD_PAL_I| \
  35. V4L2_STD_PAL_D| \
  36. V4L2_STD_PAL_D1| \
  37. V4L2_STD_PAL_K| \
  38. V4L2_STD_PAL_M| \
  39. V4L2_STD_PAL_N| \
  40. V4L2_STD_PAL_Nc| \
  41. V4L2_STD_PAL_60)
  42. #define CSTD_NTSC \
  43. (V4L2_STD_NTSC_M| \
  44. V4L2_STD_NTSC_M_JP| \
  45. V4L2_STD_NTSC_M_KR| \
  46. V4L2_STD_NTSC_443)
  47. #define CSTD_SECAM \
  48. (V4L2_STD_SECAM_B| \
  49. V4L2_STD_SECAM_D| \
  50. V4L2_STD_SECAM_G| \
  51. V4L2_STD_SECAM_H| \
  52. V4L2_STD_SECAM_K| \
  53. V4L2_STD_SECAM_K1| \
  54. V4L2_STD_SECAM_L| \
  55. V4L2_STD_SECAM_LC)
  56. #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
  57. #define TSTD_B1 (V4L2_STD_PAL_B1)
  58. #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
  59. #define TSTD_D1 (V4L2_STD_PAL_D1)
  60. #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
  61. #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
  62. #define TSTD_I (V4L2_STD_PAL_I)
  63. #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
  64. #define TSTD_K1 (V4L2_STD_SECAM_K1)
  65. #define TSTD_L (V4L2_STD_SECAM_L)
  66. #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
  67. #define TSTD_N (V4L2_STD_PAL_N)
  68. #define TSTD_Nc (V4L2_STD_PAL_Nc)
  69. #define TSTD_60 (V4L2_STD_PAL_60)
  70. #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM)
  71. /* Mapping of standard bits to color system */
  72. static const struct std_name std_groups[] = {
  73. {"PAL",CSTD_PAL},
  74. {"NTSC",CSTD_NTSC},
  75. {"SECAM",CSTD_SECAM},
  76. };
  77. /* Mapping of standard bits to modulation system */
  78. static const struct std_name std_items[] = {
  79. {"B",TSTD_B},
  80. {"B1",TSTD_B1},
  81. {"D",TSTD_D},
  82. {"D1",TSTD_D1},
  83. {"G",TSTD_G},
  84. {"H",TSTD_H},
  85. {"I",TSTD_I},
  86. {"K",TSTD_K},
  87. {"K1",TSTD_K1},
  88. {"L",TSTD_L},
  89. {"LC",V4L2_STD_SECAM_LC},
  90. {"M",TSTD_M},
  91. {"Mj",V4L2_STD_NTSC_M_JP},
  92. {"443",V4L2_STD_NTSC_443},
  93. {"Mk",V4L2_STD_NTSC_M_KR},
  94. {"N",TSTD_N},
  95. {"Nc",TSTD_Nc},
  96. {"60",TSTD_60},
  97. };
  98. // Search an array of std_name structures and return a pointer to the
  99. // element with the matching name.
  100. static const struct std_name *find_std_name(const struct std_name *arrPtr,
  101. unsigned int arrSize,
  102. const char *bufPtr,
  103. unsigned int bufSize)
  104. {
  105. unsigned int idx;
  106. const struct std_name *p;
  107. for (idx = 0; idx < arrSize; idx++) {
  108. p = arrPtr + idx;
  109. if (strlen(p->name) != bufSize) continue;
  110. if (!memcmp(bufPtr,p->name,bufSize)) return p;
  111. }
  112. return NULL;
  113. }
  114. int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
  115. unsigned int bufSize)
  116. {
  117. v4l2_std_id id = 0;
  118. v4l2_std_id cmsk = 0;
  119. v4l2_std_id t;
  120. int mMode = 0;
  121. unsigned int cnt;
  122. char ch;
  123. const struct std_name *sp;
  124. while (bufSize) {
  125. if (!mMode) {
  126. cnt = 0;
  127. while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
  128. if (cnt >= bufSize) return 0; // No more characters
  129. sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
  130. bufPtr,cnt);
  131. if (!sp) return 0; // Illegal color system name
  132. cnt++;
  133. bufPtr += cnt;
  134. bufSize -= cnt;
  135. mMode = !0;
  136. cmsk = sp->id;
  137. continue;
  138. }
  139. cnt = 0;
  140. while (cnt < bufSize) {
  141. ch = bufPtr[cnt];
  142. if (ch == ';') {
  143. mMode = 0;
  144. break;
  145. }
  146. if (ch == '/') break;
  147. cnt++;
  148. }
  149. sp = find_std_name(std_items, ARRAY_SIZE(std_items),
  150. bufPtr,cnt);
  151. if (!sp) return 0; // Illegal modulation system ID
  152. t = sp->id & cmsk;
  153. if (!t) return 0; // Specific color + modulation system illegal
  154. id |= t;
  155. if (cnt < bufSize) cnt++;
  156. bufPtr += cnt;
  157. bufSize -= cnt;
  158. }
  159. if (idPtr) *idPtr = id;
  160. return !0;
  161. }
  162. unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
  163. v4l2_std_id id)
  164. {
  165. unsigned int idx1,idx2;
  166. const struct std_name *ip,*gp;
  167. int gfl,cfl;
  168. unsigned int c1,c2;
  169. cfl = 0;
  170. c1 = 0;
  171. for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
  172. gp = std_groups + idx1;
  173. gfl = 0;
  174. for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
  175. ip = std_items + idx2;
  176. if (!(gp->id & ip->id & id)) continue;
  177. if (!gfl) {
  178. if (cfl) {
  179. c2 = scnprintf(bufPtr,bufSize,";");
  180. c1 += c2;
  181. bufSize -= c2;
  182. bufPtr += c2;
  183. }
  184. cfl = !0;
  185. c2 = scnprintf(bufPtr,bufSize,
  186. "%s-",gp->name);
  187. gfl = !0;
  188. } else {
  189. c2 = scnprintf(bufPtr,bufSize,"/");
  190. }
  191. c1 += c2;
  192. bufSize -= c2;
  193. bufPtr += c2;
  194. c2 = scnprintf(bufPtr,bufSize,
  195. ip->name);
  196. c1 += c2;
  197. bufSize -= c2;
  198. bufPtr += c2;
  199. }
  200. }
  201. return c1;
  202. }
  203. // Template data for possible enumerated video standards. Here we group
  204. // standards which share common frame rates and resolution.
  205. static struct v4l2_standard generic_standards[] = {
  206. {
  207. .id = (TSTD_B|TSTD_B1|
  208. TSTD_D|TSTD_D1|
  209. TSTD_G|
  210. TSTD_H|
  211. TSTD_I|
  212. TSTD_K|TSTD_K1|
  213. TSTD_L|
  214. V4L2_STD_SECAM_LC |
  215. TSTD_N|TSTD_Nc),
  216. .frameperiod =
  217. {
  218. .numerator = 1,
  219. .denominator= 25
  220. },
  221. .framelines = 625,
  222. .reserved = {0,0,0,0}
  223. }, {
  224. .id = (TSTD_M|
  225. V4L2_STD_NTSC_M_JP|
  226. V4L2_STD_NTSC_M_KR),
  227. .frameperiod =
  228. {
  229. .numerator = 1001,
  230. .denominator= 30000
  231. },
  232. .framelines = 525,
  233. .reserved = {0,0,0,0}
  234. }, { // This is a total wild guess
  235. .id = (TSTD_60),
  236. .frameperiod =
  237. {
  238. .numerator = 1001,
  239. .denominator= 30000
  240. },
  241. .framelines = 525,
  242. .reserved = {0,0,0,0}
  243. }, { // This is total wild guess
  244. .id = V4L2_STD_NTSC_443,
  245. .frameperiod =
  246. {
  247. .numerator = 1001,
  248. .denominator= 30000
  249. },
  250. .framelines = 525,
  251. .reserved = {0,0,0,0}
  252. }
  253. };
  254. #define generic_standards_cnt ARRAY_SIZE(generic_standards)
  255. static struct v4l2_standard *match_std(v4l2_std_id id)
  256. {
  257. unsigned int idx;
  258. for (idx = 0; idx < generic_standards_cnt; idx++) {
  259. if (generic_standards[idx].id & id) {
  260. return generic_standards + idx;
  261. }
  262. }
  263. return NULL;
  264. }
  265. static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
  266. {
  267. struct v4l2_standard *template;
  268. int idx;
  269. unsigned int bcnt;
  270. template = match_std(id);
  271. if (!template) return 0;
  272. idx = std->index;
  273. memcpy(std,template,sizeof(*template));
  274. std->index = idx;
  275. std->id = id;
  276. bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
  277. std->name[bcnt] = 0;
  278. pvr2_trace(PVR2_TRACE_INIT,"Set up standard idx=%u name=%s",
  279. std->index,std->name);
  280. return !0;
  281. }
  282. /* These are special cases of combined standards that we should enumerate
  283. separately if the component pieces are present. */
  284. static v4l2_std_id std_mixes[] = {
  285. V4L2_STD_PAL_B | V4L2_STD_PAL_G,
  286. V4L2_STD_PAL_D | V4L2_STD_PAL_K,
  287. V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
  288. V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
  289. };
  290. struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
  291. v4l2_std_id id)
  292. {
  293. unsigned int std_cnt = 0;
  294. unsigned int idx,bcnt,idx2;
  295. v4l2_std_id idmsk,cmsk,fmsk;
  296. struct v4l2_standard *stddefs;
  297. if (pvrusb2_debug & PVR2_TRACE_INIT) {
  298. char buf[50];
  299. bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
  300. pvr2_trace(
  301. PVR2_TRACE_INIT,"Mapping standards mask=0x%x (%.*s)",
  302. (int)id,bcnt,buf);
  303. }
  304. *countptr = 0;
  305. std_cnt = 0;
  306. fmsk = 0;
  307. for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
  308. if (!(idmsk & cmsk)) continue;
  309. cmsk &= ~idmsk;
  310. if (match_std(idmsk)) {
  311. std_cnt++;
  312. continue;
  313. }
  314. fmsk |= idmsk;
  315. }
  316. for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
  317. if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
  318. }
  319. if (fmsk) {
  320. char buf[50];
  321. bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
  322. pvr2_trace(
  323. PVR2_TRACE_ERROR_LEGS,
  324. "WARNING:"
  325. " Failed to classify the following standard(s): %.*s",
  326. bcnt,buf);
  327. }
  328. pvr2_trace(PVR2_TRACE_INIT,"Setting up %u unique standard(s)",
  329. std_cnt);
  330. if (!std_cnt) return NULL; // paranoia
  331. stddefs = kzalloc(sizeof(struct v4l2_standard) * std_cnt,
  332. GFP_KERNEL);
  333. for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
  334. idx = 0;
  335. /* Enumerate potential special cases */
  336. for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
  337. idx2++) {
  338. if (!(id & std_mixes[idx2])) continue;
  339. if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
  340. }
  341. /* Now enumerate individual pieces */
  342. for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
  343. if (!(idmsk & cmsk)) continue;
  344. cmsk &= ~idmsk;
  345. if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
  346. idx++;
  347. }
  348. *countptr = std_cnt;
  349. return stddefs;
  350. }
  351. v4l2_std_id pvr2_std_get_usable(void)
  352. {
  353. return CSTD_ALL;
  354. }
  355. /*
  356. Stuff for Emacs to see, in order to encourage consistent editing style:
  357. *** Local Variables: ***
  358. *** mode: c ***
  359. *** fill-column: 75 ***
  360. *** tab-width: 8 ***
  361. *** c-basic-offset: 8 ***
  362. *** End: ***
  363. */