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>
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/usb.h>
27 #include <linux/vmalloc.h>
28 #include <media/v4l2-common.h>
31 #include "cx231xx-reg.h"
33 /* #define ENABLE_DEBUG_ISOC_FRAMES */
35 static unsigned int core_debug;
36 module_param(core_debug, int, 0644);
37 MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
39 #define cx231xx_coredbg(fmt, arg...) do {\
41 printk(KERN_INFO "%s %s :"fmt, \
42 dev->name, __func__ , ##arg); } while (0)
44 static unsigned int reg_debug;
45 module_param(reg_debug, int, 0644);
46 MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
48 #define cx231xx_regdbg(fmt, arg...) do {\
50 printk(KERN_INFO "%s %s :"fmt, \
51 dev->name, __func__ , ##arg); } while (0)
53 static int alt = CX231XX_PINOUT;
54 module_param(alt, int, 0644);
55 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
57 #define cx231xx_isocdbg(fmt, arg...) do {\
59 printk(KERN_INFO "%s %s :"fmt, \
60 dev->name, __func__ , ##arg); } while (0)
62 /*****************************************************************
63 * Device control list functions *
64 ******************************************************************/
66 static LIST_HEAD(cx231xx_devlist);
67 static DEFINE_MUTEX(cx231xx_devlist_mutex);
69 struct cx231xx *cx231xx_get_device(int minor,
70 enum v4l2_buf_type *fh_type, int *has_radio)
72 struct cx231xx *h, *dev = NULL;
74 *fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
77 mutex_lock(&cx231xx_devlist_mutex);
78 list_for_each_entry(h, &cx231xx_devlist, devlist) {
79 if (h->vdev->minor == minor)
81 if (h->vbi_dev->minor == minor) {
83 *fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
85 if (h->radio_dev && h->radio_dev->minor == minor) {
90 mutex_unlock(&cx231xx_devlist_mutex);
96 * cx231xx_realease_resources()
97 * unregisters the v4l2,i2c and usb devices
98 * called when the device gets disconected or at module unload
100 void cx231xx_remove_from_devlist(struct cx231xx *dev)
102 mutex_lock(&cx231xx_devlist_mutex);
103 list_del(&dev->devlist);
104 mutex_unlock(&cx231xx_devlist_mutex);
107 void cx231xx_add_into_devlist(struct cx231xx *dev)
109 mutex_lock(&cx231xx_devlist_mutex);
110 list_add_tail(&dev->devlist, &cx231xx_devlist);
111 mutex_unlock(&cx231xx_devlist_mutex);
114 static LIST_HEAD(cx231xx_extension_devlist);
115 static DEFINE_MUTEX(cx231xx_extension_devlist_lock);
117 int cx231xx_register_extension(struct cx231xx_ops *ops)
119 struct cx231xx *dev = NULL;
121 mutex_lock(&cx231xx_devlist_mutex);
122 mutex_lock(&cx231xx_extension_devlist_lock);
123 list_add_tail(&ops->next, &cx231xx_extension_devlist);
124 list_for_each_entry(dev, &cx231xx_devlist, devlist) {
128 printk(KERN_INFO DRIVER_NAME ": %s initialized\n", ops->name);
129 mutex_unlock(&cx231xx_extension_devlist_lock);
130 mutex_unlock(&cx231xx_devlist_mutex);
133 EXPORT_SYMBOL(cx231xx_register_extension);
135 void cx231xx_unregister_extension(struct cx231xx_ops *ops)
137 struct cx231xx *dev = NULL;
139 mutex_lock(&cx231xx_devlist_mutex);
140 list_for_each_entry(dev, &cx231xx_devlist, devlist) {
145 mutex_lock(&cx231xx_extension_devlist_lock);
146 printk(KERN_INFO DRIVER_NAME ": %s removed\n", ops->name);
147 list_del(&ops->next);
148 mutex_unlock(&cx231xx_extension_devlist_lock);
149 mutex_unlock(&cx231xx_devlist_mutex);
151 EXPORT_SYMBOL(cx231xx_unregister_extension);
153 void cx231xx_init_extension(struct cx231xx *dev)
155 struct cx231xx_ops *ops = NULL;
157 mutex_lock(&cx231xx_extension_devlist_lock);
158 if (!list_empty(&cx231xx_extension_devlist)) {
159 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
164 mutex_unlock(&cx231xx_extension_devlist_lock);
167 void cx231xx_close_extension(struct cx231xx *dev)
169 struct cx231xx_ops *ops = NULL;
171 mutex_lock(&cx231xx_extension_devlist_lock);
172 if (!list_empty(&cx231xx_extension_devlist)) {
173 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
178 mutex_unlock(&cx231xx_extension_devlist_lock);
181 /****************************************************************
182 * U S B related functions *
183 *****************************************************************/
184 int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
185 struct cx231xx_i2c_xfer_data *req_data)
188 struct cx231xx *dev = i2c_bus->dev;
189 struct VENDOR_REQUEST_IN ven_req;
196 /* Get the I2C period, nostop and reserve parameters */
197 _i2c_period = i2c_bus->i2c_period;
198 _i2c_nostop = i2c_bus->i2c_nostop;
199 _i2c_reserve = i2c_bus->i2c_reserve;
201 saddr_len = req_data->saddr_len;
204 if (saddr_len == 1) /* need check saddr_len == 0 */
207 dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
208 _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
212 dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
213 _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
215 /* set channel number */
216 if (req_data->direction & I2C_M_RD) {
217 /* channel number, for read,spec required channel_num +4 */
218 ven_req.bRequest = i2c_bus->nr + 4;
220 ven_req.bRequest = i2c_bus->nr; /* channel number, */
222 /* set index value */
225 ven_req.wIndex = 0; /* need check */
228 ven_req.wIndex = (req_data->saddr_dat & 0xff);
231 ven_req.wIndex = req_data->saddr_dat;
235 /* set wLength value */
236 ven_req.wLength = req_data->buf_size;
238 /* set bData value */
241 /* set the direction */
242 if (req_data->direction) {
243 ven_req.direction = USB_DIR_IN;
244 memset(req_data->p_buffer, 0x00, ven_req.wLength);
246 ven_req.direction = USB_DIR_OUT;
248 /* set the buffer for read / write */
249 ven_req.pBuff = req_data->p_buffer;
252 /* call common vendor command request */
253 status = cx231xx_send_vendor_cmd(dev, &ven_req);
256 ("UsbInterface::sendCommand, failed with status -%d\n",
262 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
265 * cx231xx_read_ctrl_reg()
266 * reads data from the usb device specifying bRequest and wValue
268 int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
273 int pipe = usb_rcvctrlpipe(dev->udev, 0);
275 if (dev->state & DEV_DISCONNECTED)
278 if (len > URB_MAX_CTRL_SIZE)
283 val = ENABLE_ONE_BYTE;
286 val = ENABLE_TWE_BYTE;
289 val = ENABLE_THREE_BYTE;
292 val = ENABLE_FOUR_BYTE;
295 val = 0xFF; /* invalid option */
302 cx231xx_isocdbg("(pipe 0x%08x): "
303 "IN: %02x %02x %02x %02x %02x %02x %02x %02x ",
305 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
307 reg & 0xff, reg >> 8, len & 0xff, len >> 8);
310 mutex_lock(&dev->ctrl_urb_lock);
311 ret = usb_control_msg(dev->udev, pipe, req,
312 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
313 val, reg, dev->urb_buf, len, HZ);
315 cx231xx_isocdbg(" failed!\n");
316 /* mutex_unlock(&dev->ctrl_urb_lock); */
321 memcpy(buf, dev->urb_buf, len);
323 mutex_unlock(&dev->ctrl_urb_lock);
328 cx231xx_isocdbg("<<<");
329 for (byte = 0; byte < len; byte++)
330 cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
331 cx231xx_isocdbg("\n");
337 int cx231xx_send_vendor_cmd(struct cx231xx *dev,
338 struct VENDOR_REQUEST_IN *ven_req)
343 if (dev->state & DEV_DISCONNECTED)
346 if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
349 if (ven_req->direction)
350 pipe = usb_rcvctrlpipe(dev->udev, 0);
352 pipe = usb_sndctrlpipe(dev->udev, 0);
357 cx231xx_isocdbg("(pipe 0x%08x): "
358 "OUT: %02x %02x %02x %04x %04x %04x >>>",
361 direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
362 ven_req->bRequest, 0, ven_req->wValue,
363 ven_req->wIndex, ven_req->wLength);
365 for (byte = 0; byte < ven_req->wLength; byte++)
366 cx231xx_isocdbg(" %02x",
367 (unsigned char)ven_req->pBuff[byte]);
368 cx231xx_isocdbg("\n");
371 mutex_lock(&dev->ctrl_urb_lock);
372 ret = usb_control_msg(dev->udev, pipe, ven_req->bRequest,
374 direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
375 ven_req->wValue, ven_req->wIndex, ven_req->pBuff,
376 ven_req->wLength, HZ);
377 mutex_unlock(&dev->ctrl_urb_lock);
383 * cx231xx_write_ctrl_reg()
384 * sends data to the usb device, specifying bRequest
386 int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
391 int pipe = usb_sndctrlpipe(dev->udev, 0);
393 if (dev->state & DEV_DISCONNECTED)
396 if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
401 val = ENABLE_ONE_BYTE;
404 val = ENABLE_TWE_BYTE;
407 val = ENABLE_THREE_BYTE;
410 val = ENABLE_FOUR_BYTE;
413 val = 0xFF; /* invalid option */
422 cx231xx_isocdbg("(pipe 0x%08x): "
423 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
425 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
426 req, 0, val, reg & 0xff,
427 reg >> 8, len & 0xff, len >> 8);
429 for (byte = 0; byte < len; byte++)
430 cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
431 cx231xx_isocdbg("\n");
434 mutex_lock(&dev->ctrl_urb_lock);
435 memcpy(dev->urb_buf, buf, len);
436 ret = usb_control_msg(dev->udev, pipe, req,
437 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
438 val, reg, dev->urb_buf, len, HZ);
439 mutex_unlock(&dev->ctrl_urb_lock);
444 /****************************************************************
445 * USB Alternate Setting functions *
446 *****************************************************************/
448 int cx231xx_set_video_alternate(struct cx231xx *dev)
450 int errCode, prev_alt = dev->video_mode.alt;
451 unsigned int min_pkt_size = dev->width * 2 + 4;
452 u32 usb_interface_index = 0;
454 /* When image size is bigger than a certain value,
455 the frame size should be increased, otherwise, only
456 green screen will be received.
458 if (dev->width * 2 * dev->height > 720 * 240 * 2)
461 if (dev->width > 360) {
462 /* resolutions: 720,704,640 */
463 dev->video_mode.alt = 3;
464 } else if (dev->width > 180) {
465 /* resolutions: 360,352,320,240 */
466 dev->video_mode.alt = 2;
467 } else if (dev->width > 0) {
468 /* resolutions: 180,176,160,128,88 */
469 dev->video_mode.alt = 1;
471 /* Change to alt0 BULK to release USB bandwidth */
472 dev->video_mode.alt = 0;
475 /* Get the correct video interface Index */
476 usb_interface_index =
477 dev->current_pcb_config.hs_config_info[0].interface_info.
480 if (dev->video_mode.alt != prev_alt) {
481 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
482 min_pkt_size, dev->video_mode.alt);
483 dev->video_mode.max_pkt_size =
484 dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
485 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
487 dev->video_mode.max_pkt_size);
489 (" setting alt %d with wMaxPktSize=%u , Interface = %d\n",
490 dev->video_mode.alt, dev->video_mode.max_pkt_size,
491 usb_interface_index);
493 usb_set_interface(dev->udev, usb_interface_index,
494 dev->video_mode.alt);
497 ("cannot change alt number to %d (error=%i)\n",
498 dev->video_mode.alt, errCode);
505 int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
508 u32 usb_interface_index = 0;
509 u32 max_pkt_size = 0;
513 usb_interface_index =
514 dev->current_pcb_config.hs_config_info[0].interface_info.
516 dev->video_mode.alt = alt;
517 if (dev->ts1_mode.alt_max_pkt_size != NULL)
518 max_pkt_size = dev->ts1_mode.max_pkt_size =
519 dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
522 usb_interface_index =
523 dev->current_pcb_config.hs_config_info[0].interface_info.
527 usb_interface_index =
528 dev->current_pcb_config.hs_config_info[0].interface_info.
531 if (dev->adev.alt_max_pkt_size != NULL)
532 max_pkt_size = dev->adev.max_pkt_size =
533 dev->adev.alt_max_pkt_size[dev->adev.alt];
536 usb_interface_index =
537 dev->current_pcb_config.hs_config_info[0].interface_info.
539 dev->video_mode.alt = alt;
540 if (dev->video_mode.alt_max_pkt_size != NULL)
541 max_pkt_size = dev->video_mode.max_pkt_size =
542 dev->video_mode.alt_max_pkt_size[dev->video_mode.
546 usb_interface_index =
547 dev->current_pcb_config.hs_config_info[0].interface_info.
549 dev->vbi_mode.alt = alt;
550 if (dev->vbi_mode.alt_max_pkt_size != NULL)
551 max_pkt_size = dev->vbi_mode.max_pkt_size =
552 dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
555 usb_interface_index =
556 dev->current_pcb_config.hs_config_info[0].interface_info.
558 dev->sliced_cc_mode.alt = alt;
559 if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
560 max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
561 dev->sliced_cc_mode.alt_max_pkt_size[dev->
569 if (alt > 0 && max_pkt_size == 0) {
571 ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
572 usb_interface_index, alt);
577 (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n",
578 alt, max_pkt_size, usb_interface_index);
580 if (usb_interface_index > 0) {
581 status = usb_set_interface(dev->udev, usb_interface_index, alt);
584 ("can't change interface %d alt no. to %d (err=%i)\n",
585 usb_interface_index, alt, status);
592 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
594 int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
601 /* Send GPIO reset sequences specified at board entry */
602 while (gpio->sleep >= 0) {
603 rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
615 int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
617 if (dev->mode == set_mode)
620 if (set_mode == CX231XX_SUSPEND) {
621 /* Set the chip in power saving mode */
622 dev->mode = set_mode;
625 /* Resource is locked */
626 if (dev->mode != CX231XX_SUSPEND)
629 dev->mode = set_mode;
631 if (dev->mode == CX231XX_DIGITAL_MODE)
632 ;/* Set Digital power mode */
634 ;/* Set Analog Power mode */
638 EXPORT_SYMBOL_GPL(cx231xx_set_mode);
640 /*****************************************************************
641 * URB Streaming functions *
642 ******************************************************************/
645 * IRQ callback, called by URB callback
647 static void cx231xx_irq_callback(struct urb *urb)
649 struct cx231xx_dmaqueue *dma_q = urb->context;
650 struct cx231xx_video_mode *vmode =
651 container_of(dma_q, struct cx231xx_video_mode, vidq);
652 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
655 switch (urb->status) {
656 case 0: /* success */
657 case -ETIMEDOUT: /* NAK */
659 case -ECONNRESET: /* kill */
664 cx231xx_isocdbg("urb completition error %d.\n", urb->status);
668 /* Copy data from URB */
669 spin_lock(&dev->video_mode.slock);
670 rc = dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
671 spin_unlock(&dev->video_mode.slock);
673 /* Reset urb buffers */
674 for (i = 0; i < urb->number_of_packets; i++) {
675 urb->iso_frame_desc[i].status = 0;
676 urb->iso_frame_desc[i].actual_length = 0;
680 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
682 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
688 * Stop and Deallocate URBs
690 void cx231xx_uninit_isoc(struct cx231xx *dev)
695 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
697 dev->video_mode.isoc_ctl.nfields = -1;
698 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
699 urb = dev->video_mode.isoc_ctl.urb[i];
701 if (!irqs_disabled())
706 if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
707 usb_buffer_free(dev->udev,
708 urb->transfer_buffer_length,
709 dev->video_mode.isoc_ctl.
714 dev->video_mode.isoc_ctl.urb[i] = NULL;
716 dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
719 kfree(dev->video_mode.isoc_ctl.urb);
720 kfree(dev->video_mode.isoc_ctl.transfer_buffer);
722 dev->video_mode.isoc_ctl.urb = NULL;
723 dev->video_mode.isoc_ctl.transfer_buffer = NULL;
724 dev->video_mode.isoc_ctl.num_bufs = 0;
726 cx231xx_capture_start(dev, 0, Raw_Video);
728 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
731 * Allocate URBs and start IRQ
733 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
734 int num_bufs, int max_pkt_size,
735 int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
737 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
744 cx231xx_isocdbg("cx231xx: called cx231xx_prepare_isoc\n");
746 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
748 cx231xx_info("Setting Video mux to %d\n", dev->video_input);
749 video_mux(dev, dev->video_input);
751 /* De-allocates all pending stuff */
752 cx231xx_uninit_isoc(dev);
754 dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
755 dev->video_mode.isoc_ctl.num_bufs = num_bufs;
757 dma_q->is_partial_line = 0;
759 dma_q->current_field = -1;
760 dma_q->field1_done = 0;
761 dma_q->lines_per_field = dev->height / 2;
762 dma_q->bytes_left_in_line = dev->width << 1;
763 dma_q->lines_completed = 0;
764 for (i = 0; i < 8; i++)
765 dma_q->partial_buf[i] = 0;
767 dev->video_mode.isoc_ctl.urb =
768 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
769 if (!dev->video_mode.isoc_ctl.urb) {
770 cx231xx_errdev("cannot alloc memory for usb buffers\n");
774 dev->video_mode.isoc_ctl.transfer_buffer =
775 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
776 if (!dev->video_mode.isoc_ctl.transfer_buffer) {
777 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
778 kfree(dev->video_mode.isoc_ctl.urb);
782 dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
783 dev->video_mode.isoc_ctl.buf = NULL;
785 sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
787 /* allocate urbs and transfer buffers */
788 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
789 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
791 cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i);
792 cx231xx_uninit_isoc(dev);
795 dev->video_mode.isoc_ctl.urb[i] = urb;
797 dev->video_mode.isoc_ctl.transfer_buffer[i] =
798 usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,
800 if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
801 cx231xx_err("unable to allocate %i bytes for transfer"
804 in_interrupt() ? " while in int" : "");
805 cx231xx_uninit_isoc(dev);
808 memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
811 usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
813 usb_fill_int_urb(urb, dev->udev, pipe,
814 dev->video_mode.isoc_ctl.transfer_buffer[i],
815 sb_size, cx231xx_irq_callback, dma_q, 1);
817 urb->number_of_packets = max_packets;
818 urb->transfer_flags = URB_ISO_ASAP;
821 for (j = 0; j < max_packets; j++) {
822 urb->iso_frame_desc[j].offset = k;
823 urb->iso_frame_desc[j].length =
824 dev->video_mode.isoc_ctl.max_pkt_size;
825 k += dev->video_mode.isoc_ctl.max_pkt_size;
829 init_waitqueue_head(&dma_q->wq);
831 /* submit urbs and enables IRQ */
832 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
833 rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
836 cx231xx_err("submit of urb %i failed (error=%i)\n", i,
838 cx231xx_uninit_isoc(dev);
843 cx231xx_capture_start(dev, 1, Raw_Video);
847 EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
849 /*****************************************************************
850 * Device Init/UnInit functions *
851 ******************************************************************/
852 int cx231xx_dev_init(struct cx231xx *dev)
856 /* Initialize I2C bus */
858 /* External Master 1 Bus */
859 dev->i2c_bus[0].nr = 0;
860 dev->i2c_bus[0].dev = dev;
861 dev->i2c_bus[0].i2c_period = I2C_SPEED_1M; /* 1MHz */
862 dev->i2c_bus[0].i2c_nostop = 0;
863 dev->i2c_bus[0].i2c_reserve = 0;
865 /* External Master 2 Bus */
866 dev->i2c_bus[1].nr = 1;
867 dev->i2c_bus[1].dev = dev;
868 dev->i2c_bus[1].i2c_period = I2C_SPEED_1M; /* 1MHz */
869 dev->i2c_bus[1].i2c_nostop = 0;
870 dev->i2c_bus[1].i2c_reserve = 0;
872 /* Internal Master 3 Bus */
873 dev->i2c_bus[2].nr = 2;
874 dev->i2c_bus[2].dev = dev;
875 dev->i2c_bus[2].i2c_period = I2C_SPEED_400K; /* 400kHz */
876 dev->i2c_bus[2].i2c_nostop = 0;
877 dev->i2c_bus[2].i2c_reserve = 0;
879 /* register I2C buses */
880 cx231xx_i2c_register(&dev->i2c_bus[0]);
881 cx231xx_i2c_register(&dev->i2c_bus[1]);
882 cx231xx_i2c_register(&dev->i2c_bus[2]);
885 /* Note : with out calling set power mode function,
886 afe can not be set up correctly */
887 errCode = cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
890 ("%s: Failed to set Power - errCode [%d]!\n",
895 /* initialize Colibri block */
896 errCode = cx231xx_afe_init_super_block(dev, 0x23c);
899 ("%s: cx231xx_afe init super block - errCode [%d]!\n",
903 errCode = cx231xx_afe_init_channels(dev);
906 ("%s: cx231xx_afe init channels - errCode [%d]!\n",
911 /* Set DIF in By pass mode */
912 errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
915 ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
920 /* I2S block related functions */
921 errCode = cx231xx_i2s_blk_initialize(dev);
924 ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
929 /* init control pins */
930 errCode = cx231xx_init_ctrl_pin_status(dev);
932 cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
937 /* set AGC mode to Analog */
938 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
941 ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
946 /* set all alternate settings to zero initially */
947 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
948 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
949 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
950 if (dev->board.has_dvb)
951 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
953 /* set the I2C master port to 3 on channel 1 */
954 errCode = cx231xx_enable_i2c_for_tuner(dev, I2C_3);
958 EXPORT_SYMBOL_GPL(cx231xx_dev_init);
960 void cx231xx_dev_uninit(struct cx231xx *dev)
962 /* Un Initialize I2C bus */
963 cx231xx_i2c_unregister(&dev->i2c_bus[2]);
964 cx231xx_i2c_unregister(&dev->i2c_bus[1]);
965 cx231xx_i2c_unregister(&dev->i2c_bus[0]);
967 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
969 /*****************************************************************
970 * G P I O related functions *
971 ******************************************************************/
972 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 * gpio_val,
973 u8 len, u8 request, u8 direction)
976 struct VENDOR_REQUEST_IN ven_req;
979 ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
984 ven_req.bRequest = VRT_GET_GPIO; /* 0x8 gpio */
986 ven_req.bRequest = VRT_SET_GPIO; /* 0x9 gpio */
989 ven_req.bRequest = VRT_GET_GPIE; /* 0xa gpie */
991 ven_req.bRequest = VRT_SET_GPIE; /* 0xb gpie */
994 /* set index value */
995 ven_req.wIndex = (u16) (gpio_bit & 0xffff);
997 /* set wLength value */
998 ven_req.wLength = len;
1000 /* set bData value */
1003 /* set the buffer for read / write */
1004 ven_req.pBuff = gpio_val;
1006 /* set the direction */
1008 ven_req.direction = USB_DIR_IN;
1009 memset(ven_req.pBuff, 0x00, ven_req.wLength);
1011 ven_req.direction = USB_DIR_OUT;
1014 /* call common vendor command request */
1015 status = cx231xx_send_vendor_cmd(dev, &ven_req);
1018 ("UsbInterface::sendCommand, failed with status -%d\n",
1024 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
1026 /*****************************************************************
1027 * C O N T R O L - Register R E A D / W R I T E functions *
1028 *****************************************************************/
1029 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
1031 u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
1036 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
1040 tmp = *((u32 *) value);
1043 value[0] = (u8) tmp;
1044 value[1] = (u8) (tmp >> 8);
1045 value[2] = (u8) (tmp >> 16);
1046 value[3] = (u8) (tmp >> 24);
1049 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1054 /*****************************************************************
1055 * I 2 C Internal C O N T R O L functions *
1056 *****************************************************************/
1057 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1058 u8 saddr_len, u32 *data, u8 data_len)
1061 struct cx231xx_i2c_xfer_data req_data;
1062 u8 value[4] = { 0, 0, 0, 0 };
1066 else if (saddr_len == 0)
1069 /* prepare xfer_data struct */
1070 req_data.dev_addr = dev_addr >> 1;
1071 req_data.direction = I2C_M_RD;
1072 req_data.saddr_len = saddr_len;
1073 req_data.saddr_dat = saddr;
1074 req_data.buf_size = data_len;
1075 req_data.p_buffer = (u8 *) value;
1077 /* usb send command */
1078 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1081 /* Copy the data read back to main buffer */
1086 value[0] | value[1] << 8 | value[2] << 16 | value[3]
1093 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1094 u8 saddr_len, u32 data, u8 data_len)
1097 u8 value[4] = { 0, 0, 0, 0 };
1098 struct cx231xx_i2c_xfer_data req_data;
1100 value[0] = (u8) data;
1101 value[1] = (u8) (data >> 8);
1102 value[2] = (u8) (data >> 16);
1103 value[3] = (u8) (data >> 24);
1107 else if (saddr_len == 0)
1110 /* prepare xfer_data struct */
1111 req_data.dev_addr = dev_addr >> 1;
1112 req_data.direction = 0;
1113 req_data.saddr_len = saddr_len;
1114 req_data.saddr_dat = saddr;
1115 req_data.buf_size = data_len;
1116 req_data.p_buffer = value;
1118 /* usb send command */
1119 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1124 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
1125 u16 register_address, u8 bit_start, u8 bit_end,
1133 if (bit_start > (size - 1) || bit_end > (size - 1))
1138 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1142 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1149 mask = 1 << bit_end;
1150 for (i = bit_end; i > bit_start && i > 0; i--)
1151 mask = mask + (1 << (i - 1));
1153 value <<= bit_start;
1160 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1166 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1173 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1174 u16 saddr, u32 mask, u32 value)
1179 status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1187 status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1192 u32 cx231xx_set_field(u32 field_mask, u32 data)
1196 for (temp = field_mask; (temp & 1) == 0; temp >>= 1)