2 * @file microdia-sysfs.c
3 * @author Nicolas VIVIEN
7 * @brief Driver for Microdia USB video camera
9 * @note Copyright (C) Nicolas VIVIEN
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
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 * $Date: 2008-02-05 10:10:01 +0100 (mar, 05 fév 2008) $
30 * $Author: nicklas79 $
31 * $HeadURL: https://syntekdriver.svn.sourceforge.net/svnroot/syntekdriver/trunk/driver/microdia-sysfs.c $
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/version.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/kref.h>
41 #include <linux/device.h>
43 #include <linux/usb.h>
44 #include <media/v4l2-common.h>
49 extern const struct microdia_coord microdia_image_sizes
[MICRODIA_NBR_SIZES
];
55 * @param class Class device
57 * @retval buf Adress of buffer with the 'release' value
59 * @returns Size of buffer
61 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
62 static ssize_t
show_release(struct class_device
*class, char *buf
)
64 static ssize_t
show_release(struct device
*class, struct device_attribute
*attr
, char *buf
)
67 struct video_device
*vdev
= to_video_device(class);
68 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
70 return sprintf(buf
, "%d\n", dev
->release
);
75 * @brief show_videostatus
77 * @param class Class device
79 * @retval buf Adress of buffer with the 'videostatus' value
81 * @returns Size of buffer
83 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
84 static ssize_t
show_videostatus(struct class_device
*class, char *buf
)
86 static ssize_t
show_videostatus(struct device
*class, struct device_attribute
*attr
, char *buf
)
89 struct video_device
*vdev
= to_video_device(class);
90 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
93 "Nbr ISOC errors : %d\n"
94 "Nbr dropped frames : %d\n"
95 "Nbr dumped frames : %d\n",
103 * @brief show_informations
105 * @param class Class device
107 * @retval buf Adress of buffer with the 'informations' value
109 * @returns Size of buffer
111 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
112 static ssize_t
show_informations(struct class_device
*class, char *buf
)
114 static ssize_t
show_informations(struct device
*class, struct device_attribute
*attr
, char *buf
)
118 char *pixelfmt
= NULL
;
120 struct video_device
*vdev
= to_video_device(class);
121 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
123 char *palette_rgb24
= "RGB24 - RGB-8-8-8 - 24 bits";
124 char *palette_rgb32
= "RGB32 - RGB-8-8-8-8 - 32 bits";
125 char *palette_bgr24
= "BGR24 - BGR-8-8-8 - 24 bits";
126 char *palette_bgr32
= "BGR32 - BGR-8-8-8-8 - 32 bits";
127 char *palette_uyvy
= "UYVY - YUV 4:2:2 - 16 bits";
128 char *palette_yuyv
= "YUYV - YUV 4:2:2 - 16 bits";
129 char *palette_i420
= "I420 - YUV 4:2:0 - 12 bits";
132 switch (dev
->vsettings
.palette
) {
133 case MICRODIA_PALETTE_RGB24
:
134 pixelfmt
= palette_rgb24
;
137 case MICRODIA_PALETTE_RGB32
:
138 pixelfmt
= palette_rgb32
;
141 case MICRODIA_PALETTE_BGR24
:
142 pixelfmt
= palette_bgr24
;
145 case MICRODIA_PALETTE_BGR32
:
146 pixelfmt
= palette_bgr32
;
149 case MICRODIA_PALETTE_UYVY
:
150 pixelfmt
= palette_uyvy
;
153 case MICRODIA_PALETTE_YUYV
:
154 pixelfmt
= palette_yuyv
;
157 case MICRODIA_PALETTE_I420
:
158 pixelfmt
= palette_i420
;
162 switch (dev
->resolution
) {
164 /*case MICRODIA_128x96:*/
165 case MICRODIA_160x120
:
166 /*case MICRODIA_213x160: */
167 case MICRODIA_320x240
:
168 case MICRODIA_640x480
:
169 width
= microdia_image_sizes
[MICRODIA_640x480
].x
;
170 height
= microdia_image_sizes
[MICRODIA_640x480
].y
;
173 case MICRODIA_800x600
:
174 case MICRODIA_1024x768
:
175 case MICRODIA_1280x1024
:
176 width
= microdia_image_sizes
[MICRODIA_1280x1024
].x
;
177 height
= microdia_image_sizes
[MICRODIA_1280x1024
].y
;
186 "Asked resolution : %dx%d\n"
187 "Driver resolution : %dx%d\n"
188 "Webcam resolution : %dx%d\n"
192 "Brightness : 0x%X\n"
196 dev
->view
.x
, dev
->view
.y
,
197 microdia_image_sizes
[dev
->resolution
].x
, microdia_image_sizes
[dev
->resolution
].y
,
200 0xFFFF & dev
->vsettings
.brightness
,
201 0xFFFF & dev
->vsettings
.contrast
,
202 0xFFFF & dev
->vsettings
.whiteness
,
203 0xFFFF & dev
->vsettings
.colour
);
210 * @param class Class device
212 * @retval buf Adress of buffer with the 'fps' value
214 * @returns Size of buffer
216 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
217 static ssize_t
show_fps(struct class_device
*class, char *buf
)
219 static ssize_t
show_fps(struct device
*class, struct device_attribute
*attr
, char *buf
)
222 struct video_device
*vdev
= to_video_device(class);
223 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
225 return sprintf(buf
, "%d\n", dev
->vsettings
.fps
);
230 * @brief show_exposure
232 * @param class Class device
234 * @retval buf Adress of buffer with the 'exposure' value
236 * @returns Size of buffer
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
239 static ssize_t
show_exposure(struct class_device
*class, char *buf
)
241 static ssize_t
show_exposure(struct device
*class, struct device_attribute
*attr
, char *buf
)
244 struct video_device
*vdev
= to_video_device(class);
245 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
247 return sprintf(buf
, "%X\n", dev
->vsettings
.exposure
);
251 * @brief store_exposure
253 * @param class Class device
255 * @param count Counter
257 * @returns Size of buffer
259 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
260 static ssize_t
store_exposure(struct class_device
*class, const char *buf
, size_t count
)
262 static ssize_t
store_exposure(struct device
*class, struct device_attribute
*attr
,
263 const char *buf
, size_t count
)
269 struct video_device
*vdev
= to_video_device(class);
270 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
272 value
= simple_strtoul(buf
, &endp
, 16);
274 dev
->vsettings
.exposure
= (int) value
;
276 dev_microdia_camera_set_exposure(dev
);
282 * @brief show_brightness
284 * @param class Class device
286 * @retval buf Adress of buffer with the 'brightness' value
288 * @returns Size of buffer
290 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
291 static ssize_t
show_brightness(struct class_device
*class, char *buf
)
293 static ssize_t
show_brightness(struct device
*class, struct device_attribute
*attr
, char *buf
)
296 struct video_device
*vdev
= to_video_device(class);
297 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
299 return sprintf(buf
, "%X\n", dev
->vsettings
.brightness
);
303 * @brief store_brightness
305 * @param class Class device
307 * @param count Counter
309 * @returns Size of buffer
311 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
312 static ssize_t
store_brightness(struct class_device
*class, const char *buf
, size_t count
)
314 static ssize_t
store_brightness(struct device
*class, struct device_attribute
*attr
,
315 const char *buf
, size_t count
)
321 struct video_device
*vdev
= to_video_device(class);
322 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
324 value
= simple_strtoul(buf
, &endp
, 16);
326 dev
->vsettings
.brightness
= (int) value
;
328 dev_microdia_camera_set_brightness(dev
);
334 * @brief show_contrast
336 * @param class Class device
338 * @retval buf Adress of buffer with the 'contrast' value
340 * @returns Size of buffer
342 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
343 static ssize_t
show_contrast(struct class_device
*class, char *buf
)
345 static ssize_t
show_contrast(struct device
*class, struct device_attribute
*attr
, char *buf
)
348 struct video_device
*vdev
= to_video_device(class);
349 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
351 return sprintf(buf
, "%X\n", dev
->vsettings
.contrast
);
356 * @brief store_contrast
358 * @param class Class device
360 * @param count Counter
362 * @returns Size of buffer
364 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
365 static ssize_t
store_contrast(struct class_device
*class, const char *buf
, size_t count
)
367 static ssize_t
store_contrast(struct device
*class, struct device_attribute
*attr
,
368 const char *buf
, size_t count
)
374 struct video_device
*vdev
= to_video_device(class);
375 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
377 value
= simple_strtoul(buf
, &endp
, 16);
379 dev
->vsettings
.contrast
= (int) value
;
381 dev_microdia_camera_set_contrast(dev
);
382 // dev_microdia_set_camera_quality(dev);
389 * @brief show_whitebalance
391 * @param class Class device
393 * @retval buf Adress of buffer with the 'whitebalance' value
395 * @returns Size of buffer
397 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
398 static ssize_t
show_whitebalance(struct class_device
*class, char *buf
)
400 static ssize_t
show_whitebalance(struct device
*class, struct device_attribute
*attr
, char *buf
)
403 struct video_device
*vdev
= to_video_device(class);
404 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
406 return sprintf(buf
, "%X\n", dev
->vsettings
.whiteness
);
411 * @brief store_whitebalance
413 * @param class Class device
415 * @param count Counter
417 * @returns Size of buffer
419 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
420 static ssize_t
store_whitebalance(struct class_device
*class, const char *buf
, size_t count
)
422 static ssize_t
store_whitebalance(struct device
*class, struct device_attribute
*attr
,
423 const char *buf
, size_t count
)
429 struct video_device
*vdev
= to_video_device(class);
430 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
432 value
= simple_strtoul(buf
, &endp
, 16);
434 dev
->vsettings
.whiteness
= (int) value
;
436 // dev_microdia_set_camera_quality(dev);
445 * @param class Class device
447 * @retval buf Adress of buffer with the 'colour' value
449 * @returns Size of buffer
451 /*#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
452 static ssize_t show_colour(struct class_device *class, char *buf)
454 static ssize_t show_colour(struct device *class, struct device_attribute *attr, char *buf)
457 struct video_device *vdev = to_video_device(class);
458 struct usb_microdia *dev = video_get_drvdata(vdev);
460 return sprintf(buf, "%X\n", dev->vsettings.colour);
465 * @brief store_colour
467 * @param class Class device
469 * @param count Counter
471 * @returns Size of buffer
473 /*#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
474 static ssize_t store_colour(struct class_device *class, const char *buf, size_t count)
476 static ssize_t store_colour(struct device *class, struct device_attribute *attr,
477 const char *buf, size_t count)
483 struct video_device *vdev = to_video_device(class);
484 struct usb_microdia *dev = video_get_drvdata(vdev);
486 value = simple_strtoul(buf, &endp, 16);
488 dev->vsettings.colour = (int) value;
490 // dev_microdia_set_camera_quality(dev);
499 * @param class Class device
501 * @retval buf Adress of buffer with the 'hflip' value
503 * @returns Size of buffer
505 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
506 static ssize_t
show_hflip(struct class_device
*class, char *buf
)
508 static ssize_t
show_hflip(struct device
*class, struct device_attribute
*attr
, char *buf
)
511 struct video_device
*vdev
= to_video_device(class);
512 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
514 return sprintf(buf
, "%d\n", dev
->vsettings
.hflip
);
521 * @param class Class device
523 * @param count Counter
525 * @returns Size of buffer
527 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
528 static ssize_t
store_hflip(struct class_device
*class, const char *buf
, size_t count
)
530 static ssize_t
store_hflip(struct device
*class, struct device_attribute
*attr
,
531 const char *buf
, size_t count
)
534 struct video_device
*vdev
= to_video_device(class);
535 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
537 if (strncmp(buf
, "1", 1) == 0)
538 dev
->vsettings
.hflip
= 1;
539 else if (strncmp(buf
, "0", 1) == 0)
540 dev
->vsettings
.hflip
= 0;
551 * @param class Class device
553 * @retval buf Adress of buffer with the 'vflip' value
555 * @returns Size of buffer
557 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
558 static ssize_t
show_vflip(struct class_device
*class, char *buf
)
560 static ssize_t
show_vflip(struct device
*class, struct device_attribute
*attr
, char *buf
)
563 struct video_device
*vdev
= to_video_device(class);
564 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
566 return sprintf(buf
, "%d\n", dev
->vsettings
.vflip
);
573 * @param class Class device
575 * @param count Counter
577 * @returns Size of buffer
579 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
580 static ssize_t
store_vflip(struct class_device
*class, const char *buf
, size_t count
)
582 static ssize_t
store_vflip(struct device
*class, struct device_attribute
*attr
, const char *buf
, size_t count
)
585 struct video_device
*vdev
= to_video_device(class);
586 struct usb_microdia
*dev
= video_get_drvdata(vdev
);
588 if (strncmp(buf
, "1", 1) == 0)
589 dev
->vsettings
.vflip
= 1;
590 else if (strncmp(buf
, "0", 1) == 0)
591 dev
->vsettings
.vflip
= 0;
599 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
600 static CLASS_DEVICE_ATTR(release
, S_IRUGO
, show_release
, NULL
); /**< Release value */
601 static CLASS_DEVICE_ATTR(videostatus
, S_IRUGO
, show_videostatus
, NULL
); /**< Video status */
602 static CLASS_DEVICE_ATTR(informations
, S_IRUGO
, show_informations
, NULL
); /**< Informations */
603 static CLASS_DEVICE_ATTR(fps
, S_IRUGO
, show_fps
, NULL
); /**< FPS value */
604 static CLASS_DEVICE_ATTR(brightness
, S_IRUGO
| S_IWUGO
, show_brightness
, store_brightness
); /**< Brightness value */
605 static CLASS_DEVICE_ATTR(exposure
, S_IRUGO
| S_IWUGO
, show_exposure
, store_exposure
); /**< Brightness exposure */
606 static CLASS_DEVICE_ATTR(contrast
, S_IRUGO
| S_IWUGO
, show_contrast
, store_contrast
); /**< Contrast value */
607 static CLASS_DEVICE_ATTR(whitebalance
, S_IRUGO
| S_IWUGO
, show_whitebalance
, store_whitebalance
); /**< Whitebalance value */
608 //static CLASS_DEVICE_ATTR(colour, S_IRUGO | S_IWUGO, show_colour, store_colour); /**< Hue value */
609 static CLASS_DEVICE_ATTR(hflip
, S_IRUGO
| S_IWUGO
, show_hflip
, store_hflip
); /**< Horizontal filp value */
610 static CLASS_DEVICE_ATTR(vflip
, S_IRUGO
| S_IWUGO
, show_vflip
, store_vflip
); /**< Vertical filp value */
612 static DEVICE_ATTR(release
, S_IRUGO
, show_release
, NULL
); /**< Release value */
613 static DEVICE_ATTR(videostatus
, S_IRUGO
, show_videostatus
, NULL
); /**< Video status */
614 static DEVICE_ATTR(informations
, S_IRUGO
, show_informations
, NULL
); /**< Informations */
615 static DEVICE_ATTR(fps
, S_IRUGO
, show_fps
, NULL
); /**< FPS value */
616 static DEVICE_ATTR(brightness
, S_IRUGO
| S_IWUGO
, show_brightness
, store_brightness
); /**< Brightness value */
617 static DEVICE_ATTR(exposure
, S_IRUGO
| S_IWUGO
, show_exposure
, store_exposure
); /**< Exposure value */
618 static DEVICE_ATTR(contrast
, S_IRUGO
| S_IWUGO
, show_contrast
, store_contrast
); /**< Contrast value */
619 static DEVICE_ATTR(whitebalance
, S_IRUGO
| S_IWUGO
, show_whitebalance
, store_whitebalance
); /**< Whitebalance value */
620 //static DEVICE_ATTR(colour, S_IRUGO | S_IWUGO, show_colour, store_colour); /**< Hue value */
621 static DEVICE_ATTR(hflip
, S_IRUGO
| S_IWUGO
, show_hflip
, store_hflip
); /**< Horizontal filp value */
622 static DEVICE_ATTR(vflip
, S_IRUGO
| S_IWUGO
, show_vflip
, store_vflip
); /**< Vertical filp value */
627 * @brief Create the 'sys' entries.
629 * This function permits to create all the entries in the 'sys' filesystem.
631 * @param vdev Video device structure
633 * @returns 0 if all is OK
635 int microdia_create_sysfs_files(struct video_device
*vdev
)
639 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
640 ret
= video_device_create_file(vdev
, &class_device_attr_release
);
641 ret
= video_device_create_file(vdev
, &class_device_attr_videostatus
);
642 ret
= video_device_create_file(vdev
, &class_device_attr_informations
);
643 ret
= video_device_create_file(vdev
, &class_device_attr_fps
);
644 ret
= video_device_create_file(vdev
, &class_device_attr_brightness
);
645 ret
= video_device_create_file(vdev
, &class_device_attr_exposure
);
646 ret
= video_device_create_file(vdev
, &class_device_attr_contrast
);
647 ret
= video_device_create_file(vdev
, &class_device_attr_whitebalance
);
648 /*ret = video_device_create_file(vdev, &class_device_attr_colour);*/
649 ret
= video_device_create_file(vdev
, &class_device_attr_hflip
);
650 ret
= video_device_create_file(vdev
, &class_device_attr_vflip
);
652 ret
= video_device_create_file(vdev
, &dev_attr_release
);
653 ret
= video_device_create_file(vdev
, &dev_attr_videostatus
);
654 ret
= video_device_create_file(vdev
, &dev_attr_informations
);
655 ret
= video_device_create_file(vdev
, &dev_attr_fps
);
656 ret
= video_device_create_file(vdev
, &dev_attr_brightness
);
657 ret
= video_device_create_file(vdev
, &dev_attr_exposure
);
658 ret
= video_device_create_file(vdev
, &dev_attr_contrast
);
659 ret
= video_device_create_file(vdev
, &dev_attr_whitebalance
);
660 /*ret = video_device_create_file(vdev, &dev_attr_colour);*/
661 ret
= video_device_create_file(vdev
, &dev_attr_hflip
);
662 ret
= video_device_create_file(vdev
, &dev_attr_vflip
);
670 * @brief Remove the 'sys' entries.
672 * This function permits to remove all the entries in the 'sys' filesystem.
674 * @param vdev Video device structure
676 * @returns 0 if all is OK
678 void microdia_remove_sysfs_files(struct video_device
*vdev
)
680 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
681 video_device_remove_file(vdev
, &class_device_attr_release
);
682 video_device_remove_file(vdev
, &class_device_attr_videostatus
);
683 video_device_remove_file(vdev
, &class_device_attr_informations
);
684 video_device_remove_file(vdev
, &class_device_attr_fps
);
685 video_device_remove_file(vdev
, &class_device_attr_brightness
);
686 video_device_remove_file(vdev
, &class_device_attr_exposure
);
687 video_device_remove_file(vdev
, &class_device_attr_contrast
);
688 video_device_remove_file(vdev
, &class_device_attr_whitebalance
);
689 /*video_device_remove_file(vdev, &class_device_attr_colour);*/
690 video_device_remove_file(vdev
, &class_device_attr_hflip
);
691 video_device_remove_file(vdev
, &class_device_attr_vflip
);
693 video_device_remove_file(vdev
, &dev_attr_release
);
694 video_device_remove_file(vdev
, &dev_attr_videostatus
);
695 video_device_remove_file(vdev
, &dev_attr_informations
);
696 video_device_remove_file(vdev
, &dev_attr_fps
);
697 video_device_remove_file(vdev
, &dev_attr_brightness
);
698 video_device_remove_file(vdev
, &dev_attr_exposure
);
699 video_device_remove_file(vdev
, &dev_attr_contrast
);
700 video_device_remove_file(vdev
, &dev_attr_whitebalance
);
701 /*video_device_remove_file(vdev, &dev_attr_colour);*/
702 video_device_remove_file(vdev
, &dev_attr_hflip
);
703 video_device_remove_file(vdev
, &dev_attr_vflip
);