dvbdev.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. */
  19. #define pr_fmt(fmt) "dvbdev: " fmt
  20. #include <linux/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/string.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/i2c.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/device.h>
  29. #include <linux/fs.h>
  30. #include <linux/cdev.h>
  31. #include <linux/mutex.h>
  32. #include <media/dvbdev.h>
  33. /* Due to enum tuner_pad_index */
  34. #include <media/tuner.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(fmt, arg...) do { \
  40. if (dvbdev_debug) \
  41. printk(KERN_DEBUG pr_fmt("%s: " fmt), \
  42. __func__, ##arg); \
  43. } while (0)
  44. static LIST_HEAD(dvb_adapter_list);
  45. static DEFINE_MUTEX(dvbdev_register_lock);
  46. static const char * const dnames[] = {
  47. [DVB_DEVICE_VIDEO] = "video",
  48. [DVB_DEVICE_AUDIO] = "audio",
  49. [DVB_DEVICE_SEC] = "sec",
  50. [DVB_DEVICE_FRONTEND] = "frontend",
  51. [DVB_DEVICE_DEMUX] = "demux",
  52. [DVB_DEVICE_DVR] = "dvr",
  53. [DVB_DEVICE_CA] = "ca",
  54. [DVB_DEVICE_NET] = "net",
  55. [DVB_DEVICE_OSD] = "osd"
  56. };
  57. #ifdef CONFIG_DVB_DYNAMIC_MINORS
  58. #define MAX_DVB_MINORS 256
  59. #define DVB_MAX_IDS MAX_DVB_MINORS
  60. #else
  61. #define DVB_MAX_IDS 4
  62. static const u8 minor_type[] = {
  63. [DVB_DEVICE_VIDEO] = 0,
  64. [DVB_DEVICE_AUDIO] = 1,
  65. [DVB_DEVICE_SEC] = 2,
  66. [DVB_DEVICE_FRONTEND] = 3,
  67. [DVB_DEVICE_DEMUX] = 4,
  68. [DVB_DEVICE_DVR] = 5,
  69. [DVB_DEVICE_CA] = 6,
  70. [DVB_DEVICE_NET] = 7,
  71. [DVB_DEVICE_OSD] = 8,
  72. };
  73. #define nums2minor(num, type, id) \
  74. (((num) << 6) | ((id) << 4) | minor_type[type])
  75. #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
  76. #endif
  77. static struct class *dvb_class;
  78. static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
  79. static DECLARE_RWSEM(minor_rwsem);
  80. static int dvb_device_open(struct inode *inode, struct file *file)
  81. {
  82. struct dvb_device *dvbdev;
  83. mutex_lock(&dvbdev_mutex);
  84. down_read(&minor_rwsem);
  85. dvbdev = dvb_minors[iminor(inode)];
  86. if (dvbdev && dvbdev->fops) {
  87. int err = 0;
  88. const struct file_operations *new_fops;
  89. new_fops = fops_get(dvbdev->fops);
  90. if (!new_fops)
  91. goto fail;
  92. file->private_data = dvbdev;
  93. replace_fops(file, new_fops);
  94. if (file->f_op->open)
  95. err = file->f_op->open(inode, file);
  96. up_read(&minor_rwsem);
  97. mutex_unlock(&dvbdev_mutex);
  98. return err;
  99. }
  100. fail:
  101. up_read(&minor_rwsem);
  102. mutex_unlock(&dvbdev_mutex);
  103. return -ENODEV;
  104. }
  105. static const struct file_operations dvb_device_fops =
  106. {
  107. .owner = THIS_MODULE,
  108. .open = dvb_device_open,
  109. .llseek = noop_llseek,
  110. };
  111. static struct cdev dvb_device_cdev;
  112. int dvb_generic_open(struct inode *inode, struct file *file)
  113. {
  114. struct dvb_device *dvbdev = file->private_data;
  115. if (!dvbdev)
  116. return -ENODEV;
  117. if (!dvbdev->users)
  118. return -EBUSY;
  119. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  120. if (!dvbdev->readers)
  121. return -EBUSY;
  122. dvbdev->readers--;
  123. } else {
  124. if (!dvbdev->writers)
  125. return -EBUSY;
  126. dvbdev->writers--;
  127. }
  128. dvbdev->users--;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(dvb_generic_open);
  132. int dvb_generic_release(struct inode *inode, struct file *file)
  133. {
  134. struct dvb_device *dvbdev = file->private_data;
  135. if (!dvbdev)
  136. return -ENODEV;
  137. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  138. dvbdev->readers++;
  139. } else {
  140. dvbdev->writers++;
  141. }
  142. dvbdev->users++;
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(dvb_generic_release);
  146. long dvb_generic_ioctl(struct file *file,
  147. unsigned int cmd, unsigned long arg)
  148. {
  149. struct dvb_device *dvbdev = file->private_data;
  150. if (!dvbdev)
  151. return -ENODEV;
  152. if (!dvbdev->kernel_ioctl)
  153. return -EINVAL;
  154. return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
  155. }
  156. EXPORT_SYMBOL(dvb_generic_ioctl);
  157. static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
  158. {
  159. u32 id = 0;
  160. while (id < DVB_MAX_IDS) {
  161. struct dvb_device *dev;
  162. list_for_each_entry(dev, &adap->device_list, list_head)
  163. if (dev->type == type && dev->id == id)
  164. goto skip;
  165. return id;
  166. skip:
  167. id++;
  168. }
  169. return -ENFILE;
  170. }
  171. static void dvb_media_device_free(struct dvb_device *dvbdev)
  172. {
  173. #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
  174. if (dvbdev->entity) {
  175. media_device_unregister_entity(dvbdev->entity);
  176. kfree(dvbdev->entity);
  177. kfree(dvbdev->pads);
  178. dvbdev->entity = NULL;
  179. dvbdev->pads = NULL;
  180. }
  181. if (dvbdev->tsout_entity) {
  182. int i;
  183. for (i = 0; i < dvbdev->tsout_num_entities; i++) {
  184. media_device_unregister_entity(&dvbdev->tsout_entity[i]);
  185. kfree(dvbdev->tsout_entity[i].name);
  186. }
  187. kfree(dvbdev->tsout_entity);
  188. kfree(dvbdev->tsout_pads);
  189. dvbdev->tsout_entity = NULL;
  190. dvbdev->tsout_pads = NULL;
  191. dvbdev->tsout_num_entities = 0;
  192. }
  193. if (dvbdev->intf_devnode) {
  194. media_devnode_remove(dvbdev->intf_devnode);
  195. dvbdev->intf_devnode = NULL;
  196. }
  197. if (dvbdev->adapter->conn) {
  198. media_device_unregister_entity(dvbdev->adapter->conn);
  199. dvbdev->adapter->conn = NULL;
  200. kfree(dvbdev->adapter->conn_pads);
  201. dvbdev->adapter->conn_pads = NULL;
  202. }
  203. #endif
  204. }
  205. #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
  206. static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
  207. const char *name, int npads)
  208. {
  209. int i, ret = 0;
  210. dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
  211. GFP_KERNEL);
  212. if (!dvbdev->tsout_pads)
  213. return -ENOMEM;
  214. dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
  215. GFP_KERNEL);
  216. if (!dvbdev->tsout_entity)
  217. return -ENOMEM;
  218. dvbdev->tsout_num_entities = npads;
  219. for (i = 0; i < npads; i++) {
  220. struct media_pad *pads = &dvbdev->tsout_pads[i];
  221. struct media_entity *entity = &dvbdev->tsout_entity[i];
  222. entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
  223. if (!entity->name)
  224. return -ENOMEM;
  225. entity->function = MEDIA_ENT_F_IO_DTV;
  226. pads->flags = MEDIA_PAD_FL_SINK;
  227. ret = media_entity_pads_init(entity, 1, pads);
  228. if (ret < 0)
  229. return ret;
  230. ret = media_device_register_entity(dvbdev->adapter->mdev,
  231. entity);
  232. if (ret < 0)
  233. return ret;
  234. }
  235. return 0;
  236. }
  237. #define DEMUX_TSOUT "demux-tsout"
  238. #define DVR_TSOUT "dvr-tsout"
  239. static int dvb_create_media_entity(struct dvb_device *dvbdev,
  240. int type, int demux_sink_pads)
  241. {
  242. int i, ret, npads;
  243. switch (type) {
  244. case DVB_DEVICE_FRONTEND:
  245. npads = 2;
  246. break;
  247. case DVB_DEVICE_DVR:
  248. ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
  249. demux_sink_pads);
  250. return ret;
  251. case DVB_DEVICE_DEMUX:
  252. npads = 1 + demux_sink_pads;
  253. ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
  254. demux_sink_pads);
  255. if (ret < 0)
  256. return ret;
  257. break;
  258. case DVB_DEVICE_CA:
  259. npads = 2;
  260. break;
  261. case DVB_DEVICE_NET:
  262. /*
  263. * We should be creating entities for the MPE/ULE
  264. * decapsulation hardware (or software implementation).
  265. *
  266. * However, the number of for the MPE/ULE decaps may not be
  267. * fixed. As we don't have yet dynamic support for PADs at
  268. * the Media Controller, let's not create the decap
  269. * entities yet.
  270. */
  271. return 0;
  272. default:
  273. return 0;
  274. }
  275. dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
  276. if (!dvbdev->entity)
  277. return -ENOMEM;
  278. dvbdev->entity->name = dvbdev->name;
  279. if (npads) {
  280. dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
  281. GFP_KERNEL);
  282. if (!dvbdev->pads) {
  283. kfree(dvbdev->entity);
  284. return -ENOMEM;
  285. }
  286. }
  287. switch (type) {
  288. case DVB_DEVICE_FRONTEND:
  289. dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
  290. dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
  291. dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
  292. break;
  293. case DVB_DEVICE_DEMUX:
  294. dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
  295. dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
  296. for (i = 1; i < npads; i++)
  297. dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
  298. break;
  299. case DVB_DEVICE_CA:
  300. dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
  301. dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
  302. dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
  303. break;
  304. default:
  305. /* Should never happen, as the first switch prevents it */
  306. kfree(dvbdev->entity);
  307. kfree(dvbdev->pads);
  308. dvbdev->entity = NULL;
  309. dvbdev->pads = NULL;
  310. return 0;
  311. }
  312. if (npads) {
  313. ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
  314. if (ret)
  315. return ret;
  316. }
  317. ret = media_device_register_entity(dvbdev->adapter->mdev,
  318. dvbdev->entity);
  319. if (ret)
  320. return ret;
  321. pr_info("%s: media entity '%s' registered.\n",
  322. __func__, dvbdev->entity->name);
  323. return 0;
  324. }
  325. #endif
  326. static int dvb_register_media_device(struct dvb_device *dvbdev,
  327. int type, int minor,
  328. unsigned demux_sink_pads)
  329. {
  330. #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
  331. struct media_link *link;
  332. u32 intf_type;
  333. int ret;
  334. if (!dvbdev->adapter->mdev)
  335. return 0;
  336. ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
  337. if (ret)
  338. return ret;
  339. switch (type) {
  340. case DVB_DEVICE_FRONTEND:
  341. intf_type = MEDIA_INTF_T_DVB_FE;
  342. break;
  343. case DVB_DEVICE_DEMUX:
  344. intf_type = MEDIA_INTF_T_DVB_DEMUX;
  345. break;
  346. case DVB_DEVICE_DVR:
  347. intf_type = MEDIA_INTF_T_DVB_DVR;
  348. break;
  349. case DVB_DEVICE_CA:
  350. intf_type = MEDIA_INTF_T_DVB_CA;
  351. break;
  352. case DVB_DEVICE_NET:
  353. intf_type = MEDIA_INTF_T_DVB_NET;
  354. break;
  355. default:
  356. return 0;
  357. }
  358. dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
  359. intf_type, 0,
  360. DVB_MAJOR, minor);
  361. if (!dvbdev->intf_devnode)
  362. return -ENOMEM;
  363. /*
  364. * Create the "obvious" link, e. g. the ones that represent
  365. * a direct association between an interface and an entity.
  366. * Other links should be created elsewhere, like:
  367. * DVB FE intf -> tuner
  368. * DVB demux intf -> dvr
  369. */
  370. if (!dvbdev->entity)
  371. return 0;
  372. link = media_create_intf_link(dvbdev->entity,
  373. &dvbdev->intf_devnode->intf,
  374. MEDIA_LNK_FL_ENABLED |
  375. MEDIA_LNK_FL_IMMUTABLE);
  376. if (!link)
  377. return -ENOMEM;
  378. #endif
  379. return 0;
  380. }
  381. int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
  382. const struct dvb_device *template, void *priv,
  383. enum dvb_device_type type, int demux_sink_pads)
  384. {
  385. struct dvb_device *dvbdev;
  386. struct file_operations *dvbdevfops;
  387. struct device *clsdev;
  388. int minor;
  389. int id, ret;
  390. mutex_lock(&dvbdev_register_lock);
  391. if ((id = dvbdev_get_free_id (adap, type)) < 0){
  392. mutex_unlock(&dvbdev_register_lock);
  393. *pdvbdev = NULL;
  394. pr_err("%s: couldn't find free device id\n", __func__);
  395. return -ENFILE;
  396. }
  397. *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
  398. if (!dvbdev){
  399. mutex_unlock(&dvbdev_register_lock);
  400. return -ENOMEM;
  401. }
  402. dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
  403. if (!dvbdevfops){
  404. kfree (dvbdev);
  405. mutex_unlock(&dvbdev_register_lock);
  406. return -ENOMEM;
  407. }
  408. memcpy(dvbdev, template, sizeof(struct dvb_device));
  409. dvbdev->type = type;
  410. dvbdev->id = id;
  411. dvbdev->adapter = adap;
  412. dvbdev->priv = priv;
  413. dvbdev->fops = dvbdevfops;
  414. init_waitqueue_head (&dvbdev->wait_queue);
  415. memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
  416. dvbdevfops->owner = adap->module;
  417. list_add_tail (&dvbdev->list_head, &adap->device_list);
  418. down_write(&minor_rwsem);
  419. #ifdef CONFIG_DVB_DYNAMIC_MINORS
  420. for (minor = 0; minor < MAX_DVB_MINORS; minor++)
  421. if (dvb_minors[minor] == NULL)
  422. break;
  423. if (minor == MAX_DVB_MINORS) {
  424. kfree(dvbdevfops);
  425. kfree(dvbdev);
  426. up_write(&minor_rwsem);
  427. mutex_unlock(&dvbdev_register_lock);
  428. return -EINVAL;
  429. }
  430. #else
  431. minor = nums2minor(adap->num, type, id);
  432. #endif
  433. dvbdev->minor = minor;
  434. dvb_minors[minor] = dvbdev;
  435. up_write(&minor_rwsem);
  436. ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
  437. if (ret) {
  438. pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
  439. __func__);
  440. dvb_media_device_free(dvbdev);
  441. kfree(dvbdevfops);
  442. kfree(dvbdev);
  443. up_write(&minor_rwsem);
  444. mutex_unlock(&dvbdev_register_lock);
  445. return ret;
  446. }
  447. mutex_unlock(&dvbdev_register_lock);
  448. clsdev = device_create(dvb_class, adap->device,
  449. MKDEV(DVB_MAJOR, minor),
  450. dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
  451. if (IS_ERR(clsdev)) {
  452. pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
  453. __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
  454. return PTR_ERR(clsdev);
  455. }
  456. dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
  457. adap->num, dnames[type], id, minor, minor);
  458. return 0;
  459. }
  460. EXPORT_SYMBOL(dvb_register_device);
  461. void dvb_remove_device(struct dvb_device *dvbdev)
  462. {
  463. if (!dvbdev)
  464. return;
  465. down_write(&minor_rwsem);
  466. dvb_minors[dvbdev->minor] = NULL;
  467. up_write(&minor_rwsem);
  468. dvb_media_device_free(dvbdev);
  469. device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
  470. list_del (&dvbdev->list_head);
  471. }
  472. EXPORT_SYMBOL(dvb_remove_device);
  473. void dvb_free_device(struct dvb_device *dvbdev)
  474. {
  475. if (!dvbdev)
  476. return;
  477. kfree (dvbdev->fops);
  478. kfree (dvbdev);
  479. }
  480. EXPORT_SYMBOL(dvb_free_device);
  481. void dvb_unregister_device(struct dvb_device *dvbdev)
  482. {
  483. dvb_remove_device(dvbdev);
  484. dvb_free_device(dvbdev);
  485. }
  486. EXPORT_SYMBOL(dvb_unregister_device);
  487. #ifdef CONFIG_MEDIA_CONTROLLER_DVB
  488. static int dvb_create_io_intf_links(struct dvb_adapter *adap,
  489. struct media_interface *intf,
  490. char *name)
  491. {
  492. struct media_device *mdev = adap->mdev;
  493. struct media_entity *entity;
  494. struct media_link *link;
  495. media_device_for_each_entity(entity, mdev) {
  496. if (entity->function == MEDIA_ENT_F_IO_DTV) {
  497. if (strncmp(entity->name, name, strlen(name)))
  498. continue;
  499. link = media_create_intf_link(entity, intf,
  500. MEDIA_LNK_FL_ENABLED |
  501. MEDIA_LNK_FL_IMMUTABLE);
  502. if (!link)
  503. return -ENOMEM;
  504. }
  505. }
  506. return 0;
  507. }
  508. int dvb_create_media_graph(struct dvb_adapter *adap,
  509. bool create_rf_connector)
  510. {
  511. struct media_device *mdev = adap->mdev;
  512. struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
  513. struct media_entity *demux = NULL, *ca = NULL;
  514. struct media_link *link;
  515. struct media_interface *intf;
  516. unsigned demux_pad = 0;
  517. unsigned dvr_pad = 0;
  518. unsigned ntuner = 0, ndemod = 0;
  519. int ret;
  520. static const char *connector_name = "Television";
  521. if (!mdev)
  522. return 0;
  523. media_device_for_each_entity(entity, mdev) {
  524. switch (entity->function) {
  525. case MEDIA_ENT_F_TUNER:
  526. tuner = entity;
  527. ntuner++;
  528. break;
  529. case MEDIA_ENT_F_DTV_DEMOD:
  530. demod = entity;
  531. ndemod++;
  532. break;
  533. case MEDIA_ENT_F_TS_DEMUX:
  534. demux = entity;
  535. break;
  536. case MEDIA_ENT_F_DTV_CA:
  537. ca = entity;
  538. break;
  539. }
  540. }
  541. /*
  542. * Prepare to signalize to media_create_pad_links() that multiple
  543. * entities of the same type exists and a 1:n or n:1 links need to be
  544. * created.
  545. * NOTE: if both tuner and demod have multiple instances, it is up
  546. * to the caller driver to create such links.
  547. */
  548. if (ntuner > 1)
  549. tuner = NULL;
  550. if (ndemod > 1)
  551. demod = NULL;
  552. if (create_rf_connector) {
  553. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  554. if (!conn)
  555. return -ENOMEM;
  556. adap->conn = conn;
  557. adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
  558. if (!adap->conn_pads)
  559. return -ENOMEM;
  560. conn->flags = MEDIA_ENT_FL_CONNECTOR;
  561. conn->function = MEDIA_ENT_F_CONN_RF;
  562. conn->name = connector_name;
  563. adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
  564. ret = media_entity_pads_init(conn, 1, adap->conn_pads);
  565. if (ret)
  566. return ret;
  567. ret = media_device_register_entity(mdev, conn);
  568. if (ret)
  569. return ret;
  570. if (!ntuner)
  571. ret = media_create_pad_links(mdev,
  572. MEDIA_ENT_F_CONN_RF,
  573. conn, 0,
  574. MEDIA_ENT_F_DTV_DEMOD,
  575. demod, 0,
  576. MEDIA_LNK_FL_ENABLED,
  577. false);
  578. else
  579. ret = media_create_pad_links(mdev,
  580. MEDIA_ENT_F_CONN_RF,
  581. conn, 0,
  582. MEDIA_ENT_F_TUNER,
  583. tuner, TUNER_PAD_RF_INPUT,
  584. MEDIA_LNK_FL_ENABLED,
  585. false);
  586. if (ret)
  587. return ret;
  588. }
  589. if (ntuner && ndemod) {
  590. ret = media_create_pad_links(mdev,
  591. MEDIA_ENT_F_TUNER,
  592. tuner, TUNER_PAD_OUTPUT,
  593. MEDIA_ENT_F_DTV_DEMOD,
  594. demod, 0, MEDIA_LNK_FL_ENABLED,
  595. false);
  596. if (ret)
  597. return ret;
  598. }
  599. if (ndemod && demux) {
  600. ret = media_create_pad_links(mdev,
  601. MEDIA_ENT_F_DTV_DEMOD,
  602. demod, 1,
  603. MEDIA_ENT_F_TS_DEMUX,
  604. demux, 0, MEDIA_LNK_FL_ENABLED,
  605. false);
  606. if (ret)
  607. return ret;
  608. }
  609. if (demux && ca) {
  610. ret = media_create_pad_link(demux, 1, ca,
  611. 0, MEDIA_LNK_FL_ENABLED);
  612. if (ret)
  613. return ret;
  614. }
  615. /* Create demux links for each ringbuffer/pad */
  616. if (demux) {
  617. media_device_for_each_entity(entity, mdev) {
  618. if (entity->function == MEDIA_ENT_F_IO_DTV) {
  619. if (!strncmp(entity->name, DVR_TSOUT,
  620. strlen(DVR_TSOUT))) {
  621. ret = media_create_pad_link(demux,
  622. ++dvr_pad,
  623. entity, 0, 0);
  624. if (ret)
  625. return ret;
  626. }
  627. if (!strncmp(entity->name, DEMUX_TSOUT,
  628. strlen(DEMUX_TSOUT))) {
  629. ret = media_create_pad_link(demux,
  630. ++demux_pad,
  631. entity, 0, 0);
  632. if (ret)
  633. return ret;
  634. }
  635. }
  636. }
  637. }
  638. /* Create interface links for FE->tuner, DVR->demux and CA->ca */
  639. media_device_for_each_intf(intf, mdev) {
  640. if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
  641. link = media_create_intf_link(ca, intf,
  642. MEDIA_LNK_FL_ENABLED |
  643. MEDIA_LNK_FL_IMMUTABLE);
  644. if (!link)
  645. return -ENOMEM;
  646. }
  647. if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
  648. link = media_create_intf_link(tuner, intf,
  649. MEDIA_LNK_FL_ENABLED |
  650. MEDIA_LNK_FL_IMMUTABLE);
  651. if (!link)
  652. return -ENOMEM;
  653. }
  654. #if 0
  655. /*
  656. * Indirect link - let's not create yet, as we don't know how
  657. * to handle indirect links, nor if this will
  658. * actually be needed.
  659. */
  660. if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
  661. link = media_create_intf_link(demux, intf,
  662. MEDIA_LNK_FL_ENABLED |
  663. MEDIA_LNK_FL_IMMUTABLE);
  664. if (!link)
  665. return -ENOMEM;
  666. }
  667. #endif
  668. if (intf->type == MEDIA_INTF_T_DVB_DVR) {
  669. ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
  670. if (ret)
  671. return ret;
  672. }
  673. if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
  674. ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
  675. if (ret)
  676. return ret;
  677. }
  678. }
  679. return 0;
  680. }
  681. EXPORT_SYMBOL_GPL(dvb_create_media_graph);
  682. #endif
  683. static int dvbdev_check_free_adapter_num(int num)
  684. {
  685. struct list_head *entry;
  686. list_for_each(entry, &dvb_adapter_list) {
  687. struct dvb_adapter *adap;
  688. adap = list_entry(entry, struct dvb_adapter, list_head);
  689. if (adap->num == num)
  690. return 0;
  691. }
  692. return 1;
  693. }
  694. static int dvbdev_get_free_adapter_num (void)
  695. {
  696. int num = 0;
  697. while (num < DVB_MAX_ADAPTERS) {
  698. if (dvbdev_check_free_adapter_num(num))
  699. return num;
  700. num++;
  701. }
  702. return -ENFILE;
  703. }
  704. int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
  705. struct module *module, struct device *device,
  706. short *adapter_nums)
  707. {
  708. int i, num;
  709. mutex_lock(&dvbdev_register_lock);
  710. for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
  711. num = adapter_nums[i];
  712. if (num >= 0 && num < DVB_MAX_ADAPTERS) {
  713. /* use the one the driver asked for */
  714. if (dvbdev_check_free_adapter_num(num))
  715. break;
  716. } else {
  717. num = dvbdev_get_free_adapter_num();
  718. break;
  719. }
  720. num = -1;
  721. }
  722. if (num < 0) {
  723. mutex_unlock(&dvbdev_register_lock);
  724. return -ENFILE;
  725. }
  726. memset (adap, 0, sizeof(struct dvb_adapter));
  727. INIT_LIST_HEAD (&adap->device_list);
  728. pr_info("DVB: registering new adapter (%s)\n", name);
  729. adap->num = num;
  730. adap->name = name;
  731. adap->module = module;
  732. adap->device = device;
  733. adap->mfe_shared = 0;
  734. adap->mfe_dvbdev = NULL;
  735. mutex_init (&adap->mfe_lock);
  736. #ifdef CONFIG_MEDIA_CONTROLLER_DVB
  737. mutex_init(&adap->mdev_lock);
  738. #endif
  739. list_add_tail (&adap->list_head, &dvb_adapter_list);
  740. mutex_unlock(&dvbdev_register_lock);
  741. return num;
  742. }
  743. EXPORT_SYMBOL(dvb_register_adapter);
  744. int dvb_unregister_adapter(struct dvb_adapter *adap)
  745. {
  746. mutex_lock(&dvbdev_register_lock);
  747. list_del (&adap->list_head);
  748. mutex_unlock(&dvbdev_register_lock);
  749. return 0;
  750. }
  751. EXPORT_SYMBOL(dvb_unregister_adapter);
  752. /* if the miracle happens and "generic_usercopy()" is included into
  753. the kernel, then this can vanish. please don't make the mistake and
  754. define this as video_usercopy(). this will introduce a dependecy
  755. to the v4l "videodev.o" module, which is unnecessary for some
  756. cards (ie. the budget dvb-cards don't need the v4l module...) */
  757. int dvb_usercopy(struct file *file,
  758. unsigned int cmd, unsigned long arg,
  759. int (*func)(struct file *file,
  760. unsigned int cmd, void *arg))
  761. {
  762. char sbuf[128];
  763. void *mbuf = NULL;
  764. void *parg = NULL;
  765. int err = -EINVAL;
  766. /* Copy arguments into temp kernel buffer */
  767. switch (_IOC_DIR(cmd)) {
  768. case _IOC_NONE:
  769. /*
  770. * For this command, the pointer is actually an integer
  771. * argument.
  772. */
  773. parg = (void *) arg;
  774. break;
  775. case _IOC_READ: /* some v4l ioctls are marked wrong ... */
  776. case _IOC_WRITE:
  777. case (_IOC_WRITE | _IOC_READ):
  778. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  779. parg = sbuf;
  780. } else {
  781. /* too big to allocate from stack */
  782. mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  783. if (NULL == mbuf)
  784. return -ENOMEM;
  785. parg = mbuf;
  786. }
  787. err = -EFAULT;
  788. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  789. goto out;
  790. break;
  791. }
  792. /* call driver */
  793. if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
  794. err = -ENOTTY;
  795. if (err < 0)
  796. goto out;
  797. /* Copy results into user buffer */
  798. switch (_IOC_DIR(cmd))
  799. {
  800. case _IOC_READ:
  801. case (_IOC_WRITE | _IOC_READ):
  802. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  803. err = -EFAULT;
  804. break;
  805. }
  806. out:
  807. kfree(mbuf);
  808. return err;
  809. }
  810. #if IS_ENABLED(CONFIG_I2C)
  811. struct i2c_client *dvb_module_probe(const char *module_name,
  812. const char *name,
  813. struct i2c_adapter *adap,
  814. unsigned char addr,
  815. void *platform_data)
  816. {
  817. struct i2c_client *client;
  818. struct i2c_board_info *board_info;
  819. board_info = kzalloc(sizeof(*board_info), GFP_KERNEL);
  820. if (!board_info)
  821. return NULL;
  822. if (name)
  823. strlcpy(board_info->type, name, I2C_NAME_SIZE);
  824. else
  825. strlcpy(board_info->type, module_name, I2C_NAME_SIZE);
  826. board_info->addr = addr;
  827. board_info->platform_data = platform_data;
  828. request_module(module_name);
  829. client = i2c_new_device(adap, board_info);
  830. if (client == NULL || client->dev.driver == NULL) {
  831. kfree(board_info);
  832. return NULL;
  833. }
  834. if (!try_module_get(client->dev.driver->owner)) {
  835. i2c_unregister_device(client);
  836. client = NULL;
  837. }
  838. kfree(board_info);
  839. return client;
  840. }
  841. EXPORT_SYMBOL_GPL(dvb_module_probe);
  842. void dvb_module_release(struct i2c_client *client)
  843. {
  844. if (!client)
  845. return;
  846. module_put(client->dev.driver->owner);
  847. i2c_unregister_device(client);
  848. }
  849. EXPORT_SYMBOL_GPL(dvb_module_release);
  850. #endif
  851. static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
  852. {
  853. struct dvb_device *dvbdev = dev_get_drvdata(dev);
  854. add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
  855. add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
  856. add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
  857. return 0;
  858. }
  859. static char *dvb_devnode(struct device *dev, umode_t *mode)
  860. {
  861. struct dvb_device *dvbdev = dev_get_drvdata(dev);
  862. return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
  863. dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
  864. }
  865. static int __init init_dvbdev(void)
  866. {
  867. int retval;
  868. dev_t dev = MKDEV(DVB_MAJOR, 0);
  869. if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
  870. pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
  871. return retval;
  872. }
  873. cdev_init(&dvb_device_cdev, &dvb_device_fops);
  874. if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
  875. pr_err("dvb-core: unable register character device\n");
  876. goto error;
  877. }
  878. dvb_class = class_create(THIS_MODULE, "dvb");
  879. if (IS_ERR(dvb_class)) {
  880. retval = PTR_ERR(dvb_class);
  881. goto error;
  882. }
  883. dvb_class->dev_uevent = dvb_uevent;
  884. dvb_class->devnode = dvb_devnode;
  885. return 0;
  886. error:
  887. cdev_del(&dvb_device_cdev);
  888. unregister_chrdev_region(dev, MAX_DVB_MINORS);
  889. return retval;
  890. }
  891. static void __exit exit_dvbdev(void)
  892. {
  893. class_destroy(dvb_class);
  894. cdev_del(&dvb_device_cdev);
  895. unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
  896. }
  897. subsys_initcall(init_dvbdev);
  898. module_exit(exit_dvbdev);
  899. MODULE_DESCRIPTION("DVB Core Driver");
  900. MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
  901. MODULE_LICENSE("GPL");