2 * FireDTV driver (formerly known as FireSAT)
4 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
5 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
13 #include <linux/device.h>
14 #include <linux/dvb/ca.h>
16 #include <linux/module.h>
18 #include <media/dvbdev.h>
22 #define EN50221_TAG_APP_INFO_ENQUIRY 0x9f8020
23 #define EN50221_TAG_CA_INFO_ENQUIRY 0x9f8030
24 #define EN50221_TAG_CA_PMT 0x9f8032
25 #define EN50221_TAG_ENTER_MENU 0x9f8022
27 static int fdtv_ca_ready(struct firedtv_tuner_status
*stat
)
29 return stat
->ca_initialization_status
== 1 &&
30 stat
->ca_error_flag
== 0 &&
31 stat
->ca_dvb_flag
== 1 &&
32 stat
->ca_module_present_status
== 1;
35 static int fdtv_get_ca_flags(struct firedtv_tuner_status
*stat
)
39 if (stat
->ca_module_present_status
== 1)
40 flags
|= CA_CI_MODULE_PRESENT
;
41 if (stat
->ca_initialization_status
== 1 &&
42 stat
->ca_error_flag
== 0 &&
43 stat
->ca_dvb_flag
== 1)
44 flags
|= CA_CI_MODULE_READY
;
48 static int fdtv_ca_get_caps(void *arg
)
50 struct ca_caps
*cap
= arg
;
53 cap
->slot_type
= CA_CI
;
55 cap
->descr_type
= CA_ECD
;
59 static int fdtv_ca_get_slot_info(struct firedtv
*fdtv
, void *arg
)
61 struct firedtv_tuner_status stat
;
62 struct ca_slot_info
*slot
= arg
;
65 err
= avc_tuner_status(fdtv
, &stat
);
73 slot
->flags
= fdtv_get_ca_flags(&stat
);
77 static int fdtv_ca_app_info(struct firedtv
*fdtv
, void *arg
)
79 struct ca_msg
*reply
= arg
;
81 return avc_ca_app_info(fdtv
, reply
->msg
, &reply
->length
);
84 static int fdtv_ca_info(struct firedtv
*fdtv
, void *arg
)
86 struct ca_msg
*reply
= arg
;
88 return avc_ca_info(fdtv
, reply
->msg
, &reply
->length
);
91 static int fdtv_ca_get_mmi(struct firedtv
*fdtv
, void *arg
)
93 struct ca_msg
*reply
= arg
;
95 return avc_ca_get_mmi(fdtv
, reply
->msg
, &reply
->length
);
98 static int fdtv_ca_get_msg(struct firedtv
*fdtv
, void *arg
)
100 struct firedtv_tuner_status stat
;
103 switch (fdtv
->ca_last_command
) {
104 case EN50221_TAG_APP_INFO_ENQUIRY
:
105 err
= fdtv_ca_app_info(fdtv
, arg
);
107 case EN50221_TAG_CA_INFO_ENQUIRY
:
108 err
= fdtv_ca_info(fdtv
, arg
);
111 err
= avc_tuner_status(fdtv
, &stat
);
114 if (stat
.ca_mmi
== 1)
115 err
= fdtv_ca_get_mmi(fdtv
, arg
);
117 dev_info(fdtv
->device
, "unhandled CA message 0x%08x\n",
118 fdtv
->ca_last_command
);
122 fdtv
->ca_last_command
= 0;
126 static int fdtv_ca_pmt(struct firedtv
*fdtv
, void *arg
)
128 struct ca_msg
*msg
= arg
;
134 if (msg
->msg
[3] & 0x80) {
136 for (i
= 0; i
< (msg
->msg
[3] & 0x7f); i
++)
137 data_length
= (data_length
<< 8) + msg
->msg
[data_pos
++];
139 data_length
= msg
->msg
[3];
142 return avc_ca_pmt(fdtv
, &msg
->msg
[data_pos
], data_length
);
145 static int fdtv_ca_send_msg(struct firedtv
*fdtv
, void *arg
)
147 struct ca_msg
*msg
= arg
;
150 /* Do we need a semaphore for this? */
151 fdtv
->ca_last_command
=
152 (msg
->msg
[0] << 16) + (msg
->msg
[1] << 8) + msg
->msg
[2];
153 switch (fdtv
->ca_last_command
) {
154 case EN50221_TAG_CA_PMT
:
155 err
= fdtv_ca_pmt(fdtv
, arg
);
157 case EN50221_TAG_APP_INFO_ENQUIRY
:
158 /* handled in ca_get_msg */
161 case EN50221_TAG_CA_INFO_ENQUIRY
:
162 /* handled in ca_get_msg */
165 case EN50221_TAG_ENTER_MENU
:
166 err
= avc_ca_enter_menu(fdtv
);
169 dev_err(fdtv
->device
, "unhandled CA message 0x%08x\n",
170 fdtv
->ca_last_command
);
176 static int fdtv_ca_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
178 struct dvb_device
*dvbdev
= file
->private_data
;
179 struct firedtv
*fdtv
= dvbdev
->priv
;
180 struct firedtv_tuner_status stat
;
185 err
= avc_ca_reset(fdtv
);
188 err
= fdtv_ca_get_caps(arg
);
190 case CA_GET_SLOT_INFO
:
191 err
= fdtv_ca_get_slot_info(fdtv
, arg
);
194 err
= fdtv_ca_get_msg(fdtv
, arg
);
197 err
= fdtv_ca_send_msg(fdtv
, arg
);
200 dev_info(fdtv
->device
, "unhandled CA ioctl %u\n", cmd
);
204 /* FIXME Is this necessary? */
205 avc_tuner_status(fdtv
, &stat
);
210 static __poll_t
fdtv_ca_io_poll(struct file
*file
, poll_table
*wait
)
215 static const struct file_operations fdtv_ca_fops
= {
216 .owner
= THIS_MODULE
,
217 .unlocked_ioctl
= dvb_generic_ioctl
,
218 .open
= dvb_generic_open
,
219 .release
= dvb_generic_release
,
220 .poll
= fdtv_ca_io_poll
,
221 .llseek
= noop_llseek
,
224 static struct dvb_device fdtv_ca
= {
228 .fops
= &fdtv_ca_fops
,
229 .kernel_ioctl
= fdtv_ca_ioctl
,
232 int fdtv_ca_register(struct firedtv
*fdtv
)
234 struct firedtv_tuner_status stat
;
237 if (avc_tuner_status(fdtv
, &stat
))
240 if (!fdtv_ca_ready(&stat
))
243 err
= dvb_register_device(&fdtv
->adapter
, &fdtv
->cadev
,
244 &fdtv_ca
, fdtv
, DVB_DEVICE_CA
, 0);
246 if (stat
.ca_application_info
== 0)
247 dev_err(fdtv
->device
, "CaApplicationInfo is not set\n");
248 if (stat
.ca_date_time_request
== 1)
249 avc_ca_get_time_date(fdtv
, &fdtv
->ca_time_interval
);
254 void fdtv_ca_release(struct firedtv
*fdtv
)
256 dvb_unregister_device(fdtv
->cadev
);