1 /*
2  * DVB USB framework
3  *
4  * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@desy.de>
5  * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License along
18  *    with this program; if not, write to the Free Software Foundation, Inc.,
19  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef DVB_USB_H
23 #define DVB_USB_H
24 
25 #include <linux/usb/input.h>
26 #include <linux/firmware.h>
27 #include <media/rc-core.h>
28 
29 #include "dvb_frontend.h"
30 #include "dvb_demux.h"
31 #include "dvb_net.h"
32 #include "dmxdev.h"
33 #include "dvb-usb-ids.h"
34 
35 /*
36  * device file: /dev/dvb/adapter[0-1]/frontend[0-2]
37  *
38  * |-- device
39  * |   |-- adapter0
40  * |   |   |-- frontend0
41  * |   |   |-- frontend1
42  * |   |   `-- frontend2
43  * |   `-- adapter1
44  * |       |-- frontend0
45  * |       |-- frontend1
46  * |       `-- frontend2
47  *
48  *
49  * Commonly used variable names:
50  * d = pointer to device (struct dvb_usb_device *)
51  * adap = pointer to adapter (struct dvb_usb_adapter *)
52  * fe = pointer to frontend (struct dvb_frontend *)
53  *
54  * Use macros defined in that file to resolve needed pointers.
55  */
56 
57 /* helper macros for every DVB USB driver use */
58 #define adap_to_d(adap) (container_of(adap, struct dvb_usb_device, \
59 		adapter[adap->id]))
60 #define adap_to_priv(adap) (adap_to_d(adap)->priv)
61 #define fe_to_adap(fe) ((struct dvb_usb_adapter *) ((fe)->dvb->priv))
62 #define fe_to_d(fe) (adap_to_d(fe_to_adap(fe)))
63 #define fe_to_priv(fe) (fe_to_d(fe)->priv)
64 #define d_to_priv(d) (d->priv)
65 
66 #define DVB_USB_STREAM_BULK(endpoint_, count_, size_) { \
67 	.type = USB_BULK, \
68 	.count = count_, \
69 	.endpoint = endpoint_, \
70 	.u = { \
71 		.bulk = { \
72 			.buffersize = size_, \
73 		} \
74 	} \
75 }
76 
77 #define DVB_USB_STREAM_ISOC(endpoint_, count_, frames_, size_, interval_) { \
78 	.type = USB_ISOC, \
79 	.count = count_, \
80 	.endpoint = endpoint_, \
81 	.u = { \
82 		.isoc = { \
83 			.framesperurb = frames_, \
84 			.framesize = size_,\
85 			.interval = interval_, \
86 		} \
87 	} \
88 }
89 
90 #define DVB_USB_DEVICE(vend, prod, props_, name_, rc) \
91 	.match_flags = USB_DEVICE_ID_MATCH_DEVICE, \
92 	.idVendor = (vend), \
93 	.idProduct = (prod), \
94 	.driver_info = (kernel_ulong_t) &((const struct dvb_usb_driver_info) { \
95 		.props = (props_), \
96 		.name = (name_), \
97 		.rc_map = (rc), \
98 	})
99 
100 struct dvb_usb_device;
101 struct dvb_usb_adapter;
102 
103 /**
104  * structure for carrying all needed data from the 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  * 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  */
126 struct dvb_usb_rc {
127 	const char *map_name;
128 	u64 allowed_protos;
129 	int (*change_protocol)(struct rc_dev *dev, u64 rc_type);
130 	int (*query) (struct dvb_usb_device *d);
131 	unsigned int interval;
132 	const enum rc_driver_type driver_type;
133 	bool bulk_mode;
134 };
135 
136 /**
137  * usb streaming configration for adapter
138  * @type: urb type
139  * @count: count of used urbs
140  * @endpoint: stream usb endpoint number
141  */
142 struct usb_data_stream_properties {
143 #define USB_BULK  1
144 #define USB_ISOC  2
145 	u8 type;
146 	u8 count;
147 	u8 endpoint;
148 
149 	union {
150 		struct {
151 			unsigned int buffersize; /* per URB */
152 		} bulk;
153 		struct {
154 			int framesperurb;
155 			int framesize;
156 			int interval;
157 		} isoc;
158 	} u;
159 };
160 
161 /**
162  * properties of dvb usb device adapter
163  * @caps: adapter capabilities
164  * @pid_filter_count: pid count of adapter pid-filter
165  * @pid_filter_ctrl: called to enable/disable pid-filter
166  * @pid_filter: called to set/unset pid for filtering
167  * @stream: adapter usb stream configuration
168  */
169 #define MAX_NO_OF_FE_PER_ADAP 3
170 struct dvb_usb_adapter_properties {
171 #define DVB_USB_ADAP_HAS_PID_FILTER               0x01
172 #define DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF 0x02
173 #define DVB_USB_ADAP_NEED_PID_FILTERING           0x04
174 	u8 caps;
175 
176 	u8 pid_filter_count;
177 	int (*pid_filter_ctrl) (struct dvb_usb_adapter *, int);
178 	int (*pid_filter) (struct dvb_usb_adapter *, int, u16, int);
179 
180 	struct usb_data_stream_properties stream;
181 };
182 
183 /**
184  * struct dvb_usb_device_properties - properties of a dvb-usb-device
185  * @driver_name: name of the owning driver module
186  * @owner: owner of the dvb_adapter
187  * @adapter_nr: values from the DVB_DEFINE_MOD_OPT_ADAPTER_NR() macro
188  * @bInterfaceNumber: usb interface number driver binds
189  * @size_of_priv: bytes allocated for the driver private data
190  * @generic_bulk_ctrl_endpoint: bulk control endpoint number for sent
191  * @generic_bulk_ctrl_endpoint_response: bulk control endpoint number for
192  *  receive
193  * @generic_bulk_ctrl_delay: delay between bulk control sent and receive message
194  * @identify_state: called to determine the firmware state (cold or warm) and
195  *  return possible firmware file name to be loaded
196  * @firmware: name of the firmware file to be loaded
197  * @download_firmware: called to download the firmware
198  * @i2c_algo: i2c_algorithm if the device has i2c-adapter
199  * @num_adapters: dvb usb device adapter count
200  * @get_adapter_count: called to resolve adapter count
201  * @adapter: array of all adapter properties of device
202  * @power_ctrl: called to enable/disable power of the device
203  * @read_config: called to resolve device configuration
204  * @read_mac_address: called to resolve adapter mac-address
205  * @frontend_attach: called to attach the possible frontends
206  * @tuner_attach: called to attach the possible tuners
207  * @frontend_ctrl: called to power on/off active frontend
208  * @streaming_ctrl: called to start/stop the usb streaming of adapter
209  * @init: called after adapters are created in order to finalize device
210  *  configuration
211  * @exit: called when driver is unloaded
212  * @get_rc_config: called to resolve used remote controller configuration
213  * @get_stream_config: called to resolve input and output stream configuration
214  *  of the adapter just before streaming is started. input stream is transport
215  *  stream from the demodulator and output stream is usb stream to host.
216  */
217 #define MAX_NO_OF_ADAPTER_PER_DEVICE 2
218 struct dvb_usb_device_properties {
219 	const char *driver_name;
220 	struct module *owner;
221 	short *adapter_nr;
222 
223 	u8 bInterfaceNumber;
224 	unsigned int size_of_priv;
225 	u8 generic_bulk_ctrl_endpoint;
226 	u8 generic_bulk_ctrl_endpoint_response;
227 	unsigned int generic_bulk_ctrl_delay;
228 
229 #define WARM                  0
230 #define COLD                  1
231 	int (*identify_state) (struct dvb_usb_device *, const char **);
232 	const char *firmware;
233 #define RECONNECTS_USB        1
234 	int (*download_firmware) (struct dvb_usb_device *,
235 			const struct firmware *);
236 
237 	struct i2c_algorithm *i2c_algo;
238 
239 	unsigned int num_adapters;
240 	int (*get_adapter_count) (struct dvb_usb_device *);
241 	struct dvb_usb_adapter_properties adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
242 	int (*power_ctrl) (struct dvb_usb_device *, int);
243 	int (*read_config) (struct dvb_usb_device *d);
244 	int (*read_mac_address) (struct dvb_usb_adapter *, u8 []);
245 	int (*frontend_attach) (struct dvb_usb_adapter *);
246 	int (*tuner_attach) (struct dvb_usb_adapter *);
247 	int (*frontend_ctrl) (struct dvb_frontend *, int);
248 	int (*streaming_ctrl) (struct dvb_frontend *, int);
249 	int (*init) (struct dvb_usb_device *);
250 	void (*exit) (struct dvb_usb_device *);
251 	int (*get_rc_config) (struct dvb_usb_device *, struct dvb_usb_rc *);
252 #define DVB_USB_FE_TS_TYPE_188        0
253 #define DVB_USB_FE_TS_TYPE_204        1
254 #define DVB_USB_FE_TS_TYPE_RAW        2
255 	int (*get_stream_config) (struct dvb_frontend *,  u8 *,
256 			struct usb_data_stream_properties *);
257 };
258 
259 /**
260  * generic object of an usb stream
261  * @buf_num: number of buffer allocated
262  * @buf_size: size of each buffer in buf_list
263  * @buf_list: array containing all allocate buffers for streaming
264  * @dma_addr: list of dma_addr_t for each buffer in buf_list
265  *
266  * @urbs_initialized: number of URBs initialized
267  * @urbs_submitted: number of URBs submitted
268  */
269 #define MAX_NO_URBS_FOR_DATA_STREAM 10
270 struct usb_data_stream {
271 	struct usb_device *udev;
272 	struct usb_data_stream_properties props;
273 
274 #define USB_STATE_INIT    0x00
275 #define USB_STATE_URB_BUF 0x01
276 	u8 state;
277 
278 	void (*complete) (struct usb_data_stream *, u8 *, size_t);
279 
280 	struct urb    *urb_list[MAX_NO_URBS_FOR_DATA_STREAM];
281 	int            buf_num;
282 	unsigned long  buf_size;
283 	u8            *buf_list[MAX_NO_URBS_FOR_DATA_STREAM];
284 	dma_addr_t     dma_addr[MAX_NO_URBS_FOR_DATA_STREAM];
285 
286 	int urbs_initialized;
287 	int urbs_submitted;
288 
289 	void *user_priv;
290 };
291 
292 /**
293  * dvb adapter object on dvb usb device
294  * @props: pointer to adapter properties
295  * @stream: adapter the usb data stream
296  * @id: index of this adapter (starting with 0)
297  * @ts_type: transport stream, input stream, type
298  * @pid_filtering: is hardware pid_filtering used or not
299  * @feed_count: current feed count
300  * @max_feed_count: maimum feed count device can handle
301  * @dvb_adap: adapter dvb_adapter
302  * @dmxdev: adapter dmxdev
303  * @demux: adapter software demuxer
304  * @dvb_net: adapter dvb_net interfaces
305  * @sync_mutex: mutex used to sync control and streaming of the adapter
306  * @fe: adapter frontends
307  * @fe_init: rerouted frontend-init function
308  * @fe_sleep: rerouted frontend-sleep function
309  */
310 struct dvb_usb_adapter {
311 	const struct dvb_usb_adapter_properties *props;
312 	struct usb_data_stream stream;
313 	u8 id;
314 	u8 ts_type;
315 	bool pid_filtering;
316 	u8 feed_count;
317 	u8 max_feed_count;
318 	s8 active_fe;
319 
320 	/* dvb */
321 	struct dvb_adapter   dvb_adap;
322 	struct dmxdev        dmxdev;
323 	struct dvb_demux     demux;
324 	struct dvb_net       dvb_net;
325 	struct mutex         sync_mutex;
326 
327 	struct dvb_frontend *fe[MAX_NO_OF_FE_PER_ADAP];
328 	int (*fe_init[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
329 	int (*fe_sleep[MAX_NO_OF_FE_PER_ADAP]) (struct dvb_frontend *);
330 };
331 
332 /**
333  * dvb usb device object
334  * @props: device properties
335  * @name: device name
336  * @rc_map: name of rc codes table
337  * @udev: pointer to the device's struct usb_device
338  * @intf: pointer to the device's usb interface
339  * @rc: remote controller configuration
340  * @probe_work: work to defer .probe()
341  * @powered: indicated whether the device is power or not
342  * @usb_mutex: mutex for usb control messages
343  * @i2c_mutex: mutex for i2c-transfers
344  * @i2c_adap: device's i2c-adapter
345  * @rc_dev: rc device for the remote control
346  * @rc_query_work: work for polling remote
347  * @priv: private data of the actual driver (allocate by dvb usb, size defined
348  *  in size_of_priv of dvb_usb_properties).
349  */
350 struct dvb_usb_device {
351 	const struct dvb_usb_device_properties *props;
352 	const char *name;
353 	const char *rc_map;
354 
355 	struct usb_device *udev;
356 	struct usb_interface *intf;
357 	struct dvb_usb_rc rc;
358 	struct work_struct probe_work;
359 	pid_t work_pid;
360 	int powered;
361 
362 	/* locking */
363 	struct mutex usb_mutex;
364 
365 	/* i2c */
366 	struct mutex i2c_mutex;
367 	struct i2c_adapter i2c_adap;
368 
369 	struct dvb_usb_adapter adapter[MAX_NO_OF_ADAPTER_PER_DEVICE];
370 
371 	/* remote control */
372 	struct rc_dev *rc_dev;
373 	char rc_phys[64];
374 	struct delayed_work rc_query_work;
375 
376 	void *priv;
377 };
378 
379 extern int dvb_usbv2_probe(struct usb_interface *,
380 		const struct usb_device_id *);
381 extern void dvb_usbv2_disconnect(struct usb_interface *);
382 extern int dvb_usbv2_suspend(struct usb_interface *, pm_message_t);
383 extern int dvb_usbv2_resume(struct usb_interface *);
384 
385 /* the generic read/write method for device control */
386 extern int dvb_usbv2_generic_rw(struct dvb_usb_device *, u8 *, u16, u8 *, u16);
387 extern int dvb_usbv2_generic_write(struct dvb_usb_device *, u8 *, u16);
388 
389 #endif
390