sn9c102.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /***************************************************************************
  2. * V4L2 driver for SN9C10x PC Camera Controllers *
  3. * *
  4. * Copyright (C) 2004-2006 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 _SN9C102_H_
  21. #define _SN9C102_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/rwsem.h>
  34. #include <linux/mutex.h>
  35. #include <asm/semaphore.h>
  36. #include "sn9c102_sensor.h"
  37. /*****************************************************************************/
  38. #define SN9C102_DEBUG
  39. #define SN9C102_DEBUG_LEVEL 2
  40. #define SN9C102_MAX_DEVICES 64
  41. #define SN9C102_PRESERVE_IMGSCALE 0
  42. #define SN9C102_FORCE_MUNMAP 0
  43. #define SN9C102_MAX_FRAMES 32
  44. #define SN9C102_URBS 2
  45. #define SN9C102_ISO_PACKETS 7
  46. #define SN9C102_ALTERNATE_SETTING 8
  47. #define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS)
  48. #define SN9C102_CTRL_TIMEOUT 300
  49. /*****************************************************************************/
  50. enum sn9c102_bridge {
  51. BRIDGE_SN9C101 = 0x01,
  52. BRIDGE_SN9C102 = 0x02,
  53. BRIDGE_SN9C103 = 0x04,
  54. };
  55. SN9C102_ID_TABLE
  56. SN9C102_SENSOR_TABLE
  57. enum sn9c102_frame_state {
  58. F_UNUSED,
  59. F_QUEUED,
  60. F_GRABBING,
  61. F_DONE,
  62. F_ERROR,
  63. };
  64. struct sn9c102_frame_t {
  65. void* bufmem;
  66. struct v4l2_buffer buf;
  67. enum sn9c102_frame_state state;
  68. struct list_head frame;
  69. unsigned long vma_use_count;
  70. };
  71. enum sn9c102_dev_state {
  72. DEV_INITIALIZED = 0x01,
  73. DEV_DISCONNECTED = 0x02,
  74. DEV_MISCONFIGURED = 0x04,
  75. };
  76. enum sn9c102_io_method {
  77. IO_NONE,
  78. IO_READ,
  79. IO_MMAP,
  80. };
  81. enum sn9c102_stream_state {
  82. STREAM_OFF,
  83. STREAM_INTERRUPT,
  84. STREAM_ON,
  85. };
  86. typedef char sn9c103_sof_header_t[18];
  87. typedef char sn9c102_sof_header_t[12];
  88. typedef char sn9c102_eof_header_t[4];
  89. struct sn9c102_sysfs_attr {
  90. u8 reg, i2c_reg;
  91. sn9c103_sof_header_t frame_header;
  92. };
  93. struct sn9c102_module_param {
  94. u8 force_munmap;
  95. };
  96. static DEFINE_MUTEX(sn9c102_sysfs_lock);
  97. static DECLARE_RWSEM(sn9c102_disconnect);
  98. struct sn9c102_device {
  99. struct video_device* v4ldev;
  100. enum sn9c102_bridge bridge;
  101. struct sn9c102_sensor* sensor;
  102. struct usb_device* usbdev;
  103. struct urb* urb[SN9C102_URBS];
  104. void* transfer_buffer[SN9C102_URBS];
  105. u8* control_buffer;
  106. struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
  107. struct list_head inqueue, outqueue;
  108. u32 frame_count, nbuffers, nreadbuffers;
  109. enum sn9c102_io_method io;
  110. enum sn9c102_stream_state stream;
  111. struct v4l2_jpegcompression compression;
  112. struct sn9c102_sysfs_attr sysfs;
  113. sn9c103_sof_header_t sof_header;
  114. u16 reg[63];
  115. struct sn9c102_module_param module_param;
  116. enum sn9c102_dev_state state;
  117. u8 users;
  118. struct mutex dev_mutex, fileop_mutex;
  119. spinlock_t queue_lock;
  120. wait_queue_head_t open, wait_frame, wait_stream;
  121. };
  122. /*****************************************************************************/
  123. void
  124. sn9c102_attach_sensor(struct sn9c102_device* cam,
  125. struct sn9c102_sensor* sensor)
  126. {
  127. cam->sensor = sensor;
  128. cam->sensor->usbdev = cam->usbdev;
  129. }
  130. /*****************************************************************************/
  131. #undef DBG
  132. #undef KDBG
  133. #ifdef SN9C102_DEBUG
  134. # define DBG(level, fmt, args...) \
  135. do { \
  136. if (debug >= (level)) { \
  137. if ((level) == 1) \
  138. dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
  139. else if ((level) == 2) \
  140. dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
  141. else if ((level) >= 3) \
  142. dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
  143. __FUNCTION__, __LINE__ , ## args); \
  144. } \
  145. } while (0)
  146. # define V4LDBG(level, name, cmd) \
  147. do { \
  148. if (debug >= (level)) \
  149. v4l_print_ioctl(name, cmd); \
  150. } while (0)
  151. # define KDBG(level, fmt, args...) \
  152. do { \
  153. if (debug >= (level)) { \
  154. if ((level) == 1 || (level) == 2) \
  155. pr_info("sn9c102: " fmt "\n", ## args); \
  156. else if ((level) == 3) \
  157. pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__, \
  158. __LINE__ , ## args); \
  159. } \
  160. } while (0)
  161. #else
  162. # define DBG(level, fmt, args...) do {;} while(0)
  163. # define V4LDBG(level, name, cmd) do {;} while(0)
  164. # define KDBG(level, fmt, args...) do {;} while(0)
  165. #endif
  166. #undef PDBG
  167. #define PDBG(fmt, args...) \
  168. dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args)
  169. #undef PDBGG
  170. #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
  171. #endif /* _SN9C102_H_ */