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