dvb-usb-init.c 5.9 KB

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