tm6000-dvb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * tm6000-dvb.c - dvb-t support for TM5600/TM6000/TM6010 USB video capture devices
  3. *
  4. * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.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 version 2
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb.h>
  22. #include "tm6000.h"
  23. #include "tm6000-regs.h"
  24. #include "zl10353.h"
  25. #include <media/tuner.h>
  26. #include "tuner-xc2028.h"
  27. #include "xc5000.h"
  28. MODULE_DESCRIPTION("DVB driver extension module for tm5600/6000/6010 based TV cards");
  29. MODULE_AUTHOR("Mauro Carvalho Chehab");
  30. MODULE_LICENSE("GPL");
  31. MODULE_SUPPORTED_DEVICE("{{Trident, tm5600},"
  32. "{{Trident, tm6000},"
  33. "{{Trident, tm6010}");
  34. static int debug;
  35. module_param(debug, int, 0644);
  36. MODULE_PARM_DESC(debug, "enable debug message");
  37. static inline void print_err_status(struct tm6000_core *dev,
  38. int packet, int status)
  39. {
  40. char *errmsg = "Unknown";
  41. switch (status) {
  42. case -ENOENT:
  43. errmsg = "unlinked synchronuously";
  44. break;
  45. case -ECONNRESET:
  46. errmsg = "unlinked asynchronuously";
  47. break;
  48. case -ENOSR:
  49. errmsg = "Buffer error (overrun)";
  50. break;
  51. case -EPIPE:
  52. errmsg = "Stalled (device not responding)";
  53. break;
  54. case -EOVERFLOW:
  55. errmsg = "Babble (bad cable?)";
  56. break;
  57. case -EPROTO:
  58. errmsg = "Bit-stuff error (bad cable?)";
  59. break;
  60. case -EILSEQ:
  61. errmsg = "CRC/Timeout (could be anything)";
  62. break;
  63. case -ETIME:
  64. errmsg = "Device does not respond";
  65. break;
  66. }
  67. if (packet < 0) {
  68. dprintk(dev, 1, "URB status %d [%s].\n",
  69. status, errmsg);
  70. } else {
  71. dprintk(dev, 1, "URB packet %d, status %d [%s].\n",
  72. packet, status, errmsg);
  73. }
  74. }
  75. static void tm6000_urb_received(struct urb *urb)
  76. {
  77. int ret;
  78. struct tm6000_core *dev = urb->context;
  79. switch (urb->status) {
  80. case 0:
  81. case -ETIMEDOUT:
  82. break;
  83. case -ENOENT:
  84. case -ECONNRESET:
  85. case -ESHUTDOWN:
  86. return;
  87. default:
  88. print_err_status(dev, 0, urb->status);
  89. }
  90. if (urb->actual_length > 0)
  91. dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
  92. urb->actual_length);
  93. if (dev->dvb->streams > 0) {
  94. ret = usb_submit_urb(urb, GFP_ATOMIC);
  95. if (ret < 0) {
  96. printk(KERN_ERR "tm6000: error %s\n", __func__);
  97. kfree(urb->transfer_buffer);
  98. usb_free_urb(urb);
  99. }
  100. }
  101. }
  102. static int tm6000_start_stream(struct tm6000_core *dev)
  103. {
  104. int ret;
  105. unsigned int pipe, size;
  106. struct tm6000_dvb *dvb = dev->dvb;
  107. printk(KERN_INFO "tm6000: got start stream request %s\n", __func__);
  108. if (dev->mode != TM6000_MODE_DIGITAL) {
  109. tm6000_init_digital_mode(dev);
  110. dev->mode = TM6000_MODE_DIGITAL;
  111. }
  112. dvb->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
  113. if (dvb->bulk_urb == NULL) {
  114. printk(KERN_ERR "tm6000: couldn't allocate urb\n");
  115. return -ENOMEM;
  116. }
  117. pipe = usb_rcvbulkpipe(dev->udev, dev->bulk_in.endp->desc.bEndpointAddress
  118. & USB_ENDPOINT_NUMBER_MASK);
  119. size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
  120. size = size * 15; /* 512 x 8 or 12 or 15 */
  121. dvb->bulk_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
  122. if (dvb->bulk_urb->transfer_buffer == NULL) {
  123. usb_free_urb(dvb->bulk_urb);
  124. printk(KERN_ERR "tm6000: couldn't allocate transfer buffer!\n");
  125. return -ENOMEM;
  126. }
  127. usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
  128. dvb->bulk_urb->transfer_buffer,
  129. size,
  130. tm6000_urb_received, dev);
  131. ret = usb_clear_halt(dev->udev, pipe);
  132. if (ret < 0) {
  133. printk(KERN_ERR "tm6000: error %i in %s during pipe reset\n",
  134. ret, __func__);
  135. return ret;
  136. } else
  137. printk(KERN_ERR "tm6000: pipe resetted\n");
  138. /* mutex_lock(&tm6000_driver.open_close_mutex); */
  139. ret = usb_submit_urb(dvb->bulk_urb, GFP_ATOMIC);
  140. /* mutex_unlock(&tm6000_driver.open_close_mutex); */
  141. if (ret) {
  142. printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",
  143. ret);
  144. kfree(dvb->bulk_urb->transfer_buffer);
  145. usb_free_urb(dvb->bulk_urb);
  146. return ret;
  147. }
  148. return 0;
  149. }
  150. static void tm6000_stop_stream(struct tm6000_core *dev)
  151. {
  152. struct tm6000_dvb *dvb = dev->dvb;
  153. if (dvb->bulk_urb) {
  154. printk(KERN_INFO "urb killing\n");
  155. usb_kill_urb(dvb->bulk_urb);
  156. printk(KERN_INFO "urb buffer free\n");
  157. kfree(dvb->bulk_urb->transfer_buffer);
  158. usb_free_urb(dvb->bulk_urb);
  159. dvb->bulk_urb = NULL;
  160. }
  161. }
  162. static int tm6000_start_feed(struct dvb_demux_feed *feed)
  163. {
  164. struct dvb_demux *demux = feed->demux;
  165. struct tm6000_core *dev = demux->priv;
  166. struct tm6000_dvb *dvb = dev->dvb;
  167. printk(KERN_INFO "tm6000: got start feed request %s\n", __func__);
  168. mutex_lock(&dvb->mutex);
  169. if (dvb->streams == 0) {
  170. dvb->streams = 1;
  171. /* mutex_init(&tm6000_dev->streming_mutex); */
  172. tm6000_start_stream(dev);
  173. } else
  174. ++(dvb->streams);
  175. mutex_unlock(&dvb->mutex);
  176. return 0;
  177. }
  178. static int tm6000_stop_feed(struct dvb_demux_feed *feed)
  179. {
  180. struct dvb_demux *demux = feed->demux;
  181. struct tm6000_core *dev = demux->priv;
  182. struct tm6000_dvb *dvb = dev->dvb;
  183. printk(KERN_INFO "tm6000: got stop feed request %s\n", __func__);
  184. mutex_lock(&dvb->mutex);
  185. printk(KERN_INFO "stream %#x\n", dvb->streams);
  186. --(dvb->streams);
  187. if (dvb->streams == 0) {
  188. printk(KERN_INFO "stop stream\n");
  189. tm6000_stop_stream(dev);
  190. /* mutex_destroy(&tm6000_dev->streaming_mutex); */
  191. }
  192. mutex_unlock(&dvb->mutex);
  193. /* mutex_destroy(&tm6000_dev->streaming_mutex); */
  194. return 0;
  195. }
  196. static int tm6000_dvb_attach_frontend(struct tm6000_core *dev)
  197. {
  198. struct tm6000_dvb *dvb = dev->dvb;
  199. if (dev->caps.has_zl10353) {
  200. struct zl10353_config config = {
  201. .demod_address = dev->demod_addr,
  202. .no_tuner = 1,
  203. .parallel_ts = 1,
  204. .if2 = 45700,
  205. .disable_i2c_gate_ctrl = 1,
  206. };
  207. dvb->frontend = dvb_attach(zl10353_attach, &config,
  208. &dev->i2c_adap);
  209. } else {
  210. printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
  211. return -1;
  212. }
  213. return (!dvb->frontend) ? -1 : 0;
  214. }
  215. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  216. static int register_dvb(struct tm6000_core *dev)
  217. {
  218. int ret = -1;
  219. struct tm6000_dvb *dvb = dev->dvb;
  220. mutex_init(&dvb->mutex);
  221. dvb->streams = 0;
  222. /* attach the frontend */
  223. ret = tm6000_dvb_attach_frontend(dev);
  224. if (ret < 0) {
  225. printk(KERN_ERR "tm6000: couldn't attach the frontend!\n");
  226. goto err;
  227. }
  228. ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
  229. THIS_MODULE, &dev->udev->dev, adapter_nr);
  230. if (ret < 0) {
  231. pr_err("tm6000: couldn't register the adapter!\n");
  232. goto err;
  233. }
  234. dvb->adapter.priv = dev;
  235. if (dvb->frontend) {
  236. switch (dev->tuner_type) {
  237. case TUNER_XC2028: {
  238. struct xc2028_config cfg = {
  239. .i2c_adap = &dev->i2c_adap,
  240. .i2c_addr = dev->tuner_addr,
  241. };
  242. dvb->frontend->callback = tm6000_tuner_callback;
  243. ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
  244. if (ret < 0) {
  245. printk(KERN_ERR
  246. "tm6000: couldn't register frontend\n");
  247. goto adapter_err;
  248. }
  249. if (!dvb_attach(xc2028_attach, dvb->frontend, &cfg)) {
  250. printk(KERN_ERR "tm6000: couldn't register "
  251. "frontend (xc3028)\n");
  252. ret = -EINVAL;
  253. goto frontend_err;
  254. }
  255. printk(KERN_INFO "tm6000: XC2028/3028 asked to be "
  256. "attached to frontend!\n");
  257. break;
  258. }
  259. case TUNER_XC5000: {
  260. struct xc5000_config cfg = {
  261. .i2c_address = dev->tuner_addr,
  262. };
  263. dvb->frontend->callback = tm6000_xc5000_callback;
  264. ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
  265. if (ret < 0) {
  266. printk(KERN_ERR
  267. "tm6000: couldn't register frontend\n");
  268. goto adapter_err;
  269. }
  270. if (!dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap, &cfg)) {
  271. printk(KERN_ERR "tm6000: couldn't register "
  272. "frontend (xc5000)\n");
  273. ret = -EINVAL;
  274. goto frontend_err;
  275. }
  276. printk(KERN_INFO "tm6000: XC5000 asked to be "
  277. "attached to frontend!\n");
  278. break;
  279. }
  280. }
  281. } else
  282. printk(KERN_ERR "tm6000: no frontend found\n");
  283. dvb->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING
  284. | DMX_MEMORY_BASED_FILTERING;
  285. dvb->demux.priv = dev;
  286. dvb->demux.filternum = 8;
  287. dvb->demux.feednum = 8;
  288. dvb->demux.start_feed = tm6000_start_feed;
  289. dvb->demux.stop_feed = tm6000_stop_feed;
  290. dvb->demux.write_to_decoder = NULL;
  291. ret = dvb_dmx_init(&dvb->demux);
  292. if (ret < 0) {
  293. printk(KERN_ERR "tm6000: dvb_dmx_init failed (errno = %d)\n", ret);
  294. goto frontend_err;
  295. }
  296. dvb->dmxdev.filternum = dev->dvb->demux.filternum;
  297. dvb->dmxdev.demux = &dev->dvb->demux.dmx;
  298. dvb->dmxdev.capabilities = 0;
  299. ret = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
  300. if (ret < 0) {
  301. printk(KERN_ERR "tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret);
  302. goto dvb_dmx_err;
  303. }
  304. return 0;
  305. dvb_dmx_err:
  306. dvb_dmx_release(&dvb->demux);
  307. frontend_err:
  308. if (dvb->frontend) {
  309. dvb_unregister_frontend(dvb->frontend);
  310. dvb_frontend_detach(dvb->frontend);
  311. }
  312. adapter_err:
  313. dvb_unregister_adapter(&dvb->adapter);
  314. err:
  315. return ret;
  316. }
  317. static void unregister_dvb(struct tm6000_core *dev)
  318. {
  319. struct tm6000_dvb *dvb = dev->dvb;
  320. if (dvb->bulk_urb != NULL) {
  321. struct urb *bulk_urb = dvb->bulk_urb;
  322. kfree(bulk_urb->transfer_buffer);
  323. bulk_urb->transfer_buffer = NULL;
  324. usb_unlink_urb(bulk_urb);
  325. usb_free_urb(bulk_urb);
  326. }
  327. /* mutex_lock(&tm6000_driver.open_close_mutex); */
  328. if (dvb->frontend) {
  329. dvb_unregister_frontend(dvb->frontend);
  330. dvb_frontend_detach(dvb->frontend);
  331. }
  332. dvb_dmxdev_release(&dvb->dmxdev);
  333. dvb_dmx_release(&dvb->demux);
  334. dvb_unregister_adapter(&dvb->adapter);
  335. mutex_destroy(&dvb->mutex);
  336. /* mutex_unlock(&tm6000_driver.open_close_mutex); */
  337. }
  338. static int dvb_init(struct tm6000_core *dev)
  339. {
  340. struct tm6000_dvb *dvb;
  341. int rc;
  342. if (!dev)
  343. return 0;
  344. if (!dev->caps.has_dvb)
  345. return 0;
  346. if (dev->udev->speed == USB_SPEED_FULL) {
  347. printk(KERN_INFO "This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)\n");
  348. return 0;
  349. }
  350. dvb = kzalloc(sizeof(struct tm6000_dvb), GFP_KERNEL);
  351. if (!dvb) {
  352. printk(KERN_INFO "Cannot allocate memory\n");
  353. return -ENOMEM;
  354. }
  355. dev->dvb = dvb;
  356. rc = register_dvb(dev);
  357. if (rc < 0) {
  358. kfree(dvb);
  359. dev->dvb = NULL;
  360. return 0;
  361. }
  362. return 0;
  363. }
  364. static int dvb_fini(struct tm6000_core *dev)
  365. {
  366. if (!dev)
  367. return 0;
  368. if (!dev->caps.has_dvb)
  369. return 0;
  370. if (dev->dvb) {
  371. unregister_dvb(dev);
  372. kfree(dev->dvb);
  373. dev->dvb = NULL;
  374. }
  375. return 0;
  376. }
  377. static struct tm6000_ops dvb_ops = {
  378. .type = TM6000_DVB,
  379. .name = "TM6000 dvb Extension",
  380. .init = dvb_init,
  381. .fini = dvb_fini,
  382. };
  383. static int __init tm6000_dvb_register(void)
  384. {
  385. return tm6000_register_extension(&dvb_ops);
  386. }
  387. static void __exit tm6000_dvb_unregister(void)
  388. {
  389. tm6000_unregister_extension(&dvb_ops);
  390. }
  391. module_init(tm6000_dvb_register);
  392. module_exit(tm6000_dvb_unregister);