1 // SPDX-License-Identifier: MIT
3 * Copyright 2020 Noralf Trønnes
6 #include <linux/dma-buf.h>
7 #include <linux/dma-mapping.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/string_helpers.h>
12 #include <linux/usb.h>
13 #include <linux/vmalloc.h>
14 #include <linux/workqueue.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_damage_helper.h>
18 #include <drm/drm_debugfs.h>
19 #include <drm/drm_drv.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_fourcc.h>
22 #include <drm/drm_gem_atomic_helper.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_gem_shmem_helper.h>
25 #include <drm/drm_managed.h>
26 #include <drm/drm_print.h>
27 #include <drm/drm_probe_helper.h>
28 #include <drm/drm_simple_kms_helper.h>
31 #include "gud_internal.h"
33 /* Only used internally */
34 static const struct drm_format_info gud_drm_format_r1 = {
35 .format = GUD_DRM_FORMAT_R1,
37 .char_per_block = { 1, 0, 0 },
38 .block_w = { 8, 0, 0 },
39 .block_h = { 1, 0, 0 },
44 static const struct drm_format_info gud_drm_format_xrgb1111 = {
45 .format = GUD_DRM_FORMAT_XRGB1111,
47 .char_per_block = { 1, 0, 0 },
48 .block_w = { 2, 0, 0 },
49 .block_h = { 1, 0, 0 },
54 static int gud_usb_control_msg(struct usb_interface *intf, bool in,
55 u8 request, u16 value, void *buf, size_t len)
57 u8 requesttype = USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
58 u8 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
59 struct usb_device *usb = interface_to_usbdev(intf);
66 pipe = usb_rcvctrlpipe(usb, 0);
67 requesttype |= USB_DIR_IN;
69 pipe = usb_sndctrlpipe(usb, 0);
70 requesttype |= USB_DIR_OUT;
73 return usb_control_msg(usb, pipe, request, requesttype, value,
74 ifnum, buf, len, USB_CTRL_GET_TIMEOUT);
77 static int gud_get_display_descriptor(struct usb_interface *intf,
78 struct gud_display_descriptor_req *desc)
83 buf = kmalloc(sizeof(*desc), GFP_KERNEL);
87 ret = gud_usb_control_msg(intf, true, GUD_REQ_GET_DESCRIPTOR, 0, buf, sizeof(*desc));
88 memcpy(desc, buf, sizeof(*desc));
92 if (ret != sizeof(*desc))
95 if (desc->magic != le32_to_cpu(GUD_DISPLAY_MAGIC))
98 DRM_DEV_DEBUG_DRIVER(&intf->dev,
99 "version=%u flags=0x%x compression=0x%x max_buffer_size=%u\n",
100 desc->version, le32_to_cpu(desc->flags), desc->compression,
101 le32_to_cpu(desc->max_buffer_size));
103 if (!desc->version || !desc->max_width || !desc->max_height ||
104 le32_to_cpu(desc->min_width) > le32_to_cpu(desc->max_width) ||
105 le32_to_cpu(desc->min_height) > le32_to_cpu(desc->max_height))
111 static int gud_status_to_errno(u8 status)
116 case GUD_STATUS_BUSY:
118 case GUD_STATUS_REQUEST_NOT_SUPPORTED:
120 case GUD_STATUS_PROTOCOL_ERROR:
122 case GUD_STATUS_INVALID_PARAMETER:
124 case GUD_STATUS_ERROR:
131 static int gud_usb_get_status(struct usb_interface *intf)
133 int ret, status = -EIO;
136 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
140 ret = gud_usb_control_msg(intf, true, GUD_REQ_GET_STATUS, 0, buf, sizeof(*buf));
141 if (ret == sizeof(*buf))
142 status = gud_status_to_errno(*buf);
151 static int gud_usb_transfer(struct gud_device *gdrm, bool in, u8 request, u16 index,
152 void *buf, size_t len)
154 struct usb_interface *intf = to_usb_interface(gdrm->drm.dev);
157 drm_dbg(&gdrm->drm, "%s: request=0x%x index=%u len=%zu\n",
158 in ? "get" : "set", request, index, len);
160 if (!drm_dev_enter(&gdrm->drm, &idx))
163 mutex_lock(&gdrm->ctrl_lock);
165 ret = gud_usb_control_msg(intf, in, request, index, buf, len);
166 if (ret == -EPIPE || ((gdrm->flags & GUD_DISPLAY_FLAG_STATUS_ON_SET) && !in && ret >= 0)) {
169 status = gud_usb_get_status(intf);
172 } else if (ret < 0) {
173 dev_err_once(gdrm->drm.dev,
174 "Unexpected status OK for failed transfer\n");
180 drm_dbg(&gdrm->drm, "ret=%d\n", ret);
181 gdrm->stats_num_errors++;
184 mutex_unlock(&gdrm->ctrl_lock);
191 * @buf cannot be allocated on the stack.
192 * Returns number of bytes received or negative error code on failure.
194 int gud_usb_get(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t max_len)
196 return gud_usb_transfer(gdrm, true, request, index, buf, max_len);
200 * @buf can be allocated on the stack or NULL.
201 * Returns zero on success or negative error code on failure.
203 int gud_usb_set(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t len)
209 trbuf = kmemdup(buf, len, GFP_KERNEL);
214 ret = gud_usb_transfer(gdrm, false, request, index, trbuf, len);
219 return ret != len ? -EIO : 0;
223 * @val can be allocated on the stack.
224 * Returns zero on success or negative error code on failure.
226 int gud_usb_get_u8(struct gud_device *gdrm, u8 request, u16 index, u8 *val)
231 buf = kmalloc(sizeof(*val), GFP_KERNEL);
235 ret = gud_usb_get(gdrm, request, index, buf, sizeof(*val));
241 return ret != sizeof(*val) ? -EIO : 0;
244 /* Returns zero on success or negative error code on failure. */
245 int gud_usb_set_u8(struct gud_device *gdrm, u8 request, u8 val)
247 return gud_usb_set(gdrm, request, 0, &val, sizeof(val));
250 static int gud_get_properties(struct gud_device *gdrm)
252 struct gud_property_req *properties;
253 unsigned int i, num_properties;
256 properties = kcalloc(GUD_PROPERTIES_MAX_NUM, sizeof(*properties), GFP_KERNEL);
260 ret = gud_usb_get(gdrm, GUD_REQ_GET_PROPERTIES, 0,
261 properties, GUD_PROPERTIES_MAX_NUM * sizeof(*properties));
264 if (ret % sizeof(*properties)) {
269 num_properties = ret / sizeof(*properties);
272 gdrm->properties = drmm_kcalloc(&gdrm->drm, num_properties, sizeof(*gdrm->properties),
274 if (!gdrm->properties) {
279 for (i = 0; i < num_properties; i++) {
280 u16 prop = le16_to_cpu(properties[i].prop);
281 u64 val = le64_to_cpu(properties[i].val);
284 case GUD_PROPERTY_ROTATION:
286 * DRM UAPI matches the protocol so use the value directly,
287 * but mask out any additions on future devices.
289 val &= GUD_ROTATION_MASK;
290 ret = drm_plane_create_rotation_property(&gdrm->pipe.plane,
291 DRM_MODE_ROTATE_0, val);
294 /* New ones might show up in future devices, skip those we don't know. */
295 drm_dbg(&gdrm->drm, "Ignoring unknown property: %u\n", prop);
302 gdrm->properties[gdrm->num_properties++] = prop;
311 * FIXME: Dma-buf sharing requires DMA support by the importing device.
312 * This function is a workaround to make USB devices work as well.
313 * See todo.rst for how to fix the issue in the dma-buf framework.
315 static struct drm_gem_object *gud_gem_prime_import(struct drm_device *drm, struct dma_buf *dma_buf)
317 struct gud_device *gdrm = to_gud_device(drm);
320 return ERR_PTR(-ENODEV);
322 return drm_gem_prime_import_dev(drm, dma_buf, gdrm->dmadev);
325 static int gud_stats_debugfs(struct seq_file *m, void *data)
327 struct drm_info_node *node = m->private;
328 struct gud_device *gdrm = to_gud_device(node->minor->dev);
331 string_get_size(gdrm->bulk_len, 1, STRING_UNITS_2, buf, sizeof(buf));
332 seq_printf(m, "Max buffer size: %s\n", buf);
333 seq_printf(m, "Number of errors: %u\n", gdrm->stats_num_errors);
335 seq_puts(m, "Compression: ");
336 if (gdrm->compression & GUD_COMPRESSION_LZ4)
338 if (!gdrm->compression)
339 seq_puts(m, " none");
342 if (gdrm->compression) {
344 u64 ratio = div64_u64_rem(gdrm->stats_length, gdrm->stats_actual_length,
346 u64 ratio_frac = div64_u64(remainder * 10, gdrm->stats_actual_length);
348 seq_printf(m, "Compression ratio: %llu.%llu\n", ratio, ratio_frac);
354 static const struct drm_info_list gud_debugfs_list[] = {
355 { "stats", gud_stats_debugfs, 0, NULL },
358 static void gud_debugfs_init(struct drm_minor *minor)
360 drm_debugfs_create_files(gud_debugfs_list, ARRAY_SIZE(gud_debugfs_list),
361 minor->debugfs_root, minor);
364 static const struct drm_simple_display_pipe_funcs gud_pipe_funcs = {
365 .check = gud_pipe_check,
366 .update = gud_pipe_update,
369 static const struct drm_mode_config_funcs gud_mode_config_funcs = {
370 .fb_create = drm_gem_fb_create_with_dirty,
371 .atomic_check = drm_atomic_helper_check,
372 .atomic_commit = drm_atomic_helper_commit,
375 static const u64 gud_pipe_modifiers[] = {
376 DRM_FORMAT_MOD_LINEAR,
377 DRM_FORMAT_MOD_INVALID
380 DEFINE_DRM_GEM_FOPS(gud_fops);
382 static const struct drm_driver gud_drm_driver = {
383 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
385 DRM_GEM_SHMEM_DRIVER_OPS,
386 .gem_prime_import = gud_gem_prime_import,
387 .debugfs_init = gud_debugfs_init,
390 .desc = "Generic USB Display",
396 static void gud_free_buffers_and_mutex(struct drm_device *drm, void *unused)
398 struct gud_device *gdrm = to_gud_device(drm);
400 vfree(gdrm->compress_buf);
401 kfree(gdrm->bulk_buf);
402 mutex_destroy(&gdrm->ctrl_lock);
403 mutex_destroy(&gdrm->damage_lock);
406 static int gud_probe(struct usb_interface *intf, const struct usb_device_id *id)
408 const struct drm_format_info *xrgb8888_emulation_format = NULL;
409 bool rgb565_supported = false, xrgb8888_supported = false;
410 unsigned int num_formats_dev, num_formats = 0;
411 struct usb_endpoint_descriptor *bulk_out;
412 struct gud_display_descriptor_req desc;
413 struct device *dev = &intf->dev;
414 size_t max_buffer_size = 0;
415 struct gud_device *gdrm;
416 struct drm_device *drm;
421 ret = usb_find_bulk_out_endpoint(intf->cur_altsetting, &bulk_out);
425 ret = gud_get_display_descriptor(intf, &desc);
427 DRM_DEV_DEBUG_DRIVER(dev, "Not a display interface: ret=%d\n", ret);
431 if (desc.version > 1) {
432 dev_err(dev, "Protocol version %u is not supported\n", desc.version);
436 gdrm = devm_drm_dev_alloc(dev, &gud_drm_driver, struct gud_device, drm);
438 return PTR_ERR(gdrm);
441 drm->mode_config.funcs = &gud_mode_config_funcs;
442 ret = drmm_mode_config_init(drm);
446 gdrm->flags = le32_to_cpu(desc.flags);
447 gdrm->compression = desc.compression & GUD_COMPRESSION_LZ4;
449 if (gdrm->flags & GUD_DISPLAY_FLAG_FULL_UPDATE && gdrm->compression)
452 mutex_init(&gdrm->ctrl_lock);
453 mutex_init(&gdrm->damage_lock);
454 INIT_WORK(&gdrm->work, gud_flush_work);
455 gud_clear_damage(gdrm);
457 ret = drmm_add_action_or_reset(drm, gud_free_buffers_and_mutex, NULL);
461 drm->mode_config.min_width = le32_to_cpu(desc.min_width);
462 drm->mode_config.max_width = le32_to_cpu(desc.max_width);
463 drm->mode_config.min_height = le32_to_cpu(desc.min_height);
464 drm->mode_config.max_height = le32_to_cpu(desc.max_height);
466 formats_dev = devm_kmalloc(dev, GUD_FORMATS_MAX_NUM, GFP_KERNEL);
467 /* Add room for emulated XRGB8888 */
468 formats = devm_kmalloc_array(dev, GUD_FORMATS_MAX_NUM + 1, sizeof(*formats), GFP_KERNEL);
469 if (!formats_dev || !formats)
472 ret = gud_usb_get(gdrm, GUD_REQ_GET_FORMATS, 0, formats_dev, GUD_FORMATS_MAX_NUM);
476 num_formats_dev = ret;
477 for (i = 0; i < num_formats_dev; i++) {
478 const struct drm_format_info *info;
482 format = gud_to_fourcc(formats_dev[i]);
484 drm_dbg(drm, "Unsupported format: 0x%02x\n", formats_dev[i]);
488 if (format == GUD_DRM_FORMAT_R1)
489 info = &gud_drm_format_r1;
490 else if (format == GUD_DRM_FORMAT_XRGB1111)
491 info = &gud_drm_format_xrgb1111;
493 info = drm_format_info(format);
496 case GUD_DRM_FORMAT_R1:
498 case GUD_DRM_FORMAT_XRGB1111:
499 if (!xrgb8888_emulation_format)
500 xrgb8888_emulation_format = info;
502 case DRM_FORMAT_RGB565:
503 rgb565_supported = true;
504 if (!xrgb8888_emulation_format)
505 xrgb8888_emulation_format = info;
507 case DRM_FORMAT_XRGB8888:
508 xrgb8888_supported = true;
512 fmt_buf_size = drm_format_info_min_pitch(info, 0, drm->mode_config.max_width) *
513 drm->mode_config.max_height;
514 max_buffer_size = max(max_buffer_size, fmt_buf_size);
516 if (format == GUD_DRM_FORMAT_R1 || format == GUD_DRM_FORMAT_XRGB1111)
517 continue; /* Internal not for userspace */
519 formats[num_formats++] = format;
522 if (!num_formats && !xrgb8888_emulation_format) {
523 dev_err(dev, "No supported pixel formats found\n");
527 /* Prefer speed over color depth */
528 if (rgb565_supported)
529 drm->mode_config.preferred_depth = 16;
531 if (!xrgb8888_supported && xrgb8888_emulation_format) {
532 gdrm->xrgb8888_emulation_format = xrgb8888_emulation_format;
533 formats[num_formats++] = DRM_FORMAT_XRGB8888;
536 if (desc.max_buffer_size)
537 max_buffer_size = le32_to_cpu(desc.max_buffer_size);
540 * Use plain kmalloc here since devm_kmalloc() places struct devres at the beginning
541 * of the buffer it allocates. This wastes a lot of memory when allocating big buffers.
542 * Asking for 2M would actually allocate 4M. This would also prevent getting the biggest
543 * possible buffer potentially leading to split transfers.
545 gdrm->bulk_buf = kmalloc(max_buffer_size, GFP_KERNEL | __GFP_NOWARN);
546 if (!gdrm->bulk_buf) {
547 max_buffer_size = roundup_pow_of_two(max_buffer_size) / 2;
548 if (max_buffer_size < SZ_512K)
553 gdrm->bulk_pipe = usb_sndbulkpipe(interface_to_usbdev(intf), usb_endpoint_num(bulk_out));
554 gdrm->bulk_len = max_buffer_size;
556 if (gdrm->compression & GUD_COMPRESSION_LZ4) {
557 gdrm->lz4_comp_mem = devm_kmalloc(dev, LZ4_MEM_COMPRESS, GFP_KERNEL);
558 if (!gdrm->lz4_comp_mem)
561 gdrm->compress_buf = vmalloc(gdrm->bulk_len);
562 if (!gdrm->compress_buf)
566 ret = drm_simple_display_pipe_init(drm, &gdrm->pipe, &gud_pipe_funcs,
567 formats, num_formats,
568 gud_pipe_modifiers, NULL);
572 devm_kfree(dev, formats);
573 devm_kfree(dev, formats_dev);
575 ret = gud_get_properties(gdrm);
577 dev_err(dev, "Failed to get properties (error=%d)\n", ret);
581 drm_plane_enable_fb_damage_clips(&gdrm->pipe.plane);
583 ret = gud_get_connectors(gdrm);
585 dev_err(dev, "Failed to get connectors (error=%d)\n", ret);
589 drm_mode_config_reset(drm);
591 usb_set_intfdata(intf, gdrm);
593 gdrm->dmadev = usb_intf_get_dma_device(intf);
595 dev_warn(dev, "buffer sharing not supported");
597 ret = drm_dev_register(drm, 0);
599 put_device(gdrm->dmadev);
603 drm_kms_helper_poll_init(drm);
605 drm_fbdev_generic_setup(drm, 0);
610 static void gud_disconnect(struct usb_interface *interface)
612 struct gud_device *gdrm = usb_get_intfdata(interface);
613 struct drm_device *drm = &gdrm->drm;
615 drm_dbg(drm, "%s:\n", __func__);
617 drm_kms_helper_poll_fini(drm);
619 drm_atomic_helper_shutdown(drm);
620 put_device(gdrm->dmadev);
624 static int gud_suspend(struct usb_interface *intf, pm_message_t message)
626 struct gud_device *gdrm = usb_get_intfdata(intf);
628 return drm_mode_config_helper_suspend(&gdrm->drm);
631 static int gud_resume(struct usb_interface *intf)
633 struct gud_device *gdrm = usb_get_intfdata(intf);
635 drm_mode_config_helper_resume(&gdrm->drm);
640 static const struct usb_device_id gud_id_table[] = {
641 { USB_DEVICE_INTERFACE_CLASS(0x1d50, 0x614d, USB_CLASS_VENDOR_SPEC) },
645 MODULE_DEVICE_TABLE(usb, gud_id_table);
647 static struct usb_driver gud_usb_driver = {
650 .disconnect = gud_disconnect,
651 .id_table = gud_id_table,
652 .suspend = gud_suspend,
653 .resume = gud_resume,
654 .reset_resume = gud_resume,
657 module_usb_driver(gud_usb_driver);
659 MODULE_AUTHOR("Noralf Trønnes");
660 MODULE_LICENSE("Dual MIT/GPL");