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