Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * DVB USB library - provides a generic interface for a DVB USB device driver.
0004  *
0005  * dvb-usb-init.c
0006  *
0007  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
0008  *
0009  * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
0010  */
0011 #include "dvb-usb-common.h"
0012 
0013 /* debug */
0014 int dvb_usb_debug;
0015 module_param_named(debug, dvb_usb_debug, int, 0644);
0016 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);
0017 
0018 int dvb_usb_disable_rc_polling;
0019 module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644);
0020 MODULE_PARM_DESC(disable_rc_polling, "disable remote control polling (default: 0).");
0021 
0022 static int dvb_usb_force_pid_filter_usage;
0023 module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444);
0024 MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
0025 
0026 static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs)
0027 {
0028     struct dvb_usb_adapter *adap;
0029     int ret, n, o;
0030 
0031     for (n = 0; n < d->props.num_adapters; n++) {
0032         adap = &d->adapter[n];
0033         adap->dev = d;
0034         adap->id  = n;
0035 
0036         memcpy(&adap->props, &d->props.adapter[n], sizeof(struct dvb_usb_adapter_properties));
0037 
0038         for (o = 0; o < adap->props.num_frontends; o++) {
0039             struct dvb_usb_adapter_fe_properties *props = &adap->props.fe[o];
0040             /* speed - when running at FULL speed we need a HW PID filter */
0041             if (d->udev->speed == USB_SPEED_FULL && !(props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
0042                 err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
0043                 return -ENODEV;
0044             }
0045 
0046             if ((d->udev->speed == USB_SPEED_FULL && props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
0047                 (props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
0048                 info("will use the device's hardware PID filter (table count: %d).", props->pid_filter_count);
0049                 adap->fe_adap[o].pid_filtering  = 1;
0050                 adap->fe_adap[o].max_feed_count = props->pid_filter_count;
0051             } else {
0052                 info("will pass the complete MPEG2 transport stream to the software demuxer.");
0053                 adap->fe_adap[o].pid_filtering  = 0;
0054                 adap->fe_adap[o].max_feed_count = 255;
0055             }
0056 
0057             if (!adap->fe_adap[o].pid_filtering &&
0058                 dvb_usb_force_pid_filter_usage &&
0059                 props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
0060                 info("pid filter enabled by module option.");
0061                 adap->fe_adap[o].pid_filtering  = 1;
0062                 adap->fe_adap[o].max_feed_count = props->pid_filter_count;
0063             }
0064 
0065             if (props->size_of_priv > 0) {
0066                 adap->fe_adap[o].priv = kzalloc(props->size_of_priv, GFP_KERNEL);
0067                 if (adap->fe_adap[o].priv == NULL) {
0068                     err("no memory for priv for adapter %d fe %d.", n, o);
0069                     return -ENOMEM;
0070                 }
0071             }
0072         }
0073 
0074         if (adap->props.size_of_priv > 0) {
0075             adap->priv = kzalloc(adap->props.size_of_priv, GFP_KERNEL);
0076             if (adap->priv == NULL) {
0077                 err("no memory for priv for adapter %d.", n);
0078                 return -ENOMEM;
0079             }
0080         }
0081 
0082         ret = dvb_usb_adapter_stream_init(adap);
0083         if (ret)
0084             return ret;
0085 
0086         ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs);
0087         if (ret)
0088             goto dvb_init_err;
0089 
0090         ret = dvb_usb_adapter_frontend_init(adap);
0091         if (ret)
0092             goto frontend_init_err;
0093 
0094         /* use exclusive FE lock if there is multiple shared FEs */
0095         if (adap->fe_adap[1].fe)
0096             adap->dvb_adap.mfe_shared = 1;
0097 
0098         d->num_adapters_initialized++;
0099         d->state |= DVB_USB_STATE_DVB;
0100     }
0101 
0102     /*
0103      * when reloading the driver w/o replugging the device
0104      * sometimes a timeout occurs, this helps
0105      */
0106     if (d->props.generic_bulk_ctrl_endpoint != 0) {
0107         usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
0108         usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
0109     }
0110 
0111     return 0;
0112 
0113 frontend_init_err:
0114     dvb_usb_adapter_dvb_exit(adap);
0115 dvb_init_err:
0116     dvb_usb_adapter_stream_exit(adap);
0117     return ret;
0118 }
0119 
0120 static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
0121 {
0122     int n;
0123 
0124     for (n = 0; n < d->num_adapters_initialized; n++) {
0125         dvb_usb_adapter_frontend_exit(&d->adapter[n]);
0126         dvb_usb_adapter_dvb_exit(&d->adapter[n]);
0127         dvb_usb_adapter_stream_exit(&d->adapter[n]);
0128         kfree(d->adapter[n].priv);
0129     }
0130     d->num_adapters_initialized = 0;
0131     d->state &= ~DVB_USB_STATE_DVB;
0132     return 0;
0133 }
0134 
0135 
0136 /* general initialization functions */
0137 static int dvb_usb_exit(struct dvb_usb_device *d)
0138 {
0139     deb_info("state before exiting everything: %x\n", d->state);
0140     dvb_usb_remote_exit(d);
0141     dvb_usb_adapter_exit(d);
0142     dvb_usb_i2c_exit(d);
0143     deb_info("state should be zero now: %x\n", d->state);
0144     d->state = DVB_USB_STATE_INIT;
0145 
0146     if (d->priv != NULL && d->props.priv_destroy != NULL)
0147         d->props.priv_destroy(d);
0148 
0149     kfree(d->priv);
0150     kfree(d);
0151     return 0;
0152 }
0153 
0154 static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
0155 {
0156     int ret = 0;
0157 
0158     mutex_init(&d->data_mutex);
0159     mutex_init(&d->usb_mutex);
0160     mutex_init(&d->i2c_mutex);
0161 
0162     d->state = DVB_USB_STATE_INIT;
0163 
0164     if (d->props.size_of_priv > 0) {
0165         d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
0166         if (d->priv == NULL) {
0167             err("no memory for priv in 'struct dvb_usb_device'");
0168             return -ENOMEM;
0169         }
0170 
0171         if (d->props.priv_init != NULL) {
0172             ret = d->props.priv_init(d);
0173             if (ret != 0)
0174                 goto err_priv_init;
0175         }
0176     }
0177 
0178     /* check the capabilities and set appropriate variables */
0179     dvb_usb_device_power_ctrl(d, 1);
0180 
0181     ret = dvb_usb_i2c_init(d);
0182     if (ret)
0183         goto err_i2c_init;
0184     ret = dvb_usb_adapter_init(d, adapter_nums);
0185     if (ret)
0186         goto err_adapter_init;
0187 
0188     if ((ret = dvb_usb_remote_init(d)))
0189         err("could not initialize remote control.");
0190 
0191     dvb_usb_device_power_ctrl(d, 0);
0192 
0193     return 0;
0194 
0195 err_adapter_init:
0196     dvb_usb_adapter_exit(d);
0197     dvb_usb_i2c_exit(d);
0198 err_i2c_init:
0199     if (d->priv && d->props.priv_destroy)
0200         d->props.priv_destroy(d);
0201 err_priv_init:
0202     kfree(d->priv);
0203     d->priv = NULL;
0204     return ret;
0205 }
0206 
0207 /* determine the name and the state of the just found USB device */
0208 static const struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, const struct dvb_usb_device_properties *props, int *cold)
0209 {
0210     int i, j;
0211     const struct dvb_usb_device_description *desc = NULL;
0212 
0213     *cold = -1;
0214 
0215     for (i = 0; i < props->num_device_descs; i++) {
0216 
0217         for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
0218             deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
0219             if (props->devices[i].cold_ids[j]->idVendor  == le16_to_cpu(udev->descriptor.idVendor) &&
0220                 props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
0221                 *cold = 1;
0222                 desc = &props->devices[i];
0223                 break;
0224             }
0225         }
0226 
0227         if (desc != NULL)
0228             break;
0229 
0230         for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
0231             deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
0232             if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
0233                 props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
0234                 *cold = 0;
0235                 desc = &props->devices[i];
0236                 break;
0237             }
0238         }
0239     }
0240 
0241     if (desc != NULL && props->identify_state != NULL)
0242         props->identify_state(udev, props, &desc, cold);
0243 
0244     return desc;
0245 }
0246 
0247 int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
0248 {
0249     if (onoff)
0250         d->powered++;
0251     else
0252         d->powered--;
0253 
0254     if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
0255         deb_info("power control: %d\n", onoff);
0256         if (d->props.power_ctrl)
0257             return d->props.power_ctrl(d, onoff);
0258     }
0259     return 0;
0260 }
0261 
0262 /*
0263  * USB
0264  */
0265 int dvb_usb_device_init(struct usb_interface *intf,
0266             const struct dvb_usb_device_properties *props,
0267             struct module *owner, struct dvb_usb_device **du,
0268             short *adapter_nums)
0269 {
0270     struct usb_device *udev = interface_to_usbdev(intf);
0271     struct dvb_usb_device *d = NULL;
0272     const struct dvb_usb_device_description *desc = NULL;
0273 
0274     int ret = -ENOMEM, cold = 0;
0275 
0276     if (du != NULL)
0277         *du = NULL;
0278 
0279     d = kzalloc(sizeof(*d), GFP_KERNEL);
0280     if (!d) {
0281         err("no memory for 'struct dvb_usb_device'");
0282         return -ENOMEM;
0283     }
0284 
0285     memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
0286 
0287     desc = dvb_usb_find_device(udev, &d->props, &cold);
0288     if (!desc) {
0289         deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
0290         ret = -ENODEV;
0291         goto error;
0292     }
0293 
0294     if (cold) {
0295         info("found a '%s' in cold state, will try to load a firmware", desc->name);
0296         ret = dvb_usb_download_firmware(udev, props);
0297         if (!props->no_reconnect || ret != 0)
0298             goto error;
0299     }
0300 
0301     info("found a '%s' in warm state.", desc->name);
0302     d->udev = udev;
0303     d->desc = desc;
0304     d->owner = owner;
0305 
0306     usb_set_intfdata(intf, d);
0307 
0308     ret = dvb_usb_init(d, adapter_nums);
0309     if (ret) {
0310         info("%s error while loading driver (%d)", desc->name, ret);
0311         goto error;
0312     }
0313 
0314     if (du)
0315         *du = d;
0316 
0317     info("%s successfully initialized and connected.", desc->name);
0318     return 0;
0319 
0320  error:
0321     usb_set_intfdata(intf, NULL);
0322     kfree(d);
0323     return ret;
0324 }
0325 EXPORT_SYMBOL(dvb_usb_device_init);
0326 
0327 void dvb_usb_device_exit(struct usb_interface *intf)
0328 {
0329     struct dvb_usb_device *d = usb_get_intfdata(intf);
0330     const char *default_name = "generic DVB-USB module";
0331     char name[40];
0332 
0333     usb_set_intfdata(intf, NULL);
0334     if (d != NULL && d->desc != NULL) {
0335         strscpy(name, d->desc->name, sizeof(name));
0336         dvb_usb_exit(d);
0337     } else {
0338         strscpy(name, default_name, sizeof(name));
0339     }
0340     info("%s successfully deinitialized and disconnected.", name);
0341 
0342 }
0343 EXPORT_SYMBOL(dvb_usb_device_exit);
0344 
0345 MODULE_VERSION("1.0");
0346 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
0347 MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
0348 MODULE_LICENSE("GPL");