pd-dvb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #include "pd-common.h"
  2. #include <linux/kernel.h>
  3. #include <linux/usb.h>
  4. #include <linux/dvb/dmx.h>
  5. #include <linux/delay.h>
  6. #include <linux/gfp.h>
  7. #include "vendorcmds.h"
  8. #include <linux/sched.h>
  9. #include <linux/atomic.h>
  10. static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb);
  11. static int dvb_bandwidth[][2] = {
  12. { TLG_BW_8, 8000000 },
  13. { TLG_BW_7, 7000000 },
  14. { TLG_BW_6, 6000000 }
  15. };
  16. static int dvb_bandwidth_length = ARRAY_SIZE(dvb_bandwidth);
  17. static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb);
  18. static int poseidon_check_mode_dvbt(struct poseidon *pd)
  19. {
  20. s32 ret = 0, cmd_status = 0;
  21. set_current_state(TASK_INTERRUPTIBLE);
  22. schedule_timeout(HZ/4);
  23. ret = usb_set_interface(pd->udev, 0, BULK_ALTERNATE_IFACE);
  24. if (ret != 0)
  25. return ret;
  26. ret = set_tuner_mode(pd, TLG_MODE_CAPS_DVB_T);
  27. if (ret)
  28. return ret;
  29. /* signal source */
  30. ret = send_set_req(pd, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &cmd_status);
  31. if (ret|cmd_status)
  32. return ret;
  33. return 0;
  34. }
  35. /* acquire :
  36. * 1 == open
  37. * 0 == release
  38. */
  39. static int poseidon_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
  40. {
  41. struct poseidon *pd = fe->demodulator_priv;
  42. struct pd_dvb_adapter *pd_dvb;
  43. int ret = 0;
  44. if (!pd)
  45. return -ENODEV;
  46. pd_dvb = container_of(fe, struct pd_dvb_adapter, dvb_fe);
  47. if (acquire) {
  48. mutex_lock(&pd->lock);
  49. if (pd->state & POSEIDON_STATE_DISCONNECT) {
  50. ret = -ENODEV;
  51. goto open_out;
  52. }
  53. if (pd->state && !(pd->state & POSEIDON_STATE_DVBT)) {
  54. ret = -EBUSY;
  55. goto open_out;
  56. }
  57. usb_autopm_get_interface(pd->interface);
  58. if (0 == pd->state) {
  59. ret = poseidon_check_mode_dvbt(pd);
  60. if (ret < 0) {
  61. usb_autopm_put_interface(pd->interface);
  62. goto open_out;
  63. }
  64. pd->state |= POSEIDON_STATE_DVBT;
  65. pd_dvb->bandwidth = 0;
  66. pd_dvb->prev_freq = 0;
  67. }
  68. atomic_inc(&pd_dvb->users);
  69. kref_get(&pd->kref);
  70. open_out:
  71. mutex_unlock(&pd->lock);
  72. } else {
  73. dvb_stop_streaming(pd_dvb);
  74. if (atomic_dec_and_test(&pd_dvb->users)) {
  75. mutex_lock(&pd->lock);
  76. pd->state &= ~POSEIDON_STATE_DVBT;
  77. mutex_unlock(&pd->lock);
  78. }
  79. kref_put(&pd->kref, poseidon_delete);
  80. usb_autopm_put_interface(pd->interface);
  81. }
  82. return ret;
  83. }
  84. #ifdef CONFIG_PM
  85. static void poseidon_fe_release(struct dvb_frontend *fe)
  86. {
  87. struct poseidon *pd = fe->demodulator_priv;
  88. pd->pm_suspend = NULL;
  89. pd->pm_resume = NULL;
  90. }
  91. #else
  92. #define poseidon_fe_release NULL
  93. #endif
  94. static s32 poseidon_fe_sleep(struct dvb_frontend *fe)
  95. {
  96. return 0;
  97. }
  98. /*
  99. * return true if we can satisfy the conditions, else return false.
  100. */
  101. static bool check_scan_ok(__u32 freq, int bandwidth,
  102. struct pd_dvb_adapter *adapter)
  103. {
  104. if (bandwidth < 0)
  105. return false;
  106. if (adapter->prev_freq == freq
  107. && adapter->bandwidth == bandwidth) {
  108. long nl = jiffies - adapter->last_jiffies;
  109. unsigned int msec ;
  110. msec = jiffies_to_msecs(abs(nl));
  111. return msec > 15000 ? true : false;
  112. }
  113. return true;
  114. }
  115. /*
  116. * Check if the firmware delays too long for an invalid frequency.
  117. */
  118. static int fw_delay_overflow(struct pd_dvb_adapter *adapter)
  119. {
  120. long nl = jiffies - adapter->last_jiffies;
  121. unsigned int msec ;
  122. msec = jiffies_to_msecs(abs(nl));
  123. return msec > 800 ? true : false;
  124. }
  125. static int poseidon_set_fe(struct dvb_frontend *fe)
  126. {
  127. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  128. s32 ret = 0, cmd_status = 0;
  129. s32 i, bandwidth = -1;
  130. struct poseidon *pd = fe->demodulator_priv;
  131. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  132. if (in_hibernation(pd))
  133. return -EBUSY;
  134. mutex_lock(&pd->lock);
  135. for (i = 0; i < dvb_bandwidth_length; i++)
  136. if (fep->bandwidth_hz == dvb_bandwidth[i][1])
  137. bandwidth = dvb_bandwidth[i][0];
  138. if (check_scan_ok(fep->frequency, bandwidth, pd_dvb)) {
  139. ret = send_set_req(pd, TUNE_FREQ_SELECT,
  140. fep->frequency / 1000, &cmd_status);
  141. if (ret | cmd_status) {
  142. log("error line");
  143. goto front_out;
  144. }
  145. ret = send_set_req(pd, DVBT_BANDW_SEL,
  146. bandwidth, &cmd_status);
  147. if (ret | cmd_status) {
  148. log("error line");
  149. goto front_out;
  150. }
  151. ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
  152. if (ret | cmd_status) {
  153. log("error line");
  154. goto front_out;
  155. }
  156. /* save the context for future */
  157. memcpy(&pd_dvb->fe_param, fep, sizeof(*fep));
  158. pd_dvb->bandwidth = bandwidth;
  159. pd_dvb->prev_freq = fep->frequency;
  160. pd_dvb->last_jiffies = jiffies;
  161. }
  162. front_out:
  163. mutex_unlock(&pd->lock);
  164. return ret;
  165. }
  166. #ifdef CONFIG_PM
  167. static int pm_dvb_suspend(struct poseidon *pd)
  168. {
  169. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  170. dvb_stop_streaming(pd_dvb);
  171. dvb_urb_cleanup(pd_dvb);
  172. msleep(500);
  173. return 0;
  174. }
  175. static int pm_dvb_resume(struct poseidon *pd)
  176. {
  177. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  178. poseidon_check_mode_dvbt(pd);
  179. msleep(300);
  180. poseidon_set_fe(&pd_dvb->dvb_fe);
  181. dvb_start_streaming(pd_dvb);
  182. return 0;
  183. }
  184. #endif
  185. static s32 poseidon_fe_init(struct dvb_frontend *fe)
  186. {
  187. struct poseidon *pd = fe->demodulator_priv;
  188. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  189. #ifdef CONFIG_PM
  190. pd->pm_suspend = pm_dvb_suspend;
  191. pd->pm_resume = pm_dvb_resume;
  192. #endif
  193. memset(&pd_dvb->fe_param, 0,
  194. sizeof(struct dtv_frontend_properties));
  195. return 0;
  196. }
  197. static int poseidon_get_fe(struct dvb_frontend *fe)
  198. {
  199. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  200. struct poseidon *pd = fe->demodulator_priv;
  201. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  202. memcpy(fep, &pd_dvb->fe_param, sizeof(*fep));
  203. return 0;
  204. }
  205. static int poseidon_fe_get_tune_settings(struct dvb_frontend *fe,
  206. struct dvb_frontend_tune_settings *tune)
  207. {
  208. tune->min_delay_ms = 1000;
  209. return 0;
  210. }
  211. static int poseidon_read_status(struct dvb_frontend *fe, fe_status_t *stat)
  212. {
  213. struct poseidon *pd = fe->demodulator_priv;
  214. s32 ret = -1, cmd_status;
  215. struct tuner_dtv_sig_stat_s status = {};
  216. if (in_hibernation(pd))
  217. return -EBUSY;
  218. mutex_lock(&pd->lock);
  219. ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
  220. &status, &cmd_status, sizeof(status));
  221. if (ret | cmd_status) {
  222. log("get tuner status error");
  223. goto out;
  224. }
  225. if (debug_mode)
  226. log("P : %d, L %d, LB :%d", status.sig_present,
  227. status.sig_locked, status.sig_lock_busy);
  228. if (status.sig_lock_busy) {
  229. goto out;
  230. } else if (status.sig_present || status.sig_locked) {
  231. *stat |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER
  232. | FE_HAS_SYNC | FE_HAS_VITERBI;
  233. } else {
  234. if (fw_delay_overflow(&pd->dvb_data))
  235. *stat |= FE_TIMEDOUT;
  236. }
  237. out:
  238. mutex_unlock(&pd->lock);
  239. return ret;
  240. }
  241. static int poseidon_read_ber(struct dvb_frontend *fe, u32 *ber)
  242. {
  243. struct poseidon *pd = fe->demodulator_priv;
  244. struct tuner_ber_rate_s tlg_ber = {};
  245. s32 ret = -1, cmd_status;
  246. mutex_lock(&pd->lock);
  247. ret = send_get_req(pd, TUNER_BER_RATE, 0,
  248. &tlg_ber, &cmd_status, sizeof(tlg_ber));
  249. if (ret | cmd_status)
  250. goto out;
  251. *ber = tlg_ber.ber_rate;
  252. out:
  253. mutex_unlock(&pd->lock);
  254. return ret;
  255. }
  256. static s32 poseidon_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  257. {
  258. struct poseidon *pd = fe->demodulator_priv;
  259. struct tuner_dtv_sig_stat_s status = {};
  260. s32 ret = 0, cmd_status;
  261. mutex_lock(&pd->lock);
  262. ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
  263. &status, &cmd_status, sizeof(status));
  264. if (ret | cmd_status)
  265. goto out;
  266. if ((status.sig_present || status.sig_locked) && !status.sig_strength)
  267. *strength = 0xFFFF;
  268. else
  269. *strength = status.sig_strength;
  270. out:
  271. mutex_unlock(&pd->lock);
  272. return ret;
  273. }
  274. static int poseidon_read_snr(struct dvb_frontend *fe, u16 *snr)
  275. {
  276. return 0;
  277. }
  278. static int poseidon_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  279. {
  280. *unc = 0;
  281. return 0;
  282. }
  283. static struct dvb_frontend_ops poseidon_frontend_ops = {
  284. .delsys = { SYS_DVBT },
  285. .info = {
  286. .name = "Poseidon DVB-T",
  287. .frequency_min = 174000000,
  288. .frequency_max = 862000000,
  289. .frequency_stepsize = 62500,/* FIXME */
  290. .caps = FE_CAN_INVERSION_AUTO |
  291. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  292. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  293. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  294. FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
  295. FE_CAN_GUARD_INTERVAL_AUTO |
  296. FE_CAN_RECOVER |
  297. FE_CAN_HIERARCHY_AUTO,
  298. },
  299. .release = poseidon_fe_release,
  300. .init = poseidon_fe_init,
  301. .sleep = poseidon_fe_sleep,
  302. .set_frontend = poseidon_set_fe,
  303. .get_frontend = poseidon_get_fe,
  304. .get_tune_settings = poseidon_fe_get_tune_settings,
  305. .read_status = poseidon_read_status,
  306. .read_ber = poseidon_read_ber,
  307. .read_signal_strength = poseidon_read_signal_strength,
  308. .read_snr = poseidon_read_snr,
  309. .read_ucblocks = poseidon_read_unc_blocks,
  310. .ts_bus_ctrl = poseidon_ts_bus_ctrl,
  311. };
  312. static void dvb_urb_irq(struct urb *urb)
  313. {
  314. struct pd_dvb_adapter *pd_dvb = urb->context;
  315. int len = urb->transfer_buffer_length;
  316. struct dvb_demux *demux = &pd_dvb->demux;
  317. s32 ret;
  318. if (!pd_dvb->is_streaming || urb->status) {
  319. if (urb->status == -EPROTO)
  320. goto resend;
  321. return;
  322. }
  323. if (urb->actual_length == len)
  324. dvb_dmx_swfilter(demux, urb->transfer_buffer, len);
  325. else if (urb->actual_length == len - 4) {
  326. int offset;
  327. u8 *buf = urb->transfer_buffer;
  328. /*
  329. * The packet size is 512,
  330. * last packet contains 456 bytes tsp data
  331. */
  332. for (offset = 456; offset < len; offset += 512) {
  333. if (!strncmp(buf + offset, "DVHS", 4)) {
  334. dvb_dmx_swfilter(demux, buf, offset);
  335. if (len > offset + 52 + 4) {
  336. /*16 bytes trailer + 36 bytes padding */
  337. buf += offset + 52;
  338. len -= offset + 52 + 4;
  339. dvb_dmx_swfilter(demux, buf, len);
  340. }
  341. break;
  342. }
  343. }
  344. }
  345. resend:
  346. ret = usb_submit_urb(urb, GFP_ATOMIC);
  347. if (ret)
  348. log(" usb_submit_urb failed: error %d", ret);
  349. }
  350. static int dvb_urb_init(struct pd_dvb_adapter *pd_dvb)
  351. {
  352. if (pd_dvb->urb_array[0])
  353. return 0;
  354. alloc_bulk_urbs_generic(pd_dvb->urb_array, DVB_SBUF_NUM,
  355. pd_dvb->pd_device->udev, pd_dvb->ep_addr,
  356. DVB_URB_BUF_SIZE, GFP_KERNEL,
  357. dvb_urb_irq, pd_dvb);
  358. return 0;
  359. }
  360. static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb)
  361. {
  362. free_all_urb_generic(pd_dvb->urb_array, DVB_SBUF_NUM);
  363. }
  364. static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb)
  365. {
  366. struct poseidon *pd = pd_dvb->pd_device;
  367. int ret = 0;
  368. if (pd->state & POSEIDON_STATE_DISCONNECT)
  369. return -ENODEV;
  370. mutex_lock(&pd->lock);
  371. if (!pd_dvb->is_streaming) {
  372. s32 i, cmd_status = 0;
  373. /*
  374. * Once upon a time, there was a difficult bug lying here.
  375. * ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
  376. */
  377. ret = send_set_req(pd, PLAY_SERVICE, 1, &cmd_status);
  378. if (ret | cmd_status)
  379. goto out;
  380. ret = dvb_urb_init(pd_dvb);
  381. if (ret < 0)
  382. goto out;
  383. pd_dvb->is_streaming = 1;
  384. for (i = 0; i < DVB_SBUF_NUM; i++) {
  385. ret = usb_submit_urb(pd_dvb->urb_array[i],
  386. GFP_KERNEL);
  387. if (ret) {
  388. log(" submit urb error %d", ret);
  389. goto out;
  390. }
  391. }
  392. }
  393. out:
  394. mutex_unlock(&pd->lock);
  395. return ret;
  396. }
  397. void dvb_stop_streaming(struct pd_dvb_adapter *pd_dvb)
  398. {
  399. struct poseidon *pd = pd_dvb->pd_device;
  400. mutex_lock(&pd->lock);
  401. if (pd_dvb->is_streaming) {
  402. s32 i, ret, cmd_status = 0;
  403. pd_dvb->is_streaming = 0;
  404. for (i = 0; i < DVB_SBUF_NUM; i++)
  405. if (pd_dvb->urb_array[i])
  406. usb_kill_urb(pd_dvb->urb_array[i]);
  407. ret = send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
  408. &cmd_status);
  409. if (ret | cmd_status)
  410. log("error");
  411. }
  412. mutex_unlock(&pd->lock);
  413. }
  414. static int pd_start_feed(struct dvb_demux_feed *feed)
  415. {
  416. struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
  417. int ret = 0;
  418. if (!pd_dvb)
  419. return -1;
  420. if (atomic_inc_return(&pd_dvb->active_feed) == 1)
  421. ret = dvb_start_streaming(pd_dvb);
  422. return ret;
  423. }
  424. static int pd_stop_feed(struct dvb_demux_feed *feed)
  425. {
  426. struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
  427. if (!pd_dvb)
  428. return -1;
  429. if (atomic_dec_and_test(&pd_dvb->active_feed))
  430. dvb_stop_streaming(pd_dvb);
  431. return 0;
  432. }
  433. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  434. int pd_dvb_usb_device_init(struct poseidon *pd)
  435. {
  436. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  437. struct dvb_demux *dvbdemux;
  438. int ret = 0;
  439. pd_dvb->ep_addr = 0x82;
  440. atomic_set(&pd_dvb->users, 0);
  441. atomic_set(&pd_dvb->active_feed, 0);
  442. pd_dvb->pd_device = pd;
  443. ret = dvb_register_adapter(&pd_dvb->dvb_adap,
  444. "Poseidon dvbt adapter",
  445. THIS_MODULE,
  446. NULL /* for hibernation correctly*/,
  447. adapter_nr);
  448. if (ret < 0)
  449. goto error1;
  450. /* register frontend */
  451. pd_dvb->dvb_fe.demodulator_priv = pd;
  452. memcpy(&pd_dvb->dvb_fe.ops, &poseidon_frontend_ops,
  453. sizeof(struct dvb_frontend_ops));
  454. ret = dvb_register_frontend(&pd_dvb->dvb_adap, &pd_dvb->dvb_fe);
  455. if (ret < 0)
  456. goto error2;
  457. /* register demux device */
  458. dvbdemux = &pd_dvb->demux;
  459. dvbdemux->dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  460. dvbdemux->priv = pd_dvb;
  461. dvbdemux->feednum = dvbdemux->filternum = 64;
  462. dvbdemux->start_feed = pd_start_feed;
  463. dvbdemux->stop_feed = pd_stop_feed;
  464. dvbdemux->write_to_decoder = NULL;
  465. ret = dvb_dmx_init(dvbdemux);
  466. if (ret < 0)
  467. goto error3;
  468. pd_dvb->dmxdev.filternum = pd_dvb->demux.filternum;
  469. pd_dvb->dmxdev.demux = &pd_dvb->demux.dmx;
  470. pd_dvb->dmxdev.capabilities = 0;
  471. ret = dvb_dmxdev_init(&pd_dvb->dmxdev, &pd_dvb->dvb_adap);
  472. if (ret < 0)
  473. goto error3;
  474. return 0;
  475. error3:
  476. dvb_unregister_frontend(&pd_dvb->dvb_fe);
  477. error2:
  478. dvb_unregister_adapter(&pd_dvb->dvb_adap);
  479. error1:
  480. return ret;
  481. }
  482. void pd_dvb_usb_device_exit(struct poseidon *pd)
  483. {
  484. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  485. while (atomic_read(&pd_dvb->users) != 0
  486. || atomic_read(&pd_dvb->active_feed) != 0) {
  487. set_current_state(TASK_INTERRUPTIBLE);
  488. schedule_timeout(HZ);
  489. }
  490. dvb_dmxdev_release(&pd_dvb->dmxdev);
  491. dvb_unregister_frontend(&pd_dvb->dvb_fe);
  492. dvb_unregister_adapter(&pd_dvb->dvb_adap);
  493. pd_dvb_usb_device_cleanup(pd);
  494. }
  495. void pd_dvb_usb_device_cleanup(struct poseidon *pd)
  496. {
  497. struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
  498. dvb_urb_cleanup(pd_dvb);
  499. }
  500. int pd_dvb_get_adapter_num(struct pd_dvb_adapter *pd_dvb)
  501. {
  502. return pd_dvb->dvb_adap.num;
  503. }