seq_ports.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * ALSA sequencer Ports
  3. * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. * Jaroslav Kysela <perex@perex.cz>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/core.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include "seq_system.h"
  26. #include "seq_ports.h"
  27. #include "seq_clientmgr.h"
  28. /*
  29. registration of client ports
  30. */
  31. /*
  32. NOTE: the current implementation of the port structure as a linked list is
  33. not optimal for clients that have many ports. For sending messages to all
  34. subscribers of a port we first need to find the address of the port
  35. structure, which means we have to traverse the list. A direct access table
  36. (array) would be better, but big preallocated arrays waste memory.
  37. Possible actions:
  38. 1) leave it this way, a client does normaly does not have more than a few
  39. ports
  40. 2) replace the linked list of ports by a array of pointers which is
  41. dynamicly kmalloced. When a port is added or deleted we can simply allocate
  42. a new array, copy the corresponding pointers, and delete the old one. We
  43. then only need a pointer to this array, and an integer that tells us how
  44. much elements are in array.
  45. */
  46. /* return pointer to port structure - port is locked if found */
  47. struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client,
  48. int num)
  49. {
  50. struct snd_seq_client_port *port;
  51. if (client == NULL)
  52. return NULL;
  53. read_lock(&client->ports_lock);
  54. list_for_each_entry(port, &client->ports_list_head, list) {
  55. if (port->addr.port == num) {
  56. if (port->closing)
  57. break; /* deleting now */
  58. snd_use_lock_use(&port->use_lock);
  59. read_unlock(&client->ports_lock);
  60. return port;
  61. }
  62. }
  63. read_unlock(&client->ports_lock);
  64. return NULL; /* not found */
  65. }
  66. /* search for the next port - port is locked if found */
  67. struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client,
  68. struct snd_seq_port_info *pinfo)
  69. {
  70. int num;
  71. struct snd_seq_client_port *port, *found;
  72. num = pinfo->addr.port;
  73. found = NULL;
  74. read_lock(&client->ports_lock);
  75. list_for_each_entry(port, &client->ports_list_head, list) {
  76. if (port->addr.port < num)
  77. continue;
  78. if (port->addr.port == num) {
  79. found = port;
  80. break;
  81. }
  82. if (found == NULL || port->addr.port < found->addr.port)
  83. found = port;
  84. }
  85. if (found) {
  86. if (found->closing)
  87. found = NULL;
  88. else
  89. snd_use_lock_use(&found->use_lock);
  90. }
  91. read_unlock(&client->ports_lock);
  92. return found;
  93. }
  94. /* initialize snd_seq_port_subs_info */
  95. static void port_subs_info_init(struct snd_seq_port_subs_info *grp)
  96. {
  97. INIT_LIST_HEAD(&grp->list_head);
  98. grp->count = 0;
  99. grp->exclusive = 0;
  100. rwlock_init(&grp->list_lock);
  101. init_rwsem(&grp->list_mutex);
  102. grp->open = NULL;
  103. grp->close = NULL;
  104. }
  105. /* create a port, port number is returned (-1 on failure);
  106. * the caller needs to unref the port via snd_seq_port_unlock() appropriately
  107. */
  108. struct snd_seq_client_port *snd_seq_create_port(struct snd_seq_client *client,
  109. int port)
  110. {
  111. unsigned long flags;
  112. struct snd_seq_client_port *new_port, *p;
  113. int num = -1;
  114. /* sanity check */
  115. if (snd_BUG_ON(!client))
  116. return NULL;
  117. if (client->num_ports >= SNDRV_SEQ_MAX_PORTS - 1) {
  118. pr_warn("ALSA: seq: too many ports for client %d\n", client->number);
  119. return NULL;
  120. }
  121. /* create a new port */
  122. new_port = kzalloc(sizeof(*new_port), GFP_KERNEL);
  123. if (! new_port) {
  124. pr_debug("ALSA: seq: malloc failed for registering client port\n");
  125. return NULL; /* failure, out of memory */
  126. }
  127. /* init port data */
  128. new_port->addr.client = client->number;
  129. new_port->addr.port = -1;
  130. new_port->owner = THIS_MODULE;
  131. sprintf(new_port->name, "port-%d", num);
  132. snd_use_lock_init(&new_port->use_lock);
  133. port_subs_info_init(&new_port->c_src);
  134. port_subs_info_init(&new_port->c_dest);
  135. snd_use_lock_use(&new_port->use_lock);
  136. num = port >= 0 ? port : 0;
  137. mutex_lock(&client->ports_mutex);
  138. write_lock_irqsave(&client->ports_lock, flags);
  139. list_for_each_entry(p, &client->ports_list_head, list) {
  140. if (p->addr.port > num)
  141. break;
  142. if (port < 0) /* auto-probe mode */
  143. num = p->addr.port + 1;
  144. }
  145. /* insert the new port */
  146. list_add_tail(&new_port->list, &p->list);
  147. client->num_ports++;
  148. new_port->addr.port = num; /* store the port number in the port */
  149. sprintf(new_port->name, "port-%d", num);
  150. write_unlock_irqrestore(&client->ports_lock, flags);
  151. mutex_unlock(&client->ports_mutex);
  152. return new_port;
  153. }
  154. /* */
  155. enum group_type {
  156. SRC_LIST, DEST_LIST
  157. };
  158. static int subscribe_port(struct snd_seq_client *client,
  159. struct snd_seq_client_port *port,
  160. struct snd_seq_port_subs_info *grp,
  161. struct snd_seq_port_subscribe *info, int send_ack);
  162. static int unsubscribe_port(struct snd_seq_client *client,
  163. struct snd_seq_client_port *port,
  164. struct snd_seq_port_subs_info *grp,
  165. struct snd_seq_port_subscribe *info, int send_ack);
  166. static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr,
  167. struct snd_seq_client **cp)
  168. {
  169. struct snd_seq_client_port *p;
  170. *cp = snd_seq_client_use_ptr(addr->client);
  171. if (*cp) {
  172. p = snd_seq_port_use_ptr(*cp, addr->port);
  173. if (! p) {
  174. snd_seq_client_unlock(*cp);
  175. *cp = NULL;
  176. }
  177. return p;
  178. }
  179. return NULL;
  180. }
  181. /*
  182. * remove all subscribers on the list
  183. * this is called from port_delete, for each src and dest list.
  184. */
  185. static void clear_subscriber_list(struct snd_seq_client *client,
  186. struct snd_seq_client_port *port,
  187. struct snd_seq_port_subs_info *grp,
  188. int grptype)
  189. {
  190. struct list_head *p, *n;
  191. list_for_each_safe(p, n, &grp->list_head) {
  192. struct snd_seq_subscribers *subs;
  193. struct snd_seq_client *c;
  194. struct snd_seq_client_port *aport;
  195. if (grptype == SRC_LIST) {
  196. subs = list_entry(p, struct snd_seq_subscribers, src_list);
  197. aport = get_client_port(&subs->info.dest, &c);
  198. } else {
  199. subs = list_entry(p, struct snd_seq_subscribers, dest_list);
  200. aport = get_client_port(&subs->info.sender, &c);
  201. }
  202. list_del(p);
  203. unsubscribe_port(client, port, grp, &subs->info, 0);
  204. if (!aport) {
  205. /* looks like the connected port is being deleted.
  206. * we decrease the counter, and when both ports are deleted
  207. * remove the subscriber info
  208. */
  209. if (atomic_dec_and_test(&subs->ref_count))
  210. kfree(subs);
  211. } else {
  212. /* ok we got the connected port */
  213. struct snd_seq_port_subs_info *agrp;
  214. agrp = (grptype == SRC_LIST) ? &aport->c_dest : &aport->c_src;
  215. down_write(&agrp->list_mutex);
  216. if (grptype == SRC_LIST)
  217. list_del(&subs->dest_list);
  218. else
  219. list_del(&subs->src_list);
  220. up_write(&agrp->list_mutex);
  221. unsubscribe_port(c, aport, agrp, &subs->info, 1);
  222. kfree(subs);
  223. snd_seq_port_unlock(aport);
  224. snd_seq_client_unlock(c);
  225. }
  226. }
  227. }
  228. /* delete port data */
  229. static int port_delete(struct snd_seq_client *client,
  230. struct snd_seq_client_port *port)
  231. {
  232. /* set closing flag and wait for all port access are gone */
  233. port->closing = 1;
  234. snd_use_lock_sync(&port->use_lock);
  235. /* clear subscribers info */
  236. clear_subscriber_list(client, port, &port->c_src, SRC_LIST);
  237. clear_subscriber_list(client, port, &port->c_dest, DEST_LIST);
  238. if (port->private_free)
  239. port->private_free(port->private_data);
  240. snd_BUG_ON(port->c_src.count != 0);
  241. snd_BUG_ON(port->c_dest.count != 0);
  242. kfree(port);
  243. return 0;
  244. }
  245. /* delete a port with the given port id */
  246. int snd_seq_delete_port(struct snd_seq_client *client, int port)
  247. {
  248. unsigned long flags;
  249. struct snd_seq_client_port *found = NULL, *p;
  250. mutex_lock(&client->ports_mutex);
  251. write_lock_irqsave(&client->ports_lock, flags);
  252. list_for_each_entry(p, &client->ports_list_head, list) {
  253. if (p->addr.port == port) {
  254. /* ok found. delete from the list at first */
  255. list_del(&p->list);
  256. client->num_ports--;
  257. found = p;
  258. break;
  259. }
  260. }
  261. write_unlock_irqrestore(&client->ports_lock, flags);
  262. mutex_unlock(&client->ports_mutex);
  263. if (found)
  264. return port_delete(client, found);
  265. else
  266. return -ENOENT;
  267. }
  268. /* delete the all ports belonging to the given client */
  269. int snd_seq_delete_all_ports(struct snd_seq_client *client)
  270. {
  271. unsigned long flags;
  272. struct list_head deleted_list;
  273. struct snd_seq_client_port *port, *tmp;
  274. /* move the port list to deleted_list, and
  275. * clear the port list in the client data.
  276. */
  277. mutex_lock(&client->ports_mutex);
  278. write_lock_irqsave(&client->ports_lock, flags);
  279. if (! list_empty(&client->ports_list_head)) {
  280. list_add(&deleted_list, &client->ports_list_head);
  281. list_del_init(&client->ports_list_head);
  282. } else {
  283. INIT_LIST_HEAD(&deleted_list);
  284. }
  285. client->num_ports = 0;
  286. write_unlock_irqrestore(&client->ports_lock, flags);
  287. /* remove each port in deleted_list */
  288. list_for_each_entry_safe(port, tmp, &deleted_list, list) {
  289. list_del(&port->list);
  290. snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
  291. port_delete(client, port);
  292. }
  293. mutex_unlock(&client->ports_mutex);
  294. return 0;
  295. }
  296. /* set port info fields */
  297. int snd_seq_set_port_info(struct snd_seq_client_port * port,
  298. struct snd_seq_port_info * info)
  299. {
  300. if (snd_BUG_ON(!port || !info))
  301. return -EINVAL;
  302. /* set port name */
  303. if (info->name[0])
  304. strlcpy(port->name, info->name, sizeof(port->name));
  305. /* set capabilities */
  306. port->capability = info->capability;
  307. /* get port type */
  308. port->type = info->type;
  309. /* information about supported channels/voices */
  310. port->midi_channels = info->midi_channels;
  311. port->midi_voices = info->midi_voices;
  312. port->synth_voices = info->synth_voices;
  313. /* timestamping */
  314. port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
  315. port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
  316. port->time_queue = info->time_queue;
  317. return 0;
  318. }
  319. /* get port info fields */
  320. int snd_seq_get_port_info(struct snd_seq_client_port * port,
  321. struct snd_seq_port_info * info)
  322. {
  323. if (snd_BUG_ON(!port || !info))
  324. return -EINVAL;
  325. /* get port name */
  326. strlcpy(info->name, port->name, sizeof(info->name));
  327. /* get capabilities */
  328. info->capability = port->capability;
  329. /* get port type */
  330. info->type = port->type;
  331. /* information about supported channels/voices */
  332. info->midi_channels = port->midi_channels;
  333. info->midi_voices = port->midi_voices;
  334. info->synth_voices = port->synth_voices;
  335. /* get subscriber counts */
  336. info->read_use = port->c_src.count;
  337. info->write_use = port->c_dest.count;
  338. /* timestamping */
  339. info->flags = 0;
  340. if (port->timestamping) {
  341. info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
  342. if (port->time_real)
  343. info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
  344. info->time_queue = port->time_queue;
  345. }
  346. return 0;
  347. }
  348. /*
  349. * call callback functions (if any):
  350. * the callbacks are invoked only when the first (for connection) or
  351. * the last subscription (for disconnection) is done. Second or later
  352. * subscription results in increment of counter, but no callback is
  353. * invoked.
  354. * This feature is useful if these callbacks are associated with
  355. * initialization or termination of devices (see seq_midi.c).
  356. *
  357. * If callback_all option is set, the callback function is invoked
  358. * at each connection/disconnection.
  359. */
  360. static int subscribe_port(struct snd_seq_client *client,
  361. struct snd_seq_client_port *port,
  362. struct snd_seq_port_subs_info *grp,
  363. struct snd_seq_port_subscribe *info,
  364. int send_ack)
  365. {
  366. int err = 0;
  367. if (!try_module_get(port->owner))
  368. return -EFAULT;
  369. grp->count++;
  370. if (grp->open && (port->callback_all || grp->count == 1)) {
  371. err = grp->open(port->private_data, info);
  372. if (err < 0) {
  373. module_put(port->owner);
  374. grp->count--;
  375. }
  376. }
  377. if (err >= 0 && send_ack && client->type == USER_CLIENT)
  378. snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
  379. info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
  380. return err;
  381. }
  382. static int unsubscribe_port(struct snd_seq_client *client,
  383. struct snd_seq_client_port *port,
  384. struct snd_seq_port_subs_info *grp,
  385. struct snd_seq_port_subscribe *info,
  386. int send_ack)
  387. {
  388. int err = 0;
  389. if (! grp->count)
  390. return -EINVAL;
  391. grp->count--;
  392. if (grp->close && (port->callback_all || grp->count == 0))
  393. err = grp->close(port->private_data, info);
  394. if (send_ack && client->type == USER_CLIENT)
  395. snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
  396. info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
  397. module_put(port->owner);
  398. return err;
  399. }
  400. /* check if both addresses are identical */
  401. static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
  402. {
  403. return (r->client == s->client) && (r->port == s->port);
  404. }
  405. /* check the two subscribe info match */
  406. /* if flags is zero, checks only sender and destination addresses */
  407. static int match_subs_info(struct snd_seq_port_subscribe *r,
  408. struct snd_seq_port_subscribe *s)
  409. {
  410. if (addr_match(&r->sender, &s->sender) &&
  411. addr_match(&r->dest, &s->dest)) {
  412. if (r->flags && r->flags == s->flags)
  413. return r->queue == s->queue;
  414. else if (! r->flags)
  415. return 1;
  416. }
  417. return 0;
  418. }
  419. /* connect two ports */
  420. int snd_seq_port_connect(struct snd_seq_client *connector,
  421. struct snd_seq_client *src_client,
  422. struct snd_seq_client_port *src_port,
  423. struct snd_seq_client *dest_client,
  424. struct snd_seq_client_port *dest_port,
  425. struct snd_seq_port_subscribe *info)
  426. {
  427. struct snd_seq_port_subs_info *src = &src_port->c_src;
  428. struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
  429. struct snd_seq_subscribers *subs, *s;
  430. int err, src_called = 0;
  431. unsigned long flags;
  432. int exclusive;
  433. subs = kzalloc(sizeof(*subs), GFP_KERNEL);
  434. if (! subs)
  435. return -ENOMEM;
  436. subs->info = *info;
  437. atomic_set(&subs->ref_count, 2);
  438. down_write(&src->list_mutex);
  439. down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
  440. exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
  441. err = -EBUSY;
  442. if (exclusive) {
  443. if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
  444. goto __error;
  445. } else {
  446. if (src->exclusive || dest->exclusive)
  447. goto __error;
  448. /* check whether already exists */
  449. list_for_each_entry(s, &src->list_head, src_list) {
  450. if (match_subs_info(info, &s->info))
  451. goto __error;
  452. }
  453. list_for_each_entry(s, &dest->list_head, dest_list) {
  454. if (match_subs_info(info, &s->info))
  455. goto __error;
  456. }
  457. }
  458. if ((err = subscribe_port(src_client, src_port, src, info,
  459. connector->number != src_client->number)) < 0)
  460. goto __error;
  461. src_called = 1;
  462. if ((err = subscribe_port(dest_client, dest_port, dest, info,
  463. connector->number != dest_client->number)) < 0)
  464. goto __error;
  465. /* add to list */
  466. write_lock_irqsave(&src->list_lock, flags);
  467. // write_lock(&dest->list_lock); // no other lock yet
  468. list_add_tail(&subs->src_list, &src->list_head);
  469. list_add_tail(&subs->dest_list, &dest->list_head);
  470. // write_unlock(&dest->list_lock); // no other lock yet
  471. write_unlock_irqrestore(&src->list_lock, flags);
  472. src->exclusive = dest->exclusive = exclusive;
  473. up_write(&dest->list_mutex);
  474. up_write(&src->list_mutex);
  475. return 0;
  476. __error:
  477. if (src_called)
  478. unsubscribe_port(src_client, src_port, src, info,
  479. connector->number != src_client->number);
  480. kfree(subs);
  481. up_write(&dest->list_mutex);
  482. up_write(&src->list_mutex);
  483. return err;
  484. }
  485. /* remove the connection */
  486. int snd_seq_port_disconnect(struct snd_seq_client *connector,
  487. struct snd_seq_client *src_client,
  488. struct snd_seq_client_port *src_port,
  489. struct snd_seq_client *dest_client,
  490. struct snd_seq_client_port *dest_port,
  491. struct snd_seq_port_subscribe *info)
  492. {
  493. struct snd_seq_port_subs_info *src = &src_port->c_src;
  494. struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
  495. struct snd_seq_subscribers *subs;
  496. int err = -ENOENT;
  497. unsigned long flags;
  498. down_write(&src->list_mutex);
  499. down_write_nested(&dest->list_mutex, SINGLE_DEPTH_NESTING);
  500. /* look for the connection */
  501. list_for_each_entry(subs, &src->list_head, src_list) {
  502. if (match_subs_info(info, &subs->info)) {
  503. write_lock_irqsave(&src->list_lock, flags);
  504. // write_lock(&dest->list_lock); // no lock yet
  505. list_del(&subs->src_list);
  506. list_del(&subs->dest_list);
  507. // write_unlock(&dest->list_lock);
  508. write_unlock_irqrestore(&src->list_lock, flags);
  509. src->exclusive = dest->exclusive = 0;
  510. unsubscribe_port(src_client, src_port, src, info,
  511. connector->number != src_client->number);
  512. unsubscribe_port(dest_client, dest_port, dest, info,
  513. connector->number != dest_client->number);
  514. kfree(subs);
  515. err = 0;
  516. break;
  517. }
  518. }
  519. up_write(&dest->list_mutex);
  520. up_write(&src->list_mutex);
  521. return err;
  522. }
  523. /* get matched subscriber */
  524. struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
  525. struct snd_seq_addr *dest_addr)
  526. {
  527. struct snd_seq_subscribers *s, *found = NULL;
  528. down_read(&src_grp->list_mutex);
  529. list_for_each_entry(s, &src_grp->list_head, src_list) {
  530. if (addr_match(dest_addr, &s->info.dest)) {
  531. found = s;
  532. break;
  533. }
  534. }
  535. up_read(&src_grp->list_mutex);
  536. return found;
  537. }
  538. /*
  539. * Attach a device driver that wants to receive events from the
  540. * sequencer. Returns the new port number on success.
  541. * A driver that wants to receive the events converted to midi, will
  542. * use snd_seq_midisynth_register_port().
  543. */
  544. /* exported */
  545. int snd_seq_event_port_attach(int client,
  546. struct snd_seq_port_callback *pcbp,
  547. int cap, int type, int midi_channels,
  548. int midi_voices, char *portname)
  549. {
  550. struct snd_seq_port_info portinfo;
  551. int ret;
  552. /* Set up the port */
  553. memset(&portinfo, 0, sizeof(portinfo));
  554. portinfo.addr.client = client;
  555. strlcpy(portinfo.name, portname ? portname : "Unamed port",
  556. sizeof(portinfo.name));
  557. portinfo.capability = cap;
  558. portinfo.type = type;
  559. portinfo.kernel = pcbp;
  560. portinfo.midi_channels = midi_channels;
  561. portinfo.midi_voices = midi_voices;
  562. /* Create it */
  563. ret = snd_seq_kernel_client_ctl(client,
  564. SNDRV_SEQ_IOCTL_CREATE_PORT,
  565. &portinfo);
  566. if (ret >= 0)
  567. ret = portinfo.addr.port;
  568. return ret;
  569. }
  570. EXPORT_SYMBOL(snd_seq_event_port_attach);
  571. /*
  572. * Detach the driver from a port.
  573. */
  574. /* exported */
  575. int snd_seq_event_port_detach(int client, int port)
  576. {
  577. struct snd_seq_port_info portinfo;
  578. int err;
  579. memset(&portinfo, 0, sizeof(portinfo));
  580. portinfo.addr.client = client;
  581. portinfo.addr.port = port;
  582. err = snd_seq_kernel_client_ctl(client,
  583. SNDRV_SEQ_IOCTL_DELETE_PORT,
  584. &portinfo);
  585. return err;
  586. }
  587. EXPORT_SYMBOL(snd_seq_event_port_detach);