dvb-usb-init.c (9762B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * DVB USB library - provides a generic interface for a DVB USB device driver. 4 * 5 * dvb-usb-init.c 6 * 7 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de) 8 * 9 * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information 10 */ 11#include "dvb-usb-common.h" 12 13/* debug */ 14int dvb_usb_debug; 15module_param_named(debug, dvb_usb_debug, int, 0644); 16MODULE_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); 17 18int dvb_usb_disable_rc_polling; 19module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644); 20MODULE_PARM_DESC(disable_rc_polling, "disable remote control polling (default: 0)."); 21 22static int dvb_usb_force_pid_filter_usage; 23module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444); 24MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0)."); 25 26static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs) 27{ 28 struct dvb_usb_adapter *adap; 29 int ret, n, o; 30 31 for (n = 0; n < d->props.num_adapters; n++) { 32 adap = &d->adapter[n]; 33 adap->dev = d; 34 adap->id = n; 35 36 memcpy(&adap->props, &d->props.adapter[n], sizeof(struct dvb_usb_adapter_properties)); 37 38 for (o = 0; o < adap->props.num_frontends; o++) { 39 struct dvb_usb_adapter_fe_properties *props = &adap->props.fe[o]; 40 /* speed - when running at FULL speed we need a HW PID filter */ 41 if (d->udev->speed == USB_SPEED_FULL && !(props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) { 42 err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)"); 43 return -ENODEV; 44 } 45 46 if ((d->udev->speed == USB_SPEED_FULL && props->caps & DVB_USB_ADAP_HAS_PID_FILTER) || 47 (props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) { 48 info("will use the device's hardware PID filter (table count: %d).", props->pid_filter_count); 49 adap->fe_adap[o].pid_filtering = 1; 50 adap->fe_adap[o].max_feed_count = props->pid_filter_count; 51 } else { 52 info("will pass the complete MPEG2 transport stream to the software demuxer."); 53 adap->fe_adap[o].pid_filtering = 0; 54 adap->fe_adap[o].max_feed_count = 255; 55 } 56 57 if (!adap->fe_adap[o].pid_filtering && 58 dvb_usb_force_pid_filter_usage && 59 props->caps & DVB_USB_ADAP_HAS_PID_FILTER) { 60 info("pid filter enabled by module option."); 61 adap->fe_adap[o].pid_filtering = 1; 62 adap->fe_adap[o].max_feed_count = props->pid_filter_count; 63 } 64 65 if (props->size_of_priv > 0) { 66 adap->fe_adap[o].priv = kzalloc(props->size_of_priv, GFP_KERNEL); 67 if (adap->fe_adap[o].priv == NULL) { 68 err("no memory for priv for adapter %d fe %d.", n, o); 69 return -ENOMEM; 70 } 71 } 72 } 73 74 if (adap->props.size_of_priv > 0) { 75 adap->priv = kzalloc(adap->props.size_of_priv, GFP_KERNEL); 76 if (adap->priv == NULL) { 77 err("no memory for priv for adapter %d.", n); 78 return -ENOMEM; 79 } 80 } 81 82 ret = dvb_usb_adapter_stream_init(adap); 83 if (ret) 84 return ret; 85 86 ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs); 87 if (ret) 88 goto dvb_init_err; 89 90 ret = dvb_usb_adapter_frontend_init(adap); 91 if (ret) 92 goto frontend_init_err; 93 94 /* use exclusive FE lock if there is multiple shared FEs */ 95 if (adap->fe_adap[1].fe) 96 adap->dvb_adap.mfe_shared = 1; 97 98 d->num_adapters_initialized++; 99 d->state |= DVB_USB_STATE_DVB; 100 } 101 102 /* 103 * when reloading the driver w/o replugging the device 104 * sometimes a timeout occurs, this helps 105 */ 106 if (d->props.generic_bulk_ctrl_endpoint != 0) { 107 usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint)); 108 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint)); 109 } 110 111 return 0; 112 113frontend_init_err: 114 dvb_usb_adapter_dvb_exit(adap); 115dvb_init_err: 116 dvb_usb_adapter_stream_exit(adap); 117 return ret; 118} 119 120static int dvb_usb_adapter_exit(struct dvb_usb_device *d) 121{ 122 int n; 123 124 for (n = 0; n < d->num_adapters_initialized; n++) { 125 dvb_usb_adapter_frontend_exit(&d->adapter[n]); 126 dvb_usb_adapter_dvb_exit(&d->adapter[n]); 127 dvb_usb_adapter_stream_exit(&d->adapter[n]); 128 kfree(d->adapter[n].priv); 129 } 130 d->num_adapters_initialized = 0; 131 d->state &= ~DVB_USB_STATE_DVB; 132 return 0; 133} 134 135 136/* general initialization functions */ 137static int dvb_usb_exit(struct dvb_usb_device *d) 138{ 139 deb_info("state before exiting everything: %x\n", d->state); 140 dvb_usb_remote_exit(d); 141 dvb_usb_adapter_exit(d); 142 dvb_usb_i2c_exit(d); 143 deb_info("state should be zero now: %x\n", d->state); 144 d->state = DVB_USB_STATE_INIT; 145 146 if (d->priv != NULL && d->props.priv_destroy != NULL) 147 d->props.priv_destroy(d); 148 149 kfree(d->priv); 150 kfree(d); 151 return 0; 152} 153 154static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums) 155{ 156 int ret = 0; 157 158 mutex_init(&d->data_mutex); 159 mutex_init(&d->usb_mutex); 160 mutex_init(&d->i2c_mutex); 161 162 d->state = DVB_USB_STATE_INIT; 163 164 if (d->props.size_of_priv > 0) { 165 d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL); 166 if (d->priv == NULL) { 167 err("no memory for priv in 'struct dvb_usb_device'"); 168 return -ENOMEM; 169 } 170 171 if (d->props.priv_init != NULL) { 172 ret = d->props.priv_init(d); 173 if (ret != 0) 174 goto err_priv_init; 175 } 176 } 177 178 /* check the capabilities and set appropriate variables */ 179 dvb_usb_device_power_ctrl(d, 1); 180 181 ret = dvb_usb_i2c_init(d); 182 if (ret) 183 goto err_i2c_init; 184 ret = dvb_usb_adapter_init(d, adapter_nums); 185 if (ret) 186 goto err_adapter_init; 187 188 if ((ret = dvb_usb_remote_init(d))) 189 err("could not initialize remote control."); 190 191 dvb_usb_device_power_ctrl(d, 0); 192 193 return 0; 194 195err_adapter_init: 196 dvb_usb_adapter_exit(d); 197 dvb_usb_i2c_exit(d); 198err_i2c_init: 199 if (d->priv && d->props.priv_destroy) 200 d->props.priv_destroy(d); 201err_priv_init: 202 kfree(d->priv); 203 d->priv = NULL; 204 return ret; 205} 206 207/* determine the name and the state of the just found USB device */ 208static const struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, const struct dvb_usb_device_properties *props, int *cold) 209{ 210 int i, j; 211 const struct dvb_usb_device_description *desc = NULL; 212 213 *cold = -1; 214 215 for (i = 0; i < props->num_device_descs; i++) { 216 217 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) { 218 deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct); 219 if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) && 220 props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) { 221 *cold = 1; 222 desc = &props->devices[i]; 223 break; 224 } 225 } 226 227 if (desc != NULL) 228 break; 229 230 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) { 231 deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct); 232 if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) && 233 props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) { 234 *cold = 0; 235 desc = &props->devices[i]; 236 break; 237 } 238 } 239 } 240 241 if (desc != NULL && props->identify_state != NULL) 242 props->identify_state(udev, props, &desc, cold); 243 244 return desc; 245} 246 247int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff) 248{ 249 if (onoff) 250 d->powered++; 251 else 252 d->powered--; 253 254 if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */ 255 deb_info("power control: %d\n", onoff); 256 if (d->props.power_ctrl) 257 return d->props.power_ctrl(d, onoff); 258 } 259 return 0; 260} 261 262/* 263 * USB 264 */ 265int dvb_usb_device_init(struct usb_interface *intf, 266 const struct dvb_usb_device_properties *props, 267 struct module *owner, struct dvb_usb_device **du, 268 short *adapter_nums) 269{ 270 struct usb_device *udev = interface_to_usbdev(intf); 271 struct dvb_usb_device *d = NULL; 272 const struct dvb_usb_device_description *desc = NULL; 273 274 int ret = -ENOMEM, cold = 0; 275 276 if (du != NULL) 277 *du = NULL; 278 279 d = kzalloc(sizeof(*d), GFP_KERNEL); 280 if (!d) { 281 err("no memory for 'struct dvb_usb_device'"); 282 return -ENOMEM; 283 } 284 285 memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties)); 286 287 desc = dvb_usb_find_device(udev, &d->props, &cold); 288 if (!desc) { 289 deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n"); 290 ret = -ENODEV; 291 goto error; 292 } 293 294 if (cold) { 295 info("found a '%s' in cold state, will try to load a firmware", desc->name); 296 ret = dvb_usb_download_firmware(udev, props); 297 if (!props->no_reconnect || ret != 0) 298 goto error; 299 } 300 301 info("found a '%s' in warm state.", desc->name); 302 d->udev = udev; 303 d->desc = desc; 304 d->owner = owner; 305 306 usb_set_intfdata(intf, d); 307 308 ret = dvb_usb_init(d, adapter_nums); 309 if (ret) { 310 info("%s error while loading driver (%d)", desc->name, ret); 311 goto error; 312 } 313 314 if (du) 315 *du = d; 316 317 info("%s successfully initialized and connected.", desc->name); 318 return 0; 319 320 error: 321 usb_set_intfdata(intf, NULL); 322 kfree(d); 323 return ret; 324} 325EXPORT_SYMBOL(dvb_usb_device_init); 326 327void dvb_usb_device_exit(struct usb_interface *intf) 328{ 329 struct dvb_usb_device *d = usb_get_intfdata(intf); 330 const char *default_name = "generic DVB-USB module"; 331 char name[40]; 332 333 usb_set_intfdata(intf, NULL); 334 if (d != NULL && d->desc != NULL) { 335 strscpy(name, d->desc->name, sizeof(name)); 336 dvb_usb_exit(d); 337 } else { 338 strscpy(name, default_name, sizeof(name)); 339 } 340 info("%s successfully deinitialized and disconnected.", name); 341 342} 343EXPORT_SYMBOL(dvb_usb_device_exit); 344 345MODULE_VERSION("1.0"); 346MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>"); 347MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices"); 348MODULE_LICENSE("GPL");