1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* dvb-usb.h is part of the DVB USB library.
3  *
4  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
5  * see dvb-usb-init.c for copyright information.
6  *
7  * the headerfile, all dvb-usb-drivers have to include.
8  *
9  * TODO: clean-up the structures for unused fields and update the comments
10  */
11 #ifndef __DVB_USB_H__
12 #define __DVB_USB_H__
13 
14 #include <linux/input.h>
15 #include <linux/usb.h>
16 #include <linux/firmware.h>
17 #include <linux/mutex.h>
18 #include <media/rc-core.h>
19 
20 #include <media/dvb_frontend.h>
21 #include <media/dvb_demux.h>
22 #include <media/dvb_net.h>
23 #include <media/dmxdev.h>
24 
25 #include "dvb-pll.h"
26 
27 #include <media/dvb-usb-ids.h>
28 
29 /* debug */
30 #ifdef CONFIG_DVB_USB_DEBUG
31 #define dprintk(var, level, args...) \
32 	    do { if (((var) & (level))) { printk(args); } } while (0)
33 
34 #define debug_dump(b, l, func) {\
35 	int loop_; \
36 	for (loop_ = 0; loop_ < (l); loop_++) \
37 		func("%02x ", b[loop_]); \
38 	func("\n");\
39 }
40 #define DVB_USB_DEBUG_STATUS
41 #else
42 #define dprintk(var, level, args...) no_printk(args)
43 #define debug_dump(b, l, func) do { } while (0)
44 
45 #define DVB_USB_DEBUG_STATUS " (debugging is not enabled)"
46 
47 #endif
48 
49 /* generic log methods - taken from usb.h */
50 #ifndef DVB_USB_LOG_PREFIX
51  #define DVB_USB_LOG_PREFIX "dvb-usb (please define a log prefix)"
52 #endif
53 
54 #undef err
55 #define err(format, arg...)  printk(KERN_ERR     DVB_USB_LOG_PREFIX ": " format "\n" , ## arg)
56 #undef info
57 #define info(format, arg...) printk(KERN_INFO    DVB_USB_LOG_PREFIX ": " format "\n" , ## arg)
58 #undef warn
59 #define warn(format, arg...) printk(KERN_WARNING DVB_USB_LOG_PREFIX ": " format "\n" , ## arg)
60 
61 /**
62  * struct dvb_usb_device_description - name and its according USB IDs
63  * @name: real name of the box, regardless which DVB USB device class is in use
64  * @cold_ids: array of struct usb_device_id which describe the device in
65  *  pre-firmware state
66  * @warm_ids: array of struct usb_device_id which describe the device in
67  *  post-firmware state
68  *
69  * Each DVB USB device class can have one or more actual devices, this struct
70  * assigns a name to it.
71  */
72 struct dvb_usb_device_description {
73 	const char *name;
74 
75 #define DVB_USB_ID_MAX_NUM 15
76 	struct usb_device_id *cold_ids[DVB_USB_ID_MAX_NUM];
77 	struct usb_device_id *warm_ids[DVB_USB_ID_MAX_NUM];
78 };
79 
80 static inline u8 rc5_custom(struct rc_map_table *key)
81 {
82 	return (key->scancode >> 8) & 0xff;
83 }
84 
85 static inline u8 rc5_data(struct rc_map_table *key)
86 {
87 	return key->scancode & 0xff;
88 }
89 
90 static inline u16 rc5_scan(struct rc_map_table *key)
91 {
92 	return key->scancode & 0xffff;
93 }
94 
95 struct dvb_usb_device;
96 struct dvb_usb_adapter;
97 struct usb_data_stream;
98 
99 /*
100  * Properties of USB streaming - TODO this structure should be somewhere else
101  * describes the kind of USB transfer used for data-streaming.
102  *  (BULK or ISOC)
103  */
104 struct usb_data_stream_properties {
105 #define USB_BULK  1
106 #define USB_ISOC  2
107 	int type;
108 	int count;
109 	int endpoint;
110 
111 	union {
112 		struct {
113 			int buffersize; /* per URB */
114 		} bulk;
115 		struct {
116 			int framesperurb;
117 			int framesize;
118 			int interval;
119 		} isoc;
120 	} u;
121 };
122 
123 /**
124  * struct dvb_usb_adapter_fe_properties - properties of a dvb-usb-adapter.
125  *    A DVB-USB-Adapter is basically a dvb_adapter which is present on a USB-device.
126  * @caps: capabilities of the DVB USB device.
127  * @pid_filter_count: number of PID filter position in the optional hardware
128  *  PID-filter.
129  * @num_frontends: number of frontends of the DVB USB adapter.
130  * @frontend_ctrl: called to power on/off active frontend.
131  * @streaming_ctrl: called to start and stop the MPEG2-TS streaming of the
132  *  device (not URB submitting/killing).
133  *  This callback will be called without data URBs being active - data URBs
134  *  will be submitted only after streaming_ctrl(1) returns successfully and
135  *  they will be killed before streaming_ctrl(0) gets called.
136  * @pid_filter_ctrl: called to en/disable the PID filter, if any.
137  * @pid_filter: called to set/unset a PID for filtering.
138  * @frontend_attach: called to attach the possible frontends (fill fe-field
139  *  of struct dvb_usb_device).
140  * @tuner_attach: called to attach the correct tuner and to fill pll_addr,
141  *  pll_desc and pll_init_buf of struct dvb_usb_device).
142  * @stream: configuration of the USB streaming
143  * @size_of_priv: size of the priv memory in struct dvb_usb_adapter
144  */
145 struct dvb_usb_adapter_fe_properties {
146 #define DVB_USB_ADAP_HAS_PID_FILTER               0x01
147 #define DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF 0x02
148 #define DVB_USB_ADAP_NEED_PID_FILTERING           0x04
149 #define DVB_USB_ADAP_RECEIVES_204_BYTE_TS         0x08
150 #define DVB_USB_ADAP_RECEIVES_RAW_PAYLOAD         0x10
151 	int caps;
152 	int pid_filter_count;
153 
154 	int (*streaming_ctrl)  (struct dvb_usb_adapter *, int);
155 	int (*pid_filter_ctrl) (struct dvb_usb_adapter *, int);
156 	int (*pid_filter)      (struct dvb_usb_adapter *, int, u16, int);
157 
158 	int (*frontend_attach) (struct dvb_usb_adapter *);
159 	int (*tuner_attach)    (struct dvb_usb_adapter *);
160 
161 	struct usb_data_stream_properties stream;
162 
163 	int size_of_priv;
164 };
165 
166 #define MAX_NO_OF_FE_PER_ADAP 3
167 struct dvb_usb_adapter_properties {
168 	int size_of_priv;
169 
170 	int (*frontend_ctrl)   (struct dvb_frontend *, int);
171 
172 	int num_frontends;
173 	struct dvb_usb_adapter_fe_properties fe[MAX_NO_OF_FE_PER_ADAP];
174 };
175 
176 /**
177  * struct dvb_rc_legacy - old properties of remote controller
178  * @rc_map_table: a hard-wired array of struct rc_map_table (NULL to disable
179  *  remote control handling).
180  * @rc_map_size: number of items in @rc_map_table.
181  * @rc_query: called to query an event event.
182  * @rc_interval: time in ms between two queries.
183  */
184 struct dvb_rc_legacy {
185 /* remote control properties */
186 #define REMOTE_NO_KEY_PRESSED      0x00
187 #define REMOTE_KEY_PRESSED         0x01
188 #define REMOTE_KEY_REPEAT          0x02
189 	struct rc_map_table  *rc_map_table;
190 	int rc_map_size;
191 	int (*rc_query) (struct dvb_usb_device *, u32 *, int *);
192 	int rc_interval;
193 };
194 
195 /**
196  * struct dvb_rc - properties of remote controller, using rc-core
197  * @rc_codes: name of rc codes table
198  * @protocol: type of protocol(s) currently used by the driver
199  * @allowed_protos: protocol(s) supported by the driver
200  * @driver_type: Used to point if a device supports raw mode
201  * @change_protocol: callback to change protocol
202  * @module_name: module name
203  * @rc_query: called to query an event event.
204  * @rc_interval: time in ms between two queries.
205  * @bulk_mode: device supports bulk mode for RC (disable polling mode)
206  * @scancode_mask: scancode mask
207  */
208 struct dvb_rc {
209 	char *rc_codes;
210 	u64 protocol;
211 	u64 allowed_protos;
212 	enum rc_driver_type driver_type;
213 	int (*change_protocol)(struct rc_dev *dev, u64 *rc_proto);
214 	char *module_name;
215 	int (*rc_query) (struct dvb_usb_device *d);
216 	int rc_interval;
217 	bool bulk_mode;				/* uses bulk mode */
218 	u32 scancode_mask;
219 };
220 
221 /**
222  * enum dvb_usb_mode - Specifies if it is using a legacy driver or a new one
223  *		       based on rc-core
224  * This is initialized/used only inside dvb-usb-remote.c.
225  * It shouldn't be set by the drivers.
226  *
227  * @DVB_RC_LEGACY: legacy driver
228  * @DVB_RC_CORE: rc-core driver
229  */
230 enum dvb_usb_mode {
231 	DVB_RC_LEGACY,
232 	DVB_RC_CORE,
233 };
234 
235 /**
236  * struct dvb_usb_device_properties - properties of a dvb-usb-device
237  * @caps: capabilities
238  * @usb_ctrl: which USB device-side controller is in use. Needed for firmware
239  *  download.
240  * @firmware: name of the firmware file.
241  * @download_firmware: called to download the firmware when the usb_ctrl is
242  *  DEVICE_SPECIFIC.
243  * @no_reconnect: device doesn't do a reconnect after downloading the firmware,
244  *  so do the warm initialization right after it
245  *
246  * @size_of_priv: how many bytes shall be allocated for the private field
247  *  of struct dvb_usb_device.
248  * @priv_init: optional callback to initialize the variable that private field
249  * of struct dvb_usb_device has pointer to just after it had been allocated and
250  * zeroed.
251  * @priv_destroy: just like priv_init, only called before deallocating
252  * the memory pointed by private field of struct dvb_usb_device.
253  *
254  * @num_adapters: the number of adapters in @adapters
255  * @adapter: the adapters
256  * @power_ctrl: called to enable/disable power of the device.
257  * @read_mac_address: called to read the MAC address of the device.
258  * @identify_state: called to determine the state (cold or warm), when it
259  *  is not distinguishable by the USB IDs.
260  *
261  * @rc: remote controller properties
262  *
263  * @i2c_algo: i2c_algorithm if the device has I2CoverUSB.
264  *
265  * @generic_bulk_ctrl_endpoint: most of the DVB USB devices have a generic
266  *  endpoint which received control messages with bulk transfers. When this
267  *  is non-zero, one can use dvb_usb_generic_rw and dvb_usb_generic_write-
268  *  helper functions.
269  *
270  * @generic_bulk_ctrl_endpoint_response: some DVB USB devices use a separate
271  *  endpoint for responses to control messages sent with bulk transfers via
272  *  the generic_bulk_ctrl_endpoint. When this is non-zero, this will be used
273  *  instead of the generic_bulk_ctrl_endpoint when reading usb responses in
274  *  the dvb_usb_generic_rw helper function.
275  *
276  * @num_device_descs: number of struct dvb_usb_device_description in @devices
277  * @devices: array of struct dvb_usb_device_description compatibles with these
278  *  properties.
279  */
280 struct dvb_usb_device_properties {
281 #define MAX_NO_OF_ADAPTER_PER_DEVICE 2
282 #define DVB_USB_IS_AN_I2C_ADAPTER            0x01
283 	int caps;
284 
285 #define DEVICE_SPECIFIC 0
286 #define CYPRESS_AN2135  1
287 #define CYPRESS_AN2235  2
288 #define CYPRESS_FX2     3
289 	int        usb_ctrl;
290 	int        (*download_firmware) (struct usb_device *, const struct firmware *);
291 	const char *firmware;
292 	int        no_reconnect;
293 
294 	int size_of_priv;
295 	int (*priv_init)(struct dvb_usb_device *);
296 	void (*priv_destroy)(struct dvb_usb_device *);
297 
298 	int num_adapters;
299 	struct dvb_usb_adapter_properties adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
300 
301 	int (*power_ctrl)       (struct dvb_usb_device *, int);
302 	int (*read_mac_address) (struct dvb_usb_device *, u8 []);
303 	int (*identify_state)(struct usb_device *udev,
304 			      const struct dvb_usb_device_properties *props,
305 			      const struct dvb_usb_device_description **desc,
306 			      int *cold);
307 
308 	struct {
309 		enum dvb_usb_mode mode;	/* Drivers shouldn't touch on it */
310 		struct dvb_rc_legacy legacy;
311 		struct dvb_rc core;
312 	} rc;
313 
314 	struct i2c_algorithm *i2c_algo;
315 
316 	int generic_bulk_ctrl_endpoint;
317 	int generic_bulk_ctrl_endpoint_response;
318 
319 	int num_device_descs;
320 	struct dvb_usb_device_description devices[12];
321 };
322 
323 /**
324  * struct usb_data_stream - generic object of an USB stream
325  * @udev: the USB device
326  * @props: data stream properties
327  * @state: state of the stream
328  * @complete: complete callback
329  * @urb_list: list of URBs
330  * @buf_num: number of buffer allocated.
331  * @buf_size: size of each buffer in buf_list.
332  * @buf_list: array containing all allocate buffers for streaming.
333  * @dma_addr: list of dma_addr_t for each buffer in buf_list.
334  *
335  * @urbs_initialized: number of URBs initialized.
336  * @urbs_submitted: number of URBs submitted.
337  * @user_priv: for private use.
338  */
339 struct usb_data_stream {
340 #define MAX_NO_URBS_FOR_DATA_STREAM 10
341 	struct usb_device                 *udev;
342 	struct usb_data_stream_properties  props;
343 
344 #define USB_STATE_INIT    0x00
345 #define USB_STATE_URB_BUF 0x01
346 	int state;
347 
348 	void (*complete) (struct usb_data_stream *, u8 *, size_t);
349 
350 	struct urb    *urb_list[MAX_NO_URBS_FOR_DATA_STREAM];
351 	int            buf_num;
352 	unsigned long  buf_size;
353 	u8            *buf_list[MAX_NO_URBS_FOR_DATA_STREAM];
354 	dma_addr_t     dma_addr[MAX_NO_URBS_FOR_DATA_STREAM];
355 
356 	int urbs_initialized;
357 	int urbs_submitted;
358 
359 	void *user_priv;
360 };
361 
362 /**
363  * struct dvb_usb_fe_adapter - a DVB adapter on a USB device
364  * @fe: frontend
365  * @fe_init:  rerouted frontend-init (wakeup) function.
366  * @fe_sleep: rerouted frontend-sleep function.
367  * @stream: the usb data stream.
368  * @pid_filtering: is hardware pid_filtering used or not.
369  * @max_feed_count: how many feeds can be handled simultaneously by this
370  *  device
371  * @priv: private pointer
372  */
373 struct dvb_usb_fe_adapter {
374 	struct dvb_frontend *fe;
375 
376 	int (*fe_init)  (struct dvb_frontend *);
377 	int (*fe_sleep) (struct dvb_frontend *);
378 
379 	struct usb_data_stream stream;
380 
381 	int pid_filtering;
382 	int max_feed_count;
383 
384 	void *priv;
385 };
386 
387 /**
388  * struct dvb_usb_adapter - a DVB adapter on a USB device
389  * @dev: DVB USB device pointer
390  * @props: properties
391  * @state: status
392  * @id: index of this adapter (starting with 0).
393  *
394  * @feedcount: number of requested feeds (used for streaming-activation)
395  *
396  * @dvb_adap: device's dvb_adapter.
397  * @dmxdev: device's dmxdev.
398  * @demux: device's software demuxer.
399  * @dvb_net: device's dvb_net interfaces.
400  *
401  * @fe_adap: frontend adapters
402  * @active_fe: active frontend
403  * @num_frontends_initialized: number of initialized frontends
404  * @priv: private pointer
405  */
406 struct dvb_usb_adapter {
407 	struct dvb_usb_device *dev;
408 	struct dvb_usb_adapter_properties props;
409 
410 #define DVB_USB_ADAP_STATE_INIT 0x000
411 #define DVB_USB_ADAP_STATE_DVB  0x001
412 	int state;
413 
414 	u8  id;
415 
416 	int feedcount;
417 
418 	/* dvb */
419 	struct dvb_adapter   dvb_adap;
420 	struct dmxdev        dmxdev;
421 	struct dvb_demux     demux;
422 	struct dvb_net       dvb_net;
423 
424 	struct dvb_usb_fe_adapter fe_adap[MAX_NO_OF_FE_PER_ADAP];
425 	int active_fe;
426 	int num_frontends_initialized;
427 
428 	void *priv;
429 };
430 
431 /**
432  * struct dvb_usb_device - object of a DVB USB device
433  * @props: copy of the struct dvb_usb_properties this device belongs to.
434  * @desc: pointer to the device's struct dvb_usb_device_description.
435  * @state: initialization and runtime state of the device.
436  *
437  * @powered: indicated whether the device is power or not.
438  *  Powered is in/decremented for each call to modify the state.
439  * @udev: pointer to the device's struct usb_device.
440  *
441  * @data_mutex: mutex to protect the data structure used to store URB data
442  * @usb_mutex: mutex of USB control messages (reading needs two messages).
443  *	Please notice that this mutex is used internally at the generic
444  *	URB control functions. So, drivers using dvb_usb_generic_rw() and
445  *	derivated functions should not lock it internally.
446  * @i2c_mutex: mutex for i2c-transfers
447  *
448  * @i2c_adap: device's i2c_adapter if it uses I2CoverUSB
449  *
450  * @num_adapters_initialized: number of initialized adapters
451  * @adapter: adapters
452  *
453  * @rc_dev: rc device for the remote control (rc-core mode)
454  * @input_dev: input device for the remote control (legacy mode)
455  * @rc_phys: rc device path
456  * @rc_query_work: struct work_struct frequent rc queries
457  * @last_event: last triggered event
458  * @last_state: last state (no, pressed, repeat)
459  * @owner: owner of the dvb_adapter
460  * @priv: private data of the actual driver (allocate by dvb-usb, size defined
461  *  in size_of_priv of dvb_usb_properties).
462  */
463 struct dvb_usb_device {
464 	struct dvb_usb_device_properties props;
465 	const struct dvb_usb_device_description *desc;
466 
467 	struct usb_device *udev;
468 
469 #define DVB_USB_STATE_INIT        0x000
470 #define DVB_USB_STATE_I2C         0x001
471 #define DVB_USB_STATE_DVB         0x002
472 #define DVB_USB_STATE_REMOTE      0x004
473 	int state;
474 
475 	int powered;
476 
477 	/* locking */
478 	struct mutex data_mutex;
479 	struct mutex usb_mutex;
480 
481 	/* i2c */
482 	struct mutex i2c_mutex;
483 	struct i2c_adapter i2c_adap;
484 
485 	int                    num_adapters_initialized;
486 	struct dvb_usb_adapter adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
487 
488 	/* remote control */
489 	struct rc_dev *rc_dev;
490 	struct input_dev *input_dev;
491 	char rc_phys[64];
492 	struct delayed_work rc_query_work;
493 	u32 last_event;
494 	int last_state;
495 
496 	struct module *owner;
497 
498 	void *priv;
499 };
500 
501 extern int dvb_usb_device_init(struct usb_interface *,
502 			       const struct dvb_usb_device_properties *,
503 			       struct module *, struct dvb_usb_device **,
504 			       short *adapter_nums);
505 extern void dvb_usb_device_exit(struct usb_interface *);
506 
507 /* the generic read/write method for device control */
508 extern int __must_check
509 dvb_usb_generic_rw(struct dvb_usb_device *, u8 *, u16, u8 *, u16, int);
510 extern int __must_check
511 dvb_usb_generic_write(struct dvb_usb_device *, u8 *, u16);
512 
513 /* commonly used remote control parsing */
514 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d, u8 keybuf[5],
515 				u32 *event, int *state);
516 
517 /* commonly used firmware download types and function */
518 struct hexline {
519 	u8 len;
520 	u32 addr;
521 	u8 type;
522 	u8 data[255];
523 	u8 chk;
524 };
525 extern int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type);
526 extern int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx, int *pos);
527 
528 
529 #endif
530