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
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_probe_helper.h>
18 /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
21 #define NR_USB_REQUEST_CHANNEL 0x12
23 #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
24 #define WRITES_IN_FLIGHT (4)
25 #define MAX_VENDOR_DESCRIPTOR_SIZE 256
27 #define GET_URB_TIMEOUT HZ
28 #define FREE_URB_TIMEOUT (HZ*2)
30 static int udl_parse_vendor_descriptor(struct drm_device
*dev
,
31 struct usb_device
*usbdev
)
33 struct udl_device
*udl
= dev
->dev_private
;
40 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
45 total_len
= usb_get_descriptor(usbdev
, 0x5f, /* vendor specific */
46 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
48 DRM_INFO("vendor descriptor length:%x data:%11ph\n",
51 if ((desc
[0] != total_len
) || /* descriptor length */
52 (desc
[1] != 0x5f) || /* vendor descriptor type */
53 (desc
[2] != 0x01) || /* version (2 bytes) */
55 (desc
[4] != total_len
- 2)) /* length after type */
58 desc_end
= desc
+ total_len
;
59 desc
+= 5; /* the fixed header we've already parsed */
61 while (desc
< desc_end
) {
65 key
= le16_to_cpu(*((u16
*) desc
));
71 case 0x0200: { /* max_area */
73 max_area
= le32_to_cpu(*((u32
*)desc
));
74 DRM_DEBUG("DL chip limited to %d pixel modes\n",
76 udl
->sku_pixel_limit
= max_area
;
89 /* allow udlfb to load for now even if firmware unrecognized */
90 DRM_ERROR("Unrecognized vendor firmware descriptor\n");
98 * Need to ensure a channel is selected before submitting URBs
100 static int udl_select_std_channel(struct udl_device
*udl
)
103 static const u8 set_def_chn
[] = {0x57, 0xCD, 0xDC, 0xA7,
104 0x1C, 0x88, 0x5E, 0x15,
105 0x60, 0xFE, 0xC6, 0x97,
106 0x16, 0x3D, 0x47, 0xF2};
109 sendbuf
= kmemdup(set_def_chn
, sizeof(set_def_chn
), GFP_KERNEL
);
113 ret
= usb_control_msg(udl
->udev
,
114 usb_sndctrlpipe(udl
->udev
, 0),
115 NR_USB_REQUEST_CHANNEL
,
116 (USB_DIR_OUT
| USB_TYPE_VENDOR
), 0, 0,
117 sendbuf
, sizeof(set_def_chn
),
118 USB_CTRL_SET_TIMEOUT
);
120 return ret
< 0 ? ret
: 0;
123 static void udl_release_urb_work(struct work_struct
*work
)
125 struct urb_node
*unode
= container_of(work
, struct urb_node
,
126 release_urb_work
.work
);
128 up(&unode
->dev
->urbs
.limit_sem
);
131 void udl_urb_completion(struct urb
*urb
)
133 struct urb_node
*unode
= urb
->context
;
134 struct udl_device
*udl
= unode
->dev
;
137 /* sync/async unlink faults aren't errors */
139 if (!(urb
->status
== -ENOENT
||
140 urb
->status
== -ECONNRESET
||
141 urb
->status
== -ESHUTDOWN
)) {
142 DRM_ERROR("%s - nonzero write bulk status received: %d\n",
143 __func__
, urb
->status
);
144 atomic_set(&udl
->lost_pixels
, 1);
148 urb
->transfer_buffer_length
= udl
->urbs
.size
; /* reset to actual */
150 spin_lock_irqsave(&udl
->urbs
.lock
, flags
);
151 list_add_tail(&unode
->entry
, &udl
->urbs
.list
);
152 udl
->urbs
.available
++;
153 spin_unlock_irqrestore(&udl
->urbs
.lock
, flags
);
157 * When using fb_defio, we deadlock if up() is called
158 * while another is waiting. So queue to another process.
161 schedule_delayed_work(&unode
->release_urb_work
, 0);
164 up(&udl
->urbs
.limit_sem
);
167 static void udl_free_urb_list(struct drm_device
*dev
)
169 struct udl_device
*udl
= dev
->dev_private
;
170 int count
= udl
->urbs
.count
;
171 struct list_head
*node
;
172 struct urb_node
*unode
;
175 DRM_DEBUG("Waiting for completes and freeing all render urbs\n");
177 /* keep waiting and freeing, until we've got 'em all */
179 down(&udl
->urbs
.limit_sem
);
181 spin_lock_irq(&udl
->urbs
.lock
);
183 node
= udl
->urbs
.list
.next
; /* have reserved one with sem */
186 spin_unlock_irq(&udl
->urbs
.lock
);
188 unode
= list_entry(node
, struct urb_node
, entry
);
191 /* Free each separately allocated piece */
192 usb_free_coherent(urb
->dev
, udl
->urbs
.size
,
193 urb
->transfer_buffer
, urb
->transfer_dma
);
200 static int udl_alloc_urb_list(struct drm_device
*dev
, int count
, size_t size
)
202 struct udl_device
*udl
= dev
->dev_private
;
204 struct urb_node
*unode
;
206 size_t wanted_size
= count
* size
;
208 spin_lock_init(&udl
->urbs
.lock
);
211 udl
->urbs
.size
= size
;
212 INIT_LIST_HEAD(&udl
->urbs
.list
);
214 sema_init(&udl
->urbs
.limit_sem
, 0);
216 udl
->urbs
.available
= 0;
218 while (udl
->urbs
.count
* size
< wanted_size
) {
219 unode
= kzalloc(sizeof(struct urb_node
), GFP_KERNEL
);
224 INIT_DELAYED_WORK(&unode
->release_urb_work
,
225 udl_release_urb_work
);
227 urb
= usb_alloc_urb(0, GFP_KERNEL
);
234 buf
= usb_alloc_coherent(udl
->udev
, size
, GFP_KERNEL
,
239 if (size
> PAGE_SIZE
) {
241 udl_free_urb_list(dev
);
247 /* urb->transfer_buffer_length set to actual before submit */
248 usb_fill_bulk_urb(urb
, udl
->udev
, usb_sndbulkpipe(udl
->udev
, 1),
249 buf
, size
, udl_urb_completion
, unode
);
250 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
252 list_add_tail(&unode
->entry
, &udl
->urbs
.list
);
254 up(&udl
->urbs
.limit_sem
);
256 udl
->urbs
.available
++;
259 DRM_DEBUG("allocated %d %d byte urbs\n", udl
->urbs
.count
, (int) size
);
261 return udl
->urbs
.count
;
264 struct urb
*udl_get_urb(struct drm_device
*dev
)
266 struct udl_device
*udl
= dev
->dev_private
;
268 struct list_head
*entry
;
269 struct urb_node
*unode
;
270 struct urb
*urb
= NULL
;
272 /* Wait for an in-flight buffer to complete and get re-queued */
273 ret
= down_timeout(&udl
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
275 atomic_set(&udl
->lost_pixels
, 1);
276 DRM_INFO("wait for urb interrupted: %x available: %d\n",
277 ret
, udl
->urbs
.available
);
281 spin_lock_irq(&udl
->urbs
.lock
);
283 BUG_ON(list_empty(&udl
->urbs
.list
)); /* reserved one with limit_sem */
284 entry
= udl
->urbs
.list
.next
;
285 list_del_init(entry
);
286 udl
->urbs
.available
--;
288 spin_unlock_irq(&udl
->urbs
.lock
);
290 unode
= list_entry(entry
, struct urb_node
, entry
);
297 int udl_submit_urb(struct drm_device
*dev
, struct urb
*urb
, size_t len
)
299 struct udl_device
*udl
= dev
->dev_private
;
302 BUG_ON(len
> udl
->urbs
.size
);
304 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
305 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
307 udl_urb_completion(urb
); /* because no one else will */
308 atomic_set(&udl
->lost_pixels
, 1);
309 DRM_ERROR("usb_submit_urb error %x\n", ret
);
314 int udl_driver_load(struct drm_device
*dev
, unsigned long flags
)
316 struct usb_device
*udev
= (void*)flags
;
317 struct udl_device
*udl
;
321 udl
= kzalloc(sizeof(struct udl_device
), GFP_KERNEL
);
327 dev
->dev_private
= udl
;
329 mutex_init(&udl
->gem_lock
);
331 if (!udl_parse_vendor_descriptor(dev
, udl
->udev
)) {
333 DRM_ERROR("firmware not recognized. Assume incompatible device\n");
337 if (udl_select_std_channel(udl
))
338 DRM_ERROR("Selecting channel failed\n");
340 if (!udl_alloc_urb_list(dev
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
341 DRM_ERROR("udl_alloc_urb_list failed\n");
346 ret
= udl_modeset_init(dev
);
350 ret
= udl_fbdev_init(dev
);
354 drm_kms_helper_poll_init(dev
);
360 udl_free_urb_list(dev
);
362 DRM_ERROR("%d\n", ret
);
366 int udl_drop_usb(struct drm_device
*dev
)
368 udl_free_urb_list(dev
);
372 void udl_driver_unload(struct drm_device
*dev
)
374 struct udl_device
*udl
= dev
->dev_private
;
376 drm_kms_helper_poll_fini(dev
);
379 udl_free_urb_list(dev
);
381 udl_fbdev_cleanup(dev
);
385 void udl_driver_release(struct drm_device
*dev
)
387 udl_modeset_cleanup(dev
);