seq_ports.c 19 KB

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