seq_ports.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. down_write(&grp->list_mutex);
  194. list_for_each_safe(p, n, &grp->list_head) {
  195. struct snd_seq_subscribers *subs;
  196. struct snd_seq_client *c;
  197. struct snd_seq_client_port *aport;
  198. if (grptype == SRC_LIST) {
  199. subs = list_entry(p, struct snd_seq_subscribers, src_list);
  200. aport = get_client_port(&subs->info.dest, &c);
  201. } else {
  202. subs = list_entry(p, struct snd_seq_subscribers, dest_list);
  203. aport = get_client_port(&subs->info.sender, &c);
  204. }
  205. list_del(p);
  206. unsubscribe_port(client, port, grp, &subs->info, 0);
  207. if (!aport) {
  208. /* looks like the connected port is being deleted.
  209. * we decrease the counter, and when both ports are deleted
  210. * remove the subscriber info
  211. */
  212. if (atomic_dec_and_test(&subs->ref_count))
  213. kfree(subs);
  214. } else {
  215. /* ok we got the connected port */
  216. struct snd_seq_port_subs_info *agrp;
  217. agrp = (grptype == SRC_LIST) ? &aport->c_dest : &aport->c_src;
  218. down_write(&agrp->list_mutex);
  219. if (grptype == SRC_LIST)
  220. list_del(&subs->dest_list);
  221. else
  222. list_del(&subs->src_list);
  223. unsubscribe_port(c, aport, agrp, &subs->info, 1);
  224. kfree(subs);
  225. up_write(&agrp->list_mutex);
  226. snd_seq_port_unlock(aport);
  227. snd_seq_client_unlock(c);
  228. }
  229. }
  230. up_write(&grp->list_mutex);
  231. }
  232. /* delete port data */
  233. static int port_delete(struct snd_seq_client *client,
  234. struct snd_seq_client_port *port)
  235. {
  236. /* set closing flag and wait for all port access are gone */
  237. port->closing = 1;
  238. snd_use_lock_sync(&port->use_lock);
  239. /* clear subscribers info */
  240. clear_subscriber_list(client, port, &port->c_src, SRC_LIST);
  241. clear_subscriber_list(client, port, &port->c_dest, DEST_LIST);
  242. if (port->private_free)
  243. port->private_free(port->private_data);
  244. snd_assert(port->c_src.count == 0,);
  245. snd_assert(port->c_dest.count == 0,);
  246. kfree(port);
  247. return 0;
  248. }
  249. /* delete a port with the given port id */
  250. int snd_seq_delete_port(struct snd_seq_client *client, int port)
  251. {
  252. unsigned long flags;
  253. struct list_head *l;
  254. struct snd_seq_client_port *found = NULL;
  255. mutex_lock(&client->ports_mutex);
  256. write_lock_irqsave(&client->ports_lock, flags);
  257. list_for_each(l, &client->ports_list_head) {
  258. struct snd_seq_client_port *p = list_entry(l, struct snd_seq_client_port, list);
  259. if (p->addr.port == port) {
  260. /* ok found. delete from the list at first */
  261. list_del(l);
  262. client->num_ports--;
  263. found = p;
  264. break;
  265. }
  266. }
  267. write_unlock_irqrestore(&client->ports_lock, flags);
  268. mutex_unlock(&client->ports_mutex);
  269. if (found)
  270. return port_delete(client, found);
  271. else
  272. return -ENOENT;
  273. }
  274. /* delete the all ports belonging to the given client */
  275. int snd_seq_delete_all_ports(struct snd_seq_client *client)
  276. {
  277. unsigned long flags;
  278. struct list_head deleted_list, *p, *n;
  279. /* move the port list to deleted_list, and
  280. * clear the port list in the client data.
  281. */
  282. mutex_lock(&client->ports_mutex);
  283. write_lock_irqsave(&client->ports_lock, flags);
  284. if (! list_empty(&client->ports_list_head)) {
  285. __list_add(&deleted_list,
  286. client->ports_list_head.prev,
  287. client->ports_list_head.next);
  288. INIT_LIST_HEAD(&client->ports_list_head);
  289. } else {
  290. INIT_LIST_HEAD(&deleted_list);
  291. }
  292. client->num_ports = 0;
  293. write_unlock_irqrestore(&client->ports_lock, flags);
  294. /* remove each port in deleted_list */
  295. list_for_each_safe(p, n, &deleted_list) {
  296. struct snd_seq_client_port *port = list_entry(p, struct snd_seq_client_port, list);
  297. list_del(p);
  298. snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
  299. port_delete(client, port);
  300. }
  301. mutex_unlock(&client->ports_mutex);
  302. return 0;
  303. }
  304. /* set port info fields */
  305. int snd_seq_set_port_info(struct snd_seq_client_port * port,
  306. struct snd_seq_port_info * info)
  307. {
  308. snd_assert(port && info, return -EINVAL);
  309. /* set port name */
  310. if (info->name[0])
  311. strlcpy(port->name, info->name, sizeof(port->name));
  312. /* set capabilities */
  313. port->capability = info->capability;
  314. /* get port type */
  315. port->type = info->type;
  316. /* information about supported channels/voices */
  317. port->midi_channels = info->midi_channels;
  318. port->midi_voices = info->midi_voices;
  319. port->synth_voices = info->synth_voices;
  320. /* timestamping */
  321. port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0;
  322. port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0;
  323. port->time_queue = info->time_queue;
  324. return 0;
  325. }
  326. /* get port info fields */
  327. int snd_seq_get_port_info(struct snd_seq_client_port * port,
  328. struct snd_seq_port_info * info)
  329. {
  330. snd_assert(port && info, return -EINVAL);
  331. /* get port name */
  332. strlcpy(info->name, port->name, sizeof(info->name));
  333. /* get capabilities */
  334. info->capability = port->capability;
  335. /* get port type */
  336. info->type = port->type;
  337. /* information about supported channels/voices */
  338. info->midi_channels = port->midi_channels;
  339. info->midi_voices = port->midi_voices;
  340. info->synth_voices = port->synth_voices;
  341. /* get subscriber counts */
  342. info->read_use = port->c_src.count;
  343. info->write_use = port->c_dest.count;
  344. /* timestamping */
  345. info->flags = 0;
  346. if (port->timestamping) {
  347. info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP;
  348. if (port->time_real)
  349. info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL;
  350. info->time_queue = port->time_queue;
  351. }
  352. return 0;
  353. }
  354. /*
  355. * call callback functions (if any):
  356. * the callbacks are invoked only when the first (for connection) or
  357. * the last subscription (for disconnection) is done. Second or later
  358. * subscription results in increment of counter, but no callback is
  359. * invoked.
  360. * This feature is useful if these callbacks are associated with
  361. * initialization or termination of devices (see seq_midi.c).
  362. *
  363. * If callback_all option is set, the callback function is invoked
  364. * at each connnection/disconnection.
  365. */
  366. static int subscribe_port(struct snd_seq_client *client,
  367. struct snd_seq_client_port *port,
  368. struct snd_seq_port_subs_info *grp,
  369. struct snd_seq_port_subscribe *info,
  370. int send_ack)
  371. {
  372. int err = 0;
  373. if (!try_module_get(port->owner))
  374. return -EFAULT;
  375. grp->count++;
  376. if (grp->open && (port->callback_all || grp->count == 1)) {
  377. err = grp->open(port->private_data, info);
  378. if (err < 0) {
  379. module_put(port->owner);
  380. grp->count--;
  381. }
  382. }
  383. if (err >= 0 && send_ack && client->type == USER_CLIENT)
  384. snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
  385. info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
  386. return err;
  387. }
  388. static int unsubscribe_port(struct snd_seq_client *client,
  389. struct snd_seq_client_port *port,
  390. struct snd_seq_port_subs_info *grp,
  391. struct snd_seq_port_subscribe *info,
  392. int send_ack)
  393. {
  394. int err = 0;
  395. if (! grp->count)
  396. return -EINVAL;
  397. grp->count--;
  398. if (grp->close && (port->callback_all || grp->count == 0))
  399. err = grp->close(port->private_data, info);
  400. if (send_ack && client->type == USER_CLIENT)
  401. snd_seq_client_notify_subscription(port->addr.client, port->addr.port,
  402. info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
  403. module_put(port->owner);
  404. return err;
  405. }
  406. /* check if both addresses are identical */
  407. static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s)
  408. {
  409. return (r->client == s->client) && (r->port == s->port);
  410. }
  411. /* check the two subscribe info match */
  412. /* if flags is zero, checks only sender and destination addresses */
  413. static int match_subs_info(struct snd_seq_port_subscribe *r,
  414. struct snd_seq_port_subscribe *s)
  415. {
  416. if (addr_match(&r->sender, &s->sender) &&
  417. addr_match(&r->dest, &s->dest)) {
  418. if (r->flags && r->flags == s->flags)
  419. return r->queue == s->queue;
  420. else if (! r->flags)
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. /* connect two ports */
  426. int snd_seq_port_connect(struct snd_seq_client *connector,
  427. struct snd_seq_client *src_client,
  428. struct snd_seq_client_port *src_port,
  429. struct snd_seq_client *dest_client,
  430. struct snd_seq_client_port *dest_port,
  431. struct snd_seq_port_subscribe *info)
  432. {
  433. struct snd_seq_port_subs_info *src = &src_port->c_src;
  434. struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
  435. struct snd_seq_subscribers *subs;
  436. struct list_head *p;
  437. int err, src_called = 0;
  438. unsigned long flags;
  439. int exclusive;
  440. subs = kzalloc(sizeof(*subs), GFP_KERNEL);
  441. if (! subs)
  442. return -ENOMEM;
  443. subs->info = *info;
  444. atomic_set(&subs->ref_count, 2);
  445. down_write(&src->list_mutex);
  446. down_write(&dest->list_mutex);
  447. exclusive = info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE ? 1 : 0;
  448. err = -EBUSY;
  449. if (exclusive) {
  450. if (! list_empty(&src->list_head) || ! list_empty(&dest->list_head))
  451. goto __error;
  452. } else {
  453. if (src->exclusive || dest->exclusive)
  454. goto __error;
  455. /* check whether already exists */
  456. list_for_each(p, &src->list_head) {
  457. struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, src_list);
  458. if (match_subs_info(info, &s->info))
  459. goto __error;
  460. }
  461. list_for_each(p, &dest->list_head) {
  462. struct snd_seq_subscribers *s = list_entry(p, struct snd_seq_subscribers, dest_list);
  463. if (match_subs_info(info, &s->info))
  464. goto __error;
  465. }
  466. }
  467. if ((err = subscribe_port(src_client, src_port, src, info,
  468. connector->number != src_client->number)) < 0)
  469. goto __error;
  470. src_called = 1;
  471. if ((err = subscribe_port(dest_client, dest_port, dest, info,
  472. connector->number != dest_client->number)) < 0)
  473. goto __error;
  474. /* add to list */
  475. write_lock_irqsave(&src->list_lock, flags);
  476. // write_lock(&dest->list_lock); // no other lock yet
  477. list_add_tail(&subs->src_list, &src->list_head);
  478. list_add_tail(&subs->dest_list, &dest->list_head);
  479. // write_unlock(&dest->list_lock); // no other lock yet
  480. write_unlock_irqrestore(&src->list_lock, flags);
  481. src->exclusive = dest->exclusive = exclusive;
  482. up_write(&dest->list_mutex);
  483. up_write(&src->list_mutex);
  484. return 0;
  485. __error:
  486. if (src_called)
  487. unsubscribe_port(src_client, src_port, src, info,
  488. connector->number != src_client->number);
  489. kfree(subs);
  490. up_write(&dest->list_mutex);
  491. up_write(&src->list_mutex);
  492. return err;
  493. }
  494. /* remove the connection */
  495. int snd_seq_port_disconnect(struct snd_seq_client *connector,
  496. struct snd_seq_client *src_client,
  497. struct snd_seq_client_port *src_port,
  498. struct snd_seq_client *dest_client,
  499. struct snd_seq_client_port *dest_port,
  500. struct snd_seq_port_subscribe *info)
  501. {
  502. struct snd_seq_port_subs_info *src = &src_port->c_src;
  503. struct snd_seq_port_subs_info *dest = &dest_port->c_dest;
  504. struct snd_seq_subscribers *subs;
  505. struct list_head *p;
  506. int err = -ENOENT;
  507. unsigned long flags;
  508. down_write(&src->list_mutex);
  509. down_write(&dest->list_mutex);
  510. /* look for the connection */
  511. list_for_each(p, &src->list_head) {
  512. subs = list_entry(p, struct snd_seq_subscribers, src_list);
  513. if (match_subs_info(info, &subs->info)) {
  514. write_lock_irqsave(&src->list_lock, flags);
  515. // write_lock(&dest->list_lock); // no lock yet
  516. list_del(&subs->src_list);
  517. list_del(&subs->dest_list);
  518. // write_unlock(&dest->list_lock);
  519. write_unlock_irqrestore(&src->list_lock, flags);
  520. src->exclusive = dest->exclusive = 0;
  521. unsubscribe_port(src_client, src_port, src, info,
  522. connector->number != src_client->number);
  523. unsubscribe_port(dest_client, dest_port, dest, info,
  524. connector->number != dest_client->number);
  525. kfree(subs);
  526. err = 0;
  527. break;
  528. }
  529. }
  530. up_write(&dest->list_mutex);
  531. up_write(&src->list_mutex);
  532. return err;
  533. }
  534. /* get matched subscriber */
  535. struct snd_seq_subscribers *snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
  536. struct snd_seq_addr *dest_addr)
  537. {
  538. struct list_head *p;
  539. struct snd_seq_subscribers *s, *found = NULL;
  540. down_read(&src_grp->list_mutex);
  541. list_for_each(p, &src_grp->list_head) {
  542. s = list_entry(p, struct snd_seq_subscribers, src_list);
  543. if (addr_match(dest_addr, &s->info.dest)) {
  544. found = s;
  545. break;
  546. }
  547. }
  548. up_read(&src_grp->list_mutex);
  549. return found;
  550. }
  551. /*
  552. * Attach a device driver that wants to receive events from the
  553. * sequencer. Returns the new port number on success.
  554. * A driver that wants to receive the events converted to midi, will
  555. * use snd_seq_midisynth_register_port().
  556. */
  557. /* exported */
  558. int snd_seq_event_port_attach(int client,
  559. struct snd_seq_port_callback *pcbp,
  560. int cap, int type, int midi_channels,
  561. int midi_voices, char *portname)
  562. {
  563. struct snd_seq_port_info portinfo;
  564. int ret;
  565. /* Set up the port */
  566. memset(&portinfo, 0, sizeof(portinfo));
  567. portinfo.addr.client = client;
  568. strlcpy(portinfo.name, portname ? portname : "Unamed port",
  569. sizeof(portinfo.name));
  570. portinfo.capability = cap;
  571. portinfo.type = type;
  572. portinfo.kernel = pcbp;
  573. portinfo.midi_channels = midi_channels;
  574. portinfo.midi_voices = midi_voices;
  575. /* Create it */
  576. ret = snd_seq_kernel_client_ctl(client,
  577. SNDRV_SEQ_IOCTL_CREATE_PORT,
  578. &portinfo);
  579. if (ret >= 0)
  580. ret = portinfo.addr.port;
  581. return ret;
  582. }
  583. /*
  584. * Detach the driver from a port.
  585. */
  586. /* exported */
  587. int snd_seq_event_port_detach(int client, int port)
  588. {
  589. struct snd_seq_port_info portinfo;
  590. int err;
  591. memset(&portinfo, 0, sizeof(portinfo));
  592. portinfo.addr.client = client;
  593. portinfo.addr.port = port;
  594. err = snd_seq_kernel_client_ctl(client,
  595. SNDRV_SEQ_IOCTL_DELETE_PORT,
  596. &portinfo);
  597. return err;
  598. }