dvb-ttusb-budget.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /*
  2. * TTUSB DVB driver
  3. *
  4. * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
  5. * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/usb.h>
  18. #include <linux/delay.h>
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <asm/semaphore.h>
  22. #include "dvb_frontend.h"
  23. #include "dmxdev.h"
  24. #include "dvb_demux.h"
  25. #include "dvb_net.h"
  26. #include "ves1820.h"
  27. #include "cx22700.h"
  28. #include "tda1004x.h"
  29. #include "stv0299.h"
  30. #include "tda8083.h"
  31. #include <linux/dvb/frontend.h>
  32. #include <linux/dvb/dmx.h>
  33. #include <linux/pci.h>
  34. /*
  35. TTUSB_HWSECTIONS:
  36. the DSP supports filtering in hardware, however, since the "muxstream"
  37. is a bit braindead (no matching channel masks or no matching filter mask),
  38. we won't support this - yet. it doesn't event support negative filters,
  39. so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
  40. parse TS data. USB bandwith will be a problem when having large
  41. datastreams, especially for dvb-net, but hey, that's not my problem.
  42. TTUSB_DISEQC, TTUSB_TONE:
  43. let the STC do the diseqc/tone stuff. this isn't supported at least with
  44. my TTUSB, so let it undef'd unless you want to implement another
  45. frontend. never tested.
  46. DEBUG:
  47. define it to > 3 for really hardcore debugging. you probably don't want
  48. this unless the device doesn't load at all. > 2 for bandwidth statistics.
  49. */
  50. static int debug;
  51. module_param(debug, int, 0644);
  52. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  53. #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
  54. #define ISO_BUF_COUNT 4
  55. #define FRAMES_PER_ISO_BUF 4
  56. #define ISO_FRAME_SIZE 912
  57. #define TTUSB_MAXCHANNEL 32
  58. #ifdef TTUSB_HWSECTIONS
  59. #define TTUSB_MAXFILTER 16 /* ??? */
  60. #endif
  61. #define TTUSB_REV_2_2 0x22
  62. #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
  63. /**
  64. * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
  65. * the dvb_demux field must be the first in struct!!
  66. */
  67. struct ttusb {
  68. struct dvb_demux dvb_demux;
  69. struct dmxdev dmxdev;
  70. struct dvb_net dvbnet;
  71. /* and one for USB access. */
  72. struct semaphore semi2c;
  73. struct semaphore semusb;
  74. struct dvb_adapter adapter;
  75. struct usb_device *dev;
  76. struct i2c_adapter i2c_adap;
  77. int disconnecting;
  78. int iso_streaming;
  79. unsigned int bulk_out_pipe;
  80. unsigned int bulk_in_pipe;
  81. unsigned int isoc_in_pipe;
  82. void *iso_buffer;
  83. dma_addr_t iso_dma_handle;
  84. struct urb *iso_urb[ISO_BUF_COUNT];
  85. int running_feed_count;
  86. int last_channel;
  87. int last_filter;
  88. u8 c; /* transaction counter, wraps around... */
  89. fe_sec_tone_mode_t tone;
  90. fe_sec_voltage_t voltage;
  91. int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
  92. u8 mux_npacks;
  93. u8 muxpack[256 + 8];
  94. int muxpack_ptr, muxpack_len;
  95. int insync;
  96. int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
  97. /* (including stuffing. yes. really.) */
  98. u8 last_result[32];
  99. int revision;
  100. #if 0
  101. devfs_handle_t stc_devfs_handle;
  102. #endif
  103. struct dvb_frontend* fe;
  104. };
  105. /* ugly workaround ... don't know why it's neccessary to read */
  106. /* all result codes. */
  107. #define DEBUG 0
  108. static int ttusb_cmd(struct ttusb *ttusb,
  109. const u8 * data, int len, int needresult)
  110. {
  111. int actual_len;
  112. int err;
  113. #if DEBUG >= 3
  114. int i;
  115. printk(">");
  116. for (i = 0; i < len; ++i)
  117. printk(" %02x", data[i]);
  118. printk("\n");
  119. #endif
  120. if (down_interruptible(&ttusb->semusb) < 0)
  121. return -EAGAIN;
  122. err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
  123. (u8 *) data, len, &actual_len, 1000);
  124. if (err != 0) {
  125. dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
  126. __FUNCTION__, err);
  127. up(&ttusb->semusb);
  128. return err;
  129. }
  130. if (actual_len != len) {
  131. dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
  132. actual_len, len);
  133. up(&ttusb->semusb);
  134. return -1;
  135. }
  136. err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
  137. ttusb->last_result, 32, &actual_len, 1000);
  138. if (err != 0) {
  139. printk("%s: failed, receive error %d\n", __FUNCTION__,
  140. err);
  141. up(&ttusb->semusb);
  142. return err;
  143. }
  144. #if DEBUG >= 3
  145. actual_len = ttusb->last_result[3] + 4;
  146. printk("<");
  147. for (i = 0; i < actual_len; ++i)
  148. printk(" %02x", ttusb->last_result[i]);
  149. printk("\n");
  150. #endif
  151. if (!needresult)
  152. up(&ttusb->semusb);
  153. return 0;
  154. }
  155. static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
  156. {
  157. memcpy(data, ttusb->last_result, len);
  158. up(&ttusb->semusb);
  159. return 0;
  160. }
  161. static int ttusb_i2c_msg(struct ttusb *ttusb,
  162. u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
  163. u8 rcv_len)
  164. {
  165. u8 b[0x28];
  166. u8 id = ++ttusb->c;
  167. int i, err;
  168. if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
  169. return -EINVAL;
  170. b[0] = 0xaa;
  171. b[1] = id;
  172. b[2] = 0x31;
  173. b[3] = snd_len + 3;
  174. b[4] = addr << 1;
  175. b[5] = snd_len;
  176. b[6] = rcv_len;
  177. for (i = 0; i < snd_len; i++)
  178. b[7 + i] = snd_buf[i];
  179. err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
  180. if (err)
  181. return -EREMOTEIO;
  182. err = ttusb_result(ttusb, b, 0x20);
  183. /* check if the i2c transaction was successful */
  184. if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
  185. if (rcv_len > 0) {
  186. if (err || b[0] != 0x55 || b[1] != id) {
  187. dprintk
  188. ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
  189. __FUNCTION__, err, id);
  190. return -EREMOTEIO;
  191. }
  192. for (i = 0; i < rcv_len; i++)
  193. rcv_buf[i] = b[7 + i];
  194. }
  195. return rcv_len;
  196. }
  197. static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
  198. {
  199. struct ttusb *ttusb = i2c_get_adapdata(adapter);
  200. int i = 0;
  201. int inc;
  202. if (down_interruptible(&ttusb->semi2c) < 0)
  203. return -EAGAIN;
  204. while (i < num) {
  205. u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
  206. int err;
  207. if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
  208. addr = msg[i].addr;
  209. snd_buf = msg[i].buf;
  210. snd_len = msg[i].len;
  211. rcv_buf = msg[i + 1].buf;
  212. rcv_len = msg[i + 1].len;
  213. inc = 2;
  214. } else {
  215. addr = msg[i].addr;
  216. snd_buf = msg[i].buf;
  217. snd_len = msg[i].len;
  218. rcv_buf = NULL;
  219. rcv_len = 0;
  220. inc = 1;
  221. }
  222. err = ttusb_i2c_msg(ttusb, addr,
  223. snd_buf, snd_len, rcv_buf, rcv_len);
  224. if (err < rcv_len) {
  225. dprintk("%s: i == %i\n", __FUNCTION__, i);
  226. break;
  227. }
  228. i += inc;
  229. }
  230. up(&ttusb->semi2c);
  231. return i;
  232. }
  233. #include "dvb-ttusb-dspbootcode.h"
  234. static int ttusb_boot_dsp(struct ttusb *ttusb)
  235. {
  236. int i, err;
  237. u8 b[40];
  238. /* BootBlock */
  239. b[0] = 0xaa;
  240. b[2] = 0x13;
  241. b[3] = 28;
  242. /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
  243. /* 32 is max packet size, no messages should be splitted. */
  244. for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
  245. memcpy(&b[4], &dsp_bootcode[i], 28);
  246. b[1] = ++ttusb->c;
  247. err = ttusb_cmd(ttusb, b, 32, 0);
  248. if (err)
  249. goto done;
  250. }
  251. /* last block ... */
  252. b[1] = ++ttusb->c;
  253. b[2] = 0x13;
  254. b[3] = 0;
  255. err = ttusb_cmd(ttusb, b, 4, 0);
  256. if (err)
  257. goto done;
  258. /* BootEnd */
  259. b[1] = ++ttusb->c;
  260. b[2] = 0x14;
  261. b[3] = 0;
  262. err = ttusb_cmd(ttusb, b, 4, 0);
  263. done:
  264. if (err) {
  265. dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
  266. __FUNCTION__, err);
  267. }
  268. return err;
  269. }
  270. static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
  271. int pid)
  272. {
  273. int err;
  274. /* SetChannel */
  275. u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
  276. (pid >> 8) & 0xff, pid & 0xff
  277. };
  278. err = ttusb_cmd(ttusb, b, sizeof(b), 0);
  279. return err;
  280. }
  281. static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
  282. {
  283. int err;
  284. /* DelChannel */
  285. u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
  286. err = ttusb_cmd(ttusb, b, sizeof(b), 0);
  287. return err;
  288. }
  289. #ifdef TTUSB_HWSECTIONS
  290. static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
  291. int associated_chan, u8 filter[8], u8 mask[8])
  292. {
  293. int err;
  294. /* SetFilter */
  295. u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
  296. filter[0], filter[1], filter[2], filter[3],
  297. filter[4], filter[5], filter[6], filter[7],
  298. filter[8], filter[9], filter[10], filter[11],
  299. mask[0], mask[1], mask[2], mask[3],
  300. mask[4], mask[5], mask[6], mask[7],
  301. mask[8], mask[9], mask[10], mask[11]
  302. };
  303. err = ttusb_cmd(ttusb, b, sizeof(b), 0);
  304. return err;
  305. }
  306. static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
  307. {
  308. int err;
  309. /* DelFilter */
  310. u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
  311. err = ttusb_cmd(ttusb, b, sizeof(b), 0);
  312. return err;
  313. }
  314. #endif
  315. static int ttusb_init_controller(struct ttusb *ttusb)
  316. {
  317. u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
  318. u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
  319. u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
  320. /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
  321. u8 b3[] =
  322. { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
  323. u8 b4[] =
  324. { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
  325. u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
  326. u8 get_dsp_version[0x20] =
  327. { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
  328. int err;
  329. /* reset board */
  330. if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
  331. return err;
  332. /* reset board (again?) */
  333. if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
  334. return err;
  335. ttusb_boot_dsp(ttusb);
  336. /* set i2c bit rate */
  337. if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
  338. return err;
  339. if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
  340. return err;
  341. err = ttusb_result(ttusb, b4, sizeof(b4));
  342. if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
  343. return err;
  344. if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
  345. return err;
  346. dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
  347. get_version[4], get_version[5], get_version[6],
  348. get_version[7], get_version[8]);
  349. if (memcmp(get_version + 4, "V 0.0", 5) &&
  350. memcmp(get_version + 4, "V 1.1", 5) &&
  351. memcmp(get_version + 4, "V 2.1", 5) &&
  352. memcmp(get_version + 4, "V 2.2", 5)) {
  353. printk
  354. ("%s: unknown STC version %c%c%c%c%c, please report!\n",
  355. __FUNCTION__, get_version[4], get_version[5],
  356. get_version[6], get_version[7], get_version[8]);
  357. }
  358. ttusb->revision = ((get_version[6] - '0') << 4) |
  359. (get_version[8] - '0');
  360. err =
  361. ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
  362. if (err)
  363. return err;
  364. err =
  365. ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
  366. if (err)
  367. return err;
  368. printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
  369. get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
  370. return 0;
  371. }
  372. #ifdef TTUSB_DISEQC
  373. static int ttusb_send_diseqc(struct dvb_frontend* fe,
  374. const struct dvb_diseqc_master_cmd *cmd)
  375. {
  376. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  377. u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
  378. int err;
  379. b[3] = 4 + 2 + cmd->msg_len;
  380. b[4] = 0xFF; /* send diseqc master, not burst */
  381. b[5] = cmd->msg_len;
  382. memcpy(b + 5, cmd->msg, cmd->msg_len);
  383. /* Diseqc */
  384. if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
  385. dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
  386. __FUNCTION__, err);
  387. }
  388. return err;
  389. }
  390. #endif
  391. static int lnbp21_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  392. {
  393. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  394. int ret;
  395. u8 data[1];
  396. struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = data, .len = sizeof(data) };
  397. switch(voltage) {
  398. case SEC_VOLTAGE_OFF:
  399. data[0] = 0x00;
  400. break;
  401. case SEC_VOLTAGE_13:
  402. data[0] = 0x44;
  403. break;
  404. case SEC_VOLTAGE_18:
  405. data[0] = 0x4c;
  406. break;
  407. default:
  408. return -EINVAL;
  409. };
  410. ret = i2c_transfer(&ttusb->i2c_adap, &msg, 1);
  411. return (ret != 1) ? -EIO : 0;
  412. }
  413. static int ttusb_update_lnb(struct ttusb *ttusb)
  414. {
  415. u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
  416. ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
  417. ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
  418. };
  419. int err;
  420. /* SetLNB */
  421. if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
  422. dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
  423. __FUNCTION__, err);
  424. }
  425. return err;
  426. }
  427. static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  428. {
  429. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  430. ttusb->voltage = voltage;
  431. return ttusb_update_lnb(ttusb);
  432. }
  433. #ifdef TTUSB_TONE
  434. static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  435. {
  436. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  437. ttusb->tone = tone;
  438. return ttusb_update_lnb(ttusb);
  439. }
  440. #endif
  441. #if 0
  442. static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
  443. {
  444. u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
  445. int err, actual_len;
  446. err = ttusb_cmd(ttusb, b, sizeof(b), 0);
  447. if (err) {
  448. dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
  449. __FUNCTION__, err);
  450. }
  451. }
  452. #endif
  453. /*****************************************************************************/
  454. #ifdef TTUSB_HWSECTIONS
  455. static void ttusb_handle_ts_data(struct ttusb_channel *channel,
  456. const u8 * data, int len);
  457. static void ttusb_handle_sec_data(struct ttusb_channel *channel,
  458. const u8 * data, int len);
  459. #endif
  460. static int numpkt = 0, lastj, numts, numstuff, numsec, numinvalid;
  461. static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
  462. int len)
  463. {
  464. u16 csum = 0, cc;
  465. int i;
  466. for (i = 0; i < len; i += 2)
  467. csum ^= le16_to_cpup((u16 *) (muxpack + i));
  468. if (csum) {
  469. printk("%s: muxpack with incorrect checksum, ignoring\n",
  470. __FUNCTION__);
  471. numinvalid++;
  472. return;
  473. }
  474. cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
  475. cc &= 0x7FFF;
  476. if ((cc != ttusb->cc) && (ttusb->cc != -1))
  477. printk("%s: cc discontinuity (%d frames missing)\n",
  478. __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
  479. ttusb->cc = (cc + 1) & 0x7FFF;
  480. if (muxpack[0] & 0x80) {
  481. #ifdef TTUSB_HWSECTIONS
  482. /* section data */
  483. int pusi = muxpack[0] & 0x40;
  484. int channel = muxpack[0] & 0x1F;
  485. int payload = muxpack[1];
  486. const u8 *data = muxpack + 2;
  487. /* check offset flag */
  488. if (muxpack[0] & 0x20)
  489. data++;
  490. ttusb_handle_sec_data(ttusb->channel + channel, data,
  491. payload);
  492. data += payload;
  493. if ((!!(ttusb->muxpack[0] & 0x20)) ^
  494. !!(ttusb->muxpack[1] & 1))
  495. data++;
  496. #warning TODO: pusi
  497. printk("cc: %04x\n", (data[0] << 8) | data[1]);
  498. #endif
  499. numsec++;
  500. } else if (muxpack[0] == 0x47) {
  501. #ifdef TTUSB_HWSECTIONS
  502. /* we have TS data here! */
  503. int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
  504. int channel;
  505. for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
  506. if (ttusb->channel[channel].active
  507. && (pid == ttusb->channel[channel].pid))
  508. ttusb_handle_ts_data(ttusb->channel +
  509. channel, muxpack,
  510. 188);
  511. #endif
  512. numts++;
  513. dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
  514. } else if (muxpack[0] != 0) {
  515. numinvalid++;
  516. printk("illegal muxpack type %02x\n", muxpack[0]);
  517. } else
  518. numstuff++;
  519. }
  520. static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
  521. {
  522. int maxwork = 1024;
  523. while (len) {
  524. if (!(maxwork--)) {
  525. printk("%s: too much work\n", __FUNCTION__);
  526. break;
  527. }
  528. switch (ttusb->mux_state) {
  529. case 0:
  530. case 1:
  531. case 2:
  532. len--;
  533. if (*data++ == 0xAA)
  534. ++ttusb->mux_state;
  535. else {
  536. ttusb->mux_state = 0;
  537. #if DEBUG > 3
  538. if (ttusb->insync)
  539. printk("%02x ", data[-1]);
  540. #else
  541. if (ttusb->insync) {
  542. printk("%s: lost sync.\n",
  543. __FUNCTION__);
  544. ttusb->insync = 0;
  545. }
  546. #endif
  547. }
  548. break;
  549. case 3:
  550. ttusb->insync = 1;
  551. len--;
  552. ttusb->mux_npacks = *data++;
  553. ++ttusb->mux_state;
  554. ttusb->muxpack_ptr = 0;
  555. /* maximum bytes, until we know the length */
  556. ttusb->muxpack_len = 2;
  557. break;
  558. case 4:
  559. {
  560. int avail;
  561. avail = len;
  562. if (avail >
  563. (ttusb->muxpack_len -
  564. ttusb->muxpack_ptr))
  565. avail =
  566. ttusb->muxpack_len -
  567. ttusb->muxpack_ptr;
  568. memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
  569. data, avail);
  570. ttusb->muxpack_ptr += avail;
  571. if (ttusb->muxpack_ptr > 264)
  572. BUG();
  573. data += avail;
  574. len -= avail;
  575. /* determine length */
  576. if (ttusb->muxpack_ptr == 2) {
  577. if (ttusb->muxpack[0] & 0x80) {
  578. ttusb->muxpack_len =
  579. ttusb->muxpack[1] + 2;
  580. if (ttusb->
  581. muxpack[0] & 0x20)
  582. ttusb->
  583. muxpack_len++;
  584. if ((!!
  585. (ttusb->
  586. muxpack[0] & 0x20)) ^
  587. !!(ttusb->
  588. muxpack[1] & 1))
  589. ttusb->
  590. muxpack_len++;
  591. ttusb->muxpack_len += 4;
  592. } else if (ttusb->muxpack[0] ==
  593. 0x47)
  594. ttusb->muxpack_len =
  595. 188 + 4;
  596. else if (ttusb->muxpack[0] == 0x00)
  597. ttusb->muxpack_len =
  598. ttusb->muxpack[1] + 2 +
  599. 4;
  600. else {
  601. dprintk
  602. ("%s: invalid state: first byte is %x\n",
  603. __FUNCTION__,
  604. ttusb->muxpack[0]);
  605. ttusb->mux_state = 0;
  606. }
  607. }
  608. /**
  609. * if length is valid and we reached the end:
  610. * goto next muxpack
  611. */
  612. if ((ttusb->muxpack_ptr >= 2) &&
  613. (ttusb->muxpack_ptr ==
  614. ttusb->muxpack_len)) {
  615. ttusb_process_muxpack(ttusb,
  616. ttusb->
  617. muxpack,
  618. ttusb->
  619. muxpack_ptr);
  620. ttusb->muxpack_ptr = 0;
  621. /* maximum bytes, until we know the length */
  622. ttusb->muxpack_len = 2;
  623. /**
  624. * no muxpacks left?
  625. * return to search-sync state
  626. */
  627. if (!ttusb->mux_npacks--) {
  628. ttusb->mux_state = 0;
  629. break;
  630. }
  631. }
  632. break;
  633. }
  634. default:
  635. BUG();
  636. break;
  637. }
  638. }
  639. }
  640. static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
  641. {
  642. struct ttusb *ttusb = urb->context;
  643. if (!ttusb->iso_streaming)
  644. return;
  645. #if 0
  646. printk("%s: status %d, errcount == %d, length == %i\n",
  647. __FUNCTION__,
  648. urb->status, urb->error_count, urb->actual_length);
  649. #endif
  650. if (!urb->status) {
  651. int i;
  652. for (i = 0; i < urb->number_of_packets; ++i) {
  653. struct usb_iso_packet_descriptor *d;
  654. u8 *data;
  655. int len;
  656. numpkt++;
  657. if ((jiffies - lastj) >= HZ) {
  658. #if DEBUG > 2
  659. printk
  660. ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
  661. numpkt * HZ / (jiffies - lastj),
  662. numts, numstuff, numsec, numinvalid,
  663. numts + numstuff + numsec +
  664. numinvalid);
  665. #endif
  666. numts = numstuff = numsec = numinvalid = 0;
  667. lastj = jiffies;
  668. numpkt = 0;
  669. }
  670. d = &urb->iso_frame_desc[i];
  671. data = urb->transfer_buffer + d->offset;
  672. len = d->actual_length;
  673. d->actual_length = 0;
  674. d->status = 0;
  675. ttusb_process_frame(ttusb, data, len);
  676. }
  677. }
  678. usb_submit_urb(urb, GFP_ATOMIC);
  679. }
  680. static void ttusb_free_iso_urbs(struct ttusb *ttusb)
  681. {
  682. int i;
  683. for (i = 0; i < ISO_BUF_COUNT; i++)
  684. if (ttusb->iso_urb[i])
  685. usb_free_urb(ttusb->iso_urb[i]);
  686. pci_free_consistent(NULL,
  687. ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
  688. ISO_BUF_COUNT, ttusb->iso_buffer,
  689. ttusb->iso_dma_handle);
  690. }
  691. static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
  692. {
  693. int i;
  694. ttusb->iso_buffer = pci_alloc_consistent(NULL,
  695. ISO_FRAME_SIZE *
  696. FRAMES_PER_ISO_BUF *
  697. ISO_BUF_COUNT,
  698. &ttusb->iso_dma_handle);
  699. memset(ttusb->iso_buffer, 0,
  700. ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
  701. for (i = 0; i < ISO_BUF_COUNT; i++) {
  702. struct urb *urb;
  703. if (!
  704. (urb =
  705. usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
  706. ttusb_free_iso_urbs(ttusb);
  707. return -ENOMEM;
  708. }
  709. ttusb->iso_urb[i] = urb;
  710. }
  711. return 0;
  712. }
  713. static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
  714. {
  715. int i;
  716. for (i = 0; i < ISO_BUF_COUNT; i++)
  717. usb_kill_urb(ttusb->iso_urb[i]);
  718. ttusb->iso_streaming = 0;
  719. }
  720. static int ttusb_start_iso_xfer(struct ttusb *ttusb)
  721. {
  722. int i, j, err, buffer_offset = 0;
  723. if (ttusb->iso_streaming) {
  724. printk("%s: iso xfer already running!\n", __FUNCTION__);
  725. return 0;
  726. }
  727. ttusb->cc = -1;
  728. ttusb->insync = 0;
  729. ttusb->mux_state = 0;
  730. for (i = 0; i < ISO_BUF_COUNT; i++) {
  731. int frame_offset = 0;
  732. struct urb *urb = ttusb->iso_urb[i];
  733. urb->dev = ttusb->dev;
  734. urb->context = ttusb;
  735. urb->complete = ttusb_iso_irq;
  736. urb->pipe = ttusb->isoc_in_pipe;
  737. urb->transfer_flags = URB_ISO_ASAP;
  738. urb->interval = 1;
  739. urb->number_of_packets = FRAMES_PER_ISO_BUF;
  740. urb->transfer_buffer_length =
  741. ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
  742. urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
  743. buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
  744. for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
  745. urb->iso_frame_desc[j].offset = frame_offset;
  746. urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
  747. frame_offset += ISO_FRAME_SIZE;
  748. }
  749. }
  750. for (i = 0; i < ISO_BUF_COUNT; i++) {
  751. if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
  752. ttusb_stop_iso_xfer(ttusb);
  753. printk
  754. ("%s: failed urb submission (%i: err = %i)!\n",
  755. __FUNCTION__, i, err);
  756. return err;
  757. }
  758. }
  759. ttusb->iso_streaming = 1;
  760. return 0;
  761. }
  762. #ifdef TTUSB_HWSECTIONS
  763. static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
  764. int len)
  765. {
  766. dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
  767. }
  768. static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
  769. int len)
  770. {
  771. // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
  772. #error TODO: handle ugly stuff
  773. // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
  774. }
  775. #endif
  776. static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  777. {
  778. struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
  779. int feed_type = 1;
  780. dprintk("ttusb_start_feed\n");
  781. switch (dvbdmxfeed->type) {
  782. case DMX_TYPE_TS:
  783. break;
  784. case DMX_TYPE_SEC:
  785. break;
  786. default:
  787. return -EINVAL;
  788. }
  789. if (dvbdmxfeed->type == DMX_TYPE_TS) {
  790. switch (dvbdmxfeed->pes_type) {
  791. case DMX_TS_PES_VIDEO:
  792. case DMX_TS_PES_AUDIO:
  793. case DMX_TS_PES_TELETEXT:
  794. case DMX_TS_PES_PCR:
  795. case DMX_TS_PES_OTHER:
  796. break;
  797. default:
  798. return -EINVAL;
  799. }
  800. }
  801. #ifdef TTUSB_HWSECTIONS
  802. #error TODO: allocate filters
  803. if (dvbdmxfeed->type == DMX_TYPE_TS) {
  804. feed_type = 1;
  805. } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
  806. feed_type = 2;
  807. }
  808. #endif
  809. ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
  810. if (0 == ttusb->running_feed_count++)
  811. ttusb_start_iso_xfer(ttusb);
  812. return 0;
  813. }
  814. static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  815. {
  816. struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
  817. ttusb_del_channel(ttusb, dvbdmxfeed->index);
  818. if (--ttusb->running_feed_count == 0)
  819. ttusb_stop_iso_xfer(ttusb);
  820. return 0;
  821. }
  822. static int ttusb_setup_interfaces(struct ttusb *ttusb)
  823. {
  824. usb_set_interface(ttusb->dev, 1, 1);
  825. ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
  826. ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
  827. ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
  828. return 0;
  829. }
  830. #if 0
  831. static u8 stc_firmware[8192];
  832. static int stc_open(struct inode *inode, struct file *file)
  833. {
  834. struct ttusb *ttusb = file->private_data;
  835. int addr;
  836. for (addr = 0; addr < 8192; addr += 16) {
  837. u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
  838. ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
  839. 16);
  840. }
  841. return 0;
  842. }
  843. static ssize_t stc_read(struct file *file, char *buf, size_t count,
  844. loff_t * offset)
  845. {
  846. int tc = count;
  847. if ((tc + *offset) > 8192)
  848. tc = 8192 - *offset;
  849. if (tc < 0)
  850. return 0;
  851. if (copy_to_user(buf, stc_firmware + *offset, tc))
  852. return -EFAULT;
  853. *offset += tc;
  854. return tc;
  855. }
  856. static int stc_release(struct inode *inode, struct file *file)
  857. {
  858. return 0;
  859. }
  860. static struct file_operations stc_fops = {
  861. .owner = THIS_MODULE,
  862. .read = stc_read,
  863. .open = stc_open,
  864. .release = stc_release,
  865. };
  866. #endif
  867. static u32 functionality(struct i2c_adapter *adapter)
  868. {
  869. return I2C_FUNC_I2C;
  870. }
  871. static int alps_tdmb7_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
  872. {
  873. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  874. u8 data[4];
  875. struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
  876. u32 div;
  877. div = (params->frequency + 36166667) / 166667;
  878. data[0] = (div >> 8) & 0x7f;
  879. data[1] = div & 0xff;
  880. data[2] = ((div >> 10) & 0x60) | 0x85;
  881. data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
  882. if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
  883. return 0;
  884. }
  885. static struct cx22700_config alps_tdmb7_config = {
  886. .demod_address = 0x43,
  887. .pll_set = alps_tdmb7_pll_set,
  888. };
  889. static int philips_tdm1316l_pll_init(struct dvb_frontend* fe)
  890. {
  891. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  892. static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
  893. static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
  894. struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
  895. // setup PLL configuration
  896. if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
  897. msleep(1);
  898. // disable the mc44BC374c (do not check for errors)
  899. tuner_msg.addr = 0x65;
  900. tuner_msg.buf = disable_mc44BC374c;
  901. tuner_msg.len = sizeof(disable_mc44BC374c);
  902. if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
  903. i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
  904. }
  905. return 0;
  906. }
  907. static int philips_tdm1316l_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
  908. {
  909. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  910. u8 tuner_buf[4];
  911. struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
  912. int tuner_frequency = 0;
  913. u8 band, cp, filter;
  914. // determine charge pump
  915. tuner_frequency = params->frequency + 36130000;
  916. if (tuner_frequency < 87000000) return -EINVAL;
  917. else if (tuner_frequency < 130000000) cp = 3;
  918. else if (tuner_frequency < 160000000) cp = 5;
  919. else if (tuner_frequency < 200000000) cp = 6;
  920. else if (tuner_frequency < 290000000) cp = 3;
  921. else if (tuner_frequency < 420000000) cp = 5;
  922. else if (tuner_frequency < 480000000) cp = 6;
  923. else if (tuner_frequency < 620000000) cp = 3;
  924. else if (tuner_frequency < 830000000) cp = 5;
  925. else if (tuner_frequency < 895000000) cp = 7;
  926. else return -EINVAL;
  927. // determine band
  928. if (params->frequency < 49000000) return -EINVAL;
  929. else if (params->frequency < 159000000) band = 1;
  930. else if (params->frequency < 444000000) band = 2;
  931. else if (params->frequency < 861000000) band = 4;
  932. else return -EINVAL;
  933. // setup PLL filter
  934. switch (params->u.ofdm.bandwidth) {
  935. case BANDWIDTH_6_MHZ:
  936. tda1004x_write_byte(fe, 0x0C, 0);
  937. filter = 0;
  938. break;
  939. case BANDWIDTH_7_MHZ:
  940. tda1004x_write_byte(fe, 0x0C, 0);
  941. filter = 0;
  942. break;
  943. case BANDWIDTH_8_MHZ:
  944. tda1004x_write_byte(fe, 0x0C, 0xFF);
  945. filter = 1;
  946. break;
  947. default:
  948. return -EINVAL;
  949. }
  950. // calculate divisor
  951. // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
  952. tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
  953. // setup tuner buffer
  954. tuner_buf[0] = tuner_frequency >> 8;
  955. tuner_buf[1] = tuner_frequency & 0xff;
  956. tuner_buf[2] = 0xca;
  957. tuner_buf[3] = (cp << 5) | (filter << 3) | band;
  958. if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
  959. return -EIO;
  960. msleep(1);
  961. return 0;
  962. }
  963. static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
  964. {
  965. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  966. return request_firmware(fw, name, &ttusb->dev->dev);
  967. }
  968. static struct tda1004x_config philips_tdm1316l_config = {
  969. .demod_address = 0x8,
  970. .invert = 1,
  971. .invert_oclk = 0,
  972. .pll_init = philips_tdm1316l_pll_init,
  973. .pll_set = philips_tdm1316l_pll_set,
  974. .request_firmware = philips_tdm1316l_request_firmware,
  975. };
  976. static u8 alps_bsbe1_inittab[] = {
  977. 0x01, 0x15,
  978. 0x02, 0x30,
  979. 0x03, 0x00,
  980. 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
  981. 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
  982. 0x06, 0x40, /* DAC not used, set to high impendance mode */
  983. 0x07, 0x00, /* DAC LSB */
  984. 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
  985. 0x09, 0x00, /* FIFO */
  986. 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
  987. 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
  988. 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
  989. 0x10, 0x3f, // AGC2 0x3d
  990. 0x11, 0x84,
  991. 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
  992. 0x15, 0xc9, // lock detector threshold
  993. 0x16, 0x00,
  994. 0x17, 0x00,
  995. 0x18, 0x00,
  996. 0x19, 0x00,
  997. 0x1a, 0x00,
  998. 0x1f, 0x50,
  999. 0x20, 0x00,
  1000. 0x21, 0x00,
  1001. 0x22, 0x00,
  1002. 0x23, 0x00,
  1003. 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
  1004. 0x29, 0x1e, // 1/2 threshold
  1005. 0x2a, 0x14, // 2/3 threshold
  1006. 0x2b, 0x0f, // 3/4 threshold
  1007. 0x2c, 0x09, // 5/6 threshold
  1008. 0x2d, 0x05, // 7/8 threshold
  1009. 0x2e, 0x01,
  1010. 0x31, 0x1f, // test all FECs
  1011. 0x32, 0x19, // viterbi and synchro search
  1012. 0x33, 0xfc, // rs control
  1013. 0x34, 0x93, // error control
  1014. 0x0f, 0x92,
  1015. 0xff, 0xff
  1016. };
  1017. static u8 alps_bsru6_inittab[] = {
  1018. 0x01, 0x15,
  1019. 0x02, 0x30,
  1020. 0x03, 0x00,
  1021. 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
  1022. 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
  1023. 0x06, 0x40, /* DAC not used, set to high impendance mode */
  1024. 0x07, 0x00, /* DAC LSB */
  1025. 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
  1026. 0x09, 0x00, /* FIFO */
  1027. 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
  1028. 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
  1029. 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
  1030. 0x10, 0x3f, // AGC2 0x3d
  1031. 0x11, 0x84,
  1032. 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
  1033. 0x15, 0xc9, // lock detector threshold
  1034. 0x16, 0x00,
  1035. 0x17, 0x00,
  1036. 0x18, 0x00,
  1037. 0x19, 0x00,
  1038. 0x1a, 0x00,
  1039. 0x1f, 0x50,
  1040. 0x20, 0x00,
  1041. 0x21, 0x00,
  1042. 0x22, 0x00,
  1043. 0x23, 0x00,
  1044. 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
  1045. 0x29, 0x1e, // 1/2 threshold
  1046. 0x2a, 0x14, // 2/3 threshold
  1047. 0x2b, 0x0f, // 3/4 threshold
  1048. 0x2c, 0x09, // 5/6 threshold
  1049. 0x2d, 0x05, // 7/8 threshold
  1050. 0x2e, 0x01,
  1051. 0x31, 0x1f, // test all FECs
  1052. 0x32, 0x19, // viterbi and synchro search
  1053. 0x33, 0xfc, // rs control
  1054. 0x34, 0x93, // error control
  1055. 0x0f, 0x52,
  1056. 0xff, 0xff
  1057. };
  1058. static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
  1059. {
  1060. u8 aclk = 0;
  1061. u8 bclk = 0;
  1062. if (srate < 1500000) {
  1063. aclk = 0xb7;
  1064. bclk = 0x47;
  1065. } else if (srate < 3000000) {
  1066. aclk = 0xb7;
  1067. bclk = 0x4b;
  1068. } else if (srate < 7000000) {
  1069. aclk = 0xb7;
  1070. bclk = 0x4f;
  1071. } else if (srate < 14000000) {
  1072. aclk = 0xb7;
  1073. bclk = 0x53;
  1074. } else if (srate < 30000000) {
  1075. aclk = 0xb6;
  1076. bclk = 0x53;
  1077. } else if (srate < 45000000) {
  1078. aclk = 0xb4;
  1079. bclk = 0x51;
  1080. }
  1081. stv0299_writereg(fe, 0x13, aclk);
  1082. stv0299_writereg(fe, 0x14, bclk);
  1083. stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
  1084. stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
  1085. stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
  1086. return 0;
  1087. }
  1088. static int philips_tsa5059_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
  1089. {
  1090. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  1091. u8 buf[4];
  1092. u32 div;
  1093. struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
  1094. if ((params->frequency < 950000) || (params->frequency > 2150000))
  1095. return -EINVAL;
  1096. div = (params->frequency + (125 - 1)) / 125; // round correctly
  1097. buf[0] = (div >> 8) & 0x7f;
  1098. buf[1] = div & 0xff;
  1099. buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
  1100. buf[3] = 0xC4;
  1101. if (params->frequency > 1530000)
  1102. buf[3] = 0xC0;
  1103. /* BSBE1 wants XCE bit set */
  1104. if (ttusb->revision == TTUSB_REV_2_2)
  1105. buf[3] |= 0x20;
  1106. if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
  1107. return -EIO;
  1108. return 0;
  1109. }
  1110. static struct stv0299_config alps_stv0299_config = {
  1111. .demod_address = 0x68,
  1112. .inittab = alps_bsru6_inittab,
  1113. .mclk = 88000000UL,
  1114. .invert = 1,
  1115. .enhanced_tuning = 0,
  1116. .skip_reinit = 0,
  1117. .lock_output = STV0229_LOCKOUTPUT_1,
  1118. .volt13_op0_op1 = STV0299_VOLT13_OP1,
  1119. .min_delay_ms = 100,
  1120. .set_symbol_rate = alps_stv0299_set_symbol_rate,
  1121. .pll_set = philips_tsa5059_pll_set,
  1122. };
  1123. static int ttusb_novas_grundig_29504_491_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
  1124. {
  1125. struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
  1126. u8 buf[4];
  1127. u32 div;
  1128. struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
  1129. div = params->frequency / 125;
  1130. buf[0] = (div >> 8) & 0x7f;
  1131. buf[1] = div & 0xff;
  1132. buf[2] = 0x8e;
  1133. buf[3] = 0x00;
  1134. if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
  1135. return -EIO;
  1136. return 0;
  1137. }
  1138. static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
  1139. .demod_address = 0x68,
  1140. .pll_set = ttusb_novas_grundig_29504_491_pll_set,
  1141. };
  1142. static int alps_tdbe2_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
  1143. {
  1144. struct ttusb* ttusb = fe->dvb->priv;
  1145. u32 div;
  1146. u8 data[4];
  1147. struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
  1148. div = (params->frequency + 35937500 + 31250) / 62500;
  1149. data[0] = (div >> 8) & 0x7f;
  1150. data[1] = div & 0xff;
  1151. data[2] = 0x85 | ((div >> 10) & 0x60);
  1152. data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
  1153. if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
  1154. return -EIO;
  1155. return 0;
  1156. }
  1157. static struct ves1820_config alps_tdbe2_config = {
  1158. .demod_address = 0x09,
  1159. .xin = 57840000UL,
  1160. .invert = 1,
  1161. .selagc = VES1820_SELAGC_SIGNAMPERR,
  1162. .pll_set = alps_tdbe2_pll_set,
  1163. };
  1164. static u8 read_pwm(struct ttusb* ttusb)
  1165. {
  1166. u8 b = 0xff;
  1167. u8 pwm;
  1168. struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
  1169. { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
  1170. if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
  1171. pwm = 0x48;
  1172. return pwm;
  1173. }
  1174. static void frontend_init(struct ttusb* ttusb)
  1175. {
  1176. switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
  1177. case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
  1178. // try the stv0299 based first
  1179. ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
  1180. if (ttusb->fe != NULL) {
  1181. if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
  1182. alps_stv0299_config.inittab = alps_bsbe1_inittab;
  1183. ttusb->fe->ops->set_voltage = lnbp21_set_voltage;
  1184. } else { // ALPS BSRU6
  1185. ttusb->fe->ops->set_voltage = ttusb_set_voltage;
  1186. }
  1187. break;
  1188. }
  1189. // Grundig 29504-491
  1190. ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
  1191. if (ttusb->fe != NULL) {
  1192. ttusb->fe->ops->set_voltage = ttusb_set_voltage;
  1193. break;
  1194. }
  1195. break;
  1196. case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
  1197. ttusb->fe = ves1820_attach(&alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
  1198. if (ttusb->fe != NULL)
  1199. break;
  1200. break;
  1201. case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
  1202. // try the ALPS TDMB7 first
  1203. ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
  1204. if (ttusb->fe != NULL)
  1205. break;
  1206. // Philips td1316
  1207. ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
  1208. if (ttusb->fe != NULL)
  1209. break;
  1210. break;
  1211. }
  1212. if (ttusb->fe == NULL) {
  1213. printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
  1214. le16_to_cpu(ttusb->dev->descriptor.idVendor),
  1215. le16_to_cpu(ttusb->dev->descriptor.idProduct));
  1216. } else {
  1217. if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
  1218. printk("dvb-ttusb-budget: Frontend registration failed!\n");
  1219. if (ttusb->fe->ops->release)
  1220. ttusb->fe->ops->release(ttusb->fe);
  1221. ttusb->fe = NULL;
  1222. }
  1223. }
  1224. }
  1225. static struct i2c_algorithm ttusb_dec_algo = {
  1226. .master_xfer = master_xfer,
  1227. .functionality = functionality,
  1228. };
  1229. static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1230. {
  1231. struct usb_device *udev;
  1232. struct ttusb *ttusb;
  1233. int result;
  1234. dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
  1235. udev = interface_to_usbdev(intf);
  1236. if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
  1237. if (!(ttusb = kmalloc(sizeof(struct ttusb), GFP_KERNEL)))
  1238. return -ENOMEM;
  1239. memset(ttusb, 0, sizeof(struct ttusb));
  1240. ttusb->dev = udev;
  1241. ttusb->c = 0;
  1242. ttusb->mux_state = 0;
  1243. sema_init(&ttusb->semi2c, 0);
  1244. sema_init(&ttusb->semusb, 1);
  1245. ttusb_setup_interfaces(ttusb);
  1246. ttusb_alloc_iso_urbs(ttusb);
  1247. if (ttusb_init_controller(ttusb))
  1248. printk("ttusb_init_controller: error\n");
  1249. up(&ttusb->semi2c);
  1250. dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
  1251. ttusb->adapter.priv = ttusb;
  1252. /* i2c */
  1253. memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
  1254. strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
  1255. i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
  1256. #ifdef I2C_ADAP_CLASS_TV_DIGITAL
  1257. ttusb->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
  1258. #else
  1259. ttusb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
  1260. #endif
  1261. ttusb->i2c_adap.algo = &ttusb_dec_algo;
  1262. ttusb->i2c_adap.algo_data = NULL;
  1263. result = i2c_add_adapter(&ttusb->i2c_adap);
  1264. if (result) {
  1265. dvb_unregister_adapter (&ttusb->adapter);
  1266. return result;
  1267. }
  1268. memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
  1269. ttusb->dvb_demux.dmx.capabilities =
  1270. DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  1271. ttusb->dvb_demux.priv = NULL;
  1272. #ifdef TTUSB_HWSECTIONS
  1273. ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
  1274. #else
  1275. ttusb->dvb_demux.filternum = 32;
  1276. #endif
  1277. ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
  1278. ttusb->dvb_demux.start_feed = ttusb_start_feed;
  1279. ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
  1280. ttusb->dvb_demux.write_to_decoder = NULL;
  1281. if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
  1282. printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
  1283. i2c_del_adapter(&ttusb->i2c_adap);
  1284. dvb_unregister_adapter (&ttusb->adapter);
  1285. return -ENODEV;
  1286. }
  1287. //FIXME dmxdev (nur WAS?)
  1288. ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
  1289. ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
  1290. ttusb->dmxdev.capabilities = 0;
  1291. if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
  1292. printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
  1293. result);
  1294. dvb_dmx_release(&ttusb->dvb_demux);
  1295. i2c_del_adapter(&ttusb->i2c_adap);
  1296. dvb_unregister_adapter (&ttusb->adapter);
  1297. return -ENODEV;
  1298. }
  1299. if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
  1300. printk("ttusb_dvb: dvb_net_init failed!\n");
  1301. dvb_dmxdev_release(&ttusb->dmxdev);
  1302. dvb_dmx_release(&ttusb->dvb_demux);
  1303. i2c_del_adapter(&ttusb->i2c_adap);
  1304. dvb_unregister_adapter (&ttusb->adapter);
  1305. return -ENODEV;
  1306. }
  1307. #if 0
  1308. ttusb->stc_devfs_handle =
  1309. devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
  1310. DEVFS_FL_DEFAULT, 0, 192,
  1311. S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
  1312. | S_IROTH | S_IWOTH, &stc_fops, ttusb);
  1313. #endif
  1314. usb_set_intfdata(intf, (void *) ttusb);
  1315. frontend_init(ttusb);
  1316. return 0;
  1317. }
  1318. static void ttusb_disconnect(struct usb_interface *intf)
  1319. {
  1320. struct ttusb *ttusb = usb_get_intfdata(intf);
  1321. usb_set_intfdata(intf, NULL);
  1322. ttusb->disconnecting = 1;
  1323. ttusb_stop_iso_xfer(ttusb);
  1324. ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
  1325. dvb_net_release(&ttusb->dvbnet);
  1326. dvb_dmxdev_release(&ttusb->dmxdev);
  1327. dvb_dmx_release(&ttusb->dvb_demux);
  1328. if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
  1329. i2c_del_adapter(&ttusb->i2c_adap);
  1330. dvb_unregister_adapter(&ttusb->adapter);
  1331. ttusb_free_iso_urbs(ttusb);
  1332. kfree(ttusb);
  1333. dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
  1334. }
  1335. static struct usb_device_id ttusb_table[] = {
  1336. {USB_DEVICE(0xb48, 0x1003)},
  1337. {USB_DEVICE(0xb48, 0x1004)},
  1338. {USB_DEVICE(0xb48, 0x1005)},
  1339. {}
  1340. };
  1341. MODULE_DEVICE_TABLE(usb, ttusb_table);
  1342. static struct usb_driver ttusb_driver = {
  1343. .name = "ttusb",
  1344. .probe = ttusb_probe,
  1345. .disconnect = ttusb_disconnect,
  1346. .id_table = ttusb_table,
  1347. };
  1348. static int __init ttusb_init(void)
  1349. {
  1350. int err;
  1351. if ((err = usb_register(&ttusb_driver)) < 0) {
  1352. printk("%s: usb_register failed! Error number %d",
  1353. __FILE__, err);
  1354. return err;
  1355. }
  1356. return 0;
  1357. }
  1358. static void __exit ttusb_exit(void)
  1359. {
  1360. usb_deregister(&ttusb_driver);
  1361. }
  1362. module_init(ttusb_init);
  1363. module_exit(ttusb_exit);
  1364. MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
  1365. MODULE_DESCRIPTION("TTUSB DVB Driver");
  1366. MODULE_LICENSE("GPL");