Implement RGB gain/balance
[microdia.git] / microdia-sysfs.c
blobf1fff9808d9527228eb500b347d714c937a818a8
1 /**
2 * @file microdia-sysfs.c
3 * @author Nicolas VIVIEN
4 * @date 2008-02-01
5 * @version v0.0.0
7 * @brief Driver for Microdia USB video camera
9 * @note Copyright (C) Nicolas VIVIEN
11 * @par Licences
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/version.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/kref.h>
35 #include <linux/device.h>
37 #include <linux/usb.h>
38 #include <media/v4l2-common.h>
40 #include "microdia.h"
41 #include "sn9c20x.h"
44 extern const struct microdia_coord microdia_image_sizes[MICRODIA_NBR_SIZES];
47 /**
48 * @brief show_release
50 * @param class Class device
52 * @retval buf Adress of buffer with the 'release' value
54 * @returns Size of buffer
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
57 static ssize_t show_release(struct class_device *class, char *buf)
58 #else
59 static ssize_t show_release(struct device *class, struct device_attribute *attr, char *buf)
60 #endif
62 struct video_device *vdev = to_video_device(class);
63 struct usb_microdia *dev = video_get_drvdata(vdev);
65 return sprintf(buf, "%d\n", dev->release);
69 /**
70 * @brief show_videostatus
72 * @param class Class device
74 * @retval buf Adress of buffer with the 'videostatus' value
76 * @returns Size of buffer
78 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
79 static ssize_t show_videostatus(struct class_device *class, char *buf)
80 #else
81 static ssize_t show_videostatus(struct device *class, struct device_attribute *attr, char *buf)
82 #endif
84 struct video_device *vdev = to_video_device(class);
85 struct usb_microdia *dev = video_get_drvdata(vdev);
87 return sprintf(buf,
88 "Overflow frames : %d\n"
89 "Incomplete frames : %d\n"
90 "Dropped frames : %d\n",
91 dev->vframes_overflow,
92 dev->vframes_incomplete,
93 dev->vframes_dropped);
97 /**
98 * @brief show_informations
100 * @param class Class device
102 * @retval buf Adress of buffer with the 'informations' value
104 * @returns Size of buffer
106 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
107 static ssize_t show_informations(struct class_device *class, char *buf)
108 #else
109 static ssize_t show_informations(struct device *class, struct device_attribute *attr, char *buf)
110 #endif
112 int width, height;
113 char *pixelfmt = NULL;
115 struct video_device *vdev = to_video_device(class);
116 struct usb_microdia *dev = video_get_drvdata(vdev);
118 char *palette_bgr24 = "BGR24 - BGR-8-8-8 - 24 bits";
119 char *palette_rgb24 = "RGB24 - RGB-8-8-8 - 24 bits";
120 char *palette_yuyv = "YUYV - YUV 4:2:2 - 16 bits";
121 char *palette_i420 = "I420 - YUV 4:2:0 - 12 bits";
124 switch (dev->vsettings.format.pixelformat) {
125 case V4L2_PIX_FMT_BGR24:
126 pixelfmt = palette_bgr24;
127 break;
129 case V4L2_PIX_FMT_RGB24:
130 pixelfmt = palette_rgb24;
131 break;
133 case V4L2_PIX_FMT_YUYV:
134 pixelfmt = palette_yuyv;
135 break;
137 case V4L2_PIX_FMT_YUV420:
138 pixelfmt = palette_i420;
139 break;
142 switch (dev->resolution) {
143 case MICRODIA_80x60:
144 /*case MICRODIA_128x96:*/
145 case MICRODIA_160x120:
146 /*case MICRODIA_213x160: */
147 case MICRODIA_320x240:
148 case MICRODIA_640x480:
149 width = microdia_image_sizes[MICRODIA_640x480].x;
150 height = microdia_image_sizes[MICRODIA_640x480].y;
151 break;
153 case MICRODIA_800x600:
154 case MICRODIA_1024x768:
155 case MICRODIA_1280x1024:
156 width = microdia_image_sizes[MICRODIA_1280x1024].x;
157 height = microdia_image_sizes[MICRODIA_1280x1024].y;
158 break;
160 default:
161 width = 0;
162 height = 0;
165 return sprintf(buf,
166 "Asked resolution : %dx%d\n"
167 "Driver resolution : %dx%d\n"
168 "Webcam resolution : %dx%d\n"
169 "\n"
170 "%s\n"
171 "\n"
172 "Brightness : 0x%X\n"
173 "Contrast : 0x%X\n"
174 "Whiteness : 0x%X\n"
175 "Exposure : 0x%X\n"
176 "Sharpness : 0x%X\n"
177 "Horizontal flip : %d\n"
178 "Vertical flip : %d\n"
179 "Auto-exposure : %d\n"
180 "Auto-whitebalance : %d\n",
181 dev->view.x, dev->view.y,
182 microdia_image_sizes[dev->resolution].x, microdia_image_sizes[dev->resolution].y,
183 width, height,
184 pixelfmt,
185 0xFFFF & dev->vsettings.brightness,
186 0xFFFF & dev->vsettings.contrast,
187 0xFFFF & dev->vsettings.whiteness,
188 0xFFFF & dev->vsettings.exposure,
189 0x3F & dev->vsettings.sharpness,
190 dev->vsettings.hflip,
191 dev->vsettings.vflip,
192 dev->vsettings.auto_exposure,
193 dev->vsettings.auto_whitebalance);
197 /**
198 * @brief show_fps
200 * @param class Class device
202 * @retval buf Adress of buffer with the 'fps' value
204 * @returns Size of buffer
206 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
207 static ssize_t show_fps(struct class_device *class, char *buf)
208 #else
209 static ssize_t show_fps(struct device *class, struct device_attribute *attr, char *buf)
210 #endif
212 struct video_device *vdev = to_video_device(class);
213 struct usb_microdia *dev = video_get_drvdata(vdev);
215 return sprintf(buf, "%d\n", dev->vsettings.fps);
219 * @brief show_rgb_gain
221 * @param class Class device
223 * @retval buf Address of buffer with the 'rgb_gain' value
225 * @returns Size of buffer
227 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
228 static ssize_t show_rgb_gain(struct class_device *class, char *buf)
229 #else
230 static ssize_t show_rgb_gain(struct device *class,
231 struct device_attribute *attr, char *buf)
232 #endif
234 int rgb_gain = 0;
236 struct video_device *vdev = to_video_device(class);
237 struct usb_microdia *dev = video_get_drvdata(vdev);
238 rgb_gain |= dev->vsettings.rgb_gain[0] << 16;
239 rgb_gain |= dev->vsettings.rgb_gain[1] << 8;
240 rgb_gain |= dev->vsettings.rgb_gain[3];
241 return sprintf(buf, "0x%06X\n", rgb_gain);
245 * @brief store_exposure
247 * @param class Class device
248 * @param buf Buffer
249 * @param count Counter
251 * @returns Size of buffer
253 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
254 static ssize_t store_rgb_gain(struct class_device *class,
255 const char *buf, size_t count)
256 #else
257 static ssize_t store_rgb_gain(struct device *class,
258 struct device_attribute *attr, const char *buf, size_t count)
259 #endif
261 char *endp;
262 unsigned long rgb_gain;
264 struct video_device *vdev = to_video_device(class);
265 struct usb_microdia *dev = video_get_drvdata(vdev);
267 rgb_gain = simple_strtoul(buf, &endp, 16);
269 dev->vsettings.rgb_gain[0] = (rgb_gain >> 16) & 0x7f;
270 dev->vsettings.rgb_gain[1] = (rgb_gain >> 8) & 0x7f;
271 dev->vsettings.rgb_gain[2] = (rgb_gain >> 8) & 0x7f;
272 dev->vsettings.rgb_gain[3] = rgb_gain & 0x7f;
274 dev_microdia_camera_set_rgb_gain(dev);
276 return strlen(buf);
279 /**
280 * @brief show_exposure
282 * @param class Class device
284 * @retval buf Adress of buffer with the 'exposure' value
286 * @returns Size of buffer
288 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
289 static ssize_t show_exposure(struct class_device *class, char *buf)
290 #else
291 static ssize_t show_exposure(struct device *class, struct device_attribute *attr, char *buf)
292 #endif
294 struct video_device *vdev = to_video_device(class);
295 struct usb_microdia *dev = video_get_drvdata(vdev);
297 return sprintf(buf, "%X\n", dev->vsettings.exposure);
301 * @brief store_exposure
303 * @param class Class device
304 * @param buf Buffer
305 * @param count Counter
307 * @returns Size of buffer
309 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
310 static ssize_t store_exposure(struct class_device *class, const char *buf, size_t count)
311 #else
312 static ssize_t store_exposure(struct device *class, struct device_attribute *attr,
313 const char *buf, size_t count)
314 #endif
316 char *endp;
317 unsigned long value;
319 struct video_device *vdev = to_video_device(class);
320 struct usb_microdia *dev = video_get_drvdata(vdev);
322 value = simple_strtoul(buf, &endp, 16);
324 dev->vsettings.exposure = (int) value;
326 dev_microdia_camera_set_exposure(dev);
328 return strlen(buf);
331 /**
332 * @brief show_brightness
334 * @param class Class device
336 * @retval buf Adress of buffer with the 'brightness' value
338 * @returns Size of buffer
340 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
341 static ssize_t show_brightness(struct class_device *class, char *buf)
342 #else
343 static ssize_t show_brightness(struct device *class, struct device_attribute *attr, char *buf)
344 #endif
346 struct video_device *vdev = to_video_device(class);
347 struct usb_microdia *dev = video_get_drvdata(vdev);
349 return sprintf(buf, "%X\n", dev->vsettings.brightness);
352 /**
353 * @brief store_brightness
355 * @param class Class device
356 * @param buf Buffer
357 * @param count Counter
359 * @returns Size of buffer
361 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
362 static ssize_t store_brightness(struct class_device *class, const char *buf, size_t count)
363 #else
364 static ssize_t store_brightness(struct device *class, struct device_attribute *attr,
365 const char *buf, size_t count)
366 #endif
368 char *endp;
369 unsigned long value;
371 struct video_device *vdev = to_video_device(class);
372 struct usb_microdia *dev = video_get_drvdata(vdev);
374 value = simple_strtoul(buf, &endp, 16);
376 dev->vsettings.brightness = (int) value;
378 dev_microdia_camera_set_brightness(dev);
380 return strlen(buf);
383 /**
384 * @brief show_contrast
386 * @param class Class device
388 * @retval buf Adress of buffer with the 'contrast' value
390 * @returns Size of buffer
392 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
393 static ssize_t show_contrast(struct class_device *class, char *buf)
394 #else
395 static ssize_t show_contrast(struct device *class, struct device_attribute *attr, char *buf)
396 #endif
398 struct video_device *vdev = to_video_device(class);
399 struct usb_microdia *dev = video_get_drvdata(vdev);
401 return sprintf(buf, "%X\n", dev->vsettings.contrast);
405 /**
406 * @brief store_contrast
408 * @param class Class device
409 * @param buf Buffer
410 * @param count Counter
412 * @returns Size of buffer
414 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
415 static ssize_t store_contrast(struct class_device *class, const char *buf, size_t count)
416 #else
417 static ssize_t store_contrast(struct device *class, struct device_attribute *attr,
418 const char *buf, size_t count)
419 #endif
421 char *endp;
422 unsigned long value;
424 struct video_device *vdev = to_video_device(class);
425 struct usb_microdia *dev = video_get_drvdata(vdev);
427 value = simple_strtoul(buf, &endp, 16);
429 dev->vsettings.contrast = (int) value;
431 dev_microdia_camera_set_contrast(dev);
432 // dev_microdia_set_camera_quality(dev);
434 return strlen(buf);
438 /**
439 * @brief show_whitebalance
441 * @param class Class device
443 * @retval buf Adress of buffer with the 'whitebalance' value
445 * @returns Size of buffer
447 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
448 static ssize_t show_whitebalance(struct class_device *class, char *buf)
449 #else
450 static ssize_t show_whitebalance(struct device *class, struct device_attribute *attr, char *buf)
451 #endif
453 struct video_device *vdev = to_video_device(class);
454 struct usb_microdia *dev = video_get_drvdata(vdev);
456 return sprintf(buf, "%X\n", dev->vsettings.whiteness);
460 /**
461 * @brief store_whitebalance
463 * @param class Class device
464 * @param buf Buffer
465 * @param count Counter
467 * @returns Size of buffer
469 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
470 static ssize_t store_whitebalance(struct class_device *class, const char *buf, size_t count)
471 #else
472 static ssize_t store_whitebalance(struct device *class, struct device_attribute *attr,
473 const char *buf, size_t count)
474 #endif
476 char *endp;
477 unsigned long value;
479 struct video_device *vdev = to_video_device(class);
480 struct usb_microdia *dev = video_get_drvdata(vdev);
482 value = simple_strtoul(buf, &endp, 16);
484 dev->vsettings.whiteness = (int) value;
486 dev_microdia_camera_set_gamma(dev);
487 // dev_microdia_set_camera_quality(dev);
489 return strlen(buf);
493 /**
494 * @brief show_colour
496 * @param class Class device
498 * @retval buf Adress of buffer with the 'colour' value
500 * @returns Size of buffer
502 /*#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
503 static ssize_t show_colour(struct class_device *class, char *buf)
504 #else
505 static ssize_t show_colour(struct device *class, struct device_attribute *attr, char *buf)
506 #endif
508 struct video_device *vdev = to_video_device(class);
509 struct usb_microdia *dev = video_get_drvdata(vdev);
511 return sprintf(buf, "%X\n", dev->vsettings.colour);
515 /**
516 * @brief store_colour
518 * @param class Class device
519 * @param buf Buffer
520 * @param count Counter
522 * @returns Size of buffer
524 /*#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
525 static ssize_t store_colour(struct class_device *class, const char *buf, size_t count)
526 #else
527 static ssize_t store_colour(struct device *class, struct device_attribute *attr,
528 const char *buf, size_t count)
529 #endif
531 char *endp;
532 unsigned long value;
534 struct video_device *vdev = to_video_device(class);
535 struct usb_microdia *dev = video_get_drvdata(vdev);
537 value = simple_strtoul(buf, &endp, 16);
539 dev->vsettings.colour = (int) value;
541 // dev_microdia_set_camera_quality(dev);
543 return strlen(buf);
548 /**
549 * @brief show_sharpness
551 * @param class Class device
553 * @retval buf Adress of buffer with the 'sharpness' value
555 * @returns Size of buffer
557 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
558 static ssize_t show_sharpness(struct class_device *class, char *buf)
559 #else
560 static ssize_t show_sharpness(struct device *class, struct device_attribute *attr, char *buf)
561 #endif
563 struct video_device *vdev = to_video_device(class);
564 struct usb_microdia *dev = video_get_drvdata(vdev);
566 return sprintf(buf, "%X\n", dev->vsettings.sharpness);
569 /**
570 * @brief store_sharpness
572 * @param class Class device
573 * @param buf Buffer
574 * @param count Counter
576 * @returns Size of buffer
578 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
579 static ssize_t store_sharpness(struct class_device *class, const char *buf, size_t count)
580 #else
581 static ssize_t store_sharpness(struct device *class, struct device_attribute *attr,
582 const char *buf, size_t count)
583 #endif
585 char *endp;
586 unsigned long value;
588 struct video_device *vdev = to_video_device(class);
589 struct usb_microdia *dev = video_get_drvdata(vdev);
591 value = simple_strtoul(buf, &endp, 16);
593 if (value < 0 || value > 0x3f)
594 return -EINVAL;
596 dev->vsettings.sharpness = (int) value;
598 dev_microdia_camera_set_sharpness(dev);
600 return strlen(buf);
604 /**
605 * @brief show_hflip
607 * @param class Class device
609 * @retval buf Adress of buffer with the 'hflip' value
611 * @returns Size of buffer
613 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
614 static ssize_t show_hflip(struct class_device *class, char *buf)
615 #else
616 static ssize_t show_hflip(struct device *class, struct device_attribute *attr, char *buf)
617 #endif
619 struct video_device *vdev = to_video_device(class);
620 struct usb_microdia *dev = video_get_drvdata(vdev);
622 return sprintf(buf, "%d\n", dev->vsettings.hflip);
626 /**
627 * @brief store_hflip
629 * @param class Class device
630 * @param buf Buffer
631 * @param count Counter
633 * @returns Size of buffer
635 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
636 static ssize_t store_hflip(struct class_device *class, const char *buf, size_t count)
637 #else
638 static ssize_t store_hflip(struct device *class, struct device_attribute *attr,
639 const char *buf, size_t count)
640 #endif
642 struct video_device *vdev = to_video_device(class);
643 struct usb_microdia *dev = video_get_drvdata(vdev);
645 if (strncmp(buf, "1", 1) == 0)
646 dev->vsettings.hflip = 1;
647 else if (strncmp(buf, "0", 1) == 0)
648 dev->vsettings.hflip = 0;
649 else
650 return -EINVAL;
652 dev_microdia_camera_set_hvflip(dev);
654 return strlen(buf);
658 /**
659 * @brief show_vflip
661 * @param class Class device
663 * @retval buf Adress of buffer with the 'vflip' value
665 * @returns Size of buffer
667 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
668 static ssize_t show_vflip(struct class_device *class, char *buf)
669 #else
670 static ssize_t show_vflip(struct device *class, struct device_attribute *attr, char *buf)
671 #endif
673 struct video_device *vdev = to_video_device(class);
674 struct usb_microdia *dev = video_get_drvdata(vdev);
676 return sprintf(buf, "%d\n", dev->vsettings.vflip);
680 /**
681 * @brief store_vflip
683 * @param class Class device
684 * @param buf Buffer
685 * @param count Counter
687 * @returns Size of buffer
689 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
690 static ssize_t store_vflip(struct class_device *class, const char *buf, size_t count)
691 #else
692 static ssize_t store_vflip(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
693 #endif
695 struct video_device *vdev = to_video_device(class);
696 struct usb_microdia *dev = video_get_drvdata(vdev);
698 if (strncmp(buf, "1", 1) == 0)
699 dev->vsettings.vflip = 1;
700 else if (strncmp(buf, "0", 1) == 0)
701 dev->vsettings.vflip = 0;
702 else
703 return -EINVAL;
705 dev_microdia_camera_set_hvflip(dev);
707 return strlen(buf);
711 /**
712 * @brief show_autoexposure
714 * @param class Class device
716 * @retval buf Adress of buffer with the 'hflip' value
718 * @returns Size of buffer
720 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
721 static ssize_t show_autoexposure(struct class_device *class, char *buf)
722 #else
723 static ssize_t show_autoexposure(struct device *class, struct device_attribute *attr, char *buf)
724 #endif
726 struct video_device *vdev = to_video_device(class);
727 struct usb_microdia *dev = video_get_drvdata(vdev);
729 return sprintf(buf, "%d\n", dev->vsettings.auto_exposure);
733 /**
734 * @brief store_autoexposure
736 * @param class Class device
737 * @param buf Buffer
738 * @param count Counter
740 * @returns Size of buffer
742 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
743 static ssize_t store_autoexposure(struct class_device *class, const char *buf, size_t count)
744 #else
745 static ssize_t store_autoexposure(struct device *class, struct device_attribute *attr,
746 const char *buf, size_t count)
747 #endif
749 struct video_device *vdev = to_video_device(class);
750 struct usb_microdia *dev = video_get_drvdata(vdev);
752 if (strncmp(buf, "1", 1) == 0)
753 dev->vsettings.auto_exposure = 1;
754 else if (strncmp(buf, "0", 1) == 0)
755 dev->vsettings.auto_exposure = 0;
756 else
757 return -EINVAL;
759 dev_microdia_camera_set_auto_exposure(dev);
761 return strlen(buf);
765 /**
766 * @brief show_autowhitebalance
768 * @param class Class device
770 * @retval buf Adress of buffer with the 'vflip' value
772 * @returns Size of buffer
774 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
775 static ssize_t show_autowhitebalance(struct class_device *class, char *buf)
776 #else
777 static ssize_t show_autowhitebalance(struct device *class, struct device_attribute *attr, char *buf)
778 #endif
780 struct video_device *vdev = to_video_device(class);
781 struct usb_microdia *dev = video_get_drvdata(vdev);
783 return sprintf(buf, "%d\n", dev->vsettings.auto_whitebalance);
787 /**
788 * @brief store_autowhitebalance
790 * @param class Class device
791 * @param buf Buffer
792 * @param count Counter
794 * @returns Size of buffer
796 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
797 static ssize_t store_autowhitebalance(struct class_device *class, const char *buf, size_t count)
798 #else
799 static ssize_t store_autowhitebalance(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
800 #endif
802 struct video_device *vdev = to_video_device(class);
803 struct usb_microdia *dev = video_get_drvdata(vdev);
805 if (strncmp(buf, "1", 1) == 0)
806 dev->vsettings.auto_whitebalance = 1;
807 else if (strncmp(buf, "0", 1) == 0)
808 dev->vsettings.auto_whitebalance = 0;
809 else
810 return -EINVAL;
812 dev_microdia_camera_set_auto_whitebalance(dev);
814 return strlen(buf);
818 /**
819 * @brief show_register adress
821 * @param class Class device
823 * @retval buf Adress of buffer with the 'register' value
825 * @returns Size of buffer
827 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
828 static ssize_t show_register(struct class_device *class, char *buf)
829 #else
830 static ssize_t show_register(struct device *class, struct device_attribute *attr, char *buf)
831 #endif
833 __u8 buf1;
835 struct video_device *vdev = to_video_device(class);
836 struct usb_microdia *dev = video_get_drvdata(vdev);
838 usb_microdia_control_read(dev, dev->vsettings.reg, &buf1, 1);
840 dev->vsettings.val = buf1;
842 return sprintf(buf, "0x%x = 0x%x\n",dev->vsettings.reg, dev->vsettings.val);
846 /**
847 * @brief store_register
849 * @param class Class device
850 * @param buf Buffer
851 * @param count Counter
853 * @returns Size of buffer
855 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
856 static ssize_t store_register(struct class_device *class, const char *buf, size_t count)
857 #else
858 static ssize_t store_register(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
859 #endif
861 char *endp;
862 unsigned long value;
863 __u8 buf1;
865 struct video_device *vdev = to_video_device(class);
866 struct usb_microdia *dev = video_get_drvdata(vdev);
868 value = simple_strtoul(buf, &endp, 16);
870 dev->vsettings.val = (int) value;
871 buf1 = dev->vsettings.val;
872 usb_microdia_control_write(dev, dev->vsettings.reg, &buf1, 1);
874 return strlen(buf);
877 /**
878 * @brief show_adress
880 * @param class Class device
882 * @retval buf Buffer
884 * @returns Size of buffer
886 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
887 static ssize_t show_adress(struct class_device *class, char *buf)
888 #else
889 static ssize_t show_adress(struct device *class, struct device_attribute *attr, char *buf)
890 #endif
892 struct video_device *vdev = to_video_device(class);
893 struct usb_microdia *dev = video_get_drvdata(vdev);
895 return sprintf(buf, "0x%x\n", dev->vsettings.reg);
899 /**
900 * @brief store_adress
902 * @param class Class device
903 * @param buf Buffer
904 * @param count Counter
906 * @returns Size of buffer
908 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
909 static ssize_t store_adress(struct class_device *class, const char *buf, size_t count)
910 #else
911 static ssize_t store_adress(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
912 #endif
914 char *endp;
915 unsigned long value;
917 struct video_device *vdev = to_video_device(class);
918 struct usb_microdia *dev = video_get_drvdata(vdev);
920 value = simple_strtoul(buf, &endp, 16);
922 dev->vsettings.reg = (int) value;
924 return strlen(buf);
927 /**
928 * @brief show_i2cadr
930 * @param class Class device
932 * @retval buf Buffer
934 * @returns Size of buffer
936 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
937 static ssize_t show_i2cadr(struct class_device *class, char *buf)
938 #else
939 static ssize_t show_i2cadr(struct device *class, struct device_attribute *attr, char *buf)
940 #endif
942 struct video_device *vdev = to_video_device(class);
943 struct usb_microdia *dev = video_get_drvdata(vdev);
945 return sprintf(buf, "0x%x\n", dev->vsettings.i2creg);
949 /**
950 * @brief store_i2cadr
952 * @param class Class device
953 * @param buf Buffer
954 * @param count Counter
956 * @returns Size of buffer
958 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
959 static ssize_t store_i2cadr(struct class_device *class, const char *buf, size_t count)
960 #else
961 static ssize_t store_i2cadr(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
962 #endif
964 char *endp;
965 unsigned long value;
967 struct video_device *vdev = to_video_device(class);
968 struct usb_microdia *dev = video_get_drvdata(vdev);
970 value = simple_strtoul(buf, &endp, 16);
972 dev->vsettings.i2creg = (int) value;
974 return strlen(buf);
977 /**
978 * @brief show_i2creg adress
980 * @param class Class device
982 * @retval buf Buffer
984 * @returns Size of buffer
986 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
987 static ssize_t show_i2creg(struct class_device *class, char *buf)
988 #else
989 static ssize_t show_i2creg(struct device *class, struct device_attribute *attr, char *buf)
990 #endif
992 __u8 buf1[8];
993 // __u8 sensor_slave_id = 0x30; /* slave ID currently hard coded !! */
994 __u8 i2c_flags = SN9C20X_I2C_2WIRE;
996 struct video_device *vdev = to_video_device(class);
997 struct usb_microdia *dev = video_get_drvdata(vdev);
999 sn9c20x_read_i2c_data(dev, dev->vsettings.i2cslave /*sensor_slave_id*/,
1000 dev->vsettings.reg, dev->vsettings.i2creg, i2c_flags, buf1);
1002 dev->vsettings.i2cval = buf1[0];
1003 return sprintf(buf, "I2C-register 0x%x = 0x%x 0x%x (%d)\n",
1004 dev->vsettings.i2creg,buf1[0],buf1[1],dev->vsettings.reg);
1005 // return strlen(buf);
1009 /**
1010 * @brief store_i2creg
1012 * @param class Class device
1013 * @param buf Buffer
1014 * @param count Counter
1016 * @returns Size of buffer
1018 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1019 static ssize_t store_i2creg(struct class_device *class, const char *buf, size_t count)
1020 #else
1021 static ssize_t store_i2creg(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
1022 #endif
1024 char *endp;
1025 unsigned long value;
1027 __u8 buf1[5];
1028 // __u8 sensor_slave_id = 0x30; /* slave ID currently hard coded !! */
1029 __u8 i2c_flags = SN9C20X_I2C_2WIRE;
1031 struct video_device *vdev = to_video_device(class);
1032 struct usb_microdia *dev = video_get_drvdata(vdev);
1034 value = simple_strtoul(buf, &endp, 16);
1036 dev->vsettings.i2cval = (int) value;
1037 buf1[0] = ((dev->vsettings.i2cval & 0xff00) >> 8);
1038 buf1[1] = (dev->vsettings.i2cval & 0xFF);
1039 buf1[2] = 0;
1040 buf1[3] = 0;
1041 buf1[4] = 0;
1043 sn9c20x_write_i2c_data(dev,dev->vsettings.i2cslave /*sensor_slave_id*/,
1044 dev->vsettings.reg, dev->vsettings.i2creg, i2c_flags, buf1);
1045 // sprintf(buf, "store_i2creg: set I2C-reg. 0x%x to 0x%x 0x%x (%d)\n",
1046 // dev->vsettings.i2creg,buf1[0],buf1[1],dev->vsettings.reg);
1047 return strlen(buf);
1051 /**
1052 * @brief store_i2c_slave_adr
1054 * @param class Class device
1055 * @param buf Buffer
1056 * @param count Counter
1058 * @returns Size of buffer
1060 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1061 static ssize_t store_i2c_slave_adr(struct class_device *class, const char *buf, size_t count)
1062 #else
1063 static ssize_t store_i2c_slave_adr(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
1064 #endif
1066 char *endp;
1067 unsigned long value;
1069 struct video_device *vdev = to_video_device(class);
1070 struct usb_microdia *dev = video_get_drvdata(vdev);
1072 value = simple_strtoul(buf, &endp, 16);
1074 dev->vsettings.i2cslave = (int) value;
1076 return strlen(buf);
1080 /**
1081 * @brief show_i2c_slave_adr
1083 * @param class Class device
1085 * @retval buf Buffer
1087 * @returns Size of buffer
1089 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1090 static ssize_t show_i2c_slave_adr(struct class_device *class, char *buf)
1091 #else
1092 static ssize_t show_i2c_slave_adr(struct device *class, struct device_attribute *attr, char *buf)
1093 #endif
1095 struct video_device *vdev = to_video_device(class);
1096 struct usb_microdia *dev = video_get_drvdata(vdev);
1098 return sprintf(buf, "0x%x\n", dev->vsettings.i2cslave);
1104 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1105 static CLASS_DEVICE_ATTR(release, S_IRUGO, show_release, NULL); /**< Release value */
1106 static CLASS_DEVICE_ATTR(videostatus, S_IRUGO, show_videostatus, NULL); /**< Video status */
1107 static CLASS_DEVICE_ATTR(informations, S_IRUGO, show_informations, NULL); /**< Informations */
1108 static CLASS_DEVICE_ATTR(fps, S_IRUGO, show_fps, NULL); /**< FPS value */
1109 static CLASS_DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO, show_brightness, store_brightness); /**< Brightness value */
1110 static CLASS_DEVICE_ATTR(rgb_gain, S_IRUGO | S_IWUGO, show_rgb_gain, store_rgb_gain); /**< RGB Gain */
1111 static CLASS_DEVICE_ATTR(exposure, S_IRUGO | S_IWUGO, show_exposure, store_exposure); /**< Brightness exposure */
1112 static CLASS_DEVICE_ATTR(contrast, S_IRUGO | S_IWUGO, show_contrast, store_contrast); /**< Contrast value */
1113 static CLASS_DEVICE_ATTR(whitebalance, S_IRUGO | S_IWUGO, show_whitebalance, store_whitebalance); /**< Whitebalance value */
1114 static CLASS_DEVICE_ATTR(sharpness, S_IRUGO | S_IWUGO, show_sharpness, store_sharpness); /**< Sharpness value */
1115 //static CLASS_DEVICE_ATTR(colour, S_IRUGO | S_IWUGO, show_colour, store_colour); /**< Hue value */
1116 static CLASS_DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip); /**< Horizontal flip value */
1117 static CLASS_DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip); /**< Vertical flip value */
1118 static CLASS_DEVICE_ATTR(auto_exposure, S_IRUGO | S_IWUGO, show_autoexposure, store_autoexposure); /**< Automatic exposure control value */
1119 static CLASS_DEVICE_ATTR(auto_whitebalance, S_IRUGO | S_IWUGO, show_autowhitebalance, store_autowhitebalance); /**< Automatic whitebalance control value */
1120 static CLASS_DEVICE_ATTR(adr, S_IRUGO | S_IWUGO, show_adress, store_adress); /**< Register adress */
1121 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUGO, show_register, store_register); /**< Register value */
1122 static CLASS_DEVICE_ATTR(i2cadr, S_IRUGO | S_IWUGO, show_i2cadr, store_i2cadr); /**< I2C Register adress */
1123 static CLASS_DEVICE_ATTR(i2creg, S_IRUGO | S_IWUGO, show_i2creg, store_i2creg); /**< I2C Register value */
1124 static CLASS_DEVICE_ATTR(i2cslave, S_IRUGO | S_IWUGO, show_i2c_slave_adr, store_i2c_slave_adr); /**< I2C Slave adress */
1125 #else
1126 static DEVICE_ATTR(release, S_IRUGO, show_release, NULL); /**< Release value */
1127 static DEVICE_ATTR(videostatus, S_IRUGO, show_videostatus, NULL); /**< Video status */
1128 static DEVICE_ATTR(informations, S_IRUGO, show_informations, NULL); /**< Informations */
1129 static DEVICE_ATTR(fps, S_IRUGO, show_fps, NULL); /**< FPS value */
1130 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO, show_brightness, store_brightness); /**< Brightness value */
1131 static DEVICE_ATTR(rgb_gain, S_IRUGO | S_IWUGO, show_rgb_gain, store_rgb_gain); /**< RGB Gain */
1132 static DEVICE_ATTR(exposure, S_IRUGO | S_IWUGO, show_exposure, store_exposure); /**< Exposure value */
1133 static DEVICE_ATTR(contrast, S_IRUGO | S_IWUGO, show_contrast, store_contrast); /**< Contrast value */
1134 static DEVICE_ATTR(whitebalance, S_IRUGO | S_IWUGO, show_whitebalance, store_whitebalance); /**< Whitebalance value */
1135 static DEVICE_ATTR(sharpness, S_IRUGO | S_IWUGO, show_sharpness, store_sharpness); /**< Sharpness value */
1136 //static DEVICE_ATTR(colour, S_IRUGO | S_IWUGO, show_colour, store_colour); /**< Hue value */
1137 static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip); /**< Horizontal flip value */
1138 static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip); /**< Vertical flip value */
1139 static DEVICE_ATTR(auto_exposure, S_IRUGO | S_IWUGO, show_autoexposure, store_autoexposure); /**< Automatic exposure control value */
1140 static DEVICE_ATTR(auto_whitebalance, S_IRUGO | S_IWUGO, show_autowhitebalance, store_autowhitebalance); /**< Automatic whitebalance control value */
1141 static DEVICE_ATTR(adr, S_IRUGO | S_IWUGO, show_adress, store_adress); /**< Register adress */
1142 static DEVICE_ATTR(reg, S_IRUGO | S_IWUGO, show_register, store_register); /**< Register value */
1143 static DEVICE_ATTR(i2cadr, S_IRUGO | S_IWUGO, show_i2cadr, store_i2cadr); /**< I2C Register adress */
1144 static DEVICE_ATTR(i2creg, S_IRUGO | S_IWUGO, show_i2creg, store_i2creg); /**< I2C Register value */
1145 static DEVICE_ATTR(i2cslave, S_IRUGO | S_IWUGO, show_i2c_slave_adr, store_i2c_slave_adr); /**< I2C Slave adress */
1146 #endif
1149 /**
1150 * @brief Create the 'sys' entries.
1152 * This function permits to create all the entries in the 'sys' filesystem.
1154 * @param vdev Video device structure
1156 * @returns 0 if all is OK
1158 int microdia_create_sysfs_files(struct video_device *vdev)
1160 int ret;
1162 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1163 ret = video_device_create_file(vdev, &class_device_attr_release);
1164 ret = video_device_create_file(vdev, &class_device_attr_videostatus);
1165 ret = video_device_create_file(vdev, &class_device_attr_informations);
1166 ret = video_device_create_file(vdev, &class_device_attr_fps);
1167 ret = video_device_create_file(vdev, &class_device_attr_brightness);
1168 ret = video_device_create_file(vdev, &class_device_attr_rgb_gain);
1169 ret = video_device_create_file(vdev, &class_device_attr_exposure);
1170 ret = video_device_create_file(vdev, &class_device_attr_contrast);
1171 ret = video_device_create_file(vdev, &class_device_attr_whitebalance);
1172 ret = video_device_create_file(vdev, &class_device_attr_sharpness);
1173 /*ret = video_device_create_file(vdev, &class_device_attr_colour);*/
1174 ret = video_device_create_file(vdev, &class_device_attr_hflip);
1175 ret = video_device_create_file(vdev, &class_device_attr_vflip);
1176 ret = video_device_create_file(vdev, &class_device_attr_auto_exposure);
1177 ret = video_device_create_file(vdev, &class_device_attr_auto_whitebalance);
1178 ret = video_device_create_file(vdev, &class_device_attr_reg);
1179 ret = video_device_create_file(vdev, &class_device_attr_adr);
1180 ret = video_device_create_file(vdev, &class_device_attr_i2creg);
1181 ret = video_device_create_file(vdev, &class_device_attr_i2cadr);
1182 ret = video_device_create_file(vdev, &class_device_attr_i2cslave);
1183 #else
1184 ret = video_device_create_file(vdev, &dev_attr_release);
1185 ret = video_device_create_file(vdev, &dev_attr_videostatus);
1186 ret = video_device_create_file(vdev, &dev_attr_informations);
1187 ret = video_device_create_file(vdev, &dev_attr_fps);
1188 ret = video_device_create_file(vdev, &dev_attr_brightness);
1189 ret = video_device_create_file(vdev, &dev_attr_rgb_gain);
1190 ret = video_device_create_file(vdev, &dev_attr_exposure);
1191 ret = video_device_create_file(vdev, &dev_attr_contrast);
1192 ret = video_device_create_file(vdev, &dev_attr_whitebalance);
1193 ret = video_device_create_file(vdev, &dev_attr_sharpness);
1194 /*ret = video_device_create_file(vdev, &dev_attr_colour);*/
1195 ret = video_device_create_file(vdev, &dev_attr_hflip);
1196 ret = video_device_create_file(vdev, &dev_attr_vflip);
1197 ret = video_device_create_file(vdev, &dev_attr_auto_exposure);
1198 ret = video_device_create_file(vdev, &dev_attr_auto_whitebalance);
1199 ret = video_device_create_file(vdev, &dev_attr_reg);
1200 ret = video_device_create_file(vdev, &dev_attr_adr);
1201 ret = video_device_create_file(vdev, &dev_attr_i2creg);
1202 ret = video_device_create_file(vdev, &dev_attr_i2cadr);
1203 ret = video_device_create_file(vdev, &dev_attr_i2cslave);
1204 #endif
1205 return ret;
1209 /**
1210 * @brief Remove the 'sys' entries.
1212 * This function permits to remove all the entries in the 'sys' filesystem.
1214 * @param vdev Video device structure
1216 * @returns 0 if all is OK
1218 void microdia_remove_sysfs_files(struct video_device *vdev)
1220 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1221 video_device_remove_file(vdev, &class_device_attr_release);
1222 video_device_remove_file(vdev, &class_device_attr_videostatus);
1223 video_device_remove_file(vdev, &class_device_attr_informations);
1224 video_device_remove_file(vdev, &class_device_attr_fps);
1225 video_device_remove_file(vdev, &class_device_attr_brightness);
1226 video_device_remove_file(vdev, &class_device_attr_exposure);
1227 video_device_remove_file(vdev, &class_device_attr_rgb_gain);
1228 video_device_remove_file(vdev, &class_device_attr_contrast);
1229 video_device_remove_file(vdev, &class_device_attr_whitebalance);
1230 video_device_remove_file(vdev, &class_device_attr_sharpness);
1231 /*video_device_remove_file(vdev, &class_device_attr_colour);*/
1232 video_device_remove_file(vdev, &class_device_attr_hflip);
1233 video_device_remove_file(vdev, &class_device_attr_vflip);
1234 video_device_remove_file(vdev, &class_device_attr_auto_exposure);
1235 video_device_remove_file(vdev, &class_device_attr_auto_whitebalance);
1236 video_device_remove_file(vdev, &class_device_attr_reg);
1237 video_device_remove_file(vdev, &class_device_attr_adr);
1238 video_device_remove_file(vdev, &class_device_attr_i2creg);
1239 video_device_remove_file(vdev, &class_device_attr_i2cadr);
1240 video_device_remove_file(vdev, &class_device_attr_i2cslave);
1241 #else
1242 video_device_remove_file(vdev, &dev_attr_release);
1243 video_device_remove_file(vdev, &dev_attr_videostatus);
1244 video_device_remove_file(vdev, &dev_attr_informations);
1245 video_device_remove_file(vdev, &dev_attr_fps);
1246 video_device_remove_file(vdev, &dev_attr_brightness);
1247 video_device_remove_file(vdev, &dev_attr_rgb_gain);
1248 video_device_remove_file(vdev, &dev_attr_exposure);
1249 video_device_remove_file(vdev, &dev_attr_contrast);
1250 video_device_remove_file(vdev, &dev_attr_whitebalance);
1251 video_device_remove_file(vdev, &dev_attr_sharpness);
1252 /*video_device_remove_file(vdev, &dev_attr_colour);*/
1253 video_device_remove_file(vdev, &dev_attr_hflip);
1254 video_device_remove_file(vdev, &dev_attr_vflip);
1255 video_device_remove_file(vdev, &dev_attr_auto_exposure);
1256 video_device_remove_file(vdev, &dev_attr_auto_whitebalance);
1257 video_device_remove_file(vdev, &dev_attr_reg);
1258 video_device_remove_file(vdev, &dev_attr_adr);
1259 video_device_remove_file(vdev, &dev_attr_i2creg);
1260 video_device_remove_file(vdev, &dev_attr_i2cadr);
1261 video_device_remove_file(vdev, &dev_attr_i2cslave);
1262 #endif