aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ps3/ps3av.c
blob: d318eabce3c32e012ad64619a07f2a5403657489 (plain) (blame)
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
/*
 * Copyright (C) 2006 Sony Computer Entertainment Inc.
 * Copyright 2006, 2007 Sony Corporation
 *
 * AV backend support for PS3
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published
 * by the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/notifier.h>
#include <linux/reboot.h>
#include <linux/kernel.h>
#include <linux/ioctl.h>

#include <asm/firmware.h>
#include <asm/lv1call.h>
#include <asm/ps3av.h>
#include <asm/ps3.h>

#include "vuart.h"

#define BUFSIZE          4096	/* vuart buf size */
#define PS3AV_BUF_SIZE   512	/* max packet size */

static int timeout = 5000;	/* in msec ( 5 sec ) */
module_param(timeout, int, 0644);

static struct ps3av ps3av;

static struct ps3_vuart_port_device ps3av_dev = {
	.match_id = PS3_MATCH_ID_AV_SETTINGS
};

/* color space */
#define YUV444 PS3AV_CMD_VIDEO_CS_YUV444_8
#define RGB8   PS3AV_CMD_VIDEO_CS_RGB_8
/* format */
#define XRGB   PS3AV_CMD_VIDEO_FMT_X8R8G8B8
/* aspect */
#define A_N    PS3AV_CMD_AV_ASPECT_4_3
#define A_W    PS3AV_CMD_AV_ASPECT_16_9
static const struct avset_video_mode {
	u32 cs;
	u32 fmt;
	u32 vid;
	u32 aspect;
	u32 x;
	u32 y;
	u32 interlace;
	u32 freq;
} video_mode_table[] = {
	{     0, }, /* auto */
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480I,       A_N,  720,  480, 1, 60},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480P,       A_N,  720,  480, 0, 60},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_60HZ,  A_N, 1280,  720, 0, 60},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_60HZ, A_W, 1920, 1080, 1, 60},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_60HZ, A_W, 1920, 1080, 0, 60},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576I,       A_N,  720,  576, 1, 50},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576P,       A_N,  720,  576, 0, 50},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_50HZ,  A_N, 1280,  720, 0, 50},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_50HZ, A_W, 1920, 1080, 1, 50},
	{YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_50HZ, A_W, 1920, 1080, 0, 50},
	{  RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WXGA,       A_W, 1280,  768, 0, 60},
	{  RGB8, XRGB, PS3AV_CMD_VIDEO_VID_SXGA,       A_N, 1280, 1024, 0, 60},
	{  RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WUXGA,      A_W, 1920, 1200, 0, 60},
};

/* supported CIDs */
static u32 cmd_table[] = {
	/* init */
	PS3AV_CID_AV_INIT,
	PS3AV_CID_AV_FIN,
	PS3AV_CID_VIDEO_INIT,
	PS3AV_CID_AUDIO_INIT,

	/* set */
	PS3AV_CID_AV_ENABLE_EVENT,
	PS3AV_CID_AV_DISABLE_EVENT,

	PS3AV_CID_AV_VIDEO_CS,
	PS3AV_CID_AV_VIDEO_MUTE,
	PS3AV_CID_AV_VIDEO_DISABLE_SIG,
	PS3AV_CID_AV_AUDIO_PARAM,
	PS3AV_CID_AV_AUDIO_MUTE,
	PS3AV_CID_AV_HDMI_MODE,
	PS3AV_CID_AV_TV_MUTE,

	PS3AV_CID_VIDEO_MODE,
	PS3AV_CID_VIDEO_FORMAT,
	PS3AV_CID_VIDEO_PITCH,

	PS3AV_CID_AUDIO_MODE,
	PS3AV_CID_AUDIO_MUTE,
	PS3AV_CID_AUDIO_ACTIVE,
	PS3AV_CID_AUDIO_INACTIVE,
	PS3AV_CID_AVB_PARAM,

	/* get */
	PS3AV_CID_AV_GET_HW_CONF,
	PS3AV_CID_AV_GET_MONITOR_INFO,

	/* event */
	PS3AV_CID_EVENT_UNPLUGGED,
	PS3AV_CID_EVENT_PLUGGED,
	PS3AV_CID_EVENT_HDCP_DONE,
	PS3AV_CID_EVENT_HDCP_FAIL,
	PS3AV_CID_EVENT_HDCP_AUTH,
	PS3AV_CID_EVENT_HDCP_ERROR,

	0
};

#define PS3AV_EVENT_CMD_MASK           0x10000000
#define PS3AV_EVENT_ID_MASK            0x0000ffff
#define PS3AV_CID_MASK                 0xffffffff
#define PS3AV_REPLY_BIT                0x80000000

#define ps3av_event_get_port_id(cid)   ((cid >> 16) & 0xff)

static u32 *ps3av_search_cmd_table(u32 cid, u32 mask)
{
	u32 *table;
	int i;

	table = cmd_table;
	for (i = 0;; table++, i++) {
		if ((*table & mask) == (cid & mask))
			break;
		if (*table == 0)
			return NULL;
	}
	return table;
}

static int ps3av_parse_event_packet(const struct ps3av_reply_hdr *hdr)
{
	u32 *table;

	if (hdr->cid & PS3AV_EVENT_CMD_MASK) {
		table = ps3av_search_cmd_table(hdr->cid, PS3AV_EVENT_CMD_MASK);
		if (table)
			dev_dbg(&ps3av_dev.core,
				"recv event packet cid:%08x port:0x%x size:%d\n",
				hdr->cid, ps3av_event_get_port_id(hdr->cid),
				hdr->size);
		else
			printk(KERN_ERR
			       "%s: failed event packet, cid:%08x size:%d\n",
			       __FUNCTION__, hdr->cid, hdr->size);
		return 1;	/* receive event packet */
	}
	return 0;
}

static int ps3av_send_cmd_pkt(const struct ps3av_send_hdr *send_buf,
			      struct ps3av_reply_hdr *recv_buf, int write_len,
			      int read_len)
{
	int res;
	u32 cmd;
	int event;

	if (!ps3av.available)
		return -ENODEV;

	/* send pkt */
	res = ps3av_vuart_write(ps3av.dev, send_buf, write_len);
	if (res < 0) {
		dev_dbg(&ps3av_dev.core,
			"%s: ps3av_vuart_write() failed (result=%d)\n",
			__FUNCTION__, res);
		return res;
	}

	/* recv pkt */
	cmd = send_buf->cid;
	do {
		/* read header */
		res = ps3av_vuart_read(ps3av.dev, recv_buf, PS3AV_HDR_SIZE,
				       timeout);
		if (res != PS3AV_HDR_SIZE) {
			dev_dbg(&ps3av_dev.core,
				"%s: ps3av_vuart_read() failed (result=%d)\n",
				__FUNCTION__, res);
			return res;
		}

		/* read body */
		res = ps3av_vuart_read(ps3av.dev, &recv_buf->cid,
				       recv_buf->size, timeout);
		if (res < 0) {
			dev_dbg(&ps3av_dev.core,
				"%s: ps3av_vuart_read() failed (result=%d)\n",
				__FUNCTION__, res);
			return res;
		}
		res += PS3AV_HDR_SIZE;	/* total len */
		event = ps3av_parse_event_packet(recv_buf);
		/* ret > 0 event packet */
	} while (event);

	if ((cmd | PS3AV_REPLY_BIT) != recv_buf->cid) {
		dev_dbg(&ps3av_dev.core, "%s: reply err (result=%x)\n",
			__FUNCTION__, recv_buf->cid);
		return -EINVAL;
	}

	return 0;
}

static int ps3av_process_reply_packet(struct ps3av_send_hdr *cmd_buf,
				      const struct ps3av_reply_hdr *recv_buf,
				      int user_buf_size)
{
	int return_len;

	if (recv_buf->version != PS3AV_VERSION) {
		dev_dbg(&ps3av_dev.core, "reply_packet invalid version:%x\n",
			recv_buf->version);
		return -EFAULT;
	}
	return_len = recv_buf->size + PS3AV_HDR_SIZE;
	if (return_len > user_buf_size)
		return_len = user_buf_size;
	memcpy(cmd_buf, recv_buf, return_len);
	return 0;		/* success */
}

void ps3av_set_hdr(u32 cid, u16 size, struct ps3av_send_hdr *hdr)
{
	hdr->version = PS3AV_VERSION;
	hdr->size = size - PS3AV_HDR_SIZE;
	hdr->cid = cid;
}

int ps3av_do_pkt(u32 cid, u16 send_len, size_t usr_buf_size,
		 struct ps3av_send_hdr *buf)
{
	int res = 0;
	union {
		struct ps3av_reply_hdr reply_hdr;
		u8 raw[PS3AV_BUF_SIZE];
	} recv_buf;

	u32 *table;

	BUG_ON(!ps3av.available);

	if (down_interruptible(&ps3av.sem))
		return -ERESTARTSYS;

	table = ps3av_search_cmd_table(cid, PS3AV_CID_MASK);
	BUG_ON(!table);
	BUG_ON(send_len < PS3AV_HDR_SIZE);
	BUG_ON(usr_buf_size < send_len);
	BUG_ON(usr_buf_size > PS3AV_BUF_SIZE);

	/* create header */
	ps3av_set_hdr(cid, send_len, buf);

	/* send packet via vuart */
	res = ps3av_send_cmd_pkt(buf, &recv_buf.reply_hdr, send_len,
				 usr_buf_size);
	if (res < 0) {
		printk(KERN_ERR
		       "%s: ps3av_send_cmd_pkt() failed (result=%d)\n",
		       __FUNCTION__, res);
		goto err;
	}

	/* process reply packet */
	res = ps3av_process_reply_packet(buf, &recv_buf.reply_hdr,
					 usr_buf_size);
	if (res < 0) {
		printk(KERN_ERR "%s: put_return_status() failed (result=%d)\n",
		       __FUNCTION__, res);
		goto err;
	}

	up(&ps3av.sem);
	return 0;

      err:
	up(&ps3av.sem);
	printk(KERN_ERR "%s: failed cid:%x res:%d\n", __FUNCTION__, cid, res);
	return res;
}

static int ps3av_set_av_video_mute(u32 mute)
{
	int i, num_of_av_port, res;

	num_of_av_port = ps3av.av_hw_conf.num_of_hdmi +
			 ps3av.av_hw_conf.num_of_avmulti;
	/* video mute on */
	for (i = 0; i < num_of_av_port; i++) {
		res = ps3av_cmd_av_video_mute(1, &ps3av.av_port[i], mute);
		if (res < 0)
			return -1;
	}

	return 0;
}

static int ps3av_set_video_disable_sig(void)
{
	int i, num_of_hdmi_port, num_of_av_port, res;

	num_of_hdmi_port = ps3av.av_hw_conf.num_of_hdmi;
	num_of_av_port = ps3av.av_hw_conf.num_of_hdmi +
			 ps3av.av_hw_conf.num_of_avmulti;

	/* tv mute */
	for (i = 0; i < num_of_hdmi_port; i++) {
		res = ps3av_cmd_av_tv_mute(ps3av.av_port[i],
					   PS3AV_CMD_MUTE_ON);
		if (res < 0)
			return -1;
	}
	msleep(100);

	/* video mute on */
	for (i = 0; i < num_of_av_port; i++) {
		res = ps3av_cmd_av_video_disable_sig(ps3av.av_port[i]);
		if (res < 0)
			return -1;
		if (i < num_of_hdmi_port) {
			res = ps3av_cmd_av_tv_mute(ps3av.av_port[i],
						   PS3AV_CMD_MUTE_OFF);
			if (res < 0)
				return -1;
		}
	}
	msleep(300);

	return 0;
}

static int ps3av_set_audio_mute(u32 mute)
{
	int i, num_of_av_port, num_of_opt_port, res;

	num_of_av_port = ps3av.av_hw_conf.num_of_hdmi +
			 ps3av.av_hw_conf.num_of_avmulti;
	num_of_opt_port = ps3av.av_hw_conf.num_of_spdif;

	for (i = 0; i < num_of_av_port; i++) {
		res = ps3av_cmd_av_audio_mute(1, &ps3av.av_port[i], mute);
		if (res < 0)
			return -1;
	}
	for (i = 0; i < num_of_opt_port; i++) {
		res = ps3av_cmd_audio_mute(1, &ps3av.opt_port[i], mute);
		if (res < 0)
			return -1;
	}

	return 0;
}

int ps3av_set_audio_mode(u32 ch, u32 fs, u32 word_bits, u32 format, u32 source)
{
	struct ps3av_pkt_avb_param avb_param;
	int i, num_of_audio, vid, res;
	struct ps3av_pkt_audio_mode audio_mode;
	u32 len = 0;

	num_of_audio = ps3av.av_hw_conf.num_of_hdmi +
		       ps3av.av_hw_conf.num_of_avmulti +
		       ps3av.av_hw_conf.num_of_spdif;

	avb_param.num_of_video_pkt = 0;
	avb_param.num_of_audio_pkt = PS3AV_AVB_NUM_AUDIO;	/* always 0 */
	avb_param.num_of_av_video_pkt = 0;
	avb_param.num_of_av_audio_pkt = ps3av.av_hw_conf.num_of_hdmi;

	vid = video_mode_table[ps3av.ps3av_mode].vid;

	/* audio mute */
	ps3av_set_audio_mute(PS3AV_CMD_MUTE_ON);

	/* audio inactive */
	res = ps3av_cmd_audio_active(0, ps3av.audio_port);
	if (res < 0)
		dev_dbg(&ps3av_dev.core,
			"ps3av_cmd_audio_active OFF failed\n");

	/* audio_pkt */
	for (i = 0; i < num_of_audio; i++) {
		ps3av_cmd_set_audio_mode(&audio_mode, ps3av.av_port[i], ch, fs,
					 word_bits, format, source);
		if (i < ps3av.av_hw_conf.num_of_hdmi) {
			/* hdmi only */
			len += ps3av_cmd_set_av_audio_param(&avb_param.buf[len],
							    ps3av.av_port[i],
							    &audio_mode, vid);
		}
		/* audio_mode pkt should be sent separately */
		res = ps3av_cmd_audio_mode(&audio_mode);
		if (res < 0)
			dev_dbg(&ps3av_dev.core,
				"ps3av_cmd_audio_mode failed, port:%x\n", i);
	}

	/* send command using avb pkt */
	len += offsetof(struct ps3av_pkt_avb_param, buf);
	res = ps3av_cmd_avb_param(&avb_param, len);
	if (res < 0)
		dev_dbg(&ps3av_dev.core, "ps3av_cmd_avb_param failed\n");

	/* audio mute */
	ps3av_set_audio_mute(PS3AV_CMD_MUTE_OFF);

	/* audio active */
	res = ps3av_cmd_audio_active(1, ps3av.audio_port);
	if (res < 0)
		dev_dbg(&ps3av_dev.core, "ps3av_cmd_audio_active ON failed\n");

	return 0;
}

EXPORT_SYMBOL_GPL(ps3av_set_audio_mode);

static int ps3av_set_videomode(void)
{
	/* av video mute */
	ps3av_set_av_video_mute(PS3AV_CMD_MUTE_ON);

	/* wake up ps3avd to do the actual video mode setting */
	queue_work(ps3av.wq, &ps3av.work);

	return 0;
}

static void ps3av_set_videomode_cont(u32 id, u32 old_id)
{
	struct ps3av_pkt_avb_param avb_param;
	int i;
	u32 len = 0, av_video_cs;
	const struct avset_video_mode *video_mode;
	int res;

	video_mode = &video_mode_table[id & PS3AV_MODE_MASK];

	avb_param.num_of_video_pkt = PS3AV_AVB_NUM_VIDEO;	/* num of head */
	avb_param.num_of_audio_pkt = 0;
	avb_param.num_of_av_video_pkt = ps3av.av_hw_conf.num_of_hdmi +
					ps3av.av_hw_conf.num_of_avmulti;
	avb_param.num_of_av_audio_pkt = 0;

	/* video signal off */
	ps3av_set_video_disable_sig();

	/* Retail PS3 product doesn't support this */
	if (id & PS3AV_MODE_HDCP_OFF) {
		res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_HDCP_OFF);
		if (res == PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
			dev_dbg(&ps3av_dev.core, "Not supported\n");
		else if (res)
			dev_dbg(&ps3av_dev.core,
				"ps3av_cmd_av_hdmi_mode failed\n");
	} else if (old_id & PS3AV_MODE_HDCP_OFF) {
		res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_MODE_NORMAL);
		if (res < 0 && res != PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
			dev_dbg(&ps3av_dev.core,
				"ps3av_cmd_av_hdmi_mode failed\n");
	}

	/* video_pkt */
	for (i = 0; i < avb_param.num_of_video_pkt; i++)
		len += ps3av_cmd_set_video_mode(&avb_param.buf[len],
						ps3av.head[i], video_mode->vid,
						video_mode->fmt, id);
	/* av_video_pkt */
	for (i = 0; i < avb_param.num_of_av_video_pkt; i++) {
		if (id & PS3AV_MODE_DVI || id & PS3AV_MODE_RGB)
			av_video_cs = RGB8;
		else
			av_video_cs = video_mode->cs;
#ifndef PS3AV_HDMI_YUV
		if (ps3av.av_port[i] == PS3AV_CMD_AVPORT_HDMI_0 ||
		    ps3av.av_port[i] == PS3AV_CMD_AVPORT_HDMI_1)
			av_video_cs = RGB8;	/* use RGB for HDMI */
#endif
		len += ps3av_cmd_set_av_video_cs(&avb_param.buf[len],
						 ps3av.av_port[i],
						 video_mode->vid, av_video_cs,
						 video_mode->aspect, id);
	}
	/* send command using avb pkt */
	len += offsetof(struct ps3av_pkt_avb_param, buf);
	res = ps3av_cmd_avb_param(&avb_param, len);
	if (res == PS3AV_STATUS_NO_SYNC_HEAD)
		printk(KERN_WARNING
		       "%s: Command failed. Please try your request again. \n",
		       __FUNCTION__);
	else if (res)
		dev_dbg(&ps3av_dev.core, "ps3av_cmd_avb_param failed\n");

	msleep(1500);
	/* av video mute */
	ps3av_set_av_video_mute(PS3AV_CMD_MUTE_OFF);
}

static void ps3avd(struct work_struct *work)
{
	ps3av_set_videomode_cont(ps3av.ps3av_mode, ps3av.ps3av_mode_old);
	complete(&ps3av.done);
}

static int ps3av_vid2table_id(int vid)
{
	int i;

	for (i = 1; i < ARRAY_SIZE(video_mode_table); i++)
		if (video_mode_table[i].vid == vid)
			return i;
	return -1;
}

static int ps3av_resbit2vid(u32 res_50, u32 res_60)
{
	int vid = -1;

	if (res_50 > res_60) {	/* if res_50 == res_60, res_60 will be used */
		if (res_50 & PS3AV_RESBIT_1920x1080P)
			vid = PS3AV_CMD_VIDEO_VID_1080P_50HZ;
		else if (res_50 & PS3AV_RESBIT_1920x1080I)
			vid = PS3AV_CMD_VIDEO_VID_1080I_50HZ;
		else if (res_50 & PS3AV_RESBIT_1280x720P)
			vid = PS3AV_CMD_VIDEO_VID_720P_50HZ;
		else if (res_50 & PS3AV_RESBIT_720x576P)
			vid = PS3AV_CMD_VIDEO_VID_576P;
		else
			vid = -1;
	} else {
		if (res_60 & PS3AV_RESBIT_1920x1080P)
			vid = PS3AV_CMD_VIDEO_VID_1080P_60HZ;
		else if (res_60 & PS3AV_RESBIT_1920x1080I)
			vid = PS3AV_CMD_VIDEO_VID_1080I_60HZ;
		else if (res_60 & PS3AV_RESBIT_1280x720P)
			vid = PS3AV_CMD_VIDEO_VID_720P_60HZ;
		else if (res_60 & PS3AV_RESBIT_720x480P)
			vid = PS3AV_CMD_VIDEO_VID_480P;
		else
			vid = -1;
	}
	return vid;
}

static int ps3av_hdmi_get_vid(struct ps3av_info_monitor *info)
{
	u32 res_50, res_60;
	int vid = -1;

	if (info->monitor_type != PS3AV_MONITOR_TYPE_HDMI)
		return -1;

	/* check native resolution */
	res_50 = info->res_50.native & PS3AV_RES_MASK_50;
	res_60 = info->res_60.native & PS3AV_RES_MASK_60;
	if (res_50 || res_60) {
		vid = ps3av_resbit2vid(res_50, res_60);
		return vid;
	}

	/* check resolution */
	res_50 = info->res_50.res_bits & PS3AV_RES_MASK_50;
	res_60 = info->res_60.res_bits & PS3AV_RES_MASK_60;
	if (res_50 || res_60) {
		vid = ps3av_resbit2vid(res_50, res_60);
		return vid;
	}

	if (ps3av.region & PS3AV_REGION_60)
		vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
	else
		vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
	return vid;
}

static int ps3av_auto_videomode(struct ps3av_pkt_av_get_hw_conf *av_hw_conf,
				int boot)
{
	int i, res, vid = -1, dvi = 0, rgb = 0;
	struct ps3av_pkt_av_get_monitor_info monitor_info;
	struct ps3av_info_monitor *info;

	/* get vid for hdmi */
	for (i = 0; i < av_hw_conf->num_of_hdmi; i++) {
		res = ps3av_cmd_video_get_monitor_info(&monitor_info,
						       PS3AV_CMD_AVPORT_HDMI_0 +
						       i);
		if (res < 0)
			return -1;

		ps3av_cmd_av_monitor_info_dump(&monitor_info);
		info = &monitor_info.info;
		/* check DVI */
		if (info->monitor_type == PS3AV_MONITOR_TYPE_DVI) {
			dvi = PS3AV_MODE_DVI;
			break;
		}
		/* check HDMI */
		vid = ps3av_hdmi_get_vid(info);
		if (vid != -1) {
			/* got valid vid */
			break;
		}
	}

	if (dvi) {
		/* DVI mode */
		vid = PS3AV_DEFAULT_DVI_VID;
	} else if (vid == -1) {
		/* no HDMI interface or HDMI is off */
		if (ps3av.region & PS3AV_REGION_60)
			vid = PS3AV_DEFAULT_AVMULTI_VID_REG_60;
		else
			vid = PS3AV_DEFAULT_AVMULTI_VID_REG_50;
		if (ps3av.region & PS3AV_REGION_RGB)
			rgb = PS3AV_MODE_RGB;
	} else if (boot) {
		/* HDMI: using DEFAULT HDMI_VID while booting up */
		info = &monitor_info.info;
		if (ps3av.region & PS3AV_REGION_60) {
			if (info->res_60.res_bits & PS3AV_RESBIT_720x480P)
				vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
			else if (info->res_50.res_bits & PS3AV_RESBIT_720x576P)
				vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
			else {
				/* default */
				vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
			}
		} else {
			if (info->res_50.res_bits & PS3AV_RESBIT_720x576P)
				vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
			else if (info->res_60.res_bits & PS3AV_RESBIT_720x480P)
				vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
			else {
				/* default */
				vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
			}
		}
	}

	return (ps3av_vid2table_id(vid) | dvi | rgb);
}

static int ps3av_get_hw_conf(struct ps3av *ps3av)
{
	int i, j, k, res;

	/* get av_hw_conf */
	res = ps3av_cmd_av_get_hw_conf(&ps3av->av_hw_conf);
	if (res < 0)
		return -1;

	ps3av_cmd_av_hw_conf_dump(&ps3av->av_hw_conf);

	for (i = 0; i < PS3AV_HEAD_MAX; i++)
		ps3av->head[i] = PS3AV_CMD_VIDEO_HEAD_A + i;
	for (i = 0; i < PS3AV_OPT_PORT_MAX; i++)
		ps3av->opt_port[i] = PS3AV_CMD_AVPORT_SPDIF_0 + i;
	for (i = 0; i < ps3av->av_hw_conf.num_of_hdmi; i++)
		ps3av->av_port[i] = PS3AV_CMD_AVPORT_HDMI_0 + i;
	for (j = 0; j < ps3av->av_hw_conf.num_of_avmulti; j++)
		ps3av->av_port[i + j] = PS3AV_CMD_AVPORT_AVMULTI_0 + j;
	for (k = 0; k < ps3av->av_hw_conf.num_of_spdif; k++)
		ps3av->av_port[i + j + k] = PS3AV_CMD_AVPORT_SPDIF_0 + k;

	/* set all audio port */
	ps3av->audio_port = PS3AV_CMD_AUDIO_PORT_HDMI_0
	    | PS3AV_CMD_AUDIO_PORT_HDMI_1
	    | PS3AV_CMD_AUDIO_PORT_AVMULTI_0
	    | PS3AV_CMD_AUDIO_PORT_SPDIF_0 | PS3AV_CMD_AUDIO_PORT_SPDIF_1;

	return 0;
}

/* set mode using id */
int ps3av_set_video_mode(u32 id, int boot)
{
	int size;
	u32 option;

	size = ARRAY_SIZE(video_mode_table);
	if ((id & PS3AV_MODE_MASK) > size - 1 || id < 0) {
		dev_dbg(&ps3av_dev.core, "%s: error id :%d\n", __FUNCTION__,
			id);
		return -EINVAL;
	}

	/* auto mode */
	option = id & ~PS3AV_MODE_MASK;
	if ((id & PS3AV_MODE_MASK) == 0) {
		id = ps3av_auto_videomode(&ps3av.av_hw_conf, boot);
		if (id < 1) {
			printk(KERN_ERR "%s: invalid id :%d\n", __FUNCTION__,
			       id);
			return -EINVAL;
		}
		id |= option;
	}

	/* set videomode */
	wait_for_completion(&ps3av.done);
	ps3av.ps3av_mode_old = ps3av.ps3av_mode;
	ps3av.ps3av_mode = id;
	if (ps3av_set_videomode())
		ps3av.ps3av_mode = ps3av.ps3av_mode_old;

	return 0;
}

EXPORT_SYMBOL_GPL(ps3av_set_video_mode);

int ps3av_set_mode(u32 id, int boot)
{
	int res;

	res = ps3av_set_video_mode(id, boot);
	if (res)
		return res;

	res = ps3av_set_audio_mode(PS3AV_CMD_AUDIO_NUM_OF_CH_2,
				   PS3AV_CMD_AUDIO_FS_48K,
				   PS3AV_CMD_AUDIO_WORD_BITS_16,
				   PS3AV_CMD_AUDIO_FORMAT_PCM,
				   PS3AV_CMD_AUDIO_SOURCE_SERIAL);
	if (res)
		return res;

	return 0;
}

EXPORT_SYMBOL_GPL(ps3av_set_mode);

int ps3av_get_mode(void)
{
	return ps3av.ps3av_mode;
}

EXPORT_SYMBOL_GPL(ps3av_get_mode);

int ps3av_get_scanmode(int id)
{
	int size;

	id = id & PS3AV_MODE_MASK;
	size = ARRAY_SIZE(video_mode_table);
	if (id > size - 1 || id < 0) {
		printk(KERN_ERR "%s: invalid mode %d\n", __FUNCTION__, id);
		return -EINVAL;
	}
	return video_mode_table[id].interlace;
}

EXPORT_SYMBOL_GPL(ps3av_get_scanmode);

int ps3av_get_refresh_rate(int id)
{
	int size;

	id = id & PS3AV_MODE_MASK;
	size = ARRAY_SIZE(video_mode_table);
	if (id > size - 1 || id < 0) {
		printk(KERN_ERR "%s: invalid mode %d\n", __FUNCTION__, id);
		return -EINVAL;
	}
	return video_mode_table[id].freq;
}

EXPORT_SYMBOL_GPL(ps3av_get_refresh_rate);

/* get resolution by video_mode */
int ps3av_video_mode2res(u32 id, u32 *xres, u32 *yres)
{
	int size;

	id = id & PS3AV_MODE_MASK;
	size = ARRAY_SIZE(video_mode_table);
	if (id > size - 1 || id < 0) {
		printk(KERN_ERR "%s: invalid mode %d\n", __FUNCTION__, id);
		return -EINVAL;
	}
	*xres = video_mode_table[id].x;
	*yres = video_mode_table[id].y;
	return 0;
}

EXPORT_SYMBOL_GPL(ps3av_video_mode2res);

/* mute */
int ps3av_video_mute(int mute)
{
	return ps3av_set_av_video_mute(mute ? PS3AV_CMD_MUTE_ON
					    : PS3AV_CMD_MUTE_OFF);
}

EXPORT_SYMBOL_GPL(ps3av_video_mute);

int ps3av_audio_mute(int mute)
{
	return ps3av_set_audio_mute(mute ? PS3AV_CMD_MUTE_ON
					 : PS3AV_CMD_MUTE_OFF);
}

EXPORT_SYMBOL_GPL(ps3av_audio_mute);

int ps3av_dev_open(void)
{
	int status = 0;

	mutex_lock(&ps3av.mutex);
	if (!ps3av.open_count++) {
		status = lv1_gpu_open(0);
		if (status) {
			printk(KERN_ERR "%s: lv1_gpu_open failed %d\n",
			       __FUNCTION__, status);
			ps3av.open_count--;
		}
	}
	mutex_unlock(&ps3av.mutex);

	return status;
}

EXPORT_SYMBOL_GPL(ps3av_dev_open);

int ps3av_dev_close(void)
{
	int status = 0;

	mutex_lock(&ps3av.mutex);
	if (ps3av.open_count <= 0) {
		printk(KERN_ERR "%s: GPU already closed\n", __FUNCTION__);
		status = -1;
	} else if (!--ps3av.open_count) {
		status = lv1_gpu_close();
		if (status)
			printk(KERN_WARNING "%s: lv1_gpu_close failed %d\n",
			       __FUNCTION__, status);
	}
	mutex_unlock(&ps3av.mutex);

	return status;
}

EXPORT_SYMBOL_GPL(ps3av_dev_close);

static int ps3av_probe(struct ps3_vuart_port_device *dev)
{
	int res;
	u32 id;

	dev_dbg(&ps3av_dev.core, "init ...\n");
	dev_dbg(&ps3av_dev.core, "  timeout=%d\n", timeout);

	memset(&ps3av, 0, sizeof(ps3av));

	init_MUTEX(&ps3av.sem);
	mutex_init(&ps3av.mutex);
	ps3av.ps3av_mode = 0;
	ps3av.dev = dev;

	INIT_WORK(&ps3av.work, ps3avd);
	init_completion(&ps3av.done);
	complete(&ps3av.done);
	ps3av.wq = create_singlethread_workqueue("ps3avd");
	if (!ps3av.wq)
		return -ENOMEM;

	ps3av.available = 1;
	switch (ps3_os_area_get_av_multi_out()) {
	case PS3_PARAM_AV_MULTI_OUT_NTSC:
		ps3av.region = PS3AV_REGION_60;
		break;
	case PS3_PARAM_AV_MULTI_OUT_PAL_YCBCR:
	case PS3_PARAM_AV_MULTI_OUT_SECAM:
		ps3av.region = PS3AV_REGION_50;
		break;
	case PS3_PARAM_AV_MULTI_OUT_PAL_RGB:
		ps3av.region = PS3AV_REGION_50 | PS3AV_REGION_RGB;
		break;
	default:
		ps3av.region = PS3AV_REGION_60;
		break;
	}

	/* init avsetting modules */
	res = ps3av_cmd_init();
	if (res < 0)
		printk(KERN_ERR "%s: ps3av_cmd_init failed %d\n", __FUNCTION__,
		       res);

	ps3av_get_hw_conf(&ps3av);
	id = ps3av_auto_videomode(&ps3av.av_hw_conf, 1);
	mutex_lock(&ps3av.mutex);
	ps3av.ps3av_mode = id;
	mutex_unlock(&ps3av.mutex);

	dev_dbg(&ps3av_dev.core, "init...done\n");

	return 0;
}

static int ps3av_remove(struct ps3_vuart_port_device *dev)
{
	if (ps3av.available) {
		ps3av_cmd_fin();
		if (ps3av.wq)
			destroy_workqueue(ps3av.wq);
		ps3av.available = 0;
	}

	return 0;
}

static void ps3av_shutdown(struct ps3_vuart_port_device *dev)
{
	ps3av_remove(dev);
}

static struct ps3_vuart_port_driver ps3av_driver = {
	.match_id = PS3_MATCH_ID_AV_SETTINGS,
	.core = {
		.name = "ps3_av",
	},
	.probe = ps3av_probe,
	.remove = ps3av_remove,
	.shutdown = ps3av_shutdown,
};

static int ps3av_module_init(void)
{
	int error;

	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
		return -ENODEV;

	error = ps3_vuart_port_driver_register(&ps3av_driver);
	if (error) {
		printk(KERN_ERR
		       "%s: ps3_vuart_port_driver_register failed %d\n",
		       __FUNCTION__, error);
		return error;
	}

	error = ps3_vuart_port_device_register(&ps3av_dev);
	if (error)
		printk(KERN_ERR
		       "%s: ps3_vuart_port_device_register failed %d\n",
		       __FUNCTION__, error);

	return error;
}

static void __exit ps3av_module_exit(void)
{
	device_unregister(&ps3av_dev.core);
	ps3_vuart_port_driver_unregister(&ps3av_driver);
}

subsys_initcall(ps3av_module_init);
module_exit(ps3av_module_exit);