2 * DVB USB library - provides a generic interface for a DVB USB device driver.
6 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
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.
12 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
14 #include "dvb-usb-common.h"
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,mem=128,uxfer=256 (or-able))." DVB_USB_DEBUG_STATUS
);
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).");
25 static int dvb_usb_force_pid_filter_usage
;
26 module_param_named(force_pid_filter_usage
, dvb_usb_force_pid_filter_usage
, int, 0444);
27 MODULE_PARM_DESC(force_pid_filter_usage
, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
29 static int dvb_usb_adapter_init(struct dvb_usb_device
*d
, short *adapter_nrs
)
31 struct dvb_usb_adapter
*adap
;
34 for (n
= 0; n
< d
->props
.num_adapters
; n
++) {
35 adap
= &d
->adapter
[n
];
39 memcpy(&adap
->props
, &d
->props
.adapter
[n
], sizeof(struct dvb_usb_adapter_properties
));
41 for (o
= 0; o
< adap
->props
.num_frontends
; o
++) {
42 struct dvb_usb_adapter_fe_properties
*props
= &adap
->props
.fe
[o
];
43 /* speed - when running at FULL speed we need a HW PID filter */
44 if (d
->udev
->speed
== USB_SPEED_FULL
&& !(props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
)) {
45 err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
49 if ((d
->udev
->speed
== USB_SPEED_FULL
&& props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
) ||
50 (props
->caps
& DVB_USB_ADAP_NEED_PID_FILTERING
)) {
51 info("will use the device's hardware PID filter (table count: %d).", props
->pid_filter_count
);
52 adap
->fe_adap
[o
].pid_filtering
= 1;
53 adap
->fe_adap
[o
].max_feed_count
= props
->pid_filter_count
;
55 info("will pass the complete MPEG2 transport stream to the software demuxer.");
56 adap
->fe_adap
[o
].pid_filtering
= 0;
57 adap
->fe_adap
[o
].max_feed_count
= 255;
60 if (!adap
->fe_adap
[o
].pid_filtering
&&
61 dvb_usb_force_pid_filter_usage
&&
62 props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
) {
63 info("pid filter enabled by module option.");
64 adap
->fe_adap
[o
].pid_filtering
= 1;
65 adap
->fe_adap
[o
].max_feed_count
= props
->pid_filter_count
;
68 if (props
->size_of_priv
> 0) {
69 adap
->fe_adap
[o
].priv
= kzalloc(props
->size_of_priv
, GFP_KERNEL
);
70 if (adap
->fe_adap
[o
].priv
== NULL
) {
71 err("no memory for priv for adapter %d fe %d.", n
, o
);
77 if (adap
->props
.size_of_priv
> 0) {
78 adap
->priv
= kzalloc(adap
->props
.size_of_priv
, GFP_KERNEL
);
79 if (adap
->priv
== NULL
) {
80 err("no memory for priv for adapter %d.", n
);
85 if ((ret
= dvb_usb_adapter_stream_init(adap
)) ||
86 (ret
= dvb_usb_adapter_dvb_init(adap
, adapter_nrs
)) ||
87 (ret
= dvb_usb_adapter_frontend_init(adap
))) {
91 /* use exclusive FE lock if there is multiple shared FEs */
92 if (adap
->fe_adap
[1].fe
)
93 adap
->dvb_adap
.mfe_shared
= 1;
95 d
->num_adapters_initialized
++;
96 d
->state
|= DVB_USB_STATE_DVB
;
100 * when reloading the driver w/o replugging the device
101 * sometimes a timeout occures, this helps
103 if (d
->props
.generic_bulk_ctrl_endpoint
!= 0) {
104 usb_clear_halt(d
->udev
, usb_sndbulkpipe(d
->udev
, d
->props
.generic_bulk_ctrl_endpoint
));
105 usb_clear_halt(d
->udev
, usb_rcvbulkpipe(d
->udev
, d
->props
.generic_bulk_ctrl_endpoint
));
111 static int dvb_usb_adapter_exit(struct dvb_usb_device
*d
)
115 for (n
= 0; n
< d
->num_adapters_initialized
; n
++) {
116 dvb_usb_adapter_frontend_exit(&d
->adapter
[n
]);
117 dvb_usb_adapter_dvb_exit(&d
->adapter
[n
]);
118 dvb_usb_adapter_stream_exit(&d
->adapter
[n
]);
119 kfree(d
->adapter
[n
].priv
);
121 d
->num_adapters_initialized
= 0;
122 d
->state
&= ~DVB_USB_STATE_DVB
;
127 /* general initialization functions */
128 static int dvb_usb_exit(struct dvb_usb_device
*d
)
130 deb_info("state before exiting everything: %x\n", d
->state
);
131 dvb_usb_remote_exit(d
);
132 dvb_usb_adapter_exit(d
);
134 deb_info("state should be zero now: %x\n", d
->state
);
135 d
->state
= DVB_USB_STATE_INIT
;
141 static int dvb_usb_init(struct dvb_usb_device
*d
, short *adapter_nums
)
145 mutex_init(&d
->data_mutex
);
146 mutex_init(&d
->usb_mutex
);
147 mutex_init(&d
->i2c_mutex
);
149 d
->state
= DVB_USB_STATE_INIT
;
151 if (d
->props
.size_of_priv
> 0) {
152 d
->priv
= kzalloc(d
->props
.size_of_priv
, GFP_KERNEL
);
153 if (d
->priv
== NULL
) {
154 err("no memory for priv in 'struct dvb_usb_device'");
159 /* check the capabilities and set appropriate variables */
160 dvb_usb_device_power_ctrl(d
, 1);
162 if ((ret
= dvb_usb_i2c_init(d
)) ||
163 (ret
= dvb_usb_adapter_init(d
, adapter_nums
))) {
168 if ((ret
= dvb_usb_remote_init(d
)))
169 err("could not initialize remote control.");
171 dvb_usb_device_power_ctrl(d
, 0);
176 /* determine the name and the state of the just found USB device */
177 static struct dvb_usb_device_description
*dvb_usb_find_device(struct usb_device
*udev
, struct dvb_usb_device_properties
*props
, int *cold
)
180 struct dvb_usb_device_description
*desc
= NULL
;
184 for (i
= 0; i
< props
->num_device_descs
; i
++) {
186 for (j
= 0; j
< DVB_USB_ID_MAX_NUM
&& props
->devices
[i
].cold_ids
[j
] != NULL
; j
++) {
187 deb_info("check for cold %x %x\n", props
->devices
[i
].cold_ids
[j
]->idVendor
, props
->devices
[i
].cold_ids
[j
]->idProduct
);
188 if (props
->devices
[i
].cold_ids
[j
]->idVendor
== le16_to_cpu(udev
->descriptor
.idVendor
) &&
189 props
->devices
[i
].cold_ids
[j
]->idProduct
== le16_to_cpu(udev
->descriptor
.idProduct
)) {
191 desc
= &props
->devices
[i
];
199 for (j
= 0; j
< DVB_USB_ID_MAX_NUM
&& props
->devices
[i
].warm_ids
[j
] != NULL
; j
++) {
200 deb_info("check for warm %x %x\n", props
->devices
[i
].warm_ids
[j
]->idVendor
, props
->devices
[i
].warm_ids
[j
]->idProduct
);
201 if (props
->devices
[i
].warm_ids
[j
]->idVendor
== le16_to_cpu(udev
->descriptor
.idVendor
) &&
202 props
->devices
[i
].warm_ids
[j
]->idProduct
== le16_to_cpu(udev
->descriptor
.idProduct
)) {
204 desc
= &props
->devices
[i
];
210 if (desc
!= NULL
&& props
->identify_state
!= NULL
)
211 props
->identify_state(udev
, props
, &desc
, cold
);
216 int dvb_usb_device_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
223 if (d
->powered
== 0 || (onoff
&& d
->powered
== 1)) { /* when switching from 1 to 0 or from 0 to 1 */
224 deb_info("power control: %d\n", onoff
);
225 if (d
->props
.power_ctrl
)
226 return d
->props
.power_ctrl(d
, onoff
);
234 int dvb_usb_device_init(struct usb_interface
*intf
,
235 struct dvb_usb_device_properties
*props
,
236 struct module
*owner
, struct dvb_usb_device
**du
,
239 struct usb_device
*udev
= interface_to_usbdev(intf
);
240 struct dvb_usb_device
*d
= NULL
;
241 struct dvb_usb_device_description
*desc
= NULL
;
243 int ret
= -ENOMEM
, cold
= 0;
248 if ((desc
= dvb_usb_find_device(udev
, props
, &cold
)) == NULL
) {
249 deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
254 info("found a '%s' in cold state, will try to load a firmware", desc
->name
);
255 ret
= dvb_usb_download_firmware(udev
, props
);
256 if (!props
->no_reconnect
|| ret
!= 0)
260 info("found a '%s' in warm state.", desc
->name
);
261 d
= kzalloc(sizeof(struct dvb_usb_device
), GFP_KERNEL
);
263 err("no memory for 'struct dvb_usb_device'");
268 memcpy(&d
->props
, props
, sizeof(struct dvb_usb_device_properties
));
272 usb_set_intfdata(intf
, d
);
277 ret
= dvb_usb_init(d
, adapter_nums
);
280 info("%s successfully initialized and connected.", desc
->name
);
282 info("%s error while loading driver (%d)", desc
->name
, ret
);
285 EXPORT_SYMBOL(dvb_usb_device_init
);
287 void dvb_usb_device_exit(struct usb_interface
*intf
)
289 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
290 const char *name
= "generic DVB-USB module";
292 usb_set_intfdata(intf
, NULL
);
293 if (d
!= NULL
&& d
->desc
!= NULL
) {
294 name
= d
->desc
->name
;
297 info("%s successfully deinitialized and disconnected.", name
);
300 EXPORT_SYMBOL(dvb_usb_device_exit
);
302 MODULE_VERSION("1.0");
303 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
304 MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
305 MODULE_LICENSE("GPL");