firedtv-avc.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. /*
  2. * FireDTV driver (formerly known as FireSAT)
  3. *
  4. * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
  5. * Copyright (C) 2008 Ben Backx <ben@bbackx.com>
  6. * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <linux/bug.h>
  14. #include <linux/crc32.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kernel.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/mutex.h>
  21. #include <linux/string.h>
  22. #include <linux/stringify.h>
  23. #include <linux/wait.h>
  24. #include <linux/workqueue.h>
  25. #include <dvb_frontend.h>
  26. #include "firedtv.h"
  27. #define FCP_COMMAND_REGISTER 0xfffff0000b00ULL
  28. #define AVC_CTYPE_CONTROL 0x0
  29. #define AVC_CTYPE_STATUS 0x1
  30. #define AVC_CTYPE_NOTIFY 0x3
  31. #define AVC_RESPONSE_ACCEPTED 0x9
  32. #define AVC_RESPONSE_STABLE 0xc
  33. #define AVC_RESPONSE_CHANGED 0xd
  34. #define AVC_RESPONSE_INTERIM 0xf
  35. #define AVC_SUBUNIT_TYPE_TUNER (0x05 << 3)
  36. #define AVC_SUBUNIT_TYPE_UNIT (0x1f << 3)
  37. #define AVC_OPCODE_VENDOR 0x00
  38. #define AVC_OPCODE_READ_DESCRIPTOR 0x09
  39. #define AVC_OPCODE_DSIT 0xc8
  40. #define AVC_OPCODE_DSD 0xcb
  41. #define DESCRIPTOR_TUNER_STATUS 0x80
  42. #define DESCRIPTOR_SUBUNIT_IDENTIFIER 0x00
  43. #define SFE_VENDOR_DE_COMPANYID_0 0x00 /* OUI of Digital Everywhere */
  44. #define SFE_VENDOR_DE_COMPANYID_1 0x12
  45. #define SFE_VENDOR_DE_COMPANYID_2 0x87
  46. #define SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL 0x0a
  47. #define SFE_VENDOR_OPCODE_LNB_CONTROL 0x52
  48. #define SFE_VENDOR_OPCODE_TUNE_QPSK 0x58 /* for DVB-S */
  49. #define SFE_VENDOR_OPCODE_GET_FIRMWARE_VERSION 0x00
  50. #define SFE_VENDOR_OPCODE_HOST2CA 0x56
  51. #define SFE_VENDOR_OPCODE_CA2HOST 0x57
  52. #define SFE_VENDOR_OPCODE_CISTATUS 0x59
  53. #define SFE_VENDOR_OPCODE_TUNE_QPSK2 0x60 /* for DVB-S2 */
  54. #define SFE_VENDOR_TAG_CA_RESET 0x00
  55. #define SFE_VENDOR_TAG_CA_APPLICATION_INFO 0x01
  56. #define SFE_VENDOR_TAG_CA_PMT 0x02
  57. #define SFE_VENDOR_TAG_CA_DATE_TIME 0x04
  58. #define SFE_VENDOR_TAG_CA_MMI 0x05
  59. #define SFE_VENDOR_TAG_CA_ENTER_MENU 0x07
  60. #define EN50221_LIST_MANAGEMENT_ONLY 0x03
  61. #define EN50221_TAG_APP_INFO 0x9f8021
  62. #define EN50221_TAG_CA_INFO 0x9f8031
  63. struct avc_command_frame {
  64. u8 ctype;
  65. u8 subunit;
  66. u8 opcode;
  67. u8 operand[509];
  68. };
  69. struct avc_response_frame {
  70. u8 response;
  71. u8 subunit;
  72. u8 opcode;
  73. u8 operand[509];
  74. };
  75. #define LAST_OPERAND (509 - 1)
  76. static inline void clear_operands(struct avc_command_frame *c, int from, int to)
  77. {
  78. memset(&c->operand[from], 0, to - from + 1);
  79. }
  80. static void pad_operands(struct avc_command_frame *c, int from)
  81. {
  82. int to = ALIGN(from, 4);
  83. if (from <= to && to <= LAST_OPERAND)
  84. clear_operands(c, from, to);
  85. }
  86. #define AVC_DEBUG_READ_DESCRIPTOR 0x0001
  87. #define AVC_DEBUG_DSIT 0x0002
  88. #define AVC_DEBUG_DSD 0x0004
  89. #define AVC_DEBUG_REGISTER_REMOTE_CONTROL 0x0008
  90. #define AVC_DEBUG_LNB_CONTROL 0x0010
  91. #define AVC_DEBUG_TUNE_QPSK 0x0020
  92. #define AVC_DEBUG_TUNE_QPSK2 0x0040
  93. #define AVC_DEBUG_HOST2CA 0x0080
  94. #define AVC_DEBUG_CA2HOST 0x0100
  95. #define AVC_DEBUG_APPLICATION_PMT 0x4000
  96. #define AVC_DEBUG_FCP_PAYLOADS 0x8000
  97. static int avc_debug;
  98. module_param_named(debug, avc_debug, int, 0644);
  99. MODULE_PARM_DESC(debug, "Verbose logging (none = 0"
  100. ", FCP subactions"
  101. ": READ DESCRIPTOR = " __stringify(AVC_DEBUG_READ_DESCRIPTOR)
  102. ", DSIT = " __stringify(AVC_DEBUG_DSIT)
  103. ", REGISTER_REMOTE_CONTROL = " __stringify(AVC_DEBUG_REGISTER_REMOTE_CONTROL)
  104. ", LNB CONTROL = " __stringify(AVC_DEBUG_LNB_CONTROL)
  105. ", TUNE QPSK = " __stringify(AVC_DEBUG_TUNE_QPSK)
  106. ", TUNE QPSK2 = " __stringify(AVC_DEBUG_TUNE_QPSK2)
  107. ", HOST2CA = " __stringify(AVC_DEBUG_HOST2CA)
  108. ", CA2HOST = " __stringify(AVC_DEBUG_CA2HOST)
  109. "; Application sent PMT = " __stringify(AVC_DEBUG_APPLICATION_PMT)
  110. ", FCP payloads = " __stringify(AVC_DEBUG_FCP_PAYLOADS)
  111. ", or a combination, or all = -1)");
  112. /*
  113. * This is a workaround since there is no vendor specific command to retrieve
  114. * ca_info using AVC. If this parameter is not used, ca_system_id will be
  115. * filled with application_manufacturer from ca_app_info.
  116. * Digital Everywhere have said that adding ca_info is on their TODO list.
  117. */
  118. static unsigned int num_fake_ca_system_ids;
  119. static int fake_ca_system_ids[4] = { -1, -1, -1, -1 };
  120. module_param_array(fake_ca_system_ids, int, &num_fake_ca_system_ids, 0644);
  121. MODULE_PARM_DESC(fake_ca_system_ids, "If your CAM application manufacturer "
  122. "does not have the same ca_system_id as your CAS, you can "
  123. "override what ca_system_ids are presented to the "
  124. "application by setting this field to an array of ids.");
  125. static const char *debug_fcp_ctype(unsigned int ctype)
  126. {
  127. static const char *ctypes[] = {
  128. [0x0] = "CONTROL", [0x1] = "STATUS",
  129. [0x2] = "SPECIFIC INQUIRY", [0x3] = "NOTIFY",
  130. [0x4] = "GENERAL INQUIRY", [0x8] = "NOT IMPLEMENTED",
  131. [0x9] = "ACCEPTED", [0xa] = "REJECTED",
  132. [0xb] = "IN TRANSITION", [0xc] = "IMPLEMENTED/STABLE",
  133. [0xd] = "CHANGED", [0xf] = "INTERIM",
  134. };
  135. const char *ret = ctype < ARRAY_SIZE(ctypes) ? ctypes[ctype] : NULL;
  136. return ret ? ret : "?";
  137. }
  138. static const char *debug_fcp_opcode(unsigned int opcode,
  139. const u8 *data, int length)
  140. {
  141. switch (opcode) {
  142. case AVC_OPCODE_VENDOR:
  143. break;
  144. case AVC_OPCODE_READ_DESCRIPTOR:
  145. return avc_debug & AVC_DEBUG_READ_DESCRIPTOR ?
  146. "ReadDescriptor" : NULL;
  147. case AVC_OPCODE_DSIT:
  148. return avc_debug & AVC_DEBUG_DSIT ?
  149. "DirectSelectInfo.Type" : NULL;
  150. case AVC_OPCODE_DSD:
  151. return avc_debug & AVC_DEBUG_DSD ? "DirectSelectData" : NULL;
  152. default:
  153. return "Unknown";
  154. }
  155. if (length < 7 ||
  156. data[3] != SFE_VENDOR_DE_COMPANYID_0 ||
  157. data[4] != SFE_VENDOR_DE_COMPANYID_1 ||
  158. data[5] != SFE_VENDOR_DE_COMPANYID_2)
  159. return "Vendor/Unknown";
  160. switch (data[6]) {
  161. case SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL:
  162. return avc_debug & AVC_DEBUG_REGISTER_REMOTE_CONTROL ?
  163. "RegisterRC" : NULL;
  164. case SFE_VENDOR_OPCODE_LNB_CONTROL:
  165. return avc_debug & AVC_DEBUG_LNB_CONTROL ? "LNBControl" : NULL;
  166. case SFE_VENDOR_OPCODE_TUNE_QPSK:
  167. return avc_debug & AVC_DEBUG_TUNE_QPSK ? "TuneQPSK" : NULL;
  168. case SFE_VENDOR_OPCODE_TUNE_QPSK2:
  169. return avc_debug & AVC_DEBUG_TUNE_QPSK2 ? "TuneQPSK2" : NULL;
  170. case SFE_VENDOR_OPCODE_HOST2CA:
  171. return avc_debug & AVC_DEBUG_HOST2CA ? "Host2CA" : NULL;
  172. case SFE_VENDOR_OPCODE_CA2HOST:
  173. return avc_debug & AVC_DEBUG_CA2HOST ? "CA2Host" : NULL;
  174. }
  175. return "Vendor/Unknown";
  176. }
  177. static void debug_fcp(const u8 *data, int length)
  178. {
  179. unsigned int subunit_type, subunit_id, opcode;
  180. const char *op, *prefix;
  181. prefix = data[0] > 7 ? "FCP <- " : "FCP -> ";
  182. subunit_type = data[1] >> 3;
  183. subunit_id = data[1] & 7;
  184. opcode = subunit_type == 0x1e || subunit_id == 5 ? ~0 : data[2];
  185. op = debug_fcp_opcode(opcode, data, length);
  186. if (op) {
  187. printk(KERN_INFO "%ssu=%x.%x l=%d: %-8s - %s\n",
  188. prefix, subunit_type, subunit_id, length,
  189. debug_fcp_ctype(data[0]), op);
  190. if (avc_debug & AVC_DEBUG_FCP_PAYLOADS)
  191. print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_NONE,
  192. 16, 1, data, length, false);
  193. }
  194. }
  195. static void debug_pmt(char *msg, int length)
  196. {
  197. printk(KERN_INFO "APP PMT -> l=%d\n", length);
  198. print_hex_dump(KERN_INFO, "APP PMT -> ", DUMP_PREFIX_NONE,
  199. 16, 1, msg, length, false);
  200. }
  201. static int avc_write(struct firedtv *fdtv)
  202. {
  203. int err, retry;
  204. fdtv->avc_reply_received = false;
  205. for (retry = 0; retry < 6; retry++) {
  206. if (unlikely(avc_debug))
  207. debug_fcp(fdtv->avc_data, fdtv->avc_data_length);
  208. err = fdtv_write(fdtv, FCP_COMMAND_REGISTER,
  209. fdtv->avc_data, fdtv->avc_data_length);
  210. if (err) {
  211. dev_err(fdtv->device, "FCP command write failed\n");
  212. return err;
  213. }
  214. /*
  215. * AV/C specs say that answers should be sent within 150 ms.
  216. * Time out after 200 ms.
  217. */
  218. if (wait_event_timeout(fdtv->avc_wait,
  219. fdtv->avc_reply_received,
  220. msecs_to_jiffies(200)) != 0)
  221. return 0;
  222. }
  223. dev_err(fdtv->device, "FCP response timed out\n");
  224. return -ETIMEDOUT;
  225. }
  226. static bool is_register_rc(struct avc_response_frame *r)
  227. {
  228. return r->opcode == AVC_OPCODE_VENDOR &&
  229. r->operand[0] == SFE_VENDOR_DE_COMPANYID_0 &&
  230. r->operand[1] == SFE_VENDOR_DE_COMPANYID_1 &&
  231. r->operand[2] == SFE_VENDOR_DE_COMPANYID_2 &&
  232. r->operand[3] == SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
  233. }
  234. int avc_recv(struct firedtv *fdtv, void *data, size_t length)
  235. {
  236. struct avc_response_frame *r = data;
  237. if (unlikely(avc_debug))
  238. debug_fcp(data, length);
  239. if (length >= 8 && is_register_rc(r)) {
  240. switch (r->response) {
  241. case AVC_RESPONSE_CHANGED:
  242. fdtv_handle_rc(fdtv, r->operand[4] << 8 | r->operand[5]);
  243. schedule_work(&fdtv->remote_ctrl_work);
  244. break;
  245. case AVC_RESPONSE_INTERIM:
  246. if (is_register_rc((void *)fdtv->avc_data))
  247. goto wake;
  248. break;
  249. default:
  250. dev_info(fdtv->device,
  251. "remote control result = %d\n", r->response);
  252. }
  253. return 0;
  254. }
  255. if (fdtv->avc_reply_received) {
  256. dev_err(fdtv->device, "out-of-order AVC response, ignored\n");
  257. return -EIO;
  258. }
  259. memcpy(fdtv->avc_data, data, length);
  260. fdtv->avc_data_length = length;
  261. wake:
  262. fdtv->avc_reply_received = true;
  263. wake_up(&fdtv->avc_wait);
  264. return 0;
  265. }
  266. static int add_pid_filter(struct firedtv *fdtv, u8 *operand)
  267. {
  268. int i, n, pos = 1;
  269. for (i = 0, n = 0; i < 16; i++) {
  270. if (test_bit(i, &fdtv->channel_active)) {
  271. operand[pos++] = 0x13; /* flowfunction relay */
  272. operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
  273. operand[pos++] = (fdtv->channel_pid[i] >> 8) & 0x1f;
  274. operand[pos++] = fdtv->channel_pid[i] & 0xff;
  275. operand[pos++] = 0x00; /* tableID */
  276. operand[pos++] = 0x00; /* filter_length */
  277. n++;
  278. }
  279. }
  280. operand[0] = n;
  281. return pos;
  282. }
  283. /*
  284. * tuning command for setting the relative LNB frequency
  285. * (not supported by the AVC standard)
  286. */
  287. static int avc_tuner_tuneqpsk(struct firedtv *fdtv,
  288. struct dtv_frontend_properties *p)
  289. {
  290. struct avc_command_frame *c = (void *)fdtv->avc_data;
  291. c->opcode = AVC_OPCODE_VENDOR;
  292. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  293. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  294. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  295. if (fdtv->type == FIREDTV_DVB_S2)
  296. c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK2;
  297. else
  298. c->operand[3] = SFE_VENDOR_OPCODE_TUNE_QPSK;
  299. c->operand[4] = (p->frequency >> 24) & 0xff;
  300. c->operand[5] = (p->frequency >> 16) & 0xff;
  301. c->operand[6] = (p->frequency >> 8) & 0xff;
  302. c->operand[7] = p->frequency & 0xff;
  303. c->operand[8] = ((p->symbol_rate / 1000) >> 8) & 0xff;
  304. c->operand[9] = (p->symbol_rate / 1000) & 0xff;
  305. switch (p->fec_inner) {
  306. case FEC_1_2: c->operand[10] = 0x1; break;
  307. case FEC_2_3: c->operand[10] = 0x2; break;
  308. case FEC_3_4: c->operand[10] = 0x3; break;
  309. case FEC_5_6: c->operand[10] = 0x4; break;
  310. case FEC_7_8: c->operand[10] = 0x5; break;
  311. case FEC_4_5:
  312. case FEC_8_9:
  313. case FEC_AUTO:
  314. default: c->operand[10] = 0x0;
  315. }
  316. if (fdtv->voltage == 0xff)
  317. c->operand[11] = 0xff;
  318. else if (fdtv->voltage == SEC_VOLTAGE_18) /* polarisation */
  319. c->operand[11] = 0;
  320. else
  321. c->operand[11] = 1;
  322. if (fdtv->tone == 0xff)
  323. c->operand[12] = 0xff;
  324. else if (fdtv->tone == SEC_TONE_ON) /* band */
  325. c->operand[12] = 1;
  326. else
  327. c->operand[12] = 0;
  328. if (fdtv->type == FIREDTV_DVB_S2) {
  329. if (fdtv->fe.dtv_property_cache.delivery_system == SYS_DVBS2) {
  330. switch (fdtv->fe.dtv_property_cache.modulation) {
  331. case QAM_16: c->operand[13] = 0x1; break;
  332. case QPSK: c->operand[13] = 0x2; break;
  333. case PSK_8: c->operand[13] = 0x3; break;
  334. default: c->operand[13] = 0x2; break;
  335. }
  336. switch (fdtv->fe.dtv_property_cache.rolloff) {
  337. case ROLLOFF_35: c->operand[14] = 0x2; break;
  338. case ROLLOFF_20: c->operand[14] = 0x0; break;
  339. case ROLLOFF_25: c->operand[14] = 0x1; break;
  340. case ROLLOFF_AUTO:
  341. default: c->operand[14] = 0x2; break;
  342. /* case ROLLOFF_NONE: c->operand[14] = 0xff; break; */
  343. }
  344. switch (fdtv->fe.dtv_property_cache.pilot) {
  345. case PILOT_AUTO: c->operand[15] = 0x0; break;
  346. case PILOT_OFF: c->operand[15] = 0x0; break;
  347. case PILOT_ON: c->operand[15] = 0x1; break;
  348. }
  349. } else {
  350. c->operand[13] = 0x1; /* auto modulation */
  351. c->operand[14] = 0xff; /* disable rolloff */
  352. c->operand[15] = 0xff; /* disable pilot */
  353. }
  354. return 16;
  355. } else {
  356. return 13;
  357. }
  358. }
  359. static int avc_tuner_dsd_dvb_c(struct firedtv *fdtv,
  360. struct dtv_frontend_properties *p)
  361. {
  362. struct avc_command_frame *c = (void *)fdtv->avc_data;
  363. c->opcode = AVC_OPCODE_DSD;
  364. c->operand[0] = 0; /* source plug */
  365. c->operand[1] = 0xd2; /* subfunction replace */
  366. c->operand[2] = 0x20; /* system id = DVB */
  367. c->operand[3] = 0x00; /* antenna number */
  368. c->operand[4] = 0x11; /* system_specific_multiplex selection_length */
  369. /* multiplex_valid_flags, high byte */
  370. c->operand[5] = 0 << 7 /* reserved */
  371. | 0 << 6 /* Polarisation */
  372. | 0 << 5 /* Orbital_Pos */
  373. | 1 << 4 /* Frequency */
  374. | 1 << 3 /* Symbol_Rate */
  375. | 0 << 2 /* FEC_outer */
  376. | (p->fec_inner != FEC_AUTO ? 1 << 1 : 0)
  377. | (p->modulation != QAM_AUTO ? 1 << 0 : 0);
  378. /* multiplex_valid_flags, low byte */
  379. c->operand[6] = 0 << 7 /* NetworkID */
  380. | 0 << 0 /* reserved */ ;
  381. c->operand[7] = 0x00;
  382. c->operand[8] = 0x00;
  383. c->operand[9] = 0x00;
  384. c->operand[10] = 0x00;
  385. c->operand[11] = (((p->frequency / 4000) >> 16) & 0xff) | (2 << 6);
  386. c->operand[12] = ((p->frequency / 4000) >> 8) & 0xff;
  387. c->operand[13] = (p->frequency / 4000) & 0xff;
  388. c->operand[14] = ((p->symbol_rate / 1000) >> 12) & 0xff;
  389. c->operand[15] = ((p->symbol_rate / 1000) >> 4) & 0xff;
  390. c->operand[16] = ((p->symbol_rate / 1000) << 4) & 0xf0;
  391. c->operand[17] = 0x00;
  392. switch (p->fec_inner) {
  393. case FEC_1_2: c->operand[18] = 0x1; break;
  394. case FEC_2_3: c->operand[18] = 0x2; break;
  395. case FEC_3_4: c->operand[18] = 0x3; break;
  396. case FEC_5_6: c->operand[18] = 0x4; break;
  397. case FEC_7_8: c->operand[18] = 0x5; break;
  398. case FEC_8_9: c->operand[18] = 0x6; break;
  399. case FEC_4_5: c->operand[18] = 0x8; break;
  400. case FEC_AUTO:
  401. default: c->operand[18] = 0x0;
  402. }
  403. switch (p->modulation) {
  404. case QAM_16: c->operand[19] = 0x08; break;
  405. case QAM_32: c->operand[19] = 0x10; break;
  406. case QAM_64: c->operand[19] = 0x18; break;
  407. case QAM_128: c->operand[19] = 0x20; break;
  408. case QAM_256: c->operand[19] = 0x28; break;
  409. case QAM_AUTO:
  410. default: c->operand[19] = 0x00;
  411. }
  412. c->operand[20] = 0x00;
  413. c->operand[21] = 0x00;
  414. return 22 + add_pid_filter(fdtv, &c->operand[22]);
  415. }
  416. static int avc_tuner_dsd_dvb_t(struct firedtv *fdtv,
  417. struct dtv_frontend_properties *p)
  418. {
  419. struct avc_command_frame *c = (void *)fdtv->avc_data;
  420. c->opcode = AVC_OPCODE_DSD;
  421. c->operand[0] = 0; /* source plug */
  422. c->operand[1] = 0xd2; /* subfunction replace */
  423. c->operand[2] = 0x20; /* system id = DVB */
  424. c->operand[3] = 0x00; /* antenna number */
  425. c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
  426. /* multiplex_valid_flags, high byte */
  427. c->operand[5] =
  428. 0 << 7 /* reserved */
  429. | 1 << 6 /* CenterFrequency */
  430. | (p->bandwidth_hz != 0 ? 1 << 5 : 0)
  431. | (p->modulation != QAM_AUTO ? 1 << 4 : 0)
  432. | (p->hierarchy != HIERARCHY_AUTO ? 1 << 3 : 0)
  433. | (p->code_rate_HP != FEC_AUTO ? 1 << 2 : 0)
  434. | (p->code_rate_LP != FEC_AUTO ? 1 << 1 : 0)
  435. | (p->guard_interval != GUARD_INTERVAL_AUTO ? 1 << 0 : 0);
  436. /* multiplex_valid_flags, low byte */
  437. c->operand[6] =
  438. 0 << 7 /* NetworkID */
  439. | (p->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
  440. | 0 << 5 /* OtherFrequencyFlag */
  441. | 0 << 0 /* reserved */ ;
  442. c->operand[7] = 0x0;
  443. c->operand[8] = (p->frequency / 10) >> 24;
  444. c->operand[9] = ((p->frequency / 10) >> 16) & 0xff;
  445. c->operand[10] = ((p->frequency / 10) >> 8) & 0xff;
  446. c->operand[11] = (p->frequency / 10) & 0xff;
  447. switch (p->bandwidth_hz) {
  448. case 7000000: c->operand[12] = 0x20; break;
  449. case 8000000:
  450. case 6000000: /* not defined by AVC spec */
  451. case 0:
  452. default: c->operand[12] = 0x00;
  453. }
  454. switch (p->modulation) {
  455. case QAM_16: c->operand[13] = 1 << 6; break;
  456. case QAM_64: c->operand[13] = 2 << 6; break;
  457. case QPSK:
  458. default: c->operand[13] = 0x00;
  459. }
  460. switch (p->hierarchy) {
  461. case HIERARCHY_1: c->operand[13] |= 1 << 3; break;
  462. case HIERARCHY_2: c->operand[13] |= 2 << 3; break;
  463. case HIERARCHY_4: c->operand[13] |= 3 << 3; break;
  464. case HIERARCHY_AUTO:
  465. case HIERARCHY_NONE:
  466. default: break;
  467. }
  468. switch (p->code_rate_HP) {
  469. case FEC_2_3: c->operand[13] |= 1; break;
  470. case FEC_3_4: c->operand[13] |= 2; break;
  471. case FEC_5_6: c->operand[13] |= 3; break;
  472. case FEC_7_8: c->operand[13] |= 4; break;
  473. case FEC_1_2:
  474. default: break;
  475. }
  476. switch (p->code_rate_LP) {
  477. case FEC_2_3: c->operand[14] = 1 << 5; break;
  478. case FEC_3_4: c->operand[14] = 2 << 5; break;
  479. case FEC_5_6: c->operand[14] = 3 << 5; break;
  480. case FEC_7_8: c->operand[14] = 4 << 5; break;
  481. case FEC_1_2:
  482. default: c->operand[14] = 0x00; break;
  483. }
  484. switch (p->guard_interval) {
  485. case GUARD_INTERVAL_1_16: c->operand[14] |= 1 << 3; break;
  486. case GUARD_INTERVAL_1_8: c->operand[14] |= 2 << 3; break;
  487. case GUARD_INTERVAL_1_4: c->operand[14] |= 3 << 3; break;
  488. case GUARD_INTERVAL_1_32:
  489. case GUARD_INTERVAL_AUTO:
  490. default: break;
  491. }
  492. switch (p->transmission_mode) {
  493. case TRANSMISSION_MODE_8K: c->operand[14] |= 1 << 1; break;
  494. case TRANSMISSION_MODE_2K:
  495. case TRANSMISSION_MODE_AUTO:
  496. default: break;
  497. }
  498. c->operand[15] = 0x00; /* network_ID[0] */
  499. c->operand[16] = 0x00; /* network_ID[1] */
  500. return 17 + add_pid_filter(fdtv, &c->operand[17]);
  501. }
  502. int avc_tuner_dsd(struct firedtv *fdtv,
  503. struct dtv_frontend_properties *p)
  504. {
  505. struct avc_command_frame *c = (void *)fdtv->avc_data;
  506. int pos, ret;
  507. mutex_lock(&fdtv->avc_mutex);
  508. c->ctype = AVC_CTYPE_CONTROL;
  509. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  510. switch (fdtv->type) {
  511. case FIREDTV_DVB_S:
  512. case FIREDTV_DVB_S2: pos = avc_tuner_tuneqpsk(fdtv, p); break;
  513. case FIREDTV_DVB_C: pos = avc_tuner_dsd_dvb_c(fdtv, p); break;
  514. case FIREDTV_DVB_T: pos = avc_tuner_dsd_dvb_t(fdtv, p); break;
  515. default:
  516. BUG();
  517. }
  518. pad_operands(c, pos);
  519. fdtv->avc_data_length = ALIGN(3 + pos, 4);
  520. ret = avc_write(fdtv);
  521. #if 0
  522. /*
  523. * FIXME:
  524. * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
  525. * Check for AVC_RESPONSE_ACCEPTED here instead?
  526. */
  527. if (status)
  528. *status = r->operand[2];
  529. #endif
  530. mutex_unlock(&fdtv->avc_mutex);
  531. if (ret == 0)
  532. msleep(500);
  533. return ret;
  534. }
  535. int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
  536. {
  537. struct avc_command_frame *c = (void *)fdtv->avc_data;
  538. int ret, pos, k;
  539. if (pidc > 16 && pidc != 0xff)
  540. return -EINVAL;
  541. mutex_lock(&fdtv->avc_mutex);
  542. c->ctype = AVC_CTYPE_CONTROL;
  543. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  544. c->opcode = AVC_OPCODE_DSD;
  545. c->operand[0] = 0; /* source plug */
  546. c->operand[1] = 0xd2; /* subfunction replace */
  547. c->operand[2] = 0x20; /* system id = DVB */
  548. c->operand[3] = 0x00; /* antenna number */
  549. c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
  550. c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
  551. pos = 6;
  552. if (pidc != 0xff)
  553. for (k = 0; k < pidc; k++) {
  554. c->operand[pos++] = 0x13; /* flowfunction relay */
  555. c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
  556. c->operand[pos++] = (pid[k] >> 8) & 0x1f;
  557. c->operand[pos++] = pid[k] & 0xff;
  558. c->operand[pos++] = 0x00; /* tableID */
  559. c->operand[pos++] = 0x00; /* filter_length */
  560. }
  561. pad_operands(c, pos);
  562. fdtv->avc_data_length = ALIGN(3 + pos, 4);
  563. ret = avc_write(fdtv);
  564. /* FIXME: check response code? */
  565. mutex_unlock(&fdtv->avc_mutex);
  566. if (ret == 0)
  567. msleep(50);
  568. return ret;
  569. }
  570. int avc_tuner_get_ts(struct firedtv *fdtv)
  571. {
  572. struct avc_command_frame *c = (void *)fdtv->avc_data;
  573. int ret, sl;
  574. mutex_lock(&fdtv->avc_mutex);
  575. c->ctype = AVC_CTYPE_CONTROL;
  576. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  577. c->opcode = AVC_OPCODE_DSIT;
  578. sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
  579. c->operand[0] = 0; /* source plug */
  580. c->operand[1] = 0xd2; /* subfunction replace */
  581. c->operand[2] = 0xff; /* status */
  582. c->operand[3] = 0x20; /* system id = DVB */
  583. c->operand[4] = 0x00; /* antenna number */
  584. c->operand[5] = 0x0; /* system_specific_search_flags */
  585. c->operand[6] = sl; /* system_specific_multiplex selection_length */
  586. /*
  587. * operand[7]: valid_flags[0]
  588. * operand[8]: valid_flags[1]
  589. * operand[7 + sl]: nr_of_dsit_sel_specs (always 0)
  590. */
  591. clear_operands(c, 7, 24);
  592. fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
  593. ret = avc_write(fdtv);
  594. /* FIXME: check response code? */
  595. mutex_unlock(&fdtv->avc_mutex);
  596. if (ret == 0)
  597. msleep(250);
  598. return ret;
  599. }
  600. int avc_identify_subunit(struct firedtv *fdtv)
  601. {
  602. struct avc_command_frame *c = (void *)fdtv->avc_data;
  603. struct avc_response_frame *r = (void *)fdtv->avc_data;
  604. int ret;
  605. mutex_lock(&fdtv->avc_mutex);
  606. c->ctype = AVC_CTYPE_CONTROL;
  607. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  608. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  609. c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
  610. c->operand[1] = 0xff;
  611. c->operand[2] = 0x00;
  612. c->operand[3] = 0x00; /* length highbyte */
  613. c->operand[4] = 0x08; /* length lowbyte */
  614. c->operand[5] = 0x00; /* offset highbyte */
  615. c->operand[6] = 0x0d; /* offset lowbyte */
  616. clear_operands(c, 7, 8); /* padding */
  617. fdtv->avc_data_length = 12;
  618. ret = avc_write(fdtv);
  619. if (ret < 0)
  620. goto out;
  621. if ((r->response != AVC_RESPONSE_STABLE &&
  622. r->response != AVC_RESPONSE_ACCEPTED) ||
  623. (r->operand[3] << 8) + r->operand[4] != 8) {
  624. dev_err(fdtv->device, "cannot read subunit identifier\n");
  625. ret = -EINVAL;
  626. }
  627. out:
  628. mutex_unlock(&fdtv->avc_mutex);
  629. return ret;
  630. }
  631. #define SIZEOF_ANTENNA_INPUT_INFO 22
  632. int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
  633. {
  634. struct avc_command_frame *c = (void *)fdtv->avc_data;
  635. struct avc_response_frame *r = (void *)fdtv->avc_data;
  636. int length, ret;
  637. mutex_lock(&fdtv->avc_mutex);
  638. c->ctype = AVC_CTYPE_CONTROL;
  639. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  640. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  641. c->operand[0] = DESCRIPTOR_TUNER_STATUS;
  642. c->operand[1] = 0xff; /* read_result_status */
  643. /*
  644. * operand[2]: reserved
  645. * operand[3]: SIZEOF_ANTENNA_INPUT_INFO >> 8
  646. * operand[4]: SIZEOF_ANTENNA_INPUT_INFO & 0xff
  647. */
  648. clear_operands(c, 2, 31);
  649. fdtv->avc_data_length = 12;
  650. ret = avc_write(fdtv);
  651. if (ret < 0)
  652. goto out;
  653. if (r->response != AVC_RESPONSE_STABLE &&
  654. r->response != AVC_RESPONSE_ACCEPTED) {
  655. dev_err(fdtv->device, "cannot read tuner status\n");
  656. ret = -EINVAL;
  657. goto out;
  658. }
  659. length = r->operand[9];
  660. if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
  661. dev_err(fdtv->device, "got invalid tuner status\n");
  662. ret = -EINVAL;
  663. goto out;
  664. }
  665. stat->active_system = r->operand[10];
  666. stat->searching = r->operand[11] >> 7 & 1;
  667. stat->moving = r->operand[11] >> 6 & 1;
  668. stat->no_rf = r->operand[11] >> 5 & 1;
  669. stat->input = r->operand[12] >> 7 & 1;
  670. stat->selected_antenna = r->operand[12] & 0x7f;
  671. stat->ber = r->operand[13] << 24 |
  672. r->operand[14] << 16 |
  673. r->operand[15] << 8 |
  674. r->operand[16];
  675. stat->signal_strength = r->operand[17];
  676. stat->raster_frequency = r->operand[18] >> 6 & 2;
  677. stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
  678. r->operand[19] << 8 |
  679. r->operand[20];
  680. stat->man_dep_info_length = r->operand[21];
  681. stat->front_end_error = r->operand[22] >> 4 & 1;
  682. stat->antenna_error = r->operand[22] >> 3 & 1;
  683. stat->front_end_power_status = r->operand[22] >> 1 & 1;
  684. stat->power_supply = r->operand[22] & 1;
  685. stat->carrier_noise_ratio = r->operand[23] << 8 |
  686. r->operand[24];
  687. stat->power_supply_voltage = r->operand[27];
  688. stat->antenna_voltage = r->operand[28];
  689. stat->firewire_bus_voltage = r->operand[29];
  690. stat->ca_mmi = r->operand[30] & 1;
  691. stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
  692. stat->ca_date_time_request = r->operand[31] >> 6 & 1;
  693. stat->ca_application_info = r->operand[31] >> 5 & 1;
  694. stat->ca_module_present_status = r->operand[31] >> 4 & 1;
  695. stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
  696. stat->ca_error_flag = r->operand[31] >> 2 & 1;
  697. stat->ca_initialization_status = r->operand[31] >> 1 & 1;
  698. out:
  699. mutex_unlock(&fdtv->avc_mutex);
  700. return ret;
  701. }
  702. int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
  703. char conttone, char nrdiseq,
  704. struct dvb_diseqc_master_cmd *diseqcmd)
  705. {
  706. struct avc_command_frame *c = (void *)fdtv->avc_data;
  707. struct avc_response_frame *r = (void *)fdtv->avc_data;
  708. int pos, j, k, ret;
  709. mutex_lock(&fdtv->avc_mutex);
  710. c->ctype = AVC_CTYPE_CONTROL;
  711. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  712. c->opcode = AVC_OPCODE_VENDOR;
  713. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  714. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  715. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  716. c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
  717. c->operand[4] = voltage;
  718. c->operand[5] = nrdiseq;
  719. pos = 6;
  720. for (j = 0; j < nrdiseq; j++) {
  721. c->operand[pos++] = diseqcmd[j].msg_len;
  722. for (k = 0; k < diseqcmd[j].msg_len; k++)
  723. c->operand[pos++] = diseqcmd[j].msg[k];
  724. }
  725. c->operand[pos++] = burst;
  726. c->operand[pos++] = conttone;
  727. pad_operands(c, pos);
  728. fdtv->avc_data_length = ALIGN(3 + pos, 4);
  729. ret = avc_write(fdtv);
  730. if (ret < 0)
  731. goto out;
  732. if (r->response != AVC_RESPONSE_ACCEPTED) {
  733. dev_err(fdtv->device, "LNB control failed\n");
  734. ret = -EINVAL;
  735. }
  736. out:
  737. mutex_unlock(&fdtv->avc_mutex);
  738. return ret;
  739. }
  740. int avc_register_remote_control(struct firedtv *fdtv)
  741. {
  742. struct avc_command_frame *c = (void *)fdtv->avc_data;
  743. int ret;
  744. mutex_lock(&fdtv->avc_mutex);
  745. c->ctype = AVC_CTYPE_NOTIFY;
  746. c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
  747. c->opcode = AVC_OPCODE_VENDOR;
  748. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  749. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  750. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  751. c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
  752. c->operand[4] = 0; /* padding */
  753. fdtv->avc_data_length = 8;
  754. ret = avc_write(fdtv);
  755. /* FIXME: check response code? */
  756. mutex_unlock(&fdtv->avc_mutex);
  757. return ret;
  758. }
  759. void avc_remote_ctrl_work(struct work_struct *work)
  760. {
  761. struct firedtv *fdtv =
  762. container_of(work, struct firedtv, remote_ctrl_work);
  763. /* Should it be rescheduled in failure cases? */
  764. avc_register_remote_control(fdtv);
  765. }
  766. #if 0 /* FIXME: unused */
  767. int avc_tuner_host2ca(struct firedtv *fdtv)
  768. {
  769. struct avc_command_frame *c = (void *)fdtv->avc_data;
  770. int ret;
  771. mutex_lock(&fdtv->avc_mutex);
  772. c->ctype = AVC_CTYPE_CONTROL;
  773. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  774. c->opcode = AVC_OPCODE_VENDOR;
  775. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  776. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  777. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  778. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  779. c->operand[4] = 0; /* slot */
  780. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  781. clear_operands(c, 6, 8);
  782. fdtv->avc_data_length = 12;
  783. ret = avc_write(fdtv);
  784. /* FIXME: check response code? */
  785. mutex_unlock(&fdtv->avc_mutex);
  786. return ret;
  787. }
  788. #endif
  789. static int get_ca_object_pos(struct avc_response_frame *r)
  790. {
  791. int length = 1;
  792. /* Check length of length field */
  793. if (r->operand[7] & 0x80)
  794. length = (r->operand[7] & 0x7f) + 1;
  795. return length + 7;
  796. }
  797. static int get_ca_object_length(struct avc_response_frame *r)
  798. {
  799. #if 0 /* FIXME: unused */
  800. int size = 0;
  801. int i;
  802. if (r->operand[7] & 0x80)
  803. for (i = 0; i < (r->operand[7] & 0x7f); i++) {
  804. size <<= 8;
  805. size += r->operand[8 + i];
  806. }
  807. #endif
  808. return r->operand[7];
  809. }
  810. int avc_ca_app_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
  811. {
  812. struct avc_command_frame *c = (void *)fdtv->avc_data;
  813. struct avc_response_frame *r = (void *)fdtv->avc_data;
  814. int pos, ret;
  815. mutex_lock(&fdtv->avc_mutex);
  816. c->ctype = AVC_CTYPE_STATUS;
  817. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  818. c->opcode = AVC_OPCODE_VENDOR;
  819. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  820. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  821. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  822. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  823. c->operand[4] = 0; /* slot */
  824. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  825. clear_operands(c, 6, LAST_OPERAND);
  826. fdtv->avc_data_length = 12;
  827. ret = avc_write(fdtv);
  828. if (ret < 0)
  829. goto out;
  830. /* FIXME: check response code and validate response data */
  831. pos = get_ca_object_pos(r);
  832. app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
  833. app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
  834. app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
  835. app_info[3] = 6 + r->operand[pos + 4];
  836. app_info[4] = 0x01;
  837. memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
  838. *len = app_info[3] + 4;
  839. out:
  840. mutex_unlock(&fdtv->avc_mutex);
  841. return ret;
  842. }
  843. int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
  844. {
  845. struct avc_command_frame *c = (void *)fdtv->avc_data;
  846. struct avc_response_frame *r = (void *)fdtv->avc_data;
  847. int i, pos, ret;
  848. mutex_lock(&fdtv->avc_mutex);
  849. c->ctype = AVC_CTYPE_STATUS;
  850. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  851. c->opcode = AVC_OPCODE_VENDOR;
  852. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  853. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  854. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  855. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  856. c->operand[4] = 0; /* slot */
  857. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  858. clear_operands(c, 6, LAST_OPERAND);
  859. fdtv->avc_data_length = 12;
  860. ret = avc_write(fdtv);
  861. if (ret < 0)
  862. goto out;
  863. /* FIXME: check response code and validate response data */
  864. pos = get_ca_object_pos(r);
  865. app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
  866. app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
  867. app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
  868. if (num_fake_ca_system_ids == 0) {
  869. app_info[3] = 2;
  870. app_info[4] = r->operand[pos + 0];
  871. app_info[5] = r->operand[pos + 1];
  872. } else {
  873. app_info[3] = num_fake_ca_system_ids * 2;
  874. for (i = 0; i < num_fake_ca_system_ids; i++) {
  875. app_info[4 + i * 2] =
  876. (fake_ca_system_ids[i] >> 8) & 0xff;
  877. app_info[5 + i * 2] = fake_ca_system_ids[i] & 0xff;
  878. }
  879. }
  880. *len = app_info[3] + 4;
  881. out:
  882. mutex_unlock(&fdtv->avc_mutex);
  883. return ret;
  884. }
  885. int avc_ca_reset(struct firedtv *fdtv)
  886. {
  887. struct avc_command_frame *c = (void *)fdtv->avc_data;
  888. int ret;
  889. mutex_lock(&fdtv->avc_mutex);
  890. c->ctype = AVC_CTYPE_CONTROL;
  891. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  892. c->opcode = AVC_OPCODE_VENDOR;
  893. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  894. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  895. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  896. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  897. c->operand[4] = 0; /* slot */
  898. c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
  899. c->operand[6] = 0; /* more/last */
  900. c->operand[7] = 1; /* length */
  901. c->operand[8] = 0; /* force hardware reset */
  902. fdtv->avc_data_length = 12;
  903. ret = avc_write(fdtv);
  904. /* FIXME: check response code? */
  905. mutex_unlock(&fdtv->avc_mutex);
  906. return ret;
  907. }
  908. int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
  909. {
  910. struct avc_command_frame *c = (void *)fdtv->avc_data;
  911. struct avc_response_frame *r = (void *)fdtv->avc_data;
  912. int list_management;
  913. int program_info_length;
  914. int pmt_cmd_id;
  915. int read_pos;
  916. int write_pos;
  917. int es_info_length;
  918. int crc32_csum;
  919. int ret;
  920. if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
  921. debug_pmt(msg, length);
  922. mutex_lock(&fdtv->avc_mutex);
  923. c->ctype = AVC_CTYPE_CONTROL;
  924. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  925. c->opcode = AVC_OPCODE_VENDOR;
  926. if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
  927. dev_info(fdtv->device, "forcing list_management to ONLY\n");
  928. msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
  929. }
  930. /* We take the cmd_id from the programme level only! */
  931. list_management = msg[0];
  932. program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
  933. if (program_info_length > 0)
  934. program_info_length--; /* Remove pmt_cmd_id */
  935. pmt_cmd_id = msg[6];
  936. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  937. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  938. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  939. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  940. c->operand[4] = 0; /* slot */
  941. c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
  942. c->operand[6] = 0; /* more/last */
  943. /* Use three bytes for length field in case length > 127 */
  944. c->operand[10] = list_management;
  945. c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
  946. /* TS program map table */
  947. c->operand[12] = 0x02; /* Table id=2 */
  948. c->operand[13] = 0x80; /* Section syntax + length */
  949. c->operand[15] = msg[1]; /* Program number */
  950. c->operand[16] = msg[2];
  951. c->operand[17] = msg[3]; /* Version number and current/next */
  952. c->operand[18] = 0x00; /* Section number=0 */
  953. c->operand[19] = 0x00; /* Last section number=0 */
  954. c->operand[20] = 0x1f; /* PCR_PID=1FFF */
  955. c->operand[21] = 0xff;
  956. c->operand[22] = (program_info_length >> 8); /* Program info length */
  957. c->operand[23] = (program_info_length & 0xff);
  958. /* CA descriptors at programme level */
  959. read_pos = 6;
  960. write_pos = 24;
  961. if (program_info_length > 0) {
  962. pmt_cmd_id = msg[read_pos++];
  963. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  964. dev_err(fdtv->device,
  965. "invalid pmt_cmd_id %d\n", pmt_cmd_id);
  966. if (program_info_length > sizeof(c->operand) - 4 - write_pos) {
  967. ret = -EINVAL;
  968. goto out;
  969. }
  970. memcpy(&c->operand[write_pos], &msg[read_pos],
  971. program_info_length);
  972. read_pos += program_info_length;
  973. write_pos += program_info_length;
  974. }
  975. while (read_pos < length) {
  976. c->operand[write_pos++] = msg[read_pos++];
  977. c->operand[write_pos++] = msg[read_pos++];
  978. c->operand[write_pos++] = msg[read_pos++];
  979. es_info_length =
  980. ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
  981. read_pos += 2;
  982. if (es_info_length > 0)
  983. es_info_length--; /* Remove pmt_cmd_id */
  984. c->operand[write_pos++] = es_info_length >> 8;
  985. c->operand[write_pos++] = es_info_length & 0xff;
  986. if (es_info_length > 0) {
  987. pmt_cmd_id = msg[read_pos++];
  988. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  989. dev_err(fdtv->device, "invalid pmt_cmd_id %d "
  990. "at stream level\n", pmt_cmd_id);
  991. if (es_info_length > sizeof(c->operand) - 4 -
  992. write_pos) {
  993. ret = -EINVAL;
  994. goto out;
  995. }
  996. memcpy(&c->operand[write_pos], &msg[read_pos],
  997. es_info_length);
  998. read_pos += es_info_length;
  999. write_pos += es_info_length;
  1000. }
  1001. }
  1002. write_pos += 4; /* CRC */
  1003. c->operand[7] = 0x82;
  1004. c->operand[8] = (write_pos - 10) >> 8;
  1005. c->operand[9] = (write_pos - 10) & 0xff;
  1006. c->operand[14] = write_pos - 15;
  1007. crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
  1008. c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
  1009. c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
  1010. c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
  1011. c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
  1012. pad_operands(c, write_pos);
  1013. fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
  1014. ret = avc_write(fdtv);
  1015. if (ret < 0)
  1016. goto out;
  1017. if (r->response != AVC_RESPONSE_ACCEPTED) {
  1018. dev_err(fdtv->device,
  1019. "CA PMT failed with response 0x%x\n", r->response);
  1020. ret = -EACCES;
  1021. }
  1022. out:
  1023. mutex_unlock(&fdtv->avc_mutex);
  1024. return ret;
  1025. }
  1026. int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
  1027. {
  1028. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1029. struct avc_response_frame *r = (void *)fdtv->avc_data;
  1030. int ret;
  1031. mutex_lock(&fdtv->avc_mutex);
  1032. c->ctype = AVC_CTYPE_STATUS;
  1033. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1034. c->opcode = AVC_OPCODE_VENDOR;
  1035. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1036. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1037. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1038. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  1039. c->operand[4] = 0; /* slot */
  1040. c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
  1041. clear_operands(c, 6, LAST_OPERAND);
  1042. fdtv->avc_data_length = 12;
  1043. ret = avc_write(fdtv);
  1044. if (ret < 0)
  1045. goto out;
  1046. /* FIXME: check response code and validate response data */
  1047. *interval = r->operand[get_ca_object_pos(r)];
  1048. out:
  1049. mutex_unlock(&fdtv->avc_mutex);
  1050. return ret;
  1051. }
  1052. int avc_ca_enter_menu(struct firedtv *fdtv)
  1053. {
  1054. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1055. int ret;
  1056. mutex_lock(&fdtv->avc_mutex);
  1057. c->ctype = AVC_CTYPE_STATUS;
  1058. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1059. c->opcode = AVC_OPCODE_VENDOR;
  1060. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1061. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1062. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1063. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  1064. c->operand[4] = 0; /* slot */
  1065. c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
  1066. clear_operands(c, 6, 8);
  1067. fdtv->avc_data_length = 12;
  1068. ret = avc_write(fdtv);
  1069. /* FIXME: check response code? */
  1070. mutex_unlock(&fdtv->avc_mutex);
  1071. return ret;
  1072. }
  1073. int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
  1074. {
  1075. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1076. struct avc_response_frame *r = (void *)fdtv->avc_data;
  1077. int ret;
  1078. mutex_lock(&fdtv->avc_mutex);
  1079. c->ctype = AVC_CTYPE_STATUS;
  1080. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1081. c->opcode = AVC_OPCODE_VENDOR;
  1082. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1083. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1084. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1085. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  1086. c->operand[4] = 0; /* slot */
  1087. c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
  1088. clear_operands(c, 6, LAST_OPERAND);
  1089. fdtv->avc_data_length = 12;
  1090. ret = avc_write(fdtv);
  1091. if (ret < 0)
  1092. goto out;
  1093. /* FIXME: check response code and validate response data */
  1094. *len = get_ca_object_length(r);
  1095. memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
  1096. out:
  1097. mutex_unlock(&fdtv->avc_mutex);
  1098. return ret;
  1099. }
  1100. #define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
  1101. static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
  1102. {
  1103. int ret;
  1104. ret = fdtv_read(fdtv, addr, data);
  1105. if (ret < 0)
  1106. dev_err(fdtv->device, "CMP: read I/O error\n");
  1107. return ret;
  1108. }
  1109. static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
  1110. {
  1111. int ret;
  1112. ret = fdtv_lock(fdtv, addr, data);
  1113. if (ret < 0)
  1114. dev_err(fdtv->device, "CMP: lock I/O error\n");
  1115. return ret;
  1116. }
  1117. static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
  1118. {
  1119. return (be32_to_cpu(opcr) >> shift) & mask;
  1120. }
  1121. static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
  1122. {
  1123. *opcr &= ~cpu_to_be32(mask << shift);
  1124. *opcr |= cpu_to_be32((value & mask) << shift);
  1125. }
  1126. #define get_opcr_online(v) get_opcr((v), 0x1, 31)
  1127. #define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
  1128. #define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
  1129. #define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
  1130. #define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
  1131. #define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
  1132. #define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
  1133. int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1134. {
  1135. __be32 old_opcr, opcr[2];
  1136. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1137. int attempts = 0;
  1138. int ret;
  1139. ret = cmp_read(fdtv, opcr_address, opcr);
  1140. if (ret < 0)
  1141. return ret;
  1142. repeat:
  1143. if (!get_opcr_online(*opcr)) {
  1144. dev_err(fdtv->device, "CMP: output offline\n");
  1145. return -EBUSY;
  1146. }
  1147. old_opcr = *opcr;
  1148. if (get_opcr_p2p_connections(*opcr)) {
  1149. if (get_opcr_channel(*opcr) != channel) {
  1150. dev_err(fdtv->device, "CMP: cannot change channel\n");
  1151. return -EBUSY;
  1152. }
  1153. dev_info(fdtv->device, "CMP: overlaying connection\n");
  1154. /* We don't allocate isochronous resources. */
  1155. } else {
  1156. set_opcr_channel(opcr, channel);
  1157. set_opcr_data_rate(opcr, 2); /* S400 */
  1158. /* FIXME: this is for the worst case - optimize */
  1159. set_opcr_overhead_id(opcr, 0);
  1160. /* FIXME: allocate isochronous channel and bandwidth at IRM */
  1161. }
  1162. set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
  1163. opcr[1] = *opcr;
  1164. opcr[0] = old_opcr;
  1165. ret = cmp_lock(fdtv, opcr_address, opcr);
  1166. if (ret < 0)
  1167. return ret;
  1168. if (old_opcr != *opcr) {
  1169. /*
  1170. * FIXME: if old_opcr.P2P_Connections > 0,
  1171. * deallocate isochronous channel and bandwidth at IRM
  1172. */
  1173. if (++attempts < 6) /* arbitrary limit */
  1174. goto repeat;
  1175. return -EBUSY;
  1176. }
  1177. return 0;
  1178. }
  1179. void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1180. {
  1181. __be32 old_opcr, opcr[2];
  1182. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1183. int attempts = 0;
  1184. if (cmp_read(fdtv, opcr_address, opcr) < 0)
  1185. return;
  1186. repeat:
  1187. if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
  1188. get_opcr_channel(*opcr) != channel) {
  1189. dev_err(fdtv->device, "CMP: no connection to break\n");
  1190. return;
  1191. }
  1192. old_opcr = *opcr;
  1193. set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
  1194. opcr[1] = *opcr;
  1195. opcr[0] = old_opcr;
  1196. if (cmp_lock(fdtv, opcr_address, opcr) < 0)
  1197. return;
  1198. if (old_opcr != *opcr) {
  1199. /*
  1200. * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
  1201. * owner, deallocate isochronous channel and bandwidth at IRM
  1202. * if (...)
  1203. * fdtv->backend->dealloc_resources(fdtv, channel, bw);
  1204. */
  1205. if (++attempts < 6) /* arbitrary limit */
  1206. goto repeat;
  1207. }
  1208. }