Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / cx231xx / cx231xx-core.c
blobd4457f9488eee6c6e06feb1dd7c7ef727d6b91b8
1 /*
2 cx231xx-core.c - driver for Conexant Cx23100/101/102
3 USB video capture devices
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 Based on em28xx driver
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/usb.h>
28 #include <linux/vmalloc.h>
29 #include <media/v4l2-common.h>
30 #include <media/tuner.h>
32 #include "cx231xx.h"
33 #include "cx231xx-reg.h"
35 /* #define ENABLE_DEBUG_ISOC_FRAMES */
37 static unsigned int core_debug;
38 module_param(core_debug, int, 0644);
39 MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
41 #define cx231xx_coredbg(fmt, arg...) do {\
42 if (core_debug) \
43 printk(KERN_INFO "%s %s :"fmt, \
44 dev->name, __func__ , ##arg); } while (0)
46 static unsigned int reg_debug;
47 module_param(reg_debug, int, 0644);
48 MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
50 static int alt = CX231XX_PINOUT;
51 module_param(alt, int, 0644);
52 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
54 #define cx231xx_isocdbg(fmt, arg...) do {\
55 if (core_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
57 dev->name, __func__ , ##arg); } while (0)
59 /*****************************************************************
60 * Device control list functions *
61 ******************************************************************/
63 LIST_HEAD(cx231xx_devlist);
64 static DEFINE_MUTEX(cx231xx_devlist_mutex);
67 * cx231xx_realease_resources()
68 * unregisters the v4l2,i2c and usb devices
69 * called when the device gets disconected or at module unload
71 void cx231xx_remove_from_devlist(struct cx231xx *dev)
73 if (dev == NULL)
74 return;
75 if (dev->udev == NULL)
76 return;
78 if (atomic_read(&dev->devlist_count) > 0) {
79 mutex_lock(&cx231xx_devlist_mutex);
80 list_del(&dev->devlist);
81 atomic_dec(&dev->devlist_count);
82 mutex_unlock(&cx231xx_devlist_mutex);
86 void cx231xx_add_into_devlist(struct cx231xx *dev)
88 mutex_lock(&cx231xx_devlist_mutex);
89 list_add_tail(&dev->devlist, &cx231xx_devlist);
90 atomic_inc(&dev->devlist_count);
91 mutex_unlock(&cx231xx_devlist_mutex);
94 static LIST_HEAD(cx231xx_extension_devlist);
96 int cx231xx_register_extension(struct cx231xx_ops *ops)
98 struct cx231xx *dev = NULL;
100 mutex_lock(&cx231xx_devlist_mutex);
101 list_add_tail(&ops->next, &cx231xx_extension_devlist);
102 list_for_each_entry(dev, &cx231xx_devlist, devlist)
103 ops->init(dev);
105 printk(KERN_INFO DRIVER_NAME ": %s initialized\n", ops->name);
106 mutex_unlock(&cx231xx_devlist_mutex);
107 return 0;
109 EXPORT_SYMBOL(cx231xx_register_extension);
111 void cx231xx_unregister_extension(struct cx231xx_ops *ops)
113 struct cx231xx *dev = NULL;
115 mutex_lock(&cx231xx_devlist_mutex);
116 list_for_each_entry(dev, &cx231xx_devlist, devlist)
117 ops->fini(dev);
120 printk(KERN_INFO DRIVER_NAME ": %s removed\n", ops->name);
121 list_del(&ops->next);
122 mutex_unlock(&cx231xx_devlist_mutex);
124 EXPORT_SYMBOL(cx231xx_unregister_extension);
126 void cx231xx_init_extension(struct cx231xx *dev)
128 struct cx231xx_ops *ops = NULL;
130 mutex_lock(&cx231xx_devlist_mutex);
131 if (!list_empty(&cx231xx_extension_devlist)) {
132 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
133 if (ops->init)
134 ops->init(dev);
137 mutex_unlock(&cx231xx_devlist_mutex);
140 void cx231xx_close_extension(struct cx231xx *dev)
142 struct cx231xx_ops *ops = NULL;
144 mutex_lock(&cx231xx_devlist_mutex);
145 if (!list_empty(&cx231xx_extension_devlist)) {
146 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
147 if (ops->fini)
148 ops->fini(dev);
151 mutex_unlock(&cx231xx_devlist_mutex);
154 /****************************************************************
155 * U S B related functions *
156 *****************************************************************/
157 int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
158 struct cx231xx_i2c_xfer_data *req_data)
160 int status = 0;
161 struct cx231xx *dev = i2c_bus->dev;
162 struct VENDOR_REQUEST_IN ven_req;
164 u8 saddr_len = 0;
165 u8 _i2c_period = 0;
166 u8 _i2c_nostop = 0;
167 u8 _i2c_reserve = 0;
169 /* Get the I2C period, nostop and reserve parameters */
170 _i2c_period = i2c_bus->i2c_period;
171 _i2c_nostop = i2c_bus->i2c_nostop;
172 _i2c_reserve = i2c_bus->i2c_reserve;
174 saddr_len = req_data->saddr_len;
176 /* Set wValue */
177 if (saddr_len == 1) /* need check saddr_len == 0 */
178 ven_req.wValue =
179 req_data->
180 dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
181 _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
182 else
183 ven_req.wValue =
184 req_data->
185 dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
186 _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
188 /* set channel number */
189 if (req_data->direction & I2C_M_RD) {
190 /* channel number, for read,spec required channel_num +4 */
191 ven_req.bRequest = i2c_bus->nr + 4;
192 } else
193 ven_req.bRequest = i2c_bus->nr; /* channel number, */
195 /* set index value */
196 switch (saddr_len) {
197 case 0:
198 ven_req.wIndex = 0; /* need check */
199 break;
200 case 1:
201 ven_req.wIndex = (req_data->saddr_dat & 0xff);
202 break;
203 case 2:
204 ven_req.wIndex = req_data->saddr_dat;
205 break;
208 /* set wLength value */
209 ven_req.wLength = req_data->buf_size;
211 /* set bData value */
212 ven_req.bData = 0;
214 /* set the direction */
215 if (req_data->direction) {
216 ven_req.direction = USB_DIR_IN;
217 memset(req_data->p_buffer, 0x00, ven_req.wLength);
218 } else
219 ven_req.direction = USB_DIR_OUT;
221 /* set the buffer for read / write */
222 ven_req.pBuff = req_data->p_buffer;
225 /* call common vendor command request */
226 status = cx231xx_send_vendor_cmd(dev, &ven_req);
227 if (status < 0) {
228 cx231xx_info
229 ("UsbInterface::sendCommand, failed with status -%d\n",
230 status);
233 return status;
235 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
238 * Sends/Receives URB control messages, assuring to use a kalloced buffer
239 * for all operations (dev->urb_buf), to avoid using stacked buffers, as
240 * they aren't safe for usage with USB, due to DMA restrictions.
241 * Also implements the debug code for control URB's.
243 static int __usb_control_msg(struct cx231xx *dev, unsigned int pipe,
244 __u8 request, __u8 requesttype, __u16 value, __u16 index,
245 void *data, __u16 size, int timeout)
247 int rc, i;
249 if (reg_debug) {
250 printk(KERN_DEBUG "%s: (pipe 0x%08x): "
251 "%s: %02x %02x %02x %02x %02x %02x %02x %02x ",
252 dev->name,
253 pipe,
254 (requesttype & USB_DIR_IN) ? "IN" : "OUT",
255 requesttype,
256 request,
257 value & 0xff, value >> 8,
258 index & 0xff, index >> 8,
259 size & 0xff, size >> 8);
260 if (!(requesttype & USB_DIR_IN)) {
261 printk(KERN_CONT ">>>");
262 for (i = 0; i < size; i++)
263 printk(KERN_CONT " %02x",
264 ((unsigned char *)data)[i]);
268 /* Do the real call to usb_control_msg */
269 mutex_lock(&dev->ctrl_urb_lock);
270 if (!(requesttype & USB_DIR_IN) && size)
271 memcpy(dev->urb_buf, data, size);
272 rc = usb_control_msg(dev->udev, pipe, request, requesttype, value,
273 index, dev->urb_buf, size, timeout);
274 if ((requesttype & USB_DIR_IN) && size)
275 memcpy(data, dev->urb_buf, size);
276 mutex_unlock(&dev->ctrl_urb_lock);
278 if (reg_debug) {
279 if (unlikely(rc < 0)) {
280 printk(KERN_CONT "FAILED!\n");
281 return rc;
284 if ((requesttype & USB_DIR_IN)) {
285 printk(KERN_CONT "<<<");
286 for (i = 0; i < size; i++)
287 printk(KERN_CONT " %02x",
288 ((unsigned char *)data)[i]);
290 printk(KERN_CONT "\n");
293 return rc;
298 * cx231xx_read_ctrl_reg()
299 * reads data from the usb device specifying bRequest and wValue
301 int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
302 char *buf, int len)
304 u8 val = 0;
305 int ret;
306 int pipe = usb_rcvctrlpipe(dev->udev, 0);
308 if (dev->state & DEV_DISCONNECTED)
309 return -ENODEV;
311 if (len > URB_MAX_CTRL_SIZE)
312 return -EINVAL;
314 switch (len) {
315 case 1:
316 val = ENABLE_ONE_BYTE;
317 break;
318 case 2:
319 val = ENABLE_TWE_BYTE;
320 break;
321 case 3:
322 val = ENABLE_THREE_BYTE;
323 break;
324 case 4:
325 val = ENABLE_FOUR_BYTE;
326 break;
327 default:
328 val = 0xFF; /* invalid option */
331 if (val == 0xFF)
332 return -EINVAL;
334 ret = __usb_control_msg(dev, pipe, req,
335 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
336 val, reg, buf, len, HZ);
337 return ret;
340 int cx231xx_send_vendor_cmd(struct cx231xx *dev,
341 struct VENDOR_REQUEST_IN *ven_req)
343 int ret;
344 int pipe = 0;
345 int unsend_size = 0;
346 u8 *pdata;
348 if (dev->state & DEV_DISCONNECTED)
349 return -ENODEV;
351 if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
352 return -EINVAL;
354 if (ven_req->direction)
355 pipe = usb_rcvctrlpipe(dev->udev, 0);
356 else
357 pipe = usb_sndctrlpipe(dev->udev, 0);
360 * If the cx23102 read more than 4 bytes with i2c bus,
361 * need chop to 4 byte per request
363 if ((ven_req->wLength > 4) && ((ven_req->bRequest == 0x4) ||
364 (ven_req->bRequest == 0x5) ||
365 (ven_req->bRequest == 0x6))) {
366 unsend_size = 0;
367 pdata = ven_req->pBuff;
370 unsend_size = ven_req->wLength;
372 /* the first package */
373 ven_req->wValue = ven_req->wValue & 0xFFFB;
374 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x2;
375 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
376 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
377 ven_req->wValue, ven_req->wIndex, pdata,
378 0x0004, HZ);
379 unsend_size = unsend_size - 4;
381 /* the middle package */
382 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x42;
383 while (unsend_size - 4 > 0) {
384 pdata = pdata + 4;
385 ret = __usb_control_msg(dev, pipe,
386 ven_req->bRequest,
387 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
388 ven_req->wValue, ven_req->wIndex, pdata,
389 0x0004, HZ);
390 unsend_size = unsend_size - 4;
393 /* the last package */
394 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x40;
395 pdata = pdata + 4;
396 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
397 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
398 ven_req->wValue, ven_req->wIndex, pdata,
399 unsend_size, HZ);
400 } else {
401 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
402 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
403 ven_req->wValue, ven_req->wIndex,
404 ven_req->pBuff, ven_req->wLength, HZ);
407 return ret;
411 * cx231xx_write_ctrl_reg()
412 * sends data to the usb device, specifying bRequest
414 int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
415 int len)
417 u8 val = 0;
418 int ret;
419 int pipe = usb_sndctrlpipe(dev->udev, 0);
421 if (dev->state & DEV_DISCONNECTED)
422 return -ENODEV;
424 if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
425 return -EINVAL;
427 switch (len) {
428 case 1:
429 val = ENABLE_ONE_BYTE;
430 break;
431 case 2:
432 val = ENABLE_TWE_BYTE;
433 break;
434 case 3:
435 val = ENABLE_THREE_BYTE;
436 break;
437 case 4:
438 val = ENABLE_FOUR_BYTE;
439 break;
440 default:
441 val = 0xFF; /* invalid option */
444 if (val == 0xFF)
445 return -EINVAL;
447 if (reg_debug) {
448 int byte;
450 cx231xx_isocdbg("(pipe 0x%08x): "
451 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
452 pipe,
453 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
454 req, 0, val, reg & 0xff,
455 reg >> 8, len & 0xff, len >> 8);
457 for (byte = 0; byte < len; byte++)
458 cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
459 cx231xx_isocdbg("\n");
462 ret = __usb_control_msg(dev, pipe, req,
463 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
464 val, reg, buf, len, HZ);
466 return ret;
469 /****************************************************************
470 * USB Alternate Setting functions *
471 *****************************************************************/
473 int cx231xx_set_video_alternate(struct cx231xx *dev)
475 int errCode, prev_alt = dev->video_mode.alt;
476 unsigned int min_pkt_size = dev->width * 2 + 4;
477 u32 usb_interface_index = 0;
479 /* When image size is bigger than a certain value,
480 the frame size should be increased, otherwise, only
481 green screen will be received.
483 if (dev->width * 2 * dev->height > 720 * 240 * 2)
484 min_pkt_size *= 2;
486 if (dev->width > 360) {
487 /* resolutions: 720,704,640 */
488 dev->video_mode.alt = 3;
489 } else if (dev->width > 180) {
490 /* resolutions: 360,352,320,240 */
491 dev->video_mode.alt = 2;
492 } else if (dev->width > 0) {
493 /* resolutions: 180,176,160,128,88 */
494 dev->video_mode.alt = 1;
495 } else {
496 /* Change to alt0 BULK to release USB bandwidth */
497 dev->video_mode.alt = 0;
500 if (dev->USE_ISO == 0)
501 dev->video_mode.alt = 0;
503 cx231xx_coredbg("dev->video_mode.alt= %d\n", dev->video_mode.alt);
505 /* Get the correct video interface Index */
506 usb_interface_index =
507 dev->current_pcb_config.hs_config_info[0].interface_info.
508 video_index + 1;
510 if (dev->video_mode.alt != prev_alt) {
511 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
512 min_pkt_size, dev->video_mode.alt);
514 if (dev->video_mode.alt_max_pkt_size != NULL)
515 dev->video_mode.max_pkt_size =
516 dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
517 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
518 dev->video_mode.alt,
519 dev->video_mode.max_pkt_size);
520 errCode =
521 usb_set_interface(dev->udev, usb_interface_index,
522 dev->video_mode.alt);
523 if (errCode < 0) {
524 cx231xx_errdev
525 ("cannot change alt number to %d (error=%i)\n",
526 dev->video_mode.alt, errCode);
527 return errCode;
530 return 0;
533 int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
535 int status = 0;
536 u32 usb_interface_index = 0;
537 u32 max_pkt_size = 0;
539 switch (index) {
540 case INDEX_TS1:
541 usb_interface_index =
542 dev->current_pcb_config.hs_config_info[0].interface_info.
543 ts1_index + 1;
544 dev->ts1_mode.alt = alt;
545 if (dev->ts1_mode.alt_max_pkt_size != NULL)
546 max_pkt_size = dev->ts1_mode.max_pkt_size =
547 dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
548 break;
549 case INDEX_TS2:
550 usb_interface_index =
551 dev->current_pcb_config.hs_config_info[0].interface_info.
552 ts2_index + 1;
553 break;
554 case INDEX_AUDIO:
555 usb_interface_index =
556 dev->current_pcb_config.hs_config_info[0].interface_info.
557 audio_index + 1;
558 dev->adev.alt = alt;
559 if (dev->adev.alt_max_pkt_size != NULL)
560 max_pkt_size = dev->adev.max_pkt_size =
561 dev->adev.alt_max_pkt_size[dev->adev.alt];
562 break;
563 case INDEX_VIDEO:
564 usb_interface_index =
565 dev->current_pcb_config.hs_config_info[0].interface_info.
566 video_index + 1;
567 dev->video_mode.alt = alt;
568 if (dev->video_mode.alt_max_pkt_size != NULL)
569 max_pkt_size = dev->video_mode.max_pkt_size =
570 dev->video_mode.alt_max_pkt_size[dev->video_mode.
571 alt];
572 break;
573 case INDEX_VANC:
574 if (dev->board.no_alt_vanc)
575 return 0;
576 usb_interface_index =
577 dev->current_pcb_config.hs_config_info[0].interface_info.
578 vanc_index + 1;
579 dev->vbi_mode.alt = alt;
580 if (dev->vbi_mode.alt_max_pkt_size != NULL)
581 max_pkt_size = dev->vbi_mode.max_pkt_size =
582 dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
583 break;
584 case INDEX_HANC:
585 usb_interface_index =
586 dev->current_pcb_config.hs_config_info[0].interface_info.
587 hanc_index + 1;
588 dev->sliced_cc_mode.alt = alt;
589 if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
590 max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
591 dev->sliced_cc_mode.alt_max_pkt_size[dev->
592 sliced_cc_mode.
593 alt];
594 break;
595 default:
596 break;
599 if (alt > 0 && max_pkt_size == 0) {
600 cx231xx_errdev
601 ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
602 usb_interface_index, alt);
603 /*To workaround error number=-71 on EP0 for videograbber,
604 need add following codes.*/
605 if (dev->board.no_alt_vanc)
606 return -1;
609 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u,"
610 "Interface = %d\n", alt, max_pkt_size,
611 usb_interface_index);
613 if (usb_interface_index > 0) {
614 status = usb_set_interface(dev->udev, usb_interface_index, alt);
615 if (status < 0) {
616 cx231xx_errdev
617 ("can't change interface %d alt no. to %d (err=%i)\n",
618 usb_interface_index, alt, status);
619 return status;
623 return status;
625 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
627 int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
629 int rc = 0;
631 if (!gpio)
632 return rc;
634 /* Send GPIO reset sequences specified at board entry */
635 while (gpio->sleep >= 0) {
636 rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
637 if (rc < 0)
638 return rc;
640 if (gpio->sleep > 0)
641 msleep(gpio->sleep);
643 gpio++;
645 return rc;
648 int cx231xx_demod_reset(struct cx231xx *dev)
651 u8 status = 0;
652 u8 value[4] = { 0, 0, 0, 0 };
654 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
655 value, 4);
657 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
658 value[0], value[1], value[2], value[3]);
660 cx231xx_coredbg("Enter cx231xx_demod_reset()\n");
662 value[1] = (u8) 0x3;
663 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
664 PWR_CTL_EN, value, 4);
665 msleep(10);
667 value[1] = (u8) 0x0;
668 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
669 PWR_CTL_EN, value, 4);
670 msleep(10);
672 value[1] = (u8) 0x3;
673 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
674 PWR_CTL_EN, value, 4);
675 msleep(10);
679 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
680 value, 4);
682 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
683 value[0], value[1], value[2], value[3]);
685 return status;
687 EXPORT_SYMBOL_GPL(cx231xx_demod_reset);
688 int is_fw_load(struct cx231xx *dev)
690 return cx231xx_check_fw(dev);
692 EXPORT_SYMBOL_GPL(is_fw_load);
694 int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
696 int errCode = 0;
698 if (dev->mode == set_mode)
699 return 0;
701 if (set_mode == CX231XX_SUSPEND) {
702 /* Set the chip in power saving mode */
703 dev->mode = set_mode;
706 /* Resource is locked */
707 if (dev->mode != CX231XX_SUSPEND)
708 return -EINVAL;
710 dev->mode = set_mode;
712 if (dev->mode == CX231XX_DIGITAL_MODE)/* Set Digital power mode */ {
713 /* set AGC mode to Digital */
714 switch (dev->model) {
715 case CX231XX_BOARD_CNXT_CARRAERA:
716 case CX231XX_BOARD_CNXT_RDE_250:
717 case CX231XX_BOARD_CNXT_SHELBY:
718 case CX231XX_BOARD_CNXT_RDU_250:
719 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
720 break;
721 case CX231XX_BOARD_CNXT_RDE_253S:
722 case CX231XX_BOARD_CNXT_RDU_253S:
723 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
724 break;
725 case CX231XX_BOARD_HAUPPAUGE_EXETER:
726 errCode = cx231xx_set_power_mode(dev,
727 POLARIS_AVMODE_DIGITAL);
728 break;
729 default:
730 break;
732 } else/* Set Analog Power mode */ {
733 /* set AGC mode to Analog */
734 switch (dev->model) {
735 case CX231XX_BOARD_CNXT_CARRAERA:
736 case CX231XX_BOARD_CNXT_RDE_250:
737 case CX231XX_BOARD_CNXT_SHELBY:
738 case CX231XX_BOARD_CNXT_RDU_250:
739 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
740 break;
741 case CX231XX_BOARD_CNXT_RDE_253S:
742 case CX231XX_BOARD_CNXT_RDU_253S:
743 case CX231XX_BOARD_HAUPPAUGE_EXETER:
744 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
745 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
746 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
747 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
748 break;
749 default:
750 break;
754 return 0;
756 EXPORT_SYMBOL_GPL(cx231xx_set_mode);
758 int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size)
760 int errCode = 0;
761 int actlen, ret = -ENOMEM;
762 u32 *buffer;
764 buffer = kzalloc(4096, GFP_KERNEL);
765 if (buffer == NULL) {
766 cx231xx_info("out of mem\n");
767 return -ENOMEM;
769 memcpy(&buffer[0], firmware, 4096);
771 ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5),
772 buffer, 4096, &actlen, 2000);
774 if (ret)
775 cx231xx_info("bulk message failed: %d (%d/%d)", ret,
776 size, actlen);
777 else {
778 errCode = actlen != size ? -1 : 0;
780 kfree(buffer);
781 return 0;
784 /*****************************************************************
785 * URB Streaming functions *
786 ******************************************************************/
789 * IRQ callback, called by URB callback
791 static void cx231xx_isoc_irq_callback(struct urb *urb)
793 struct cx231xx_dmaqueue *dma_q = urb->context;
794 struct cx231xx_video_mode *vmode =
795 container_of(dma_q, struct cx231xx_video_mode, vidq);
796 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
797 int rc, i;
799 switch (urb->status) {
800 case 0: /* success */
801 case -ETIMEDOUT: /* NAK */
802 break;
803 case -ECONNRESET: /* kill */
804 case -ENOENT:
805 case -ESHUTDOWN:
806 return;
807 default: /* error */
808 cx231xx_isocdbg("urb completition error %d.\n", urb->status);
809 break;
812 /* Copy data from URB */
813 spin_lock(&dev->video_mode.slock);
814 rc = dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
815 spin_unlock(&dev->video_mode.slock);
817 /* Reset urb buffers */
818 for (i = 0; i < urb->number_of_packets; i++) {
819 urb->iso_frame_desc[i].status = 0;
820 urb->iso_frame_desc[i].actual_length = 0;
822 urb->status = 0;
824 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
825 if (urb->status) {
826 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
827 urb->status);
830 /*****************************************************************
831 * URB Streaming functions *
832 ******************************************************************/
835 * IRQ callback, called by URB callback
837 static void cx231xx_bulk_irq_callback(struct urb *urb)
839 struct cx231xx_dmaqueue *dma_q = urb->context;
840 struct cx231xx_video_mode *vmode =
841 container_of(dma_q, struct cx231xx_video_mode, vidq);
842 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
843 int rc;
845 switch (urb->status) {
846 case 0: /* success */
847 case -ETIMEDOUT: /* NAK */
848 break;
849 case -ECONNRESET: /* kill */
850 case -ENOENT:
851 case -ESHUTDOWN:
852 return;
853 default: /* error */
854 cx231xx_isocdbg("urb completition error %d.\n", urb->status);
855 break;
858 /* Copy data from URB */
859 spin_lock(&dev->video_mode.slock);
860 rc = dev->video_mode.bulk_ctl.bulk_copy(dev, urb);
861 spin_unlock(&dev->video_mode.slock);
863 /* Reset urb buffers */
864 urb->status = 0;
866 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
867 if (urb->status) {
868 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
869 urb->status);
873 * Stop and Deallocate URBs
875 void cx231xx_uninit_isoc(struct cx231xx *dev)
877 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
878 struct urb *urb;
879 int i;
881 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
883 dev->video_mode.isoc_ctl.nfields = -1;
884 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
885 urb = dev->video_mode.isoc_ctl.urb[i];
886 if (urb) {
887 if (!irqs_disabled())
888 usb_kill_urb(urb);
889 else
890 usb_unlink_urb(urb);
892 if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
893 usb_free_coherent(dev->udev,
894 urb->transfer_buffer_length,
895 dev->video_mode.isoc_ctl.
896 transfer_buffer[i],
897 urb->transfer_dma);
899 usb_free_urb(urb);
900 dev->video_mode.isoc_ctl.urb[i] = NULL;
902 dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
905 kfree(dev->video_mode.isoc_ctl.urb);
906 kfree(dev->video_mode.isoc_ctl.transfer_buffer);
907 kfree(dma_q->p_left_data);
909 dev->video_mode.isoc_ctl.urb = NULL;
910 dev->video_mode.isoc_ctl.transfer_buffer = NULL;
911 dev->video_mode.isoc_ctl.num_bufs = 0;
912 dma_q->p_left_data = NULL;
914 if (dev->mode_tv == 0)
915 cx231xx_capture_start(dev, 0, Raw_Video);
916 else
917 cx231xx_capture_start(dev, 0, TS1_serial_mode);
921 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
924 * Stop and Deallocate URBs
926 void cx231xx_uninit_bulk(struct cx231xx *dev)
928 struct urb *urb;
929 int i;
931 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n");
933 dev->video_mode.bulk_ctl.nfields = -1;
934 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
935 urb = dev->video_mode.bulk_ctl.urb[i];
936 if (urb) {
937 if (!irqs_disabled())
938 usb_kill_urb(urb);
939 else
940 usb_unlink_urb(urb);
942 if (dev->video_mode.bulk_ctl.transfer_buffer[i]) {
943 usb_free_coherent(dev->udev,
944 urb->transfer_buffer_length,
945 dev->video_mode.isoc_ctl.
946 transfer_buffer[i],
947 urb->transfer_dma);
949 usb_free_urb(urb);
950 dev->video_mode.bulk_ctl.urb[i] = NULL;
952 dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL;
955 kfree(dev->video_mode.bulk_ctl.urb);
956 kfree(dev->video_mode.bulk_ctl.transfer_buffer);
958 dev->video_mode.bulk_ctl.urb = NULL;
959 dev->video_mode.bulk_ctl.transfer_buffer = NULL;
960 dev->video_mode.bulk_ctl.num_bufs = 0;
962 if (dev->mode_tv == 0)
963 cx231xx_capture_start(dev, 0, Raw_Video);
964 else
965 cx231xx_capture_start(dev, 0, TS1_serial_mode);
969 EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk);
972 * Allocate URBs and start IRQ
974 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
975 int num_bufs, int max_pkt_size,
976 int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
978 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
979 int i;
980 int sb_size, pipe;
981 struct urb *urb;
982 int j, k;
983 int rc;
985 /* De-allocates all pending stuff */
986 cx231xx_uninit_isoc(dev);
988 dma_q->p_left_data = kzalloc(4096, GFP_KERNEL);
989 if (dma_q->p_left_data == NULL) {
990 cx231xx_info("out of mem\n");
991 return -ENOMEM;
996 dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
997 dev->video_mode.isoc_ctl.num_bufs = num_bufs;
998 dma_q->pos = 0;
999 dma_q->is_partial_line = 0;
1000 dma_q->last_sav = 0;
1001 dma_q->current_field = -1;
1002 dma_q->field1_done = 0;
1003 dma_q->lines_per_field = dev->height / 2;
1004 dma_q->bytes_left_in_line = dev->width << 1;
1005 dma_q->lines_completed = 0;
1006 dma_q->mpeg_buffer_done = 0;
1007 dma_q->left_data_count = 0;
1008 dma_q->mpeg_buffer_completed = 0;
1009 dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD;
1010 dma_q->ps_head[0] = 0x00;
1011 dma_q->ps_head[1] = 0x00;
1012 dma_q->ps_head[2] = 0x01;
1013 dma_q->ps_head[3] = 0xBA;
1014 for (i = 0; i < 8; i++)
1015 dma_q->partial_buf[i] = 0;
1017 dev->video_mode.isoc_ctl.urb =
1018 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1019 if (!dev->video_mode.isoc_ctl.urb) {
1020 cx231xx_errdev("cannot alloc memory for usb buffers\n");
1021 return -ENOMEM;
1024 dev->video_mode.isoc_ctl.transfer_buffer =
1025 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1026 if (!dev->video_mode.isoc_ctl.transfer_buffer) {
1027 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
1028 kfree(dev->video_mode.isoc_ctl.urb);
1029 return -ENOMEM;
1032 dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
1033 dev->video_mode.isoc_ctl.buf = NULL;
1035 sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
1037 if (dev->mode_tv == 1)
1038 dev->video_mode.end_point_addr = 0x81;
1039 else
1040 dev->video_mode.end_point_addr = 0x84;
1043 /* allocate urbs and transfer buffers */
1044 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
1045 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
1046 if (!urb) {
1047 cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i);
1048 cx231xx_uninit_isoc(dev);
1049 return -ENOMEM;
1051 dev->video_mode.isoc_ctl.urb[i] = urb;
1053 dev->video_mode.isoc_ctl.transfer_buffer[i] =
1054 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
1055 &urb->transfer_dma);
1056 if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
1057 cx231xx_err("unable to allocate %i bytes for transfer"
1058 " buffer %i%s\n",
1059 sb_size, i,
1060 in_interrupt() ? " while in int" : "");
1061 cx231xx_uninit_isoc(dev);
1062 return -ENOMEM;
1064 memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
1066 pipe =
1067 usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
1069 usb_fill_int_urb(urb, dev->udev, pipe,
1070 dev->video_mode.isoc_ctl.transfer_buffer[i],
1071 sb_size, cx231xx_isoc_irq_callback, dma_q, 1);
1073 urb->number_of_packets = max_packets;
1074 urb->transfer_flags = URB_ISO_ASAP;
1076 k = 0;
1077 for (j = 0; j < max_packets; j++) {
1078 urb->iso_frame_desc[j].offset = k;
1079 urb->iso_frame_desc[j].length =
1080 dev->video_mode.isoc_ctl.max_pkt_size;
1081 k += dev->video_mode.isoc_ctl.max_pkt_size;
1085 init_waitqueue_head(&dma_q->wq);
1087 /* submit urbs and enables IRQ */
1088 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
1089 rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
1090 GFP_ATOMIC);
1091 if (rc) {
1092 cx231xx_err("submit of urb %i failed (error=%i)\n", i,
1093 rc);
1094 cx231xx_uninit_isoc(dev);
1095 return rc;
1099 if (dev->mode_tv == 0)
1100 cx231xx_capture_start(dev, 1, Raw_Video);
1101 else
1102 cx231xx_capture_start(dev, 1, TS1_serial_mode);
1104 return 0;
1106 EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
1109 * Allocate URBs and start IRQ
1111 int cx231xx_init_bulk(struct cx231xx *dev, int max_packets,
1112 int num_bufs, int max_pkt_size,
1113 int (*bulk_copy) (struct cx231xx *dev, struct urb *urb))
1115 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
1116 int i;
1117 int sb_size, pipe;
1118 struct urb *urb;
1119 int rc;
1121 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1123 cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input);
1125 video_mux(dev, dev->video_input);
1127 /* De-allocates all pending stuff */
1128 cx231xx_uninit_bulk(dev);
1130 dev->video_mode.bulk_ctl.bulk_copy = bulk_copy;
1131 dev->video_mode.bulk_ctl.num_bufs = num_bufs;
1132 dma_q->pos = 0;
1133 dma_q->is_partial_line = 0;
1134 dma_q->last_sav = 0;
1135 dma_q->current_field = -1;
1136 dma_q->field1_done = 0;
1137 dma_q->lines_per_field = dev->height / 2;
1138 dma_q->bytes_left_in_line = dev->width << 1;
1139 dma_q->lines_completed = 0;
1140 dma_q->mpeg_buffer_done = 0;
1141 dma_q->left_data_count = 0;
1142 dma_q->mpeg_buffer_completed = 0;
1143 dma_q->ps_head[0] = 0x00;
1144 dma_q->ps_head[1] = 0x00;
1145 dma_q->ps_head[2] = 0x01;
1146 dma_q->ps_head[3] = 0xBA;
1147 for (i = 0; i < 8; i++)
1148 dma_q->partial_buf[i] = 0;
1150 dev->video_mode.bulk_ctl.urb =
1151 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1152 if (!dev->video_mode.bulk_ctl.urb) {
1153 cx231xx_errdev("cannot alloc memory for usb buffers\n");
1154 return -ENOMEM;
1157 dev->video_mode.bulk_ctl.transfer_buffer =
1158 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1159 if (!dev->video_mode.bulk_ctl.transfer_buffer) {
1160 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
1161 kfree(dev->video_mode.bulk_ctl.urb);
1162 return -ENOMEM;
1165 dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size;
1166 dev->video_mode.bulk_ctl.buf = NULL;
1168 sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size;
1170 if (dev->mode_tv == 1)
1171 dev->video_mode.end_point_addr = 0x81;
1172 else
1173 dev->video_mode.end_point_addr = 0x84;
1176 /* allocate urbs and transfer buffers */
1177 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
1178 urb = usb_alloc_urb(0, GFP_KERNEL);
1179 if (!urb) {
1180 cx231xx_err("cannot alloc bulk_ctl.urb %i\n", i);
1181 cx231xx_uninit_bulk(dev);
1182 return -ENOMEM;
1184 dev->video_mode.bulk_ctl.urb[i] = urb;
1185 urb->transfer_flags = 0;
1187 dev->video_mode.bulk_ctl.transfer_buffer[i] =
1188 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
1189 &urb->transfer_dma);
1190 if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) {
1191 cx231xx_err("unable to allocate %i bytes for transfer"
1192 " buffer %i%s\n",
1193 sb_size, i,
1194 in_interrupt() ? " while in int" : "");
1195 cx231xx_uninit_bulk(dev);
1196 return -ENOMEM;
1198 memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size);
1200 pipe = usb_rcvbulkpipe(dev->udev,
1201 dev->video_mode.end_point_addr);
1202 usb_fill_bulk_urb(urb, dev->udev, pipe,
1203 dev->video_mode.bulk_ctl.transfer_buffer[i],
1204 sb_size, cx231xx_bulk_irq_callback, dma_q);
1207 init_waitqueue_head(&dma_q->wq);
1209 /* submit urbs and enables IRQ */
1210 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
1211 rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i],
1212 GFP_ATOMIC);
1213 if (rc) {
1214 cx231xx_err("submit of urb %i failed (error=%i)\n", i,
1215 rc);
1216 cx231xx_uninit_bulk(dev);
1217 return rc;
1221 if (dev->mode_tv == 0)
1222 cx231xx_capture_start(dev, 1, Raw_Video);
1223 else
1224 cx231xx_capture_start(dev, 1, TS1_serial_mode);
1226 return 0;
1228 EXPORT_SYMBOL_GPL(cx231xx_init_bulk);
1229 void cx231xx_stop_TS1(struct cx231xx *dev)
1231 int status = 0;
1232 u8 val[4] = { 0, 0, 0, 0 };
1234 val[0] = 0x00;
1235 val[1] = 0x03;
1236 val[2] = 0x00;
1237 val[3] = 0x00;
1238 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1239 TS_MODE_REG, val, 4);
1241 val[0] = 0x00;
1242 val[1] = 0x70;
1243 val[2] = 0x04;
1244 val[3] = 0x00;
1245 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1246 TS1_CFG_REG, val, 4);
1248 /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */
1249 void cx231xx_start_TS1(struct cx231xx *dev)
1251 int status = 0;
1252 u8 val[4] = { 0, 0, 0, 0 };
1254 val[0] = 0x03;
1255 val[1] = 0x03;
1256 val[2] = 0x00;
1257 val[3] = 0x00;
1258 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1259 TS_MODE_REG, val, 4);
1261 val[0] = 0x04;
1262 val[1] = 0xA3;
1263 val[2] = 0x3B;
1264 val[3] = 0x00;
1265 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1266 TS1_CFG_REG, val, 4);
1268 /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */
1269 /*****************************************************************
1270 * Device Init/UnInit functions *
1271 ******************************************************************/
1272 int cx231xx_dev_init(struct cx231xx *dev)
1274 int errCode = 0;
1276 /* Initialize I2C bus */
1278 /* External Master 1 Bus */
1279 dev->i2c_bus[0].nr = 0;
1280 dev->i2c_bus[0].dev = dev;
1281 dev->i2c_bus[0].i2c_period = I2C_SPEED_100K; /* 100 KHz */
1282 dev->i2c_bus[0].i2c_nostop = 0;
1283 dev->i2c_bus[0].i2c_reserve = 0;
1285 /* External Master 2 Bus */
1286 dev->i2c_bus[1].nr = 1;
1287 dev->i2c_bus[1].dev = dev;
1288 dev->i2c_bus[1].i2c_period = I2C_SPEED_100K; /* 100 KHz */
1289 dev->i2c_bus[1].i2c_nostop = 0;
1290 dev->i2c_bus[1].i2c_reserve = 0;
1292 /* Internal Master 3 Bus */
1293 dev->i2c_bus[2].nr = 2;
1294 dev->i2c_bus[2].dev = dev;
1295 dev->i2c_bus[2].i2c_period = I2C_SPEED_100K; /* 100kHz */
1296 dev->i2c_bus[2].i2c_nostop = 0;
1297 dev->i2c_bus[2].i2c_reserve = 0;
1299 /* register I2C buses */
1300 cx231xx_i2c_register(&dev->i2c_bus[0]);
1301 cx231xx_i2c_register(&dev->i2c_bus[1]);
1302 cx231xx_i2c_register(&dev->i2c_bus[2]);
1304 /* init hardware */
1305 /* Note : with out calling set power mode function,
1306 afe can not be set up correctly */
1307 if (dev->board.external_av) {
1308 errCode = cx231xx_set_power_mode(dev,
1309 POLARIS_AVMODE_ENXTERNAL_AV);
1310 if (errCode < 0) {
1311 cx231xx_errdev
1312 ("%s: Failed to set Power - errCode [%d]!\n",
1313 __func__, errCode);
1314 return errCode;
1316 } else {
1317 errCode = cx231xx_set_power_mode(dev,
1318 POLARIS_AVMODE_ANALOGT_TV);
1319 if (errCode < 0) {
1320 cx231xx_errdev
1321 ("%s: Failed to set Power - errCode [%d]!\n",
1322 __func__, errCode);
1323 return errCode;
1327 /* reset the Tuner, if it is a Xceive tuner */
1328 if ((dev->board.tuner_type == TUNER_XC5000) ||
1329 (dev->board.tuner_type == TUNER_XC2028))
1330 cx231xx_gpio_set(dev, dev->board.tuner_gpio);
1332 /* initialize Colibri block */
1333 errCode = cx231xx_afe_init_super_block(dev, 0x23c);
1334 if (errCode < 0) {
1335 cx231xx_errdev
1336 ("%s: cx231xx_afe init super block - errCode [%d]!\n",
1337 __func__, errCode);
1338 return errCode;
1340 errCode = cx231xx_afe_init_channels(dev);
1341 if (errCode < 0) {
1342 cx231xx_errdev
1343 ("%s: cx231xx_afe init channels - errCode [%d]!\n",
1344 __func__, errCode);
1345 return errCode;
1348 /* Set DIF in By pass mode */
1349 errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
1350 if (errCode < 0) {
1351 cx231xx_errdev
1352 ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
1353 __func__, errCode);
1354 return errCode;
1357 /* I2S block related functions */
1358 errCode = cx231xx_i2s_blk_initialize(dev);
1359 if (errCode < 0) {
1360 cx231xx_errdev
1361 ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
1362 __func__, errCode);
1363 return errCode;
1366 /* init control pins */
1367 errCode = cx231xx_init_ctrl_pin_status(dev);
1368 if (errCode < 0) {
1369 cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
1370 __func__, errCode);
1371 return errCode;
1374 /* set AGC mode to Analog */
1375 switch (dev->model) {
1376 case CX231XX_BOARD_CNXT_CARRAERA:
1377 case CX231XX_BOARD_CNXT_RDE_250:
1378 case CX231XX_BOARD_CNXT_SHELBY:
1379 case CX231XX_BOARD_CNXT_RDU_250:
1380 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
1381 break;
1382 case CX231XX_BOARD_CNXT_RDE_253S:
1383 case CX231XX_BOARD_CNXT_RDU_253S:
1384 case CX231XX_BOARD_HAUPPAUGE_EXETER:
1385 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
1386 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
1387 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
1388 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
1389 break;
1390 default:
1391 break;
1393 if (errCode < 0) {
1394 cx231xx_errdev
1395 ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
1396 __func__, errCode);
1397 return errCode;
1400 /* set all alternate settings to zero initially */
1401 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1402 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1403 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1404 if (dev->board.has_dvb)
1405 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
1407 /* set the I2C master port to 3 on channel 1 */
1408 errCode = cx231xx_enable_i2c_port_3(dev, true);
1410 return errCode;
1412 EXPORT_SYMBOL_GPL(cx231xx_dev_init);
1414 void cx231xx_dev_uninit(struct cx231xx *dev)
1416 /* Un Initialize I2C bus */
1417 cx231xx_i2c_unregister(&dev->i2c_bus[2]);
1418 cx231xx_i2c_unregister(&dev->i2c_bus[1]);
1419 cx231xx_i2c_unregister(&dev->i2c_bus[0]);
1421 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
1423 /*****************************************************************
1424 * G P I O related functions *
1425 ******************************************************************/
1426 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val,
1427 u8 len, u8 request, u8 direction)
1429 int status = 0;
1430 struct VENDOR_REQUEST_IN ven_req;
1432 /* Set wValue */
1433 ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
1435 /* set request */
1436 if (!request) {
1437 if (direction)
1438 ven_req.bRequest = VRT_GET_GPIO; /* 0x8 gpio */
1439 else
1440 ven_req.bRequest = VRT_SET_GPIO; /* 0x9 gpio */
1441 } else {
1442 if (direction)
1443 ven_req.bRequest = VRT_GET_GPIE; /* 0xa gpie */
1444 else
1445 ven_req.bRequest = VRT_SET_GPIE; /* 0xb gpie */
1448 /* set index value */
1449 ven_req.wIndex = (u16) (gpio_bit & 0xffff);
1451 /* set wLength value */
1452 ven_req.wLength = len;
1454 /* set bData value */
1455 ven_req.bData = 0;
1457 /* set the buffer for read / write */
1458 ven_req.pBuff = gpio_val;
1460 /* set the direction */
1461 if (direction) {
1462 ven_req.direction = USB_DIR_IN;
1463 memset(ven_req.pBuff, 0x00, ven_req.wLength);
1464 } else
1465 ven_req.direction = USB_DIR_OUT;
1468 /* call common vendor command request */
1469 status = cx231xx_send_vendor_cmd(dev, &ven_req);
1470 if (status < 0) {
1471 cx231xx_info
1472 ("UsbInterface::sendCommand, failed with status -%d\n",
1473 status);
1476 return status;
1478 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
1480 /*****************************************************************
1481 * C O N T R O L - Register R E A D / W R I T E functions *
1482 *****************************************************************/
1483 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
1485 u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
1486 u32 tmp = 0;
1487 int status = 0;
1489 status =
1490 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
1491 if (status < 0)
1492 return status;
1494 tmp = *((u32 *) value);
1495 tmp |= mode;
1497 value[0] = (u8) tmp;
1498 value[1] = (u8) (tmp >> 8);
1499 value[2] = (u8) (tmp >> 16);
1500 value[3] = (u8) (tmp >> 24);
1502 status =
1503 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1505 return status;
1508 /*****************************************************************
1509 * I 2 C Internal C O N T R O L functions *
1510 *****************************************************************/
1511 int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1512 u8 saddr_len, u32 *data, u8 data_len, int master)
1514 int status = 0;
1515 struct cx231xx_i2c_xfer_data req_data;
1516 u8 value[64] = "0";
1518 if (saddr_len == 0)
1519 saddr = 0;
1520 else if (saddr_len == 1)
1521 saddr &= 0xff;
1523 /* prepare xfer_data struct */
1524 req_data.dev_addr = dev_addr >> 1;
1525 req_data.direction = I2C_M_RD;
1526 req_data.saddr_len = saddr_len;
1527 req_data.saddr_dat = saddr;
1528 req_data.buf_size = data_len;
1529 req_data.p_buffer = (u8 *) value;
1531 /* usb send command */
1532 if (master == 0)
1533 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
1534 &req_data);
1535 else if (master == 1)
1536 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
1537 &req_data);
1538 else if (master == 2)
1539 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
1540 &req_data);
1542 if (status >= 0) {
1543 /* Copy the data read back to main buffer */
1544 if (data_len == 1)
1545 *data = value[0];
1546 else if (data_len == 4)
1547 *data =
1548 value[0] | value[1] << 8 | value[2] << 16 | value[3]
1549 << 24;
1550 else if (data_len > 4)
1551 *data = value[saddr];
1554 return status;
1557 int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1558 u8 saddr_len, u32 data, u8 data_len, int master)
1560 int status = 0;
1561 u8 value[4] = { 0, 0, 0, 0 };
1562 struct cx231xx_i2c_xfer_data req_data;
1564 value[0] = (u8) data;
1565 value[1] = (u8) (data >> 8);
1566 value[2] = (u8) (data >> 16);
1567 value[3] = (u8) (data >> 24);
1569 if (saddr_len == 0)
1570 saddr = 0;
1571 else if (saddr_len == 1)
1572 saddr &= 0xff;
1574 /* prepare xfer_data struct */
1575 req_data.dev_addr = dev_addr >> 1;
1576 req_data.direction = 0;
1577 req_data.saddr_len = saddr_len;
1578 req_data.saddr_dat = saddr;
1579 req_data.buf_size = data_len;
1580 req_data.p_buffer = value;
1582 /* usb send command */
1583 if (master == 0)
1584 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
1585 &req_data);
1586 else if (master == 1)
1587 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
1588 &req_data);
1589 else if (master == 2)
1590 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
1591 &req_data);
1593 return status;
1596 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1597 u8 saddr_len, u32 *data, u8 data_len)
1599 int status = 0;
1600 struct cx231xx_i2c_xfer_data req_data;
1601 u8 value[4] = { 0, 0, 0, 0 };
1603 if (saddr_len == 0)
1604 saddr = 0;
1605 else if (saddr_len == 1)
1606 saddr &= 0xff;
1608 /* prepare xfer_data struct */
1609 req_data.dev_addr = dev_addr >> 1;
1610 req_data.direction = I2C_M_RD;
1611 req_data.saddr_len = saddr_len;
1612 req_data.saddr_dat = saddr;
1613 req_data.buf_size = data_len;
1614 req_data.p_buffer = (u8 *) value;
1616 /* usb send command */
1617 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1619 if (status >= 0) {
1620 /* Copy the data read back to main buffer */
1621 if (data_len == 1)
1622 *data = value[0];
1623 else
1624 *data =
1625 value[0] | value[1] << 8 | value[2] << 16 | value[3]
1626 << 24;
1629 return status;
1632 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1633 u8 saddr_len, u32 data, u8 data_len)
1635 int status = 0;
1636 u8 value[4] = { 0, 0, 0, 0 };
1637 struct cx231xx_i2c_xfer_data req_data;
1639 value[0] = (u8) data;
1640 value[1] = (u8) (data >> 8);
1641 value[2] = (u8) (data >> 16);
1642 value[3] = (u8) (data >> 24);
1644 if (saddr_len == 0)
1645 saddr = 0;
1646 else if (saddr_len == 1)
1647 saddr &= 0xff;
1649 /* prepare xfer_data struct */
1650 req_data.dev_addr = dev_addr >> 1;
1651 req_data.direction = 0;
1652 req_data.saddr_len = saddr_len;
1653 req_data.saddr_dat = saddr;
1654 req_data.buf_size = data_len;
1655 req_data.p_buffer = value;
1657 /* usb send command */
1658 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1660 return status;
1663 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
1664 u16 register_address, u8 bit_start, u8 bit_end,
1665 u32 value)
1667 int status = 0;
1668 u32 tmp;
1669 u32 mask = 0;
1670 int i;
1672 if (bit_start > (size - 1) || bit_end > (size - 1))
1673 return -1;
1675 if (size == 8) {
1676 status =
1677 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1678 &tmp, 1);
1679 } else {
1680 status =
1681 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1682 &tmp, 4);
1685 if (status < 0)
1686 return status;
1688 mask = 1 << bit_end;
1689 for (i = bit_end; i > bit_start && i > 0; i--)
1690 mask = mask + (1 << (i - 1));
1692 value <<= bit_start;
1694 if (size == 8) {
1695 tmp &= ~mask;
1696 tmp |= value;
1697 tmp &= 0xff;
1698 status =
1699 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1700 tmp, 1);
1701 } else {
1702 tmp &= ~mask;
1703 tmp |= value;
1704 status =
1705 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1706 tmp, 4);
1709 return status;
1712 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1713 u16 saddr, u32 mask, u32 value)
1715 u32 temp;
1716 int status = 0;
1718 status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1720 if (status < 0)
1721 return status;
1723 temp &= ~mask;
1724 temp |= value;
1726 status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1728 return status;
1731 u32 cx231xx_set_field(u32 field_mask, u32 data)
1733 u32 temp;
1735 for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
1736 data <<= 1;
1738 return data;