• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
13 #include <drm/drmP.h>
14 #include "udl_drv.h"
15 
16 /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
17 #define BULK_SIZE 512
18 
19 #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
20 #define WRITES_IN_FLIGHT (4)
21 #define MAX_VENDOR_DESCRIPTOR_SIZE 256
22 
23 #define GET_URB_TIMEOUT	HZ
24 #define FREE_URB_TIMEOUT (HZ*2)
25 
udl_parse_vendor_descriptor(struct drm_device * dev,struct usb_device * usbdev)26 static int udl_parse_vendor_descriptor(struct drm_device *dev,
27 				       struct usb_device *usbdev)
28 {
29 	struct udl_device *udl = dev->dev_private;
30 	char *desc;
31 	char *buf;
32 	char *desc_end;
33 
34 	u8 total_len = 0;
35 
36 	buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
37 	if (!buf)
38 		return false;
39 	desc = buf;
40 
41 	total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
42 				    0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
43 	if (total_len > 5) {
44 		DRM_INFO("vendor descriptor length:%x data:%11ph\n",
45 			total_len, desc);
46 
47 		if ((desc[0] != total_len) || /* descriptor length */
48 		    (desc[1] != 0x5f) ||   /* vendor descriptor type */
49 		    (desc[2] != 0x01) ||   /* version (2 bytes) */
50 		    (desc[3] != 0x00) ||
51 		    (desc[4] != total_len - 2)) /* length after type */
52 			goto unrecognized;
53 
54 		desc_end = desc + total_len;
55 		desc += 5; /* the fixed header we've already parsed */
56 
57 		while (desc < desc_end) {
58 			u8 length;
59 			u16 key;
60 
61 			key = le16_to_cpu(*((u16 *) desc));
62 			desc += sizeof(u16);
63 			length = *desc;
64 			desc++;
65 
66 			switch (key) {
67 			case 0x0200: { /* max_area */
68 				u32 max_area;
69 				max_area = le32_to_cpu(*((u32 *)desc));
70 				DRM_DEBUG("DL chip limited to %d pixel modes\n",
71 					max_area);
72 				udl->sku_pixel_limit = max_area;
73 				break;
74 			}
75 			default:
76 				break;
77 			}
78 			desc += length;
79 		}
80 	}
81 
82 	goto success;
83 
84 unrecognized:
85 	/* allow udlfb to load for now even if firmware unrecognized */
86 	DRM_ERROR("Unrecognized vendor firmware descriptor\n");
87 
88 success:
89 	kfree(buf);
90 	return true;
91 }
92 
udl_release_urb_work(struct work_struct * work)93 static void udl_release_urb_work(struct work_struct *work)
94 {
95 	struct urb_node *unode = container_of(work, struct urb_node,
96 					      release_urb_work.work);
97 
98 	up(&unode->dev->urbs.limit_sem);
99 }
100 
udl_urb_completion(struct urb * urb)101 void udl_urb_completion(struct urb *urb)
102 {
103 	struct urb_node *unode = urb->context;
104 	struct udl_device *udl = unode->dev;
105 	unsigned long flags;
106 
107 	/* sync/async unlink faults aren't errors */
108 	if (urb->status) {
109 		if (!(urb->status == -ENOENT ||
110 		    urb->status == -ECONNRESET ||
111 		    urb->status == -ESHUTDOWN)) {
112 			DRM_ERROR("%s - nonzero write bulk status received: %d\n",
113 				__func__, urb->status);
114 			atomic_set(&udl->lost_pixels, 1);
115 		}
116 	}
117 
118 	urb->transfer_buffer_length = udl->urbs.size; /* reset to actual */
119 
120 	spin_lock_irqsave(&udl->urbs.lock, flags);
121 	list_add_tail(&unode->entry, &udl->urbs.list);
122 	udl->urbs.available++;
123 	spin_unlock_irqrestore(&udl->urbs.lock, flags);
124 
125 #if 0
126 	/*
127 	 * When using fb_defio, we deadlock if up() is called
128 	 * while another is waiting. So queue to another process.
129 	 */
130 	if (fb_defio)
131 		schedule_delayed_work(&unode->release_urb_work, 0);
132 	else
133 #endif
134 		up(&udl->urbs.limit_sem);
135 }
136 
udl_free_urb_list(struct drm_device * dev)137 static void udl_free_urb_list(struct drm_device *dev)
138 {
139 	struct udl_device *udl = dev->dev_private;
140 	int count = udl->urbs.count;
141 	struct list_head *node;
142 	struct urb_node *unode;
143 	struct urb *urb;
144 	unsigned long flags;
145 
146 	DRM_DEBUG("Waiting for completes and freeing all render urbs\n");
147 
148 	/* keep waiting and freeing, until we've got 'em all */
149 	while (count--) {
150 		down(&udl->urbs.limit_sem);
151 
152 		spin_lock_irqsave(&udl->urbs.lock, flags);
153 
154 		node = udl->urbs.list.next; /* have reserved one with sem */
155 		list_del_init(node);
156 
157 		spin_unlock_irqrestore(&udl->urbs.lock, flags);
158 
159 		unode = list_entry(node, struct urb_node, entry);
160 		urb = unode->urb;
161 
162 		/* Free each separately allocated piece */
163 		usb_free_coherent(urb->dev, udl->urbs.size,
164 				  urb->transfer_buffer, urb->transfer_dma);
165 		usb_free_urb(urb);
166 		kfree(node);
167 	}
168 	udl->urbs.count = 0;
169 }
170 
udl_alloc_urb_list(struct drm_device * dev,int count,size_t size)171 static int udl_alloc_urb_list(struct drm_device *dev, int count, size_t size)
172 {
173 	struct udl_device *udl = dev->dev_private;
174 	struct urb *urb;
175 	struct urb_node *unode;
176 	char *buf;
177 	size_t wanted_size = count * size;
178 
179 	spin_lock_init(&udl->urbs.lock);
180 
181 retry:
182 	udl->urbs.size = size;
183 	INIT_LIST_HEAD(&udl->urbs.list);
184 
185 	sema_init(&udl->urbs.limit_sem, 0);
186 	udl->urbs.count = 0;
187 	udl->urbs.available = 0;
188 
189 	while (udl->urbs.count * size < wanted_size) {
190 		unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
191 		if (!unode)
192 			break;
193 		unode->dev = udl;
194 
195 		INIT_DELAYED_WORK(&unode->release_urb_work,
196 			  udl_release_urb_work);
197 
198 		urb = usb_alloc_urb(0, GFP_KERNEL);
199 		if (!urb) {
200 			kfree(unode);
201 			break;
202 		}
203 		unode->urb = urb;
204 
205 		buf = usb_alloc_coherent(udl->udev, size, GFP_KERNEL,
206 					 &urb->transfer_dma);
207 		if (!buf) {
208 			kfree(unode);
209 			usb_free_urb(urb);
210 			if (size > PAGE_SIZE) {
211 				size /= 2;
212 				udl_free_urb_list(dev);
213 				goto retry;
214 			}
215 			break;
216 		}
217 
218 		/* urb->transfer_buffer_length set to actual before submit */
219 		usb_fill_bulk_urb(urb, udl->udev, usb_sndbulkpipe(udl->udev, 1),
220 			buf, size, udl_urb_completion, unode);
221 		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
222 
223 		list_add_tail(&unode->entry, &udl->urbs.list);
224 
225 		up(&udl->urbs.limit_sem);
226 		udl->urbs.count++;
227 		udl->urbs.available++;
228 	}
229 
230 	DRM_DEBUG("allocated %d %d byte urbs\n", udl->urbs.count, (int) size);
231 
232 	return udl->urbs.count;
233 }
234 
udl_get_urb(struct drm_device * dev)235 struct urb *udl_get_urb(struct drm_device *dev)
236 {
237 	struct udl_device *udl = dev->dev_private;
238 	int ret = 0;
239 	struct list_head *entry;
240 	struct urb_node *unode;
241 	struct urb *urb = NULL;
242 	unsigned long flags;
243 
244 	/* Wait for an in-flight buffer to complete and get re-queued */
245 	ret = down_timeout(&udl->urbs.limit_sem, GET_URB_TIMEOUT);
246 	if (ret) {
247 		atomic_set(&udl->lost_pixels, 1);
248 		DRM_INFO("wait for urb interrupted: %x available: %d\n",
249 		       ret, udl->urbs.available);
250 		goto error;
251 	}
252 
253 	spin_lock_irqsave(&udl->urbs.lock, flags);
254 
255 	BUG_ON(list_empty(&udl->urbs.list)); /* reserved one with limit_sem */
256 	entry = udl->urbs.list.next;
257 	list_del_init(entry);
258 	udl->urbs.available--;
259 
260 	spin_unlock_irqrestore(&udl->urbs.lock, flags);
261 
262 	unode = list_entry(entry, struct urb_node, entry);
263 	urb = unode->urb;
264 
265 error:
266 	return urb;
267 }
268 
udl_submit_urb(struct drm_device * dev,struct urb * urb,size_t len)269 int udl_submit_urb(struct drm_device *dev, struct urb *urb, size_t len)
270 {
271 	struct udl_device *udl = dev->dev_private;
272 	int ret;
273 
274 	BUG_ON(len > udl->urbs.size);
275 
276 	urb->transfer_buffer_length = len; /* set to actual payload len */
277 	ret = usb_submit_urb(urb, GFP_ATOMIC);
278 	if (ret) {
279 		udl_urb_completion(urb); /* because no one else will */
280 		atomic_set(&udl->lost_pixels, 1);
281 		DRM_ERROR("usb_submit_urb error %x\n", ret);
282 	}
283 	return ret;
284 }
285 
udl_driver_load(struct drm_device * dev,unsigned long flags)286 int udl_driver_load(struct drm_device *dev, unsigned long flags)
287 {
288 	struct usb_device *udev = (void*)flags;
289 	struct udl_device *udl;
290 	int ret = -ENOMEM;
291 
292 	DRM_DEBUG("\n");
293 	udl = kzalloc(sizeof(struct udl_device), GFP_KERNEL);
294 	if (!udl)
295 		return -ENOMEM;
296 
297 	udl->udev = udev;
298 	udl->ddev = dev;
299 	dev->dev_private = udl;
300 
301 	if (!udl_parse_vendor_descriptor(dev, udl->udev)) {
302 		ret = -ENODEV;
303 		DRM_ERROR("firmware not recognized. Assume incompatible device\n");
304 		goto err;
305 	}
306 
307 	if (!udl_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
308 		DRM_ERROR("udl_alloc_urb_list failed\n");
309 		goto err;
310 	}
311 
312 	DRM_DEBUG("\n");
313 	ret = udl_modeset_init(dev);
314 	if (ret)
315 		goto err;
316 
317 	ret = udl_fbdev_init(dev);
318 	if (ret)
319 		goto err;
320 
321 	ret = drm_vblank_init(dev, 1);
322 	if (ret)
323 		goto err_fb;
324 
325 	return 0;
326 err_fb:
327 	udl_fbdev_cleanup(dev);
328 err:
329 	if (udl->urbs.count)
330 		udl_free_urb_list(dev);
331 	kfree(udl);
332 	DRM_ERROR("%d\n", ret);
333 	return ret;
334 }
335 
udl_drop_usb(struct drm_device * dev)336 int udl_drop_usb(struct drm_device *dev)
337 {
338 	udl_free_urb_list(dev);
339 	return 0;
340 }
341 
udl_driver_unload(struct drm_device * dev)342 int udl_driver_unload(struct drm_device *dev)
343 {
344 	struct udl_device *udl = dev->dev_private;
345 
346 	drm_vblank_cleanup(dev);
347 
348 	if (udl->urbs.count)
349 		udl_free_urb_list(dev);
350 
351 	udl_fbdev_cleanup(dev);
352 	udl_modeset_cleanup(dev);
353 	kfree(udl);
354 	return 0;
355 }
356