60805e43914e52ecc3590870be2875e9f1f8e593
[sfrench/cifs-2.6.git] / drivers / video / fbdev / core / fb_io_fops.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/fb.h>
4 #include <linux/module.h>
5 #include <linux/uaccess.h>
6
7 ssize_t fb_io_read(struct fb_info *info, char __user *buf, size_t count, loff_t *ppos)
8 {
9         unsigned long p = *ppos;
10         u8 *buffer, *dst;
11         u8 __iomem *src;
12         int c, cnt = 0, err = 0;
13         unsigned long total_size, trailing;
14
15         if (!info->screen_base)
16                 return -ENODEV;
17
18         total_size = info->screen_size;
19
20         if (total_size == 0)
21                 total_size = info->fix.smem_len;
22
23         if (p >= total_size)
24                 return 0;
25
26         if (count >= total_size)
27                 count = total_size;
28
29         if (count + p > total_size)
30                 count = total_size - p;
31
32         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
33                          GFP_KERNEL);
34         if (!buffer)
35                 return -ENOMEM;
36
37         src = (u8 __iomem *) (info->screen_base + p);
38
39         if (info->fbops->fb_sync)
40                 info->fbops->fb_sync(info);
41
42         while (count) {
43                 c  = (count > PAGE_SIZE) ? PAGE_SIZE : count;
44                 dst = buffer;
45                 fb_memcpy_fromio(dst, src, c);
46                 dst += c;
47                 src += c;
48
49                 trailing = copy_to_user(buf, buffer, c);
50                 if (trailing == c) {
51                         err = -EFAULT;
52                         break;
53                 }
54                 c -= trailing;
55
56                 *ppos += c;
57                 buf += c;
58                 cnt += c;
59                 count -= c;
60         }
61
62         kfree(buffer);
63
64         return cnt ? cnt : err;
65 }
66 EXPORT_SYMBOL(fb_io_read);
67
68 ssize_t fb_io_write(struct fb_info *info, const char __user *buf, size_t count, loff_t *ppos)
69 {
70         unsigned long p = *ppos;
71         u8 *buffer, *src;
72         u8 __iomem *dst;
73         int c, cnt = 0, err = 0;
74         unsigned long total_size, trailing;
75
76         if (!info->screen_base)
77                 return -ENODEV;
78
79         total_size = info->screen_size;
80
81         if (total_size == 0)
82                 total_size = info->fix.smem_len;
83
84         if (p > total_size)
85                 return -EFBIG;
86
87         if (count > total_size) {
88                 err = -EFBIG;
89                 count = total_size;
90         }
91
92         if (count + p > total_size) {
93                 if (!err)
94                         err = -ENOSPC;
95
96                 count = total_size - p;
97         }
98
99         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
100                          GFP_KERNEL);
101         if (!buffer)
102                 return -ENOMEM;
103
104         dst = (u8 __iomem *) (info->screen_base + p);
105
106         if (info->fbops->fb_sync)
107                 info->fbops->fb_sync(info);
108
109         while (count) {
110                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
111                 src = buffer;
112
113                 trailing = copy_from_user(src, buf, c);
114                 if (trailing == c) {
115                         err = -EFAULT;
116                         break;
117                 }
118                 c -= trailing;
119
120                 fb_memcpy_toio(dst, src, c);
121                 dst += c;
122                 src += c;
123                 *ppos += c;
124                 buf += c;
125                 cnt += c;
126                 count -= c;
127         }
128
129         kfree(buffer);
130
131         return (cnt) ? cnt : err;
132 }
133 EXPORT_SYMBOL(fb_io_write);
134
135 int fb_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
136 {
137         unsigned long start = info->fix.smem_start;
138         u32 len = info->fix.smem_len;
139         unsigned long mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT;
140
141         /*
142          * This can be either the framebuffer mapping, or if pgoff points
143          * past it, the mmio mapping.
144          */
145         if (vma->vm_pgoff >= mmio_pgoff) {
146                 if (info->var.accel_flags)
147                         return -EINVAL;
148
149                 vma->vm_pgoff -= mmio_pgoff;
150                 start = info->fix.mmio_start;
151                 len = info->fix.mmio_len;
152         }
153
154         vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
155         vma->vm_page_prot = pgprot_framebuffer(vma->vm_page_prot, vma->vm_start,
156                                                vma->vm_end, start);
157
158         return vm_iomap_memory(vma, start, len);
159 }
160 EXPORT_SYMBOL(fb_io_mmap);
161
162 MODULE_DESCRIPTION("Fbdev helpers for framebuffers in I/O memory");
163 MODULE_LICENSE("GPL");