Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
/*	$NetBSD: hpcfb.c,v 1.65 2021/12/19 21:15:28 andvar Exp $	*/

/*-
 * Copyright (c) 1999
 *         Shin Takemura and PocketBSD Project. All rights reserved.
 * Copyright (c) 2000,2001
 *         SATO Kazumi. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the PocketBSD project
 *	and its contributors.
 * 4. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/*
 * jump scroll, scroll thread, multiscreen, virtual text vram
 * and hpcfb_emulops functions
 * written by SATO Kazumi.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: hpcfb.c,v 1.65 2021/12/19 21:15:28 andvar Exp $");

#ifdef _KERNEL_OPT
#include "opt_hpcfb.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/signalvar.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/ioctl.h>

#include <sys/bus.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wscons_callbacks.h>

#include <dev/wsfont/wsfont.h>
#include <dev/rasops/rasops.h>

#include <dev/hpc/hpcfbvar.h>
#include <dev/hpc/hpcfbio.h>

#include "bivideo.h"
#if NBIVIDEO > 0
#include <dev/hpc/bivideovar.h>
#endif

#ifdef FBDEBUG
int	hpcfb_debug = 0;
#define	DPRINTF(arg)	if (hpcfb_debug) printf arg
#else
#define	DPRINTF(arg)	do {} while (/* CONSTCOND */ 0)
#endif

#ifndef HPCFB_MAX_COLUMN
#define HPCFB_MAX_COLUMN 130
#endif /* HPCFB_MAX_COLUMN */
#ifndef HPCFB_MAX_ROW
#define HPCFB_MAX_ROW 80
#endif /* HPCFB_MAX_ROW */

/*
 * currently experimental
#define HPCFB_JUMP
*/

struct hpcfb_vchar {
	u_int c;
	long attr;
};

struct hpcfb_tvrow {
	int maxcol;
	int spacecol;
	struct hpcfb_vchar col[HPCFB_MAX_COLUMN];
};

struct hpcfb_devconfig {
	struct rasops_info	dc_rinfo;	/* rasops information */

	int		dc_blanked;	/* currently had video disabled */
	struct hpcfb_softc *dc_sc;
	int dc_rows;
	int dc_cols;
	struct hpcfb_tvrow *dc_tvram;
	int dc_curx;
	int dc_cury;
#ifdef HPCFB_JUMP
	int dc_min_row;
	int dc_max_row;
	int dc_scroll;
	struct callout dc_scroll_ch;
	int dc_scroll_src;
	int dc_scroll_dst;
	int dc_scroll_num;
#endif /* HPCFB_JUMP */
	volatile int dc_state;
#define HPCFB_DC_CURRENT		0x80000000
#define HPCFB_DC_DRAWING		0x01	/* drawing raster ops */
#define HPCFB_DC_TDRAWING		0x02	/* drawing tvram */
#define HPCFB_DC_SCROLLPENDING		0x04	/* scroll is pending */
#define HPCFB_DC_UPDATE			0x08	/* tvram update */
#define HPCFB_DC_SCRDELAY		0x10	/* scroll time but delay it */
#define HPCFB_DC_SCRTHREAD		0x20	/* in scroll thread or callout */
#define HPCFB_DC_UPDATEALL		0x40	/* need to redraw all */
#define HPCFB_DC_ABORT			0x80	/* abort redrawing */
#define	HPCFB_DC_SWITCHREQ		0x100	/* switch request exist */
	int	dc_memsize;
	u_char *dc_fbaddr;
};

#define IS_DRAWABLE(dc) \
	(((dc)->dc_state&HPCFB_DC_CURRENT)&& \
	 (((dc)->dc_state&(HPCFB_DC_DRAWING|HPCFB_DC_SWITCHREQ)) == 0))

#define HPCFB_MAX_SCREEN 5
#define HPCFB_MAX_JUMP 5

struct hpcfb_softc {
	device_t sc_dev;
	struct	hpcfb_devconfig *sc_dc;	/* device configuration */
	const struct hpcfb_accessops	*sc_accessops;
	void *sc_accessctx;
	device_t sc_wsdisplay;
	int sc_screen_resumed;
	int sc_polling;
	int sc_mapping;
	struct proc *sc_thread;
	void *sc_wantedscreen;
	void (*sc_switchcb)(void *, int, int);
	void *sc_switchcbarg;
	struct callout sc_switch_callout;
	int sc_nfbconf;
	struct hpcfb_fbconf *sc_fbconflist;
};

/*
 *  function prototypes
 */
int	hpcfbmatch(device_t, cfdata_t, void *);
void	hpcfbattach(device_t, device_t, void *);
int	hpcfbprint(void *, const char *);

int	hpcfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
paddr_t	hpcfb_mmap(void *, void *, off_t, int);

void	hpcfb_refresh_screen(struct hpcfb_softc *);
void	hpcfb_doswitch(struct hpcfb_softc *);

#ifdef HPCFB_JUMP
static void	hpcfb_thread(void *);
#endif /* HPCFB_JUMP */

static int	hpcfb_init(struct hpcfb_fbconf *, struct hpcfb_devconfig *);
static int	hpcfb_alloc_screen(void *, const struct wsscreen_descr *,
		    void **, int *, int *, long *);
static void	hpcfb_free_screen(void *, void *);
static int	hpcfb_show_screen(void *, void *, int,
		    void (*) (void *, int, int), void *);
static void     hpcfb_pollc(void *, int);
static void	hpcfb_cmap_reorder(struct hpcfb_fbconf *,
		    struct hpcfb_devconfig *);

static void	hpcfb_power(int, void *);
static bool	hpcfb_suspend(device_t, const pmf_qual_t *);
static bool	hpcfb_resume(device_t, const pmf_qual_t *);


void    hpcfb_cursor(void *, int, int, int);
int     hpcfb_mapchar(void *, int, unsigned int *);
void    hpcfb_putchar(void *, int, int, u_int, long);
void    hpcfb_copycols(void *, int, int, int, int);
void    hpcfb_erasecols(void *, int, int, int, long);
void    hpcfb_redraw(void *, int, int, int);
void    hpcfb_copyrows(void *, int, int, int);
void    hpcfb_eraserows(void *, int, int, long);
int     hpcfb_allocattr(void *, int, int, int, long *);
void    hpcfb_cursor_raw(void *, int, int, int);

#ifdef HPCFB_JUMP
void	hpcfb_update(void *);
void	hpcfb_do_scroll(void *);
void	hpcfb_check_update(void *);
#endif /* HPCFB_JUMP */

struct wsdisplay_emulops hpcfb_emulops = {
	.cursor		= hpcfb_cursor,
	.mapchar	= hpcfb_mapchar,
	.putchar	= hpcfb_putchar,
	.copycols	= hpcfb_copycols,
	.erasecols	= hpcfb_erasecols,
	.copyrows	= hpcfb_copyrows,
	.eraserows	= hpcfb_eraserows,
	.allocattr	= hpcfb_allocattr,
	.replaceattr	= NULL,
};

/*
 *  static variables
 */
CFATTACH_DECL_NEW(hpcfb, sizeof(struct hpcfb_softc),
    hpcfbmatch, hpcfbattach, NULL, NULL);

struct wsscreen_descr hpcfb_stdscreen = {
	.name		= "std",
	.textops	= &hpcfb_emulops, /* XXX */
	.capabilities	= WSSCREEN_REVERSE,
	/* XXX: ncols/nrows will be filled in -- shouldn't, they are global */
};

const struct wsscreen_descr *_hpcfb_scrlist[] = {
	&hpcfb_stdscreen,
	/* XXX other formats, graphics screen? */
};

struct wsscreen_list hpcfb_screenlist = {
	.nscreens = __arraycount(_hpcfb_scrlist),
	.screens = _hpcfb_scrlist,
};

struct wsdisplay_accessops hpcfb_accessops = {
	.ioctl		= hpcfb_ioctl,
	.mmap		= hpcfb_mmap,
	.alloc_screen	= hpcfb_alloc_screen,
	.free_screen	= hpcfb_free_screen,
	.show_screen	= hpcfb_show_screen,
	.load_font	= NULL,
	.pollc		= hpcfb_pollc,
	.scroll		= NULL,
};

void    hpcfb_tv_putchar(struct hpcfb_devconfig *, int, int, u_int, long);
void    hpcfb_tv_copycols(struct hpcfb_devconfig *, int, int, int, int);
void    hpcfb_tv_erasecols(struct hpcfb_devconfig *, int, int, int, long);
void    hpcfb_tv_copyrows(struct hpcfb_devconfig *, int, int, int);
void    hpcfb_tv_eraserows(struct hpcfb_devconfig *, int, int, long);

struct wsdisplay_emulops rasops_emul;

static int hpcfbconsole;
struct hpcfb_devconfig hpcfb_console_dc;
struct wsscreen_descr hpcfb_console_wsscreen;
struct hpcfb_tvrow hpcfb_console_tvram[HPCFB_MAX_ROW];

/*
 *  function bodies
 */

int
hpcfbmatch(device_t parent, cfdata_t match, void *aux)
{
	return (1);
}

void
hpcfbattach(device_t parent, device_t self, void *aux)
{
	struct hpcfb_softc *sc;
	struct hpcfb_attach_args *ha = aux;
	struct wsemuldisplaydev_attach_args wa;

	sc = device_private(self);
	sc->sc_dev = self;

	sc->sc_accessops = ha->ha_accessops;
	sc->sc_accessctx = ha->ha_accessctx;
	sc->sc_nfbconf = ha->ha_nfbconf;
	sc->sc_fbconflist = ha->ha_fbconflist;

	if (hpcfbconsole) {
		sc->sc_dc = &hpcfb_console_dc;
		sc->sc_dc->dc_rinfo.ri_flg &= ~RI_NO_AUTO;
		hpcfb_console_dc.dc_sc = sc;
		printf(": %dx%d pixels, %d colors, %dx%d chars",
		    sc->sc_dc->dc_rinfo.ri_width,sc->sc_dc->dc_rinfo.ri_height,
		    (1 << sc->sc_dc->dc_rinfo.ri_depth),
		    sc->sc_dc->dc_rinfo.ri_cols,sc->sc_dc->dc_rinfo.ri_rows);
		/* Set video chip dependent CLUT if any. */
		if (sc->sc_accessops->setclut)
			sc->sc_accessops->setclut(sc->sc_accessctx,
			    &hpcfb_console_dc.dc_rinfo);
	}
	printf("\n");

	sc->sc_polling = 0; /* XXX */
	sc->sc_mapping = 0; /* XXX */
	callout_init(&sc->sc_switch_callout, 0);

	wa.console = hpcfbconsole;
	wa.scrdata = &hpcfb_screenlist;
	wa.accessops = &hpcfb_accessops;
	wa.accesscookie = sc;

	sc->sc_wsdisplay = config_found(self, &wa, wsemuldisplaydevprint,
	    CFARGS_NONE);

#ifdef HPCFB_JUMP
	/*
	 * Create a kernel thread to scroll,
	 */
	if (kthread_create(PRI_NONE, 0, NULL, hpcfb_thread, sc,
	    &sc->sc_thread, "%s", device_xname(sc->sc_dev)) != 0) {
		/*
		 * We were unable to create the HPCFB thread; bail out.
		 */
		sc->sc_thread = 0;
		aprint_error_dev(sc->sc_dev, "unable to create thread, kernel "
		    "hpcfb scroll support disabled\n");
	}
#endif /* HPCFB_JUMP */

	if (!pmf_device_register(self, hpcfb_suspend, hpcfb_resume))
		aprint_error_dev(self, "unable to establish power handler\n");
}

#ifdef HPCFB_JUMP
void
hpcfb_thread(void *arg)
{
	struct hpcfb_softc *sc = arg;

	/*
	 * Loop forever, doing a periodic check for update events.
	 */
	for (;;) {
		/* HPCFB_LOCK(sc); */
		sc->sc_dc->dc_state |= HPCFB_DC_SCRTHREAD;
		if (!sc->sc_mapping) /* draw only EMUL mode */
			hpcfb_update(sc->sc_dc);
		sc->sc_dc->dc_state &= ~HPCFB_DC_SCRTHREAD;
		/* APM_UNLOCK(sc); */
		(void) tsleep(sc, PWAIT, "hpcfb",  (8 * hz) / 7 / 10);
	}
}
#endif /* HPCFB_JUMP */

/* Print function (for parent devices). */
int
hpcfbprint(void *aux, const char *pnp)
{
	if (pnp)
		aprint_normal("hpcfb at %s", pnp);

	return (UNCONF);
}

int
hpcfb_cnattach(struct hpcfb_fbconf *fbconf)
{
#if NBIVIDEO > 0
	struct hpcfb_fbconf __fbconf;
#endif
	long defattr;

	DPRINTF(("%s(%d): hpcfb_cnattach()\n", __FILE__, __LINE__));
#if NBIVIDEO > 0
	if (fbconf == NULL) {
		memset(&__fbconf, 0, sizeof(struct hpcfb_fbconf));
		if (bivideo_getcnfb(&__fbconf) != 0)
			return (ENXIO);
		fbconf = &__fbconf;
	}
#endif /* NBIVIDEO > 0 */
	memset(&hpcfb_console_dc, 0, sizeof(struct hpcfb_devconfig));
	if (hpcfb_init(fbconf, &hpcfb_console_dc) != 0)
		return (ENXIO);
	hpcfb_console_dc.dc_state |= HPCFB_DC_CURRENT;

	hpcfb_console_dc.dc_tvram = hpcfb_console_tvram;
	/* clear screen */
	memset(hpcfb_console_tvram, 0, sizeof(hpcfb_console_tvram));
	hpcfb_redraw(&hpcfb_console_dc, 0, hpcfb_console_dc.dc_rows, 1);

	hpcfb_console_wsscreen = hpcfb_stdscreen;
	hpcfb_console_wsscreen.nrows = hpcfb_console_dc.dc_rows;
	hpcfb_console_wsscreen.ncols = hpcfb_console_dc.dc_cols;
	hpcfb_console_wsscreen.capabilities = hpcfb_console_dc.dc_rinfo.ri_caps;
	hpcfb_allocattr(&hpcfb_console_dc,
			WSCOL_WHITE, WSCOL_BLACK, 0, &defattr);
#if NBIVIDEO > 0
	/*
	 * This is early console. Do not really attach wsdisplay.
	 */
	if (fbconf == &__fbconf)
		wsdisplay_preattach(&hpcfb_console_wsscreen, &hpcfb_console_dc,
		    0, 0, defattr);
	else
#endif
		wsdisplay_cnattach(&hpcfb_console_wsscreen, &hpcfb_console_dc,
		    0, 0, defattr);
	hpcfbconsole = 1;

	return (0);
}

int
hpcfb_init(struct hpcfb_fbconf *fbconf,	struct hpcfb_devconfig *dc)
{
	struct rasops_info *ri;
	vaddr_t fbaddr;

	fbaddr = (vaddr_t)fbconf->hf_baseaddr;
	dc->dc_fbaddr = (u_char *)fbaddr;

	/* init rasops */
	ri = &dc->dc_rinfo;
	memset(ri, 0, sizeof(struct rasops_info));
	ri->ri_depth = fbconf->hf_pixel_width;
	ri->ri_bits = (void *)fbaddr;
	ri->ri_width = fbconf->hf_width;
	ri->ri_height = fbconf->hf_height;
	ri->ri_stride = fbconf->hf_bytes_per_line;
#if 0
	ri->ri_flg = RI_FORCEMONO | RI_CURSOR;
#else
	ri->ri_flg = RI_CURSOR;
#endif
	if (dc == &hpcfb_console_dc)
		ri->ri_flg |= RI_NO_AUTO;

	switch (ri->ri_depth) {
	case 8:
		if (32 <= fbconf->hf_pack_width &&
		    (fbconf->hf_order_flags & HPCFB_REVORDER_BYTE) &&
		    (fbconf->hf_order_flags & HPCFB_REVORDER_WORD)) {
			ri->ri_flg |= RI_BSWAP;
		}
		break;
	default:
		if (fbconf->hf_order_flags & HPCFB_REVORDER_BYTE) {
#if BYTE_ORDER == BIG_ENDIAN
			ri->ri_flg |= RI_BSWAP;
#endif
		} else {
#if BYTE_ORDER == LITTLE_ENDIAN
			ri->ri_flg |= RI_BSWAP;
#endif
		}
		break;
	}

	if (fbconf->hf_class == HPCFB_CLASS_RGBCOLOR) {
		ri->ri_rnum = fbconf->hf_u.hf_rgb.hf_red_width;
		ri->ri_rpos = fbconf->hf_u.hf_rgb.hf_red_shift;
		ri->ri_gnum = fbconf->hf_u.hf_rgb.hf_green_width;
		ri->ri_gpos = fbconf->hf_u.hf_rgb.hf_green_shift;
		ri->ri_bnum = fbconf->hf_u.hf_rgb.hf_blue_width;
		ri->ri_bpos = fbconf->hf_u.hf_rgb.hf_blue_shift;
	}

	if (rasops_init(ri, HPCFB_MAX_ROW, HPCFB_MAX_COLUMN)) {
		aprint_error_dev(dc->dc_sc->sc_dev, "rasops_init() failed!");
		return -1;
	}

	/* over write color map of rasops */
	hpcfb_cmap_reorder (fbconf, dc);

	dc->dc_curx = -1;
	dc->dc_cury = -1;
	dc->dc_rows = dc->dc_rinfo.ri_rows;
	dc->dc_cols = dc->dc_rinfo.ri_cols;
#ifdef HPCFB_JUMP
	dc->dc_max_row = 0;
	dc->dc_min_row = dc->dc_rows;
	dc->dc_scroll = 0;
	callout_init(&dc->dc_scroll_ch, 0);
#endif /* HPCFB_JUMP */
	dc->dc_memsize = ri->ri_stride * ri->ri_height;
	/* hook rasops in hpcfb_ops */
	rasops_emul = ri->ri_ops; /* struct copy */
	ri->ri_ops = hpcfb_emulops; /* struct copy */

	return (0);
}

static void
hpcfb_cmap_reorder(struct hpcfb_fbconf *fbconf, struct hpcfb_devconfig *dc)
{
	struct rasops_info *ri = &dc->dc_rinfo;
	int reverse = fbconf->hf_access_flags & HPCFB_ACCESS_REVERSE;
	int *cmap = ri->ri_devcmap;
	int i, j, bg, fg, tmp;

	/*
	 * Set foreground and background so that the screen
	 * looks black on white.
	 * Normally, black = 00 and white = ff.
	 * HPCFB_ACCESS_REVERSE means black = ff and white = 00.
	 */
	switch (fbconf->hf_pixel_width) {
	case 1:
		/* FALLTHROUGH */
	case 2:
		/* FALLTHROUGH */
	case 4:
		if (reverse) {
			bg = 0;
			fg = ~0;
		} else {
			bg = ~0;
			fg = 0;
		}
		/* for gray-scale LCD, hi-contrast color map */
		cmap[0] = bg;
		for (i = 1; i < 16; i++)
			cmap[i] = fg;
		break;
	case 8:
		/* FALLTHROUGH */
	case 16:
		if (reverse) {
			for (i = 0, j = 15; i < 8; i++, j--) {
				tmp = cmap[i];
				cmap[i] = cmap[j];
				cmap[j] = tmp;
			}
		}
		break;
	}
}

int
hpcfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
	struct lwp *l)
{
	struct hpcfb_softc *sc = v;
	struct hpcfb_devconfig *dc = sc->sc_dc;
	struct wsdisplay_fbinfo *wdf;

	DPRINTF(("hpcfb_ioctl(cmd=0x%lx)\n", cmd));
	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = WSDISPLAY_TYPE_HPCFB;
		return (0);

	case WSDISPLAYIO_GINFO:
		wdf = (void *)data;
		wdf->height = dc->dc_rinfo.ri_height;
		wdf->width = dc->dc_rinfo.ri_width;
		wdf->depth = dc->dc_rinfo.ri_depth;
		wdf->cmsize = 256;	/* XXXX */
		return (0);

	case WSDISPLAYIO_LINEBYTES:
		*(u_int *)data = dc->dc_rinfo.ri_stride;
		return 0;

	case WSDISPLAYIO_SMODE:
		if (*(int *)data == WSDISPLAYIO_MODE_EMUL){
			if (sc->sc_mapping){
				sc->sc_mapping = 0;
				if (dc->dc_state&HPCFB_DC_DRAWING)
					dc->dc_state &= ~HPCFB_DC_ABORT;
#ifdef HPCFB_FORCE_REDRAW
				hpcfb_refresh_screen(sc);
#else
				dc->dc_state |= HPCFB_DC_UPDATEALL;
#endif
			}
		} else {
			if (!sc->sc_mapping) {
				sc->sc_mapping = 1;
				dc->dc_state |= HPCFB_DC_ABORT;
			}
			sc->sc_mapping = 1;
		}
		if (sc && sc->sc_accessops->iodone)
			(*sc->sc_accessops->iodone)(sc->sc_accessctx);
		return (0);

	case WSDISPLAYIO_GETCMAP:
	case WSDISPLAYIO_PUTCMAP:
	case WSDISPLAYIO_SVIDEO:
	case WSDISPLAYIO_GVIDEO:
	case WSDISPLAYIO_GETPARAM:
	case WSDISPLAYIO_SETPARAM:
	case HPCFBIO_GCONF:
	case HPCFBIO_SCONF:
	case HPCFBIO_GDSPCONF:
	case HPCFBIO_SDSPCONF:
	case HPCFBIO_GOP:
	case HPCFBIO_SOP:
		return ((*sc->sc_accessops->ioctl)(sc->sc_accessctx,
		    cmd, data, flag, l));

	default:
		if (IOCGROUP(cmd) != 't')
			DPRINTF(("%s(%d): hpcfb_ioctl(%lx, %lx) grp=%c num=%ld\n",
			    __FILE__, __LINE__,
			    cmd, (u_long)data, (char)IOCGROUP(cmd), cmd&0xff));
		break;
	}

	return (EPASSTHROUGH); /* Inappropriate ioctl for device */
}

paddr_t
hpcfb_mmap(void *v, void *vs, off_t offset, int prot)
{
	struct hpcfb_softc *sc = v;

	return ((*sc->sc_accessops->mmap)(sc->sc_accessctx, offset, prot));
}

static void
hpcfb_power(int why, void *arg)
{
	struct hpcfb_softc *sc = arg;

	if (sc->sc_dc == NULL)
		return;	/* You have no screen yet. */

	switch (why) {
	case PWR_STANDBY:
		break;
	case PWR_SOFTSUSPEND: {
		struct wsdisplay_softc *wsc = device_private(sc->sc_wsdisplay);

		sc->sc_screen_resumed = wsdisplay_getactivescreen(wsc);

		if (wsdisplay_switch(sc->sc_wsdisplay,
		    WSDISPLAY_NULLSCREEN, 1 /* waitok */) == 0) {
			wsscreen_switchwait(wsc, WSDISPLAY_NULLSCREEN);
		} else {
			sc->sc_screen_resumed = WSDISPLAY_NULLSCREEN;
		}

		sc->sc_dc->dc_state &= ~HPCFB_DC_CURRENT;
		break;
	    }
	case PWR_SOFTRESUME:
		sc->sc_dc->dc_state |= HPCFB_DC_CURRENT;
		if (sc->sc_screen_resumed != WSDISPLAY_NULLSCREEN)
			wsdisplay_switch(sc->sc_wsdisplay,
			    sc->sc_screen_resumed, 1 /* waitok */);
		break;
	}
}

static bool
hpcfb_suspend(device_t self, const pmf_qual_t *qual)
{
	struct hpcfb_softc *sc = device_private(self);

	hpcfb_power(PWR_SOFTSUSPEND, sc);
	return true;
}

static bool
hpcfb_resume(device_t self, const pmf_qual_t *qual)
{
	struct hpcfb_softc *sc = device_private(self);

	hpcfb_power(PWR_SOFTRESUME, sc);
	return true;
}

void
hpcfb_refresh_screen(struct hpcfb_softc *sc)
{
	struct hpcfb_devconfig *dc = sc->sc_dc;
	int x, y;

	DPRINTF(("hpcfb_refres_screen()\n"));
	if (dc == NULL)
		return;

#ifdef HPCFB_JUMP
	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
		dc->dc_state &= ~HPCFB_DC_SCROLLPENDING;
		dc->dc_state &= ~HPCFB_DC_UPDATE;
		callout_stop(&dc->dc_scroll_ch);
	}
#endif /* HPCFB_JUMP */
	/*
	 * refresh screen
	 */
	dc->dc_state &= ~HPCFB_DC_UPDATEALL;
	x = dc->dc_curx;
	y = dc->dc_cury;
	if (0 <= x && 0 <= y)
		hpcfb_cursor_raw(dc, 0,  y, x); /* disable cursor */
	/* redraw all text */
	hpcfb_redraw(dc, 0, dc->dc_rows, 1);
	if (0 <= x && 0 <= y)
		hpcfb_cursor_raw(dc, 1,  y, x); /* enable cursor */
}

static int
hpcfb_alloc_screen(void *v, const struct wsscreen_descr *type,
		   void **cookiep, int *curxp, int *curyp, long *attrp)
{
	struct hpcfb_softc *sc = v;
	struct hpcfb_devconfig *dc;

	DPRINTF(("%s(%d): hpcfb_alloc_screen()\n", __FILE__, __LINE__));

	dc = malloc(sizeof(*dc), M_DEVBUF, M_WAITOK|M_ZERO);
	if (dc == NULL)
		return ENOMEM;

	dc->dc_sc = sc;
	if (hpcfb_init(&sc->sc_fbconflist[0], dc) != 0) {
		free(dc, M_DEVBUF);
		return EINVAL;
	}
	if (sc->sc_accessops->font) {
		sc->sc_accessops->font(sc->sc_accessctx,
		    dc->dc_rinfo.ri_font);
	}
	/* Set video chip dependent CLUT if any. */
	if (sc->sc_accessops->setclut)
		sc->sc_accessops->setclut(sc->sc_accessctx, &dc->dc_rinfo);
	printf("hpcfb: %dx%d pixels, %d colors, %dx%d chars\n",
	    dc->dc_rinfo.ri_width, dc->dc_rinfo.ri_height,
	    (1 << dc->dc_rinfo.ri_depth),
	    dc->dc_rinfo.ri_cols, dc->dc_rinfo.ri_rows);

	/*
	 * XXX, wsdisplay won't reffer the information in wsscreen_descr
	 * structure until alloc_screen will be called, at least, under
	 * current implementation...
	 */
	hpcfb_stdscreen.nrows = dc->dc_rows;
        hpcfb_stdscreen.ncols = dc->dc_cols;
	hpcfb_stdscreen.capabilities = dc->dc_rinfo.ri_caps;

	dc->dc_fbaddr = dc->dc_rinfo.ri_bits;
	dc->dc_rows = dc->dc_rinfo.ri_rows;
	dc->dc_cols = dc->dc_rinfo.ri_cols;
	dc->dc_memsize = dc->dc_rinfo.ri_stride * dc->dc_rinfo.ri_height;

	dc->dc_curx = -1;
	dc->dc_cury = -1;
	dc->dc_tvram = malloc(sizeof(struct hpcfb_tvrow)*dc->dc_rows,
	    M_DEVBUF, M_WAITOK|M_ZERO);
	if (dc->dc_tvram == NULL){
		free(dc, M_DEVBUF);
		return (ENOMEM);
	}

	*curxp = 0;
	*curyp = 0;
	*cookiep = dc;
	hpcfb_allocattr(*cookiep, WSCOL_WHITE, WSCOL_BLACK, 0, attrp);
	DPRINTF(("%s(%d): hpcfb_alloc_screen(): %p\n",
	    __FILE__, __LINE__, dc));

	return (0);
}

static void
hpcfb_free_screen(void *v, void *cookie)
{
	struct hpcfb_devconfig *dc = cookie;

	DPRINTF(("%s(%d): hpcfb_free_screen(%p)\n",
	    __FILE__, __LINE__, cookie));
#ifdef DIAGNOSTIC
	if (dc == &hpcfb_console_dc)
		panic("hpcfb_free_screen: console");
#endif
	free(dc->dc_tvram, M_DEVBUF);
	free(dc, M_DEVBUF);
}

static int
hpcfb_show_screen(void *v, void *cookie, int waitok,
    void (*cb)(void *, int, int), void *cbarg)
{
	struct hpcfb_softc *sc = v;
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct hpcfb_devconfig *odc;

	DPRINTF(("%s(%d): hpcfb_show_screen(%p)\n",
	    __FILE__, __LINE__, dc));

	odc = sc->sc_dc;

	if (dc == NULL || odc == dc) {
		hpcfb_refresh_screen(sc);
		return (0);
	}

	if (odc != NULL) {
		odc->dc_state |= HPCFB_DC_SWITCHREQ;

		if ((odc->dc_state&HPCFB_DC_DRAWING) != 0) {
			odc->dc_state |= HPCFB_DC_ABORT;
		}
	}

	sc->sc_wantedscreen = cookie;
	sc->sc_switchcb = cb;
	sc->sc_switchcbarg = cbarg;
	if (cb) {
		callout_reset(&sc->sc_switch_callout, 0,
		    (void(*)(void *))hpcfb_doswitch, sc);
		return (EAGAIN);
	}

	hpcfb_doswitch(sc);
	return (0);
}

void
hpcfb_doswitch(struct hpcfb_softc *sc)
{
	struct hpcfb_devconfig *dc;
	struct hpcfb_devconfig *odc;

	DPRINTF(("hpcfb_doswitch()\n"));
	odc = sc->sc_dc;
	dc = sc->sc_wantedscreen;

	if (!dc) {
		(*sc->sc_switchcb)(sc->sc_switchcbarg, EIO, 0);
		odc->dc_state &= ~HPCFB_DC_SWITCHREQ;
		return;
	}

	if (odc == dc) {
		odc->dc_state &= ~HPCFB_DC_SWITCHREQ;
		return;
	}

	if (odc) {
#ifdef HPCFB_JUMP
		odc->dc_state |= HPCFB_DC_ABORT;
#endif /* HPCFB_JUMP */

		if (odc->dc_curx >= 0 && odc->dc_cury >= 0)
			hpcfb_cursor_raw(odc, 0,  odc->dc_cury, odc->dc_curx);
		/* disable cursor */
		/* disable old screen */
		odc->dc_state &= ~HPCFB_DC_CURRENT;
		/* XXX, This is too dangerous.
		odc->dc_rinfo.ri_bits = NULL;
		*/
	}
	/* switch screen to new one */
	dc->dc_state |= HPCFB_DC_CURRENT;
	dc->dc_state &= ~HPCFB_DC_ABORT;
	dc->dc_rinfo.ri_bits = dc->dc_fbaddr;
	sc->sc_dc = dc;

	/* redraw screen image */
	hpcfb_refresh_screen(sc);

	sc->sc_wantedscreen = NULL;
	if (sc->sc_switchcb)
		(*sc->sc_switchcb)(sc->sc_switchcbarg, 0, 0);

	if (odc != NULL)
		odc->dc_state &= ~HPCFB_DC_SWITCHREQ;
	dc->dc_state &= ~HPCFB_DC_SWITCHREQ;
	return;
}

static void
hpcfb_pollc(void *v, int on)
{
	struct hpcfb_softc *sc = v;

	if (sc == NULL)
		return;
	sc->sc_polling = on;
	if (sc->sc_accessops->iodone)
		(*sc->sc_accessops->iodone)(sc->sc_accessctx);
	if (on) {
		hpcfb_refresh_screen(sc);
		if (sc->sc_accessops->iodone)
			(*sc->sc_accessops->iodone)(sc->sc_accessctx);
	}

	return;
}

/*
 * cursor
 */
void
hpcfb_cursor(void *cookie, int on, int row, int col)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;

	if (on) {
		dc->dc_curx = col;
		dc->dc_cury = row;
	} else {
		dc->dc_curx = -1;
		dc->dc_cury = -1;
	}

	hpcfb_cursor_raw(cookie, on, row, col);
}

void
hpcfb_cursor_raw(void *cookie, int on, int row, int col)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct hpcfb_softc *sc = dc->dc_sc;
	struct rasops_info *ri = &dc->dc_rinfo;
	int curwidth, curheight;
	int xoff, yoff;

#ifdef HPCFB_JUMP
	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
		dc->dc_state |= HPCFB_DC_UPDATE;
		return;
	}
#endif /* HPCFB_JUMP */
	if (!IS_DRAWABLE(dc)) {
		return;
	}

	if (ri->ri_bits == NULL)
		return;

	dc->dc_state |= HPCFB_DC_DRAWING;
	if (sc && sc->sc_accessops->cursor) {
		xoff = col * ri->ri_font->fontwidth;
		yoff = row * ri->ri_font->fontheight;
		curheight = ri->ri_font->fontheight;
		curwidth = ri->ri_font->fontwidth;
		(*sc->sc_accessops->cursor)(sc->sc_accessctx,
		    on, xoff, yoff, curwidth, curheight);
	} else
		rasops_emul.cursor(ri, on, row, col);
	dc->dc_state &= ~HPCFB_DC_DRAWING;
}

/*
 * mapchar
 */
int
hpcfb_mapchar(void *cookie, int c, unsigned int *cp)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct rasops_info *ri = &dc->dc_rinfo;

	return (rasops_emul.mapchar(ri, c, cp));
}

/*
 * putchar
 */
void
hpcfb_tv_putchar(struct hpcfb_devconfig *dc, int row, int col, u_int uc,
    long attr)
{
	struct hpcfb_tvrow *vscn = dc->dc_tvram;
	struct hpcfb_vchar *vc = &vscn[row].col[col];
	struct hpcfb_vchar *vcb;

	if (vscn == 0)
		return;

	dc->dc_state |= HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	if (row < dc->dc_min_row)
		dc->dc_min_row = row;
	if (row > dc->dc_max_row)
		dc->dc_max_row = row;

#endif /* HPCFB_JUMP */
	if (vscn[row].maxcol +1 == col)
		vscn[row].maxcol = col;
	else if (vscn[row].maxcol < col) {
		vcb =  &vscn[row].col[vscn[row].maxcol+1];
		memset(vcb, 0,
		    sizeof(struct hpcfb_vchar)*(col-vscn[row].maxcol-1));
		vscn[row].maxcol = col;
	}
	vc->c = uc;
	vc->attr = attr;
	dc->dc_state &= ~HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

void
hpcfb_putchar(void *cookie, int row, int col, u_int uc, long attr)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct hpcfb_softc *sc = dc->dc_sc;
	struct rasops_info *ri = &dc->dc_rinfo;
	int xoff;
	int yoff;
	int fclr, uclr;
	struct wsdisplay_font *font;

	hpcfb_tv_putchar(dc, row, col, uc, attr);
#ifdef HPCFB_JUMP
	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
		dc->dc_state |= HPCFB_DC_UPDATE;
		return;
	}
#endif /* HPCFB_JUMP */

	if (!IS_DRAWABLE(dc)) {
		return;
	}

	if (ri->ri_bits == NULL)
		return;

	dc->dc_state |= HPCFB_DC_DRAWING;
	if (sc && sc->sc_accessops->putchar
	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
		font = ri->ri_font;
		yoff = row * ri->ri_font->fontheight;
		xoff =  col * ri->ri_font->fontwidth;
		fclr = ri->ri_devcmap[((u_int)attr >> 24) & 15];
		uclr = ri->ri_devcmap[((u_int)attr >> 16) & 15];

		(*sc->sc_accessops->putchar)(sc->sc_accessctx,
		    xoff, yoff, font, fclr, uclr, uc, attr);
	} else
		rasops_emul.putchar(ri, row, col, uc, attr);
	dc->dc_state &= ~HPCFB_DC_DRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

/*
 * copycols
 */
void
hpcfb_tv_copycols(struct hpcfb_devconfig *dc, int row, int srccol, int dstcol,
    int ncols)
{
	struct hpcfb_tvrow *vscn = dc->dc_tvram;
	struct hpcfb_vchar *svc = &vscn[row].col[srccol];
	struct hpcfb_vchar *dvc = &vscn[row].col[dstcol];

	if (vscn == 0)
		return;

	dc->dc_state |= HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	if (row < dc->dc_min_row)
		dc->dc_min_row = row;
	if (row > dc->dc_max_row)
		dc->dc_max_row = row;
#endif /* HPCFB_JUMP */

	memcpy(dvc, svc, ncols*sizeof(struct hpcfb_vchar));
	if (vscn[row].maxcol < srccol+ncols-1)
		vscn[row].maxcol = srccol+ncols-1;
	if (vscn[row].maxcol < dstcol+ncols-1)
		vscn[row].maxcol = dstcol+ncols-1;
	dc->dc_state &= ~HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

void
hpcfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct hpcfb_softc *sc = dc->dc_sc;
	struct rasops_info *ri = &dc->dc_rinfo;
	int srcxoff,dstxoff;
	int srcyoff,dstyoff;
	int height, width;

	hpcfb_tv_copycols(dc, row, srccol, dstcol, ncols);
#ifdef HPCFB_JUMP
	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
		dc->dc_state |= HPCFB_DC_UPDATE;
		return;
	}
#endif /* HPCFB_JUMP */
	if (!IS_DRAWABLE(dc)) {
		return;
	}

	if (ri->ri_bits == NULL)
		return;

	dc->dc_state |= HPCFB_DC_DRAWING;
	if (sc && sc->sc_accessops->bitblit
	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
		srcxoff = srccol * ri->ri_font->fontwidth;
		srcyoff = row * ri->ri_font->fontheight;
		dstxoff = dstcol * ri->ri_font->fontwidth;
		dstyoff = row * ri->ri_font->fontheight;
		width = ncols * ri->ri_font->fontwidth;
		height = ri->ri_font->fontheight;
		(*sc->sc_accessops->bitblit)(sc->sc_accessctx,
		    srcxoff, srcyoff, dstxoff, dstyoff, height, width);
	} else
		rasops_emul.copycols(ri, row, srccol, dstcol, ncols);
	dc->dc_state &= ~HPCFB_DC_DRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}


/*
 * erasecols
 */
void
hpcfb_tv_erasecols(struct hpcfb_devconfig *dc,
		   int row, int startcol, int ncols, long attr)
{
	struct hpcfb_tvrow *vscn = dc->dc_tvram;

	if (vscn == 0)
		return;

	dc->dc_state |= HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	if (row < dc->dc_min_row)
		dc->dc_min_row = row;
	if (row > dc->dc_max_row)
		dc->dc_max_row = row;
#endif /* HPCFB_JUMP */

	vscn[row].maxcol = startcol-1;
	if (vscn[row].spacecol < startcol+ncols-1)
		vscn[row].spacecol = startcol+ncols-1;
	dc->dc_state &= ~HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

void
hpcfb_erasecols(void *cookie, int row, int startcol, int ncols, long attr)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct hpcfb_softc *sc = dc->dc_sc;
	struct rasops_info *ri = &dc->dc_rinfo;
	int xoff, yoff;
	int width, height;

	hpcfb_tv_erasecols(dc, row, startcol, ncols, attr);
#ifdef HPCFB_JUMP
	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
		dc->dc_state |= HPCFB_DC_UPDATE;
		return;
	}
#endif /* HPCFB_JUMP */
	if (!IS_DRAWABLE(dc)) {
		return;
	}

	if (ri->ri_bits == NULL)
		return;

	dc->dc_state |= HPCFB_DC_DRAWING;
	if (sc && sc->sc_accessops->erase
	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
		xoff = startcol * ri->ri_font->fontwidth;
		yoff = row * ri->ri_font->fontheight;
		width = ncols * ri->ri_font->fontwidth;
		height = ri->ri_font->fontheight;
		(*sc->sc_accessops->erase)(sc->sc_accessctx,
		    xoff, yoff, height, width, attr);
	} else
		rasops_emul.erasecols(ri, row, startcol, ncols, attr);
	dc->dc_state &= ~HPCFB_DC_DRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

/*
 * Copy rows.
 */
void
hpcfb_tv_copyrows(struct hpcfb_devconfig *dc, int src, int dst, int num)
{
	struct hpcfb_tvrow *vscn = dc->dc_tvram;
	struct hpcfb_tvrow *svc = &vscn[src];
	struct hpcfb_tvrow *dvc = &vscn[dst];
	int i;
	int d;

	if (vscn == 0)
		return;

	dc->dc_state |= HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	if (dst < dc->dc_min_row)
		dc->dc_min_row = dst;
	if (dst + num > dc->dc_max_row)
		dc->dc_max_row = dst + num -1;
#endif /* HPCFB_JUMP */

	if (svc > dvc)
		d = 1;
	else if (svc < dvc) {
		svc += num-1;
		dvc += num-1;
		d = -1;
	} else  {
		dc->dc_state &= ~HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
		hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
		return;
	}

	for (i = 0; i < num; i++) {
		memcpy(&dvc->col[0], &svc->col[0], sizeof(struct hpcfb_vchar)*(svc->maxcol+1));
		if (svc->maxcol < dvc->maxcol && dvc->spacecol < dvc->maxcol)
			dvc->spacecol = dvc->maxcol;
		dvc->maxcol = svc->maxcol;
		svc+=d;
		dvc+=d;
	}
	dc->dc_state &= ~HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

void
hpcfb_redraw(void *cookie, int row, int num, int all)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct rasops_info *ri = &dc->dc_rinfo;
	int cols;
	struct hpcfb_tvrow *vscn = dc->dc_tvram;
	struct hpcfb_vchar *svc;
	int i, j;

#ifdef HPCFB_JUMP
	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
		dc->dc_state |= HPCFB_DC_UPDATE;
		return;
	}
#endif /* HPCFB_JUMP */
	if (dc->dc_sc != NULL
	    && !dc->dc_sc->sc_polling
	    && dc->dc_sc->sc_mapping)
		return;

	dc->dc_state &= ~HPCFB_DC_ABORT;

	if (vscn == 0)
		return;

	if (!IS_DRAWABLE(dc)) {
		return;
	}

	if (ri->ri_bits == NULL)
		return;

	dc->dc_state |= HPCFB_DC_DRAWING;
	dc->dc_state |= HPCFB_DC_TDRAWING;
	for (i = 0; i < num; i++) {
		if (dc->dc_state&HPCFB_DC_ABORT)
			break;
		if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
			break;
		cols = vscn[row+i].maxcol;
		for (j = 0; j <= cols; j++) {
			if (dc->dc_state&HPCFB_DC_ABORT)
				continue;
			if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
				continue;
			svc = &vscn[row+i].col[j];
			rasops_emul.putchar(ri, row + i, j, svc->c, svc->attr);
		}
		if (all)
			cols = dc->dc_cols-1;
		else
			cols = vscn[row+i].spacecol;
		for (; j <= cols; j++) {
			if (dc->dc_state&HPCFB_DC_ABORT)
				continue;
			if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
				continue;
			rasops_emul.putchar(ri, row + i, j, ' ', 0);
		}
		vscn[row+i].spacecol = 0;
	}
	if (dc->dc_state&HPCFB_DC_ABORT)
		dc->dc_state &= ~HPCFB_DC_ABORT;
	dc->dc_state &= ~HPCFB_DC_DRAWING;
	dc->dc_state &= ~HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

#ifdef HPCFB_JUMP
void
hpcfb_update(void *v)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)v;

	/* callout_stop(&dc->dc_scroll_ch); */
	dc->dc_state &= ~HPCFB_DC_SCROLLPENDING;
	if (dc->dc_curx > 0 && dc->dc_cury > 0)
		hpcfb_cursor_raw(dc, 0,  dc->dc_cury, dc->dc_curx);
	if ((dc->dc_state&HPCFB_DC_UPDATEALL)) {
		hpcfb_redraw(dc, 0, dc->dc_rows, 1);
		dc->dc_state &= ~(HPCFB_DC_UPDATE|HPCFB_DC_UPDATEALL);
	} else if ((dc->dc_state&HPCFB_DC_UPDATE)) {
		hpcfb_redraw(dc, dc->dc_min_row,
		    dc->dc_max_row - dc->dc_min_row, 0);
		dc->dc_state &= ~HPCFB_DC_UPDATE;
	} else {
		hpcfb_redraw(dc, dc->dc_scroll_dst, dc->dc_scroll_num, 0);
	}
	if (dc->dc_curx > 0 && dc->dc_cury > 0)
		hpcfb_cursor_raw(dc, 1,  dc->dc_cury, dc->dc_curx);
}

void
hpcfb_do_scroll(void *v)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)v;

	dc->dc_state |= HPCFB_DC_SCRTHREAD;
	if (dc->dc_state&(HPCFB_DC_DRAWING|HPCFB_DC_TDRAWING))
		dc->dc_state |= HPCFB_DC_SCRDELAY;
	else if (dc->dc_sc != NULL && dc->dc_sc->sc_thread)
		wakeup(dc->dc_sc);
	else if (dc->dc_sc != NULL && !dc->dc_sc->sc_mapping) {
		/* draw only EMUL mode */
		hpcfb_update(v);
	}
	dc->dc_state &= ~HPCFB_DC_SCRTHREAD;
}

void
hpcfb_check_update(void *v)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)v;

	if (dc->dc_sc != NULL
	    && dc->dc_sc->sc_polling
	    && (dc->dc_state&HPCFB_DC_SCROLLPENDING)){
		callout_stop(&dc->dc_scroll_ch);
		dc->dc_state &= ~HPCFB_DC_SCRDELAY;
		hpcfb_update(v);
	}
	else if (dc->dc_state&HPCFB_DC_SCRDELAY){
		dc->dc_state &= ~HPCFB_DC_SCRDELAY;
		hpcfb_update(v);
	} else if (dc->dc_state&HPCFB_DC_UPDATEALL){
		dc->dc_state &= ~HPCFB_DC_UPDATEALL;
		hpcfb_update(v);
	}
}
#endif /* HPCFB_JUMP */

void
hpcfb_copyrows(void *cookie, int src, int dst, int num)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct rasops_info *ri = &dc->dc_rinfo;
	struct hpcfb_softc *sc = dc->dc_sc;
	int srcyoff, dstyoff;
	int width, height;

	hpcfb_tv_copyrows(cookie, src, dst, num);

	if (!IS_DRAWABLE(dc)) {
		return;
	}

	if (ri->ri_bits == NULL)
		return;

	if (sc && sc->sc_accessops->bitblit
	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
		dc->dc_state |= HPCFB_DC_DRAWING;
		srcyoff = src * ri->ri_font->fontheight;
		dstyoff = dst * ri->ri_font->fontheight;
		width = dc->dc_cols * ri->ri_font->fontwidth;
		height = num * ri->ri_font->fontheight;
		(*sc->sc_accessops->bitblit)(sc->sc_accessctx,
		    0, srcyoff, 0, dstyoff, height, width);
		dc->dc_state &= ~HPCFB_DC_DRAWING;
	}
	else {
#ifdef HPCFB_JUMP
		if (sc && sc->sc_polling) {
			hpcfb_check_update(dc);
		} else if ((dc->dc_state&HPCFB_DC_SCROLLPENDING) == 0) {
			dc->dc_state |= HPCFB_DC_SCROLLPENDING;
			dc->dc_scroll = 1;
			dc->dc_scroll_src = src;
			dc->dc_scroll_dst = dst;
			dc->dc_scroll_num = num;
			callout_reset(&dc->dc_scroll_ch, hz/100, &hpcfb_do_scroll, dc);
			return;
		} else if (dc->dc_scroll++ < dc->dc_rows/HPCFB_MAX_JUMP) {
			dc->dc_state |= HPCFB_DC_UPDATE;
			return;
		} else {
			dc->dc_state &= ~HPCFB_DC_SCROLLPENDING;
			callout_stop(&dc->dc_scroll_ch);
		}
		if (dc->dc_state&HPCFB_DC_UPDATE) {
			dc->dc_state &= ~HPCFB_DC_UPDATE;
			hpcfb_redraw(cookie, dc->dc_min_row,
			    dc->dc_max_row - dc->dc_min_row, 0);
			dc->dc_max_row = 0;
			dc->dc_min_row = dc->dc_rows;
			if (dc->dc_curx > 0 && dc->dc_cury > 0)
				hpcfb_cursor(dc, 1,  dc->dc_cury, dc->dc_curx);
			return;
		}
#endif /* HPCFB_JUMP */
		hpcfb_redraw(cookie, dst, num, 0);
	}
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

/*
 * eraserows
 */
void
hpcfb_tv_eraserows(struct hpcfb_devconfig *dc,
		   int row, int nrow, long attr)
{
	struct hpcfb_tvrow *vscn = dc->dc_tvram;
	int cols;
	int i;

	if (vscn == 0)
		return;

	dc->dc_state |= HPCFB_DC_TDRAWING;
	dc->dc_state &= ~HPCFB_DC_TDRAWING;
#ifdef HPCFB_JUMP
	if (row < dc->dc_min_row)
		dc->dc_min_row = row;
	if (row + nrow > dc->dc_max_row)
		dc->dc_max_row = row + nrow;
#endif /* HPCFB_JUMP */

	for (i = 0; i < nrow; i++) {
		cols = vscn[row+i].maxcol;
		if (vscn[row+i].spacecol < cols)
			vscn[row+i].spacecol = cols;
		vscn[row+i].maxcol = -1;
	}
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

void
hpcfb_eraserows(void *cookie, int row, int nrow, long attr)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct hpcfb_softc *sc = dc->dc_sc;
	struct rasops_info *ri = &dc->dc_rinfo;
	int yoff;
	int width;
	int height;

	hpcfb_tv_eraserows(dc, row, nrow, attr);
#ifdef HPCFB_JUMP
	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
		dc->dc_state |= HPCFB_DC_UPDATE;
		return;
	}
#endif /* HPCFB_JUMP */
	if (!IS_DRAWABLE(dc)) {
		return;
	}

	if (ri->ri_bits == NULL)
		return;

	dc->dc_state |= HPCFB_DC_DRAWING;
	if (sc && sc->sc_accessops->erase
	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
		yoff = row * ri->ri_font->fontheight;
		width = dc->dc_cols * ri->ri_font->fontwidth;
		height = nrow * ri->ri_font->fontheight;
		(*sc->sc_accessops->erase)(sc->sc_accessctx,
		    0, yoff, height, width, attr);
	} else
		rasops_emul.eraserows(ri, row, nrow, attr);
	dc->dc_state &= ~HPCFB_DC_DRAWING;
#ifdef HPCFB_JUMP
	hpcfb_check_update(dc);
#endif /* HPCFB_JUMP */
}

/*
 * allocattr
 */
int
hpcfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
{
	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
	struct rasops_info *ri = &dc->dc_rinfo;

	return (rasops_emul.allocattr(ri, fg, bg, flags, attrp));
}