as102_drv.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Abilis Systems Single DVB-T Receiver
  3. * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
  4. * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/mm.h>
  21. #include <linux/kref.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/usb.h>
  24. /* header file for usb device driver*/
  25. #include "as102_drv.h"
  26. #include "as10x_cmd.h"
  27. #include "as102_fe.h"
  28. #include "as102_fw.h"
  29. #include "dvbdev.h"
  30. int dual_tuner;
  31. module_param_named(dual_tuner, dual_tuner, int, 0644);
  32. MODULE_PARM_DESC(dual_tuner, "Activate Dual-Tuner config (default: off)");
  33. static int fw_upload = 1;
  34. module_param_named(fw_upload, fw_upload, int, 0644);
  35. MODULE_PARM_DESC(fw_upload, "Turn on/off default FW upload (default: on)");
  36. static int pid_filtering;
  37. module_param_named(pid_filtering, pid_filtering, int, 0644);
  38. MODULE_PARM_DESC(pid_filtering, "Activate HW PID filtering (default: off)");
  39. static int ts_auto_disable;
  40. module_param_named(ts_auto_disable, ts_auto_disable, int, 0644);
  41. MODULE_PARM_DESC(ts_auto_disable, "Stream Auto Enable on FW (default: off)");
  42. int elna_enable = 1;
  43. module_param_named(elna_enable, elna_enable, int, 0644);
  44. MODULE_PARM_DESC(elna_enable, "Activate eLNA (default: on)");
  45. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  46. static void as102_stop_stream(struct as102_dev_t *dev)
  47. {
  48. struct as10x_bus_adapter_t *bus_adap;
  49. if (dev != NULL)
  50. bus_adap = &dev->bus_adap;
  51. else
  52. return;
  53. if (bus_adap->ops->stop_stream != NULL)
  54. bus_adap->ops->stop_stream(dev);
  55. if (ts_auto_disable) {
  56. if (mutex_lock_interruptible(&dev->bus_adap.lock))
  57. return;
  58. if (as10x_cmd_stop_streaming(bus_adap) < 0)
  59. dev_dbg(&dev->bus_adap.usb_dev->dev,
  60. "as10x_cmd_stop_streaming failed\n");
  61. mutex_unlock(&dev->bus_adap.lock);
  62. }
  63. }
  64. static int as102_start_stream(struct as102_dev_t *dev)
  65. {
  66. struct as10x_bus_adapter_t *bus_adap;
  67. int ret = -EFAULT;
  68. if (dev != NULL)
  69. bus_adap = &dev->bus_adap;
  70. else
  71. return ret;
  72. if (bus_adap->ops->start_stream != NULL)
  73. ret = bus_adap->ops->start_stream(dev);
  74. if (ts_auto_disable) {
  75. if (mutex_lock_interruptible(&dev->bus_adap.lock))
  76. return -EFAULT;
  77. ret = as10x_cmd_start_streaming(bus_adap);
  78. mutex_unlock(&dev->bus_adap.lock);
  79. }
  80. return ret;
  81. }
  82. static int as10x_pid_filter(struct as102_dev_t *dev,
  83. int index, u16 pid, int onoff) {
  84. struct as10x_bus_adapter_t *bus_adap = &dev->bus_adap;
  85. int ret = -EFAULT;
  86. if (mutex_lock_interruptible(&dev->bus_adap.lock)) {
  87. dev_dbg(&dev->bus_adap.usb_dev->dev,
  88. "amutex_lock_interruptible(lock) failed !\n");
  89. return -EBUSY;
  90. }
  91. switch (onoff) {
  92. case 0:
  93. ret = as10x_cmd_del_PID_filter(bus_adap, (uint16_t) pid);
  94. dev_dbg(&dev->bus_adap.usb_dev->dev,
  95. "DEL_PID_FILTER([%02d] 0x%04x) ret = %d\n",
  96. index, pid, ret);
  97. break;
  98. case 1:
  99. {
  100. struct as10x_ts_filter filter;
  101. filter.type = TS_PID_TYPE_TS;
  102. filter.idx = 0xFF;
  103. filter.pid = pid;
  104. ret = as10x_cmd_add_PID_filter(bus_adap, &filter);
  105. dev_dbg(&dev->bus_adap.usb_dev->dev,
  106. "ADD_PID_FILTER([%02d -> %02d], 0x%04x) ret = %d\n",
  107. index, filter.idx, filter.pid, ret);
  108. break;
  109. }
  110. }
  111. mutex_unlock(&dev->bus_adap.lock);
  112. return ret;
  113. }
  114. static int as102_dvb_dmx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  115. {
  116. int ret = 0;
  117. struct dvb_demux *demux = dvbdmxfeed->demux;
  118. struct as102_dev_t *as102_dev = demux->priv;
  119. if (mutex_lock_interruptible(&as102_dev->sem))
  120. return -ERESTARTSYS;
  121. if (pid_filtering)
  122. as10x_pid_filter(as102_dev, dvbdmxfeed->index,
  123. dvbdmxfeed->pid, 1);
  124. if (as102_dev->streaming++ == 0)
  125. ret = as102_start_stream(as102_dev);
  126. mutex_unlock(&as102_dev->sem);
  127. return ret;
  128. }
  129. static int as102_dvb_dmx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  130. {
  131. struct dvb_demux *demux = dvbdmxfeed->demux;
  132. struct as102_dev_t *as102_dev = demux->priv;
  133. if (mutex_lock_interruptible(&as102_dev->sem))
  134. return -ERESTARTSYS;
  135. if (--as102_dev->streaming == 0)
  136. as102_stop_stream(as102_dev);
  137. if (pid_filtering)
  138. as10x_pid_filter(as102_dev, dvbdmxfeed->index,
  139. dvbdmxfeed->pid, 0);
  140. mutex_unlock(&as102_dev->sem);
  141. return 0;
  142. }
  143. static int as102_set_tune(void *priv, struct as10x_tune_args *tune_args)
  144. {
  145. struct as10x_bus_adapter_t *bus_adap = priv;
  146. int ret;
  147. /* Set frontend arguments */
  148. if (mutex_lock_interruptible(&bus_adap->lock))
  149. return -EBUSY;
  150. ret = as10x_cmd_set_tune(bus_adap, tune_args);
  151. if (ret != 0)
  152. dev_dbg(&bus_adap->usb_dev->dev,
  153. "as10x_cmd_set_tune failed. (err = %d)\n", ret);
  154. mutex_unlock(&bus_adap->lock);
  155. return ret;
  156. }
  157. static int as102_get_tps(void *priv, struct as10x_tps *tps)
  158. {
  159. struct as10x_bus_adapter_t *bus_adap = priv;
  160. int ret;
  161. if (mutex_lock_interruptible(&bus_adap->lock))
  162. return -EBUSY;
  163. /* send abilis command: GET_TPS */
  164. ret = as10x_cmd_get_tps(bus_adap, tps);
  165. mutex_unlock(&bus_adap->lock);
  166. return ret;
  167. }
  168. static int as102_get_status(void *priv, struct as10x_tune_status *tstate)
  169. {
  170. struct as10x_bus_adapter_t *bus_adap = priv;
  171. int ret;
  172. if (mutex_lock_interruptible(&bus_adap->lock))
  173. return -EBUSY;
  174. /* send abilis command: GET_TUNE_STATUS */
  175. ret = as10x_cmd_get_tune_status(bus_adap, tstate);
  176. if (ret < 0) {
  177. dev_dbg(&bus_adap->usb_dev->dev,
  178. "as10x_cmd_get_tune_status failed (err = %d)\n",
  179. ret);
  180. }
  181. mutex_unlock(&bus_adap->lock);
  182. return ret;
  183. }
  184. static int as102_get_stats(void *priv, struct as10x_demod_stats *demod_stats)
  185. {
  186. struct as10x_bus_adapter_t *bus_adap = priv;
  187. int ret;
  188. if (mutex_lock_interruptible(&bus_adap->lock))
  189. return -EBUSY;
  190. /* send abilis command: GET_TUNE_STATUS */
  191. ret = as10x_cmd_get_demod_stats(bus_adap, demod_stats);
  192. if (ret < 0) {
  193. dev_dbg(&bus_adap->usb_dev->dev,
  194. "as10x_cmd_get_demod_stats failed (probably not tuned)\n");
  195. } else {
  196. dev_dbg(&bus_adap->usb_dev->dev,
  197. "demod status: fc: 0x%08x, bad fc: 0x%08x, bytes corrected: 0x%08x , MER: 0x%04x\n",
  198. demod_stats->frame_count,
  199. demod_stats->bad_frame_count,
  200. demod_stats->bytes_fixed_by_rs,
  201. demod_stats->mer);
  202. }
  203. mutex_unlock(&bus_adap->lock);
  204. return ret;
  205. }
  206. static int as102_stream_ctrl(void *priv, int acquire, uint32_t elna_cfg)
  207. {
  208. struct as10x_bus_adapter_t *bus_adap = priv;
  209. int ret;
  210. if (mutex_lock_interruptible(&bus_adap->lock))
  211. return -EBUSY;
  212. if (acquire) {
  213. if (elna_enable)
  214. as10x_cmd_set_context(bus_adap,
  215. CONTEXT_LNA, elna_cfg);
  216. ret = as10x_cmd_turn_on(bus_adap);
  217. } else {
  218. ret = as10x_cmd_turn_off(bus_adap);
  219. }
  220. mutex_unlock(&bus_adap->lock);
  221. return ret;
  222. }
  223. static const struct as102_fe_ops as102_fe_ops = {
  224. .set_tune = as102_set_tune,
  225. .get_tps = as102_get_tps,
  226. .get_status = as102_get_status,
  227. .get_stats = as102_get_stats,
  228. .stream_ctrl = as102_stream_ctrl,
  229. };
  230. int as102_dvb_register(struct as102_dev_t *as102_dev)
  231. {
  232. struct device *dev = &as102_dev->bus_adap.usb_dev->dev;
  233. int ret;
  234. ret = dvb_register_adapter(&as102_dev->dvb_adap,
  235. as102_dev->name, THIS_MODULE,
  236. dev, adapter_nr);
  237. if (ret < 0) {
  238. dev_err(dev, "%s: dvb_register_adapter() failed: %d\n",
  239. __func__, ret);
  240. return ret;
  241. }
  242. as102_dev->dvb_dmx.priv = as102_dev;
  243. as102_dev->dvb_dmx.filternum = pid_filtering ? 16 : 256;
  244. as102_dev->dvb_dmx.feednum = 256;
  245. as102_dev->dvb_dmx.start_feed = as102_dvb_dmx_start_feed;
  246. as102_dev->dvb_dmx.stop_feed = as102_dvb_dmx_stop_feed;
  247. as102_dev->dvb_dmx.dmx.capabilities = DMX_TS_FILTERING |
  248. DMX_SECTION_FILTERING;
  249. as102_dev->dvb_dmxdev.filternum = as102_dev->dvb_dmx.filternum;
  250. as102_dev->dvb_dmxdev.demux = &as102_dev->dvb_dmx.dmx;
  251. as102_dev->dvb_dmxdev.capabilities = 0;
  252. ret = dvb_dmx_init(&as102_dev->dvb_dmx);
  253. if (ret < 0) {
  254. dev_err(dev, "%s: dvb_dmx_init() failed: %d\n", __func__, ret);
  255. goto edmxinit;
  256. }
  257. ret = dvb_dmxdev_init(&as102_dev->dvb_dmxdev, &as102_dev->dvb_adap);
  258. if (ret < 0) {
  259. dev_err(dev, "%s: dvb_dmxdev_init() failed: %d\n",
  260. __func__, ret);
  261. goto edmxdinit;
  262. }
  263. /* Attach the frontend */
  264. as102_dev->dvb_fe = dvb_attach(as102_attach, as102_dev->name,
  265. &as102_fe_ops,
  266. &as102_dev->bus_adap,
  267. as102_dev->elna_cfg);
  268. if (!as102_dev->dvb_fe) {
  269. ret = -ENODEV;
  270. dev_err(dev, "%s: as102_attach() failed: %d",
  271. __func__, ret);
  272. goto efereg;
  273. }
  274. ret = dvb_register_frontend(&as102_dev->dvb_adap, as102_dev->dvb_fe);
  275. if (ret < 0) {
  276. dev_err(dev, "%s: as102_dvb_register_frontend() failed: %d",
  277. __func__, ret);
  278. goto efereg;
  279. }
  280. /* init bus mutex for token locking */
  281. mutex_init(&as102_dev->bus_adap.lock);
  282. /* init start / stop stream mutex */
  283. mutex_init(&as102_dev->sem);
  284. /*
  285. * try to load as102 firmware. If firmware upload failed, we'll be
  286. * able to upload it later.
  287. */
  288. if (fw_upload)
  289. try_then_request_module(as102_fw_upload(&as102_dev->bus_adap),
  290. "firmware_class");
  291. pr_info("Registered device %s", as102_dev->name);
  292. return 0;
  293. efereg:
  294. dvb_dmxdev_release(&as102_dev->dvb_dmxdev);
  295. edmxdinit:
  296. dvb_dmx_release(&as102_dev->dvb_dmx);
  297. edmxinit:
  298. dvb_unregister_adapter(&as102_dev->dvb_adap);
  299. return ret;
  300. }
  301. void as102_dvb_unregister(struct as102_dev_t *as102_dev)
  302. {
  303. /* unregister as102 frontend */
  304. dvb_unregister_frontend(as102_dev->dvb_fe);
  305. /* detach frontend */
  306. dvb_frontend_detach(as102_dev->dvb_fe);
  307. /* unregister demux device */
  308. dvb_dmxdev_release(&as102_dev->dvb_dmxdev);
  309. dvb_dmx_release(&as102_dev->dvb_dmx);
  310. /* unregister dvb adapter */
  311. dvb_unregister_adapter(&as102_dev->dvb_adap);
  312. pr_info("Unregistered device %s", as102_dev->name);
  313. }
  314. module_usb_driver(as102_usb_driver);
  315. /* modinfo details */
  316. MODULE_DESCRIPTION(DRIVER_FULL_NAME);
  317. MODULE_LICENSE("GPL");
  318. MODULE_AUTHOR("Pierrick Hascoet <pierrick.hascoet@abilis.com>");