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
);
70 * cx231xx_realease_resources()
71 * unregisters the v4l2,i2c and usb devices
72 * called when the device gets disconected or at module unload
74 void cx231xx_remove_from_devlist(struct cx231xx
*dev
)
76 mutex_lock(&cx231xx_devlist_mutex
);
77 list_del(&dev
->devlist
);
78 mutex_unlock(&cx231xx_devlist_mutex
);
81 void cx231xx_add_into_devlist(struct cx231xx
*dev
)
83 mutex_lock(&cx231xx_devlist_mutex
);
84 list_add_tail(&dev
->devlist
, &cx231xx_devlist
);
85 mutex_unlock(&cx231xx_devlist_mutex
);
88 static LIST_HEAD(cx231xx_extension_devlist
);
89 static DEFINE_MUTEX(cx231xx_extension_devlist_lock
);
91 int cx231xx_register_extension(struct cx231xx_ops
*ops
)
93 struct cx231xx
*dev
= NULL
;
95 mutex_lock(&cx231xx_devlist_mutex
);
96 mutex_lock(&cx231xx_extension_devlist_lock
);
97 list_add_tail(&ops
->next
, &cx231xx_extension_devlist
);
98 list_for_each_entry(dev
, &cx231xx_devlist
, devlist
) {
102 printk(KERN_INFO DRIVER_NAME
": %s initialized\n", ops
->name
);
103 mutex_unlock(&cx231xx_extension_devlist_lock
);
104 mutex_unlock(&cx231xx_devlist_mutex
);
107 EXPORT_SYMBOL(cx231xx_register_extension
);
109 void cx231xx_unregister_extension(struct cx231xx_ops
*ops
)
111 struct cx231xx
*dev
= NULL
;
113 mutex_lock(&cx231xx_devlist_mutex
);
114 list_for_each_entry(dev
, &cx231xx_devlist
, devlist
) {
119 mutex_lock(&cx231xx_extension_devlist_lock
);
120 printk(KERN_INFO DRIVER_NAME
": %s removed\n", ops
->name
);
121 list_del(&ops
->next
);
122 mutex_unlock(&cx231xx_extension_devlist_lock
);
123 mutex_unlock(&cx231xx_devlist_mutex
);
125 EXPORT_SYMBOL(cx231xx_unregister_extension
);
127 void cx231xx_init_extension(struct cx231xx
*dev
)
129 struct cx231xx_ops
*ops
= NULL
;
131 mutex_lock(&cx231xx_extension_devlist_lock
);
132 if (!list_empty(&cx231xx_extension_devlist
)) {
133 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
138 mutex_unlock(&cx231xx_extension_devlist_lock
);
141 void cx231xx_close_extension(struct cx231xx
*dev
)
143 struct cx231xx_ops
*ops
= NULL
;
145 mutex_lock(&cx231xx_extension_devlist_lock
);
146 if (!list_empty(&cx231xx_extension_devlist
)) {
147 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
152 mutex_unlock(&cx231xx_extension_devlist_lock
);
155 /****************************************************************
156 * U S B related functions *
157 *****************************************************************/
158 int cx231xx_send_usb_command(struct cx231xx_i2c
*i2c_bus
,
159 struct cx231xx_i2c_xfer_data
*req_data
)
162 struct cx231xx
*dev
= i2c_bus
->dev
;
163 struct VENDOR_REQUEST_IN ven_req
;
170 /* Get the I2C period, nostop and reserve parameters */
171 _i2c_period
= i2c_bus
->i2c_period
;
172 _i2c_nostop
= i2c_bus
->i2c_nostop
;
173 _i2c_reserve
= i2c_bus
->i2c_reserve
;
175 saddr_len
= req_data
->saddr_len
;
178 if (saddr_len
== 1) /* need check saddr_len == 0 */
181 dev_addr
<< 9 | _i2c_period
<< 4 | saddr_len
<< 2 |
182 _i2c_nostop
<< 1 | I2C_SYNC
| _i2c_reserve
<< 6;
186 dev_addr
<< 9 | _i2c_period
<< 4 | saddr_len
<< 2 |
187 _i2c_nostop
<< 1 | I2C_SYNC
| _i2c_reserve
<< 6;
189 /* set channel number */
190 if (req_data
->direction
& I2C_M_RD
) {
191 /* channel number, for read,spec required channel_num +4 */
192 ven_req
.bRequest
= i2c_bus
->nr
+ 4;
194 ven_req
.bRequest
= i2c_bus
->nr
; /* channel number, */
196 /* set index value */
199 ven_req
.wIndex
= 0; /* need check */
202 ven_req
.wIndex
= (req_data
->saddr_dat
& 0xff);
205 ven_req
.wIndex
= req_data
->saddr_dat
;
209 /* set wLength value */
210 ven_req
.wLength
= req_data
->buf_size
;
212 /* set bData value */
215 /* set the direction */
216 if (req_data
->direction
) {
217 ven_req
.direction
= USB_DIR_IN
;
218 memset(req_data
->p_buffer
, 0x00, ven_req
.wLength
);
220 ven_req
.direction
= USB_DIR_OUT
;
222 /* set the buffer for read / write */
223 ven_req
.pBuff
= req_data
->p_buffer
;
226 /* call common vendor command request */
227 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
230 ("UsbInterface::sendCommand, failed with status -%d\n",
236 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command
);
239 * cx231xx_read_ctrl_reg()
240 * reads data from the usb device specifying bRequest and wValue
242 int cx231xx_read_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
,
247 int pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
249 if (dev
->state
& DEV_DISCONNECTED
)
252 if (len
> URB_MAX_CTRL_SIZE
)
257 val
= ENABLE_ONE_BYTE
;
260 val
= ENABLE_TWE_BYTE
;
263 val
= ENABLE_THREE_BYTE
;
266 val
= ENABLE_FOUR_BYTE
;
269 val
= 0xFF; /* invalid option */
276 cx231xx_isocdbg("(pipe 0x%08x): "
277 "IN: %02x %02x %02x %02x %02x %02x %02x %02x ",
279 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
281 reg
& 0xff, reg
>> 8, len
& 0xff, len
>> 8);
284 mutex_lock(&dev
->ctrl_urb_lock
);
285 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
286 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
287 val
, reg
, dev
->urb_buf
, len
, HZ
);
289 cx231xx_isocdbg(" failed!\n");
290 /* mutex_unlock(&dev->ctrl_urb_lock); */
295 memcpy(buf
, dev
->urb_buf
, len
);
297 mutex_unlock(&dev
->ctrl_urb_lock
);
302 cx231xx_isocdbg("<<<");
303 for (byte
= 0; byte
< len
; byte
++)
304 cx231xx_isocdbg(" %02x", (unsigned char)buf
[byte
]);
305 cx231xx_isocdbg("\n");
311 int cx231xx_send_vendor_cmd(struct cx231xx
*dev
,
312 struct VENDOR_REQUEST_IN
*ven_req
)
317 if (dev
->state
& DEV_DISCONNECTED
)
320 if ((ven_req
->wLength
> URB_MAX_CTRL_SIZE
))
323 if (ven_req
->direction
)
324 pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
326 pipe
= usb_sndctrlpipe(dev
->udev
, 0);
331 cx231xx_isocdbg("(pipe 0x%08x): "
332 "OUT: %02x %02x %02x %04x %04x %04x >>>",
335 direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
336 ven_req
->bRequest
, 0, ven_req
->wValue
,
337 ven_req
->wIndex
, ven_req
->wLength
);
339 for (byte
= 0; byte
< ven_req
->wLength
; byte
++)
340 cx231xx_isocdbg(" %02x",
341 (unsigned char)ven_req
->pBuff
[byte
]);
342 cx231xx_isocdbg("\n");
345 mutex_lock(&dev
->ctrl_urb_lock
);
346 ret
= usb_control_msg(dev
->udev
, pipe
, ven_req
->bRequest
,
348 direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
349 ven_req
->wValue
, ven_req
->wIndex
, ven_req
->pBuff
,
350 ven_req
->wLength
, HZ
);
351 mutex_unlock(&dev
->ctrl_urb_lock
);
357 * cx231xx_write_ctrl_reg()
358 * sends data to the usb device, specifying bRequest
360 int cx231xx_write_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
, char *buf
,
365 int pipe
= usb_sndctrlpipe(dev
->udev
, 0);
367 if (dev
->state
& DEV_DISCONNECTED
)
370 if ((len
< 1) || (len
> URB_MAX_CTRL_SIZE
))
375 val
= ENABLE_ONE_BYTE
;
378 val
= ENABLE_TWE_BYTE
;
381 val
= ENABLE_THREE_BYTE
;
384 val
= ENABLE_FOUR_BYTE
;
387 val
= 0xFF; /* invalid option */
396 cx231xx_isocdbg("(pipe 0x%08x): "
397 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
399 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
400 req
, 0, val
, reg
& 0xff,
401 reg
>> 8, len
& 0xff, len
>> 8);
403 for (byte
= 0; byte
< len
; byte
++)
404 cx231xx_isocdbg(" %02x", (unsigned char)buf
[byte
]);
405 cx231xx_isocdbg("\n");
408 mutex_lock(&dev
->ctrl_urb_lock
);
409 memcpy(dev
->urb_buf
, buf
, len
);
410 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
411 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
412 val
, reg
, dev
->urb_buf
, len
, HZ
);
413 mutex_unlock(&dev
->ctrl_urb_lock
);
418 /****************************************************************
419 * USB Alternate Setting functions *
420 *****************************************************************/
422 int cx231xx_set_video_alternate(struct cx231xx
*dev
)
424 int errCode
, prev_alt
= dev
->video_mode
.alt
;
425 unsigned int min_pkt_size
= dev
->width
* 2 + 4;
426 u32 usb_interface_index
= 0;
428 /* When image size is bigger than a certain value,
429 the frame size should be increased, otherwise, only
430 green screen will be received.
432 if (dev
->width
* 2 * dev
->height
> 720 * 240 * 2)
435 if (dev
->width
> 360) {
436 /* resolutions: 720,704,640 */
437 dev
->video_mode
.alt
= 3;
438 } else if (dev
->width
> 180) {
439 /* resolutions: 360,352,320,240 */
440 dev
->video_mode
.alt
= 2;
441 } else if (dev
->width
> 0) {
442 /* resolutions: 180,176,160,128,88 */
443 dev
->video_mode
.alt
= 1;
445 /* Change to alt0 BULK to release USB bandwidth */
446 dev
->video_mode
.alt
= 0;
449 /* Get the correct video interface Index */
450 usb_interface_index
=
451 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
454 if (dev
->video_mode
.alt
!= prev_alt
) {
455 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
456 min_pkt_size
, dev
->video_mode
.alt
);
457 dev
->video_mode
.max_pkt_size
=
458 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.alt
];
459 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
461 dev
->video_mode
.max_pkt_size
);
463 (" setting alt %d with wMaxPktSize=%u , Interface = %d\n",
464 dev
->video_mode
.alt
, dev
->video_mode
.max_pkt_size
,
465 usb_interface_index
);
467 usb_set_interface(dev
->udev
, usb_interface_index
,
468 dev
->video_mode
.alt
);
471 ("cannot change alt number to %d (error=%i)\n",
472 dev
->video_mode
.alt
, errCode
);
479 int cx231xx_set_alt_setting(struct cx231xx
*dev
, u8 index
, u8 alt
)
482 u32 usb_interface_index
= 0;
483 u32 max_pkt_size
= 0;
487 usb_interface_index
=
488 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
490 dev
->video_mode
.alt
= alt
;
491 if (dev
->ts1_mode
.alt_max_pkt_size
!= NULL
)
492 max_pkt_size
= dev
->ts1_mode
.max_pkt_size
=
493 dev
->ts1_mode
.alt_max_pkt_size
[dev
->ts1_mode
.alt
];
496 usb_interface_index
=
497 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
501 usb_interface_index
=
502 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
505 if (dev
->adev
.alt_max_pkt_size
!= NULL
)
506 max_pkt_size
= dev
->adev
.max_pkt_size
=
507 dev
->adev
.alt_max_pkt_size
[dev
->adev
.alt
];
510 usb_interface_index
=
511 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
513 dev
->video_mode
.alt
= alt
;
514 if (dev
->video_mode
.alt_max_pkt_size
!= NULL
)
515 max_pkt_size
= dev
->video_mode
.max_pkt_size
=
516 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.
520 usb_interface_index
=
521 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
523 dev
->vbi_mode
.alt
= alt
;
524 if (dev
->vbi_mode
.alt_max_pkt_size
!= NULL
)
525 max_pkt_size
= dev
->vbi_mode
.max_pkt_size
=
526 dev
->vbi_mode
.alt_max_pkt_size
[dev
->vbi_mode
.alt
];
529 usb_interface_index
=
530 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
532 dev
->sliced_cc_mode
.alt
= alt
;
533 if (dev
->sliced_cc_mode
.alt_max_pkt_size
!= NULL
)
534 max_pkt_size
= dev
->sliced_cc_mode
.max_pkt_size
=
535 dev
->sliced_cc_mode
.alt_max_pkt_size
[dev
->
543 if (alt
> 0 && max_pkt_size
== 0) {
545 ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
546 usb_interface_index
, alt
);
551 (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n",
552 alt
, max_pkt_size
, usb_interface_index
);
554 if (usb_interface_index
> 0) {
555 status
= usb_set_interface(dev
->udev
, usb_interface_index
, alt
);
558 ("can't change interface %d alt no. to %d (err=%i)\n",
559 usb_interface_index
, alt
, status
);
566 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting
);
568 int cx231xx_gpio_set(struct cx231xx
*dev
, struct cx231xx_reg_seq
*gpio
)
575 /* Send GPIO reset sequences specified at board entry */
576 while (gpio
->sleep
>= 0) {
577 rc
= cx231xx_set_gpio_value(dev
, gpio
->bit
, gpio
->val
);
589 int cx231xx_set_mode(struct cx231xx
*dev
, enum cx231xx_mode set_mode
)
591 if (dev
->mode
== set_mode
)
594 if (set_mode
== CX231XX_SUSPEND
) {
595 /* Set the chip in power saving mode */
596 dev
->mode
= set_mode
;
599 /* Resource is locked */
600 if (dev
->mode
!= CX231XX_SUSPEND
)
603 dev
->mode
= set_mode
;
605 if (dev
->mode
== CX231XX_DIGITAL_MODE
)
606 ;/* Set Digital power mode */
608 ;/* Set Analog Power mode */
612 EXPORT_SYMBOL_GPL(cx231xx_set_mode
);
614 /*****************************************************************
615 * URB Streaming functions *
616 ******************************************************************/
619 * IRQ callback, called by URB callback
621 static void cx231xx_irq_callback(struct urb
*urb
)
623 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
624 struct cx231xx_video_mode
*vmode
=
625 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
626 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, video_mode
);
629 switch (urb
->status
) {
630 case 0: /* success */
631 case -ETIMEDOUT
: /* NAK */
633 case -ECONNRESET
: /* kill */
638 cx231xx_isocdbg("urb completition error %d.\n", urb
->status
);
642 /* Copy data from URB */
643 spin_lock(&dev
->video_mode
.slock
);
644 rc
= dev
->video_mode
.isoc_ctl
.isoc_copy(dev
, urb
);
645 spin_unlock(&dev
->video_mode
.slock
);
647 /* Reset urb buffers */
648 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
649 urb
->iso_frame_desc
[i
].status
= 0;
650 urb
->iso_frame_desc
[i
].actual_length
= 0;
654 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
656 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
662 * Stop and Deallocate URBs
664 void cx231xx_uninit_isoc(struct cx231xx
*dev
)
669 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
671 dev
->video_mode
.isoc_ctl
.nfields
= -1;
672 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
673 urb
= dev
->video_mode
.isoc_ctl
.urb
[i
];
675 if (!irqs_disabled())
680 if (dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
681 usb_buffer_free(dev
->udev
,
682 urb
->transfer_buffer_length
,
683 dev
->video_mode
.isoc_ctl
.
688 dev
->video_mode
.isoc_ctl
.urb
[i
] = NULL
;
690 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] = NULL
;
693 kfree(dev
->video_mode
.isoc_ctl
.urb
);
694 kfree(dev
->video_mode
.isoc_ctl
.transfer_buffer
);
696 dev
->video_mode
.isoc_ctl
.urb
= NULL
;
697 dev
->video_mode
.isoc_ctl
.transfer_buffer
= NULL
;
698 dev
->video_mode
.isoc_ctl
.num_bufs
= 0;
700 cx231xx_capture_start(dev
, 0, Raw_Video
);
702 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc
);
705 * Allocate URBs and start IRQ
707 int cx231xx_init_isoc(struct cx231xx
*dev
, int max_packets
,
708 int num_bufs
, int max_pkt_size
,
709 int (*isoc_copy
) (struct cx231xx
*dev
, struct urb
*urb
))
711 struct cx231xx_dmaqueue
*dma_q
= &dev
->video_mode
.vidq
;
718 cx231xx_isocdbg("cx231xx: called cx231xx_prepare_isoc\n");
720 dev
->video_input
= dev
->video_input
> 2 ? 2 : dev
->video_input
;
722 cx231xx_info("Setting Video mux to %d\n", dev
->video_input
);
723 video_mux(dev
, dev
->video_input
);
725 /* De-allocates all pending stuff */
726 cx231xx_uninit_isoc(dev
);
728 dev
->video_mode
.isoc_ctl
.isoc_copy
= isoc_copy
;
729 dev
->video_mode
.isoc_ctl
.num_bufs
= num_bufs
;
731 dma_q
->is_partial_line
= 0;
733 dma_q
->current_field
= -1;
734 dma_q
->field1_done
= 0;
735 dma_q
->lines_per_field
= dev
->height
/ 2;
736 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
737 dma_q
->lines_completed
= 0;
738 for (i
= 0; i
< 8; i
++)
739 dma_q
->partial_buf
[i
] = 0;
741 dev
->video_mode
.isoc_ctl
.urb
=
742 kzalloc(sizeof(void *) * num_bufs
, GFP_KERNEL
);
743 if (!dev
->video_mode
.isoc_ctl
.urb
) {
744 cx231xx_errdev("cannot alloc memory for usb buffers\n");
748 dev
->video_mode
.isoc_ctl
.transfer_buffer
=
749 kzalloc(sizeof(void *) * num_bufs
, GFP_KERNEL
);
750 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
) {
751 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
752 kfree(dev
->video_mode
.isoc_ctl
.urb
);
756 dev
->video_mode
.isoc_ctl
.max_pkt_size
= max_pkt_size
;
757 dev
->video_mode
.isoc_ctl
.buf
= NULL
;
759 sb_size
= max_packets
* dev
->video_mode
.isoc_ctl
.max_pkt_size
;
761 /* allocate urbs and transfer buffers */
762 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
763 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
765 cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i
);
766 cx231xx_uninit_isoc(dev
);
769 dev
->video_mode
.isoc_ctl
.urb
[i
] = urb
;
771 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] =
772 usb_buffer_alloc(dev
->udev
, sb_size
, GFP_KERNEL
,
774 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
775 cx231xx_err("unable to allocate %i bytes for transfer"
778 in_interrupt() ? " while in int" : "");
779 cx231xx_uninit_isoc(dev
);
782 memset(dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
785 usb_rcvisocpipe(dev
->udev
, dev
->video_mode
.end_point_addr
);
787 usb_fill_int_urb(urb
, dev
->udev
, pipe
,
788 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
],
789 sb_size
, cx231xx_irq_callback
, dma_q
, 1);
791 urb
->number_of_packets
= max_packets
;
792 urb
->transfer_flags
= URB_ISO_ASAP
;
795 for (j
= 0; j
< max_packets
; j
++) {
796 urb
->iso_frame_desc
[j
].offset
= k
;
797 urb
->iso_frame_desc
[j
].length
=
798 dev
->video_mode
.isoc_ctl
.max_pkt_size
;
799 k
+= dev
->video_mode
.isoc_ctl
.max_pkt_size
;
803 init_waitqueue_head(&dma_q
->wq
);
805 /* submit urbs and enables IRQ */
806 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
807 rc
= usb_submit_urb(dev
->video_mode
.isoc_ctl
.urb
[i
],
810 cx231xx_err("submit of urb %i failed (error=%i)\n", i
,
812 cx231xx_uninit_isoc(dev
);
817 cx231xx_capture_start(dev
, 1, Raw_Video
);
821 EXPORT_SYMBOL_GPL(cx231xx_init_isoc
);
823 /*****************************************************************
824 * Device Init/UnInit functions *
825 ******************************************************************/
826 int cx231xx_dev_init(struct cx231xx
*dev
)
830 /* Initialize I2C bus */
832 /* External Master 1 Bus */
833 dev
->i2c_bus
[0].nr
= 0;
834 dev
->i2c_bus
[0].dev
= dev
;
835 dev
->i2c_bus
[0].i2c_period
= I2C_SPEED_1M
; /* 1MHz */
836 dev
->i2c_bus
[0].i2c_nostop
= 0;
837 dev
->i2c_bus
[0].i2c_reserve
= 0;
839 /* External Master 2 Bus */
840 dev
->i2c_bus
[1].nr
= 1;
841 dev
->i2c_bus
[1].dev
= dev
;
842 dev
->i2c_bus
[1].i2c_period
= I2C_SPEED_1M
; /* 1MHz */
843 dev
->i2c_bus
[1].i2c_nostop
= 0;
844 dev
->i2c_bus
[1].i2c_reserve
= 0;
846 /* Internal Master 3 Bus */
847 dev
->i2c_bus
[2].nr
= 2;
848 dev
->i2c_bus
[2].dev
= dev
;
849 dev
->i2c_bus
[2].i2c_period
= I2C_SPEED_400K
; /* 400kHz */
850 dev
->i2c_bus
[2].i2c_nostop
= 0;
851 dev
->i2c_bus
[2].i2c_reserve
= 0;
853 /* register I2C buses */
854 cx231xx_i2c_register(&dev
->i2c_bus
[0]);
855 cx231xx_i2c_register(&dev
->i2c_bus
[1]);
856 cx231xx_i2c_register(&dev
->i2c_bus
[2]);
859 /* Note : with out calling set power mode function,
860 afe can not be set up correctly */
861 errCode
= cx231xx_set_power_mode(dev
, POLARIS_AVMODE_ANALOGT_TV
);
864 ("%s: Failed to set Power - errCode [%d]!\n",
869 /* initialize Colibri block */
870 errCode
= cx231xx_afe_init_super_block(dev
, 0x23c);
873 ("%s: cx231xx_afe init super block - errCode [%d]!\n",
877 errCode
= cx231xx_afe_init_channels(dev
);
880 ("%s: cx231xx_afe init channels - errCode [%d]!\n",
885 /* Set DIF in By pass mode */
886 errCode
= cx231xx_dif_set_standard(dev
, DIF_USE_BASEBAND
);
889 ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
894 /* I2S block related functions */
895 errCode
= cx231xx_i2s_blk_initialize(dev
);
898 ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
903 /* init control pins */
904 errCode
= cx231xx_init_ctrl_pin_status(dev
);
906 cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
911 /* set AGC mode to Analog */
912 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 1);
915 ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
920 /* set all alternate settings to zero initially */
921 cx231xx_set_alt_setting(dev
, INDEX_VIDEO
, 0);
922 cx231xx_set_alt_setting(dev
, INDEX_VANC
, 0);
923 cx231xx_set_alt_setting(dev
, INDEX_HANC
, 0);
924 if (dev
->board
.has_dvb
)
925 cx231xx_set_alt_setting(dev
, INDEX_TS1
, 0);
927 /* set the I2C master port to 3 on channel 1 */
928 errCode
= cx231xx_enable_i2c_for_tuner(dev
, I2C_3
);
932 EXPORT_SYMBOL_GPL(cx231xx_dev_init
);
934 void cx231xx_dev_uninit(struct cx231xx
*dev
)
936 /* Un Initialize I2C bus */
937 cx231xx_i2c_unregister(&dev
->i2c_bus
[2]);
938 cx231xx_i2c_unregister(&dev
->i2c_bus
[1]);
939 cx231xx_i2c_unregister(&dev
->i2c_bus
[0]);
941 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit
);
943 /*****************************************************************
944 * G P I O related functions *
945 ******************************************************************/
946 int cx231xx_send_gpio_cmd(struct cx231xx
*dev
, u32 gpio_bit
, u8
* gpio_val
,
947 u8 len
, u8 request
, u8 direction
)
950 struct VENDOR_REQUEST_IN ven_req
;
953 ven_req
.wValue
= (u16
) (gpio_bit
>> 16 & 0xffff);
958 ven_req
.bRequest
= VRT_GET_GPIO
; /* 0x8 gpio */
960 ven_req
.bRequest
= VRT_SET_GPIO
; /* 0x9 gpio */
963 ven_req
.bRequest
= VRT_GET_GPIE
; /* 0xa gpie */
965 ven_req
.bRequest
= VRT_SET_GPIE
; /* 0xb gpie */
968 /* set index value */
969 ven_req
.wIndex
= (u16
) (gpio_bit
& 0xffff);
971 /* set wLength value */
972 ven_req
.wLength
= len
;
974 /* set bData value */
977 /* set the buffer for read / write */
978 ven_req
.pBuff
= gpio_val
;
980 /* set the direction */
982 ven_req
.direction
= USB_DIR_IN
;
983 memset(ven_req
.pBuff
, 0x00, ven_req
.wLength
);
985 ven_req
.direction
= USB_DIR_OUT
;
988 /* call common vendor command request */
989 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
992 ("UsbInterface::sendCommand, failed with status -%d\n",
998 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd
);
1000 /*****************************************************************
1001 * C O N T R O L - Register R E A D / W R I T E functions *
1002 *****************************************************************/
1003 int cx231xx_mode_register(struct cx231xx
*dev
, u16 address
, u32 mode
)
1005 u8 value
[4] = { 0x0, 0x0, 0x0, 0x0 };
1010 cx231xx_read_ctrl_reg(dev
, VRT_GET_REGISTER
, address
, value
, 4);
1014 tmp
= *((u32
*) value
);
1017 value
[0] = (u8
) tmp
;
1018 value
[1] = (u8
) (tmp
>> 8);
1019 value
[2] = (u8
) (tmp
>> 16);
1020 value
[3] = (u8
) (tmp
>> 24);
1023 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
, address
, value
, 4);
1028 /*****************************************************************
1029 * I 2 C Internal C O N T R O L functions *
1030 *****************************************************************/
1031 int cx231xx_read_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1032 u8 saddr_len
, u32
*data
, u8 data_len
)
1035 struct cx231xx_i2c_xfer_data req_data
;
1036 u8 value
[4] = { 0, 0, 0, 0 };
1040 else if (saddr_len
== 0)
1043 /* prepare xfer_data struct */
1044 req_data
.dev_addr
= dev_addr
>> 1;
1045 req_data
.direction
= I2C_M_RD
;
1046 req_data
.saddr_len
= saddr_len
;
1047 req_data
.saddr_dat
= saddr
;
1048 req_data
.buf_size
= data_len
;
1049 req_data
.p_buffer
= (u8
*) value
;
1051 /* usb send command */
1052 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1055 /* Copy the data read back to main buffer */
1060 value
[0] | value
[1] << 8 | value
[2] << 16 | value
[3]
1067 int cx231xx_write_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1068 u8 saddr_len
, u32 data
, u8 data_len
)
1071 u8 value
[4] = { 0, 0, 0, 0 };
1072 struct cx231xx_i2c_xfer_data req_data
;
1074 value
[0] = (u8
) data
;
1075 value
[1] = (u8
) (data
>> 8);
1076 value
[2] = (u8
) (data
>> 16);
1077 value
[3] = (u8
) (data
>> 24);
1081 else if (saddr_len
== 0)
1084 /* prepare xfer_data struct */
1085 req_data
.dev_addr
= dev_addr
>> 1;
1086 req_data
.direction
= 0;
1087 req_data
.saddr_len
= saddr_len
;
1088 req_data
.saddr_dat
= saddr
;
1089 req_data
.buf_size
= data_len
;
1090 req_data
.p_buffer
= value
;
1092 /* usb send command */
1093 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1098 int cx231xx_reg_mask_write(struct cx231xx
*dev
, u8 dev_addr
, u8 size
,
1099 u16 register_address
, u8 bit_start
, u8 bit_end
,
1107 if (bit_start
> (size
- 1) || bit_end
> (size
- 1))
1112 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1116 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1123 mask
= 1 << bit_end
;
1124 for (i
= bit_end
; i
> bit_start
&& i
> 0; i
--)
1125 mask
= mask
+ (1 << (i
- 1));
1127 value
<<= bit_start
;
1134 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1140 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1147 int cx231xx_read_modify_write_i2c_dword(struct cx231xx
*dev
, u8 dev_addr
,
1148 u16 saddr
, u32 mask
, u32 value
)
1153 status
= cx231xx_read_i2c_data(dev
, dev_addr
, saddr
, 2, &temp
, 4);
1161 status
= cx231xx_write_i2c_data(dev
, dev_addr
, saddr
, 2, temp
, 4);
1166 u32
cx231xx_set_field(u32 field_mask
, u32 data
)
1170 for (temp
= field_mask
; (temp
& 1) == 0; temp
>>= 1)