4 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
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.
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.
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.
25 #include <linux/usb/input.h>
26 #include <linux/firmware.h>
27 #include <media/rc-core.h>
28 #include <media/media-device.h>
30 #include <media/dvb_frontend.h>
31 #include <media/dvb_demux.h>
32 #include <media/dvb_net.h>
33 #include <media/dmxdev.h>
34 #include <media/dvb-usb-ids.h>
37 * device file: /dev/dvb/adapter[0-1]/frontend[0-2]
50 * Commonly used variable names:
51 * d = pointer to device (struct dvb_usb_device *)
52 * adap = pointer to adapter (struct dvb_usb_adapter *)
53 * fe = pointer to frontend (struct dvb_frontend *)
55 * Use macros defined in that file to resolve needed pointers.
58 /* helper macros for every DVB USB driver use */
59 #define adap_to_d(adap) (container_of(adap, struct dvb_usb_device, \
61 #define adap_to_priv(adap) (adap_to_d(adap)->priv)
62 #define fe_to_adap(fe) ((struct dvb_usb_adapter *) ((fe)->dvb->priv))
63 #define fe_to_d(fe) (adap_to_d(fe_to_adap(fe)))
64 #define fe_to_priv(fe) (fe_to_d(fe)->priv)
65 #define d_to_priv(d) (d->priv)
67 #define dvb_usb_dbg_usb_control_msg(udev, r, t, v, i, b, l) { \
69 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
73 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
74 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
75 i & 0xff, i >> 8, l & 0xff, l >> 8, direction, l, b); \
78 #define DVB_USB_STREAM_BULK(endpoint_, count_, size_) { \
81 .endpoint = endpoint_, \
84 .buffersize = size_, \
89 #define DVB_USB_STREAM_ISOC(endpoint_, count_, frames_, size_, interval_) { \
92 .endpoint = endpoint_, \
95 .framesperurb = frames_, \
97 .interval = interval_, \
102 #define DVB_USB_DEVICE(vend, prod, props_, name_, rc) \
103 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, \
104 .idVendor = (vend), \
105 .idProduct = (prod), \
106 .driver_info = (kernel_ulong_t) &((const struct dvb_usb_driver_info) { \
112 struct dvb_usb_device
;
113 struct dvb_usb_adapter
;
116 * structure for carrying all needed data from the device driver to the general
119 * @rc_map: name of rc codes table
120 * @props: structure containing all device properties
122 struct dvb_usb_driver_info
{
125 const struct dvb_usb_device_properties
*props
;
129 * structure for remote controller configuration
130 * @map_name: name of rc codes table
131 * @allowed_protos: protocol(s) supported by the driver
132 * @change_protocol: callback to change protocol
133 * @query: called to query an event from the device
134 * @interval: time in ms between two queries
135 * @driver_type: used to point if a device supports raw mode
136 * @bulk_mode: device supports bulk mode for rc (disable polling mode)
139 const char *map_name
;
141 int (*change_protocol
)(struct rc_dev
*dev
, u64
*rc_proto
);
142 int (*query
) (struct dvb_usb_device
*d
);
143 unsigned int interval
;
144 enum rc_driver_type driver_type
;
149 * usb streaming configration for adapter
151 * @count: count of used urbs
152 * @endpoint: stream usb endpoint number
154 struct usb_data_stream_properties
{
163 unsigned int buffersize
; /* per URB */
174 * properties of dvb usb device adapter
175 * @caps: adapter capabilities
176 * @pid_filter_count: pid count of adapter pid-filter
177 * @pid_filter_ctrl: called to enable/disable pid-filter
178 * @pid_filter: called to set/unset pid for filtering
179 * @stream: adapter usb stream configuration
181 #define MAX_NO_OF_FE_PER_ADAP 3
182 struct dvb_usb_adapter_properties
{
183 #define DVB_USB_ADAP_HAS_PID_FILTER 0x01
184 #define DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF 0x02
185 #define DVB_USB_ADAP_NEED_PID_FILTERING 0x04
189 int (*pid_filter_ctrl
) (struct dvb_usb_adapter
*, int);
190 int (*pid_filter
) (struct dvb_usb_adapter
*, int, u16
, int);
192 struct usb_data_stream_properties stream
;
196 * struct dvb_usb_device_properties - properties of a dvb-usb-device
197 * @driver_name: name of the owning driver module
198 * @owner: owner of the dvb_adapter
199 * @adapter_nr: values from the DVB_DEFINE_MOD_OPT_ADAPTER_NR() macro
200 * @bInterfaceNumber: usb interface number driver binds
201 * @size_of_priv: bytes allocated for the driver private data
202 * @generic_bulk_ctrl_endpoint: bulk control endpoint number for sent
203 * @generic_bulk_ctrl_endpoint_response: bulk control endpoint number for
205 * @generic_bulk_ctrl_delay: delay between bulk control sent and receive message
206 * @identify_state: called to determine the firmware state (cold or warm) and
207 * return possible firmware file name to be loaded
208 * @firmware: name of the firmware file to be loaded
209 * @download_firmware: called to download the firmware
210 * @i2c_algo: i2c_algorithm if the device has i2c-adapter
211 * @num_adapters: dvb usb device adapter count
212 * @get_adapter_count: called to resolve adapter count
213 * @adapter: array of all adapter properties of device
214 * @power_ctrl: called to enable/disable power of the device
215 * @read_config: called to resolve device configuration
216 * @read_mac_address: called to resolve adapter mac-address
217 * @frontend_attach: called to attach the possible frontends
218 * @frontend_detach: called to detach the possible frontends
219 * @tuner_attach: called to attach the possible tuners
220 * @frontend_ctrl: called to power on/off active frontend
221 * @streaming_ctrl: called to start/stop the usb streaming of adapter
222 * @init: called after adapters are created in order to finalize device
224 * @exit: called when driver is unloaded
225 * @get_rc_config: called to resolve used remote controller configuration
226 * @get_stream_config: called to resolve input and output stream configuration
227 * of the adapter just before streaming is started. input stream is transport
228 * stream from the demodulator and output stream is usb stream to host.
230 #define MAX_NO_OF_ADAPTER_PER_DEVICE 2
231 struct dvb_usb_device_properties
{
232 const char *driver_name
;
233 struct module
*owner
;
237 unsigned int size_of_priv
;
238 u8 generic_bulk_ctrl_endpoint
;
239 u8 generic_bulk_ctrl_endpoint_response
;
240 unsigned int generic_bulk_ctrl_delay
;
244 int (*identify_state
) (struct dvb_usb_device
*, const char **);
245 const char *firmware
;
246 #define RECONNECTS_USB 1
247 int (*download_firmware
) (struct dvb_usb_device
*,
248 const struct firmware
*);
250 struct i2c_algorithm
*i2c_algo
;
252 unsigned int num_adapters
;
253 int (*get_adapter_count
) (struct dvb_usb_device
*);
254 struct dvb_usb_adapter_properties adapter
[MAX_NO_OF_ADAPTER_PER_DEVICE
];
255 int (*power_ctrl
) (struct dvb_usb_device
*, int);
256 int (*read_config
) (struct dvb_usb_device
*d
);
257 int (*read_mac_address
) (struct dvb_usb_adapter
*, u8
[]);
258 int (*frontend_attach
) (struct dvb_usb_adapter
*);
259 int (*frontend_detach
)(struct dvb_usb_adapter
*);
260 int (*tuner_attach
) (struct dvb_usb_adapter
*);
261 int (*tuner_detach
)(struct dvb_usb_adapter
*);
262 int (*frontend_ctrl
) (struct dvb_frontend
*, int);
263 int (*streaming_ctrl
) (struct dvb_frontend
*, int);
264 int (*init
) (struct dvb_usb_device
*);
265 void (*exit
) (struct dvb_usb_device
*);
266 int (*get_rc_config
) (struct dvb_usb_device
*, struct dvb_usb_rc
*);
267 #define DVB_USB_FE_TS_TYPE_188 0
268 #define DVB_USB_FE_TS_TYPE_204 1
269 #define DVB_USB_FE_TS_TYPE_RAW 2
270 int (*get_stream_config
) (struct dvb_frontend
*, u8
*,
271 struct usb_data_stream_properties
*);
275 * generic object of an usb stream
276 * @buf_num: number of buffer allocated
277 * @buf_size: size of each buffer in buf_list
278 * @buf_list: array containing all allocate buffers for streaming
279 * @dma_addr: list of dma_addr_t for each buffer in buf_list
281 * @urbs_initialized: number of URBs initialized
282 * @urbs_submitted: number of URBs submitted
284 #define MAX_NO_URBS_FOR_DATA_STREAM 10
285 struct usb_data_stream
{
286 struct usb_device
*udev
;
287 struct usb_data_stream_properties props
;
289 #define USB_STATE_INIT 0x00
290 #define USB_STATE_URB_BUF 0x01
293 void (*complete
) (struct usb_data_stream
*, u8
*, size_t);
295 struct urb
*urb_list
[MAX_NO_URBS_FOR_DATA_STREAM
];
297 unsigned long buf_size
;
298 u8
*buf_list
[MAX_NO_URBS_FOR_DATA_STREAM
];
299 dma_addr_t dma_addr
[MAX_NO_URBS_FOR_DATA_STREAM
];
301 int urbs_initialized
;
308 * dvb adapter object on dvb usb device
309 * @props: pointer to adapter properties
310 * @stream: adapter the usb data stream
311 * @id: index of this adapter (starting with 0)
312 * @ts_type: transport stream, input stream, type
313 * @suspend_resume_active: set when there is ongoing suspend / resume
314 * @pid_filtering: is hardware pid_filtering used or not
315 * @feed_count: current feed count
316 * @max_feed_count: maimum feed count device can handle
317 * @dvb_adap: adapter dvb_adapter
318 * @dmxdev: adapter dmxdev
319 * @demux: adapter software demuxer
320 * @dvb_net: adapter dvb_net interfaces
321 * @sync_mutex: mutex used to sync control and streaming of the adapter
322 * @fe: adapter frontends
323 * @fe_init: rerouted frontend-init function
324 * @fe_sleep: rerouted frontend-sleep function
326 struct dvb_usb_adapter
{
327 const struct dvb_usb_adapter_properties
*props
;
328 struct usb_data_stream stream
;
331 bool suspend_resume_active
;
338 #define ADAP_STREAMING 2
339 unsigned long state_bits
;
342 struct dvb_adapter dvb_adap
;
343 struct dmxdev dmxdev
;
344 struct dvb_demux demux
;
345 struct dvb_net dvb_net
;
347 struct dvb_frontend
*fe
[MAX_NO_OF_FE_PER_ADAP
];
348 int (*fe_init
[MAX_NO_OF_FE_PER_ADAP
]) (struct dvb_frontend
*);
349 int (*fe_sleep
[MAX_NO_OF_FE_PER_ADAP
]) (struct dvb_frontend
*);
353 * dvb usb device object
354 * @props: device properties
356 * @rc_map: name of rc codes table
357 * @rc_polling_active: set when RC polling is active
358 * @intf: pointer to the device's struct usb_interface
359 * @udev: pointer to the device's struct usb_device
360 * @rc: remote controller configuration
361 * @powered: indicated whether the device is power or not
362 * @usb_mutex: mutex for usb control messages
363 * @i2c_mutex: mutex for i2c-transfers
364 * @i2c_adap: device's i2c-adapter
365 * @rc_dev: rc device for the remote control
366 * @rc_query_work: work for polling remote
367 * @priv: private data of the actual driver (allocate by dvb usb, size defined
368 * in size_of_priv of dvb_usb_properties).
370 struct dvb_usb_device
{
371 const struct dvb_usb_device_properties
*props
;
374 bool rc_polling_active
;
375 struct usb_interface
*intf
;
376 struct usb_device
*udev
;
377 struct dvb_usb_rc rc
;
381 struct mutex usb_mutex
;
384 struct mutex i2c_mutex
;
385 struct i2c_adapter i2c_adap
;
387 struct dvb_usb_adapter adapter
[MAX_NO_OF_ADAPTER_PER_DEVICE
];
390 struct rc_dev
*rc_dev
;
392 struct delayed_work rc_query_work
;
397 extern int dvb_usbv2_probe(struct usb_interface
*,
398 const struct usb_device_id
*);
399 extern void dvb_usbv2_disconnect(struct usb_interface
*);
400 extern int dvb_usbv2_suspend(struct usb_interface
*, pm_message_t
);
401 extern int dvb_usbv2_resume(struct usb_interface
*);
402 extern int dvb_usbv2_reset_resume(struct usb_interface
*);
404 /* the generic read/write method for device control */
405 extern int dvb_usbv2_generic_rw(struct dvb_usb_device
*, u8
*, u16
, u8
*, u16
);
406 extern int dvb_usbv2_generic_write(struct dvb_usb_device
*, u8
*, u16
);
407 /* caller must hold lock when locked versions are called */
408 extern int dvb_usbv2_generic_rw_locked(struct dvb_usb_device
*,
409 u8
*, u16
, u8
*, u16
);
410 extern int dvb_usbv2_generic_write_locked(struct dvb_usb_device
*, u8
*, u16
);