| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * FireDTV driver (formerly known as FireSAT) | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com> | 
 | 5 |  * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se> | 
 | 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 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 13 | #include <linux/device.h> | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 14 | #include <linux/dvb/ca.h> | 
 | 15 | #include <linux/fs.h> | 
 | 16 | #include <linux/module.h> | 
 | 17 |  | 
 | 18 | #include <dvbdev.h> | 
 | 19 |  | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 20 | #include "firedtv.h" | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 21 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 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 | 
 | 26 |  | 
 | 27 | static int fdtv_ca_ready(struct firedtv_tuner_status *stat) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 28 | { | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 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; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 33 | } | 
 | 34 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 35 | static int fdtv_get_ca_flags(struct firedtv_tuner_status *stat) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 36 | { | 
 | 37 | 	int flags = 0; | 
 | 38 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 39 | 	if (stat->ca_module_present_status == 1) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 40 | 		flags |= CA_CI_MODULE_PRESENT; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 41 | 	if (stat->ca_initialization_status == 1 && | 
 | 42 | 	    stat->ca_error_flag            == 0 && | 
 | 43 | 	    stat->ca_dvb_flag              == 1) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 44 | 		flags |= CA_CI_MODULE_READY; | 
 | 45 | 	return flags; | 
 | 46 | } | 
 | 47 |  | 
 | 48 | static int fdtv_ca_reset(struct firedtv *fdtv) | 
 | 49 | { | 
 | 50 | 	return avc_ca_reset(fdtv) ? -EFAULT : 0; | 
 | 51 | } | 
 | 52 |  | 
 | 53 | static int fdtv_ca_get_caps(void *arg) | 
 | 54 | { | 
 | 55 | 	struct ca_caps *cap = arg; | 
 | 56 |  | 
 | 57 | 	cap->slot_num = 1; | 
 | 58 | 	cap->slot_type = CA_CI; | 
 | 59 | 	cap->descr_num = 1; | 
 | 60 | 	cap->descr_type = CA_ECD; | 
 | 61 | 	return 0; | 
 | 62 | } | 
 | 63 |  | 
 | 64 | static int fdtv_ca_get_slot_info(struct firedtv *fdtv, void *arg) | 
 | 65 | { | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 66 | 	struct firedtv_tuner_status stat; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 67 | 	struct ca_slot_info *slot = arg; | 
 | 68 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 69 | 	if (avc_tuner_status(fdtv, &stat)) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 70 | 		return -EFAULT; | 
 | 71 |  | 
 | 72 | 	if (slot->num != 0) | 
 | 73 | 		return -EFAULT; | 
 | 74 |  | 
 | 75 | 	slot->type = CA_CI; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 76 | 	slot->flags = fdtv_get_ca_flags(&stat); | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 77 | 	return 0; | 
 | 78 | } | 
 | 79 |  | 
 | 80 | static int fdtv_ca_app_info(struct firedtv *fdtv, void *arg) | 
 | 81 | { | 
 | 82 | 	struct ca_msg *reply = arg; | 
 | 83 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 84 | 	return avc_ca_app_info(fdtv, reply->msg, &reply->length) ? -EFAULT : 0; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 85 | } | 
 | 86 |  | 
 | 87 | static int fdtv_ca_info(struct firedtv *fdtv, void *arg) | 
 | 88 | { | 
 | 89 | 	struct ca_msg *reply = arg; | 
 | 90 |  | 
 | 91 | 	return avc_ca_info(fdtv, reply->msg, &reply->length) ? -EFAULT : 0; | 
 | 92 | } | 
 | 93 |  | 
 | 94 | static int fdtv_ca_get_mmi(struct firedtv *fdtv, void *arg) | 
 | 95 | { | 
 | 96 | 	struct ca_msg *reply = arg; | 
 | 97 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 98 | 	return avc_ca_get_mmi(fdtv, reply->msg, &reply->length) ? -EFAULT : 0; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 99 | } | 
 | 100 |  | 
 | 101 | static int fdtv_ca_get_msg(struct firedtv *fdtv, void *arg) | 
 | 102 | { | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 103 | 	struct firedtv_tuner_status stat; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 104 | 	int err; | 
 | 105 |  | 
 | 106 | 	switch (fdtv->ca_last_command) { | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 107 | 	case EN50221_TAG_APP_INFO_ENQUIRY: | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 108 | 		err = fdtv_ca_app_info(fdtv, arg); | 
 | 109 | 		break; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 110 | 	case EN50221_TAG_CA_INFO_ENQUIRY: | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 111 | 		err = fdtv_ca_info(fdtv, arg); | 
 | 112 | 		break; | 
 | 113 | 	default: | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 114 | 		if (avc_tuner_status(fdtv, &stat)) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 115 | 			err = -EFAULT; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 116 | 		else if (stat.ca_mmi == 1) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 117 | 			err = fdtv_ca_get_mmi(fdtv, arg); | 
 | 118 | 		else { | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 119 | 			dev_info(fdtv->device, "unhandled CA message 0x%08x\n", | 
 | 120 | 				 fdtv->ca_last_command); | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 121 | 			err = -EFAULT; | 
 | 122 | 		} | 
 | 123 | 	} | 
 | 124 | 	fdtv->ca_last_command = 0; | 
 | 125 | 	return err; | 
 | 126 | } | 
 | 127 |  | 
 | 128 | static int fdtv_ca_pmt(struct firedtv *fdtv, void *arg) | 
 | 129 | { | 
 | 130 | 	struct ca_msg *msg = arg; | 
 | 131 | 	int data_pos; | 
 | 132 | 	int data_length; | 
 | 133 | 	int i; | 
 | 134 |  | 
 | 135 | 	data_pos = 4; | 
 | 136 | 	if (msg->msg[3] & 0x80) { | 
 | 137 | 		data_length = 0; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 138 | 		for (i = 0; i < (msg->msg[3] & 0x7f); i++) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 139 | 			data_length = (data_length << 8) + msg->msg[data_pos++]; | 
 | 140 | 	} else { | 
 | 141 | 		data_length = msg->msg[3]; | 
 | 142 | 	} | 
 | 143 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 144 | 	return avc_ca_pmt(fdtv, &msg->msg[data_pos], data_length) ? -EFAULT : 0; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 145 | } | 
 | 146 |  | 
 | 147 | static int fdtv_ca_send_msg(struct firedtv *fdtv, void *arg) | 
 | 148 | { | 
 | 149 | 	struct ca_msg *msg = arg; | 
 | 150 | 	int err; | 
 | 151 |  | 
 | 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) { | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 156 | 	case EN50221_TAG_CA_PMT: | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 157 | 		err = fdtv_ca_pmt(fdtv, arg); | 
 | 158 | 		break; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 159 | 	case EN50221_TAG_APP_INFO_ENQUIRY: | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 160 | 		/* handled in ca_get_msg */ | 
 | 161 | 		err = 0; | 
 | 162 | 		break; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 163 | 	case EN50221_TAG_CA_INFO_ENQUIRY: | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 164 | 		/* handled in ca_get_msg */ | 
 | 165 | 		err = 0; | 
 | 166 | 		break; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 167 | 	case EN50221_TAG_ENTER_MENU: | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 168 | 		err = avc_ca_enter_menu(fdtv); | 
 | 169 | 		break; | 
 | 170 | 	default: | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 171 | 		dev_err(fdtv->device, "unhandled CA message 0x%08x\n", | 
 | 172 | 			fdtv->ca_last_command); | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 173 | 		err = -EFAULT; | 
 | 174 | 	} | 
 | 175 | 	return err; | 
 | 176 | } | 
 | 177 |  | 
| Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 178 | static int fdtv_ca_ioctl(struct file *file, unsigned int cmd, void *arg) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 179 | { | 
 | 180 | 	struct dvb_device *dvbdev = file->private_data; | 
 | 181 | 	struct firedtv *fdtv = dvbdev->priv; | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 182 | 	struct firedtv_tuner_status stat; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 183 | 	int err; | 
 | 184 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 185 | 	switch (cmd) { | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 186 | 	case CA_RESET: | 
 | 187 | 		err = fdtv_ca_reset(fdtv); | 
 | 188 | 		break; | 
 | 189 | 	case CA_GET_CAP: | 
 | 190 | 		err = fdtv_ca_get_caps(arg); | 
 | 191 | 		break; | 
 | 192 | 	case CA_GET_SLOT_INFO: | 
 | 193 | 		err = fdtv_ca_get_slot_info(fdtv, arg); | 
 | 194 | 		break; | 
 | 195 | 	case CA_GET_MSG: | 
 | 196 | 		err = fdtv_ca_get_msg(fdtv, arg); | 
 | 197 | 		break; | 
 | 198 | 	case CA_SEND_MSG: | 
 | 199 | 		err = fdtv_ca_send_msg(fdtv, arg); | 
 | 200 | 		break; | 
 | 201 | 	default: | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 202 | 		dev_info(fdtv->device, "unhandled CA ioctl %u\n", cmd); | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 203 | 		err = -EOPNOTSUPP; | 
 | 204 | 	} | 
 | 205 |  | 
 | 206 | 	/* FIXME Is this necessary? */ | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 207 | 	avc_tuner_status(fdtv, &stat); | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 208 |  | 
 | 209 | 	return err; | 
 | 210 | } | 
 | 211 |  | 
 | 212 | static unsigned int fdtv_ca_io_poll(struct file *file, poll_table *wait) | 
 | 213 | { | 
 | 214 | 	return POLLIN; | 
 | 215 | } | 
 | 216 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 217 | static const struct file_operations fdtv_ca_fops = { | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 218 | 	.owner		= THIS_MODULE, | 
| Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 219 | 	.unlocked_ioctl	= dvb_generic_ioctl, | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 220 | 	.open		= dvb_generic_open, | 
 | 221 | 	.release	= dvb_generic_release, | 
 | 222 | 	.poll		= fdtv_ca_io_poll, | 
 | 223 | }; | 
 | 224 |  | 
 | 225 | static struct dvb_device fdtv_ca = { | 
 | 226 | 	.users		= 1, | 
 | 227 | 	.readers	= 1, | 
 | 228 | 	.writers	= 1, | 
 | 229 | 	.fops		= &fdtv_ca_fops, | 
 | 230 | 	.kernel_ioctl	= fdtv_ca_ioctl, | 
 | 231 | }; | 
 | 232 |  | 
 | 233 | int fdtv_ca_register(struct firedtv *fdtv) | 
 | 234 | { | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 235 | 	struct firedtv_tuner_status stat; | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 236 | 	int err; | 
 | 237 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 238 | 	if (avc_tuner_status(fdtv, &stat)) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 239 | 		return -EINVAL; | 
 | 240 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 241 | 	if (!fdtv_ca_ready(&stat)) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 242 | 		return -EFAULT; | 
 | 243 |  | 
 | 244 | 	err = dvb_register_device(&fdtv->adapter, &fdtv->cadev, | 
 | 245 | 				  &fdtv_ca, fdtv, DVB_DEVICE_CA); | 
 | 246 |  | 
| Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 247 | 	if (stat.ca_application_info == 0) | 
 | 248 | 		dev_err(fdtv->device, "CaApplicationInfo is not set\n"); | 
 | 249 | 	if (stat.ca_date_time_request == 1) | 
| Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 250 | 		avc_ca_get_time_date(fdtv, &fdtv->ca_time_interval); | 
 | 251 |  | 
 | 252 | 	return err; | 
 | 253 | } | 
 | 254 |  | 
 | 255 | void fdtv_ca_release(struct firedtv *fdtv) | 
 | 256 | { | 
 | 257 | 	if (fdtv->cadev) | 
 | 258 | 		dvb_unregister_device(fdtv->cadev); | 
 | 259 | } |