V4L/DVB (7595): Improve generic support for setting gpio values
[deliverable/linux.git] / drivers / media / video / em28xx / em28xx-core.c
1 /*
2 em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
3
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/usb.h>
28 #include <linux/vmalloc.h>
29
30 #include "em28xx.h"
31
32 /* #define ENABLE_DEBUG_ISOC_FRAMES */
33
34 static unsigned int core_debug;
35 module_param(core_debug,int,0644);
36 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
37
38 #define em28xx_coredbg(fmt, arg...) do {\
39 if (core_debug) \
40 printk(KERN_INFO "%s %s :"fmt, \
41 dev->name, __func__ , ##arg); } while (0)
42
43 static unsigned int reg_debug;
44 module_param(reg_debug,int,0644);
45 MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
46
47 #define em28xx_regdbg(fmt, arg...) do {\
48 if (reg_debug) \
49 printk(KERN_INFO "%s %s :"fmt, \
50 dev->name, __func__ , ##arg); } while (0)
51
52 static int alt = EM28XX_PINOUT;
53 module_param(alt, int, 0644);
54 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
55
56 /*
57 * em28xx_read_reg_req()
58 * reads data from the usb device specifying bRequest
59 */
60 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
61 char *buf, int len)
62 {
63 int ret, byte;
64
65 if (dev->state & DEV_DISCONNECTED)
66 return(-ENODEV);
67
68 em28xx_regdbg("req=%02x, reg=%02x ", req, reg);
69
70 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
71 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
72 0x0000, reg, buf, len, HZ);
73
74 if (reg_debug){
75 printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
76 for (byte = 0; byte < len; byte++) {
77 printk(" %02x", (unsigned char)buf[byte]);
78 }
79 printk("\n");
80 }
81
82 return ret;
83 }
84
85 /*
86 * em28xx_read_reg_req()
87 * reads data from the usb device specifying bRequest
88 */
89 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
90 {
91 u8 val;
92 int ret;
93
94 if (dev->state & DEV_DISCONNECTED)
95 return(-ENODEV);
96
97 em28xx_regdbg("req=%02x, reg=%02x:", req, reg);
98
99 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
100 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
101 0x0000, reg, &val, 1, HZ);
102
103 if (reg_debug)
104 printk(ret < 0 ? " failed!\n" :
105 "%02x\n", (unsigned char) val);
106
107 if (ret < 0)
108 return ret;
109
110 return val;
111 }
112
113 int em28xx_read_reg(struct em28xx *dev, u16 reg)
114 {
115 return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
116 }
117
118 /*
119 * em28xx_write_regs_req()
120 * sends data to the usb device, specifying bRequest
121 */
122 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
123 int len)
124 {
125 int ret;
126
127 /*usb_control_msg seems to expect a kmalloced buffer */
128 unsigned char *bufs;
129
130 if (dev->state & DEV_DISCONNECTED)
131 return(-ENODEV);
132
133 bufs = kmalloc(len, GFP_KERNEL);
134
135 em28xx_regdbg("req=%02x reg=%02x:", req, reg);
136
137 if (reg_debug) {
138 int i;
139 for (i = 0; i < len; ++i)
140 printk (" %02x", (unsigned char)buf[i]);
141 printk ("\n");
142 }
143
144 if (!bufs)
145 return -ENOMEM;
146 memcpy(bufs, buf, len);
147 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
148 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
149 0x0000, reg, bufs, len, HZ);
150 msleep(5); /* FIXME: magic number */
151 kfree(bufs);
152 return ret;
153 }
154
155 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
156 {
157 return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
158 }
159
160 /*
161 * em28xx_write_reg_bits()
162 * sets only some bits (specified by bitmask) of a register, by first reading
163 * the actual value
164 */
165 static int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
166 u8 bitmask)
167 {
168 int oldval;
169 u8 newval;
170 if ((oldval = em28xx_read_reg(dev, reg)) < 0)
171 return oldval;
172 newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
173 return em28xx_write_regs(dev, reg, &newval, 1);
174 }
175
176 /*
177 * em28xx_write_ac97()
178 * write a 16 bit value to the specified AC97 address (LSB first!)
179 */
180 static int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 *val)
181 {
182 int ret, i;
183 u8 addr = reg & 0x7f;
184 if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
185 return ret;
186 if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
187 return ret;
188
189 /* Wait up to 50 ms for AC97 command to complete */
190 for (i = 0; i < 10; i++) {
191 if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
192 return ret;
193 if (!(ret & 0x01))
194 return 0;
195 msleep(5);
196 }
197 em28xx_warn ("AC97 command still being executed: not handled properly!\n");
198 return 0;
199 }
200
201 static int em28xx_set_audio_source(struct em28xx *dev)
202 {
203 static char *enable = "\x08\x08";
204 static char *disable = "\x08\x88";
205 char *video = enable, *line = disable;
206 int ret;
207 u8 input;
208
209 if (dev->is_em2800) {
210 if (dev->ctl_ainput)
211 input = EM2800_AUDIO_SRC_LINE;
212 else
213 input = EM2800_AUDIO_SRC_TUNER;
214
215 ret = em28xx_write_regs(dev, EM2800_AUDIOSRC_REG, &input, 1);
216 if (ret < 0)
217 return ret;
218 }
219
220 if (dev->has_msp34xx)
221 input = EM28XX_AUDIO_SRC_TUNER;
222 else {
223 switch (dev->ctl_ainput) {
224 case EM28XX_AMUX_VIDEO:
225 input = EM28XX_AUDIO_SRC_TUNER;
226 break;
227 case EM28XX_AMUX_LINE_IN:
228 input = EM28XX_AUDIO_SRC_LINE;
229 break;
230 case EM28XX_AMUX_AC97_VIDEO:
231 input = EM28XX_AUDIO_SRC_LINE;
232 break;
233 case EM28XX_AMUX_AC97_LINE_IN:
234 input = EM28XX_AUDIO_SRC_LINE;
235 video = disable;
236 line = enable;
237 break;
238 }
239 }
240
241 ret = em28xx_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
242 if (ret < 0)
243 return ret;
244 msleep(5);
245
246 /* Sets AC97 mixer registers
247 This is seems to be needed, even for non-ac97 configs
248 */
249 ret = em28xx_write_ac97(dev, VIDEO_AC97, video);
250 if (ret < 0)
251 return ret;
252
253 ret = em28xx_write_ac97(dev, LINE_IN_AC97, line);
254
255 return ret;
256 }
257
258 int em28xx_audio_analog_set(struct em28xx *dev)
259 {
260 int ret;
261 char s[2] = { 0x00, 0x00 };
262 u8 xclk = 0x07;
263
264 s[0] |= 0x1f - dev->volume;
265 s[1] |= 0x1f - dev->volume;
266
267 /* Mute */
268 s[1] |= 0x80;
269 ret = em28xx_write_ac97(dev, MASTER_AC97, s);
270
271 if (ret < 0)
272 return ret;
273
274 if (dev->has_12mhz_i2s)
275 xclk |= 0x20;
276
277 if (!dev->mute)
278 xclk |= 0x80;
279
280 ret = em28xx_write_reg_bits(dev, XCLK_REG, xclk, 0xa7);
281 if (ret < 0)
282 return ret;
283 msleep(10);
284
285 /* Selects the proper audio input */
286 ret = em28xx_set_audio_source(dev);
287
288 /* Unmute device */
289 if (!dev->mute)
290 s[1] &= ~0x80;
291 ret = em28xx_write_ac97(dev, MASTER_AC97, s);
292
293 return ret;
294 }
295 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
296
297 int em28xx_colorlevels_set_default(struct em28xx *dev)
298 {
299 em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
300 em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
301 em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
302 em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
303 em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
304 em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
305
306 em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
307 em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
308 em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
309 em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
310 em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
311 em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
312 return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
313 }
314
315 int em28xx_capture_start(struct em28xx *dev, int start)
316 {
317 int rc;
318 /* FIXME: which is the best order? */
319 /* video registers are sampled by VREF */
320 rc = em28xx_write_reg_bits(dev, USBSUSP_REG,
321 start ? 0x10 : 0x00, 0x10);
322 if (rc < 0)
323 return rc;
324
325 if (!start) {
326 /* disable video capture */
327 rc = em28xx_write_regs(dev, VINENABLE_REG, "\x27", 1);
328 if (rc < 0)
329 return rc;
330 }
331
332 /* enable video capture */
333 rc = em28xx_write_regs_req(dev, 0x00, 0x48, "\x00", 1);
334 if (rc < 0)
335 return rc;
336 if (dev->mode == EM28XX_ANALOG_MODE)
337 rc = em28xx_write_regs(dev, VINENABLE_REG,"\x67", 1);
338 else
339 rc = em28xx_write_regs(dev, VINENABLE_REG,"\x37", 1);
340
341 if (rc < 0)
342 return rc;
343
344 msleep (6);
345
346 return rc;
347 }
348
349 int em28xx_outfmt_set_yuv422(struct em28xx *dev)
350 {
351 em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
352 em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
353 return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
354 }
355
356 static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
357 u8 ymin, u8 ymax)
358 {
359 em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
360
361 em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
362 em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
363 em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
364 return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
365 }
366
367 static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
368 u16 width, u16 height)
369 {
370 u8 cwidth = width;
371 u8 cheight = height;
372 u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
373
374 em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
375 (height | (overflow & 1) << 8));
376
377 em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
378 em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
379 em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
380 em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
381 return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
382 }
383
384 static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
385 {
386 u8 mode;
387 /* the em2800 scaler only supports scaling down to 50% */
388 if(dev->is_em2800)
389 mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
390 else {
391 u8 buf[2];
392 buf[0] = h;
393 buf[1] = h >> 8;
394 em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
395 buf[0] = v;
396 buf[1] = v >> 8;
397 em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
398 /* it seems that both H and V scalers must be active to work correctly */
399 mode = (h || v)? 0x30: 0x00;
400 }
401 return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
402 }
403
404 /* FIXME: this only function read values from dev */
405 int em28xx_resolution_set(struct em28xx *dev)
406 {
407 int width, height;
408 width = norm_maxw(dev);
409 height = norm_maxh(dev) >> 1;
410
411 em28xx_outfmt_set_yuv422(dev);
412 em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
413 em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
414 return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
415 }
416
417 int em28xx_set_alternate(struct em28xx *dev)
418 {
419 int errCode, prev_alt = dev->alt;
420 int i;
421 unsigned int min_pkt_size = dev->width * 2 + 4;
422
423 /* When image size is bigger than a certain value,
424 the frame size should be increased, otherwise, only
425 green screen will be received.
426 */
427 if (dev->width * 2 * dev->height > 720 * 240 * 2)
428 min_pkt_size *= 2;
429
430 for (i = 0; i < dev->num_alt; i++) {
431 /* stop when the selected alt setting offers enough bandwidth */
432 if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
433 dev->alt = i;
434 break;
435 /* otherwise make sure that we end up with the maximum bandwidth
436 because the min_pkt_size equation might be wrong...
437 */
438 } else if (dev->alt_max_pkt_size[i] >
439 dev->alt_max_pkt_size[dev->alt])
440 dev->alt = i;
441 }
442
443 if (dev->alt != prev_alt) {
444 em28xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
445 min_pkt_size, dev->alt);
446 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
447 em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
448 dev->alt, dev->max_pkt_size);
449 errCode = usb_set_interface(dev->udev, 0, dev->alt);
450 if (errCode < 0) {
451 em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",
452 dev->alt, errCode);
453 return errCode;
454 }
455 }
456 return 0;
457 }
This page took 0.063951 seconds and 5 git commands to generate.