dvbdev.c 10 KB

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