Subversion Repositories Kolibri OS

Rev

Rev 3480 | Rev 4246 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <ddk.h>
  2. #include <linux/mm.h>
  3. #include <drm/drmP.h>
  4. #include <drm/i915_drm.h>
  5. #include "i915_drv.h"
  6. #include "intel_drv.h"
  7. #include <linux/hdmi.h>
  8.  
  9.  
  10. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  11. {
  12.     struct file *filep;
  13.     int count;
  14.  
  15.     filep = malloc(sizeof(*filep));
  16.  
  17.     if(unlikely(filep == NULL))
  18.         return ERR_PTR(-ENOMEM);
  19.  
  20.     count = size / PAGE_SIZE;
  21.  
  22.     filep->pages = kzalloc(sizeof(struct page *) * count, 0);
  23.     if(unlikely(filep->pages == NULL))
  24.     {
  25.         kfree(filep);
  26.         return ERR_PTR(-ENOMEM);
  27.     };
  28.  
  29.     filep->count     = count;
  30.     filep->allocated = 0;
  31.     filep->vma       = NULL;
  32.  
  33. //    printf("%s file %p pages %p count %d\n",
  34. //              __FUNCTION__,filep, filep->pages, count);
  35.  
  36.     return filep;
  37. }
  38.  
  39. struct page *shmem_read_mapping_page_gfp(struct file *filep,
  40.                                          pgoff_t index, gfp_t gfp)
  41. {
  42.     struct page *page;
  43.  
  44. //    dbgprintf("%s, file %p index %d\n", __FUNCTION__, filep, index);
  45.  
  46.     if(unlikely(index >= filep->count))
  47.         return ERR_PTR(-EINVAL);
  48.  
  49.     page = filep->pages[index];
  50.  
  51.     if(unlikely(page == NULL))
  52.     {
  53.         page = (struct page *)AllocPage();
  54.  
  55.         if(unlikely(page == NULL))
  56.             return ERR_PTR(-ENOMEM);
  57.  
  58.         filep->pages[index] = page;
  59.     };
  60.  
  61.     return page;
  62. };
  63.  
  64. unsigned long vm_mmap(struct file *file, unsigned long addr,
  65.          unsigned long len, unsigned long prot,
  66.          unsigned long flag, unsigned long offset)
  67. {
  68.     char *mem, *ptr;
  69.     int i;
  70.  
  71.     if (unlikely(offset + PAGE_ALIGN(len) < offset))
  72.         return -EINVAL;
  73.     if (unlikely(offset & ~PAGE_MASK))
  74.         return -EINVAL;
  75.  
  76.     mem = UserAlloc(len);
  77.     if(unlikely(mem == NULL))
  78.         return -ENOMEM;
  79.  
  80.     for(i = offset, ptr = mem; i < offset+len; i+= 4096, ptr+= 4096)
  81.     {
  82.         struct page *page;
  83.  
  84.         page = shmem_read_mapping_page_gfp(file, i/PAGE_SIZE,0);
  85.  
  86.         if (unlikely(IS_ERR(page)))
  87.             goto err;
  88.  
  89.         MapPage(ptr, (addr_t)page, PG_SHARED|PG_UW);
  90.     }
  91.  
  92.     return (unsigned long)mem;
  93. err:
  94.     UserFree(mem);
  95.     return -ENOMEM;
  96. };
  97.  
  98. void shmem_file_delete(struct file *filep)
  99. {
  100. //    printf("%s file %p pages %p count %d\n",
  101. //            __FUNCTION__, filep, filep->pages, filep->count);
  102.  
  103.     if(filep->pages)
  104.         kfree(filep->pages);
  105. }
  106.  
  107.  
  108.  
  109. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  110. {
  111.         while (bytes) {
  112.                 if (*start != value)
  113.                         return (void *)start;
  114.                 start++;
  115.                 bytes--;
  116.         }
  117.         return NULL;
  118. }
  119.  
  120. /**
  121.  * memchr_inv - Find an unmatching character in an area of memory.
  122.  * @start: The memory area
  123.  * @c: Find a character other than c
  124.  * @bytes: The size of the area.
  125.  *
  126.  * returns the address of the first character other than @c, or %NULL
  127.  * if the whole buffer contains just @c.
  128.  */
  129. void *memchr_inv(const void *start, int c, size_t bytes)
  130. {
  131.         u8 value = c;
  132.         u64 value64;
  133.         unsigned int words, prefix;
  134.  
  135.         if (bytes <= 16)
  136.                 return check_bytes8(start, value, bytes);
  137.  
  138.         value64 = value;
  139. #if defined(ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  140.         value64 *= 0x0101010101010101;
  141. #elif defined(ARCH_HAS_FAST_MULTIPLIER)
  142.         value64 *= 0x01010101;
  143.         value64 |= value64 << 32;
  144. #else
  145.         value64 |= value64 << 8;
  146.         value64 |= value64 << 16;
  147.         value64 |= value64 << 32;
  148. #endif
  149.  
  150.         prefix = (unsigned long)start % 8;
  151.         if (prefix) {
  152.                 u8 *r;
  153.  
  154.                 prefix = 8 - prefix;
  155.                 r = check_bytes8(start, value, prefix);
  156.                 if (r)
  157.                         return r;
  158.                 start += prefix;
  159.                 bytes -= prefix;
  160.         }
  161.  
  162.         words = bytes / 8;
  163.  
  164.         while (words) {
  165.                 if (*(u64 *)start != value64)
  166.                         return check_bytes8(start, value, 8);
  167.                 start += 8;
  168.                 words--;
  169.         }
  170.  
  171.         return check_bytes8(start, value, bytes % 8);
  172. }
  173.  
  174.  
  175.  
  176. int dma_map_sg(struct device *dev, struct scatterlist *sglist,
  177.                            int nelems, int dir)
  178. {
  179.     struct scatterlist *s;
  180.     int i;
  181.  
  182.     for_each_sg(sglist, s, nelems, i) {
  183.         s->dma_address = (dma_addr_t)sg_phys(s);
  184. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  185.         s->dma_length  = s->length;
  186. #endif
  187.     }
  188.  
  189.     return nelems;
  190. }
  191.  
  192.  
  193. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  194. {
  195.     int i;
  196.  
  197.     i = vsnprintf(buf, size, fmt, args);
  198.  
  199.     if (likely(i < size))
  200.             return i;
  201.     if (size != 0)
  202.             return size - 1;
  203.     return 0;
  204. }
  205.  
  206.  
  207. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  208. {
  209.         va_list args;
  210.         int i;
  211.  
  212.         va_start(args, fmt);
  213.         i = vscnprintf(buf, size, fmt, args);
  214.         va_end(args);
  215.  
  216.         return i;
  217. }
  218.  
  219.  
  220.  
  221. #define _U  0x01    /* upper */
  222. #define _L  0x02    /* lower */
  223. #define _D  0x04    /* digit */
  224. #define _C  0x08    /* cntrl */
  225. #define _P  0x10    /* punct */
  226. #define _S  0x20    /* white space (space/lf/tab) */
  227. #define _X  0x40    /* hex digit */
  228. #define _SP 0x80    /* hard space (0x20) */
  229.  
  230. extern const unsigned char _ctype[];
  231.  
  232. #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
  233.  
  234. #define isalnum(c)  ((__ismask(c)&(_U|_L|_D)) != 0)
  235. #define isalpha(c)  ((__ismask(c)&(_U|_L)) != 0)
  236. #define iscntrl(c)  ((__ismask(c)&(_C)) != 0)
  237. #define isdigit(c)  ((__ismask(c)&(_D)) != 0)
  238. #define isgraph(c)  ((__ismask(c)&(_P|_U|_L|_D)) != 0)
  239. #define islower(c)  ((__ismask(c)&(_L)) != 0)
  240. #define isprint(c)  ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
  241. #define ispunct(c)  ((__ismask(c)&(_P)) != 0)
  242. /* Note: isspace() must return false for %NUL-terminator */
  243. #define isspace(c)  ((__ismask(c)&(_S)) != 0)
  244. #define isupper(c)  ((__ismask(c)&(_U)) != 0)
  245. #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
  246.  
  247. #define isascii(c) (((unsigned char)(c))<=0x7f)
  248. #define toascii(c) (((unsigned char)(c))&0x7f)
  249.  
  250. static inline unsigned char __tolower(unsigned char c)
  251. {
  252.     if (isupper(c))
  253.         c -= 'A'-'a';
  254.     return c;
  255. }
  256.  
  257. static inline unsigned char __toupper(unsigned char c)
  258. {
  259.     if (islower(c))
  260.         c -= 'a'-'A';
  261.     return c;
  262. }
  263.  
  264. #define tolower(c) __tolower(c)
  265. #define toupper(c) __toupper(c)
  266.  
  267. /*
  268.  * Fast implementation of tolower() for internal usage. Do not use in your
  269.  * code.
  270.  */
  271. static inline char _tolower(const char c)
  272. {
  273.     return c | 0x20;
  274. }
  275.  
  276.  
  277.  
  278. //const char hex_asc[] = "0123456789abcdef";
  279.  
  280. /**
  281.  * hex_to_bin - convert a hex digit to its real value
  282.  * @ch: ascii character represents hex digit
  283.  *
  284.  * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  285.  * input.
  286.  */
  287. int hex_to_bin(char ch)
  288. {
  289.     if ((ch >= '0') && (ch <= '9'))
  290.         return ch - '0';
  291.     ch = tolower(ch);
  292.     if ((ch >= 'a') && (ch <= 'f'))
  293.         return ch - 'a' + 10;
  294.     return -1;
  295. }
  296. EXPORT_SYMBOL(hex_to_bin);
  297.  
  298. /**
  299.  * hex2bin - convert an ascii hexadecimal string to its binary representation
  300.  * @dst: binary result
  301.  * @src: ascii hexadecimal string
  302.  * @count: result length
  303.  *
  304.  * Return 0 on success, -1 in case of bad input.
  305.  */
  306. int hex2bin(u8 *dst, const char *src, size_t count)
  307. {
  308.     while (count--) {
  309.         int hi = hex_to_bin(*src++);
  310.         int lo = hex_to_bin(*src++);
  311.  
  312.         if ((hi < 0) || (lo < 0))
  313.             return -1;
  314.  
  315.         *dst++ = (hi << 4) | lo;
  316.     }
  317.     return 0;
  318. }
  319. EXPORT_SYMBOL(hex2bin);
  320.  
  321. /**
  322.  * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  323.  * @buf: data blob to dump
  324.  * @len: number of bytes in the @buf
  325.  * @rowsize: number of bytes to print per line; must be 16 or 32
  326.  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  327.  * @linebuf: where to put the converted data
  328.  * @linebuflen: total size of @linebuf, including space for terminating NUL
  329.  * @ascii: include ASCII after the hex output
  330.  *
  331.  * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  332.  * 16 or 32 bytes of input data converted to hex + ASCII output.
  333.  *
  334.  * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  335.  * to a hex + ASCII dump at the supplied memory location.
  336.  * The converted output is always NUL-terminated.
  337.  *
  338.  * E.g.:
  339.  *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
  340.  *          linebuf, sizeof(linebuf), true);
  341.  *
  342.  * example output buffer:
  343.  * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
  344.  */
  345. void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
  346.             int groupsize, char *linebuf, size_t linebuflen,
  347.             bool ascii)
  348. {
  349.     const u8 *ptr = buf;
  350.     u8 ch;
  351.     int j, lx = 0;
  352.     int ascii_column;
  353.  
  354.     if (rowsize != 16 && rowsize != 32)
  355.         rowsize = 16;
  356.  
  357.     if (!len)
  358.         goto nil;
  359.     if (len > rowsize)      /* limit to one line at a time */
  360.         len = rowsize;
  361.     if ((len % groupsize) != 0) /* no mixed size output */
  362.         groupsize = 1;
  363.  
  364.     switch (groupsize) {
  365.     case 8: {
  366.         const u64 *ptr8 = buf;
  367.         int ngroups = len / groupsize;
  368.  
  369.         for (j = 0; j < ngroups; j++)
  370.             lx += scnprintf(linebuf + lx, linebuflen - lx,
  371.                     "%s%16.16llx", j ? " " : "",
  372.                     (unsigned long long)*(ptr8 + j));
  373.         ascii_column = 17 * ngroups + 2;
  374.         break;
  375.     }
  376.  
  377.     case 4: {
  378.         const u32 *ptr4 = buf;
  379.         int ngroups = len / groupsize;
  380.  
  381.         for (j = 0; j < ngroups; j++)
  382.             lx += scnprintf(linebuf + lx, linebuflen - lx,
  383.                     "%s%8.8x", j ? " " : "", *(ptr4 + j));
  384.         ascii_column = 9 * ngroups + 2;
  385.         break;
  386.     }
  387.  
  388.     case 2: {
  389.         const u16 *ptr2 = buf;
  390.         int ngroups = len / groupsize;
  391.  
  392.         for (j = 0; j < ngroups; j++)
  393.             lx += scnprintf(linebuf + lx, linebuflen - lx,
  394.                     "%s%4.4x", j ? " " : "", *(ptr2 + j));
  395.         ascii_column = 5 * ngroups + 2;
  396.         break;
  397.     }
  398.  
  399.     default:
  400.         for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
  401.             ch = ptr[j];
  402.             linebuf[lx++] = hex_asc_hi(ch);
  403.             linebuf[lx++] = hex_asc_lo(ch);
  404.             linebuf[lx++] = ' ';
  405.         }
  406.         if (j)
  407.             lx--;
  408.  
  409.         ascii_column = 3 * rowsize + 2;
  410.         break;
  411.     }
  412.     if (!ascii)
  413.         goto nil;
  414.  
  415.     while (lx < (linebuflen - 1) && lx < (ascii_column - 1))
  416.         linebuf[lx++] = ' ';
  417.     for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
  418.         ch = ptr[j];
  419.         linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
  420.     }
  421. nil:
  422.     linebuf[lx++] = '\0';
  423. }
  424.  
  425. /**
  426.  * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  427.  * @level: kernel log level (e.g. KERN_DEBUG)
  428.  * @prefix_str: string to prefix each line with;
  429.  *  caller supplies trailing spaces for alignment if desired
  430.  * @prefix_type: controls whether prefix of an offset, address, or none
  431.  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  432.  * @rowsize: number of bytes to print per line; must be 16 or 32
  433.  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  434.  * @buf: data blob to dump
  435.  * @len: number of bytes in the @buf
  436.  * @ascii: include ASCII after the hex output
  437.  *
  438.  * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  439.  * to the kernel log at the specified kernel log level, with an optional
  440.  * leading prefix.
  441.  *
  442.  * print_hex_dump() works on one "line" of output at a time, i.e.,
  443.  * 16 or 32 bytes of input data converted to hex + ASCII output.
  444.  * print_hex_dump() iterates over the entire input @buf, breaking it into
  445.  * "line size" chunks to format and print.
  446.  *
  447.  * E.g.:
  448.  *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
  449.  *          16, 1, frame->data, frame->len, true);
  450.  *
  451.  * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
  452.  * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
  453.  * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
  454.  * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
  455.  */
  456. void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
  457.             int rowsize, int groupsize,
  458.             const void *buf, size_t len, bool ascii)
  459. {
  460.     const u8 *ptr = buf;
  461.     int i, linelen, remaining = len;
  462.     unsigned char linebuf[32 * 3 + 2 + 32 + 1];
  463.  
  464.     if (rowsize != 16 && rowsize != 32)
  465.         rowsize = 16;
  466.  
  467.     for (i = 0; i < len; i += rowsize) {
  468.         linelen = min(remaining, rowsize);
  469.         remaining -= rowsize;
  470.  
  471.         hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  472.                    linebuf, sizeof(linebuf), ascii);
  473.  
  474.         switch (prefix_type) {
  475.         case DUMP_PREFIX_ADDRESS:
  476.             printk("%s%s%p: %s\n",
  477.                    level, prefix_str, ptr + i, linebuf);
  478.             break;
  479.         case DUMP_PREFIX_OFFSET:
  480.             printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
  481.             break;
  482.         default:
  483.             printk("%s%s%s\n", level, prefix_str, linebuf);
  484.             break;
  485.         }
  486.     }
  487. }
  488.  
  489. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  490.                           const void *buf, size_t len)
  491. {
  492.     print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
  493.                        buf, len, true);
  494. }
  495.  
  496. void *kmemdup(const void *src, size_t len, gfp_t gfp)
  497. {
  498.     void *p;
  499.  
  500.     p = kmalloc(len, gfp);
  501.     if (p)
  502.         memcpy(p, src, len);
  503.     return p;
  504. }
  505.  
  506.  
  507.