xref: /linux/drivers/gpu/drm/udl/udl_drv.c (revision 9e26a3740cc08ef8bcdc5e5d824792cd677affce)
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/clients/drm_client_setup.h>
9 #include <drm/drm_drv.h>
10 #include <drm/drm_fbdev_shmem.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_modeset_helper.h>
15 #include <drm/drm_ioctl.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_print.h>
18 
19 #include "udl_drv.h"
20 
21 static int udl_usb_suspend(struct usb_interface *interface,
22 			   pm_message_t message)
23 {
24 	struct drm_device *dev = usb_get_intfdata(interface);
25 	int ret;
26 
27 	ret = drm_mode_config_helper_suspend(dev);
28 	if (ret)
29 		return ret;
30 
31 	udl_sync_pending_urbs(dev);
32 	return 0;
33 }
34 
35 static int udl_usb_resume(struct usb_interface *interface)
36 {
37 	struct drm_device *dev = usb_get_intfdata(interface);
38 
39 	return drm_mode_config_helper_resume(dev);
40 }
41 
42 static int udl_usb_reset_resume(struct usb_interface *interface)
43 {
44 	struct drm_device *dev = usb_get_intfdata(interface);
45 	struct udl_device *udl = to_udl(dev);
46 
47 	udl_select_std_channel(udl);
48 
49 	return drm_mode_config_helper_resume(dev);
50 }
51 
52 DEFINE_DRM_GEM_FOPS(udl_driver_fops);
53 
54 static const struct drm_driver driver = {
55 	.driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
56 
57 	/* GEM hooks */
58 	.fops = &udl_driver_fops,
59 	DRM_GEM_SHMEM_DRIVER_OPS,
60 	DRM_FBDEV_SHMEM_DRIVER_OPS,
61 
62 	.name = DRIVER_NAME,
63 	.desc = DRIVER_DESC,
64 	.major = DRIVER_MAJOR,
65 	.minor = DRIVER_MINOR,
66 	.patchlevel = DRIVER_PATCHLEVEL,
67 };
68 
69 static struct udl_device *udl_driver_create(struct usb_interface *interface)
70 {
71 	struct udl_device *udl;
72 	int r;
73 
74 	udl = devm_drm_dev_alloc(&interface->dev, &driver,
75 				 struct udl_device, drm);
76 	if (IS_ERR(udl))
77 		return udl;
78 
79 	r = udl_init(udl);
80 	if (r)
81 		return ERR_PTR(r);
82 
83 	usb_set_intfdata(interface, udl);
84 
85 	return udl;
86 }
87 
88 static int udl_usb_probe(struct usb_interface *interface,
89 			 const struct usb_device_id *id)
90 {
91 	int r;
92 	struct udl_device *udl;
93 
94 	udl = udl_driver_create(interface);
95 	if (IS_ERR(udl))
96 		return PTR_ERR(udl);
97 
98 	r = drm_dev_register(&udl->drm, 0);
99 	if (r)
100 		return r;
101 
102 	DRM_INFO("Initialized udl on minor %d\n", udl->drm.primary->index);
103 
104 	drm_client_setup(&udl->drm, NULL);
105 
106 	return 0;
107 }
108 
109 static void udl_usb_disconnect(struct usb_interface *interface)
110 {
111 	struct drm_device *dev = usb_get_intfdata(interface);
112 
113 	drm_dev_unplug(dev);
114 	udl_drop_usb(dev);
115 }
116 
117 /*
118  * There are many DisplayLink-based graphics products, all with unique PIDs.
119  * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
120  * We also require a match on SubClass (0x00) and Protocol (0x00),
121  * which is compatible with all known USB 2.0 era graphics chips and firmware,
122  * but allows DisplayLink to increment those for any future incompatible chips
123  */
124 static const struct usb_device_id id_table[] = {
125 	{.idVendor = 0x17e9, .bInterfaceClass = 0xff,
126 	 .bInterfaceSubClass = 0x00,
127 	 .bInterfaceProtocol = 0x00,
128 	 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
129 			USB_DEVICE_ID_MATCH_INT_CLASS |
130 			USB_DEVICE_ID_MATCH_INT_SUBCLASS |
131 			USB_DEVICE_ID_MATCH_INT_PROTOCOL,},
132 	{},
133 };
134 MODULE_DEVICE_TABLE(usb, id_table);
135 
136 static struct usb_driver udl_driver = {
137 	.name = "udl",
138 	.probe = udl_usb_probe,
139 	.disconnect = udl_usb_disconnect,
140 	.suspend = udl_usb_suspend,
141 	.resume = udl_usb_resume,
142 	.reset_resume = udl_usb_reset_resume,
143 	.id_table = id_table,
144 };
145 module_usb_driver(udl_driver);
146 MODULE_DESCRIPTION("KMS driver for the USB displaylink video adapters");
147 MODULE_LICENSE("GPL");
148