dvb_usb.h 13 KB

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