GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / gpu / drm / udl / udl_drv.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Red Hat
4  */
5
6 #include <linux/module.h>
7
8 #include <drm/drm_crtc_helper.h>
9 #include <drm/drm_drv.h>
10 #include <drm/drm_fb_helper.h>
11 #include <drm/drm_file.h>
12 #include <drm/drm_gem_shmem_helper.h>
13 #include <drm/drm_managed.h>
14 #include <drm/drm_ioctl.h>
15 #include <drm/drm_probe_helper.h>
16 #include <drm/drm_print.h>
17
18 #include "udl_drv.h"
19
20 static int udl_usb_suspend(struct usb_interface *interface,
21                            pm_message_t message)
22 {
23         struct drm_device *dev = usb_get_intfdata(interface);
24
25         return drm_mode_config_helper_suspend(dev);
26 }
27
28 static int udl_usb_resume(struct usb_interface *interface)
29 {
30         struct drm_device *dev = usb_get_intfdata(interface);
31
32         return drm_mode_config_helper_resume(dev);
33 }
34
35 /*
36  * FIXME: Dma-buf sharing requires DMA support by the importing device.
37  *        This function is a workaround to make USB devices work as well.
38  *        See todo.rst for how to fix the issue in the dma-buf framework.
39  */
40 static struct drm_gem_object *udl_driver_gem_prime_import(struct drm_device *dev,
41                                                           struct dma_buf *dma_buf)
42 {
43         struct udl_device *udl = to_udl(dev);
44
45         if (!udl->dmadev)
46                 return ERR_PTR(-ENODEV);
47
48         return drm_gem_prime_import_dev(dev, dma_buf, udl->dmadev);
49 }
50
51 DEFINE_DRM_GEM_FOPS(udl_driver_fops);
52
53 static struct drm_driver driver = {
54         .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
55
56         /* GEM hooks */
57         .gem_create_object = drm_gem_shmem_create_object_cached,
58
59         .fops = &udl_driver_fops,
60         DRM_GEM_SHMEM_DRIVER_OPS,
61         .gem_prime_import = udl_driver_gem_prime_import,
62
63         .name = DRIVER_NAME,
64         .desc = DRIVER_DESC,
65         .date = DRIVER_DATE,
66         .major = DRIVER_MAJOR,
67         .minor = DRIVER_MINOR,
68         .patchlevel = DRIVER_PATCHLEVEL,
69 };
70
71 static struct udl_device *udl_driver_create(struct usb_interface *interface)
72 {
73         struct usb_device *udev = interface_to_usbdev(interface);
74         struct udl_device *udl;
75         int r;
76
77         udl = devm_drm_dev_alloc(&interface->dev, &driver,
78                                  struct udl_device, drm);
79         if (IS_ERR(udl))
80                 return udl;
81
82         udl->udev = udev;
83
84         r = udl_init(udl);
85         if (r)
86                 return ERR_PTR(r);
87
88         usb_set_intfdata(interface, udl);
89
90         return udl;
91 }
92
93 static int udl_usb_probe(struct usb_interface *interface,
94                          const struct usb_device_id *id)
95 {
96         int r;
97         struct udl_device *udl;
98
99         udl = udl_driver_create(interface);
100         if (IS_ERR(udl))
101                 return PTR_ERR(udl);
102
103         r = drm_dev_register(&udl->drm, 0);
104         if (r)
105                 return r;
106
107         DRM_INFO("Initialized udl on minor %d\n", udl->drm.primary->index);
108
109         drm_fbdev_generic_setup(&udl->drm, 0);
110
111         return 0;
112 }
113
114 static void udl_usb_disconnect(struct usb_interface *interface)
115 {
116         struct drm_device *dev = usb_get_intfdata(interface);
117
118         drm_kms_helper_poll_fini(dev);
119         udl_drop_usb(dev);
120         drm_dev_unplug(dev);
121 }
122
123 /*
124  * There are many DisplayLink-based graphics products, all with unique PIDs.
125  * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
126  * We also require a match on SubClass (0x00) and Protocol (0x00),
127  * which is compatible with all known USB 2.0 era graphics chips and firmware,
128  * but allows DisplayLink to increment those for any future incompatible chips
129  */
130 static const struct usb_device_id id_table[] = {
131         {.idVendor = 0x17e9, .bInterfaceClass = 0xff,
132          .bInterfaceSubClass = 0x00,
133          .bInterfaceProtocol = 0x00,
134          .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
135                         USB_DEVICE_ID_MATCH_INT_CLASS |
136                         USB_DEVICE_ID_MATCH_INT_SUBCLASS |
137                         USB_DEVICE_ID_MATCH_INT_PROTOCOL,},
138         {},
139 };
140 MODULE_DEVICE_TABLE(usb, id_table);
141
142 static struct usb_driver udl_driver = {
143         .name = "udl",
144         .probe = udl_usb_probe,
145         .disconnect = udl_usb_disconnect,
146         .suspend = udl_usb_suspend,
147         .resume = udl_usb_resume,
148         .id_table = id_table,
149 };
150 module_usb_driver(udl_driver);
151 MODULE_LICENSE("GPL");