| 1 |
/* |
| 2 |
* Transcoder Interface for DAHDI |
| 3 |
* |
| 4 |
* Written by Mark Spencer <[email protected]> |
| 5 |
* |
| 6 |
* Copyright (C) 2006-2008, Digium, Inc. |
| 7 |
* |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* See http://www.asterisk.org for more information about |
| 14 |
* the Asterisk project. Please do not directly contact |
| 15 |
* any of the maintainers of this project for assistance; |
| 16 |
* the project provides a web site, mailing lists and IRC |
| 17 |
* channels for your use. |
| 18 |
* |
| 19 |
* This program is free software, distributed under the terms of |
| 20 |
* the GNU General Public License Version 2 as published by the |
| 21 |
* Free Software Foundation. See the LICENSE file included with |
| 22 |
* this program for more details. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <linux/kernel.h> |
| 26 |
#include <linux/errno.h> |
| 27 |
#include <linux/module.h> |
| 28 |
#include <linux/init.h> |
| 29 |
#include <linux/spinlock.h> |
| 30 |
#include <linux/slab.h> |
| 31 |
#include <linux/kmod.h> |
| 32 |
#include <linux/sched.h> |
| 33 |
#include <linux/interrupt.h> |
| 34 |
#include <linux/vmalloc.h> |
| 35 |
#include <linux/mm.h> |
| 36 |
#include <linux/page-flags.h> |
| 37 |
#include <asm/io.h> |
| 38 |
|
| 39 |
#include <dahdi/kernel.h> |
| 40 |
|
| 41 |
static int debug; |
| 42 |
/* The registration list contains transcoders in the order in which they were |
| 43 |
* registered. */ |
| 44 |
static LIST_HEAD(registration_list); |
| 45 |
/* The active list is sorted by the most recently used transcoder is last. This |
| 46 |
* is used as a simplistic way to spread the load amongst the different hardware |
| 47 |
* transcoders in the system. */ |
| 48 |
static LIST_HEAD(active_list); |
| 49 |
static DEFINE_SPINLOCK(translock); |
| 50 |
|
| 51 |
EXPORT_SYMBOL(dahdi_transcoder_register); |
| 52 |
EXPORT_SYMBOL(dahdi_transcoder_unregister); |
| 53 |
EXPORT_SYMBOL(dahdi_transcoder_alert); |
| 54 |
EXPORT_SYMBOL(dahdi_transcoder_alloc); |
| 55 |
EXPORT_SYMBOL(dahdi_transcoder_free); |
| 56 |
|
| 57 |
struct dahdi_transcoder *dahdi_transcoder_alloc(int numchans) |
| 58 |
{ |
| 59 |
struct dahdi_transcoder *tc; |
| 60 |
unsigned int x; |
| 61 |
size_t size = sizeof(*tc) + (sizeof(tc->channels[0]) * numchans); |
| 62 |
|
| 63 |
if (!(tc = kmalloc(size, GFP_KERNEL))) |
| 64 |
return NULL; |
| 65 |
|
| 66 |
memset(tc, 0, size); |
| 67 |
strcpy(tc->name, "<unspecified>"); |
| 68 |
INIT_LIST_HEAD(&tc->registration_list_node); |
| 69 |
INIT_LIST_HEAD(&tc->active_list_node); |
| 70 |
tc->numchannels = numchans; |
| 71 |
for (x=0; x < tc->numchannels; x++) { |
| 72 |
init_waitqueue_head(&tc->channels[x].ready); |
| 73 |
tc->channels[x].parent = tc; |
| 74 |
} |
| 75 |
|
| 76 |
WARN_ON(!dahdi_transcode_fops); |
| 77 |
/* Individual transcoders should supply their own file_operations for |
| 78 |
* write and read. But they will by default use the file_operations |
| 79 |
* provided by the dahdi_transcode layer. */ |
| 80 |
memcpy(&tc->fops, dahdi_transcode_fops, sizeof(*dahdi_transcode_fops)); |
| 81 |
return tc; |
| 82 |
} |
| 83 |
|
| 84 |
void dahdi_transcoder_free(struct dahdi_transcoder *tc) |
| 85 |
{ |
| 86 |
kfree(tc); |
| 87 |
} |
| 88 |
|
| 89 |
/* Returns 1 if the item is on the list pointed to by head, otherwise, returns |
| 90 |
* 0 */ |
| 91 |
static int is_on_list(struct list_head *entry, struct list_head *head) |
| 92 |
{ |
| 93 |
struct list_head *cur; |
| 94 |
list_for_each(cur, head) { |
| 95 |
if (cur == entry) return 1; |
| 96 |
} |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
/* Register a transcoder */ |
| 101 |
int dahdi_transcoder_register(struct dahdi_transcoder *tc) |
| 102 |
{ |
| 103 |
spin_lock(&translock); |
| 104 |
BUG_ON(is_on_list(&tc->registration_list_node, ®istration_list)); |
| 105 |
list_add_tail(&tc->registration_list_node, ®istration_list); |
| 106 |
list_add_tail(&tc->active_list_node, &active_list); |
| 107 |
spin_unlock(&translock); |
| 108 |
|
| 109 |
printk(KERN_INFO "%s: Registered codec translator '%s' " \ |
| 110 |
"with %d transcoders (srcs=%08x, dsts=%08x)\n", |
| 111 |
THIS_MODULE->name, tc->name, tc->numchannels, |
| 112 |
tc->srcfmts, tc->dstfmts); |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
/* Unregister a transcoder */ |
| 118 |
int dahdi_transcoder_unregister(struct dahdi_transcoder *tc) |
| 119 |
{ |
| 120 |
int res = -EINVAL; |
| 121 |
|
| 122 |
/* \todo Perhaps we should check to make sure there isn't a channel |
| 123 |
* that is still in use? */ |
| 124 |
|
| 125 |
spin_lock(&translock); |
| 126 |
if (!is_on_list(&tc->registration_list_node, ®istration_list)) { |
| 127 |
spin_unlock(&translock); |
| 128 |
printk(KERN_WARNING "%s: Failed to unregister %s, which is " \ |
| 129 |
"not currently registered.\n", THIS_MODULE->name, tc->name); |
| 130 |
return -EINVAL; |
| 131 |
} |
| 132 |
list_del_init(&tc->registration_list_node); |
| 133 |
list_del_init(&tc->active_list_node); |
| 134 |
spin_unlock(&translock); |
| 135 |
|
| 136 |
printk(KERN_INFO "Unregistered codec translator '%s' with %d " \ |
| 137 |
"transcoders (srcs=%08x, dsts=%08x)\n", |
| 138 |
tc->name, tc->numchannels, tc->srcfmts, tc->dstfmts); |
| 139 |
res = 0; |
| 140 |
|
| 141 |
return res; |
| 142 |
} |
| 143 |
|
| 144 |
/* Alert a transcoder */ |
| 145 |
int dahdi_transcoder_alert(struct dahdi_transcoder_channel *chan) |
| 146 |
{ |
| 147 |
wake_up_interruptible(&chan->ready); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
static int dahdi_tc_open(struct inode *inode, struct file *file) |
| 152 |
{ |
| 153 |
const struct file_operations *original_fops; |
| 154 |
BUG_ON(!dahdi_transcode_fops); |
| 155 |
original_fops = file->f_op; |
| 156 |
file->f_op = dahdi_transcode_fops; |
| 157 |
file->private_data = NULL; |
| 158 |
/* Under normal operation, this releases the reference on the DAHDI |
| 159 |
* module that was created when the file was opened. dahdi_open is |
| 160 |
* responsible for taking a reference out on this module before |
| 161 |
* calling this function. */ |
| 162 |
module_put(original_fops->owner); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
static void dtc_release(struct dahdi_transcoder_channel *chan) |
| 167 |
{ |
| 168 |
BUG_ON(!chan); |
| 169 |
if (chan->parent && chan->parent->release) { |
| 170 |
chan->parent->release(chan); |
| 171 |
} else { |
| 172 |
dahdi_tc_clear_busy(chan); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
static int dahdi_tc_release(struct inode *inode, struct file *file) |
| 177 |
{ |
| 178 |
struct dahdi_transcoder_channel *chan = file->private_data; |
| 179 |
/* There will not be a transcoder channel associated with this file if |
| 180 |
* the ALLOCATE ioctl never succeeded. |
| 181 |
*/ |
| 182 |
if (chan) { |
| 183 |
dtc_release(chan); |
| 184 |
} |
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
/* Find a free channel on the transcoder and mark it busy. */ |
| 189 |
static inline struct dahdi_transcoder_channel * |
| 190 |
get_free_channel(struct dahdi_transcoder *tc, |
| 191 |
const struct dahdi_transcoder_formats *fmts) |
| 192 |
{ |
| 193 |
struct dahdi_transcoder_channel *chan; |
| 194 |
int i; |
| 195 |
/* Should be called with the translock held. */ |
| 196 |
#ifdef CONFIG_SMP |
| 197 |
WARN_ON(!spin_is_locked(&translock)); |
| 198 |
#endif |
| 199 |
|
| 200 |
for (i = 0; i < tc->numchannels; i++) { |
| 201 |
chan = &tc->channels[i]; |
| 202 |
if (!dahdi_tc_is_busy(chan)) { |
| 203 |
if (!dahdi_tc_is_built(chan)) { |
| 204 |
dahdi_tc_set_busy(chan); |
| 205 |
return chan; |
| 206 |
} else { |
| 207 |
/* If the channel is already built, we must |
| 208 |
* make sure that it can support the formats |
| 209 |
* that we're interested in. */ |
| 210 |
if ((fmts->srcfmt|fmts->dstfmt) == chan->built_fmts) { |
| 211 |
dahdi_tc_set_busy(chan); |
| 212 |
return chan; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
return NULL; |
| 218 |
} |
| 219 |
|
| 220 |
/* Search the list for a transcoder that supports the specified format, and |
| 221 |
* allocate and return an available channel on it. |
| 222 |
* |
| 223 |
* Returns either a pointer to the allocated channel, -EBUSY if the format is |
| 224 |
* supported but all the channels are busy, or -ENODEV if there are not any |
| 225 |
* transcoders that support the formats. |
| 226 |
*/ |
| 227 |
static struct dahdi_transcoder_channel * |
| 228 |
__find_free_channel(struct list_head *list, const struct dahdi_transcoder_formats *fmts) |
| 229 |
{ |
| 230 |
struct dahdi_transcoder *tc; |
| 231 |
struct dahdi_transcoder_channel *chan = NULL; |
| 232 |
unsigned int match = 0; |
| 233 |
|
| 234 |
list_for_each_entry(tc, list, active_list_node) { |
| 235 |
if ((tc->dstfmts & fmts->dstfmt) && (tc->srcfmts & fmts->srcfmt)) { |
| 236 |
/* We found a transcoder that can handle our formats. |
| 237 |
* Now look for an available channel. */ |
| 238 |
match = 1; |
| 239 |
if ((chan = get_free_channel(tc, fmts))) { |
| 240 |
/* transcoder tc has a free channel. In order |
| 241 |
* to spread the load among available |
| 242 |
* transcoders (when there are more than one |
| 243 |
* transcoder in the system) we'll move tc |
| 244 |
* to the end of the list. */ |
| 245 |
list_move_tail(&tc->active_list_node, list); |
| 246 |
return chan; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
return (void*)((long)((match) ? -EBUSY : -ENODEV)); |
| 251 |
} |
| 252 |
|
| 253 |
static long dahdi_tc_allocate(struct file *file, unsigned long data) |
| 254 |
{ |
| 255 |
struct dahdi_transcoder_channel *chan = NULL; |
| 256 |
struct dahdi_transcoder_formats fmts; |
| 257 |
|
| 258 |
if (copy_from_user(&fmts, (__user const void *) data, sizeof(fmts))) { |
| 259 |
return -EFAULT; |
| 260 |
} |
| 261 |
|
| 262 |
spin_lock(&translock); |
| 263 |
chan = __find_free_channel(&active_list, &fmts); |
| 264 |
spin_unlock(&translock); |
| 265 |
|
| 266 |
if (IS_ERR(chan)) { |
| 267 |
return PTR_ERR(chan); |
| 268 |
} |
| 269 |
|
| 270 |
/* Every transcoder channel must be associated with a parent |
| 271 |
* transcoder. */ |
| 272 |
BUG_ON(!chan->parent); |
| 273 |
|
| 274 |
chan->srcfmt = fmts.srcfmt; |
| 275 |
chan->dstfmt = fmts.dstfmt; |
| 276 |
|
| 277 |
if (file->private_data) { |
| 278 |
/* This open file is moving to a new channel. Cleanup and |
| 279 |
* close the old channel here. */ |
| 280 |
dtc_release(file->private_data); |
| 281 |
} |
| 282 |
|
| 283 |
file->private_data = chan; |
| 284 |
if (chan->parent->fops.owner != file->f_op->owner) { |
| 285 |
if (!try_module_get(chan->parent->fops.owner)) { |
| 286 |
/* Failed to get a reference on the driver for the |
| 287 |
* actual transcoding hardware. */ |
| 288 |
return -EINVAL; |
| 289 |
} |
| 290 |
/* Release the reference on the existing driver. */ |
| 291 |
module_put(file->f_op->owner); |
| 292 |
file->f_op = &chan->parent->fops; |
| 293 |
} |
| 294 |
|
| 295 |
if (file->f_flags & O_NONBLOCK) { |
| 296 |
dahdi_tc_set_nonblock(chan); |
| 297 |
} else { |
| 298 |
dahdi_tc_clear_nonblock(chan); |
| 299 |
} |
| 300 |
|
| 301 |
/* Actually reset the transcoder channel */ |
| 302 |
if (chan->parent->allocate) |
| 303 |
return chan->parent->allocate(chan); |
| 304 |
|
| 305 |
return -EINVAL; |
| 306 |
} |
| 307 |
|
| 308 |
static long dahdi_tc_getinfo(unsigned long data) |
| 309 |
{ |
| 310 |
struct dahdi_transcoder_info info; |
| 311 |
struct dahdi_transcoder *cur; |
| 312 |
struct dahdi_transcoder *tc = NULL; |
| 313 |
unsigned int count = 0; |
| 314 |
|
| 315 |
if (copy_from_user(&info, (__user const void *) data, sizeof(info))) { |
| 316 |
return -EFAULT; |
| 317 |
} |
| 318 |
|
| 319 |
spin_lock(&translock); |
| 320 |
list_for_each_entry(cur, ®istration_list, registration_list_node) { |
| 321 |
if (info.tcnum == count++) { |
| 322 |
tc = cur; |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
spin_unlock(&translock); |
| 327 |
|
| 328 |
if (!tc) { |
| 329 |
return -ENOSYS; |
| 330 |
} |
| 331 |
|
| 332 |
strlcpy(info.name, tc->name, sizeof(info.name)); |
| 333 |
info.numchannels = tc->numchannels; |
| 334 |
info.srcfmts = tc->srcfmts; |
| 335 |
info.dstfmts = tc->dstfmts; |
| 336 |
|
| 337 |
return copy_to_user((__user void *) data, &info, sizeof(info)) ? -EFAULT : 0; |
| 338 |
} |
| 339 |
|
| 340 |
static ssize_t dahdi_tc_write(struct file *file, __user const char *usrbuf, size_t count, loff_t *ppos) |
| 341 |
{ |
| 342 |
if (file->private_data) { |
| 343 |
/* file->private_data will not be NULL if DAHDI_TC_ALLOCATE was |
| 344 |
* called, and therefore indicates that the transcoder driver |
| 345 |
* did not export a read function. */ |
| 346 |
WARN_ON(1); |
| 347 |
return -ENOSYS; |
| 348 |
} else { |
| 349 |
printk(KERN_INFO "%s: Attempt to write to unallocated " \ |
| 350 |
"channel.\n", THIS_MODULE->name); |
| 351 |
return -EINVAL; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
static ssize_t dahdi_tc_read(struct file *file, __user char *usrbuf, size_t count, loff_t *ppos) |
| 356 |
{ |
| 357 |
if (file->private_data) { |
| 358 |
/* file->private_data will not be NULL if DAHDI_TC_ALLOCATE was |
| 359 |
* called, and therefore indicates that the transcoder driver |
| 360 |
* did not export a write function. */ |
| 361 |
WARN_ON(1); |
| 362 |
return -ENOSYS; |
| 363 |
} else { |
| 364 |
printk(KERN_INFO "%s: Attempt to read from unallocated " \ |
| 365 |
"channel.\n", THIS_MODULE->name); |
| 366 |
return -EINVAL; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
static long dahdi_tc_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long data) |
| 371 |
{ |
| 372 |
switch (cmd) { |
| 373 |
case DAHDI_TC_ALLOCATE: |
| 374 |
return dahdi_tc_allocate(file, data); |
| 375 |
case DAHDI_TC_GETINFO: |
| 376 |
return dahdi_tc_getinfo(data); |
| 377 |
case DAHDI_TRANSCODE_OP: |
| 378 |
/* This is a deprecated call from the previous transcoder |
| 379 |
* interface, which was all routed through the dahdi_ioctl in |
| 380 |
* dahdi-base.c, and this ioctl request was used to indicate |
| 381 |
* that the call should be forwarded to this function. Now |
| 382 |
* when the file is opened, the f_ops pointer is updated to |
| 383 |
* point directly to this function, and we don't need a |
| 384 |
* general indication that the ioctl is destined for the |
| 385 |
* transcoder. |
| 386 |
* |
| 387 |
* I'm keeping this ioctl here in order to explain why there |
| 388 |
* might be a hole in the ioctl numbering scheme in the header |
| 389 |
* files. |
| 390 |
*/ |
| 391 |
printk(KERN_WARNING "%s: DAHDI_TRANSCODE_OP is no longer " \ |
| 392 |
"supported. Please call DAHDI_TC ioctls directly.\n", |
| 393 |
THIS_MODULE->name); |
| 394 |
return -EINVAL; |
| 395 |
default: |
| 396 |
return -EINVAL; |
| 397 |
}; |
| 398 |
} |
| 399 |
|
| 400 |
#ifndef HAVE_UNLOCKED_IOCTL |
| 401 |
static int dahdi_tc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long data) |
| 402 |
{ |
| 403 |
return (int)dahdi_tc_unlocked_ioctl(file, cmd, data); |
| 404 |
} |
| 405 |
#endif |
| 406 |
|
| 407 |
static unsigned int dahdi_tc_poll(struct file *file, struct poll_table_struct *wait_table) |
| 408 |
{ |
| 409 |
int ret; |
| 410 |
struct dahdi_transcoder_channel *chan = file->private_data; |
| 411 |
|
| 412 |
if (!chan) { |
| 413 |
/* This is because the DAHDI_TC_ALLOCATE ioctl was not called |
| 414 |
* before calling poll, which is invalid. */ |
| 415 |
return -EINVAL; |
| 416 |
} |
| 417 |
|
| 418 |
poll_wait(file, &chan->ready, wait_table); |
| 419 |
|
| 420 |
ret = dahdi_tc_is_busy(chan) ? 0 : POLLPRI; |
| 421 |
ret |= dahdi_tc_is_built(chan) ? POLLOUT : 0; |
| 422 |
ret |= dahdi_tc_is_data_waiting(chan) ? POLLIN : 0; |
| 423 |
return ret; |
| 424 |
} |
| 425 |
|
| 426 |
static struct file_operations __dahdi_transcode_fops = { |
| 427 |
.owner = THIS_MODULE, |
| 428 |
.open = dahdi_tc_open, |
| 429 |
.release = dahdi_tc_release, |
| 430 |
#ifdef HAVE_UNLOCKED_IOCTL |
| 431 |
.unlocked_ioctl = dahdi_tc_unlocked_ioctl, |
| 432 |
#else |
| 433 |
.ioctl = dahdi_tc_ioctl, |
| 434 |
#endif |
| 435 |
.read = dahdi_tc_read, |
| 436 |
.write = dahdi_tc_write, |
| 437 |
.poll = dahdi_tc_poll, |
| 438 |
}; |
| 439 |
|
| 440 |
static struct dahdi_chardev transcode_chardev = { |
| 441 |
.name = "transcode", |
| 442 |
.minor = DAHDI_TRANSCODE, |
| 443 |
}; |
| 444 |
|
| 445 |
static int dahdi_transcode_init(void) |
| 446 |
{ |
| 447 |
int res; |
| 448 |
|
| 449 |
if (dahdi_transcode_fops) { |
| 450 |
printk(KERN_WARNING "dahdi_transcode_fops already set.\n"); |
| 451 |
return -EBUSY; |
| 452 |
} |
| 453 |
|
| 454 |
dahdi_transcode_fops = &__dahdi_transcode_fops; |
| 455 |
|
| 456 |
if ((res = dahdi_register_chardev(&transcode_chardev))) |
| 457 |
return res; |
| 458 |
|
| 459 |
printk(KERN_INFO "%s: Loaded.\n", THIS_MODULE->name); |
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
static void dahdi_transcode_cleanup(void) |
| 464 |
{ |
| 465 |
dahdi_unregister_chardev(&transcode_chardev); |
| 466 |
|
| 467 |
dahdi_transcode_fops = NULL; |
| 468 |
|
| 469 |
printk(KERN_DEBUG "%s: Unloaded.\n", THIS_MODULE->name); |
| 470 |
} |
| 471 |
|
| 472 |
module_param(debug, int, S_IRUGO | S_IWUSR); |
| 473 |
MODULE_DESCRIPTION("DAHDI Transcoder Support"); |
| 474 |
MODULE_AUTHOR("Mark Spencer <[email protected]>"); |
| 475 |
#ifdef MODULE_LICENSE |
| 476 |
MODULE_LICENSE("GPL"); |
| 477 |
#endif |
| 478 |
|
| 479 |
module_init(dahdi_transcode_init); |
| 480 |
module_exit(dahdi_transcode_cleanup); |