mm: treewide: remove GFP_TEMPORARY allocation flag
[linux/fpc-iii.git] / drivers / gpu / drm / udl / udl_main.c
blob0328b2c7b210a0331f8721e93c3b25124ad27daf
1 /*
2 * Copyright (C) 2012 Red Hat
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>
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.
13 #include <drm/drmP.h>
14 #include "udl_drv.h"
16 /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
17 #define BULK_SIZE 512
19 #define NR_USB_REQUEST_CHANNEL 0x12
21 #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
22 #define WRITES_IN_FLIGHT (4)
23 #define MAX_VENDOR_DESCRIPTOR_SIZE 256
25 #define GET_URB_TIMEOUT HZ
26 #define FREE_URB_TIMEOUT (HZ*2)
28 static int udl_parse_vendor_descriptor(struct drm_device *dev,
29 struct usb_device *usbdev)
31 struct udl_device *udl = dev->dev_private;
32 char *desc;
33 char *buf;
34 char *desc_end;
36 u8 total_len = 0;
38 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
39 if (!buf)
40 return false;
41 desc = buf;
43 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
44 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
45 if (total_len > 5) {
46 DRM_INFO("vendor descriptor length:%x data:%11ph\n",
47 total_len, desc);
49 if ((desc[0] != total_len) || /* descriptor length */
50 (desc[1] != 0x5f) || /* vendor descriptor type */
51 (desc[2] != 0x01) || /* version (2 bytes) */
52 (desc[3] != 0x00) ||
53 (desc[4] != total_len - 2)) /* length after type */
54 goto unrecognized;
56 desc_end = desc + total_len;
57 desc += 5; /* the fixed header we've already parsed */
59 while (desc < desc_end) {
60 u8 length;
61 u16 key;
63 key = le16_to_cpu(*((u16 *) desc));
64 desc += sizeof(u16);
65 length = *desc;
66 desc++;
68 switch (key) {
69 case 0x0200: { /* max_area */
70 u32 max_area;
71 max_area = le32_to_cpu(*((u32 *)desc));
72 DRM_DEBUG("DL chip limited to %d pixel modes\n",
73 max_area);
74 udl->sku_pixel_limit = max_area;
75 break;
77 default:
78 break;
80 desc += length;
84 goto success;
86 unrecognized:
87 /* allow udlfb to load for now even if firmware unrecognized */
88 DRM_ERROR("Unrecognized vendor firmware descriptor\n");
90 success:
91 kfree(buf);
92 return true;
96 * Need to ensure a channel is selected before submitting URBs
98 static int udl_select_std_channel(struct udl_device *udl)
100 int ret;
101 static const u8 set_def_chn[] = {0x57, 0xCD, 0xDC, 0xA7,
102 0x1C, 0x88, 0x5E, 0x15,
103 0x60, 0xFE, 0xC6, 0x97,
104 0x16, 0x3D, 0x47, 0xF2};
105 void *sendbuf;
107 sendbuf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL);
108 if (!sendbuf)
109 return -ENOMEM;
111 ret = usb_control_msg(udl->udev,
112 usb_sndctrlpipe(udl->udev, 0),
113 NR_USB_REQUEST_CHANNEL,
114 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
115 sendbuf, sizeof(set_def_chn),
116 USB_CTRL_SET_TIMEOUT);
117 kfree(sendbuf);
118 return ret < 0 ? ret : 0;
121 static void udl_release_urb_work(struct work_struct *work)
123 struct urb_node *unode = container_of(work, struct urb_node,
124 release_urb_work.work);
126 up(&unode->dev->urbs.limit_sem);
129 void udl_urb_completion(struct urb *urb)
131 struct urb_node *unode = urb->context;
132 struct udl_device *udl = unode->dev;
133 unsigned long flags;
135 /* sync/async unlink faults aren't errors */
136 if (urb->status) {
137 if (!(urb->status == -ENOENT ||
138 urb->status == -ECONNRESET ||
139 urb->status == -ESHUTDOWN)) {
140 DRM_ERROR("%s - nonzero write bulk status received: %d\n",
141 __func__, urb->status);
142 atomic_set(&udl->lost_pixels, 1);
146 urb->transfer_buffer_length = udl->urbs.size; /* reset to actual */
148 spin_lock_irqsave(&udl->urbs.lock, flags);
149 list_add_tail(&unode->entry, &udl->urbs.list);
150 udl->urbs.available++;
151 spin_unlock_irqrestore(&udl->urbs.lock, flags);
153 #if 0
155 * When using fb_defio, we deadlock if up() is called
156 * while another is waiting. So queue to another process.
158 if (fb_defio)
159 schedule_delayed_work(&unode->release_urb_work, 0);
160 else
161 #endif
162 up(&udl->urbs.limit_sem);
165 static void udl_free_urb_list(struct drm_device *dev)
167 struct udl_device *udl = dev->dev_private;
168 int count = udl->urbs.count;
169 struct list_head *node;
170 struct urb_node *unode;
171 struct urb *urb;
172 int ret;
173 unsigned long flags;
175 DRM_DEBUG("Waiting for completes and freeing all render urbs\n");
177 /* keep waiting and freeing, until we've got 'em all */
178 while (count--) {
180 /* Getting interrupted means a leak, but ok at shutdown*/
181 ret = down_interruptible(&udl->urbs.limit_sem);
182 if (ret)
183 break;
185 spin_lock_irqsave(&udl->urbs.lock, flags);
187 node = udl->urbs.list.next; /* have reserved one with sem */
188 list_del_init(node);
190 spin_unlock_irqrestore(&udl->urbs.lock, flags);
192 unode = list_entry(node, struct urb_node, entry);
193 urb = unode->urb;
195 /* Free each separately allocated piece */
196 usb_free_coherent(urb->dev, udl->urbs.size,
197 urb->transfer_buffer, urb->transfer_dma);
198 usb_free_urb(urb);
199 kfree(node);
201 udl->urbs.count = 0;
204 static int udl_alloc_urb_list(struct drm_device *dev, int count, size_t size)
206 struct udl_device *udl = dev->dev_private;
207 int i = 0;
208 struct urb *urb;
209 struct urb_node *unode;
210 char *buf;
212 spin_lock_init(&udl->urbs.lock);
214 udl->urbs.size = size;
215 INIT_LIST_HEAD(&udl->urbs.list);
217 while (i < count) {
218 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
219 if (!unode)
220 break;
221 unode->dev = udl;
223 INIT_DELAYED_WORK(&unode->release_urb_work,
224 udl_release_urb_work);
226 urb = usb_alloc_urb(0, GFP_KERNEL);
227 if (!urb) {
228 kfree(unode);
229 break;
231 unode->urb = urb;
233 buf = usb_alloc_coherent(udl->udev, MAX_TRANSFER, GFP_KERNEL,
234 &urb->transfer_dma);
235 if (!buf) {
236 kfree(unode);
237 usb_free_urb(urb);
238 break;
241 /* urb->transfer_buffer_length set to actual before submit */
242 usb_fill_bulk_urb(urb, udl->udev, usb_sndbulkpipe(udl->udev, 1),
243 buf, size, udl_urb_completion, unode);
244 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
246 list_add_tail(&unode->entry, &udl->urbs.list);
248 i++;
251 sema_init(&udl->urbs.limit_sem, i);
252 udl->urbs.count = i;
253 udl->urbs.available = i;
255 DRM_DEBUG("allocated %d %d byte urbs\n", i, (int) size);
257 return i;
260 struct urb *udl_get_urb(struct drm_device *dev)
262 struct udl_device *udl = dev->dev_private;
263 int ret = 0;
264 struct list_head *entry;
265 struct urb_node *unode;
266 struct urb *urb = NULL;
267 unsigned long flags;
269 /* Wait for an in-flight buffer to complete and get re-queued */
270 ret = down_timeout(&udl->urbs.limit_sem, GET_URB_TIMEOUT);
271 if (ret) {
272 atomic_set(&udl->lost_pixels, 1);
273 DRM_INFO("wait for urb interrupted: %x available: %d\n",
274 ret, udl->urbs.available);
275 goto error;
278 spin_lock_irqsave(&udl->urbs.lock, flags);
280 BUG_ON(list_empty(&udl->urbs.list)); /* reserved one with limit_sem */
281 entry = udl->urbs.list.next;
282 list_del_init(entry);
283 udl->urbs.available--;
285 spin_unlock_irqrestore(&udl->urbs.lock, flags);
287 unode = list_entry(entry, struct urb_node, entry);
288 urb = unode->urb;
290 error:
291 return urb;
294 int udl_submit_urb(struct drm_device *dev, struct urb *urb, size_t len)
296 struct udl_device *udl = dev->dev_private;
297 int ret;
299 BUG_ON(len > udl->urbs.size);
301 urb->transfer_buffer_length = len; /* set to actual payload len */
302 ret = usb_submit_urb(urb, GFP_ATOMIC);
303 if (ret) {
304 udl_urb_completion(urb); /* because no one else will */
305 atomic_set(&udl->lost_pixels, 1);
306 DRM_ERROR("usb_submit_urb error %x\n", ret);
308 return ret;
311 int udl_driver_load(struct drm_device *dev, unsigned long flags)
313 struct usb_device *udev = (void*)flags;
314 struct udl_device *udl;
315 int ret = -ENOMEM;
317 DRM_DEBUG("\n");
318 udl = kzalloc(sizeof(struct udl_device), GFP_KERNEL);
319 if (!udl)
320 return -ENOMEM;
322 udl->udev = udev;
323 udl->ddev = dev;
324 dev->dev_private = udl;
326 if (!udl_parse_vendor_descriptor(dev, udl->udev)) {
327 ret = -ENODEV;
328 DRM_ERROR("firmware not recognized. Assume incompatible device\n");
329 goto err;
332 if (udl_select_std_channel(udl))
333 DRM_ERROR("Selecting channel failed\n");
335 if (!udl_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
336 DRM_ERROR("udl_alloc_urb_list failed\n");
337 goto err;
340 DRM_DEBUG("\n");
341 ret = udl_modeset_init(dev);
342 if (ret)
343 goto err;
345 ret = udl_fbdev_init(dev);
346 if (ret)
347 goto err;
349 ret = drm_vblank_init(dev, 1);
350 if (ret)
351 goto err_fb;
353 return 0;
354 err_fb:
355 udl_fbdev_cleanup(dev);
356 err:
357 if (udl->urbs.count)
358 udl_free_urb_list(dev);
359 kfree(udl);
360 DRM_ERROR("%d\n", ret);
361 return ret;
364 int udl_drop_usb(struct drm_device *dev)
366 udl_free_urb_list(dev);
367 return 0;
370 void udl_driver_unload(struct drm_device *dev)
372 struct udl_device *udl = dev->dev_private;
374 if (udl->urbs.count)
375 udl_free_urb_list(dev);
377 udl_fbdev_cleanup(dev);
378 udl_modeset_cleanup(dev);
379 kfree(udl);