| 1 | // SPDX-License-Identifier: GPL-2.0-or-later | 
|---|
| 2 | /* | 
|---|
| 3 | *   ALSA sequencer Ports | 
|---|
| 4 | *   Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl> | 
|---|
| 5 | *                         Jaroslav Kysela <perex@perex.cz> | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include <sound/core.h> | 
|---|
| 9 | #include <linux/slab.h> | 
|---|
| 10 | #include <linux/module.h> | 
|---|
| 11 | #include "seq_system.h" | 
|---|
| 12 | #include "seq_ports.h" | 
|---|
| 13 | #include "seq_clientmgr.h" | 
|---|
| 14 |  | 
|---|
| 15 | /* | 
|---|
| 16 |  | 
|---|
| 17 | registration of client ports | 
|---|
| 18 |  | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | /* | 
|---|
| 23 |  | 
|---|
| 24 | NOTE: the current implementation of the port structure as a linked list is | 
|---|
| 25 | not optimal for clients that have many ports. For sending messages to all | 
|---|
| 26 | subscribers of a port we first need to find the address of the port | 
|---|
| 27 | structure, which means we have to traverse the list. A direct access table | 
|---|
| 28 | (array) would be better, but big preallocated arrays waste memory. | 
|---|
| 29 |  | 
|---|
| 30 | Possible actions: | 
|---|
| 31 |  | 
|---|
| 32 | 1) leave it this way, a client does normaly does not have more than a few | 
|---|
| 33 | ports | 
|---|
| 34 |  | 
|---|
| 35 | 2) replace the linked list of ports by a array of pointers which is | 
|---|
| 36 | dynamicly kmalloced. When a port is added or deleted we can simply allocate | 
|---|
| 37 | a new array, copy the corresponding pointers, and delete the old one. We | 
|---|
| 38 | then only need a pointer to this array, and an integer that tells us how | 
|---|
| 39 | much elements are in array. | 
|---|
| 40 |  | 
|---|
| 41 | */ | 
|---|
| 42 |  | 
|---|
| 43 | /* return pointer to port structure - port is locked if found */ | 
|---|
| 44 | struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client, | 
|---|
| 45 | int num) | 
|---|
| 46 | { | 
|---|
| 47 | struct snd_seq_client_port *port; | 
|---|
| 48 |  | 
|---|
| 49 | if (client == NULL) | 
|---|
| 50 | return NULL; | 
|---|
| 51 | guard(read_lock)(l: &client->ports_lock); | 
|---|
| 52 | list_for_each_entry(port, &client->ports_list_head, list) { | 
|---|
| 53 | if (port->addr.port == num) { | 
|---|
| 54 | if (port->closing) | 
|---|
| 55 | break; /* deleting now */ | 
|---|
| 56 | snd_use_lock_use(&port->use_lock); | 
|---|
| 57 | return port; | 
|---|
| 58 | } | 
|---|
| 59 | } | 
|---|
| 60 | return NULL;		/* not found */ | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | /* search for the next port - port is locked if found */ | 
|---|
| 65 | struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client, | 
|---|
| 66 | struct snd_seq_port_info *pinfo) | 
|---|
| 67 | { | 
|---|
| 68 | int num; | 
|---|
| 69 | struct snd_seq_client_port *port, *found; | 
|---|
| 70 | bool check_inactive = (pinfo->capability & SNDRV_SEQ_PORT_CAP_INACTIVE); | 
|---|
| 71 |  | 
|---|
| 72 | num = pinfo->addr.port; | 
|---|
| 73 | found = NULL; | 
|---|
| 74 | guard(read_lock)(l: &client->ports_lock); | 
|---|
| 75 | list_for_each_entry(port, &client->ports_list_head, list) { | 
|---|
| 76 | if ((port->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) && | 
|---|
| 77 | !check_inactive) | 
|---|
| 78 | continue; /* skip inactive ports */ | 
|---|
| 79 | if (port->addr.port < num) | 
|---|
| 80 | continue; | 
|---|
| 81 | if (port->addr.port == num) { | 
|---|
| 82 | found = port; | 
|---|
| 83 | break; | 
|---|
| 84 | } | 
|---|
| 85 | if (found == NULL || port->addr.port < found->addr.port) | 
|---|
| 86 | found = port; | 
|---|
| 87 | } | 
|---|
| 88 | if (found) { | 
|---|
| 89 | if (found->closing) | 
|---|
| 90 | found = NULL; | 
|---|
| 91 | else | 
|---|
| 92 | snd_use_lock_use(&found->use_lock); | 
|---|
| 93 | } | 
|---|
| 94 | return found; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | /* initialize snd_seq_port_subs_info */ | 
|---|
| 99 | static void port_subs_info_init(struct snd_seq_port_subs_info *grp) | 
|---|
| 100 | { | 
|---|
| 101 | INIT_LIST_HEAD(list: &grp->list_head); | 
|---|
| 102 | grp->count = 0; | 
|---|
| 103 | grp->exclusive = 0; | 
|---|
| 104 | rwlock_init(&grp->list_lock); | 
|---|
| 105 | init_rwsem(&grp->list_mutex); | 
|---|
| 106 | grp->open = NULL; | 
|---|
| 107 | grp->close = NULL; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | /* create a port, port number or a negative error code is returned | 
|---|
| 112 | * the caller needs to unref the port via snd_seq_port_unlock() appropriately | 
|---|
| 113 | */ | 
|---|
| 114 | int snd_seq_create_port(struct snd_seq_client *client, int port, | 
|---|
| 115 | struct snd_seq_client_port **port_ret) | 
|---|
| 116 | { | 
|---|
| 117 | struct snd_seq_client_port *new_port, *p; | 
|---|
| 118 | int num; | 
|---|
| 119 |  | 
|---|
| 120 | *port_ret = NULL; | 
|---|
| 121 |  | 
|---|
| 122 | /* sanity check */ | 
|---|
| 123 | if (snd_BUG_ON(!client)) | 
|---|
| 124 | return -EINVAL; | 
|---|
| 125 |  | 
|---|
| 126 | if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) { | 
|---|
| 127 | pr_warn( "ALSA: seq: too many ports for client %d\n", client->number); | 
|---|
| 128 | return -EINVAL; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /* create a new port */ | 
|---|
| 132 | new_port = kzalloc(sizeof(*new_port), GFP_KERNEL); | 
|---|
| 133 | if (!new_port) | 
|---|
| 134 | return -ENOMEM;	/* failure, out of memory */ | 
|---|
| 135 | /* init port data */ | 
|---|
| 136 | new_port->addr.client = client->number; | 
|---|
| 137 | new_port->addr.port = -1; | 
|---|
| 138 | new_port->owner = THIS_MODULE; | 
|---|
| 139 | snd_use_lock_init(&new_port->use_lock); | 
|---|
| 140 | port_subs_info_init(grp: &new_port->c_src); | 
|---|
| 141 | port_subs_info_init(grp: &new_port->c_dest); | 
|---|
| 142 | snd_use_lock_use(&new_port->use_lock); | 
|---|
| 143 |  | 
|---|
| 144 | num = max(port, 0); | 
|---|
| 145 | guard(mutex)(T: &client->ports_mutex); | 
|---|
| 146 | guard(write_lock_irq)(l: &client->ports_lock); | 
|---|
| 147 | list_for_each_entry(p, &client->ports_list_head, list) { | 
|---|
| 148 | if (p->addr.port == port) { | 
|---|
| 149 | kfree(objp: new_port); | 
|---|
| 150 | return -EBUSY; | 
|---|
| 151 | } | 
|---|
| 152 | if (p->addr.port > num) | 
|---|
| 153 | break; | 
|---|
| 154 | if (port < 0) /* auto-probe mode */ | 
|---|
| 155 | num = p->addr.port + 1; | 
|---|
| 156 | } | 
|---|
| 157 | /* insert the new port */ | 
|---|
| 158 | list_add_tail(new: &new_port->list, head: &p->list); | 
|---|
| 159 | client->num_ports++; | 
|---|
| 160 | new_port->addr.port = num;	/* store the port number in the port */ | 
|---|
| 161 | sprintf(buf: new_port->name, fmt: "port-%d", num); | 
|---|
| 162 | *port_ret = new_port; | 
|---|
| 163 |  | 
|---|
| 164 | return num; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | /* */ | 
|---|
| 168 | static int subscribe_port(struct snd_seq_client *client, | 
|---|
| 169 | struct snd_seq_client_port *port, | 
|---|
| 170 | struct snd_seq_port_subs_info *grp, | 
|---|
| 171 | struct snd_seq_port_subscribe *info, int send_ack); | 
|---|
| 172 | static int unsubscribe_port(struct snd_seq_client *client, | 
|---|
| 173 | struct snd_seq_client_port *port, | 
|---|
| 174 | struct snd_seq_port_subs_info *grp, | 
|---|
| 175 | struct snd_seq_port_subscribe *info, int send_ack); | 
|---|
| 176 |  | 
|---|
| 177 |  | 
|---|
| 178 | static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr, | 
|---|
| 179 | struct snd_seq_client **cp) | 
|---|
| 180 | { | 
|---|
| 181 | *cp = snd_seq_client_use_ptr(clientid: addr->client); | 
|---|
| 182 | if (!*cp) | 
|---|
| 183 | return NULL; | 
|---|
| 184 | return snd_seq_port_use_ptr(client: *cp, num: addr->port); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | static void delete_and_unsubscribe_port(struct snd_seq_client *client, | 
|---|
| 188 | struct snd_seq_client_port *port, | 
|---|
| 189 | struct snd_seq_subscribers *subs, | 
|---|
| 190 | bool is_src, bool ack); | 
|---|
| 191 |  | 
|---|
| 192 | static inline struct snd_seq_subscribers * | 
|---|
| 193 | get_subscriber(struct list_head *p, bool is_src) | 
|---|
| 194 | { | 
|---|
| 195 | if (is_src) | 
|---|
| 196 | return list_entry(p, struct snd_seq_subscribers, src_list); | 
|---|
| 197 | else | 
|---|
| 198 | return list_entry(p, struct snd_seq_subscribers, dest_list); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | /* | 
|---|
| 202 | * remove all subscribers on the list | 
|---|
| 203 | * this is called from port_delete, for each src and dest list. | 
|---|
| 204 | */ | 
|---|
| 205 | static void clear_subscriber_list(struct snd_seq_client *client, | 
|---|
| 206 | struct snd_seq_client_port *port, | 
|---|
| 207 | struct snd_seq_port_subs_info *grp, | 
|---|
| 208 | int is_src) | 
|---|
| 209 | { | 
|---|
| 210 | struct list_head *p, *n; | 
|---|
| 211 |  | 
|---|
| 212 | list_for_each_safe(p, n, &grp->list_head) { | 
|---|
| 213 | struct snd_seq_subscribers *subs; | 
|---|
| 214 | struct snd_seq_client *c __free(snd_seq_client) = NULL; | 
|---|
| 215 | struct snd_seq_client_port *aport __free(snd_seq_port) = NULL; | 
|---|
| 216 |  | 
|---|
| 217 | subs = get_subscriber(p, is_src); | 
|---|
| 218 | if (is_src) | 
|---|
| 219 | aport = get_client_port(addr: &subs->info.dest, cp: &c); | 
|---|
| 220 | else | 
|---|
| 221 | aport = get_client_port(addr: &subs->info.sender, cp: &c); | 
|---|
| 222 | delete_and_unsubscribe_port(client, port, subs, is_src, ack: false); | 
|---|
| 223 |  | 
|---|
| 224 | if (!aport) { | 
|---|
| 225 | /* looks like the connected port is being deleted. | 
|---|
| 226 | * we decrease the counter, and when both ports are deleted | 
|---|
| 227 | * remove the subscriber info | 
|---|
| 228 | */ | 
|---|
| 229 | if (atomic_dec_and_test(v: &subs->ref_count)) | 
|---|
| 230 | kfree(objp: subs); | 
|---|
| 231 | continue; | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | /* ok we got the connected port */ | 
|---|
| 235 | delete_and_unsubscribe_port(client: c, port: aport, subs, is_src: !is_src, ack: true); | 
|---|
| 236 | kfree(objp: subs); | 
|---|
| 237 | } | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | /* delete port data */ | 
|---|
| 241 | static int port_delete(struct snd_seq_client *client, | 
|---|
| 242 | struct snd_seq_client_port *port) | 
|---|
| 243 | { | 
|---|
| 244 | /* set closing flag and wait for all port access are gone */ | 
|---|
| 245 | port->closing = 1; | 
|---|
| 246 | snd_use_lock_sync(&port->use_lock); | 
|---|
| 247 |  | 
|---|
| 248 | /* clear subscribers info */ | 
|---|
| 249 | clear_subscriber_list(client, port, grp: &port->c_src, is_src: true); | 
|---|
| 250 | clear_subscriber_list(client, port, grp: &port->c_dest, is_src: false); | 
|---|
| 251 |  | 
|---|
| 252 | if (port->private_free) | 
|---|
| 253 | port->private_free(port->private_data); | 
|---|
| 254 |  | 
|---|
| 255 | snd_BUG_ON(port->c_src.count != 0); | 
|---|
| 256 | snd_BUG_ON(port->c_dest.count != 0); | 
|---|
| 257 |  | 
|---|
| 258 | kfree(objp: port); | 
|---|
| 259 | return 0; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 | /* delete a port with the given port id */ | 
|---|
| 264 | int snd_seq_delete_port(struct snd_seq_client *client, int port) | 
|---|
| 265 | { | 
|---|
| 266 | struct snd_seq_client_port *found = NULL, *p; | 
|---|
| 267 |  | 
|---|
| 268 | scoped_guard(mutex, &client->ports_mutex) { | 
|---|
| 269 | guard(write_lock_irq)(l: &client->ports_lock); | 
|---|
| 270 | list_for_each_entry(p, &client->ports_list_head, list) { | 
|---|
| 271 | if (p->addr.port == port) { | 
|---|
| 272 | /* ok found.  delete from the list at first */ | 
|---|
| 273 | list_del(entry: &p->list); | 
|---|
| 274 | client->num_ports--; | 
|---|
| 275 | found = p; | 
|---|
| 276 | break; | 
|---|
| 277 | } | 
|---|
| 278 | } | 
|---|
| 279 | } | 
|---|
| 280 | if (found) | 
|---|
| 281 | return port_delete(client, port: found); | 
|---|
| 282 | else | 
|---|
| 283 | return -ENOENT; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | /* delete the all ports belonging to the given client */ | 
|---|
| 287 | int snd_seq_delete_all_ports(struct snd_seq_client *client) | 
|---|
| 288 | { | 
|---|
| 289 | struct list_head deleted_list; | 
|---|
| 290 | struct snd_seq_client_port *port, *tmp; | 
|---|
| 291 |  | 
|---|
| 292 | /* move the port list to deleted_list, and | 
|---|
| 293 | * clear the port list in the client data. | 
|---|
| 294 | */ | 
|---|
| 295 | guard(mutex)(T: &client->ports_mutex); | 
|---|
| 296 | scoped_guard(write_lock_irq, &client->ports_lock) { | 
|---|
| 297 | if (!list_empty(head: &client->ports_list_head)) { | 
|---|
| 298 | list_add(new: &deleted_list, head: &client->ports_list_head); | 
|---|
| 299 | list_del_init(entry: &client->ports_list_head); | 
|---|
| 300 | } else { | 
|---|
| 301 | INIT_LIST_HEAD(list: &deleted_list); | 
|---|
| 302 | } | 
|---|
| 303 | client->num_ports = 0; | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | /* remove each port in deleted_list */ | 
|---|
| 307 | list_for_each_entry_safe(port, tmp, &deleted_list, list) { | 
|---|
| 308 | list_del(entry: &port->list); | 
|---|
| 309 | snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port); | 
|---|
| 310 | port_delete(client, port); | 
|---|
| 311 | } | 
|---|
| 312 | return 0; | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | /* set port info fields */ | 
|---|
| 316 | int snd_seq_set_port_info(struct snd_seq_client_port * port, | 
|---|
| 317 | struct snd_seq_port_info * info) | 
|---|
| 318 | { | 
|---|
| 319 | if (snd_BUG_ON(!port || !info)) | 
|---|
| 320 | return -EINVAL; | 
|---|
| 321 |  | 
|---|
| 322 | /* set port name */ | 
|---|
| 323 | if (info->name[0]) | 
|---|
| 324 | strscpy(port->name, info->name, sizeof(port->name)); | 
|---|
| 325 |  | 
|---|
| 326 | /* set capabilities */ | 
|---|
| 327 | port->capability = info->capability; | 
|---|
| 328 |  | 
|---|
| 329 | /* get port type */ | 
|---|
| 330 | port->type = info->type; | 
|---|
| 331 |  | 
|---|
| 332 | /* information about supported channels/voices */ | 
|---|
| 333 | port->midi_channels = info->midi_channels; | 
|---|
| 334 | port->midi_voices = info->midi_voices; | 
|---|
| 335 | port->synth_voices = info->synth_voices; | 
|---|
| 336 |  | 
|---|
| 337 | /* timestamping */ | 
|---|
| 338 | port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0; | 
|---|
| 339 | port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0; | 
|---|
| 340 | port->time_queue = info->time_queue; | 
|---|
| 341 |  | 
|---|
| 342 | /* UMP direction and group */ | 
|---|
| 343 | port->direction = info->direction; | 
|---|
| 344 | port->ump_group = info->ump_group; | 
|---|
| 345 | if (port->ump_group > SNDRV_UMP_MAX_GROUPS) | 
|---|
| 346 | port->ump_group = 0; | 
|---|
| 347 |  | 
|---|
| 348 | /* fill default port direction */ | 
|---|
| 349 | if (!port->direction) { | 
|---|
| 350 | if (info->capability & SNDRV_SEQ_PORT_CAP_READ) | 
|---|
| 351 | port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; | 
|---|
| 352 | if (info->capability & SNDRV_SEQ_PORT_CAP_WRITE) | 
|---|
| 353 | port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | port->is_midi1 = !!(info->flags & SNDRV_SEQ_PORT_FLG_IS_MIDI1); | 
|---|
| 357 |  | 
|---|
| 358 | return 0; | 
|---|
| 359 | } | 
|---|
| 360 |  | 
|---|
| 361 | /* get port info fields */ | 
|---|
| 362 | int snd_seq_get_port_info(struct snd_seq_client_port * port, | 
|---|
| 363 | struct snd_seq_port_info * info) | 
|---|
| 364 | { | 
|---|
| 365 | if (snd_BUG_ON(!port || !info)) | 
|---|
| 366 | return -EINVAL; | 
|---|
| 367 |  | 
|---|
| 368 | /* get port name */ | 
|---|
| 369 | strscpy(info->name, port->name, sizeof(info->name)); | 
|---|
| 370 |  | 
|---|
| 371 | /* get capabilities */ | 
|---|
| 372 | info->capability = port->capability; | 
|---|
| 373 |  | 
|---|
| 374 | /* get port type */ | 
|---|
| 375 | info->type = port->type; | 
|---|
| 376 |  | 
|---|
| 377 | /* information about supported channels/voices */ | 
|---|
| 378 | info->midi_channels = port->midi_channels; | 
|---|
| 379 | info->midi_voices = port->midi_voices; | 
|---|
| 380 | info->synth_voices = port->synth_voices; | 
|---|
| 381 |  | 
|---|
| 382 | /* get subscriber counts */ | 
|---|
| 383 | info->read_use = port->c_src.count; | 
|---|
| 384 | info->write_use = port->c_dest.count; | 
|---|
| 385 |  | 
|---|
| 386 | /* timestamping */ | 
|---|
| 387 | info->flags = 0; | 
|---|
| 388 | if (port->timestamping) { | 
|---|
| 389 | info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP; | 
|---|
| 390 | if (port->time_real) | 
|---|
| 391 | info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL; | 
|---|
| 392 | info->time_queue = port->time_queue; | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | if (port->is_midi1) | 
|---|
| 396 | info->flags |= SNDRV_SEQ_PORT_FLG_IS_MIDI1; | 
|---|
| 397 |  | 
|---|
| 398 | /* UMP direction and group */ | 
|---|
| 399 | info->direction = port->direction; | 
|---|
| 400 | info->ump_group = port->ump_group; | 
|---|
| 401 |  | 
|---|
| 402 | return 0; | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 |  | 
|---|
| 406 |  | 
|---|
| 407 | /* | 
|---|
| 408 | * call callback functions (if any): | 
|---|
| 409 | * the callbacks are invoked only when the first (for connection) or | 
|---|
| 410 | * the last subscription (for disconnection) is done.  Second or later | 
|---|
| 411 | * subscription results in increment of counter, but no callback is | 
|---|
| 412 | * invoked. | 
|---|
| 413 | * This feature is useful if these callbacks are associated with | 
|---|
| 414 | * initialization or termination of devices (see seq_midi.c). | 
|---|
| 415 | */ | 
|---|
| 416 |  | 
|---|
| 417 | static int subscribe_port(struct snd_seq_client *client, | 
|---|
| 418 | struct snd_seq_client_port *port, | 
|---|
| 419 | struct snd_seq_port_subs_info *grp, | 
|---|
| 420 | struct snd_seq_port_subscribe *info, | 
|---|
| 421 | int send_ack) | 
|---|
| 422 | { | 
|---|
| 423 | int err = 0; | 
|---|
| 424 |  | 
|---|
| 425 | if (!try_module_get(module: port->owner)) | 
|---|
| 426 | return -EFAULT; | 
|---|
| 427 | grp->count++; | 
|---|
| 428 | if (grp->open && grp->count == 1) { | 
|---|
| 429 | err = grp->open(port->private_data, info); | 
|---|
| 430 | if (err < 0) { | 
|---|
| 431 | module_put(module: port->owner); | 
|---|
| 432 | grp->count--; | 
|---|
| 433 | } | 
|---|
| 434 | } | 
|---|
| 435 | if (err >= 0 && send_ack && client->type == USER_CLIENT) | 
|---|
| 436 | snd_seq_client_notify_subscription(client: port->addr.client, port: port->addr.port, | 
|---|
| 437 | info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); | 
|---|
| 438 |  | 
|---|
| 439 | return err; | 
|---|
| 440 | } | 
|---|
| 441 |  | 
|---|
| 442 | static int unsubscribe_port(struct snd_seq_client *client, | 
|---|
| 443 | struct snd_seq_client_port *port, | 
|---|
| 444 | struct snd_seq_port_subs_info *grp, | 
|---|
| 445 | struct snd_seq_port_subscribe *info, | 
|---|
| 446 | int send_ack) | 
|---|
| 447 | { | 
|---|
| 448 | int err = 0; | 
|---|
| 449 |  | 
|---|
| 450 | if (! grp->count) | 
|---|
| 451 | return -EINVAL; | 
|---|
| 452 | grp->count--; | 
|---|
| 453 | if (grp->close && grp->count == 0) | 
|---|
| 454 | err = grp->close(port->private_data, info); | 
|---|
| 455 | if (send_ack && client->type == USER_CLIENT) | 
|---|
| 456 | snd_seq_client_notify_subscription(client: port->addr.client, port: port->addr.port, | 
|---|
| 457 | info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); | 
|---|
| 458 | module_put(module: port->owner); | 
|---|
| 459 | return err; | 
|---|
| 460 | } | 
|---|
| 461 |  | 
|---|
| 462 |  | 
|---|
| 463 |  | 
|---|
| 464 | /* check if both addresses are identical */ | 
|---|
| 465 | static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s) | 
|---|
| 466 | { | 
|---|
| 467 | return (r->client == s->client) && (r->port == s->port); | 
|---|
| 468 | } | 
|---|
| 469 |  | 
|---|
| 470 | /* check the two subscribe info match */ | 
|---|
| 471 | /* if flags is zero, checks only sender and destination addresses */ | 
|---|
| 472 | static int match_subs_info(struct snd_seq_port_subscribe *r, | 
|---|
| 473 | struct snd_seq_port_subscribe *s) | 
|---|
| 474 | { | 
|---|
| 475 | if (addr_match(r: &r->sender, s: &s->sender) && | 
|---|
| 476 | addr_match(r: &r->dest, s: &s->dest)) { | 
|---|
| 477 | if (r->flags && r->flags == s->flags) | 
|---|
| 478 | return r->queue == s->queue; | 
|---|
| 479 | else if (! r->flags) | 
|---|
| 480 | return 1; | 
|---|
| 481 | } | 
|---|
| 482 | return 0; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | static int check_and_subscribe_port(struct snd_seq_client *client, | 
|---|
| 486 | struct snd_seq_client_port *port, | 
|---|
| 487 | struct snd_seq_subscribers *subs, | 
|---|
| 488 | bool is_src, bool exclusive, bool ack) | 
|---|
| 489 | { | 
|---|
| 490 | struct snd_seq_port_subs_info *grp; | 
|---|
| 491 | struct list_head *p; | 
|---|
| 492 | struct snd_seq_subscribers *s; | 
|---|
| 493 | int err; | 
|---|
| 494 |  | 
|---|
| 495 | grp = is_src ? &port->c_src : &port->c_dest; | 
|---|
| 496 | guard(rwsem_write)(T: &grp->list_mutex); | 
|---|
| 497 | if (exclusive) { | 
|---|
| 498 | if (!list_empty(head: &grp->list_head)) | 
|---|
| 499 | return -EBUSY; | 
|---|
| 500 | } else { | 
|---|
| 501 | if (grp->exclusive) | 
|---|
| 502 | return -EBUSY; | 
|---|
| 503 | /* check whether already exists */ | 
|---|
| 504 | list_for_each(p, &grp->list_head) { | 
|---|
| 505 | s = get_subscriber(p, is_src); | 
|---|
| 506 | if (match_subs_info(r: &subs->info, s: &s->info)) | 
|---|
| 507 | return -EBUSY; | 
|---|
| 508 | } | 
|---|
| 509 | } | 
|---|
| 510 |  | 
|---|
| 511 | err = subscribe_port(client, port, grp, info: &subs->info, send_ack: ack); | 
|---|
| 512 | if (err < 0) { | 
|---|
| 513 | grp->exclusive = 0; | 
|---|
| 514 | return err; | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 | /* add to list */ | 
|---|
| 518 | guard(write_lock_irq)(l: &grp->list_lock); | 
|---|
| 519 | if (is_src) | 
|---|
| 520 | list_add_tail(new: &subs->src_list, head: &grp->list_head); | 
|---|
| 521 | else | 
|---|
| 522 | list_add_tail(new: &subs->dest_list, head: &grp->list_head); | 
|---|
| 523 | grp->exclusive = exclusive; | 
|---|
| 524 | atomic_inc(v: &subs->ref_count); | 
|---|
| 525 |  | 
|---|
| 526 | return 0; | 
|---|
| 527 | } | 
|---|
| 528 |  | 
|---|
| 529 | /* called with grp->list_mutex held */ | 
|---|
| 530 | static void __delete_and_unsubscribe_port(struct snd_seq_client *client, | 
|---|
| 531 | struct snd_seq_client_port *port, | 
|---|
| 532 | struct snd_seq_subscribers *subs, | 
|---|
| 533 | bool is_src, bool ack) | 
|---|
| 534 | { | 
|---|
| 535 | struct snd_seq_port_subs_info *grp; | 
|---|
| 536 | struct list_head *list; | 
|---|
| 537 | bool empty; | 
|---|
| 538 |  | 
|---|
| 539 | grp = is_src ? &port->c_src : &port->c_dest; | 
|---|
| 540 | list = is_src ? &subs->src_list : &subs->dest_list; | 
|---|
| 541 | scoped_guard(write_lock_irq, &grp->list_lock) { | 
|---|
| 542 | empty = list_empty(head: list); | 
|---|
| 543 | if (!empty) | 
|---|
| 544 | list_del_init(entry: list); | 
|---|
| 545 | grp->exclusive = 0; | 
|---|
| 546 | } | 
|---|
| 547 |  | 
|---|
| 548 | if (!empty) | 
|---|
| 549 | unsubscribe_port(client, port, grp, info: &subs->info, send_ack: ack); | 
|---|
| 550 | } | 
|---|
| 551 |  | 
|---|
| 552 | static void delete_and_unsubscribe_port(struct snd_seq_client *client, | 
|---|
| 553 | struct snd_seq_client_port *port, | 
|---|
| 554 | struct snd_seq_subscribers *subs, | 
|---|
| 555 | bool is_src, bool ack) | 
|---|
| 556 | { | 
|---|
| 557 | struct snd_seq_port_subs_info *grp; | 
|---|
| 558 |  | 
|---|
| 559 | grp = is_src ? &port->c_src : &port->c_dest; | 
|---|
| 560 | guard(rwsem_write)(T: &grp->list_mutex); | 
|---|
| 561 | __delete_and_unsubscribe_port(client, port, subs, is_src, ack); | 
|---|
| 562 | } | 
|---|
| 563 |  | 
|---|
| 564 | /* connect two ports */ | 
|---|
| 565 | int snd_seq_port_connect(struct snd_seq_client *connector, | 
|---|
| 566 | struct snd_seq_client *src_client, | 
|---|
| 567 | struct snd_seq_client_port *src_port, | 
|---|
| 568 | struct snd_seq_client *dest_client, | 
|---|
| 569 | struct snd_seq_client_port *dest_port, | 
|---|
| 570 | struct snd_seq_port_subscribe *info) | 
|---|
| 571 | { | 
|---|
| 572 | struct snd_seq_subscribers *subs; | 
|---|
| 573 | bool exclusive; | 
|---|
| 574 | int err; | 
|---|
| 575 |  | 
|---|
| 576 | subs = kzalloc(sizeof(*subs), GFP_KERNEL); | 
|---|
| 577 | if (!subs) | 
|---|
| 578 | return -ENOMEM; | 
|---|
| 579 |  | 
|---|
| 580 | subs->info = *info; | 
|---|
| 581 | atomic_set(v: &subs->ref_count, i: 0); | 
|---|
| 582 | INIT_LIST_HEAD(list: &subs->src_list); | 
|---|
| 583 | INIT_LIST_HEAD(list: &subs->dest_list); | 
|---|
| 584 |  | 
|---|
| 585 | exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE); | 
|---|
| 586 |  | 
|---|
| 587 | err = check_and_subscribe_port(client: src_client, port: src_port, subs, is_src: true, | 
|---|
| 588 | exclusive, | 
|---|
| 589 | ack: connector->number != src_client->number); | 
|---|
| 590 | if (err < 0) | 
|---|
| 591 | goto error; | 
|---|
| 592 | err = check_and_subscribe_port(client: dest_client, port: dest_port, subs, is_src: false, | 
|---|
| 593 | exclusive, | 
|---|
| 594 | ack: connector->number != dest_client->number); | 
|---|
| 595 | if (err < 0) | 
|---|
| 596 | goto error_dest; | 
|---|
| 597 |  | 
|---|
| 598 | return 0; | 
|---|
| 599 |  | 
|---|
| 600 | error_dest: | 
|---|
| 601 | delete_and_unsubscribe_port(client: src_client, port: src_port, subs, is_src: true, | 
|---|
| 602 | ack: connector->number != src_client->number); | 
|---|
| 603 | error: | 
|---|
| 604 | kfree(objp: subs); | 
|---|
| 605 | return err; | 
|---|
| 606 | } | 
|---|
| 607 |  | 
|---|
| 608 | /* remove the connection */ | 
|---|
| 609 | int snd_seq_port_disconnect(struct snd_seq_client *connector, | 
|---|
| 610 | struct snd_seq_client *src_client, | 
|---|
| 611 | struct snd_seq_client_port *src_port, | 
|---|
| 612 | struct snd_seq_client *dest_client, | 
|---|
| 613 | struct snd_seq_client_port *dest_port, | 
|---|
| 614 | struct snd_seq_port_subscribe *info) | 
|---|
| 615 | { | 
|---|
| 616 | struct snd_seq_port_subs_info *dest = &dest_port->c_dest; | 
|---|
| 617 | struct snd_seq_subscribers *subs; | 
|---|
| 618 | int err = -ENOENT; | 
|---|
| 619 |  | 
|---|
| 620 | /* always start from deleting the dest port for avoiding concurrent | 
|---|
| 621 | * deletions | 
|---|
| 622 | */ | 
|---|
| 623 | scoped_guard(rwsem_write, &dest->list_mutex) { | 
|---|
| 624 | /* look for the connection */ | 
|---|
| 625 | list_for_each_entry(subs, &dest->list_head, dest_list) { | 
|---|
| 626 | if (match_subs_info(r: info, s: &subs->info)) { | 
|---|
| 627 | __delete_and_unsubscribe_port(client: dest_client, port: dest_port, | 
|---|
| 628 | subs, is_src: false, | 
|---|
| 629 | ack: connector->number != dest_client->number); | 
|---|
| 630 | err = 0; | 
|---|
| 631 | break; | 
|---|
| 632 | } | 
|---|
| 633 | } | 
|---|
| 634 | } | 
|---|
| 635 | if (err < 0) | 
|---|
| 636 | return err; | 
|---|
| 637 |  | 
|---|
| 638 | delete_and_unsubscribe_port(client: src_client, port: src_port, subs, is_src: true, | 
|---|
| 639 | ack: connector->number != src_client->number); | 
|---|
| 640 | kfree(objp: subs); | 
|---|
| 641 | return 0; | 
|---|
| 642 | } | 
|---|
| 643 |  | 
|---|
| 644 |  | 
|---|
| 645 | /* get matched subscriber */ | 
|---|
| 646 | int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp, | 
|---|
| 647 | struct snd_seq_addr *dest_addr, | 
|---|
| 648 | struct snd_seq_port_subscribe *subs) | 
|---|
| 649 | { | 
|---|
| 650 | struct snd_seq_subscribers *s; | 
|---|
| 651 | int err = -ENOENT; | 
|---|
| 652 |  | 
|---|
| 653 | guard(rwsem_read)(T: &src_grp->list_mutex); | 
|---|
| 654 | list_for_each_entry(s, &src_grp->list_head, src_list) { | 
|---|
| 655 | if (addr_match(r: dest_addr, s: &s->info.dest)) { | 
|---|
| 656 | *subs = s->info; | 
|---|
| 657 | err = 0; | 
|---|
| 658 | break; | 
|---|
| 659 | } | 
|---|
| 660 | } | 
|---|
| 661 | return err; | 
|---|
| 662 | } | 
|---|
| 663 |  | 
|---|
| 664 | /* | 
|---|
| 665 | * Attach a device driver that wants to receive events from the | 
|---|
| 666 | * sequencer.  Returns the new port number on success. | 
|---|
| 667 | * A driver that wants to receive the events converted to midi, will | 
|---|
| 668 | * use snd_seq_midisynth_register_port(). | 
|---|
| 669 | */ | 
|---|
| 670 | /* exported */ | 
|---|
| 671 | int snd_seq_event_port_attach(int client, | 
|---|
| 672 | struct snd_seq_port_callback *pcbp, | 
|---|
| 673 | int cap, int type, int midi_channels, | 
|---|
| 674 | int midi_voices, char *portname) | 
|---|
| 675 | { | 
|---|
| 676 | struct snd_seq_port_info portinfo; | 
|---|
| 677 | int  ret; | 
|---|
| 678 |  | 
|---|
| 679 | /* Set up the port */ | 
|---|
| 680 | memset(s: &portinfo, c: 0, n: sizeof(portinfo)); | 
|---|
| 681 | portinfo.addr.client = client; | 
|---|
| 682 | strscpy(portinfo.name, portname ? portname : "Unnamed port", | 
|---|
| 683 | sizeof(portinfo.name)); | 
|---|
| 684 |  | 
|---|
| 685 | portinfo.capability = cap; | 
|---|
| 686 | portinfo.type = type; | 
|---|
| 687 | portinfo.kernel = pcbp; | 
|---|
| 688 | portinfo.midi_channels = midi_channels; | 
|---|
| 689 | portinfo.midi_voices = midi_voices; | 
|---|
| 690 |  | 
|---|
| 691 | /* Create it */ | 
|---|
| 692 | ret = snd_seq_kernel_client_ctl(client, | 
|---|
| 693 | SNDRV_SEQ_IOCTL_CREATE_PORT, | 
|---|
| 694 | arg: &portinfo); | 
|---|
| 695 |  | 
|---|
| 696 | if (ret >= 0) | 
|---|
| 697 | ret = portinfo.addr.port; | 
|---|
| 698 |  | 
|---|
| 699 | return ret; | 
|---|
| 700 | } | 
|---|
| 701 | EXPORT_SYMBOL(snd_seq_event_port_attach); | 
|---|
| 702 |  | 
|---|
| 703 | /* | 
|---|
| 704 | * Detach the driver from a port. | 
|---|
| 705 | */ | 
|---|
| 706 | /* exported */ | 
|---|
| 707 | int snd_seq_event_port_detach(int client, int port) | 
|---|
| 708 | { | 
|---|
| 709 | struct snd_seq_port_info portinfo; | 
|---|
| 710 | int  err; | 
|---|
| 711 |  | 
|---|
| 712 | memset(s: &portinfo, c: 0, n: sizeof(portinfo)); | 
|---|
| 713 | portinfo.addr.client = client; | 
|---|
| 714 | portinfo.addr.port   = port; | 
|---|
| 715 | err = snd_seq_kernel_client_ctl(client, | 
|---|
| 716 | SNDRV_SEQ_IOCTL_DELETE_PORT, | 
|---|
| 717 | arg: &portinfo); | 
|---|
| 718 |  | 
|---|
| 719 | return err; | 
|---|
| 720 | } | 
|---|
| 721 | EXPORT_SYMBOL(snd_seq_event_port_detach); | 
|---|
| 722 |  | 
|---|