videodev.c 40 KB

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