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