aboutsummaryrefslogtreecommitdiff
path: root/mmux_driver/mmux.c
blob: cec7fa1f6c38b0d4d97c3758dbbeef7917d5bc1e (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
/*================================================
 *               ***************                 *
 *            **      \ | /      **              *
 *          *  ****   ****   ****** *            *
 *         * ---**---**-|-**--**--** *           *
 *        *     **  **  |  *  **  **  *          *
 *        *-----**--**--o-----****----*          *
 *        *     **  **  |     **      *          *
 *         * ---**---**-|--*--**---- *           *
 *          *  ****   ****   ****  *             *
 *            **      / | \     **               *
 *               **************                  *
 *            Copyright (C) 2009 ~ 2010          *
 *===============================================*/
/*-----------------------------------------------* 
    $Author: Kenneth $
    $Date$
    $Revision$
 *-----------------------------------------------*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/pci.h>
#include <linux/fs.h>
#include <linux/ioport.h>
#include <asm/uaccess.h>
#include <linux/spinlock.h>

#include "mmux.h"

#define MMUX_DRIVER_NAME	"media_mux"
//KERN_DEBUG KERN_WARNING
#define MSG(fmt, arg...)		printk(KERN_DEBUG MMUX_DRIVER_NAME":" fmt"\n", ##arg)
#define ERR(fmt, arg...)		printk(KERN_WARNING MMUX_DRIVER_NAME":" fmt"\n", ##arg)


#define MMUX_CARD_MAX		2
#define MMXU_VERSION		"0.0.1.1"

#define MMUX_VENDOR_ID		0x1172
#define MMUX_DEVICE_ID		0x0008

#define MMUX_CPLD_VERSION		0x0304C008
#define MMUX_FPGA_VERSION		0x0304E008
#define MMUX_MCU_VERSION		0x8051A101

#define REGISTER_REGION_NO		0


#define REG_FG_READ		0x01
#define REG_FG_WRITE	0x02



struct tMmux_private_data {
	struct pci_dev* pDev;
	struct cdev		cdev;
	unsigned int	input_ch_total;
	unsigned int 	output_ch_total;
	unsigned int	codec_chip_total;
	int				minor;
	unsigned int	reg_base;
	unsigned int	reg_length;
	spinlock_t		lock;
};



struct tag_MMUX_REG {
	unsigned long offset;
	unsigned long mask;
	unsigned long shift;
	unsigned long fg;	//[1:0] w|r.
};



//===============================================================================//
static const struct tag_MMUX_REG	gt_Reg_A[MMUX_INFO_MAX] = {
	{0x00,	0x00000003,	 0,	REG_FG_READ|REG_FG_WRITE},	//CODEC_0_SRC,
	{0x04,	0x00000003,	 0,	REG_FG_READ|REG_FG_WRITE},	//CODEC_1_SRC,
	{0x08,	0x00000003,	 0,	REG_FG_READ|REG_FG_WRITE},	//CODEC_2_SRC,
	{0x0C,	0x00000003,	 0,	REG_FG_READ|REG_FG_WRITE},	//CODEC_3_SRC,
	{0x10,	0x00000007,	 0,	REG_FG_READ|REG_FG_WRITE},	//VIDEO_OUT_MODE,
	{0x60,	0x0000001F,	 0,	REG_FG_READ				},	//CODEC_0_SRC_RESOLUTION,
	{0x64,	0x0000001F,	 0,	REG_FG_READ				},	//CODEC_1_SRC_RESOLUTION,
	{0x68,	0x0000001F,	 0,	REG_FG_READ				},	//CODEC_2_SRC_RESOLUTION,
	{0x6C,	0x0000001F,	 0,	REG_FG_READ				},	//CODEC_3_SRC_RESOLUTION,
	{0x60,	0x00000100,	 8,	REG_FG_READ				},	//CODEC_0_SRC_HOTPLUG,
	{0x64,	0x00000100,	 8,	REG_FG_READ				},	//CODEC_1_SRC_HOTPLUG,
	{0x68,	0x00000100,	 8,	REG_FG_READ				},	//CODEC_2_SRC_HOTPLUG,
	{0x6C,	0x00000100,	 8,	REG_FG_READ				},	//CODEC_3_SRC_HOTPLUG,
	{0x60,	0x00001000,	12,	REG_FG_READ				},	//CODEC_0_SRC_SCDT,
	{0x64,	0x00001000,	12,	REG_FG_READ				},	//CODEC_1_SRC_SCDT,
	{0x68,	0x00001000,	12,	REG_FG_READ				},	//CODEC_2_SRC_SCDT,
	{0x6C,	0x00001000,	12,	REG_FG_READ				},	//CODEC_3_SRC_SCDT,
	{0x60,	0x00010000,	12,	REG_FG_READ				},	//CODEC_0_SRC_DISABLE,
	{0x64,	0x00010000,	12,	REG_FG_READ				},	//CODEC_1_SRC_DISABLE,
	{0x68,	0x00010000,	12,	REG_FG_READ				},	//CODEC_2_SRC_DISABLE,
	{0x6C,	0x00010000,	12,	REG_FG_READ				},	//CODEC_3_SRC_DISABLE,
	{0x70,	0x0000001F,	 0,				REG_FG_WRITE},	//VIDEO_OUT_RESOLUTION,
	
	{0xF0,	0xFFFFFFFF,	 0,	REG_FG_READ				},	//CPLD_VER,
	{0xF4,	0xFFFFFFFF,	 0,	REG_FG_READ				},	//FPGA_VER,
	{0xEC,	0xFFFFFFFF,	 0,	REG_FG_READ				},	//MCU_VER,
	
};

static const struct tag_MMUX_REG	gtc_IsBusy = {
	0xF8,	0x01,	0x0
};
static const struct tag_MMUX_REG	gtc_Refresh = {
	0xFC,	0xFF,	0x0
};

static const int gicRetryNum = 5;

static int giMmux_card_num = 0;


//===============================================================================//
struct tMmux_private_data *gptPrivate_data[MMUX_CARD_MAX];

static dev_t gtMmux_dev_no;



//===============================================================================//
static int Mmux_InterfaceIsBusy(struct tMmux_private_data *ptPrivate)
{
	unsigned long data;

	data = inl(ptPrivate->reg_base + gtc_IsBusy.offset);
	data = (data & gtc_IsBusy.mask) >> gtc_IsBusy.shift;
	//MSG("IsBusy: offset=0x%lx, data=0x%lx",
	//		gtc_IsBusy.offset, data);

	return (data == 0 ? 0 : 1);
}

static int Mmux_WaitInterfaceFree(struct tMmux_private_data *ptPrivate)
{
	int i;
	int ret = -EBUSY;

	for (i=0; i<gicRetryNum; i++) 
	{
		if (!Mmux_InterfaceIsBusy(ptPrivate)) {
			MSG("WaitInterfaceFree: (%d).", i);
			ret = 0;
			break;
		}
	}

	return ret;
}

static int Mmux_Refresh(struct tMmux_private_data *ptPrivate, unsigned long offset)
{
	int ret = 0;
	unsigned long data;

	//MSG("Refresh: start");
	MSG("Refresh: offset=0x%lx", offset);
	
	if (Mmux_InterfaceIsBusy(ptPrivate)) {
		ret = -EBUSY;
	}
	else {
		data = (offset & gtc_Refresh.mask) >> gtc_Refresh.shift;
		outl(data, ptPrivate->reg_base + gtc_Refresh.offset);
		ret = Mmux_WaitInterfaceFree(ptPrivate);
	}
	
	MSG("Refresh: end(%d)", ret);

	return ret;
}

//===============================================================================//
static int Mmux_IoGetReg(struct tMmux_private_data *ptPrivate, unsigned long arg )
{
	struct MMUX_REG_INFO reg_info;

	
	MSG("IoGetReg: start");

	if (copy_from_user(&reg_info, (void*)arg, sizeof(struct MMUX_REG_INFO))) {
		ERR( "Failed in a copy from user's area.");
		return -EFAULT;
	}
	
	if ((ptPrivate->reg_length >>2 <<2) < reg_info.reg_add ||
		(reg_info.reg_add >>2 <<2) != reg_info.reg_add ) {
		ERR("Failed reg_add:0x%lx", reg_info.reg_add);
		return -EINVAL;
	}
	
	if (Mmux_Refresh(ptPrivate, reg_info.reg_add) != 0) {
		return -EBUSY;
	}
	
	reg_info.reg_data = inl(ptPrivate->reg_base + reg_info.reg_add);
	MSG("reg_base: 0x%x, offset: 0x%lx, data:0x%lx",
		ptPrivate->reg_base, reg_info.reg_add, reg_info.reg_data);
	
	if ( copy_to_user((void*)arg, &reg_info, sizeof(struct MMUX_REG_INFO)) ) {
		ERR("Failed in a copy to user's area.");
		return -EFAULT;
	}
	
	MSG("IoGetReg: success");


	return 0;
}

static int Mmux_IoSetReg(struct tMmux_private_data *ptPrivate, unsigned long arg )
{
	struct MMUX_REG_INFO reg_info;
	int ret;
	
	MSG("IoSetReg: start");

	ret = Mmux_WaitInterfaceFree(ptPrivate);
	if (ret != 0) {
		return ret;
	}

	if (copy_from_user(&reg_info, (void*)arg, sizeof(struct MMUX_REG_INFO))) {
		ERR( "Failed in a copy from user's area.");
		return -EFAULT;
	}
	
	if ((ptPrivate->reg_length >>2 <<2) < reg_info.reg_add ||
		(reg_info.reg_add >>2 <<2) != reg_info.reg_add ) {
		ERR("Failed reg_add:0x%lx", reg_info.reg_add);
		return -EINVAL;
	}

	outl(reg_info.reg_data, ptPrivate->reg_base + reg_info.reg_add);
	MSG("reg_base: 0x%x, offset: 0x%lx, data:0x%lx",
		ptPrivate->reg_base, reg_info.reg_add, reg_info.reg_data);
	
	MSG("IoSetReg: success");


	return 0;
}

static int Mmux_IoGetParam(struct tMmux_private_data *ptPrivate, unsigned long arg)
{
	struct MMUX_PARAM_INFO param_info;
	unsigned long offset;
	unsigned long data;
	int ret;

	MSG("IoGetParam: start");

	if (copy_from_user(&param_info, (void*)arg, sizeof(struct MMUX_PARAM_INFO))) {
		ERR( "Failed in a copy from user's area.");
		return -EFAULT;
	}

	if (param_info.id < 0 || MMUX_INFO_MAX <= param_info.id) {
		ERR( "Failed parameter id!");
		return -EFAULT;
	}

	if (!(gt_Reg_A[param_info.id ].fg | REG_FG_READ)) {
		ERR( "Parameter Can't read!");
		return -EFAULT;
	}

	offset = gt_Reg_A[param_info.id].offset;

	ret = Mmux_Refresh(ptPrivate, offset);
	if (ret != 0) {
		return ret;
	}
	
	data = inl(ptPrivate->reg_base + offset);
	MSG("id:0x%x data: 0x%lx, mask: 0x%lx, shift:0x%lx",
		param_info.id, data, gt_Reg_A[param_info.id].mask, gt_Reg_A[param_info.id].shift);
	param_info.data = (data & gt_Reg_A[param_info.id].mask) >> gt_Reg_A[param_info.id].shift;
	MSG("reg_base: 0x%x, offset: 0x%lx, data:0x%lx",
		ptPrivate->reg_base, offset, param_info.data);
	
	if ( copy_to_user((void*)arg, &param_info, sizeof(struct MMUX_PARAM_INFO)) ) {
		ERR("Failed in a copy to user's area.");
		return -EFAULT;
	}

	MSG("IoGetParam: success");

	return 0;
}

static int Mmux_IoSetParam(struct tMmux_private_data *ptPrivate, unsigned long arg)
{
	struct MMUX_PARAM_INFO param_info;
	unsigned long offset;
	unsigned long data;
	int ret;

	MSG("IoSetParam: start");

	ret = Mmux_WaitInterfaceFree(ptPrivate);
	if (ret != 0) {
		return ret;
	}

	if (copy_from_user(&param_info, (void*)arg, sizeof(struct MMUX_PARAM_INFO))) {
		ERR( "Failed in a copy from user's area.");
		return -EFAULT;
	}

	if (param_info.id < 0 || MMUX_INFO_MAX <= param_info.id) {
		ERR( "Failed parameter id!");
		return -EFAULT;
	}

	if (!(gt_Reg_A[param_info.id ].fg | REG_FG_WRITE)) {
		ERR( "Parameter Can't write!");
		return -EFAULT;
	}

	offset = gt_Reg_A[param_info.id].offset;
	data = inl(ptPrivate->reg_base + offset);
	data &= !gt_Reg_A[param_info.id].mask;
	data |= (param_info.data << gt_Reg_A[param_info.id].shift) & gt_Reg_A[param_info.id].mask;

	outl(data, ptPrivate->reg_base +offset);
	MSG("reg_base: 0x%x, offset: 0x%lx, data:0x%lx",
		ptPrivate->reg_base, offset, data);

	MSG("IoSetParam: success");

	return 0;
}

static int Mmux_IoGetVersion(struct tMmux_private_data *ptPrivate, unsigned long arg)
{
	struct MMUX_VERSION_INFO		version_info;
	char *pVersion;

	MSG("IoGetVersion: start");

	if (copy_from_user(&version_info, (void*)arg, sizeof(struct MMUX_VERSION_INFO))) {
		ERR( "Failed in a copy from user's area.");
		return -EFAULT;
	}

	pVersion = MMXU_VERSION;
	if ( copy_to_user(version_info.driver_ver, pVersion, sizeof(MMXU_VERSION)) ) {
		ERR("Failed in a copy to user's area.");
		return -EFAULT;
	}

	if ( copy_to_user((void*)arg, &version_info, sizeof(struct MMUX_VERSION_INFO)) ) {
		ERR("Failed in a copy to user's area.");
		return -EFAULT;
	}

	MSG("IoGetVersion: success");

	return 0;
}

//===============================================================================//
static int mmux_ioctl(struct inode *inode, struct file *filp,
					unsigned int cmd, unsigned long arg)
{
	struct tMmux_private_data *ptPrivate;
	int ret = 0;
	
	ptPrivate = filp->private_data;

	switch (cmd) {
	case MMUX_IOCG_REG:
		ret = Mmux_IoGetReg(ptPrivate, arg);
		break;
	case MMUX_IOCS_REG:
		ret = Mmux_IoSetReg(ptPrivate, arg);
		break;
	case MMUX_IOCG_PARAM:
		ret = Mmux_IoGetParam(ptPrivate, arg);
		break;
	case MMUX_IOCS_PARAM:
		ret = Mmux_IoSetParam(ptPrivate, arg);
		break;
	case MMUX_IOCG_VERSION:
		ret = Mmux_IoGetVersion(ptPrivate, arg);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

static int mmux_open(struct inode *inode, struct file *filp)
{
	struct tMmux_private_data *ptPrivate;
	
	MSG("Open: start");
	
	ptPrivate = container_of(inode->i_cdev, struct tMmux_private_data, cdev);
	filp->private_data = ptPrivate;

	MSG("minor: %d, private_data:%p", ptPrivate->minor, filp->private_data);
	
	MSG("Open: end");

	return 0;
}

static int mmux_release(struct inode *inode, struct file *filp)
{

	MSG("Release: start");

	MSG("Release: end");

	return 0;
}

static int Mmux_private_data_init(struct tMmux_private_data **ppPrivate,
									struct pci_dev* pDev)
{
	struct tMmux_private_data *ptPrivate;
	
	MSG("private data init: start");

	ptPrivate = kmalloc(sizeof(struct tMmux_private_data), GFP_KERNEL);
	if (ptPrivate == NULL) {
		ERR("kmalloc failed:(%d)", sizeof(struct tMmux_private_data));
		return -ENOMEM;
	}
	
	ptPrivate->minor = giMmux_card_num;
	giMmux_card_num++;
	
	ptPrivate->input_ch_total = 4;
	ptPrivate->output_ch_total = 1;
	ptPrivate->codec_chip_total = 4;
	ptPrivate->pDev = pDev;

	ptPrivate->reg_base = pci_resource_start(pDev, REGISTER_REGION_NO);
	ptPrivate->reg_length = pci_resource_len(pDev, REGISTER_REGION_NO);
	MSG("base: 0x%x, len: 0x%x", ptPrivate->reg_base, ptPrivate->reg_length);

	if( !request_region(ptPrivate->reg_base, ptPrivate->reg_length, MMUX_DRIVER_NAME)) {
		ERR("Request region failed");
		kfree(ptPrivate);

		return -EBUSY;
	}
	
	spin_lock_init(&ptPrivate->lock);

	MSG("Region %d: 0x%x 0x%x 0x%x 0x%x 0x%x", 
		REGISTER_REGION_NO, inl(ptPrivate->reg_base),
		inl(ptPrivate->reg_base +4), inl(ptPrivate->reg_base +8),
		inl(ptPrivate->reg_base +12), inl(ptPrivate->reg_base +16) );
	
	gptPrivate_data[ptPrivate->minor] = ptPrivate;
	*ppPrivate = ptPrivate;
	
	MSG("private data init: success");

	return 0;
}

static void Mmux_private_data_release(struct tMmux_private_data *ptPrivate)
{
	gptPrivate_data[ptPrivate->minor] = NULL;

	release_region(ptPrivate->reg_base, ptPrivate->reg_length);


	kfree(ptPrivate);
}

static struct file_operations mmux_fops =
{
	.owner		= THIS_MODULE,
	.ioctl		= mmux_ioctl,
	.open		= mmux_open,
	.release	= mmux_release,
};


//===============================================================================//
static struct pci_device_id mmux_tbl[] __devinitdata = {
	{MMUX_VENDOR_ID, MMUX_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{ 0,},
};
MODULE_DEVICE_TABLE(pci, mmux_tbl);

static int __devinit mmux_probe(struct pci_dev* pDev, const struct pci_device_id* pId)
{
	int result;
	struct tMmux_private_data *ptPrivate;
	dev_t dev_no;
	
	MSG("Probe: start");

	result = pci_enable_device(pDev);
	if(result != 0){
		ERR("pci_enable_device() error: %d", result);
		return result;
	}
	
	result = Mmux_private_data_init(&ptPrivate, pDev);
	if (result < 0) {
		ERR("private data init failed (%d)", result);
		pci_disable_device(pDev);
		return result;
	}
	
	pci_set_master(ptPrivate->pDev);					
	pci_set_drvdata(ptPrivate->pDev, ptPrivate);

	cdev_init( &ptPrivate->cdev, &mmux_fops);
	dev_no = MKDEV(MAJOR(gtMmux_dev_no), ptPrivate->minor);
	ptPrivate->cdev.owner = THIS_MODULE;
	ptPrivate->cdev.ops = &mmux_fops;
	result = cdev_add(&ptPrivate->cdev, dev_no, 1);
	if (result < 0) {
		ERR("failed to add cdev (%d)", result);
		return result;
	}

	MSG("Probe: success");
	return 0;
}

static void __devexit mmux_remove(struct pci_dev* pDev)
{
	struct tMmux_private_data *ptPrivate;

	MSG("Remove: start");

	ptPrivate = pci_get_drvdata(pDev);
	
	pci_disable_device(pDev);
	
	cdev_del(&ptPrivate->cdev);
	Mmux_private_data_release(ptPrivate);
	
	MSG("Remove: end");
}

static struct pci_driver mmux_driver = {
	.name = MMUX_DRIVER_NAME,
	.id_table = mmux_tbl,
	.probe = mmux_probe,
	.remove = __devexit_p(mmux_remove),
};

//===============================================================================//
static int __init mmux_init(void)
{
	int result;

	MSG("Module Init: start");
	
	result = alloc_chrdev_region(&gtMmux_dev_no, 0, MMUX_CARD_MAX, MMUX_DRIVER_NAME);
	if (result < 0) {
		ERR("Failed to alloc chrdev:(%d)", result);
		return result;
	}
	
	result = pci_register_driver(&mmux_driver);
	
	if (result < 0) {
		ERR("Register pci driver faildd: %d",result);
		unregister_chrdev_region(gtMmux_dev_no, MMUX_CARD_MAX);
		return result;
	}

	MSG("Module Init: Success");
	
	return result;
}

static void __exit mmux_exit(void)
{
	MSG("Module exit: start");

	unregister_chrdev_region(gtMmux_dev_no, MMUX_CARD_MAX);	
	pci_unregister_driver(&mmux_driver);

	MSG("Module exit: End");
}

MODULE_LICENSE("GPL");
MODULE_VERSION(MMXU_VERSION);

module_init(mmux_init);
module_exit(mmux_exit);