udl_transfer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Red Hat
  4. * based in parts on udlfb.c:
  5. * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
  6. * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
  7. * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
  8. */
  9. #include <linux/unaligned.h>
  10. #include "udl_drv.h"
  11. #include "udl_proto.h"
  12. #define MAX_CMD_PIXELS 255
  13. #define RLX_HEADER_BYTES 7
  14. #define MIN_RLX_PIX_BYTES 4
  15. #define MIN_RLX_CMD_BYTES (RLX_HEADER_BYTES + MIN_RLX_PIX_BYTES)
  16. #define RLE_HEADER_BYTES 6
  17. #define MIN_RLE_PIX_BYTES 3
  18. #define MIN_RLE_CMD_BYTES (RLE_HEADER_BYTES + MIN_RLE_PIX_BYTES)
  19. #define RAW_HEADER_BYTES 6
  20. #define MIN_RAW_PIX_BYTES 2
  21. #define MIN_RAW_CMD_BYTES (RAW_HEADER_BYTES + MIN_RAW_PIX_BYTES)
  22. static inline u16 pixel32_to_be16(const uint32_t pixel)
  23. {
  24. return (((pixel >> 3) & 0x001f) |
  25. ((pixel >> 5) & 0x07e0) |
  26. ((pixel >> 8) & 0xf800));
  27. }
  28. static inline u16 get_pixel_val16(const uint8_t *pixel, int log_bpp)
  29. {
  30. u16 pixel_val16;
  31. if (log_bpp == 1)
  32. pixel_val16 = *(const uint16_t *)pixel;
  33. else
  34. pixel_val16 = pixel32_to_be16(*(const uint32_t *)pixel);
  35. return pixel_val16;
  36. }
  37. /*
  38. * Render a command stream for an encoded horizontal line segment of pixels.
  39. *
  40. * A command buffer holds several commands.
  41. * It always begins with a fresh command header
  42. * (the protocol doesn't require this, but we enforce it to allow
  43. * multiple buffers to be potentially encoded and sent in parallel).
  44. * A single command encodes one contiguous horizontal line of pixels
  45. *
  46. * The function relies on the client to do all allocation, so that
  47. * rendering can be done directly to output buffers (e.g. USB URBs).
  48. * The function fills the supplied command buffer, providing information
  49. * on where it left off, so the client may call in again with additional
  50. * buffers if the line will take several buffers to complete.
  51. *
  52. * A single command can transmit a maximum of 256 pixels,
  53. * regardless of the compression ratio (protocol design limit).
  54. * To the hardware, 0 for a size byte means 256
  55. *
  56. * Rather than 256 pixel commands which are either rl or raw encoded,
  57. * the rlx command simply assumes alternating raw and rl spans within one cmd.
  58. * This has a slightly larger header overhead, but produces more even results.
  59. * It also processes all data (read and write) in a single pass.
  60. * Performance benchmarks of common cases show it having just slightly better
  61. * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
  62. * But for very rl friendly data, will compress not quite as well.
  63. */
  64. static void udl_compress_hline16(
  65. const u8 **pixel_start_ptr,
  66. const u8 *const pixel_end,
  67. uint32_t *device_address_ptr,
  68. uint8_t **command_buffer_ptr,
  69. const uint8_t *const cmd_buffer_end, int log_bpp)
  70. {
  71. const int bpp = 1 << log_bpp;
  72. const u8 *pixel = *pixel_start_ptr;
  73. uint32_t dev_addr = *device_address_ptr;
  74. uint8_t *cmd = *command_buffer_ptr;
  75. while ((pixel_end > pixel) &&
  76. (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
  77. uint8_t *raw_pixels_count_byte = NULL;
  78. uint8_t *cmd_pixels_count_byte = NULL;
  79. const u8 *raw_pixel_start = NULL;
  80. const u8 *cmd_pixel_start, *cmd_pixel_end = NULL;
  81. uint16_t pixel_val16;
  82. *cmd++ = UDL_MSG_BULK;
  83. *cmd++ = UDL_CMD_WRITERLX16;
  84. *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
  85. *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
  86. *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
  87. cmd_pixels_count_byte = cmd++; /* we'll know this later */
  88. cmd_pixel_start = pixel;
  89. raw_pixels_count_byte = cmd++; /* we'll know this later */
  90. raw_pixel_start = pixel;
  91. cmd_pixel_end = pixel + (min3(MAX_CMD_PIXELS + 1UL,
  92. (unsigned long)(pixel_end - pixel) >> log_bpp,
  93. (unsigned long)(cmd_buffer_end - 1 - cmd) / 2) << log_bpp);
  94. pixel_val16 = get_pixel_val16(pixel, log_bpp);
  95. while (pixel < cmd_pixel_end) {
  96. const u8 *const start = pixel;
  97. const uint16_t repeating_pixel_val16 = pixel_val16;
  98. put_unaligned_be16(pixel_val16, cmd);
  99. cmd += 2;
  100. pixel += bpp;
  101. while (pixel < cmd_pixel_end) {
  102. pixel_val16 = get_pixel_val16(pixel, log_bpp);
  103. if (pixel_val16 != repeating_pixel_val16)
  104. break;
  105. pixel += bpp;
  106. }
  107. if (unlikely(pixel > start + bpp)) {
  108. /* go back and fill in raw pixel count */
  109. *raw_pixels_count_byte = (((start -
  110. raw_pixel_start) >> log_bpp) + 1) & 0xFF;
  111. /* immediately after raw data is repeat byte */
  112. *cmd++ = (((pixel - start) >> log_bpp) - 1) & 0xFF;
  113. /* Then start another raw pixel span */
  114. raw_pixel_start = pixel;
  115. raw_pixels_count_byte = cmd++;
  116. }
  117. }
  118. if (pixel > raw_pixel_start) {
  119. /* finalize last RAW span */
  120. *raw_pixels_count_byte = ((pixel - raw_pixel_start) >> log_bpp) & 0xFF;
  121. } else {
  122. /* undo unused byte */
  123. cmd--;
  124. }
  125. *cmd_pixels_count_byte = ((pixel - cmd_pixel_start) >> log_bpp) & 0xFF;
  126. dev_addr += ((pixel - cmd_pixel_start) >> log_bpp) * 2;
  127. }
  128. if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
  129. /* Fill leftover bytes with no-ops */
  130. if (cmd_buffer_end > cmd)
  131. memset(cmd, UDL_MSG_BULK, cmd_buffer_end - cmd);
  132. cmd = (uint8_t *) cmd_buffer_end;
  133. }
  134. *command_buffer_ptr = cmd;
  135. *pixel_start_ptr = pixel;
  136. *device_address_ptr = dev_addr;
  137. return;
  138. }
  139. /*
  140. * There are 3 copies of every pixel: The front buffer that the fbdev
  141. * client renders to, the actual framebuffer across the USB bus in hardware
  142. * (that we can only write to, slowly, and can never read), and (optionally)
  143. * our shadow copy that tracks what's been sent to that hardware buffer.
  144. */
  145. int udl_render_hline(struct udl_device *udl, int log_bpp, struct urb **urb_ptr,
  146. const char *front, char **urb_buf_ptr,
  147. u32 byte_offset, u32 device_byte_offset,
  148. u32 byte_width)
  149. {
  150. const u8 *line_start, *line_end, *next_pixel;
  151. u32 base16 = 0 + (device_byte_offset >> log_bpp) * 2;
  152. struct urb *urb = *urb_ptr;
  153. u8 *cmd = *urb_buf_ptr;
  154. u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
  155. if (WARN_ON(!(log_bpp == 1 || log_bpp == 2))) {
  156. /* need to finish URB at error from this function */
  157. udl_urb_completion(urb);
  158. return -EINVAL;
  159. }
  160. line_start = (u8 *) (front + byte_offset);
  161. next_pixel = line_start;
  162. line_end = next_pixel + byte_width;
  163. while (next_pixel < line_end) {
  164. udl_compress_hline16(&next_pixel,
  165. line_end, &base16,
  166. (u8 **) &cmd, (u8 *) cmd_end, log_bpp);
  167. if (cmd >= cmd_end) {
  168. int len = cmd - (u8 *) urb->transfer_buffer;
  169. int ret = udl_submit_urb(udl, urb, len);
  170. if (ret)
  171. return ret;
  172. urb = udl_get_urb(udl);
  173. if (!urb)
  174. return -EAGAIN;
  175. *urb_ptr = urb;
  176. cmd = urb->transfer_buffer;
  177. cmd_end = &cmd[urb->transfer_buffer_length];
  178. }
  179. }
  180. *urb_buf_ptr = cmd;
  181. return 0;
  182. }