firedtv-avc.c 36 KB

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