15320918bSDave Airlie /* 25320918bSDave Airlie * Copyright (C) 2012 Red Hat 35320918bSDave Airlie * 45320918bSDave Airlie * based in parts on udlfb.c: 55320918bSDave Airlie * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it> 65320918bSDave Airlie * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com> 75320918bSDave Airlie * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com> 85320918bSDave Airlie * 95320918bSDave Airlie * This file is subject to the terms and conditions of the GNU General Public 105320918bSDave Airlie * License v2. See the file COPYING in the main directory of this archive for 115320918bSDave Airlie * more details. 125320918bSDave Airlie */ 13760285e7SDavid Howells #include <drm/drmP.h> 14afdfc4c6SRobert Tarasov #include <drm/drm_crtc_helper.h> 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 295320918bSDave Airlie static int udl_parse_vendor_descriptor(struct drm_device *dev, 305320918bSDave Airlie struct usb_device *usbdev) 315320918bSDave Airlie { 325320918bSDave Airlie struct udl_device *udl = dev->dev_private; 335320918bSDave Airlie char *desc; 345320918bSDave Airlie char *buf; 355320918bSDave Airlie char *desc_end; 365320918bSDave Airlie 375320918bSDave Airlie u8 total_len = 0; 385320918bSDave Airlie 395320918bSDave Airlie buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL); 405320918bSDave Airlie if (!buf) 415320918bSDave Airlie return false; 425320918bSDave Airlie desc = buf; 435320918bSDave Airlie 445320918bSDave Airlie total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */ 455320918bSDave Airlie 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE); 465320918bSDave Airlie if (total_len > 5) { 4708fcd72bSAndy Shevchenko DRM_INFO("vendor descriptor length:%x data:%11ph\n", 4808fcd72bSAndy Shevchenko total_len, desc); 495320918bSDave Airlie 505320918bSDave Airlie if ((desc[0] != total_len) || /* descriptor length */ 515320918bSDave Airlie (desc[1] != 0x5f) || /* vendor descriptor type */ 525320918bSDave Airlie (desc[2] != 0x01) || /* version (2 bytes) */ 535320918bSDave Airlie (desc[3] != 0x00) || 545320918bSDave Airlie (desc[4] != total_len - 2)) /* length after type */ 555320918bSDave Airlie goto unrecognized; 565320918bSDave Airlie 575320918bSDave Airlie desc_end = desc + total_len; 585320918bSDave Airlie desc += 5; /* the fixed header we've already parsed */ 595320918bSDave Airlie 605320918bSDave Airlie while (desc < desc_end) { 615320918bSDave Airlie u8 length; 625320918bSDave Airlie u16 key; 635320918bSDave Airlie 64d42f0349SDave Airlie key = le16_to_cpu(*((u16 *) desc)); 655320918bSDave Airlie desc += sizeof(u16); 665320918bSDave Airlie length = *desc; 675320918bSDave Airlie desc++; 685320918bSDave Airlie 695320918bSDave Airlie switch (key) { 705320918bSDave Airlie case 0x0200: { /* max_area */ 715320918bSDave Airlie u32 max_area; 725320918bSDave Airlie max_area = le32_to_cpu(*((u32 *)desc)); 735320918bSDave Airlie DRM_DEBUG("DL chip limited to %d pixel modes\n", 745320918bSDave Airlie max_area); 755320918bSDave Airlie udl->sku_pixel_limit = max_area; 765320918bSDave Airlie break; 775320918bSDave Airlie } 785320918bSDave Airlie default: 795320918bSDave Airlie break; 805320918bSDave Airlie } 815320918bSDave Airlie desc += length; 825320918bSDave Airlie } 835320918bSDave Airlie } 845320918bSDave Airlie 855320918bSDave Airlie goto success; 865320918bSDave Airlie 875320918bSDave Airlie unrecognized: 885320918bSDave Airlie /* allow udlfb to load for now even if firmware unrecognized */ 895320918bSDave Airlie DRM_ERROR("Unrecognized vendor firmware descriptor\n"); 905320918bSDave Airlie 915320918bSDave Airlie success: 925320918bSDave Airlie kfree(buf); 935320918bSDave Airlie return true; 945320918bSDave Airlie } 955320918bSDave Airlie 96d1c151dcSJamie Lentin /* 97d1c151dcSJamie Lentin * Need to ensure a channel is selected before submitting URBs 98d1c151dcSJamie Lentin */ 99d1c151dcSJamie Lentin static int udl_select_std_channel(struct udl_device *udl) 100d1c151dcSJamie Lentin { 101d1c151dcSJamie Lentin int ret; 102e5581fe2SDave Airlie static const u8 set_def_chn[] = {0x57, 0xCD, 0xDC, 0xA7, 103d1c151dcSJamie Lentin 0x1C, 0x88, 0x5E, 0x15, 104d1c151dcSJamie Lentin 0x60, 0xFE, 0xC6, 0x97, 105d1c151dcSJamie Lentin 0x16, 0x3D, 0x47, 0xF2}; 106e5581fe2SDave Airlie void *sendbuf; 107e5581fe2SDave Airlie 108e5581fe2SDave Airlie sendbuf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL); 109e5581fe2SDave Airlie if (!sendbuf) 110e5581fe2SDave Airlie return -ENOMEM; 111d1c151dcSJamie Lentin 112d1c151dcSJamie Lentin ret = usb_control_msg(udl->udev, 113d1c151dcSJamie Lentin usb_sndctrlpipe(udl->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 atomic_set(&udl->lost_pixels, 1); 1445320918bSDave Airlie } 1455320918bSDave Airlie } 1465320918bSDave Airlie 1475320918bSDave Airlie urb->transfer_buffer_length = udl->urbs.size; /* reset to actual */ 1485320918bSDave Airlie 1495320918bSDave Airlie spin_lock_irqsave(&udl->urbs.lock, flags); 1505320918bSDave Airlie list_add_tail(&unode->entry, &udl->urbs.list); 1515320918bSDave Airlie udl->urbs.available++; 1525320918bSDave Airlie spin_unlock_irqrestore(&udl->urbs.lock, flags); 1535320918bSDave Airlie 1545320918bSDave Airlie #if 0 1555320918bSDave Airlie /* 1565320918bSDave Airlie * When using fb_defio, we deadlock if up() is called 1575320918bSDave Airlie * while another is waiting. So queue to another process. 1585320918bSDave Airlie */ 1595320918bSDave Airlie if (fb_defio) 1605320918bSDave Airlie schedule_delayed_work(&unode->release_urb_work, 0); 1615320918bSDave Airlie else 1625320918bSDave Airlie #endif 1635320918bSDave Airlie up(&udl->urbs.limit_sem); 1645320918bSDave Airlie } 1655320918bSDave Airlie 1665320918bSDave Airlie static void udl_free_urb_list(struct drm_device *dev) 1675320918bSDave Airlie { 1685320918bSDave Airlie struct udl_device *udl = dev->dev_private; 1695320918bSDave Airlie int count = udl->urbs.count; 1705320918bSDave Airlie struct list_head *node; 1715320918bSDave Airlie struct urb_node *unode; 1725320918bSDave Airlie struct urb *urb; 1735320918bSDave Airlie int ret; 1745320918bSDave Airlie unsigned long flags; 1755320918bSDave Airlie 1765320918bSDave Airlie DRM_DEBUG("Waiting for completes and freeing all render urbs\n"); 1775320918bSDave Airlie 1785320918bSDave Airlie /* keep waiting and freeing, until we've got 'em all */ 1795320918bSDave Airlie while (count--) { 1805320918bSDave Airlie 1815320918bSDave Airlie /* Getting interrupted means a leak, but ok at shutdown*/ 1825320918bSDave Airlie ret = down_interruptible(&udl->urbs.limit_sem); 1835320918bSDave Airlie if (ret) 1845320918bSDave Airlie break; 1855320918bSDave Airlie 1865320918bSDave Airlie spin_lock_irqsave(&udl->urbs.lock, flags); 1875320918bSDave Airlie 1885320918bSDave Airlie node = udl->urbs.list.next; /* have reserved one with sem */ 1895320918bSDave Airlie list_del_init(node); 1905320918bSDave Airlie 1915320918bSDave Airlie spin_unlock_irqrestore(&udl->urbs.lock, flags); 1925320918bSDave Airlie 1935320918bSDave Airlie unode = list_entry(node, struct urb_node, entry); 1945320918bSDave Airlie urb = unode->urb; 1955320918bSDave Airlie 1965320918bSDave Airlie /* Free each separately allocated piece */ 1975320918bSDave Airlie usb_free_coherent(urb->dev, udl->urbs.size, 1985320918bSDave Airlie urb->transfer_buffer, urb->transfer_dma); 1995320918bSDave Airlie usb_free_urb(urb); 2005320918bSDave Airlie kfree(node); 2015320918bSDave Airlie } 2025320918bSDave Airlie udl->urbs.count = 0; 2035320918bSDave Airlie } 2045320918bSDave Airlie 2055320918bSDave Airlie static int udl_alloc_urb_list(struct drm_device *dev, int count, size_t size) 2065320918bSDave Airlie { 2075320918bSDave Airlie struct udl_device *udl = dev->dev_private; 2085320918bSDave Airlie int i = 0; 2095320918bSDave Airlie struct urb *urb; 2105320918bSDave Airlie struct urb_node *unode; 2115320918bSDave Airlie char *buf; 2125320918bSDave Airlie 2135320918bSDave Airlie spin_lock_init(&udl->urbs.lock); 2145320918bSDave Airlie 2155320918bSDave Airlie udl->urbs.size = size; 2165320918bSDave Airlie INIT_LIST_HEAD(&udl->urbs.list); 2175320918bSDave Airlie 2185320918bSDave Airlie while (i < count) { 2195320918bSDave Airlie unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL); 2205320918bSDave Airlie if (!unode) 2215320918bSDave Airlie break; 2225320918bSDave Airlie unode->dev = udl; 2235320918bSDave Airlie 2245320918bSDave Airlie INIT_DELAYED_WORK(&unode->release_urb_work, 2255320918bSDave Airlie udl_release_urb_work); 2265320918bSDave Airlie 2275320918bSDave Airlie urb = usb_alloc_urb(0, GFP_KERNEL); 2285320918bSDave Airlie if (!urb) { 2295320918bSDave Airlie kfree(unode); 2305320918bSDave Airlie break; 2315320918bSDave Airlie } 2325320918bSDave Airlie unode->urb = urb; 2335320918bSDave Airlie 234d4f68a75SDavid Herrmann buf = usb_alloc_coherent(udl->udev, MAX_TRANSFER, GFP_KERNEL, 2355320918bSDave Airlie &urb->transfer_dma); 2365320918bSDave Airlie if (!buf) { 2375320918bSDave Airlie kfree(unode); 2385320918bSDave Airlie usb_free_urb(urb); 2395320918bSDave Airlie break; 2405320918bSDave Airlie } 2415320918bSDave Airlie 2425320918bSDave Airlie /* urb->transfer_buffer_length set to actual before submit */ 243d4f68a75SDavid Herrmann usb_fill_bulk_urb(urb, udl->udev, usb_sndbulkpipe(udl->udev, 1), 2445320918bSDave Airlie buf, size, udl_urb_completion, unode); 2455320918bSDave Airlie urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 2465320918bSDave Airlie 2475320918bSDave Airlie list_add_tail(&unode->entry, &udl->urbs.list); 2485320918bSDave Airlie 2495320918bSDave Airlie i++; 2505320918bSDave Airlie } 2515320918bSDave Airlie 2525320918bSDave Airlie sema_init(&udl->urbs.limit_sem, i); 2535320918bSDave Airlie udl->urbs.count = i; 2545320918bSDave Airlie udl->urbs.available = i; 2555320918bSDave Airlie 2565320918bSDave Airlie DRM_DEBUG("allocated %d %d byte urbs\n", i, (int) size); 2575320918bSDave Airlie 2585320918bSDave Airlie return i; 2595320918bSDave Airlie } 2605320918bSDave Airlie 2615320918bSDave Airlie struct urb *udl_get_urb(struct drm_device *dev) 2625320918bSDave Airlie { 2635320918bSDave Airlie struct udl_device *udl = dev->dev_private; 2645320918bSDave Airlie int ret = 0; 2655320918bSDave Airlie struct list_head *entry; 2665320918bSDave Airlie struct urb_node *unode; 2675320918bSDave Airlie struct urb *urb = NULL; 2685320918bSDave Airlie unsigned long flags; 2695320918bSDave Airlie 2705320918bSDave Airlie /* Wait for an in-flight buffer to complete and get re-queued */ 2715320918bSDave Airlie ret = down_timeout(&udl->urbs.limit_sem, GET_URB_TIMEOUT); 2725320918bSDave Airlie if (ret) { 2735320918bSDave Airlie atomic_set(&udl->lost_pixels, 1); 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 2795320918bSDave Airlie spin_lock_irqsave(&udl->urbs.lock, flags); 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 2865320918bSDave Airlie spin_unlock_irqrestore(&udl->urbs.lock, flags); 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 { 2975320918bSDave Airlie struct udl_device *udl = dev->dev_private; 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 atomic_set(&udl->lost_pixels, 1); 3075320918bSDave Airlie DRM_ERROR("usb_submit_urb error %x\n", ret); 3085320918bSDave Airlie } 3095320918bSDave Airlie return ret; 3105320918bSDave Airlie } 3115320918bSDave Airlie 3125320918bSDave Airlie int udl_driver_load(struct drm_device *dev, unsigned long flags) 3135320918bSDave Airlie { 314d4f68a75SDavid Herrmann struct usb_device *udev = (void*)flags; 3155320918bSDave Airlie struct udl_device *udl; 316737583f0SOliver Neukum int ret = -ENOMEM; 3175320918bSDave Airlie 3185320918bSDave Airlie DRM_DEBUG("\n"); 3195320918bSDave Airlie udl = kzalloc(sizeof(struct udl_device), GFP_KERNEL); 3205320918bSDave Airlie if (!udl) 3215320918bSDave Airlie return -ENOMEM; 3225320918bSDave Airlie 323d4f68a75SDavid Herrmann udl->udev = udev; 3245320918bSDave Airlie udl->ddev = dev; 3255320918bSDave Airlie dev->dev_private = udl; 3265320918bSDave Airlie 327*ae358dacSDaniel Vetter mutex_init(&udl->gem_lock); 328*ae358dacSDaniel Vetter 329d4f68a75SDavid Herrmann if (!udl_parse_vendor_descriptor(dev, udl->udev)) { 330e39a52daSDaniel Vetter ret = -ENODEV; 3315320918bSDave Airlie DRM_ERROR("firmware not recognized. Assume incompatible device\n"); 3325320918bSDave Airlie goto err; 3335320918bSDave Airlie } 3345320918bSDave Airlie 335d1c151dcSJamie Lentin if (udl_select_std_channel(udl)) 336d1c151dcSJamie Lentin DRM_ERROR("Selecting channel failed\n"); 337d1c151dcSJamie Lentin 3385320918bSDave Airlie if (!udl_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) { 3395320918bSDave Airlie DRM_ERROR("udl_alloc_urb_list failed\n"); 3405320918bSDave Airlie goto err; 3415320918bSDave Airlie } 3425320918bSDave Airlie 3435320918bSDave Airlie DRM_DEBUG("\n"); 3445320918bSDave Airlie ret = udl_modeset_init(dev); 34526507b06SStéphane Marchesin if (ret) 34626507b06SStéphane Marchesin goto err; 3475320918bSDave Airlie 3485320918bSDave Airlie ret = udl_fbdev_init(dev); 34926507b06SStéphane Marchesin if (ret) 35026507b06SStéphane Marchesin goto err; 35126507b06SStéphane Marchesin 35226507b06SStéphane Marchesin ret = drm_vblank_init(dev, 1); 35326507b06SStéphane Marchesin if (ret) 35426507b06SStéphane Marchesin goto err_fb; 35526507b06SStéphane Marchesin 356afdfc4c6SRobert Tarasov drm_kms_helper_poll_init(dev); 357afdfc4c6SRobert Tarasov 3585320918bSDave Airlie return 0; 35926507b06SStéphane Marchesin err_fb: 36026507b06SStéphane Marchesin udl_fbdev_cleanup(dev); 3615320918bSDave Airlie err: 36226507b06SStéphane Marchesin if (udl->urbs.count) 36326507b06SStéphane Marchesin udl_free_urb_list(dev); 3645320918bSDave Airlie kfree(udl); 3655320918bSDave Airlie DRM_ERROR("%d\n", ret); 3665320918bSDave Airlie return ret; 3675320918bSDave Airlie } 3685320918bSDave Airlie 3695320918bSDave Airlie int udl_drop_usb(struct drm_device *dev) 3705320918bSDave Airlie { 3715320918bSDave Airlie udl_free_urb_list(dev); 3725320918bSDave Airlie return 0; 3735320918bSDave Airlie } 3745320918bSDave Airlie 37511b3c20bSGabriel Krisman Bertazi void udl_driver_unload(struct drm_device *dev) 3765320918bSDave Airlie { 3775320918bSDave Airlie struct udl_device *udl = dev->dev_private; 3785320918bSDave Airlie 379afdfc4c6SRobert Tarasov drm_kms_helper_poll_fini(dev); 380afdfc4c6SRobert Tarasov 3815320918bSDave Airlie if (udl->urbs.count) 3825320918bSDave Airlie udl_free_urb_list(dev); 3835320918bSDave Airlie 3845320918bSDave Airlie udl_fbdev_cleanup(dev); 3855320918bSDave Airlie udl_modeset_cleanup(dev); 3865320918bSDave Airlie kfree(udl); 3875320918bSDave Airlie } 388