1 /* 2 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org 3 * 4 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California. 5 * All Rights Reserved. 6 * 7 * Author Rickard E. (Rik) Faith <faith@valinux.com> 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice (including the next 17 * paragraph) shall be included in all copies or substantial portions of the 18 * Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 * DEALINGS IN THE SOFTWARE. 27 */ 28 29 #include <linux/module.h> 30 #include <linux/moduleparam.h> 31 #include <drm/drmP.h> 32 #include <drm/drm_core.h> 33 #include "drm_legacy.h" 34 #include "drm_internal.h" 35 36 /* 37 * drm_debug: Enable debug output. 38 * Bitmask of DRM_UT_x. See include/drm/drmP.h for details. 39 */ 40 #ifdef __DragonFly__ 41 /* Provides three levels of debug: off, minimal, verbose */ 42 #if DRM_DEBUG_DEFAULT_ON == 1 43 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \ 44 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL) 45 #elif DRM_DEBUG_DEFAULT_ON == 2 46 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \ 47 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL | \ 48 DRM_UT_PID | DRM_UT_IOCTL | DRM_UT_VBLANK) 49 #else 50 #define DRM_DEBUGBITS_ON (0x0) 51 #endif 52 unsigned int drm_debug = DRM_DEBUGBITS_ON; /* defaults to 0 */ 53 #else 54 unsigned int drm_debug = 0; 55 #endif /* __DragonFly__ */ 56 EXPORT_SYMBOL(drm_debug); 57 58 MODULE_AUTHOR(CORE_AUTHOR); 59 MODULE_DESCRIPTION(CORE_DESC); 60 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n" 61 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n" 62 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n" 63 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n" 64 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n" 65 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n" 66 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)"); 67 module_param_named(debug, drm_debug, int, 0600); 68 69 static DEFINE_MUTEX(drm_minor_lock); 70 static struct idr drm_minors_idr; 71 72 #if 0 73 static struct dentry *drm_debugfs_root; 74 #endif 75 76 void drm_err(const char *func, const char *format, ...) 77 { 78 va_list args; 79 80 kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* ", DRM_CURRENTPID, func); 81 82 va_start(args, format); 83 kvprintf(format, args); 84 va_end(args); 85 } 86 EXPORT_SYMBOL(drm_err); 87 88 void drm_ut_debug_printk(const char *function_name, const char *format, ...) 89 { 90 va_list args; 91 92 if (unlikely(drm_debug & DRM_UT_PID)) { 93 kprintf("[" DRM_NAME ":pid%d:%s] ", 94 DRM_CURRENTPID, function_name); 95 } else { 96 kprintf("[" DRM_NAME ":%s] ", function_name); 97 } 98 99 va_start(args, format); 100 kvprintf(format, args); 101 va_end(args); 102 } 103 EXPORT_SYMBOL(drm_ut_debug_printk); 104 105 #if 0 106 struct drm_master *drm_master_create(struct drm_minor *minor) 107 { 108 struct drm_master *master; 109 110 master = kzalloc(sizeof(*master), GFP_KERNEL); 111 if (!master) 112 return NULL; 113 114 kref_init(&master->refcount); 115 spin_lock_init(&master->lock.spinlock); 116 init_waitqueue_head(&master->lock.lock_queue); 117 if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) { 118 kfree(master); 119 return NULL; 120 } 121 master->minor = minor; 122 123 return master; 124 } 125 126 struct drm_master *drm_master_get(struct drm_master *master) 127 { 128 kref_get(&master->refcount); 129 return master; 130 } 131 EXPORT_SYMBOL(drm_master_get); 132 133 static void drm_master_destroy(struct kref *kref) 134 { 135 struct drm_master *master = container_of(kref, struct drm_master, refcount); 136 struct drm_device *dev = master->minor->dev; 137 struct drm_map_list *r_list, *list_temp; 138 139 if (dev->driver->master_destroy) 140 dev->driver->master_destroy(dev, master); 141 142 mutex_lock(&dev->struct_mutex); 143 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) { 144 if (r_list->master == master) { 145 drm_legacy_rmmap_locked(dev, r_list->map); 146 r_list = NULL; 147 } 148 } 149 150 if (master->unique) { 151 kfree(master->unique); 152 master->unique = NULL; 153 master->unique_len = 0; 154 } 155 156 drm_ht_remove(&master->magiclist); 157 158 mutex_unlock(&dev->struct_mutex); 159 kfree(master); 160 } 161 162 void drm_master_put(struct drm_master **master) 163 { 164 kref_put(&(*master)->refcount, drm_master_destroy); 165 *master = NULL; 166 } 167 EXPORT_SYMBOL(drm_master_put); 168 #endif 169 170 int drm_setmaster_ioctl(struct drm_device *dev, void *data, 171 struct drm_file *file_priv) 172 { 173 DRM_DEBUG("setmaster\n"); 174 175 if (file_priv->master != 0) 176 return (0); 177 178 return (-EPERM); 179 } 180 181 int drm_dropmaster_ioctl(struct drm_device *dev, void *data, 182 struct drm_file *file_priv) 183 { 184 DRM_DEBUG("dropmaster\n"); 185 if (file_priv->master != 0) 186 return -EINVAL; 187 return 0; 188 } 189 190 /* 191 * DRM Minors 192 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each 193 * of them is represented by a drm_minor object. Depending on the capabilities 194 * of the device-driver, different interfaces are registered. 195 * 196 * Minors can be accessed via dev->$minor_name. This pointer is either 197 * NULL or a valid drm_minor pointer and stays valid as long as the device is 198 * valid. This means, DRM minors have the same life-time as the underlying 199 * device. However, this doesn't mean that the minor is active. Minors are 200 * registered and unregistered dynamically according to device-state. 201 */ 202 203 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev, 204 unsigned int type) 205 { 206 switch (type) { 207 case DRM_MINOR_LEGACY: 208 return &dev->primary; 209 case DRM_MINOR_RENDER: 210 return &dev->render; 211 case DRM_MINOR_CONTROL: 212 return &dev->control; 213 default: 214 return NULL; 215 } 216 } 217 218 static int drm_minor_alloc(struct drm_device *dev, unsigned int type) 219 { 220 struct drm_minor *minor; 221 unsigned long flags; 222 int r; 223 224 minor = kzalloc(sizeof(*minor), GFP_KERNEL); 225 if (!minor) 226 return -ENOMEM; 227 228 minor->type = type; 229 minor->dev = dev; 230 231 idr_preload(GFP_KERNEL); 232 spin_lock_irqsave(&drm_minor_lock, flags); 233 r = idr_alloc(&drm_minors_idr, 234 NULL, 235 64 * type, 236 64 * (type + 1), 237 GFP_NOWAIT); 238 spin_unlock_irqrestore(&drm_minor_lock, flags); 239 idr_preload_end(); 240 241 if (r < 0) 242 goto err_free; 243 244 minor->index = r; 245 246 #if 0 247 minor->kdev = drm_sysfs_minor_alloc(minor); 248 if (IS_ERR(minor->kdev)) { 249 r = PTR_ERR(minor->kdev); 250 goto err_index; 251 } 252 #endif 253 254 *drm_minor_get_slot(dev, type) = minor; 255 return 0; 256 257 #if 0 258 err_index: 259 spin_lock_irqsave(&drm_minor_lock, flags); 260 idr_remove(&drm_minors_idr, minor->index); 261 spin_unlock_irqrestore(&drm_minor_lock, flags); 262 #endif 263 err_free: 264 kfree(minor); 265 return r; 266 } 267 268 static void drm_minor_free(struct drm_device *dev, unsigned int type) 269 { 270 struct drm_minor **slot, *minor; 271 unsigned long flags; 272 273 slot = drm_minor_get_slot(dev, type); 274 minor = *slot; 275 if (!minor) 276 return; 277 278 #if 0 279 put_device(minor->kdev); 280 #endif 281 282 spin_lock_irqsave(&drm_minor_lock, flags); 283 idr_remove(&drm_minors_idr, minor->index); 284 spin_unlock_irqrestore(&drm_minor_lock, flags); 285 286 kfree(minor); 287 *slot = NULL; 288 } 289 290 static int drm_minor_register(struct drm_device *dev, unsigned int type) 291 { 292 struct drm_minor *minor; 293 unsigned long flags; 294 #if 0 295 int ret; 296 #endif 297 298 DRM_DEBUG("\n"); 299 300 minor = *drm_minor_get_slot(dev, type); 301 if (!minor) 302 return 0; 303 304 #if 0 305 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root); 306 if (ret) { 307 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n"); 308 return ret; 309 } 310 311 ret = device_add(minor->kdev); 312 if (ret) 313 goto err_debugfs; 314 #endif 315 316 /* replace NULL with @minor so lookups will succeed from now on */ 317 spin_lock_irqsave(&drm_minor_lock, flags); 318 idr_replace(&drm_minors_idr, minor, minor->index); 319 spin_unlock_irqrestore(&drm_minor_lock, flags); 320 321 DRM_DEBUG("new minor registered %d\n", minor->index); 322 return 0; 323 324 #if 0 325 err_debugfs: 326 drm_debugfs_cleanup(minor); 327 return ret; 328 #endif 329 } 330 331 static void drm_minor_unregister(struct drm_device *dev, unsigned int type) 332 { 333 struct drm_minor *minor; 334 unsigned long flags; 335 336 minor = *drm_minor_get_slot(dev, type); 337 #if 0 338 if (!minor || !device_is_registered(minor->kdev)) 339 #else 340 if (!minor) 341 #endif 342 return; 343 344 /* replace @minor with NULL so lookups will fail from now on */ 345 spin_lock_irqsave(&drm_minor_lock, flags); 346 idr_replace(&drm_minors_idr, NULL, minor->index); 347 spin_unlock_irqrestore(&drm_minor_lock, flags); 348 349 #if 0 350 device_del(minor->kdev); 351 dev_set_drvdata(minor->kdev, NULL); /* safety belt */ 352 #endif 353 drm_debugfs_cleanup(minor); 354 } 355 356 #if 0 357 /** 358 * drm_minor_acquire - Acquire a DRM minor 359 * @minor_id: Minor ID of the DRM-minor 360 * 361 * Looks up the given minor-ID and returns the respective DRM-minor object. The 362 * refence-count of the underlying device is increased so you must release this 363 * object with drm_minor_release(). 364 * 365 * As long as you hold this minor, it is guaranteed that the object and the 366 * minor->dev pointer will stay valid! However, the device may get unplugged and 367 * unregistered while you hold the minor. 368 * 369 * Returns: 370 * Pointer to minor-object with increased device-refcount, or PTR_ERR on 371 * failure. 372 */ 373 struct drm_minor *drm_minor_acquire(unsigned int minor_id) 374 { 375 struct drm_minor *minor; 376 unsigned long flags; 377 378 spin_lock_irqsave(&drm_minor_lock, flags); 379 minor = idr_find(&drm_minors_idr, minor_id); 380 if (minor) 381 drm_dev_ref(minor->dev); 382 spin_unlock_irqrestore(&drm_minor_lock, flags); 383 384 if (!minor) { 385 return ERR_PTR(-ENODEV); 386 } else if (drm_device_is_unplugged(minor->dev)) { 387 drm_dev_unref(minor->dev); 388 return ERR_PTR(-ENODEV); 389 } 390 391 return minor; 392 } 393 394 /** 395 * drm_minor_release - Release DRM minor 396 * @minor: Pointer to DRM minor object 397 * 398 * Release a minor that was previously acquired via drm_minor_acquire(). 399 */ 400 void drm_minor_release(struct drm_minor *minor) 401 { 402 drm_dev_unref(minor->dev); 403 } 404 405 /** 406 * DOC: driver instance overview 407 * 408 * A device instance for a drm driver is represented by struct &drm_device. This 409 * is allocated with drm_dev_alloc(), usually from bus-specific ->probe() 410 * callbacks implemented by the driver. The driver then needs to initialize all 411 * the various subsystems for the drm device like memory management, vblank 412 * handling, modesetting support and intial output configuration plus obviously 413 * initialize all the corresponding hardware bits. An important part of this is 414 * also calling drm_dev_set_unique() to set the userspace-visible unique name of 415 * this device instance. Finally when everything is up and running and ready for 416 * userspace the device instance can be published using drm_dev_register(). 417 * 418 * There is also deprecated support for initalizing device instances using 419 * bus-specific helpers and the ->load() callback. But due to 420 * backwards-compatibility needs the device instance have to be published too 421 * early, which requires unpretty global locking to make safe and is therefore 422 * only support for existing drivers not yet converted to the new scheme. 423 * 424 * When cleaning up a device instance everything needs to be done in reverse: 425 * First unpublish the device instance with drm_dev_unregister(). Then clean up 426 * any other resources allocated at device initialization and drop the driver's 427 * reference to &drm_device using drm_dev_unref(). 428 * 429 * Note that the lifetime rules for &drm_device instance has still a lot of 430 * historical baggage. Hence use the reference counting provided by 431 * drm_dev_ref() and drm_dev_unref() only carefully. 432 * 433 * Also note that embedding of &drm_device is currently not (yet) supported (but 434 * it would be easy to add). Drivers can store driver-private data in the 435 * dev_priv field of &drm_device. 436 */ 437 438 /** 439 * drm_put_dev - Unregister and release a DRM device 440 * @dev: DRM device 441 * 442 * Called at module unload time or when a PCI device is unplugged. 443 * 444 * Cleans up all DRM device, calling drm_lastclose(). 445 * 446 * Note: Use of this function is deprecated. It will eventually go away 447 * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly 448 * instead to make sure that the device isn't userspace accessible any more 449 * while teardown is in progress, ensuring that userspace can't access an 450 * inconsistent state. 451 */ 452 void drm_put_dev(struct drm_device *dev) 453 { 454 DRM_DEBUG("\n"); 455 456 if (!dev) { 457 DRM_ERROR("cleanup called no dev\n"); 458 return; 459 } 460 461 drm_dev_unregister(dev); 462 drm_dev_unref(dev); 463 } 464 EXPORT_SYMBOL(drm_put_dev); 465 466 void drm_unplug_dev(struct drm_device *dev) 467 { 468 /* for a USB device */ 469 drm_minor_unregister(dev, DRM_MINOR_LEGACY); 470 drm_minor_unregister(dev, DRM_MINOR_RENDER); 471 drm_minor_unregister(dev, DRM_MINOR_CONTROL); 472 473 mutex_lock(&drm_global_mutex); 474 475 drm_device_set_unplugged(dev); 476 477 if (dev->open_count == 0) { 478 drm_put_dev(dev); 479 } 480 mutex_unlock(&drm_global_mutex); 481 } 482 EXPORT_SYMBOL(drm_unplug_dev); 483 484 /* 485 * DRM internal mount 486 * We want to be able to allocate our own "struct address_space" to control 487 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow 488 * stand-alone address_space objects, so we need an underlying inode. As there 489 * is no way to allocate an independent inode easily, we need a fake internal 490 * VFS mount-point. 491 * 492 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free() 493 * frees it again. You are allowed to use iget() and iput() to get references to 494 * the inode. But each drm_fs_inode_new() call must be paired with exactly one 495 * drm_fs_inode_free() call (which does not have to be the last iput()). 496 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it 497 * between multiple inode-users. You could, technically, call 498 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an 499 * iput(), but this way you'd end up with a new vfsmount for each inode. 500 */ 501 502 static int drm_fs_cnt; 503 static struct vfsmount *drm_fs_mnt; 504 505 static const struct dentry_operations drm_fs_dops = { 506 .d_dname = simple_dname, 507 }; 508 509 static const struct super_operations drm_fs_sops = { 510 .statfs = simple_statfs, 511 }; 512 513 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags, 514 const char *dev_name, void *data) 515 { 516 return mount_pseudo(fs_type, 517 "drm:", 518 &drm_fs_sops, 519 &drm_fs_dops, 520 0x010203ff); 521 } 522 523 static struct file_system_type drm_fs_type = { 524 .name = "drm", 525 .owner = THIS_MODULE, 526 .mount = drm_fs_mount, 527 .kill_sb = kill_anon_super, 528 }; 529 530 static struct inode *drm_fs_inode_new(void) 531 { 532 struct inode *inode; 533 int r; 534 535 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt); 536 if (r < 0) { 537 DRM_ERROR("Cannot mount pseudo fs: %d\n", r); 538 return ERR_PTR(r); 539 } 540 541 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb); 542 if (IS_ERR(inode)) 543 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 544 545 return inode; 546 } 547 548 static void drm_fs_inode_free(struct inode *inode) 549 { 550 if (inode) { 551 iput(inode); 552 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt); 553 } 554 } 555 #endif 556 557 /** 558 * drm_dev_alloc - Allocate new DRM device 559 * @driver: DRM driver to allocate device for 560 * @parent: Parent device object 561 * 562 * Allocate and initialize a new DRM device. No device registration is done. 563 * Call drm_dev_register() to advertice the device to user space and register it 564 * with other core subsystems. This should be done last in the device 565 * initialization sequence to make sure userspace can't access an inconsistent 566 * state. 567 * 568 * The initial ref-count of the object is 1. Use drm_dev_ref() and 569 * drm_dev_unref() to take and drop further ref-counts. 570 * 571 * Note that for purely virtual devices @parent can be NULL. 572 * 573 * RETURNS: 574 * Pointer to new DRM device, or NULL if out of memory. 575 */ 576 struct drm_device *drm_dev_alloc(struct drm_driver *driver, 577 struct device *parent) 578 { 579 struct drm_device *dev; 580 int ret; 581 582 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 583 if (!dev) 584 return NULL; 585 586 kref_init(&dev->ref); 587 dev->dev = parent; 588 dev->driver = driver; 589 590 INIT_LIST_HEAD(&dev->filelist); 591 INIT_LIST_HEAD(&dev->ctxlist); 592 INIT_LIST_HEAD(&dev->vmalist); 593 INIT_LIST_HEAD(&dev->maplist); 594 INIT_LIST_HEAD(&dev->vblank_event_list); 595 596 spin_init(&dev->buf_lock, "drmdbl"); 597 lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE); 598 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE); 599 lockinit(&dev->filelist_mutex, "drmflm", 0, LK_CANRECURSE); 600 lockinit(&dev->ctxlist_mutex, "drmclm", 0, LK_CANRECURSE); 601 lockinit(&dev->master_mutex, "drmmm", 0, LK_CANRECURSE); 602 603 #ifndef __DragonFly__ 604 dev->anon_inode = drm_fs_inode_new(); 605 if (IS_ERR(dev->anon_inode)) { 606 ret = PTR_ERR(dev->anon_inode); 607 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret); 608 goto err_free; 609 } 610 #else 611 dev->anon_inode = NULL; 612 #endif 613 614 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 615 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL); 616 if (ret) 617 goto err_minors; 618 619 WARN_ON(driver->suspend || driver->resume); 620 } 621 622 if (drm_core_check_feature(dev, DRIVER_RENDER)) { 623 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER); 624 if (ret) 625 goto err_minors; 626 } 627 628 ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY); 629 if (ret) 630 goto err_minors; 631 632 if (drm_ht_create(&dev->map_hash, 12)) 633 goto err_minors; 634 635 drm_legacy_ctxbitmap_init(dev); 636 637 if (drm_core_check_feature(dev, DRIVER_GEM)) { 638 ret = drm_gem_init(dev); 639 if (ret) { 640 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n"); 641 goto err_ctxbitmap; 642 } 643 } 644 645 #if 0 646 if (parent) { 647 ret = drm_dev_set_unique(dev, dev_name(parent)); 648 if (ret) 649 goto err_setunique; 650 } 651 #endif 652 653 return dev; 654 655 #if 0 656 err_setunique: 657 if (drm_core_check_feature(dev, DRIVER_GEM)) 658 drm_gem_destroy(dev); 659 #endif 660 err_ctxbitmap: 661 drm_legacy_ctxbitmap_cleanup(dev); 662 drm_ht_remove(&dev->map_hash); 663 err_minors: 664 drm_minor_free(dev, DRM_MINOR_LEGACY); 665 drm_minor_free(dev, DRM_MINOR_RENDER); 666 drm_minor_free(dev, DRM_MINOR_CONTROL); 667 #ifndef __DragonFly__ 668 drm_fs_inode_free(dev->anon_inode); 669 err_free: 670 #endif 671 mutex_destroy(&dev->master_mutex); 672 kfree(dev); 673 return NULL; 674 } 675 EXPORT_SYMBOL(drm_dev_alloc); 676 677 #if 0 678 static void drm_dev_release(struct kref *ref) 679 { 680 struct drm_device *dev = container_of(ref, struct drm_device, ref); 681 682 if (drm_core_check_feature(dev, DRIVER_GEM)) 683 drm_gem_destroy(dev); 684 685 drm_legacy_ctxbitmap_cleanup(dev); 686 drm_ht_remove(&dev->map_hash); 687 drm_fs_inode_free(dev->anon_inode); 688 689 drm_minor_free(dev, DRM_MINOR_LEGACY); 690 drm_minor_free(dev, DRM_MINOR_RENDER); 691 drm_minor_free(dev, DRM_MINOR_CONTROL); 692 693 mutex_destroy(&dev->master_mutex); 694 kfree(dev->unique); 695 kfree(dev); 696 } 697 698 /** 699 * drm_dev_ref - Take reference of a DRM device 700 * @dev: device to take reference of or NULL 701 * 702 * This increases the ref-count of @dev by one. You *must* already own a 703 * reference when calling this. Use drm_dev_unref() to drop this reference 704 * again. 705 * 706 * This function never fails. However, this function does not provide *any* 707 * guarantee whether the device is alive or running. It only provides a 708 * reference to the object and the memory associated with it. 709 */ 710 void drm_dev_ref(struct drm_device *dev) 711 { 712 if (dev) 713 kref_get(&dev->ref); 714 } 715 EXPORT_SYMBOL(drm_dev_ref); 716 717 /** 718 * drm_dev_unref - Drop reference of a DRM device 719 * @dev: device to drop reference of or NULL 720 * 721 * This decreases the ref-count of @dev by one. The device is destroyed if the 722 * ref-count drops to zero. 723 */ 724 void drm_dev_unref(struct drm_device *dev) 725 { 726 if (dev) 727 kref_put(&dev->ref, drm_dev_release); 728 } 729 EXPORT_SYMBOL(drm_dev_unref); 730 #endif 731 732 /** 733 * drm_dev_register - Register DRM device 734 * @dev: Device to register 735 * @flags: Flags passed to the driver's .load() function 736 * 737 * Register the DRM device @dev with the system, advertise device to user-space 738 * and start normal device operation. @dev must be allocated via drm_dev_alloc() 739 * previously. Right after drm_dev_register() the driver should call 740 * drm_connector_register_all() to register all connectors in sysfs. This is 741 * a separate call for backward compatibility with drivers still using 742 * the deprecated ->load() callback, where connectors are registered from within 743 * the ->load() callback. 744 * 745 * Never call this twice on any device! 746 * 747 * NOTE: To ensure backward compatibility with existing drivers method this 748 * function calls the ->load() method after registering the device nodes, 749 * creating race conditions. Usage of the ->load() methods is therefore 750 * deprecated, drivers must perform all initialization before calling 751 * drm_dev_register(). 752 * 753 * RETURNS: 754 * 0 on success, negative error code on failure. 755 */ 756 int drm_dev_register(struct drm_device *dev, unsigned long flags) 757 { 758 int ret; 759 760 mutex_lock(&drm_global_mutex); 761 762 ret = drm_minor_register(dev, DRM_MINOR_CONTROL); 763 if (ret) 764 goto err_minors; 765 766 ret = drm_minor_register(dev, DRM_MINOR_RENDER); 767 if (ret) 768 goto err_minors; 769 770 ret = drm_minor_register(dev, DRM_MINOR_LEGACY); 771 if (ret) 772 goto err_minors; 773 774 if (dev->driver->load) { 775 ret = dev->driver->load(dev, flags); 776 if (ret) 777 goto err_minors; 778 } 779 780 ret = 0; 781 goto out_unlock; 782 783 err_minors: 784 drm_minor_unregister(dev, DRM_MINOR_LEGACY); 785 drm_minor_unregister(dev, DRM_MINOR_RENDER); 786 drm_minor_unregister(dev, DRM_MINOR_CONTROL); 787 out_unlock: 788 mutex_unlock(&drm_global_mutex); 789 return ret; 790 } 791 EXPORT_SYMBOL(drm_dev_register); 792 793 #if 0 794 /** 795 * drm_dev_unregister - Unregister DRM device 796 * @dev: Device to unregister 797 * 798 * Unregister the DRM device from the system. This does the reverse of 799 * drm_dev_register() but does not deallocate the device. The caller must call 800 * drm_dev_unref() to drop their final reference. 801 * 802 * This should be called first in the device teardown code to make sure 803 * userspace can't access the device instance any more. 804 */ 805 void drm_dev_unregister(struct drm_device *dev) 806 { 807 struct drm_map_list *r_list, *list_temp; 808 809 drm_lastclose(dev); 810 811 if (dev->driver->unload) 812 dev->driver->unload(dev); 813 814 if (dev->agp) 815 drm_pci_agp_destroy(dev); 816 817 drm_vblank_cleanup(dev); 818 819 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) 820 drm_legacy_rmmap(dev, r_list->map); 821 822 drm_minor_unregister(dev, DRM_MINOR_LEGACY); 823 drm_minor_unregister(dev, DRM_MINOR_RENDER); 824 drm_minor_unregister(dev, DRM_MINOR_CONTROL); 825 } 826 EXPORT_SYMBOL(drm_dev_unregister); 827 828 /** 829 * drm_dev_set_unique - Set the unique name of a DRM device 830 * @dev: device of which to set the unique name 831 * @name: unique name 832 * 833 * Sets the unique name of a DRM device using the specified string. Drivers 834 * can use this at driver probe time if the unique name of the devices they 835 * drive is static. 836 * 837 * Return: 0 on success or a negative error code on failure. 838 */ 839 int drm_dev_set_unique(struct drm_device *dev, const char *name) 840 { 841 kfree(dev->unique); 842 dev->unique = kstrdup(name, GFP_KERNEL); 843 844 return dev->unique ? 0 : -ENOMEM; 845 } 846 EXPORT_SYMBOL(drm_dev_set_unique); 847 #endif 848 849 /* 850 * DRM Core 851 * The DRM core module initializes all global DRM objects and makes them 852 * available to drivers. Once setup, drivers can probe their respective 853 * devices. 854 * Currently, core management includes: 855 * - The "DRM-Global" key/value database 856 * - Global ID management for connectors 857 * - DRM major number allocation 858 * - DRM minor management 859 * - DRM sysfs class 860 * - DRM debugfs root 861 * 862 * Furthermore, the DRM core provides dynamic char-dev lookups. For each 863 * interface registered on a DRM device, you can request minor numbers from DRM 864 * core. DRM core takes care of major-number management and char-dev 865 * registration. A stub ->open() callback forwards any open() requests to the 866 * registered minor. 867 */ 868 869 #if 0 870 static int drm_stub_open(struct inode *inode, struct file *filp) 871 { 872 const struct file_operations *new_fops; 873 struct drm_minor *minor; 874 int err; 875 876 DRM_DEBUG("\n"); 877 878 mutex_lock(&drm_global_mutex); 879 minor = drm_minor_acquire(iminor(inode)); 880 if (IS_ERR(minor)) { 881 err = PTR_ERR(minor); 882 goto out_unlock; 883 } 884 885 new_fops = fops_get(minor->dev->driver->fops); 886 if (!new_fops) { 887 err = -ENODEV; 888 goto out_release; 889 } 890 891 replace_fops(filp, new_fops); 892 if (filp->f_op->open) 893 err = filp->f_op->open(inode, filp); 894 else 895 err = 0; 896 897 out_release: 898 drm_minor_release(minor); 899 out_unlock: 900 mutex_unlock(&drm_global_mutex); 901 return err; 902 } 903 904 static const struct file_operations drm_stub_fops = { 905 .owner = THIS_MODULE, 906 .open = drm_stub_open, 907 .llseek = noop_llseek, 908 }; 909 910 static int __init drm_core_init(void) 911 { 912 int ret = -ENOMEM; 913 914 drm_global_init(); 915 drm_connector_ida_init(); 916 idr_init(&drm_minors_idr); 917 918 if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops)) 919 goto err_p1; 920 921 ret = drm_sysfs_init(); 922 if (ret < 0) { 923 printk(KERN_ERR "DRM: Error creating drm class.\n"); 924 goto err_p2; 925 } 926 927 drm_debugfs_root = debugfs_create_dir("dri", NULL); 928 if (!drm_debugfs_root) { 929 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n"); 930 ret = -1; 931 goto err_p3; 932 } 933 934 DRM_INFO("Initialized %s %d.%d.%d %s\n", 935 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE); 936 return 0; 937 err_p3: 938 drm_sysfs_destroy(); 939 err_p2: 940 unregister_chrdev(DRM_MAJOR, "drm"); 941 942 idr_destroy(&drm_minors_idr); 943 err_p1: 944 return ret; 945 } 946 947 static void __exit drm_core_exit(void) 948 { 949 debugfs_remove(drm_debugfs_root); 950 drm_sysfs_destroy(); 951 952 unregister_chrdev(DRM_MAJOR, "drm"); 953 954 drm_connector_ida_destroy(); 955 idr_destroy(&drm_minors_idr); 956 } 957 958 module_init(drm_core_init); 959 module_exit(drm_core_exit); 960 #endif 961 962 #include <sys/devfs.h> 963 964 #include <linux/export.h> 965 #include <linux/dmi.h> 966 #include <drm/drmP.h> 967 #include <drm/drm_core.h> 968 969 static int drm_load(struct drm_device *dev); 970 drm_pci_id_list_t *drm_find_description(int vendor, int device, 971 drm_pci_id_list_t *idlist); 972 973 #define DRIVER_SOFTC(unit) \ 974 ((struct drm_device *)devclass_get_softc(drm_devclass, unit)) 975 976 static int 977 drm_modevent(module_t mod, int type, void *data) 978 { 979 980 switch (type) { 981 case MOD_LOAD: 982 TUNABLE_INT_FETCH("drm.debug", &drm_debug); 983 linux_task_drop_callback = linux_task_drop; 984 linux_proc_drop_callback = linux_proc_drop; 985 break; 986 case MOD_UNLOAD: 987 linux_task_drop_callback = NULL; 988 linux_proc_drop_callback = NULL; 989 break; 990 } 991 return (0); 992 } 993 994 static moduledata_t drm_mod = { 995 "drm", 996 drm_modevent, 997 0 998 }; 999 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1000 MODULE_VERSION(drm, 1); 1001 MODULE_DEPEND(drm, agp, 1, 1, 1); 1002 MODULE_DEPEND(drm, pci, 1, 1, 1); 1003 MODULE_DEPEND(drm, iicbus, 1, 1, 1); 1004 1005 static struct dev_ops drm_cdevsw = { 1006 { "drm", 0, D_TRACKCLOSE | D_MPSAFE }, 1007 .d_open = drm_open, 1008 .d_close = drm_close, 1009 .d_read = drm_read, 1010 .d_ioctl = drm_ioctl, 1011 .d_kqfilter = drm_kqfilter, 1012 .d_mmap = drm_mmap, 1013 .d_mmap_single = drm_mmap_single, 1014 }; 1015 1016 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device"); 1017 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0, 1018 "DRM debugging"); 1019 1020 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist) 1021 { 1022 drm_pci_id_list_t *id_entry; 1023 int vendor, device; 1024 1025 vendor = pci_get_vendor(kdev); 1026 device = pci_get_device(kdev); 1027 1028 if (pci_get_class(kdev) != PCIC_DISPLAY) 1029 return ENXIO; 1030 1031 id_entry = drm_find_description(vendor, device, idlist); 1032 if (id_entry != NULL) { 1033 if (!device_get_desc(kdev)) { 1034 device_set_desc(kdev, id_entry->name); 1035 DRM_DEBUG("desc : %s\n", device_get_desc(kdev)); 1036 } 1037 return 0; 1038 } 1039 1040 return ENXIO; 1041 } 1042 1043 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist) 1044 { 1045 struct drm_device *dev; 1046 drm_pci_id_list_t *id_entry; 1047 int unit, error; 1048 1049 unit = device_get_unit(kdev); 1050 dev = device_get_softc(kdev); 1051 1052 /* Initialize Linux struct device */ 1053 dev->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1054 1055 if (!strcmp(device_get_name(kdev), "drmsub")) 1056 dev->dev->bsddev = device_get_parent(kdev); 1057 else 1058 dev->dev->bsddev = kdev; 1059 1060 dev->pci_domain = pci_get_domain(dev->dev->bsddev); 1061 dev->pci_bus = pci_get_bus(dev->dev->bsddev); 1062 dev->pci_slot = pci_get_slot(dev->dev->bsddev); 1063 dev->pci_func = pci_get_function(dev->dev->bsddev); 1064 drm_init_pdev(dev->dev->bsddev, &dev->pdev); 1065 1066 id_entry = drm_find_description(dev->pdev->vendor, 1067 dev->pdev->device, idlist); 1068 dev->id_entry = id_entry; 1069 1070 /* Print the contents of pdev struct. */ 1071 drm_print_pdev(dev->pdev); 1072 1073 lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE); 1074 lwkt_serialize_init(&dev->irq_lock); 1075 lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE); 1076 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE); 1077 1078 error = drm_load(dev); 1079 if (error) 1080 goto error; 1081 1082 error = drm_create_cdevs(kdev); 1083 1084 error: 1085 return (error); 1086 } 1087 1088 int 1089 drm_create_cdevs(device_t kdev) 1090 { 1091 struct drm_device *dev; 1092 int error, unit; 1093 1094 unit = device_get_unit(kdev); 1095 dev = device_get_softc(kdev); 1096 1097 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID, 1098 DRM_DEV_MODE, "dri/card%d", unit); 1099 error = 0; 1100 if (error == 0) 1101 dev->devnode->si_drv1 = dev; 1102 return (error); 1103 } 1104 1105 #ifndef DRM_DEV_NAME 1106 #define DRM_DEV_NAME "drm" 1107 #endif 1108 1109 devclass_t drm_devclass; 1110 1111 drm_pci_id_list_t *drm_find_description(int vendor, int device, 1112 drm_pci_id_list_t *idlist) 1113 { 1114 int i = 0; 1115 1116 for (i = 0; idlist[i].vendor != 0; i++) { 1117 if ((idlist[i].vendor == vendor) && 1118 ((idlist[i].device == device) || 1119 (idlist[i].device == 0))) { 1120 return &idlist[i]; 1121 } 1122 } 1123 return NULL; 1124 } 1125 1126 static int drm_load(struct drm_device *dev) 1127 { 1128 int i, retcode; 1129 1130 DRM_DEBUG("\n"); 1131 1132 INIT_LIST_HEAD(&dev->maplist); 1133 1134 drm_sysctl_init(dev); 1135 INIT_LIST_HEAD(&dev->filelist); 1136 1137 dev->counters = 6; 1138 dev->types[0] = _DRM_STAT_LOCK; 1139 dev->types[1] = _DRM_STAT_OPENS; 1140 dev->types[2] = _DRM_STAT_CLOSES; 1141 dev->types[3] = _DRM_STAT_IOCTLS; 1142 dev->types[4] = _DRM_STAT_LOCKS; 1143 dev->types[5] = _DRM_STAT_UNLOCKS; 1144 1145 for (i = 0; i < ARRAY_SIZE(dev->counts); i++) 1146 atomic_set(&dev->counts[i], 0); 1147 1148 INIT_LIST_HEAD(&dev->vblank_event_list); 1149 1150 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) { 1151 if (drm_pci_device_is_agp(dev)) 1152 dev->agp = drm_agp_init(dev); 1153 } 1154 1155 if (dev->driver->driver_features & DRIVER_GEM) { 1156 retcode = drm_gem_init(dev); 1157 if (retcode != 0) { 1158 DRM_ERROR("Cannot initialize graphics execution " 1159 "manager (GEM)\n"); 1160 goto error1; 1161 } 1162 } 1163 1164 if (dev->driver->load != NULL) { 1165 DRM_LOCK(dev); 1166 /* Shared code returns -errno. */ 1167 retcode = -dev->driver->load(dev, 1168 dev->id_entry->driver_private); 1169 if (pci_enable_busmaster(dev->dev->bsddev)) 1170 DRM_ERROR("Request to enable bus-master failed.\n"); 1171 DRM_UNLOCK(dev); 1172 if (retcode != 0) 1173 goto error1; 1174 } 1175 1176 DRM_INFO("Initialized %s %d.%d.%d %s\n", 1177 dev->driver->name, 1178 dev->driver->major, 1179 dev->driver->minor, 1180 dev->driver->patchlevel, 1181 dev->driver->date); 1182 1183 return 0; 1184 1185 error1: 1186 drm_gem_destroy(dev); 1187 drm_sysctl_cleanup(dev); 1188 DRM_LOCK(dev); 1189 drm_lastclose(dev); 1190 DRM_UNLOCK(dev); 1191 if (dev->devnode != NULL) 1192 destroy_dev(dev->devnode); 1193 1194 lockuninit(&dev->vbl_lock); 1195 lockuninit(&dev->dev_lock); 1196 lockuninit(&dev->event_lock); 1197 lockuninit(&dev->struct_mutex); 1198 1199 return retcode; 1200 } 1201 1202 /* 1203 * Stub is needed for devfs 1204 */ 1205 int drm_close(struct dev_close_args *ap) 1206 { 1207 return 0; 1208 } 1209 1210 /* XXX: this is supposed to be drm_release() */ 1211 void drm_cdevpriv_dtor(void *cd) 1212 { 1213 struct drm_file *file_priv = cd; 1214 struct drm_device *dev = file_priv->dev; 1215 1216 DRM_DEBUG("open_count = %d\n", dev->open_count); 1217 1218 DRM_LOCK(dev); 1219 1220 if (dev->driver->preclose != NULL) 1221 dev->driver->preclose(dev, file_priv); 1222 1223 /* ======================================================== 1224 * Begin inline drm_release 1225 */ 1226 1227 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", 1228 DRM_CURRENTPID, (long)dev->dev, dev->open_count); 1229 1230 if (dev->driver->driver_features & DRIVER_GEM) 1231 drm_gem_release(dev, file_priv); 1232 1233 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1234 drm_legacy_reclaim_buffers(dev, file_priv); 1235 1236 funsetown(&dev->buf_sigio); 1237 1238 if (dev->driver->postclose != NULL) 1239 dev->driver->postclose(dev, file_priv); 1240 list_del(&file_priv->lhead); 1241 1242 1243 /* ======================================================== 1244 * End inline drm_release 1245 */ 1246 1247 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]); 1248 device_unbusy(dev->dev->bsddev); 1249 if (--dev->open_count == 0) { 1250 drm_lastclose(dev); 1251 } 1252 1253 DRM_UNLOCK(dev); 1254 } 1255 1256 int 1257 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx, 1258 struct sysctl_oid *top) 1259 { 1260 struct sysctl_oid *oid; 1261 1262 ksnprintf(dev->busid_str, sizeof(dev->busid_str), 1263 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus, 1264 dev->pci_slot, dev->pci_func); 1265 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid", 1266 CTLFLAG_RD, dev->busid_str, 0, NULL); 1267 if (oid == NULL) 1268 return (ENOMEM); 1269 dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0; 1270 oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO, 1271 "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL); 1272 if (oid == NULL) 1273 return (ENOMEM); 1274 1275 return (0); 1276 } 1277 1278 int 1279 drm_mmap_single(struct dev_mmap_single_args *ap) 1280 { 1281 struct drm_device *dev; 1282 struct cdev *kdev = ap->a_head.a_dev; 1283 vm_ooffset_t *offset = ap->a_offset; 1284 vm_size_t size = ap->a_size; 1285 struct vm_object **obj_res = ap->a_object; 1286 int nprot = ap->a_nprot; 1287 1288 dev = drm_get_device_from_kdev(kdev); 1289 if (dev->drm_ttm_bdev != NULL) { 1290 return (ttm_bo_mmap_single(dev, offset, size, obj_res, nprot)); 1291 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) { 1292 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot)); 1293 } else { 1294 return (ENODEV); 1295 } 1296 } 1297 1298 static int 1299 drm_core_init(void *arg) 1300 { 1301 1302 drm_global_init(); 1303 1304 DRM_INFO("Initialized %s %d.%d.%d %s\n", 1305 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE); 1306 return 0; 1307 } 1308 1309 static void 1310 drm_core_exit(void *arg) 1311 { 1312 1313 drm_global_release(); 1314 } 1315 1316 SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, 1317 drm_core_init, NULL); 1318 SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, 1319 drm_core_exit, NULL); 1320 1321 1322 #include <linux/dmi.h> 1323 1324 /* 1325 * Check if dmi_system_id structure matches system DMI data 1326 */ 1327 static bool 1328 dmi_found(const struct dmi_system_id *dsi) 1329 { 1330 int i, slot; 1331 bool found = false; 1332 char *sys_vendor, *board_vendor, *product_name, *board_name; 1333 1334 sys_vendor = kgetenv("smbios.system.maker"); 1335 board_vendor = kgetenv("smbios.planar.maker"); 1336 product_name = kgetenv("smbios.system.product"); 1337 board_name = kgetenv("smbios.planar.product"); 1338 1339 for (i = 0; i < NELEM(dsi->matches); i++) { 1340 slot = dsi->matches[i].slot; 1341 switch (slot) { 1342 case DMI_NONE: 1343 break; 1344 case DMI_SYS_VENDOR: 1345 if (sys_vendor != NULL && 1346 !strcmp(sys_vendor, dsi->matches[i].substr)) 1347 break; 1348 else 1349 goto done; 1350 case DMI_BOARD_VENDOR: 1351 if (board_vendor != NULL && 1352 !strcmp(board_vendor, dsi->matches[i].substr)) 1353 break; 1354 else 1355 goto done; 1356 case DMI_PRODUCT_NAME: 1357 if (product_name != NULL && 1358 !strcmp(product_name, dsi->matches[i].substr)) 1359 break; 1360 else 1361 goto done; 1362 case DMI_BOARD_NAME: 1363 if (board_name != NULL && 1364 !strcmp(board_name, dsi->matches[i].substr)) 1365 break; 1366 else 1367 goto done; 1368 default: 1369 goto done; 1370 } 1371 } 1372 found = true; 1373 1374 done: 1375 if (sys_vendor != NULL) 1376 kfreeenv(sys_vendor); 1377 if (board_vendor != NULL) 1378 kfreeenv(board_vendor); 1379 if (product_name != NULL) 1380 kfreeenv(product_name); 1381 if (board_name != NULL) 1382 kfreeenv(board_name); 1383 1384 return found; 1385 } 1386 1387 int dmi_check_system(const struct dmi_system_id *sysid) 1388 { 1389 const struct dmi_system_id *dsi; 1390 int num = 0; 1391 1392 for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) { 1393 if (dmi_found(dsi)) { 1394 num++; 1395 if (dsi->callback && dsi->callback(dsi)) 1396 break; 1397 } 1398 } 1399 return (num); 1400 } 1401