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
16 /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
19 #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
20 #define WRITES_IN_FLIGHT (4)
21 #define MAX_VENDOR_DESCRIPTOR_SIZE 256
23 #define GET_URB_TIMEOUT HZ
24 #define FREE_URB_TIMEOUT (HZ*2)
26 static int udl_parse_vendor_descriptor(struct drm_device
*dev
,
27 struct usb_device
*usbdev
)
29 struct udl_device
*udl
= dev
->dev_private
;
36 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
41 total_len
= usb_get_descriptor(usbdev
, 0x5f, /* vendor specific */
42 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
44 DRM_INFO("vendor descriptor length:%x data:%11ph\n",
47 if ((desc
[0] != total_len
) || /* descriptor length */
48 (desc
[1] != 0x5f) || /* vendor descriptor type */
49 (desc
[2] != 0x01) || /* version (2 bytes) */
51 (desc
[4] != total_len
- 2)) /* length after type */
54 desc_end
= desc
+ total_len
;
55 desc
+= 5; /* the fixed header we've already parsed */
57 while (desc
< desc_end
) {
61 key
= le16_to_cpu(*((u16
*) desc
));
67 case 0x0200: { /* max_area */
69 max_area
= le32_to_cpu(*((u32
*)desc
));
70 DRM_DEBUG("DL chip limited to %d pixel modes\n",
72 udl
->sku_pixel_limit
= max_area
;
85 /* allow udlfb to load for now even if firmware unrecognized */
86 DRM_ERROR("Unrecognized vendor firmware descriptor\n");
93 static void udl_release_urb_work(struct work_struct
*work
)
95 struct urb_node
*unode
= container_of(work
, struct urb_node
,
96 release_urb_work
.work
);
98 up(&unode
->dev
->urbs
.limit_sem
);
101 void udl_urb_completion(struct urb
*urb
)
103 struct urb_node
*unode
= urb
->context
;
104 struct udl_device
*udl
= unode
->dev
;
107 /* sync/async unlink faults aren't errors */
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);
118 urb
->transfer_buffer_length
= udl
->urbs
.size
; /* reset to actual */
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
);
127 * When using fb_defio, we deadlock if up() is called
128 * while another is waiting. So queue to another process.
131 schedule_delayed_work(&unode
->release_urb_work
, 0);
134 up(&udl
->urbs
.limit_sem
);
137 static void udl_free_urb_list(struct drm_device
*dev
)
139 struct udl_device
*udl
= dev
->dev_private
;
140 int count
= udl
->urbs
.count
;
141 struct list_head
*node
;
142 struct urb_node
*unode
;
147 DRM_DEBUG("Waiting for completes and freeing all render urbs\n");
149 /* keep waiting and freeing, until we've got 'em all */
152 /* Getting interrupted means a leak, but ok at shutdown*/
153 ret
= down_interruptible(&udl
->urbs
.limit_sem
);
157 spin_lock_irqsave(&udl
->urbs
.lock
, flags
);
159 node
= udl
->urbs
.list
.next
; /* have reserved one with sem */
162 spin_unlock_irqrestore(&udl
->urbs
.lock
, flags
);
164 unode
= list_entry(node
, struct urb_node
, entry
);
167 /* Free each separately allocated piece */
168 usb_free_coherent(urb
->dev
, udl
->urbs
.size
,
169 urb
->transfer_buffer
, urb
->transfer_dma
);
176 static int udl_alloc_urb_list(struct drm_device
*dev
, int count
, size_t size
)
178 struct udl_device
*udl
= dev
->dev_private
;
181 struct urb_node
*unode
;
184 spin_lock_init(&udl
->urbs
.lock
);
186 udl
->urbs
.size
= size
;
187 INIT_LIST_HEAD(&udl
->urbs
.list
);
190 unode
= kzalloc(sizeof(struct urb_node
), GFP_KERNEL
);
195 INIT_DELAYED_WORK(&unode
->release_urb_work
,
196 udl_release_urb_work
);
198 urb
= usb_alloc_urb(0, GFP_KERNEL
);
205 buf
= usb_alloc_coherent(udl
->udev
, MAX_TRANSFER
, GFP_KERNEL
,
213 /* urb->transfer_buffer_length set to actual before submit */
214 usb_fill_bulk_urb(urb
, udl
->udev
, usb_sndbulkpipe(udl
->udev
, 1),
215 buf
, size
, udl_urb_completion
, unode
);
216 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
218 list_add_tail(&unode
->entry
, &udl
->urbs
.list
);
223 sema_init(&udl
->urbs
.limit_sem
, i
);
225 udl
->urbs
.available
= i
;
227 DRM_DEBUG("allocated %d %d byte urbs\n", i
, (int) size
);
232 struct urb
*udl_get_urb(struct drm_device
*dev
)
234 struct udl_device
*udl
= dev
->dev_private
;
236 struct list_head
*entry
;
237 struct urb_node
*unode
;
238 struct urb
*urb
= NULL
;
241 /* Wait for an in-flight buffer to complete and get re-queued */
242 ret
= down_timeout(&udl
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
244 atomic_set(&udl
->lost_pixels
, 1);
245 DRM_INFO("wait for urb interrupted: %x available: %d\n",
246 ret
, udl
->urbs
.available
);
250 spin_lock_irqsave(&udl
->urbs
.lock
, flags
);
252 BUG_ON(list_empty(&udl
->urbs
.list
)); /* reserved one with limit_sem */
253 entry
= udl
->urbs
.list
.next
;
254 list_del_init(entry
);
255 udl
->urbs
.available
--;
257 spin_unlock_irqrestore(&udl
->urbs
.lock
, flags
);
259 unode
= list_entry(entry
, struct urb_node
, entry
);
266 int udl_submit_urb(struct drm_device
*dev
, struct urb
*urb
, size_t len
)
268 struct udl_device
*udl
= dev
->dev_private
;
271 BUG_ON(len
> udl
->urbs
.size
);
273 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
274 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
276 udl_urb_completion(urb
); /* because no one else will */
277 atomic_set(&udl
->lost_pixels
, 1);
278 DRM_ERROR("usb_submit_urb error %x\n", ret
);
283 int udl_driver_load(struct drm_device
*dev
, unsigned long flags
)
285 struct usb_device
*udev
= (void*)flags
;
286 struct udl_device
*udl
;
290 udl
= kzalloc(sizeof(struct udl_device
), GFP_KERNEL
);
296 dev
->dev_private
= udl
;
298 if (!udl_parse_vendor_descriptor(dev
, udl
->udev
)) {
300 DRM_ERROR("firmware not recognized. Assume incompatible device\n");
304 if (!udl_alloc_urb_list(dev
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
305 DRM_ERROR("udl_alloc_urb_list failed\n");
310 ret
= udl_modeset_init(dev
);
314 ret
= udl_fbdev_init(dev
);
318 ret
= drm_vblank_init(dev
, 1);
324 udl_fbdev_cleanup(dev
);
327 udl_free_urb_list(dev
);
329 DRM_ERROR("%d\n", ret
);
333 int udl_drop_usb(struct drm_device
*dev
)
335 udl_free_urb_list(dev
);
339 int udl_driver_unload(struct drm_device
*dev
)
341 struct udl_device
*udl
= dev
->dev_private
;
343 drm_vblank_cleanup(dev
);
346 udl_free_urb_list(dev
);
348 udl_fbdev_cleanup(dev
);
349 udl_modeset_cleanup(dev
);