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