videodev.c 40 KB

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