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
)
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
)
117 mutex_lock(&cx231xx_extension_devlist_lock
);
118 printk(KERN_INFO DRIVER_NAME
": %s removed\n", ops
->name
);
119 list_del(&ops
->next
);
120 mutex_unlock(&cx231xx_extension_devlist_lock
);
121 mutex_unlock(&cx231xx_devlist_mutex
);
123 EXPORT_SYMBOL(cx231xx_unregister_extension
);
125 void cx231xx_init_extension(struct cx231xx
*dev
)
127 struct cx231xx_ops
*ops
= NULL
;
129 mutex_lock(&cx231xx_extension_devlist_lock
);
130 if (!list_empty(&cx231xx_extension_devlist
)) {
131 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
136 mutex_unlock(&cx231xx_extension_devlist_lock
);
139 void cx231xx_close_extension(struct cx231xx
*dev
)
141 struct cx231xx_ops
*ops
= NULL
;
143 mutex_lock(&cx231xx_extension_devlist_lock
);
144 if (!list_empty(&cx231xx_extension_devlist
)) {
145 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
150 mutex_unlock(&cx231xx_extension_devlist_lock
);
153 /****************************************************************
154 * U S B related functions *
155 *****************************************************************/
156 int cx231xx_send_usb_command(struct cx231xx_i2c
*i2c_bus
,
157 struct cx231xx_i2c_xfer_data
*req_data
)
160 struct cx231xx
*dev
= i2c_bus
->dev
;
161 struct VENDOR_REQUEST_IN ven_req
;
168 /* Get the I2C period, nostop and reserve parameters */
169 _i2c_period
= i2c_bus
->i2c_period
;
170 _i2c_nostop
= i2c_bus
->i2c_nostop
;
171 _i2c_reserve
= i2c_bus
->i2c_reserve
;
173 saddr_len
= req_data
->saddr_len
;
176 if (saddr_len
== 1) /* need check saddr_len == 0 */
179 dev_addr
<< 9 | _i2c_period
<< 4 | saddr_len
<< 2 |
180 _i2c_nostop
<< 1 | I2C_SYNC
| _i2c_reserve
<< 6;
184 dev_addr
<< 9 | _i2c_period
<< 4 | saddr_len
<< 2 |
185 _i2c_nostop
<< 1 | I2C_SYNC
| _i2c_reserve
<< 6;
187 /* set channel number */
188 if (req_data
->direction
& I2C_M_RD
) {
189 /* channel number, for read,spec required channel_num +4 */
190 ven_req
.bRequest
= i2c_bus
->nr
+ 4;
192 ven_req
.bRequest
= i2c_bus
->nr
; /* channel number, */
194 /* set index value */
197 ven_req
.wIndex
= 0; /* need check */
200 ven_req
.wIndex
= (req_data
->saddr_dat
& 0xff);
203 ven_req
.wIndex
= req_data
->saddr_dat
;
207 /* set wLength value */
208 ven_req
.wLength
= req_data
->buf_size
;
210 /* set bData value */
213 /* set the direction */
214 if (req_data
->direction
) {
215 ven_req
.direction
= USB_DIR_IN
;
216 memset(req_data
->p_buffer
, 0x00, ven_req
.wLength
);
218 ven_req
.direction
= USB_DIR_OUT
;
220 /* set the buffer for read / write */
221 ven_req
.pBuff
= req_data
->p_buffer
;
224 /* call common vendor command request */
225 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
228 ("UsbInterface::sendCommand, failed with status -%d\n",
234 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command
);
237 * cx231xx_read_ctrl_reg()
238 * reads data from the usb device specifying bRequest and wValue
240 int cx231xx_read_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
,
245 int pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
247 if (dev
->state
& DEV_DISCONNECTED
)
250 if (len
> URB_MAX_CTRL_SIZE
)
255 val
= ENABLE_ONE_BYTE
;
258 val
= ENABLE_TWE_BYTE
;
261 val
= ENABLE_THREE_BYTE
;
264 val
= ENABLE_FOUR_BYTE
;
267 val
= 0xFF; /* invalid option */
274 cx231xx_isocdbg("(pipe 0x%08x): "
275 "IN: %02x %02x %02x %02x %02x %02x %02x %02x ",
277 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
279 reg
& 0xff, reg
>> 8, len
& 0xff, len
>> 8);
282 mutex_lock(&dev
->ctrl_urb_lock
);
283 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
284 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
285 val
, reg
, dev
->urb_buf
, len
, HZ
);
287 cx231xx_isocdbg(" failed!\n");
288 /* mutex_unlock(&dev->ctrl_urb_lock); */
293 memcpy(buf
, dev
->urb_buf
, len
);
295 mutex_unlock(&dev
->ctrl_urb_lock
);
300 cx231xx_isocdbg("<<<");
301 for (byte
= 0; byte
< len
; byte
++)
302 cx231xx_isocdbg(" %02x", (unsigned char)buf
[byte
]);
303 cx231xx_isocdbg("\n");
309 int cx231xx_send_vendor_cmd(struct cx231xx
*dev
,
310 struct VENDOR_REQUEST_IN
*ven_req
)
315 if (dev
->state
& DEV_DISCONNECTED
)
318 if ((ven_req
->wLength
> URB_MAX_CTRL_SIZE
))
321 if (ven_req
->direction
)
322 pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
324 pipe
= usb_sndctrlpipe(dev
->udev
, 0);
329 cx231xx_isocdbg("(pipe 0x%08x): "
330 "OUT: %02x %02x %02x %04x %04x %04x >>>",
333 direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
334 ven_req
->bRequest
, 0, ven_req
->wValue
,
335 ven_req
->wIndex
, ven_req
->wLength
);
337 for (byte
= 0; byte
< ven_req
->wLength
; byte
++)
338 cx231xx_isocdbg(" %02x",
339 (unsigned char)ven_req
->pBuff
[byte
]);
340 cx231xx_isocdbg("\n");
343 mutex_lock(&dev
->ctrl_urb_lock
);
344 ret
= usb_control_msg(dev
->udev
, pipe
, ven_req
->bRequest
,
346 direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
347 ven_req
->wValue
, ven_req
->wIndex
, ven_req
->pBuff
,
348 ven_req
->wLength
, HZ
);
349 mutex_unlock(&dev
->ctrl_urb_lock
);
355 * cx231xx_write_ctrl_reg()
356 * sends data to the usb device, specifying bRequest
358 int cx231xx_write_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
, char *buf
,
363 int pipe
= usb_sndctrlpipe(dev
->udev
, 0);
365 if (dev
->state
& DEV_DISCONNECTED
)
368 if ((len
< 1) || (len
> URB_MAX_CTRL_SIZE
))
373 val
= ENABLE_ONE_BYTE
;
376 val
= ENABLE_TWE_BYTE
;
379 val
= ENABLE_THREE_BYTE
;
382 val
= ENABLE_FOUR_BYTE
;
385 val
= 0xFF; /* invalid option */
394 cx231xx_isocdbg("(pipe 0x%08x): "
395 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
397 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
398 req
, 0, val
, reg
& 0xff,
399 reg
>> 8, len
& 0xff, len
>> 8);
401 for (byte
= 0; byte
< len
; byte
++)
402 cx231xx_isocdbg(" %02x", (unsigned char)buf
[byte
]);
403 cx231xx_isocdbg("\n");
406 mutex_lock(&dev
->ctrl_urb_lock
);
407 memcpy(dev
->urb_buf
, buf
, len
);
408 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
409 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
410 val
, reg
, dev
->urb_buf
, len
, HZ
);
411 mutex_unlock(&dev
->ctrl_urb_lock
);
416 /****************************************************************
417 * USB Alternate Setting functions *
418 *****************************************************************/
420 int cx231xx_set_video_alternate(struct cx231xx
*dev
)
422 int errCode
, prev_alt
= dev
->video_mode
.alt
;
423 unsigned int min_pkt_size
= dev
->width
* 2 + 4;
424 u32 usb_interface_index
= 0;
426 /* When image size is bigger than a certain value,
427 the frame size should be increased, otherwise, only
428 green screen will be received.
430 if (dev
->width
* 2 * dev
->height
> 720 * 240 * 2)
433 if (dev
->width
> 360) {
434 /* resolutions: 720,704,640 */
435 dev
->video_mode
.alt
= 3;
436 } else if (dev
->width
> 180) {
437 /* resolutions: 360,352,320,240 */
438 dev
->video_mode
.alt
= 2;
439 } else if (dev
->width
> 0) {
440 /* resolutions: 180,176,160,128,88 */
441 dev
->video_mode
.alt
= 1;
443 /* Change to alt0 BULK to release USB bandwidth */
444 dev
->video_mode
.alt
= 0;
447 /* Get the correct video interface Index */
448 usb_interface_index
=
449 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
452 if (dev
->video_mode
.alt
!= prev_alt
) {
453 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
454 min_pkt_size
, dev
->video_mode
.alt
);
455 dev
->video_mode
.max_pkt_size
=
456 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.alt
];
457 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
459 dev
->video_mode
.max_pkt_size
);
461 (" setting alt %d with wMaxPktSize=%u , Interface = %d\n",
462 dev
->video_mode
.alt
, dev
->video_mode
.max_pkt_size
,
463 usb_interface_index
);
465 usb_set_interface(dev
->udev
, usb_interface_index
,
466 dev
->video_mode
.alt
);
469 ("cannot change alt number to %d (error=%i)\n",
470 dev
->video_mode
.alt
, errCode
);
477 int cx231xx_set_alt_setting(struct cx231xx
*dev
, u8 index
, u8 alt
)
480 u32 usb_interface_index
= 0;
481 u32 max_pkt_size
= 0;
485 usb_interface_index
=
486 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
488 dev
->video_mode
.alt
= alt
;
489 if (dev
->ts1_mode
.alt_max_pkt_size
!= NULL
)
490 max_pkt_size
= dev
->ts1_mode
.max_pkt_size
=
491 dev
->ts1_mode
.alt_max_pkt_size
[dev
->ts1_mode
.alt
];
494 usb_interface_index
=
495 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
499 usb_interface_index
=
500 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
503 if (dev
->adev
.alt_max_pkt_size
!= NULL
)
504 max_pkt_size
= dev
->adev
.max_pkt_size
=
505 dev
->adev
.alt_max_pkt_size
[dev
->adev
.alt
];
508 usb_interface_index
=
509 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
511 dev
->video_mode
.alt
= alt
;
512 if (dev
->video_mode
.alt_max_pkt_size
!= NULL
)
513 max_pkt_size
= dev
->video_mode
.max_pkt_size
=
514 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.
518 usb_interface_index
=
519 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
521 dev
->vbi_mode
.alt
= alt
;
522 if (dev
->vbi_mode
.alt_max_pkt_size
!= NULL
)
523 max_pkt_size
= dev
->vbi_mode
.max_pkt_size
=
524 dev
->vbi_mode
.alt_max_pkt_size
[dev
->vbi_mode
.alt
];
527 usb_interface_index
=
528 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
530 dev
->sliced_cc_mode
.alt
= alt
;
531 if (dev
->sliced_cc_mode
.alt_max_pkt_size
!= NULL
)
532 max_pkt_size
= dev
->sliced_cc_mode
.max_pkt_size
=
533 dev
->sliced_cc_mode
.alt_max_pkt_size
[dev
->
541 if (alt
> 0 && max_pkt_size
== 0) {
543 ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
544 usb_interface_index
, alt
);
549 (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n",
550 alt
, max_pkt_size
, usb_interface_index
);
552 if (usb_interface_index
> 0) {
553 status
= usb_set_interface(dev
->udev
, usb_interface_index
, alt
);
556 ("can't change interface %d alt no. to %d (err=%i)\n",
557 usb_interface_index
, alt
, status
);
564 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting
);
566 int cx231xx_gpio_set(struct cx231xx
*dev
, struct cx231xx_reg_seq
*gpio
)
573 /* Send GPIO reset sequences specified at board entry */
574 while (gpio
->sleep
>= 0) {
575 rc
= cx231xx_set_gpio_value(dev
, gpio
->bit
, gpio
->val
);
587 int cx231xx_set_mode(struct cx231xx
*dev
, enum cx231xx_mode set_mode
)
589 if (dev
->mode
== set_mode
)
592 if (set_mode
== CX231XX_SUSPEND
) {
593 /* Set the chip in power saving mode */
594 dev
->mode
= set_mode
;
597 /* Resource is locked */
598 if (dev
->mode
!= CX231XX_SUSPEND
)
601 dev
->mode
= set_mode
;
603 if (dev
->mode
== CX231XX_DIGITAL_MODE
)
604 ;/* Set Digital power mode */
606 ;/* Set Analog Power mode */
610 EXPORT_SYMBOL_GPL(cx231xx_set_mode
);
612 /*****************************************************************
613 * URB Streaming functions *
614 ******************************************************************/
617 * IRQ callback, called by URB callback
619 static void cx231xx_irq_callback(struct urb
*urb
)
621 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
622 struct cx231xx_video_mode
*vmode
=
623 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
624 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, video_mode
);
627 switch (urb
->status
) {
628 case 0: /* success */
629 case -ETIMEDOUT
: /* NAK */
631 case -ECONNRESET
: /* kill */
636 cx231xx_isocdbg("urb completition error %d.\n", urb
->status
);
640 /* Copy data from URB */
641 spin_lock(&dev
->video_mode
.slock
);
642 rc
= dev
->video_mode
.isoc_ctl
.isoc_copy(dev
, urb
);
643 spin_unlock(&dev
->video_mode
.slock
);
645 /* Reset urb buffers */
646 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
647 urb
->iso_frame_desc
[i
].status
= 0;
648 urb
->iso_frame_desc
[i
].actual_length
= 0;
652 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
654 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
660 * Stop and Deallocate URBs
662 void cx231xx_uninit_isoc(struct cx231xx
*dev
)
667 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
669 dev
->video_mode
.isoc_ctl
.nfields
= -1;
670 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
671 urb
= dev
->video_mode
.isoc_ctl
.urb
[i
];
673 if (!irqs_disabled())
678 if (dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
679 usb_free_coherent(dev
->udev
,
680 urb
->transfer_buffer_length
,
681 dev
->video_mode
.isoc_ctl
.
686 dev
->video_mode
.isoc_ctl
.urb
[i
] = NULL
;
688 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] = NULL
;
691 kfree(dev
->video_mode
.isoc_ctl
.urb
);
692 kfree(dev
->video_mode
.isoc_ctl
.transfer_buffer
);
694 dev
->video_mode
.isoc_ctl
.urb
= NULL
;
695 dev
->video_mode
.isoc_ctl
.transfer_buffer
= NULL
;
696 dev
->video_mode
.isoc_ctl
.num_bufs
= 0;
698 cx231xx_capture_start(dev
, 0, Raw_Video
);
700 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc
);
703 * Allocate URBs and start IRQ
705 int cx231xx_init_isoc(struct cx231xx
*dev
, int max_packets
,
706 int num_bufs
, int max_pkt_size
,
707 int (*isoc_copy
) (struct cx231xx
*dev
, struct urb
*urb
))
709 struct cx231xx_dmaqueue
*dma_q
= &dev
->video_mode
.vidq
;
716 cx231xx_isocdbg("cx231xx: called cx231xx_prepare_isoc\n");
718 dev
->video_input
= dev
->video_input
> 2 ? 2 : dev
->video_input
;
720 cx231xx_info("Setting Video mux to %d\n", dev
->video_input
);
721 video_mux(dev
, dev
->video_input
);
723 /* De-allocates all pending stuff */
724 cx231xx_uninit_isoc(dev
);
726 dev
->video_mode
.isoc_ctl
.isoc_copy
= isoc_copy
;
727 dev
->video_mode
.isoc_ctl
.num_bufs
= num_bufs
;
729 dma_q
->is_partial_line
= 0;
731 dma_q
->current_field
= -1;
732 dma_q
->field1_done
= 0;
733 dma_q
->lines_per_field
= dev
->height
/ 2;
734 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
735 dma_q
->lines_completed
= 0;
736 for (i
= 0; i
< 8; i
++)
737 dma_q
->partial_buf
[i
] = 0;
739 dev
->video_mode
.isoc_ctl
.urb
=
740 kzalloc(sizeof(void *) * num_bufs
, GFP_KERNEL
);
741 if (!dev
->video_mode
.isoc_ctl
.urb
) {
742 cx231xx_errdev("cannot alloc memory for usb buffers\n");
746 dev
->video_mode
.isoc_ctl
.transfer_buffer
=
747 kzalloc(sizeof(void *) * num_bufs
, GFP_KERNEL
);
748 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
) {
749 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
750 kfree(dev
->video_mode
.isoc_ctl
.urb
);
754 dev
->video_mode
.isoc_ctl
.max_pkt_size
= max_pkt_size
;
755 dev
->video_mode
.isoc_ctl
.buf
= NULL
;
757 sb_size
= max_packets
* dev
->video_mode
.isoc_ctl
.max_pkt_size
;
759 /* allocate urbs and transfer buffers */
760 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
761 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
763 cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i
);
764 cx231xx_uninit_isoc(dev
);
767 dev
->video_mode
.isoc_ctl
.urb
[i
] = urb
;
769 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] =
770 usb_alloc_coherent(dev
->udev
, sb_size
, GFP_KERNEL
,
772 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
773 cx231xx_err("unable to allocate %i bytes for transfer"
776 in_interrupt() ? " while in int" : "");
777 cx231xx_uninit_isoc(dev
);
780 memset(dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
783 usb_rcvisocpipe(dev
->udev
, dev
->video_mode
.end_point_addr
);
785 usb_fill_int_urb(urb
, dev
->udev
, pipe
,
786 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
],
787 sb_size
, cx231xx_irq_callback
, dma_q
, 1);
789 urb
->number_of_packets
= max_packets
;
790 urb
->transfer_flags
= URB_ISO_ASAP
;
793 for (j
= 0; j
< max_packets
; j
++) {
794 urb
->iso_frame_desc
[j
].offset
= k
;
795 urb
->iso_frame_desc
[j
].length
=
796 dev
->video_mode
.isoc_ctl
.max_pkt_size
;
797 k
+= dev
->video_mode
.isoc_ctl
.max_pkt_size
;
801 init_waitqueue_head(&dma_q
->wq
);
803 /* submit urbs and enables IRQ */
804 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
805 rc
= usb_submit_urb(dev
->video_mode
.isoc_ctl
.urb
[i
],
808 cx231xx_err("submit of urb %i failed (error=%i)\n", i
,
810 cx231xx_uninit_isoc(dev
);
815 cx231xx_capture_start(dev
, 1, Raw_Video
);
819 EXPORT_SYMBOL_GPL(cx231xx_init_isoc
);
821 /*****************************************************************
822 * Device Init/UnInit functions *
823 ******************************************************************/
824 int cx231xx_dev_init(struct cx231xx
*dev
)
828 /* Initialize I2C bus */
830 /* External Master 1 Bus */
831 dev
->i2c_bus
[0].nr
= 0;
832 dev
->i2c_bus
[0].dev
= dev
;
833 dev
->i2c_bus
[0].i2c_period
= I2C_SPEED_1M
; /* 1MHz */
834 dev
->i2c_bus
[0].i2c_nostop
= 0;
835 dev
->i2c_bus
[0].i2c_reserve
= 0;
837 /* External Master 2 Bus */
838 dev
->i2c_bus
[1].nr
= 1;
839 dev
->i2c_bus
[1].dev
= dev
;
840 dev
->i2c_bus
[1].i2c_period
= I2C_SPEED_1M
; /* 1MHz */
841 dev
->i2c_bus
[1].i2c_nostop
= 0;
842 dev
->i2c_bus
[1].i2c_reserve
= 0;
844 /* Internal Master 3 Bus */
845 dev
->i2c_bus
[2].nr
= 2;
846 dev
->i2c_bus
[2].dev
= dev
;
847 dev
->i2c_bus
[2].i2c_period
= I2C_SPEED_400K
; /* 400kHz */
848 dev
->i2c_bus
[2].i2c_nostop
= 0;
849 dev
->i2c_bus
[2].i2c_reserve
= 0;
851 /* register I2C buses */
852 cx231xx_i2c_register(&dev
->i2c_bus
[0]);
853 cx231xx_i2c_register(&dev
->i2c_bus
[1]);
854 cx231xx_i2c_register(&dev
->i2c_bus
[2]);
857 /* Note : with out calling set power mode function,
858 afe can not be set up correctly */
859 errCode
= cx231xx_set_power_mode(dev
, POLARIS_AVMODE_ANALOGT_TV
);
862 ("%s: Failed to set Power - errCode [%d]!\n",
867 /* initialize Colibri block */
868 errCode
= cx231xx_afe_init_super_block(dev
, 0x23c);
871 ("%s: cx231xx_afe init super block - errCode [%d]!\n",
875 errCode
= cx231xx_afe_init_channels(dev
);
878 ("%s: cx231xx_afe init channels - errCode [%d]!\n",
883 /* Set DIF in By pass mode */
884 errCode
= cx231xx_dif_set_standard(dev
, DIF_USE_BASEBAND
);
887 ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
892 /* I2S block related functions */
893 errCode
= cx231xx_i2s_blk_initialize(dev
);
896 ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
901 /* init control pins */
902 errCode
= cx231xx_init_ctrl_pin_status(dev
);
904 cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
909 /* set AGC mode to Analog */
910 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 1);
913 ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
918 /* set all alternate settings to zero initially */
919 cx231xx_set_alt_setting(dev
, INDEX_VIDEO
, 0);
920 cx231xx_set_alt_setting(dev
, INDEX_VANC
, 0);
921 cx231xx_set_alt_setting(dev
, INDEX_HANC
, 0);
922 if (dev
->board
.has_dvb
)
923 cx231xx_set_alt_setting(dev
, INDEX_TS1
, 0);
925 /* set the I2C master port to 3 on channel 1 */
926 errCode
= cx231xx_enable_i2c_for_tuner(dev
, I2C_3
);
930 EXPORT_SYMBOL_GPL(cx231xx_dev_init
);
932 void cx231xx_dev_uninit(struct cx231xx
*dev
)
934 /* Un Initialize I2C bus */
935 cx231xx_i2c_unregister(&dev
->i2c_bus
[2]);
936 cx231xx_i2c_unregister(&dev
->i2c_bus
[1]);
937 cx231xx_i2c_unregister(&dev
->i2c_bus
[0]);
939 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit
);
941 /*****************************************************************
942 * G P I O related functions *
943 ******************************************************************/
944 int cx231xx_send_gpio_cmd(struct cx231xx
*dev
, u32 gpio_bit
, u8
* gpio_val
,
945 u8 len
, u8 request
, u8 direction
)
948 struct VENDOR_REQUEST_IN ven_req
;
951 ven_req
.wValue
= (u16
) (gpio_bit
>> 16 & 0xffff);
956 ven_req
.bRequest
= VRT_GET_GPIO
; /* 0x8 gpio */
958 ven_req
.bRequest
= VRT_SET_GPIO
; /* 0x9 gpio */
961 ven_req
.bRequest
= VRT_GET_GPIE
; /* 0xa gpie */
963 ven_req
.bRequest
= VRT_SET_GPIE
; /* 0xb gpie */
966 /* set index value */
967 ven_req
.wIndex
= (u16
) (gpio_bit
& 0xffff);
969 /* set wLength value */
970 ven_req
.wLength
= len
;
972 /* set bData value */
975 /* set the buffer for read / write */
976 ven_req
.pBuff
= gpio_val
;
978 /* set the direction */
980 ven_req
.direction
= USB_DIR_IN
;
981 memset(ven_req
.pBuff
, 0x00, ven_req
.wLength
);
983 ven_req
.direction
= USB_DIR_OUT
;
986 /* call common vendor command request */
987 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
990 ("UsbInterface::sendCommand, failed with status -%d\n",
996 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd
);
998 /*****************************************************************
999 * C O N T R O L - Register R E A D / W R I T E functions *
1000 *****************************************************************/
1001 int cx231xx_mode_register(struct cx231xx
*dev
, u16 address
, u32 mode
)
1003 u8 value
[4] = { 0x0, 0x0, 0x0, 0x0 };
1008 cx231xx_read_ctrl_reg(dev
, VRT_GET_REGISTER
, address
, value
, 4);
1012 tmp
= *((u32
*) value
);
1015 value
[0] = (u8
) tmp
;
1016 value
[1] = (u8
) (tmp
>> 8);
1017 value
[2] = (u8
) (tmp
>> 16);
1018 value
[3] = (u8
) (tmp
>> 24);
1021 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
, address
, value
, 4);
1026 /*****************************************************************
1027 * I 2 C Internal C O N T R O L functions *
1028 *****************************************************************/
1029 int cx231xx_read_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1030 u8 saddr_len
, u32
*data
, u8 data_len
)
1033 struct cx231xx_i2c_xfer_data req_data
;
1034 u8 value
[4] = { 0, 0, 0, 0 };
1038 else if (saddr_len
== 0)
1041 /* prepare xfer_data struct */
1042 req_data
.dev_addr
= dev_addr
>> 1;
1043 req_data
.direction
= I2C_M_RD
;
1044 req_data
.saddr_len
= saddr_len
;
1045 req_data
.saddr_dat
= saddr
;
1046 req_data
.buf_size
= data_len
;
1047 req_data
.p_buffer
= (u8
*) value
;
1049 /* usb send command */
1050 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1053 /* Copy the data read back to main buffer */
1058 value
[0] | value
[1] << 8 | value
[2] << 16 | value
[3]
1065 int cx231xx_write_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1066 u8 saddr_len
, u32 data
, u8 data_len
)
1069 u8 value
[4] = { 0, 0, 0, 0 };
1070 struct cx231xx_i2c_xfer_data req_data
;
1072 value
[0] = (u8
) data
;
1073 value
[1] = (u8
) (data
>> 8);
1074 value
[2] = (u8
) (data
>> 16);
1075 value
[3] = (u8
) (data
>> 24);
1079 else if (saddr_len
== 0)
1082 /* prepare xfer_data struct */
1083 req_data
.dev_addr
= dev_addr
>> 1;
1084 req_data
.direction
= 0;
1085 req_data
.saddr_len
= saddr_len
;
1086 req_data
.saddr_dat
= saddr
;
1087 req_data
.buf_size
= data_len
;
1088 req_data
.p_buffer
= value
;
1090 /* usb send command */
1091 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1096 int cx231xx_reg_mask_write(struct cx231xx
*dev
, u8 dev_addr
, u8 size
,
1097 u16 register_address
, u8 bit_start
, u8 bit_end
,
1105 if (bit_start
> (size
- 1) || bit_end
> (size
- 1))
1110 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1114 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1121 mask
= 1 << bit_end
;
1122 for (i
= bit_end
; i
> bit_start
&& i
> 0; i
--)
1123 mask
= mask
+ (1 << (i
- 1));
1125 value
<<= bit_start
;
1132 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1138 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1145 int cx231xx_read_modify_write_i2c_dword(struct cx231xx
*dev
, u8 dev_addr
,
1146 u16 saddr
, u32 mask
, u32 value
)
1151 status
= cx231xx_read_i2c_data(dev
, dev_addr
, saddr
, 2, &temp
, 4);
1159 status
= cx231xx_write_i2c_data(dev
, dev_addr
, saddr
, 2, temp
, 4);
1164 u32
cx231xx_set_field(u32 field_mask
, u32 data
)
1168 for (temp
= field_mask
; (temp
& 1) == 0; temp
>>= 1)