summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/1394/targets/dcam1394/dcam.c
blob: 924b6344b4ceba0dc71813afd1e6380a31afea1f (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
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2018, Joyent, Inc.
 */


/*
 * dcam.c
 *
 * dcam1394 driver. Controls IIDC compliant devices attached through a
 * IEEE-1394 bus.
 */

#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/modctl.h>
#include <sys/sunndi.h>
#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/cred.h>
#include <sys/mkdev.h>
#include <sys/kmem.h>
#include <sys/stat.h>
#include <sys/cmn_err.h>
#include <sys/stream.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/devops.h>
#include <sys/1394/t1394.h>

#include <sys/dcam/dcam1394_io.h>
#include <sys/1394/targets/dcam1394/dcam.h>
#include <sys/1394/targets/dcam1394/dcam_reg.h>
#include <sys/1394/targets/dcam1394/dcam_param.h>
#include <sys/1394/targets/dcam1394/dcam_frame.h>

/* for power management (we have only one component) */
static char *dcam_pmc[] = {
	"NAME=dcam1394",
	"0=Off",
	"1=On"
};

int g_vid_mode_frame_num_bytes[] =
{
	57600,		/* vid mode 0 */
	153600,		/* vid mode 1 */
	460800,		/* vid mode 2 */
	614400,		/* vid mode 3 */
	921600,		/* vid mode 4 */
	307200		/* vid mode 5 */
};

static int	byte_copy_to_user_buff(uchar_t *src_addr_p, struct uio *uio_p,
		    size_t num_bytes, int start_index, int *end_index);
static int	byte_copy_from_user_buff(uchar_t *dst_addr_p, struct uio *uio_p,
		    size_t num_bytes, int start_index, int *end_index);
static int	dcam_reset(dcam_state_t *softc_p);

/* opaque state structure head */
void *dcam_state_p;

static struct cb_ops dcam_cb_ops = {
	dcam_open,		/* open		*/
	dcam_close,		/* close	*/
	nodev,			/* strategy	*/
	nodev,			/* print	*/
	nodev,			/* dump		*/
	dcam_read,		/* read		*/
	nodev,			/* write	*/
	dcam_ioctl,		/* ioctl	*/
	nodev,			/* devmap	*/
	nodev,			/* mmap		*/
	nodev,			/* segmap	*/
	dcam_chpoll,		/* chpoll	*/
	ddi_prop_op,		/* prop_op	*/
	NULL,			/* streams	*/
				/* flags	*/
	D_NEW | D_MP | D_64BIT | D_HOTPLUG,
	CB_REV,			/* rev		*/
	nodev,			/* aread	*/
	nodev			/* awrite	*/
};

static struct dev_ops dcam_dev_ops = {
	DEVO_REV,		/* DEVO_REV indicated by manual	*/
	0,			/* device reference count	*/
	dcam_getinfo,		/* getinfo			*/
	nulldev,		/* identify			*/
	nulldev,		/* probe			*/
	dcam_attach,		/* attach			*/
	dcam_detach,		/* detach			*/
	nodev,			/* reset			*/
	&dcam_cb_ops,		/* ptr to cb_ops struct		*/
	NULL,			/* ptr to bus_ops struct; none	*/
	dcam_power,		/* power			*/
	ddi_quiesce_not_supported,	/* devo_quiesce */
};

extern	struct	mod_ops mod_driverops;

static	struct modldrv modldrv = {
	&mod_driverops,
	"SUNW 1394-based Digital Camera driver",
	&dcam_dev_ops,
};

static	struct modlinkage modlinkage = {
	MODREV_1,
	(void *)&modldrv,
	NULL,
};


int
_init(void)
{
	int err;

	err = ddi_soft_state_init(&dcam_state_p, sizeof (dcam_state_t), 2);

	if (err) {
		return (err);
	}

	if (err = mod_install(&modlinkage)) {
		ddi_soft_state_fini(&dcam_state_p);
	}

	return (err);
}


int
_info(struct modinfo *modinfop)
{
	int err;

	err = mod_info(&modlinkage, modinfop);
	return (err);
}


int
_fini(void)
{
	int err;

	if ((err = mod_remove(&modlinkage)) != 0) {
		return (err);
	}

	ddi_soft_state_fini(&dcam_state_p);

	return (err);
}


/*
 * dcam_attach
 */
/* ARGSUSED */
int
dcam_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	char			tmp_str[MAX_STR_LEN];
	dcam_state_t		*softc_p;
	ddi_eventcookie_t	ev_cookie;
	int			instance;
	int			ret_val;

	switch (cmd) {

	case DDI_ATTACH:
		instance = ddi_get_instance(dip);

		if (ddi_soft_state_zalloc(dcam_state_p, instance) !=
		    DDI_SUCCESS) {
			return (DDI_FAILURE);
		}

		if ((softc_p = ddi_get_soft_state(dcam_state_p, instance)) ==
		    NULL) {
			ddi_soft_state_free(dcam_state_p, instance);
			return (DDI_FAILURE);
		}

		/*
		 * Initialize soft state
		 */
		softc_p->dip			= dip;
		softc_p->instance		= instance;
		softc_p->usr_model		= -1;
		softc_p->ixlp			= NULL;

		softc_p->seq_count 		= 0;
		softc_p->param_status 		= 0;

		/*
		 * set default vid_mode, frame_rate and ring_buff_capacity
		 */
		softc_p->cur_vid_mode 		= 1;
		softc_p->cur_frame_rate 	= 3;
		softc_p->cur_ring_buff_capacity = 10;
		softc_p->camera_online		= 1;

		(void) sprintf(tmp_str, "dcam%d", instance);

		if (ddi_create_minor_node(dip, tmp_str, S_IFCHR, instance,
		    DDI_PSEUDO, 0) != DDI_SUCCESS) {
			ddi_soft_state_free(dcam_state_p, instance);

			return (DDI_FAILURE);
		}

		(void) sprintf(tmp_str, "dcamctl%d", instance);

		if (ddi_create_minor_node(dip, tmp_str, S_IFCHR,
		    instance + DCAM1394_MINOR_CTRL, "ddi_dcam1394", 0) !=
		    DDI_SUCCESS) {
			ddi_soft_state_free(dcam_state_p, instance);

			return (DDI_FAILURE);
		}

		if (t1394_attach(dip, T1394_VERSION_V1, 0,
		    &(softc_p->attachinfo),
		    &(softc_p->sl_handle)) != DDI_SUCCESS) {
			ddi_soft_state_free(dcam_state_p, instance);
			ddi_remove_minor_node(dip, NULL);

			return (DDI_FAILURE);
		}

		if (t1394_get_targetinfo(softc_p->sl_handle,
		    softc_p->attachinfo.localinfo.bus_generation, 0,
		    &(softc_p->targetinfo)) != DDI_SUCCESS) {
			cmn_err(CE_WARN,
			    "dcam_attach: t1394_get_targetinfo failed\n");
		}

		if (ddi_get_eventcookie(dip, DDI_DEVI_BUS_RESET_EVENT,
		    &ev_cookie) != DDI_SUCCESS) {
			(void) t1394_detach(&softc_p->sl_handle, 0);

			ddi_soft_state_free(dcam_state_p, instance);
			ddi_remove_minor_node(dip, NULL);

			return (DDI_FAILURE);
		}

		if (ddi_add_event_handler(dip, ev_cookie, dcam_bus_reset_notify,
		    softc_p, &softc_p->event_id) != DDI_SUCCESS) {
			(void) t1394_detach(&softc_p->sl_handle, 0);

			ddi_soft_state_free(dcam_state_p, instance);
			ddi_remove_minor_node(dip, NULL);

			return (DDI_FAILURE);
		}

		mutex_init(&softc_p->softc_mutex, NULL, MUTEX_DRIVER,
		    softc_p->attachinfo.iblock_cookie);

		mutex_init(&softc_p->dcam_frame_is_done_mutex, NULL,
		    MUTEX_DRIVER, softc_p->attachinfo.iblock_cookie);

		/*
		 * init the soft state's parameter attribute structure
		 */
		if (param_attr_init(softc_p, softc_p->param_attr) !=
		    DDI_SUCCESS) {
			(void) ddi_remove_event_handler(softc_p->event_id);
			(void) t1394_detach(&softc_p->sl_handle, 0);

			ddi_soft_state_free(dcam_state_p, instance);
			ddi_remove_minor_node(dip, NULL);

			return (DDI_FAILURE);
		}

		/*
		 * power management stuff
		 */
		if (ddi_prop_update_string_array(DDI_DEV_T_NONE,
		    dip, "pm-components", dcam_pmc,
		    sizeof (dcam_pmc)/sizeof (char *)) == DDI_PROP_SUCCESS) {

			(void) pm_raise_power(dip, 0, 1);
			if (ddi_prop_exists(DDI_DEV_T_ANY, dip, 0,
			    "power-managed?")) {
				(void) pm_idle_component(dip, 0);
			} else {
				(void) pm_busy_component(dip, 0);
			}
		}

		softc_p->flags |= DCAM1394_FLAG_ATTACH_COMPLETE;

		ddi_report_dev(dip);
		ret_val = DDI_SUCCESS;
		break;

	case DDI_RESUME:
		instance = ddi_get_instance(dip);
		if ((softc_p = ddi_get_soft_state(dcam_state_p, instance)) ==
		    NULL) {
			ddi_soft_state_free(dcam_state_p, instance);
			return (DDI_FAILURE);
		}

		mutex_enter(&softc_p->softc_mutex);

		if (softc_p->flags & DCAM1394_FLAG_FRAME_RCV_INIT) {
			(void) dcam1394_ioctl_frame_rcv_start(softc_p);
		}

		softc_p->suspended = 0;

		mutex_exit(&softc_p->softc_mutex);

		ret_val = DDI_SUCCESS;
		break;

	default:
		ret_val = DDI_FAILURE;
		break;
	}

	return (ret_val);
}


/*
 * dcam_power: perform dcam power management
 */
/* ARGSUSED */
int
dcam_power(dev_info_t *dip, int component, int level)
{
	dcam_state_t	*softc_p;
	int		instance;

	instance = ddi_get_instance(dip);
	softc_p  = (dcam_state_t *)ddi_get_soft_state(dcam_state_p, instance);

	if (softc_p == NULL)
		return (DDI_FAILURE);

	softc_p->pm_cable_power = level;

	return (DDI_SUCCESS);

}


/*
 * dcam_getinfo
 */
/* ARGSUSED */
int
dcam_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
{
	dev_t		 dev;
	dcam_state_t	*softc_p;
	int		 status;
	int		 instance;

	switch (cmd) {

	case DDI_INFO_DEVT2DEVINFO:
		dev	 = (dev_t)arg;
		instance = DEV_TO_INSTANCE(dev);
		softc_p  = (dcam_state_t *)
		    ddi_get_soft_state(dcam_state_p, instance);

		if (softc_p == NULL) {
			return (DDI_FAILURE);
		}

		*result = (void *)softc_p->dip;
		status  = DDI_SUCCESS;
		break;

	case DDI_INFO_DEVT2INSTANCE:
		dev	 = (dev_t)arg;
		instance = DEV_TO_INSTANCE(dev);
		*result	 = (void *)(uintptr_t)instance;
		status	 = DDI_SUCCESS;
		break;

	default:
		status = DDI_FAILURE;
	}

	return (status);
}


/*
 * dcam_detach
 */
/* ARGSUSED */
int
dcam_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	int			 instance;
	dcam_state_t		*softc_p;

	instance = ddi_get_instance(dip);

	softc_p = (dcam_state_t *)ddi_get_soft_state(dcam_state_p, instance);
	if (softc_p == NULL) {
		return (DDI_FAILURE);
	}


	switch (cmd) {

	case DDI_SUSPEND:
		mutex_enter(&softc_p->softc_mutex);

		softc_p->suspended = 1;

		if (softc_p->flags & DCAM1394_FLAG_FRAME_RCV_INIT) {
			(void) dcam_frame_rcv_stop(softc_p);
		}

		mutex_exit(&softc_p->softc_mutex);
		return (DDI_SUCCESS);


	case DDI_DETACH:
		/*
		 * power management stuff
		 */
		(void) pm_lower_power(dip, 0, 0);
		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components");

		/*
		 * deregister with 1394 DDI framework
		 */
		if (t1394_detach(&softc_p->sl_handle, 0) != DDI_SUCCESS) {
			return (DDI_FAILURE);
		}

		(void) ddi_remove_event_handler(softc_p->event_id);

		/*
		 * free state structures, mutexes, condvars;
		 * deregister interrupts
		 */
		mutex_destroy(&softc_p->softc_mutex);
		mutex_destroy(&softc_p->dcam_frame_is_done_mutex);

		/*
		 * Remove all minor nodes, all dev_t's properties
		 */
		ddi_remove_minor_node(dip, NULL);

		ddi_soft_state_free(dcam_state_p, instance);
		ddi_prop_remove_all(dip);

		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);

	}
}


/*
 * dcam_open
 */
/* ARGSUSED */
int
dcam_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
{
	dcam_state_t	*softc_p;
	int		instance;
	int		is_ctrl_file;
	uint_t		new_flags;

	instance = (int)DEV_TO_INSTANCE(*dev_p);

	if ((softc_p = ddi_get_soft_state(dcam_state_p, instance)) == NULL) {
		return (ENXIO);
	}

	/*
	 * if dcam_attach hasn't completed, return error
	 * XXX: Check this out
	 */
	if (!(softc_p->flags & DCAM1394_FLAG_ATTACH_COMPLETE)) {
		return (ENXIO);
	}

	/* disallow block, mount, and layered opens */
	if (otyp != OTYP_CHR) {
		return (EINVAL);
	}

	new_flags    = 0;
	is_ctrl_file = (getminor(*dev_p) & DCAM1394_MINOR_CTRL) ? 1 : 0;

	mutex_enter(&softc_p->softc_mutex);

	/*
	 * The open is either for the capture file or the control file.
	 * If it's the control file construct new flags.
	 *
	 * If it's the capture file return busy if it's already open,
	 * otherwise construct new flags.
	 */
	if (is_ctrl_file) {
		new_flags |= DCAM1394_FLAG_OPEN_CONTROL;
	} else {
		if (softc_p->flags & DCAM1394_FLAG_OPEN_CAPTURE) {
			mutex_exit(&softc_p->softc_mutex);
			return (EBUSY);
		}

		new_flags |= DCAM1394_FLAG_OPEN_CAPTURE;
	}

	new_flags |= DCAM1394_FLAG_OPEN;
	softc_p->flags |= new_flags;

	mutex_exit(&softc_p->softc_mutex);

	/*
	 * power management stuff
	 */
	if (softc_p->pm_open_count == 0) {
		if (ddi_prop_exists(DDI_DEV_T_ANY, softc_p->dip, 0,
		    "power-managed?")) {
			(void) pm_busy_component(softc_p->dip, 0);
			if (softc_p->pm_cable_power == 0) {
				int i;

				(void) pm_raise_power(softc_p->dip, 0, 1);

				/*
				 * Wait for the power to be up and stable
				 * before proceeding.  100 msecs should
				 * certainly be enough, and if we check
				 * every msec we'll probably loop just a
				 * few times.
				 */
				for (i = 0; i < 100; i++) {
					if (param_power_set(softc_p, 1) == 0) {
						break;
					}
					delay((clock_t)drv_usectohz(1000));
				}
			}
		}
	}
	softc_p->pm_open_count++;

	return (0);
}


/*
 * dcam_close
 */
/* ARGSUSED */
int
dcam_close(dev_t dev, int flags, int otyp, cred_t *cred_p)
{
	int instance;
	dcam_state_t *softc;

	instance = DEV_TO_INSTANCE(dev);
	softc    = (dcam_state_t *)ddi_get_soft_state(dcam_state_p, instance);

	/*
	 * power management stuff
	 */
	softc->pm_open_count = 0;
	if (ddi_prop_exists(DDI_DEV_T_ANY, softc->dip, 0, "power-managed?")) {
		(void) pm_idle_component(softc->dip, 0);
	}

	mutex_enter(&softc->softc_mutex);

	if (getminor(dev) & DCAM1394_MINOR_CTRL) {
		softc->flags &= ~DCAM1394_FLAG_OPEN_CONTROL;
	} else {
		/*
		 * If an application which has opened the camera capture
		 * device exits without calling DCAM1394_CMD_FRAME_RCV_STOP
		 * ioctl, then we need to release resources.
		 */
		if (softc->flags & DCAM1394_FLAG_FRAME_RCV_INIT) {
			(void) dcam_frame_rcv_stop(softc);
			softc->flags &= ~DCAM1394_FLAG_FRAME_RCV_INIT;
		}

		(void) param_power_set(softc, 0);

		softc->flags &= ~DCAM1394_FLAG_OPEN_CAPTURE;
	}

	/*
	 * If driver is completely closed, then stabilize the camera
	 * and turn off transient flags
	 */
	if (!(softc->flags &
	    (DCAM1394_FLAG_OPEN_CONTROL | DCAM1394_FLAG_OPEN_CAPTURE))) {
		softc->flags &= DCAM1394_FLAG_ATTACH_COMPLETE;
	}

	mutex_exit(&softc->softc_mutex);

	return (DDI_SUCCESS);

}


/*
 * dcam_read
 *
 * If read pointer is not pointing to the same position as write pointer
 * copy frame data from ring buffer position pointed to by read pointer.
 *
 *	If during the course of copying frame data, the device driver
 *	invalidated this read() request processing operation, restart
 *	this operation.
 *
 *     Increment read pointer and return frame data to user process.
 *
 * Else return error
 *
 */
/* ARGSUSED */
int
dcam_read(dev_t dev, struct uio *uio_p, cred_t *cred_p)
{
	buff_info_t	*buff_info_p;
	dcam_state_t	*softc_p;
	hrtime_t	 timestamp;
	int		 index, instance;
	int		 read_ptr_id;
	size_t		 read_ptr_pos, write_ptr_pos;
	int		 read_req_invalid;
	ring_buff_t	*ring_buff_p;
	uchar_t		*frame_data_p;
	uint_t		 seq_num;
	unsigned long	 user_frame_buff_addr;
	uint_t		 vid_mode;
	int		 gotten_addr_flag;

	instance = DEV_TO_INSTANCE(dev);

	softc_p = (dcam_state_t *)ddi_get_soft_state(dcam_state_p, instance);
	if (softc_p == NULL) {
		return (ENXIO);
	}

	if ((ring_buff_p = softc_p->ring_buff_p) == NULL) {
		return (EAGAIN);
	}

	read_ptr_id = 0;

	mutex_enter(&softc_p->dcam_frame_is_done_mutex);

	softc_p->reader_flags[read_ptr_id] |= DCAM1394_FLAG_READ_REQ_PROC;

	user_frame_buff_addr = 0;
	gotten_addr_flag = 0;

	do {
		read_ptr_pos = ring_buff_read_ptr_pos_get(ring_buff_p,
		    read_ptr_id);

		write_ptr_pos = ring_buff_write_ptr_pos_get(ring_buff_p);

		if (read_ptr_pos != write_ptr_pos) {
			/*
			 * Since the app wants realtime video, set the read
			 * pointer to the newest data.
			 */
			if (write_ptr_pos == 0) {
				read_ptr_pos = ring_buff_p->num_buffs - 1;
			} else {
				read_ptr_pos = write_ptr_pos - 1;
			}

			/*
			 * copy frame data from ring buffer position pointed
			 * to by read pointer
			 */
			index = 0;
			buff_info_p =
			    &(ring_buff_p->buff_info_array_p[read_ptr_pos]);

			vid_mode = softc_p->cur_vid_mode;
			seq_num  = buff_info_p->seq_num;
			timestamp = buff_info_p->timestamp;
			frame_data_p = (uchar_t *)buff_info_p->kaddr_p;

			mutex_exit(&softc_p->dcam_frame_is_done_mutex);

			/*
			 * Fix for bug #4424042
			 * don't lock this section
			 */

			if (byte_copy_to_user_buff((uchar_t *)&vid_mode,
			    uio_p, sizeof (uint_t), index, &index)) {

				return (EFAULT);
			}

			if (byte_copy_to_user_buff((uchar_t *)&seq_num,
			    uio_p, sizeof (unsigned int), index, &index)) {

				return (EFAULT);
			}

			if (byte_copy_to_user_buff((uchar_t *)&timestamp,
			    uio_p, sizeof (hrtime_t), index, &index)) {

				return (EFAULT);
			}

			/*
			 * get buff pointer; do ddi_copyout()
			 * get user buffer address only once
			 */
			if (!gotten_addr_flag) {
				if (byte_copy_from_user_buff(
				    (uchar_t *)&user_frame_buff_addr, uio_p,
				    softc_p->usr_model, index, &index)) {

					return (EFAULT);
				}

#ifdef _MULTI_DATAMODEL
				if (softc_p->usr_model == ILP32_PTR_SIZE) {
					user_frame_buff_addr =
					    ((user_frame_buff_addr >> 32) &
					    0xffffffffULL) |
					    ((user_frame_buff_addr << 32) &
					    0xffffffff00000000ULL);
				}
#endif /* _MULTI_DATAMODEL */

				gotten_addr_flag = 1;
			}

			if (ddi_copyout(
			    (caddr_t)frame_data_p,
			    (caddr_t)user_frame_buff_addr,
			    g_vid_mode_frame_num_bytes[softc_p->cur_vid_mode],
			    0)) {
				return (EFAULT);
			}

			/*
			 * if during the course of copying frame data,
			 * the device driver invalidated this read()
			 * request processing operation; restart this
			 * operation
			 */

			mutex_enter(&softc_p->dcam_frame_is_done_mutex);

			read_req_invalid = softc_p->reader_flags[read_ptr_id] &
			    DCAM1394_FLAG_READ_REQ_INVALID;

			softc_p->reader_flags[read_ptr_id] &=
			    ~(DCAM1394_FLAG_READ_REQ_INVALID);

			mutex_exit(&softc_p->dcam_frame_is_done_mutex);

		} else {
			mutex_exit(&softc_p->dcam_frame_is_done_mutex);
			return (EAGAIN);
		}

		mutex_enter(&softc_p->dcam_frame_is_done_mutex);
	} while (read_req_invalid);

	/*
	 * return number of bytes actually written to user space
	 */
	uio_p->uio_resid -= g_vid_mode_frame_num_bytes[softc_p->cur_vid_mode];

	softc_p->reader_flags[read_ptr_id] &= ~(DCAM1394_FLAG_READ_REQ_PROC);

	/* increment read pointer */
	ring_buff_read_ptr_incr(ring_buff_p, read_ptr_id);

	mutex_exit(&softc_p->dcam_frame_is_done_mutex);

	return (0);
}


/*
 * dcam_ioctl
 */
/* ARGSUSED */
int
dcam_ioctl(dev_t dev, int cmd, intptr_t  arg, int mode, cred_t *cred_p,
    int *rvalp)
{
	dcam_state_t		*softc_p;
	dcam1394_param_list_t	*param_list;
	dcam1394_reg_io_t	 dcam_reg_io;
	int			 instance, is_ctrl_file, rc, i;

	rc = 0;
	param_list = (dcam1394_param_list_t *)0;

	instance = DEV_TO_INSTANCE(dev);

	if ((softc_p = ddi_get_soft_state(dcam_state_p, instance)) == NULL) {
		rc = ENXIO;
		goto done;
	}

	/*
	 * determine user applications data model
	 */
	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
		softc_p->usr_model = ILP32_PTR_SIZE;
	else
		softc_p->usr_model = LP64_PTR_SIZE;


	switch (cmd) {

	case DCAM1394_CMD_REG_READ:
		if (ddi_copyin((caddr_t)arg, &dcam_reg_io,
		    sizeof (dcam1394_reg_io_t), mode)) {
			rc = EFAULT;
			goto done;
		}

		if (dcam_reg_read(softc_p, &dcam_reg_io)) {
			rc = EFAULT;
			goto done;
		}

		if (ddi_copyout(&dcam_reg_io, (caddr_t)arg,
		    sizeof (dcam1394_reg_io_t), mode)) {
			rc = EFAULT;
			goto done;
		}
		break;

	case DCAM1394_CMD_REG_WRITE:
		if (ddi_copyin((caddr_t)arg, &dcam_reg_io,
		    sizeof (dcam1394_reg_io_t), mode)) {
			rc = EFAULT;
			goto done;
		}

		if (dcam_reg_write(softc_p, &dcam_reg_io)) {
			rc = EFAULT;
			goto done;
		}

		if (ddi_copyout(&dcam_reg_io, (caddr_t)arg,
		    sizeof (dcam1394_reg_io_t), mode)) {
			rc = EFAULT;
			goto done;
		}
		break;

	case DCAM1394_CMD_CAM_RESET:
		if (dcam_reset(softc_p)) {
			rc = EIO;
			goto done;
		}
		break;

	case DCAM1394_CMD_PARAM_GET:
		param_list = (dcam1394_param_list_t *)
		    kmem_alloc(sizeof (dcam1394_param_list_t), KM_SLEEP);

		if (ddi_copyin((caddr_t)arg, (caddr_t)param_list,
		    sizeof (dcam1394_param_list_t), mode)) {
			rc = EFAULT;
			goto done;
		}

		if (dcam1394_ioctl_param_get(softc_p, *param_list)) {
			rc = EINVAL;
		}

		if (ddi_copyout((caddr_t)param_list, (caddr_t)arg,
		    sizeof (dcam1394_param_list_t), mode)) {
			rc = EFAULT;
			goto done;
		}
		break;

	case DCAM1394_CMD_PARAM_SET:
		param_list = (dcam1394_param_list_t *)
		    kmem_alloc((size_t)sizeof (dcam1394_param_list_t),
		    KM_SLEEP);

		if (ddi_copyin((caddr_t)arg, (caddr_t)param_list,
		    sizeof (dcam1394_param_list_t), mode)) {
			rc = EFAULT;
			goto done;
		}

		is_ctrl_file = (getminor(dev) & DCAM1394_MINOR_CTRL) ? 1:0;

		if (dcam1394_ioctl_param_set(softc_p, is_ctrl_file,
		    *param_list)) {
			rc = EINVAL;
		}

		if (is_ctrl_file) {
			mutex_enter(&softc_p->dcam_frame_is_done_mutex);
			softc_p->param_status |= DCAM1394_STATUS_PARAM_CHANGE;
			mutex_exit(&softc_p->dcam_frame_is_done_mutex);
		}

		if (ddi_copyout(param_list, (caddr_t)arg,
		    sizeof (dcam1394_param_list_t), mode)) {
			rc = EFAULT;
			goto done;
		}
		break;

	case DCAM1394_CMD_FRAME_RCV_START:
		if (dcam1394_ioctl_frame_rcv_start(softc_p)) {
			rc = ENXIO;
		}
		break;

	case DCAM1394_CMD_FRAME_RCV_STOP:
		if (dcam_frame_rcv_stop(softc_p)) {
			rc = ENXIO;
		}
		break;

	case DCAM1394_CMD_RING_BUFF_FLUSH:
		if (softc_p->ring_buff_p == NULL) {
			rc = EAGAIN;
			break;
		}

		/*
		 * the simplest way to flush ring_buff is to empty it
		 */
		for (i = 0; i < softc_p->ring_buff_p->num_read_ptrs; i++) {
			softc_p->ring_buff_p->read_ptr_pos[i] =
			    softc_p->ring_buff_p->write_ptr_pos;

			/*
			 * if device driver is processing a user
			 * process's read() request
			 */
			if (softc_p->reader_flags[i] &
			    DCAM1394_FLAG_READ_REQ_PROC) {

				/*
				 * invalidate the read() request processing
				 * operation
				 */
				softc_p->reader_flags[i] |=
				    DCAM1394_FLAG_READ_REQ_INVALID;
			}
		}
		break;

	case DCAM1394_CMD_FRAME_SEQ_NUM_COUNT_RESET:
		mutex_enter(&softc_p->dcam_frame_is_done_mutex);
		softc_p->seq_count = 0;
		mutex_exit(&softc_p->dcam_frame_is_done_mutex);
		break;

	default:
		rc = EIO;
		break;

	}

done:
	if (param_list)
		kmem_free(param_list, sizeof (dcam1394_param_list_t));

	return (rc);
}


/* ARGSUSED */
int
dcam_chpoll(dev_t dev, short events, int anyyet, short *reventsp,
    struct pollhead **phpp)
{
	dcam_state_t	*softc_p;
	int		instance;
	short		revent = 0;

	/*
	 * Without the logic to perform wakeups (see comment below), reject
	 * attempts at edge-triggered polling.
	 */
	if (events & POLLET) {
		return (EPERM);
	}

	instance = DEV_TO_INSTANCE(dev);
	softc_p = (dcam_state_t *)ddi_get_soft_state(dcam_state_p, instance);
	if (softc_p == NULL) {
		return (ENXIO);
	}

	if (softc_p->ring_buff_p != NULL) {
		size_t read_ptr_pos, write_ptr_pos;

		mutex_enter(&softc_p->dcam_frame_is_done_mutex);
		read_ptr_pos =
		    ring_buff_read_ptr_pos_get(softc_p->ring_buff_p, 0);
		write_ptr_pos =
		    ring_buff_write_ptr_pos_get(softc_p->ring_buff_p);
		mutex_exit(&softc_p->dcam_frame_is_done_mutex);

		if ((events & POLLRDNORM) && read_ptr_pos != write_ptr_pos) {
			revent |= POLLRDNORM;
		}
	}

	if ((events & POLLPRI) && softc_p->param_status) {
		revent |= POLLPRI;
	}

	/*
	 * No portion of this driver was ever wired up to perform a
	 * pollwakeup() on an associated pollhead.  The lack of an emitted
	 * pollhead informs poll/devpoll that the event status of this resource
	 * is not cacheable.
	 */
	*reventsp = revent;

	return (0);
}


/*
 * dcam_bus_reset_notify
 */
/* ARGSUSED */
void
dcam_bus_reset_notify(dev_info_t *dip, ddi_eventcookie_t ev_cookie, void *arg,
    void *impl_data)
{

	dcam_state_t 		*softc_p;
	t1394_localinfo_t 	*localinfo = impl_data;
	t1394_targetinfo_t 	targetinfo;

	softc_p = arg;

	/*
	 * this is needed to handle LG camera "changing GUID" bug
	 * XXX: What's this about?
	 */
	if ((dip == NULL) || (arg == NULL) || (impl_data == NULL) ||
	    (softc_p->sl_handle == NULL)) {
		return;
	}

	localinfo = impl_data;

	/*
	 * simply return if no target info
	 */
	if (t1394_get_targetinfo(softc_p->sl_handle,
	    localinfo->bus_generation, 0, &targetinfo) != DDI_SUCCESS)
		return;

	if (localinfo->local_nodeID == softc_p->targetinfo.target_nodeID) {
		softc_p->param_status |= DCAM1394_STATUS_CAM_UNPLUG;
	} else {
		softc_p->param_status &= ~DCAM1394_STATUS_CAM_UNPLUG;
	}

	/* struct copies */
	softc_p->attachinfo.localinfo = *localinfo;

	if (targetinfo.target_nodeID != T1394_INVALID_NODEID) {
		softc_p->targetinfo.current_max_payload =
		    targetinfo.current_max_payload;

		softc_p->targetinfo.current_max_speed =
		    targetinfo.current_max_speed;

		softc_p->targetinfo.target_nodeID =
		    targetinfo.target_nodeID;
	}
}


/*
 * byte_copy_to_user_buff
 */
static int
byte_copy_to_user_buff(uchar_t *src_addr_p, struct uio *uio_p, size_t num_bytes,
    int start_index, int *end_index_p)
{
	int	 index;
	size_t	 len;
	uchar_t	*u8_p;

	index = start_index;
	u8_p  = (uchar_t *)src_addr_p;

	while (num_bytes) {

		len = num_bytes;

		if (uiomove(u8_p, len, UIO_READ, uio_p)) {
			return (-1);
		}

		index++;
		u8_p		+= len;
		num_bytes	-= len;
	}

	*end_index_p = index;

	return (0);
}


/*
 * byte_copy_from_user_buff
 */
static int
byte_copy_from_user_buff(uchar_t *dst_addr_p, struct uio *uio_p,
    size_t num_bytes, int start_index, int *end_index_p)
{
	int	 index;
	size_t	 len;
	uchar_t	*u8_p;

	index = start_index;
	u8_p  = (uchar_t *)dst_addr_p;

	while (num_bytes) {
		len = num_bytes;

		if (uiomove(u8_p, len, UIO_WRITE, uio_p)) {
			return (-1);

		}

		index++;
		u8_p		+= len;
		num_bytes	-= len;

	}

	*end_index_p = index;

	return (0);
}


/*
 * dcam_reset()
 */
static int
dcam_reset(dcam_state_t *softc_p)
{
	dcam1394_reg_io_t dcam_reg_io;

	dcam_reg_io.offs = DCAM1394_REG_OFFS_INITIALIZE;
	dcam_reg_io.val  = DCAM1394_REG_VAL_INITIALIZE_ASSERT;

	if (dcam_reg_write(softc_p, &dcam_reg_io)) {
		return (-1);
	}

	/*
	 * If the camera has a TI VSP, tweak the iris feature
	 * to "on" and value 4.
	 */
	dcam_reg_io.offs = DCAM1394_REG_OFFS_FEATURE_CSR_BASE +
	    DCAM1394_REG_OFFS_IRIS_CSR;
	dcam_reg_io.val  = 0x82000004;

	if (dcam_reg_write(softc_p, &dcam_reg_io)) {
		return (-1);
	}

	return (0);
}