1 /*
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 4
50 #define STK1160_SVIDEO_INPUT 4
51 
52 #define STK1160_I2C_TIMEOUT 100
53 
54 /* TODO: Print helpers
55  * I could use dev_xxx, pr_xxx, v4l2_xxx or printk.
56  * However, there isn't a solid consensus on which
57  * new drivers should use.
58  *
59  */
60 #define DEBUG
61 #ifdef DEBUG
62 #define stk1160_dbg(fmt, args...) \
63 	printk(KERN_DEBUG "stk1160: " fmt,  ## args)
64 #else
65 #define stk1160_dbg(fmt, args...)
66 #endif
67 
68 #define stk1160_info(fmt, args...) \
69 	pr_info("stk1160: " fmt, ## args)
70 
71 #define stk1160_warn(fmt, args...) \
72 	pr_warn("stk1160: " fmt, ## args)
73 
74 #define stk1160_err(fmt, args...) \
75 	pr_err("stk1160: " fmt, ## args)
76 
77 /* Buffer for one video frame */
78 struct stk1160_buffer {
79 	/* common v4l buffer stuff -- must be first */
80 	struct vb2_buffer vb;
81 	struct list_head list;
82 
83 	void *mem;
84 	unsigned int length;		/* buffer length */
85 	unsigned int bytesused;		/* bytes written */
86 	int odd;			/* current oddity */
87 
88 	/*
89 	 * Since we interlace two fields per frame,
90 	 * this is different from bytesused.
91 	 */
92 	unsigned int pos;		/* current pos inside buffer */
93 };
94 
95 struct stk1160_isoc_ctl {
96 	/* max packet size of isoc transaction */
97 	int max_pkt_size;
98 
99 	/* number of allocated urbs */
100 	int num_bufs;
101 
102 	/* urb for isoc transfers */
103 	struct urb **urb;
104 
105 	/* transfer buffers for isoc transfer */
106 	char **transfer_buffer;
107 
108 	/* current buffer */
109 	struct stk1160_buffer *buf;
110 };
111 
112 struct stk1160_fmt {
113 	char  *name;
114 	u32   fourcc;          /* v4l2 format id */
115 	int   depth;
116 };
117 
118 struct stk1160 {
119 	struct v4l2_device v4l2_dev;
120 	struct video_device vdev;
121 	struct v4l2_ctrl_handler ctrl_handler;
122 
123 	struct device *dev;
124 	struct usb_device *udev;
125 
126 	/* saa7115 subdev */
127 	struct v4l2_subdev *sd_saa7115;
128 
129 	/* isoc control struct */
130 	struct list_head avail_bufs;
131 
132 	/* video capture */
133 	struct vb2_queue vb_vidq;
134 
135 	/* max packet size of isoc transaction */
136 	int max_pkt_size;
137 	/* array of wMaxPacketSize */
138 	unsigned int *alt_max_pkt_size;
139 	/* alternate */
140 	int alt;
141 	/* Number of alternative settings */
142 	int num_alt;
143 
144 	struct stk1160_isoc_ctl isoc_ctl;
145 	char urb_buf[255];	 /* urb control msg buffer */
146 
147 	/* frame properties */
148 	int width;		  /* current frame width */
149 	int height;		  /* current frame height */
150 	unsigned int ctl_input;	  /* selected input */
151 	v4l2_std_id norm;	  /* current norm */
152 	struct stk1160_fmt *fmt;  /* selected format */
153 
154 	unsigned int field_count; /* not sure ??? */
155 	enum v4l2_field field;    /* also not sure :/ */
156 
157 	/* i2c i/o */
158 	struct i2c_adapter i2c_adap;
159 	struct i2c_client i2c_client;
160 
161 	struct mutex v4l_lock;
162 	struct mutex vb_queue_lock;
163 	spinlock_t buf_lock;
164 
165 	struct file *fh_owner;	/* filehandle ownership */
166 
167 	/* EXPERIMENTAL */
168 	struct snd_card *snd_card;
169 };
170 
171 struct regval {
172 	u16 reg;
173 	u16 val;
174 };
175 
176 /* Provided by stk1160-v4l.c */
177 int stk1160_vb2_setup(struct stk1160 *dev);
178 int stk1160_video_register(struct stk1160 *dev);
179 void stk1160_video_unregister(struct stk1160 *dev);
180 void stk1160_clear_queue(struct stk1160 *dev);
181 
182 /* Provided by stk1160-video.c */
183 int stk1160_alloc_isoc(struct stk1160 *dev);
184 void stk1160_free_isoc(struct stk1160 *dev);
185 void stk1160_cancel_isoc(struct stk1160 *dev);
186 void stk1160_uninit_isoc(struct stk1160 *dev);
187 
188 /* Provided by stk1160-i2c.c */
189 int stk1160_i2c_register(struct stk1160 *dev);
190 int stk1160_i2c_unregister(struct stk1160 *dev);
191 
192 /* Provided by stk1160-core.c */
193 int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value);
194 int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value);
195 int stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg,
196 		char *buf, int len);
197 int stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg,
198 		char *buf, int len);
199 void stk1160_select_input(struct stk1160 *dev);
200 
201 /* Provided by stk1160-ac97.c */
202 #ifdef CONFIG_VIDEO_STK1160_AC97
203 int stk1160_ac97_register(struct stk1160 *dev);
204 int stk1160_ac97_unregister(struct stk1160 *dev);
205 #else
206 static inline int stk1160_ac97_register(struct stk1160 *dev) { return 0; }
207 static inline int stk1160_ac97_unregister(struct stk1160 *dev) { return 0; }
208 #endif
209 
210