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