videodev.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * Video capture interface for Linux version 2
  3. *
  4. * A generic video device interface for the LINUX operating system
  5. * using a set of device structures/vectors for low level operations.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Authors: Alan Cox, <alan@redhat.com> (version 1)
  13. * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
  14. *
  15. * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
  16. * - Added procfs support
  17. */
  18. #define dbgarg(cmd, fmt, arg...) \
  19. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
  20. printk (KERN_DEBUG "%s: ", vfd->name); \
  21. v4l_printk_ioctl(cmd); \
  22. printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg); \
  23. }
  24. #define dbgarg2(fmt, arg...) \
  25. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
  26. printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/kernel.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/mm.h>
  32. #include <linux/string.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/kmod.h>
  36. #include <linux/slab.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/system.h>
  39. #define __OLD_VIDIOC_ /* To allow fixing old calls*/
  40. #include <linux/videodev2.h>
  41. #ifdef CONFIG_VIDEO_V4L1
  42. #include <linux/videodev.h>
  43. #endif
  44. #include <media/v4l2-common.h>
  45. #define VIDEO_NUM_DEVICES 256
  46. #define VIDEO_NAME "video4linux"
  47. /*
  48. * sysfs stuff
  49. */
  50. static ssize_t show_name(struct class_device *cd, char *buf)
  51. {
  52. struct video_device *vfd = container_of(cd, struct video_device,
  53. class_dev);
  54. return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
  55. }
  56. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  57. struct video_device *video_device_alloc(void)
  58. {
  59. struct video_device *vfd;
  60. vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
  61. return vfd;
  62. }
  63. void video_device_release(struct video_device *vfd)
  64. {
  65. kfree(vfd);
  66. }
  67. static void video_release(struct class_device *cd)
  68. {
  69. struct video_device *vfd = container_of(cd, struct video_device,
  70. class_dev);
  71. #if 1
  72. /* needed until all drivers are fixed */
  73. if (!vfd->release)
  74. return;
  75. #endif
  76. vfd->release(vfd);
  77. }
  78. static struct class video_class = {
  79. .name = VIDEO_NAME,
  80. .release = video_release,
  81. };
  82. /*
  83. * Active devices
  84. */
  85. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  86. static DEFINE_MUTEX(videodev_lock);
  87. struct video_device* video_devdata(struct file *file)
  88. {
  89. return video_device[iminor(file->f_path.dentry->d_inode)];
  90. }
  91. /*
  92. * Open a video device - FIXME: Obsoleted
  93. */
  94. static int video_open(struct inode *inode, struct file *file)
  95. {
  96. unsigned int minor = iminor(inode);
  97. int err = 0;
  98. struct video_device *vfl;
  99. const struct file_operations *old_fops;
  100. if(minor>=VIDEO_NUM_DEVICES)
  101. return -ENODEV;
  102. mutex_lock(&videodev_lock);
  103. vfl=video_device[minor];
  104. if(vfl==NULL) {
  105. mutex_unlock(&videodev_lock);
  106. request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
  107. mutex_lock(&videodev_lock);
  108. vfl=video_device[minor];
  109. if (vfl==NULL) {
  110. mutex_unlock(&videodev_lock);
  111. return -ENODEV;
  112. }
  113. }
  114. old_fops = file->f_op;
  115. file->f_op = fops_get(vfl->fops);
  116. if(file->f_op->open)
  117. err = file->f_op->open(inode,file);
  118. if (err) {
  119. fops_put(file->f_op);
  120. file->f_op = fops_get(old_fops);
  121. }
  122. fops_put(old_fops);
  123. mutex_unlock(&videodev_lock);
  124. return err;
  125. }
  126. /*
  127. * helper function -- handles userspace copying for ioctl arguments
  128. */
  129. #ifdef __OLD_VIDIOC_
  130. static unsigned int
  131. video_fix_command(unsigned int cmd)
  132. {
  133. switch (cmd) {
  134. case VIDIOC_OVERLAY_OLD:
  135. cmd = VIDIOC_OVERLAY;
  136. break;
  137. case VIDIOC_S_PARM_OLD:
  138. cmd = VIDIOC_S_PARM;
  139. break;
  140. case VIDIOC_S_CTRL_OLD:
  141. cmd = VIDIOC_S_CTRL;
  142. break;
  143. case VIDIOC_G_AUDIO_OLD:
  144. cmd = VIDIOC_G_AUDIO;
  145. break;
  146. case VIDIOC_G_AUDOUT_OLD:
  147. cmd = VIDIOC_G_AUDOUT;
  148. break;
  149. case VIDIOC_CROPCAP_OLD:
  150. cmd = VIDIOC_CROPCAP;
  151. break;
  152. }
  153. return cmd;
  154. }
  155. #endif
  156. /*
  157. * Obsolete usercopy function - Should be removed soon
  158. */
  159. int
  160. video_usercopy(struct inode *inode, struct file *file,
  161. unsigned int cmd, unsigned long arg,
  162. int (*func)(struct inode *inode, struct file *file,
  163. unsigned int cmd, void *arg))
  164. {
  165. char sbuf[128];
  166. void *mbuf = NULL;
  167. void *parg = NULL;
  168. int err = -EINVAL;
  169. int is_ext_ctrl;
  170. size_t ctrls_size = 0;
  171. void __user *user_ptr = NULL;
  172. #ifdef __OLD_VIDIOC_
  173. cmd = video_fix_command(cmd);
  174. #endif
  175. is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
  176. cmd == VIDIOC_TRY_EXT_CTRLS);
  177. /* Copy arguments into temp kernel buffer */
  178. switch (_IOC_DIR(cmd)) {
  179. case _IOC_NONE:
  180. parg = NULL;
  181. break;
  182. case _IOC_READ:
  183. case _IOC_WRITE:
  184. case (_IOC_WRITE | _IOC_READ):
  185. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  186. parg = sbuf;
  187. } else {
  188. /* too big to allocate from stack */
  189. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  190. if (NULL == mbuf)
  191. return -ENOMEM;
  192. parg = mbuf;
  193. }
  194. err = -EFAULT;
  195. if (_IOC_DIR(cmd) & _IOC_WRITE)
  196. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  197. goto out;
  198. break;
  199. }
  200. if (is_ext_ctrl) {
  201. struct v4l2_ext_controls *p = parg;
  202. /* In case of an error, tell the caller that it wasn't
  203. a specific control that caused it. */
  204. p->error_idx = p->count;
  205. user_ptr = (void __user *)p->controls;
  206. if (p->count) {
  207. ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
  208. /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
  209. mbuf = kmalloc(ctrls_size, GFP_KERNEL);
  210. err = -ENOMEM;
  211. if (NULL == mbuf)
  212. goto out_ext_ctrl;
  213. err = -EFAULT;
  214. if (copy_from_user(mbuf, user_ptr, ctrls_size))
  215. goto out_ext_ctrl;
  216. p->controls = mbuf;
  217. }
  218. }
  219. /* call driver */
  220. err = func(inode, file, cmd, parg);
  221. if (err == -ENOIOCTLCMD)
  222. err = -EINVAL;
  223. if (is_ext_ctrl) {
  224. struct v4l2_ext_controls *p = parg;
  225. p->controls = (void *)user_ptr;
  226. if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
  227. err = -EFAULT;
  228. goto out_ext_ctrl;
  229. }
  230. if (err < 0)
  231. goto out;
  232. out_ext_ctrl:
  233. /* Copy results into user buffer */
  234. switch (_IOC_DIR(cmd))
  235. {
  236. case _IOC_READ:
  237. case (_IOC_WRITE | _IOC_READ):
  238. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  239. err = -EFAULT;
  240. break;
  241. }
  242. out:
  243. kfree(mbuf);
  244. return err;
  245. }
  246. /*
  247. * open/release helper functions -- handle exclusive opens
  248. * Should be removed soon
  249. */
  250. int video_exclusive_open(struct inode *inode, struct file *file)
  251. {
  252. struct video_device *vfl = video_devdata(file);
  253. int retval = 0;
  254. mutex_lock(&vfl->lock);
  255. if (vfl->users) {
  256. retval = -EBUSY;
  257. } else {
  258. vfl->users++;
  259. }
  260. mutex_unlock(&vfl->lock);
  261. return retval;
  262. }
  263. int video_exclusive_release(struct inode *inode, struct file *file)
  264. {
  265. struct video_device *vfl = video_devdata(file);
  266. vfl->users--;
  267. return 0;
  268. }
  269. static char *v4l2_memory_names[] = {
  270. [V4L2_MEMORY_MMAP] = "mmap",
  271. [V4L2_MEMORY_USERPTR] = "userptr",
  272. [V4L2_MEMORY_OVERLAY] = "overlay",
  273. };
  274. /* FIXME: Those stuff are replicated also on v4l2-common.c */
  275. static char *v4l2_type_names_FIXME[] = {
  276. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
  277. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
  278. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
  279. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  280. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  281. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  282. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-capture",
  283. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "video-out-over",
  284. [V4L2_BUF_TYPE_PRIVATE] = "private",
  285. };
  286. static char *v4l2_field_names_FIXME[] = {
  287. [V4L2_FIELD_ANY] = "any",
  288. [V4L2_FIELD_NONE] = "none",
  289. [V4L2_FIELD_TOP] = "top",
  290. [V4L2_FIELD_BOTTOM] = "bottom",
  291. [V4L2_FIELD_INTERLACED] = "interlaced",
  292. [V4L2_FIELD_SEQ_TB] = "seq-tb",
  293. [V4L2_FIELD_SEQ_BT] = "seq-bt",
  294. [V4L2_FIELD_ALTERNATE] = "alternate",
  295. [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
  296. [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
  297. };
  298. #define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
  299. static void dbgbuf(unsigned int cmd, struct video_device *vfd,
  300. struct v4l2_buffer *p)
  301. {
  302. struct v4l2_timecode *tc=&p->timecode;
  303. dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
  304. "bytesused=%d, flags=0x%08d, "
  305. "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
  306. (p->timestamp.tv_sec/3600),
  307. (int)(p->timestamp.tv_sec/60)%60,
  308. (int)(p->timestamp.tv_sec%60),
  309. p->timestamp.tv_usec,
  310. p->index,
  311. prt_names(p->type,v4l2_type_names_FIXME),
  312. p->bytesused,p->flags,
  313. p->field,p->sequence,
  314. prt_names(p->memory,v4l2_memory_names),
  315. p->m.userptr, p->length);
  316. dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
  317. "flags=0x%08d, frames=%d, userbits=0x%08x\n",
  318. tc->hours,tc->minutes,tc->seconds,
  319. tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
  320. }
  321. static inline void dbgrect(struct video_device *vfd, char *s,
  322. struct v4l2_rect *r)
  323. {
  324. dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
  325. r->width, r->height);
  326. };
  327. static inline void v4l_print_pix_fmt (struct video_device *vfd,
  328. struct v4l2_pix_format *fmt)
  329. {
  330. dbgarg2 ("width=%d, height=%d, format=%c%c%c%c, field=%s, "
  331. "bytesperline=%d sizeimage=%d, colorspace=%d\n",
  332. fmt->width,fmt->height,
  333. (fmt->pixelformat & 0xff),
  334. (fmt->pixelformat >> 8) & 0xff,
  335. (fmt->pixelformat >> 16) & 0xff,
  336. (fmt->pixelformat >> 24) & 0xff,
  337. prt_names(fmt->field,v4l2_field_names_FIXME),
  338. fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
  339. };
  340. static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
  341. {
  342. switch (type) {
  343. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  344. if (vfd->vidioc_try_fmt_cap)
  345. return (0);
  346. break;
  347. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  348. if (vfd->vidioc_try_fmt_overlay)
  349. return (0);
  350. break;
  351. case V4L2_BUF_TYPE_VBI_CAPTURE:
  352. if (vfd->vidioc_try_fmt_vbi)
  353. return (0);
  354. break;
  355. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  356. if (vfd->vidioc_try_fmt_vbi_output)
  357. return (0);
  358. break;
  359. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  360. if (vfd->vidioc_try_fmt_vbi_capture)
  361. return (0);
  362. break;
  363. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  364. if (vfd->vidioc_try_fmt_video_output)
  365. return (0);
  366. break;
  367. case V4L2_BUF_TYPE_VBI_OUTPUT:
  368. if (vfd->vidioc_try_fmt_vbi_output)
  369. return (0);
  370. break;
  371. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  372. if (vfd->vidioc_try_fmt_output_overlay)
  373. return (0);
  374. break;
  375. case V4L2_BUF_TYPE_PRIVATE:
  376. if (vfd->vidioc_try_fmt_type_private)
  377. return (0);
  378. break;
  379. }
  380. return (-EINVAL);
  381. }
  382. static int __video_do_ioctl(struct inode *inode, struct file *file,
  383. unsigned int cmd, void *arg)
  384. {
  385. struct video_device *vfd = video_devdata(file);
  386. void *fh = file->private_data;
  387. int ret = -EINVAL;
  388. if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
  389. !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
  390. v4l_print_ioctl(vfd->name, cmd);
  391. }
  392. if (_IOC_TYPE(cmd)=='v')
  393. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  394. __video_do_ioctl);
  395. switch(cmd) {
  396. /* --- capabilities ------------------------------------------ */
  397. case VIDIOC_QUERYCAP:
  398. {
  399. struct v4l2_capability *cap = (struct v4l2_capability*)arg;
  400. memset(cap, 0, sizeof(*cap));
  401. if (!vfd->vidioc_querycap)
  402. break;
  403. ret=vfd->vidioc_querycap(file, fh, cap);
  404. if (!ret)
  405. dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
  406. "version=0x%08x, "
  407. "capabilities=0x%08x\n",
  408. cap->driver,cap->card,cap->bus_info,
  409. cap->version,
  410. cap->capabilities);
  411. break;
  412. }
  413. /* --- priority ------------------------------------------ */
  414. case VIDIOC_G_PRIORITY:
  415. {
  416. enum v4l2_priority *p=arg;
  417. if (!vfd->vidioc_g_priority)
  418. break;
  419. ret=vfd->vidioc_g_priority(file, fh, p);
  420. if (!ret)
  421. dbgarg(cmd, "priority is %d\n", *p);
  422. break;
  423. }
  424. case VIDIOC_S_PRIORITY:
  425. {
  426. enum v4l2_priority *p=arg;
  427. if (!vfd->vidioc_s_priority)
  428. break;
  429. dbgarg(cmd, "setting priority to %d\n", *p);
  430. ret=vfd->vidioc_s_priority(file, fh, *p);
  431. break;
  432. }
  433. /* --- capture ioctls ---------------------------------------- */
  434. case VIDIOC_ENUM_FMT:
  435. {
  436. struct v4l2_fmtdesc *f = arg;
  437. enum v4l2_buf_type type;
  438. unsigned int index;
  439. index = f->index;
  440. type = f->type;
  441. memset(f,0,sizeof(*f));
  442. f->index = index;
  443. f->type = type;
  444. switch (type) {
  445. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  446. if (vfd->vidioc_enum_fmt_cap)
  447. ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
  448. break;
  449. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  450. if (vfd->vidioc_enum_fmt_overlay)
  451. ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
  452. break;
  453. case V4L2_BUF_TYPE_VBI_CAPTURE:
  454. if (vfd->vidioc_enum_fmt_vbi)
  455. ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
  456. break;
  457. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  458. if (vfd->vidioc_enum_fmt_vbi_output)
  459. ret=vfd->vidioc_enum_fmt_vbi_output(file,
  460. fh, f);
  461. break;
  462. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  463. if (vfd->vidioc_enum_fmt_vbi_capture)
  464. ret=vfd->vidioc_enum_fmt_vbi_capture(file,
  465. fh, f);
  466. break;
  467. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  468. if (vfd->vidioc_enum_fmt_video_output)
  469. ret=vfd->vidioc_enum_fmt_video_output(file,
  470. fh, f);
  471. break;
  472. case V4L2_BUF_TYPE_VBI_OUTPUT:
  473. if (vfd->vidioc_enum_fmt_vbi_output)
  474. ret=vfd->vidioc_enum_fmt_vbi_output(file,
  475. fh, f);
  476. break;
  477. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  478. if (vfd->vidioc_enum_fmt_output_overlay)
  479. ret=vfd->vidioc_enum_fmt_output_overlay(file, fh, f);
  480. break;
  481. case V4L2_BUF_TYPE_PRIVATE:
  482. if (vfd->vidioc_enum_fmt_type_private)
  483. ret=vfd->vidioc_enum_fmt_type_private(file,
  484. fh, f);
  485. break;
  486. }
  487. if (!ret)
  488. dbgarg (cmd, "index=%d, type=%d, flags=%d, "
  489. "pixelformat=%c%c%c%c, description='%s'\n",
  490. f->index, f->type, f->flags,
  491. (f->pixelformat & 0xff),
  492. (f->pixelformat >> 8) & 0xff,
  493. (f->pixelformat >> 16) & 0xff,
  494. (f->pixelformat >> 24) & 0xff,
  495. f->description);
  496. break;
  497. }
  498. case VIDIOC_G_FMT:
  499. {
  500. struct v4l2_format *f = (struct v4l2_format *)arg;
  501. enum v4l2_buf_type type=f->type;
  502. memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
  503. f->type=type;
  504. /* FIXME: Should be one dump per type */
  505. dbgarg (cmd, "type=%s\n", prt_names(type,
  506. v4l2_type_names_FIXME));
  507. switch (type) {
  508. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  509. if (vfd->vidioc_g_fmt_cap)
  510. ret=vfd->vidioc_g_fmt_cap(file, fh, f);
  511. if (!ret)
  512. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  513. break;
  514. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  515. if (vfd->vidioc_g_fmt_overlay)
  516. ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
  517. break;
  518. case V4L2_BUF_TYPE_VBI_CAPTURE:
  519. if (vfd->vidioc_g_fmt_vbi)
  520. ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
  521. break;
  522. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  523. if (vfd->vidioc_g_fmt_vbi_output)
  524. ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
  525. break;
  526. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  527. if (vfd->vidioc_g_fmt_vbi_capture)
  528. ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
  529. break;
  530. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  531. if (vfd->vidioc_g_fmt_video_output)
  532. ret=vfd->vidioc_g_fmt_video_output(file,
  533. fh, f);
  534. break;
  535. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  536. if (vfd->vidioc_g_fmt_output_overlay)
  537. ret=vfd->vidioc_g_fmt_output_overlay(file, fh, f);
  538. break;
  539. case V4L2_BUF_TYPE_VBI_OUTPUT:
  540. if (vfd->vidioc_g_fmt_vbi_output)
  541. ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
  542. break;
  543. case V4L2_BUF_TYPE_PRIVATE:
  544. if (vfd->vidioc_g_fmt_type_private)
  545. ret=vfd->vidioc_g_fmt_type_private(file,
  546. fh, f);
  547. break;
  548. }
  549. break;
  550. }
  551. case VIDIOC_S_FMT:
  552. {
  553. struct v4l2_format *f = (struct v4l2_format *)arg;
  554. /* FIXME: Should be one dump per type */
  555. dbgarg (cmd, "type=%s\n", prt_names(f->type,
  556. v4l2_type_names_FIXME));
  557. switch (f->type) {
  558. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  559. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  560. if (vfd->vidioc_s_fmt_cap)
  561. ret=vfd->vidioc_s_fmt_cap(file, fh, f);
  562. break;
  563. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  564. if (vfd->vidioc_s_fmt_overlay)
  565. ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
  566. break;
  567. case V4L2_BUF_TYPE_VBI_CAPTURE:
  568. if (vfd->vidioc_s_fmt_vbi)
  569. ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
  570. break;
  571. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  572. if (vfd->vidioc_s_fmt_vbi_output)
  573. ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
  574. break;
  575. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  576. if (vfd->vidioc_s_fmt_vbi_capture)
  577. ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
  578. break;
  579. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  580. if (vfd->vidioc_s_fmt_video_output)
  581. ret=vfd->vidioc_s_fmt_video_output(file,
  582. fh, f);
  583. break;
  584. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  585. if (vfd->vidioc_s_fmt_output_overlay)
  586. ret=vfd->vidioc_s_fmt_output_overlay(file, fh, f);
  587. break;
  588. case V4L2_BUF_TYPE_VBI_OUTPUT:
  589. if (vfd->vidioc_s_fmt_vbi_output)
  590. ret=vfd->vidioc_s_fmt_vbi_output(file,
  591. fh, f);
  592. break;
  593. case V4L2_BUF_TYPE_PRIVATE:
  594. if (vfd->vidioc_s_fmt_type_private)
  595. ret=vfd->vidioc_s_fmt_type_private(file,
  596. fh, f);
  597. break;
  598. }
  599. break;
  600. }
  601. case VIDIOC_TRY_FMT:
  602. {
  603. struct v4l2_format *f = (struct v4l2_format *)arg;
  604. /* FIXME: Should be one dump per type */
  605. dbgarg (cmd, "type=%s\n", prt_names(f->type,
  606. v4l2_type_names_FIXME));
  607. switch (f->type) {
  608. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  609. if (vfd->vidioc_try_fmt_cap)
  610. ret=vfd->vidioc_try_fmt_cap(file, fh, f);
  611. if (!ret)
  612. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  613. break;
  614. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  615. if (vfd->vidioc_try_fmt_overlay)
  616. ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
  617. break;
  618. case V4L2_BUF_TYPE_VBI_CAPTURE:
  619. if (vfd->vidioc_try_fmt_vbi)
  620. ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
  621. break;
  622. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  623. if (vfd->vidioc_try_fmt_vbi_output)
  624. ret=vfd->vidioc_try_fmt_vbi_output(file,
  625. fh, f);
  626. break;
  627. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  628. if (vfd->vidioc_try_fmt_vbi_capture)
  629. ret=vfd->vidioc_try_fmt_vbi_capture(file,
  630. fh, f);
  631. break;
  632. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  633. if (vfd->vidioc_try_fmt_video_output)
  634. ret=vfd->vidioc_try_fmt_video_output(file,
  635. fh, f);
  636. break;
  637. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  638. if (vfd->vidioc_try_fmt_output_overlay)
  639. ret=vfd->vidioc_try_fmt_output_overlay(file, fh, f);
  640. break;
  641. case V4L2_BUF_TYPE_VBI_OUTPUT:
  642. if (vfd->vidioc_try_fmt_vbi_output)
  643. ret=vfd->vidioc_try_fmt_vbi_output(file,
  644. fh, f);
  645. break;
  646. case V4L2_BUF_TYPE_PRIVATE:
  647. if (vfd->vidioc_try_fmt_type_private)
  648. ret=vfd->vidioc_try_fmt_type_private(file,
  649. fh, f);
  650. break;
  651. }
  652. break;
  653. }
  654. /* FIXME: Those buf reqs could be handled here,
  655. with some changes on videobuf to allow its header to be included at
  656. videodev2.h or being merged at videodev2.
  657. */
  658. case VIDIOC_REQBUFS:
  659. {
  660. struct v4l2_requestbuffers *p=arg;
  661. if (!vfd->vidioc_reqbufs)
  662. break;
  663. ret = check_fmt (vfd, p->type);
  664. if (ret)
  665. break;
  666. ret=vfd->vidioc_reqbufs(file, fh, p);
  667. dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
  668. p->count,
  669. prt_names(p->type,v4l2_type_names_FIXME),
  670. prt_names(p->memory,v4l2_memory_names));
  671. break;
  672. }
  673. case VIDIOC_QUERYBUF:
  674. {
  675. struct v4l2_buffer *p=arg;
  676. if (!vfd->vidioc_querybuf)
  677. break;
  678. ret = check_fmt (vfd, p->type);
  679. if (ret)
  680. break;
  681. ret=vfd->vidioc_querybuf(file, fh, p);
  682. if (!ret)
  683. dbgbuf(cmd,vfd,p);
  684. break;
  685. }
  686. case VIDIOC_QBUF:
  687. {
  688. struct v4l2_buffer *p=arg;
  689. if (!vfd->vidioc_qbuf)
  690. break;
  691. ret = check_fmt (vfd, p->type);
  692. if (ret)
  693. break;
  694. ret=vfd->vidioc_qbuf(file, fh, p);
  695. if (!ret)
  696. dbgbuf(cmd,vfd,p);
  697. break;
  698. }
  699. case VIDIOC_DQBUF:
  700. {
  701. struct v4l2_buffer *p=arg;
  702. if (!vfd->vidioc_dqbuf)
  703. break;
  704. ret = check_fmt (vfd, p->type);
  705. if (ret)
  706. break;
  707. ret=vfd->vidioc_dqbuf(file, fh, p);
  708. if (!ret)
  709. dbgbuf(cmd,vfd,p);
  710. break;
  711. }
  712. case VIDIOC_OVERLAY:
  713. {
  714. int *i = arg;
  715. if (!vfd->vidioc_overlay)
  716. break;
  717. dbgarg (cmd, "value=%d\n",*i);
  718. ret=vfd->vidioc_overlay(file, fh, *i);
  719. break;
  720. }
  721. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  722. /* --- streaming capture ------------------------------------- */
  723. case VIDIOCGMBUF:
  724. {
  725. struct video_mbuf *p=arg;
  726. memset(p,0,sizeof(p));
  727. if (!vfd->vidiocgmbuf)
  728. break;
  729. ret=vfd->vidiocgmbuf(file, fh, p);
  730. if (!ret)
  731. dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
  732. p->size, p->frames,
  733. (unsigned long)p->offsets);
  734. break;
  735. }
  736. #endif
  737. case VIDIOC_G_FBUF:
  738. {
  739. struct v4l2_framebuffer *p=arg;
  740. if (!vfd->vidioc_g_fbuf)
  741. break;
  742. ret=vfd->vidioc_g_fbuf(file, fh, arg);
  743. if (!ret) {
  744. dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
  745. p->capability,p->flags,
  746. (unsigned long)p->base);
  747. v4l_print_pix_fmt (vfd, &p->fmt);
  748. }
  749. break;
  750. }
  751. case VIDIOC_S_FBUF:
  752. {
  753. struct v4l2_framebuffer *p=arg;
  754. if (!vfd->vidioc_s_fbuf)
  755. break;
  756. dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
  757. p->capability,p->flags,(unsigned long)p->base);
  758. v4l_print_pix_fmt (vfd, &p->fmt);
  759. ret=vfd->vidioc_s_fbuf(file, fh, arg);
  760. break;
  761. }
  762. case VIDIOC_STREAMON:
  763. {
  764. enum v4l2_buf_type i = *(int *)arg;
  765. if (!vfd->vidioc_streamon)
  766. break;
  767. dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
  768. ret=vfd->vidioc_streamon(file, fh,i);
  769. break;
  770. }
  771. case VIDIOC_STREAMOFF:
  772. {
  773. enum v4l2_buf_type i = *(int *)arg;
  774. if (!vfd->vidioc_streamoff)
  775. break;
  776. dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
  777. ret=vfd->vidioc_streamoff(file, fh, i);
  778. break;
  779. }
  780. /* ---------- tv norms ---------- */
  781. case VIDIOC_ENUMSTD:
  782. {
  783. struct v4l2_standard *p = arg;
  784. v4l2_std_id id = vfd->tvnorms,curr_id=0;
  785. unsigned int index = p->index,i;
  786. if (index<0) {
  787. ret=-EINVAL;
  788. break;
  789. }
  790. /* Return norm array on a canonical way */
  791. for (i=0;i<= index && id; i++) {
  792. if ( (id & V4L2_STD_PAL) == V4L2_STD_PAL) {
  793. curr_id = V4L2_STD_PAL;
  794. } else if ( (id & V4L2_STD_PAL_BG) == V4L2_STD_PAL_BG) {
  795. curr_id = V4L2_STD_PAL_BG;
  796. } else if ( (id & V4L2_STD_PAL_DK) == V4L2_STD_PAL_DK) {
  797. curr_id = V4L2_STD_PAL_DK;
  798. } else if ( (id & V4L2_STD_PAL_B) == V4L2_STD_PAL_B) {
  799. curr_id = V4L2_STD_PAL_B;
  800. } else if ( (id & V4L2_STD_PAL_B1) == V4L2_STD_PAL_B1) {
  801. curr_id = V4L2_STD_PAL_B1;
  802. } else if ( (id & V4L2_STD_PAL_G) == V4L2_STD_PAL_G) {
  803. curr_id = V4L2_STD_PAL_G;
  804. } else if ( (id & V4L2_STD_PAL_H) == V4L2_STD_PAL_H) {
  805. curr_id = V4L2_STD_PAL_H;
  806. } else if ( (id & V4L2_STD_PAL_I) == V4L2_STD_PAL_I) {
  807. curr_id = V4L2_STD_PAL_I;
  808. } else if ( (id & V4L2_STD_PAL_D) == V4L2_STD_PAL_D) {
  809. curr_id = V4L2_STD_PAL_D;
  810. } else if ( (id & V4L2_STD_PAL_D1) == V4L2_STD_PAL_D1) {
  811. curr_id = V4L2_STD_PAL_D1;
  812. } else if ( (id & V4L2_STD_PAL_K) == V4L2_STD_PAL_K) {
  813. curr_id = V4L2_STD_PAL_K;
  814. } else if ( (id & V4L2_STD_PAL_M) == V4L2_STD_PAL_M) {
  815. curr_id = V4L2_STD_PAL_M;
  816. } else if ( (id & V4L2_STD_PAL_N) == V4L2_STD_PAL_N) {
  817. curr_id = V4L2_STD_PAL_N;
  818. } else if ( (id & V4L2_STD_PAL_Nc) == V4L2_STD_PAL_Nc) {
  819. curr_id = V4L2_STD_PAL_Nc;
  820. } else if ( (id & V4L2_STD_PAL_60) == V4L2_STD_PAL_60) {
  821. curr_id = V4L2_STD_PAL_60;
  822. } else if ( (id & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
  823. curr_id = V4L2_STD_NTSC;
  824. } else if ( (id & V4L2_STD_NTSC_M) == V4L2_STD_NTSC_M) {
  825. curr_id = V4L2_STD_NTSC_M;
  826. } else if ( (id & V4L2_STD_NTSC_M_JP) == V4L2_STD_NTSC_M_JP) {
  827. curr_id = V4L2_STD_NTSC_M_JP;
  828. } else if ( (id & V4L2_STD_NTSC_443) == V4L2_STD_NTSC_443) {
  829. curr_id = V4L2_STD_NTSC_443;
  830. } else if ( (id & V4L2_STD_NTSC_M_KR) == V4L2_STD_NTSC_M_KR) {
  831. curr_id = V4L2_STD_NTSC_M_KR;
  832. } else if ( (id & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
  833. curr_id = V4L2_STD_SECAM;
  834. } else if ( (id & V4L2_STD_SECAM_DK) == V4L2_STD_SECAM_DK) {
  835. curr_id = V4L2_STD_SECAM_DK;
  836. } else if ( (id & V4L2_STD_SECAM_B) == V4L2_STD_SECAM_B) {
  837. curr_id = V4L2_STD_SECAM_B;
  838. } else if ( (id & V4L2_STD_SECAM_D) == V4L2_STD_SECAM_D) {
  839. curr_id = V4L2_STD_SECAM_D;
  840. } else if ( (id & V4L2_STD_SECAM_G) == V4L2_STD_SECAM_G) {
  841. curr_id = V4L2_STD_SECAM_G;
  842. } else if ( (id & V4L2_STD_SECAM_H) == V4L2_STD_SECAM_H) {
  843. curr_id = V4L2_STD_SECAM_H;
  844. } else if ( (id & V4L2_STD_SECAM_K) == V4L2_STD_SECAM_K) {
  845. curr_id = V4L2_STD_SECAM_K;
  846. } else if ( (id & V4L2_STD_SECAM_K1) == V4L2_STD_SECAM_K1) {
  847. curr_id = V4L2_STD_SECAM_K1;
  848. } else if ( (id & V4L2_STD_SECAM_L) == V4L2_STD_SECAM_L) {
  849. curr_id = V4L2_STD_SECAM_L;
  850. } else if ( (id & V4L2_STD_SECAM_LC) == V4L2_STD_SECAM_LC) {
  851. curr_id = V4L2_STD_SECAM_LC;
  852. } else {
  853. break;
  854. }
  855. id &= ~curr_id;
  856. }
  857. if (i<=index)
  858. return -EINVAL;
  859. v4l2_video_std_construct(p, curr_id,v4l2_norm_to_name(curr_id));
  860. p->index = index;
  861. dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
  862. "framelines=%d\n", p->index,
  863. (unsigned long long)p->id, p->name,
  864. p->frameperiod.numerator,
  865. p->frameperiod.denominator,
  866. p->framelines);
  867. ret=0;
  868. break;
  869. }
  870. case VIDIOC_G_STD:
  871. {
  872. v4l2_std_id *id = arg;
  873. *id = vfd->current_norm;
  874. dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
  875. ret=0;
  876. break;
  877. }
  878. case VIDIOC_S_STD:
  879. {
  880. v4l2_std_id *id = arg,norm;
  881. dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
  882. norm = (*id) & vfd->tvnorms;
  883. if ( vfd->tvnorms && !norm) /* Check if std is supported */
  884. break;
  885. /* Calls the specific handler */
  886. if (vfd->vidioc_s_std)
  887. ret=vfd->vidioc_s_std(file, fh, &norm);
  888. else
  889. ret=-EINVAL;
  890. /* Updates standard information */
  891. if (ret>=0)
  892. vfd->current_norm=norm;
  893. break;
  894. }
  895. case VIDIOC_QUERYSTD:
  896. {
  897. v4l2_std_id *p=arg;
  898. if (!vfd->vidioc_querystd)
  899. break;
  900. ret=vfd->vidioc_querystd(file, fh, arg);
  901. if (!ret)
  902. dbgarg (cmd, "detected std=%Lu\n",
  903. (unsigned long long)*p);
  904. break;
  905. }
  906. /* ------ input switching ---------- */
  907. /* FIXME: Inputs can be handled inside videodev2 */
  908. case VIDIOC_ENUMINPUT:
  909. {
  910. struct v4l2_input *p=arg;
  911. int i=p->index;
  912. if (!vfd->vidioc_enum_input)
  913. break;
  914. memset(p, 0, sizeof(*p));
  915. p->index=i;
  916. ret=vfd->vidioc_enum_input(file, fh, p);
  917. if (!ret)
  918. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  919. "audioset=%d, "
  920. "tuner=%d, std=%Ld, status=%d\n",
  921. p->index,p->name,p->type,p->audioset,
  922. p->tuner,
  923. (unsigned long long)p->std,
  924. p->status);
  925. break;
  926. }
  927. case VIDIOC_G_INPUT:
  928. {
  929. unsigned int *i = arg;
  930. if (!vfd->vidioc_g_input)
  931. break;
  932. ret=vfd->vidioc_g_input(file, fh, i);
  933. if (!ret)
  934. dbgarg (cmd, "value=%d\n",*i);
  935. break;
  936. }
  937. case VIDIOC_S_INPUT:
  938. {
  939. unsigned int *i = arg;
  940. if (!vfd->vidioc_s_input)
  941. break;
  942. dbgarg (cmd, "value=%d\n",*i);
  943. ret=vfd->vidioc_s_input(file, fh, *i);
  944. break;
  945. }
  946. /* ------ output switching ---------- */
  947. case VIDIOC_G_OUTPUT:
  948. {
  949. unsigned int *i = arg;
  950. if (!vfd->vidioc_g_output)
  951. break;
  952. ret=vfd->vidioc_g_output(file, fh, i);
  953. if (!ret)
  954. dbgarg (cmd, "value=%d\n",*i);
  955. break;
  956. }
  957. case VIDIOC_S_OUTPUT:
  958. {
  959. unsigned int *i = arg;
  960. if (!vfd->vidioc_s_output)
  961. break;
  962. dbgarg (cmd, "value=%d\n",*i);
  963. ret=vfd->vidioc_s_output(file, fh, *i);
  964. break;
  965. }
  966. /* --- controls ---------------------------------------------- */
  967. case VIDIOC_QUERYCTRL:
  968. {
  969. struct v4l2_queryctrl *p=arg;
  970. if (!vfd->vidioc_queryctrl)
  971. break;
  972. ret=vfd->vidioc_queryctrl(file, fh, p);
  973. if (!ret)
  974. dbgarg (cmd, "id=%d, type=%d, name=%s, "
  975. "min/max=%d/%d,"
  976. " step=%d, default=%d, flags=0x%08x\n",
  977. p->id,p->type,p->name,p->minimum,
  978. p->maximum,p->step,p->default_value,
  979. p->flags);
  980. break;
  981. }
  982. case VIDIOC_G_CTRL:
  983. {
  984. struct v4l2_control *p = arg;
  985. if (!vfd->vidioc_g_ctrl)
  986. break;
  987. dbgarg(cmd, "Enum for index=%d\n", p->id);
  988. ret=vfd->vidioc_g_ctrl(file, fh, p);
  989. if (!ret)
  990. dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
  991. break;
  992. }
  993. case VIDIOC_S_CTRL:
  994. {
  995. struct v4l2_control *p = arg;
  996. if (!vfd->vidioc_s_ctrl)
  997. break;
  998. dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
  999. ret=vfd->vidioc_s_ctrl(file, fh, p);
  1000. break;
  1001. }
  1002. case VIDIOC_G_EXT_CTRLS:
  1003. {
  1004. struct v4l2_ext_controls *p = arg;
  1005. if (vfd->vidioc_g_ext_ctrls) {
  1006. dbgarg(cmd, "count=%d\n", p->count);
  1007. ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
  1008. }
  1009. break;
  1010. }
  1011. case VIDIOC_S_EXT_CTRLS:
  1012. {
  1013. struct v4l2_ext_controls *p = arg;
  1014. if (vfd->vidioc_s_ext_ctrls) {
  1015. dbgarg(cmd, "count=%d\n", p->count);
  1016. ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
  1017. }
  1018. break;
  1019. }
  1020. case VIDIOC_TRY_EXT_CTRLS:
  1021. {
  1022. struct v4l2_ext_controls *p = arg;
  1023. if (vfd->vidioc_try_ext_ctrls) {
  1024. dbgarg(cmd, "count=%d\n", p->count);
  1025. ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
  1026. }
  1027. break;
  1028. }
  1029. case VIDIOC_QUERYMENU:
  1030. {
  1031. struct v4l2_querymenu *p=arg;
  1032. if (!vfd->vidioc_querymenu)
  1033. break;
  1034. ret=vfd->vidioc_querymenu(file, fh, p);
  1035. if (!ret)
  1036. dbgarg (cmd, "id=%d, index=%d, name=%s\n",
  1037. p->id,p->index,p->name);
  1038. break;
  1039. }
  1040. /* --- audio ---------------------------------------------- */
  1041. case VIDIOC_ENUMAUDIO:
  1042. {
  1043. struct v4l2_audio *p=arg;
  1044. if (!vfd->vidioc_enumaudio)
  1045. break;
  1046. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1047. ret=vfd->vidioc_enumaudio(file, fh, p);
  1048. if (!ret)
  1049. dbgarg2("index=%d, name=%s, capability=%d, "
  1050. "mode=%d\n",p->index,p->name,
  1051. p->capability, p->mode);
  1052. break;
  1053. }
  1054. case VIDIOC_G_AUDIO:
  1055. {
  1056. struct v4l2_audio *p=arg;
  1057. __u32 index=p->index;
  1058. if (!vfd->vidioc_g_audio)
  1059. break;
  1060. memset(p,0,sizeof(*p));
  1061. p->index=index;
  1062. dbgarg(cmd, "Get for index=%d\n", p->index);
  1063. ret=vfd->vidioc_g_audio(file, fh, p);
  1064. if (!ret)
  1065. dbgarg2("index=%d, name=%s, capability=%d, "
  1066. "mode=%d\n",p->index,
  1067. p->name,p->capability, p->mode);
  1068. break;
  1069. }
  1070. case VIDIOC_S_AUDIO:
  1071. {
  1072. struct v4l2_audio *p=arg;
  1073. if (!vfd->vidioc_s_audio)
  1074. break;
  1075. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1076. "mode=%d\n", p->index, p->name,
  1077. p->capability, p->mode);
  1078. ret=vfd->vidioc_s_audio(file, fh, p);
  1079. break;
  1080. }
  1081. case VIDIOC_ENUMAUDOUT:
  1082. {
  1083. struct v4l2_audioout *p=arg;
  1084. if (!vfd->vidioc_enumaudout)
  1085. break;
  1086. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1087. ret=vfd->vidioc_enumaudout(file, fh, p);
  1088. if (!ret)
  1089. dbgarg2("index=%d, name=%s, capability=%d, "
  1090. "mode=%d\n", p->index, p->name,
  1091. p->capability,p->mode);
  1092. break;
  1093. }
  1094. case VIDIOC_G_AUDOUT:
  1095. {
  1096. struct v4l2_audioout *p=arg;
  1097. if (!vfd->vidioc_g_audout)
  1098. break;
  1099. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1100. ret=vfd->vidioc_g_audout(file, fh, p);
  1101. if (!ret)
  1102. dbgarg2("index=%d, name=%s, capability=%d, "
  1103. "mode=%d\n", p->index, p->name,
  1104. p->capability,p->mode);
  1105. break;
  1106. }
  1107. case VIDIOC_S_AUDOUT:
  1108. {
  1109. struct v4l2_audioout *p=arg;
  1110. if (!vfd->vidioc_s_audout)
  1111. break;
  1112. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1113. "mode=%d\n", p->index, p->name,
  1114. p->capability,p->mode);
  1115. ret=vfd->vidioc_s_audout(file, fh, p);
  1116. break;
  1117. }
  1118. case VIDIOC_G_MODULATOR:
  1119. {
  1120. struct v4l2_modulator *p=arg;
  1121. if (!vfd->vidioc_g_modulator)
  1122. break;
  1123. ret=vfd->vidioc_g_modulator(file, fh, p);
  1124. if (!ret)
  1125. dbgarg(cmd, "index=%d, name=%s, "
  1126. "capability=%d, rangelow=%d,"
  1127. " rangehigh=%d, txsubchans=%d\n",
  1128. p->index, p->name,p->capability,
  1129. p->rangelow, p->rangehigh,
  1130. p->txsubchans);
  1131. break;
  1132. }
  1133. case VIDIOC_S_MODULATOR:
  1134. {
  1135. struct v4l2_modulator *p=arg;
  1136. if (!vfd->vidioc_s_modulator)
  1137. break;
  1138. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1139. "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
  1140. p->index, p->name,p->capability,p->rangelow,
  1141. p->rangehigh,p->txsubchans);
  1142. ret=vfd->vidioc_s_modulator(file, fh, p);
  1143. break;
  1144. }
  1145. case VIDIOC_G_CROP:
  1146. {
  1147. struct v4l2_crop *p=arg;
  1148. if (!vfd->vidioc_g_crop)
  1149. break;
  1150. ret=vfd->vidioc_g_crop(file, fh, p);
  1151. if (!ret) {
  1152. dbgarg(cmd, "type=%d\n", p->type);
  1153. dbgrect(vfd, "", &p->c);
  1154. }
  1155. break;
  1156. }
  1157. case VIDIOC_S_CROP:
  1158. {
  1159. struct v4l2_crop *p=arg;
  1160. if (!vfd->vidioc_s_crop)
  1161. break;
  1162. dbgarg(cmd, "type=%d\n", p->type);
  1163. dbgrect(vfd, "", &p->c);
  1164. ret=vfd->vidioc_s_crop(file, fh, p);
  1165. break;
  1166. }
  1167. case VIDIOC_CROPCAP:
  1168. {
  1169. struct v4l2_cropcap *p=arg;
  1170. /*FIXME: Should also show v4l2_fract pixelaspect */
  1171. if (!vfd->vidioc_cropcap)
  1172. break;
  1173. dbgarg(cmd, "type=%d\n", p->type);
  1174. dbgrect(vfd, "bounds ", &p->bounds);
  1175. dbgrect(vfd, "defrect ", &p->defrect);
  1176. ret=vfd->vidioc_cropcap(file, fh, p);
  1177. break;
  1178. }
  1179. case VIDIOC_G_MPEGCOMP:
  1180. {
  1181. struct v4l2_mpeg_compression *p=arg;
  1182. /*FIXME: Several fields not shown */
  1183. if (!vfd->vidioc_g_mpegcomp)
  1184. break;
  1185. ret=vfd->vidioc_g_mpegcomp(file, fh, p);
  1186. if (!ret)
  1187. dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
  1188. " ts_pid_video=%d, ts_pid_pcr=%d, "
  1189. "ps_size=%d, au_sample_rate=%d, "
  1190. "au_pesid=%c, vi_frame_rate=%d, "
  1191. "vi_frames_per_gop=%d, "
  1192. "vi_bframes_count=%d, vi_pesid=%c\n",
  1193. p->ts_pid_pmt,p->ts_pid_audio,
  1194. p->ts_pid_video,p->ts_pid_pcr,
  1195. p->ps_size, p->au_sample_rate,
  1196. p->au_pesid, p->vi_frame_rate,
  1197. p->vi_frames_per_gop,
  1198. p->vi_bframes_count, p->vi_pesid);
  1199. break;
  1200. }
  1201. case VIDIOC_S_MPEGCOMP:
  1202. {
  1203. struct v4l2_mpeg_compression *p=arg;
  1204. /*FIXME: Several fields not shown */
  1205. if (!vfd->vidioc_s_mpegcomp)
  1206. break;
  1207. dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
  1208. "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
  1209. "au_sample_rate=%d, au_pesid=%c, "
  1210. "vi_frame_rate=%d, vi_frames_per_gop=%d, "
  1211. "vi_bframes_count=%d, vi_pesid=%c\n",
  1212. p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
  1213. p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
  1214. p->au_pesid, p->vi_frame_rate,
  1215. p->vi_frames_per_gop, p->vi_bframes_count,
  1216. p->vi_pesid);
  1217. ret=vfd->vidioc_s_mpegcomp(file, fh, p);
  1218. break;
  1219. }
  1220. case VIDIOC_G_JPEGCOMP:
  1221. {
  1222. struct v4l2_jpegcompression *p=arg;
  1223. if (!vfd->vidioc_g_jpegcomp)
  1224. break;
  1225. ret=vfd->vidioc_g_jpegcomp(file, fh, p);
  1226. if (!ret)
  1227. dbgarg (cmd, "quality=%d, APPn=%d, "
  1228. "APP_len=%d, COM_len=%d, "
  1229. "jpeg_markers=%d\n",
  1230. p->quality,p->APPn,p->APP_len,
  1231. p->COM_len,p->jpeg_markers);
  1232. break;
  1233. }
  1234. case VIDIOC_S_JPEGCOMP:
  1235. {
  1236. struct v4l2_jpegcompression *p=arg;
  1237. if (!vfd->vidioc_g_jpegcomp)
  1238. break;
  1239. dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
  1240. "COM_len=%d, jpeg_markers=%d\n",
  1241. p->quality,p->APPn,p->APP_len,
  1242. p->COM_len,p->jpeg_markers);
  1243. ret=vfd->vidioc_s_jpegcomp(file, fh, p);
  1244. break;
  1245. }
  1246. case VIDIOC_G_ENC_INDEX:
  1247. {
  1248. struct v4l2_enc_idx *p=arg;
  1249. if (!vfd->vidioc_g_enc_index)
  1250. break;
  1251. ret=vfd->vidioc_g_enc_index(file, fh, p);
  1252. if (!ret)
  1253. dbgarg (cmd, "entries=%d, entries_cap=%d\n",
  1254. p->entries,p->entries_cap);
  1255. break;
  1256. }
  1257. case VIDIOC_ENCODER_CMD:
  1258. {
  1259. struct v4l2_encoder_cmd *p=arg;
  1260. if (!vfd->vidioc_encoder_cmd)
  1261. break;
  1262. ret=vfd->vidioc_encoder_cmd(file, fh, p);
  1263. if (!ret)
  1264. dbgarg (cmd, "cmd=%d, flags=%d\n",
  1265. p->cmd,p->flags);
  1266. break;
  1267. }
  1268. case VIDIOC_TRY_ENCODER_CMD:
  1269. {
  1270. struct v4l2_encoder_cmd *p=arg;
  1271. if (!vfd->vidioc_try_encoder_cmd)
  1272. break;
  1273. ret=vfd->vidioc_try_encoder_cmd(file, fh, p);
  1274. if (!ret)
  1275. dbgarg (cmd, "cmd=%d, flags=%d\n",
  1276. p->cmd,p->flags);
  1277. break;
  1278. }
  1279. case VIDIOC_G_PARM:
  1280. {
  1281. struct v4l2_streamparm *p=arg;
  1282. __u32 type=p->type;
  1283. memset(p,0,sizeof(*p));
  1284. p->type=type;
  1285. if (vfd->vidioc_g_parm) {
  1286. ret=vfd->vidioc_g_parm(file, fh, p);
  1287. } else {
  1288. struct v4l2_standard s;
  1289. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1290. return -EINVAL;
  1291. v4l2_video_std_construct(&s, vfd->current_norm,
  1292. v4l2_norm_to_name(vfd->current_norm));
  1293. p->parm.capture.timeperframe = s.frameperiod;
  1294. ret=0;
  1295. }
  1296. dbgarg (cmd, "type=%d\n", p->type);
  1297. break;
  1298. }
  1299. case VIDIOC_S_PARM:
  1300. {
  1301. struct v4l2_streamparm *p=arg;
  1302. if (!vfd->vidioc_s_parm)
  1303. break;
  1304. dbgarg (cmd, "type=%d\n", p->type);
  1305. ret=vfd->vidioc_s_parm(file, fh, p);
  1306. break;
  1307. }
  1308. case VIDIOC_G_TUNER:
  1309. {
  1310. struct v4l2_tuner *p=arg;
  1311. __u32 index=p->index;
  1312. if (!vfd->vidioc_g_tuner)
  1313. break;
  1314. memset(p,0,sizeof(*p));
  1315. p->index=index;
  1316. ret=vfd->vidioc_g_tuner(file, fh, p);
  1317. if (!ret)
  1318. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  1319. "capability=%d, rangelow=%d, "
  1320. "rangehigh=%d, signal=%d, afc=%d, "
  1321. "rxsubchans=%d, audmode=%d\n",
  1322. p->index, p->name, p->type,
  1323. p->capability, p->rangelow,
  1324. p->rangehigh, p->rxsubchans,
  1325. p->audmode, p->signal, p->afc);
  1326. break;
  1327. }
  1328. case VIDIOC_S_TUNER:
  1329. {
  1330. struct v4l2_tuner *p=arg;
  1331. if (!vfd->vidioc_s_tuner)
  1332. break;
  1333. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  1334. "capability=%d, rangelow=%d, rangehigh=%d, "
  1335. "signal=%d, afc=%d, rxsubchans=%d, "
  1336. "audmode=%d\n",p->index, p->name, p->type,
  1337. p->capability, p->rangelow,p->rangehigh,
  1338. p->rxsubchans, p->audmode, p->signal,
  1339. p->afc);
  1340. ret=vfd->vidioc_s_tuner(file, fh, p);
  1341. break;
  1342. }
  1343. case VIDIOC_G_FREQUENCY:
  1344. {
  1345. struct v4l2_frequency *p=arg;
  1346. if (!vfd->vidioc_g_frequency)
  1347. break;
  1348. memset(p,0,sizeof(*p));
  1349. ret=vfd->vidioc_g_frequency(file, fh, p);
  1350. if (!ret)
  1351. dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
  1352. p->tuner,p->type,p->frequency);
  1353. break;
  1354. }
  1355. case VIDIOC_S_FREQUENCY:
  1356. {
  1357. struct v4l2_frequency *p=arg;
  1358. if (!vfd->vidioc_s_frequency)
  1359. break;
  1360. dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
  1361. p->tuner,p->type,p->frequency);
  1362. ret=vfd->vidioc_s_frequency(file, fh, p);
  1363. break;
  1364. }
  1365. case VIDIOC_G_SLICED_VBI_CAP:
  1366. {
  1367. struct v4l2_sliced_vbi_cap *p=arg;
  1368. if (!vfd->vidioc_g_sliced_vbi_cap)
  1369. break;
  1370. ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
  1371. if (!ret)
  1372. dbgarg (cmd, "service_set=%d\n", p->service_set);
  1373. break;
  1374. }
  1375. case VIDIOC_LOG_STATUS:
  1376. {
  1377. if (!vfd->vidioc_log_status)
  1378. break;
  1379. ret=vfd->vidioc_log_status(file, fh);
  1380. break;
  1381. }
  1382. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1383. case VIDIOC_DBG_G_REGISTER:
  1384. {
  1385. struct v4l2_register *p=arg;
  1386. if (!capable(CAP_SYS_ADMIN))
  1387. ret=-EPERM;
  1388. else if (vfd->vidioc_g_register)
  1389. ret=vfd->vidioc_g_register(file, fh, p);
  1390. break;
  1391. }
  1392. case VIDIOC_DBG_S_REGISTER:
  1393. {
  1394. struct v4l2_register *p=arg;
  1395. if (!capable(CAP_SYS_ADMIN))
  1396. ret=-EPERM;
  1397. else if (vfd->vidioc_s_register)
  1398. ret=vfd->vidioc_s_register(file, fh, p);
  1399. break;
  1400. }
  1401. #endif
  1402. case VIDIOC_G_CHIP_IDENT:
  1403. {
  1404. struct v4l2_chip_ident *p=arg;
  1405. if (!vfd->vidioc_g_chip_ident)
  1406. break;
  1407. ret=vfd->vidioc_g_chip_ident(file, fh, p);
  1408. if (!ret)
  1409. dbgarg (cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
  1410. break;
  1411. }
  1412. } /* switch */
  1413. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
  1414. if (ret<0) {
  1415. printk ("%s: err:\n", vfd->name);
  1416. v4l_print_ioctl(vfd->name, cmd);
  1417. }
  1418. }
  1419. return ret;
  1420. }
  1421. int video_ioctl2 (struct inode *inode, struct file *file,
  1422. unsigned int cmd, unsigned long arg)
  1423. {
  1424. char sbuf[128];
  1425. void *mbuf = NULL;
  1426. void *parg = NULL;
  1427. int err = -EINVAL;
  1428. int is_ext_ctrl;
  1429. size_t ctrls_size = 0;
  1430. void __user *user_ptr = NULL;
  1431. #ifdef __OLD_VIDIOC_
  1432. cmd = video_fix_command(cmd);
  1433. #endif
  1434. is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
  1435. cmd == VIDIOC_TRY_EXT_CTRLS);
  1436. /* Copy arguments into temp kernel buffer */
  1437. switch (_IOC_DIR(cmd)) {
  1438. case _IOC_NONE:
  1439. parg = NULL;
  1440. break;
  1441. case _IOC_READ:
  1442. case _IOC_WRITE:
  1443. case (_IOC_WRITE | _IOC_READ):
  1444. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  1445. parg = sbuf;
  1446. } else {
  1447. /* too big to allocate from stack */
  1448. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  1449. if (NULL == mbuf)
  1450. return -ENOMEM;
  1451. parg = mbuf;
  1452. }
  1453. err = -EFAULT;
  1454. if (_IOC_DIR(cmd) & _IOC_WRITE)
  1455. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  1456. goto out;
  1457. break;
  1458. }
  1459. if (is_ext_ctrl) {
  1460. struct v4l2_ext_controls *p = parg;
  1461. /* In case of an error, tell the caller that it wasn't
  1462. a specific control that caused it. */
  1463. p->error_idx = p->count;
  1464. user_ptr = (void __user *)p->controls;
  1465. if (p->count) {
  1466. ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
  1467. /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
  1468. mbuf = kmalloc(ctrls_size, GFP_KERNEL);
  1469. err = -ENOMEM;
  1470. if (NULL == mbuf)
  1471. goto out_ext_ctrl;
  1472. err = -EFAULT;
  1473. if (copy_from_user(mbuf, user_ptr, ctrls_size))
  1474. goto out_ext_ctrl;
  1475. p->controls = mbuf;
  1476. }
  1477. }
  1478. /* Handles IOCTL */
  1479. err = __video_do_ioctl(inode, file, cmd, parg);
  1480. if (err == -ENOIOCTLCMD)
  1481. err = -EINVAL;
  1482. if (is_ext_ctrl) {
  1483. struct v4l2_ext_controls *p = parg;
  1484. p->controls = (void *)user_ptr;
  1485. if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
  1486. err = -EFAULT;
  1487. goto out_ext_ctrl;
  1488. }
  1489. if (err < 0)
  1490. goto out;
  1491. out_ext_ctrl:
  1492. /* Copy results into user buffer */
  1493. switch (_IOC_DIR(cmd))
  1494. {
  1495. case _IOC_READ:
  1496. case (_IOC_WRITE | _IOC_READ):
  1497. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  1498. err = -EFAULT;
  1499. break;
  1500. }
  1501. out:
  1502. kfree(mbuf);
  1503. return err;
  1504. }
  1505. static const struct file_operations video_fops;
  1506. /**
  1507. * video_register_device - register video4linux devices
  1508. * @vfd: video device structure we want to register
  1509. * @type: type of device to register
  1510. * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
  1511. * -1 == first free)
  1512. *
  1513. * The registration code assigns minor numbers based on the type
  1514. * requested. -ENFILE is returned in all the device slots for this
  1515. * category are full. If not then the minor field is set and the
  1516. * driver initialize function is called (if non %NULL).
  1517. *
  1518. * Zero is returned on success.
  1519. *
  1520. * Valid types are
  1521. *
  1522. * %VFL_TYPE_GRABBER - A frame grabber
  1523. *
  1524. * %VFL_TYPE_VTX - A teletext device
  1525. *
  1526. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  1527. *
  1528. * %VFL_TYPE_RADIO - A radio card
  1529. */
  1530. int video_register_device(struct video_device *vfd, int type, int nr)
  1531. {
  1532. int i=0;
  1533. int base;
  1534. int end;
  1535. int ret;
  1536. char *name_base;
  1537. switch(type)
  1538. {
  1539. case VFL_TYPE_GRABBER:
  1540. base=MINOR_VFL_TYPE_GRABBER_MIN;
  1541. end=MINOR_VFL_TYPE_GRABBER_MAX+1;
  1542. name_base = "video";
  1543. break;
  1544. case VFL_TYPE_VTX:
  1545. base=MINOR_VFL_TYPE_VTX_MIN;
  1546. end=MINOR_VFL_TYPE_VTX_MAX+1;
  1547. name_base = "vtx";
  1548. break;
  1549. case VFL_TYPE_VBI:
  1550. base=MINOR_VFL_TYPE_VBI_MIN;
  1551. end=MINOR_VFL_TYPE_VBI_MAX+1;
  1552. name_base = "vbi";
  1553. break;
  1554. case VFL_TYPE_RADIO:
  1555. base=MINOR_VFL_TYPE_RADIO_MIN;
  1556. end=MINOR_VFL_TYPE_RADIO_MAX+1;
  1557. name_base = "radio";
  1558. break;
  1559. default:
  1560. printk(KERN_ERR "%s called with unknown type: %d\n",
  1561. __FUNCTION__, type);
  1562. return -1;
  1563. }
  1564. /* pick a minor number */
  1565. mutex_lock(&videodev_lock);
  1566. if (nr >= 0 && nr < end-base) {
  1567. /* use the one the driver asked for */
  1568. i = base+nr;
  1569. if (NULL != video_device[i]) {
  1570. mutex_unlock(&videodev_lock);
  1571. return -ENFILE;
  1572. }
  1573. } else {
  1574. /* use first free */
  1575. for(i=base;i<end;i++)
  1576. if (NULL == video_device[i])
  1577. break;
  1578. if (i == end) {
  1579. mutex_unlock(&videodev_lock);
  1580. return -ENFILE;
  1581. }
  1582. }
  1583. video_device[i]=vfd;
  1584. vfd->minor=i;
  1585. mutex_unlock(&videodev_lock);
  1586. mutex_init(&vfd->lock);
  1587. /* sysfs class */
  1588. memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
  1589. if (vfd->dev)
  1590. vfd->class_dev.dev = vfd->dev;
  1591. vfd->class_dev.class = &video_class;
  1592. vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
  1593. sprintf(vfd->class_dev.class_id, "%s%d", name_base, i - base);
  1594. ret = class_device_register(&vfd->class_dev);
  1595. if (ret < 0) {
  1596. printk(KERN_ERR "%s: class_device_register failed\n",
  1597. __FUNCTION__);
  1598. goto fail_minor;
  1599. }
  1600. ret = class_device_create_file(&vfd->class_dev, &class_device_attr_name);
  1601. if (ret < 0) {
  1602. printk(KERN_ERR "%s: class_device_create_file 'name' failed\n",
  1603. __FUNCTION__);
  1604. goto fail_classdev;
  1605. }
  1606. #if 1
  1607. /* needed until all drivers are fixed */
  1608. if (!vfd->release)
  1609. printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
  1610. "Please fix your driver for proper sysfs support, see "
  1611. "http://lwn.net/Articles/36850/\n", vfd->name);
  1612. #endif
  1613. return 0;
  1614. fail_classdev:
  1615. class_device_unregister(&vfd->class_dev);
  1616. fail_minor:
  1617. mutex_lock(&videodev_lock);
  1618. video_device[vfd->minor] = NULL;
  1619. vfd->minor = -1;
  1620. mutex_unlock(&videodev_lock);
  1621. return ret;
  1622. }
  1623. /**
  1624. * video_unregister_device - unregister a video4linux device
  1625. * @vfd: the device to unregister
  1626. *
  1627. * This unregisters the passed device and deassigns the minor
  1628. * number. Future open calls will be met with errors.
  1629. */
  1630. void video_unregister_device(struct video_device *vfd)
  1631. {
  1632. mutex_lock(&videodev_lock);
  1633. if(video_device[vfd->minor]!=vfd)
  1634. panic("videodev: bad unregister");
  1635. video_device[vfd->minor]=NULL;
  1636. class_device_unregister(&vfd->class_dev);
  1637. mutex_unlock(&videodev_lock);
  1638. }
  1639. /*
  1640. * Video fs operations
  1641. */
  1642. static const struct file_operations video_fops=
  1643. {
  1644. .owner = THIS_MODULE,
  1645. .llseek = no_llseek,
  1646. .open = video_open,
  1647. };
  1648. /*
  1649. * Initialise video for linux
  1650. */
  1651. static int __init videodev_init(void)
  1652. {
  1653. int ret;
  1654. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  1655. if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
  1656. printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
  1657. return -EIO;
  1658. }
  1659. ret = class_register(&video_class);
  1660. if (ret < 0) {
  1661. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  1662. printk(KERN_WARNING "video_dev: class_register failed\n");
  1663. return -EIO;
  1664. }
  1665. return 0;
  1666. }
  1667. static void __exit videodev_exit(void)
  1668. {
  1669. class_unregister(&video_class);
  1670. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  1671. }
  1672. module_init(videodev_init)
  1673. module_exit(videodev_exit)
  1674. EXPORT_SYMBOL(video_register_device);
  1675. EXPORT_SYMBOL(video_unregister_device);
  1676. EXPORT_SYMBOL(video_devdata);
  1677. EXPORT_SYMBOL(video_usercopy);
  1678. EXPORT_SYMBOL(video_exclusive_open);
  1679. EXPORT_SYMBOL(video_exclusive_release);
  1680. EXPORT_SYMBOL(video_ioctl2);
  1681. EXPORT_SYMBOL(video_device_alloc);
  1682. EXPORT_SYMBOL(video_device_release);
  1683. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  1684. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  1685. MODULE_LICENSE("GPL");
  1686. /*
  1687. * Local variables:
  1688. * c-basic-offset: 8
  1689. * End:
  1690. */