1 /*
2  * Copyright (c) 2015 Carlos Pizano-Uribe  cpu@chromium.org
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 #include <lk/compiler.h>
11 #include <sys/types.h>
12 
13 __BEGIN_CDECLS
14 
15 /* Ports are named, opaque objects and come in three flavors, the
16  * write-side, the read-side and a port group which is a collection
17  * of read-side ports.
18  */
19 
20 #define PORT_NAME_LEN 12
21 
22 typedef void *port_t;
23 
24 /* A Port packet is wide enough to carry two full words of data */
25 #define PORT_PACKET_LEN (sizeof(void *) * 2)
26 typedef struct {
27     char value[PORT_PACKET_LEN];
28 } port_packet_t;
29 
30 typedef struct {
31     void *ctx;
32     port_packet_t packet;
33 } port_result_t;
34 
35 typedef enum {
36     PORT_MODE_BROADCAST   = 0,
37     PORT_MODE_UNICAST     = 1,
38     PORT_MODE_BIG_BUFFER  = 2,
39 } port_mode_t;
40 
41 /* Inits the port subsystem
42  */
43 void port_init(void);
44 
45 /* Make a named write-side port. broadcast ports can be opened by any
46  * number of read-clients. |name| can be up to PORT_NAME_LEN chars. If
47  * the write port exists it is returned even if the |mode| does not match.
48  */
49 status_t port_create(const char *name, port_mode_t mode, port_t *port);
50 
51 /* Make a read-side port. Only non-destroyed existing write ports can
52  * be opened with this api. Unicast ports can only be opened once. For
53  * broadcast ports, each call if successful returns a new port.
54  */
55 status_t port_open(const char *name, void *ctx, port_t *port);
56 
57 /* Creates a read-side port group which behaves just like a regular
58  * read-side port. A given port can only be assoicated with one port group.
59  */
60 status_t port_group(port_t *ports, size_t count, port_t *group);
61 
62 /* Adds a read-side port to an existing port group.
63  */
64 status_t port_group_add(port_t group, port_t port);
65 
66 /* Removes a read-side port to an existing port group.
67  */
68 status_t port_group_remove(port_t group, port_t port);
69 
70 /* Write to a port |count| packets, non-blocking, all or none atomic success.
71  */
72 status_t port_write(port_t port, const port_packet_t *pk, size_t count);
73 
74 /* Read one packet from the port or port group, blocking. The |result| contains
75  * the port that the message was read from. If |timeout| is zero the call
76  * does not block.
77  */
78 status_t port_read(port_t port, lk_time_t timeout, port_result_t *result);
79 
80 /* Destroy the write-side port, flush queued packets and release all resources,
81  * all calls will now fail on that port. Only a closed port can be destroyed.
82  */
83 status_t port_destroy(port_t port);
84 
85 /* Close the read-side port or the write side port. A closed write side port
86  * can be opened and the pending packets read. closing a port group does not
87  * close the included ports.
88  */
89 status_t port_close(port_t port);
90 
91 __END_CDECLS
92