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