dvbdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. mutex_unlock(&dvbdev_register_lock);
  179. memcpy(dvbdev, template, sizeof(struct dvb_device));
  180. dvbdev->type = type;
  181. dvbdev->id = id;
  182. dvbdev->adapter = adap;
  183. dvbdev->priv = priv;
  184. dvbdev->fops->owner = adap->module;
  185. list_add_tail (&dvbdev->list_head, &adap->device_list);
  186. devfs_mk_cdev(MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
  187. S_IFCHR | S_IRUSR | S_IWUSR,
  188. "dvb/adapter%d/%s%d", adap->num, dnames[type], id);
  189. class_device_create(dvb_class, NULL, MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
  190. NULL, "dvb%d.%s%d", adap->num, dnames[type], id);
  191. dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
  192. adap->num, dnames[type], id, nums2minor(adap->num, type, id),
  193. nums2minor(adap->num, type, id));
  194. return 0;
  195. }
  196. EXPORT_SYMBOL(dvb_register_device);
  197. void dvb_unregister_device(struct dvb_device *dvbdev)
  198. {
  199. if (!dvbdev)
  200. return;
  201. devfs_remove("dvb/adapter%d/%s%d", dvbdev->adapter->num,
  202. dnames[dvbdev->type], dvbdev->id);
  203. class_device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
  204. dvbdev->type, dvbdev->id)));
  205. list_del (&dvbdev->list_head);
  206. kfree (dvbdev);
  207. }
  208. EXPORT_SYMBOL(dvb_unregister_device);
  209. static int dvbdev_get_free_adapter_num (void)
  210. {
  211. int num = 0;
  212. while (num < DVB_MAX_ADAPTERS) {
  213. struct list_head *entry;
  214. list_for_each (entry, &dvb_adapter_list) {
  215. struct dvb_adapter *adap;
  216. adap = list_entry (entry, struct dvb_adapter, list_head);
  217. if (adap->num == num)
  218. goto skip;
  219. }
  220. return num;
  221. skip:
  222. num++;
  223. }
  224. return -ENFILE;
  225. }
  226. int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module)
  227. {
  228. int num;
  229. if (mutex_lock_interruptible(&dvbdev_register_lock))
  230. return -ERESTARTSYS;
  231. if ((num = dvbdev_get_free_adapter_num ()) < 0) {
  232. mutex_unlock(&dvbdev_register_lock);
  233. return -ENFILE;
  234. }
  235. memset (adap, 0, sizeof(struct dvb_adapter));
  236. INIT_LIST_HEAD (&adap->device_list);
  237. printk ("DVB: registering new adapter (%s).\n", name);
  238. devfs_mk_dir("dvb/adapter%d", num);
  239. adap->num = num;
  240. adap->name = name;
  241. adap->module = module;
  242. list_add_tail (&adap->list_head, &dvb_adapter_list);
  243. mutex_unlock(&dvbdev_register_lock);
  244. return num;
  245. }
  246. EXPORT_SYMBOL(dvb_register_adapter);
  247. int dvb_unregister_adapter(struct dvb_adapter *adap)
  248. {
  249. devfs_remove("dvb/adapter%d", adap->num);
  250. if (mutex_lock_interruptible(&dvbdev_register_lock))
  251. return -ERESTARTSYS;
  252. list_del (&adap->list_head);
  253. mutex_unlock(&dvbdev_register_lock);
  254. return 0;
  255. }
  256. EXPORT_SYMBOL(dvb_unregister_adapter);
  257. /* if the miracle happens and "generic_usercopy()" is included into
  258. the kernel, then this can vanish. please don't make the mistake and
  259. define this as video_usercopy(). this will introduce a dependecy
  260. to the v4l "videodev.o" module, which is unnecessary for some
  261. cards (ie. the budget dvb-cards don't need the v4l module...) */
  262. int dvb_usercopy(struct inode *inode, struct file *file,
  263. unsigned int cmd, unsigned long arg,
  264. int (*func)(struct inode *inode, struct file *file,
  265. unsigned int cmd, void *arg))
  266. {
  267. char sbuf[128];
  268. void *mbuf = NULL;
  269. void *parg = NULL;
  270. int err = -EINVAL;
  271. /* Copy arguments into temp kernel buffer */
  272. switch (_IOC_DIR(cmd)) {
  273. case _IOC_NONE:
  274. /*
  275. * For this command, the pointer is actually an integer
  276. * argument.
  277. */
  278. parg = (void *) arg;
  279. break;
  280. case _IOC_READ: /* some v4l ioctls are marked wrong ... */
  281. case _IOC_WRITE:
  282. case (_IOC_WRITE | _IOC_READ):
  283. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  284. parg = sbuf;
  285. } else {
  286. /* too big to allocate from stack */
  287. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  288. if (NULL == mbuf)
  289. return -ENOMEM;
  290. parg = mbuf;
  291. }
  292. err = -EFAULT;
  293. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  294. goto out;
  295. break;
  296. }
  297. /* call driver */
  298. if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
  299. err = -EINVAL;
  300. if (err < 0)
  301. goto out;
  302. /* Copy results into user buffer */
  303. switch (_IOC_DIR(cmd))
  304. {
  305. case _IOC_READ:
  306. case (_IOC_WRITE | _IOC_READ):
  307. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  308. err = -EFAULT;
  309. break;
  310. }
  311. out:
  312. kfree(mbuf);
  313. return err;
  314. }
  315. static int __init init_dvbdev(void)
  316. {
  317. int retval;
  318. dev_t dev = MKDEV(DVB_MAJOR, 0);
  319. if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
  320. printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
  321. return retval;
  322. }
  323. cdev_init(&dvb_device_cdev, &dvb_device_fops);
  324. if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
  325. printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
  326. goto error;
  327. }
  328. devfs_mk_dir("dvb");
  329. dvb_class = class_create(THIS_MODULE, "dvb");
  330. if (IS_ERR(dvb_class)) {
  331. retval = PTR_ERR(dvb_class);
  332. goto error;
  333. }
  334. return 0;
  335. error:
  336. cdev_del(&dvb_device_cdev);
  337. unregister_chrdev_region(dev, MAX_DVB_MINORS);
  338. return retval;
  339. }
  340. static void __exit exit_dvbdev(void)
  341. {
  342. devfs_remove("dvb");
  343. class_destroy(dvb_class);
  344. cdev_del(&dvb_device_cdev);
  345. unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
  346. }
  347. module_init(init_dvbdev);
  348. module_exit(exit_dvbdev);
  349. MODULE_DESCRIPTION("DVB Core Driver");
  350. MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
  351. MODULE_LICENSE("GPL");