ttusb_dec.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. /*
  2. * TTUSB DEC Driver
  3. *
  4. * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
  5. * IR support by Peter Beutner <p.beutner@gmx.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/usb.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/firmware.h>
  26. #include <linux/crc32.h>
  27. #include <linux/init.h>
  28. #include <linux/input.h>
  29. #include <linux/mutex.h>
  30. #include <media/dmxdev.h>
  31. #include <media/dvb_demux.h>
  32. #include <media/dvb_frontend.h>
  33. #include <media/dvb_net.h>
  34. #include "ttusbdecfe.h"
  35. static int debug;
  36. static int output_pva;
  37. static int enable_rc;
  38. module_param(debug, int, 0644);
  39. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  40. module_param(output_pva, int, 0444);
  41. MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)");
  42. module_param(enable_rc, int, 0644);
  43. MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)");
  44. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  45. #define dprintk if (debug) printk
  46. #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
  47. #define COMMAND_PIPE 0x03
  48. #define RESULT_PIPE 0x04
  49. #define IN_PIPE 0x08
  50. #define OUT_PIPE 0x07
  51. #define IRQ_PIPE 0x0A
  52. #define COMMAND_PACKET_SIZE 0x3c
  53. #define ARM_PACKET_SIZE 0x1000
  54. #define IRQ_PACKET_SIZE 0x8
  55. #define ISO_BUF_COUNT 0x04
  56. #define FRAMES_PER_ISO_BUF 0x04
  57. #define ISO_FRAME_SIZE 0x0380
  58. #define MAX_PVA_LENGTH 6144
  59. enum ttusb_dec_model {
  60. TTUSB_DEC2000T,
  61. TTUSB_DEC2540T,
  62. TTUSB_DEC3000S
  63. };
  64. enum ttusb_dec_packet_type {
  65. TTUSB_DEC_PACKET_PVA,
  66. TTUSB_DEC_PACKET_SECTION,
  67. TTUSB_DEC_PACKET_EMPTY
  68. };
  69. enum ttusb_dec_interface {
  70. TTUSB_DEC_INTERFACE_INITIAL,
  71. TTUSB_DEC_INTERFACE_IN,
  72. TTUSB_DEC_INTERFACE_OUT
  73. };
  74. typedef int (dvb_filter_pes2ts_cb_t) (void *, unsigned char *);
  75. struct dvb_filter_pes2ts {
  76. unsigned char buf[188];
  77. unsigned char cc;
  78. dvb_filter_pes2ts_cb_t *cb;
  79. void *priv;
  80. };
  81. struct ttusb_dec {
  82. enum ttusb_dec_model model;
  83. char *model_name;
  84. char *firmware_name;
  85. int can_playback;
  86. /* DVB bits */
  87. struct dvb_adapter adapter;
  88. struct dmxdev dmxdev;
  89. struct dvb_demux demux;
  90. struct dmx_frontend frontend;
  91. struct dvb_net dvb_net;
  92. struct dvb_frontend* fe;
  93. u16 pid[DMX_PES_OTHER];
  94. /* USB bits */
  95. struct usb_device *udev;
  96. u8 trans_count;
  97. unsigned int command_pipe;
  98. unsigned int result_pipe;
  99. unsigned int in_pipe;
  100. unsigned int out_pipe;
  101. unsigned int irq_pipe;
  102. enum ttusb_dec_interface interface;
  103. struct mutex usb_mutex;
  104. void *irq_buffer;
  105. struct urb *irq_urb;
  106. dma_addr_t irq_dma_handle;
  107. void *iso_buffer;
  108. struct urb *iso_urb[ISO_BUF_COUNT];
  109. int iso_stream_count;
  110. struct mutex iso_mutex;
  111. u8 packet[MAX_PVA_LENGTH + 4];
  112. enum ttusb_dec_packet_type packet_type;
  113. int packet_state;
  114. int packet_length;
  115. int packet_payload_length;
  116. u16 next_packet_id;
  117. int pva_stream_count;
  118. int filter_stream_count;
  119. struct dvb_filter_pes2ts a_pes2ts;
  120. struct dvb_filter_pes2ts v_pes2ts;
  121. u8 v_pes[16 + MAX_PVA_LENGTH];
  122. int v_pes_length;
  123. int v_pes_postbytes;
  124. struct list_head urb_frame_list;
  125. struct tasklet_struct urb_tasklet;
  126. spinlock_t urb_frame_list_lock;
  127. struct dvb_demux_filter *audio_filter;
  128. struct dvb_demux_filter *video_filter;
  129. struct list_head filter_info_list;
  130. spinlock_t filter_info_list_lock;
  131. struct input_dev *rc_input_dev;
  132. char rc_phys[64];
  133. int active; /* Loaded successfully */
  134. };
  135. struct urb_frame {
  136. u8 data[ISO_FRAME_SIZE];
  137. int length;
  138. struct list_head urb_frame_list;
  139. };
  140. struct filter_info {
  141. u8 stream_id;
  142. struct dvb_demux_filter *filter;
  143. struct list_head filter_info_list;
  144. };
  145. static u16 rc_keys[] = {
  146. KEY_POWER,
  147. KEY_MUTE,
  148. KEY_1,
  149. KEY_2,
  150. KEY_3,
  151. KEY_4,
  152. KEY_5,
  153. KEY_6,
  154. KEY_7,
  155. KEY_8,
  156. KEY_9,
  157. KEY_0,
  158. KEY_CHANNELUP,
  159. KEY_VOLUMEDOWN,
  160. KEY_OK,
  161. KEY_VOLUMEUP,
  162. KEY_CHANNELDOWN,
  163. KEY_PREVIOUS,
  164. KEY_ESC,
  165. KEY_RED,
  166. KEY_GREEN,
  167. KEY_YELLOW,
  168. KEY_BLUE,
  169. KEY_OPTION,
  170. KEY_M,
  171. KEY_RADIO
  172. };
  173. static void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts,
  174. unsigned short pid,
  175. dvb_filter_pes2ts_cb_t *cb, void *priv)
  176. {
  177. unsigned char *buf=p2ts->buf;
  178. buf[0]=0x47;
  179. buf[1]=(pid>>8);
  180. buf[2]=pid&0xff;
  181. p2ts->cc=0;
  182. p2ts->cb=cb;
  183. p2ts->priv=priv;
  184. }
  185. static int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts,
  186. unsigned char *pes, int len, int payload_start)
  187. {
  188. unsigned char *buf=p2ts->buf;
  189. int ret=0, rest;
  190. //len=6+((pes[4]<<8)|pes[5]);
  191. if (payload_start)
  192. buf[1]|=0x40;
  193. else
  194. buf[1]&=~0x40;
  195. while (len>=184) {
  196. buf[3]=0x10|((p2ts->cc++)&0x0f);
  197. memcpy(buf+4, pes, 184);
  198. if ((ret=p2ts->cb(p2ts->priv, buf)))
  199. return ret;
  200. len-=184; pes+=184;
  201. buf[1]&=~0x40;
  202. }
  203. if (!len)
  204. return 0;
  205. buf[3]=0x30|((p2ts->cc++)&0x0f);
  206. rest=183-len;
  207. if (rest) {
  208. buf[5]=0x00;
  209. if (rest-1)
  210. memset(buf+6, 0xff, rest-1);
  211. }
  212. buf[4]=rest;
  213. memcpy(buf+5+rest, pes, len);
  214. return p2ts->cb(p2ts->priv, buf);
  215. }
  216. static void ttusb_dec_set_model(struct ttusb_dec *dec,
  217. enum ttusb_dec_model model);
  218. static void ttusb_dec_handle_irq( struct urb *urb)
  219. {
  220. struct ttusb_dec *dec = urb->context;
  221. char *buffer = dec->irq_buffer;
  222. int retval;
  223. switch(urb->status) {
  224. case 0: /*success*/
  225. break;
  226. case -ECONNRESET:
  227. case -ENOENT:
  228. case -ESHUTDOWN:
  229. case -ETIME:
  230. /* this urb is dead, cleanup */
  231. dprintk("%s:urb shutting down with status: %d\n",
  232. __func__, urb->status);
  233. return;
  234. default:
  235. dprintk("%s:nonzero status received: %d\n",
  236. __func__,urb->status);
  237. goto exit;
  238. }
  239. if ((buffer[0] == 0x1) && (buffer[2] == 0x15)) {
  240. /*
  241. * IR - Event
  242. *
  243. * this is an fact a bit too simple implementation;
  244. * the box also reports a keyrepeat signal
  245. * (with buffer[3] == 0x40) in an intervall of ~100ms.
  246. * But to handle this correctly we had to imlemenent some
  247. * kind of timer which signals a 'key up' event if no
  248. * keyrepeat signal is received for lets say 200ms.
  249. * this should/could be added later ...
  250. * for now lets report each signal as a key down and up
  251. */
  252. if (buffer[4] - 1 < ARRAY_SIZE(rc_keys)) {
  253. dprintk("%s:rc signal:%d\n", __func__, buffer[4]);
  254. input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1);
  255. input_sync(dec->rc_input_dev);
  256. input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0);
  257. input_sync(dec->rc_input_dev);
  258. }
  259. }
  260. exit:
  261. retval = usb_submit_urb(urb, GFP_ATOMIC);
  262. if (retval)
  263. printk("%s - usb_commit_urb failed with result: %d\n",
  264. __func__, retval);
  265. }
  266. static u16 crc16(u16 crc, const u8 *buf, size_t len)
  267. {
  268. u16 tmp;
  269. while (len--) {
  270. crc ^= *buf++;
  271. crc ^= (u8)crc >> 4;
  272. tmp = (u8)crc;
  273. crc ^= (tmp ^ (tmp << 1)) << 4;
  274. }
  275. return crc;
  276. }
  277. static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
  278. int param_length, const u8 params[],
  279. int *result_length, u8 cmd_result[])
  280. {
  281. int result, actual_len;
  282. u8 *b;
  283. dprintk("%s\n", __func__);
  284. b = kzalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
  285. if (!b)
  286. return -ENOMEM;
  287. if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
  288. kfree(b);
  289. printk("%s: Failed to lock usb mutex.\n", __func__);
  290. return result;
  291. }
  292. b[0] = 0xaa;
  293. b[1] = ++dec->trans_count;
  294. b[2] = command;
  295. b[3] = param_length;
  296. if (params)
  297. memcpy(&b[4], params, param_length);
  298. if (debug) {
  299. printk(KERN_DEBUG "%s: command: %*ph\n",
  300. __func__, param_length, b);
  301. }
  302. result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
  303. COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
  304. if (result) {
  305. printk("%s: command bulk message failed: error %d\n",
  306. __func__, result);
  307. mutex_unlock(&dec->usb_mutex);
  308. kfree(b);
  309. return result;
  310. }
  311. result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
  312. COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
  313. if (result) {
  314. printk("%s: result bulk message failed: error %d\n",
  315. __func__, result);
  316. mutex_unlock(&dec->usb_mutex);
  317. kfree(b);
  318. return result;
  319. } else {
  320. if (debug) {
  321. printk(KERN_DEBUG "%s: result: %*ph\n",
  322. __func__, actual_len, b);
  323. }
  324. if (result_length)
  325. *result_length = b[3];
  326. if (cmd_result && b[3] > 0)
  327. memcpy(cmd_result, &b[4], b[3]);
  328. mutex_unlock(&dec->usb_mutex);
  329. kfree(b);
  330. return 0;
  331. }
  332. }
  333. static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
  334. unsigned int *model, unsigned int *version)
  335. {
  336. u8 c[COMMAND_PACKET_SIZE];
  337. int c_length;
  338. int result;
  339. __be32 tmp;
  340. dprintk("%s\n", __func__);
  341. result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
  342. if (result)
  343. return result;
  344. if (c_length >= 0x0c) {
  345. if (mode != NULL) {
  346. memcpy(&tmp, c, 4);
  347. *mode = ntohl(tmp);
  348. }
  349. if (model != NULL) {
  350. memcpy(&tmp, &c[4], 4);
  351. *model = ntohl(tmp);
  352. }
  353. if (version != NULL) {
  354. memcpy(&tmp, &c[8], 4);
  355. *version = ntohl(tmp);
  356. }
  357. return 0;
  358. } else {
  359. return -ENOENT;
  360. }
  361. }
  362. static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
  363. {
  364. struct ttusb_dec *dec = priv;
  365. dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
  366. &dec->audio_filter->feed->feed.ts, NULL);
  367. return 0;
  368. }
  369. static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
  370. {
  371. struct ttusb_dec *dec = priv;
  372. dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
  373. &dec->video_filter->feed->feed.ts, NULL);
  374. return 0;
  375. }
  376. static void ttusb_dec_set_pids(struct ttusb_dec *dec)
  377. {
  378. u8 b[] = { 0x00, 0x00, 0x00, 0x00,
  379. 0x00, 0x00, 0xff, 0xff,
  380. 0xff, 0xff, 0xff, 0xff };
  381. __be16 pcr = htons(dec->pid[DMX_PES_PCR]);
  382. __be16 audio = htons(dec->pid[DMX_PES_AUDIO]);
  383. __be16 video = htons(dec->pid[DMX_PES_VIDEO]);
  384. dprintk("%s\n", __func__);
  385. memcpy(&b[0], &pcr, 2);
  386. memcpy(&b[2], &audio, 2);
  387. memcpy(&b[4], &video, 2);
  388. ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
  389. dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
  390. ttusb_dec_audio_pes2ts_cb, dec);
  391. dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
  392. ttusb_dec_video_pes2ts_cb, dec);
  393. dec->v_pes_length = 0;
  394. dec->v_pes_postbytes = 0;
  395. }
  396. static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
  397. {
  398. if (length < 8) {
  399. printk("%s: packet too short - discarding\n", __func__);
  400. return;
  401. }
  402. if (length > 8 + MAX_PVA_LENGTH) {
  403. printk("%s: packet too long - discarding\n", __func__);
  404. return;
  405. }
  406. switch (pva[2]) {
  407. case 0x01: { /* VideoStream */
  408. int prebytes = pva[5] & 0x03;
  409. int postbytes = (pva[5] & 0x0c) >> 2;
  410. __be16 v_pes_payload_length;
  411. if (output_pva) {
  412. dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
  413. &dec->video_filter->feed->feed.ts, NULL);
  414. return;
  415. }
  416. if (dec->v_pes_postbytes > 0 &&
  417. dec->v_pes_postbytes == prebytes) {
  418. memcpy(&dec->v_pes[dec->v_pes_length],
  419. &pva[12], prebytes);
  420. dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
  421. dec->v_pes_length + prebytes, 1);
  422. }
  423. if (pva[5] & 0x10) {
  424. dec->v_pes[7] = 0x80;
  425. dec->v_pes[8] = 0x05;
  426. dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
  427. dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
  428. ((pva[9] & 0xc0) >> 6);
  429. dec->v_pes[11] = 0x01 |
  430. ((pva[9] & 0x3f) << 2) |
  431. ((pva[10] & 0x80) >> 6);
  432. dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
  433. ((pva[11] & 0xc0) >> 7);
  434. dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
  435. memcpy(&dec->v_pes[14], &pva[12 + prebytes],
  436. length - 12 - prebytes);
  437. dec->v_pes_length = 14 + length - 12 - prebytes;
  438. } else {
  439. dec->v_pes[7] = 0x00;
  440. dec->v_pes[8] = 0x00;
  441. memcpy(&dec->v_pes[9], &pva[8], length - 8);
  442. dec->v_pes_length = 9 + length - 8;
  443. }
  444. dec->v_pes_postbytes = postbytes;
  445. if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
  446. dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
  447. dec->v_pes[11 + dec->v_pes[8]] == 0x01)
  448. dec->v_pes[6] = 0x84;
  449. else
  450. dec->v_pes[6] = 0x80;
  451. v_pes_payload_length = htons(dec->v_pes_length - 6 +
  452. postbytes);
  453. memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
  454. if (postbytes == 0)
  455. dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
  456. dec->v_pes_length, 1);
  457. break;
  458. }
  459. case 0x02: /* MainAudioStream */
  460. if (output_pva) {
  461. dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
  462. &dec->audio_filter->feed->feed.ts, NULL);
  463. return;
  464. }
  465. dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
  466. pva[5] & 0x10);
  467. break;
  468. default:
  469. printk("%s: unknown PVA type: %02x.\n", __func__,
  470. pva[2]);
  471. break;
  472. }
  473. }
  474. static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
  475. int length)
  476. {
  477. struct list_head *item;
  478. struct filter_info *finfo;
  479. struct dvb_demux_filter *filter = NULL;
  480. unsigned long flags;
  481. u8 sid;
  482. sid = packet[1];
  483. spin_lock_irqsave(&dec->filter_info_list_lock, flags);
  484. for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
  485. item = item->next) {
  486. finfo = list_entry(item, struct filter_info, filter_info_list);
  487. if (finfo->stream_id == sid) {
  488. filter = finfo->filter;
  489. break;
  490. }
  491. }
  492. spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
  493. if (filter)
  494. filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
  495. &filter->filter, NULL);
  496. }
  497. static void ttusb_dec_process_packet(struct ttusb_dec *dec)
  498. {
  499. int i;
  500. u16 csum = 0;
  501. u16 packet_id;
  502. if (dec->packet_length % 2) {
  503. printk("%s: odd sized packet - discarding\n", __func__);
  504. return;
  505. }
  506. for (i = 0; i < dec->packet_length; i += 2)
  507. csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
  508. if (csum) {
  509. printk("%s: checksum failed - discarding\n", __func__);
  510. return;
  511. }
  512. packet_id = dec->packet[dec->packet_length - 4] << 8;
  513. packet_id += dec->packet[dec->packet_length - 3];
  514. if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
  515. printk("%s: warning: lost packets between %u and %u\n",
  516. __func__, dec->next_packet_id - 1, packet_id);
  517. }
  518. if (packet_id == 0xffff)
  519. dec->next_packet_id = 0x8000;
  520. else
  521. dec->next_packet_id = packet_id + 1;
  522. switch (dec->packet_type) {
  523. case TTUSB_DEC_PACKET_PVA:
  524. if (dec->pva_stream_count)
  525. ttusb_dec_process_pva(dec, dec->packet,
  526. dec->packet_payload_length);
  527. break;
  528. case TTUSB_DEC_PACKET_SECTION:
  529. if (dec->filter_stream_count)
  530. ttusb_dec_process_filter(dec, dec->packet,
  531. dec->packet_payload_length);
  532. break;
  533. case TTUSB_DEC_PACKET_EMPTY:
  534. break;
  535. }
  536. }
  537. static void swap_bytes(u8 *b, int length)
  538. {
  539. length -= length % 2;
  540. for (; length; b += 2, length -= 2)
  541. swap(*b, *(b + 1));
  542. }
  543. static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
  544. int length)
  545. {
  546. swap_bytes(b, length);
  547. while (length) {
  548. switch (dec->packet_state) {
  549. case 0:
  550. case 1:
  551. case 2:
  552. if (*b++ == 0xaa)
  553. dec->packet_state++;
  554. else
  555. dec->packet_state = 0;
  556. length--;
  557. break;
  558. case 3:
  559. if (*b == 0x00) {
  560. dec->packet_state++;
  561. dec->packet_length = 0;
  562. } else if (*b != 0xaa) {
  563. dec->packet_state = 0;
  564. }
  565. b++;
  566. length--;
  567. break;
  568. case 4:
  569. dec->packet[dec->packet_length++] = *b++;
  570. if (dec->packet_length == 2) {
  571. if (dec->packet[0] == 'A' &&
  572. dec->packet[1] == 'V') {
  573. dec->packet_type =
  574. TTUSB_DEC_PACKET_PVA;
  575. dec->packet_state++;
  576. } else if (dec->packet[0] == 'S') {
  577. dec->packet_type =
  578. TTUSB_DEC_PACKET_SECTION;
  579. dec->packet_state++;
  580. } else if (dec->packet[0] == 0x00) {
  581. dec->packet_type =
  582. TTUSB_DEC_PACKET_EMPTY;
  583. dec->packet_payload_length = 2;
  584. dec->packet_state = 7;
  585. } else {
  586. printk("%s: unknown packet type: %02x%02x\n",
  587. __func__,
  588. dec->packet[0], dec->packet[1]);
  589. dec->packet_state = 0;
  590. }
  591. }
  592. length--;
  593. break;
  594. case 5:
  595. dec->packet[dec->packet_length++] = *b++;
  596. if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
  597. dec->packet_length == 8) {
  598. dec->packet_state++;
  599. dec->packet_payload_length = 8 +
  600. (dec->packet[6] << 8) +
  601. dec->packet[7];
  602. } else if (dec->packet_type ==
  603. TTUSB_DEC_PACKET_SECTION &&
  604. dec->packet_length == 5) {
  605. dec->packet_state++;
  606. dec->packet_payload_length = 5 +
  607. ((dec->packet[3] & 0x0f) << 8) +
  608. dec->packet[4];
  609. }
  610. length--;
  611. break;
  612. case 6: {
  613. int remainder = dec->packet_payload_length -
  614. dec->packet_length;
  615. if (length >= remainder) {
  616. memcpy(dec->packet + dec->packet_length,
  617. b, remainder);
  618. dec->packet_length += remainder;
  619. b += remainder;
  620. length -= remainder;
  621. dec->packet_state++;
  622. } else {
  623. memcpy(&dec->packet[dec->packet_length],
  624. b, length);
  625. dec->packet_length += length;
  626. length = 0;
  627. }
  628. break;
  629. }
  630. case 7: {
  631. int tail = 4;
  632. dec->packet[dec->packet_length++] = *b++;
  633. if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
  634. dec->packet_payload_length % 2)
  635. tail++;
  636. if (dec->packet_length ==
  637. dec->packet_payload_length + tail) {
  638. ttusb_dec_process_packet(dec);
  639. dec->packet_state = 0;
  640. }
  641. length--;
  642. break;
  643. }
  644. default:
  645. printk("%s: illegal packet state encountered.\n",
  646. __func__);
  647. dec->packet_state = 0;
  648. }
  649. }
  650. }
  651. static void ttusb_dec_process_urb_frame_list(unsigned long data)
  652. {
  653. struct ttusb_dec *dec = (struct ttusb_dec *)data;
  654. struct list_head *item;
  655. struct urb_frame *frame;
  656. unsigned long flags;
  657. while (1) {
  658. spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
  659. if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
  660. frame = list_entry(item, struct urb_frame,
  661. urb_frame_list);
  662. list_del(&frame->urb_frame_list);
  663. } else {
  664. spin_unlock_irqrestore(&dec->urb_frame_list_lock,
  665. flags);
  666. return;
  667. }
  668. spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
  669. ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
  670. kfree(frame);
  671. }
  672. }
  673. static void ttusb_dec_process_urb(struct urb *urb)
  674. {
  675. struct ttusb_dec *dec = urb->context;
  676. if (!urb->status) {
  677. int i;
  678. for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
  679. struct usb_iso_packet_descriptor *d;
  680. u8 *b;
  681. int length;
  682. struct urb_frame *frame;
  683. d = &urb->iso_frame_desc[i];
  684. b = urb->transfer_buffer + d->offset;
  685. length = d->actual_length;
  686. if ((frame = kmalloc(sizeof(struct urb_frame),
  687. GFP_ATOMIC))) {
  688. unsigned long flags;
  689. memcpy(frame->data, b, length);
  690. frame->length = length;
  691. spin_lock_irqsave(&dec->urb_frame_list_lock,
  692. flags);
  693. list_add_tail(&frame->urb_frame_list,
  694. &dec->urb_frame_list);
  695. spin_unlock_irqrestore(&dec->urb_frame_list_lock,
  696. flags);
  697. tasklet_schedule(&dec->urb_tasklet);
  698. }
  699. }
  700. } else {
  701. /* -ENOENT is expected when unlinking urbs */
  702. if (urb->status != -ENOENT)
  703. dprintk("%s: urb error: %d\n", __func__,
  704. urb->status);
  705. }
  706. if (dec->iso_stream_count)
  707. usb_submit_urb(urb, GFP_ATOMIC);
  708. }
  709. static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
  710. {
  711. int i, j, buffer_offset = 0;
  712. dprintk("%s\n", __func__);
  713. for (i = 0; i < ISO_BUF_COUNT; i++) {
  714. int frame_offset = 0;
  715. struct urb *urb = dec->iso_urb[i];
  716. urb->dev = dec->udev;
  717. urb->context = dec;
  718. urb->complete = ttusb_dec_process_urb;
  719. urb->pipe = dec->in_pipe;
  720. urb->transfer_flags = URB_ISO_ASAP;
  721. urb->interval = 1;
  722. urb->number_of_packets = FRAMES_PER_ISO_BUF;
  723. urb->transfer_buffer_length = ISO_FRAME_SIZE *
  724. FRAMES_PER_ISO_BUF;
  725. urb->transfer_buffer = dec->iso_buffer + buffer_offset;
  726. buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
  727. for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
  728. urb->iso_frame_desc[j].offset = frame_offset;
  729. urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
  730. frame_offset += ISO_FRAME_SIZE;
  731. }
  732. }
  733. }
  734. static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
  735. {
  736. int i;
  737. dprintk("%s\n", __func__);
  738. if (mutex_lock_interruptible(&dec->iso_mutex))
  739. return;
  740. dec->iso_stream_count--;
  741. if (!dec->iso_stream_count) {
  742. for (i = 0; i < ISO_BUF_COUNT; i++)
  743. usb_kill_urb(dec->iso_urb[i]);
  744. }
  745. mutex_unlock(&dec->iso_mutex);
  746. }
  747. /* Setting the interface of the DEC tends to take down the USB communications
  748. * for a short period, so it's important not to call this function just before
  749. * trying to talk to it.
  750. */
  751. static int ttusb_dec_set_interface(struct ttusb_dec *dec,
  752. enum ttusb_dec_interface interface)
  753. {
  754. int result = 0;
  755. u8 b[] = { 0x05 };
  756. if (interface != dec->interface) {
  757. switch (interface) {
  758. case TTUSB_DEC_INTERFACE_INITIAL:
  759. result = usb_set_interface(dec->udev, 0, 0);
  760. break;
  761. case TTUSB_DEC_INTERFACE_IN:
  762. result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
  763. b, NULL, NULL);
  764. if (result)
  765. return result;
  766. result = usb_set_interface(dec->udev, 0, 8);
  767. break;
  768. case TTUSB_DEC_INTERFACE_OUT:
  769. result = usb_set_interface(dec->udev, 0, 1);
  770. break;
  771. }
  772. if (result)
  773. return result;
  774. dec->interface = interface;
  775. }
  776. return 0;
  777. }
  778. static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
  779. {
  780. int i, result;
  781. dprintk("%s\n", __func__);
  782. if (mutex_lock_interruptible(&dec->iso_mutex))
  783. return -EAGAIN;
  784. if (!dec->iso_stream_count) {
  785. ttusb_dec_setup_urbs(dec);
  786. dec->packet_state = 0;
  787. dec->v_pes_postbytes = 0;
  788. dec->next_packet_id = 0;
  789. for (i = 0; i < ISO_BUF_COUNT; i++) {
  790. if ((result = usb_submit_urb(dec->iso_urb[i],
  791. GFP_ATOMIC))) {
  792. printk("%s: failed urb submission %d: error %d\n",
  793. __func__, i, result);
  794. while (i) {
  795. usb_kill_urb(dec->iso_urb[i - 1]);
  796. i--;
  797. }
  798. mutex_unlock(&dec->iso_mutex);
  799. return result;
  800. }
  801. }
  802. }
  803. dec->iso_stream_count++;
  804. mutex_unlock(&dec->iso_mutex);
  805. return 0;
  806. }
  807. static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
  808. {
  809. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  810. struct ttusb_dec *dec = dvbdmx->priv;
  811. u8 b0[] = { 0x05 };
  812. int result = 0;
  813. dprintk("%s\n", __func__);
  814. dprintk(" ts_type:");
  815. if (dvbdmxfeed->ts_type & TS_DECODER)
  816. dprintk(" TS_DECODER");
  817. if (dvbdmxfeed->ts_type & TS_PACKET)
  818. dprintk(" TS_PACKET");
  819. if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
  820. dprintk(" TS_PAYLOAD_ONLY");
  821. dprintk("\n");
  822. switch (dvbdmxfeed->pes_type) {
  823. case DMX_PES_VIDEO:
  824. dprintk(" pes_type: DMX_PES_VIDEO\n");
  825. dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
  826. dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
  827. dec->video_filter = dvbdmxfeed->filter;
  828. ttusb_dec_set_pids(dec);
  829. break;
  830. case DMX_PES_AUDIO:
  831. dprintk(" pes_type: DMX_PES_AUDIO\n");
  832. dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
  833. dec->audio_filter = dvbdmxfeed->filter;
  834. ttusb_dec_set_pids(dec);
  835. break;
  836. case DMX_PES_TELETEXT:
  837. dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
  838. dprintk(" pes_type: DMX_PES_TELETEXT(not supported)\n");
  839. return -ENOSYS;
  840. case DMX_PES_PCR:
  841. dprintk(" pes_type: DMX_PES_PCR\n");
  842. dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
  843. ttusb_dec_set_pids(dec);
  844. break;
  845. case DMX_PES_OTHER:
  846. dprintk(" pes_type: DMX_PES_OTHER(not supported)\n");
  847. return -ENOSYS;
  848. default:
  849. dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
  850. return -EINVAL;
  851. }
  852. result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
  853. if (result)
  854. return result;
  855. dec->pva_stream_count++;
  856. return ttusb_dec_start_iso_xfer(dec);
  857. }
  858. static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
  859. {
  860. struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
  861. u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
  862. 0x00, 0x00, 0x00, 0x00,
  863. 0x00, 0x00, 0x00, 0x00,
  864. 0x00, 0x00, 0x00, 0x00,
  865. 0x00, 0xff, 0x00, 0x00,
  866. 0x00, 0x00, 0x00, 0x00,
  867. 0x00, 0x00, 0x00, 0x00,
  868. 0x00 };
  869. __be16 pid;
  870. u8 c[COMMAND_PACKET_SIZE];
  871. int c_length;
  872. int result;
  873. struct filter_info *finfo;
  874. unsigned long flags;
  875. u8 x = 1;
  876. dprintk("%s\n", __func__);
  877. pid = htons(dvbdmxfeed->pid);
  878. memcpy(&b0[0], &pid, 2);
  879. memcpy(&b0[4], &x, 1);
  880. memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
  881. result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
  882. &c_length, c);
  883. if (!result) {
  884. if (c_length == 2) {
  885. if (!(finfo = kmalloc(sizeof(struct filter_info),
  886. GFP_ATOMIC)))
  887. return -ENOMEM;
  888. finfo->stream_id = c[1];
  889. finfo->filter = dvbdmxfeed->filter;
  890. spin_lock_irqsave(&dec->filter_info_list_lock, flags);
  891. list_add_tail(&finfo->filter_info_list,
  892. &dec->filter_info_list);
  893. spin_unlock_irqrestore(&dec->filter_info_list_lock,
  894. flags);
  895. dvbdmxfeed->priv = finfo;
  896. dec->filter_stream_count++;
  897. return ttusb_dec_start_iso_xfer(dec);
  898. }
  899. return -EAGAIN;
  900. } else
  901. return result;
  902. }
  903. static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  904. {
  905. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  906. dprintk("%s\n", __func__);
  907. if (!dvbdmx->dmx.frontend)
  908. return -EINVAL;
  909. dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
  910. switch (dvbdmxfeed->type) {
  911. case DMX_TYPE_TS:
  912. return ttusb_dec_start_ts_feed(dvbdmxfeed);
  913. break;
  914. case DMX_TYPE_SEC:
  915. return ttusb_dec_start_sec_feed(dvbdmxfeed);
  916. break;
  917. default:
  918. dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
  919. return -EINVAL;
  920. }
  921. }
  922. static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
  923. {
  924. struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
  925. u8 b0[] = { 0x00 };
  926. ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
  927. dec->pva_stream_count--;
  928. ttusb_dec_stop_iso_xfer(dec);
  929. return 0;
  930. }
  931. static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
  932. {
  933. struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
  934. u8 b0[] = { 0x00, 0x00 };
  935. struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
  936. unsigned long flags;
  937. b0[1] = finfo->stream_id;
  938. spin_lock_irqsave(&dec->filter_info_list_lock, flags);
  939. list_del(&finfo->filter_info_list);
  940. spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
  941. kfree(finfo);
  942. ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
  943. dec->filter_stream_count--;
  944. ttusb_dec_stop_iso_xfer(dec);
  945. return 0;
  946. }
  947. static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  948. {
  949. dprintk("%s\n", __func__);
  950. switch (dvbdmxfeed->type) {
  951. case DMX_TYPE_TS:
  952. return ttusb_dec_stop_ts_feed(dvbdmxfeed);
  953. break;
  954. case DMX_TYPE_SEC:
  955. return ttusb_dec_stop_sec_feed(dvbdmxfeed);
  956. break;
  957. }
  958. return 0;
  959. }
  960. static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
  961. {
  962. int i;
  963. dprintk("%s\n", __func__);
  964. for (i = 0; i < ISO_BUF_COUNT; i++)
  965. usb_free_urb(dec->iso_urb[i]);
  966. kfree(dec->iso_buffer);
  967. }
  968. static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
  969. {
  970. int i;
  971. dprintk("%s\n", __func__);
  972. dec->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
  973. ISO_FRAME_SIZE, GFP_KERNEL);
  974. if (!dec->iso_buffer)
  975. return -ENOMEM;
  976. for (i = 0; i < ISO_BUF_COUNT; i++) {
  977. struct urb *urb;
  978. if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
  979. ttusb_dec_free_iso_urbs(dec);
  980. return -ENOMEM;
  981. }
  982. dec->iso_urb[i] = urb;
  983. }
  984. ttusb_dec_setup_urbs(dec);
  985. return 0;
  986. }
  987. static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
  988. {
  989. spin_lock_init(&dec->urb_frame_list_lock);
  990. INIT_LIST_HEAD(&dec->urb_frame_list);
  991. tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list,
  992. (unsigned long)dec);
  993. }
  994. static int ttusb_init_rc( struct ttusb_dec *dec)
  995. {
  996. struct input_dev *input_dev;
  997. u8 b[] = { 0x00, 0x01 };
  998. int i;
  999. int err;
  1000. usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
  1001. strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
  1002. input_dev = input_allocate_device();
  1003. if (!input_dev)
  1004. return -ENOMEM;
  1005. input_dev->name = "ttusb_dec remote control";
  1006. input_dev->phys = dec->rc_phys;
  1007. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  1008. input_dev->keycodesize = sizeof(u16);
  1009. input_dev->keycodemax = 0x1a;
  1010. input_dev->keycode = rc_keys;
  1011. for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
  1012. set_bit(rc_keys[i], input_dev->keybit);
  1013. err = input_register_device(input_dev);
  1014. if (err) {
  1015. input_free_device(input_dev);
  1016. return err;
  1017. }
  1018. dec->rc_input_dev = input_dev;
  1019. if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
  1020. printk("%s: usb_submit_urb failed\n",__func__);
  1021. /* enable irq pipe */
  1022. ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
  1023. return 0;
  1024. }
  1025. static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
  1026. {
  1027. dprintk("%s\n", __func__);
  1028. dec->v_pes[0] = 0x00;
  1029. dec->v_pes[1] = 0x00;
  1030. dec->v_pes[2] = 0x01;
  1031. dec->v_pes[3] = 0xe0;
  1032. }
  1033. static int ttusb_dec_init_usb(struct ttusb_dec *dec)
  1034. {
  1035. int result;
  1036. dprintk("%s\n", __func__);
  1037. mutex_init(&dec->usb_mutex);
  1038. mutex_init(&dec->iso_mutex);
  1039. dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
  1040. dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
  1041. dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
  1042. dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
  1043. dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
  1044. if(enable_rc) {
  1045. dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  1046. if(!dec->irq_urb) {
  1047. return -ENOMEM;
  1048. }
  1049. dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE,
  1050. GFP_KERNEL, &dec->irq_dma_handle);
  1051. if(!dec->irq_buffer) {
  1052. usb_free_urb(dec->irq_urb);
  1053. return -ENOMEM;
  1054. }
  1055. usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
  1056. dec->irq_buffer, IRQ_PACKET_SIZE,
  1057. ttusb_dec_handle_irq, dec, 1);
  1058. dec->irq_urb->transfer_dma = dec->irq_dma_handle;
  1059. dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1060. }
  1061. result = ttusb_dec_alloc_iso_urbs(dec);
  1062. if (result) {
  1063. usb_free_urb(dec->irq_urb);
  1064. usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
  1065. dec->irq_buffer, dec->irq_dma_handle);
  1066. }
  1067. return result;
  1068. }
  1069. static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
  1070. {
  1071. int i, j, actual_len, result, size, trans_count;
  1072. u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
  1073. 0x00, 0x00, 0x00, 0x00,
  1074. 0x61, 0x00 };
  1075. u8 b1[] = { 0x61 };
  1076. u8 *b;
  1077. char idstring[21];
  1078. const u8 *firmware = NULL;
  1079. size_t firmware_size = 0;
  1080. u16 firmware_csum = 0;
  1081. __be16 firmware_csum_ns;
  1082. __be32 firmware_size_nl;
  1083. u32 crc32_csum, crc32_check;
  1084. __be32 tmp;
  1085. const struct firmware *fw_entry = NULL;
  1086. dprintk("%s\n", __func__);
  1087. result = request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev);
  1088. if (result)
  1089. return result;
  1090. firmware = fw_entry->data;
  1091. firmware_size = fw_entry->size;
  1092. if (firmware_size < 60) {
  1093. printk("%s: firmware size too small for DSP code (%zu < 60).\n",
  1094. __func__, firmware_size);
  1095. release_firmware(fw_entry);
  1096. return -ENOENT;
  1097. }
  1098. /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
  1099. at offset 56 of file, so use it to check if the firmware file is
  1100. valid. */
  1101. crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
  1102. memcpy(&tmp, &firmware[56], 4);
  1103. crc32_check = ntohl(tmp);
  1104. if (crc32_csum != crc32_check) {
  1105. printk("%s: crc32 check of DSP code failed (calculated 0x%08x != 0x%08x in file), file invalid.\n",
  1106. __func__, crc32_csum, crc32_check);
  1107. release_firmware(fw_entry);
  1108. return -ENOENT;
  1109. }
  1110. memcpy(idstring, &firmware[36], 20);
  1111. idstring[20] = '\0';
  1112. printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
  1113. firmware_size_nl = htonl(firmware_size);
  1114. memcpy(b0, &firmware_size_nl, 4);
  1115. firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
  1116. firmware_csum_ns = htons(firmware_csum);
  1117. memcpy(&b0[6], &firmware_csum_ns, 2);
  1118. result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
  1119. if (result) {
  1120. release_firmware(fw_entry);
  1121. return result;
  1122. }
  1123. trans_count = 0;
  1124. j = 0;
  1125. b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
  1126. if (b == NULL) {
  1127. release_firmware(fw_entry);
  1128. return -ENOMEM;
  1129. }
  1130. for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
  1131. size = firmware_size - i;
  1132. if (size > COMMAND_PACKET_SIZE)
  1133. size = COMMAND_PACKET_SIZE;
  1134. b[j + 0] = 0xaa;
  1135. b[j + 1] = trans_count++;
  1136. b[j + 2] = 0xf0;
  1137. b[j + 3] = size;
  1138. memcpy(&b[j + 4], &firmware[i], size);
  1139. j += COMMAND_PACKET_SIZE + 4;
  1140. if (j >= ARM_PACKET_SIZE) {
  1141. result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
  1142. ARM_PACKET_SIZE, &actual_len,
  1143. 100);
  1144. j = 0;
  1145. } else if (size < COMMAND_PACKET_SIZE) {
  1146. result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
  1147. j - COMMAND_PACKET_SIZE + size,
  1148. &actual_len, 100);
  1149. }
  1150. }
  1151. result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
  1152. release_firmware(fw_entry);
  1153. kfree(b);
  1154. return result;
  1155. }
  1156. static int ttusb_dec_init_stb(struct ttusb_dec *dec)
  1157. {
  1158. int result;
  1159. unsigned int mode = 0, model = 0, version = 0;
  1160. dprintk("%s\n", __func__);
  1161. result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
  1162. if (result)
  1163. return result;
  1164. if (!mode) {
  1165. if (version == 0xABCDEFAB)
  1166. printk(KERN_INFO "ttusb_dec: no version info in Firmware\n");
  1167. else
  1168. printk(KERN_INFO "ttusb_dec: Firmware %x.%02x%c%c\n",
  1169. version >> 24, (version >> 16) & 0xff,
  1170. (version >> 8) & 0xff, version & 0xff);
  1171. result = ttusb_dec_boot_dsp(dec);
  1172. if (result)
  1173. return result;
  1174. } else {
  1175. /* We can't trust the USB IDs that some firmwares
  1176. give the box */
  1177. switch (model) {
  1178. case 0x00070001:
  1179. case 0x00070008:
  1180. case 0x0007000c:
  1181. ttusb_dec_set_model(dec, TTUSB_DEC3000S);
  1182. break;
  1183. case 0x00070009:
  1184. case 0x00070013:
  1185. ttusb_dec_set_model(dec, TTUSB_DEC2000T);
  1186. break;
  1187. case 0x00070011:
  1188. ttusb_dec_set_model(dec, TTUSB_DEC2540T);
  1189. break;
  1190. default:
  1191. printk(KERN_ERR "%s: unknown model returned by firmware (%08x) - please report\n",
  1192. __func__, model);
  1193. return -ENOENT;
  1194. }
  1195. if (version >= 0x01770000)
  1196. dec->can_playback = 1;
  1197. }
  1198. return 0;
  1199. }
  1200. static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
  1201. {
  1202. int result;
  1203. dprintk("%s\n", __func__);
  1204. if ((result = dvb_register_adapter(&dec->adapter,
  1205. dec->model_name, THIS_MODULE,
  1206. &dec->udev->dev,
  1207. adapter_nr)) < 0) {
  1208. printk("%s: dvb_register_adapter failed: error %d\n",
  1209. __func__, result);
  1210. return result;
  1211. }
  1212. dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  1213. dec->demux.priv = (void *)dec;
  1214. dec->demux.filternum = 31;
  1215. dec->demux.feednum = 31;
  1216. dec->demux.start_feed = ttusb_dec_start_feed;
  1217. dec->demux.stop_feed = ttusb_dec_stop_feed;
  1218. dec->demux.write_to_decoder = NULL;
  1219. if ((result = dvb_dmx_init(&dec->demux)) < 0) {
  1220. printk("%s: dvb_dmx_init failed: error %d\n", __func__,
  1221. result);
  1222. dvb_unregister_adapter(&dec->adapter);
  1223. return result;
  1224. }
  1225. dec->dmxdev.filternum = 32;
  1226. dec->dmxdev.demux = &dec->demux.dmx;
  1227. dec->dmxdev.capabilities = 0;
  1228. if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
  1229. printk("%s: dvb_dmxdev_init failed: error %d\n",
  1230. __func__, result);
  1231. dvb_dmx_release(&dec->demux);
  1232. dvb_unregister_adapter(&dec->adapter);
  1233. return result;
  1234. }
  1235. dec->frontend.source = DMX_FRONTEND_0;
  1236. if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
  1237. &dec->frontend)) < 0) {
  1238. printk("%s: dvb_dmx_init failed: error %d\n", __func__,
  1239. result);
  1240. dvb_dmxdev_release(&dec->dmxdev);
  1241. dvb_dmx_release(&dec->demux);
  1242. dvb_unregister_adapter(&dec->adapter);
  1243. return result;
  1244. }
  1245. if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
  1246. &dec->frontend)) < 0) {
  1247. printk("%s: dvb_dmx_init failed: error %d\n", __func__,
  1248. result);
  1249. dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
  1250. dvb_dmxdev_release(&dec->dmxdev);
  1251. dvb_dmx_release(&dec->demux);
  1252. dvb_unregister_adapter(&dec->adapter);
  1253. return result;
  1254. }
  1255. dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
  1256. return 0;
  1257. }
  1258. static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
  1259. {
  1260. dprintk("%s\n", __func__);
  1261. dvb_net_release(&dec->dvb_net);
  1262. dec->demux.dmx.close(&dec->demux.dmx);
  1263. dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
  1264. dvb_dmxdev_release(&dec->dmxdev);
  1265. dvb_dmx_release(&dec->demux);
  1266. if (dec->fe) {
  1267. dvb_unregister_frontend(dec->fe);
  1268. if (dec->fe->ops.release)
  1269. dec->fe->ops.release(dec->fe);
  1270. }
  1271. dvb_unregister_adapter(&dec->adapter);
  1272. }
  1273. static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
  1274. {
  1275. dprintk("%s\n", __func__);
  1276. if (dec->rc_input_dev) {
  1277. input_unregister_device(dec->rc_input_dev);
  1278. dec->rc_input_dev = NULL;
  1279. }
  1280. }
  1281. static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
  1282. {
  1283. int i;
  1284. dprintk("%s\n", __func__);
  1285. if (enable_rc) {
  1286. /* we have to check whether the irq URB is already submitted.
  1287. * As the irq is submitted after the interface is changed,
  1288. * this is the best method i figured out.
  1289. * Any others?*/
  1290. if (dec->interface == TTUSB_DEC_INTERFACE_IN)
  1291. usb_kill_urb(dec->irq_urb);
  1292. usb_free_urb(dec->irq_urb);
  1293. usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
  1294. dec->irq_buffer, dec->irq_dma_handle);
  1295. }
  1296. dec->iso_stream_count = 0;
  1297. for (i = 0; i < ISO_BUF_COUNT; i++)
  1298. usb_kill_urb(dec->iso_urb[i]);
  1299. ttusb_dec_free_iso_urbs(dec);
  1300. }
  1301. static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
  1302. {
  1303. struct list_head *item;
  1304. struct urb_frame *frame;
  1305. tasklet_kill(&dec->urb_tasklet);
  1306. while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
  1307. frame = list_entry(item, struct urb_frame, urb_frame_list);
  1308. list_del(&frame->urb_frame_list);
  1309. kfree(frame);
  1310. }
  1311. }
  1312. static void ttusb_dec_init_filters(struct ttusb_dec *dec)
  1313. {
  1314. INIT_LIST_HEAD(&dec->filter_info_list);
  1315. spin_lock_init(&dec->filter_info_list_lock);
  1316. }
  1317. static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
  1318. {
  1319. struct list_head *item;
  1320. struct filter_info *finfo;
  1321. while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
  1322. finfo = list_entry(item, struct filter_info, filter_info_list);
  1323. list_del(&finfo->filter_info_list);
  1324. kfree(finfo);
  1325. }
  1326. }
  1327. static int fe_send_command(struct dvb_frontend* fe, const u8 command,
  1328. int param_length, const u8 params[],
  1329. int *result_length, u8 cmd_result[])
  1330. {
  1331. struct ttusb_dec* dec = fe->dvb->priv;
  1332. return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
  1333. }
  1334. static const struct ttusbdecfe_config fe_config = {
  1335. .send_command = fe_send_command
  1336. };
  1337. static int ttusb_dec_probe(struct usb_interface *intf,
  1338. const struct usb_device_id *id)
  1339. {
  1340. struct usb_device *udev;
  1341. struct ttusb_dec *dec;
  1342. int result;
  1343. dprintk("%s\n", __func__);
  1344. udev = interface_to_usbdev(intf);
  1345. if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
  1346. printk("%s: couldn't allocate memory.\n", __func__);
  1347. return -ENOMEM;
  1348. }
  1349. usb_set_intfdata(intf, (void *)dec);
  1350. switch (id->idProduct) {
  1351. case 0x1006:
  1352. ttusb_dec_set_model(dec, TTUSB_DEC3000S);
  1353. break;
  1354. case 0x1008:
  1355. ttusb_dec_set_model(dec, TTUSB_DEC2000T);
  1356. break;
  1357. case 0x1009:
  1358. ttusb_dec_set_model(dec, TTUSB_DEC2540T);
  1359. break;
  1360. }
  1361. dec->udev = udev;
  1362. result = ttusb_dec_init_usb(dec);
  1363. if (result)
  1364. goto err_usb;
  1365. result = ttusb_dec_init_stb(dec);
  1366. if (result)
  1367. goto err_stb;
  1368. result = ttusb_dec_init_dvb(dec);
  1369. if (result)
  1370. goto err_stb;
  1371. dec->adapter.priv = dec;
  1372. switch (id->idProduct) {
  1373. case 0x1006:
  1374. dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
  1375. break;
  1376. case 0x1008:
  1377. case 0x1009:
  1378. dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
  1379. break;
  1380. }
  1381. if (dec->fe == NULL) {
  1382. printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
  1383. le16_to_cpu(dec->udev->descriptor.idVendor),
  1384. le16_to_cpu(dec->udev->descriptor.idProduct));
  1385. } else {
  1386. if (dvb_register_frontend(&dec->adapter, dec->fe)) {
  1387. printk("budget-ci: Frontend registration failed!\n");
  1388. if (dec->fe->ops.release)
  1389. dec->fe->ops.release(dec->fe);
  1390. dec->fe = NULL;
  1391. }
  1392. }
  1393. ttusb_dec_init_v_pes(dec);
  1394. ttusb_dec_init_filters(dec);
  1395. ttusb_dec_init_tasklet(dec);
  1396. dec->active = 1;
  1397. ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
  1398. if (enable_rc)
  1399. ttusb_init_rc(dec);
  1400. return 0;
  1401. err_stb:
  1402. ttusb_dec_exit_usb(dec);
  1403. err_usb:
  1404. kfree(dec);
  1405. return result;
  1406. }
  1407. static void ttusb_dec_disconnect(struct usb_interface *intf)
  1408. {
  1409. struct ttusb_dec *dec = usb_get_intfdata(intf);
  1410. usb_set_intfdata(intf, NULL);
  1411. dprintk("%s\n", __func__);
  1412. if (dec->active) {
  1413. ttusb_dec_exit_tasklet(dec);
  1414. ttusb_dec_exit_filters(dec);
  1415. if(enable_rc)
  1416. ttusb_dec_exit_rc(dec);
  1417. ttusb_dec_exit_usb(dec);
  1418. ttusb_dec_exit_dvb(dec);
  1419. }
  1420. kfree(dec);
  1421. }
  1422. static void ttusb_dec_set_model(struct ttusb_dec *dec,
  1423. enum ttusb_dec_model model)
  1424. {
  1425. dec->model = model;
  1426. switch (model) {
  1427. case TTUSB_DEC2000T:
  1428. dec->model_name = "DEC2000-t";
  1429. dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
  1430. break;
  1431. case TTUSB_DEC2540T:
  1432. dec->model_name = "DEC2540-t";
  1433. dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
  1434. break;
  1435. case TTUSB_DEC3000S:
  1436. dec->model_name = "DEC3000-s";
  1437. dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
  1438. break;
  1439. }
  1440. }
  1441. static const struct usb_device_id ttusb_dec_table[] = {
  1442. {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
  1443. /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
  1444. {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
  1445. {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
  1446. {}
  1447. };
  1448. static struct usb_driver ttusb_dec_driver = {
  1449. .name = "ttusb-dec",
  1450. .probe = ttusb_dec_probe,
  1451. .disconnect = ttusb_dec_disconnect,
  1452. .id_table = ttusb_dec_table,
  1453. };
  1454. module_usb_driver(ttusb_dec_driver);
  1455. MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
  1456. MODULE_DESCRIPTION(DRIVER_NAME);
  1457. MODULE_LICENSE("GPL");
  1458. MODULE_DEVICE_TABLE(usb, ttusb_dec_table);