sn9c102.h 6.8 KB

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