stk1160.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * STK1160 driver
  4. *
  5. * Copyright (C) 2012 Ezequiel Garcia
  6. * <elezegarcia--a.t--gmail.com>
  7. *
  8. * Based on Easycap driver by R.M. Thomas
  9. * Copyright (C) 2010 R.M. Thomas
  10. * <rmthomas--a.t--sciolus.org>
  11. */
  12. #include <linux/i2c.h>
  13. #include <sound/core.h>
  14. #include <sound/ac97_codec.h>
  15. #include <media/videobuf2-v4l2.h>
  16. #include <media/v4l2-device.h>
  17. #include <media/v4l2-ctrls.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/hcd.h>
  20. #define STK1160_VERSION "0.9.5"
  21. #define STK1160_VERSION_NUM 0x000905
  22. /* Decide on number of packets for each buffer */
  23. #define STK1160_NUM_PACKETS 64
  24. /* Number of buffers for isoc transfers */
  25. #define STK1160_NUM_BUFS 16
  26. #define STK1160_MIN_BUFS 1
  27. /* TODO: This endpoint address should be retrieved */
  28. #define STK1160_EP_VIDEO 0x82
  29. #define STK1160_EP_AUDIO 0x81
  30. /* Max and min video buffers */
  31. #define STK1160_MIN_VIDEO_BUFFERS 8
  32. #define STK1160_MAX_VIDEO_BUFFERS 32
  33. #define STK1160_MIN_PKT_SIZE 3072
  34. #define STK1160_MAX_INPUT 4
  35. #define STK1160_SVIDEO_INPUT 4
  36. #define STK1160_AC97_TIMEOUT 50
  37. #define STK1160_I2C_TIMEOUT 100
  38. /* TODO: Print helpers
  39. * I could use dev_xxx, pr_xxx, v4l2_xxx or printk.
  40. * However, there isn't a solid consensus on which
  41. * new drivers should use.
  42. *
  43. */
  44. #ifdef DEBUG
  45. #define stk1160_dbg(fmt, args...) \
  46. printk(KERN_DEBUG "stk1160: " fmt, ## args)
  47. #else
  48. #define stk1160_dbg(fmt, args...)
  49. #endif
  50. #define stk1160_info(fmt, args...) \
  51. pr_info("stk1160: " fmt, ## args)
  52. #define stk1160_warn(fmt, args...) \
  53. pr_warn("stk1160: " fmt, ## args)
  54. #define stk1160_err(fmt, args...) \
  55. pr_err("stk1160: " fmt, ## args)
  56. /* Buffer for one video frame */
  57. struct stk1160_buffer {
  58. /* common v4l buffer stuff -- must be first */
  59. struct vb2_v4l2_buffer vb;
  60. struct list_head list;
  61. void *mem;
  62. unsigned int length; /* buffer length */
  63. unsigned int bytesused; /* bytes written */
  64. int odd; /* current oddity */
  65. /*
  66. * Since we interlace two fields per frame,
  67. * this is different from bytesused.
  68. */
  69. unsigned int pos; /* current pos inside buffer */
  70. };
  71. struct stk1160_urb {
  72. struct urb *urb;
  73. char *transfer_buffer;
  74. struct sg_table *sgt;
  75. struct stk1160 *dev;
  76. dma_addr_t dma;
  77. };
  78. struct stk1160_isoc_ctl {
  79. /* max packet size of isoc transaction */
  80. int max_pkt_size;
  81. /* number of allocated urbs */
  82. int num_bufs;
  83. struct stk1160_urb urb_ctl[STK1160_NUM_BUFS];
  84. /* current buffer */
  85. struct stk1160_buffer *buf;
  86. };
  87. struct stk1160_fmt {
  88. u32 fourcc; /* v4l2 format id */
  89. int depth;
  90. };
  91. struct stk1160 {
  92. struct v4l2_device v4l2_dev;
  93. struct video_device vdev;
  94. struct v4l2_ctrl_handler ctrl_handler;
  95. struct device *dev;
  96. struct usb_device *udev;
  97. /* saa7115 subdev */
  98. struct v4l2_subdev *sd_saa7115;
  99. /* isoc control struct */
  100. struct list_head avail_bufs;
  101. /* video capture */
  102. struct vb2_queue vb_vidq;
  103. /* max packet size of isoc transaction */
  104. int max_pkt_size;
  105. /* array of wMaxPacketSize */
  106. unsigned int *alt_max_pkt_size;
  107. /* alternate */
  108. int alt;
  109. /* Number of alternative settings */
  110. int num_alt;
  111. struct stk1160_isoc_ctl isoc_ctl;
  112. /* frame properties */
  113. int width; /* current frame width */
  114. int height; /* current frame height */
  115. unsigned int ctl_input; /* selected input */
  116. v4l2_std_id norm; /* current norm */
  117. struct stk1160_fmt *fmt; /* selected format */
  118. unsigned int sequence;
  119. /* i2c i/o */
  120. struct i2c_adapter i2c_adap;
  121. struct i2c_client i2c_client;
  122. struct mutex v4l_lock;
  123. struct mutex vb_queue_lock;
  124. spinlock_t buf_lock;
  125. struct file *fh_owner; /* filehandle ownership */
  126. /* EXPERIMENTAL */
  127. struct snd_card *snd_card;
  128. };
  129. struct regval {
  130. u16 reg;
  131. u16 val;
  132. };
  133. /* Provided by stk1160-v4l.c */
  134. int stk1160_vb2_setup(struct stk1160 *dev);
  135. int stk1160_video_register(struct stk1160 *dev);
  136. void stk1160_video_unregister(struct stk1160 *dev);
  137. void stk1160_clear_queue(struct stk1160 *dev, enum vb2_buffer_state vb2_state);
  138. /* Provided by stk1160-video.c */
  139. int stk1160_alloc_isoc(struct stk1160 *dev);
  140. void stk1160_free_isoc(struct stk1160 *dev);
  141. void stk1160_cancel_isoc(struct stk1160 *dev);
  142. void stk1160_uninit_isoc(struct stk1160 *dev);
  143. /* Provided by stk1160-i2c.c */
  144. int stk1160_i2c_register(struct stk1160 *dev);
  145. int stk1160_i2c_unregister(struct stk1160 *dev);
  146. /* Provided by stk1160-core.c */
  147. int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value);
  148. int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value);
  149. int stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg,
  150. char *buf, int len);
  151. int stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg,
  152. char *buf, int len);
  153. void stk1160_select_input(struct stk1160 *dev);
  154. /* Provided by stk1160-ac97.c */
  155. void stk1160_ac97_setup(struct stk1160 *dev);
  156. static inline struct device *stk1160_get_dmadev(struct stk1160 *dev)
  157. {
  158. return bus_to_hcd(dev->udev->bus)->self.sysdev;
  159. }