dvb-usb-init.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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;
  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. /* speed - when running at FULL speed we need a HW PID filter */
  35. if (d->udev->speed == USB_SPEED_FULL && !(adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
  36. err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
  37. return -ENODEV;
  38. }
  39. if ((d->udev->speed == USB_SPEED_FULL && adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
  40. (adap->props.caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
  41. info("will use the device's hardware PID filter (table count: %d).",adap->props.pid_filter_count);
  42. adap->pid_filtering = 1;
  43. adap->max_feed_count = adap->props.pid_filter_count;
  44. } else {
  45. info("will pass the complete MPEG2 transport stream to the software demuxer.");
  46. adap->pid_filtering = 0;
  47. adap->max_feed_count = 255;
  48. }
  49. if (!adap->pid_filtering &&
  50. dvb_usb_force_pid_filter_usage &&
  51. adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) {
  52. info("pid filter enabled by module option.");
  53. adap->pid_filtering = 1;
  54. adap->max_feed_count = adap->props.pid_filter_count;
  55. }
  56. if (adap->props.size_of_priv > 0) {
  57. adap->priv = kzalloc(adap->props.size_of_priv,GFP_KERNEL);
  58. if (adap->priv == NULL) {
  59. err("no memory for priv for adapter %d.",n);
  60. return -ENOMEM;
  61. }
  62. }
  63. if ((ret = dvb_usb_adapter_stream_init(adap)) ||
  64. (ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs)) ||
  65. (ret = dvb_usb_adapter_frontend_init(adap))) {
  66. return ret;
  67. }
  68. d->num_adapters_initialized++;
  69. d->state |= DVB_USB_STATE_DVB;
  70. }
  71. /*
  72. * when reloading the driver w/o replugging the device
  73. * sometimes a timeout occures, this helps
  74. */
  75. if (d->props.generic_bulk_ctrl_endpoint != 0) {
  76. usb_clear_halt(d->udev,usb_sndbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
  77. usb_clear_halt(d->udev,usb_rcvbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
  78. }
  79. return 0;
  80. }
  81. static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
  82. {
  83. int n;
  84. for (n = 0; n < d->num_adapters_initialized; n++) {
  85. dvb_usb_adapter_frontend_exit(&d->adapter[n]);
  86. dvb_usb_adapter_dvb_exit(&d->adapter[n]);
  87. dvb_usb_adapter_stream_exit(&d->adapter[n]);
  88. kfree(d->adapter[n].priv);
  89. }
  90. d->num_adapters_initialized = 0;
  91. d->state &= ~DVB_USB_STATE_DVB;
  92. return 0;
  93. }
  94. /* general initialization functions */
  95. static int dvb_usb_exit(struct dvb_usb_device *d)
  96. {
  97. deb_info("state before exiting everything: %x\n",d->state);
  98. dvb_usb_remote_exit(d);
  99. dvb_usb_adapter_exit(d);
  100. dvb_usb_i2c_exit(d);
  101. deb_info("state should be zero now: %x\n",d->state);
  102. d->state = DVB_USB_STATE_INIT;
  103. kfree(d->priv);
  104. kfree(d);
  105. return 0;
  106. }
  107. static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
  108. {
  109. int ret = 0;
  110. mutex_init(&d->usb_mutex);
  111. mutex_init(&d->i2c_mutex);
  112. d->state = DVB_USB_STATE_INIT;
  113. if (d->props.size_of_priv > 0) {
  114. d->priv = kzalloc(d->props.size_of_priv,GFP_KERNEL);
  115. if (d->priv == NULL) {
  116. err("no memory for priv in 'struct dvb_usb_device'");
  117. return -ENOMEM;
  118. }
  119. }
  120. /* check the capabilities and set appropriate variables */
  121. dvb_usb_device_power_ctrl(d, 1);
  122. if ((ret = dvb_usb_i2c_init(d)) ||
  123. (ret = dvb_usb_adapter_init(d, adapter_nums))) {
  124. dvb_usb_exit(d);
  125. return ret;
  126. }
  127. if ((ret = dvb_usb_remote_init(d)))
  128. err("could not initialize remote control.");
  129. dvb_usb_device_power_ctrl(d, 0);
  130. return 0;
  131. }
  132. /* determine the name and the state of the just found USB device */
  133. static struct dvb_usb_device_description * dvb_usb_find_device(struct usb_device *udev,struct dvb_usb_device_properties *props, int *cold)
  134. {
  135. int i,j;
  136. struct dvb_usb_device_description *desc = NULL;
  137. *cold = -1;
  138. for (i = 0; i < props->num_device_descs; i++) {
  139. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
  140. deb_info("check for cold %x %x\n",props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
  141. if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  142. props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  143. *cold = 1;
  144. desc = &props->devices[i];
  145. break;
  146. }
  147. }
  148. if (desc != NULL)
  149. break;
  150. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
  151. deb_info("check for warm %x %x\n",props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
  152. if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  153. props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  154. *cold = 0;
  155. desc = &props->devices[i];
  156. break;
  157. }
  158. }
  159. }
  160. if (desc != NULL && props->identify_state != NULL)
  161. props->identify_state(udev,props,&desc,cold);
  162. return desc;
  163. }
  164. int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
  165. {
  166. if (onoff)
  167. d->powered++;
  168. else
  169. d->powered--;
  170. if (d->powered == 0 || (onoff && d->powered == 1)) { // when switching from 1 to 0 or from 0 to 1
  171. deb_info("power control: %d\n", onoff);
  172. if (d->props.power_ctrl)
  173. return d->props.power_ctrl(d, onoff);
  174. }
  175. return 0;
  176. }
  177. /*
  178. * USB
  179. */
  180. int dvb_usb_device_init(struct usb_interface *intf,
  181. struct dvb_usb_device_properties *props,
  182. struct module *owner, struct dvb_usb_device **du,
  183. short *adapter_nums)
  184. {
  185. struct usb_device *udev = interface_to_usbdev(intf);
  186. struct dvb_usb_device *d = NULL;
  187. struct dvb_usb_device_description *desc = NULL;
  188. int ret = -ENOMEM,cold=0;
  189. if (du != NULL)
  190. *du = NULL;
  191. if ((desc = dvb_usb_find_device(udev,props,&cold)) == NULL) {
  192. deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
  193. return -ENODEV;
  194. }
  195. if (cold) {
  196. info("found a '%s' in cold state, will try to load a firmware",desc->name);
  197. ret = dvb_usb_download_firmware(udev,props);
  198. if (!props->no_reconnect || ret != 0)
  199. return ret;
  200. }
  201. info("found a '%s' in warm state.",desc->name);
  202. d = kzalloc(sizeof(struct dvb_usb_device),GFP_KERNEL);
  203. if (d == NULL) {
  204. err("no memory for 'struct dvb_usb_device'");
  205. return ret;
  206. }
  207. d->udev = udev;
  208. memcpy(&d->props,props,sizeof(struct dvb_usb_device_properties));
  209. d->desc = desc;
  210. d->owner = owner;
  211. usb_set_intfdata(intf, d);
  212. if (du != NULL)
  213. *du = d;
  214. ret = dvb_usb_init(d, adapter_nums);
  215. if (ret == 0)
  216. info("%s successfully initialized and connected.",desc->name);
  217. else
  218. info("%s error while loading driver (%d)",desc->name,ret);
  219. return ret;
  220. }
  221. EXPORT_SYMBOL(dvb_usb_device_init);
  222. void dvb_usb_device_exit(struct usb_interface *intf)
  223. {
  224. struct dvb_usb_device *d = usb_get_intfdata(intf);
  225. const char *name = "generic DVB-USB module";
  226. usb_set_intfdata(intf,NULL);
  227. if (d != NULL && d->desc != NULL) {
  228. name = d->desc->name;
  229. dvb_usb_exit(d);
  230. }
  231. info("%s successfully deinitialized and disconnected.",name);
  232. }
  233. EXPORT_SYMBOL(dvb_usb_device_exit);
  234. MODULE_VERSION("1.0");
  235. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  236. MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
  237. MODULE_LICENSE("GPL");