四、s3cfb_ops变量详解
在上面的文字中,较为详细的解释了platform device相关的代码,通过上面的代码的执行,一个platform设备(framebuffer被当作了platform设备)就加载到内核中去了。就像一个PCI的网卡被加入到内核一样,不同的是PCI的网卡占用的是PCI总线,内核会直接支持它。而对于platform设备需要用上面软件的方法加载到内核,同PCI网卡一样,设备需要驱动程序,刚才只是将platform设备注册到内核中,现在它还需要驱动程序,本节中就来看看这些驱动。
4.1 struct fb_ops and s3cfb_ops
/*
* Frame buffer operations
*
* LOCKING NOTE: those functions must _ALL_ be called with the console
* semaphore held, this is the only suitable locking mechanism we have
* in 2.6. Some may be called at interrupt time at this point though.
*
* The exception to this is the debug related hooks. Putting the fb
* into a debug state (e.g. flipping to the kernel console) and restoring
* it must be done in a lock-free manner, so low level drivers should
* keep track of the initial console (if applicable) and may need to
* perform direct, unlocked hardware writes in these hooks.
*/
struct fb_ops {
/* open/release and usage marking */
struct module *owner;
int (*fb_open)(struct fb_info *info, int user);
int (*fb_release)(struct fb_info *info, int user);
/* For framebuffers with strange non linear layouts or that do not
* work with normal memory mapped access
*/
ssize_t (*fb_read)(struct fb_info *info, char __user *buf,
size_t count, loff_t *ppos);
ssize_t (*fb_write)(struct fb_info *info, const char __user *buf,
size_t count, loff_t *ppos);
/* checks var and eventually tweaks it to something supported,
* DO NOT MODIFY PAR */
int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
/* set the video mode according to info->var */
int (*fb_set_par)(struct fb_info *info);
/* set color register */
int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,
unsigned blue, unsigned transp, struct fb_info *info);
/* set color registers in batch */
int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);
/* blank display */
int (*fb_blank)(int blank, struct fb_info *info);
/* pan display */
int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);
/* Draws a rectangle */
void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);
/* Copy data from area to another */
void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);
/* Draws a image to the display */
void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);
/* Draws cursor */
int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
/* Rotates the display */
void (*fb_rotate)(struct fb_info *info, int angle);
/* wait for blit idle, optional */
int (*fb_sync)(struct fb_info *info);
/* perform fb specific ioctl (optional) */
int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,
unsigned long arg);
/* Handle 32bit compat ioctl (optional) */
int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,
unsigned long arg);
/* perform fb specific mmap */
int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);
/* get capability given var */
void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,
struct fb_var_screeninfo *var);
/* teardown any resources to do with this framebuffer */
void (*fb_destroy)(struct fb_info *info);
/* called at KDB enter and leave time to prepare the console */
int (*fb_debug_enter)(struct fb_info *info);
int (*fb_debug_leave)(struct fb_info *info);
};
struct fb_ops s3cfb_ops = {
.owner = THIS_MODULE,
.fb_check_var = s3cfb_check_var,
.fb_set_par = s3cfb_set_par,
.fb_blank = s3cfb_blank,
.fb_pan_display= s3cfb_pan_display,
.fb_setcolreg = s3cfb_setcolreg,
.fb_fillrect = cfb_fillrect,
.fb_copyarea = cfb_copyarea,
.fb_imageblit = cfb_imageblit,
.fb_cursor = soft_cursor,
.fb_ioctl = s3cfb_ioctl,
};
对于s3cfb_blank函数实现的功能非常简单,而且也有较详细的说明,因此对它的说明就省略了。s3cfb_setcolreg函数的功能是设置颜色寄存器。它需要6个参数,分别代表寄存器编号,红色,绿色,蓝色,透明和fb_info结构。
/**
* s3cfb_blank
* @blank_mode: the blank mode we want.
* @info: frame buffer structure that represents a single frame buffer
*
* Blank the screen if blank_mode != 0, else unblank. Return 0 if
* blanking succeeded, != 0 if un-/blanking failed due to e.g. a
* video mode which doesn't support it. Implements VESA suspend
* and powerdown modes on hardware that supports disabling hsync/vsync:
* blank_mode == 2: suspend vsync
* blank_mode == 3: suspend hsync
* blank_mode == 4: powerdown
*
* Returns negative errno on error, or zero on success.
*
*/
static int s3cfb_blank(int blank_mode, struct fb_info *info)
{
DPRINTK("blank(mode=%d, info=%p)\n", blank_mode, info);
switch (blank_mode) {
case VESA_NO_BLANKING:/* lcd on, backlight on */
s3cfb_set_lcd_power(1);
s3cfb_set_backlight_power(1);
break;
case VESA_VSYNC_SUSPEND: /* lcd on, backlight off */
case VESA_HSYNC_SUSPEND:
s3cfb_set_lcd_power(1);
s3cfb_set_backlight_power(0);
break;
case VESA_POWERDOWN: /* lcd and backlight off */
s3cfb_set_lcd_power(0);
s3cfb_set_backlight_power(0);
break;
default:
return -EINVAL;
}
return 0;
}
static int s3cfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green, unsigned int blue, unsigned int transp, struct fb_info *info)
{
s3cfb_info_t *fbi = (s3cfb_info_t *)info;
unsigned int val = 0;
switch (fbi->fb.fix.visual) {
case FB_VISUAL_TRUECOLOR:真彩色,使用了调色板
if (regno < 16) {
/* Fake palette of 16 colors */
unsigned int *pal = fbi->fb.pseudo_palette;
根据颜色值生成需要的数据
val = s3cfb_chan_to_field(red, fbi->fb.var.red);
val |= s3cfb_chan_to_field(green, fbi->fb.var.green);
val |= s3cfb_chan_to_field(blue, fbi->fb.var.blue);
val |= s3cfb_chan_to_field(transp, fbi->fb.var.transp);
pal[regno] = val;
}
break;
case FB_VISUAL_PSEUDOCOLOR:/* This means that the color format isn't 16, 24, 28 bpp. */
/* S3C6410 has 256 palette entries */
if (regno < 256) {
/* When var.bits_per_pixel is 8bp, then WIN0's palette is always set as 16 bit */
val = ((red >> 0) & 0xf800);
val |= ((green >> 5) & 0x07e0);
val |= ((blue >> 11) & 0x001f);
DPRINTK("index = %d, val = 0x%08x\n", regno, val);
s3cfb_update_palette(fbi, regno, val);更新相关寄存器
}
break;
default:
return 1; /* unknown type */
}
return 0;
}
下面来看看对编写应用层程序非常重要的一个函数s3cfb_ioctl,应用程序都是利用ioctl设置Frambuffer的。
int s3cfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
{
s3cfb_info_t *fbi = container_of(info, s3cfb_info_t, fb);
s3cfb_win_info_t win_info;
s3cfb_color_key_info_t colkey_info;
s3cfb_color_val_info_t colval_info;
s3cfb_dma_info_t dma_info;
s3cfb_next_info_t next_fb_info;
struct fb_var_screeninfo *var= &fbi->fb.var;
unsigned int crt, alpha_level, alpha_mode;
#if defined(CONFIG_S3C6410_PWM)
int brightness;
#endif
#if defined(CONFIG_FB_S3C_EXT_DOUBLE_BUFFERING)
unsigned int f_num_val;
#endif
#if defined(CONFIG_FB_S3C_EXT_VIRTUAL_SCREEN)
s3cfb_vs_info_t vs_info;
#endif
switch(cmd){
case S3CFB_GET_INFO:
dma_info.map_dma_f1 = fbi->map_dma_f1;
dma_info.map_dma_f2 = fbi->map_dma_f2;
if(copy_to_user((void *) arg, (const void *) &dma_info, sizeof(s3cfb_dma_info_t)))
return -EFAULT;
break;
case S3CFB_OSD_SET_INFO:
if (copy_from_user(&win_info, (s3cfb_win_info_t *) arg, sizeof(s3cfb_win_info_t)))
return -EFAULT;
s3cfb_init_win(fbi, win_info.bpp, win_info.left_x, win_info.top_y, win_info.width, win_info.height, OFF);
break;
case S3CFB_OSD_START:
s3cfb_onoff_win(fbi, ON);
break;
case S3CFB_OSD_STOP:
s3cfb_onoff_win(fbi, OFF);
break;
case S3CFB_OSD_ALPHA_UP:
alpha_level = readl(S3C_VIDOSD0C + (0x10 * fbi->win_id)) & 0xf;
if (alpha_level < S3CFB_MAX_ALPHA_LEVEL)
alpha_level++;
s3cfb_set_alpha_level(fbi, alpha_level, 1);
break;
case S3CFB_OSD_ALPHA_DOWN:
alpha_level = readl(S3C_VIDOSD0C + (0x10 * fbi->win_id)) & 0xf;
if (alpha_level > 0)
alpha_level--;
s3cfb_set_alpha_level(fbi, alpha_level, 1);
break;
case S3CFB_OSD_ALPHA0_SET:
alpha_level = (unsigned int) arg;
if (alpha_level > S3CFB_MAX_ALPHA_LEVEL)
alpha_level = S3CFB_MAX_ALPHA_LEVEL;
s3cfb_set_alpha_level(fbi, alpha_level, 0);
break;
case S3CFB_OSD_ALPHA1_SET:
alpha_level = (unsigned int) arg;
if (alpha_level > S3CFB_MAX_ALPHA_LEVEL)
alpha_level = S3CFB_MAX_ALPHA_LEVEL;
s3cfb_set_alpha_level(fbi, alpha_level, 1);
break;
case S3CFB_OSD_ALPHA_MODE:
alpha_mode = (unsigned int) arg;
s3cfb_set_alpha_mode(fbi, alpha_mode);
break;
case S3CFB_OSD_MOVE_LEFT:
if (var->xoffset > 0)
var->xoffset--;
s3cfb_set_win_position(fbi, var->xoffset, var->yoffset, var->xres, var->yres);
break;
case S3CFB_OSD_MOVE_RIGHT:
if (var->xoffset < (s3cfb_fimd.width - var->xres))
var->xoffset++;
s3cfb_set_win_position(fbi, var->xoffset, var->yoffset, var->xres, var->yres);
break;
case S3CFB_OSD_MOVE_UP:
if (var->yoffset > 0)
var->yoffset--;
s3cfb_set_win_position(fbi, var->xoffset, var->yoffset, var->xres, var->yres);
break;
case S3CFB_OSD_MOVE_DOWN:
if (var->yoffset < (s3cfb_fimd.height - var->yres))
var->yoffset++;
s3cfb_set_win_position(fbi, var->xoffset, var->yoffset, var->xres, var->yres);
break;
case FBIO_WAITFORVSYNC:
if (get_user(crt, (unsigned int __user *)arg))
return -EFAULT;
return s3cfb_wait_for_vsync();
case S3CFB_COLOR_KEY_START:
s3cfb_onoff_color_key(fbi, ON);
break;
case S3CFB_COLOR_KEY_STOP:
s3cfb_onoff_color_key(fbi, OFF);
break;
case S3CFB_COLOR_KEY_ALPHA_START:
s3cfb_onoff_color_key_alpha(fbi, ON);
break;
case S3CFB_COLOR_KEY_ALPHA_STOP:
s3cfb_onoff_color_key_alpha(fbi, OFF);
break;
case S3CFB_COLOR_KEY_SET_INFO:
if (copy_from_user(&colkey_info, (s3cfb_color_key_info_t *) arg, sizeof(s3cfb_color_key_info_t)))
return -EFAULT;
s3cfb_set_color_key_registers(fbi, colkey_info);
break;
case S3CFB_COLOR_KEY_VALUE:
if (copy_from_user(&colval_info, (s3cfb_color_val_info_t *) arg, sizeof(s3cfb_color_val_info_t)))
return -EFAULT;
s3cfb_set_color_value(fbi, colval_info);
break;
case S3CFB_SET_VSYNC_INT:
s3cfb_fimd.vidintcon0 &= ~S3C_VIDINTCON0_FRAMESEL0_MASK;
s3cfb_fimd.vidintcon0 |= S3C_VIDINTCON0_FRAMESEL0_VSYNC;
if (arg)
s3cfb_fimd.vidintcon0 |= S3C_VIDINTCON0_INTFRMEN_ENABLE;
else
s3cfb_fimd.vidintcon0 &= ~S3C_VIDINTCON0_INTFRMEN_ENABLE;
writel(s3cfb_fimd.vidintcon0, S3C_VIDINTCON0);
break;
case S3CFB_SET_NEXT_FB_INFO:
if (copy_from_user(&next_fb_info, (s3cfb_next_info_t *) arg, sizeof(s3cfb_next_info_t)))
return -EFAULT;
/* check arguments */
if ((next_fb_info.xres + next_fb_info.xoffset) > next_fb_info.xres_virtual ||
(next_fb_info.yres + next_fb_info.yoffset) > next_fb_info.yres_virtual ||
(next_fb_info.xres + next_fb_info.lcd_offset_x ) > s3cfb_fimd.width ||
(next_fb_info.yres + next_fb_info.lcd_offset_y ) > s3cfb_fimd.height) {
printk("Error : S3CFB_SET_NEXT_FB_INFO\n");
return -EINVAL;
}
fbi->next_fb_info = next_fb_info;
fbi->next_fb_info_change_req = 1;
break;
case S3CFB_GET_CURR_FB_INFO:
next_fb_info.phy_start_addr = fbi->fb.fix.smem_start;
next_fb_info.xres = fbi->fb.var.xres;
next_fb_info.yres = fbi->fb.var.yres;
next_fb_info.xres_virtual = fbi->fb.var.xres_virtual;
next_fb_info.yres_virtual = fbi->fb.var.yres_virtual;
next_fb_info.xoffset = fbi->fb.var.xoffset;
next_fb_info.yoffset = fbi->fb.var.yoffset;
next_fb_info.lcd_offset_x = fbi->lcd_offset_x;
next_fb_info.lcd_offset_y = fbi->lcd_offset_y;
if (copy_to_user((void *)arg, (s3cfb_next_info_t *) &next_fb_info, sizeof(s3cfb_next_info_t)))
return -EFAULT;
break;
case S3CFB_GET_BRIGHTNESS:
if (copy_to_user((void *)arg, (const void *) &s3cfb_fimd.brightness, sizeof(int)))
return -EFAULT;
break;
#if defined(CONFIG_S3C6410_PWM)
case S3CFB_SET_BRIGHTNESS:
if (copy_from_user(&brightness, (int *) arg, sizeof(int)))
return -EFAULT;
s3cfb_set_brightness(brightness);
break;
#endif
#if defined(CONFIG_FB_S3C_EXT_VIRTUAL_SCREEN)
case S3CFB_VS_START:
s3cfb_fimd.wincon0 &= ~(S3C_WINCONx_ENWIN_F_ENABLE);
writel(s3cfb_fimd.wincon0 | S3C_WINCONx_ENWIN_F_ENABLE, S3C_WINCON0);
fbi->fb.var.xoffset = s3cfb_fimd.xoffset;
fbi->fb.var.yoffset = s3cfb_fimd.yoffset;
break;
case S3CFB_VS_STOP:
s3cfb_fimd.vidw00add0b0 = fbi->screen_dma_f1;
s3cfb_fimd.vidw00add0b1 = fbi->screen_dma_f2;
fbi->fb.var.xoffset = 0;
fbi->fb.var.yoffset = 0;
writel(s3cfb_fimd.vidw00add0b0, S3C_VIDW00ADD0B0);
writel(s3cfb_fimd.vidw00add0b1, S3C_VIDW00ADD0B1);
break;
case S3CFB_VS_SET_INFO:
if (copy_from_user(&vs_info, (s3cfb_vs_info_t *) arg, sizeof(s3cfb_vs_info_t)))
return -EFAULT;
if (s3cfb_set_vs_info(vs_info)) {
printk("Error S3CFB_VS_SET_INFO\n");
return -EINVAL;
}
s3cfb_set_vs_registers(S3CFB_VS_SET);
fbi->fb.var.xoffset = s3cfb_fimd.xoffset;
fbi->fb.var.yoffset = s3cfb_fimd.yoffset;
break;
case S3CFB_VS_MOVE:
s3cfb_set_vs_registers(arg);
fbi->fb.var.xoffset = s3cfb_fimd.xoffset;
fbi->fb.var.yoffset = s3cfb_fimd.yoffset;
break;
#endif
#if defined(CONFIG_FB_S3C_EXT_DOUBLE_BUFFERING)
case S3CFB_GET_NUM:
if (copy_from_user((void *)&f_num_val, (const void *)arg, sizeof(u_int)))
return -EFAULT;
if (copy_to_user((void *)arg, (const void *) &f_num_val, sizeof(u_int)))
return -EFAULT;
break;
case S3CFB_CHANGE_REQ:
s3cfb_change_buff(0, (int) arg);
break;
#endif
default:
return -EINVAL;
}
return 0;
}