dvbdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * dvbdev.c
  3. *
  4. * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
  5. * & Marcus Metzler <marcus@convergence.de>
  6. * for convergence integrated media GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. */
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/string.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. #include <linux/device.h>
  32. #include <linux/fs.h>
  33. #include <linux/cdev.h>
  34. #include <linux/mutex.h>
  35. #include "dvbdev.h"
  36. static int dvbdev_debug;
  37. module_param(dvbdev_debug, int, 0644);
  38. MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
  39. #define dprintk if (dvbdev_debug) printk
  40. static LIST_HEAD(dvb_adapter_list);
  41. static DEFINE_MUTEX(dvbdev_register_lock);
  42. static const char * const dnames[] = {
  43. "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
  44. "net", "osd"
  45. };
  46. #define DVB_MAX_ADAPTERS 8
  47. #define DVB_MAX_IDS 4
  48. #define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
  49. #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
  50. static struct class *dvb_class;
  51. static struct dvb_device* dvbdev_find_device (int minor)
  52. {
  53. struct list_head *entry;
  54. list_for_each (entry, &dvb_adapter_list) {
  55. struct list_head *entry0;
  56. struct dvb_adapter *adap;
  57. adap = list_entry (entry, struct dvb_adapter, list_head);
  58. list_for_each (entry0, &adap->device_list) {
  59. struct dvb_device *dev;
  60. dev = list_entry (entry0, struct dvb_device, list_head);
  61. if (nums2minor(adap->num, dev->type, dev->id) == minor)
  62. return dev;
  63. }
  64. }
  65. return NULL;
  66. }
  67. static int dvb_device_open(struct inode *inode, struct file *file)
  68. {
  69. struct dvb_device *dvbdev;
  70. dvbdev = dvbdev_find_device (iminor(inode));
  71. if (dvbdev && dvbdev->fops) {
  72. int err = 0;
  73. const struct file_operations *old_fops;
  74. file->private_data = dvbdev;
  75. old_fops = file->f_op;
  76. file->f_op = fops_get(dvbdev->fops);
  77. if(file->f_op->open)
  78. err = file->f_op->open(inode,file);
  79. if (err) {
  80. fops_put(file->f_op);
  81. file->f_op = fops_get(old_fops);
  82. }
  83. fops_put(old_fops);
  84. return err;
  85. }
  86. return -ENODEV;
  87. }
  88. static struct file_operations dvb_device_fops =
  89. {
  90. .owner = THIS_MODULE,
  91. .open = dvb_device_open,
  92. };
  93. static struct cdev dvb_device_cdev = {
  94. .kobj = {.name = "dvb", },
  95. .owner = THIS_MODULE,
  96. };
  97. int dvb_generic_open(struct inode *inode, struct file *file)
  98. {
  99. struct dvb_device *dvbdev = file->private_data;
  100. if (!dvbdev)
  101. return -ENODEV;
  102. if (!dvbdev->users)
  103. return -EBUSY;
  104. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  105. if (!dvbdev->readers)
  106. return -EBUSY;
  107. dvbdev->readers--;
  108. } else {
  109. if (!dvbdev->writers)
  110. return -EBUSY;
  111. dvbdev->writers--;
  112. }
  113. dvbdev->users--;
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(dvb_generic_open);
  117. int dvb_generic_release(struct inode *inode, struct file *file)
  118. {
  119. struct dvb_device *dvbdev = file->private_data;
  120. if (!dvbdev)
  121. return -ENODEV;
  122. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  123. dvbdev->readers++;
  124. } else {
  125. dvbdev->writers++;
  126. }
  127. dvbdev->users++;
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(dvb_generic_release);
  131. int dvb_generic_ioctl(struct inode *inode, struct file *file,
  132. unsigned int cmd, unsigned long arg)
  133. {
  134. struct dvb_device *dvbdev = file->private_data;
  135. if (!dvbdev)
  136. return -ENODEV;
  137. if (!dvbdev->kernel_ioctl)
  138. return -EINVAL;
  139. return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
  140. }
  141. EXPORT_SYMBOL(dvb_generic_ioctl);
  142. static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
  143. {
  144. u32 id = 0;
  145. while (id < DVB_MAX_IDS) {
  146. struct list_head *entry;
  147. list_for_each (entry, &adap->device_list) {
  148. struct dvb_device *dev;
  149. dev = list_entry (entry, struct dvb_device, list_head);
  150. if (dev->type == type && dev->id == id)
  151. goto skip;
  152. }
  153. return id;
  154. skip:
  155. id++;
  156. }
  157. return -ENFILE;
  158. }
  159. int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
  160. const struct dvb_device *template, void *priv, int type)
  161. {
  162. struct dvb_device *dvbdev;
  163. struct file_operations *dvbdevfops;
  164. struct device *clsdev;
  165. int id;
  166. mutex_lock(&dvbdev_register_lock);
  167. if ((id = dvbdev_get_free_id (adap, type)) < 0){
  168. mutex_unlock(&dvbdev_register_lock);
  169. *pdvbdev = NULL;
  170. printk(KERN_ERR "%s: couldn't find free device id\n", __FUNCTION__);
  171. return -ENFILE;
  172. }
  173. *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
  174. if (!dvbdev){
  175. mutex_unlock(&dvbdev_register_lock);
  176. return -ENOMEM;
  177. }
  178. dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
  179. if (!dvbdevfops){
  180. kfree (dvbdev);
  181. mutex_unlock(&dvbdev_register_lock);
  182. return -ENOMEM;
  183. }
  184. memcpy(dvbdev, template, sizeof(struct dvb_device));
  185. dvbdev->type = type;
  186. dvbdev->id = id;
  187. dvbdev->adapter = adap;
  188. dvbdev->priv = priv;
  189. dvbdev->fops = dvbdevfops;
  190. init_waitqueue_head (&dvbdev->wait_queue);
  191. memcpy(dvbdev->fops, template->fops, sizeof(struct file_operations));
  192. dvbdev->fops->owner = adap->module;
  193. list_add_tail (&dvbdev->list_head, &adap->device_list);
  194. mutex_unlock(&dvbdev_register_lock);
  195. clsdev = device_create(dvb_class, adap->device,
  196. MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
  197. "dvb%d.%s%d", adap->num, dnames[type], id);
  198. if (IS_ERR(clsdev)) {
  199. printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
  200. __FUNCTION__, adap->num, dnames[type], id, PTR_ERR(clsdev));
  201. return PTR_ERR(clsdev);
  202. }
  203. dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
  204. adap->num, dnames[type], id, nums2minor(adap->num, type, id),
  205. nums2minor(adap->num, type, id));
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(dvb_register_device);
  209. void dvb_unregister_device(struct dvb_device *dvbdev)
  210. {
  211. if (!dvbdev)
  212. return;
  213. device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
  214. dvbdev->type, dvbdev->id)));
  215. list_del (&dvbdev->list_head);
  216. kfree (dvbdev->fops);
  217. kfree (dvbdev);
  218. }
  219. EXPORT_SYMBOL(dvb_unregister_device);
  220. static int dvbdev_get_free_adapter_num (void)
  221. {
  222. int num = 0;
  223. while (num < DVB_MAX_ADAPTERS) {
  224. struct list_head *entry;
  225. list_for_each (entry, &dvb_adapter_list) {
  226. struct dvb_adapter *adap;
  227. adap = list_entry (entry, struct dvb_adapter, list_head);
  228. if (adap->num == num)
  229. goto skip;
  230. }
  231. return num;
  232. skip:
  233. num++;
  234. }
  235. return -ENFILE;
  236. }
  237. int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
  238. {
  239. int num;
  240. mutex_lock(&dvbdev_register_lock);
  241. if ((num = dvbdev_get_free_adapter_num ()) < 0) {
  242. mutex_unlock(&dvbdev_register_lock);
  243. return -ENFILE;
  244. }
  245. memset (adap, 0, sizeof(struct dvb_adapter));
  246. INIT_LIST_HEAD (&adap->device_list);
  247. printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
  248. adap->num = num;
  249. adap->name = name;
  250. adap->module = module;
  251. adap->device = device;
  252. list_add_tail (&adap->list_head, &dvb_adapter_list);
  253. mutex_unlock(&dvbdev_register_lock);
  254. return num;
  255. }
  256. EXPORT_SYMBOL(dvb_register_adapter);
  257. int dvb_unregister_adapter(struct dvb_adapter *adap)
  258. {
  259. mutex_lock(&dvbdev_register_lock);
  260. list_del (&adap->list_head);
  261. mutex_unlock(&dvbdev_register_lock);
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(dvb_unregister_adapter);
  265. /* if the miracle happens and "generic_usercopy()" is included into
  266. the kernel, then this can vanish. please don't make the mistake and
  267. define this as video_usercopy(). this will introduce a dependecy
  268. to the v4l "videodev.o" module, which is unnecessary for some
  269. cards (ie. the budget dvb-cards don't need the v4l module...) */
  270. int dvb_usercopy(struct inode *inode, struct file *file,
  271. unsigned int cmd, unsigned long arg,
  272. int (*func)(struct inode *inode, struct file *file,
  273. unsigned int cmd, void *arg))
  274. {
  275. char sbuf[128];
  276. void *mbuf = NULL;
  277. void *parg = NULL;
  278. int err = -EINVAL;
  279. /* Copy arguments into temp kernel buffer */
  280. switch (_IOC_DIR(cmd)) {
  281. case _IOC_NONE:
  282. /*
  283. * For this command, the pointer is actually an integer
  284. * argument.
  285. */
  286. parg = (void *) arg;
  287. break;
  288. case _IOC_READ: /* some v4l ioctls are marked wrong ... */
  289. case _IOC_WRITE:
  290. case (_IOC_WRITE | _IOC_READ):
  291. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  292. parg = sbuf;
  293. } else {
  294. /* too big to allocate from stack */
  295. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  296. if (NULL == mbuf)
  297. return -ENOMEM;
  298. parg = mbuf;
  299. }
  300. err = -EFAULT;
  301. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  302. goto out;
  303. break;
  304. }
  305. /* call driver */
  306. if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
  307. err = -EINVAL;
  308. if (err < 0)
  309. goto out;
  310. /* Copy results into user buffer */
  311. switch (_IOC_DIR(cmd))
  312. {
  313. case _IOC_READ:
  314. case (_IOC_WRITE | _IOC_READ):
  315. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  316. err = -EFAULT;
  317. break;
  318. }
  319. out:
  320. kfree(mbuf);
  321. return err;
  322. }
  323. static int __init init_dvbdev(void)
  324. {
  325. int retval;
  326. dev_t dev = MKDEV(DVB_MAJOR, 0);
  327. if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
  328. printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
  329. return retval;
  330. }
  331. cdev_init(&dvb_device_cdev, &dvb_device_fops);
  332. if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
  333. printk(KERN_ERR "dvb-core: unable register character device\n");
  334. goto error;
  335. }
  336. dvb_class = class_create(THIS_MODULE, "dvb");
  337. if (IS_ERR(dvb_class)) {
  338. retval = PTR_ERR(dvb_class);
  339. goto error;
  340. }
  341. return 0;
  342. error:
  343. cdev_del(&dvb_device_cdev);
  344. unregister_chrdev_region(dev, MAX_DVB_MINORS);
  345. return retval;
  346. }
  347. static void __exit exit_dvbdev(void)
  348. {
  349. class_destroy(dvb_class);
  350. cdev_del(&dvb_device_cdev);
  351. unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
  352. }
  353. subsys_initcall(init_dvbdev);
  354. module_exit(exit_dvbdev);
  355. MODULE_DESCRIPTION("DVB Core Driver");
  356. MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
  357. MODULE_LICENSE("GPL");