sn9c102.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <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 <linux/kref.h>
  38. #include "sn9c102_config.h"
  39. #include "sn9c102_sensor.h"
  40. #include "sn9c102_devtable.h"
  41. enum sn9c102_frame_state {
  42. F_UNUSED,
  43. F_QUEUED,
  44. F_GRABBING,
  45. F_DONE,
  46. F_ERROR,
  47. };
  48. struct sn9c102_frame_t {
  49. void* bufmem;
  50. struct v4l2_buffer buf;
  51. enum sn9c102_frame_state state;
  52. struct list_head frame;
  53. unsigned long vma_use_count;
  54. };
  55. enum sn9c102_dev_state {
  56. DEV_INITIALIZED = 0x01,
  57. DEV_DISCONNECTED = 0x02,
  58. DEV_MISCONFIGURED = 0x04,
  59. };
  60. enum sn9c102_io_method {
  61. IO_NONE,
  62. IO_READ,
  63. IO_MMAP,
  64. };
  65. enum sn9c102_stream_state {
  66. STREAM_OFF,
  67. STREAM_INTERRUPT,
  68. STREAM_ON,
  69. };
  70. typedef char sn9c102_sof_header_t[62];
  71. struct sn9c102_sof_t {
  72. sn9c102_sof_header_t header;
  73. u16 bytesread;
  74. };
  75. struct sn9c102_sysfs_attr {
  76. u16 reg, i2c_reg;
  77. sn9c102_sof_header_t frame_header;
  78. };
  79. struct sn9c102_module_param {
  80. u8 force_munmap;
  81. u16 frame_timeout;
  82. };
  83. static DEFINE_MUTEX(sn9c102_sysfs_lock);
  84. static DECLARE_RWSEM(sn9c102_dev_lock);
  85. struct sn9c102_device {
  86. struct video_device* v4ldev;
  87. enum sn9c102_bridge bridge;
  88. struct sn9c102_sensor sensor;
  89. struct usb_device* usbdev;
  90. struct urb* urb[SN9C102_URBS];
  91. void* transfer_buffer[SN9C102_URBS];
  92. u8* control_buffer;
  93. struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
  94. struct list_head inqueue, outqueue;
  95. u32 frame_count, nbuffers, nreadbuffers;
  96. enum sn9c102_io_method io;
  97. enum sn9c102_stream_state stream;
  98. struct v4l2_jpegcompression compression;
  99. struct sn9c102_sysfs_attr sysfs;
  100. struct sn9c102_sof_t sof;
  101. u16 reg[384];
  102. struct sn9c102_module_param module_param;
  103. struct kref kref;
  104. enum sn9c102_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 sn9c102_device*
  113. sn9c102_match_id(struct sn9c102_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. sn9c102_attach_sensor(struct sn9c102_device* cam,
  119. const struct sn9c102_sensor* sensor)
  120. {
  121. memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor));
  122. }
  123. enum sn9c102_bridge
  124. sn9c102_get_bridge(struct sn9c102_device* cam)
  125. {
  126. return cam->bridge;
  127. }
  128. struct sn9c102_sensor* sn9c102_get_sensor(struct sn9c102_device* cam)
  129. {
  130. return &cam->sensor;
  131. }
  132. /*****************************************************************************/
  133. #undef DBG
  134. #undef KDBG
  135. #ifdef SN9C102_DEBUG
  136. # define DBG(level, fmt, args...) \
  137. do { \
  138. if (debug >= (level)) { \
  139. if ((level) == 1) \
  140. dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
  141. else if ((level) == 2) \
  142. dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
  143. else if ((level) >= 3) \
  144. dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
  145. __func__, __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. # define KDBG(level, fmt, args...) \
  154. do { \
  155. if (debug >= (level)) { \
  156. if ((level) == 1 || (level) == 2) \
  157. pr_info("sn9c102: " fmt "\n", ## args); \
  158. else if ((level) == 3) \
  159. pr_debug("sn9c102: [%s:%d] " fmt "\n", \
  160. __func__, __LINE__ , ## args); \
  161. } \
  162. } while (0)
  163. #else
  164. # define DBG(level, fmt, args...) do {;} while(0)
  165. # define V4LDBG(level, name, cmd) do {;} while(0)
  166. # define KDBG(level, fmt, args...) do {;} while(0)
  167. #endif
  168. #undef PDBG
  169. #define PDBG(fmt, args...) \
  170. dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, \
  171. __LINE__ , ## args)
  172. #undef PDBGG
  173. #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
  174. #endif /* _SN9C102_H_ */