videodev.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  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. switch(cmd) {
  383. /* --- capabilities ------------------------------------------ */
  384. case VIDIOC_QUERYCAP:
  385. {
  386. struct v4l2_capability *cap = (struct v4l2_capability*)arg;
  387. memset(cap, 0, sizeof(*cap));
  388. if (!vfd->vidioc_querycap)
  389. break;
  390. ret=vfd->vidioc_querycap(file, fh, cap);
  391. if (!ret)
  392. dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
  393. "version=0x%08x, "
  394. "capabilities=0x%08x\n",
  395. cap->driver,cap->card,cap->bus_info,
  396. cap->version,
  397. cap->capabilities);
  398. break;
  399. }
  400. /* --- priority ------------------------------------------ */
  401. case VIDIOC_G_PRIORITY:
  402. {
  403. enum v4l2_priority *p=arg;
  404. if (!vfd->vidioc_g_priority)
  405. break;
  406. ret=vfd->vidioc_g_priority(file, fh, p);
  407. if (!ret)
  408. dbgarg(cmd, "priority is %d\n", *p);
  409. break;
  410. }
  411. case VIDIOC_S_PRIORITY:
  412. {
  413. enum v4l2_priority *p=arg;
  414. if (!vfd->vidioc_s_priority)
  415. break;
  416. dbgarg(cmd, "setting priority to %d\n", *p);
  417. ret=vfd->vidioc_s_priority(file, fh, *p);
  418. break;
  419. }
  420. /* --- capture ioctls ---------------------------------------- */
  421. case VIDIOC_ENUM_FMT:
  422. {
  423. struct v4l2_fmtdesc *f = arg;
  424. enum v4l2_buf_type type;
  425. unsigned int index;
  426. index = f->index;
  427. type = f->type;
  428. memset(f,0,sizeof(*f));
  429. f->index = index;
  430. f->type = type;
  431. switch (type) {
  432. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  433. if (vfd->vidioc_enum_fmt_cap)
  434. ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
  435. break;
  436. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  437. if (vfd->vidioc_enum_fmt_overlay)
  438. ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
  439. break;
  440. case V4L2_BUF_TYPE_VBI_CAPTURE:
  441. if (vfd->vidioc_enum_fmt_vbi)
  442. ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
  443. break;
  444. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  445. if (vfd->vidioc_enum_fmt_vbi_output)
  446. ret=vfd->vidioc_enum_fmt_vbi_output(file,
  447. fh, f);
  448. break;
  449. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  450. if (vfd->vidioc_enum_fmt_vbi_capture)
  451. ret=vfd->vidioc_enum_fmt_vbi_capture(file,
  452. fh, f);
  453. break;
  454. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  455. if (vfd->vidioc_enum_fmt_video_output)
  456. ret=vfd->vidioc_enum_fmt_video_output(file,
  457. fh, f);
  458. break;
  459. case V4L2_BUF_TYPE_VBI_OUTPUT:
  460. if (vfd->vidioc_enum_fmt_vbi_output)
  461. ret=vfd->vidioc_enum_fmt_vbi_output(file,
  462. fh, f);
  463. break;
  464. case V4L2_BUF_TYPE_PRIVATE:
  465. if (vfd->vidioc_enum_fmt_type_private)
  466. ret=vfd->vidioc_enum_fmt_type_private(file,
  467. fh, f);
  468. break;
  469. }
  470. if (!ret)
  471. dbgarg (cmd, "index=%d, type=%d, flags=%d, "
  472. "description=%s,"
  473. " pixelformat=0x%8x\n",
  474. f->index, f->type, f->flags,
  475. f->description,
  476. f->pixelformat);
  477. break;
  478. }
  479. case VIDIOC_G_FMT:
  480. {
  481. struct v4l2_format *f = (struct v4l2_format *)arg;
  482. enum v4l2_buf_type type=f->type;
  483. memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
  484. f->type=type;
  485. /* FIXME: Should be one dump per type */
  486. dbgarg (cmd, "type=%s\n", prt_names(type,
  487. v4l2_type_names_FIXME));
  488. switch (type) {
  489. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  490. if (vfd->vidioc_g_fmt_cap)
  491. ret=vfd->vidioc_g_fmt_cap(file, fh, f);
  492. if (!ret)
  493. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  494. break;
  495. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  496. if (vfd->vidioc_g_fmt_overlay)
  497. ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
  498. break;
  499. case V4L2_BUF_TYPE_VBI_CAPTURE:
  500. if (vfd->vidioc_g_fmt_vbi)
  501. ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
  502. break;
  503. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  504. if (vfd->vidioc_g_fmt_vbi_output)
  505. ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
  506. break;
  507. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  508. if (vfd->vidioc_g_fmt_vbi_capture)
  509. ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
  510. break;
  511. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  512. if (vfd->vidioc_g_fmt_video_output)
  513. ret=vfd->vidioc_g_fmt_video_output(file,
  514. fh, f);
  515. break;
  516. case V4L2_BUF_TYPE_VBI_OUTPUT:
  517. if (vfd->vidioc_g_fmt_vbi_output)
  518. ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
  519. break;
  520. case V4L2_BUF_TYPE_PRIVATE:
  521. if (vfd->vidioc_g_fmt_type_private)
  522. ret=vfd->vidioc_g_fmt_type_private(file,
  523. fh, f);
  524. break;
  525. }
  526. break;
  527. }
  528. case VIDIOC_S_FMT:
  529. {
  530. struct v4l2_format *f = (struct v4l2_format *)arg;
  531. /* FIXME: Should be one dump per type */
  532. dbgarg (cmd, "type=%s\n", prt_names(f->type,
  533. v4l2_type_names_FIXME));
  534. switch (f->type) {
  535. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  536. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  537. if (vfd->vidioc_s_fmt_cap)
  538. ret=vfd->vidioc_s_fmt_cap(file, fh, f);
  539. break;
  540. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  541. if (vfd->vidioc_s_fmt_overlay)
  542. ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
  543. break;
  544. case V4L2_BUF_TYPE_VBI_CAPTURE:
  545. if (vfd->vidioc_s_fmt_vbi)
  546. ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
  547. break;
  548. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  549. if (vfd->vidioc_s_fmt_vbi_output)
  550. ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
  551. break;
  552. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  553. if (vfd->vidioc_s_fmt_vbi_capture)
  554. ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
  555. break;
  556. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  557. if (vfd->vidioc_s_fmt_video_output)
  558. ret=vfd->vidioc_s_fmt_video_output(file,
  559. fh, f);
  560. break;
  561. case V4L2_BUF_TYPE_VBI_OUTPUT:
  562. if (vfd->vidioc_s_fmt_vbi_output)
  563. ret=vfd->vidioc_s_fmt_vbi_output(file,
  564. fh, f);
  565. break;
  566. case V4L2_BUF_TYPE_PRIVATE:
  567. if (vfd->vidioc_s_fmt_type_private)
  568. ret=vfd->vidioc_s_fmt_type_private(file,
  569. fh, f);
  570. break;
  571. }
  572. break;
  573. }
  574. case VIDIOC_TRY_FMT:
  575. {
  576. struct v4l2_format *f = (struct v4l2_format *)arg;
  577. /* FIXME: Should be one dump per type */
  578. dbgarg (cmd, "type=%s\n", prt_names(f->type,
  579. v4l2_type_names_FIXME));
  580. switch (f->type) {
  581. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  582. if (vfd->vidioc_try_fmt_cap)
  583. ret=vfd->vidioc_try_fmt_cap(file, fh, f);
  584. if (!ret)
  585. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  586. break;
  587. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  588. if (vfd->vidioc_try_fmt_overlay)
  589. ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
  590. break;
  591. case V4L2_BUF_TYPE_VBI_CAPTURE:
  592. if (vfd->vidioc_try_fmt_vbi)
  593. ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
  594. break;
  595. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  596. if (vfd->vidioc_try_fmt_vbi_output)
  597. ret=vfd->vidioc_try_fmt_vbi_output(file,
  598. fh, f);
  599. break;
  600. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  601. if (vfd->vidioc_try_fmt_vbi_capture)
  602. ret=vfd->vidioc_try_fmt_vbi_capture(file,
  603. fh, f);
  604. break;
  605. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  606. if (vfd->vidioc_try_fmt_video_output)
  607. ret=vfd->vidioc_try_fmt_video_output(file,
  608. fh, f);
  609. break;
  610. case V4L2_BUF_TYPE_VBI_OUTPUT:
  611. if (vfd->vidioc_try_fmt_vbi_output)
  612. ret=vfd->vidioc_try_fmt_vbi_output(file,
  613. fh, f);
  614. break;
  615. case V4L2_BUF_TYPE_PRIVATE:
  616. if (vfd->vidioc_try_fmt_type_private)
  617. ret=vfd->vidioc_try_fmt_type_private(file,
  618. fh, f);
  619. break;
  620. }
  621. break;
  622. }
  623. /* FIXME: Those buf reqs could be handled here,
  624. with some changes on videobuf to allow its header to be included at
  625. videodev2.h or being merged at videodev2.
  626. */
  627. case VIDIOC_REQBUFS:
  628. {
  629. struct v4l2_requestbuffers *p=arg;
  630. if (!vfd->vidioc_reqbufs)
  631. break;
  632. ret = check_fmt (vfd, p->type);
  633. if (ret)
  634. break;
  635. ret=vfd->vidioc_reqbufs(file, fh, p);
  636. dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
  637. p->count,
  638. prt_names(p->type,v4l2_type_names_FIXME),
  639. prt_names(p->memory,v4l2_memory_names));
  640. break;
  641. }
  642. case VIDIOC_QUERYBUF:
  643. {
  644. struct v4l2_buffer *p=arg;
  645. if (!vfd->vidioc_querybuf)
  646. break;
  647. ret = check_fmt (vfd, p->type);
  648. if (ret)
  649. break;
  650. ret=vfd->vidioc_querybuf(file, fh, p);
  651. if (!ret)
  652. dbgbuf(cmd,vfd,p);
  653. break;
  654. }
  655. case VIDIOC_QBUF:
  656. {
  657. struct v4l2_buffer *p=arg;
  658. if (!vfd->vidioc_qbuf)
  659. break;
  660. ret = check_fmt (vfd, p->type);
  661. if (ret)
  662. break;
  663. ret=vfd->vidioc_qbuf(file, fh, p);
  664. if (!ret)
  665. dbgbuf(cmd,vfd,p);
  666. break;
  667. }
  668. case VIDIOC_DQBUF:
  669. {
  670. struct v4l2_buffer *p=arg;
  671. if (!vfd->vidioc_dqbuf)
  672. break;
  673. ret = check_fmt (vfd, p->type);
  674. if (ret)
  675. break;
  676. ret=vfd->vidioc_dqbuf(file, fh, p);
  677. if (!ret)
  678. dbgbuf(cmd,vfd,p);
  679. break;
  680. }
  681. case VIDIOC_OVERLAY:
  682. {
  683. int *i = arg;
  684. if (!vfd->vidioc_overlay)
  685. break;
  686. dbgarg (cmd, "value=%d\n",*i);
  687. ret=vfd->vidioc_overlay(file, fh, *i);
  688. break;
  689. }
  690. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  691. /* --- streaming capture ------------------------------------- */
  692. case VIDIOCGMBUF:
  693. {
  694. struct video_mbuf *p=arg;
  695. memset(p,0,sizeof(p));
  696. if (!vfd->vidiocgmbuf)
  697. break;
  698. ret=vfd->vidiocgmbuf(file, fh, p);
  699. if (!ret)
  700. dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
  701. p->size, p->frames,
  702. (unsigned long)p->offsets);
  703. break;
  704. }
  705. #endif
  706. case VIDIOC_G_FBUF:
  707. {
  708. struct v4l2_framebuffer *p=arg;
  709. if (!vfd->vidioc_g_fbuf)
  710. break;
  711. ret=vfd->vidioc_g_fbuf(file, fh, arg);
  712. if (!ret) {
  713. dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
  714. p->capability,p->flags,
  715. (unsigned long)p->base);
  716. v4l_print_pix_fmt (vfd, &p->fmt);
  717. }
  718. break;
  719. }
  720. case VIDIOC_S_FBUF:
  721. {
  722. struct v4l2_framebuffer *p=arg;
  723. if (!vfd->vidioc_s_fbuf)
  724. break;
  725. dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
  726. p->capability,p->flags,(unsigned long)p->base);
  727. v4l_print_pix_fmt (vfd, &p->fmt);
  728. ret=vfd->vidioc_s_fbuf(file, fh, arg);
  729. break;
  730. }
  731. case VIDIOC_STREAMON:
  732. {
  733. enum v4l2_buf_type i = *(int *)arg;
  734. if (!vfd->vidioc_streamon)
  735. break;
  736. dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
  737. ret=vfd->vidioc_streamon(file, fh,i);
  738. break;
  739. }
  740. case VIDIOC_STREAMOFF:
  741. {
  742. enum v4l2_buf_type i = *(int *)arg;
  743. if (!vfd->vidioc_streamoff)
  744. break;
  745. dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
  746. ret=vfd->vidioc_streamoff(file, fh, i);
  747. break;
  748. }
  749. /* ---------- tv norms ---------- */
  750. case VIDIOC_ENUMSTD:
  751. {
  752. struct v4l2_standard *p = arg;
  753. unsigned int index = p->index;
  754. if (!vfd->tvnormsize) {
  755. printk (KERN_WARNING "%s: no TV norms defined!\n",
  756. vfd->name);
  757. break;
  758. }
  759. if (index<0 || index >= vfd->tvnormsize) {
  760. ret=-EINVAL;
  761. break;
  762. }
  763. v4l2_video_std_construct(p, vfd->tvnorms[p->index].id,
  764. vfd->tvnorms[p->index].name);
  765. p->index = index;
  766. dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
  767. "framelines=%d\n", p->index,
  768. (unsigned long long)p->id, p->name,
  769. p->frameperiod.numerator,
  770. p->frameperiod.denominator,
  771. p->framelines);
  772. ret=0;
  773. break;
  774. }
  775. case VIDIOC_G_STD:
  776. {
  777. v4l2_std_id *id = arg;
  778. *id = vfd->current_norm;
  779. dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
  780. ret=0;
  781. break;
  782. }
  783. case VIDIOC_S_STD:
  784. {
  785. v4l2_std_id *id = arg;
  786. unsigned int i;
  787. if (!vfd->tvnormsize) {
  788. printk (KERN_WARNING "%s: no TV norms defined!\n",
  789. vfd->name);
  790. break;
  791. }
  792. dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
  793. /* First search for exact match */
  794. for (i = 0; i < vfd->tvnormsize; i++)
  795. if (*id == vfd->tvnorms[i].id)
  796. break;
  797. /* Then for a generic video std that contains desired std */
  798. if (i == vfd->tvnormsize)
  799. for (i = 0; i < vfd->tvnormsize; i++)
  800. if (*id & vfd->tvnorms[i].id)
  801. break;
  802. if (i == vfd->tvnormsize) {
  803. break;
  804. }
  805. /* Calls the specific handler */
  806. if (vfd->vidioc_s_std)
  807. ret=vfd->vidioc_s_std(file, fh, i);
  808. else
  809. ret=-EINVAL;
  810. /* Updates standard information */
  811. if (!ret)
  812. vfd->current_norm=*id;
  813. break;
  814. }
  815. case VIDIOC_QUERYSTD:
  816. {
  817. v4l2_std_id *p=arg;
  818. if (!vfd->vidioc_querystd)
  819. break;
  820. ret=vfd->vidioc_querystd(file, fh, arg);
  821. if (!ret)
  822. dbgarg (cmd, "detected std=%Lu\n",
  823. (unsigned long long)*p);
  824. break;
  825. }
  826. /* ------ input switching ---------- */
  827. /* FIXME: Inputs can be handled inside videodev2 */
  828. case VIDIOC_ENUMINPUT:
  829. {
  830. struct v4l2_input *p=arg;
  831. int i=p->index;
  832. if (!vfd->vidioc_enum_input)
  833. break;
  834. memset(p, 0, sizeof(*p));
  835. p->index=i;
  836. ret=vfd->vidioc_enum_input(file, fh, p);
  837. if (!ret)
  838. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  839. "audioset=%d, "
  840. "tuner=%d, std=%Ld, status=%d\n",
  841. p->index,p->name,p->type,p->audioset,
  842. p->tuner,
  843. (unsigned long long)p->std,
  844. p->status);
  845. break;
  846. }
  847. case VIDIOC_G_INPUT:
  848. {
  849. unsigned int *i = arg;
  850. if (!vfd->vidioc_g_input)
  851. break;
  852. ret=vfd->vidioc_g_input(file, fh, i);
  853. if (!ret)
  854. dbgarg (cmd, "value=%d\n",*i);
  855. break;
  856. }
  857. case VIDIOC_S_INPUT:
  858. {
  859. unsigned int *i = arg;
  860. if (!vfd->vidioc_s_input)
  861. break;
  862. dbgarg (cmd, "value=%d\n",*i);
  863. ret=vfd->vidioc_s_input(file, fh, *i);
  864. break;
  865. }
  866. /* ------ output switching ---------- */
  867. case VIDIOC_G_OUTPUT:
  868. {
  869. unsigned int *i = arg;
  870. if (!vfd->vidioc_g_output)
  871. break;
  872. ret=vfd->vidioc_g_output(file, fh, i);
  873. if (!ret)
  874. dbgarg (cmd, "value=%d\n",*i);
  875. break;
  876. }
  877. case VIDIOC_S_OUTPUT:
  878. {
  879. unsigned int *i = arg;
  880. if (!vfd->vidioc_s_output)
  881. break;
  882. dbgarg (cmd, "value=%d\n",*i);
  883. ret=vfd->vidioc_s_output(file, fh, *i);
  884. break;
  885. }
  886. /* --- controls ---------------------------------------------- */
  887. case VIDIOC_QUERYCTRL:
  888. {
  889. struct v4l2_queryctrl *p=arg;
  890. if (!vfd->vidioc_queryctrl)
  891. break;
  892. ret=vfd->vidioc_queryctrl(file, fh, p);
  893. if (!ret)
  894. dbgarg (cmd, "id=%d, type=%d, name=%s, "
  895. "min/max=%d/%d,"
  896. " step=%d, default=%d, flags=0x%08x\n",
  897. p->id,p->type,p->name,p->minimum,
  898. p->maximum,p->step,p->default_value,
  899. p->flags);
  900. break;
  901. }
  902. case VIDIOC_G_CTRL:
  903. {
  904. struct v4l2_control *p = arg;
  905. if (!vfd->vidioc_g_ctrl)
  906. break;
  907. dbgarg(cmd, "Enum for index=%d\n", p->id);
  908. ret=vfd->vidioc_g_ctrl(file, fh, p);
  909. if (!ret)
  910. dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
  911. break;
  912. }
  913. case VIDIOC_S_CTRL:
  914. {
  915. struct v4l2_control *p = arg;
  916. if (!vfd->vidioc_s_ctrl)
  917. break;
  918. dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
  919. ret=vfd->vidioc_s_ctrl(file, fh, p);
  920. break;
  921. }
  922. case VIDIOC_G_EXT_CTRLS:
  923. {
  924. struct v4l2_ext_controls *p = arg;
  925. if (vfd->vidioc_g_ext_ctrls) {
  926. dbgarg(cmd, "count=%d\n", p->count);
  927. ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
  928. }
  929. break;
  930. }
  931. case VIDIOC_S_EXT_CTRLS:
  932. {
  933. struct v4l2_ext_controls *p = arg;
  934. if (vfd->vidioc_s_ext_ctrls) {
  935. dbgarg(cmd, "count=%d\n", p->count);
  936. ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
  937. }
  938. break;
  939. }
  940. case VIDIOC_TRY_EXT_CTRLS:
  941. {
  942. struct v4l2_ext_controls *p = arg;
  943. if (vfd->vidioc_try_ext_ctrls) {
  944. dbgarg(cmd, "count=%d\n", p->count);
  945. ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
  946. }
  947. break;
  948. }
  949. case VIDIOC_QUERYMENU:
  950. {
  951. struct v4l2_querymenu *p=arg;
  952. if (!vfd->vidioc_querymenu)
  953. break;
  954. ret=vfd->vidioc_querymenu(file, fh, p);
  955. if (!ret)
  956. dbgarg (cmd, "id=%d, index=%d, name=%s\n",
  957. p->id,p->index,p->name);
  958. break;
  959. }
  960. /* --- audio ---------------------------------------------- */
  961. case VIDIOC_ENUMAUDIO:
  962. {
  963. struct v4l2_audio *p=arg;
  964. if (!vfd->vidioc_enumaudio)
  965. break;
  966. dbgarg(cmd, "Enum for index=%d\n", p->index);
  967. ret=vfd->vidioc_enumaudio(file, fh, p);
  968. if (!ret)
  969. dbgarg2("index=%d, name=%s, capability=%d, "
  970. "mode=%d\n",p->index,p->name,
  971. p->capability, p->mode);
  972. break;
  973. }
  974. case VIDIOC_G_AUDIO:
  975. {
  976. struct v4l2_audio *p=arg;
  977. __u32 index=p->index;
  978. if (!vfd->vidioc_g_audio)
  979. break;
  980. memset(p,0,sizeof(*p));
  981. p->index=index;
  982. dbgarg(cmd, "Get for index=%d\n", p->index);
  983. ret=vfd->vidioc_g_audio(file, fh, p);
  984. if (!ret)
  985. dbgarg2("index=%d, name=%s, capability=%d, "
  986. "mode=%d\n",p->index,
  987. p->name,p->capability, p->mode);
  988. break;
  989. }
  990. case VIDIOC_S_AUDIO:
  991. {
  992. struct v4l2_audio *p=arg;
  993. if (!vfd->vidioc_s_audio)
  994. break;
  995. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  996. "mode=%d\n", p->index, p->name,
  997. p->capability, p->mode);
  998. ret=vfd->vidioc_s_audio(file, fh, p);
  999. break;
  1000. }
  1001. case VIDIOC_ENUMAUDOUT:
  1002. {
  1003. struct v4l2_audioout *p=arg;
  1004. if (!vfd->vidioc_enumaudout)
  1005. break;
  1006. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1007. ret=vfd->vidioc_enumaudout(file, fh, p);
  1008. if (!ret)
  1009. dbgarg2("index=%d, name=%s, capability=%d, "
  1010. "mode=%d\n", p->index, p->name,
  1011. p->capability,p->mode);
  1012. break;
  1013. }
  1014. case VIDIOC_G_AUDOUT:
  1015. {
  1016. struct v4l2_audioout *p=arg;
  1017. if (!vfd->vidioc_g_audout)
  1018. break;
  1019. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1020. ret=vfd->vidioc_g_audout(file, fh, p);
  1021. if (!ret)
  1022. dbgarg2("index=%d, name=%s, capability=%d, "
  1023. "mode=%d\n", p->index, p->name,
  1024. p->capability,p->mode);
  1025. break;
  1026. }
  1027. case VIDIOC_S_AUDOUT:
  1028. {
  1029. struct v4l2_audioout *p=arg;
  1030. if (!vfd->vidioc_s_audout)
  1031. break;
  1032. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1033. "mode=%d\n", p->index, p->name,
  1034. p->capability,p->mode);
  1035. ret=vfd->vidioc_s_audout(file, fh, p);
  1036. break;
  1037. }
  1038. case VIDIOC_G_MODULATOR:
  1039. {
  1040. struct v4l2_modulator *p=arg;
  1041. if (!vfd->vidioc_g_modulator)
  1042. break;
  1043. ret=vfd->vidioc_g_modulator(file, fh, p);
  1044. if (!ret)
  1045. dbgarg(cmd, "index=%d, name=%s, "
  1046. "capability=%d, rangelow=%d,"
  1047. " rangehigh=%d, txsubchans=%d\n",
  1048. p->index, p->name,p->capability,
  1049. p->rangelow, p->rangehigh,
  1050. p->txsubchans);
  1051. break;
  1052. }
  1053. case VIDIOC_S_MODULATOR:
  1054. {
  1055. struct v4l2_modulator *p=arg;
  1056. if (!vfd->vidioc_s_modulator)
  1057. break;
  1058. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1059. "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
  1060. p->index, p->name,p->capability,p->rangelow,
  1061. p->rangehigh,p->txsubchans);
  1062. ret=vfd->vidioc_s_modulator(file, fh, p);
  1063. break;
  1064. }
  1065. case VIDIOC_G_CROP:
  1066. {
  1067. struct v4l2_crop *p=arg;
  1068. if (!vfd->vidioc_g_crop)
  1069. break;
  1070. ret=vfd->vidioc_g_crop(file, fh, p);
  1071. if (!ret) {
  1072. dbgarg(cmd, "type=%d\n", p->type);
  1073. dbgrect(vfd, "", &p->c);
  1074. }
  1075. break;
  1076. }
  1077. case VIDIOC_S_CROP:
  1078. {
  1079. struct v4l2_crop *p=arg;
  1080. if (!vfd->vidioc_s_crop)
  1081. break;
  1082. dbgarg(cmd, "type=%d\n", p->type);
  1083. dbgrect(vfd, "", &p->c);
  1084. ret=vfd->vidioc_s_crop(file, fh, p);
  1085. break;
  1086. }
  1087. case VIDIOC_CROPCAP:
  1088. {
  1089. struct v4l2_cropcap *p=arg;
  1090. /*FIXME: Should also show v4l2_fract pixelaspect */
  1091. if (!vfd->vidioc_cropcap)
  1092. break;
  1093. dbgarg(cmd, "type=%d\n", p->type);
  1094. dbgrect(vfd, "bounds ", &p->bounds);
  1095. dbgrect(vfd, "defrect ", &p->defrect);
  1096. ret=vfd->vidioc_cropcap(file, fh, p);
  1097. break;
  1098. }
  1099. case VIDIOC_G_MPEGCOMP:
  1100. {
  1101. struct v4l2_mpeg_compression *p=arg;
  1102. /*FIXME: Several fields not shown */
  1103. if (!vfd->vidioc_g_mpegcomp)
  1104. break;
  1105. ret=vfd->vidioc_g_mpegcomp(file, fh, p);
  1106. if (!ret)
  1107. dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
  1108. " ts_pid_video=%d, ts_pid_pcr=%d, "
  1109. "ps_size=%d, au_sample_rate=%d, "
  1110. "au_pesid=%c, vi_frame_rate=%d, "
  1111. "vi_frames_per_gop=%d, "
  1112. "vi_bframes_count=%d, vi_pesid=%c\n",
  1113. p->ts_pid_pmt,p->ts_pid_audio,
  1114. p->ts_pid_video,p->ts_pid_pcr,
  1115. p->ps_size, p->au_sample_rate,
  1116. p->au_pesid, p->vi_frame_rate,
  1117. p->vi_frames_per_gop,
  1118. p->vi_bframes_count, p->vi_pesid);
  1119. break;
  1120. }
  1121. case VIDIOC_S_MPEGCOMP:
  1122. {
  1123. struct v4l2_mpeg_compression *p=arg;
  1124. /*FIXME: Several fields not shown */
  1125. if (!vfd->vidioc_s_mpegcomp)
  1126. break;
  1127. dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
  1128. "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
  1129. "au_sample_rate=%d, au_pesid=%c, "
  1130. "vi_frame_rate=%d, vi_frames_per_gop=%d, "
  1131. "vi_bframes_count=%d, vi_pesid=%c\n",
  1132. p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
  1133. p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
  1134. p->au_pesid, p->vi_frame_rate,
  1135. p->vi_frames_per_gop, p->vi_bframes_count,
  1136. p->vi_pesid);
  1137. ret=vfd->vidioc_s_mpegcomp(file, fh, p);
  1138. break;
  1139. }
  1140. case VIDIOC_G_JPEGCOMP:
  1141. {
  1142. struct v4l2_jpegcompression *p=arg;
  1143. if (!vfd->vidioc_g_jpegcomp)
  1144. break;
  1145. ret=vfd->vidioc_g_jpegcomp(file, fh, p);
  1146. if (!ret)
  1147. dbgarg (cmd, "quality=%d, APPn=%d, "
  1148. "APP_len=%d, COM_len=%d, "
  1149. "jpeg_markers=%d\n",
  1150. p->quality,p->APPn,p->APP_len,
  1151. p->COM_len,p->jpeg_markers);
  1152. break;
  1153. }
  1154. case VIDIOC_S_JPEGCOMP:
  1155. {
  1156. struct v4l2_jpegcompression *p=arg;
  1157. if (!vfd->vidioc_g_jpegcomp)
  1158. break;
  1159. dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
  1160. "COM_len=%d, jpeg_markers=%d\n",
  1161. p->quality,p->APPn,p->APP_len,
  1162. p->COM_len,p->jpeg_markers);
  1163. ret=vfd->vidioc_s_jpegcomp(file, fh, p);
  1164. break;
  1165. }
  1166. case VIDIOC_G_PARM:
  1167. {
  1168. struct v4l2_streamparm *p=arg;
  1169. if (vfd->vidioc_g_parm) {
  1170. ret=vfd->vidioc_g_parm(file, fh, p);
  1171. } else {
  1172. struct v4l2_standard s;
  1173. int i;
  1174. if (!vfd->tvnormsize) {
  1175. printk (KERN_WARNING "%s: no TV norms defined!\n",
  1176. vfd->name);
  1177. break;
  1178. }
  1179. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1180. return -EINVAL;
  1181. for (i = 0; i < vfd->tvnormsize; i++)
  1182. if (vfd->tvnorms[i].id == vfd->current_norm)
  1183. break;
  1184. if (i >= vfd->tvnormsize)
  1185. return -EINVAL;
  1186. v4l2_video_std_construct(&s, vfd->current_norm,
  1187. vfd->tvnorms[i].name);
  1188. memset(p,0,sizeof(*p));
  1189. p->parm.capture.timeperframe = s.frameperiod;
  1190. ret=0;
  1191. }
  1192. dbgarg (cmd, "type=%d\n", p->type);
  1193. break;
  1194. }
  1195. case VIDIOC_S_PARM:
  1196. {
  1197. struct v4l2_streamparm *p=arg;
  1198. if (!vfd->vidioc_s_parm)
  1199. break;
  1200. dbgarg (cmd, "type=%d\n", p->type);
  1201. ret=vfd->vidioc_s_parm(file, fh, p);
  1202. break;
  1203. }
  1204. case VIDIOC_G_TUNER:
  1205. {
  1206. struct v4l2_tuner *p=arg;
  1207. __u32 index=p->index;
  1208. if (!vfd->vidioc_g_tuner)
  1209. break;
  1210. memset(p,0,sizeof(*p));
  1211. p->index=index;
  1212. ret=vfd->vidioc_g_tuner(file, fh, p);
  1213. if (!ret)
  1214. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  1215. "capability=%d, rangelow=%d, "
  1216. "rangehigh=%d, signal=%d, afc=%d, "
  1217. "rxsubchans=%d, audmode=%d\n",
  1218. p->index, p->name, p->type,
  1219. p->capability, p->rangelow,
  1220. p->rangehigh, p->rxsubchans,
  1221. p->audmode, p->signal, p->afc);
  1222. break;
  1223. }
  1224. case VIDIOC_S_TUNER:
  1225. {
  1226. struct v4l2_tuner *p=arg;
  1227. if (!vfd->vidioc_s_tuner)
  1228. break;
  1229. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  1230. "capability=%d, rangelow=%d, rangehigh=%d, "
  1231. "signal=%d, afc=%d, rxsubchans=%d, "
  1232. "audmode=%d\n",p->index, p->name, p->type,
  1233. p->capability, p->rangelow,p->rangehigh,
  1234. p->rxsubchans, p->audmode, p->signal,
  1235. p->afc);
  1236. ret=vfd->vidioc_s_tuner(file, fh, p);
  1237. break;
  1238. }
  1239. case VIDIOC_G_FREQUENCY:
  1240. {
  1241. struct v4l2_frequency *p=arg;
  1242. if (!vfd->vidioc_g_frequency)
  1243. break;
  1244. memset(p,0,sizeof(*p));
  1245. ret=vfd->vidioc_g_frequency(file, fh, p);
  1246. if (!ret)
  1247. dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
  1248. p->tuner,p->type,p->frequency);
  1249. break;
  1250. }
  1251. case VIDIOC_S_FREQUENCY:
  1252. {
  1253. struct v4l2_frequency *p=arg;
  1254. if (!vfd->vidioc_s_frequency)
  1255. break;
  1256. dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
  1257. p->tuner,p->type,p->frequency);
  1258. ret=vfd->vidioc_s_frequency(file, fh, p);
  1259. break;
  1260. }
  1261. case VIDIOC_G_SLICED_VBI_CAP:
  1262. {
  1263. struct v4l2_sliced_vbi_cap *p=arg;
  1264. if (!vfd->vidioc_g_sliced_vbi_cap)
  1265. break;
  1266. ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
  1267. if (!ret)
  1268. dbgarg (cmd, "service_set=%d\n", p->service_set);
  1269. break;
  1270. }
  1271. case VIDIOC_LOG_STATUS:
  1272. {
  1273. if (!vfd->vidioc_log_status)
  1274. break;
  1275. ret=vfd->vidioc_log_status(file, fh);
  1276. break;
  1277. }
  1278. /* --- Others --------------------------------------------- */
  1279. default:
  1280. ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,__video_do_ioctl);
  1281. }
  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. */