dvb-usb.h 16 KB

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