xref: /openbmc/linux/drivers/gpu/drm/udl/udl_drv.c (revision 659ab7a4)
112eb90f1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25320918bSDave Airlie /*
35320918bSDave Airlie  * Copyright (C) 2012 Red Hat
45320918bSDave Airlie  */
55320918bSDave Airlie 
65320918bSDave Airlie #include <linux/module.h>
7a9dcf380SSam Ravnborg 
8760285e7SDavid Howells #include <drm/drm_crtc_helper.h>
9a9dcf380SSam Ravnborg #include <drm/drm_drv.h>
10d0c4fc5aSThomas Zimmermann #include <drm/drm_fb_helper.h>
11a9dcf380SSam Ravnborg #include <drm/drm_file.h>
1208b22f65SThomas Zimmermann #include <drm/drm_gem_shmem_helper.h>
13d0c116adSDaniel Vetter #include <drm/drm_managed.h>
14a9dcf380SSam Ravnborg #include <drm/drm_ioctl.h>
15fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
16a9dcf380SSam Ravnborg #include <drm/drm_print.h>
17a9dcf380SSam Ravnborg 
185320918bSDave Airlie #include "udl_drv.h"
195320918bSDave Airlie 
20737ba109SHaixia Shi static int udl_usb_suspend(struct usb_interface *interface,
21737ba109SHaixia Shi 			   pm_message_t message)
22737ba109SHaixia Shi {
23afdfc4c6SRobert Tarasov 	struct drm_device *dev = usb_get_intfdata(interface);
24afdfc4c6SRobert Tarasov 
25ae08b88aSThomas Zimmermann 	return drm_mode_config_helper_suspend(dev);
26737ba109SHaixia Shi }
27737ba109SHaixia Shi 
28737ba109SHaixia Shi static int udl_usb_resume(struct usb_interface *interface)
29737ba109SHaixia Shi {
30737ba109SHaixia Shi 	struct drm_device *dev = usb_get_intfdata(interface);
31737ba109SHaixia Shi 
32ae08b88aSThomas Zimmermann 	return drm_mode_config_helper_resume(dev);
33737ba109SHaixia Shi }
34737ba109SHaixia Shi 
35*659ab7a4SThomas Zimmermann /*
36*659ab7a4SThomas Zimmermann  * FIXME: Dma-buf sharing requires DMA support by the importing device.
37*659ab7a4SThomas Zimmermann  *        This function is a workaround to make USB devices work as well.
38*659ab7a4SThomas Zimmermann  *        See todo.rst for how to fix the issue in the dma-buf framework.
39*659ab7a4SThomas Zimmermann  */
40*659ab7a4SThomas Zimmermann static struct drm_gem_object *udl_driver_gem_prime_import(struct drm_device *dev,
41*659ab7a4SThomas Zimmermann 							  struct dma_buf *dma_buf)
42*659ab7a4SThomas Zimmermann {
43*659ab7a4SThomas Zimmermann 	struct udl_device *udl = to_udl(dev);
44*659ab7a4SThomas Zimmermann 
45*659ab7a4SThomas Zimmermann 	if (!udl->dmadev)
46*659ab7a4SThomas Zimmermann 		return ERR_PTR(-ENODEV);
47*659ab7a4SThomas Zimmermann 
48*659ab7a4SThomas Zimmermann 	return drm_gem_prime_import_dev(dev, dma_buf, udl->dmadev);
49*659ab7a4SThomas Zimmermann }
50*659ab7a4SThomas Zimmermann 
5108b22f65SThomas Zimmermann DEFINE_DRM_GEM_FOPS(udl_driver_fops);
525320918bSDave Airlie 
5370a59dd8SDaniel Vetter static const struct drm_driver driver = {
549fda81e0SThomas Zimmermann 	.driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
555320918bSDave Airlie 
5699f55c79SThomas Zimmermann 	/* GEM hooks */
575320918bSDave Airlie 	.fops = &udl_driver_fops,
5808b22f65SThomas Zimmermann 	DRM_GEM_SHMEM_DRIVER_OPS,
59*659ab7a4SThomas Zimmermann 	.gem_prime_import = udl_driver_gem_prime_import,
6096503f59SDave Airlie 
615320918bSDave Airlie 	.name = DRIVER_NAME,
625320918bSDave Airlie 	.desc = DRIVER_DESC,
635320918bSDave Airlie 	.date = DRIVER_DATE,
645320918bSDave Airlie 	.major = DRIVER_MAJOR,
655320918bSDave Airlie 	.minor = DRIVER_MINOR,
665320918bSDave Airlie 	.patchlevel = DRIVER_PATCHLEVEL,
675320918bSDave Airlie };
685320918bSDave Airlie 
696ecac85eSDave Airlie static struct udl_device *udl_driver_create(struct usb_interface *interface)
706ecac85eSDave Airlie {
716ecac85eSDave Airlie 	struct udl_device *udl;
726ecac85eSDave Airlie 	int r;
736ecac85eSDave Airlie 
7450b9bbecSDaniel Vetter 	udl = devm_drm_dev_alloc(&interface->dev, &driver,
7550b9bbecSDaniel Vetter 				 struct udl_device, drm);
7650b9bbecSDaniel Vetter 	if (IS_ERR(udl))
7750b9bbecSDaniel Vetter 		return udl;
786ecac85eSDave Airlie 
796ecac85eSDave Airlie 	r = udl_init(udl);
8050b9bbecSDaniel Vetter 	if (r)
816ecac85eSDave Airlie 		return ERR_PTR(r);
826ecac85eSDave Airlie 
836ecac85eSDave Airlie 	usb_set_intfdata(interface, udl);
8450b9bbecSDaniel Vetter 
856ecac85eSDave Airlie 	return udl;
866ecac85eSDave Airlie }
876ecac85eSDave Airlie 
88d4f68a75SDavid Herrmann static int udl_usb_probe(struct usb_interface *interface,
89d4f68a75SDavid Herrmann 			 const struct usb_device_id *id)
90d4f68a75SDavid Herrmann {
91d4f68a75SDavid Herrmann 	int r;
926ecac85eSDave Airlie 	struct udl_device *udl;
93d4f68a75SDavid Herrmann 
946ecac85eSDave Airlie 	udl = udl_driver_create(interface);
956ecac85eSDave Airlie 	if (IS_ERR(udl))
966ecac85eSDave Airlie 		return PTR_ERR(udl);
97d4f68a75SDavid Herrmann 
986ecac85eSDave Airlie 	r = drm_dev_register(&udl->drm, 0);
99d4f68a75SDavid Herrmann 	if (r)
10050b9bbecSDaniel Vetter 		return r;
101d4f68a75SDavid Herrmann 
1026ecac85eSDave Airlie 	DRM_INFO("Initialized udl on minor %d\n", udl->drm.primary->index);
103d4f68a75SDavid Herrmann 
10466459fa2SThomas Zimmermann 	drm_fbdev_generic_setup(&udl->drm, 0);
105d0c4fc5aSThomas Zimmermann 
106d4f68a75SDavid Herrmann 	return 0;
107d4f68a75SDavid Herrmann }
108d4f68a75SDavid Herrmann 
109d4f68a75SDavid Herrmann static void udl_usb_disconnect(struct usb_interface *interface)
110d4f68a75SDavid Herrmann {
111d4f68a75SDavid Herrmann 	struct drm_device *dev = usb_get_intfdata(interface);
112d4f68a75SDavid Herrmann 
113b1a981bdSDaniel Vetter 	drm_kms_helper_poll_fini(dev);
114d4f68a75SDavid Herrmann 	udl_drop_usb(dev);
115c07dcd61SDaniel Vetter 	drm_dev_unplug(dev);
116d4f68a75SDavid Herrmann }
117d4f68a75SDavid Herrmann 
118d4f68a75SDavid Herrmann /*
119d4f68a75SDavid Herrmann  * There are many DisplayLink-based graphics products, all with unique PIDs.
120d4f68a75SDavid Herrmann  * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
121d4f68a75SDavid Herrmann  * We also require a match on SubClass (0x00) and Protocol (0x00),
122d4f68a75SDavid Herrmann  * which is compatible with all known USB 2.0 era graphics chips and firmware,
123d4f68a75SDavid Herrmann  * but allows DisplayLink to increment those for any future incompatible chips
124d4f68a75SDavid Herrmann  */
12536436f4eSArvind Yadav static const struct usb_device_id id_table[] = {
126d4f68a75SDavid Herrmann 	{.idVendor = 0x17e9, .bInterfaceClass = 0xff,
127d4f68a75SDavid Herrmann 	 .bInterfaceSubClass = 0x00,
128d4f68a75SDavid Herrmann 	 .bInterfaceProtocol = 0x00,
129d4f68a75SDavid Herrmann 	 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
130d4f68a75SDavid Herrmann 			USB_DEVICE_ID_MATCH_INT_CLASS |
131d4f68a75SDavid Herrmann 			USB_DEVICE_ID_MATCH_INT_SUBCLASS |
132d4f68a75SDavid Herrmann 			USB_DEVICE_ID_MATCH_INT_PROTOCOL,},
133d4f68a75SDavid Herrmann 	{},
134d4f68a75SDavid Herrmann };
135d4f68a75SDavid Herrmann MODULE_DEVICE_TABLE(usb, id_table);
136d4f68a75SDavid Herrmann 
1375320918bSDave Airlie static struct usb_driver udl_driver = {
1385320918bSDave Airlie 	.name = "udl",
1395320918bSDave Airlie 	.probe = udl_usb_probe,
1405320918bSDave Airlie 	.disconnect = udl_usb_disconnect,
141737ba109SHaixia Shi 	.suspend = udl_usb_suspend,
142737ba109SHaixia Shi 	.resume = udl_usb_resume,
1435320918bSDave Airlie 	.id_table = id_table,
1445320918bSDave Airlie };
145a6ddd2f1SAmitoj Kaur Chawla module_usb_driver(udl_driver);
146d4f68a75SDavid Herrmann MODULE_LICENSE("GPL");
147