1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * FireDTV driver (formerly known as FireSAT)
5 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
6 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
9 #include <linux/device.h>
10 #include <linux/dvb/ca.h>
12 #include <linux/module.h>
14 #include <media/dvbdev.h>
18 #define EN50221_TAG_APP_INFO_ENQUIRY 0x9f8020
19 #define EN50221_TAG_CA_INFO_ENQUIRY 0x9f8030
20 #define EN50221_TAG_CA_PMT 0x9f8032
21 #define EN50221_TAG_ENTER_MENU 0x9f8022
23 static int fdtv_ca_ready(struct firedtv_tuner_status
*stat
)
25 return stat
->ca_initialization_status
== 1 &&
26 stat
->ca_error_flag
== 0 &&
27 stat
->ca_dvb_flag
== 1 &&
28 stat
->ca_module_present_status
== 1;
31 static int fdtv_get_ca_flags(struct firedtv_tuner_status
*stat
)
35 if (stat
->ca_module_present_status
== 1)
36 flags
|= CA_CI_MODULE_PRESENT
;
37 if (stat
->ca_initialization_status
== 1 &&
38 stat
->ca_error_flag
== 0 &&
39 stat
->ca_dvb_flag
== 1)
40 flags
|= CA_CI_MODULE_READY
;
44 static int fdtv_ca_get_caps(void *arg
)
46 struct ca_caps
*cap
= arg
;
49 cap
->slot_type
= CA_CI
;
51 cap
->descr_type
= CA_ECD
;
55 static int fdtv_ca_get_slot_info(struct firedtv
*fdtv
, void *arg
)
57 struct firedtv_tuner_status stat
;
58 struct ca_slot_info
*slot
= arg
;
61 err
= avc_tuner_status(fdtv
, &stat
);
69 slot
->flags
= fdtv_get_ca_flags(&stat
);
73 static int fdtv_ca_app_info(struct firedtv
*fdtv
, void *arg
)
75 struct ca_msg
*reply
= arg
;
77 return avc_ca_app_info(fdtv
, reply
->msg
, &reply
->length
);
80 static int fdtv_ca_info(struct firedtv
*fdtv
, void *arg
)
82 struct ca_msg
*reply
= arg
;
84 return avc_ca_info(fdtv
, reply
->msg
, &reply
->length
);
87 static int fdtv_ca_get_mmi(struct firedtv
*fdtv
, void *arg
)
89 struct ca_msg
*reply
= arg
;
91 return avc_ca_get_mmi(fdtv
, reply
->msg
, &reply
->length
);
94 static int fdtv_ca_get_msg(struct firedtv
*fdtv
, void *arg
)
96 struct firedtv_tuner_status stat
;
99 switch (fdtv
->ca_last_command
) {
100 case EN50221_TAG_APP_INFO_ENQUIRY
:
101 err
= fdtv_ca_app_info(fdtv
, arg
);
103 case EN50221_TAG_CA_INFO_ENQUIRY
:
104 err
= fdtv_ca_info(fdtv
, arg
);
107 err
= avc_tuner_status(fdtv
, &stat
);
110 if (stat
.ca_mmi
== 1)
111 err
= fdtv_ca_get_mmi(fdtv
, arg
);
113 dev_info(fdtv
->device
, "unhandled CA message 0x%08x\n",
114 fdtv
->ca_last_command
);
118 fdtv
->ca_last_command
= 0;
122 static int fdtv_ca_pmt(struct firedtv
*fdtv
, void *arg
)
124 struct ca_msg
*msg
= arg
;
130 if (msg
->msg
[3] & 0x80) {
132 for (i
= 0; i
< (msg
->msg
[3] & 0x7f); i
++)
133 data_length
= (data_length
<< 8) + msg
->msg
[data_pos
++];
135 data_length
= msg
->msg
[3];
138 return avc_ca_pmt(fdtv
, &msg
->msg
[data_pos
], data_length
);
141 static int fdtv_ca_send_msg(struct firedtv
*fdtv
, void *arg
)
143 struct ca_msg
*msg
= arg
;
146 /* Do we need a semaphore for this? */
147 fdtv
->ca_last_command
=
148 (msg
->msg
[0] << 16) + (msg
->msg
[1] << 8) + msg
->msg
[2];
149 switch (fdtv
->ca_last_command
) {
150 case EN50221_TAG_CA_PMT
:
151 err
= fdtv_ca_pmt(fdtv
, arg
);
153 case EN50221_TAG_APP_INFO_ENQUIRY
:
154 /* handled in ca_get_msg */
157 case EN50221_TAG_CA_INFO_ENQUIRY
:
158 /* handled in ca_get_msg */
161 case EN50221_TAG_ENTER_MENU
:
162 err
= avc_ca_enter_menu(fdtv
);
165 dev_err(fdtv
->device
, "unhandled CA message 0x%08x\n",
166 fdtv
->ca_last_command
);
172 static int fdtv_ca_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
174 struct dvb_device
*dvbdev
= file
->private_data
;
175 struct firedtv
*fdtv
= dvbdev
->priv
;
176 struct firedtv_tuner_status stat
;
181 err
= avc_ca_reset(fdtv
);
184 err
= fdtv_ca_get_caps(arg
);
186 case CA_GET_SLOT_INFO
:
187 err
= fdtv_ca_get_slot_info(fdtv
, arg
);
190 err
= fdtv_ca_get_msg(fdtv
, arg
);
193 err
= fdtv_ca_send_msg(fdtv
, arg
);
196 dev_info(fdtv
->device
, "unhandled CA ioctl %u\n", cmd
);
200 /* FIXME Is this necessary? */
201 avc_tuner_status(fdtv
, &stat
);
206 static __poll_t
fdtv_ca_io_poll(struct file
*file
, poll_table
*wait
)
211 static const struct file_operations fdtv_ca_fops
= {
212 .owner
= THIS_MODULE
,
213 .unlocked_ioctl
= dvb_generic_ioctl
,
214 .open
= dvb_generic_open
,
215 .release
= dvb_generic_release
,
216 .poll
= fdtv_ca_io_poll
,
217 .llseek
= noop_llseek
,
220 static const struct dvb_device fdtv_ca
= {
224 .fops
= &fdtv_ca_fops
,
225 .kernel_ioctl
= fdtv_ca_ioctl
,
228 int fdtv_ca_register(struct firedtv
*fdtv
)
230 struct firedtv_tuner_status stat
;
233 if (avc_tuner_status(fdtv
, &stat
))
236 if (!fdtv_ca_ready(&stat
))
239 err
= dvb_register_device(&fdtv
->adapter
, &fdtv
->cadev
,
240 &fdtv_ca
, fdtv
, DVB_DEVICE_CA
, 0);
242 if (stat
.ca_application_info
== 0)
243 dev_err(fdtv
->device
, "CaApplicationInfo is not set\n");
244 if (stat
.ca_date_time_request
== 1)
245 avc_ca_get_time_date(fdtv
, &fdtv
->ca_time_interval
);
250 void fdtv_ca_release(struct firedtv
*fdtv
)
252 dvb_unregister_device(fdtv
->cadev
);