blob: fad1900e816a035f63dddf84653ac9e8f16647fc [file] [log] [blame]
Olivier Naudanb28313f2012-04-16 08:10:18 -04001/*
2 * camdevice.c - GStreamer hardware CAM support
3 * Copyright (C) 2007 Alessandro Decina
4 *
5 * Authors:
6 * Alessandro Decina <alessandro@nnva.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
Sebastian Drögee6513c12013-07-14 12:12:42 +020020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
Olivier Naudanb28313f2012-04-16 08:10:18 -040022 */
23
24#include <glib.h>
25#include <sys/ioctl.h>
26#include <fcntl.h>
27#include <unistd.h>
28#include <string.h>
29#include <linux/dvb/ca.h>
30
31#include "camdevice.h"
32
33#define GST_CAT_DEFAULT cam_debug_cat
34
35CamDevice *
36cam_device_new (void)
37{
38 CamDevice *device = g_new0 (CamDevice, 1);
39
40 device->state = CAM_DEVICE_STATE_CLOSED;
41
42 return device;
43}
44
45static void
46reset_state (CamDevice * device)
47{
48 if (device->filename) {
49 g_free (device->filename);
50 device->filename = NULL;
51 }
52
53 if (device->fd) {
54 close (device->fd);
55 device->fd = -1;
56 }
57
58 if (device->cas) {
59 cam_conditional_access_destroy (device->cas);
60 device->cas = NULL;
61 }
62
63 if (device->mgr) {
64 cam_resource_manager_destroy (device->mgr);
65 device->mgr = NULL;
66 }
67
68 if (device->info) {
69 cam_application_info_destroy (device->info);
70 device->info = NULL;
71 }
72
73 if (device->al) {
74 cam_al_destroy (device->al);
75 device->al = NULL;
76 }
77
78 if (device->sl) {
79 cam_sl_destroy (device->sl);
80 device->sl = NULL;
81 }
82
83 if (device->tl) {
84 cam_tl_destroy (device->tl);
85 device->tl = NULL;
86 }
87
88 device->state = CAM_DEVICE_STATE_CLOSED;
89}
90
91void
92cam_device_free (CamDevice * device)
93{
94 if (device->state != CAM_DEVICE_STATE_CLOSED)
95 GST_WARNING ("device not in CLOSED state when free'd");
96
97 reset_state (device);
98 g_free (device);
99}
100
101gboolean
102cam_device_open (CamDevice * device, const char *filename)
103{
104 ca_caps_t ca_caps;
105 int ret;
106 int i;
Sebastian Dröge5a155a02012-09-14 10:49:31 +0200107 int count = 10;
Olivier Naudanb28313f2012-04-16 08:10:18 -0400108
109 g_return_val_if_fail (device != NULL, FALSE);
110 g_return_val_if_fail (device->state == CAM_DEVICE_STATE_CLOSED, FALSE);
111 g_return_val_if_fail (filename != NULL, FALSE);
112
Sebastian Dröge1491e032014-06-22 19:09:53 +0200113 GST_INFO ("opening CA device %s", filename);
Olivier Naudanb28313f2012-04-16 08:10:18 -0400114
115 ret = open (filename, O_RDWR);
116 if (ret == -1) {
Sebastian Dröge1491e032014-06-22 19:09:53 +0200117 GST_ERROR ("can't open CA device: %s", g_strerror (errno));
Olivier Naudanb28313f2012-04-16 08:10:18 -0400118 return FALSE;
119 }
120
Sebastian Dröge5a155a02012-09-14 10:49:31 +0200121 GST_DEBUG ("Successfully opened device %s", filename);
122
Olivier Naudanb28313f2012-04-16 08:10:18 -0400123 device->fd = ret;
124
125 ret = ioctl (device->fd, CA_RESET);
Olivier Naudanb28313f2012-04-16 08:10:18 -0400126
Sebastian Dröge5a155a02012-09-14 10:49:31 +0200127 g_usleep (G_USEC_PER_SEC / 10);
128
129 while (TRUE) {
130 /* get the capabilities of the CA */
131 ret = ioctl (device->fd, CA_GET_CAP, &ca_caps);
132 if (ret == -1) {
Sebastian Dröge1491e032014-06-22 19:09:53 +0200133 GST_ERROR ("CA_GET_CAP ioctl failed: %s", g_strerror (errno));
Sebastian Dröge5a155a02012-09-14 10:49:31 +0200134 reset_state (device);
135 return FALSE;
136 }
137 if (ca_caps.slot_num > 0)
138 break;
139 if (!count) {
140 GST_ERROR ("CA_GET_CAP succeeded but not slots");
141 reset_state (device);
142 return FALSE;
143 }
144 count--;
145 g_usleep (G_USEC_PER_SEC / 5);
Olivier Naudanb28313f2012-04-16 08:10:18 -0400146 }
147
148 device->tl = cam_tl_new (device->fd);
149 device->sl = cam_sl_new (device->tl);
150 device->al = cam_al_new (device->sl);
151
152 device->mgr = cam_resource_manager_new ();
153 cam_al_install (device->al, CAM_AL_APPLICATION (device->mgr));
154
155 device->info = cam_application_info_new ();
156 cam_al_install (device->al, CAM_AL_APPLICATION (device->info));
157
158 device->cas = cam_conditional_access_new ();
159 cam_al_install (device->al, CAM_AL_APPLICATION (device->cas));
160
161 /* open a connection to each slot */
162 for (i = 0; i < ca_caps.slot_num; ++i) {
163 CamTLConnection *connection;
164
165 ret = cam_tl_create_connection (device->tl, i, &connection);
166 if (CAM_FAILED (ret)) {
167 /* just ignore the slot, error out later only if no connection has been
168 * established */
169 GST_WARNING ("connection to slot %d failed, error: %d", i, ret);
170 continue;
171 }
172 }
173
174 if (g_hash_table_size (device->tl->connections) == 0) {
175 GST_ERROR ("couldn't connect to any slot");
176
177 reset_state (device);
178 return FALSE;
179 }
180
181 device->state = CAM_DEVICE_STATE_OPEN;
182 device->filename = g_strdup (filename);
183
184 /* poll each connection to initiate the protocol */
185 cam_tl_read_all (device->tl, TRUE);
186
187 return TRUE;
188}
189
190void
191cam_device_close (CamDevice * device)
192{
193 g_return_if_fail (device != NULL);
194 g_return_if_fail (device->state == CAM_DEVICE_STATE_OPEN);
195
Sebastian Dröge1491e032014-06-22 19:09:53 +0200196 GST_INFO ("closing CA device %s", device->filename);
Olivier Naudanb28313f2012-04-16 08:10:18 -0400197 reset_state (device);
198}
199
200void
201cam_device_poll (CamDevice * device)
202{
203 g_return_if_fail (device != NULL);
204 g_return_if_fail (device->state == CAM_DEVICE_STATE_OPEN);
205
206 cam_tl_read_all (device->tl, TRUE);
207}
208
209gboolean
210cam_device_ready (CamDevice * device)
211{
212 g_return_val_if_fail (device != NULL, FALSE);
213 g_return_val_if_fail (device->state == CAM_DEVICE_STATE_OPEN, FALSE);
214
215 return device->cas->ready;
216}
217
218void
219cam_device_set_pmt (CamDevice * device,
Sebastian Dröge06cb1e42014-06-28 12:00:27 +0200220 GstMpegtsPMT * pmt, CamConditionalAccessPmtFlag flag)
Olivier Naudanb28313f2012-04-16 08:10:18 -0400221{
222 g_return_if_fail (device != NULL);
223 g_return_if_fail (device->state == CAM_DEVICE_STATE_OPEN);
224 g_return_if_fail (pmt != NULL);
225
226 cam_conditional_access_set_pmt (device->cas, pmt, flag);
227 cam_tl_read_all (device->tl, FALSE);
228}