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  * structure for carrying all needed data from the device driver to the general
104  * dvb usb routines
105  * @name: device name
106  * @rc_map: name of rc codes table
107  * @props: structure containing all device properties
108  */
109 struct dvb_usb_driver_info {
110 	const char *name;
111 	const char *rc_map;
112 	const struct dvb_usb_device_properties *props;
113 };
114 
115 /**
116  * structure for remote controller configuration
117  * @map_name: name of rc codes table
118  * @allowed_protos: protocol(s) supported by the driver
119  * @change_protocol: callback to change protocol
120  * @query: called to query an event from the device
121  * @interval: time in ms between two queries
122  * @driver_type: used to point if a device supports raw mode
123  * @bulk_mode: device supports bulk mode for rc (disable polling mode)
124  * @timeout: set to length of last space before raw IR goes idle
125  */
126 struct dvb_usb_rc {
127 	const char *map_name;
128 	u64 allowed_protos;
129 	int (*change_protocol)(struct rc_dev *dev, u64 *rc_proto);
130 	int (*query) (struct dvb_usb_device *d);
131 	unsigned int interval;
132 	enum rc_driver_type driver_type;
133 	bool bulk_mode;
134 	int timeout;
135 };
136 
137 /**
138  * usb streaming configuration for adapter
139  * @type: urb type
140  * @count: count of used urbs
141  * @endpoint: stream usb endpoint number
142  */
143 struct usb_data_stream_properties {
144 #define USB_BULK  1
145 #define USB_ISOC  2
146 	u8 type;
147 	u8 count;
148 	u8 endpoint;
149 
150 	union {
151 		struct {
152 			unsigned int buffersize; /* per URB */
153 		} bulk;
154 		struct {
155 			int framesperurb;
156 			int framesize;
157 			int interval;
158 		} isoc;
159 	} u;
160 };
161 
162 /**
163  * properties of dvb usb device adapter
164  * @caps: adapter capabilities
165  * @pid_filter_count: pid count of adapter pid-filter
166  * @pid_filter_ctrl: called to enable/disable pid-filter
167  * @pid_filter: called to set/unset pid for filtering
168  * @stream: adapter usb stream configuration
169  */
170 #define MAX_NO_OF_FE_PER_ADAP 3
171 struct dvb_usb_adapter_properties {
172 #define DVB_USB_ADAP_HAS_PID_FILTER               0x01
173 #define DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF 0x02
174 #define DVB_USB_ADAP_NEED_PID_FILTERING           0x04
175 	u8 caps;
176 
177 	u8 pid_filter_count;
178 	int (*pid_filter_ctrl) (struct dvb_usb_adapter *, int);
179 	int (*pid_filter) (struct dvb_usb_adapter *, int, u16, int);
180 
181 	struct usb_data_stream_properties stream;
182 };
183 
184 /**
185  * struct dvb_usb_device_properties - properties of a dvb-usb-device
186  * @driver_name: name of the owning driver module
187  * @owner: owner of the dvb_adapter
188  * @adapter_nr: values from the DVB_DEFINE_MOD_OPT_ADAPTER_NR() macro
189  * @bInterfaceNumber: usb interface number driver binds
190  * @size_of_priv: bytes allocated for the driver private data
191  * @generic_bulk_ctrl_endpoint: bulk control endpoint number for sent
192  * @generic_bulk_ctrl_endpoint_response: bulk control endpoint number for
193  *  receive
194  * @generic_bulk_ctrl_delay: delay between bulk control sent and receive message
195  * @probe: like probe on driver model
196  * @disconnect: like disconnect on driver model
197  * @identify_state: called to determine the firmware state (cold or warm) and
198  *  return possible firmware file name to be loaded
199  * @firmware: name of the firmware file to be loaded
200  * @download_firmware: called to download the firmware
201  * @i2c_algo: i2c_algorithm if the device has i2c-adapter
202  * @num_adapters: dvb usb device adapter count
203  * @get_adapter_count: called to resolve adapter count
204  * @adapter: array of all adapter properties of device
205  * @power_ctrl: called to enable/disable power of the device
206  * @read_config: called to resolve device configuration
207  * @read_mac_address: called to resolve adapter mac-address
208  * @frontend_attach: called to attach the possible frontends
209  * @frontend_detach: called to detach the possible frontends
210  * @tuner_attach: called to attach the possible tuners
211  * @frontend_ctrl: called to power on/off active frontend
212  * @streaming_ctrl: called to start/stop the usb streaming of adapter
213  * @init: called after adapters are created in order to finalize device
214  *  configuration
215  * @exit: called when driver is unloaded
216  * @get_rc_config: called to resolve used remote controller configuration
217  * @get_stream_config: called to resolve input and output stream configuration
218  *  of the adapter just before streaming is started. input stream is transport
219  *  stream from the demodulator and output stream is usb stream to host.
220  */
221 #define MAX_NO_OF_ADAPTER_PER_DEVICE 2
222 struct dvb_usb_device_properties {
223 	const char *driver_name;
224 	struct module *owner;
225 	short *adapter_nr;
226 
227 	u8 bInterfaceNumber;
228 	unsigned int size_of_priv;
229 	u8 generic_bulk_ctrl_endpoint;
230 	u8 generic_bulk_ctrl_endpoint_response;
231 	unsigned int generic_bulk_ctrl_delay;
232 
233 	int (*probe)(struct dvb_usb_device *);
234 	void (*disconnect)(struct dvb_usb_device *);
235 #define WARM                  0
236 #define COLD                  1
237 	int (*identify_state) (struct dvb_usb_device *, const char **);
238 	const char *firmware;
239 #define RECONNECTS_USB        1
240 	int (*download_firmware) (struct dvb_usb_device *,
241 			const struct firmware *);
242 
243 	struct i2c_algorithm *i2c_algo;
244 
245 	unsigned int num_adapters;
246 	int (*get_adapter_count) (struct dvb_usb_device *);
247 	struct dvb_usb_adapter_properties adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
248 	int (*power_ctrl) (struct dvb_usb_device *, int);
249 	int (*read_config) (struct dvb_usb_device *d);
250 	int (*read_mac_address) (struct dvb_usb_adapter *, u8 []);
251 	int (*frontend_attach) (struct dvb_usb_adapter *);
252 	int (*frontend_detach)(struct dvb_usb_adapter *);
253 	int (*tuner_attach) (struct dvb_usb_adapter *);
254 	int (*tuner_detach)(struct dvb_usb_adapter *);
255 	int (*frontend_ctrl) (struct dvb_frontend *, int);
256 	int (*streaming_ctrl) (struct dvb_frontend *, int);
257 	int (*init) (struct dvb_usb_device *);
258 	void (*exit) (struct dvb_usb_device *);
259 	int (*get_rc_config) (struct dvb_usb_device *, struct dvb_usb_rc *);
260 #define DVB_USB_FE_TS_TYPE_188        0
261 #define DVB_USB_FE_TS_TYPE_204        1
262 #define DVB_USB_FE_TS_TYPE_RAW        2
263 	int (*get_stream_config) (struct dvb_frontend *,  u8 *,
264 			struct usb_data_stream_properties *);
265 };
266 
267 /**
268  * generic object of an usb stream
269  * @buf_num: number of buffer allocated
270  * @buf_size: size of each buffer in buf_list
271  * @buf_list: array containing all allocate buffers for streaming
272  * @dma_addr: list of dma_addr_t for each buffer in buf_list
273  *
274  * @urbs_initialized: number of URBs initialized
275  * @urbs_submitted: number of URBs submitted
276  */
277 #define MAX_NO_URBS_FOR_DATA_STREAM 10
278 struct usb_data_stream {
279 	struct usb_device *udev;
280 	struct usb_data_stream_properties props;
281 
282 #define USB_STATE_INIT    0x00
283 #define USB_STATE_URB_BUF 0x01
284 	u8 state;
285 
286 	void (*complete) (struct usb_data_stream *, u8 *, size_t);
287 
288 	struct urb    *urb_list[MAX_NO_URBS_FOR_DATA_STREAM];
289 	int            buf_num;
290 	unsigned long  buf_size;
291 	u8            *buf_list[MAX_NO_URBS_FOR_DATA_STREAM];
292 	dma_addr_t     dma_addr[MAX_NO_URBS_FOR_DATA_STREAM];
293 
294 	int urbs_initialized;
295 	int urbs_submitted;
296 
297 	void *user_priv;
298 };
299 
300 /**
301  * dvb adapter object on dvb usb device
302  * @props: pointer to adapter properties
303  * @stream: adapter the usb data stream
304  * @id: index of this adapter (starting with 0)
305  * @ts_type: transport stream, input stream, type
306  * @suspend_resume_active: set when there is ongoing suspend / resume
307  * @pid_filtering: is hardware pid_filtering used or not
308  * @feed_count: current feed count
309  * @max_feed_count: maimum feed count device can handle
310  * @dvb_adap: adapter dvb_adapter
311  * @dmxdev: adapter dmxdev
312  * @demux: adapter software demuxer
313  * @dvb_net: adapter dvb_net interfaces
314  * @sync_mutex: mutex used to sync control and streaming of the adapter
315  * @fe: adapter frontends
316  * @fe_init: rerouted frontend-init function
317  * @fe_sleep: rerouted frontend-sleep function
318  */
319 struct dvb_usb_adapter {
320 	const struct dvb_usb_adapter_properties *props;
321 	struct usb_data_stream stream;
322 	u8 id;
323 	u8 ts_type;
324 	bool suspend_resume_active;
325 	bool pid_filtering;
326 	u8 feed_count;
327 	u8 max_feed_count;
328 	s8 active_fe;
329 #define ADAP_INIT                0
330 #define ADAP_SLEEP               1
331 #define ADAP_STREAMING           2
332 	unsigned long state_bits;
333 
334 	/* dvb */
335 	struct dvb_adapter   dvb_adap;
336 	struct dmxdev        dmxdev;
337 	struct dvb_demux     demux;
338 	struct dvb_net       dvb_net;
339 
340 	struct dvb_frontend *fe[MAX_NO_OF_FE_PER_ADAP];
341 	int (*fe_init[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
342 	int (*fe_sleep[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
343 };
344 
345 /**
346  * dvb usb device object
347  * @props: device properties
348  * @name: device name
349  * @rc_map: name of rc codes table
350  * @rc_polling_active: set when RC polling is active
351  * @intf: pointer to the device's struct usb_interface
352  * @udev: pointer to the device's struct usb_device
353  * @rc: remote controller configuration
354  * @powered: indicated whether the device is power or not
355  * @usb_mutex: mutex for usb control messages
356  * @i2c_mutex: mutex for i2c-transfers
357  * @i2c_adap: device's i2c-adapter
358  * @rc_dev: rc device for the remote control
359  * @rc_query_work: work for polling remote
360  * @priv: private data of the actual driver (allocate by dvb usb, size defined
361  *  in size_of_priv of dvb_usb_properties).
362  */
363 struct dvb_usb_device {
364 	const struct dvb_usb_device_properties *props;
365 	const char *name;
366 	const char *rc_map;
367 	bool rc_polling_active;
368 	struct usb_interface *intf;
369 	struct usb_device *udev;
370 	struct dvb_usb_rc rc;
371 	int powered;
372 
373 	/* locking */
374 	struct mutex usb_mutex;
375 
376 	/* i2c */
377 	struct mutex i2c_mutex;
378 	struct i2c_adapter i2c_adap;
379 
380 	struct dvb_usb_adapter adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
381 
382 	/* remote control */
383 	struct rc_dev *rc_dev;
384 	char rc_phys[64];
385 	struct delayed_work rc_query_work;
386 
387 	void *priv;
388 };
389 
390 extern int dvb_usbv2_probe(struct usb_interface *,
391 		const struct usb_device_id *);
392 extern void dvb_usbv2_disconnect(struct usb_interface *);
393 extern int dvb_usbv2_suspend(struct usb_interface *, pm_message_t);
394 extern int dvb_usbv2_resume(struct usb_interface *);
395 extern int dvb_usbv2_reset_resume(struct usb_interface *);
396 
397 /* the generic read/write method for device control */
398 extern int dvb_usbv2_generic_rw(struct dvb_usb_device *, u8 *, u16, u8 *, u16);
399 extern int dvb_usbv2_generic_write(struct dvb_usb_device *, u8 *, u16);
400 /* caller must hold lock when locked versions are called */
401 extern int dvb_usbv2_generic_rw_locked(struct dvb_usb_device *,
402 		u8 *, u16, u8 *, u16);
403 extern int dvb_usbv2_generic_write_locked(struct dvb_usb_device *, u8 *, u16);
404 
405 #endif
406