firedtv-avc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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 dvb_frontend_parameters *params)
  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] = (params->frequency >> 24) & 0xff;
  300. c->operand[5] = (params->frequency >> 16) & 0xff;
  301. c->operand[6] = (params->frequency >> 8) & 0xff;
  302. c->operand[7] = params->frequency & 0xff;
  303. c->operand[8] = ((params->u.qpsk.symbol_rate / 1000) >> 8) & 0xff;
  304. c->operand[9] = (params->u.qpsk.symbol_rate / 1000) & 0xff;
  305. switch (params->u.qpsk.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 dvb_frontend_parameters *params)
  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. | (params->u.qam.fec_inner != FEC_AUTO ? 1 << 1 : 0)
  377. | (params->u.qam.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] = (((params->frequency / 4000) >> 16) & 0xff) | (2 << 6);
  386. c->operand[12] = ((params->frequency / 4000) >> 8) & 0xff;
  387. c->operand[13] = (params->frequency / 4000) & 0xff;
  388. c->operand[14] = ((params->u.qpsk.symbol_rate / 1000) >> 12) & 0xff;
  389. c->operand[15] = ((params->u.qpsk.symbol_rate / 1000) >> 4) & 0xff;
  390. c->operand[16] = ((params->u.qpsk.symbol_rate / 1000) << 4) & 0xf0;
  391. c->operand[17] = 0x00;
  392. switch (params->u.qpsk.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 (params->u.qam.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 dvb_frontend_parameters *params)
  418. {
  419. struct dvb_ofdm_parameters *ofdm = &params->u.ofdm;
  420. struct avc_command_frame *c = (void *)fdtv->avc_data;
  421. c->opcode = AVC_OPCODE_DSD;
  422. c->operand[0] = 0; /* source plug */
  423. c->operand[1] = 0xd2; /* subfunction replace */
  424. c->operand[2] = 0x20; /* system id = DVB */
  425. c->operand[3] = 0x00; /* antenna number */
  426. c->operand[4] = 0x0c; /* system_specific_multiplex selection_length */
  427. /* multiplex_valid_flags, high byte */
  428. c->operand[5] =
  429. 0 << 7 /* reserved */
  430. | 1 << 6 /* CenterFrequency */
  431. | (ofdm->bandwidth != BANDWIDTH_AUTO ? 1 << 5 : 0)
  432. | (ofdm->constellation != QAM_AUTO ? 1 << 4 : 0)
  433. | (ofdm->hierarchy_information != HIERARCHY_AUTO ? 1 << 3 : 0)
  434. | (ofdm->code_rate_HP != FEC_AUTO ? 1 << 2 : 0)
  435. | (ofdm->code_rate_LP != FEC_AUTO ? 1 << 1 : 0)
  436. | (ofdm->guard_interval != GUARD_INTERVAL_AUTO ? 1 << 0 : 0);
  437. /* multiplex_valid_flags, low byte */
  438. c->operand[6] =
  439. 0 << 7 /* NetworkID */
  440. | (ofdm->transmission_mode != TRANSMISSION_MODE_AUTO ? 1 << 6 : 0)
  441. | 0 << 5 /* OtherFrequencyFlag */
  442. | 0 << 0 /* reserved */ ;
  443. c->operand[7] = 0x0;
  444. c->operand[8] = (params->frequency / 10) >> 24;
  445. c->operand[9] = ((params->frequency / 10) >> 16) & 0xff;
  446. c->operand[10] = ((params->frequency / 10) >> 8) & 0xff;
  447. c->operand[11] = (params->frequency / 10) & 0xff;
  448. switch (ofdm->bandwidth) {
  449. case BANDWIDTH_7_MHZ: c->operand[12] = 0x20; break;
  450. case BANDWIDTH_8_MHZ:
  451. case BANDWIDTH_6_MHZ: /* not defined by AVC spec */
  452. case BANDWIDTH_AUTO:
  453. default: c->operand[12] = 0x00;
  454. }
  455. switch (ofdm->constellation) {
  456. case QAM_16: c->operand[13] = 1 << 6; break;
  457. case QAM_64: c->operand[13] = 2 << 6; break;
  458. case QPSK:
  459. default: c->operand[13] = 0x00;
  460. }
  461. switch (ofdm->hierarchy_information) {
  462. case HIERARCHY_1: c->operand[13] |= 1 << 3; break;
  463. case HIERARCHY_2: c->operand[13] |= 2 << 3; break;
  464. case HIERARCHY_4: c->operand[13] |= 3 << 3; break;
  465. case HIERARCHY_AUTO:
  466. case HIERARCHY_NONE:
  467. default: break;
  468. }
  469. switch (ofdm->code_rate_HP) {
  470. case FEC_2_3: c->operand[13] |= 1; break;
  471. case FEC_3_4: c->operand[13] |= 2; break;
  472. case FEC_5_6: c->operand[13] |= 3; break;
  473. case FEC_7_8: c->operand[13] |= 4; break;
  474. case FEC_1_2:
  475. default: break;
  476. }
  477. switch (ofdm->code_rate_LP) {
  478. case FEC_2_3: c->operand[14] = 1 << 5; break;
  479. case FEC_3_4: c->operand[14] = 2 << 5; break;
  480. case FEC_5_6: c->operand[14] = 3 << 5; break;
  481. case FEC_7_8: c->operand[14] = 4 << 5; break;
  482. case FEC_1_2:
  483. default: c->operand[14] = 0x00; break;
  484. }
  485. switch (ofdm->guard_interval) {
  486. case GUARD_INTERVAL_1_16: c->operand[14] |= 1 << 3; break;
  487. case GUARD_INTERVAL_1_8: c->operand[14] |= 2 << 3; break;
  488. case GUARD_INTERVAL_1_4: c->operand[14] |= 3 << 3; break;
  489. case GUARD_INTERVAL_1_32:
  490. case GUARD_INTERVAL_AUTO:
  491. default: break;
  492. }
  493. switch (ofdm->transmission_mode) {
  494. case TRANSMISSION_MODE_8K: c->operand[14] |= 1 << 1; break;
  495. case TRANSMISSION_MODE_2K:
  496. case TRANSMISSION_MODE_AUTO:
  497. default: break;
  498. }
  499. c->operand[15] = 0x00; /* network_ID[0] */
  500. c->operand[16] = 0x00; /* network_ID[1] */
  501. return 17 + add_pid_filter(fdtv, &c->operand[17]);
  502. }
  503. int avc_tuner_dsd(struct firedtv *fdtv,
  504. struct dvb_frontend_parameters *params)
  505. {
  506. struct avc_command_frame *c = (void *)fdtv->avc_data;
  507. int pos, ret;
  508. mutex_lock(&fdtv->avc_mutex);
  509. c->ctype = AVC_CTYPE_CONTROL;
  510. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  511. switch (fdtv->type) {
  512. case FIREDTV_DVB_S:
  513. case FIREDTV_DVB_S2: pos = avc_tuner_tuneqpsk(fdtv, params); break;
  514. case FIREDTV_DVB_C: pos = avc_tuner_dsd_dvb_c(fdtv, params); break;
  515. case FIREDTV_DVB_T: pos = avc_tuner_dsd_dvb_t(fdtv, params); break;
  516. default:
  517. BUG();
  518. }
  519. pad_operands(c, pos);
  520. fdtv->avc_data_length = ALIGN(3 + pos, 4);
  521. ret = avc_write(fdtv);
  522. #if 0
  523. /*
  524. * FIXME:
  525. * u8 *status was an out-parameter of avc_tuner_dsd, unused by caller.
  526. * Check for AVC_RESPONSE_ACCEPTED here instead?
  527. */
  528. if (status)
  529. *status = r->operand[2];
  530. #endif
  531. mutex_unlock(&fdtv->avc_mutex);
  532. if (ret == 0)
  533. msleep(500);
  534. return ret;
  535. }
  536. int avc_tuner_set_pids(struct firedtv *fdtv, unsigned char pidc, u16 pid[])
  537. {
  538. struct avc_command_frame *c = (void *)fdtv->avc_data;
  539. int ret, pos, k;
  540. if (pidc > 16 && pidc != 0xff)
  541. return -EINVAL;
  542. mutex_lock(&fdtv->avc_mutex);
  543. c->ctype = AVC_CTYPE_CONTROL;
  544. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  545. c->opcode = AVC_OPCODE_DSD;
  546. c->operand[0] = 0; /* source plug */
  547. c->operand[1] = 0xd2; /* subfunction replace */
  548. c->operand[2] = 0x20; /* system id = DVB */
  549. c->operand[3] = 0x00; /* antenna number */
  550. c->operand[4] = 0x00; /* system_specific_multiplex selection_length */
  551. c->operand[5] = pidc; /* Nr_of_dsd_sel_specs */
  552. pos = 6;
  553. if (pidc != 0xff)
  554. for (k = 0; k < pidc; k++) {
  555. c->operand[pos++] = 0x13; /* flowfunction relay */
  556. c->operand[pos++] = 0x80; /* dsd_sel_spec_valid_flags -> PID */
  557. c->operand[pos++] = (pid[k] >> 8) & 0x1f;
  558. c->operand[pos++] = pid[k] & 0xff;
  559. c->operand[pos++] = 0x00; /* tableID */
  560. c->operand[pos++] = 0x00; /* filter_length */
  561. }
  562. pad_operands(c, pos);
  563. fdtv->avc_data_length = ALIGN(3 + pos, 4);
  564. ret = avc_write(fdtv);
  565. /* FIXME: check response code? */
  566. mutex_unlock(&fdtv->avc_mutex);
  567. if (ret == 0)
  568. msleep(50);
  569. return ret;
  570. }
  571. int avc_tuner_get_ts(struct firedtv *fdtv)
  572. {
  573. struct avc_command_frame *c = (void *)fdtv->avc_data;
  574. int ret, sl;
  575. mutex_lock(&fdtv->avc_mutex);
  576. c->ctype = AVC_CTYPE_CONTROL;
  577. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  578. c->opcode = AVC_OPCODE_DSIT;
  579. sl = fdtv->type == FIREDTV_DVB_T ? 0x0c : 0x11;
  580. c->operand[0] = 0; /* source plug */
  581. c->operand[1] = 0xd2; /* subfunction replace */
  582. c->operand[2] = 0xff; /* status */
  583. c->operand[3] = 0x20; /* system id = DVB */
  584. c->operand[4] = 0x00; /* antenna number */
  585. c->operand[5] = 0x0; /* system_specific_search_flags */
  586. c->operand[6] = sl; /* system_specific_multiplex selection_length */
  587. /*
  588. * operand[7]: valid_flags[0]
  589. * operand[8]: valid_flags[1]
  590. * operand[7 + sl]: nr_of_dsit_sel_specs (always 0)
  591. */
  592. clear_operands(c, 7, 24);
  593. fdtv->avc_data_length = fdtv->type == FIREDTV_DVB_T ? 24 : 28;
  594. ret = avc_write(fdtv);
  595. /* FIXME: check response code? */
  596. mutex_unlock(&fdtv->avc_mutex);
  597. if (ret == 0)
  598. msleep(250);
  599. return ret;
  600. }
  601. int avc_identify_subunit(struct firedtv *fdtv)
  602. {
  603. struct avc_command_frame *c = (void *)fdtv->avc_data;
  604. struct avc_response_frame *r = (void *)fdtv->avc_data;
  605. int ret;
  606. mutex_lock(&fdtv->avc_mutex);
  607. c->ctype = AVC_CTYPE_CONTROL;
  608. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  609. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  610. c->operand[0] = DESCRIPTOR_SUBUNIT_IDENTIFIER;
  611. c->operand[1] = 0xff;
  612. c->operand[2] = 0x00;
  613. c->operand[3] = 0x00; /* length highbyte */
  614. c->operand[4] = 0x08; /* length lowbyte */
  615. c->operand[5] = 0x00; /* offset highbyte */
  616. c->operand[6] = 0x0d; /* offset lowbyte */
  617. clear_operands(c, 7, 8); /* padding */
  618. fdtv->avc_data_length = 12;
  619. ret = avc_write(fdtv);
  620. if (ret < 0)
  621. goto out;
  622. if ((r->response != AVC_RESPONSE_STABLE &&
  623. r->response != AVC_RESPONSE_ACCEPTED) ||
  624. (r->operand[3] << 8) + r->operand[4] != 8) {
  625. dev_err(fdtv->device, "cannot read subunit identifier\n");
  626. ret = -EINVAL;
  627. }
  628. out:
  629. mutex_unlock(&fdtv->avc_mutex);
  630. return ret;
  631. }
  632. #define SIZEOF_ANTENNA_INPUT_INFO 22
  633. int avc_tuner_status(struct firedtv *fdtv, struct firedtv_tuner_status *stat)
  634. {
  635. struct avc_command_frame *c = (void *)fdtv->avc_data;
  636. struct avc_response_frame *r = (void *)fdtv->avc_data;
  637. int length, ret;
  638. mutex_lock(&fdtv->avc_mutex);
  639. c->ctype = AVC_CTYPE_CONTROL;
  640. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  641. c->opcode = AVC_OPCODE_READ_DESCRIPTOR;
  642. c->operand[0] = DESCRIPTOR_TUNER_STATUS;
  643. c->operand[1] = 0xff; /* read_result_status */
  644. /*
  645. * operand[2]: reserved
  646. * operand[3]: SIZEOF_ANTENNA_INPUT_INFO >> 8
  647. * operand[4]: SIZEOF_ANTENNA_INPUT_INFO & 0xff
  648. */
  649. clear_operands(c, 2, 31);
  650. fdtv->avc_data_length = 12;
  651. ret = avc_write(fdtv);
  652. if (ret < 0)
  653. goto out;
  654. if (r->response != AVC_RESPONSE_STABLE &&
  655. r->response != AVC_RESPONSE_ACCEPTED) {
  656. dev_err(fdtv->device, "cannot read tuner status\n");
  657. ret = -EINVAL;
  658. goto out;
  659. }
  660. length = r->operand[9];
  661. if (r->operand[1] != 0x10 || length != SIZEOF_ANTENNA_INPUT_INFO) {
  662. dev_err(fdtv->device, "got invalid tuner status\n");
  663. ret = -EINVAL;
  664. goto out;
  665. }
  666. stat->active_system = r->operand[10];
  667. stat->searching = r->operand[11] >> 7 & 1;
  668. stat->moving = r->operand[11] >> 6 & 1;
  669. stat->no_rf = r->operand[11] >> 5 & 1;
  670. stat->input = r->operand[12] >> 7 & 1;
  671. stat->selected_antenna = r->operand[12] & 0x7f;
  672. stat->ber = r->operand[13] << 24 |
  673. r->operand[14] << 16 |
  674. r->operand[15] << 8 |
  675. r->operand[16];
  676. stat->signal_strength = r->operand[17];
  677. stat->raster_frequency = r->operand[18] >> 6 & 2;
  678. stat->rf_frequency = (r->operand[18] & 0x3f) << 16 |
  679. r->operand[19] << 8 |
  680. r->operand[20];
  681. stat->man_dep_info_length = r->operand[21];
  682. stat->front_end_error = r->operand[22] >> 4 & 1;
  683. stat->antenna_error = r->operand[22] >> 3 & 1;
  684. stat->front_end_power_status = r->operand[22] >> 1 & 1;
  685. stat->power_supply = r->operand[22] & 1;
  686. stat->carrier_noise_ratio = r->operand[23] << 8 |
  687. r->operand[24];
  688. stat->power_supply_voltage = r->operand[27];
  689. stat->antenna_voltage = r->operand[28];
  690. stat->firewire_bus_voltage = r->operand[29];
  691. stat->ca_mmi = r->operand[30] & 1;
  692. stat->ca_pmt_reply = r->operand[31] >> 7 & 1;
  693. stat->ca_date_time_request = r->operand[31] >> 6 & 1;
  694. stat->ca_application_info = r->operand[31] >> 5 & 1;
  695. stat->ca_module_present_status = r->operand[31] >> 4 & 1;
  696. stat->ca_dvb_flag = r->operand[31] >> 3 & 1;
  697. stat->ca_error_flag = r->operand[31] >> 2 & 1;
  698. stat->ca_initialization_status = r->operand[31] >> 1 & 1;
  699. out:
  700. mutex_unlock(&fdtv->avc_mutex);
  701. return ret;
  702. }
  703. int avc_lnb_control(struct firedtv *fdtv, char voltage, char burst,
  704. char conttone, char nrdiseq,
  705. struct dvb_diseqc_master_cmd *diseqcmd)
  706. {
  707. struct avc_command_frame *c = (void *)fdtv->avc_data;
  708. struct avc_response_frame *r = (void *)fdtv->avc_data;
  709. int pos, j, k, ret;
  710. mutex_lock(&fdtv->avc_mutex);
  711. c->ctype = AVC_CTYPE_CONTROL;
  712. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  713. c->opcode = AVC_OPCODE_VENDOR;
  714. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  715. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  716. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  717. c->operand[3] = SFE_VENDOR_OPCODE_LNB_CONTROL;
  718. c->operand[4] = voltage;
  719. c->operand[5] = nrdiseq;
  720. pos = 6;
  721. for (j = 0; j < nrdiseq; j++) {
  722. c->operand[pos++] = diseqcmd[j].msg_len;
  723. for (k = 0; k < diseqcmd[j].msg_len; k++)
  724. c->operand[pos++] = diseqcmd[j].msg[k];
  725. }
  726. c->operand[pos++] = burst;
  727. c->operand[pos++] = conttone;
  728. pad_operands(c, pos);
  729. fdtv->avc_data_length = ALIGN(3 + pos, 4);
  730. ret = avc_write(fdtv);
  731. if (ret < 0)
  732. goto out;
  733. if (r->response != AVC_RESPONSE_ACCEPTED) {
  734. dev_err(fdtv->device, "LNB control failed\n");
  735. ret = -EINVAL;
  736. }
  737. out:
  738. mutex_unlock(&fdtv->avc_mutex);
  739. return ret;
  740. }
  741. int avc_register_remote_control(struct firedtv *fdtv)
  742. {
  743. struct avc_command_frame *c = (void *)fdtv->avc_data;
  744. int ret;
  745. mutex_lock(&fdtv->avc_mutex);
  746. c->ctype = AVC_CTYPE_NOTIFY;
  747. c->subunit = AVC_SUBUNIT_TYPE_UNIT | 7;
  748. c->opcode = AVC_OPCODE_VENDOR;
  749. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  750. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  751. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  752. c->operand[3] = SFE_VENDOR_OPCODE_REGISTER_REMOTE_CONTROL;
  753. c->operand[4] = 0; /* padding */
  754. fdtv->avc_data_length = 8;
  755. ret = avc_write(fdtv);
  756. /* FIXME: check response code? */
  757. mutex_unlock(&fdtv->avc_mutex);
  758. return ret;
  759. }
  760. void avc_remote_ctrl_work(struct work_struct *work)
  761. {
  762. struct firedtv *fdtv =
  763. container_of(work, struct firedtv, remote_ctrl_work);
  764. /* Should it be rescheduled in failure cases? */
  765. avc_register_remote_control(fdtv);
  766. }
  767. #if 0 /* FIXME: unused */
  768. int avc_tuner_host2ca(struct firedtv *fdtv)
  769. {
  770. struct avc_command_frame *c = (void *)fdtv->avc_data;
  771. int ret;
  772. mutex_lock(&fdtv->avc_mutex);
  773. c->ctype = AVC_CTYPE_CONTROL;
  774. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  775. c->opcode = AVC_OPCODE_VENDOR;
  776. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  777. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  778. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  779. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  780. c->operand[4] = 0; /* slot */
  781. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  782. clear_operands(c, 6, 8);
  783. fdtv->avc_data_length = 12;
  784. ret = avc_write(fdtv);
  785. /* FIXME: check response code? */
  786. mutex_unlock(&fdtv->avc_mutex);
  787. return ret;
  788. }
  789. #endif
  790. static int get_ca_object_pos(struct avc_response_frame *r)
  791. {
  792. int length = 1;
  793. /* Check length of length field */
  794. if (r->operand[7] & 0x80)
  795. length = (r->operand[7] & 0x7f) + 1;
  796. return length + 7;
  797. }
  798. static int get_ca_object_length(struct avc_response_frame *r)
  799. {
  800. #if 0 /* FIXME: unused */
  801. int size = 0;
  802. int i;
  803. if (r->operand[7] & 0x80)
  804. for (i = 0; i < (r->operand[7] & 0x7f); i++) {
  805. size <<= 8;
  806. size += r->operand[8 + i];
  807. }
  808. #endif
  809. return r->operand[7];
  810. }
  811. int avc_ca_app_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
  812. {
  813. struct avc_command_frame *c = (void *)fdtv->avc_data;
  814. struct avc_response_frame *r = (void *)fdtv->avc_data;
  815. int pos, ret;
  816. mutex_lock(&fdtv->avc_mutex);
  817. c->ctype = AVC_CTYPE_STATUS;
  818. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  819. c->opcode = AVC_OPCODE_VENDOR;
  820. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  821. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  822. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  823. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  824. c->operand[4] = 0; /* slot */
  825. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  826. clear_operands(c, 6, LAST_OPERAND);
  827. fdtv->avc_data_length = 12;
  828. ret = avc_write(fdtv);
  829. if (ret < 0)
  830. goto out;
  831. /* FIXME: check response code and validate response data */
  832. pos = get_ca_object_pos(r);
  833. app_info[0] = (EN50221_TAG_APP_INFO >> 16) & 0xff;
  834. app_info[1] = (EN50221_TAG_APP_INFO >> 8) & 0xff;
  835. app_info[2] = (EN50221_TAG_APP_INFO >> 0) & 0xff;
  836. app_info[3] = 6 + r->operand[pos + 4];
  837. app_info[4] = 0x01;
  838. memcpy(&app_info[5], &r->operand[pos], 5 + r->operand[pos + 4]);
  839. *len = app_info[3] + 4;
  840. out:
  841. mutex_unlock(&fdtv->avc_mutex);
  842. return ret;
  843. }
  844. int avc_ca_info(struct firedtv *fdtv, char *app_info, unsigned int *len)
  845. {
  846. struct avc_command_frame *c = (void *)fdtv->avc_data;
  847. struct avc_response_frame *r = (void *)fdtv->avc_data;
  848. int i, pos, ret;
  849. mutex_lock(&fdtv->avc_mutex);
  850. c->ctype = AVC_CTYPE_STATUS;
  851. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  852. c->opcode = AVC_OPCODE_VENDOR;
  853. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  854. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  855. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  856. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  857. c->operand[4] = 0; /* slot */
  858. c->operand[5] = SFE_VENDOR_TAG_CA_APPLICATION_INFO; /* ca tag */
  859. clear_operands(c, 6, LAST_OPERAND);
  860. fdtv->avc_data_length = 12;
  861. ret = avc_write(fdtv);
  862. if (ret < 0)
  863. goto out;
  864. /* FIXME: check response code and validate response data */
  865. pos = get_ca_object_pos(r);
  866. app_info[0] = (EN50221_TAG_CA_INFO >> 16) & 0xff;
  867. app_info[1] = (EN50221_TAG_CA_INFO >> 8) & 0xff;
  868. app_info[2] = (EN50221_TAG_CA_INFO >> 0) & 0xff;
  869. if (num_fake_ca_system_ids == 0) {
  870. app_info[3] = 2;
  871. app_info[4] = r->operand[pos + 0];
  872. app_info[5] = r->operand[pos + 1];
  873. } else {
  874. app_info[3] = num_fake_ca_system_ids * 2;
  875. for (i = 0; i < num_fake_ca_system_ids; i++) {
  876. app_info[4 + i * 2] =
  877. (fake_ca_system_ids[i] >> 8) & 0xff;
  878. app_info[5 + i * 2] = fake_ca_system_ids[i] & 0xff;
  879. }
  880. }
  881. *len = app_info[3] + 4;
  882. out:
  883. mutex_unlock(&fdtv->avc_mutex);
  884. return ret;
  885. }
  886. int avc_ca_reset(struct firedtv *fdtv)
  887. {
  888. struct avc_command_frame *c = (void *)fdtv->avc_data;
  889. int ret;
  890. mutex_lock(&fdtv->avc_mutex);
  891. c->ctype = AVC_CTYPE_CONTROL;
  892. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  893. c->opcode = AVC_OPCODE_VENDOR;
  894. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  895. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  896. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  897. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  898. c->operand[4] = 0; /* slot */
  899. c->operand[5] = SFE_VENDOR_TAG_CA_RESET; /* ca tag */
  900. c->operand[6] = 0; /* more/last */
  901. c->operand[7] = 1; /* length */
  902. c->operand[8] = 0; /* force hardware reset */
  903. fdtv->avc_data_length = 12;
  904. ret = avc_write(fdtv);
  905. /* FIXME: check response code? */
  906. mutex_unlock(&fdtv->avc_mutex);
  907. return ret;
  908. }
  909. int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length)
  910. {
  911. struct avc_command_frame *c = (void *)fdtv->avc_data;
  912. struct avc_response_frame *r = (void *)fdtv->avc_data;
  913. int list_management;
  914. int program_info_length;
  915. int pmt_cmd_id;
  916. int read_pos;
  917. int write_pos;
  918. int es_info_length;
  919. int crc32_csum;
  920. int ret;
  921. if (unlikely(avc_debug & AVC_DEBUG_APPLICATION_PMT))
  922. debug_pmt(msg, length);
  923. mutex_lock(&fdtv->avc_mutex);
  924. c->ctype = AVC_CTYPE_CONTROL;
  925. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  926. c->opcode = AVC_OPCODE_VENDOR;
  927. if (msg[0] != EN50221_LIST_MANAGEMENT_ONLY) {
  928. dev_info(fdtv->device, "forcing list_management to ONLY\n");
  929. msg[0] = EN50221_LIST_MANAGEMENT_ONLY;
  930. }
  931. /* We take the cmd_id from the programme level only! */
  932. list_management = msg[0];
  933. program_info_length = ((msg[4] & 0x0f) << 8) + msg[5];
  934. if (program_info_length > 0)
  935. program_info_length--; /* Remove pmt_cmd_id */
  936. pmt_cmd_id = msg[6];
  937. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  938. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  939. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  940. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  941. c->operand[4] = 0; /* slot */
  942. c->operand[5] = SFE_VENDOR_TAG_CA_PMT; /* ca tag */
  943. c->operand[6] = 0; /* more/last */
  944. /* Use three bytes for length field in case length > 127 */
  945. c->operand[10] = list_management;
  946. c->operand[11] = 0x01; /* pmt_cmd=OK_descramble */
  947. /* TS program map table */
  948. c->operand[12] = 0x02; /* Table id=2 */
  949. c->operand[13] = 0x80; /* Section syntax + length */
  950. c->operand[15] = msg[1]; /* Program number */
  951. c->operand[16] = msg[2];
  952. c->operand[17] = msg[3]; /* Version number and current/next */
  953. c->operand[18] = 0x00; /* Section number=0 */
  954. c->operand[19] = 0x00; /* Last section number=0 */
  955. c->operand[20] = 0x1f; /* PCR_PID=1FFF */
  956. c->operand[21] = 0xff;
  957. c->operand[22] = (program_info_length >> 8); /* Program info length */
  958. c->operand[23] = (program_info_length & 0xff);
  959. /* CA descriptors at programme level */
  960. read_pos = 6;
  961. write_pos = 24;
  962. if (program_info_length > 0) {
  963. pmt_cmd_id = msg[read_pos++];
  964. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  965. dev_err(fdtv->device,
  966. "invalid pmt_cmd_id %d\n", pmt_cmd_id);
  967. memcpy(&c->operand[write_pos], &msg[read_pos],
  968. program_info_length);
  969. read_pos += program_info_length;
  970. write_pos += program_info_length;
  971. }
  972. while (read_pos < length) {
  973. c->operand[write_pos++] = msg[read_pos++];
  974. c->operand[write_pos++] = msg[read_pos++];
  975. c->operand[write_pos++] = msg[read_pos++];
  976. es_info_length =
  977. ((msg[read_pos] & 0x0f) << 8) + msg[read_pos + 1];
  978. read_pos += 2;
  979. if (es_info_length > 0)
  980. es_info_length--; /* Remove pmt_cmd_id */
  981. c->operand[write_pos++] = es_info_length >> 8;
  982. c->operand[write_pos++] = es_info_length & 0xff;
  983. if (es_info_length > 0) {
  984. pmt_cmd_id = msg[read_pos++];
  985. if (pmt_cmd_id != 1 && pmt_cmd_id != 4)
  986. dev_err(fdtv->device, "invalid pmt_cmd_id %d "
  987. "at stream level\n", pmt_cmd_id);
  988. memcpy(&c->operand[write_pos], &msg[read_pos],
  989. es_info_length);
  990. read_pos += es_info_length;
  991. write_pos += es_info_length;
  992. }
  993. }
  994. write_pos += 4; /* CRC */
  995. c->operand[7] = 0x82;
  996. c->operand[8] = (write_pos - 10) >> 8;
  997. c->operand[9] = (write_pos - 10) & 0xff;
  998. c->operand[14] = write_pos - 15;
  999. crc32_csum = crc32_be(0, &c->operand[10], c->operand[12] - 1);
  1000. c->operand[write_pos - 4] = (crc32_csum >> 24) & 0xff;
  1001. c->operand[write_pos - 3] = (crc32_csum >> 16) & 0xff;
  1002. c->operand[write_pos - 2] = (crc32_csum >> 8) & 0xff;
  1003. c->operand[write_pos - 1] = (crc32_csum >> 0) & 0xff;
  1004. pad_operands(c, write_pos);
  1005. fdtv->avc_data_length = ALIGN(3 + write_pos, 4);
  1006. ret = avc_write(fdtv);
  1007. if (ret < 0)
  1008. goto out;
  1009. if (r->response != AVC_RESPONSE_ACCEPTED) {
  1010. dev_err(fdtv->device,
  1011. "CA PMT failed with response 0x%x\n", r->response);
  1012. ret = -EACCES;
  1013. }
  1014. out:
  1015. mutex_unlock(&fdtv->avc_mutex);
  1016. return ret;
  1017. }
  1018. int avc_ca_get_time_date(struct firedtv *fdtv, int *interval)
  1019. {
  1020. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1021. struct avc_response_frame *r = (void *)fdtv->avc_data;
  1022. int ret;
  1023. mutex_lock(&fdtv->avc_mutex);
  1024. c->ctype = AVC_CTYPE_STATUS;
  1025. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1026. c->opcode = AVC_OPCODE_VENDOR;
  1027. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1028. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1029. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1030. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  1031. c->operand[4] = 0; /* slot */
  1032. c->operand[5] = SFE_VENDOR_TAG_CA_DATE_TIME; /* ca tag */
  1033. clear_operands(c, 6, LAST_OPERAND);
  1034. fdtv->avc_data_length = 12;
  1035. ret = avc_write(fdtv);
  1036. if (ret < 0)
  1037. goto out;
  1038. /* FIXME: check response code and validate response data */
  1039. *interval = r->operand[get_ca_object_pos(r)];
  1040. out:
  1041. mutex_unlock(&fdtv->avc_mutex);
  1042. return ret;
  1043. }
  1044. int avc_ca_enter_menu(struct firedtv *fdtv)
  1045. {
  1046. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1047. int ret;
  1048. mutex_lock(&fdtv->avc_mutex);
  1049. c->ctype = AVC_CTYPE_STATUS;
  1050. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1051. c->opcode = AVC_OPCODE_VENDOR;
  1052. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1053. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1054. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1055. c->operand[3] = SFE_VENDOR_OPCODE_HOST2CA;
  1056. c->operand[4] = 0; /* slot */
  1057. c->operand[5] = SFE_VENDOR_TAG_CA_ENTER_MENU;
  1058. clear_operands(c, 6, 8);
  1059. fdtv->avc_data_length = 12;
  1060. ret = avc_write(fdtv);
  1061. /* FIXME: check response code? */
  1062. mutex_unlock(&fdtv->avc_mutex);
  1063. return ret;
  1064. }
  1065. int avc_ca_get_mmi(struct firedtv *fdtv, char *mmi_object, unsigned int *len)
  1066. {
  1067. struct avc_command_frame *c = (void *)fdtv->avc_data;
  1068. struct avc_response_frame *r = (void *)fdtv->avc_data;
  1069. int ret;
  1070. mutex_lock(&fdtv->avc_mutex);
  1071. c->ctype = AVC_CTYPE_STATUS;
  1072. c->subunit = AVC_SUBUNIT_TYPE_TUNER | fdtv->subunit;
  1073. c->opcode = AVC_OPCODE_VENDOR;
  1074. c->operand[0] = SFE_VENDOR_DE_COMPANYID_0;
  1075. c->operand[1] = SFE_VENDOR_DE_COMPANYID_1;
  1076. c->operand[2] = SFE_VENDOR_DE_COMPANYID_2;
  1077. c->operand[3] = SFE_VENDOR_OPCODE_CA2HOST;
  1078. c->operand[4] = 0; /* slot */
  1079. c->operand[5] = SFE_VENDOR_TAG_CA_MMI;
  1080. clear_operands(c, 6, LAST_OPERAND);
  1081. fdtv->avc_data_length = 12;
  1082. ret = avc_write(fdtv);
  1083. if (ret < 0)
  1084. goto out;
  1085. /* FIXME: check response code and validate response data */
  1086. *len = get_ca_object_length(r);
  1087. memcpy(mmi_object, &r->operand[get_ca_object_pos(r)], *len);
  1088. out:
  1089. mutex_unlock(&fdtv->avc_mutex);
  1090. return ret;
  1091. }
  1092. #define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
  1093. static int cmp_read(struct firedtv *fdtv, u64 addr, __be32 *data)
  1094. {
  1095. int ret;
  1096. ret = fdtv_read(fdtv, addr, data);
  1097. if (ret < 0)
  1098. dev_err(fdtv->device, "CMP: read I/O error\n");
  1099. return ret;
  1100. }
  1101. static int cmp_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
  1102. {
  1103. int ret;
  1104. ret = fdtv_lock(fdtv, addr, data);
  1105. if (ret < 0)
  1106. dev_err(fdtv->device, "CMP: lock I/O error\n");
  1107. return ret;
  1108. }
  1109. static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
  1110. {
  1111. return (be32_to_cpu(opcr) >> shift) & mask;
  1112. }
  1113. static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
  1114. {
  1115. *opcr &= ~cpu_to_be32(mask << shift);
  1116. *opcr |= cpu_to_be32((value & mask) << shift);
  1117. }
  1118. #define get_opcr_online(v) get_opcr((v), 0x1, 31)
  1119. #define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
  1120. #define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
  1121. #define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
  1122. #define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
  1123. #define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
  1124. #define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
  1125. int cmp_establish_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1126. {
  1127. __be32 old_opcr, opcr[2];
  1128. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1129. int attempts = 0;
  1130. int ret;
  1131. ret = cmp_read(fdtv, opcr_address, opcr);
  1132. if (ret < 0)
  1133. return ret;
  1134. repeat:
  1135. if (!get_opcr_online(*opcr)) {
  1136. dev_err(fdtv->device, "CMP: output offline\n");
  1137. return -EBUSY;
  1138. }
  1139. old_opcr = *opcr;
  1140. if (get_opcr_p2p_connections(*opcr)) {
  1141. if (get_opcr_channel(*opcr) != channel) {
  1142. dev_err(fdtv->device, "CMP: cannot change channel\n");
  1143. return -EBUSY;
  1144. }
  1145. dev_info(fdtv->device, "CMP: overlaying connection\n");
  1146. /* We don't allocate isochronous resources. */
  1147. } else {
  1148. set_opcr_channel(opcr, channel);
  1149. set_opcr_data_rate(opcr, 2); /* S400 */
  1150. /* FIXME: this is for the worst case - optimize */
  1151. set_opcr_overhead_id(opcr, 0);
  1152. /* FIXME: allocate isochronous channel and bandwidth at IRM */
  1153. }
  1154. set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) + 1);
  1155. opcr[1] = *opcr;
  1156. opcr[0] = old_opcr;
  1157. ret = cmp_lock(fdtv, opcr_address, opcr);
  1158. if (ret < 0)
  1159. return ret;
  1160. if (old_opcr != *opcr) {
  1161. /*
  1162. * FIXME: if old_opcr.P2P_Connections > 0,
  1163. * deallocate isochronous channel and bandwidth at IRM
  1164. */
  1165. if (++attempts < 6) /* arbitrary limit */
  1166. goto repeat;
  1167. return -EBUSY;
  1168. }
  1169. return 0;
  1170. }
  1171. void cmp_break_pp_connection(struct firedtv *fdtv, int plug, int channel)
  1172. {
  1173. __be32 old_opcr, opcr[2];
  1174. u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
  1175. int attempts = 0;
  1176. if (cmp_read(fdtv, opcr_address, opcr) < 0)
  1177. return;
  1178. repeat:
  1179. if (!get_opcr_online(*opcr) || !get_opcr_p2p_connections(*opcr) ||
  1180. get_opcr_channel(*opcr) != channel) {
  1181. dev_err(fdtv->device, "CMP: no connection to break\n");
  1182. return;
  1183. }
  1184. old_opcr = *opcr;
  1185. set_opcr_p2p_connections(opcr, get_opcr_p2p_connections(*opcr) - 1);
  1186. opcr[1] = *opcr;
  1187. opcr[0] = old_opcr;
  1188. if (cmp_lock(fdtv, opcr_address, opcr) < 0)
  1189. return;
  1190. if (old_opcr != *opcr) {
  1191. /*
  1192. * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
  1193. * owner, deallocate isochronous channel and bandwidth at IRM
  1194. * if (...)
  1195. * fdtv->backend->dealloc_resources(fdtv, channel, bw);
  1196. */
  1197. if (++attempts < 6) /* arbitrary limit */
  1198. goto repeat;
  1199. }
  1200. }