1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * DVB USB framework
4  *
5  * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de>
6  * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
7  */
8 
9 #ifndef DVB_USB_H
10 #define DVB_USB_H
11 
12 #include <linux/usb/input.h>
13 #include <linux/firmware.h>
14 #include <media/rc-core.h>
15 #include <media/media-device.h>
16 
17 #include <media/dvb_frontend.h>
18 #include <media/dvb_demux.h>
19 #include <media/dvb_net.h>
20 #include <media/dmxdev.h>
21 #include <media/dvb-usb-ids.h>
22 
23 /*
24  * device file: /dev/dvb/adapter[0-1]/frontend[0-2]
25  *
26  * |-- device
27  * |   |-- adapter0
28  * |   |   |-- frontend0
29  * |   |   |-- frontend1
30  * |   |   `-- frontend2
31  * |   `-- adapter1
32  * |       |-- frontend0
33  * |       |-- frontend1
34  * |       `-- frontend2
35  *
36  *
37  * Commonly used variable names:
38  * d = pointer to device (struct dvb_usb_device *)
39  * adap = pointer to adapter (struct dvb_usb_adapter *)
40  * fe = pointer to frontend (struct dvb_frontend *)
41  *
42  * Use macros defined in that file to resolve needed pointers.
43  */
44 
45 /* helper macros for every DVB USB driver use */
46 #define adap_to_d(adap) (container_of(adap, struct dvb_usb_device, \
47 		adapter[adap->id]))
48 #define adap_to_priv(adap) (adap_to_d(adap)->priv)
49 #define fe_to_adap(fe) ((struct dvb_usb_adapter *) ((fe)->dvb->priv))
50 #define fe_to_d(fe) (adap_to_d(fe_to_adap(fe)))
51 #define fe_to_priv(fe) (fe_to_d(fe)->priv)
52 #define d_to_priv(d) (d->priv)
53 
54 #define dvb_usb_dbg_usb_control_msg(udev, r, t, v, i, b, l) { \
55 	char *direction; \
56 	if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
57 		direction = ">>>"; \
58 	else \
59 		direction = "<<<"; \
60 	dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
61 			"%s %*ph\n",  __func__, t, r, v & 0xff, v >> 8, \
62 			i & 0xff, i >> 8, l & 0xff, l >> 8, direction, l, b); \
63 }
64 
65 #define DVB_USB_STREAM_BULK(endpoint_, count_, size_) { \
66 	.type = USB_BULK, \
67 	.count = count_, \
68 	.endpoint = endpoint_, \
69 	.u = { \
70 		.bulk = { \
71 			.buffersize = size_, \
72 		} \
73 	} \
74 }
75 
76 #define DVB_USB_STREAM_ISOC(endpoint_, count_, frames_, size_, interval_) { \
77 	.type = USB_ISOC, \
78 	.count = count_, \
79 	.endpoint = endpoint_, \
80 	.u = { \
81 		.isoc = { \
82 			.framesperurb = frames_, \
83 			.framesize = size_,\
84 			.interval = interval_, \
85 		} \
86 	} \
87 }
88 
89 #define DVB_USB_DEVICE(vend, prod, props_, name_, rc) \
90 	.match_flags = USB_DEVICE_ID_MATCH_DEVICE, \
91 	.idVendor = (vend), \
92 	.idProduct = (prod), \
93 	.driver_info = (kernel_ulong_t) &((const struct dvb_usb_driver_info) { \
94 		.props = (props_), \
95 		.name = (name_), \
96 		.rc_map = (rc), \
97 	})
98 
99 struct dvb_usb_device;
100 struct dvb_usb_adapter;
101 
102 /**
103  * struct dvb_usb_driver_info - structure for carrying all needed data from the
104  *	device driver to the general
105  * dvb usb routines
106  * @name: device name
107  * @rc_map: name of rc codes table
108  * @props: structure containing all device properties
109  */
110 struct dvb_usb_driver_info {
111 	const char *name;
112 	const char *rc_map;
113 	const struct dvb_usb_device_properties *props;
114 };
115 
116 /**
117  * struct dvb_usb_rc - structure for remote controller configuration
118  * @map_name: name of rc codes table
119  * @allowed_protos: protocol(s) supported by the driver
120  * @change_protocol: callback to change protocol
121  * @query: called to query an event from the device
122  * @interval: time in ms between two queries
123  * @driver_type: used to point if a device supports raw mode
124  * @bulk_mode: device supports bulk mode for rc (disable polling mode)
125  * @timeout: set to length of last space before raw IR goes idle
126  */
127 struct dvb_usb_rc {
128 	const char *map_name;
129 	u64 allowed_protos;
130 	int (*change_protocol)(struct rc_dev *dev, u64 *rc_proto);
131 	int (*query) (struct dvb_usb_device *d);
132 	unsigned int interval;
133 	enum rc_driver_type driver_type;
134 	bool bulk_mode;
135 	int timeout;
136 };
137 
138 /**
139  * struct usb_data_stream_properties - usb streaming configuration for adapter
140  * @type: urb type
141  * @count: count of used urbs
142  * @endpoint: stream usb endpoint number
143  * @u: union for @bulk and @isoc
144  */
145 struct usb_data_stream_properties {
146 #define USB_BULK  1
147 #define USB_ISOC  2
148 	u8 type;
149 	u8 count;
150 	u8 endpoint;
151 
152 	union {
153 		struct {
154 			unsigned int buffersize; /* per URB */
155 		} bulk;
156 		struct {
157 			int framesperurb;
158 			int framesize;
159 			int interval;
160 		} isoc;
161 	} u;
162 };
163 
164 /**
165  * struct dvb_usb_adapter_properties - properties of dvb usb device adapter
166  * @caps: adapter capabilities
167  * @pid_filter_count: pid count of adapter pid-filter
168  * @pid_filter_ctrl: called to enable/disable pid-filter
169  * @pid_filter: called to set/unset pid for filtering
170  * @stream: adapter usb stream configuration
171  */
172 struct dvb_usb_adapter_properties {
173 #define MAX_NO_OF_FE_PER_ADAP 3
174 #define DVB_USB_ADAP_HAS_PID_FILTER               0x01
175 #define DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF 0x02
176 #define DVB_USB_ADAP_NEED_PID_FILTERING           0x04
177 	u8 caps;
178 
179 	u8 pid_filter_count;
180 	int (*pid_filter_ctrl) (struct dvb_usb_adapter *, int);
181 	int (*pid_filter) (struct dvb_usb_adapter *, int, u16, int);
182 
183 	struct usb_data_stream_properties stream;
184 };
185 
186 /**
187  * struct dvb_usb_device_properties - properties of a dvb-usb-device
188  * @driver_name: name of the owning driver module
189  * @owner: owner of the dvb_adapter
190  * @adapter_nr: values from the DVB_DEFINE_MOD_OPT_ADAPTER_NR() macro
191  * @bInterfaceNumber: usb interface number driver binds
192  * @size_of_priv: bytes allocated for the driver private data
193  * @generic_bulk_ctrl_endpoint: bulk control endpoint number for sent
194  * @generic_bulk_ctrl_endpoint_response: bulk control endpoint number for
195  *  receive
196  * @generic_bulk_ctrl_delay: delay between bulk control sent and receive message
197  * @probe: like probe on driver model
198  * @disconnect: like disconnect on driver model
199  * @identify_state: called to determine the firmware state (cold or warm) and
200  *  return possible firmware file name to be loaded
201  * @firmware: name of the firmware file to be loaded
202  * @download_firmware: called to download the firmware
203  * @i2c_algo: i2c_algorithm if the device has i2c-adapter
204  * @num_adapters: dvb usb device adapter count
205  * @get_adapter_count: called to resolve adapter count
206  * @adapter: array of all adapter properties of device
207  * @power_ctrl: called to enable/disable power of the device
208  * @read_config: called to resolve device configuration
209  * @read_mac_address: called to resolve adapter mac-address
210  * @frontend_attach: called to attach the possible frontends
211  * @frontend_detach: called to detach the possible frontends
212  * @tuner_attach: called to attach the possible tuners
213  * @tuner_detach: called to detach the possible tuners
214  * @frontend_ctrl: called to power on/off active frontend
215  * @streaming_ctrl: called to start/stop the usb streaming of adapter
216  * @init: called after adapters are created in order to finalize device
217  *  configuration
218  * @exit: called when driver is unloaded
219  * @get_rc_config: called to resolve used remote controller configuration
220  * @get_stream_config: called to resolve input and output stream configuration
221  *  of the adapter just before streaming is started. input stream is transport
222  *  stream from the demodulator and output stream is usb stream to host.
223  */
224 struct dvb_usb_device_properties {
225 #define MAX_NO_OF_ADAPTER_PER_DEVICE 2
226 	const char *driver_name;
227 	struct module *owner;
228 	short *adapter_nr;
229 
230 	u8 bInterfaceNumber;
231 	unsigned int size_of_priv;
232 	u8 generic_bulk_ctrl_endpoint;
233 	u8 generic_bulk_ctrl_endpoint_response;
234 	unsigned int generic_bulk_ctrl_delay;
235 
236 	int (*probe)(struct dvb_usb_device *);
237 	void (*disconnect)(struct dvb_usb_device *);
238 #define WARM                  0
239 #define COLD                  1
240 	int (*identify_state) (struct dvb_usb_device *, const char **);
241 	const char *firmware;
242 #define RECONNECTS_USB        1
243 	int (*download_firmware) (struct dvb_usb_device *,
244 			const struct firmware *);
245 
246 	struct i2c_algorithm *i2c_algo;
247 
248 	unsigned int num_adapters;
249 	int (*get_adapter_count) (struct dvb_usb_device *);
250 	struct dvb_usb_adapter_properties adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
251 	int (*power_ctrl) (struct dvb_usb_device *, int);
252 	int (*read_config) (struct dvb_usb_device *d);
253 	int (*read_mac_address) (struct dvb_usb_adapter *, u8 []);
254 	int (*frontend_attach) (struct dvb_usb_adapter *);
255 	int (*frontend_detach)(struct dvb_usb_adapter *);
256 	int (*tuner_attach) (struct dvb_usb_adapter *);
257 	int (*tuner_detach)(struct dvb_usb_adapter *);
258 	int (*frontend_ctrl) (struct dvb_frontend *, int);
259 	int (*streaming_ctrl) (struct dvb_frontend *, int);
260 	int (*init) (struct dvb_usb_device *);
261 	void (*exit) (struct dvb_usb_device *);
262 	int (*get_rc_config) (struct dvb_usb_device *, struct dvb_usb_rc *);
263 #define DVB_USB_FE_TS_TYPE_188        0
264 #define DVB_USB_FE_TS_TYPE_204        1
265 #define DVB_USB_FE_TS_TYPE_RAW        2
266 	int (*get_stream_config) (struct dvb_frontend *,  u8 *,
267 			struct usb_data_stream_properties *);
268 };
269 
270 /**
271  * struct usb_data_stream - generic object of an usb stream
272  * @udev: USB device
273  * @props: properties
274  * @state: state of the data stream
275  * @complete: complete callback
276  * @urb_list: list of URBs
277  * @buf_num: number of buffer allocated
278  * @buf_size: size of each buffer in buf_list
279  * @buf_list: array containing all allocate buffers for streaming
280  * @dma_addr: list of dma_addr_t for each buffer in buf_list
281  *
282  * @urbs_initialized: number of URBs initialized
283  * @urbs_submitted: number of URBs submitted
284  * @user_priv: private pointer
285  */
286 struct usb_data_stream {
287 #define MAX_NO_URBS_FOR_DATA_STREAM 10
288 	struct usb_device *udev;
289 	struct usb_data_stream_properties props;
290 
291 #define USB_STATE_INIT    0x00
292 #define USB_STATE_URB_BUF 0x01
293 	u8 state;
294 
295 	void (*complete) (struct usb_data_stream *, u8 *, size_t);
296 
297 	struct urb    *urb_list[MAX_NO_URBS_FOR_DATA_STREAM];
298 	int            buf_num;
299 	unsigned long  buf_size;
300 	u8            *buf_list[MAX_NO_URBS_FOR_DATA_STREAM];
301 	dma_addr_t     dma_addr[MAX_NO_URBS_FOR_DATA_STREAM];
302 
303 	int urbs_initialized;
304 	int urbs_submitted;
305 
306 	void *user_priv;
307 };
308 
309 /**
310  * struct dvb_usb_adapter - dvb adapter object on dvb usb device
311  * @props: pointer to adapter properties
312  * @stream: adapter the usb data stream
313  * @id: index of this adapter (starting with 0)
314  * @ts_type: transport stream, input stream, type
315  * @suspend_resume_active: set when there is ongoing suspend / resume
316  * @pid_filtering: is hardware pid_filtering used or not
317  * @feed_count: current feed count
318  * @max_feed_count: maimum feed count device can handle
319  * @active_fe: active frontend
320  * @state_bits: status bits
321  * @dvb_adap: adapter dvb_adapter
322  * @dmxdev: adapter dmxdev
323  * @demux: adapter software demuxer
324  * @dvb_net: adapter dvb_net interfaces
325  * @fe: adapter frontends
326  * @fe_init: rerouted frontend-init function
327  * @fe_sleep: rerouted frontend-sleep function
328  */
329 struct dvb_usb_adapter {
330 	const struct dvb_usb_adapter_properties *props;
331 	struct usb_data_stream stream;
332 	u8 id;
333 	u8 ts_type;
334 	bool suspend_resume_active;
335 	bool pid_filtering;
336 	u8 feed_count;
337 	u8 max_feed_count;
338 	s8 active_fe;
339 #define ADAP_INIT                0
340 #define ADAP_SLEEP               1
341 #define ADAP_STREAMING           2
342 	unsigned long state_bits;
343 
344 	/* dvb */
345 	struct dvb_adapter   dvb_adap;
346 	struct dmxdev        dmxdev;
347 	struct dvb_demux     demux;
348 	struct dvb_net       dvb_net;
349 
350 	struct dvb_frontend *fe[MAX_NO_OF_FE_PER_ADAP];
351 	int (*fe_init[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
352 	int (*fe_sleep[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
353 };
354 
355 /**
356  * struct dvb_usb_device - dvb usb device object
357  * @props: device properties
358  * @name: device name
359  * @rc_map: name of rc codes table
360  * @rc_polling_active: set when RC polling is active
361  * @intf: pointer to the device's struct usb_interface
362  * @udev: pointer to the device's struct usb_device
363  * @rc: remote controller configuration
364  * @powered: indicated whether the device is power or not
365  * @usb_mutex: mutex for usb control messages
366  * @i2c_mutex: mutex for i2c-transfers
367  * @i2c_adap: device's i2c-adapter
368  * @adapter: adapters
369  * @rc_dev: rc device for the remote control
370  * @rc_phys: rc path
371  * @rc_query_work: work for polling remote
372  * @priv: private data of the actual driver (allocate by dvb usb, size defined
373  *  in size_of_priv of dvb_usb_properties).
374  */
375 struct dvb_usb_device {
376 	const struct dvb_usb_device_properties *props;
377 	const char *name;
378 	const char *rc_map;
379 	bool rc_polling_active;
380 	struct usb_interface *intf;
381 	struct usb_device *udev;
382 	struct dvb_usb_rc rc;
383 	int powered;
384 
385 	/* locking */
386 	struct mutex usb_mutex;
387 
388 	/* i2c */
389 	struct mutex i2c_mutex;
390 	struct i2c_adapter i2c_adap;
391 
392 	struct dvb_usb_adapter adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
393 
394 	/* remote control */
395 	struct rc_dev *rc_dev;
396 	char rc_phys[64];
397 	struct delayed_work rc_query_work;
398 
399 	void *priv;
400 };
401 
402 extern int dvb_usbv2_probe(struct usb_interface *,
403 		const struct usb_device_id *);
404 extern void dvb_usbv2_disconnect(struct usb_interface *);
405 extern int dvb_usbv2_suspend(struct usb_interface *, pm_message_t);
406 extern int dvb_usbv2_resume(struct usb_interface *);
407 extern int dvb_usbv2_reset_resume(struct usb_interface *);
408 
409 /* the generic read/write method for device control */
410 extern int dvb_usbv2_generic_rw(struct dvb_usb_device *, u8 *, u16, u8 *, u16);
411 extern int dvb_usbv2_generic_write(struct dvb_usb_device *, u8 *, u16);
412 /* caller must hold lock when locked versions are called */
413 extern int dvb_usbv2_generic_rw_locked(struct dvb_usb_device *,
414 		u8 *, u16, u8 *, u16);
415 extern int dvb_usbv2_generic_write_locked(struct dvb_usb_device *, u8 *, u16);
416 
417 #endif
418