media-device.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * Media device
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. * Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /* We need to access legacy defines from linux/media.h */
  23. #define __NEED_MEDIA_LEGACY_API
  24. #include <linux/compat.h>
  25. #include <linux/export.h>
  26. #include <linux/idr.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/media.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/pci.h>
  32. #include <linux/usb.h>
  33. #include <media/media-device.h>
  34. #include <media/media-devnode.h>
  35. #include <media/media-entity.h>
  36. #ifdef CONFIG_MEDIA_CONTROLLER
  37. /* -----------------------------------------------------------------------------
  38. * Userspace API
  39. */
  40. static inline void __user *media_get_uptr(__u64 arg)
  41. {
  42. return (void __user *)(uintptr_t)arg;
  43. }
  44. static int media_device_open(struct file *filp)
  45. {
  46. return 0;
  47. }
  48. static int media_device_close(struct file *filp)
  49. {
  50. return 0;
  51. }
  52. static long media_device_get_info(struct media_device *dev, void *arg)
  53. {
  54. struct media_device_info *info = arg;
  55. memset(info, 0, sizeof(*info));
  56. if (dev->driver_name[0])
  57. strlcpy(info->driver, dev->driver_name, sizeof(info->driver));
  58. else
  59. strlcpy(info->driver, dev->dev->driver->name,
  60. sizeof(info->driver));
  61. strlcpy(info->model, dev->model, sizeof(info->model));
  62. strlcpy(info->serial, dev->serial, sizeof(info->serial));
  63. strlcpy(info->bus_info, dev->bus_info, sizeof(info->bus_info));
  64. info->media_version = MEDIA_API_VERSION;
  65. info->hw_revision = dev->hw_revision;
  66. info->driver_version = dev->driver_version;
  67. return 0;
  68. }
  69. static struct media_entity *find_entity(struct media_device *mdev, u32 id)
  70. {
  71. struct media_entity *entity;
  72. int next = id & MEDIA_ENT_ID_FLAG_NEXT;
  73. id &= ~MEDIA_ENT_ID_FLAG_NEXT;
  74. media_device_for_each_entity(entity, mdev) {
  75. if (((media_entity_id(entity) == id) && !next) ||
  76. ((media_entity_id(entity) > id) && next)) {
  77. return entity;
  78. }
  79. }
  80. return NULL;
  81. }
  82. static long media_device_enum_entities(struct media_device *mdev, void *arg)
  83. {
  84. struct media_entity_desc *entd = arg;
  85. struct media_entity *ent;
  86. ent = find_entity(mdev, entd->id);
  87. if (ent == NULL)
  88. return -EINVAL;
  89. memset(entd, 0, sizeof(*entd));
  90. entd->id = media_entity_id(ent);
  91. if (ent->name)
  92. strlcpy(entd->name, ent->name, sizeof(entd->name));
  93. entd->type = ent->function;
  94. entd->revision = 0; /* Unused */
  95. entd->flags = ent->flags;
  96. entd->group_id = 0; /* Unused */
  97. entd->pads = ent->num_pads;
  98. entd->links = ent->num_links - ent->num_backlinks;
  99. /*
  100. * Workaround for a bug at media-ctl <= v1.10 that makes it to
  101. * do the wrong thing if the entity function doesn't belong to
  102. * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
  103. * Ranges.
  104. *
  105. * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
  106. * or, otherwise, will be silently ignored by media-ctl when
  107. * printing the graphviz diagram. So, map them into the devnode
  108. * old range.
  109. */
  110. if (ent->function < MEDIA_ENT_F_OLD_BASE ||
  111. ent->function > MEDIA_ENT_F_TUNER) {
  112. if (is_media_entity_v4l2_subdev(ent))
  113. entd->type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
  114. else if (ent->function != MEDIA_ENT_F_IO_V4L)
  115. entd->type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
  116. }
  117. memcpy(&entd->raw, &ent->info, sizeof(ent->info));
  118. return 0;
  119. }
  120. static void media_device_kpad_to_upad(const struct media_pad *kpad,
  121. struct media_pad_desc *upad)
  122. {
  123. upad->entity = media_entity_id(kpad->entity);
  124. upad->index = kpad->index;
  125. upad->flags = kpad->flags;
  126. }
  127. static long media_device_enum_links(struct media_device *mdev, void *arg)
  128. {
  129. struct media_links_enum *links = arg;
  130. struct media_entity *entity;
  131. entity = find_entity(mdev, links->entity);
  132. if (entity == NULL)
  133. return -EINVAL;
  134. if (links->pads) {
  135. unsigned int p;
  136. for (p = 0; p < entity->num_pads; p++) {
  137. struct media_pad_desc pad;
  138. memset(&pad, 0, sizeof(pad));
  139. media_device_kpad_to_upad(&entity->pads[p], &pad);
  140. if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
  141. return -EFAULT;
  142. }
  143. }
  144. if (links->links) {
  145. struct media_link *link;
  146. struct media_link_desc __user *ulink_desc = links->links;
  147. list_for_each_entry(link, &entity->links, list) {
  148. struct media_link_desc klink_desc;
  149. /* Ignore backlinks. */
  150. if (link->source->entity != entity)
  151. continue;
  152. memset(&klink_desc, 0, sizeof(klink_desc));
  153. media_device_kpad_to_upad(link->source,
  154. &klink_desc.source);
  155. media_device_kpad_to_upad(link->sink,
  156. &klink_desc.sink);
  157. klink_desc.flags = link->flags;
  158. if (copy_to_user(ulink_desc, &klink_desc,
  159. sizeof(*ulink_desc)))
  160. return -EFAULT;
  161. ulink_desc++;
  162. }
  163. }
  164. return 0;
  165. }
  166. static long media_device_setup_link(struct media_device *mdev, void *arg)
  167. {
  168. struct media_link_desc *linkd = arg;
  169. struct media_link *link = NULL;
  170. struct media_entity *source;
  171. struct media_entity *sink;
  172. /* Find the source and sink entities and link.
  173. */
  174. source = find_entity(mdev, linkd->source.entity);
  175. sink = find_entity(mdev, linkd->sink.entity);
  176. if (source == NULL || sink == NULL)
  177. return -EINVAL;
  178. if (linkd->source.index >= source->num_pads ||
  179. linkd->sink.index >= sink->num_pads)
  180. return -EINVAL;
  181. link = media_entity_find_link(&source->pads[linkd->source.index],
  182. &sink->pads[linkd->sink.index]);
  183. if (link == NULL)
  184. return -EINVAL;
  185. /* Setup the link on both entities. */
  186. return __media_entity_setup_link(link, linkd->flags);
  187. }
  188. static long media_device_get_topology(struct media_device *mdev, void *arg)
  189. {
  190. struct media_v2_topology *topo = arg;
  191. struct media_entity *entity;
  192. struct media_interface *intf;
  193. struct media_pad *pad;
  194. struct media_link *link;
  195. struct media_v2_entity kentity, __user *uentity;
  196. struct media_v2_interface kintf, __user *uintf;
  197. struct media_v2_pad kpad, __user *upad;
  198. struct media_v2_link klink, __user *ulink;
  199. unsigned int i;
  200. int ret = 0;
  201. topo->topology_version = mdev->topology_version;
  202. /* Get entities and number of entities */
  203. i = 0;
  204. uentity = media_get_uptr(topo->ptr_entities);
  205. media_device_for_each_entity(entity, mdev) {
  206. i++;
  207. if (ret || !uentity)
  208. continue;
  209. if (i > topo->num_entities) {
  210. ret = -ENOSPC;
  211. continue;
  212. }
  213. /* Copy fields to userspace struct if not error */
  214. memset(&kentity, 0, sizeof(kentity));
  215. kentity.id = entity->graph_obj.id;
  216. kentity.function = entity->function;
  217. strncpy(kentity.name, entity->name,
  218. sizeof(kentity.name));
  219. if (copy_to_user(uentity, &kentity, sizeof(kentity)))
  220. ret = -EFAULT;
  221. uentity++;
  222. }
  223. topo->num_entities = i;
  224. /* Get interfaces and number of interfaces */
  225. i = 0;
  226. uintf = media_get_uptr(topo->ptr_interfaces);
  227. media_device_for_each_intf(intf, mdev) {
  228. i++;
  229. if (ret || !uintf)
  230. continue;
  231. if (i > topo->num_interfaces) {
  232. ret = -ENOSPC;
  233. continue;
  234. }
  235. memset(&kintf, 0, sizeof(kintf));
  236. /* Copy intf fields to userspace struct */
  237. kintf.id = intf->graph_obj.id;
  238. kintf.intf_type = intf->type;
  239. kintf.flags = intf->flags;
  240. if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
  241. struct media_intf_devnode *devnode;
  242. devnode = intf_to_devnode(intf);
  243. kintf.devnode.major = devnode->major;
  244. kintf.devnode.minor = devnode->minor;
  245. }
  246. if (copy_to_user(uintf, &kintf, sizeof(kintf)))
  247. ret = -EFAULT;
  248. uintf++;
  249. }
  250. topo->num_interfaces = i;
  251. /* Get pads and number of pads */
  252. i = 0;
  253. upad = media_get_uptr(topo->ptr_pads);
  254. media_device_for_each_pad(pad, mdev) {
  255. i++;
  256. if (ret || !upad)
  257. continue;
  258. if (i > topo->num_pads) {
  259. ret = -ENOSPC;
  260. continue;
  261. }
  262. memset(&kpad, 0, sizeof(kpad));
  263. /* Copy pad fields to userspace struct */
  264. kpad.id = pad->graph_obj.id;
  265. kpad.entity_id = pad->entity->graph_obj.id;
  266. kpad.flags = pad->flags;
  267. if (copy_to_user(upad, &kpad, sizeof(kpad)))
  268. ret = -EFAULT;
  269. upad++;
  270. }
  271. topo->num_pads = i;
  272. /* Get links and number of links */
  273. i = 0;
  274. ulink = media_get_uptr(topo->ptr_links);
  275. media_device_for_each_link(link, mdev) {
  276. if (link->is_backlink)
  277. continue;
  278. i++;
  279. if (ret || !ulink)
  280. continue;
  281. if (i > topo->num_links) {
  282. ret = -ENOSPC;
  283. continue;
  284. }
  285. memset(&klink, 0, sizeof(klink));
  286. /* Copy link fields to userspace struct */
  287. klink.id = link->graph_obj.id;
  288. klink.source_id = link->gobj0->id;
  289. klink.sink_id = link->gobj1->id;
  290. klink.flags = link->flags;
  291. if (copy_to_user(ulink, &klink, sizeof(klink)))
  292. ret = -EFAULT;
  293. ulink++;
  294. }
  295. topo->num_links = i;
  296. return ret;
  297. }
  298. static long copy_arg_from_user(void *karg, void __user *uarg, unsigned int cmd)
  299. {
  300. /* All media IOCTLs are _IOWR() */
  301. if (copy_from_user(karg, uarg, _IOC_SIZE(cmd)))
  302. return -EFAULT;
  303. return 0;
  304. }
  305. static long copy_arg_to_user(void __user *uarg, void *karg, unsigned int cmd)
  306. {
  307. /* All media IOCTLs are _IOWR() */
  308. if (copy_to_user(uarg, karg, _IOC_SIZE(cmd)))
  309. return -EFAULT;
  310. return 0;
  311. }
  312. /* Do acquire the graph mutex */
  313. #define MEDIA_IOC_FL_GRAPH_MUTEX BIT(0)
  314. #define MEDIA_IOC_ARG(__cmd, func, fl, from_user, to_user) \
  315. [_IOC_NR(MEDIA_IOC_##__cmd)] = { \
  316. .cmd = MEDIA_IOC_##__cmd, \
  317. .fn = (long (*)(struct media_device *, void *))func, \
  318. .flags = fl, \
  319. .arg_from_user = from_user, \
  320. .arg_to_user = to_user, \
  321. }
  322. #define MEDIA_IOC(__cmd, func, fl) \
  323. MEDIA_IOC_ARG(__cmd, func, fl, copy_arg_from_user, copy_arg_to_user)
  324. /* the table is indexed by _IOC_NR(cmd) */
  325. struct media_ioctl_info {
  326. unsigned int cmd;
  327. unsigned short flags;
  328. long (*fn)(struct media_device *dev, void *arg);
  329. long (*arg_from_user)(void *karg, void __user *uarg, unsigned int cmd);
  330. long (*arg_to_user)(void __user *uarg, void *karg, unsigned int cmd);
  331. };
  332. static const struct media_ioctl_info ioctl_info[] = {
  333. MEDIA_IOC(DEVICE_INFO, media_device_get_info, MEDIA_IOC_FL_GRAPH_MUTEX),
  334. MEDIA_IOC(ENUM_ENTITIES, media_device_enum_entities, MEDIA_IOC_FL_GRAPH_MUTEX),
  335. MEDIA_IOC(ENUM_LINKS, media_device_enum_links, MEDIA_IOC_FL_GRAPH_MUTEX),
  336. MEDIA_IOC(SETUP_LINK, media_device_setup_link, MEDIA_IOC_FL_GRAPH_MUTEX),
  337. MEDIA_IOC(G_TOPOLOGY, media_device_get_topology, MEDIA_IOC_FL_GRAPH_MUTEX),
  338. };
  339. static long media_device_ioctl(struct file *filp, unsigned int cmd,
  340. unsigned long __arg)
  341. {
  342. struct media_devnode *devnode = media_devnode_data(filp);
  343. struct media_device *dev = devnode->media_dev;
  344. const struct media_ioctl_info *info;
  345. void __user *arg = (void __user *)__arg;
  346. char __karg[256], *karg = __karg;
  347. long ret;
  348. if (_IOC_NR(cmd) >= ARRAY_SIZE(ioctl_info)
  349. || ioctl_info[_IOC_NR(cmd)].cmd != cmd)
  350. return -ENOIOCTLCMD;
  351. info = &ioctl_info[_IOC_NR(cmd)];
  352. if (_IOC_SIZE(info->cmd) > sizeof(__karg)) {
  353. karg = kmalloc(_IOC_SIZE(info->cmd), GFP_KERNEL);
  354. if (!karg)
  355. return -ENOMEM;
  356. }
  357. if (info->arg_from_user) {
  358. ret = info->arg_from_user(karg, arg, cmd);
  359. if (ret)
  360. goto out_free;
  361. }
  362. if (info->flags & MEDIA_IOC_FL_GRAPH_MUTEX)
  363. mutex_lock(&dev->graph_mutex);
  364. ret = info->fn(dev, karg);
  365. if (info->flags & MEDIA_IOC_FL_GRAPH_MUTEX)
  366. mutex_unlock(&dev->graph_mutex);
  367. if (!ret && info->arg_to_user)
  368. ret = info->arg_to_user(arg, karg, cmd);
  369. out_free:
  370. if (karg != __karg)
  371. kfree(karg);
  372. return ret;
  373. }
  374. #ifdef CONFIG_COMPAT
  375. struct media_links_enum32 {
  376. __u32 entity;
  377. compat_uptr_t pads; /* struct media_pad_desc * */
  378. compat_uptr_t links; /* struct media_link_desc * */
  379. __u32 reserved[4];
  380. };
  381. static long media_device_enum_links32(struct media_device *mdev,
  382. struct media_links_enum32 __user *ulinks)
  383. {
  384. struct media_links_enum links;
  385. compat_uptr_t pads_ptr, links_ptr;
  386. memset(&links, 0, sizeof(links));
  387. if (get_user(links.entity, &ulinks->entity)
  388. || get_user(pads_ptr, &ulinks->pads)
  389. || get_user(links_ptr, &ulinks->links))
  390. return -EFAULT;
  391. links.pads = compat_ptr(pads_ptr);
  392. links.links = compat_ptr(links_ptr);
  393. return media_device_enum_links(mdev, &links);
  394. }
  395. #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
  396. static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
  397. unsigned long arg)
  398. {
  399. struct media_devnode *devnode = media_devnode_data(filp);
  400. struct media_device *dev = devnode->media_dev;
  401. long ret;
  402. switch (cmd) {
  403. case MEDIA_IOC_ENUM_LINKS32:
  404. mutex_lock(&dev->graph_mutex);
  405. ret = media_device_enum_links32(dev,
  406. (struct media_links_enum32 __user *)arg);
  407. mutex_unlock(&dev->graph_mutex);
  408. break;
  409. default:
  410. return media_device_ioctl(filp, cmd, arg);
  411. }
  412. return ret;
  413. }
  414. #endif /* CONFIG_COMPAT */
  415. static const struct media_file_operations media_device_fops = {
  416. .owner = THIS_MODULE,
  417. .open = media_device_open,
  418. .ioctl = media_device_ioctl,
  419. #ifdef CONFIG_COMPAT
  420. .compat_ioctl = media_device_compat_ioctl,
  421. #endif /* CONFIG_COMPAT */
  422. .release = media_device_close,
  423. };
  424. /* -----------------------------------------------------------------------------
  425. * sysfs
  426. */
  427. static ssize_t show_model(struct device *cd,
  428. struct device_attribute *attr, char *buf)
  429. {
  430. struct media_devnode *devnode = to_media_devnode(cd);
  431. struct media_device *mdev = devnode->media_dev;
  432. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  433. }
  434. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  435. /* -----------------------------------------------------------------------------
  436. * Registration/unregistration
  437. */
  438. static void media_device_release(struct media_devnode *mdev)
  439. {
  440. dev_dbg(mdev->parent, "Media device released\n");
  441. }
  442. /**
  443. * media_device_register_entity - Register an entity with a media device
  444. * @mdev: The media device
  445. * @entity: The entity
  446. */
  447. int __must_check media_device_register_entity(struct media_device *mdev,
  448. struct media_entity *entity)
  449. {
  450. struct media_entity_notify *notify, *next;
  451. unsigned int i;
  452. int ret;
  453. if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
  454. entity->function == MEDIA_ENT_F_UNKNOWN)
  455. dev_warn(mdev->dev,
  456. "Entity type for entity %s was not initialized!\n",
  457. entity->name);
  458. /* Warn if we apparently re-register an entity */
  459. WARN_ON(entity->graph_obj.mdev != NULL);
  460. entity->graph_obj.mdev = mdev;
  461. INIT_LIST_HEAD(&entity->links);
  462. entity->num_links = 0;
  463. entity->num_backlinks = 0;
  464. if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
  465. return -ENOMEM;
  466. mutex_lock(&mdev->graph_mutex);
  467. ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
  468. &entity->internal_idx);
  469. if (ret < 0) {
  470. mutex_unlock(&mdev->graph_mutex);
  471. return ret;
  472. }
  473. mdev->entity_internal_idx_max =
  474. max(mdev->entity_internal_idx_max, entity->internal_idx);
  475. /* Initialize media_gobj embedded at the entity */
  476. media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
  477. /* Initialize objects at the pads */
  478. for (i = 0; i < entity->num_pads; i++)
  479. media_gobj_create(mdev, MEDIA_GRAPH_PAD,
  480. &entity->pads[i].graph_obj);
  481. /* invoke entity_notify callbacks */
  482. list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
  483. (notify)->notify(entity, notify->notify_data);
  484. }
  485. if (mdev->entity_internal_idx_max
  486. >= mdev->pm_count_walk.ent_enum.idx_max) {
  487. struct media_entity_graph new = { .top = 0 };
  488. /*
  489. * Initialise the new graph walk before cleaning up
  490. * the old one in order not to spoil the graph walk
  491. * object of the media device if graph walk init fails.
  492. */
  493. ret = media_entity_graph_walk_init(&new, mdev);
  494. if (ret) {
  495. mutex_unlock(&mdev->graph_mutex);
  496. return ret;
  497. }
  498. media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
  499. mdev->pm_count_walk = new;
  500. }
  501. mutex_unlock(&mdev->graph_mutex);
  502. return 0;
  503. }
  504. EXPORT_SYMBOL_GPL(media_device_register_entity);
  505. static void __media_device_unregister_entity(struct media_entity *entity)
  506. {
  507. struct media_device *mdev = entity->graph_obj.mdev;
  508. struct media_link *link, *tmp;
  509. struct media_interface *intf;
  510. unsigned int i;
  511. ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
  512. /* Remove all interface links pointing to this entity */
  513. list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
  514. list_for_each_entry_safe(link, tmp, &intf->links, list) {
  515. if (link->entity == entity)
  516. __media_remove_intf_link(link);
  517. }
  518. }
  519. /* Remove all data links that belong to this entity */
  520. __media_entity_remove_links(entity);
  521. /* Remove all pads that belong to this entity */
  522. for (i = 0; i < entity->num_pads; i++)
  523. media_gobj_destroy(&entity->pads[i].graph_obj);
  524. /* Remove the entity */
  525. media_gobj_destroy(&entity->graph_obj);
  526. /* invoke entity_notify callbacks to handle entity removal?? */
  527. entity->graph_obj.mdev = NULL;
  528. }
  529. void media_device_unregister_entity(struct media_entity *entity)
  530. {
  531. struct media_device *mdev = entity->graph_obj.mdev;
  532. if (mdev == NULL)
  533. return;
  534. mutex_lock(&mdev->graph_mutex);
  535. __media_device_unregister_entity(entity);
  536. mutex_unlock(&mdev->graph_mutex);
  537. }
  538. EXPORT_SYMBOL_GPL(media_device_unregister_entity);
  539. /**
  540. * media_device_init() - initialize a media device
  541. * @mdev: The media device
  542. *
  543. * The caller is responsible for initializing the media device before
  544. * registration. The following fields must be set:
  545. *
  546. * - dev must point to the parent device
  547. * - model must be filled with the device model name
  548. */
  549. void media_device_init(struct media_device *mdev)
  550. {
  551. INIT_LIST_HEAD(&mdev->entities);
  552. INIT_LIST_HEAD(&mdev->interfaces);
  553. INIT_LIST_HEAD(&mdev->pads);
  554. INIT_LIST_HEAD(&mdev->links);
  555. INIT_LIST_HEAD(&mdev->entity_notify);
  556. mutex_init(&mdev->graph_mutex);
  557. ida_init(&mdev->entity_internal_idx);
  558. dev_dbg(mdev->dev, "Media device initialized\n");
  559. }
  560. EXPORT_SYMBOL_GPL(media_device_init);
  561. void media_device_cleanup(struct media_device *mdev)
  562. {
  563. ida_destroy(&mdev->entity_internal_idx);
  564. mdev->entity_internal_idx_max = 0;
  565. media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
  566. mutex_destroy(&mdev->graph_mutex);
  567. }
  568. EXPORT_SYMBOL_GPL(media_device_cleanup);
  569. int __must_check __media_device_register(struct media_device *mdev,
  570. struct module *owner)
  571. {
  572. struct media_devnode *devnode;
  573. int ret;
  574. devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
  575. if (!devnode)
  576. return -ENOMEM;
  577. /* Register the device node. */
  578. mdev->devnode = devnode;
  579. devnode->fops = &media_device_fops;
  580. devnode->parent = mdev->dev;
  581. devnode->release = media_device_release;
  582. /* Set version 0 to indicate user-space that the graph is static */
  583. mdev->topology_version = 0;
  584. ret = media_devnode_register(mdev, devnode, owner);
  585. if (ret < 0) {
  586. /* devnode free is handled in media_devnode_*() */
  587. mdev->devnode = NULL;
  588. return ret;
  589. }
  590. ret = device_create_file(&devnode->dev, &dev_attr_model);
  591. if (ret < 0) {
  592. /* devnode free is handled in media_devnode_*() */
  593. mdev->devnode = NULL;
  594. media_devnode_unregister_prepare(devnode);
  595. media_devnode_unregister(devnode);
  596. return ret;
  597. }
  598. dev_dbg(mdev->dev, "Media device registered\n");
  599. return 0;
  600. }
  601. EXPORT_SYMBOL_GPL(__media_device_register);
  602. int __must_check media_device_register_entity_notify(struct media_device *mdev,
  603. struct media_entity_notify *nptr)
  604. {
  605. mutex_lock(&mdev->graph_mutex);
  606. list_add_tail(&nptr->list, &mdev->entity_notify);
  607. mutex_unlock(&mdev->graph_mutex);
  608. return 0;
  609. }
  610. EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
  611. /*
  612. * Note: Should be called with mdev->lock held.
  613. */
  614. static void __media_device_unregister_entity_notify(struct media_device *mdev,
  615. struct media_entity_notify *nptr)
  616. {
  617. list_del(&nptr->list);
  618. }
  619. void media_device_unregister_entity_notify(struct media_device *mdev,
  620. struct media_entity_notify *nptr)
  621. {
  622. mutex_lock(&mdev->graph_mutex);
  623. __media_device_unregister_entity_notify(mdev, nptr);
  624. mutex_unlock(&mdev->graph_mutex);
  625. }
  626. EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
  627. void media_device_unregister(struct media_device *mdev)
  628. {
  629. struct media_entity *entity;
  630. struct media_entity *next;
  631. struct media_interface *intf, *tmp_intf;
  632. struct media_entity_notify *notify, *nextp;
  633. if (mdev == NULL)
  634. return;
  635. mutex_lock(&mdev->graph_mutex);
  636. /* Check if mdev was ever registered at all */
  637. if (!media_devnode_is_registered(mdev->devnode)) {
  638. mutex_unlock(&mdev->graph_mutex);
  639. return;
  640. }
  641. /* Clear the devnode register bit to avoid races with media dev open */
  642. media_devnode_unregister_prepare(mdev->devnode);
  643. /* Remove all entities from the media device */
  644. list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
  645. __media_device_unregister_entity(entity);
  646. /* Remove all entity_notify callbacks from the media device */
  647. list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
  648. __media_device_unregister_entity_notify(mdev, notify);
  649. /* Remove all interfaces from the media device */
  650. list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
  651. graph_obj.list) {
  652. __media_remove_intf_links(intf);
  653. media_gobj_destroy(&intf->graph_obj);
  654. kfree(intf);
  655. }
  656. mutex_unlock(&mdev->graph_mutex);
  657. dev_dbg(mdev->dev, "Media device unregistered\n");
  658. device_remove_file(&mdev->devnode->dev, &dev_attr_model);
  659. media_devnode_unregister(mdev->devnode);
  660. /* devnode free is handled in media_devnode_*() */
  661. mdev->devnode = NULL;
  662. }
  663. EXPORT_SYMBOL_GPL(media_device_unregister);
  664. static void media_device_release_devres(struct device *dev, void *res)
  665. {
  666. }
  667. struct media_device *media_device_get_devres(struct device *dev)
  668. {
  669. struct media_device *mdev;
  670. mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
  671. if (mdev)
  672. return mdev;
  673. mdev = devres_alloc(media_device_release_devres,
  674. sizeof(struct media_device), GFP_KERNEL);
  675. if (!mdev)
  676. return NULL;
  677. return devres_get(dev, mdev, NULL, NULL);
  678. }
  679. EXPORT_SYMBOL_GPL(media_device_get_devres);
  680. struct media_device *media_device_find_devres(struct device *dev)
  681. {
  682. return devres_find(dev, media_device_release_devres, NULL, NULL);
  683. }
  684. EXPORT_SYMBOL_GPL(media_device_find_devres);
  685. #if IS_ENABLED(CONFIG_PCI)
  686. void media_device_pci_init(struct media_device *mdev,
  687. struct pci_dev *pci_dev,
  688. const char *name)
  689. {
  690. mdev->dev = &pci_dev->dev;
  691. if (name)
  692. strlcpy(mdev->model, name, sizeof(mdev->model));
  693. else
  694. strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
  695. sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
  696. mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
  697. | pci_dev->subsystem_device;
  698. mdev->driver_version = LINUX_VERSION_CODE;
  699. media_device_init(mdev);
  700. }
  701. EXPORT_SYMBOL_GPL(media_device_pci_init);
  702. #endif
  703. #if IS_ENABLED(CONFIG_USB)
  704. void __media_device_usb_init(struct media_device *mdev,
  705. struct usb_device *udev,
  706. const char *board_name,
  707. const char *driver_name)
  708. {
  709. mdev->dev = &udev->dev;
  710. if (driver_name)
  711. strlcpy(mdev->driver_name, driver_name,
  712. sizeof(mdev->driver_name));
  713. if (board_name)
  714. strlcpy(mdev->model, board_name, sizeof(mdev->model));
  715. else if (udev->product)
  716. strlcpy(mdev->model, udev->product, sizeof(mdev->model));
  717. else
  718. strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
  719. if (udev->serial)
  720. strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
  721. usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
  722. mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
  723. mdev->driver_version = LINUX_VERSION_CODE;
  724. media_device_init(mdev);
  725. }
  726. EXPORT_SYMBOL_GPL(__media_device_usb_init);
  727. #endif
  728. #endif /* CONFIG_MEDIA_CONTROLLER */