blob: 87c2c55e146f63b7c8f45266dc2c618f91c768f0 [file] [log] [blame]
Andreas Eversberga23c7ee2012-12-18 10:47:28 +01001/* Interface handler for Sysmocom L1 (real hardware) */
2
3/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <assert.h>
23#include <stdint.h>
24#include <unistd.h>
25#include <errno.h>
26#include <fcntl.h>
27
28#include <sys/types.h>
29#include <sys/stat.h>
30
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/utils.h>
33#include <osmocom/core/select.h>
34#include <osmocom/core/write_queue.h>
35#include <osmocom/core/timer.h>
36#include <osmocom/gsm/gsm_utils.h>
37
38#include <sysmocom/femtobts/superfemto.h>
39#include <sysmocom/femtobts/gsml1prim.h>
40#include <sysmocom/femtobts/gsml1const.h>
41#include <sysmocom/femtobts/gsml1types.h>
42
43#include "gprs_debug.h"
44#include "femtobts.h"
45#include "sysmo_l1_if.h"
46
47
48#ifdef HW_SYSMOBTS_V1
49#define DEV_SYS_DSP2ARM_NAME "/dev/msgq/femtobts_dsp2arm"
50#define DEV_SYS_ARM2DSP_NAME "/dev/msgq/femtobts_arm2dsp"
51#define DEV_L1_DSP2ARM_NAME "/dev/msgq/gsml1_dsp2arm"
52#define DEV_L1_ARM2DSP_NAME "/dev/msgq/gsml1_arm2dsp"
53#else
54#define DEV_SYS_DSP2ARM_NAME "/dev/msgq/superfemto_dsp2arm"
55#define DEV_SYS_ARM2DSP_NAME "/dev/msgq/superfemto_arm2dsp"
56#define DEV_L1_DSP2ARM_NAME "/dev/msgq/gsml1_sig_dsp2arm"
57#define DEV_L1_ARM2DSP_NAME "/dev/msgq/gsml1_sig_arm2dsp"
58
59#define DEV_TCH_DSP2ARM_NAME "/dev/msgq/gsml1_tch_dsp2arm"
60#define DEV_TCH_ARM2DSP_NAME "/dev/msgq/gsml1_tch_arm2dsp"
61#define DEV_PDTCH_DSP2ARM_NAME "/dev/msgq/gsml1_pdtch_dsp2arm"
62#define DEV_PDTCH_ARM2DSP_NAME "/dev/msgq/gsml1_pdtch_arm2dsp"
63#endif
64
65static const char *rd_devnames[] = {
66 [MQ_SYS_READ] = DEV_SYS_DSP2ARM_NAME,
67 [MQ_L1_READ] = DEV_L1_DSP2ARM_NAME,
68#ifndef HW_SYSMOBTS_V1
69 [MQ_TCH_READ] = DEV_TCH_DSP2ARM_NAME,
70 [MQ_PDTCH_READ] = DEV_PDTCH_DSP2ARM_NAME,
71#endif
72};
73
74static const char *wr_devnames[] = {
75 [MQ_SYS_WRITE] = DEV_SYS_ARM2DSP_NAME,
76 [MQ_L1_WRITE] = DEV_L1_ARM2DSP_NAME,
77#ifndef HW_SYSMOBTS_V1
78 [MQ_TCH_WRITE] = DEV_TCH_ARM2DSP_NAME,
79 [MQ_PDTCH_WRITE]= DEV_PDTCH_ARM2DSP_NAME,
80#endif
81};
82
83/* callback when there's something to read from the l1 msg_queue */
84static int l1if_fd_cb(struct osmo_fd *ofd, unsigned int what)
85{
86 //struct msgb *msg = l1p_msgb_alloc();
87 struct msgb *msg = msgb_alloc_headroom(sizeof(SuperFemto_Prim_t) + 128,
88 128, "1l_fd");
89 struct femtol1_hdl *fl1h = ofd->data;
90 int rc;
91
92 msg->l1h = msg->data;
93 rc = read(ofd->fd, msg->l1h, msgb_tailroom(msg));
94 if (rc < 0) {
95 if (rc != -1)
96 LOGP(DL1IF, LOGL_ERROR, "error reading from L1 msg_queue: %s\n",
97 strerror(errno));
98 msgb_free(msg);
99 return rc;
100 }
101 msgb_put(msg, rc);
102
103 switch (ofd->priv_nr) {
104 case MQ_SYS_WRITE:
105 if (rc != sizeof(SuperFemto_Prim_t))
106 LOGP(DL1IF, LOGL_NOTICE, "%u != "
107 "sizeof(SuperFemto_Prim_t)\n", rc);
108 return l1if_handle_sysprim(fl1h, msg);
109 case MQ_L1_WRITE:
110#ifndef HW_SYSMOBTS_V1
111 case MQ_TCH_WRITE:
112 case MQ_PDTCH_WRITE:
113#endif
114 if (rc != sizeof(GsmL1_Prim_t))
115 LOGP(DL1IF, LOGL_NOTICE, "%u != "
116 "sizeof(GsmL1_Prim_t)\n", rc);
117 return l1if_handle_l1prim(ofd->priv_nr, fl1h, msg);
118 default:
119 /* The compiler can't know that priv_nr is an enum. Assist. */
120 LOGP(DL1IF, LOGL_FATAL, "writing on a wrong queue: %d\n",
121 ofd->priv_nr);
122 exit(0);
123 break;
124 }
125};
126
127/* callback when we can write to one of the l1 msg_queue devices */
128static int l1fd_write_cb(struct osmo_fd *ofd, struct msgb *msg)
129{
130 int rc;
131
132 rc = write(ofd->fd, msg->l1h, msgb_l1len(msg));
133 if (rc < 0) {
134 LOGP(DL1IF, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
135 strerror(errno));
136 return rc;
137 } else if (rc < msg->len) {
138 LOGP(DL1IF, LOGL_ERROR, "short write to L1 msg_queue: "
139 "%u < %u\n", rc, msg->len);
140 return -EIO;
141 }
142
143 return 0;
144}
145
146int l1if_transport_open(int q, struct femtol1_hdl *hdl)
147{
148 int rc;
149
150 /* Step 1: Open all msg_queue file descriptors */
151 struct osmo_fd *read_ofd = &hdl->read_ofd[q];
152 struct osmo_wqueue *wq = &hdl->write_q[q];
153 struct osmo_fd *write_ofd = &hdl->write_q[q].bfd;
154
155 rc = open(rd_devnames[q], O_RDONLY);
156 if (rc < 0) {
157 LOGP(DL1IF, LOGL_FATAL, "unable to open msg_queue: %s\n",
158 strerror(errno));
159 return rc;
160 }
161 read_ofd->fd = rc;
162 read_ofd->priv_nr = q;
163 read_ofd->data = hdl;
164 read_ofd->cb = l1if_fd_cb;
165 read_ofd->when = BSC_FD_READ;
166 rc = osmo_fd_register(read_ofd);
167 if (rc < 0) {
168 close(read_ofd->fd);
169 read_ofd->fd = -1;
170 return rc;
171 }
172
173 rc = open(wr_devnames[q], O_WRONLY);
174 if (rc < 0) {
175 LOGP(DL1IF, LOGL_FATAL, "unable to open msg_queue: %s\n",
176 strerror(errno));
177 goto out_read;
178 }
179 osmo_wqueue_init(wq, 10);
180 wq->write_cb = l1fd_write_cb;
181 write_ofd->fd = rc;
182 write_ofd->priv_nr = q;
183 write_ofd->data = hdl;
184 write_ofd->when = BSC_FD_WRITE;
185 rc = osmo_fd_register(write_ofd);
186 if (rc < 0) {
187 close(write_ofd->fd);
188 write_ofd->fd = -1;
189 goto out_read;
190 }
191
192 return 0;
193
194out_read:
195 close(hdl->read_ofd[q].fd);
196 osmo_fd_unregister(&hdl->read_ofd[q]);
197
198 return rc;
199}
200
201int l1if_transport_close(int q, struct femtol1_hdl *hdl)
202{
203 struct osmo_fd *read_ofd = &hdl->read_ofd[q];
204 struct osmo_fd *write_ofd = &hdl->write_q[q].bfd;
205
206 osmo_fd_unregister(read_ofd);
207 close(read_ofd->fd);
208 read_ofd->fd = -1;
209
210 osmo_fd_unregister(write_ofd);
211 close(write_ofd->fd);
212 write_ofd->fd = -1;
213
214 return 0;
215}