seq_ports.c 18 KB

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