dvbdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 "dvbdev.h"
  35. static DEFINE_MUTEX(dvbdev_mutex);
  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. mutex_lock(&dvbdev_mutex);
  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 *new_fops;
  66. new_fops = fops_get(dvbdev->fops);
  67. if (!new_fops)
  68. goto fail;
  69. file->private_data = dvbdev;
  70. replace_fops(file, new_fops);
  71. if (file->f_op->open)
  72. err = file->f_op->open(inode,file);
  73. up_read(&minor_rwsem);
  74. mutex_unlock(&dvbdev_mutex);
  75. return err;
  76. }
  77. fail:
  78. up_read(&minor_rwsem);
  79. mutex_unlock(&dvbdev_mutex);
  80. return -ENODEV;
  81. }
  82. static const struct file_operations dvb_device_fops =
  83. {
  84. .owner = THIS_MODULE,
  85. .open = dvb_device_open,
  86. .llseek = noop_llseek,
  87. };
  88. static struct cdev dvb_device_cdev;
  89. int dvb_generic_open(struct inode *inode, struct file *file)
  90. {
  91. struct dvb_device *dvbdev = file->private_data;
  92. if (!dvbdev)
  93. return -ENODEV;
  94. if (!dvbdev->users)
  95. return -EBUSY;
  96. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  97. if (!dvbdev->readers)
  98. return -EBUSY;
  99. dvbdev->readers--;
  100. } else {
  101. if (!dvbdev->writers)
  102. return -EBUSY;
  103. dvbdev->writers--;
  104. }
  105. dvbdev->users--;
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(dvb_generic_open);
  109. int dvb_generic_release(struct inode *inode, struct file *file)
  110. {
  111. struct dvb_device *dvbdev = file->private_data;
  112. if (!dvbdev)
  113. return -ENODEV;
  114. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  115. dvbdev->readers++;
  116. } else {
  117. dvbdev->writers++;
  118. }
  119. dvbdev->users++;
  120. return 0;
  121. }
  122. EXPORT_SYMBOL(dvb_generic_release);
  123. long dvb_generic_ioctl(struct file *file,
  124. unsigned int cmd, unsigned long arg)
  125. {
  126. struct dvb_device *dvbdev = file->private_data;
  127. if (!dvbdev)
  128. return -ENODEV;
  129. if (!dvbdev->kernel_ioctl)
  130. return -EINVAL;
  131. return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
  132. }
  133. EXPORT_SYMBOL(dvb_generic_ioctl);
  134. static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
  135. {
  136. u32 id = 0;
  137. while (id < DVB_MAX_IDS) {
  138. struct dvb_device *dev;
  139. list_for_each_entry(dev, &adap->device_list, list_head)
  140. if (dev->type == type && dev->id == id)
  141. goto skip;
  142. return id;
  143. skip:
  144. id++;
  145. }
  146. return -ENFILE;
  147. }
  148. int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
  149. const struct dvb_device *template, void *priv, int type)
  150. {
  151. struct dvb_device *dvbdev;
  152. struct file_operations *dvbdevfops;
  153. struct device *clsdev;
  154. int minor;
  155. int id;
  156. mutex_lock(&dvbdev_register_lock);
  157. if ((id = dvbdev_get_free_id (adap, type)) < 0){
  158. mutex_unlock(&dvbdev_register_lock);
  159. *pdvbdev = NULL;
  160. printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
  161. return -ENFILE;
  162. }
  163. *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
  164. if (!dvbdev){
  165. mutex_unlock(&dvbdev_register_lock);
  166. return -ENOMEM;
  167. }
  168. dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
  169. if (!dvbdevfops){
  170. kfree (dvbdev);
  171. mutex_unlock(&dvbdev_register_lock);
  172. return -ENOMEM;
  173. }
  174. memcpy(dvbdev, template, sizeof(struct dvb_device));
  175. dvbdev->type = type;
  176. dvbdev->id = id;
  177. dvbdev->adapter = adap;
  178. dvbdev->priv = priv;
  179. dvbdev->fops = dvbdevfops;
  180. init_waitqueue_head (&dvbdev->wait_queue);
  181. memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
  182. dvbdevfops->owner = adap->module;
  183. list_add_tail (&dvbdev->list_head, &adap->device_list);
  184. down_write(&minor_rwsem);
  185. #ifdef CONFIG_DVB_DYNAMIC_MINORS
  186. for (minor = 0; minor < MAX_DVB_MINORS; minor++)
  187. if (dvb_minors[minor] == NULL)
  188. break;
  189. if (minor == MAX_DVB_MINORS) {
  190. kfree(dvbdevfops);
  191. kfree(dvbdev);
  192. up_write(&minor_rwsem);
  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 file *file,
  301. unsigned int cmd, unsigned long arg,
  302. int (*func)(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(file, cmd, parg)) == -ENOIOCTLCMD)
  337. err = -ENOTTY;
  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 char *dvb_devnode(struct device *dev, umode_t *mode)
  362. {
  363. struct dvb_device *dvbdev = dev_get_drvdata(dev);
  364. return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
  365. dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
  366. }
  367. static int __init init_dvbdev(void)
  368. {
  369. int retval;
  370. dev_t dev = MKDEV(DVB_MAJOR, 0);
  371. if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
  372. printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
  373. return retval;
  374. }
  375. cdev_init(&dvb_device_cdev, &dvb_device_fops);
  376. if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
  377. printk(KERN_ERR "dvb-core: unable register character device\n");
  378. goto error;
  379. }
  380. dvb_class = class_create(THIS_MODULE, "dvb");
  381. if (IS_ERR(dvb_class)) {
  382. retval = PTR_ERR(dvb_class);
  383. goto error;
  384. }
  385. dvb_class->dev_uevent = dvb_uevent;
  386. dvb_class->devnode = dvb_devnode;
  387. return 0;
  388. error:
  389. cdev_del(&dvb_device_cdev);
  390. unregister_chrdev_region(dev, MAX_DVB_MINORS);
  391. return retval;
  392. }
  393. static void __exit exit_dvbdev(void)
  394. {
  395. class_destroy(dvb_class);
  396. cdev_del(&dvb_device_cdev);
  397. unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
  398. }
  399. subsys_initcall(init_dvbdev);
  400. module_exit(exit_dvbdev);
  401. MODULE_DESCRIPTION("DVB Core Driver");
  402. MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
  403. MODULE_LICENSE("GPL");