blob: 3feba0033f98df4439795121c2004a1c5048b256 [file] [log] [blame]
Ezequiel GarcĂ­a9cb21732012-08-11 14:32:57 -03001/*
2 * STK1160 driver
3 *
4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
6 *
7 * Based on Easycap driver by R.M. Thomas
8 * Copyright (C) 2010 R.M. Thomas
9 * <rmthomas--a.t--sciolus.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 */
22
23#include <linux/i2c.h>
24#include <sound/core.h>
25#include <sound/ac97_codec.h>
26#include <media/videobuf2-core.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-ctrls.h>
29
30#define STK1160_VERSION "0.9.5"
31#define STK1160_VERSION_NUM 0x000905
32
33/* TODO: Decide on number of packets for each buffer */
34#define STK1160_NUM_PACKETS 64
35
36/* Number of buffers for isoc transfers */
37#define STK1160_NUM_BUFS 16 /* TODO */
38
39/* TODO: This endpoint address should be retrieved */
40#define STK1160_EP_VIDEO 0x82
41#define STK1160_EP_AUDIO 0x81
42
43/* Max and min video buffers */
44#define STK1160_MIN_VIDEO_BUFFERS 8
45#define STK1160_MAX_VIDEO_BUFFERS 32
46
47#define STK1160_MIN_PKT_SIZE 3072
48
49#define STK1160_MAX_INPUT 3
50
51#define STK1160_I2C_TIMEOUT 100
52
53/* TODO: Print helpers
54 * I could use dev_xxx, pr_xxx, v4l2_xxx or printk.
55 * However, there isn't a solid consensus on which
56 * new drivers should use.
57 *
58 */
59#define DEBUG
60#ifdef DEBUG
61#define stk1160_dbg(fmt, args...) \
62 printk(KERN_DEBUG "stk1160: " fmt, ## args)
63#else
64#define stk1160_dbg(fmt, args...)
65#endif
66
67#define stk1160_info(fmt, args...) \
68 pr_info("stk1160: " fmt, ## args)
69
70#define stk1160_warn(fmt, args...) \
71 pr_warn("stk1160: " fmt, ## args)
72
73#define stk1160_err(fmt, args...) \
74 pr_err("stk1160: " fmt, ## args)
75
76/* Buffer for one video frame */
77struct stk1160_buffer {
78 /* common v4l buffer stuff -- must be first */
79 struct vb2_buffer vb;
80 struct list_head list;
81
82 void *mem;
83 unsigned int length; /* buffer length */
84 unsigned int bytesused; /* bytes written */
85 int odd; /* current oddity */
86
87 /*
88 * Since we interlace two fields per frame,
89 * this is different from bytesused.
90 */
91 unsigned int pos; /* current pos inside buffer */
92};
93
94struct stk1160_isoc_ctl {
95 /* max packet size of isoc transaction */
96 int max_pkt_size;
97
98 /* number of allocated urbs */
99 int num_bufs;
100
101 /* urb for isoc transfers */
102 struct urb **urb;
103
104 /* transfer buffers for isoc transfer */
105 char **transfer_buffer;
106
107 /* current buffer */
108 struct stk1160_buffer *buf;
109};
110
111struct stk1160_fmt {
112 char *name;
113 u32 fourcc; /* v4l2 format id */
114 int depth;
115};
116
117struct stk1160 {
118 struct v4l2_device v4l2_dev;
119 struct video_device vdev;
120 struct v4l2_ctrl_handler ctrl_handler;
121
122 struct device *dev;
123 struct usb_device *udev;
124
125 /* saa7115 subdev */
126 struct v4l2_subdev *sd_saa7115;
127
128 /* isoc control struct */
129 struct list_head avail_bufs;
130
131 /* video capture */
132 struct vb2_queue vb_vidq;
133
134 /* max packet size of isoc transaction */
135 int max_pkt_size;
136 /* array of wMaxPacketSize */
137 unsigned int *alt_max_pkt_size;
138 /* alternate */
139 int alt;
140 /* Number of alternative settings */
141 int num_alt;
142
143 struct stk1160_isoc_ctl isoc_ctl;
144 char urb_buf[255]; /* urb control msg buffer */
145
146 /* frame properties */
147 int width; /* current frame width */
148 int height; /* current frame height */
149 unsigned int ctl_input; /* selected input */
150 v4l2_std_id norm; /* current norm */
151 struct stk1160_fmt *fmt; /* selected format */
152
153 unsigned int field_count; /* not sure ??? */
154 enum v4l2_field field; /* also not sure :/ */
155
156 /* i2c i/o */
157 struct i2c_adapter i2c_adap;
158 struct i2c_client i2c_client;
159
160 struct mutex v4l_lock;
161 struct mutex vb_queue_lock;
162 spinlock_t buf_lock;
163
164 struct file *fh_owner; /* filehandle ownership */
165
166 /* EXPERIMENTAL */
167 struct snd_card *snd_card;
168};
169
170struct regval {
171 u16 reg;
172 u16 val;
173};
174
175/* Provided by stk1160-v4l.c */
176int stk1160_vb2_setup(struct stk1160 *dev);
177int stk1160_video_register(struct stk1160 *dev);
178void stk1160_video_unregister(struct stk1160 *dev);
179void stk1160_clear_queue(struct stk1160 *dev);
180
181/* Provided by stk1160-video.c */
182int stk1160_alloc_isoc(struct stk1160 *dev);
183void stk1160_free_isoc(struct stk1160 *dev);
184void stk1160_cancel_isoc(struct stk1160 *dev);
185void stk1160_uninit_isoc(struct stk1160 *dev);
186
187/* Provided by stk1160-i2c.c */
188int stk1160_i2c_register(struct stk1160 *dev);
189int stk1160_i2c_unregister(struct stk1160 *dev);
190
191/* Provided by stk1160-core.c */
192int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value);
193int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value);
194int stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg,
195 char *buf, int len);
196int stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg,
197 char *buf, int len);
198void stk1160_select_input(struct stk1160 *dev);
199
200/* Provided by stk1160-ac97.c */
201#ifdef CONFIG_VIDEO_STK1160_AC97
202int stk1160_ac97_register(struct stk1160 *dev);
203int stk1160_ac97_unregister(struct stk1160 *dev);
204#else
205static inline int stk1160_ac97_register(struct stk1160 *dev) { return 0; }
206static inline int stk1160_ac97_unregister(struct stk1160 *dev) { return 0; }
207#endif
208