/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Media device3*4* Copyright (C) 2010 Nokia Corporation5*6* Contacts: Laurent Pinchart <[email protected]>7* Sakari Ailus <[email protected]>8*/910#ifndef _MEDIA_DEVICE_H11#define _MEDIA_DEVICE_H1213#include <linux/list.h>14#include <linux/mutex.h>15#include <linux/pci.h>16#include <linux/platform_device.h>1718#include <media/media-devnode.h>19#include <media/media-entity.h>2021struct ida;22struct media_device;2324/**25* struct media_entity_notify - Media Entity Notify26*27* @list: List head28* @notify_data: Input data to invoke the callback29* @notify: Callback function pointer30*31* Drivers may register a callback to take action when new entities get32* registered with the media device. This handler is intended for creating33* links between existing entities and should not create entities and register34* them.35*/36struct media_entity_notify {37struct list_head list;38void *notify_data;39void (*notify)(struct media_entity *entity, void *notify_data);40};4142/**43* struct media_device_ops - Media device operations44* @link_notify: Link state change notification callback. This callback is45* called with the graph_mutex held.46* @req_alloc: Allocate a request. Set this if you need to allocate a struct47* larger then struct media_request. @req_alloc and @req_free must48* either both be set or both be NULL.49* @req_free: Free a request. Set this if @req_alloc was set as well, leave50* to NULL otherwise.51* @req_validate: Validate a request, but do not queue yet. The req_queue_mutex52* lock is held when this op is called.53* @req_queue: Queue a validated request, cannot fail. If something goes54* wrong when queueing this request then it should be marked55* as such internally in the driver and any related buffers56* must eventually return to vb2 with state VB2_BUF_STATE_ERROR.57* The req_queue_mutex lock is held when this op is called.58* It is important that vb2 buffer objects are queued last after59* all other object types are queued: queueing a buffer kickstarts60* the request processing, so all other objects related to the61* request (and thus the buffer) must be available to the driver.62* And once a buffer is queued, then the driver can complete63* or delete objects from the request before req_queue exits.64*/65struct media_device_ops {66int (*link_notify)(struct media_link *link, u32 flags,67unsigned int notification);68struct media_request *(*req_alloc)(struct media_device *mdev);69void (*req_free)(struct media_request *req);70int (*req_validate)(struct media_request *req);71void (*req_queue)(struct media_request *req);72};7374/**75* struct media_device - Media device76* @dev: Parent device77* @devnode: Media device node78* @driver_name: Optional device driver name. If not set, calls to79* %MEDIA_IOC_DEVICE_INFO will return ``dev->driver->name``.80* This is needed for USB drivers for example, as otherwise81* they'll all appear as if the driver name was "usb".82* @model: Device model name83* @serial: Device serial number (optional)84* @bus_info: Unique and stable device location identifier85* @hw_revision: Hardware device revision86* @topology_version: Monotonic counter for storing the version of the graph87* topology. Should be incremented each time the topology changes.88* @id: Unique ID used on the last registered graph object89* @entity_internal_idx: Unique internal entity ID used by the graph traversal90* algorithms91* @entity_internal_idx_max: Allocated internal entity indices92* @entities: List of registered entities93* @interfaces: List of registered interfaces94* @pads: List of registered pads95* @links: List of registered links96* @entity_notify: List of registered entity_notify callbacks97* @graph_mutex: Protects access to struct media_device data98* @pm_count_walk: Graph walk for power state walk. Access serialised using99* graph_mutex.100*101* @source_priv: Driver Private data for enable/disable source handlers102* @enable_source: Enable Source Handler function pointer103* @disable_source: Disable Source Handler function pointer104*105* @ops: Operation handler callbacks106* @req_queue_mutex: Serialise the MEDIA_REQUEST_IOC_QUEUE ioctl w.r.t.107* other operations that stop or start streaming.108* @request_id: Used to generate unique request IDs109*110* This structure represents an abstract high-level media device. It allows easy111* access to entities and provides basic media device-level support. The112* structure can be allocated directly or embedded in a larger structure.113*114* The parent @dev is a physical device. It must be set before registering the115* media device.116*117* @model is a descriptive model name exported through sysfs. It doesn't have to118* be unique.119*120* @enable_source is a handler to find source entity for the121* sink entity and activate the link between them if source122* entity is free. Drivers should call this handler before123* accessing the source.124*125* @disable_source is a handler to find source entity for the126* sink entity and deactivate the link between them. Drivers127* should call this handler to release the source.128*129* Use-case: find tuner entity connected to the decoder130* entity and check if it is available, and activate the131* link between them from @enable_source and deactivate132* from @disable_source.133*134* .. note::135*136* Bridge driver is expected to implement and set the137* handler when &media_device is registered or when138* bridge driver finds the media_device during probe.139* Bridge driver sets source_priv with information140* necessary to run @enable_source and @disable_source handlers.141* Callers should hold graph_mutex to access and call @enable_source142* and @disable_source handlers.143*/144struct media_device {145/* dev->driver_data points to this struct. */146struct device *dev;147struct media_devnode *devnode;148149char model[32];150char driver_name[32];151char serial[40];152char bus_info[32];153u32 hw_revision;154155u64 topology_version;156157u32 id;158struct ida entity_internal_idx;159int entity_internal_idx_max;160161struct list_head entities;162struct list_head interfaces;163struct list_head pads;164struct list_head links;165166/* notify callback list invoked when a new entity is registered */167struct list_head entity_notify;168169/* Serializes graph operations. */170struct mutex graph_mutex;171struct media_graph pm_count_walk;172173void *source_priv;174int (*enable_source)(struct media_entity *entity,175struct media_pipeline *pipe);176void (*disable_source)(struct media_entity *entity);177178const struct media_device_ops *ops;179180struct mutex req_queue_mutex;181atomic_t request_id;182};183184/* We don't need to include usb.h here */185struct usb_device;186187#ifdef CONFIG_MEDIA_CONTROLLER188189/* Supported link_notify @notification values. */190#define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0191#define MEDIA_DEV_NOTIFY_POST_LINK_CH 1192193/**194* media_device_init() - Initializes a media device element195*196* @mdev: pointer to struct &media_device197*198* This function initializes the media device prior to its registration.199* The media device initialization and registration is split in two functions200* to avoid race conditions and make the media device available to user-space201* before the media graph has been completed.202*203* So drivers need to first initialize the media device, register any entity204* within the media device, create pad to pad links and then finally register205* the media device by calling media_device_register() as a final step.206*207* The caller is responsible for initializing the media device before208* registration. The following fields must be set:209*210* - dev must point to the parent device211* - model must be filled with the device model name212*213* The bus_info field is set by media_device_init() for PCI and platform devices214* if the field begins with '\0'.215*/216void media_device_init(struct media_device *mdev);217218/**219* media_device_cleanup() - Cleanups a media device element220*221* @mdev: pointer to struct &media_device222*223* This function that will destroy the graph_mutex that is224* initialized in media_device_init().225*/226void media_device_cleanup(struct media_device *mdev);227228/**229* __media_device_register() - Registers a media device element230*231* @mdev: pointer to struct &media_device232* @owner: should be filled with %THIS_MODULE233*234* Users, should, instead, call the media_device_register() macro.235*236* The caller is responsible for initializing the &media_device structure237* before registration. The following fields of &media_device must be set:238*239* - &media_device.model must be filled with the device model name as a240* NUL-terminated UTF-8 string. The device/model revision must not be241* stored in this field.242*243* The following fields are optional:244*245* - &media_device.serial is a unique serial number stored as a246* NUL-terminated ASCII string. The field is big enough to store a GUID247* in text form. If the hardware doesn't provide a unique serial number248* this field must be left empty.249*250* - &media_device.bus_info represents the location of the device in the251* system as a NUL-terminated ASCII string. For PCI/PCIe devices252* &media_device.bus_info must be set to "PCI:" (or "PCIe:") followed by253* the value of pci_name(). For USB devices,the usb_make_path() function254* must be used. This field is used by applications to distinguish between255* otherwise identical devices that don't provide a serial number.256*257* - &media_device.hw_revision is the hardware device revision in a258* driver-specific format. When possible the revision should be formatted259* with the KERNEL_VERSION() macro.260*261* .. note::262*263* #) 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.264*265* #) Unregistering a media device that hasn't been registered is **NOT** safe.266*267* Return: returns zero on success or a negative error code.268*/269int __must_check __media_device_register(struct media_device *mdev,270struct module *owner);271272273/**274* media_device_register() - Registers a media device element275*276* @mdev: pointer to struct &media_device277*278* This macro calls __media_device_register() passing %THIS_MODULE as279* the __media_device_register() second argument (**owner**).280*/281#define media_device_register(mdev) __media_device_register(mdev, THIS_MODULE)282283/**284* media_device_unregister() - Unregisters a media device element285*286* @mdev: pointer to struct &media_device287*288* It is safe to call this function on an unregistered (but initialised)289* media device.290*/291void media_device_unregister(struct media_device *mdev);292293/**294* media_device_register_entity() - registers a media entity inside a295* previously registered media device.296*297* @mdev: pointer to struct &media_device298* @entity: pointer to struct &media_entity to be registered299*300* Entities are identified by a unique positive integer ID. The media301* controller framework will such ID automatically. IDs are not guaranteed302* to be contiguous, and the ID number can change on newer Kernel versions.303* So, neither the driver nor userspace should hardcode ID numbers to refer304* to the entities, but, instead, use the framework to find the ID, when305* needed.306*307* The media_entity name, type and flags fields should be initialized before308* calling media_device_register_entity(). Entities embedded in higher-level309* standard structures can have some of those fields set by the higher-level310* framework.311*312* If the device has pads, media_entity_pads_init() should be called before313* this function. Otherwise, the &media_entity.pad and &media_entity.num_pads314* should be zeroed before calling this function.315*316* Entities have flags that describe the entity capabilities and state:317*318* %MEDIA_ENT_FL_DEFAULT319* indicates the default entity for a given type.320* This can be used to report the default audio and video devices or the321* default camera sensor.322*323* .. note::324*325* Drivers should set the entity function before calling this function.326* Please notice that the values %MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN and327* %MEDIA_ENT_F_UNKNOWN should not be used by the drivers.328*/329int __must_check media_device_register_entity(struct media_device *mdev,330struct media_entity *entity);331332/**333* media_device_unregister_entity() - unregisters a media entity.334*335* @entity: pointer to struct &media_entity to be unregistered336*337* All links associated with the entity and all PADs are automatically338* unregistered from the media_device when this function is called.339*340* Unregistering an entity will not change the IDs of the other entities and341* the previoully used ID will never be reused for a newly registered entities.342*343* When a media device is unregistered, all its entities are unregistered344* automatically. No manual entities unregistration is then required.345*346* .. note::347*348* The media_entity instance itself must be freed explicitly by349* the driver if required.350*/351void media_device_unregister_entity(struct media_entity *entity);352353/**354* media_device_register_entity_notify() - Registers a media entity_notify355* callback356*357* @mdev: The media device358* @nptr: The media_entity_notify359*360* .. note::361*362* When a new entity is registered, all the registered363* media_entity_notify callbacks are invoked.364*/365366void media_device_register_entity_notify(struct media_device *mdev,367struct media_entity_notify *nptr);368369/**370* media_device_unregister_entity_notify() - Unregister a media entity notify371* callback372*373* @mdev: The media device374* @nptr: The media_entity_notify375*376*/377void media_device_unregister_entity_notify(struct media_device *mdev,378struct media_entity_notify *nptr);379380/* Iterate over all entities. */381#define media_device_for_each_entity(entity, mdev) \382list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)383384/* Iterate over all interfaces. */385#define media_device_for_each_intf(intf, mdev) \386list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)387388/* Iterate over all pads. */389#define media_device_for_each_pad(pad, mdev) \390list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)391392/* Iterate over all links. */393#define media_device_for_each_link(link, mdev) \394list_for_each_entry(link, &(mdev)->links, graph_obj.list)395396/**397* media_device_pci_init() - create and initialize a398* struct &media_device from a PCI device.399*400* @mdev: pointer to struct &media_device401* @pci_dev: pointer to struct pci_dev402* @name: media device name. If %NULL, the routine will use the default403* name for the pci device, given by pci_name() macro.404*/405void media_device_pci_init(struct media_device *mdev,406struct pci_dev *pci_dev,407const char *name);408/**409* __media_device_usb_init() - create and initialize a410* struct &media_device from a PCI device.411*412* @mdev: pointer to struct &media_device413* @udev: pointer to struct usb_device414* @board_name: media device name. If %NULL, the routine will use the usb415* product name, if available.416* @driver_name: name of the driver. if %NULL, the routine will use the name417* given by ``udev->dev->driver->name``, with is usually the wrong418* thing to do.419*420* .. note::421*422* It is better to call media_device_usb_init() instead, as423* such macro fills driver_name with %KBUILD_MODNAME.424*/425void __media_device_usb_init(struct media_device *mdev,426struct usb_device *udev,427const char *board_name,428const char *driver_name);429430#else431static inline void media_device_init(struct media_device *mdev)432{433}434static inline int media_device_register(struct media_device *mdev)435{436return 0;437}438static inline void media_device_unregister(struct media_device *mdev)439{440}441static inline void media_device_cleanup(struct media_device *mdev)442{443}444static inline int media_device_register_entity(struct media_device *mdev,445struct media_entity *entity)446{447return 0;448}449static inline void media_device_unregister_entity(struct media_entity *entity)450{451}452static inline void media_device_register_entity_notify(453struct media_device *mdev,454struct media_entity_notify *nptr)455{456}457static inline void media_device_unregister_entity_notify(458struct media_device *mdev,459struct media_entity_notify *nptr)460{461}462463static inline void media_device_pci_init(struct media_device *mdev,464struct pci_dev *pci_dev,465char *name)466{467}468469static inline void __media_device_usb_init(struct media_device *mdev,470struct usb_device *udev,471char *board_name,472char *driver_name)473{474}475476#endif /* CONFIG_MEDIA_CONTROLLER */477478/**479* media_device_usb_init() - create and initialize a480* struct &media_device from a PCI device.481*482* @mdev: pointer to struct &media_device483* @udev: pointer to struct usb_device484* @name: media device name. If %NULL, the routine will use the usb485* product name, if available.486*487* This macro calls media_device_usb_init() passing the488* media_device_usb_init() **driver_name** parameter filled with489* %KBUILD_MODNAME.490*/491#define media_device_usb_init(mdev, udev, name) \492__media_device_usb_init(mdev, udev, name, KBUILD_MODNAME)493494/**495* media_set_bus_info() - Set bus_info field496*497* @bus_info: Variable where to write the bus info (char array)498* @bus_info_size: Length of the bus_info499* @dev: Related struct device500*501* Sets bus information based on &dev. This is currently done for PCI and502* platform devices. dev is required to be non-NULL for this to happen.503*504* This function is not meant to be called from drivers.505*/506static inline void507media_set_bus_info(char *bus_info, size_t bus_info_size, struct device *dev)508{509if (!dev)510strscpy(bus_info, "no bus info", bus_info_size);511else if (dev_is_platform(dev))512snprintf(bus_info, bus_info_size, "platform:%s", dev_name(dev));513else if (dev_is_pci(dev))514snprintf(bus_info, bus_info_size, "PCI:%s", dev_name(dev));515}516517#endif518519520