dvbdev.c 11 KB

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