libexplain  1.4.D001
libexplain/iocontrol/vidioc_dqbuf.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2011, 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/errno.h>
00021 #include <libexplain/ac/linux/videodev2.h>
00022 #include <libexplain/ac/string.h>
00023 #include <libexplain/ac/sys/ioctl.h>
00024 
00025 #include <libexplain/buffer/efault.h>
00026 #include <libexplain/buffer/einval.h>
00027 #include <libexplain/buffer/enotsup.h>
00028 #include <libexplain/buffer/is_the_null_pointer.h>
00029 #include <libexplain/buffer/v4l2_buffer.h>
00030 #include <libexplain/buffer/v4l2_buf_type.h>
00031 #include <libexplain/iocontrol/generic.h>
00032 #include <libexplain/iocontrol/vidioc_dqbuf.h>
00033 #include <libexplain/is_efault.h>
00034 
00035 #ifdef VIDIOC_DQBUF
00036 
00037 
00038 static void
00039 print_data(const explain_iocontrol_t *p, explain_string_buffer_t *sb,
00040     int errnum, int fildes, int request, const void *data)
00041 {
00042     (void)p;
00043     (void)errnum;
00044     (void)fildes;
00045     (void)request;
00046     explain_buffer_v4l2_buffer(sb, data, 0);
00047 }
00048 
00049 
00050 static void
00051 print_explanation(const explain_iocontrol_t *p, explain_string_buffer_t *sb,
00052     int errnum, int fildes, int request, const void *data)
00053 {
00054     /*
00055      * Note: Linux:
00056      * drivers/media/video/stk-webcam.c can return EIO when it means ENODEV
00057      */
00058     switch (errnum)
00059     {
00060     case EINVAL:
00061         if (!data)
00062         {
00063             explain_buffer_is_the_null_pointer(sb, "data");
00064             return;
00065         }
00066 
00067         {
00068             const struct v4l2_buffer *arg = data;
00069 
00070             if (explain_is_efault_pointer(arg, sizeof(*arg)))
00071                 goto generic;
00072 
00073             /*
00074              * Check the type (Linux kernel always does this first).
00075              */
00076             switch (explain_v4l2_buf_type_check(fildes, arg->type))
00077             {
00078             case explain_v4l2_buf_type_check_no_idea:
00079             default:
00080                 break;
00081 
00082             case explain_v4l2_buf_type_check_notsup:
00083                 explain_buffer_enotsup_device(sb, "data->type");
00084                 return;
00085 
00086             case explain_v4l2_buf_type_check_nosuch:
00087                 explain_buffer_einval_vague(sb, "data->type");
00088                 return;
00089 
00090             case explain_v4l2_buf_type_check_ok:
00091                 break;
00092             }
00093 
00094             /*
00095              * Check the frame index
00096              */
00097             {
00098                 int             nframes;
00099 
00100                 nframes = explain_v4l2_buffer_get_nframes(fildes, arg->type);
00101                 if (nframes == 0)
00102                 {
00103                     explain_string_buffer_puts
00104                     (
00105                         sb,
00106                         /* FIXME: i18n */
00107                         "no frames have been allocated yet"
00108                     );
00109                     return;
00110                 }
00111                 else if (nframes > 0 && arg->index >= (unsigned)nframes)
00112                 {
00113                     explain_buffer_einval_out_of_range
00114                     (
00115                         sb,
00116                         "data->index",
00117                         0,
00118                         nframes - 1
00119                     );
00120                     return;
00121                 }
00122             }
00123         }
00124 
00125         /* no idea */
00126         goto generic;
00127 
00128     case EIO:
00129         explain_string_buffer_puts
00130         (
00131             sb,
00132             /* FIXME: i18n */
00133             "timeout waiting for frame to become ready"
00134         );
00135         break;
00136 
00137     case EFAULT:
00138         {
00139             const struct v4l2_buffer *arg;
00140 
00141             arg = data;
00142             if (explain_is_efault_pointer(arg, sizeof*arg))
00143             {
00144                 explain_buffer_efault(sb, "data");
00145                 return;
00146             }
00147 
00148             switch (arg->memory)
00149             {
00150             case V4L2_MEMORY_MMAP:
00151             case V4L2_MEMORY_OVERLAY:
00152             default:
00153                 break;
00154 
00155             case V4L2_MEMORY_USERPTR:
00156                 {
00157                     struct v4l2_buffer b;
00158 
00159                     /*
00160                      * Interestingly, some Linux drivers use the userptr given
00161                      * to it in VIDIOC_QBUF, and ignore the one given to this
00162                      * ioctl... which makes sense if throughput is the goal.
00163                      */
00164                     memset(&b, 0, sizeof(b));
00165                     b.index = arg->index;
00166                     b.type = arg->type;
00167                     if (ioctl(fildes, VIDIOC_QUERYBUF, &b) >= 0)
00168                     {
00169                         const void *vp = (const void *)b.m.userptr;
00170                         if (explain_is_efault_pointer(vp, b.bytesused))
00171                         {
00172                             explain_buffer_efault(sb, "data->m.userptr");
00173                             return;
00174                         }
00175                     }
00176                 }
00177                 break;
00178             }
00179         }
00180 
00181         /* no idea */
00182         goto generic;
00183 
00184     default:
00185         generic:
00186         explain_iocontrol_generic_print_explanation
00187         (
00188             p,
00189             sb,
00190             errnum,
00191             fildes,
00192             request,
00193             data
00194         );
00195         break;
00196     }
00197 }
00198 
00199 
00200 static void
00201 print_data_returned(const explain_iocontrol_t *p, explain_string_buffer_t *sb,
00202     int errnum, int fildes, int request, const void *data)
00203 {
00204     (void)p;
00205     (void)errnum;
00206     (void)fildes;
00207     (void)request;
00208     explain_buffer_v4l2_buffer(sb, data, 1);
00209 }
00210 
00211 
00212 const explain_iocontrol_t explain_iocontrol_vidioc_dqbuf =
00213 {
00214     "VIDIOC_DQBUF", /* name */
00215     VIDIOC_DQBUF, /* value */
00216     0, /* disambiguate */
00217     0, /* print_name */
00218     print_data,
00219     print_explanation,
00220     print_data_returned,
00221     sizeof(struct v4l2_buffer), /* data_size */
00222     "struct v4l2_buffer *", /* data_type */
00223     0, /* flags */
00224     __FILE__,
00225     __LINE__,
00226 };
00227 
00228 #else /* ndef VIDIOC_DQBUF */
00229 
00230 const explain_iocontrol_t explain_iocontrol_vidioc_dqbuf =
00231 {
00232     0, /* name */
00233     0, /* value */
00234     0, /* disambiguate */
00235     0, /* print_name */
00236     0, /* print_data */
00237     0, /* print_explanation */
00238     0, /* print_data_returned */
00239     0, /* data_size */
00240     0, /* data_type */
00241     0, /* flags */
00242     __FILE__,
00243     __LINE__,
00244 };
00245 
00246 #endif /* VIDIOC_DQBUF */
00247 
00248 /* vim: set ts=8 sw=4 et : */