xref: /openbmc/linux/drivers/gpu/drm/udl/udl_drv.c (revision 8795a739)
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_file.h>
11 #include <drm/drm_ioctl.h>
12 #include <drm/drm_probe_helper.h>
13 #include <drm/drm_print.h>
14 
15 #include "udl_drv.h"
16 
17 static int udl_usb_suspend(struct usb_interface *interface,
18 			   pm_message_t message)
19 {
20 	struct drm_device *dev = usb_get_intfdata(interface);
21 
22 	drm_kms_helper_poll_disable(dev);
23 	return 0;
24 }
25 
26 static int udl_usb_resume(struct usb_interface *interface)
27 {
28 	struct drm_device *dev = usb_get_intfdata(interface);
29 
30 	drm_kms_helper_poll_enable(dev);
31 	udl_modeset_restore(dev);
32 	return 0;
33 }
34 
35 static const struct vm_operations_struct udl_gem_vm_ops = {
36 	.fault = udl_gem_fault,
37 	.open = drm_gem_vm_open,
38 	.close = drm_gem_vm_close,
39 };
40 
41 static const struct file_operations udl_driver_fops = {
42 	.owner = THIS_MODULE,
43 	.open = drm_open,
44 	.mmap = udl_drm_gem_mmap,
45 	.poll = drm_poll,
46 	.read = drm_read,
47 	.unlocked_ioctl	= drm_ioctl,
48 	.release = drm_release,
49 	.compat_ioctl = drm_compat_ioctl,
50 	.llseek = noop_llseek,
51 };
52 
53 static void udl_driver_release(struct drm_device *dev)
54 {
55 	udl_fini(dev);
56 	udl_modeset_cleanup(dev);
57 	drm_dev_fini(dev);
58 	kfree(dev);
59 }
60 
61 static struct drm_driver driver = {
62 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
63 	.release = udl_driver_release,
64 
65 	/* gem hooks */
66 	.gem_free_object_unlocked = udl_gem_free_object,
67 	.gem_vm_ops = &udl_gem_vm_ops,
68 
69 	.dumb_create = udl_dumb_create,
70 	.dumb_map_offset = udl_gem_mmap,
71 	.fops = &udl_driver_fops,
72 
73 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
74 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
75 	.gem_prime_export = udl_gem_prime_export,
76 	.gem_prime_import = udl_gem_prime_import,
77 
78 	.name = DRIVER_NAME,
79 	.desc = DRIVER_DESC,
80 	.date = DRIVER_DATE,
81 	.major = DRIVER_MAJOR,
82 	.minor = DRIVER_MINOR,
83 	.patchlevel = DRIVER_PATCHLEVEL,
84 };
85 
86 static struct udl_device *udl_driver_create(struct usb_interface *interface)
87 {
88 	struct usb_device *udev = interface_to_usbdev(interface);
89 	struct udl_device *udl;
90 	int r;
91 
92 	udl = kzalloc(sizeof(*udl), GFP_KERNEL);
93 	if (!udl)
94 		return ERR_PTR(-ENOMEM);
95 
96 	r = drm_dev_init(&udl->drm, &driver, &interface->dev);
97 	if (r) {
98 		kfree(udl);
99 		return ERR_PTR(r);
100 	}
101 
102 	udl->udev = udev;
103 	udl->drm.dev_private = udl;
104 
105 	r = udl_init(udl);
106 	if (r) {
107 		drm_dev_fini(&udl->drm);
108 		kfree(udl);
109 		return ERR_PTR(r);
110 	}
111 
112 	usb_set_intfdata(interface, udl);
113 	return udl;
114 }
115 
116 static int udl_usb_probe(struct usb_interface *interface,
117 			 const struct usb_device_id *id)
118 {
119 	int r;
120 	struct udl_device *udl;
121 
122 	udl = udl_driver_create(interface);
123 	if (IS_ERR(udl))
124 		return PTR_ERR(udl);
125 
126 	r = drm_dev_register(&udl->drm, 0);
127 	if (r)
128 		goto err_free;
129 
130 	DRM_INFO("Initialized udl on minor %d\n", udl->drm.primary->index);
131 
132 	return 0;
133 
134 err_free:
135 	drm_dev_put(&udl->drm);
136 	return r;
137 }
138 
139 static void udl_usb_disconnect(struct usb_interface *interface)
140 {
141 	struct drm_device *dev = usb_get_intfdata(interface);
142 
143 	drm_kms_helper_poll_disable(dev);
144 	udl_fbdev_unplug(dev);
145 	udl_drop_usb(dev);
146 	drm_dev_unplug(dev);
147 	drm_dev_put(dev);
148 }
149 
150 /*
151  * There are many DisplayLink-based graphics products, all with unique PIDs.
152  * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
153  * We also require a match on SubClass (0x00) and Protocol (0x00),
154  * which is compatible with all known USB 2.0 era graphics chips and firmware,
155  * but allows DisplayLink to increment those for any future incompatible chips
156  */
157 static const struct usb_device_id id_table[] = {
158 	{.idVendor = 0x17e9, .bInterfaceClass = 0xff,
159 	 .bInterfaceSubClass = 0x00,
160 	 .bInterfaceProtocol = 0x00,
161 	 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
162 			USB_DEVICE_ID_MATCH_INT_CLASS |
163 			USB_DEVICE_ID_MATCH_INT_SUBCLASS |
164 			USB_DEVICE_ID_MATCH_INT_PROTOCOL,},
165 	{},
166 };
167 MODULE_DEVICE_TABLE(usb, id_table);
168 
169 static struct usb_driver udl_driver = {
170 	.name = "udl",
171 	.probe = udl_usb_probe,
172 	.disconnect = udl_usb_disconnect,
173 	.suspend = udl_usb_suspend,
174 	.resume = udl_usb_resume,
175 	.id_table = id_table,
176 };
177 module_usb_driver(udl_driver);
178 MODULE_LICENSE("GPL");
179