Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | b2cd274 | 2016-03-05 07:13:39 -0300 | [diff] [blame] | 23 | /* We need to access legacy defines from linux/media.h */ |
| 24 | #define __NEED_MEDIA_LEGACY_API |
| 25 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 26 | #include <linux/compat.h> |
| 27 | #include <linux/export.h> |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 28 | #include <linux/idr.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 29 | #include <linux/ioctl.h> |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 30 | #include <linux/media.h> |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 31 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 497e80c | 2015-12-11 07:23:09 -0200 | [diff] [blame] | 32 | #include <linux/types.h> |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 33 | #include <linux/pci.h> |
| 34 | #include <linux/usb.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 35 | |
| 36 | #include <media/media-device.h> |
| 37 | #include <media/media-devnode.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 38 | #include <media/media-entity.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 39 | |
Shuah Khan | e576d60 | 2015-06-05 17:11:54 -0300 | [diff] [blame] | 40 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 41 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 42 | /* ----------------------------------------------------------------------------- |
| 43 | * Userspace API |
| 44 | */ |
| 45 | |
Sakari Ailus | 0629e99 | 2016-02-22 17:47:04 -0300 | [diff] [blame] | 46 | static inline void __user *media_get_uptr(__u64 arg) |
| 47 | { |
| 48 | return (void __user *)(uintptr_t)arg; |
| 49 | } |
| 50 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 51 | static int media_device_open(struct file *filp) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int media_device_close(struct file *filp) |
| 57 | { |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int media_device_get_info(struct media_device *dev, |
| 62 | struct media_device_info __user *__info) |
| 63 | { |
| 64 | struct media_device_info info; |
| 65 | |
| 66 | memset(&info, 0, sizeof(info)); |
| 67 | |
Mauro Carvalho Chehab | bb07bd6 | 2016-02-11 14:24:23 -0200 | [diff] [blame] | 68 | if (dev->driver_name[0]) |
| 69 | strlcpy(info.driver, dev->driver_name, sizeof(info.driver)); |
| 70 | else |
| 71 | strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver)); |
| 72 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 73 | strlcpy(info.model, dev->model, sizeof(info.model)); |
| 74 | strlcpy(info.serial, dev->serial, sizeof(info.serial)); |
| 75 | strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info)); |
| 76 | |
| 77 | info.media_version = MEDIA_API_VERSION; |
| 78 | info.hw_revision = dev->hw_revision; |
| 79 | info.driver_version = dev->driver_version; |
| 80 | |
Nicolas THERY | b17d394 | 2012-08-07 05:24:59 -0300 | [diff] [blame] | 81 | if (copy_to_user(__info, &info, sizeof(*__info))) |
| 82 | return -EFAULT; |
| 83 | return 0; |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 84 | } |
| 85 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 86 | static struct media_entity *find_entity(struct media_device *mdev, u32 id) |
| 87 | { |
| 88 | struct media_entity *entity; |
| 89 | int next = id & MEDIA_ENT_ID_FLAG_NEXT; |
| 90 | |
| 91 | id &= ~MEDIA_ENT_ID_FLAG_NEXT; |
| 92 | |
| 93 | spin_lock(&mdev->lock); |
| 94 | |
| 95 | media_device_for_each_entity(entity, mdev) { |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 96 | if (((media_entity_id(entity) == id) && !next) || |
| 97 | ((media_entity_id(entity) > id) && next)) { |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 98 | spin_unlock(&mdev->lock); |
| 99 | return entity; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | spin_unlock(&mdev->lock); |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | static long media_device_enum_entities(struct media_device *mdev, |
| 109 | struct media_entity_desc __user *uent) |
| 110 | { |
| 111 | struct media_entity *ent; |
| 112 | struct media_entity_desc u_ent; |
| 113 | |
Salva Peiró | e6a6234 | 2014-04-30 19:48:02 +0200 | [diff] [blame] | 114 | memset(&u_ent, 0, sizeof(u_ent)); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 115 | if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id))) |
| 116 | return -EFAULT; |
| 117 | |
| 118 | ent = find_entity(mdev, u_ent.id); |
| 119 | |
| 120 | if (ent == NULL) |
| 121 | return -EINVAL; |
| 122 | |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 123 | u_ent.id = media_entity_id(ent); |
Laurent Pinchart | de8eae3 | 2014-07-17 08:52:08 -0300 | [diff] [blame] | 124 | if (ent->name) |
| 125 | strlcpy(u_ent.name, ent->name, sizeof(u_ent.name)); |
Mauro Carvalho Chehab | 0e576b7 | 2015-09-06 09:33:39 -0300 | [diff] [blame] | 126 | u_ent.type = ent->function; |
Mauro Carvalho Chehab | 8ed8c88 | 2015-12-11 08:02:19 -0200 | [diff] [blame] | 127 | u_ent.revision = 0; /* Unused */ |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 128 | u_ent.flags = ent->flags; |
Mauro Carvalho Chehab | 8ed8c88 | 2015-12-11 08:02:19 -0200 | [diff] [blame] | 129 | u_ent.group_id = 0; /* Unused */ |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 130 | u_ent.pads = ent->num_pads; |
| 131 | u_ent.links = ent->num_links - ent->num_backlinks; |
Mauro Carvalho Chehab | b2cd274 | 2016-03-05 07:13:39 -0300 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Workaround for a bug at media-ctl <= v1.10 that makes it to |
| 135 | * do the wrong thing if the entity function doesn't belong to |
| 136 | * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE |
| 137 | * Ranges. |
| 138 | * |
| 139 | * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE, |
| 140 | * or, otherwise, will be silently ignored by media-ctl when |
| 141 | * printing the graphviz diagram. So, map them into the devnode |
| 142 | * old range. |
| 143 | */ |
| 144 | if (ent->function < MEDIA_ENT_F_OLD_BASE || |
| 145 | ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) { |
| 146 | if (is_media_entity_v4l2_subdev(ent)) |
| 147 | u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; |
| 148 | else if (ent->function != MEDIA_ENT_F_IO_V4L) |
| 149 | u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN; |
| 150 | } |
| 151 | |
Clemens Ladisch | fa5034c | 2011-11-05 18:42:01 -0300 | [diff] [blame] | 152 | memcpy(&u_ent.raw, &ent->info, sizeof(ent->info)); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 153 | if (copy_to_user(uent, &u_ent, sizeof(u_ent))) |
| 154 | return -EFAULT; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static void media_device_kpad_to_upad(const struct media_pad *kpad, |
| 159 | struct media_pad_desc *upad) |
| 160 | { |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 161 | upad->entity = media_entity_id(kpad->entity); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 162 | upad->index = kpad->index; |
| 163 | upad->flags = kpad->flags; |
| 164 | } |
| 165 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 166 | static long __media_device_enum_links(struct media_device *mdev, |
| 167 | struct media_links_enum *links) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 168 | { |
| 169 | struct media_entity *entity; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 170 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 171 | entity = find_entity(mdev, links->entity); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 172 | if (entity == NULL) |
| 173 | return -EINVAL; |
| 174 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 175 | if (links->pads) { |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 176 | unsigned int p; |
| 177 | |
| 178 | for (p = 0; p < entity->num_pads; p++) { |
| 179 | struct media_pad_desc pad; |
Dan Carpenter | c88e739 | 2013-04-13 06:32:15 -0300 | [diff] [blame] | 180 | |
| 181 | memset(&pad, 0, sizeof(pad)); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 182 | media_device_kpad_to_upad(&entity->pads[p], &pad); |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 183 | if (copy_to_user(&links->pads[p], &pad, sizeof(pad))) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 184 | return -EFAULT; |
| 185 | } |
| 186 | } |
| 187 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 188 | if (links->links) { |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 189 | struct media_link *link; |
| 190 | struct media_link_desc __user *ulink_desc = links->links; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 191 | |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 192 | list_for_each_entry(link, &entity->links, list) { |
| 193 | struct media_link_desc klink_desc; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 194 | |
| 195 | /* Ignore backlinks. */ |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 196 | if (link->source->entity != entity) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 197 | continue; |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 198 | memset(&klink_desc, 0, sizeof(klink_desc)); |
| 199 | media_device_kpad_to_upad(link->source, |
| 200 | &klink_desc.source); |
| 201 | media_device_kpad_to_upad(link->sink, |
| 202 | &klink_desc.sink); |
| 203 | klink_desc.flags = link->flags; |
| 204 | if (copy_to_user(ulink_desc, &klink_desc, |
| 205 | sizeof(*ulink_desc))) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 206 | return -EFAULT; |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 207 | ulink_desc++; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 208 | } |
| 209 | } |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static long media_device_enum_links(struct media_device *mdev, |
| 215 | struct media_links_enum __user *ulinks) |
| 216 | { |
| 217 | struct media_links_enum links; |
| 218 | int rval; |
| 219 | |
| 220 | if (copy_from_user(&links, ulinks, sizeof(links))) |
| 221 | return -EFAULT; |
| 222 | |
| 223 | rval = __media_device_enum_links(mdev, &links); |
| 224 | if (rval < 0) |
| 225 | return rval; |
| 226 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 227 | if (copy_to_user(ulinks, &links, sizeof(*ulinks))) |
| 228 | return -EFAULT; |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 229 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 233 | static long media_device_setup_link(struct media_device *mdev, |
| 234 | struct media_link_desc __user *_ulink) |
| 235 | { |
| 236 | struct media_link *link = NULL; |
| 237 | struct media_link_desc ulink; |
| 238 | struct media_entity *source; |
| 239 | struct media_entity *sink; |
| 240 | int ret; |
| 241 | |
| 242 | if (copy_from_user(&ulink, _ulink, sizeof(ulink))) |
| 243 | return -EFAULT; |
| 244 | |
| 245 | /* Find the source and sink entities and link. |
| 246 | */ |
| 247 | source = find_entity(mdev, ulink.source.entity); |
| 248 | sink = find_entity(mdev, ulink.sink.entity); |
| 249 | |
| 250 | if (source == NULL || sink == NULL) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | if (ulink.source.index >= source->num_pads || |
| 254 | ulink.sink.index >= sink->num_pads) |
| 255 | return -EINVAL; |
| 256 | |
| 257 | link = media_entity_find_link(&source->pads[ulink.source.index], |
| 258 | &sink->pads[ulink.sink.index]); |
| 259 | if (link == NULL) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | /* Setup the link on both entities. */ |
| 263 | ret = __media_entity_setup_link(link, ulink.flags); |
| 264 | |
| 265 | if (copy_to_user(_ulink, &ulink, sizeof(ulink))) |
| 266 | return -EFAULT; |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 271 | static long __media_device_get_topology(struct media_device *mdev, |
| 272 | struct media_v2_topology *topo) |
| 273 | { |
| 274 | struct media_entity *entity; |
| 275 | struct media_interface *intf; |
| 276 | struct media_pad *pad; |
| 277 | struct media_link *link; |
Sakari Ailus | d37410c | 2016-02-22 17:47:03 -0300 | [diff] [blame] | 278 | struct media_v2_entity kentity, __user *uentity; |
| 279 | struct media_v2_interface kintf, __user *uintf; |
| 280 | struct media_v2_pad kpad, __user *upad; |
| 281 | struct media_v2_link klink, __user *ulink; |
Mauro Carvalho Chehab | 9033b1a | 2015-09-09 08:23:51 -0300 | [diff] [blame] | 282 | unsigned int i; |
| 283 | int ret = 0; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 284 | |
| 285 | topo->topology_version = mdev->topology_version; |
| 286 | |
| 287 | /* Get entities and number of entities */ |
| 288 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 289 | uentity = media_get_uptr(topo->ptr_entities); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 290 | media_device_for_each_entity(entity, mdev) { |
| 291 | i++; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 292 | if (ret || !uentity) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 293 | continue; |
| 294 | |
| 295 | if (i > topo->num_entities) { |
| 296 | ret = -ENOSPC; |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | /* Copy fields to userspace struct if not error */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 301 | memset(&kentity, 0, sizeof(kentity)); |
| 302 | kentity.id = entity->graph_obj.id; |
| 303 | kentity.function = entity->function; |
| 304 | strncpy(kentity.name, entity->name, |
| 305 | sizeof(kentity.name)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 306 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 307 | if (copy_to_user(uentity, &kentity, sizeof(kentity))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 308 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 309 | uentity++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 310 | } |
| 311 | topo->num_entities = i; |
| 312 | |
| 313 | /* Get interfaces and number of interfaces */ |
| 314 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 315 | uintf = media_get_uptr(topo->ptr_interfaces); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 316 | media_device_for_each_intf(intf, mdev) { |
| 317 | i++; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 318 | if (ret || !uintf) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 319 | continue; |
| 320 | |
| 321 | if (i > topo->num_interfaces) { |
| 322 | ret = -ENOSPC; |
| 323 | continue; |
| 324 | } |
| 325 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 326 | memset(&kintf, 0, sizeof(kintf)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 327 | |
| 328 | /* Copy intf fields to userspace struct */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 329 | kintf.id = intf->graph_obj.id; |
| 330 | kintf.intf_type = intf->type; |
| 331 | kintf.flags = intf->flags; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 332 | |
| 333 | if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) { |
| 334 | struct media_intf_devnode *devnode; |
| 335 | |
| 336 | devnode = intf_to_devnode(intf); |
| 337 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 338 | kintf.devnode.major = devnode->major; |
| 339 | kintf.devnode.minor = devnode->minor; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 340 | } |
| 341 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 342 | if (copy_to_user(uintf, &kintf, sizeof(kintf))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 343 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 344 | uintf++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 345 | } |
| 346 | topo->num_interfaces = i; |
| 347 | |
| 348 | /* Get pads and number of pads */ |
| 349 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 350 | upad = media_get_uptr(topo->ptr_pads); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 351 | media_device_for_each_pad(pad, mdev) { |
| 352 | i++; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 353 | if (ret || !upad) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 354 | continue; |
| 355 | |
| 356 | if (i > topo->num_pads) { |
| 357 | ret = -ENOSPC; |
| 358 | continue; |
| 359 | } |
| 360 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 361 | memset(&kpad, 0, sizeof(kpad)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 362 | |
| 363 | /* Copy pad fields to userspace struct */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 364 | kpad.id = pad->graph_obj.id; |
| 365 | kpad.entity_id = pad->entity->graph_obj.id; |
| 366 | kpad.flags = pad->flags; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 367 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 368 | if (copy_to_user(upad, &kpad, sizeof(kpad))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 369 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 370 | upad++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 371 | } |
| 372 | topo->num_pads = i; |
| 373 | |
| 374 | /* Get links and number of links */ |
| 375 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 376 | ulink = media_get_uptr(topo->ptr_links); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 377 | media_device_for_each_link(link, mdev) { |
Mauro Carvalho Chehab | 39d1ebc6 | 2015-08-30 09:53:57 -0300 | [diff] [blame] | 378 | if (link->is_backlink) |
| 379 | continue; |
| 380 | |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 381 | i++; |
| 382 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 383 | if (ret || !ulink) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 384 | continue; |
| 385 | |
| 386 | if (i > topo->num_links) { |
| 387 | ret = -ENOSPC; |
| 388 | continue; |
| 389 | } |
| 390 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 391 | memset(&klink, 0, sizeof(klink)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 392 | |
| 393 | /* Copy link fields to userspace struct */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 394 | klink.id = link->graph_obj.id; |
| 395 | klink.source_id = link->gobj0->id; |
| 396 | klink.sink_id = link->gobj1->id; |
| 397 | klink.flags = link->flags; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 398 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 399 | if (copy_to_user(ulink, &klink, sizeof(klink))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 400 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 401 | ulink++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 402 | } |
| 403 | topo->num_links = i; |
| 404 | |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | static long media_device_get_topology(struct media_device *mdev, |
| 409 | struct media_v2_topology __user *utopo) |
| 410 | { |
| 411 | struct media_v2_topology ktopo; |
| 412 | int ret; |
| 413 | |
Dan Carpenter | a5c82e5 | 2015-12-15 09:00:40 -0200 | [diff] [blame] | 414 | if (copy_from_user(&ktopo, utopo, sizeof(ktopo))) |
| 415 | return -EFAULT; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 416 | |
| 417 | ret = __media_device_get_topology(mdev, &ktopo); |
| 418 | if (ret < 0) |
| 419 | return ret; |
| 420 | |
Dan Carpenter | a5c82e5 | 2015-12-15 09:00:40 -0200 | [diff] [blame] | 421 | if (copy_to_user(utopo, &ktopo, sizeof(*utopo))) |
| 422 | return -EFAULT; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 423 | |
Dan Carpenter | a5c82e5 | 2015-12-15 09:00:40 -0200 | [diff] [blame] | 424 | return 0; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 425 | } |
| 426 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 427 | static long media_device_ioctl(struct file *filp, unsigned int cmd, |
| 428 | unsigned long arg) |
| 429 | { |
| 430 | struct media_devnode *devnode = media_devnode_data(filp); |
| 431 | struct media_device *dev = to_media_device(devnode); |
| 432 | long ret; |
| 433 | |
| 434 | switch (cmd) { |
| 435 | case MEDIA_IOC_DEVICE_INFO: |
| 436 | ret = media_device_get_info(dev, |
| 437 | (struct media_device_info __user *)arg); |
| 438 | break; |
| 439 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 440 | case MEDIA_IOC_ENUM_ENTITIES: |
| 441 | ret = media_device_enum_entities(dev, |
| 442 | (struct media_entity_desc __user *)arg); |
| 443 | break; |
| 444 | |
| 445 | case MEDIA_IOC_ENUM_LINKS: |
| 446 | mutex_lock(&dev->graph_mutex); |
| 447 | ret = media_device_enum_links(dev, |
| 448 | (struct media_links_enum __user *)arg); |
| 449 | mutex_unlock(&dev->graph_mutex); |
| 450 | break; |
| 451 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 452 | case MEDIA_IOC_SETUP_LINK: |
| 453 | mutex_lock(&dev->graph_mutex); |
| 454 | ret = media_device_setup_link(dev, |
| 455 | (struct media_link_desc __user *)arg); |
| 456 | mutex_unlock(&dev->graph_mutex); |
| 457 | break; |
| 458 | |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 459 | case MEDIA_IOC_G_TOPOLOGY: |
| 460 | mutex_lock(&dev->graph_mutex); |
| 461 | ret = media_device_get_topology(dev, |
| 462 | (struct media_v2_topology __user *)arg); |
| 463 | mutex_unlock(&dev->graph_mutex); |
| 464 | break; |
Mauro Carvalho Chehab | 333a651 | 2016-01-25 07:29:13 -0200 | [diff] [blame] | 465 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 466 | default: |
| 467 | ret = -ENOIOCTLCMD; |
| 468 | } |
| 469 | |
| 470 | return ret; |
| 471 | } |
| 472 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 473 | #ifdef CONFIG_COMPAT |
| 474 | |
| 475 | struct media_links_enum32 { |
| 476 | __u32 entity; |
| 477 | compat_uptr_t pads; /* struct media_pad_desc * */ |
| 478 | compat_uptr_t links; /* struct media_link_desc * */ |
| 479 | __u32 reserved[4]; |
| 480 | }; |
| 481 | |
| 482 | static long media_device_enum_links32(struct media_device *mdev, |
| 483 | struct media_links_enum32 __user *ulinks) |
| 484 | { |
| 485 | struct media_links_enum links; |
| 486 | compat_uptr_t pads_ptr, links_ptr; |
| 487 | |
| 488 | memset(&links, 0, sizeof(links)); |
| 489 | |
| 490 | if (get_user(links.entity, &ulinks->entity) |
| 491 | || get_user(pads_ptr, &ulinks->pads) |
| 492 | || get_user(links_ptr, &ulinks->links)) |
| 493 | return -EFAULT; |
| 494 | |
| 495 | links.pads = compat_ptr(pads_ptr); |
| 496 | links.links = compat_ptr(links_ptr); |
| 497 | |
| 498 | return __media_device_enum_links(mdev, &links); |
| 499 | } |
| 500 | |
| 501 | #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32) |
| 502 | |
| 503 | static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, |
| 504 | unsigned long arg) |
| 505 | { |
| 506 | struct media_devnode *devnode = media_devnode_data(filp); |
| 507 | struct media_device *dev = to_media_device(devnode); |
| 508 | long ret; |
| 509 | |
| 510 | switch (cmd) { |
| 511 | case MEDIA_IOC_DEVICE_INFO: |
| 512 | case MEDIA_IOC_ENUM_ENTITIES: |
| 513 | case MEDIA_IOC_SETUP_LINK: |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 514 | case MEDIA_IOC_G_TOPOLOGY: |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 515 | return media_device_ioctl(filp, cmd, arg); |
| 516 | |
| 517 | case MEDIA_IOC_ENUM_LINKS32: |
| 518 | mutex_lock(&dev->graph_mutex); |
| 519 | ret = media_device_enum_links32(dev, |
| 520 | (struct media_links_enum32 __user *)arg); |
| 521 | mutex_unlock(&dev->graph_mutex); |
| 522 | break; |
| 523 | |
| 524 | default: |
| 525 | ret = -ENOIOCTLCMD; |
| 526 | } |
| 527 | |
| 528 | return ret; |
| 529 | } |
| 530 | #endif /* CONFIG_COMPAT */ |
| 531 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 532 | static const struct media_file_operations media_device_fops = { |
| 533 | .owner = THIS_MODULE, |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 534 | .open = media_device_open, |
| 535 | .ioctl = media_device_ioctl, |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 536 | #ifdef CONFIG_COMPAT |
| 537 | .compat_ioctl = media_device_compat_ioctl, |
| 538 | #endif /* CONFIG_COMPAT */ |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 539 | .release = media_device_close, |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | /* ----------------------------------------------------------------------------- |
| 543 | * sysfs |
| 544 | */ |
| 545 | |
| 546 | static ssize_t show_model(struct device *cd, |
| 547 | struct device_attribute *attr, char *buf) |
| 548 | { |
| 549 | struct media_device *mdev = to_media_device(to_media_devnode(cd)); |
| 550 | |
| 551 | return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); |
| 552 | } |
| 553 | |
| 554 | static DEVICE_ATTR(model, S_IRUGO, show_model, NULL); |
| 555 | |
| 556 | /* ----------------------------------------------------------------------------- |
| 557 | * Registration/unregistration |
| 558 | */ |
| 559 | |
| 560 | static void media_device_release(struct media_devnode *mdev) |
| 561 | { |
Mauro Carvalho Chehab | 8f5a318 | 2015-08-13 15:22:24 -0300 | [diff] [blame] | 562 | dev_dbg(mdev->parent, "Media device released\n"); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /** |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 566 | * media_device_register_entity - Register an entity with a media device |
| 567 | * @mdev: The media device |
| 568 | * @entity: The entity |
| 569 | */ |
| 570 | int __must_check media_device_register_entity(struct media_device *mdev, |
| 571 | struct media_entity *entity) |
| 572 | { |
Shuah Khan | afcbdb5 | 2016-02-11 21:41:21 -0200 | [diff] [blame] | 573 | struct media_entity_notify *notify, *next; |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 574 | unsigned int i; |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 575 | int ret; |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 576 | |
| 577 | if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN || |
| 578 | entity->function == MEDIA_ENT_F_UNKNOWN) |
| 579 | dev_warn(mdev->dev, |
| 580 | "Entity type for entity %s was not initialized!\n", |
| 581 | entity->name); |
| 582 | |
| 583 | /* Warn if we apparently re-register an entity */ |
| 584 | WARN_ON(entity->graph_obj.mdev != NULL); |
| 585 | entity->graph_obj.mdev = mdev; |
| 586 | INIT_LIST_HEAD(&entity->links); |
| 587 | entity->num_links = 0; |
| 588 | entity->num_backlinks = 0; |
| 589 | |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 590 | if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL)) |
| 591 | return -ENOMEM; |
| 592 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 593 | spin_lock(&mdev->lock); |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 594 | |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 595 | ret = ida_get_new_above(&mdev->entity_internal_idx, 1, |
| 596 | &entity->internal_idx); |
| 597 | if (ret < 0) { |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 598 | spin_unlock(&mdev->lock); |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 599 | return ret; |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | mdev->entity_internal_idx_max = |
| 603 | max(mdev->entity_internal_idx_max, entity->internal_idx); |
| 604 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 605 | /* Initialize media_gobj embedded at the entity */ |
| 606 | media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj); |
| 607 | |
| 608 | /* Initialize objects at the pads */ |
| 609 | for (i = 0; i < entity->num_pads; i++) |
| 610 | media_gobj_create(mdev, MEDIA_GRAPH_PAD, |
| 611 | &entity->pads[i].graph_obj); |
| 612 | |
Shuah Khan | afcbdb5 | 2016-02-11 21:41:21 -0200 | [diff] [blame] | 613 | /* invoke entity_notify callbacks */ |
| 614 | list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) { |
| 615 | (notify)->notify(entity, notify->notify_data); |
| 616 | } |
| 617 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 618 | spin_unlock(&mdev->lock); |
| 619 | |
Sakari Ailus | 0c426c4 | 2016-02-21 13:25:08 -0300 | [diff] [blame] | 620 | mutex_lock(&mdev->graph_mutex); |
| 621 | if (mdev->entity_internal_idx_max |
| 622 | >= mdev->pm_count_walk.ent_enum.idx_max) { |
| 623 | struct media_entity_graph new = { .top = 0 }; |
| 624 | |
| 625 | /* |
| 626 | * Initialise the new graph walk before cleaning up |
| 627 | * the old one in order not to spoil the graph walk |
| 628 | * object of the media device if graph walk init fails. |
| 629 | */ |
| 630 | ret = media_entity_graph_walk_init(&new, mdev); |
| 631 | if (ret) { |
| 632 | mutex_unlock(&mdev->graph_mutex); |
| 633 | return ret; |
| 634 | } |
| 635 | media_entity_graph_walk_cleanup(&mdev->pm_count_walk); |
| 636 | mdev->pm_count_walk = new; |
| 637 | } |
| 638 | mutex_unlock(&mdev->graph_mutex); |
| 639 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 640 | return 0; |
| 641 | } |
| 642 | EXPORT_SYMBOL_GPL(media_device_register_entity); |
| 643 | |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 644 | static void __media_device_unregister_entity(struct media_entity *entity) |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 645 | { |
| 646 | struct media_device *mdev = entity->graph_obj.mdev; |
| 647 | struct media_link *link, *tmp; |
| 648 | struct media_interface *intf; |
| 649 | unsigned int i; |
| 650 | |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 651 | ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx); |
| 652 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 653 | /* Remove all interface links pointing to this entity */ |
| 654 | list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) { |
| 655 | list_for_each_entry_safe(link, tmp, &intf->links, list) { |
| 656 | if (link->entity == entity) |
| 657 | __media_remove_intf_link(link); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /* Remove all data links that belong to this entity */ |
| 662 | __media_entity_remove_links(entity); |
| 663 | |
| 664 | /* Remove all pads that belong to this entity */ |
| 665 | for (i = 0; i < entity->num_pads; i++) |
| 666 | media_gobj_destroy(&entity->pads[i].graph_obj); |
| 667 | |
| 668 | /* Remove the entity */ |
| 669 | media_gobj_destroy(&entity->graph_obj); |
| 670 | |
Shuah Khan | afcbdb5 | 2016-02-11 21:41:21 -0200 | [diff] [blame] | 671 | /* invoke entity_notify callbacks to handle entity removal?? */ |
| 672 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 673 | entity->graph_obj.mdev = NULL; |
| 674 | } |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 675 | |
| 676 | void media_device_unregister_entity(struct media_entity *entity) |
| 677 | { |
| 678 | struct media_device *mdev = entity->graph_obj.mdev; |
| 679 | |
| 680 | if (mdev == NULL) |
| 681 | return; |
| 682 | |
| 683 | spin_lock(&mdev->lock); |
| 684 | __media_device_unregister_entity(entity); |
| 685 | spin_unlock(&mdev->lock); |
| 686 | } |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 687 | EXPORT_SYMBOL_GPL(media_device_unregister_entity); |
| 688 | |
| 689 | /** |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 690 | * media_device_init() - initialize a media device |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 691 | * @mdev: The media device |
| 692 | * |
| 693 | * The caller is responsible for initializing the media device before |
| 694 | * registration. The following fields must be set: |
| 695 | * |
| 696 | * - dev must point to the parent device |
| 697 | * - model must be filled with the device model name |
| 698 | */ |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 699 | void media_device_init(struct media_device *mdev) |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 700 | { |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 701 | INIT_LIST_HEAD(&mdev->entities); |
Mauro Carvalho Chehab | 57cf79b | 2015-08-21 09:23:22 -0300 | [diff] [blame] | 702 | INIT_LIST_HEAD(&mdev->interfaces); |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 703 | INIT_LIST_HEAD(&mdev->pads); |
| 704 | INIT_LIST_HEAD(&mdev->links); |
Shuah Khan | afcbdb5 | 2016-02-11 21:41:21 -0200 | [diff] [blame] | 705 | INIT_LIST_HEAD(&mdev->entity_notify); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 706 | spin_lock_init(&mdev->lock); |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 707 | mutex_init(&mdev->graph_mutex); |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 708 | ida_init(&mdev->entity_internal_idx); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 709 | |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 710 | dev_dbg(mdev->dev, "Media device initialized\n"); |
| 711 | } |
| 712 | EXPORT_SYMBOL_GPL(media_device_init); |
| 713 | |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 714 | void media_device_cleanup(struct media_device *mdev) |
| 715 | { |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 716 | ida_destroy(&mdev->entity_internal_idx); |
| 717 | mdev->entity_internal_idx_max = 0; |
Sakari Ailus | 0c426c4 | 2016-02-21 13:25:08 -0300 | [diff] [blame] | 718 | media_entity_graph_walk_cleanup(&mdev->pm_count_walk); |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 719 | mutex_destroy(&mdev->graph_mutex); |
| 720 | } |
| 721 | EXPORT_SYMBOL_GPL(media_device_cleanup); |
| 722 | |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 723 | int __must_check __media_device_register(struct media_device *mdev, |
| 724 | struct module *owner) |
| 725 | { |
| 726 | int ret; |
| 727 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 728 | /* Register the device node. */ |
| 729 | mdev->devnode.fops = &media_device_fops; |
| 730 | mdev->devnode.parent = mdev->dev; |
| 731 | mdev->devnode.release = media_device_release; |
Javier Martinez Canillas | 8a95079 | 2015-12-11 20:57:09 -0200 | [diff] [blame] | 732 | |
| 733 | /* Set version 0 to indicate user-space that the graph is static */ |
| 734 | mdev->topology_version = 0; |
| 735 | |
Sakari Ailus | 85de721 | 2013-12-12 12:38:17 -0300 | [diff] [blame] | 736 | ret = media_devnode_register(&mdev->devnode, owner); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 737 | if (ret < 0) |
| 738 | return ret; |
| 739 | |
| 740 | ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); |
| 741 | if (ret < 0) { |
| 742 | media_devnode_unregister(&mdev->devnode); |
| 743 | return ret; |
| 744 | } |
| 745 | |
Mauro Carvalho Chehab | 8f5a318 | 2015-08-13 15:22:24 -0300 | [diff] [blame] | 746 | dev_dbg(mdev->dev, "Media device registered\n"); |
| 747 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 748 | return 0; |
| 749 | } |
Sakari Ailus | 85de721 | 2013-12-12 12:38:17 -0300 | [diff] [blame] | 750 | EXPORT_SYMBOL_GPL(__media_device_register); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 751 | |
Shuah Khan | afcbdb5 | 2016-02-11 21:41:21 -0200 | [diff] [blame] | 752 | int __must_check media_device_register_entity_notify(struct media_device *mdev, |
| 753 | struct media_entity_notify *nptr) |
| 754 | { |
| 755 | spin_lock(&mdev->lock); |
| 756 | list_add_tail(&nptr->list, &mdev->entity_notify); |
| 757 | spin_unlock(&mdev->lock); |
| 758 | return 0; |
| 759 | } |
| 760 | EXPORT_SYMBOL_GPL(media_device_register_entity_notify); |
| 761 | |
| 762 | /* |
| 763 | * Note: Should be called with mdev->lock held. |
| 764 | */ |
| 765 | static void __media_device_unregister_entity_notify(struct media_device *mdev, |
| 766 | struct media_entity_notify *nptr) |
| 767 | { |
| 768 | list_del(&nptr->list); |
| 769 | } |
| 770 | |
| 771 | void media_device_unregister_entity_notify(struct media_device *mdev, |
| 772 | struct media_entity_notify *nptr) |
| 773 | { |
| 774 | spin_lock(&mdev->lock); |
| 775 | __media_device_unregister_entity_notify(mdev, nptr); |
| 776 | spin_unlock(&mdev->lock); |
| 777 | } |
| 778 | EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); |
| 779 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 780 | void media_device_unregister(struct media_device *mdev) |
| 781 | { |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 782 | struct media_entity *entity; |
| 783 | struct media_entity *next; |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 784 | struct media_interface *intf, *tmp_intf; |
Shuah Khan | afcbdb5 | 2016-02-11 21:41:21 -0200 | [diff] [blame] | 785 | struct media_entity_notify *notify, *nextp; |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 786 | |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 787 | if (mdev == NULL) |
Javier Martinez Canillas | 223d19c | 2015-12-11 20:57:07 -0200 | [diff] [blame] | 788 | return; |
| 789 | |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 790 | spin_lock(&mdev->lock); |
| 791 | |
| 792 | /* Check if mdev was ever registered at all */ |
| 793 | if (!media_devnode_is_registered(&mdev->devnode)) { |
| 794 | spin_unlock(&mdev->lock); |
| 795 | return; |
| 796 | } |
| 797 | |
Mauro Carvalho Chehab | f50d516 | 2015-09-04 15:10:29 -0300 | [diff] [blame] | 798 | /* Remove all entities from the media device */ |
| 799 | list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list) |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 800 | __media_device_unregister_entity(entity); |
Mauro Carvalho Chehab | f50d516 | 2015-09-04 15:10:29 -0300 | [diff] [blame] | 801 | |
Shuah Khan | afcbdb5 | 2016-02-11 21:41:21 -0200 | [diff] [blame] | 802 | /* Remove all entity_notify callbacks from the media device */ |
| 803 | list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) |
| 804 | __media_device_unregister_entity_notify(mdev, notify); |
| 805 | |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 806 | /* Remove all interfaces from the media device */ |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 807 | list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces, |
| 808 | graph_obj.list) { |
| 809 | __media_remove_intf_links(intf); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 810 | media_gobj_destroy(&intf->graph_obj); |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 811 | kfree(intf); |
| 812 | } |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 813 | |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 814 | spin_unlock(&mdev->lock); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 815 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 816 | device_remove_file(&mdev->devnode.dev, &dev_attr_model); |
| 817 | media_devnode_unregister(&mdev->devnode); |
Mauro Carvalho Chehab | 8f5a318 | 2015-08-13 15:22:24 -0300 | [diff] [blame] | 818 | |
| 819 | dev_dbg(mdev->dev, "Media device unregistered\n"); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 820 | } |
| 821 | EXPORT_SYMBOL_GPL(media_device_unregister); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 822 | |
Shuah Khan | d062f91 | 2015-06-03 12:12:53 -0300 | [diff] [blame] | 823 | static void media_device_release_devres(struct device *dev, void *res) |
| 824 | { |
| 825 | } |
| 826 | |
Shuah Khan | d062f91 | 2015-06-03 12:12:53 -0300 | [diff] [blame] | 827 | struct media_device *media_device_get_devres(struct device *dev) |
| 828 | { |
| 829 | struct media_device *mdev; |
| 830 | |
| 831 | mdev = devres_find(dev, media_device_release_devres, NULL, NULL); |
| 832 | if (mdev) |
| 833 | return mdev; |
| 834 | |
| 835 | mdev = devres_alloc(media_device_release_devres, |
| 836 | sizeof(struct media_device), GFP_KERNEL); |
| 837 | if (!mdev) |
| 838 | return NULL; |
| 839 | return devres_get(dev, mdev, NULL, NULL); |
| 840 | } |
| 841 | EXPORT_SYMBOL_GPL(media_device_get_devres); |
| 842 | |
Shuah Khan | d062f91 | 2015-06-03 12:12:53 -0300 | [diff] [blame] | 843 | struct media_device *media_device_find_devres(struct device *dev) |
| 844 | { |
| 845 | return devres_find(dev, media_device_release_devres, NULL, NULL); |
| 846 | } |
| 847 | EXPORT_SYMBOL_GPL(media_device_find_devres); |
Shuah Khan | e576d60 | 2015-06-05 17:11:54 -0300 | [diff] [blame] | 848 | |
Mauro Carvalho Chehab | b34ecd5 | 2016-05-05 08:01:34 -0300 | [diff] [blame] | 849 | #if IS_ENABLED(CONFIG_PCI) |
Mauro Carvalho Chehab | 6cf5dad | 2016-02-22 12:10:49 -0300 | [diff] [blame] | 850 | void media_device_pci_init(struct media_device *mdev, |
| 851 | struct pci_dev *pci_dev, |
| 852 | const char *name) |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 853 | { |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 854 | mdev->dev = &pci_dev->dev; |
| 855 | |
| 856 | if (name) |
| 857 | strlcpy(mdev->model, name, sizeof(mdev->model)); |
| 858 | else |
| 859 | strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model)); |
| 860 | |
| 861 | sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev)); |
| 862 | |
| 863 | mdev->hw_revision = (pci_dev->subsystem_vendor << 16) |
| 864 | | pci_dev->subsystem_device; |
| 865 | |
| 866 | mdev->driver_version = LINUX_VERSION_CODE; |
| 867 | |
| 868 | media_device_init(mdev); |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 869 | } |
| 870 | EXPORT_SYMBOL_GPL(media_device_pci_init); |
Mauro Carvalho Chehab | b34ecd5 | 2016-05-05 08:01:34 -0300 | [diff] [blame] | 871 | #endif |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 872 | |
Mauro Carvalho Chehab | b34ecd5 | 2016-05-05 08:01:34 -0300 | [diff] [blame] | 873 | #if IS_ENABLED(CONFIG_USB) |
Mauro Carvalho Chehab | 6cf5dad | 2016-02-22 12:10:49 -0300 | [diff] [blame] | 874 | void __media_device_usb_init(struct media_device *mdev, |
| 875 | struct usb_device *udev, |
| 876 | const char *board_name, |
| 877 | const char *driver_name) |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 878 | { |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 879 | mdev->dev = &udev->dev; |
| 880 | |
| 881 | if (driver_name) |
| 882 | strlcpy(mdev->driver_name, driver_name, |
| 883 | sizeof(mdev->driver_name)); |
| 884 | |
| 885 | if (board_name) |
| 886 | strlcpy(mdev->model, board_name, sizeof(mdev->model)); |
| 887 | else if (udev->product) |
| 888 | strlcpy(mdev->model, udev->product, sizeof(mdev->model)); |
| 889 | else |
| 890 | strlcpy(mdev->model, "unknown model", sizeof(mdev->model)); |
| 891 | if (udev->serial) |
| 892 | strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial)); |
| 893 | usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info)); |
| 894 | mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice); |
| 895 | mdev->driver_version = LINUX_VERSION_CODE; |
| 896 | |
| 897 | media_device_init(mdev); |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 898 | } |
| 899 | EXPORT_SYMBOL_GPL(__media_device_usb_init); |
Mauro Carvalho Chehab | b34ecd5 | 2016-05-05 08:01:34 -0300 | [diff] [blame] | 900 | #endif |
Mauro Carvalho Chehab | 41b44e3 | 2016-02-22 11:42:04 -0300 | [diff] [blame] | 901 | |
| 902 | |
Shuah Khan | e576d60 | 2015-06-05 17:11:54 -0300 | [diff] [blame] | 903 | #endif /* CONFIG_MEDIA_CONTROLLER */ |