media-device.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Media device
  4. *
  5. * Copyright (C) 2010 Nokia Corporation
  6. *
  7. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. * Sakari Ailus <sakari.ailus@iki.fi>
  9. */
  10. #ifndef _MEDIA_DEVICE_H
  11. #define _MEDIA_DEVICE_H
  12. #include <linux/atomic.h>
  13. #include <linux/list.h>
  14. #include <linux/mutex.h>
  15. #include <linux/pci.h>
  16. #include <linux/platform_device.h>
  17. #include <media/media-devnode.h>
  18. #include <media/media-entity.h>
  19. struct ida;
  20. struct media_device;
  21. /**
  22. * struct media_entity_notify - Media Entity Notify
  23. *
  24. * @list: List head
  25. * @notify_data: Input data to invoke the callback
  26. * @notify: Callback function pointer
  27. *
  28. * Drivers may register a callback to take action when new entities get
  29. * registered with the media device. This handler is intended for creating
  30. * links between existing entities and should not create entities and register
  31. * them.
  32. */
  33. struct media_entity_notify {
  34. struct list_head list;
  35. void *notify_data;
  36. void (*notify)(struct media_entity *entity, void *notify_data);
  37. };
  38. /**
  39. * struct media_device_ops - Media device operations
  40. * @link_notify: Link state change notification callback. This callback is
  41. * called with the graph_mutex held.
  42. * @req_alloc: Allocate a request. Set this if you need to allocate a struct
  43. * larger then struct media_request. @req_alloc and @req_free must
  44. * either both be set or both be NULL.
  45. * @req_free: Free a request. Set this if @req_alloc was set as well, leave
  46. * to NULL otherwise.
  47. * @req_validate: Validate a request, but do not queue yet. The req_queue_mutex
  48. * lock is held when this op is called.
  49. * @req_queue: Queue a validated request, cannot fail. If something goes
  50. * wrong when queueing this request then it should be marked
  51. * as such internally in the driver and any related buffers
  52. * must eventually return to vb2 with state VB2_BUF_STATE_ERROR.
  53. * The req_queue_mutex lock is held when this op is called.
  54. * It is important that vb2 buffer objects are queued last after
  55. * all other object types are queued: queueing a buffer kickstarts
  56. * the request processing, so all other objects related to the
  57. * request (and thus the buffer) must be available to the driver.
  58. * And once a buffer is queued, then the driver can complete
  59. * or delete objects from the request before req_queue exits.
  60. */
  61. struct media_device_ops {
  62. int (*link_notify)(struct media_link *link, u32 flags,
  63. unsigned int notification);
  64. struct media_request *(*req_alloc)(struct media_device *mdev);
  65. void (*req_free)(struct media_request *req);
  66. int (*req_validate)(struct media_request *req);
  67. void (*req_queue)(struct media_request *req);
  68. };
  69. /**
  70. * struct media_device - Media device
  71. * @dev: Parent device
  72. * @devnode: Media device node
  73. * @driver_name: Optional device driver name. If not set, calls to
  74. * %MEDIA_IOC_DEVICE_INFO will return ``dev->driver->name``.
  75. * This is needed for USB drivers for example, as otherwise
  76. * they'll all appear as if the driver name was "usb".
  77. * @model: Device model name
  78. * @serial: Device serial number (optional)
  79. * @bus_info: Unique and stable device location identifier
  80. * @hw_revision: Hardware device revision
  81. * @topology_version: Monotonic counter for storing the version of the graph
  82. * topology. Should be incremented each time the topology changes.
  83. * @id: Unique ID used on the last registered graph object
  84. * @entity_internal_idx: Unique internal entity ID used by the graph traversal
  85. * algorithms
  86. * @entity_internal_idx_max: Allocated internal entity indices
  87. * @entities: List of registered entities
  88. * @interfaces: List of registered interfaces
  89. * @pads: List of registered pads
  90. * @links: List of registered links
  91. * @entity_notify: List of registered entity_notify callbacks
  92. * @graph_mutex: Protects access to struct media_device data
  93. * @pm_count_walk: Graph walk for power state walk. Access serialised using
  94. * graph_mutex.
  95. *
  96. * @source_priv: Driver Private data for enable/disable source handlers
  97. * @enable_source: Enable Source Handler function pointer
  98. * @disable_source: Disable Source Handler function pointer
  99. *
  100. * @ops: Operation handler callbacks
  101. * @req_queue_mutex: Serialise the MEDIA_REQUEST_IOC_QUEUE ioctl w.r.t.
  102. * other operations that stop or start streaming.
  103. * @num_requests: number of associated requests
  104. * @num_request_objects: number of associated request objects
  105. * @media_dir: DebugFS media directory
  106. * @request_id: Used to generate unique request IDs
  107. *
  108. * This structure represents an abstract high-level media device. It allows easy
  109. * access to entities and provides basic media device-level support. The
  110. * structure can be allocated directly or embedded in a larger structure.
  111. *
  112. * The parent @dev is a physical device. It must be set before registering the
  113. * media device.
  114. *
  115. * @model is a descriptive model name exported through sysfs. It doesn't have to
  116. * be unique.
  117. *
  118. * @enable_source is a handler to find source entity for the
  119. * sink entity and activate the link between them if source
  120. * entity is free. Drivers should call this handler before
  121. * accessing the source.
  122. *
  123. * @disable_source is a handler to find source entity for the
  124. * sink entity and deactivate the link between them. Drivers
  125. * should call this handler to release the source.
  126. *
  127. * Use-case: find tuner entity connected to the decoder
  128. * entity and check if it is available, and activate the
  129. * link between them from @enable_source and deactivate
  130. * from @disable_source.
  131. *
  132. * .. note::
  133. *
  134. * Bridge driver is expected to implement and set the
  135. * handler when &media_device is registered or when
  136. * bridge driver finds the media_device during probe.
  137. * Bridge driver sets source_priv with information
  138. * necessary to run @enable_source and @disable_source handlers.
  139. * Callers should hold graph_mutex to access and call @enable_source
  140. * and @disable_source handlers.
  141. */
  142. struct media_device {
  143. /* dev->driver_data points to this struct. */
  144. struct device *dev;
  145. struct media_devnode *devnode;
  146. char model[32];
  147. char driver_name[32];
  148. char serial[40];
  149. char bus_info[32];
  150. u32 hw_revision;
  151. u64 topology_version;
  152. u32 id;
  153. struct ida entity_internal_idx;
  154. int entity_internal_idx_max;
  155. struct list_head entities;
  156. struct list_head interfaces;
  157. struct list_head pads;
  158. struct list_head links;
  159. /* notify callback list invoked when a new entity is registered */
  160. struct list_head entity_notify;
  161. /* Serializes graph operations. */
  162. struct mutex graph_mutex;
  163. struct media_graph pm_count_walk;
  164. void *source_priv;
  165. int (*enable_source)(struct media_entity *entity,
  166. struct media_pipeline *pipe);
  167. void (*disable_source)(struct media_entity *entity);
  168. const struct media_device_ops *ops;
  169. struct mutex req_queue_mutex;
  170. atomic_t num_requests;
  171. atomic_t num_request_objects;
  172. /* debugfs */
  173. struct dentry *media_dir;
  174. atomic_t request_id;
  175. };
  176. /* We don't need to include usb.h here */
  177. struct usb_device;
  178. #ifdef CONFIG_MEDIA_CONTROLLER
  179. /* Supported link_notify @notification values. */
  180. #define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0
  181. #define MEDIA_DEV_NOTIFY_POST_LINK_CH 1
  182. /**
  183. * media_device_init() - Initializes a media device element
  184. *
  185. * @mdev: pointer to struct &media_device
  186. *
  187. * This function initializes the media device prior to its registration.
  188. * The media device initialization and registration is split in two functions
  189. * to avoid race conditions and make the media device available to user-space
  190. * before the media graph has been completed.
  191. *
  192. * So drivers need to first initialize the media device, register any entity
  193. * within the media device, create pad to pad links and then finally register
  194. * the media device by calling media_device_register() as a final step.
  195. *
  196. * The caller is responsible for initializing the media device before
  197. * registration. The following fields must be set:
  198. *
  199. * - dev must point to the parent device
  200. * - model must be filled with the device model name
  201. *
  202. * The bus_info field is set by media_device_init() for PCI and platform devices
  203. * if the field begins with '\0'.
  204. */
  205. void media_device_init(struct media_device *mdev);
  206. /**
  207. * media_device_cleanup() - Cleanups a media device element
  208. *
  209. * @mdev: pointer to struct &media_device
  210. *
  211. * This function that will destroy the graph_mutex that is
  212. * initialized in media_device_init().
  213. */
  214. void media_device_cleanup(struct media_device *mdev);
  215. /**
  216. * __media_device_register() - Registers a media device element
  217. *
  218. * @mdev: pointer to struct &media_device
  219. * @owner: should be filled with %THIS_MODULE
  220. *
  221. * Users, should, instead, call the media_device_register() macro.
  222. *
  223. * The caller is responsible for initializing the &media_device structure
  224. * before registration. The following fields of &media_device must be set:
  225. *
  226. * - &media_device.model must be filled with the device model name as a
  227. * NUL-terminated UTF-8 string. The device/model revision must not be
  228. * stored in this field.
  229. *
  230. * The following fields are optional:
  231. *
  232. * - &media_device.serial is a unique serial number stored as a
  233. * NUL-terminated ASCII string. The field is big enough to store a GUID
  234. * in text form. If the hardware doesn't provide a unique serial number
  235. * this field must be left empty.
  236. *
  237. * - &media_device.bus_info represents the location of the device in the
  238. * system as a NUL-terminated ASCII string. For PCI/PCIe devices
  239. * &media_device.bus_info must be set to "PCI:" (or "PCIe:") followed by
  240. * the value of pci_name(). For USB devices,the usb_make_path() function
  241. * must be used. This field is used by applications to distinguish between
  242. * otherwise identical devices that don't provide a serial number.
  243. *
  244. * - &media_device.hw_revision is the hardware device revision in a
  245. * driver-specific format. When possible the revision should be formatted
  246. * with the KERNEL_VERSION() macro.
  247. *
  248. * .. note::
  249. *
  250. * #) Upon successful registration a character device named media[0-9]+ is created. The device major and minor numbers are dynamic. The model name is exported as a sysfs attribute.
  251. *
  252. * #) Unregistering a media device that hasn't been registered is **NOT** safe.
  253. *
  254. * Return: returns zero on success or a negative error code.
  255. */
  256. int __must_check __media_device_register(struct media_device *mdev,
  257. struct module *owner);
  258. /**
  259. * media_device_register() - Registers a media device element
  260. *
  261. * @mdev: pointer to struct &media_device
  262. *
  263. * This macro calls __media_device_register() passing %THIS_MODULE as
  264. * the __media_device_register() second argument (**owner**).
  265. */
  266. #define media_device_register(mdev) __media_device_register(mdev, THIS_MODULE)
  267. /**
  268. * media_device_unregister() - Unregisters a media device element
  269. *
  270. * @mdev: pointer to struct &media_device
  271. *
  272. * It is safe to call this function on an unregistered (but initialised)
  273. * media device.
  274. */
  275. void media_device_unregister(struct media_device *mdev);
  276. /**
  277. * media_device_register_entity() - registers a media entity inside a
  278. * previously registered media device.
  279. *
  280. * @mdev: pointer to struct &media_device
  281. * @entity: pointer to struct &media_entity to be registered
  282. *
  283. * Entities are identified by a unique positive integer ID. The media
  284. * controller framework will such ID automatically. IDs are not guaranteed
  285. * to be contiguous, and the ID number can change on newer Kernel versions.
  286. * So, neither the driver nor userspace should hardcode ID numbers to refer
  287. * to the entities, but, instead, use the framework to find the ID, when
  288. * needed.
  289. *
  290. * The media_entity name, type and flags fields should be initialized before
  291. * calling media_device_register_entity(). Entities embedded in higher-level
  292. * standard structures can have some of those fields set by the higher-level
  293. * framework.
  294. *
  295. * If the device has pads, media_entity_pads_init() should be called before
  296. * this function. Otherwise, the &media_entity.pad and &media_entity.num_pads
  297. * should be zeroed before calling this function.
  298. *
  299. * Entities have flags that describe the entity capabilities and state:
  300. *
  301. * %MEDIA_ENT_FL_DEFAULT
  302. * indicates the default entity for a given type.
  303. * This can be used to report the default audio and video devices or the
  304. * default camera sensor.
  305. *
  306. * .. note::
  307. *
  308. * Drivers should set the entity function before calling this function.
  309. * Please notice that the values %MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN and
  310. * %MEDIA_ENT_F_UNKNOWN should not be used by the drivers.
  311. */
  312. int __must_check media_device_register_entity(struct media_device *mdev,
  313. struct media_entity *entity);
  314. /**
  315. * media_device_unregister_entity() - unregisters a media entity.
  316. *
  317. * @entity: pointer to struct &media_entity to be unregistered
  318. *
  319. * All links associated with the entity and all PADs are automatically
  320. * unregistered from the media_device when this function is called.
  321. *
  322. * Unregistering an entity will not change the IDs of the other entities and
  323. * the previoully used ID will never be reused for a newly registered entities.
  324. *
  325. * When a media device is unregistered, all its entities are unregistered
  326. * automatically. No manual entities unregistration is then required.
  327. *
  328. * .. note::
  329. *
  330. * The media_entity instance itself must be freed explicitly by
  331. * the driver if required.
  332. */
  333. void media_device_unregister_entity(struct media_entity *entity);
  334. /**
  335. * media_device_register_entity_notify() - Registers a media entity_notify
  336. * callback
  337. *
  338. * @mdev: The media device
  339. * @nptr: The media_entity_notify
  340. *
  341. * .. note::
  342. *
  343. * When a new entity is registered, all the registered
  344. * media_entity_notify callbacks are invoked.
  345. */
  346. void media_device_register_entity_notify(struct media_device *mdev,
  347. struct media_entity_notify *nptr);
  348. /**
  349. * media_device_unregister_entity_notify() - Unregister a media entity notify
  350. * callback
  351. *
  352. * @mdev: The media device
  353. * @nptr: The media_entity_notify
  354. *
  355. */
  356. void media_device_unregister_entity_notify(struct media_device *mdev,
  357. struct media_entity_notify *nptr);
  358. /* Iterate over all entities. */
  359. #define media_device_for_each_entity(entity, mdev) \
  360. list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)
  361. /* Iterate over all interfaces. */
  362. #define media_device_for_each_intf(intf, mdev) \
  363. list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)
  364. /* Iterate over all pads. */
  365. #define media_device_for_each_pad(pad, mdev) \
  366. list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)
  367. /* Iterate over all links. */
  368. #define media_device_for_each_link(link, mdev) \
  369. list_for_each_entry(link, &(mdev)->links, graph_obj.list)
  370. /**
  371. * media_device_pci_init() - create and initialize a
  372. * struct &media_device from a PCI device.
  373. *
  374. * @mdev: pointer to struct &media_device
  375. * @pci_dev: pointer to struct pci_dev
  376. * @name: media device name. If %NULL, the routine will use the default
  377. * name for the pci device, given by pci_name() macro.
  378. */
  379. void media_device_pci_init(struct media_device *mdev,
  380. struct pci_dev *pci_dev,
  381. const char *name);
  382. /**
  383. * __media_device_usb_init() - create and initialize a
  384. * struct &media_device from a PCI device.
  385. *
  386. * @mdev: pointer to struct &media_device
  387. * @udev: pointer to struct usb_device
  388. * @board_name: media device name. If %NULL, the routine will use the usb
  389. * product name, if available.
  390. * @driver_name: name of the driver. if %NULL, the routine will use the name
  391. * given by ``udev->dev->driver->name``, with is usually the wrong
  392. * thing to do.
  393. *
  394. * .. note::
  395. *
  396. * It is better to call media_device_usb_init() instead, as
  397. * such macro fills driver_name with %KBUILD_MODNAME.
  398. */
  399. void __media_device_usb_init(struct media_device *mdev,
  400. struct usb_device *udev,
  401. const char *board_name,
  402. const char *driver_name);
  403. #else
  404. static inline void media_device_init(struct media_device *mdev)
  405. {
  406. }
  407. static inline int media_device_register(struct media_device *mdev)
  408. {
  409. return 0;
  410. }
  411. static inline void media_device_unregister(struct media_device *mdev)
  412. {
  413. }
  414. static inline void media_device_cleanup(struct media_device *mdev)
  415. {
  416. }
  417. static inline int media_device_register_entity(struct media_device *mdev,
  418. struct media_entity *entity)
  419. {
  420. return 0;
  421. }
  422. static inline void media_device_unregister_entity(struct media_entity *entity)
  423. {
  424. }
  425. static inline void media_device_register_entity_notify(
  426. struct media_device *mdev,
  427. struct media_entity_notify *nptr)
  428. {
  429. }
  430. static inline void media_device_unregister_entity_notify(
  431. struct media_device *mdev,
  432. struct media_entity_notify *nptr)
  433. {
  434. }
  435. static inline void media_device_pci_init(struct media_device *mdev,
  436. struct pci_dev *pci_dev,
  437. char *name)
  438. {
  439. }
  440. static inline void __media_device_usb_init(struct media_device *mdev,
  441. struct usb_device *udev,
  442. char *board_name,
  443. char *driver_name)
  444. {
  445. }
  446. #endif /* CONFIG_MEDIA_CONTROLLER */
  447. /**
  448. * media_device_usb_init() - create and initialize a
  449. * struct &media_device from a PCI device.
  450. *
  451. * @mdev: pointer to struct &media_device
  452. * @udev: pointer to struct usb_device
  453. * @name: media device name. If %NULL, the routine will use the usb
  454. * product name, if available.
  455. *
  456. * This macro calls media_device_usb_init() passing the
  457. * media_device_usb_init() **driver_name** parameter filled with
  458. * %KBUILD_MODNAME.
  459. */
  460. #define media_device_usb_init(mdev, udev, name) \
  461. __media_device_usb_init(mdev, udev, name, KBUILD_MODNAME)
  462. /**
  463. * media_set_bus_info() - Set bus_info field
  464. *
  465. * @bus_info: Variable where to write the bus info (char array)
  466. * @bus_info_size: Length of the bus_info
  467. * @dev: Related struct device
  468. *
  469. * Sets bus information based on &dev. This is currently done for PCI and
  470. * platform devices. dev is required to be non-NULL for this to happen.
  471. *
  472. * This function is not meant to be called from drivers.
  473. */
  474. static inline void
  475. media_set_bus_info(char *bus_info, size_t bus_info_size, struct device *dev)
  476. {
  477. if (!dev)
  478. strscpy(bus_info, "no bus info", bus_info_size);
  479. else if (dev_is_platform(dev))
  480. snprintf(bus_info, bus_info_size, "platform:%s", dev_name(dev));
  481. else if (dev_is_pci(dev))
  482. snprintf(bus_info, bus_info_size, "PCI:%s", dev_name(dev));
  483. }
  484. #endif