blob: 1239decd46ffbe711c44dfd31c92ac3a961bdc27 [file] [log] [blame]
Dave Airlie53209182010-12-15 07:14:24 +10001/*
2 * Copyright (C) 2012 Red Hat
3 *
4 * based in parts on udlfb.c:
5 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
6 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
7 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License v2. See the file COPYING in the main directory of this archive for
11 * more details.
12 */
David Howells760285e2012-10-02 18:01:07 +010013#include <drm/drmP.h>
Robert Tarasovafdfc4c2017-10-12 17:13:49 -070014#include <drm/drm_crtc_helper.h>
Dave Airlie53209182010-12-15 07:14:24 +100015#include "udl_drv.h"
16
17/* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
18#define BULK_SIZE 512
19
Jamie Lentind1c151dc2016-08-22 23:17:34 +010020#define NR_USB_REQUEST_CHANNEL 0x12
21
Dave Airlie53209182010-12-15 07:14:24 +100022#define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
23#define WRITES_IN_FLIGHT (4)
24#define MAX_VENDOR_DESCRIPTOR_SIZE 256
25
26#define GET_URB_TIMEOUT HZ
27#define FREE_URB_TIMEOUT (HZ*2)
28
29static int udl_parse_vendor_descriptor(struct drm_device *dev,
30 struct usb_device *usbdev)
31{
32 struct udl_device *udl = dev->dev_private;
33 char *desc;
34 char *buf;
35 char *desc_end;
36
37 u8 total_len = 0;
38
39 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
40 if (!buf)
41 return false;
42 desc = buf;
43
44 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
45 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
46 if (total_len > 5) {
Andy Shevchenko08fcd722013-08-02 14:09:24 +030047 DRM_INFO("vendor descriptor length:%x data:%11ph\n",
48 total_len, desc);
Dave Airlie53209182010-12-15 07:14:24 +100049
50 if ((desc[0] != total_len) || /* descriptor length */
51 (desc[1] != 0x5f) || /* vendor descriptor type */
52 (desc[2] != 0x01) || /* version (2 bytes) */
53 (desc[3] != 0x00) ||
54 (desc[4] != total_len - 2)) /* length after type */
55 goto unrecognized;
56
57 desc_end = desc + total_len;
58 desc += 5; /* the fixed header we've already parsed */
59
60 while (desc < desc_end) {
61 u8 length;
62 u16 key;
63
Dave Airlied42f0342012-06-25 07:32:14 +010064 key = le16_to_cpu(*((u16 *) desc));
Dave Airlie53209182010-12-15 07:14:24 +100065 desc += sizeof(u16);
66 length = *desc;
67 desc++;
68
69 switch (key) {
70 case 0x0200: { /* max_area */
71 u32 max_area;
72 max_area = le32_to_cpu(*((u32 *)desc));
73 DRM_DEBUG("DL chip limited to %d pixel modes\n",
74 max_area);
75 udl->sku_pixel_limit = max_area;
76 break;
77 }
78 default:
79 break;
80 }
81 desc += length;
82 }
83 }
84
85 goto success;
86
87unrecognized:
88 /* allow udlfb to load for now even if firmware unrecognized */
89 DRM_ERROR("Unrecognized vendor firmware descriptor\n");
90
91success:
92 kfree(buf);
93 return true;
94}
95
Jamie Lentind1c151dc2016-08-22 23:17:34 +010096/*
97 * Need to ensure a channel is selected before submitting URBs
98 */
99static int udl_select_std_channel(struct udl_device *udl)
100{
101 int ret;
Dave Airliee5581fe2016-11-08 16:38:00 +1000102 static const u8 set_def_chn[] = {0x57, 0xCD, 0xDC, 0xA7,
103 0x1C, 0x88, 0x5E, 0x15,
104 0x60, 0xFE, 0xC6, 0x97,
105 0x16, 0x3D, 0x47, 0xF2};
106 void *sendbuf;
107
108 sendbuf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL);
109 if (!sendbuf)
110 return -ENOMEM;
Jamie Lentind1c151dc2016-08-22 23:17:34 +0100111
112 ret = usb_control_msg(udl->udev,
113 usb_sndctrlpipe(udl->udev, 0),
114 NR_USB_REQUEST_CHANNEL,
115 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
Dave Airliee5581fe2016-11-08 16:38:00 +1000116 sendbuf, sizeof(set_def_chn),
Jamie Lentind1c151dc2016-08-22 23:17:34 +0100117 USB_CTRL_SET_TIMEOUT);
Dave Airliee5581fe2016-11-08 16:38:00 +1000118 kfree(sendbuf);
Jamie Lentind1c151dc2016-08-22 23:17:34 +0100119 return ret < 0 ? ret : 0;
120}
121
Dave Airlie53209182010-12-15 07:14:24 +1000122static void udl_release_urb_work(struct work_struct *work)
123{
124 struct urb_node *unode = container_of(work, struct urb_node,
125 release_urb_work.work);
126
127 up(&unode->dev->urbs.limit_sem);
128}
129
130void udl_urb_completion(struct urb *urb)
131{
132 struct urb_node *unode = urb->context;
133 struct udl_device *udl = unode->dev;
134 unsigned long flags;
135
136 /* sync/async unlink faults aren't errors */
137 if (urb->status) {
138 if (!(urb->status == -ENOENT ||
139 urb->status == -ECONNRESET ||
140 urb->status == -ESHUTDOWN)) {
141 DRM_ERROR("%s - nonzero write bulk status received: %d\n",
142 __func__, urb->status);
143 atomic_set(&udl->lost_pixels, 1);
144 }
145 }
146
147 urb->transfer_buffer_length = udl->urbs.size; /* reset to actual */
148
149 spin_lock_irqsave(&udl->urbs.lock, flags);
150 list_add_tail(&unode->entry, &udl->urbs.list);
151 udl->urbs.available++;
152 spin_unlock_irqrestore(&udl->urbs.lock, flags);
153
154#if 0
155 /*
156 * When using fb_defio, we deadlock if up() is called
157 * while another is waiting. So queue to another process.
158 */
159 if (fb_defio)
160 schedule_delayed_work(&unode->release_urb_work, 0);
161 else
162#endif
163 up(&udl->urbs.limit_sem);
164}
165
166static void udl_free_urb_list(struct drm_device *dev)
167{
168 struct udl_device *udl = dev->dev_private;
169 int count = udl->urbs.count;
170 struct list_head *node;
171 struct urb_node *unode;
172 struct urb *urb;
Dave Airlie53209182010-12-15 07:14:24 +1000173 unsigned long flags;
174
175 DRM_DEBUG("Waiting for completes and freeing all render urbs\n");
176
177 /* keep waiting and freeing, until we've got 'em all */
178 while (count--) {
Mikulas Patocka8456b992018-06-03 16:40:55 +0200179 down(&udl->urbs.limit_sem);
Dave Airlie53209182010-12-15 07:14:24 +1000180
181 spin_lock_irqsave(&udl->urbs.lock, flags);
182
183 node = udl->urbs.list.next; /* have reserved one with sem */
184 list_del_init(node);
185
186 spin_unlock_irqrestore(&udl->urbs.lock, flags);
187
188 unode = list_entry(node, struct urb_node, entry);
189 urb = unode->urb;
190
191 /* Free each separately allocated piece */
192 usb_free_coherent(urb->dev, udl->urbs.size,
193 urb->transfer_buffer, urb->transfer_dma);
194 usb_free_urb(urb);
195 kfree(node);
196 }
197 udl->urbs.count = 0;
198}
199
200static int udl_alloc_urb_list(struct drm_device *dev, int count, size_t size)
201{
202 struct udl_device *udl = dev->dev_private;
203 int i = 0;
204 struct urb *urb;
205 struct urb_node *unode;
206 char *buf;
207
208 spin_lock_init(&udl->urbs.lock);
209
210 udl->urbs.size = size;
211 INIT_LIST_HEAD(&udl->urbs.list);
212
213 while (i < count) {
214 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
215 if (!unode)
216 break;
217 unode->dev = udl;
218
219 INIT_DELAYED_WORK(&unode->release_urb_work,
220 udl_release_urb_work);
221
222 urb = usb_alloc_urb(0, GFP_KERNEL);
223 if (!urb) {
224 kfree(unode);
225 break;
226 }
227 unode->urb = urb;
228
David Herrmannd4f68a72014-08-29 12:12:45 +0200229 buf = usb_alloc_coherent(udl->udev, MAX_TRANSFER, GFP_KERNEL,
Dave Airlie53209182010-12-15 07:14:24 +1000230 &urb->transfer_dma);
231 if (!buf) {
232 kfree(unode);
233 usb_free_urb(urb);
234 break;
235 }
236
237 /* urb->transfer_buffer_length set to actual before submit */
David Herrmannd4f68a72014-08-29 12:12:45 +0200238 usb_fill_bulk_urb(urb, udl->udev, usb_sndbulkpipe(udl->udev, 1),
Dave Airlie53209182010-12-15 07:14:24 +1000239 buf, size, udl_urb_completion, unode);
240 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
241
242 list_add_tail(&unode->entry, &udl->urbs.list);
243
244 i++;
245 }
246
247 sema_init(&udl->urbs.limit_sem, i);
248 udl->urbs.count = i;
249 udl->urbs.available = i;
250
251 DRM_DEBUG("allocated %d %d byte urbs\n", i, (int) size);
252
253 return i;
254}
255
256struct urb *udl_get_urb(struct drm_device *dev)
257{
258 struct udl_device *udl = dev->dev_private;
259 int ret = 0;
260 struct list_head *entry;
261 struct urb_node *unode;
262 struct urb *urb = NULL;
263 unsigned long flags;
264
265 /* Wait for an in-flight buffer to complete and get re-queued */
266 ret = down_timeout(&udl->urbs.limit_sem, GET_URB_TIMEOUT);
267 if (ret) {
268 atomic_set(&udl->lost_pixels, 1);
269 DRM_INFO("wait for urb interrupted: %x available: %d\n",
270 ret, udl->urbs.available);
271 goto error;
272 }
273
274 spin_lock_irqsave(&udl->urbs.lock, flags);
275
276 BUG_ON(list_empty(&udl->urbs.list)); /* reserved one with limit_sem */
277 entry = udl->urbs.list.next;
278 list_del_init(entry);
279 udl->urbs.available--;
280
281 spin_unlock_irqrestore(&udl->urbs.lock, flags);
282
283 unode = list_entry(entry, struct urb_node, entry);
284 urb = unode->urb;
285
286error:
287 return urb;
288}
289
290int udl_submit_urb(struct drm_device *dev, struct urb *urb, size_t len)
291{
292 struct udl_device *udl = dev->dev_private;
293 int ret;
294
295 BUG_ON(len > udl->urbs.size);
296
297 urb->transfer_buffer_length = len; /* set to actual payload len */
298 ret = usb_submit_urb(urb, GFP_ATOMIC);
299 if (ret) {
300 udl_urb_completion(urb); /* because no one else will */
301 atomic_set(&udl->lost_pixels, 1);
302 DRM_ERROR("usb_submit_urb error %x\n", ret);
303 }
304 return ret;
305}
306
307int udl_driver_load(struct drm_device *dev, unsigned long flags)
308{
David Herrmannd4f68a72014-08-29 12:12:45 +0200309 struct usb_device *udev = (void*)flags;
Dave Airlie53209182010-12-15 07:14:24 +1000310 struct udl_device *udl;
Oliver Neukum737583f2014-05-19 13:50:22 +0200311 int ret = -ENOMEM;
Dave Airlie53209182010-12-15 07:14:24 +1000312
313 DRM_DEBUG("\n");
314 udl = kzalloc(sizeof(struct udl_device), GFP_KERNEL);
315 if (!udl)
316 return -ENOMEM;
317
David Herrmannd4f68a72014-08-29 12:12:45 +0200318 udl->udev = udev;
Dave Airlie53209182010-12-15 07:14:24 +1000319 udl->ddev = dev;
320 dev->dev_private = udl;
321
Daniel Vetterae358da2018-03-27 10:23:54 +0200322 mutex_init(&udl->gem_lock);
323
David Herrmannd4f68a72014-08-29 12:12:45 +0200324 if (!udl_parse_vendor_descriptor(dev, udl->udev)) {
Daniel Vettere39a52d2014-04-05 10:25:18 +0200325 ret = -ENODEV;
Dave Airlie53209182010-12-15 07:14:24 +1000326 DRM_ERROR("firmware not recognized. Assume incompatible device\n");
327 goto err;
328 }
329
Jamie Lentind1c151dc2016-08-22 23:17:34 +0100330 if (udl_select_std_channel(udl))
331 DRM_ERROR("Selecting channel failed\n");
332
Dave Airlie53209182010-12-15 07:14:24 +1000333 if (!udl_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
Dave Airlie53209182010-12-15 07:14:24 +1000334 DRM_ERROR("udl_alloc_urb_list failed\n");
335 goto err;
336 }
337
338 DRM_DEBUG("\n");
339 ret = udl_modeset_init(dev);
Stéphane Marchesin26507b02014-07-02 15:13:42 -0700340 if (ret)
341 goto err;
Dave Airlie53209182010-12-15 07:14:24 +1000342
343 ret = udl_fbdev_init(dev);
Stéphane Marchesin26507b02014-07-02 15:13:42 -0700344 if (ret)
345 goto err;
346
347 ret = drm_vblank_init(dev, 1);
348 if (ret)
349 goto err_fb;
350
Robert Tarasovafdfc4c2017-10-12 17:13:49 -0700351 drm_kms_helper_poll_init(dev);
352
Dave Airlie53209182010-12-15 07:14:24 +1000353 return 0;
Stéphane Marchesin26507b02014-07-02 15:13:42 -0700354err_fb:
355 udl_fbdev_cleanup(dev);
Dave Airlie53209182010-12-15 07:14:24 +1000356err:
Stéphane Marchesin26507b02014-07-02 15:13:42 -0700357 if (udl->urbs.count)
358 udl_free_urb_list(dev);
Dave Airlie53209182010-12-15 07:14:24 +1000359 kfree(udl);
360 DRM_ERROR("%d\n", ret);
361 return ret;
362}
363
364int udl_drop_usb(struct drm_device *dev)
365{
366 udl_free_urb_list(dev);
367 return 0;
368}
369
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200370void udl_driver_unload(struct drm_device *dev)
Dave Airlie53209182010-12-15 07:14:24 +1000371{
372 struct udl_device *udl = dev->dev_private;
373
Robert Tarasovafdfc4c2017-10-12 17:13:49 -0700374 drm_kms_helper_poll_fini(dev);
375
Dave Airlie53209182010-12-15 07:14:24 +1000376 if (udl->urbs.count)
377 udl_free_urb_list(dev);
378
379 udl_fbdev_cleanup(dev);
380 udl_modeset_cleanup(dev);
381 kfree(udl);
Dave Airlie53209182010-12-15 07:14:24 +1000382}