112eb90f1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 25320918bSDave Airlie /* 35320918bSDave Airlie * Copyright (C) 2012 Red Hat 45320918bSDave Airlie * 55320918bSDave Airlie * based in parts on udlfb.c: 65320918bSDave Airlie * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it> 75320918bSDave Airlie * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com> 85320918bSDave Airlie * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com> 95320918bSDave Airlie */ 10a9dcf380SSam Ravnborg 11a9dcf380SSam Ravnborg #include <drm/drm.h> 12a9dcf380SSam Ravnborg #include <drm/drm_print.h> 13fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h> 14a9dcf380SSam Ravnborg 155320918bSDave Airlie #include "udl_drv.h" 165320918bSDave Airlie 175320918bSDave Airlie /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */ 185320918bSDave Airlie #define BULK_SIZE 512 195320918bSDave Airlie 20d1c151dcSJamie Lentin #define NR_USB_REQUEST_CHANNEL 0x12 21d1c151dcSJamie Lentin 225320918bSDave Airlie #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE) 235320918bSDave Airlie #define WRITES_IN_FLIGHT (4) 245320918bSDave Airlie #define MAX_VENDOR_DESCRIPTOR_SIZE 256 255320918bSDave Airlie 265320918bSDave Airlie #define GET_URB_TIMEOUT HZ 275320918bSDave Airlie #define FREE_URB_TIMEOUT (HZ*2) 285320918bSDave Airlie 29*3fb91f56SThomas Zimmermann static int udl_parse_vendor_descriptor(struct udl_device *udl) 305320918bSDave Airlie { 31*3fb91f56SThomas Zimmermann struct usb_device *udev = udl_to_usb_device(udl); 325320918bSDave Airlie char *desc; 335320918bSDave Airlie char *buf; 345320918bSDave Airlie char *desc_end; 355320918bSDave Airlie 365320918bSDave Airlie u8 total_len = 0; 375320918bSDave Airlie 385320918bSDave Airlie buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL); 395320918bSDave Airlie if (!buf) 405320918bSDave Airlie return false; 415320918bSDave Airlie desc = buf; 425320918bSDave Airlie 43*3fb91f56SThomas Zimmermann total_len = usb_get_descriptor(udev, 0x5f, /* vendor specific */ 445320918bSDave Airlie 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE); 455320918bSDave Airlie if (total_len > 5) { 4608fcd72bSAndy Shevchenko DRM_INFO("vendor descriptor length:%x data:%11ph\n", 4708fcd72bSAndy Shevchenko total_len, desc); 485320918bSDave Airlie 495320918bSDave Airlie if ((desc[0] != total_len) || /* descriptor length */ 505320918bSDave Airlie (desc[1] != 0x5f) || /* vendor descriptor type */ 515320918bSDave Airlie (desc[2] != 0x01) || /* version (2 bytes) */ 525320918bSDave Airlie (desc[3] != 0x00) || 535320918bSDave Airlie (desc[4] != total_len - 2)) /* length after type */ 545320918bSDave Airlie goto unrecognized; 555320918bSDave Airlie 565320918bSDave Airlie desc_end = desc + total_len; 575320918bSDave Airlie desc += 5; /* the fixed header we've already parsed */ 585320918bSDave Airlie 595320918bSDave Airlie while (desc < desc_end) { 605320918bSDave Airlie u8 length; 615320918bSDave Airlie u16 key; 625320918bSDave Airlie 63d42f0349SDave Airlie key = le16_to_cpu(*((u16 *) desc)); 645320918bSDave Airlie desc += sizeof(u16); 655320918bSDave Airlie length = *desc; 665320918bSDave Airlie desc++; 675320918bSDave Airlie 685320918bSDave Airlie switch (key) { 695320918bSDave Airlie case 0x0200: { /* max_area */ 705320918bSDave Airlie u32 max_area; 715320918bSDave Airlie max_area = le32_to_cpu(*((u32 *)desc)); 725320918bSDave Airlie DRM_DEBUG("DL chip limited to %d pixel modes\n", 735320918bSDave Airlie max_area); 745320918bSDave Airlie udl->sku_pixel_limit = max_area; 755320918bSDave Airlie break; 765320918bSDave Airlie } 775320918bSDave Airlie default: 785320918bSDave Airlie break; 795320918bSDave Airlie } 805320918bSDave Airlie desc += length; 815320918bSDave Airlie } 825320918bSDave Airlie } 835320918bSDave Airlie 845320918bSDave Airlie goto success; 855320918bSDave Airlie 865320918bSDave Airlie unrecognized: 875320918bSDave Airlie /* allow udlfb to load for now even if firmware unrecognized */ 885320918bSDave Airlie DRM_ERROR("Unrecognized vendor firmware descriptor\n"); 895320918bSDave Airlie 905320918bSDave Airlie success: 915320918bSDave Airlie kfree(buf); 925320918bSDave Airlie return true; 935320918bSDave Airlie } 945320918bSDave Airlie 95d1c151dcSJamie Lentin /* 96d1c151dcSJamie Lentin * Need to ensure a channel is selected before submitting URBs 97d1c151dcSJamie Lentin */ 98d1c151dcSJamie Lentin static int udl_select_std_channel(struct udl_device *udl) 99d1c151dcSJamie Lentin { 100e5581fe2SDave Airlie static const u8 set_def_chn[] = {0x57, 0xCD, 0xDC, 0xA7, 101d1c151dcSJamie Lentin 0x1C, 0x88, 0x5E, 0x15, 102d1c151dcSJamie Lentin 0x60, 0xFE, 0xC6, 0x97, 103d1c151dcSJamie Lentin 0x16, 0x3D, 0x47, 0xF2}; 104*3fb91f56SThomas Zimmermann 105e5581fe2SDave Airlie void *sendbuf; 106*3fb91f56SThomas Zimmermann int ret; 107*3fb91f56SThomas Zimmermann struct usb_device *udev = udl_to_usb_device(udl); 108e5581fe2SDave Airlie 109e5581fe2SDave Airlie sendbuf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL); 110e5581fe2SDave Airlie if (!sendbuf) 111e5581fe2SDave Airlie return -ENOMEM; 112d1c151dcSJamie Lentin 113*3fb91f56SThomas Zimmermann ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 114d1c151dcSJamie Lentin NR_USB_REQUEST_CHANNEL, 115d1c151dcSJamie Lentin (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0, 116e5581fe2SDave Airlie sendbuf, sizeof(set_def_chn), 117d1c151dcSJamie Lentin USB_CTRL_SET_TIMEOUT); 118e5581fe2SDave Airlie kfree(sendbuf); 119d1c151dcSJamie Lentin return ret < 0 ? ret : 0; 120d1c151dcSJamie Lentin } 121d1c151dcSJamie Lentin 1225320918bSDave Airlie static void udl_release_urb_work(struct work_struct *work) 1235320918bSDave Airlie { 1245320918bSDave Airlie struct urb_node *unode = container_of(work, struct urb_node, 1255320918bSDave Airlie release_urb_work.work); 1265320918bSDave Airlie 1275320918bSDave Airlie up(&unode->dev->urbs.limit_sem); 1285320918bSDave Airlie } 1295320918bSDave Airlie 1305320918bSDave Airlie void udl_urb_completion(struct urb *urb) 1315320918bSDave Airlie { 1325320918bSDave Airlie struct urb_node *unode = urb->context; 1335320918bSDave Airlie struct udl_device *udl = unode->dev; 1345320918bSDave Airlie unsigned long flags; 1355320918bSDave Airlie 1365320918bSDave Airlie /* sync/async unlink faults aren't errors */ 1375320918bSDave Airlie if (urb->status) { 1385320918bSDave Airlie if (!(urb->status == -ENOENT || 1395320918bSDave Airlie urb->status == -ECONNRESET || 1405320918bSDave Airlie urb->status == -ESHUTDOWN)) { 1415320918bSDave Airlie DRM_ERROR("%s - nonzero write bulk status received: %d\n", 1425320918bSDave Airlie __func__, urb->status); 1435320918bSDave Airlie } 1445320918bSDave Airlie } 1455320918bSDave Airlie 1465320918bSDave Airlie urb->transfer_buffer_length = udl->urbs.size; /* reset to actual */ 1475320918bSDave Airlie 1485320918bSDave Airlie spin_lock_irqsave(&udl->urbs.lock, flags); 1495320918bSDave Airlie list_add_tail(&unode->entry, &udl->urbs.list); 1505320918bSDave Airlie udl->urbs.available++; 1515320918bSDave Airlie spin_unlock_irqrestore(&udl->urbs.lock, flags); 1525320918bSDave Airlie 1535320918bSDave Airlie #if 0 1545320918bSDave Airlie /* 1555320918bSDave Airlie * When using fb_defio, we deadlock if up() is called 1565320918bSDave Airlie * while another is waiting. So queue to another process. 1575320918bSDave Airlie */ 1585320918bSDave Airlie if (fb_defio) 1595320918bSDave Airlie schedule_delayed_work(&unode->release_urb_work, 0); 1605320918bSDave Airlie else 1615320918bSDave Airlie #endif 1625320918bSDave Airlie up(&udl->urbs.limit_sem); 1635320918bSDave Airlie } 1645320918bSDave Airlie 1655320918bSDave Airlie static void udl_free_urb_list(struct drm_device *dev) 1665320918bSDave Airlie { 167fd96e0dbSDave Airlie struct udl_device *udl = to_udl(dev); 1685320918bSDave Airlie int count = udl->urbs.count; 1695320918bSDave Airlie struct list_head *node; 1705320918bSDave Airlie struct urb_node *unode; 1715320918bSDave Airlie struct urb *urb; 1725320918bSDave Airlie 1735320918bSDave Airlie DRM_DEBUG("Waiting for completes and freeing all render urbs\n"); 1745320918bSDave Airlie 1755320918bSDave Airlie /* keep waiting and freeing, until we've got 'em all */ 1765320918bSDave Airlie while (count--) { 1778456b99cSMikulas Patocka down(&udl->urbs.limit_sem); 1785320918bSDave Airlie 179c2f53119SMikulas Patocka spin_lock_irq(&udl->urbs.lock); 1805320918bSDave Airlie 1815320918bSDave Airlie node = udl->urbs.list.next; /* have reserved one with sem */ 1825320918bSDave Airlie list_del_init(node); 1835320918bSDave Airlie 184c2f53119SMikulas Patocka spin_unlock_irq(&udl->urbs.lock); 1855320918bSDave Airlie 1865320918bSDave Airlie unode = list_entry(node, struct urb_node, entry); 1875320918bSDave Airlie urb = unode->urb; 1885320918bSDave Airlie 1895320918bSDave Airlie /* Free each separately allocated piece */ 1905320918bSDave Airlie usb_free_coherent(urb->dev, udl->urbs.size, 1915320918bSDave Airlie urb->transfer_buffer, urb->transfer_dma); 1925320918bSDave Airlie usb_free_urb(urb); 1935320918bSDave Airlie kfree(node); 1945320918bSDave Airlie } 1955320918bSDave Airlie udl->urbs.count = 0; 1965320918bSDave Airlie } 1975320918bSDave Airlie 1985320918bSDave Airlie static int udl_alloc_urb_list(struct drm_device *dev, int count, size_t size) 1995320918bSDave Airlie { 200fd96e0dbSDave Airlie struct udl_device *udl = to_udl(dev); 2015320918bSDave Airlie struct urb *urb; 2025320918bSDave Airlie struct urb_node *unode; 2035320918bSDave Airlie char *buf; 204542bb978SMikulas Patocka size_t wanted_size = count * size; 205*3fb91f56SThomas Zimmermann struct usb_device *udev = udl_to_usb_device(udl); 2065320918bSDave Airlie 2075320918bSDave Airlie spin_lock_init(&udl->urbs.lock); 2085320918bSDave Airlie 209542bb978SMikulas Patocka retry: 2105320918bSDave Airlie udl->urbs.size = size; 2115320918bSDave Airlie INIT_LIST_HEAD(&udl->urbs.list); 2125320918bSDave Airlie 213542bb978SMikulas Patocka sema_init(&udl->urbs.limit_sem, 0); 214542bb978SMikulas Patocka udl->urbs.count = 0; 215542bb978SMikulas Patocka udl->urbs.available = 0; 216542bb978SMikulas Patocka 217542bb978SMikulas Patocka while (udl->urbs.count * size < wanted_size) { 2185320918bSDave Airlie unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL); 2195320918bSDave Airlie if (!unode) 2205320918bSDave Airlie break; 2215320918bSDave Airlie unode->dev = udl; 2225320918bSDave Airlie 2235320918bSDave Airlie INIT_DELAYED_WORK(&unode->release_urb_work, 2245320918bSDave Airlie udl_release_urb_work); 2255320918bSDave Airlie 2265320918bSDave Airlie urb = usb_alloc_urb(0, GFP_KERNEL); 2275320918bSDave Airlie if (!urb) { 2285320918bSDave Airlie kfree(unode); 2295320918bSDave Airlie break; 2305320918bSDave Airlie } 2315320918bSDave Airlie unode->urb = urb; 2325320918bSDave Airlie 233*3fb91f56SThomas Zimmermann buf = usb_alloc_coherent(udev, size, GFP_KERNEL, 2345320918bSDave Airlie &urb->transfer_dma); 2355320918bSDave Airlie if (!buf) { 2365320918bSDave Airlie kfree(unode); 2375320918bSDave Airlie usb_free_urb(urb); 238542bb978SMikulas Patocka if (size > PAGE_SIZE) { 239542bb978SMikulas Patocka size /= 2; 240542bb978SMikulas Patocka udl_free_urb_list(dev); 241542bb978SMikulas Patocka goto retry; 242542bb978SMikulas Patocka } 2435320918bSDave Airlie break; 2445320918bSDave Airlie } 2455320918bSDave Airlie 2465320918bSDave Airlie /* urb->transfer_buffer_length set to actual before submit */ 247*3fb91f56SThomas Zimmermann usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, 1), 2485320918bSDave Airlie buf, size, udl_urb_completion, unode); 2495320918bSDave Airlie urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 2505320918bSDave Airlie 2515320918bSDave Airlie list_add_tail(&unode->entry, &udl->urbs.list); 2525320918bSDave Airlie 253542bb978SMikulas Patocka up(&udl->urbs.limit_sem); 254542bb978SMikulas Patocka udl->urbs.count++; 255542bb978SMikulas Patocka udl->urbs.available++; 2565320918bSDave Airlie } 2575320918bSDave Airlie 258542bb978SMikulas Patocka DRM_DEBUG("allocated %d %d byte urbs\n", udl->urbs.count, (int) size); 2595320918bSDave Airlie 260542bb978SMikulas Patocka return udl->urbs.count; 2615320918bSDave Airlie } 2625320918bSDave Airlie 2635320918bSDave Airlie struct urb *udl_get_urb(struct drm_device *dev) 2645320918bSDave Airlie { 265fd96e0dbSDave Airlie struct udl_device *udl = to_udl(dev); 2665320918bSDave Airlie int ret = 0; 2675320918bSDave Airlie struct list_head *entry; 2685320918bSDave Airlie struct urb_node *unode; 2695320918bSDave Airlie struct urb *urb = NULL; 2705320918bSDave Airlie 2715320918bSDave Airlie /* Wait for an in-flight buffer to complete and get re-queued */ 2725320918bSDave Airlie ret = down_timeout(&udl->urbs.limit_sem, GET_URB_TIMEOUT); 2735320918bSDave Airlie if (ret) { 2745320918bSDave Airlie DRM_INFO("wait for urb interrupted: %x available: %d\n", 2755320918bSDave Airlie ret, udl->urbs.available); 2765320918bSDave Airlie goto error; 2775320918bSDave Airlie } 2785320918bSDave Airlie 279c2f53119SMikulas Patocka spin_lock_irq(&udl->urbs.lock); 2805320918bSDave Airlie 2815320918bSDave Airlie BUG_ON(list_empty(&udl->urbs.list)); /* reserved one with limit_sem */ 2825320918bSDave Airlie entry = udl->urbs.list.next; 2835320918bSDave Airlie list_del_init(entry); 2845320918bSDave Airlie udl->urbs.available--; 2855320918bSDave Airlie 286c2f53119SMikulas Patocka spin_unlock_irq(&udl->urbs.lock); 2875320918bSDave Airlie 2885320918bSDave Airlie unode = list_entry(entry, struct urb_node, entry); 2895320918bSDave Airlie urb = unode->urb; 2905320918bSDave Airlie 2915320918bSDave Airlie error: 2925320918bSDave Airlie return urb; 2935320918bSDave Airlie } 2945320918bSDave Airlie 2955320918bSDave Airlie int udl_submit_urb(struct drm_device *dev, struct urb *urb, size_t len) 2965320918bSDave Airlie { 297fd96e0dbSDave Airlie struct udl_device *udl = to_udl(dev); 2985320918bSDave Airlie int ret; 2995320918bSDave Airlie 3005320918bSDave Airlie BUG_ON(len > udl->urbs.size); 3015320918bSDave Airlie 3025320918bSDave Airlie urb->transfer_buffer_length = len; /* set to actual payload len */ 3035320918bSDave Airlie ret = usb_submit_urb(urb, GFP_ATOMIC); 3045320918bSDave Airlie if (ret) { 3055320918bSDave Airlie udl_urb_completion(urb); /* because no one else will */ 3065320918bSDave Airlie DRM_ERROR("usb_submit_urb error %x\n", ret); 3075320918bSDave Airlie } 3085320918bSDave Airlie return ret; 3095320918bSDave Airlie } 3105320918bSDave Airlie 3116ecac85eSDave Airlie int udl_init(struct udl_device *udl) 3125320918bSDave Airlie { 3136ecac85eSDave Airlie struct drm_device *dev = &udl->drm; 314737583f0SOliver Neukum int ret = -ENOMEM; 3155320918bSDave Airlie 3165320918bSDave Airlie DRM_DEBUG("\n"); 3175320918bSDave Airlie 318ae358dacSDaniel Vetter mutex_init(&udl->gem_lock); 319ae358dacSDaniel Vetter 320*3fb91f56SThomas Zimmermann if (!udl_parse_vendor_descriptor(udl)) { 321e39a52daSDaniel Vetter ret = -ENODEV; 3225320918bSDave Airlie DRM_ERROR("firmware not recognized. Assume incompatible device\n"); 3235320918bSDave Airlie goto err; 3245320918bSDave Airlie } 3255320918bSDave Airlie 326d1c151dcSJamie Lentin if (udl_select_std_channel(udl)) 327d1c151dcSJamie Lentin DRM_ERROR("Selecting channel failed\n"); 328d1c151dcSJamie Lentin 3295320918bSDave Airlie if (!udl_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) { 3305320918bSDave Airlie DRM_ERROR("udl_alloc_urb_list failed\n"); 3315320918bSDave Airlie goto err; 3325320918bSDave Airlie } 3335320918bSDave Airlie 3345320918bSDave Airlie DRM_DEBUG("\n"); 3355320918bSDave Airlie ret = udl_modeset_init(dev); 33626507b06SStéphane Marchesin if (ret) 33726507b06SStéphane Marchesin goto err; 3385320918bSDave Airlie 339afdfc4c6SRobert Tarasov drm_kms_helper_poll_init(dev); 340afdfc4c6SRobert Tarasov 3415320918bSDave Airlie return 0; 34232e932e3SEugeniy Paltsev 3435320918bSDave Airlie err: 34426507b06SStéphane Marchesin if (udl->urbs.count) 34526507b06SStéphane Marchesin udl_free_urb_list(dev); 3465320918bSDave Airlie DRM_ERROR("%d\n", ret); 3475320918bSDave Airlie return ret; 3485320918bSDave Airlie } 3495320918bSDave Airlie 3505320918bSDave Airlie int udl_drop_usb(struct drm_device *dev) 3515320918bSDave Airlie { 3525320918bSDave Airlie udl_free_urb_list(dev); 3535320918bSDave Airlie return 0; 3545320918bSDave Airlie } 355