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>
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_reset(struct firedtv
*fdtv
)
50 return avc_ca_reset(fdtv
) ? -EFAULT
: 0;
53 static int fdtv_ca_get_caps(void *arg
)
55 struct ca_caps
*cap
= arg
;
58 cap
->slot_type
= CA_CI
;
60 cap
->descr_type
= CA_ECD
;
64 static int fdtv_ca_get_slot_info(struct firedtv
*fdtv
, void *arg
)
66 struct firedtv_tuner_status stat
;
67 struct ca_slot_info
*slot
= arg
;
69 if (avc_tuner_status(fdtv
, &stat
))
76 slot
->flags
= fdtv_get_ca_flags(&stat
);
80 static int fdtv_ca_app_info(struct firedtv
*fdtv
, void *arg
)
82 struct ca_msg
*reply
= arg
;
84 return avc_ca_app_info(fdtv
, reply
->msg
, &reply
->length
) ? -EFAULT
: 0;
87 static int fdtv_ca_info(struct firedtv
*fdtv
, void *arg
)
89 struct ca_msg
*reply
= arg
;
91 return avc_ca_info(fdtv
, reply
->msg
, &reply
->length
) ? -EFAULT
: 0;
94 static int fdtv_ca_get_mmi(struct firedtv
*fdtv
, void *arg
)
96 struct ca_msg
*reply
= arg
;
98 return avc_ca_get_mmi(fdtv
, reply
->msg
, &reply
->length
) ? -EFAULT
: 0;
101 static int fdtv_ca_get_msg(struct firedtv
*fdtv
, void *arg
)
103 struct firedtv_tuner_status stat
;
106 switch (fdtv
->ca_last_command
) {
107 case EN50221_TAG_APP_INFO_ENQUIRY
:
108 err
= fdtv_ca_app_info(fdtv
, arg
);
110 case EN50221_TAG_CA_INFO_ENQUIRY
:
111 err
= fdtv_ca_info(fdtv
, arg
);
114 if (avc_tuner_status(fdtv
, &stat
))
116 else if (stat
.ca_mmi
== 1)
117 err
= fdtv_ca_get_mmi(fdtv
, arg
);
119 dev_info(fdtv
->device
, "unhandled CA message 0x%08x\n",
120 fdtv
->ca_last_command
);
124 fdtv
->ca_last_command
= 0;
128 static int fdtv_ca_pmt(struct firedtv
*fdtv
, void *arg
)
130 struct ca_msg
*msg
= arg
;
136 if (msg
->msg
[3] & 0x80) {
138 for (i
= 0; i
< (msg
->msg
[3] & 0x7f); i
++)
139 data_length
= (data_length
<< 8) + msg
->msg
[data_pos
++];
141 data_length
= msg
->msg
[3];
144 return avc_ca_pmt(fdtv
, &msg
->msg
[data_pos
], data_length
) ? -EFAULT
: 0;
147 static int fdtv_ca_send_msg(struct firedtv
*fdtv
, void *arg
)
149 struct ca_msg
*msg
= arg
;
152 /* Do we need a semaphore for this? */
153 fdtv
->ca_last_command
=
154 (msg
->msg
[0] << 16) + (msg
->msg
[1] << 8) + msg
->msg
[2];
155 switch (fdtv
->ca_last_command
) {
156 case EN50221_TAG_CA_PMT
:
157 err
= fdtv_ca_pmt(fdtv
, arg
);
159 case EN50221_TAG_APP_INFO_ENQUIRY
:
160 /* handled in ca_get_msg */
163 case EN50221_TAG_CA_INFO_ENQUIRY
:
164 /* handled in ca_get_msg */
167 case EN50221_TAG_ENTER_MENU
:
168 err
= avc_ca_enter_menu(fdtv
);
171 dev_err(fdtv
->device
, "unhandled CA message 0x%08x\n",
172 fdtv
->ca_last_command
);
178 static int fdtv_ca_ioctl(struct inode
*inode
, struct file
*file
,
179 unsigned int cmd
, void *arg
)
181 struct dvb_device
*dvbdev
= file
->private_data
;
182 struct firedtv
*fdtv
= dvbdev
->priv
;
183 struct firedtv_tuner_status stat
;
188 err
= fdtv_ca_reset(fdtv
);
191 err
= fdtv_ca_get_caps(arg
);
193 case CA_GET_SLOT_INFO
:
194 err
= fdtv_ca_get_slot_info(fdtv
, arg
);
197 err
= fdtv_ca_get_msg(fdtv
, arg
);
200 err
= fdtv_ca_send_msg(fdtv
, arg
);
203 dev_info(fdtv
->device
, "unhandled CA ioctl %u\n", cmd
);
207 /* FIXME Is this necessary? */
208 avc_tuner_status(fdtv
, &stat
);
213 static unsigned int fdtv_ca_io_poll(struct file
*file
, poll_table
*wait
)
218 static const struct file_operations fdtv_ca_fops
= {
219 .owner
= THIS_MODULE
,
220 .ioctl
= dvb_generic_ioctl
,
221 .open
= dvb_generic_open
,
222 .release
= dvb_generic_release
,
223 .poll
= fdtv_ca_io_poll
,
226 static struct dvb_device fdtv_ca
= {
230 .fops
= &fdtv_ca_fops
,
231 .kernel_ioctl
= fdtv_ca_ioctl
,
234 int fdtv_ca_register(struct firedtv
*fdtv
)
236 struct firedtv_tuner_status stat
;
239 if (avc_tuner_status(fdtv
, &stat
))
242 if (!fdtv_ca_ready(&stat
))
245 err
= dvb_register_device(&fdtv
->adapter
, &fdtv
->cadev
,
246 &fdtv_ca
, fdtv
, DVB_DEVICE_CA
);
248 if (stat
.ca_application_info
== 0)
249 dev_err(fdtv
->device
, "CaApplicationInfo is not set\n");
250 if (stat
.ca_date_time_request
== 1)
251 avc_ca_get_time_date(fdtv
, &fdtv
->ca_time_interval
);
256 void fdtv_ca_release(struct firedtv
*fdtv
)
259 dvb_unregister_device(fdtv
->cadev
);