dvbdev.c 10 KB

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