Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 1 | /* |
Stefan Richter | 612262a | 2008-08-26 00:17:30 +0200 | [diff] [blame] | 2 | * FireDTV driver (formerly known as FireSAT) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 3 | * |
Stefan Richter | 612262a | 2008-08-26 00:17:30 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com> |
| 5 | * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se> |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 6 | * |
| 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. |
| 11 | */ |
| 12 | |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 13 | #include <linux/dvb/ca.h> |
Stefan Richter | 612262a | 2008-08-26 00:17:30 +0200 | [diff] [blame] | 14 | #include <linux/fs.h> |
| 15 | #include <linux/module.h> |
| 16 | |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 17 | #include <dvbdev.h> |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 18 | |
Stefan Richter | 612262a | 2008-08-26 00:17:30 +0200 | [diff] [blame] | 19 | #include "avc_api.h" |
| 20 | #include "firesat.h" |
| 21 | #include "firesat-ci.h" |
| 22 | |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 23 | static int firesat_ca_ready(ANTENNA_INPUT_INFO *info) |
| 24 | { |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 25 | return info->CaInitializationStatus == 1 && |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 26 | info->CaErrorFlag == 0 && |
| 27 | info->CaDvbFlag == 1 && |
| 28 | info->CaModulePresentStatus == 1; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 29 | } |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 30 | |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 31 | static int firesat_get_ca_flags(ANTENNA_INPUT_INFO *info) |
| 32 | { |
| 33 | int flags = 0; |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 34 | |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 35 | if (info->CaModulePresentStatus == 1) |
| 36 | flags |= CA_CI_MODULE_PRESENT; |
| 37 | if (info->CaInitializationStatus == 1 && |
| 38 | info->CaErrorFlag == 0 && |
| 39 | info->CaDvbFlag == 1) |
| 40 | flags |= CA_CI_MODULE_READY; |
| 41 | return flags; |
| 42 | } |
| 43 | |
| 44 | static int firesat_ca_reset(struct firesat *firesat) |
| 45 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 46 | return avc_ca_reset(firesat) ? -EFAULT : 0; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 49 | static int firesat_ca_get_caps(void *arg) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 50 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 51 | struct ca_caps *cap = arg; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 52 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 53 | cap->slot_num = 1; |
| 54 | cap->slot_type = CA_CI; |
| 55 | cap->descr_num = 1; |
| 56 | cap->descr_type = CA_ECD; |
| 57 | return 0; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int firesat_ca_get_slot_info(struct firesat *firesat, void *arg) |
| 61 | { |
| 62 | ANTENNA_INPUT_INFO info; |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 63 | struct ca_slot_info *slot = arg; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 64 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 65 | if (avc_tuner_status(firesat, &info)) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 66 | return -EFAULT; |
| 67 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 68 | if (slot->num != 0) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 69 | return -EFAULT; |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 70 | |
| 71 | slot->type = CA_CI; |
| 72 | slot->flags = firesat_get_ca_flags(&info); |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int firesat_ca_app_info(struct firesat *firesat, void *arg) |
| 77 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 78 | struct ca_msg *reply = arg; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 79 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 80 | return |
| 81 | avc_ca_app_info(firesat, reply->msg, &reply->length) ? -EFAULT : 0; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int firesat_ca_info(struct firesat *firesat, void *arg) |
| 85 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 86 | struct ca_msg *reply = arg; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 87 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 88 | return avc_ca_info(firesat, reply->msg, &reply->length) ? -EFAULT : 0; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int firesat_ca_get_mmi(struct firesat *firesat, void *arg) |
| 92 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 93 | struct ca_msg *reply = arg; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 94 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 95 | return |
| 96 | avc_ca_get_mmi(firesat, reply->msg, &reply->length) ? -EFAULT : 0; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static int firesat_ca_get_msg(struct firesat *firesat, void *arg) |
| 100 | { |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 101 | ANTENNA_INPUT_INFO info; |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 102 | int err; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 103 | |
| 104 | switch (firesat->ca_last_command) { |
| 105 | case TAG_APP_INFO_ENQUIRY: |
| 106 | err = firesat_ca_app_info(firesat, arg); |
| 107 | break; |
| 108 | case TAG_CA_INFO_ENQUIRY: |
| 109 | err = firesat_ca_info(firesat, arg); |
| 110 | break; |
| 111 | default: |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 112 | if (avc_tuner_status(firesat, &info)) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 113 | err = -EFAULT; |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 114 | else if (info.CaMmi == 1) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 115 | err = firesat_ca_get_mmi(firesat, arg); |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 116 | else { |
| 117 | printk(KERN_INFO "%s: Unhandled message 0x%08X\n", |
| 118 | __func__, firesat->ca_last_command); |
| 119 | err = -EFAULT; |
| 120 | } |
| 121 | } |
| 122 | firesat->ca_last_command = 0; |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | static int firesat_ca_pmt(struct firesat *firesat, void *arg) |
| 127 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 128 | struct ca_msg *msg = arg; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 129 | int data_pos; |
Henrik Kurelid | 7199e52 | 2008-12-05 10:00:16 +0100 | [diff] [blame^] | 130 | int data_length; |
| 131 | int i; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 132 | |
Henrik Kurelid | 7199e52 | 2008-12-05 10:00:16 +0100 | [diff] [blame^] | 133 | data_pos = 4; |
| 134 | if (msg->msg[3] & 0x80) { |
| 135 | data_length = 0; |
| 136 | for (i = 0; i < (msg->msg[3] & 0x7F); i++) |
| 137 | data_length = (data_length << 8) + msg->msg[data_pos++]; |
| 138 | } else { |
| 139 | data_length = msg->msg[3]; |
| 140 | } |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 141 | |
Henrik Kurelid | 7199e52 | 2008-12-05 10:00:16 +0100 | [diff] [blame^] | 142 | return avc_ca_pmt(firesat, &msg->msg[data_pos], data_length) ? |
| 143 | -EFAULT : 0; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int firesat_ca_send_msg(struct firesat *firesat, void *arg) |
| 147 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 148 | struct ca_msg *msg = arg; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 149 | int err; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 150 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 151 | /* Do we need a semaphore for this? */ |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 152 | firesat->ca_last_command = |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 153 | (msg->msg[0] << 16) + (msg->msg[1] << 8) + msg->msg[2]; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 154 | switch (firesat->ca_last_command) { |
| 155 | case TAG_CA_PMT: |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 156 | err = firesat_ca_pmt(firesat, arg); |
| 157 | break; |
| 158 | case TAG_APP_INFO_ENQUIRY: |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 159 | /* handled in ca_get_msg */ |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 160 | err = 0; |
| 161 | break; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 162 | case TAG_CA_INFO_ENQUIRY: |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 163 | /* handled in ca_get_msg */ |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 164 | err = 0; |
| 165 | break; |
| 166 | case TAG_ENTER_MENU: |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 167 | err = avc_ca_enter_menu(firesat); |
| 168 | break; |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 169 | default: |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 170 | printk(KERN_ERR "%s: Unhandled unknown message 0x%08X\n", |
| 171 | __func__, firesat->ca_last_command); |
| 172 | err = -EFAULT; |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 173 | } |
| 174 | return err; |
| 175 | } |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 176 | |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 177 | static int firesat_ca_ioctl(struct inode *inode, struct file *file, |
| 178 | unsigned int cmd, void *arg) |
| 179 | { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 180 | struct dvb_device *dvbdev = file->private_data; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 181 | struct firesat *firesat = dvbdev->priv; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 182 | ANTENNA_INPUT_INFO info; |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 183 | int err; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 184 | |
| 185 | switch(cmd) { |
| 186 | case CA_RESET: |
| 187 | err = firesat_ca_reset(firesat); |
| 188 | break; |
| 189 | case CA_GET_CAP: |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 190 | err = firesat_ca_get_caps(arg); |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 191 | break; |
| 192 | case CA_GET_SLOT_INFO: |
| 193 | err = firesat_ca_get_slot_info(firesat, arg); |
| 194 | break; |
| 195 | case CA_GET_MSG: |
| 196 | err = firesat_ca_get_msg(firesat, arg); |
| 197 | break; |
| 198 | case CA_SEND_MSG: |
| 199 | err = firesat_ca_send_msg(firesat, arg); |
| 200 | break; |
| 201 | default: |
| 202 | printk(KERN_INFO "%s: Unhandled ioctl, command: %u\n",__func__, |
| 203 | cmd); |
| 204 | err = -EOPNOTSUPP; |
| 205 | } |
| 206 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 207 | /* FIXME Is this necessary? */ |
| 208 | avc_tuner_status(firesat, &info); |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 209 | |
| 210 | return err; |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 213 | static unsigned int firesat_ca_io_poll(struct file *file, poll_table *wait) |
| 214 | { |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 215 | return POLLIN; |
| 216 | } |
| 217 | |
| 218 | static struct file_operations firesat_ca_fops = { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 219 | .owner = THIS_MODULE, |
| 220 | .ioctl = dvb_generic_ioctl, |
| 221 | .open = dvb_generic_open, |
| 222 | .release = dvb_generic_release, |
| 223 | .poll = firesat_ca_io_poll, |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | static struct dvb_device firesat_ca = { |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 227 | .users = 1, |
| 228 | .readers = 1, |
| 229 | .writers = 1, |
| 230 | .fops = &firesat_ca_fops, |
| 231 | .kernel_ioctl = firesat_ca_ioctl, |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 234 | int firesat_ca_register(struct firesat *firesat) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 235 | { |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 236 | ANTENNA_INPUT_INFO info; |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 237 | int err; |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 238 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 239 | if (avc_tuner_status(firesat, &info)) |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 240 | return -EINVAL; |
| 241 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 242 | if (!firesat_ca_ready(&info)) |
| 243 | return -EFAULT; |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 244 | |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 245 | err = dvb_register_device(&firesat->adapter, &firesat->cadev, |
| 246 | &firesat_ca, firesat, DVB_DEVICE_CA); |
| 247 | |
| 248 | if (info.CaApplicationInfo == 0) |
| 249 | printk(KERN_ERR "%s: CaApplicationInfo is not set.\n", |
| 250 | __func__); |
| 251 | if (info.CaDateTimeRequest == 1) |
| 252 | avc_ca_get_time_date(firesat, &firesat->ca_time_interval); |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 253 | |
| 254 | return err; |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Henrik Kurelid | df4846c | 2008-08-01 10:00:45 +0200 | [diff] [blame] | 257 | void firesat_ca_release(struct firesat *firesat) |
| 258 | { |
| 259 | if (firesat->cadev) |
Stefan Richter | 8ae83cd | 2008-11-02 13:45:00 +0100 | [diff] [blame] | 260 | dvb_unregister_device(firesat->cadev); |
Greg Kroah-Hartman | c81c8b6 | 2008-03-06 21:30:23 -0800 | [diff] [blame] | 261 | } |