dvbdev.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/sched.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/device.h>
  33. #include <linux/fs.h>
  34. #include <linux/cdev.h>
  35. #include <linux/mutex.h>
  36. #include "dvbdev.h"
  37. static int dvbdev_debug;
  38. module_param(dvbdev_debug, int, 0644);
  39. MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
  40. #define dprintk if (dvbdev_debug) printk
  41. static LIST_HEAD(dvb_adapter_list);
  42. static DEFINE_MUTEX(dvbdev_register_lock);
  43. static const char * const dnames[] = {
  44. "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
  45. "net", "osd"
  46. };
  47. #define DVB_MAX_ADAPTERS 8
  48. #define DVB_MAX_IDS 4
  49. #define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
  50. #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
  51. static struct class *dvb_class;
  52. static struct dvb_device* dvbdev_find_device (int minor)
  53. {
  54. struct list_head *entry;
  55. list_for_each (entry, &dvb_adapter_list) {
  56. struct list_head *entry0;
  57. struct dvb_adapter *adap;
  58. adap = list_entry (entry, struct dvb_adapter, list_head);
  59. list_for_each (entry0, &adap->device_list) {
  60. struct dvb_device *dev;
  61. dev = list_entry (entry0, struct dvb_device, list_head);
  62. if (nums2minor(adap->num, dev->type, dev->id) == minor)
  63. return dev;
  64. }
  65. }
  66. return NULL;
  67. }
  68. static int dvb_device_open(struct inode *inode, struct file *file)
  69. {
  70. struct dvb_device *dvbdev;
  71. dvbdev = dvbdev_find_device (iminor(inode));
  72. if (dvbdev && dvbdev->fops) {
  73. int err = 0;
  74. const struct file_operations *old_fops;
  75. file->private_data = dvbdev;
  76. old_fops = file->f_op;
  77. file->f_op = fops_get(dvbdev->fops);
  78. if(file->f_op->open)
  79. err = file->f_op->open(inode,file);
  80. if (err) {
  81. fops_put(file->f_op);
  82. file->f_op = fops_get(old_fops);
  83. }
  84. fops_put(old_fops);
  85. return err;
  86. }
  87. return -ENODEV;
  88. }
  89. static struct file_operations dvb_device_fops =
  90. {
  91. .owner = THIS_MODULE,
  92. .open = dvb_device_open,
  93. };
  94. static struct cdev dvb_device_cdev = {
  95. .kobj = {.name = "dvb", },
  96. .owner = THIS_MODULE,
  97. };
  98. int dvb_generic_open(struct inode *inode, struct file *file)
  99. {
  100. struct dvb_device *dvbdev = file->private_data;
  101. if (!dvbdev)
  102. return -ENODEV;
  103. if (!dvbdev->users)
  104. return -EBUSY;
  105. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  106. if (!dvbdev->readers)
  107. return -EBUSY;
  108. dvbdev->readers--;
  109. } else {
  110. if (!dvbdev->writers)
  111. return -EBUSY;
  112. dvbdev->writers--;
  113. }
  114. dvbdev->users--;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(dvb_generic_open);
  118. int dvb_generic_release(struct inode *inode, struct file *file)
  119. {
  120. struct dvb_device *dvbdev = file->private_data;
  121. if (!dvbdev)
  122. return -ENODEV;
  123. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  124. dvbdev->readers++;
  125. } else {
  126. dvbdev->writers++;
  127. }
  128. dvbdev->users++;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(dvb_generic_release);
  132. int dvb_generic_ioctl(struct inode *inode, struct file *file,
  133. unsigned int cmd, unsigned long arg)
  134. {
  135. struct dvb_device *dvbdev = file->private_data;
  136. if (!dvbdev)
  137. return -ENODEV;
  138. if (!dvbdev->kernel_ioctl)
  139. return -EINVAL;
  140. return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
  141. }
  142. EXPORT_SYMBOL(dvb_generic_ioctl);
  143. static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
  144. {
  145. u32 id = 0;
  146. while (id < DVB_MAX_IDS) {
  147. struct list_head *entry;
  148. list_for_each (entry, &adap->device_list) {
  149. struct dvb_device *dev;
  150. dev = list_entry (entry, struct dvb_device, list_head);
  151. if (dev->type == type && dev->id == id)
  152. goto skip;
  153. }
  154. return id;
  155. skip:
  156. id++;
  157. }
  158. return -ENFILE;
  159. }
  160. int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
  161. const struct dvb_device *template, void *priv, int type)
  162. {
  163. struct dvb_device *dvbdev;
  164. int id;
  165. if (mutex_lock_interruptible(&dvbdev_register_lock))
  166. return -ERESTARTSYS;
  167. if ((id = dvbdev_get_free_id (adap, type)) < 0) {
  168. mutex_unlock(&dvbdev_register_lock);
  169. *pdvbdev = NULL;
  170. printk ("%s: could get 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. memcpy(dvbdev, template, sizeof(struct dvb_device));
  179. dvbdev->type = type;
  180. dvbdev->id = id;
  181. dvbdev->adapter = adap;
  182. dvbdev->priv = priv;
  183. dvbdev->fops->owner = adap->module;
  184. list_add_tail (&dvbdev->list_head, &adap->device_list);
  185. mutex_unlock(&dvbdev_register_lock);
  186. class_device_create(dvb_class, NULL, MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
  187. adap->device, "dvb%d.%s%d", adap->num, dnames[type], id);
  188. dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
  189. adap->num, dnames[type], id, nums2minor(adap->num, type, id),
  190. nums2minor(adap->num, type, id));
  191. return 0;
  192. }
  193. EXPORT_SYMBOL(dvb_register_device);
  194. void dvb_unregister_device(struct dvb_device *dvbdev)
  195. {
  196. if (!dvbdev)
  197. return;
  198. class_device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
  199. dvbdev->type, dvbdev->id)));
  200. list_del (&dvbdev->list_head);
  201. kfree (dvbdev);
  202. }
  203. EXPORT_SYMBOL(dvb_unregister_device);
  204. static int dvbdev_get_free_adapter_num (void)
  205. {
  206. int num = 0;
  207. while (num < DVB_MAX_ADAPTERS) {
  208. struct list_head *entry;
  209. list_for_each (entry, &dvb_adapter_list) {
  210. struct dvb_adapter *adap;
  211. adap = list_entry (entry, struct dvb_adapter, list_head);
  212. if (adap->num == num)
  213. goto skip;
  214. }
  215. return num;
  216. skip:
  217. num++;
  218. }
  219. return -ENFILE;
  220. }
  221. int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
  222. {
  223. int num;
  224. if (mutex_lock_interruptible(&dvbdev_register_lock))
  225. return -ERESTARTSYS;
  226. if ((num = dvbdev_get_free_adapter_num ()) < 0) {
  227. mutex_unlock(&dvbdev_register_lock);
  228. return -ENFILE;
  229. }
  230. memset (adap, 0, sizeof(struct dvb_adapter));
  231. INIT_LIST_HEAD (&adap->device_list);
  232. printk ("DVB: registering new adapter (%s).\n", name);
  233. adap->num = num;
  234. adap->name = name;
  235. adap->module = module;
  236. adap->device = device;
  237. list_add_tail (&adap->list_head, &dvb_adapter_list);
  238. mutex_unlock(&dvbdev_register_lock);
  239. return num;
  240. }
  241. EXPORT_SYMBOL(dvb_register_adapter);
  242. int dvb_unregister_adapter(struct dvb_adapter *adap)
  243. {
  244. if (mutex_lock_interruptible(&dvbdev_register_lock))
  245. return -ERESTARTSYS;
  246. list_del (&adap->list_head);
  247. mutex_unlock(&dvbdev_register_lock);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(dvb_unregister_adapter);
  251. /* if the miracle happens and "generic_usercopy()" is included into
  252. the kernel, then this can vanish. please don't make the mistake and
  253. define this as video_usercopy(). this will introduce a dependecy
  254. to the v4l "videodev.o" module, which is unnecessary for some
  255. cards (ie. the budget dvb-cards don't need the v4l module...) */
  256. int dvb_usercopy(struct inode *inode, struct file *file,
  257. unsigned int cmd, unsigned long arg,
  258. int (*func)(struct inode *inode, struct file *file,
  259. unsigned int cmd, void *arg))
  260. {
  261. char sbuf[128];
  262. void *mbuf = NULL;
  263. void *parg = NULL;
  264. int err = -EINVAL;
  265. /* Copy arguments into temp kernel buffer */
  266. switch (_IOC_DIR(cmd)) {
  267. case _IOC_NONE:
  268. /*
  269. * For this command, the pointer is actually an integer
  270. * argument.
  271. */
  272. parg = (void *) arg;
  273. break;
  274. case _IOC_READ: /* some v4l ioctls are marked wrong ... */
  275. case _IOC_WRITE:
  276. case (_IOC_WRITE | _IOC_READ):
  277. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  278. parg = sbuf;
  279. } else {
  280. /* too big to allocate from stack */
  281. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  282. if (NULL == mbuf)
  283. return -ENOMEM;
  284. parg = mbuf;
  285. }
  286. err = -EFAULT;
  287. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  288. goto out;
  289. break;
  290. }
  291. /* call driver */
  292. if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
  293. err = -EINVAL;
  294. if (err < 0)
  295. goto out;
  296. /* Copy results into user buffer */
  297. switch (_IOC_DIR(cmd))
  298. {
  299. case _IOC_READ:
  300. case (_IOC_WRITE | _IOC_READ):
  301. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  302. err = -EFAULT;
  303. break;
  304. }
  305. out:
  306. kfree(mbuf);
  307. return err;
  308. }
  309. static int __init init_dvbdev(void)
  310. {
  311. int retval;
  312. dev_t dev = MKDEV(DVB_MAJOR, 0);
  313. if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
  314. printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
  315. return retval;
  316. }
  317. cdev_init(&dvb_device_cdev, &dvb_device_fops);
  318. if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
  319. printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
  320. goto error;
  321. }
  322. dvb_class = class_create(THIS_MODULE, "dvb");
  323. if (IS_ERR(dvb_class)) {
  324. retval = PTR_ERR(dvb_class);
  325. goto error;
  326. }
  327. return 0;
  328. error:
  329. cdev_del(&dvb_device_cdev);
  330. unregister_chrdev_region(dev, MAX_DVB_MINORS);
  331. return retval;
  332. }
  333. static void __exit exit_dvbdev(void)
  334. {
  335. class_destroy(dvb_class);
  336. cdev_del(&dvb_device_cdev);
  337. unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
  338. }
  339. module_init(init_dvbdev);
  340. module_exit(exit_dvbdev);
  341. MODULE_DESCRIPTION("DVB Core Driver");
  342. MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
  343. MODULE_LICENSE("GPL");