dvbdev.c 12 KB

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