dvb-usb-init.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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-5 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 (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. /* general initialization functions */
  23. static int dvb_usb_exit(struct dvb_usb_device *d)
  24. {
  25. deb_info("state before exiting everything: %x\n",d->state);
  26. dvb_usb_remote_exit(d);
  27. dvb_usb_fe_exit(d);
  28. dvb_usb_i2c_exit(d);
  29. dvb_usb_dvb_exit(d);
  30. dvb_usb_urb_exit(d);
  31. deb_info("state should be zero now: %x\n",d->state);
  32. d->state = DVB_USB_STATE_INIT;
  33. kfree(d->priv);
  34. kfree(d);
  35. return 0;
  36. }
  37. static int dvb_usb_init(struct dvb_usb_device *d)
  38. {
  39. int ret = 0;
  40. mutex_init(&d->usb_mutex);
  41. mutex_init(&d->i2c_mutex);
  42. d->state = DVB_USB_STATE_INIT;
  43. /* check the capabilities and set appropriate variables */
  44. /* speed - when running at FULL speed we need a HW PID filter */
  45. if (d->udev->speed == USB_SPEED_FULL && !(d->props.caps & DVB_USB_HAS_PID_FILTER)) {
  46. err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
  47. return -ENODEV;
  48. }
  49. if ((d->udev->speed == USB_SPEED_FULL && d->props.caps & DVB_USB_HAS_PID_FILTER) ||
  50. (d->props.caps & DVB_USB_NEED_PID_FILTERING)) {
  51. info("will use the device's hardware PID filter (table count: %d).",d->props.pid_filter_count);
  52. d->pid_filtering = 1;
  53. d->max_feed_count = d->props.pid_filter_count;
  54. } else {
  55. info("will pass the complete MPEG2 transport stream to the software demuxer.");
  56. d->pid_filtering = 0;
  57. d->max_feed_count = 255;
  58. }
  59. if (d->props.power_ctrl)
  60. d->props.power_ctrl(d,1);
  61. if ((ret = dvb_usb_urb_init(d)) ||
  62. (ret = dvb_usb_dvb_init(d)) ||
  63. (ret = dvb_usb_i2c_init(d)) ||
  64. (ret = dvb_usb_fe_init(d))) {
  65. dvb_usb_exit(d);
  66. return ret;
  67. }
  68. if ((ret = dvb_usb_remote_init(d)))
  69. err("could not initialize remote control.");
  70. if (d->props.power_ctrl)
  71. d->props.power_ctrl(d,0);
  72. return 0;
  73. }
  74. /* determine the name and the state of the just found USB device */
  75. static struct dvb_usb_device_description * dvb_usb_find_device(struct usb_device *udev,struct dvb_usb_properties *props, int *cold)
  76. {
  77. int i,j;
  78. struct dvb_usb_device_description *desc = NULL;
  79. *cold = -1;
  80. for (i = 0; i < props->num_device_descs; i++) {
  81. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
  82. deb_info("check for cold %x %x\n",props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
  83. if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  84. props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  85. *cold = 1;
  86. desc = &props->devices[i];
  87. break;
  88. }
  89. }
  90. if (desc != NULL)
  91. break;
  92. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
  93. deb_info("check for warm %x %x\n",props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
  94. if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  95. props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  96. *cold = 0;
  97. desc = &props->devices[i];
  98. break;
  99. }
  100. }
  101. }
  102. if (desc != NULL && props->identify_state != NULL)
  103. props->identify_state(udev,props,&desc,cold);
  104. return desc;
  105. }
  106. /*
  107. * USB
  108. */
  109. int dvb_usb_device_init(struct usb_interface *intf, struct dvb_usb_properties
  110. *props, struct module *owner,struct dvb_usb_device **du)
  111. {
  112. struct usb_device *udev = interface_to_usbdev(intf);
  113. struct dvb_usb_device *d = NULL;
  114. struct dvb_usb_device_description *desc = NULL;
  115. int ret = -ENOMEM,cold=0;
  116. if (du != NULL)
  117. *du = NULL;
  118. if ((desc = dvb_usb_find_device(udev,props,&cold)) == NULL) {
  119. deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
  120. return -ENODEV;
  121. }
  122. if (cold) {
  123. info("found a '%s' in cold state, will try to load a firmware",desc->name);
  124. ret = dvb_usb_download_firmware(udev,props);
  125. if (!props->no_reconnect)
  126. return ret;
  127. }
  128. info("found a '%s' in warm state.",desc->name);
  129. d = kzalloc(sizeof(struct dvb_usb_device),GFP_KERNEL);
  130. if (d == NULL) {
  131. err("no memory for 'struct dvb_usb_device'");
  132. return ret;
  133. }
  134. d->udev = udev;
  135. memcpy(&d->props,props,sizeof(struct dvb_usb_properties));
  136. d->desc = desc;
  137. d->owner = owner;
  138. if (d->props.size_of_priv > 0) {
  139. d->priv = kzalloc(d->props.size_of_priv,GFP_KERNEL);
  140. if (d->priv == NULL) {
  141. err("no memory for priv in 'struct dvb_usb_device'");
  142. kfree(d);
  143. return -ENOMEM;
  144. }
  145. }
  146. usb_set_intfdata(intf, d);
  147. if (du != NULL)
  148. *du = d;
  149. ret = dvb_usb_init(d);
  150. if (ret == 0)
  151. info("%s successfully initialized and connected.",desc->name);
  152. else
  153. info("%s error while loading driver (%d)",desc->name,ret);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL(dvb_usb_device_init);
  157. void dvb_usb_device_exit(struct usb_interface *intf)
  158. {
  159. struct dvb_usb_device *d = usb_get_intfdata(intf);
  160. const char *name = "generic DVB-USB module";
  161. usb_set_intfdata(intf,NULL);
  162. if (d != NULL && d->desc != NULL) {
  163. name = d->desc->name;
  164. dvb_usb_exit(d);
  165. }
  166. info("%s successfully deinitialized and disconnected.",name);
  167. }
  168. EXPORT_SYMBOL(dvb_usb_device_exit);
  169. MODULE_VERSION("0.3");
  170. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  171. MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
  172. MODULE_LICENSE("GPL");