c8sectpfe-common.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * c8sectpfe-common.c - C8SECTPFE STi DVB driver
  3. *
  4. * Copyright (c) STMicroelectronics 2015
  5. *
  6. * Author: Peter Griffin <peter.griffin@linaro.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <linux/completion.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/dvb/dmx.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/time.h>
  25. #include <linux/wait.h>
  26. #include "dmxdev.h"
  27. #include "dvbdev.h"
  28. #include "dvb_demux.h"
  29. #include "dvb_frontend.h"
  30. #include "dvb_net.h"
  31. #include "c8sectpfe-common.h"
  32. #include "c8sectpfe-core.h"
  33. #include "c8sectpfe-dvb.h"
  34. static int register_dvb(struct stdemux *demux, struct dvb_adapter *adap,
  35. void *start_feed, void *stop_feed,
  36. struct c8sectpfei *fei)
  37. {
  38. int result;
  39. demux->dvb_demux.dmx.capabilities = DMX_TS_FILTERING |
  40. DMX_SECTION_FILTERING |
  41. DMX_MEMORY_BASED_FILTERING;
  42. demux->dvb_demux.priv = demux;
  43. demux->dvb_demux.filternum = C8SECTPFE_MAXCHANNEL;
  44. demux->dvb_demux.feednum = C8SECTPFE_MAXCHANNEL;
  45. demux->dvb_demux.start_feed = start_feed;
  46. demux->dvb_demux.stop_feed = stop_feed;
  47. demux->dvb_demux.write_to_decoder = NULL;
  48. result = dvb_dmx_init(&demux->dvb_demux);
  49. if (result < 0) {
  50. dev_err(fei->dev, "dvb_dmx_init failed (errno = %d)\n",
  51. result);
  52. goto err_dmx;
  53. }
  54. demux->dmxdev.filternum = demux->dvb_demux.filternum;
  55. demux->dmxdev.demux = &demux->dvb_demux.dmx;
  56. demux->dmxdev.capabilities = 0;
  57. result = dvb_dmxdev_init(&demux->dmxdev, adap);
  58. if (result < 0) {
  59. dev_err(fei->dev, "dvb_dmxdev_init failed (errno = %d)\n",
  60. result);
  61. goto err_dmxdev;
  62. }
  63. demux->hw_frontend.source = DMX_FRONTEND_0 + demux->tsin_index;
  64. result = demux->dvb_demux.dmx.add_frontend(&demux->dvb_demux.dmx,
  65. &demux->hw_frontend);
  66. if (result < 0) {
  67. dev_err(fei->dev, "add_frontend failed (errno = %d)\n", result);
  68. goto err_fe_hw;
  69. }
  70. demux->mem_frontend.source = DMX_MEMORY_FE;
  71. result = demux->dvb_demux.dmx.add_frontend(&demux->dvb_demux.dmx,
  72. &demux->mem_frontend);
  73. if (result < 0) {
  74. dev_err(fei->dev, "add_frontend failed (%d)\n", result);
  75. goto err_fe_mem;
  76. }
  77. result = demux->dvb_demux.dmx.connect_frontend(&demux->dvb_demux.dmx,
  78. &demux->hw_frontend);
  79. if (result < 0) {
  80. dev_err(fei->dev, "connect_frontend (%d)\n", result);
  81. goto err_fe_con;
  82. }
  83. return 0;
  84. err_fe_con:
  85. demux->dvb_demux.dmx.remove_frontend(&demux->dvb_demux.dmx,
  86. &demux->mem_frontend);
  87. err_fe_mem:
  88. demux->dvb_demux.dmx.remove_frontend(&demux->dvb_demux.dmx,
  89. &demux->hw_frontend);
  90. err_fe_hw:
  91. dvb_dmxdev_release(&demux->dmxdev);
  92. err_dmxdev:
  93. dvb_dmx_release(&demux->dvb_demux);
  94. err_dmx:
  95. return result;
  96. }
  97. static void unregister_dvb(struct stdemux *demux)
  98. {
  99. demux->dvb_demux.dmx.remove_frontend(&demux->dvb_demux.dmx,
  100. &demux->mem_frontend);
  101. demux->dvb_demux.dmx.remove_frontend(&demux->dvb_demux.dmx,
  102. &demux->hw_frontend);
  103. dvb_dmxdev_release(&demux->dmxdev);
  104. dvb_dmx_release(&demux->dvb_demux);
  105. }
  106. static struct c8sectpfe *c8sectpfe_create(struct c8sectpfei *fei,
  107. void *start_feed,
  108. void *stop_feed)
  109. {
  110. struct c8sectpfe *c8sectpfe;
  111. int result;
  112. int i, j;
  113. short int ids[] = { -1 };
  114. c8sectpfe = kzalloc(sizeof(struct c8sectpfe), GFP_KERNEL);
  115. if (!c8sectpfe)
  116. goto err1;
  117. mutex_init(&c8sectpfe->lock);
  118. c8sectpfe->device = fei->dev;
  119. result = dvb_register_adapter(&c8sectpfe->adapter, "STi c8sectpfe",
  120. THIS_MODULE, fei->dev, ids);
  121. if (result < 0) {
  122. dev_err(fei->dev, "dvb_register_adapter failed (errno = %d)\n",
  123. result);
  124. goto err2;
  125. }
  126. c8sectpfe->adapter.priv = fei;
  127. for (i = 0; i < fei->tsin_count; i++) {
  128. c8sectpfe->demux[i].tsin_index = i;
  129. c8sectpfe->demux[i].c8sectpfei = fei;
  130. result = register_dvb(&c8sectpfe->demux[i], &c8sectpfe->adapter,
  131. start_feed, stop_feed, fei);
  132. if (result < 0) {
  133. dev_err(fei->dev,
  134. "register_dvb feed=%d failed (errno = %d)\n",
  135. result, i);
  136. /* we take a all or nothing approach */
  137. for (j = 0; j < i; j++)
  138. unregister_dvb(&c8sectpfe->demux[j]);
  139. goto err3;
  140. }
  141. }
  142. c8sectpfe->num_feeds = fei->tsin_count;
  143. return c8sectpfe;
  144. err3:
  145. dvb_unregister_adapter(&c8sectpfe->adapter);
  146. err2:
  147. kfree(c8sectpfe);
  148. err1:
  149. return NULL;
  150. };
  151. static void c8sectpfe_delete(struct c8sectpfe *c8sectpfe)
  152. {
  153. int i;
  154. if (!c8sectpfe)
  155. return;
  156. for (i = 0; i < c8sectpfe->num_feeds; i++)
  157. unregister_dvb(&c8sectpfe->demux[i]);
  158. dvb_unregister_adapter(&c8sectpfe->adapter);
  159. kfree(c8sectpfe);
  160. };
  161. void c8sectpfe_tuner_unregister_frontend(struct c8sectpfe *c8sectpfe,
  162. struct c8sectpfei *fei)
  163. {
  164. int n;
  165. struct channel_info *tsin;
  166. for (n = 0; n < fei->tsin_count; n++) {
  167. tsin = fei->channel_data[n];
  168. if (tsin && tsin->frontend) {
  169. dvb_unregister_frontend(tsin->frontend);
  170. dvb_frontend_detach(tsin->frontend);
  171. }
  172. if (tsin && tsin->i2c_adapter)
  173. i2c_put_adapter(tsin->i2c_adapter);
  174. if (tsin && tsin->i2c_client) {
  175. if (tsin->i2c_client->dev.driver->owner)
  176. module_put(tsin->i2c_client->dev.driver->owner);
  177. i2c_unregister_device(tsin->i2c_client);
  178. }
  179. }
  180. c8sectpfe_delete(c8sectpfe);
  181. };
  182. int c8sectpfe_tuner_register_frontend(struct c8sectpfe **c8sectpfe,
  183. struct c8sectpfei *fei,
  184. void *start_feed,
  185. void *stop_feed)
  186. {
  187. struct channel_info *tsin;
  188. struct dvb_frontend *frontend;
  189. int n, res;
  190. *c8sectpfe = c8sectpfe_create(fei, start_feed, stop_feed);
  191. if (!*c8sectpfe)
  192. return -ENOMEM;
  193. for (n = 0; n < fei->tsin_count; n++) {
  194. tsin = fei->channel_data[n];
  195. res = c8sectpfe_frontend_attach(&frontend, *c8sectpfe, tsin, n);
  196. if (res)
  197. goto err;
  198. res = dvb_register_frontend(&c8sectpfe[0]->adapter, frontend);
  199. if (res < 0) {
  200. dev_err(fei->dev, "dvb_register_frontend failed (%d)\n",
  201. res);
  202. goto err;
  203. }
  204. tsin->frontend = frontend;
  205. }
  206. return 0;
  207. err:
  208. c8sectpfe_tuner_unregister_frontend(*c8sectpfe, fei);
  209. return res;
  210. }