linux/drivers/media/video/v4l2-common.c
<<
>>
Prefs
   1/*
   2 *      Video for Linux Two
   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 file replaces the videodev.c file that comes with the
   8 *      regular kernel distribution.
   9 *
  10 *      This program is free software; you can redistribute it and/or
  11 *      modify it under the terms of the GNU General Public License
  12 *      as published by the Free Software Foundation; either version
  13 *      2 of the License, or (at your option) any later version.
  14 *
  15 * Author:      Bill Dirks <bill@thedirks.org>
  16 *              based on code by Alan Cox, <alan@cymru.net>
  17 *
  18 */
  19
  20/*
  21 * Video capture interface for Linux
  22 *
  23 *      A generic video device interface for the LINUX operating system
  24 *      using a set of device structures/vectors for low level operations.
  25 *
  26 *              This program is free software; you can redistribute it and/or
  27 *              modify it under the terms of the GNU General Public License
  28 *              as published by the Free Software Foundation; either version
  29 *              2 of the License, or (at your option) any later version.
  30 *
  31 * Author:      Alan Cox, <alan@lxorguk.ukuu.org.uk>
  32 *
  33 * Fixes:
  34 */
  35
  36/*
  37 * Video4linux 1/2 integration by Justin Schoeman
  38 * <justin@suntiger.ee.up.ac.za>
  39 * 2.4 PROCFS support ported from 2.4 kernels by
  40 *  I\xC3\xB1aki Garc\xC3\xADa Etxebarria <garetxe@euskalnet.net>
  41 * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
  42 * 2.4 devfs support ported from 2.4 kernels by
  43 *  Dan Merillat <dan@merillat.org>
  44 * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
  45 */
  46
  47#include <linux/module.h>
  48#include <linux/types.h>
  49#include <linux/kernel.h>
  50#include <linux/mm.h>
  51#include <linux/string.h>
  52#include <linux/errno.h>
  53#include <linux/i2c.h>
  54#if defined(CONFIG_SPI)
  55#include <linux/spi/spi.h>
  56#endif
  57#include <asm/uaccess.h>
  58#include <asm/system.h>
  59#include <asm/pgtable.h>
  60#include <asm/io.h>
  61#include <asm/div64.h>
  62#define __OLD_VIDIOC_ /* To allow fixing old calls*/
  63#include <media/v4l2-common.h>
  64#include <media/v4l2-device.h>
  65#include <media/v4l2-chip-ident.h>
  66
  67#include <linux/videodev2.h>
  68
  69MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
  70MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
  71MODULE_LICENSE("GPL");
  72
  73/*
  74 *
  75 *      V 4 L 2   D R I V E R   H E L P E R   A P I
  76 *
  77 */
  78
  79/*
  80 *  Video Standard Operations (contributed by Michael Schimek)
  81 */
  82
  83
  84/* ----------------------------------------------------------------- */
  85/* priority handling                                                 */
  86
  87#define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND   || \
  88                              val == V4L2_PRIORITY_INTERACTIVE  || \
  89                              val == V4L2_PRIORITY_RECORD)
  90
  91void v4l2_prio_init(struct v4l2_prio_state *global)
  92{
  93        memset(global, 0, sizeof(*global));
  94}
  95EXPORT_SYMBOL(v4l2_prio_init);
  96
  97int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
  98                     enum v4l2_priority new)
  99{
 100        if (!V4L2_PRIO_VALID(new))
 101                return -EINVAL;
 102        if (*local == new)
 103                return 0;
 104
 105        atomic_inc(&global->prios[new]);
 106        if (V4L2_PRIO_VALID(*local))
 107                atomic_dec(&global->prios[*local]);
 108        *local = new;
 109        return 0;
 110}
 111EXPORT_SYMBOL(v4l2_prio_change);
 112
 113void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
 114{
 115        v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
 116}
 117EXPORT_SYMBOL(v4l2_prio_open);
 118
 119void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
 120{
 121        if (V4L2_PRIO_VALID(local))
 122                atomic_dec(&global->prios[local]);
 123}
 124EXPORT_SYMBOL(v4l2_prio_close);
 125
 126enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
 127{
 128        if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
 129                return V4L2_PRIORITY_RECORD;
 130        if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
 131                return V4L2_PRIORITY_INTERACTIVE;
 132        if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
 133                return V4L2_PRIORITY_BACKGROUND;
 134        return V4L2_PRIORITY_UNSET;
 135}
 136EXPORT_SYMBOL(v4l2_prio_max);
 137
 138int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
 139{
 140        return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
 141}
 142EXPORT_SYMBOL(v4l2_prio_check);
 143
 144/* ----------------------------------------------------------------- */
 145
 146/* Helper functions for control handling                             */
 147
 148/* Check for correctness of the ctrl's value based on the data from
 149   struct v4l2_queryctrl and the available menu items. Note that
 150   menu_items may be NULL, in that case it is ignored. */
 151int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
 152                const char **menu_items)
 153{
 154        if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
 155                return -EINVAL;
 156        if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
 157                return -EBUSY;
 158        if (qctrl->type == V4L2_CTRL_TYPE_STRING)
 159                return 0;
 160        if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
 161            qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
 162            qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
 163                return 0;
 164        if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
 165                return -ERANGE;
 166        if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
 167                if (menu_items[ctrl->value] == NULL ||
 168                    menu_items[ctrl->value][0] == '\0')
 169                        return -EINVAL;
 170        }
 171        return 0;
 172}
 173EXPORT_SYMBOL(v4l2_ctrl_check);
 174
 175/* Returns NULL or a character pointer array containing the menu for
 176   the given control ID. The pointer array ends with a NULL pointer.
 177   An empty string signifies a menu entry that is invalid. This allows
 178   drivers to disable certain options if it is not supported. */
 179const char **v4l2_ctrl_get_menu(u32 id)
 180{
 181        static const char *mpeg_audio_sampling_freq[] = {
 182                "44.1 kHz",
 183                "48 kHz",
 184                "32 kHz",
 185                NULL
 186        };
 187        static const char *mpeg_audio_encoding[] = {
 188                "MPEG-1/2 Layer I",
 189                "MPEG-1/2 Layer II",
 190                "MPEG-1/2 Layer III",
 191                "MPEG-2/4 AAC",
 192                "AC-3",
 193                NULL
 194        };
 195        static const char *mpeg_audio_l1_bitrate[] = {
 196                "32 kbps",
 197                "64 kbps",
 198                "96 kbps",
 199                "128 kbps",
 200                "160 kbps",
 201                "192 kbps",
 202                "224 kbps",
 203                "256 kbps",
 204                "288 kbps",
 205                "320 kbps",
 206                "352 kbps",
 207                "384 kbps",
 208                "416 kbps",
 209                "448 kbps",
 210                NULL
 211        };
 212        static const char *mpeg_audio_l2_bitrate[] = {
 213                "32 kbps",
 214                "48 kbps",
 215                "56 kbps",
 216                "64 kbps",
 217                "80 kbps",
 218                "96 kbps",
 219                "112 kbps",
 220                "128 kbps",
 221                "160 kbps",
 222                "192 kbps",
 223                "224 kbps",
 224                "256 kbps",
 225                "320 kbps",
 226                "384 kbps",
 227                NULL
 228        };
 229        static const char *mpeg_audio_l3_bitrate[] = {
 230                "32 kbps",
 231                "40 kbps",
 232                "48 kbps",
 233                "56 kbps",
 234                "64 kbps",
 235                "80 kbps",
 236                "96 kbps",
 237                "112 kbps",
 238                "128 kbps",
 239                "160 kbps",
 240                "192 kbps",
 241                "224 kbps",
 242                "256 kbps",
 243                "320 kbps",
 244                NULL
 245        };
 246        static const char *mpeg_audio_ac3_bitrate[] = {
 247                "32 kbps",
 248                "40 kbps",
 249                "48 kbps",
 250                "56 kbps",
 251                "64 kbps",
 252                "80 kbps",
 253                "96 kbps",
 254                "112 kbps",
 255                "128 kbps",
 256                "160 kbps",
 257                "192 kbps",
 258                "224 kbps",
 259                "256 kbps",
 260                "320 kbps",
 261                "384 kbps",
 262                "448 kbps",
 263                "512 kbps",
 264                "576 kbps",
 265                "640 kbps",
 266                NULL
 267        };
 268        static const char *mpeg_audio_mode[] = {
 269                "Stereo",
 270                "Joint Stereo",
 271                "Dual",
 272                "Mono",
 273                NULL
 274        };
 275        static const char *mpeg_audio_mode_extension[] = {
 276                "Bound 4",
 277                "Bound 8",
 278                "Bound 12",
 279                "Bound 16",
 280                NULL
 281        };
 282        static const char *mpeg_audio_emphasis[] = {
 283                "No Emphasis",
 284                "50/15 us",
 285                "CCITT J17",
 286                NULL
 287        };
 288        static const char *mpeg_audio_crc[] = {
 289                "No CRC",
 290                "16-bit CRC",
 291                NULL
 292        };
 293        static const char *mpeg_video_encoding[] = {
 294                "MPEG-1",
 295                "MPEG-2",
 296                "MPEG-4 AVC",
 297                NULL
 298        };
 299        static const char *mpeg_video_aspect[] = {
 300                "1x1",
 301                "4x3",
 302                "16x9",
 303                "2.21x1",
 304                NULL
 305        };
 306        static const char *mpeg_video_bitrate_mode[] = {
 307                "Variable Bitrate",
 308                "Constant Bitrate",
 309                NULL
 310        };
 311        static const char *mpeg_stream_type[] = {
 312                "MPEG-2 Program Stream",
 313                "MPEG-2 Transport Stream",
 314                "MPEG-1 System Stream",
 315                "MPEG-2 DVD-compatible Stream",
 316                "MPEG-1 VCD-compatible Stream",
 317                "MPEG-2 SVCD-compatible Stream",
 318                NULL
 319        };
 320        static const char *mpeg_stream_vbi_fmt[] = {
 321                "No VBI",
 322                "Private packet, IVTV format",
 323                NULL
 324        };
 325        static const char *camera_power_line_frequency[] = {
 326                "Disabled",
 327                "50 Hz",
 328                "60 Hz",
 329                NULL
 330        };
 331        static const char *camera_exposure_auto[] = {
 332                "Auto Mode",
 333                "Manual Mode",
 334                "Shutter Priority Mode",
 335                "Aperture Priority Mode",
 336                NULL
 337        };
 338        static const char *colorfx[] = {
 339                "None",
 340                "Black & White",
 341                "Sepia",
 342                "Negative",
 343                "Emboss",
 344                "Sketch",
 345                "Sky blue",
 346                "Grass green",
 347                "Skin whiten",
 348                "Vivid",
 349                NULL
 350        };
 351        static const char *tune_preemphasis[] = {
 352                "No preemphasis",
 353                "50 useconds",
 354                "75 useconds",
 355                NULL,
 356        };
 357
 358        switch (id) {
 359                case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
 360                        return mpeg_audio_sampling_freq;
 361                case V4L2_CID_MPEG_AUDIO_ENCODING:
 362                        return mpeg_audio_encoding;
 363                case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
 364                        return mpeg_audio_l1_bitrate;
 365                case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
 366                        return mpeg_audio_l2_bitrate;
 367                case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
 368                        return mpeg_audio_l3_bitrate;
 369                case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
 370                        return mpeg_audio_ac3_bitrate;
 371                case V4L2_CID_MPEG_AUDIO_MODE:
 372                        return mpeg_audio_mode;
 373                case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
 374                        return mpeg_audio_mode_extension;
 375                case V4L2_CID_MPEG_AUDIO_EMPHASIS:
 376                        return mpeg_audio_emphasis;
 377                case V4L2_CID_MPEG_AUDIO_CRC:
 378                        return mpeg_audio_crc;
 379                case V4L2_CID_MPEG_VIDEO_ENCODING:
 380                        return mpeg_video_encoding;
 381                case V4L2_CID_MPEG_VIDEO_ASPECT:
 382                        return mpeg_video_aspect;
 383                case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 384                        return mpeg_video_bitrate_mode;
 385                case V4L2_CID_MPEG_STREAM_TYPE:
 386                        return mpeg_stream_type;
 387                case V4L2_CID_MPEG_STREAM_VBI_FMT:
 388                        return mpeg_stream_vbi_fmt;
 389                case V4L2_CID_POWER_LINE_FREQUENCY:
 390                        return camera_power_line_frequency;
 391                case V4L2_CID_EXPOSURE_AUTO:
 392                        return camera_exposure_auto;
 393                case V4L2_CID_COLORFX:
 394                        return colorfx;
 395                case V4L2_CID_TUNE_PREEMPHASIS:
 396                        return tune_preemphasis;
 397                default:
 398                        return NULL;
 399        }
 400}
 401EXPORT_SYMBOL(v4l2_ctrl_get_menu);
 402
 403/* Return the control name. */
 404const char *v4l2_ctrl_get_name(u32 id)
 405{
 406        switch (id) {
 407        /* USER controls */
 408        case V4L2_CID_USER_CLASS:               return "User Controls";
 409        case V4L2_CID_BRIGHTNESS:               return "Brightness";
 410        case V4L2_CID_CONTRAST:                 return "Contrast";
 411        case V4L2_CID_SATURATION:               return "Saturation";
 412        case V4L2_CID_HUE:                      return "Hue";
 413        case V4L2_CID_AUDIO_VOLUME:             return "Volume";
 414        case V4L2_CID_AUDIO_BALANCE:            return "Balance";
 415        case V4L2_CID_AUDIO_BASS:               return "Bass";
 416        case V4L2_CID_AUDIO_TREBLE:             return "Treble";
 417        case V4L2_CID_AUDIO_MUTE:               return "Mute";
 418        case V4L2_CID_AUDIO_LOUDNESS:           return "Loudness";
 419        case V4L2_CID_BLACK_LEVEL:              return "Black Level";
 420        case V4L2_CID_AUTO_WHITE_BALANCE:       return "White Balance, Automatic";
 421        case V4L2_CID_DO_WHITE_BALANCE:         return "Do White Balance";
 422        case V4L2_CID_RED_BALANCE:              return "Red Balance";
 423        case V4L2_CID_BLUE_BALANCE:             return "Blue Balance";
 424        case V4L2_CID_GAMMA:                    return "Gamma";
 425        case V4L2_CID_EXPOSURE:                 return "Exposure";
 426        case V4L2_CID_AUTOGAIN:                 return "Gain, Automatic";
 427        case V4L2_CID_GAIN:                     return "Gain";
 428        case V4L2_CID_HFLIP:                    return "Horizontal Flip";
 429        case V4L2_CID_VFLIP:                    return "Vertical Flip";
 430        case V4L2_CID_HCENTER:                  return "Horizontal Center";
 431        case V4L2_CID_VCENTER:                  return "Vertical Center";
 432        case V4L2_CID_POWER_LINE_FREQUENCY:     return "Power Line Frequency";
 433        case V4L2_CID_HUE_AUTO:                 return "Hue, Automatic";
 434        case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
 435        case V4L2_CID_SHARPNESS:                return "Sharpness";
 436        case V4L2_CID_BACKLIGHT_COMPENSATION:   return "Backlight Compensation";
 437        case V4L2_CID_CHROMA_AGC:               return "Chroma AGC";
 438        case V4L2_CID_CHROMA_GAIN:              return "Chroma Gain";
 439        case V4L2_CID_COLOR_KILLER:             return "Color Killer";
 440        case V4L2_CID_COLORFX:                  return "Color Effects";
 441        case V4L2_CID_AUTOBRIGHTNESS:           return "Brightness, Automatic";
 442        case V4L2_CID_BAND_STOP_FILTER:         return "Band-Stop Filter";
 443        case V4L2_CID_ROTATE:                   return "Rotate";
 444        case V4L2_CID_BG_COLOR:                 return "Background Color";
 445
 446        /* MPEG controls */
 447        case V4L2_CID_MPEG_CLASS:               return "MPEG Encoder Controls";
 448        case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
 449        case V4L2_CID_MPEG_AUDIO_ENCODING:      return "Audio Encoding";
 450        case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    return "Audio Layer I Bitrate";
 451        case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    return "Audio Layer II Bitrate";
 452        case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    return "Audio Layer III Bitrate";
 453        case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:   return "Audio AAC Bitrate";
 454        case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:   return "Audio AC-3 Bitrate";
 455        case V4L2_CID_MPEG_AUDIO_MODE:          return "Audio Stereo Mode";
 456        case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
 457        case V4L2_CID_MPEG_AUDIO_EMPHASIS:      return "Audio Emphasis";
 458        case V4L2_CID_MPEG_AUDIO_CRC:           return "Audio CRC";
 459        case V4L2_CID_MPEG_AUDIO_MUTE:          return "Audio Mute";
 460        case V4L2_CID_MPEG_VIDEO_ENCODING:      return "Video Encoding";
 461        case V4L2_CID_MPEG_VIDEO_ASPECT:        return "Video Aspect";
 462        case V4L2_CID_MPEG_VIDEO_B_FRAMES:      return "Video B Frames";
 463        case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      return "Video GOP Size";
 464        case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   return "Video GOP Closure";
 465        case V4L2_CID_MPEG_VIDEO_PULLDOWN:      return "Video Pulldown";
 466        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  return "Video Bitrate Mode";
 467        case V4L2_CID_MPEG_VIDEO_BITRATE:       return "Video Bitrate";
 468        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  return "Video Peak Bitrate";
 469        case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
 470        case V4L2_CID_MPEG_VIDEO_MUTE:          return "Video Mute";
 471        case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      return "Video Mute YUV";
 472        case V4L2_CID_MPEG_STREAM_TYPE:         return "Stream Type";
 473        case V4L2_CID_MPEG_STREAM_PID_PMT:      return "Stream PMT Program ID";
 474        case V4L2_CID_MPEG_STREAM_PID_AUDIO:    return "Stream Audio Program ID";
 475        case V4L2_CID_MPEG_STREAM_PID_VIDEO:    return "Stream Video Program ID";
 476        case V4L2_CID_MPEG_STREAM_PID_PCR:      return "Stream PCR Program ID";
 477        case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
 478        case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
 479        case V4L2_CID_MPEG_STREAM_VBI_FMT:      return "Stream VBI Format";
 480
 481        /* CAMERA controls */
 482        case V4L2_CID_CAMERA_CLASS:             return "Camera Controls";
 483        case V4L2_CID_EXPOSURE_AUTO:            return "Auto Exposure";
 484        case V4L2_CID_EXPOSURE_ABSOLUTE:        return "Exposure Time, Absolute";
 485        case V4L2_CID_EXPOSURE_AUTO_PRIORITY:   return "Exposure, Dynamic Framerate";
 486        case V4L2_CID_PAN_RELATIVE:             return "Pan, Relative";
 487        case V4L2_CID_TILT_RELATIVE:            return "Tilt, Relative";
 488        case V4L2_CID_PAN_RESET:                return "Pan, Reset";
 489        case V4L2_CID_TILT_RESET:               return "Tilt, Reset";
 490        case V4L2_CID_PAN_ABSOLUTE:             return "Pan, Absolute";
 491        case V4L2_CID_TILT_ABSOLUTE:            return "Tilt, Absolute";
 492        case V4L2_CID_FOCUS_ABSOLUTE:           return "Focus, Absolute";
 493        case V4L2_CID_FOCUS_RELATIVE:           return "Focus, Relative";
 494        case V4L2_CID_FOCUS_AUTO:               return "Focus, Automatic";
 495        case V4L2_CID_IRIS_ABSOLUTE:            return "Iris, Absolute";
 496        case V4L2_CID_IRIS_RELATIVE:            return "Iris, Relative";
 497        case V4L2_CID_ZOOM_ABSOLUTE:            return "Zoom, Absolute";
 498        case V4L2_CID_ZOOM_RELATIVE:            return "Zoom, Relative";
 499        case V4L2_CID_ZOOM_CONTINUOUS:          return "Zoom, Continuous";
 500        case V4L2_CID_PRIVACY:                  return "Privacy";
 501
 502        /* FM Radio Modulator control */
 503        case V4L2_CID_FM_TX_CLASS:              return "FM Radio Modulator Controls";
 504        case V4L2_CID_RDS_TX_DEVIATION:         return "RDS Signal Deviation";
 505        case V4L2_CID_RDS_TX_PI:                return "RDS Program ID";
 506        case V4L2_CID_RDS_TX_PTY:               return "RDS Program Type";
 507        case V4L2_CID_RDS_TX_PS_NAME:           return "RDS PS Name";
 508        case V4L2_CID_RDS_TX_RADIO_TEXT:        return "RDS Radio Text";
 509        case V4L2_CID_AUDIO_LIMITER_ENABLED:    return "Audio Limiter Feature Enabled";
 510        case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
 511        case V4L2_CID_AUDIO_LIMITER_DEVIATION:  return "Audio Limiter Deviation";
 512        case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
 513        case V4L2_CID_AUDIO_COMPRESSION_GAIN:   return "Audio Compression Gain";
 514        case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
 515        case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
 516        case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
 517        case V4L2_CID_PILOT_TONE_ENABLED:       return "Pilot Tone Feature Enabled";
 518        case V4L2_CID_PILOT_TONE_DEVIATION:     return "Pilot Tone Deviation";
 519        case V4L2_CID_PILOT_TONE_FREQUENCY:     return "Pilot Tone Frequency";
 520        case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
 521        case V4L2_CID_TUNE_POWER_LEVEL:         return "Tune Power Level";
 522        case V4L2_CID_TUNE_ANTENNA_CAPACITOR:   return "Tune Antenna Capacitor";
 523
 524        default:
 525                return NULL;
 526        }
 527}
 528EXPORT_SYMBOL(v4l2_ctrl_get_name);
 529
 530/* Fill in a struct v4l2_queryctrl */
 531int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
 532{
 533        const char *name = v4l2_ctrl_get_name(qctrl->id);
 534
 535        qctrl->flags = 0;
 536        if (name == NULL)
 537                return -EINVAL;
 538
 539        switch (qctrl->id) {
 540        case V4L2_CID_AUDIO_MUTE:
 541        case V4L2_CID_AUDIO_LOUDNESS:
 542        case V4L2_CID_AUTO_WHITE_BALANCE:
 543        case V4L2_CID_AUTOGAIN:
 544        case V4L2_CID_HFLIP:
 545        case V4L2_CID_VFLIP:
 546        case V4L2_CID_HUE_AUTO:
 547        case V4L2_CID_CHROMA_AGC:
 548        case V4L2_CID_COLOR_KILLER:
 549        case V4L2_CID_MPEG_AUDIO_MUTE:
 550        case V4L2_CID_MPEG_VIDEO_MUTE:
 551        case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
 552        case V4L2_CID_MPEG_VIDEO_PULLDOWN:
 553        case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
 554        case V4L2_CID_FOCUS_AUTO:
 555        case V4L2_CID_PRIVACY:
 556        case V4L2_CID_AUDIO_LIMITER_ENABLED:
 557        case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
 558        case V4L2_CID_PILOT_TONE_ENABLED:
 559                qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
 560                min = 0;
 561                max = step = 1;
 562                break;
 563        case V4L2_CID_PAN_RESET:
 564        case V4L2_CID_TILT_RESET:
 565                qctrl->type = V4L2_CTRL_TYPE_BUTTON;
 566                qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
 567                min = max = step = def = 0;
 568                break;
 569        case V4L2_CID_POWER_LINE_FREQUENCY:
 570        case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
 571        case V4L2_CID_MPEG_AUDIO_ENCODING:
 572        case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
 573        case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
 574        case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
 575        case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
 576        case V4L2_CID_MPEG_AUDIO_MODE:
 577        case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
 578        case V4L2_CID_MPEG_AUDIO_EMPHASIS:
 579        case V4L2_CID_MPEG_AUDIO_CRC:
 580        case V4L2_CID_MPEG_VIDEO_ENCODING:
 581        case V4L2_CID_MPEG_VIDEO_ASPECT:
 582        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 583        case V4L2_CID_MPEG_STREAM_TYPE:
 584        case V4L2_CID_MPEG_STREAM_VBI_FMT:
 585        case V4L2_CID_EXPOSURE_AUTO:
 586        case V4L2_CID_COLORFX:
 587        case V4L2_CID_TUNE_PREEMPHASIS:
 588                qctrl->type = V4L2_CTRL_TYPE_MENU;
 589                step = 1;
 590                break;
 591        case V4L2_CID_RDS_TX_PS_NAME:
 592        case V4L2_CID_RDS_TX_RADIO_TEXT:
 593                qctrl->type = V4L2_CTRL_TYPE_STRING;
 594                break;
 595        case V4L2_CID_USER_CLASS:
 596        case V4L2_CID_CAMERA_CLASS:
 597        case V4L2_CID_MPEG_CLASS:
 598        case V4L2_CID_FM_TX_CLASS:
 599                qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
 600                qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
 601                min = max = step = def = 0;
 602                break;
 603        case V4L2_CID_BG_COLOR:
 604                qctrl->type = V4L2_CTRL_TYPE_INTEGER;
 605                step = 1;
 606                min = 0;
 607                /* Max is calculated as RGB888 that is 2^24 */
 608                max = 0xFFFFFF;
 609                break;
 610        default:
 611                qctrl->type = V4L2_CTRL_TYPE_INTEGER;
 612                break;
 613        }
 614        switch (qctrl->id) {
 615        case V4L2_CID_MPEG_AUDIO_ENCODING:
 616        case V4L2_CID_MPEG_AUDIO_MODE:
 617        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 618        case V4L2_CID_MPEG_VIDEO_B_FRAMES:
 619        case V4L2_CID_MPEG_STREAM_TYPE:
 620                qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
 621                break;
 622        case V4L2_CID_AUDIO_VOLUME:
 623        case V4L2_CID_AUDIO_BALANCE:
 624        case V4L2_CID_AUDIO_BASS:
 625        case V4L2_CID_AUDIO_TREBLE:
 626        case V4L2_CID_BRIGHTNESS:
 627        case V4L2_CID_CONTRAST:
 628        case V4L2_CID_SATURATION:
 629        case V4L2_CID_HUE:
 630        case V4L2_CID_RED_BALANCE:
 631        case V4L2_CID_BLUE_BALANCE:
 632        case V4L2_CID_GAMMA:
 633        case V4L2_CID_SHARPNESS:
 634        case V4L2_CID_CHROMA_GAIN:
 635        case V4L2_CID_RDS_TX_DEVIATION:
 636        case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
 637        case V4L2_CID_AUDIO_LIMITER_DEVIATION:
 638        case V4L2_CID_AUDIO_COMPRESSION_GAIN:
 639        case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
 640        case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
 641        case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
 642        case V4L2_CID_PILOT_TONE_DEVIATION:
 643        case V4L2_CID_PILOT_TONE_FREQUENCY:
 644        case V4L2_CID_TUNE_POWER_LEVEL:
 645        case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
 646                qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
 647                break;
 648        case V4L2_CID_PAN_RELATIVE:
 649        case V4L2_CID_TILT_RELATIVE:
 650        case V4L2_CID_FOCUS_RELATIVE:
 651        case V4L2_CID_IRIS_RELATIVE:
 652        case V4L2_CID_ZOOM_RELATIVE:
 653                qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
 654                break;
 655        }
 656        qctrl->minimum = min;
 657        qctrl->maximum = max;
 658        qctrl->step = step;
 659        qctrl->default_value = def;
 660        qctrl->reserved[0] = qctrl->reserved[1] = 0;
 661        strlcpy(qctrl->name, name, sizeof(qctrl->name));
 662        return 0;
 663}
 664EXPORT_SYMBOL(v4l2_ctrl_query_fill);
 665
 666/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
 667   the menu. The qctrl pointer may be NULL, in which case it is ignored.
 668   If menu_items is NULL, then the menu items are retrieved using
 669   v4l2_ctrl_get_menu. */
 670int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
 671               const char **menu_items)
 672{
 673        int i;
 674
 675        qmenu->reserved = 0;
 676        if (menu_items == NULL)
 677                menu_items = v4l2_ctrl_get_menu(qmenu->id);
 678        if (menu_items == NULL ||
 679            (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
 680                return -EINVAL;
 681        for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
 682        if (menu_items[i] == NULL || menu_items[i][0] == '\0')
 683                return -EINVAL;
 684        strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
 685        return 0;
 686}
 687EXPORT_SYMBOL(v4l2_ctrl_query_menu);
 688
 689/* Fill in a struct v4l2_querymenu based on the specified array of valid
 690   menu items (terminated by V4L2_CTRL_MENU_IDS_END).
 691   Use this if there are 'holes' in the list of valid menu items. */
 692int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
 693{
 694        const char **menu_items = v4l2_ctrl_get_menu(qmenu->id);
 695
 696        qmenu->reserved = 0;
 697        if (menu_items == NULL || ids == NULL)
 698                return -EINVAL;
 699        while (*ids != V4L2_CTRL_MENU_IDS_END) {
 700                if (*ids++ == qmenu->index) {
 701                        strlcpy(qmenu->name, menu_items[qmenu->index],
 702                                        sizeof(qmenu->name));
 703                        return 0;
 704                }
 705        }
 706        return -EINVAL;
 707}
 708EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
 709
 710/* ctrl_classes points to an array of u32 pointers, the last element is
 711   a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
 712   Each array must be sorted low to high and belong to the same control
 713   class. The array of u32 pointers must also be sorted, from low class IDs
 714   to high class IDs.
 715
 716   This function returns the first ID that follows after the given ID.
 717   When no more controls are available 0 is returned. */
 718u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
 719{
 720        u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
 721        const u32 *pctrl;
 722
 723        if (ctrl_classes == NULL)
 724                return 0;
 725
 726        /* if no query is desired, then check if the ID is part of ctrl_classes */
 727        if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
 728                /* find class */
 729                while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
 730                        ctrl_classes++;
 731                if (*ctrl_classes == NULL)
 732                        return 0;
 733                pctrl = *ctrl_classes;
 734                /* find control ID */
 735                while (*pctrl && *pctrl != id) pctrl++;
 736                return *pctrl ? id : 0;
 737        }
 738        id &= V4L2_CTRL_ID_MASK;
 739        id++;   /* select next control */
 740        /* find first class that matches (or is greater than) the class of
 741           the ID */
 742        while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
 743                ctrl_classes++;
 744        /* no more classes */
 745        if (*ctrl_classes == NULL)
 746                return 0;
 747        pctrl = *ctrl_classes;
 748        /* find first ctrl within the class that is >= ID */
 749        while (*pctrl && *pctrl < id) pctrl++;
 750        if (*pctrl)
 751                return *pctrl;
 752        /* we are at the end of the controls of the current class. */
 753        /* continue with next class if available */
 754        ctrl_classes++;
 755        if (*ctrl_classes == NULL)
 756                return 0;
 757        return **ctrl_classes;
 758}
 759EXPORT_SYMBOL(v4l2_ctrl_next);
 760
 761int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
 762{
 763        switch (match->type) {
 764        case V4L2_CHIP_MATCH_HOST:
 765                return match->addr == 0;
 766        default:
 767                return 0;
 768        }
 769}
 770EXPORT_SYMBOL(v4l2_chip_match_host);
 771
 772#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
 773int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
 774{
 775        int len;
 776
 777        if (c == NULL || match == NULL)
 778                return 0;
 779
 780        switch (match->type) {
 781        case V4L2_CHIP_MATCH_I2C_DRIVER:
 782                if (c->driver == NULL || c->driver->driver.name == NULL)
 783                        return 0;
 784                len = strlen(c->driver->driver.name);
 785                /* legacy drivers have a ' suffix, don't try to match that */
 786                if (len && c->driver->driver.name[len - 1] == '\'')
 787                        len--;
 788                return len && !strncmp(c->driver->driver.name, match->name, len);
 789        case V4L2_CHIP_MATCH_I2C_ADDR:
 790                return c->addr == match->addr;
 791        default:
 792                return 0;
 793        }
 794}
 795EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
 796
 797int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
 798                u32 ident, u32 revision)
 799{
 800        if (!v4l2_chip_match_i2c_client(c, &chip->match))
 801                return 0;
 802        if (chip->ident == V4L2_IDENT_NONE) {
 803                chip->ident = ident;
 804                chip->revision = revision;
 805        }
 806        else {
 807                chip->ident = V4L2_IDENT_AMBIGUOUS;
 808                chip->revision = 0;
 809        }
 810        return 0;
 811}
 812EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
 813
 814/* ----------------------------------------------------------------- */
 815
 816/* I2C Helper functions */
 817
 818
 819void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
 820                const struct v4l2_subdev_ops *ops)
 821{
 822        v4l2_subdev_init(sd, ops);
 823        sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
 824        /* the owner is the same as the i2c_client's driver owner */
 825        sd->owner = client->driver->driver.owner;
 826        /* i2c_client and v4l2_subdev point to one another */
 827        v4l2_set_subdevdata(sd, client);
 828        i2c_set_clientdata(client, sd);
 829        /* initialize name */
 830        snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
 831                client->driver->driver.name, i2c_adapter_id(client->adapter),
 832                client->addr);
 833}
 834EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
 835
 836
 837
 838/* Load an i2c sub-device. */
 839struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
 840                struct i2c_adapter *adapter, const char *module_name,
 841                struct i2c_board_info *info, const unsigned short *probe_addrs)
 842{
 843        struct v4l2_subdev *sd = NULL;
 844        struct i2c_client *client;
 845
 846        BUG_ON(!v4l2_dev);
 847
 848        if (module_name)
 849                request_module(module_name);
 850
 851        /* Create the i2c client */
 852        if (info->addr == 0 && probe_addrs)
 853                client = i2c_new_probed_device(adapter, info, probe_addrs);
 854        else
 855                client = i2c_new_device(adapter, info);
 856
 857        /* Note: by loading the module first we are certain that c->driver
 858           will be set if the driver was found. If the module was not loaded
 859           first, then the i2c core tries to delay-load the module for us,
 860           and then c->driver is still NULL until the module is finally
 861           loaded. This delay-load mechanism doesn't work if other drivers
 862           want to use the i2c device, so explicitly loading the module
 863           is the best alternative. */
 864        if (client == NULL || client->driver == NULL)
 865                goto error;
 866
 867        /* Lock the module so we can safely get the v4l2_subdev pointer */
 868        if (!try_module_get(client->driver->driver.owner))
 869                goto error;
 870        sd = i2c_get_clientdata(client);
 871
 872        /* Register with the v4l2_device which increases the module's
 873           use count as well. */
 874        if (v4l2_device_register_subdev(v4l2_dev, sd))
 875                sd = NULL;
 876        /* Decrease the module use count to match the first try_module_get. */
 877        module_put(client->driver->driver.owner);
 878
 879        if (sd) {
 880                /* We return errors from v4l2_subdev_call only if we have the
 881                   callback as the .s_config is not mandatory */
 882                int err = v4l2_subdev_call(sd, core, s_config,
 883                                info->irq, info->platform_data);
 884
 885                if (err && err != -ENOIOCTLCMD) {
 886                        v4l2_device_unregister_subdev(sd);
 887                        sd = NULL;
 888                }
 889        }
 890
 891error:
 892        /* If we have a client but no subdev, then something went wrong and
 893           we must unregister the client. */
 894        if (client && sd == NULL)
 895                i2c_unregister_device(client);
 896        return sd;
 897}
 898EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
 899
 900struct v4l2_subdev *v4l2_i2c_new_subdev_cfg(struct v4l2_device *v4l2_dev,
 901                struct i2c_adapter *adapter,
 902                const char *module_name, const char *client_type,
 903                int irq, void *platform_data,
 904                u8 addr, const unsigned short *probe_addrs)
 905{
 906        struct i2c_board_info info;
 907
 908        /* Setup the i2c board info with the device type and
 909           the device address. */
 910        memset(&info, 0, sizeof(info));
 911        strlcpy(info.type, client_type, sizeof(info.type));
 912        info.addr = addr;
 913        info.irq = irq;
 914        info.platform_data = platform_data;
 915
 916        return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, module_name,
 917                        &info, probe_addrs);
 918}
 919EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_cfg);
 920
 921/* Return i2c client address of v4l2_subdev. */
 922unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
 923{
 924        struct i2c_client *client = v4l2_get_subdevdata(sd);
 925
 926        return client ? client->addr : I2C_CLIENT_END;
 927}
 928EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
 929
 930/* Return a list of I2C tuner addresses to probe. Use only if the tuner
 931   addresses are unknown. */
 932const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
 933{
 934        static const unsigned short radio_addrs[] = {
 935#if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE)
 936                0x10,
 937#endif
 938                0x60,
 939                I2C_CLIENT_END
 940        };
 941        static const unsigned short demod_addrs[] = {
 942                0x42, 0x43, 0x4a, 0x4b,
 943                I2C_CLIENT_END
 944        };
 945        static const unsigned short tv_addrs[] = {
 946                0x42, 0x43, 0x4a, 0x4b,         /* tda8290 */
 947                0x60, 0x61, 0x62, 0x63, 0x64,
 948                I2C_CLIENT_END
 949        };
 950
 951        switch (type) {
 952        case ADDRS_RADIO:
 953                return radio_addrs;
 954        case ADDRS_DEMOD:
 955                return demod_addrs;
 956        case ADDRS_TV:
 957                return tv_addrs;
 958        case ADDRS_TV_WITH_DEMOD:
 959                return tv_addrs + 4;
 960        }
 961        return NULL;
 962}
 963EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
 964
 965#endif /* defined(CONFIG_I2C) */
 966
 967#if defined(CONFIG_SPI)
 968
 969/* Load a spi sub-device. */
 970
 971void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
 972                const struct v4l2_subdev_ops *ops)
 973{
 974        v4l2_subdev_init(sd, ops);
 975        sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
 976        /* the owner is the same as the spi_device's driver owner */
 977        sd->owner = spi->dev.driver->owner;
 978        /* spi_device and v4l2_subdev point to one another */
 979        v4l2_set_subdevdata(sd, spi);
 980        spi_set_drvdata(spi, sd);
 981        /* initialize name */
 982        strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
 983}
 984EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
 985
 986struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
 987                struct spi_master *master, struct spi_board_info *info)
 988{
 989        struct v4l2_subdev *sd = NULL;
 990        struct spi_device *spi = NULL;
 991
 992        BUG_ON(!v4l2_dev);
 993
 994        if (info->modalias)
 995                request_module(info->modalias);
 996
 997        spi = spi_new_device(master, info);
 998
 999        if (spi == NULL || spi->dev.driver == NULL)
1000                goto error;
1001
1002        if (!try_module_get(spi->dev.driver->owner))
1003                goto error;
1004
1005        sd = spi_get_drvdata(spi);
1006
1007        /* Register with the v4l2_device which increases the module's
1008           use count as well. */
1009        if (v4l2_device_register_subdev(v4l2_dev, sd))
1010                sd = NULL;
1011
1012        /* Decrease the module use count to match the first try_module_get. */
1013        module_put(spi->dev.driver->owner);
1014
1015error:
1016        /* If we have a client but no subdev, then something went wrong and
1017           we must unregister the client. */
1018        if (spi && sd == NULL)
1019                spi_unregister_device(spi);
1020
1021        return sd;
1022}
1023EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
1024
1025#endif /* defined(CONFIG_SPI) */
1026
1027/* Clamp x to be between min and max, aligned to a multiple of 2^align.  min
1028 * and max don't have to be aligned, but there must be at least one valid
1029 * value.  E.g., min=17,max=31,align=4 is not allowed as there are no multiples
1030 * of 16 between 17 and 31.  */
1031static unsigned int clamp_align(unsigned int x, unsigned int min,
1032                                unsigned int max, unsigned int align)
1033{
1034        /* Bits that must be zero to be aligned */
1035        unsigned int mask = ~((1 << align) - 1);
1036
1037        /* Round to nearest aligned value */
1038        if (align)
1039                x = (x + (1 << (align - 1))) & mask;
1040
1041        /* Clamp to aligned value of min and max */
1042        if (x < min)
1043                x = (min + ~mask) & mask;
1044        else if (x > max)
1045                x = max & mask;
1046
1047        return x;
1048}
1049
1050/* Bound an image to have a width between wmin and wmax, and height between
1051 * hmin and hmax, inclusive.  Additionally, the width will be a multiple of
1052 * 2^walign, the height will be a multiple of 2^halign, and the overall size
1053 * (width*height) will be a multiple of 2^salign.  The image may be shrunk
1054 * or enlarged to fit the alignment constraints.
1055 *
1056 * The width or height maximum must not be smaller than the corresponding
1057 * minimum.  The alignments must not be so high there are no possible image
1058 * sizes within the allowed bounds.  wmin and hmin must be at least 1
1059 * (don't use 0).  If you don't care about a certain alignment, specify 0,
1060 * as 2^0 is 1 and one byte alignment is equivalent to no alignment.  If
1061 * you only want to adjust downward, specify a maximum that's the same as
1062 * the initial value.
1063 */
1064void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
1065                           unsigned int walign,
1066                           u32 *h, unsigned int hmin, unsigned int hmax,
1067                           unsigned int halign, unsigned int salign)
1068{
1069        *w = clamp_align(*w, wmin, wmax, walign);
1070        *h = clamp_align(*h, hmin, hmax, halign);
1071
1072        /* Usually we don't need to align the size and are done now. */
1073        if (!salign)
1074                return;
1075
1076        /* How much alignment do we have? */
1077        walign = __ffs(*w);
1078        halign = __ffs(*h);
1079        /* Enough to satisfy the image alignment? */
1080        if (walign + halign < salign) {
1081                /* Max walign where there is still a valid width */
1082                unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
1083                /* Max halign where there is still a valid height */
1084                unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
1085
1086                /* up the smaller alignment until we have enough */
1087                do {
1088                        if (halign >= hmaxa ||
1089                            (walign <= halign && walign < wmaxa)) {
1090                                *w = clamp_align(*w, wmin, wmax, walign + 1);
1091                                walign = __ffs(*w);
1092                        } else {
1093                                *h = clamp_align(*h, hmin, hmax, halign + 1);
1094                                halign = __ffs(*h);
1095                        }
1096                } while (halign + walign < salign);
1097        }
1098}
1099EXPORT_SYMBOL_GPL(v4l_bound_align_image);
1100
1101/**
1102 * v4l_fill_dv_preset_info - fill description of a digital video preset
1103 * @preset - preset value
1104 * @info - pointer to struct v4l2_dv_enum_preset
1105 *
1106 * drivers can use this helper function to fill description of dv preset
1107 * in info.
1108 */
1109int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info)
1110{
1111        static const struct v4l2_dv_preset_info {
1112                u16 width;
1113                u16 height;
1114                const char *name;
1115        } dv_presets[] = {
1116                { 0, 0, "Invalid" },            /* V4L2_DV_INVALID */
1117                { 720,  480, "480p@59.94" },    /* V4L2_DV_480P59_94 */
1118                { 720,  576, "576p@50" },       /* V4L2_DV_576P50 */
1119                { 1280, 720, "720p@24" },       /* V4L2_DV_720P24 */
1120                { 1280, 720, "720p@25" },       /* V4L2_DV_720P25 */
1121                { 1280, 720, "720p@30" },       /* V4L2_DV_720P30 */
1122                { 1280, 720, "720p@50" },       /* V4L2_DV_720P50 */
1123                { 1280, 720, "720p@59.94" },    /* V4L2_DV_720P59_94 */
1124                { 1280, 720, "720p@60" },       /* V4L2_DV_720P60 */
1125                { 1920, 1080, "1080i@29.97" },  /* V4L2_DV_1080I29_97 */
1126                { 1920, 1080, "1080i@30" },     /* V4L2_DV_1080I30 */
1127                { 1920, 1080, "1080i@25" },     /* V4L2_DV_1080I25 */
1128                { 1920, 1080, "1080i@50" },     /* V4L2_DV_1080I50 */
1129                { 1920, 1080, "1080i@60" },     /* V4L2_DV_1080I60 */
1130                { 1920, 1080, "1080p@24" },     /* V4L2_DV_1080P24 */
1131                { 1920, 1080, "1080p@25" },     /* V4L2_DV_1080P25 */
1132                { 1920, 1080, "1080p@30" },     /* V4L2_DV_1080P30 */
1133                { 1920, 1080, "1080p@50" },     /* V4L2_DV_1080P50 */
1134                { 1920, 1080, "1080p@60" },     /* V4L2_DV_1080P60 */
1135        };
1136
1137        if (info == NULL || preset >= ARRAY_SIZE(dv_presets))
1138                return -EINVAL;
1139
1140        info->preset = preset;
1141        info->width = dv_presets[preset].width;
1142        info->height = dv_presets[preset].height;
1143        strlcpy(info->name, dv_presets[preset].name, sizeof(info->name));
1144        return 0;
1145}
1146EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info);
1147