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/slab.h>
27 #include <linux/usb.h>
28 #include <linux/vmalloc.h>
29 #include <media/v4l2-common.h>
32 #include "cx231xx-reg.h"
34 /* #define ENABLE_DEBUG_ISOC_FRAMES */
36 static unsigned int core_debug
;
37 module_param(core_debug
, int, 0644);
38 MODULE_PARM_DESC(core_debug
, "enable debug messages [core]");
40 #define cx231xx_coredbg(fmt, arg...) do {\
42 printk(KERN_INFO "%s %s :"fmt, \
43 dev->name, __func__ , ##arg); } while (0)
45 static unsigned int reg_debug
;
46 module_param(reg_debug
, int, 0644);
47 MODULE_PARM_DESC(reg_debug
, "enable debug messages [URB reg]");
49 #define cx231xx_regdbg(fmt, arg...) do {\
51 printk(KERN_INFO "%s %s :"fmt, \
52 dev->name, __func__ , ##arg); } while (0)
54 static int alt
= CX231XX_PINOUT
;
55 module_param(alt
, int, 0644);
56 MODULE_PARM_DESC(alt
, "alternate setting to use for video endpoint");
58 #define cx231xx_isocdbg(fmt, arg...) do {\
60 printk(KERN_INFO "%s %s :"fmt, \
61 dev->name, __func__ , ##arg); } while (0)
63 /*****************************************************************
64 * Device control list functions *
65 ******************************************************************/
67 static LIST_HEAD(cx231xx_devlist
);
68 static DEFINE_MUTEX(cx231xx_devlist_mutex
);
71 * cx231xx_realease_resources()
72 * unregisters the v4l2,i2c and usb devices
73 * called when the device gets disconected or at module unload
75 void cx231xx_remove_from_devlist(struct cx231xx
*dev
)
77 mutex_lock(&cx231xx_devlist_mutex
);
78 list_del(&dev
->devlist
);
79 mutex_unlock(&cx231xx_devlist_mutex
);
82 void cx231xx_add_into_devlist(struct cx231xx
*dev
)
84 mutex_lock(&cx231xx_devlist_mutex
);
85 list_add_tail(&dev
->devlist
, &cx231xx_devlist
);
86 mutex_unlock(&cx231xx_devlist_mutex
);
89 static LIST_HEAD(cx231xx_extension_devlist
);
90 static DEFINE_MUTEX(cx231xx_extension_devlist_lock
);
92 int cx231xx_register_extension(struct cx231xx_ops
*ops
)
94 struct cx231xx
*dev
= NULL
;
96 mutex_lock(&cx231xx_devlist_mutex
);
97 mutex_lock(&cx231xx_extension_devlist_lock
);
98 list_add_tail(&ops
->next
, &cx231xx_extension_devlist
);
99 list_for_each_entry(dev
, &cx231xx_devlist
, devlist
) {
103 printk(KERN_INFO DRIVER_NAME
": %s initialized\n", ops
->name
);
104 mutex_unlock(&cx231xx_extension_devlist_lock
);
105 mutex_unlock(&cx231xx_devlist_mutex
);
108 EXPORT_SYMBOL(cx231xx_register_extension
);
110 void cx231xx_unregister_extension(struct cx231xx_ops
*ops
)
112 struct cx231xx
*dev
= NULL
;
114 mutex_lock(&cx231xx_devlist_mutex
);
115 list_for_each_entry(dev
, &cx231xx_devlist
, devlist
) {
120 mutex_lock(&cx231xx_extension_devlist_lock
);
121 printk(KERN_INFO DRIVER_NAME
": %s removed\n", ops
->name
);
122 list_del(&ops
->next
);
123 mutex_unlock(&cx231xx_extension_devlist_lock
);
124 mutex_unlock(&cx231xx_devlist_mutex
);
126 EXPORT_SYMBOL(cx231xx_unregister_extension
);
128 void cx231xx_init_extension(struct cx231xx
*dev
)
130 struct cx231xx_ops
*ops
= NULL
;
132 mutex_lock(&cx231xx_extension_devlist_lock
);
133 if (!list_empty(&cx231xx_extension_devlist
)) {
134 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
139 mutex_unlock(&cx231xx_extension_devlist_lock
);
142 void cx231xx_close_extension(struct cx231xx
*dev
)
144 struct cx231xx_ops
*ops
= NULL
;
146 mutex_lock(&cx231xx_extension_devlist_lock
);
147 if (!list_empty(&cx231xx_extension_devlist
)) {
148 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
153 mutex_unlock(&cx231xx_extension_devlist_lock
);
156 /****************************************************************
157 * U S B related functions *
158 *****************************************************************/
159 int cx231xx_send_usb_command(struct cx231xx_i2c
*i2c_bus
,
160 struct cx231xx_i2c_xfer_data
*req_data
)
163 struct cx231xx
*dev
= i2c_bus
->dev
;
164 struct VENDOR_REQUEST_IN ven_req
;
171 /* Get the I2C period, nostop and reserve parameters */
172 _i2c_period
= i2c_bus
->i2c_period
;
173 _i2c_nostop
= i2c_bus
->i2c_nostop
;
174 _i2c_reserve
= i2c_bus
->i2c_reserve
;
176 saddr_len
= req_data
->saddr_len
;
179 if (saddr_len
== 1) /* need check saddr_len == 0 */
182 dev_addr
<< 9 | _i2c_period
<< 4 | saddr_len
<< 2 |
183 _i2c_nostop
<< 1 | I2C_SYNC
| _i2c_reserve
<< 6;
187 dev_addr
<< 9 | _i2c_period
<< 4 | saddr_len
<< 2 |
188 _i2c_nostop
<< 1 | I2C_SYNC
| _i2c_reserve
<< 6;
190 /* set channel number */
191 if (req_data
->direction
& I2C_M_RD
) {
192 /* channel number, for read,spec required channel_num +4 */
193 ven_req
.bRequest
= i2c_bus
->nr
+ 4;
195 ven_req
.bRequest
= i2c_bus
->nr
; /* channel number, */
197 /* set index value */
200 ven_req
.wIndex
= 0; /* need check */
203 ven_req
.wIndex
= (req_data
->saddr_dat
& 0xff);
206 ven_req
.wIndex
= req_data
->saddr_dat
;
210 /* set wLength value */
211 ven_req
.wLength
= req_data
->buf_size
;
213 /* set bData value */
216 /* set the direction */
217 if (req_data
->direction
) {
218 ven_req
.direction
= USB_DIR_IN
;
219 memset(req_data
->p_buffer
, 0x00, ven_req
.wLength
);
221 ven_req
.direction
= USB_DIR_OUT
;
223 /* set the buffer for read / write */
224 ven_req
.pBuff
= req_data
->p_buffer
;
227 /* call common vendor command request */
228 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
231 ("UsbInterface::sendCommand, failed with status -%d\n",
237 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command
);
240 * cx231xx_read_ctrl_reg()
241 * reads data from the usb device specifying bRequest and wValue
243 int cx231xx_read_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
,
248 int pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
250 if (dev
->state
& DEV_DISCONNECTED
)
253 if (len
> URB_MAX_CTRL_SIZE
)
258 val
= ENABLE_ONE_BYTE
;
261 val
= ENABLE_TWE_BYTE
;
264 val
= ENABLE_THREE_BYTE
;
267 val
= ENABLE_FOUR_BYTE
;
270 val
= 0xFF; /* invalid option */
277 cx231xx_isocdbg("(pipe 0x%08x): "
278 "IN: %02x %02x %02x %02x %02x %02x %02x %02x ",
280 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
282 reg
& 0xff, reg
>> 8, len
& 0xff, len
>> 8);
285 mutex_lock(&dev
->ctrl_urb_lock
);
286 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
287 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
288 val
, reg
, dev
->urb_buf
, len
, HZ
);
290 cx231xx_isocdbg(" failed!\n");
291 /* mutex_unlock(&dev->ctrl_urb_lock); */
296 memcpy(buf
, dev
->urb_buf
, len
);
298 mutex_unlock(&dev
->ctrl_urb_lock
);
303 cx231xx_isocdbg("<<<");
304 for (byte
= 0; byte
< len
; byte
++)
305 cx231xx_isocdbg(" %02x", (unsigned char)buf
[byte
]);
306 cx231xx_isocdbg("\n");
312 int cx231xx_send_vendor_cmd(struct cx231xx
*dev
,
313 struct VENDOR_REQUEST_IN
*ven_req
)
318 if (dev
->state
& DEV_DISCONNECTED
)
321 if ((ven_req
->wLength
> URB_MAX_CTRL_SIZE
))
324 if (ven_req
->direction
)
325 pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
327 pipe
= usb_sndctrlpipe(dev
->udev
, 0);
332 cx231xx_isocdbg("(pipe 0x%08x): "
333 "OUT: %02x %02x %02x %04x %04x %04x >>>",
336 direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
337 ven_req
->bRequest
, 0, ven_req
->wValue
,
338 ven_req
->wIndex
, ven_req
->wLength
);
340 for (byte
= 0; byte
< ven_req
->wLength
; byte
++)
341 cx231xx_isocdbg(" %02x",
342 (unsigned char)ven_req
->pBuff
[byte
]);
343 cx231xx_isocdbg("\n");
346 mutex_lock(&dev
->ctrl_urb_lock
);
347 ret
= usb_control_msg(dev
->udev
, pipe
, ven_req
->bRequest
,
349 direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
350 ven_req
->wValue
, ven_req
->wIndex
, ven_req
->pBuff
,
351 ven_req
->wLength
, HZ
);
352 mutex_unlock(&dev
->ctrl_urb_lock
);
358 * cx231xx_write_ctrl_reg()
359 * sends data to the usb device, specifying bRequest
361 int cx231xx_write_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
, char *buf
,
366 int pipe
= usb_sndctrlpipe(dev
->udev
, 0);
368 if (dev
->state
& DEV_DISCONNECTED
)
371 if ((len
< 1) || (len
> URB_MAX_CTRL_SIZE
))
376 val
= ENABLE_ONE_BYTE
;
379 val
= ENABLE_TWE_BYTE
;
382 val
= ENABLE_THREE_BYTE
;
385 val
= ENABLE_FOUR_BYTE
;
388 val
= 0xFF; /* invalid option */
397 cx231xx_isocdbg("(pipe 0x%08x): "
398 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
400 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
401 req
, 0, val
, reg
& 0xff,
402 reg
>> 8, len
& 0xff, len
>> 8);
404 for (byte
= 0; byte
< len
; byte
++)
405 cx231xx_isocdbg(" %02x", (unsigned char)buf
[byte
]);
406 cx231xx_isocdbg("\n");
409 mutex_lock(&dev
->ctrl_urb_lock
);
410 memcpy(dev
->urb_buf
, buf
, len
);
411 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
412 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
413 val
, reg
, dev
->urb_buf
, len
, HZ
);
414 mutex_unlock(&dev
->ctrl_urb_lock
);
419 /****************************************************************
420 * USB Alternate Setting functions *
421 *****************************************************************/
423 int cx231xx_set_video_alternate(struct cx231xx
*dev
)
425 int errCode
, prev_alt
= dev
->video_mode
.alt
;
426 unsigned int min_pkt_size
= dev
->width
* 2 + 4;
427 u32 usb_interface_index
= 0;
429 /* When image size is bigger than a certain value,
430 the frame size should be increased, otherwise, only
431 green screen will be received.
433 if (dev
->width
* 2 * dev
->height
> 720 * 240 * 2)
436 if (dev
->width
> 360) {
437 /* resolutions: 720,704,640 */
438 dev
->video_mode
.alt
= 3;
439 } else if (dev
->width
> 180) {
440 /* resolutions: 360,352,320,240 */
441 dev
->video_mode
.alt
= 2;
442 } else if (dev
->width
> 0) {
443 /* resolutions: 180,176,160,128,88 */
444 dev
->video_mode
.alt
= 1;
446 /* Change to alt0 BULK to release USB bandwidth */
447 dev
->video_mode
.alt
= 0;
450 /* Get the correct video interface Index */
451 usb_interface_index
=
452 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
455 if (dev
->video_mode
.alt
!= prev_alt
) {
456 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
457 min_pkt_size
, dev
->video_mode
.alt
);
458 dev
->video_mode
.max_pkt_size
=
459 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.alt
];
460 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
462 dev
->video_mode
.max_pkt_size
);
464 (" setting alt %d with wMaxPktSize=%u , Interface = %d\n",
465 dev
->video_mode
.alt
, dev
->video_mode
.max_pkt_size
,
466 usb_interface_index
);
468 usb_set_interface(dev
->udev
, usb_interface_index
,
469 dev
->video_mode
.alt
);
472 ("cannot change alt number to %d (error=%i)\n",
473 dev
->video_mode
.alt
, errCode
);
480 int cx231xx_set_alt_setting(struct cx231xx
*dev
, u8 index
, u8 alt
)
483 u32 usb_interface_index
= 0;
484 u32 max_pkt_size
= 0;
488 usb_interface_index
=
489 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
491 dev
->video_mode
.alt
= alt
;
492 if (dev
->ts1_mode
.alt_max_pkt_size
!= NULL
)
493 max_pkt_size
= dev
->ts1_mode
.max_pkt_size
=
494 dev
->ts1_mode
.alt_max_pkt_size
[dev
->ts1_mode
.alt
];
497 usb_interface_index
=
498 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
502 usb_interface_index
=
503 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
506 if (dev
->adev
.alt_max_pkt_size
!= NULL
)
507 max_pkt_size
= dev
->adev
.max_pkt_size
=
508 dev
->adev
.alt_max_pkt_size
[dev
->adev
.alt
];
511 usb_interface_index
=
512 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
514 dev
->video_mode
.alt
= alt
;
515 if (dev
->video_mode
.alt_max_pkt_size
!= NULL
)
516 max_pkt_size
= dev
->video_mode
.max_pkt_size
=
517 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.
521 usb_interface_index
=
522 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
524 dev
->vbi_mode
.alt
= alt
;
525 if (dev
->vbi_mode
.alt_max_pkt_size
!= NULL
)
526 max_pkt_size
= dev
->vbi_mode
.max_pkt_size
=
527 dev
->vbi_mode
.alt_max_pkt_size
[dev
->vbi_mode
.alt
];
530 usb_interface_index
=
531 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
533 dev
->sliced_cc_mode
.alt
= alt
;
534 if (dev
->sliced_cc_mode
.alt_max_pkt_size
!= NULL
)
535 max_pkt_size
= dev
->sliced_cc_mode
.max_pkt_size
=
536 dev
->sliced_cc_mode
.alt_max_pkt_size
[dev
->
544 if (alt
> 0 && max_pkt_size
== 0) {
546 ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
547 usb_interface_index
, alt
);
552 (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n",
553 alt
, max_pkt_size
, usb_interface_index
);
555 if (usb_interface_index
> 0) {
556 status
= usb_set_interface(dev
->udev
, usb_interface_index
, alt
);
559 ("can't change interface %d alt no. to %d (err=%i)\n",
560 usb_interface_index
, alt
, status
);
567 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting
);
569 int cx231xx_gpio_set(struct cx231xx
*dev
, struct cx231xx_reg_seq
*gpio
)
576 /* Send GPIO reset sequences specified at board entry */
577 while (gpio
->sleep
>= 0) {
578 rc
= cx231xx_set_gpio_value(dev
, gpio
->bit
, gpio
->val
);
590 int cx231xx_set_mode(struct cx231xx
*dev
, enum cx231xx_mode set_mode
)
592 if (dev
->mode
== set_mode
)
595 if (set_mode
== CX231XX_SUSPEND
) {
596 /* Set the chip in power saving mode */
597 dev
->mode
= set_mode
;
600 /* Resource is locked */
601 if (dev
->mode
!= CX231XX_SUSPEND
)
604 dev
->mode
= set_mode
;
606 if (dev
->mode
== CX231XX_DIGITAL_MODE
)
607 ;/* Set Digital power mode */
609 ;/* Set Analog Power mode */
613 EXPORT_SYMBOL_GPL(cx231xx_set_mode
);
615 /*****************************************************************
616 * URB Streaming functions *
617 ******************************************************************/
620 * IRQ callback, called by URB callback
622 static void cx231xx_irq_callback(struct urb
*urb
)
624 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
625 struct cx231xx_video_mode
*vmode
=
626 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
627 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, video_mode
);
630 switch (urb
->status
) {
631 case 0: /* success */
632 case -ETIMEDOUT
: /* NAK */
634 case -ECONNRESET
: /* kill */
639 cx231xx_isocdbg("urb completition error %d.\n", urb
->status
);
643 /* Copy data from URB */
644 spin_lock(&dev
->video_mode
.slock
);
645 rc
= dev
->video_mode
.isoc_ctl
.isoc_copy(dev
, urb
);
646 spin_unlock(&dev
->video_mode
.slock
);
648 /* Reset urb buffers */
649 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
650 urb
->iso_frame_desc
[i
].status
= 0;
651 urb
->iso_frame_desc
[i
].actual_length
= 0;
655 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
657 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
663 * Stop and Deallocate URBs
665 void cx231xx_uninit_isoc(struct cx231xx
*dev
)
670 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
672 dev
->video_mode
.isoc_ctl
.nfields
= -1;
673 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
674 urb
= dev
->video_mode
.isoc_ctl
.urb
[i
];
676 if (!irqs_disabled())
681 if (dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
682 usb_buffer_free(dev
->udev
,
683 urb
->transfer_buffer_length
,
684 dev
->video_mode
.isoc_ctl
.
689 dev
->video_mode
.isoc_ctl
.urb
[i
] = NULL
;
691 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] = NULL
;
694 kfree(dev
->video_mode
.isoc_ctl
.urb
);
695 kfree(dev
->video_mode
.isoc_ctl
.transfer_buffer
);
697 dev
->video_mode
.isoc_ctl
.urb
= NULL
;
698 dev
->video_mode
.isoc_ctl
.transfer_buffer
= NULL
;
699 dev
->video_mode
.isoc_ctl
.num_bufs
= 0;
701 cx231xx_capture_start(dev
, 0, Raw_Video
);
703 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc
);
706 * Allocate URBs and start IRQ
708 int cx231xx_init_isoc(struct cx231xx
*dev
, int max_packets
,
709 int num_bufs
, int max_pkt_size
,
710 int (*isoc_copy
) (struct cx231xx
*dev
, struct urb
*urb
))
712 struct cx231xx_dmaqueue
*dma_q
= &dev
->video_mode
.vidq
;
719 cx231xx_isocdbg("cx231xx: called cx231xx_prepare_isoc\n");
721 dev
->video_input
= dev
->video_input
> 2 ? 2 : dev
->video_input
;
723 cx231xx_info("Setting Video mux to %d\n", dev
->video_input
);
724 video_mux(dev
, dev
->video_input
);
726 /* De-allocates all pending stuff */
727 cx231xx_uninit_isoc(dev
);
729 dev
->video_mode
.isoc_ctl
.isoc_copy
= isoc_copy
;
730 dev
->video_mode
.isoc_ctl
.num_bufs
= num_bufs
;
732 dma_q
->is_partial_line
= 0;
734 dma_q
->current_field
= -1;
735 dma_q
->field1_done
= 0;
736 dma_q
->lines_per_field
= dev
->height
/ 2;
737 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
738 dma_q
->lines_completed
= 0;
739 for (i
= 0; i
< 8; i
++)
740 dma_q
->partial_buf
[i
] = 0;
742 dev
->video_mode
.isoc_ctl
.urb
=
743 kzalloc(sizeof(void *) * num_bufs
, GFP_KERNEL
);
744 if (!dev
->video_mode
.isoc_ctl
.urb
) {
745 cx231xx_errdev("cannot alloc memory for usb buffers\n");
749 dev
->video_mode
.isoc_ctl
.transfer_buffer
=
750 kzalloc(sizeof(void *) * num_bufs
, GFP_KERNEL
);
751 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
) {
752 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
753 kfree(dev
->video_mode
.isoc_ctl
.urb
);
757 dev
->video_mode
.isoc_ctl
.max_pkt_size
= max_pkt_size
;
758 dev
->video_mode
.isoc_ctl
.buf
= NULL
;
760 sb_size
= max_packets
* dev
->video_mode
.isoc_ctl
.max_pkt_size
;
762 /* allocate urbs and transfer buffers */
763 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
764 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
766 cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i
);
767 cx231xx_uninit_isoc(dev
);
770 dev
->video_mode
.isoc_ctl
.urb
[i
] = urb
;
772 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] =
773 usb_buffer_alloc(dev
->udev
, sb_size
, GFP_KERNEL
,
775 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
776 cx231xx_err("unable to allocate %i bytes for transfer"
779 in_interrupt() ? " while in int" : "");
780 cx231xx_uninit_isoc(dev
);
783 memset(dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
786 usb_rcvisocpipe(dev
->udev
, dev
->video_mode
.end_point_addr
);
788 usb_fill_int_urb(urb
, dev
->udev
, pipe
,
789 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
],
790 sb_size
, cx231xx_irq_callback
, dma_q
, 1);
792 urb
->number_of_packets
= max_packets
;
793 urb
->transfer_flags
= URB_ISO_ASAP
;
796 for (j
= 0; j
< max_packets
; j
++) {
797 urb
->iso_frame_desc
[j
].offset
= k
;
798 urb
->iso_frame_desc
[j
].length
=
799 dev
->video_mode
.isoc_ctl
.max_pkt_size
;
800 k
+= dev
->video_mode
.isoc_ctl
.max_pkt_size
;
804 init_waitqueue_head(&dma_q
->wq
);
806 /* submit urbs and enables IRQ */
807 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
808 rc
= usb_submit_urb(dev
->video_mode
.isoc_ctl
.urb
[i
],
811 cx231xx_err("submit of urb %i failed (error=%i)\n", i
,
813 cx231xx_uninit_isoc(dev
);
818 cx231xx_capture_start(dev
, 1, Raw_Video
);
822 EXPORT_SYMBOL_GPL(cx231xx_init_isoc
);
824 /*****************************************************************
825 * Device Init/UnInit functions *
826 ******************************************************************/
827 int cx231xx_dev_init(struct cx231xx
*dev
)
831 /* Initialize I2C bus */
833 /* External Master 1 Bus */
834 dev
->i2c_bus
[0].nr
= 0;
835 dev
->i2c_bus
[0].dev
= dev
;
836 dev
->i2c_bus
[0].i2c_period
= I2C_SPEED_1M
; /* 1MHz */
837 dev
->i2c_bus
[0].i2c_nostop
= 0;
838 dev
->i2c_bus
[0].i2c_reserve
= 0;
840 /* External Master 2 Bus */
841 dev
->i2c_bus
[1].nr
= 1;
842 dev
->i2c_bus
[1].dev
= dev
;
843 dev
->i2c_bus
[1].i2c_period
= I2C_SPEED_1M
; /* 1MHz */
844 dev
->i2c_bus
[1].i2c_nostop
= 0;
845 dev
->i2c_bus
[1].i2c_reserve
= 0;
847 /* Internal Master 3 Bus */
848 dev
->i2c_bus
[2].nr
= 2;
849 dev
->i2c_bus
[2].dev
= dev
;
850 dev
->i2c_bus
[2].i2c_period
= I2C_SPEED_400K
; /* 400kHz */
851 dev
->i2c_bus
[2].i2c_nostop
= 0;
852 dev
->i2c_bus
[2].i2c_reserve
= 0;
854 /* register I2C buses */
855 cx231xx_i2c_register(&dev
->i2c_bus
[0]);
856 cx231xx_i2c_register(&dev
->i2c_bus
[1]);
857 cx231xx_i2c_register(&dev
->i2c_bus
[2]);
860 /* Note : with out calling set power mode function,
861 afe can not be set up correctly */
862 errCode
= cx231xx_set_power_mode(dev
, POLARIS_AVMODE_ANALOGT_TV
);
865 ("%s: Failed to set Power - errCode [%d]!\n",
870 /* initialize Colibri block */
871 errCode
= cx231xx_afe_init_super_block(dev
, 0x23c);
874 ("%s: cx231xx_afe init super block - errCode [%d]!\n",
878 errCode
= cx231xx_afe_init_channels(dev
);
881 ("%s: cx231xx_afe init channels - errCode [%d]!\n",
886 /* Set DIF in By pass mode */
887 errCode
= cx231xx_dif_set_standard(dev
, DIF_USE_BASEBAND
);
890 ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
895 /* I2S block related functions */
896 errCode
= cx231xx_i2s_blk_initialize(dev
);
899 ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
904 /* init control pins */
905 errCode
= cx231xx_init_ctrl_pin_status(dev
);
907 cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
912 /* set AGC mode to Analog */
913 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 1);
916 ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
921 /* set all alternate settings to zero initially */
922 cx231xx_set_alt_setting(dev
, INDEX_VIDEO
, 0);
923 cx231xx_set_alt_setting(dev
, INDEX_VANC
, 0);
924 cx231xx_set_alt_setting(dev
, INDEX_HANC
, 0);
925 if (dev
->board
.has_dvb
)
926 cx231xx_set_alt_setting(dev
, INDEX_TS1
, 0);
928 /* set the I2C master port to 3 on channel 1 */
929 errCode
= cx231xx_enable_i2c_for_tuner(dev
, I2C_3
);
933 EXPORT_SYMBOL_GPL(cx231xx_dev_init
);
935 void cx231xx_dev_uninit(struct cx231xx
*dev
)
937 /* Un Initialize I2C bus */
938 cx231xx_i2c_unregister(&dev
->i2c_bus
[2]);
939 cx231xx_i2c_unregister(&dev
->i2c_bus
[1]);
940 cx231xx_i2c_unregister(&dev
->i2c_bus
[0]);
942 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit
);
944 /*****************************************************************
945 * G P I O related functions *
946 ******************************************************************/
947 int cx231xx_send_gpio_cmd(struct cx231xx
*dev
, u32 gpio_bit
, u8
* gpio_val
,
948 u8 len
, u8 request
, u8 direction
)
951 struct VENDOR_REQUEST_IN ven_req
;
954 ven_req
.wValue
= (u16
) (gpio_bit
>> 16 & 0xffff);
959 ven_req
.bRequest
= VRT_GET_GPIO
; /* 0x8 gpio */
961 ven_req
.bRequest
= VRT_SET_GPIO
; /* 0x9 gpio */
964 ven_req
.bRequest
= VRT_GET_GPIE
; /* 0xa gpie */
966 ven_req
.bRequest
= VRT_SET_GPIE
; /* 0xb gpie */
969 /* set index value */
970 ven_req
.wIndex
= (u16
) (gpio_bit
& 0xffff);
972 /* set wLength value */
973 ven_req
.wLength
= len
;
975 /* set bData value */
978 /* set the buffer for read / write */
979 ven_req
.pBuff
= gpio_val
;
981 /* set the direction */
983 ven_req
.direction
= USB_DIR_IN
;
984 memset(ven_req
.pBuff
, 0x00, ven_req
.wLength
);
986 ven_req
.direction
= USB_DIR_OUT
;
989 /* call common vendor command request */
990 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
993 ("UsbInterface::sendCommand, failed with status -%d\n",
999 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd
);
1001 /*****************************************************************
1002 * C O N T R O L - Register R E A D / W R I T E functions *
1003 *****************************************************************/
1004 int cx231xx_mode_register(struct cx231xx
*dev
, u16 address
, u32 mode
)
1006 u8 value
[4] = { 0x0, 0x0, 0x0, 0x0 };
1011 cx231xx_read_ctrl_reg(dev
, VRT_GET_REGISTER
, address
, value
, 4);
1015 tmp
= *((u32
*) value
);
1018 value
[0] = (u8
) tmp
;
1019 value
[1] = (u8
) (tmp
>> 8);
1020 value
[2] = (u8
) (tmp
>> 16);
1021 value
[3] = (u8
) (tmp
>> 24);
1024 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
, address
, value
, 4);
1029 /*****************************************************************
1030 * I 2 C Internal C O N T R O L functions *
1031 *****************************************************************/
1032 int cx231xx_read_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1033 u8 saddr_len
, u32
*data
, u8 data_len
)
1036 struct cx231xx_i2c_xfer_data req_data
;
1037 u8 value
[4] = { 0, 0, 0, 0 };
1041 else if (saddr_len
== 0)
1044 /* prepare xfer_data struct */
1045 req_data
.dev_addr
= dev_addr
>> 1;
1046 req_data
.direction
= I2C_M_RD
;
1047 req_data
.saddr_len
= saddr_len
;
1048 req_data
.saddr_dat
= saddr
;
1049 req_data
.buf_size
= data_len
;
1050 req_data
.p_buffer
= (u8
*) value
;
1052 /* usb send command */
1053 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1056 /* Copy the data read back to main buffer */
1061 value
[0] | value
[1] << 8 | value
[2] << 16 | value
[3]
1068 int cx231xx_write_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1069 u8 saddr_len
, u32 data
, u8 data_len
)
1072 u8 value
[4] = { 0, 0, 0, 0 };
1073 struct cx231xx_i2c_xfer_data req_data
;
1075 value
[0] = (u8
) data
;
1076 value
[1] = (u8
) (data
>> 8);
1077 value
[2] = (u8
) (data
>> 16);
1078 value
[3] = (u8
) (data
>> 24);
1082 else if (saddr_len
== 0)
1085 /* prepare xfer_data struct */
1086 req_data
.dev_addr
= dev_addr
>> 1;
1087 req_data
.direction
= 0;
1088 req_data
.saddr_len
= saddr_len
;
1089 req_data
.saddr_dat
= saddr
;
1090 req_data
.buf_size
= data_len
;
1091 req_data
.p_buffer
= value
;
1093 /* usb send command */
1094 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1099 int cx231xx_reg_mask_write(struct cx231xx
*dev
, u8 dev_addr
, u8 size
,
1100 u16 register_address
, u8 bit_start
, u8 bit_end
,
1108 if (bit_start
> (size
- 1) || bit_end
> (size
- 1))
1113 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1117 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1124 mask
= 1 << bit_end
;
1125 for (i
= bit_end
; i
> bit_start
&& i
> 0; i
--)
1126 mask
= mask
+ (1 << (i
- 1));
1128 value
<<= bit_start
;
1135 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1141 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1148 int cx231xx_read_modify_write_i2c_dword(struct cx231xx
*dev
, u8 dev_addr
,
1149 u16 saddr
, u32 mask
, u32 value
)
1154 status
= cx231xx_read_i2c_data(dev
, dev_addr
, saddr
, 2, &temp
, 4);
1162 status
= cx231xx_write_i2c_data(dev
, dev_addr
, saddr
, 2, temp
, 4);
1167 u32
cx231xx_set_field(u32 field_mask
, u32 data
)
1171 for (temp
= field_mask
; (temp
& 1) == 0; temp
>>= 1)