dvbdev.c 11 KB

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