zc0301.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /***************************************************************************
  2. * V4L2 driver for ZC0301[P] Image Processor and Control Chip *
  3. * *
  4. * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the Free Software *
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  19. ***************************************************************************/
  20. #ifndef _ZC0301_H_
  21. #define _ZC0301_H_
  22. #include <linux/version.h>
  23. #include <linux/usb.h>
  24. #include <linux/videodev2.h>
  25. #include <media/v4l2-common.h>
  26. #include <linux/device.h>
  27. #include <linux/list.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/time.h>
  30. #include <linux/wait.h>
  31. #include <linux/types.h>
  32. #include <linux/param.h>
  33. #include <linux/mutex.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/stddef.h>
  36. #include <linux/string.h>
  37. #include <linux/kref.h>
  38. #include "zc0301_sensor.h"
  39. /*****************************************************************************/
  40. #define ZC0301_DEBUG
  41. #define ZC0301_DEBUG_LEVEL 2
  42. #define ZC0301_MAX_DEVICES 64
  43. #define ZC0301_FORCE_MUNMAP 0
  44. #define ZC0301_MAX_FRAMES 32
  45. #define ZC0301_COMPRESSION_QUALITY 0
  46. #define ZC0301_URBS 2
  47. #define ZC0301_ISO_PACKETS 7
  48. #define ZC0301_ALTERNATE_SETTING 7
  49. #define ZC0301_URB_TIMEOUT msecs_to_jiffies(2 * ZC0301_ISO_PACKETS)
  50. #define ZC0301_CTRL_TIMEOUT 100
  51. #define ZC0301_FRAME_TIMEOUT 2
  52. /*****************************************************************************/
  53. ZC0301_ID_TABLE
  54. ZC0301_SENSOR_TABLE
  55. enum zc0301_frame_state {
  56. F_UNUSED,
  57. F_QUEUED,
  58. F_GRABBING,
  59. F_DONE,
  60. F_ERROR,
  61. };
  62. struct zc0301_frame_t {
  63. void* bufmem;
  64. struct v4l2_buffer buf;
  65. enum zc0301_frame_state state;
  66. struct list_head frame;
  67. unsigned long vma_use_count;
  68. };
  69. enum zc0301_dev_state {
  70. DEV_INITIALIZED = 0x01,
  71. DEV_DISCONNECTED = 0x02,
  72. DEV_MISCONFIGURED = 0x04,
  73. };
  74. enum zc0301_io_method {
  75. IO_NONE,
  76. IO_READ,
  77. IO_MMAP,
  78. };
  79. enum zc0301_stream_state {
  80. STREAM_OFF,
  81. STREAM_INTERRUPT,
  82. STREAM_ON,
  83. };
  84. struct zc0301_module_param {
  85. u8 force_munmap;
  86. u16 frame_timeout;
  87. };
  88. static DECLARE_RWSEM(zc0301_dev_lock);
  89. struct zc0301_device {
  90. struct video_device* v4ldev;
  91. struct zc0301_sensor sensor;
  92. struct usb_device* usbdev;
  93. struct urb* urb[ZC0301_URBS];
  94. void* transfer_buffer[ZC0301_URBS];
  95. u8* control_buffer;
  96. struct zc0301_frame_t *frame_current, frame[ZC0301_MAX_FRAMES];
  97. struct list_head inqueue, outqueue;
  98. u32 frame_count, nbuffers, nreadbuffers;
  99. enum zc0301_io_method io;
  100. enum zc0301_stream_state stream;
  101. struct v4l2_jpegcompression compression;
  102. struct zc0301_module_param module_param;
  103. struct kref kref;
  104. enum zc0301_dev_state state;
  105. u8 users;
  106. struct completion probe;
  107. struct mutex open_mutex, fileop_mutex;
  108. spinlock_t queue_lock;
  109. wait_queue_head_t wait_open, wait_frame, wait_stream;
  110. };
  111. /*****************************************************************************/
  112. struct zc0301_device*
  113. zc0301_match_id(struct zc0301_device* cam, const struct usb_device_id *id)
  114. {
  115. return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
  116. }
  117. void
  118. zc0301_attach_sensor(struct zc0301_device* cam, struct zc0301_sensor* sensor)
  119. {
  120. memcpy(&cam->sensor, sensor, sizeof(struct zc0301_sensor));
  121. }
  122. /*****************************************************************************/
  123. #undef DBG
  124. #undef KDBG
  125. #ifdef ZC0301_DEBUG
  126. # define DBG(level, fmt, args...) \
  127. do { \
  128. if (debug >= (level)) { \
  129. if ((level) == 1) \
  130. dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
  131. else if ((level) == 2) \
  132. dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
  133. else if ((level) >= 3) \
  134. dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", \
  135. __FILE__, __FUNCTION__, __LINE__ , ## args); \
  136. } \
  137. } while (0)
  138. # define KDBG(level, fmt, args...) \
  139. do { \
  140. if (debug >= (level)) { \
  141. if ((level) == 1 || (level) == 2) \
  142. pr_info("zc0301: " fmt "\n", ## args); \
  143. else if ((level) == 3) \
  144. pr_debug("sn9c102: [%s:%s:%d] " fmt "\n", __FILE__, \
  145. __FUNCTION__, __LINE__ , ## args); \
  146. } \
  147. } while (0)
  148. # define V4LDBG(level, name, cmd) \
  149. do { \
  150. if (debug >= (level)) \
  151. v4l_print_ioctl(name, cmd); \
  152. } while (0)
  153. #else
  154. # define DBG(level, fmt, args...) do {;} while(0)
  155. # define KDBG(level, fmt, args...) do {;} while(0)
  156. # define V4LDBG(level, name, cmd) do {;} while(0)
  157. #endif
  158. #undef PDBG
  159. #define PDBG(fmt, args...) \
  160. dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __FUNCTION__, \
  161. __LINE__ , ## args)
  162. #undef PDBGG
  163. #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
  164. #endif /* _ZC0301_H_ */