sn9c102.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /***************************************************************************
  2. * V4L2 driver for SN9C1xx 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/usb.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-device.h>
  27. #include <linux/device.h>
  28. #include <linux/list.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/time.h>
  31. #include <linux/wait.h>
  32. #include <linux/types.h>
  33. #include <linux/param.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/mutex.h>
  36. #include <linux/string.h>
  37. #include <linux/stddef.h>
  38. #include <linux/kref.h>
  39. #include "sn9c102_config.h"
  40. #include "sn9c102_sensor.h"
  41. #include "sn9c102_devtable.h"
  42. enum sn9c102_frame_state {
  43. F_UNUSED,
  44. F_QUEUED,
  45. F_GRABBING,
  46. F_DONE,
  47. F_ERROR,
  48. };
  49. struct sn9c102_frame_t {
  50. void* bufmem;
  51. struct v4l2_buffer buf;
  52. enum sn9c102_frame_state state;
  53. struct list_head frame;
  54. unsigned long vma_use_count;
  55. };
  56. enum sn9c102_dev_state {
  57. DEV_INITIALIZED = 0x01,
  58. DEV_DISCONNECTED = 0x02,
  59. DEV_MISCONFIGURED = 0x04,
  60. };
  61. enum sn9c102_io_method {
  62. IO_NONE,
  63. IO_READ,
  64. IO_MMAP,
  65. };
  66. enum sn9c102_stream_state {
  67. STREAM_OFF,
  68. STREAM_INTERRUPT,
  69. STREAM_ON,
  70. };
  71. typedef char sn9c102_sof_header_t[62];
  72. struct sn9c102_sof_t {
  73. sn9c102_sof_header_t header;
  74. u16 bytesread;
  75. };
  76. struct sn9c102_sysfs_attr {
  77. u16 reg, i2c_reg;
  78. sn9c102_sof_header_t frame_header;
  79. };
  80. struct sn9c102_module_param {
  81. u8 force_munmap;
  82. u16 frame_timeout;
  83. };
  84. static DEFINE_MUTEX(sn9c102_sysfs_lock);
  85. static DECLARE_RWSEM(sn9c102_dev_lock);
  86. struct sn9c102_device {
  87. struct video_device* v4ldev;
  88. struct v4l2_device v4l2_dev;
  89. enum sn9c102_bridge bridge;
  90. struct sn9c102_sensor sensor;
  91. struct usb_device* usbdev;
  92. struct urb* urb[SN9C102_URBS];
  93. void* transfer_buffer[SN9C102_URBS];
  94. u8* control_buffer;
  95. struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
  96. struct list_head inqueue, outqueue;
  97. u32 frame_count, nbuffers, nreadbuffers;
  98. enum sn9c102_io_method io;
  99. enum sn9c102_stream_state stream;
  100. struct v4l2_jpegcompression compression;
  101. struct sn9c102_sysfs_attr sysfs;
  102. struct sn9c102_sof_t sof;
  103. u16 reg[384];
  104. struct sn9c102_module_param module_param;
  105. struct kref kref;
  106. enum sn9c102_dev_state state;
  107. u8 users;
  108. struct completion probe;
  109. struct mutex open_mutex, fileop_mutex;
  110. spinlock_t queue_lock;
  111. wait_queue_head_t wait_open, wait_frame, wait_stream;
  112. };
  113. /*****************************************************************************/
  114. struct sn9c102_device*
  115. sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id)
  116. {
  117. return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
  118. }
  119. void
  120. sn9c102_attach_sensor(struct sn9c102_device* cam,
  121. const struct sn9c102_sensor* sensor)
  122. {
  123. memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor));
  124. }
  125. enum sn9c102_bridge
  126. sn9c102_get_bridge(struct sn9c102_device* cam)
  127. {
  128. return cam->bridge;
  129. }
  130. struct sn9c102_sensor* sn9c102_get_sensor(struct sn9c102_device* cam)
  131. {
  132. return &cam->sensor;
  133. }
  134. /*****************************************************************************/
  135. #undef DBG
  136. #undef KDBG
  137. #ifdef SN9C102_DEBUG
  138. # define DBG(level, fmt, args...) \
  139. do { \
  140. if (debug >= (level)) { \
  141. if ((level) == 1) \
  142. dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
  143. else if ((level) == 2) \
  144. dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
  145. else if ((level) >= 3) \
  146. dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
  147. __func__, __LINE__ , ## args); \
  148. } \
  149. } while (0)
  150. # define V4LDBG(level, name, cmd) \
  151. do { \
  152. if (debug >= (level)) \
  153. v4l_printk_ioctl(name, cmd); \
  154. } while (0)
  155. # define KDBG(level, fmt, args...) \
  156. do { \
  157. if (debug >= (level)) { \
  158. if ((level) == 1 || (level) == 2) \
  159. pr_info("sn9c102: " fmt "\n", ## args); \
  160. else if ((level) == 3) \
  161. pr_debug("sn9c102: [%s:%d] " fmt "\n", \
  162. __func__, __LINE__ , ## args); \
  163. } \
  164. } while (0)
  165. #else
  166. # define DBG(level, fmt, args...) do {;} while(0)
  167. # define V4LDBG(level, name, cmd) do {;} while(0)
  168. # define KDBG(level, fmt, args...) do {;} while(0)
  169. #endif
  170. #undef PDBG
  171. #define PDBG(fmt, args...) \
  172. dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, \
  173. __LINE__ , ## args)
  174. #undef PDBGG
  175. #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
  176. #endif /* _SN9C102_H_ */