dvb-usb-init.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * DVB USB library - provides a generic interface for a DVB USB device driver.
  3. *
  4. * dvb-usb-init.c
  5. *
  6. * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation, version 2.
  11. *
  12. * see Documentation/dvb/README.dvb-usb for more information
  13. */
  14. #include "dvb-usb-common.h"
  15. /* debug */
  16. int dvb_usb_debug;
  17. module_param_named(debug, dvb_usb_debug, int, 0644);
  18. MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64,mem=128,uxfer=256 (or-able))." DVB_USB_DEBUG_STATUS);
  19. int dvb_usb_disable_rc_polling;
  20. module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644);
  21. MODULE_PARM_DESC(disable_rc_polling, "disable remote control polling (default: 0).");
  22. static int dvb_usb_force_pid_filter_usage;
  23. module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444);
  24. MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
  25. static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs)
  26. {
  27. struct dvb_usb_adapter *adap;
  28. int ret, n, o;
  29. for (n = 0; n < d->props.num_adapters; n++) {
  30. adap = &d->adapter[n];
  31. adap->dev = d;
  32. adap->id = n;
  33. memcpy(&adap->props, &d->props.adapter[n], sizeof(struct dvb_usb_adapter_properties));
  34. for (o = 0; o < adap->props.num_frontends; o++) {
  35. struct dvb_usb_adapter_fe_properties *props = &adap->props.fe[o];
  36. /* speed - when running at FULL speed we need a HW PID filter */
  37. if (d->udev->speed == USB_SPEED_FULL && !(props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
  38. err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
  39. return -ENODEV;
  40. }
  41. if ((d->udev->speed == USB_SPEED_FULL && props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
  42. (props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
  43. info("will use the device's hardware PID filter (table count: %d).", props->pid_filter_count);
  44. adap->fe_adap[o].pid_filtering = 1;
  45. adap->fe_adap[o].max_feed_count = props->pid_filter_count;
  46. } else {
  47. info("will pass the complete MPEG2 transport stream to the software demuxer.");
  48. adap->fe_adap[o].pid_filtering = 0;
  49. adap->fe_adap[o].max_feed_count = 255;
  50. }
  51. if (!adap->fe_adap[o].pid_filtering &&
  52. dvb_usb_force_pid_filter_usage &&
  53. props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
  54. info("pid filter enabled by module option.");
  55. adap->fe_adap[o].pid_filtering = 1;
  56. adap->fe_adap[o].max_feed_count = props->pid_filter_count;
  57. }
  58. if (props->size_of_priv > 0) {
  59. adap->fe_adap[o].priv = kzalloc(props->size_of_priv, GFP_KERNEL);
  60. if (adap->fe_adap[o].priv == NULL) {
  61. err("no memory for priv for adapter %d fe %d.", n, o);
  62. return -ENOMEM;
  63. }
  64. }
  65. }
  66. if (adap->props.size_of_priv > 0) {
  67. adap->priv = kzalloc(adap->props.size_of_priv, GFP_KERNEL);
  68. if (adap->priv == NULL) {
  69. err("no memory for priv for adapter %d.", n);
  70. return -ENOMEM;
  71. }
  72. }
  73. if ((ret = dvb_usb_adapter_stream_init(adap)) ||
  74. (ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs)) ||
  75. (ret = dvb_usb_adapter_frontend_init(adap))) {
  76. return ret;
  77. }
  78. /* use exclusive FE lock if there is multiple shared FEs */
  79. if (adap->fe_adap[1].fe)
  80. adap->dvb_adap.mfe_shared = 1;
  81. d->num_adapters_initialized++;
  82. d->state |= DVB_USB_STATE_DVB;
  83. }
  84. /*
  85. * when reloading the driver w/o replugging the device
  86. * sometimes a timeout occures, this helps
  87. */
  88. if (d->props.generic_bulk_ctrl_endpoint != 0) {
  89. usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
  90. usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
  91. }
  92. return 0;
  93. }
  94. static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
  95. {
  96. int n;
  97. for (n = 0; n < d->num_adapters_initialized; n++) {
  98. dvb_usb_adapter_frontend_exit(&d->adapter[n]);
  99. dvb_usb_adapter_dvb_exit(&d->adapter[n]);
  100. dvb_usb_adapter_stream_exit(&d->adapter[n]);
  101. kfree(d->adapter[n].priv);
  102. }
  103. d->num_adapters_initialized = 0;
  104. d->state &= ~DVB_USB_STATE_DVB;
  105. return 0;
  106. }
  107. /* general initialization functions */
  108. static int dvb_usb_exit(struct dvb_usb_device *d)
  109. {
  110. deb_info("state before exiting everything: %x\n", d->state);
  111. dvb_usb_remote_exit(d);
  112. dvb_usb_adapter_exit(d);
  113. dvb_usb_i2c_exit(d);
  114. deb_info("state should be zero now: %x\n", d->state);
  115. d->state = DVB_USB_STATE_INIT;
  116. kfree(d->priv);
  117. kfree(d);
  118. return 0;
  119. }
  120. static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
  121. {
  122. int ret = 0;
  123. mutex_init(&d->usb_mutex);
  124. mutex_init(&d->i2c_mutex);
  125. d->state = DVB_USB_STATE_INIT;
  126. if (d->props.size_of_priv > 0) {
  127. d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
  128. if (d->priv == NULL) {
  129. err("no memory for priv in 'struct dvb_usb_device'");
  130. return -ENOMEM;
  131. }
  132. }
  133. /* check the capabilities and set appropriate variables */
  134. dvb_usb_device_power_ctrl(d, 1);
  135. if ((ret = dvb_usb_i2c_init(d)) ||
  136. (ret = dvb_usb_adapter_init(d, adapter_nums))) {
  137. dvb_usb_exit(d);
  138. return ret;
  139. }
  140. if ((ret = dvb_usb_remote_init(d)))
  141. err("could not initialize remote control.");
  142. dvb_usb_device_power_ctrl(d, 0);
  143. return 0;
  144. }
  145. /* determine the name and the state of the just found USB device */
  146. static struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, struct dvb_usb_device_properties *props, int *cold)
  147. {
  148. int i, j;
  149. struct dvb_usb_device_description *desc = NULL;
  150. *cold = -1;
  151. for (i = 0; i < props->num_device_descs; i++) {
  152. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
  153. deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
  154. if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  155. props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  156. *cold = 1;
  157. desc = &props->devices[i];
  158. break;
  159. }
  160. }
  161. if (desc != NULL)
  162. break;
  163. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
  164. deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
  165. if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  166. props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  167. *cold = 0;
  168. desc = &props->devices[i];
  169. break;
  170. }
  171. }
  172. }
  173. if (desc != NULL && props->identify_state != NULL)
  174. props->identify_state(udev, props, &desc, cold);
  175. return desc;
  176. }
  177. int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
  178. {
  179. if (onoff)
  180. d->powered++;
  181. else
  182. d->powered--;
  183. if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
  184. deb_info("power control: %d\n", onoff);
  185. if (d->props.power_ctrl)
  186. return d->props.power_ctrl(d, onoff);
  187. }
  188. return 0;
  189. }
  190. /*
  191. * USB
  192. */
  193. int dvb_usb_device_init(struct usb_interface *intf,
  194. struct dvb_usb_device_properties *props,
  195. struct module *owner, struct dvb_usb_device **du,
  196. short *adapter_nums)
  197. {
  198. struct usb_device *udev = interface_to_usbdev(intf);
  199. struct dvb_usb_device *d = NULL;
  200. struct dvb_usb_device_description *desc = NULL;
  201. int ret = -ENOMEM, cold = 0;
  202. if (du != NULL)
  203. *du = NULL;
  204. if ((desc = dvb_usb_find_device(udev, props, &cold)) == NULL) {
  205. deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
  206. return -ENODEV;
  207. }
  208. if (cold) {
  209. info("found a '%s' in cold state, will try to load a firmware", desc->name);
  210. ret = dvb_usb_download_firmware(udev, props);
  211. if (!props->no_reconnect || ret != 0)
  212. return ret;
  213. }
  214. info("found a '%s' in warm state.", desc->name);
  215. d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
  216. if (d == NULL) {
  217. err("no memory for 'struct dvb_usb_device'");
  218. return -ENOMEM;
  219. }
  220. d->udev = udev;
  221. memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
  222. d->desc = desc;
  223. d->owner = owner;
  224. usb_set_intfdata(intf, d);
  225. if (du != NULL)
  226. *du = d;
  227. ret = dvb_usb_init(d, adapter_nums);
  228. if (ret == 0)
  229. info("%s successfully initialized and connected.", desc->name);
  230. else
  231. info("%s error while loading driver (%d)", desc->name, ret);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL(dvb_usb_device_init);
  235. void dvb_usb_device_exit(struct usb_interface *intf)
  236. {
  237. struct dvb_usb_device *d = usb_get_intfdata(intf);
  238. const char *name = "generic DVB-USB module";
  239. usb_set_intfdata(intf, NULL);
  240. if (d != NULL && d->desc != NULL) {
  241. name = d->desc->name;
  242. dvb_usb_exit(d);
  243. }
  244. info("%s successfully deinitialized and disconnected.", name);
  245. }
  246. EXPORT_SYMBOL(dvb_usb_device_exit);
  247. MODULE_VERSION("1.0");
  248. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  249. MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
  250. MODULE_LICENSE("GPL");