firedtv-avc.c 40 KB

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