summaryrefslogtreecommitdiff
path: root/src/apm_rush.c
blob: 28cfe506cc49a568eff64dcf509386e819917b30 (plain)
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
/* $XdotOrg: xc/programs/Xserver/hw/xfree86/drivers/apm/apm_rush.c,v 1.2 2004/04/23 19:25:03 eich Exp $ */
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/apm/apm_rush.c,v 1.11tsi Exp $ */
/*
 * Copyright Loïc Grenié 1999
 */

#include "apm.h"
#include "xaalocal.h"

extern DriverRec APM;

static Bool		RushDestroyPixmap(PixmapPtr);
static __inline__ void	__xf86UnlockPixmap(ApmPtr, PixmapLinkPtr);
static int		xf86RushLockPixmap(int, PixmapPtr);
static void		xf86RushUnlockPixmap(int, PixmapPtr);
static void		xf86RushUnlockAllPixmaps(void);

static Bool RushDestroyPixmap(PixmapPtr pPix)
{
    APMDECL(xf86Screens[pPix->drawable.pScreen->myNum]);
    ApmPixmapPtr pPriv = APM_GET_PIXMAP_PRIVATE(pPix);

    if (pPriv->num)
	pApm->RushY[pPriv->num - 1] = 0;
    return (*pApm->DestroyPixmap)(pPix);
}

static PixmapPtr RushCreatePixmap(ScreenPtr pScreen, int w, int h, int depth)
{
    APMDECL(xf86Screens[pScreen->myNum]);
    PixmapPtr pPix = (*pApm->CreatePixmap)(pScreen, w, h, depth);
    ApmPixmapPtr pPriv = APM_GET_PIXMAP_PRIVATE(pPix);

    bzero(pPriv, sizeof(*pPriv));
    return pPix;
}

static int
xf86RushLockPixmap(int scrnIndex, PixmapPtr pix)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    APMDECL(pScrn);
    XAAInfoRecPtr infoRec = GET_XAAINFORECPTR_FROM_SCRNINFOPTR(pScrn);
    ApmPixmapPtr pApmPriv = APM_GET_PIXMAP_PRIVATE(pix);
    XAAPixmapPtr pXAAPriv = XAA_GET_PIXMAP_PRIVATE(pix);
    FBAreaPtr area = pXAAPriv->offscreenArea;
    int	p2, width = (pScrn->displayWidth * pScrn->bitsPerPixel) / 8;

    if (pScrn->drv != &APM || pApm->Chipset != AT3D)
	return 0;
    pApm->apmLock = TRUE;
    pApmPriv->num = 0;
    if (area && pApm->pixelStride) {
	if (area->RemoveAreaCallback) {
	    (*area->RemoveAreaCallback)(area);
	    xf86FreeOffscreenArea(area);
	    area = NULL;
	}
	else
	    return 0;
    }
    if (area) {
	/*
	 * 1) Make it unmovable so that XAA won't know we're playing
	 *    with the cache.
	 * 2) Play musical chairs if needed.
	 */
	pApmPriv->MoveAreaCallback	= area->MoveAreaCallback;
	area->MoveAreaCallback		= NULL;
	pApmPriv->RemoveAreaCallback	= area->RemoveAreaCallback;
	area->RemoveAreaCallback	= NULL;
	pApmPriv->devPriv		= area->devPrivate.ptr;
    }
    else {
	pApmPriv->MoveAreaCallback	= NULL;
	pApmPriv->RemoveAreaCallback	= NULL;
	pApmPriv->devPriv		= NULL;
    }
    if (pApm->pixelStride || !area ||
	(((pix->drawable.x + pScrn->displayWidth * pix->drawable.y) *
					    pScrn->bitsPerPixel) & 32767)) {
	int		p1, i;

	/*
	 * Not aligned on a 4KB boundary, need to move it around.
	 */
	if (area)
	    xf86FreeOffscreenArea(area);
	if (pApm->pixelStride) {
	    area = xf86AllocateLinearOffscreenArea(pScrn->pScreen,
			    ((pix->drawable.width * pix->drawable.height *
			    pix->drawable.depth) / pScrn->bitsPerPixel) +
				4095,
			    pScrn->displayWidth, NULL, NULL, NULL);
	    if (!area) {
		xf86PurgeUnlockedOffscreenAreas(pScrn->pScreen);
		area = xf86AllocateLinearOffscreenArea(pScrn->pScreen,
			    ((pix->drawable.width * pix->drawable.height *
			    pix->drawable.depth) / pScrn->bitsPerPixel) +
				4095,
			    pScrn->displayWidth, NULL, NULL, NULL);
	    }
	    if (area) {
		if (!pApmPriv->devPriv) {
		    PixmapLinkPtr pLink;
		    ScreenPtr pScreen = pScrn->pScreen;
		    PixmapPtr pScreenPix;

		    pLink = xalloc(sizeof(PixmapLink));
		    if (!pLink) {
			xf86FreeOffscreenArea(area);
			return 0;
		    }
		    pXAAPriv->flags |= OFFSCREEN;
		    pix->devKind = pApm->pixelStride;
		    pScreenPix = (*pScreen->GetScreenPixmap)(pScreen);
		    pix->devPrivate.ptr = pScreenPix->devPrivate.ptr;
		    pLink->next = infoRec->OffscreenPixmaps;
		    pLink->pPix = pix;
		    infoRec->OffscreenPixmaps = pLink;
		}
		for (i = 0; i < 7; i++)
		    if (!pApm->RushY[i])
			break;
		pApmPriv->num = i + 1;
		pix->drawable.y = area->box.y1 + (i+1)*pApm->CurrentLayout.Scanlines;
		pApm->RushY[i] = area->box.y1;
		pix->drawable.x = (32768 - (((area->box.x1 + pScrn->displayWidth * area->box.y1) * pScrn->bitsPerPixel) & 32767)) / pApm->CurrentLayout.bitsPerPixel;
		if (pix->drawable.x == 32768 / pApm->CurrentLayout.bitsPerPixel)
		    pix->drawable.x = 0;
	    }
	}
	else {
	    p2 = 1;
	    while (!(p2 & width))
		p2 *= 2;
	    p1 = 4096 / p2 - 1;
	    switch(pScrn->bitsPerPixel) {
	    case 16:
		p2 /= 2;
		break;
	    case 32:
		p2 /= 4;
		break;
	    }
	    area = xf86AllocateOffscreenArea(pScrn->pScreen,
		    (pix->drawable.width * pix->drawable.bitsPerPixel) /
				    pScrn->bitsPerPixel,
		    pix->drawable.height + p1,
		    p2, NULL, NULL, pApmPriv->devPriv);
	    if (!area) {
		xf86PurgeUnlockedOffscreenAreas(pScrn->pScreen);
		area = xf86AllocateOffscreenArea(pScrn->pScreen,
			(pix->drawable.width * pix->drawable.bitsPerPixel) /
					pScrn->bitsPerPixel,
			pix->drawable.height + p1,
			p2, NULL, NULL, pApmPriv->devPriv);
	    }
	    if (area) {
		int	devKind = (pScrn->bitsPerPixel * pScrn->displayWidth) / 8;
		int	off = devKind * p1, h;
		int	goal = (-area->box.x1 * (pScrn->bitsPerPixel >> 3) - area->box.y1 * devKind) & 4095;

		if (!pApmPriv->devPriv) {
		    PixmapLinkPtr pLink;
		    ScreenPtr pScreen = pScrn->pScreen;
		    PixmapPtr pScreenPix;

		    pLink = xalloc(sizeof(PixmapLink));
		    if (!pLink) {
			xf86FreeOffscreenArea(area);
			return 0;
		    }
		    pXAAPriv->flags |= OFFSCREEN;
		    pix->devKind = pApm->CurrentLayout.bytesPerScanline;
		    pScreenPix = (*pScreen->GetScreenPixmap)(pScreen);
		    pix->devPrivate.ptr = pScreenPix->devPrivate.ptr;
		    pLink->next = infoRec->OffscreenPixmaps;
		    pLink->pPix = pix;
		    infoRec->OffscreenPixmaps = pLink;
		}
		pix->drawable.x = area->box.x1;
		for (h = p1; h >= 0; h--, off -= devKind)
		    if ((off & 4095) == goal)
			break;
		for (i = 0; i < 7; i++)
		    if (!pApm->RushY[i])
			break;
		pApmPriv->num = i + 1;
		pix->drawable.y = area->box.y1 + h + (i+1)*pApm->CurrentLayout.Scanlines;
		pApm->RushY[i] = area->box.y1 + h;
	    }
	}
	if (!area && (pXAAPriv->flags & OFFSCREEN)) {
	    /*
	     * Failed, return the old one
	     */
	    switch(pScrn->bitsPerPixel) {
		case 24:
		case  8:	p2 = 4; break;
		case 16:	p2 = 2; break;
		case 32:	p2 = 1; break;
		default:	p2 = 0; break;
	    }
	    pXAAPriv->offscreenArea =
		    area = xf86AllocateOffscreenArea(pScrn->pScreen,
				pix->drawable.width, pix->drawable.height,
				p2,
				pApmPriv->MoveAreaCallback,
				pApmPriv->RemoveAreaCallback,
				pApmPriv->devPriv);
	    /* The allocate can not fail: we just removed the old one. */
	    pix->drawable.x = area->box.x1;
	    pix->drawable.y = area->box.y1;
	}
	if (!area)
	    return 0;
	pXAAPriv->offscreenArea = area;
    }
    return pApm->LinAddress +
	    ((pix->drawable.x + pScrn->displayWidth *
	    (pix->drawable.y % pApm->CurrentLayout.Scanlines)) * pApm->CurrentLayout.bitsPerPixel) / 8;
}

static __inline__ void
__xf86UnlockPixmap(ApmPtr pApm, PixmapLinkPtr pLink)
{
    PixmapPtr pix = pLink->pPix;
    ApmPixmapPtr pApmPriv = APM_GET_PIXMAP_PRIVATE(pix);
    XAAPixmapPtr pXAAPriv = XAA_GET_PIXMAP_PRIVATE(pix);
    FBAreaPtr area = pXAAPriv->offscreenArea;
    int i;

    if (!area)
	area = pLink->area;
    if ((pXAAPriv->flags & OFFSCREEN) && !area->MoveAreaCallback && !area->RemoveAreaCallback) {
	area->MoveAreaCallback		= pApmPriv->MoveAreaCallback;
	area->RemoveAreaCallback	= pApmPriv->RemoveAreaCallback;
	area->devPrivate.ptr		= pApmPriv->devPriv;
    }
    i = pApmPriv->num;
    if (i) {
	pApm->RushY[i - 1] = 0;
	pix->drawable.y %= pApm->CurrentLayout.Scanlines;
    }
}

static void
xf86RushUnlockPixmap(int scrnIndex, PixmapPtr pix)
{
    APMDECL(xf86Screens[scrnIndex]);
    PixmapLinkPtr pLink = GET_XAAINFORECPTR_FROM_SCREEN(xf86Screens[scrnIndex]->pScreen)->OffscreenPixmaps;

    if (xf86Screens[scrnIndex]->drv != &APM || pApm->Chipset != AT3D)
	return;
    if (pApm->apmLock) {
	/*
	 * This is just an attempt, because Daryll is tampering with MY
	 * registers.
	 */
	if (!pApm->noLinear) {
	    CARD8	db;

	    db = RDXB(0xDB);
	    WRXB(0xDB, (db & 0xF4) |  0x0A);
	    ApmWriteSeq(0x1B, 0x20);
	    ApmWriteSeq(0x1C, 0x2F);
	}
	else {
	    CARD8	db;

	    db = RDXB_IOP(0xDB);
	    WRXB_IOP(0xDB, (db & 0xF4) |  0x0A);
	    wrinx(pApm->xport, 0x1B, 0x20);
	    wrinx(pApm->xport, 0x1C, 0x2F);
	}
	pApm->apmLock = FALSE;
    }
    while (pLink && pLink->pPix != pix)
	pLink = pLink->next;
    if (pLink)
	__xf86UnlockPixmap(pApm, pLink);
}

static void
xf86RushUnlockAllPixmaps()
{
    int		scrnIndex;

    for (scrnIndex = 0; scrnIndex < screenInfo.numScreens; scrnIndex++) {
	ScrnInfoPtr	pScrn = xf86Screens[scrnIndex];
	APMDECL(pScrn);
	PixmapLinkPtr pLink = GET_XAAINFORECPTR_FROM_SCREEN(pScrn->pScreen)->OffscreenPixmaps;

	if (pScrn->drv != &APM || pApm->Chipset != AT3D)
	    continue;
	while(pLink) {
	    __xf86UnlockPixmap(pApm, pLink);	
	    pLink = pLink->next;
	}    
    }
}

/*

Copyright (c) 1998 Daryll Strauss

*/

#define NEED_REPLIES
#define NEED_EVENTS
#include "X.h"
#include "Xproto.h"
#include "misc.h"
#include "dixstruct.h"
#include "extnsionst.h"
#include "scrnintstr.h"
#define _XF86RUSH_SERVER_
#include "xf86rushstr.h"

static unsigned char RushReqCode = 0;
static int RushErrorBase;

static DISPATCH_PROC(ProcXF86RushDispatch);
static DISPATCH_PROC(ProcXF86RushQueryVersion);
static DISPATCH_PROC(ProcXF86RushLockPixmap);
static DISPATCH_PROC(ProcXF86RushUnlockPixmap);
static DISPATCH_PROC(ProcXF86RushUnlockAllPixmaps);
static DISPATCH_PROC(ProcXF86RushSetCopyMode);
#if 0
static DISPATCH_PROC(ProcXF86RushSetPixelStride);
#endif
static DISPATCH_PROC(ProcXF86RushOverlayPixmap);
static DISPATCH_PROC(ProcXF86RushStatusRegOffset);
static DISPATCH_PROC(ProcXF86RushAT3DEnableRegs);
static DISPATCH_PROC(ProcXF86RushAT3DDisableRegs);

static int rush_ext_generation = -1;

static DISPATCH_PROC(SProcXF86RushDispatch);

static void XF86RushResetProc(ExtensionEntry* extEntry);

void
XFree86RushExtensionInit(ScreenPtr pScreen)
{
    ExtensionEntry* extEntry;

#ifdef PANORAMIX
    if (!noPanoramiXExtension)
	return;
#endif
    if (rush_ext_generation == serverGeneration) {
	if (xf86Screens[pScreen->myNum]->drv == &APM &&
		APMPTR(xf86Screens[pScreen->myNum])->Chipset == AT3D) {
	    pScreen->CreatePixmap	= RushCreatePixmap;
	    pScreen->DestroyPixmap	= RushDestroyPixmap;
	}
	return;
    }
    rush_ext_generation = serverGeneration;
    if ((extEntry = AddExtension(XF86RUSHNAME,
				XF86RushNumberEvents,
				XF86RushNumberErrors,
				ProcXF86RushDispatch,
				SProcXF86RushDispatch,
				XF86RushResetProc,
				StandardMinorOpcode))) {
	RushReqCode = (unsigned char)extEntry->base;
	RushErrorBase = extEntry->errorBase;
	if (xf86Screens[pScreen->myNum]->drv == &APM &&
		APMPTR(xf86Screens[pScreen->myNum])->Chipset == AT3D) {
	    pScreen->CreatePixmap	= RushCreatePixmap;
	    pScreen->DestroyPixmap	= RushDestroyPixmap;
	}
    }
    else {
	pScreen->CreatePixmap	= APMPTR(xf86Screens[pScreen->myNum])->CreatePixmap;
	pScreen->DestroyPixmap	= APMPTR(xf86Screens[pScreen->myNum])->DestroyPixmap;
    }
}

/*ARGSUSED*/
static void
XF86RushResetProc (ExtensionEntry *extEntry)
{
}

static int
ProcXF86RushQueryVersion(register ClientPtr client)
{
    xXF86RushQueryVersionReply rep;
    register int n;

    REQUEST_SIZE_MATCH(xXF86RushQueryVersionReq);
    rep.type = X_Reply;
    rep.length = 0;
    rep.sequenceNumber = client->sequence;
    rep.majorVersion = XF86RUSH_MAJOR_VERSION;
    rep.minorVersion = XF86RUSH_MINOR_VERSION;
    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
    	swapl(&rep.length, n);
    }
    WriteToClient(client, sz_xXF86RushQueryVersionReply, (char *)&rep);
    return (client->noClientException);
}

static int
ProcXF86RushLockPixmap(register ClientPtr client)
{
  REQUEST(xXF86RushLockPixmapReq);
  xXF86RushLockPixmapReply rep;
  PixmapPtr pix;

  if (stuff->screen > screenInfo.numScreens)
    return BadValue;

  REQUEST_SIZE_MATCH(xXF86RushLockPixmapReq);
  rep.type = X_Reply;
  rep.length = 0;
  rep.sequenceNumber = client->sequence;
  pix = (PixmapPtr)SecurityLookupIDByType(client,
					  stuff->pixmap, RT_PIXMAP, 
					  SecurityReadAccess);
  rep.addr = xf86RushLockPixmap(stuff->screen, pix);

  WriteToClient(client, SIZEOF(xXF86RushLockPixmapReply), (char*)&rep);
  return client->noClientException;
}

static int
ProcXF86RushUnlockPixmap(register ClientPtr client)
{
  REQUEST(xXF86RushUnlockPixmapReq);
  PixmapPtr pix;

  if (stuff->screen > screenInfo.numScreens)
    return BadValue;

  REQUEST_SIZE_MATCH(xXF86RushUnlockPixmapReq);
  pix = (PixmapPtr)SecurityLookupIDByType(client,
					  stuff->pixmap, RT_PIXMAP, 
					  SecurityReadAccess);
  xf86RushUnlockPixmap(stuff->screen, pix);
  return client->noClientException;
}

static int
ProcXF86RushUnlockAllPixmaps(register ClientPtr client)
{

  REQUEST_SIZE_MATCH(xXF86RushUnlockAllPixmapsReq);
  xf86RushUnlockAllPixmaps();
  return client->noClientException;
}

static int
ProcXF86RushSetCopyMode(register ClientPtr client)
{
  REQUEST(xXF86RushSetCopyModeReq);

  REQUEST_SIZE_MATCH(xXF86RushSetCopyModeReq);
  APMPTR(xf86Screens[stuff->screen])->CopyMode = stuff->CopyMode;
  return client->noClientException;
}

#if 0
static int
ProcXF86RushSetPixelStride(register ClientPtr client)
{
  REQUEST(xXF86RushSetPixelStrideReq);

  REQUEST_SIZE_MATCH(xXF86RushSetPixelStrideReq);
  APMPTR(xf86Screens[stuff->screen])->pixelStride = stuff->PixelStride;
  return client->noClientException;
}
#endif

int
ProcXF86RushDispatch (register ClientPtr client)
{
    REQUEST(xReq);

    if (!LocalClient(client))
	return RushErrorBase + XF86RushClientNotLocal;

    switch (stuff->data)
    {
    case X_XF86RushQueryVersion:
	return ProcXF86RushQueryVersion(client);
    case X_XF86RushLockPixmap:
        return ProcXF86RushLockPixmap(client);
    case X_XF86RushUnlockPixmap:
        return ProcXF86RushUnlockPixmap(client);
    case X_XF86RushUnlockAllPixmaps:
        return ProcXF86RushUnlockAllPixmaps(client);
    case X_XF86RushSetCopyMode:
        return ProcXF86RushSetCopyMode(client);
#if 0
    case X_XF86RushSetPixelStride:
        return ProcXF86RushSetPixelStride(client);
#endif
    case X_XF86RushOverlayPixmap:
	return ProcXF86RushOverlayPixmap(client);
    case X_XF86RushStatusRegOffset:
	return ProcXF86RushStatusRegOffset(client);
    case X_XF86RushAT3DEnableRegs:
	return ProcXF86RushAT3DEnableRegs(client);
    case X_XF86RushAT3DDisableRegs:
	return ProcXF86RushAT3DDisableRegs(client);
    default:
	return BadRequest;
    }
}

int
SProcXF86RushDispatch (register ClientPtr client)
{
    return RushErrorBase + XF86RushClientNotLocal;
}

#include "xvdix.h"
/*
 * The one below is just a subtle modification of ProcXvShmPutImage
 */

static int 
ProcXF86RushOverlayPixmap(ClientPtr client)
{
    DrawablePtr		pDraw;
    ScrnInfoPtr		pScrn;
    PixmapPtr		pPixmap;
    XvPortPtr		pPort;
    XvImagePtr		pImage = NULL;
    GCPtr		pGC;
    int			status, i;
    unsigned char	*offset;
    ApmPtr		pApm;
    ApmPixmapPtr	pPriv;
    REQUEST(xXF86RushOverlayPixmapReq);

    REQUEST_SIZE_MATCH(xXF86RushOverlayPixmapReq);

    VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);

    pScrn = xf86Screens[pDraw->pScreen->myNum];
    pApm = APMPTR(pScrn);
    if (pScrn->drv != &APM || pApm->Chipset != AT3D)
	return (_XvBadPort);

    if(!(pPort = LOOKUP_PORT(stuff->port, client) )) {
	client->errorValue = stuff->port;
	return (_XvBadPort);
    }

    if (pPort->id != stuff->port) {
	if ((status = (*pPort->pAdaptor->ddAllocatePort)(stuff->port, pPort, &pPort)) != Success) {
	    client->errorValue = stuff->port;
	    return (status);
	}
    }

    if (!(pPort->pAdaptor->type & XvImageMask) ||
	!(pPort->pAdaptor->type & XvInputMask)) {
	client->errorValue = stuff->port;
	return (BadMatch);
    }

    status = XVCALL(diMatchPort)(pPort, pDraw);
    if (status != Success)
	return status;

    pPixmap = (PixmapPtr)SecurityLookupIDByType(client,
					stuff->pixmap, RT_PIXMAP, 
					SecurityReadAccess);
    if (!pPixmap) {
	client->errorValue = stuff->pixmap;
	return (BadPixmap);
    }
    status = XVCALL(diMatchPort)(pPort, (DrawablePtr)pPixmap);
    if (status != Success)
	return status;
    pPriv = APM_GET_PIXMAP_PRIVATE(pPixmap);
    pApm = APMPTR(pScrn);
    if (pPriv->num == 0) {
	client->errorValue = stuff->pixmap;
	return (BadMatch);
    }
    offset = (unsigned char *)pApm->FbBase +
	    pApm->RushY[pPriv->num - 1] * pApm->CurrentLayout.bytesPerScanline +
	    pPixmap->drawable.x * pScrn->bitsPerPixel / 8;

    for(i = 0; i < pPort->pAdaptor->nImages; i++) {
	if(pPort->pAdaptor->pImages[i].id == stuff->id) {
	    pImage = &(pPort->pAdaptor->pImages[i]);
	    break;
	}
    }

    if(!pImage)
	return BadMatch;

    pApm->PutImageStride = pPixmap->devKind;
    status = XVCALL(diPutImage)(client, pDraw, pPort, pGC,
				stuff->src_x, stuff->src_y,
				stuff->src_w, stuff->src_h,
				stuff->drw_x, stuff->drw_y,
				stuff->drw_w, stuff->drw_h,
				pImage, offset, TRUE,
				pPixmap->drawable.width,
				pPixmap->drawable.height);
    pApm->PutImageStride = 0;

    return status;
}

static int
ProcXF86RushStatusRegOffset(ClientPtr client)
{
    int		scrnIndex;
    ScrnInfoPtr	pScrn;
    ApmPtr	pApm;
    REQUEST(xXF86RushStatusRegOffsetReq);
    xXF86RushStatusRegOffsetReply rep;
    register int n;

    REQUEST_SIZE_MATCH(xXF86RushStatusRegOffsetReq);
    scrnIndex = stuff->screen;
    if (scrnIndex < 0 || scrnIndex > screenInfo.numScreens)
	return BadValue;
    pScrn = xf86Screens[scrnIndex];
    pApm = APMPTR(pScrn);
    if (pScrn->drv != &APM || pApm->Chipset != AT3D)
	return BadMatch;
    rep.type = X_Reply;
    rep.length = 0;
    rep.sequenceNumber = client->sequence;
    rep.offset = 0xFFEDF4;
    if (client->swapped) {
    	swapl(&rep.offset, n);
    }
    WriteToClient(client, sz_xXF86RushStatusRegOffsetReply, (char *)&rep);

    return client->noClientException;
}

static int
ProcXF86RushAT3DEnableRegs(ClientPtr client)
{
    u32		db, tmp;
    int		scrnIndex;
    ScrnInfoPtr	pScrn;
    ApmPtr	pApm;
    REQUEST(xXF86RushAT3DEnableRegsReq);

    REQUEST_SIZE_MATCH(xXF86RushAT3DEnableRegsReq);
    scrnIndex = stuff->screen;
    if (scrnIndex < 0 || scrnIndex > screenInfo.numScreens)
	return BadValue;
    pScrn = xf86Screens[scrnIndex];
    pApm = APMPTR(pScrn);
    if (pScrn->drv != &APM || pApm->Chipset != AT3D)
	return BadMatch;
    pApm->Rush = 0x04;
    if (!pApm->noLinear) {
	db = RDXL(0xDB);
	WRXL(0xDB, db | 0x04);
	WRXB(0x110, 0x03);
	tmp = RDXB(0x1F0);
	WRXB(0x1F0, tmp | 0xD0);
	tmp = RDXB(0x1F1);
	WRXB(0x1F1, (tmp & ~0xC0) | 0x10);
	tmp = RDXB(0x1F2);
	WRXB(0x1F2, tmp | 0x10);
    }
    else {
	db = RDXL(0xDB);
	WRXL_IOP(0xDB, db | 0x04);
	WRXB_IOP(0x110, 0x03);
	tmp = RDXB_IOP(0x1F0);
	WRXB_IOP(0x1F0, tmp | 0xD0);
	tmp = RDXB_IOP(0x1F1);
	WRXB_IOP(0x1F1, (tmp & ~0xC0) | 0x10);
	tmp = RDXB_IOP(0x1F2);
	WRXB_IOP(0x1F2, tmp | 0x10);
    }

    return client->noClientException;
}

static int
ProcXF86RushAT3DDisableRegs(ClientPtr client)
{
    u32		db, tmp;
    int		scrnIndex;
    ScrnInfoPtr	pScrn;
    ApmPtr	pApm;
    REQUEST(xXF86RushAT3DDisableRegsReq);

    REQUEST_SIZE_MATCH(xXF86RushAT3DDisableRegsReq);
    scrnIndex = stuff->screen;
    if (scrnIndex < 0 || scrnIndex > screenInfo.numScreens)
	return BadValue;
    pScrn = xf86Screens[scrnIndex];
    pApm = APMPTR(pScrn);
    if (pScrn->drv != &APM || pApm->Chipset != AT3D)
	return BadMatch;
    if (!pApm->noLinear) {
	tmp = RDXB(0x1F2);
	WRXB(0x1F2, tmp & ~0x10);
	tmp = RDXB(0x1F0);
	WRXB(0x1F0, tmp & ~0xD0);
	WRXB(0x110, 0);
	pApm->Rush = 0x00;
	db = RDXL(0xDB);
	WRXL(0xDB, db & ~0x04);
    }
    else {
	tmp = RDXB_IOP(0x1F2);
	WRXB_IOP(0x1F2, tmp & ~0x10);
	tmp = RDXB_IOP(0x1F0);
	WRXB_IOP(0x1F0, tmp & ~0xD0);
	WRXB_IOP(0x110, 0);
	pApm->Rush = 0x00;
	db = RDXL_IOP(0xDB);
	WRXL_IOP(0xDB, db & ~0x04);
    }

    return client->noClientException;
}