dvb-usb-init.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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@posteo.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. ret = dvb_usb_adapter_stream_init(adap);
  74. if (ret)
  75. return ret;
  76. ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs);
  77. if (ret)
  78. goto dvb_init_err;
  79. ret = dvb_usb_adapter_frontend_init(adap);
  80. if (ret)
  81. goto frontend_init_err;
  82. /* use exclusive FE lock if there is multiple shared FEs */
  83. if (adap->fe_adap[1].fe)
  84. adap->dvb_adap.mfe_shared = 1;
  85. d->num_adapters_initialized++;
  86. d->state |= DVB_USB_STATE_DVB;
  87. }
  88. /*
  89. * when reloading the driver w/o replugging the device
  90. * sometimes a timeout occures, this helps
  91. */
  92. if (d->props.generic_bulk_ctrl_endpoint != 0) {
  93. usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
  94. usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
  95. }
  96. return 0;
  97. frontend_init_err:
  98. dvb_usb_adapter_dvb_exit(adap);
  99. dvb_init_err:
  100. dvb_usb_adapter_stream_exit(adap);
  101. return ret;
  102. }
  103. static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
  104. {
  105. int n;
  106. for (n = 0; n < d->num_adapters_initialized; n++) {
  107. dvb_usb_adapter_frontend_exit(&d->adapter[n]);
  108. dvb_usb_adapter_dvb_exit(&d->adapter[n]);
  109. dvb_usb_adapter_stream_exit(&d->adapter[n]);
  110. kfree(d->adapter[n].priv);
  111. }
  112. d->num_adapters_initialized = 0;
  113. d->state &= ~DVB_USB_STATE_DVB;
  114. return 0;
  115. }
  116. /* general initialization functions */
  117. static int dvb_usb_exit(struct dvb_usb_device *d)
  118. {
  119. deb_info("state before exiting everything: %x\n", d->state);
  120. dvb_usb_remote_exit(d);
  121. dvb_usb_adapter_exit(d);
  122. dvb_usb_i2c_exit(d);
  123. deb_info("state should be zero now: %x\n", d->state);
  124. d->state = DVB_USB_STATE_INIT;
  125. kfree(d->priv);
  126. kfree(d);
  127. return 0;
  128. }
  129. static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
  130. {
  131. int ret = 0;
  132. mutex_init(&d->data_mutex);
  133. mutex_init(&d->usb_mutex);
  134. mutex_init(&d->i2c_mutex);
  135. d->state = DVB_USB_STATE_INIT;
  136. if (d->props.size_of_priv > 0) {
  137. d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
  138. if (d->priv == NULL) {
  139. err("no memory for priv in 'struct dvb_usb_device'");
  140. return -ENOMEM;
  141. }
  142. }
  143. /* check the capabilities and set appropriate variables */
  144. dvb_usb_device_power_ctrl(d, 1);
  145. if ((ret = dvb_usb_i2c_init(d)) ||
  146. (ret = dvb_usb_adapter_init(d, adapter_nums))) {
  147. dvb_usb_exit(d);
  148. return ret;
  149. }
  150. if ((ret = dvb_usb_remote_init(d)))
  151. err("could not initialize remote control.");
  152. dvb_usb_device_power_ctrl(d, 0);
  153. return 0;
  154. }
  155. /* determine the name and the state of the just found USB device */
  156. static struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, struct dvb_usb_device_properties *props, int *cold)
  157. {
  158. int i, j;
  159. struct dvb_usb_device_description *desc = NULL;
  160. *cold = -1;
  161. for (i = 0; i < props->num_device_descs; i++) {
  162. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
  163. deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
  164. if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  165. props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  166. *cold = 1;
  167. desc = &props->devices[i];
  168. break;
  169. }
  170. }
  171. if (desc != NULL)
  172. break;
  173. for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
  174. deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
  175. if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
  176. props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
  177. *cold = 0;
  178. desc = &props->devices[i];
  179. break;
  180. }
  181. }
  182. }
  183. if (desc != NULL && props->identify_state != NULL)
  184. props->identify_state(udev, props, &desc, cold);
  185. return desc;
  186. }
  187. int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
  188. {
  189. if (onoff)
  190. d->powered++;
  191. else
  192. d->powered--;
  193. if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
  194. deb_info("power control: %d\n", onoff);
  195. if (d->props.power_ctrl)
  196. return d->props.power_ctrl(d, onoff);
  197. }
  198. return 0;
  199. }
  200. /*
  201. * USB
  202. */
  203. int dvb_usb_device_init(struct usb_interface *intf,
  204. struct dvb_usb_device_properties *props,
  205. struct module *owner, struct dvb_usb_device **du,
  206. short *adapter_nums)
  207. {
  208. struct usb_device *udev = interface_to_usbdev(intf);
  209. struct dvb_usb_device *d = NULL;
  210. struct dvb_usb_device_description *desc = NULL;
  211. int ret = -ENOMEM, cold = 0;
  212. if (du != NULL)
  213. *du = NULL;
  214. if ((desc = dvb_usb_find_device(udev, props, &cold)) == NULL) {
  215. deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
  216. return -ENODEV;
  217. }
  218. if (cold) {
  219. info("found a '%s' in cold state, will try to load a firmware", desc->name);
  220. ret = dvb_usb_download_firmware(udev, props);
  221. if (!props->no_reconnect || ret != 0)
  222. return ret;
  223. }
  224. info("found a '%s' in warm state.", desc->name);
  225. d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
  226. if (d == NULL) {
  227. err("no memory for 'struct dvb_usb_device'");
  228. return -ENOMEM;
  229. }
  230. d->udev = udev;
  231. memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
  232. d->desc = desc;
  233. d->owner = owner;
  234. usb_set_intfdata(intf, d);
  235. if (du != NULL)
  236. *du = d;
  237. ret = dvb_usb_init(d, adapter_nums);
  238. if (ret == 0)
  239. info("%s successfully initialized and connected.", desc->name);
  240. else
  241. info("%s error while loading driver (%d)", desc->name, ret);
  242. return ret;
  243. }
  244. EXPORT_SYMBOL(dvb_usb_device_init);
  245. void dvb_usb_device_exit(struct usb_interface *intf)
  246. {
  247. struct dvb_usb_device *d = usb_get_intfdata(intf);
  248. const char *default_name = "generic DVB-USB module";
  249. char name[40];
  250. usb_set_intfdata(intf, NULL);
  251. if (d != NULL && d->desc != NULL) {
  252. strscpy(name, d->desc->name, sizeof(name));
  253. dvb_usb_exit(d);
  254. } else {
  255. strscpy(name, default_name, sizeof(name));
  256. }
  257. info("%s successfully deinitialized and disconnected.", name);
  258. }
  259. EXPORT_SYMBOL(dvb_usb_device_exit);
  260. MODULE_VERSION("1.0");
  261. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
  262. MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
  263. MODULE_LICENSE("GPL");