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