Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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