firedtv-avc.c 40 KB

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