Subversion Repositories Kolibri OS

Rev

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