Adding stop stream support for 627b
[microdia.git] / microdia-sysfs.c
blob3efef3bc05f619406b4e6f988a055f3553d0eb45
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
27 * @par SubVersion
28 * $Date: 2008-02-05 10:10:01 +0100 (mar, 05 fév 2008) $
29 * $Revision: 72 $
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>
46 #include "microdia.h"
49 extern const struct microdia_coord microdia_image_sizes[MICRODIA_NBR_SIZES];
52 /**
53 * @brief show_release
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)
63 #else
64 static ssize_t show_release(struct device *class, struct device_attribute *attr, char *buf)
65 #endif
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);
74 /**
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)
85 #else
86 static ssize_t show_videostatus(struct device *class, struct device_attribute *attr, char *buf)
87 #endif
89 struct video_device *vdev = to_video_device(class);
90 struct usb_microdia *dev = video_get_drvdata(vdev);
92 return sprintf(buf,
93 "Nbr ISOC errors : %d\n"
94 "Nbr dropped frames : %d\n"
95 "Nbr dumped frames : %d\n",
96 dev->visoc_errors,
97 dev->vframes_error,
98 dev->vframes_dumped);
102 /**
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)
113 #else
114 static ssize_t show_informations(struct device *class, struct device_attribute *attr, char *buf)
115 #endif
117 int width, height;
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;
135 break;
137 case MICRODIA_PALETTE_RGB32:
138 pixelfmt = palette_rgb32;
139 break;
141 case MICRODIA_PALETTE_BGR24:
142 pixelfmt = palette_bgr24;
143 break;
145 case MICRODIA_PALETTE_BGR32:
146 pixelfmt = palette_bgr32;
147 break;
149 case MICRODIA_PALETTE_UYVY:
150 pixelfmt = palette_uyvy;
151 break;
153 case MICRODIA_PALETTE_YUYV:
154 pixelfmt = palette_yuyv;
155 break;
157 case MICRODIA_PALETTE_I420:
158 pixelfmt = palette_i420;
159 break;
162 switch (dev->resolution) {
163 case MICRODIA_80x60:
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;
171 break;
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;
178 break;
180 default:
181 width = 0;
182 height = 0;
185 return sprintf(buf,
186 "Asked resolution : %dx%d\n"
187 "Driver resolution : %dx%d\n"
188 "Webcam resolution : %dx%d\n"
189 "\n"
190 "%s\n"
191 "\n"
192 "Brightness : 0x%X\n"
193 "Contrast : 0x%X\n"
194 "Whiteness : 0x%X\n"
195 "Colour : 0x%X\n",
196 dev->view.x, dev->view.y,
197 microdia_image_sizes[dev->resolution].x, microdia_image_sizes[dev->resolution].y,
198 width, height,
199 pixelfmt,
200 0xFFFF & dev->vsettings.brightness,
201 0xFFFF & dev->vsettings.contrast,
202 0xFFFF & dev->vsettings.whiteness,
203 0xFFFF & dev->vsettings.colour);
207 /**
208 * @brief show_fps
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)
218 #else
219 static ssize_t show_fps(struct device *class, struct device_attribute *attr, char *buf)
220 #endif
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);
229 /**
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)
240 #else
241 static ssize_t show_exposure(struct device *class, struct device_attribute *attr, char *buf)
242 #endif
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);
250 /**
251 * @brief store_exposure
253 * @param class Class device
254 * @param buf Buffer
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)
261 #else
262 static ssize_t store_exposure(struct device *class, struct device_attribute *attr,
263 const char *buf, size_t count)
264 #endif
266 char *endp;
267 unsigned long value;
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);
278 return strlen(buf);
281 /**
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)
292 #else
293 static ssize_t show_brightness(struct device *class, struct device_attribute *attr, char *buf)
294 #endif
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);
302 /**
303 * @brief store_brightness
305 * @param class Class device
306 * @param buf Buffer
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)
313 #else
314 static ssize_t store_brightness(struct device *class, struct device_attribute *attr,
315 const char *buf, size_t count)
316 #endif
318 char *endp;
319 unsigned long value;
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);
330 return strlen(buf);
333 /**
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)
344 #else
345 static ssize_t show_contrast(struct device *class, struct device_attribute *attr, char *buf)
346 #endif
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);
355 /**
356 * @brief store_contrast
358 * @param class Class device
359 * @param buf Buffer
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)
366 #else
367 static ssize_t store_contrast(struct device *class, struct device_attribute *attr,
368 const char *buf, size_t count)
369 #endif
371 char *endp;
372 unsigned long value;
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);
384 return strlen(buf);
388 /**
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)
399 #else
400 static ssize_t show_whitebalance(struct device *class, struct device_attribute *attr, char *buf)
401 #endif
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);
410 /**
411 * @brief store_whitebalance
413 * @param class Class device
414 * @param buf Buffer
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)
421 #else
422 static ssize_t store_whitebalance(struct device *class, struct device_attribute *attr,
423 const char *buf, size_t count)
424 #endif
426 char *endp;
427 unsigned long value;
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);
438 return strlen(buf);
442 /**
443 * @brief show_colour
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)
453 #else
454 static ssize_t show_colour(struct device *class, struct device_attribute *attr, char *buf)
455 #endif
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);
464 /**
465 * @brief store_colour
467 * @param class Class device
468 * @param buf Buffer
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)
475 #else
476 static ssize_t store_colour(struct device *class, struct device_attribute *attr,
477 const char *buf, size_t count)
478 #endif
480 char *endp;
481 unsigned long value;
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);
492 return strlen(buf);
496 /**
497 * @brief show_hflip
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)
507 #else
508 static ssize_t show_hflip(struct device *class, struct device_attribute *attr, char *buf)
509 #endif
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);
518 /**
519 * @brief store_hflip
521 * @param class Class device
522 * @param buf Buffer
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)
529 #else
530 static ssize_t store_hflip(struct device *class, struct device_attribute *attr,
531 const char *buf, size_t count)
532 #endif
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;
541 else
542 return -EINVAL;
544 return strlen(buf);
548 /**
549 * @brief show_vflip
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)
559 #else
560 static ssize_t show_vflip(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, "%d\n", dev->vsettings.vflip);
570 /**
571 * @brief store_vflip
573 * @param class Class device
574 * @param buf Buffer
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)
581 #else
582 static ssize_t store_vflip(struct device *class, struct device_attribute *attr, const char *buf, size_t count)
583 #endif
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;
592 else
593 return -EINVAL;
595 return strlen(buf);
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 */
611 #else
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 */
623 #endif
626 /**
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)
637 int ret;
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);
651 #else
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);
663 #endif
665 return ret;
669 /**
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);
692 #else
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);
704 #endif