media-device.c 22 KB

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